Restore variadic macros in DevToolsEmbedderMessageDispatcher
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-ft.cc
blob468742cfd05fef35dd256b6a264ea9ca8a21973d
1 /*
2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2009 Keith Stribley
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
28 #include "hb-private.hh"
30 #include "hb-ft.h"
32 #include "hb-font-private.hh"
34 #include FT_ADVANCES_H
35 #include FT_TRUETYPE_TABLES_H
39 #ifndef HB_DEBUG_FT
40 #define HB_DEBUG_FT (HB_DEBUG+0)
41 #endif
44 /* TODO:
46 * In general, this file does a fine job of what it's supposed to do.
47 * There are, however, things that need more work:
49 * - We don't handle any load_flags. That definitely has API implications. :(
50 * I believe hb_ft_font_create() should take load_flags input.
51 * In particular, FT_Get_Advance() without the NO_HINTING flag seems to be
52 * buggy.
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 around. This seems to work best, until we go ahead and add a full
59 * load_flags API.
61 * - We don't handle / allow for emboldening / obliqueing.
63 * - In the future, we should add constructors to create fonts in font space?
65 * - FT_Load_Glyph() is exteremely costly. Do something about it?
69 static hb_bool_t
70 hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
71 void *font_data,
72 hb_codepoint_t unicode,
73 hb_codepoint_t variation_selector,
74 hb_codepoint_t *glyph,
75 void *user_data HB_UNUSED)
78 unsigned int g;
79 FT_Face ft_face = (FT_Face) font_data;
81 if (likely (!variation_selector))
82 g = FT_Get_Char_Index (ft_face, unicode);
83 else
84 g = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
86 if (unlikely (!g))
87 return false;
89 *glyph = g;
90 return true;
93 static hb_position_t
94 hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
95 void *font_data,
96 hb_codepoint_t glyph,
97 void *user_data HB_UNUSED)
99 FT_Face ft_face = (FT_Face) font_data;
100 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
101 FT_Fixed v;
103 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
104 return 0;
106 if (font->x_scale < 0)
107 v = -v;
109 return (v + (1<<9)) >> 10;
112 static hb_position_t
113 hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
114 void *font_data,
115 hb_codepoint_t glyph,
116 void *user_data HB_UNUSED)
118 FT_Face ft_face = (FT_Face) font_data;
119 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_VERTICAL_LAYOUT;
120 FT_Fixed v;
122 if (unlikely (FT_Get_Advance (ft_face, glyph, load_flags, &v)))
123 return 0;
125 if (font->y_scale < 0)
126 v = -v;
128 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
129 * have a Y growing upward. Hence the extra negation. */
130 return (-v + (1<<9)) >> 10;
133 static hb_bool_t
134 hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
135 void *font_data HB_UNUSED,
136 hb_codepoint_t glyph HB_UNUSED,
137 hb_position_t *x HB_UNUSED,
138 hb_position_t *y HB_UNUSED,
139 void *user_data HB_UNUSED)
141 /* We always work in the horizontal coordinates. */
142 return true;
145 static hb_bool_t
146 hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
147 void *font_data,
148 hb_codepoint_t glyph,
149 hb_position_t *x,
150 hb_position_t *y,
151 void *user_data HB_UNUSED)
153 FT_Face ft_face = (FT_Face) font_data;
154 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
156 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
157 return false;
159 /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
160 * have a Y growing upward. Hence the extra negation. */
161 *x = ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX;
162 *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);
164 if (font->x_scale < 0)
165 *x = -*x;
166 if (font->y_scale < 0)
167 *y = -*y;
169 return true;
172 static hb_position_t
173 hb_ft_get_glyph_h_kerning (hb_font_t *font,
174 void *font_data,
175 hb_codepoint_t left_glyph,
176 hb_codepoint_t right_glyph,
177 void *user_data HB_UNUSED)
179 FT_Face ft_face = (FT_Face) font_data;
180 FT_Vector kerningv;
182 FT_Kerning_Mode mode = font->x_ppem ? FT_KERNING_DEFAULT : FT_KERNING_UNFITTED;
183 if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, mode, &kerningv))
184 return 0;
186 return kerningv.x;
189 static hb_position_t
190 hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
191 void *font_data HB_UNUSED,
192 hb_codepoint_t top_glyph HB_UNUSED,
193 hb_codepoint_t bottom_glyph HB_UNUSED,
194 void *user_data HB_UNUSED)
196 /* FreeType API doesn't support vertical kerning */
197 return 0;
200 static hb_bool_t
201 hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
202 void *font_data,
203 hb_codepoint_t glyph,
204 hb_glyph_extents_t *extents,
205 void *user_data HB_UNUSED)
207 FT_Face ft_face = (FT_Face) font_data;
208 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
210 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
211 return false;
213 extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
214 extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
215 extents->width = ft_face->glyph->metrics.width;
216 extents->height = -ft_face->glyph->metrics.height;
217 return true;
220 static hb_bool_t
221 hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
222 void *font_data,
223 hb_codepoint_t glyph,
224 unsigned int point_index,
225 hb_position_t *x,
226 hb_position_t *y,
227 void *user_data HB_UNUSED)
229 FT_Face ft_face = (FT_Face) font_data;
230 int load_flags = FT_LOAD_DEFAULT;
232 if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
233 return false;
235 if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
236 return false;
238 if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
239 return false;
241 *x = ft_face->glyph->outline.points[point_index].x;
242 *y = ft_face->glyph->outline.points[point_index].y;
244 return true;
247 static hb_bool_t
248 hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED,
249 void *font_data,
250 hb_codepoint_t glyph,
251 char *name, unsigned int size,
252 void *user_data HB_UNUSED)
254 FT_Face ft_face = (FT_Face) font_data;
256 hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
257 if (ret && (size && !*name))
258 ret = false;
260 return ret;
263 static hb_bool_t
264 hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
265 void *font_data,
266 const char *name, int len, /* -1 means nul-terminated */
267 hb_codepoint_t *glyph,
268 void *user_data HB_UNUSED)
270 FT_Face ft_face = (FT_Face) font_data;
272 if (len < 0)
273 *glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
274 else {
275 /* Make a nul-terminated version. */
276 char buf[128];
277 len = MIN (len, (int) sizeof (buf) - 1);
278 strncpy (buf, name, len);
279 buf[len] = '\0';
280 *glyph = FT_Get_Name_Index (ft_face, buf);
283 if (*glyph == 0)
285 /* Check whether the given name was actually the name of glyph 0. */
286 char buf[128];
287 if (!FT_Get_Glyph_Name(ft_face, 0, buf, sizeof (buf)) &&
288 len < 0 ? !strcmp (buf, name) : !strncmp (buf, name, len))
289 return true;
292 return *glyph != 0;
296 static hb_font_funcs_t *
297 _hb_ft_get_font_funcs (void)
299 static const hb_font_funcs_t ft_ffuncs = {
300 HB_OBJECT_HEADER_STATIC,
302 true, /* immutable */
305 #define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
306 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
307 #undef HB_FONT_FUNC_IMPLEMENT
311 return const_cast<hb_font_funcs_t *> (&ft_ffuncs);
315 static hb_blob_t *
316 reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
318 FT_Face ft_face = (FT_Face) user_data;
319 FT_Byte *buffer;
320 FT_ULong length = 0;
321 FT_Error error;
323 /* Note: FreeType like HarfBuzz uses the NONE tag for fetching the entire blob */
325 error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
326 if (error)
327 return NULL;
329 buffer = (FT_Byte *) malloc (length);
330 if (buffer == NULL)
331 return NULL;
333 error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
334 if (error)
335 return NULL;
337 return hb_blob_create ((const char *) buffer, length,
338 HB_MEMORY_MODE_WRITABLE,
339 buffer, free);
343 * hb_ft_face_create:
344 * @ft_face: (destroy destroy) (scope notified):
345 * @destroy:
349 * Return value: (transfer full):
350 * Since: 1.0
352 hb_face_t *
353 hb_ft_face_create (FT_Face ft_face,
354 hb_destroy_func_t destroy)
356 hb_face_t *face;
358 if (ft_face->stream->read == NULL) {
359 hb_blob_t *blob;
361 blob = hb_blob_create ((const char *) ft_face->stream->base,
362 (unsigned int) ft_face->stream->size,
363 HB_MEMORY_MODE_READONLY,
364 ft_face, destroy);
365 face = hb_face_create (blob, ft_face->face_index);
366 hb_blob_destroy (blob);
367 } else {
368 face = hb_face_create_for_tables (reference_table, ft_face, destroy);
371 hb_face_set_index (face, ft_face->face_index);
372 hb_face_set_upem (face, ft_face->units_per_EM);
374 return face;
378 * hb_ft_face_create_referenced:
379 * @ft_face:
383 * Return value: (transfer full):
384 * Since: 0.9.38
386 hb_face_t *
387 hb_ft_face_create_referenced (FT_Face ft_face)
389 FT_Reference_Face (ft_face);
390 return hb_ft_face_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
393 static void
394 hb_ft_face_finalize (FT_Face ft_face)
396 hb_face_destroy ((hb_face_t *) ft_face->generic.data);
400 * hb_ft_face_create_cached:
401 * @ft_face:
405 * Return value: (transfer full):
406 * Since: 1.0
408 hb_face_t *
409 hb_ft_face_create_cached (FT_Face ft_face)
411 if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
413 if (ft_face->generic.finalizer)
414 ft_face->generic.finalizer (ft_face);
416 ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
417 ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
420 return hb_face_reference ((hb_face_t *) ft_face->generic.data);
423 static void
424 _do_nothing (void)
430 * hb_ft_font_create:
431 * @ft_face: (destroy destroy) (scope notified):
432 * @destroy:
436 * Return value: (transfer full):
437 * Since: 1.0
439 hb_font_t *
440 hb_ft_font_create (FT_Face ft_face,
441 hb_destroy_func_t destroy)
443 hb_font_t *font;
444 hb_face_t *face;
446 face = hb_ft_face_create (ft_face, destroy);
447 font = hb_font_create (face);
448 hb_face_destroy (face);
449 hb_font_set_funcs (font,
450 _hb_ft_get_font_funcs (),
451 ft_face, (hb_destroy_func_t) _do_nothing);
452 hb_font_set_scale (font,
453 (int) (((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16),
454 (int) (((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM + (1<<15)) >> 16));
455 #if 0 /* hb-ft works in no-hinting model */
456 hb_font_set_ppem (font,
457 ft_face->size->metrics.x_ppem,
458 ft_face->size->metrics.y_ppem);
459 #endif
461 return font;
465 * hb_ft_font_create_referenced:
466 * @ft_face:
470 * Return value: (transfer full):
471 * Since: 0.9.38
473 hb_font_t *
474 hb_ft_font_create_referenced (FT_Face ft_face)
476 FT_Reference_Face (ft_face);
477 return hb_ft_font_create (ft_face, (hb_destroy_func_t) FT_Done_Face);
481 /* Thread-safe, lock-free, FT_Library */
483 static FT_Library ft_library;
485 #ifdef HB_USE_ATEXIT
486 static
487 void free_ft_library (void)
489 FT_Done_FreeType (ft_library);
491 #endif
493 static FT_Library
494 get_ft_library (void)
496 retry:
497 FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library);
499 if (unlikely (!library))
501 /* Not found; allocate one. */
502 if (FT_Init_FreeType (&library))
503 return NULL;
505 if (!hb_atomic_ptr_cmpexch (&ft_library, NULL, library)) {
506 FT_Done_FreeType (library);
507 goto retry;
510 #ifdef HB_USE_ATEXIT
511 atexit (free_ft_library); /* First person registers atexit() callback. */
512 #endif
515 return library;
518 static void
519 _release_blob (FT_Face ft_face)
521 hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
524 void
525 hb_ft_font_set_funcs (hb_font_t *font)
527 hb_blob_t *blob = hb_face_reference_blob (font->face);
528 unsigned int blob_length;
529 const char *blob_data = hb_blob_get_data (blob, &blob_length);
530 if (unlikely (!blob_length))
531 DEBUG_MSG (FT, font, "Font face has empty blob");
533 FT_Face ft_face = NULL;
534 FT_Error err = FT_New_Memory_Face (get_ft_library (),
535 (const FT_Byte *) blob_data,
536 blob_length,
537 hb_face_get_index (font->face),
538 &ft_face);
540 if (unlikely (err)) {
541 hb_blob_destroy (blob);
542 DEBUG_MSG (FT, font, "Font face FT_New_Memory_Face() failed");
543 return;
546 FT_Select_Charmap (ft_face, FT_ENCODING_UNICODE);
548 FT_Set_Char_Size (ft_face,
549 abs (font->x_scale), abs (font->y_scale),
550 0, 0);
551 #if 0
552 font->x_ppem * 72 * 64 / font->x_scale,
553 font->y_ppem * 72 * 64 / font->y_scale);
554 #endif
555 if (font->x_scale < 0 || font->y_scale < 0)
557 FT_Matrix matrix = { font->x_scale < 0 ? -1 : +1, 0,
558 0, font->y_scale < 0 ? -1 : +1};
559 FT_Set_Transform (ft_face, &matrix, NULL);
562 ft_face->generic.data = blob;
563 ft_face->generic.finalizer = (FT_Generic_Finalizer) _release_blob;
565 hb_font_set_funcs (font,
566 _hb_ft_get_font_funcs (),
567 ft_face,
568 (hb_destroy_func_t) FT_Done_Face);
571 FT_Face
572 hb_ft_font_get_face (hb_font_t *font)
574 if (font->destroy == (hb_destroy_func_t) FT_Done_Face ||
575 font->destroy == (hb_destroy_func_t) _do_nothing)
576 return (FT_Face) font->user_data;
578 return NULL;