Revert "Imported Upstream version 1.2.7"
[platform/upstream/harfbuzz.git] / src / hb-ft.cc
index 2cad8c2..322f93a 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * Copyright © 2009  Red Hat, Inc.
  * Copyright © 2009  Keith Stribley
- * Copyright © 2015  Google, Inc.
  *
  *  This is part of HarfBuzz, a text shaping library.
  *
@@ -24,7 +23,6 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  *
  * Red Hat Author(s): Behdad Esfahbod
- * Google Author(s): Behdad Esfahbod
  */
 
 #include "hb-private.hh"
  * In general, this file does a fine job of what it's supposed to do.
  * There are, however, things that need more work:
  *
- *   - I remember seeing FT_Get_Advance() without the NO_HINTING flag to be buggy.
- *     Have not investigated.
+ *   - We don't handle any load_flags.  That definitely has API implications. :(
+ *     I believe hb_ft_font_create() should take load_flags input.
+ *     In particular, FT_Get_Advance() without the NO_HINTING flag seems to be
+ *     buggy.
  *
- *   - FreeType works in 26.6 mode.  Clients can decide to use that mode, and everything
+ *     FreeType works in 26.6 mode.  Clients can decide to use that mode, and everything
  *     would work fine.  However, we also abuse this API for performing in font-space,
  *     but don't pass the correct flags to FreeType.  We just abuse the no-hinting mode
  *     for that, such that no rounding etc happens.  As such, we don't set ppem, and
- *     pass NO_HINTING as load_flags.  Would be much better to use NO_SCALE, and scale
- *     ourselves, like we do in uniscribe, etc.
+ *     pass NO_HINTING around.  This seems to work best, until we go ahead and add a full
+ *     load_flags API.
  *
  *   - We don't handle / allow for emboldening / obliqueing.
  *
  */
 
 
-struct hb_ft_font_t
-{
-  FT_Face ft_face;
-  int load_flags;
-  bool unref; /* Whether to destroy ft_face when done. */
-};
-
-static hb_ft_font_t *
-_hb_ft_font_create (FT_Face ft_face, bool unref)
-{
-  hb_ft_font_t *ft_font = (hb_ft_font_t *) calloc (1, sizeof (hb_ft_font_t));
-
-  if (unlikely (!ft_font))
-    return NULL;
-
-  ft_font->ft_face = ft_face;
-  ft_font->unref = unref;
-
-  ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
-
-  return ft_font;
-}
-
-static void
-_hb_ft_font_destroy (hb_ft_font_t *ft_font)
-{
-  if (ft_font->unref)
-    FT_Done_Face (ft_font->ft_face);
-
-  free (ft_font);
-}
-
-/**
- * hb_ft_font_set_load_flags:
- * @font:
- * @load_flags:
- *
- * 
- *
- * Since: 1.0.5
- **/
-void
-hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
-{
-  if (font->immutable)
-    return;
-
-  if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
-    return;
-
-  hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
-
-  ft_font->load_flags = load_flags;
-}
-
-/**
- * hb_ft_font_get_load_flags:
- * @font:
- *
- * 
- *
- * Return value:
- * Since: 1.0.5
- **/
-int
-hb_ft_font_get_load_flags (hb_font_t *font)
-{
-  if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
-    return 0;
-
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
-
-  return ft_font->load_flags;
-}
-
-FT_Face
-hb_ft_font_get_face (hb_font_t *font)
-{
-  if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
-    return NULL;
-
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
-
-  return ft_font->ft_face;
-}
-
-
-
 static hb_bool_t
-hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED,
-                        void *font_data,
-                        hb_codepoint_t unicode,
-                        hb_codepoint_t *glyph,
-                        void *user_data HB_UNUSED)
-{
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  unsigned int g = FT_Get_Char_Index (ft_font->ft_face, unicode);
-
-  if (unlikely (!g))
-    return false;
+hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
+                void *font_data,
+                hb_codepoint_t unicode,
+                hb_codepoint_t variation_selector,
+                hb_codepoint_t *glyph,
+                void *user_data HB_UNUSED)
 
-  *glyph = g;
-  return true;
-}
-
-static hb_bool_t
-hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED,
-                          void *font_data,
-                          hb_codepoint_t unicode,
-                          hb_codepoint_t variation_selector,
-                          hb_codepoint_t *glyph,
-                          void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  unsigned int g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
+  FT_Face ft_face = (FT_Face) font_data;
 
-  if (unlikely (!g))
-    return false;
+  if (unlikely (variation_selector)) {
+    *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
+    return *glyph != 0;
+  }
 
-  *glyph = g;
-  return true;
+  *glyph = FT_Get_Char_Index (ft_face, unicode);
+  return *glyph != 0;
 }
 
 static hb_position_t
@@ -195,10 +92,11 @@ hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
                           hb_codepoint_t glyph,
                           void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
+  FT_Face ft_face = (FT_Face) font_data;
+  int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
   FT_Fixed v;
 
-  if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags, &v)))
+  if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
     return 0;
 
   if (font->x_scale < 0)
@@ -213,10 +111,11 @@ hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
                           hb_codepoint_t glyph,
                           void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
+  FT_Face ft_face = (FT_Face) font_data;
+  int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
   FT_Fixed v;
 
-  if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
+  if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
     return 0;
 
   if (font->y_scale < 0)
@@ -228,6 +127,18 @@ hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
 }
 
 static hb_bool_t
+hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
+                         void *font_data HB_UNUSED,
+                         hb_codepoint_t glyph HB_UNUSED,
+                         hb_position_t *x HB_UNUSED,
+                         hb_position_t *y HB_UNUSED,
+                         void *user_data HB_UNUSED)
+{
+  /* We always work in the horizontal coordinates. */
+  return true;
+}
+
+static hb_bool_t
 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
                          void *font_data,
                          hb_codepoint_t glyph,
@@ -235,10 +146,10 @@ hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
                          hb_position_t *y,
                          void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  FT_Face ft_face = ft_font->ft_face;
+  FT_Face ft_face = (FT_Face) font_data;
+  int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
 
-  if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
+  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
     return false;
 
   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
@@ -261,16 +172,27 @@ hb_ft_get_glyph_h_kerning (hb_font_t *font,
                           hb_codepoint_t right_glyph,
                           void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
+  FT_Face ft_face = (FT_Face) font_data;
   FT_Vector kerningv;
 
   FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
-  if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
+  if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv))
     return 0;
 
   return kerningv.x;
 }
 
+static hb_position_t
+hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
+                          void *font_data HB_UNUSED,
+                          hb_codepoint_t top_glyph HB_UNUSED,
+                          hb_codepoint_t bottom_glyph HB_UNUSED,
+                          void *user_data HB_UNUSED)
+{
+  /* FreeType API doesn't support vertical kerning */
+  return 0;
+}
+
 static hb_bool_t
 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
                         void *font_data,
@@ -278,26 +200,16 @@ hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
                         hb_glyph_extents_t *extents,
                         void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  FT_Face ft_face = ft_font->ft_face;
+  FT_Face ft_face = (FT_Face) font_data;
+  int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
 
-  if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
+  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
     return false;
 
   extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
   extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
   extents->width = ft_face->glyph->metrics.width;
   extents->height = -ft_face->glyph->metrics.height;
-  if (font->x_scale < 0)
-  {
-    extents->x_bearing = -extents->x_bearing;
-    extents->width = -extents->width;
-  }
-  if (font->y_scale < 0)
-  {
-    extents->y_bearing = -extents->y_bearing;
-    extents->height = -extents->height;
-  }
   return true;
 }
 
@@ -310,10 +222,10 @@ hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
                               hb_position_t *y,
                               void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  FT_Face ft_face = ft_font->ft_face;
+  FT_Face ft_face = (FT_Face) font_data;
+  int load_flags = FT_LOAD_DEFAULT;
 
-  if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
+  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
       return false;
 
   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
@@ -335,9 +247,9 @@ hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
                      char *name, unsigned int size,
                      void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
+  FT_Face ft_face = (FT_Face) font_data;
 
-  hb_bool_t ret = !FT_Get_Glyph_Name (ft_font->ft_face, glyph, name, size);
+  hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
   if (ret && (size && !*name))
     ret = false;
 
@@ -351,8 +263,7 @@ hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
                           hb_codepoint_t *glyph,
                           void *user_data HB_UNUSED)
 {
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  FT_Face ft_face = ft_font->ft_face;
+  FT_Face ft_face = (FT_Face) font_data;
 
   if (len < 0)
     *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
@@ -377,77 +288,23 @@ hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
   return *glyph != 0;
 }
 
-static hb_bool_t
-hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
-                         void *font_data,
-                         hb_font_extents_t *metrics,
-                         void *user_data HB_UNUSED)
-{
-  const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
-  FT_Face ft_face = ft_font->ft_face;
-  metrics->ascender = ft_face->size->metrics.ascender;
-  metrics->descender = ft_face->size->metrics.descender;
-  metrics->line_gap = ft_face->size->metrics.height - (ft_face->size->metrics.ascender - ft_face->size->metrics.descender);
-  if (font->y_scale < 0)
-  {
-    metrics->ascender = -metrics->ascender;
-    metrics->descender = -metrics->descender;
-    metrics->line_gap = -metrics->line_gap;
-  }
-  return true;
-}
-
-static hb_font_funcs_t *static_ft_funcs = NULL;
 
-#ifdef HB_USE_ATEXIT
-static
-void free_static_ft_funcs (void)
+static hb_font_funcs_t *
+_hb_ft_get_font_funcs (void)
 {
-  hb_font_funcs_destroy (static_ft_funcs);
-}
-#endif
+  static const hb_font_funcs_t ft_ffuncs = {
+    HB_OBJECT_HEADER_STATIC,
 
-static void
-_hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
-{
-retry:
-  hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs);
+    true, /* immutable */
 
-  if (unlikely (!funcs))
-  {
-    funcs = hb_font_funcs_create ();
-
-    hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, NULL, NULL);
-    //hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, NULL, NULL);
-    hb_font_funcs_set_nominal_glyph_func (funcs, hb_ft_get_nominal_glyph, NULL, NULL);
-    hb_font_funcs_set_variation_glyph_func (funcs, hb_ft_get_variation_glyph, NULL, NULL);
-    hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, NULL, NULL);
-    hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, NULL, NULL);
-    //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ft_get_glyph_h_origin, NULL, NULL);
-    hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ft_get_glyph_v_origin, NULL, NULL);
-    hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ft_get_glyph_h_kerning, NULL, NULL);
-    //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ft_get_glyph_v_kerning, NULL, NULL);
-    hb_font_funcs_set_glyph_extents_func (funcs, hb_ft_get_glyph_extents, NULL, NULL);
-    hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ft_get_glyph_contour_point, NULL, NULL);
-    hb_font_funcs_set_glyph_name_func (funcs, hb_ft_get_glyph_name, NULL, NULL);
-    hb_font_funcs_set_glyph_from_name_func (funcs, hb_ft_get_glyph_from_name, NULL, NULL);
-
-    hb_font_funcs_make_immutable (funcs);
-
-    if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, NULL, funcs)) {
-      hb_font_funcs_destroy (funcs);
-      goto retry;
+    {
+#define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
+      HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
+#undef HB_FONT_FUNC_IMPLEMENT
     }
-
-#ifdef HB_USE_ATEXIT
-    atexit (free_static_ft_funcs); /* First person registers atexit() callback. */
-#endif
   };
 
-  hb_font_set_funcs (font,
-                    funcs,
-                    _hb_ft_font_create (ft_face, unref),
-                    (hb_destroy_func_t) _hb_ft_font_destroy);
+  return const_cast<hb_font_funcs_t *> (&ft_ffuncs);
 }
 
 
@@ -486,7 +343,7 @@ reference_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
  * 
  *
  * Return value: (transfer full): 
- * Since: 0.9.2
+ * Since: 1.0
  **/
 hb_face_t *
 hb_ft_face_create (FT_Face           ft_face,
@@ -520,7 +377,7 @@ hb_ft_face_create (FT_Face           ft_face,
  * 
  *
  * Return value: (transfer full): 
- * Since: 0.9.38
+ * Since: 1.0
  **/
 hb_face_t *
 hb_ft_face_create_referenced (FT_Face ft_face)
@@ -542,7 +399,7 @@ hb_ft_face_finalize (FT_Face ft_face)
  * 
  *
  * Return value: (transfer full): 
- * Since: 0.9.2
+ * Since: 1.0
  **/
 hb_face_t *
 hb_ft_face_create_cached (FT_Face ft_face)
@@ -559,6 +416,11 @@ hb_ft_face_create_cached (FT_Face ft_face)
   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
 }
 
+static void
+_do_nothing (void)
+{
+}
+
 
 /**
  * hb_ft_font_create:
@@ -568,7 +430,7 @@ hb_ft_face_create_cached (FT_Face ft_face)
  * 
  *
  * Return value: (transfer full): 
- * Since: 0.9.2
+ * Since: 1.0
  **/
 hb_font_t *
 hb_ft_font_create (FT_Face           ft_face,
@@ -580,7 +442,9 @@ hb_ft_font_create (FT_Face           ft_face,
   face = hb_ft_face_create (ft_face, destroy);
   font = hb_font_create (face);
   hb_face_destroy (face);
-  _hb_ft_font_set_funcs (font, ft_face, false);
+  hb_font_set_funcs (font,
+                    _hb_ft_get_font_funcs (),
+                    ft_face, (hb_destroy_func_t) _do_nothing);
   hb_font_set_scale (font,
                     (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
                     (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16));
@@ -600,7 +464,7 @@ hb_ft_font_create (FT_Face           ft_face,
  * 
  *
  * Return value: (transfer full): 
- * Since: 0.9.38
+ * Since: 1.0
  **/
 hb_font_t *
 hb_ft_font_create_referenced (FT_Face ft_face)
@@ -694,6 +558,18 @@ hb_ft_font_set_funcs (hb_font_t *font)
   ft_face->generic.data = blob;
   ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
 
-  _hb_ft_font_set_funcs (font, ft_face, true);
-  hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
+  hb_font_set_funcs (font,
+                    _hb_ft_get_font_funcs (),
+                    ft_face,
+                    (hb_destroy_func_t) FT_Done_Face);
+}
+
+FT_Face
+hb_ft_font_get_face (hb_font_t *font)
+{
+  if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
+      font->destroy == (hb_destroy_func_t) _do_nothing)
+    return (FT_Face) font->user_data;
+
+  return NULL;
 }