1 /*
   2  * Copyright © 2009  Red Hat, Inc.
   3  * Copyright © 2009  Keith Stribley
   4  * Copyright © 2015  Google, Inc.
   5  *
   6  *  This is part of HarfBuzz, a text shaping library.
   7  *
   8  * Permission is hereby granted, without written agreement and without
   9  * license or royalty fees, to use, copy, modify, and distribute this
  10  * software and its documentation for any purpose, provided that the
  11  * above copyright notice and the following two paragraphs appear in
  12  * all copies of this software.
  13  *
  14  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  15  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  17  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  18  * DAMAGE.
  19  *
  20  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  21  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  23  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25  *
  26  * Red Hat Author(s): Behdad Esfahbod
  27  * Google Author(s): Behdad Esfahbod
  28  */
  29 
  30 #include "hb-private.hh"
  31 
  32 #include "hb-ft.h"
  33 
  34 #include "hb-font-private.hh"
  35 
  36 #include FT_ADVANCES_H
  37 #include FT_TRUETYPE_TABLES_H
  38 
  39 
  40 
  41 #ifndef HB_DEBUG_FT
  42 #define HB_DEBUG_FT (HB_DEBUG+0)
  43 #endif
  44 
  45 
  46 /* TODO:
  47  *
  48  * In general, this file does a fine job of what it's supposed to do.
  49  * There are, however, things that need more work:
  50  *
  51  *   - I remember seeing FT_Get_Advance() without the NO_HINTING flag to be buggy.
  52  *     Have not investigated.
  53  *
  54  *   - FreeType works in 26.6 mode.  Clients can decide to use that mode, and everything
  55  *     would work fine.  However, we also abuse this API for performing in font-space,
  56  *     but don't pass the correct flags to FreeType.  We just abuse the no-hinting mode
  57  *     for that, such that no rounding etc happens.  As such, we don't set ppem, and
  58  *     pass NO_HINTING as load_flags.  Would be much better to use NO_SCALE, and scale
  59  *     ourselves, like we do in uniscribe, etc.
  60  *
  61  *   - We don't handle / allow for emboldening / obliqueing.
  62  *
  63  *   - In the future, we should add constructors to create fonts in font space?
  64  *
  65  *   - FT_Load_Glyph() is exteremely costly.  Do something about it?
  66  */
  67 
  68 
  69 struct hb_ft_font_t
  70 {
  71   FT_Face ft_face;
  72   int load_flags;
  73   bool unref; /* Whether to destroy ft_face when done. */
  74 };
  75 
  76 static hb_ft_font_t *
  77 _hb_ft_font_create (FT_Face ft_face, bool unref)
  78 {
  79   hb_ft_font_t *ft_font = (hb_ft_font_t *) calloc (1, sizeof (hb_ft_font_t));
  80 
  81   if (unlikely (!ft_font))
  82     return NULL;
  83 
  84   ft_font->ft_face = ft_face;
  85   ft_font->unref = unref;
  86 
  87   ft_font->load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
  88 
  89   return ft_font;
  90 }
  91 
  92 static void
  93 _hb_ft_font_destroy (hb_ft_font_t *ft_font)
  94 {
  95   if (ft_font->unref)
  96     FT_Done_Face (ft_font->ft_face);
  97 
  98   free (ft_font);
  99 }
 100 
 101 /**
 102  * hb_ft_font_set_load_flags:
 103  * @font:
 104  * @load_flags:
 105  *
 106  *
 107  *
 108  * Since: 1.0.5
 109  **/
 110 void
 111 hb_ft_font_set_load_flags (hb_font_t *font, int load_flags)
 112 {
 113   if (font->immutable)
 114     return;
 115 
 116   if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
 117     return;
 118 
 119   hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data;
 120 
 121   ft_font->load_flags = load_flags;
 122 }
 123 
 124 /**
 125  * hb_ft_font_get_load_flags:
 126  * @font:
 127  *
 128  *
 129  *
 130  * Return value:
 131  * Since: 1.0.5
 132  **/
 133 int
 134 hb_ft_font_get_load_flags (hb_font_t *font)
 135 {
 136   if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
 137     return 0;
 138 
 139   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
 140 
 141   return ft_font->load_flags;
 142 }
 143 
 144 FT_Face
 145 hb_ft_font_get_face (hb_font_t *font)
 146 {
 147   if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy)
 148     return NULL;
 149 
 150   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data;
 151 
 152   return ft_font->ft_face;
 153 }
 154 
 155 
 156 
 157 static hb_bool_t
 158 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
 159                  void *font_data,
 160                  hb_codepoint_t unicode,
 161                  hb_codepoint_t variation_selector,
 162                  hb_codepoint_t *glyph,
 163                  void *user_data HB_UNUSED)
 164 
 165 {
 166   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 167   unsigned int g;
 168 
 169   if (likely (!variation_selector))
 170     g = FT_Get_Char_Index (ft_font->ft_face, unicode);
 171   else
 172     g = FT_Face_GetCharVariantIndex (ft_font->ft_face, unicode, variation_selector);
 173 
 174   if (unlikely (!g))
 175     return false;
 176 
 177   *glyph = g;
 178   return true;
 179 }
 180 
 181 static hb_position_t
 182 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
 183                            void *font_data,
 184                            hb_codepoint_t glyph,
 185                            void *user_data HB_UNUSED)
 186 {
 187   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 188   FT_Fixed v;
 189 
 190   if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags, &v)))
 191     return 0;
 192 
 193   if (font->x_scale < 0)
 194     v = -v;
 195 
 196   return (v + (1<<9)) >> 10;
 197 }
 198 
 199 static hb_position_t
 200 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
 201                            void *font_data,
 202                            hb_codepoint_t glyph,
 203                            void *user_data HB_UNUSED)
 204 {
 205   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 206   FT_Fixed v;
 207 
 208   if (unlikely (FT_Get_Advance (ft_font->ft_face, glyph, ft_font->load_flags | FT_LOAD_VERTICAL_LAYOUT, &v)))
 209     return 0;
 210 
 211   if (font->y_scale < 0)
 212     v = -v;
 213 
 214   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
 215    * have a Y growing upward.  Hence the extra negation. */
 216   return (-v + (1<<9)) >> 10;
 217 }
 218 
 219 static hb_bool_t
 220 hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
 221                           void *font_data HB_UNUSED,
 222                           hb_codepoint_t glyph HB_UNUSED,
 223                           hb_position_t *x HB_UNUSED,
 224                           hb_position_t *y HB_UNUSED,
 225                           void *user_data HB_UNUSED)
 226 {
 227   /* We always work in the horizontal coordinates. */
 228   return true;
 229 }
 230 
 231 static hb_bool_t
 232 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
 233                           void *font_data,
 234                           hb_codepoint_t glyph,
 235                           hb_position_t *x,
 236                           hb_position_t *y,
 237                           void *user_data HB_UNUSED)
 238 {
 239   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 240   FT_Face ft_face = ft_font->ft_face;
 241 
 242   if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
 243     return false;
 244 
 245   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
 246    * have a Y growing upward.  Hence the extra negation. */
 247   *x = ft_face->glyph->metrics.horiBearingX -   ft_face->glyph->metrics.vertBearingX;
 248   *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
 249 
 250   if (font->x_scale < 0)
 251     *x = -*x;
 252   if (font->y_scale < 0)
 253     *y = -*y;
 254 
 255   return true;
 256 }
 257 
 258 static hb_position_t
 259 hb_ft_get_glyph_h_kerning (hb_font_t *font,
 260                            void *font_data,
 261                            hb_codepoint_t left_glyph,
 262                            hb_codepoint_t right_glyph,
 263                            void *user_data HB_UNUSED)
 264 {
 265   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 266   FT_Vector kerningv;
 267 
 268   FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
 269   if (FT_Get_Kerning (ft_font->ft_face, left_glyph, right_glyph, mode, &kerningv))
 270     return 0;
 271 
 272   return kerningv.x;
 273 }
 274 
 275 static hb_position_t
 276 hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
 277                            void *font_data HB_UNUSED,
 278                            hb_codepoint_t top_glyph HB_UNUSED,
 279                            hb_codepoint_t bottom_glyph HB_UNUSED,
 280                            void *user_data HB_UNUSED)
 281 {
 282   /* FreeType API doesn't support vertical kerning */
 283   return 0;
 284 }
 285 
 286 static hb_bool_t
 287 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
 288                          void *font_data,
 289                          hb_codepoint_t glyph,
 290                          hb_glyph_extents_t *extents,
 291                          void *user_data HB_UNUSED)
 292 {
 293   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 294   FT_Face ft_face = ft_font->ft_face;
 295 
 296   if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
 297     return false;
 298 
 299   extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
 300   extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
 301   extents->width = ft_face->glyph->metrics.width;
 302   extents->height = -ft_face->glyph->metrics.height;
 303   return true;
 304 }
 305 
 306 static hb_bool_t
 307 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
 308                                void *font_data,
 309                                hb_codepoint_t glyph,
 310                                unsigned int point_index,
 311                                hb_position_t *x,
 312                                hb_position_t *y,
 313                                void *user_data HB_UNUSED)
 314 {
 315   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 316   FT_Face ft_face = ft_font->ft_face;
 317 
 318   if (unlikely (FT_Load_Glyph (ft_face, glyph, ft_font->load_flags)))
 319       return false;
 320 
 321   if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
 322       return false;
 323 
 324   if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
 325       return false;
 326 
 327   *x = ft_face->glyph->outline.points[point_index].x;
 328   *y = ft_face->glyph->outline.points[point_index].y;
 329 
 330   return true;
 331 }
 332 
 333 static hb_bool_t
 334 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
 335                       void *font_data,
 336                       hb_codepoint_t glyph,
 337                       char *name, unsigned int size,
 338                       void *user_data HB_UNUSED)
 339 {
 340   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 341 
 342   hb_bool_t ret = !FT_Get_Glyph_Name (ft_font->ft_face, glyph, name, size);
 343   if (ret && (size && !*name))
 344     ret = false;
 345 
 346   return ret;
 347 }
 348 
 349 static hb_bool_t
 350 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
 351                            void *font_data,
 352                            const char *name, int len, /* -1 means nul-terminated */
 353                            hb_codepoint_t *glyph,
 354                            void *user_data HB_UNUSED)
 355 {
 356   const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
 357   FT_Face ft_face = ft_font->ft_face;
 358 
 359   if (len < 0)
 360     *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
 361   else {
 362     /* Make a nul-terminated version. */
 363     char buf[128];
 364     len = MIN (len, (int) sizeof (buf) - 1);
 365     strncpy (buf, name, len);
 366     buf[len] = '\0';
 367     *glyph = FT_Get_Name_Index (ft_face, buf);
 368   }
 369 
 370   if (*glyph == 0)
 371   {
 372     /* Check whether the given name was actually the name of glyph 0. */
 373     char buf[128];
 374     if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
 375         len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
 376       return true;
 377   }
 378 
 379   return *glyph != 0;
 380 }
 381 
 382 
 383 static void
 384 _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
 385 {
 386   static const hb_font_funcs_t ft_ffuncs = {
 387     HB_OBJECT_HEADER_STATIC,
 388 
 389     true, /* immutable */
 390 
 391     {
 392 #define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
 393       HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
 394 #undef HB_FONT_FUNC_IMPLEMENT
 395     }
 396   };
 397 
 398   hb_font_set_funcs (font,
 399                      const_cast<hb_font_funcs_t *> (&ft_ffuncs),
 400                      _hb_ft_font_create (ft_face, unref),
 401                      (hb_destroy_func_t) _hb_ft_font_destroy);
 402 }
 403 
 404 
 405 static hb_blob_t *
 406 reference_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
 407 {
 408   FT_Face ft_face = (FT_Face) user_data;
 409   FT_Byte *buffer;
 410   FT_ULong  length = 0;
 411   FT_Error error;
 412 
 413   /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
 414 
 415   error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
 416   if (error)
 417     return NULL;
 418 
 419   buffer = (FT_Byte *) malloc (length);
 420   if (buffer == NULL)
 421     return NULL;
 422 
 423   error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
 424   if (error)
 425     return NULL;
 426 
 427   return hb_blob_create ((const char *) buffer, length,
 428                          HB_MEMORY_MODE_WRITABLE,
 429                          buffer, free);
 430 }
 431 
 432 /**
 433  * hb_ft_face_create:
 434  * @ft_face: (destroy destroy) (scope notified):
 435  * @destroy:
 436  *
 437  *
 438  *
 439  * Return value: (transfer full):
 440  * Since: 0.9.2
 441  **/
 442 hb_face_t *
 443 hb_ft_face_create (FT_Face           ft_face,
 444                    hb_destroy_func_t destroy)
 445 {
 446   hb_face_t *face;
 447 
 448   if (ft_face->stream->read == NULL) {
 449     hb_blob_t *blob;
 450 
 451     blob = hb_blob_create ((const char *) ft_face->stream->base,
 452                            (unsigned int) ft_face->stream->size,
 453                            HB_MEMORY_MODE_READONLY,
 454                            ft_face, destroy);
 455     face = hb_face_create (blob, ft_face->face_index);
 456     hb_blob_destroy (blob);
 457   } else {
 458     face = hb_face_create_for_tables (reference_table, ft_face, destroy);
 459   }
 460 
 461   hb_face_set_index (face, ft_face->face_index);
 462   hb_face_set_upem (face, ft_face->units_per_EM);
 463 
 464   return face;
 465 }
 466 
 467 /**
 468  * hb_ft_face_create_referenced:
 469  * @ft_face:
 470  *
 471  *
 472  *
 473  * Return value: (transfer full):
 474  * Since: 0.9.38
 475  **/
 476 hb_face_t *
 477 hb_ft_face_create_referenced (FT_Face ft_face)
 478 {
 479   FT_Reference_Face (ft_face);
 480   return hb_ft_face_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
 481 }
 482 
 483 static void
 484 hb_ft_face_finalize (FT_Face ft_face)
 485 {
 486   hb_face_destroy ((hb_face_t *) ft_face->generic.data);
 487 }
 488 
 489 /**
 490  * hb_ft_face_create_cached:
 491  * @ft_face:
 492  *
 493  *
 494  *
 495  * Return value: (transfer full):
 496  * Since: 0.9.2
 497  **/
 498 hb_face_t *
 499 hb_ft_face_create_cached (FT_Face ft_face)
 500 {
 501   if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
 502   {
 503     if (ft_face->generic.finalizer)
 504       ft_face->generic.finalizer (ft_face);
 505 
 506     ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
 507     ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
 508   }
 509 
 510   return hb_face_reference ((hb_face_t *) ft_face->generic.data);
 511 }
 512 
 513 
 514 /**
 515  * hb_ft_font_create:
 516  * @ft_face: (destroy destroy) (scope notified):
 517  * @destroy:
 518  *
 519  *
 520  *
 521  * Return value: (transfer full):
 522  * Since: 0.9.2
 523  **/
 524 hb_font_t *
 525 hb_ft_font_create (FT_Face           ft_face,
 526                    hb_destroy_func_t destroy)
 527 {
 528   hb_font_t *font;
 529   hb_face_t *face;
 530 
 531   face = hb_ft_face_create (ft_face, destroy);
 532   font = hb_font_create (face);
 533   hb_face_destroy (face);
 534   _hb_ft_font_set_funcs (font, ft_face, false);
 535   hb_font_set_scale (font,
 536                      (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
 537                      (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16));
 538 #if 0 /* hb-ft works in no-hinting model */
 539   hb_font_set_ppem (font,
 540                     ft_face->size->metrics.x_ppem,
 541                     ft_face->size->metrics.y_ppem);
 542 #endif
 543 
 544   return font;
 545 }
 546 
 547 /**
 548  * hb_ft_font_create_referenced:
 549  * @ft_face:
 550  *
 551  *
 552  *
 553  * Return value: (transfer full):
 554  * Since: 0.9.38
 555  **/
 556 hb_font_t *
 557 hb_ft_font_create_referenced (FT_Face ft_face)
 558 {
 559   FT_Reference_Face (ft_face);
 560   return hb_ft_font_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
 561 }
 562 
 563 
 564 /* Thread-safe, lock-free, FT_Library */
 565 
 566 static FT_Library ft_library;
 567 
 568 #ifdef HB_USE_ATEXIT
 569 static
 570 void free_ft_library (void)
 571 {
 572   FT_Done_FreeType (ft_library);
 573 }
 574 #endif
 575 
 576 static FT_Library
 577 get_ft_library (void)
 578 {
 579 retry:
 580   FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
 581 
 582   if (unlikely (!library))
 583   {
 584     /* Not found; allocate one. */
 585     if (FT_Init_FreeType (&library))
 586       return NULL;
 587 
 588     if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
 589       FT_Done_FreeType (library);
 590       goto retry;
 591     }
 592 
 593 #ifdef HB_USE_ATEXIT
 594     atexit (free_ft_library); /* First person registers atexit() callback. */
 595 #endif
 596   }
 597 
 598   return library;
 599 }
 600 
 601 static void
 602 _release_blob (FT_Face ft_face)
 603 {
 604   hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
 605 }
 606 
 607 void
 608 hb_ft_font_set_funcs (hb_font_t *font)
 609 {
 610   hb_blob_t *blob = hb_face_reference_blob (font->face);
 611   unsigned int blob_length;
 612   const char *blob_data = hb_blob_get_data (blob, &blob_length);
 613   if (unlikely (!blob_length))
 614     DEBUG_MSG (FT, font, "Font face has empty blob");
 615 
 616   FT_Face ft_face = NULL;
 617   FT_Error err = FT_New_Memory_Face (get_ft_library (),
 618                                      (const FT_Byte *) blob_data,
 619                                      blob_length,
 620                                      hb_face_get_index (font->face),
 621                                      &ft_face);
 622 
 623   if (unlikely (err)) {
 624     hb_blob_destroy (blob);
 625     DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
 626     return;
 627   }
 628 
 629   FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
 630 
 631   FT_Set_Char_Size (ft_face,
 632                     abs (font->x_scale), abs (font->y_scale),
 633                     0, 0);
 634 #if 0
 635                     font->x_ppem * 72 * 64 / font->x_scale,
 636                     font->y_ppem * 72 * 64 / font->y_scale);
 637 #endif
 638   if (font->x_scale < 0 || font->y_scale < 0)
 639   {
 640     FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
 641                           0, font->y_scale < 0 ? -1 : +1};
 642     FT_Set_Transform (ft_face, &matrix, NULL);
 643   }
 644 
 645   ft_face->generic.data = blob;
 646   ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
 647 
 648   _hb_ft_font_set_funcs (font, ft_face, true);
 649   hb_ft_font_set_load_flags (font, FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING);
 650 }