src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc
changeset 50352 25db2c8f3cf8
parent 48274 51772bf1fb0c
child 50826 f5b95be8b6e2
--- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc	Wed May 30 08:10:41 2018 -0700
+++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc	Wed May 30 12:20:00 2018 -0700
@@ -39,7 +39,7 @@
 #define HB_CORETEXT_DEFAULT_FONT_SIZE 12.f
 
 static CGFloat
-coretext_font_size (float ptem)
+coretext_font_size_from_ptem (float ptem)
 {
   /* CoreText points are CSS pixels (96 per inch),
    * NOT typographic points (72 per inch).
@@ -49,6 +49,12 @@
   ptem *= 96.f / 72.f;
   return ptem <= 0.f ? HB_CORETEXT_DEFAULT_FONT_SIZE : ptem;
 }
+static float
+coretext_font_size_to_ptem (CGFloat size)
+{
+  size *= 72.f / 96.f;
+  return size <= 0.f ? 0 : size;
+}
 
 static void
 release_table_data (void *user_data)
@@ -68,7 +74,10 @@
   const char *data = reinterpret_cast<const char*> (CFDataGetBytePtr (cf_data));
   const size_t length = CFDataGetLength (cf_data);
   if (!data || !length)
+  {
+    CFRelease (cf_data);
     return nullptr;
+  }
 
   return hb_blob_create (data, length, HB_MEMORY_MODE_READONLY,
                          reinterpret_cast<void *> (const_cast<__CFData *> (cf_data)),
@@ -81,21 +90,12 @@
   CGFontRelease ((CGFontRef) data);
 }
 
-hb_face_t *
-hb_coretext_face_create (CGFontRef cg_font)
-{
-  return hb_face_create_for_tables (reference_table, CGFontRetain (cg_font), _hb_cg_font_release);
-}
 
 HB_SHAPER_DATA_ENSURE_DEFINE(coretext, face)
 HB_SHAPER_DATA_ENSURE_DEFINE_WITH_CONDITION(coretext, font,
-        fabs (CTFontGetSize((CTFontRef) data) - coretext_font_size (font->ptem)) <= .5
+        fabs (CTFontGetSize((CTFontRef) data) - coretext_font_size_from_ptem (font->ptem)) <= .5
 )
 
-/*
- * shaper face data
- */
-
 static CTFontDescriptorRef
 get_last_resort_font_desc (void)
 {
@@ -267,6 +267,12 @@
   CFRelease ((CGFontRef) data);
 }
 
+hb_face_t *
+hb_coretext_face_create (CGFontRef cg_font)
+{
+  return hb_face_create_for_tables (reference_table, CGFontRetain (cg_font), _hb_cg_font_release);
+}
+
 /*
  * Since: 0.9.10
  */
@@ -278,10 +284,6 @@
 }
 
 
-/*
- * shaper font data
- */
-
 hb_coretext_shaper_font_data_t *
 _hb_coretext_shaper_font_data_create (hb_font_t *font)
 {
@@ -289,7 +291,7 @@
   if (unlikely (!hb_coretext_shaper_face_data_ensure (face))) return nullptr;
   CGFontRef cg_font = (CGFontRef) HB_SHAPER_DATA_GET (face);
 
-  CTFontRef ct_font = create_ct_font (cg_font, coretext_font_size (font->ptem));
+  CTFontRef ct_font = create_ct_font (cg_font, coretext_font_size_from_ptem (font->ptem));
 
   if (unlikely (!ct_font))
   {
@@ -306,6 +308,37 @@
   CFRelease ((CTFontRef) data);
 }
 
+/*
+ * Since: 1.7.2
+ */
+hb_font_t *
+hb_coretext_font_create (CTFontRef ct_font)
+{
+  CGFontRef cg_font = CTFontCopyGraphicsFont (ct_font, nullptr);
+  hb_face_t *face = hb_coretext_face_create (cg_font);
+  CFRelease (cg_font);
+  hb_font_t *font = hb_font_create (face);
+  hb_face_destroy (face);
+
+  if (unlikely (hb_object_is_inert (font)))
+    return font;
+
+  hb_font_set_ptem (font, coretext_font_size_to_ptem (CTFontGetSize(ct_font)));
+
+  /* Let there be dragons here... */
+  HB_SHAPER_DATA_GET (font) = (hb_coretext_shaper_font_data_t *) CFRetain (ct_font);
+
+  return font;
+}
+
+CTFontRef
+hb_coretext_font_get_ct_font (hb_font_t *font)
+{
+  if (unlikely (!hb_coretext_shaper_font_data_ensure (font))) return nullptr;
+  return (CTFontRef) HB_SHAPER_DATA_GET (font);
+}
+
+
 
 /*
  * shaper shape_plan data
@@ -328,13 +361,6 @@
 {
 }
 
-CTFontRef
-hb_coretext_font_get_ct_font (hb_font_t *font)
-{
-  if (unlikely (!hb_coretext_shaper_font_data_ensure (font))) return nullptr;
-  return (CTFontRef)HB_SHAPER_DATA_GET (font);
-}
-
 
 /*
  * shaper
@@ -854,7 +880,10 @@
                                                             kCFStringEncodingUTF8,
                                                             kCFAllocatorNull);
         if (unlikely (!lang))
+        {
+          CFRelease (attr_string);
           FAIL ("CFStringCreateWithCStringNoCopy failed");
+        }
         CFAttributedStringSetAttribute (attr_string, CFRangeMake (0, chars_len),
                                         kCTLanguageAttributeName, lang);
         CFRelease (lang);
@@ -923,7 +952,10 @@
                                                     &kCFTypeDictionaryValueCallBacks);
       CFRelease (level_number);
       if (unlikely (!options))
+      {
+        CFRelease (attr_string);
         FAIL ("CFDictionaryCreate failed");
+      }
 
       CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedStringAndOptions (attr_string, options);
       CFRelease (options);
@@ -1000,7 +1032,7 @@
          * However, even that wouldn't work if we were passed in the CGFont to
          * construct a hb_face to begin with.
          *
-         * See: http://github.com/behdad/harfbuzz/pull/36
+         * See: http://github.com/harfbuzz/harfbuzz/pull/36
          *
          * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098
          */
@@ -1013,7 +1045,7 @@
           }
         if (!matched)
         {
-          CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, 0);
+          CGFontRef run_cg_font = CTFontCopyGraphicsFont (run_ct_font, nullptr);
           if (run_cg_font)
           {
             matched = CFEqual (run_cg_font, cg_font);
@@ -1186,7 +1218,7 @@
     }
 
     /* Mac OS 10.6 doesn't have kCTTypesetterOptionForcedEmbeddingLevel,
-     * or if it does, it doesn't resepct it.  So we get runs with wrong
+     * or if it does, it doesn't respect it.  So we get runs with wrong
      * directions.  As such, disable the assert...  It wouldn't crash, but
      * cursoring will be off...
      *
@@ -1212,8 +1244,6 @@
         pos->x_offset = info->var1.i32;
         pos->y_offset = info->var2.i32;
 
-        info->mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
-
         info++, pos++;
       }
     else
@@ -1223,8 +1253,6 @@
         pos->x_offset = info->var1.i32;
         pos->y_offset = info->var2.i32;
 
-        info->mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
-
         info++, pos++;
       }