2 * Copyright © 2011,2014 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod, Roozbeh Pournader
27 #include "hb-private.hh"
31 #include "hb-font-private.hh"
33 #include "hb-ot-cmap-table.hh"
34 #include "hb-ot-glyf-table.hh"
35 #include "hb-ot-head-table.hh"
36 #include "hb-ot-hhea-table.hh"
37 #include "hb-ot-hmtx-table.hh"
38 #include "hb-ot-os2-table.hh"
39 //#include "hb-ot-post-table.hh"
42 struct hb_ot_face_metrics_accelerator_t
44 unsigned int num_metrics;
45 unsigned int num_advances;
46 unsigned int default_advance;
47 unsigned short ascender;
48 unsigned short descender;
49 unsigned short line_gap;
51 const OT::_mtx *table;
54 inline void init (hb_face_t *face,
59 this->default_advance = face->get_upem ();
61 bool got_font_extents = false;
64 hb_blob_t *os2_blob = OT::Sanitizer<OT::os2>::sanitize (face->reference_table (os2_tag));
65 const OT::os2 *os2 = OT::Sanitizer<OT::os2>::lock_instance (os2_blob);
66 #define USE_TYPO_METRICS (1u<<7)
67 if (0 != (os2->fsSelection & USE_TYPO_METRICS))
69 this->ascender = os2->sTypoAscender;
70 this->descender = os2->sTypoDescender;
71 this->line_gap = os2->sTypoLineGap;
72 got_font_extents = (this->ascender | this->descender) != 0;
74 hb_blob_destroy (os2_blob);
77 hb_blob_t *_hea_blob = OT::Sanitizer<OT::_hea>::sanitize (face->reference_table (_hea_tag));
78 const OT::_hea *_hea = OT::Sanitizer<OT::_hea>::lock_instance (_hea_blob);
79 this->num_advances = _hea->numberOfLongMetrics;
80 if (!got_font_extents)
82 this->ascender = _hea->ascender;
83 this->descender = _hea->descender;
84 this->line_gap = _hea->lineGap;
86 hb_blob_destroy (_hea_blob);
88 this->blob = OT::Sanitizer<OT::_mtx>::sanitize (face->reference_table (_mtx_tag));
90 /* Cap num_metrics() and num_advances() based on table length. */
91 unsigned int len = hb_blob_get_length (this->blob);
92 if (unlikely (this->num_advances * 4 > len))
93 this->num_advances = len / 4;
94 this->num_metrics = this->num_advances + (len - 4 * this->num_advances) / 2;
96 /* We MUST set num_metrics to zero if num_advances is zero.
97 * Our get_advance() depends on that. */
98 if (unlikely (!this->num_advances))
100 this->num_metrics = this->num_advances = 0;
101 hb_blob_destroy (this->blob);
102 this->blob = hb_blob_get_empty ();
104 this->table = OT::Sanitizer<OT::_mtx>::lock_instance (this->blob);
107 inline void fini (void)
109 hb_blob_destroy (this->blob);
112 inline unsigned int get_advance (hb_codepoint_t glyph) const
114 if (unlikely (glyph >= this->num_metrics))
116 /* If this->num_metrics is zero, it means we don't have the metrics table
117 * for this direction: return default advance. Otherwise, it means that the
118 * glyph index is out of bound: return zero. */
119 if (this->num_metrics)
122 return this->default_advance;
125 if (glyph >= this->num_advances)
126 glyph = this->num_advances - 1;
128 return this->table->longMetric[glyph].advance;
132 struct hb_ot_face_glyf_accelerator_t
135 unsigned int num_glyphs;
136 const OT::loca *loca;
137 const OT::glyf *glyf;
138 hb_blob_t *loca_blob;
139 hb_blob_t *glyf_blob;
140 unsigned int glyf_len;
142 inline void init (hb_face_t *face)
144 hb_blob_t *head_blob = OT::Sanitizer<OT::head>::sanitize (face->reference_table (HB_OT_TAG_head));
145 const OT::head *head = OT::Sanitizer<OT::head>::lock_instance (head_blob);
146 if ((unsigned int) head->indexToLocFormat > 1 || head->glyphDataFormat != 0)
148 /* Unknown format. Leave num_glyphs=0, that takes care of disabling us. */
149 hb_blob_destroy (head_blob);
152 this->short_offset = 0 == head->indexToLocFormat;
153 hb_blob_destroy (head_blob);
155 this->loca_blob = OT::Sanitizer<OT::loca>::sanitize (face->reference_table (HB_OT_TAG_loca));
156 this->loca = OT::Sanitizer<OT::loca>::lock_instance (this->loca_blob);
157 this->glyf_blob = OT::Sanitizer<OT::glyf>::sanitize (face->reference_table (HB_OT_TAG_glyf));
158 this->glyf = OT::Sanitizer<OT::glyf>::lock_instance (this->glyf_blob);
160 this->num_glyphs = MAX (1u, hb_blob_get_length (this->loca_blob) / (this->short_offset ? 2 : 4)) - 1;
161 this->glyf_len = hb_blob_get_length (this->glyf_blob);
164 inline void fini (void)
166 hb_blob_destroy (this->loca_blob);
167 hb_blob_destroy (this->glyf_blob);
170 inline bool get_extents (hb_codepoint_t glyph,
171 hb_glyph_extents_t *extents) const
173 if (unlikely (glyph >= this->num_glyphs))
176 unsigned int start_offset, end_offset;
177 if (this->short_offset)
179 start_offset = 2 * this->loca->u.shortsZ[glyph];
180 end_offset = 2 * this->loca->u.shortsZ[glyph + 1];
184 start_offset = this->loca->u.longsZ[glyph];
185 end_offset = this->loca->u.longsZ[glyph + 1];
188 if (start_offset > end_offset || end_offset > this->glyf_len)
191 if (end_offset - start_offset < OT::glyfGlyphHeader::static_size)
192 return true; /* Empty glyph; zero extents. */
194 const OT::glyfGlyphHeader &glyph_header = OT::StructAtOffset<OT::glyfGlyphHeader> (this->glyf, start_offset);
196 extents->x_bearing = MIN (glyph_header.xMin, glyph_header.xMax);
197 extents->y_bearing = MAX (glyph_header.yMin, glyph_header.yMax);
198 extents->width = MAX (glyph_header.xMin, glyph_header.xMax) - extents->x_bearing;
199 extents->height = MIN (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing;
205 typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
206 hb_codepoint_t codepoint,
207 hb_codepoint_t *glyph);
209 template <typename Type>
210 static inline bool get_glyph_from (const void *obj,
211 hb_codepoint_t codepoint,
212 hb_codepoint_t *glyph)
214 const Type *typed_obj = (const Type *) obj;
215 return typed_obj->get_glyph (codepoint, glyph);
218 struct hb_ot_face_cmap_accelerator_t
220 hb_cmap_get_glyph_func_t get_glyph_func;
221 const void *get_glyph_data;
222 OT::CmapSubtableFormat4::accelerator_t format4_accel;
224 const OT::CmapSubtableFormat14 *uvs_table;
227 inline void init (hb_face_t *face)
229 this->blob = OT::Sanitizer<OT::cmap>::sanitize (face->reference_table (HB_OT_TAG_cmap));
230 const OT::cmap *cmap = OT::Sanitizer<OT::cmap>::lock_instance (this->blob);
231 const OT::CmapSubtable *subtable = NULL;
232 const OT::CmapSubtableFormat14 *subtable_uvs = NULL;
234 /* 32-bit subtables. */
235 if (!subtable) subtable = cmap->find_subtable (3, 10);
236 if (!subtable) subtable = cmap->find_subtable (0, 6);
237 if (!subtable) subtable = cmap->find_subtable (0, 4);
238 /* 16-bit subtables. */
239 if (!subtable) subtable = cmap->find_subtable (3, 1);
240 if (!subtable) subtable = cmap->find_subtable (0, 3);
241 if (!subtable) subtable = cmap->find_subtable (0, 2);
242 if (!subtable) subtable = cmap->find_subtable (0, 1);
243 if (!subtable) subtable = cmap->find_subtable (0, 0);
244 if (!subtable) subtable = cmap->find_subtable (3, 0);
246 if (!subtable) subtable = &OT::Null(OT::CmapSubtable);
251 const OT::CmapSubtable *st = cmap->find_subtable (0, 5);
252 if (st && st->u.format == 14)
253 subtable_uvs = &st->u.format14;
256 if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtableFormat14);
258 this->uvs_table = subtable_uvs;
260 this->get_glyph_data = subtable;
261 switch (subtable->u.format) {
262 /* Accelerate format 4 and format 12. */
263 default: this->get_glyph_func = get_glyph_from<OT::CmapSubtable>; break;
264 case 12: this->get_glyph_func = get_glyph_from<OT::CmapSubtableFormat12>; break;
267 this->format4_accel.init (&subtable->u.format4);
268 this->get_glyph_data = &this->format4_accel;
269 this->get_glyph_func = this->format4_accel.get_glyph_func;
275 inline void fini (void)
277 hb_blob_destroy (this->blob);
280 inline bool get_nominal_glyph (hb_codepoint_t unicode,
281 hb_codepoint_t *glyph) const
283 return this->get_glyph_func (this->get_glyph_data, unicode, glyph);
286 inline bool get_variation_glyph (hb_codepoint_t unicode,
287 hb_codepoint_t variation_selector,
288 hb_codepoint_t *glyph) const
290 switch (this->uvs_table->get_glyph_variant (unicode,
294 case OT::GLYPH_VARIANT_NOT_FOUND: return false;
295 case OT::GLYPH_VARIANT_FOUND: return true;
296 case OT::GLYPH_VARIANT_USE_DEFAULT: break;
299 return get_nominal_glyph (unicode, glyph);
303 template <typename T>
304 struct hb_lazy_loader_t
306 inline void init (hb_face_t *face_)
312 inline void fini (void)
314 if (instance && instance != &OT::Null(T))
321 inline const T* operator-> (void) const
324 T *p = (T *) hb_atomic_ptr_get (&instance);
327 p = (T *) calloc (1, sizeof (T));
331 if (unlikely (!hb_atomic_ptr_cmpexch (const_cast<T **>(&instance), NULL, p)))
347 hb_ot_face_cmap_accelerator_t cmap;
348 hb_ot_face_metrics_accelerator_t h_metrics;
349 hb_ot_face_metrics_accelerator_t v_metrics;
350 hb_lazy_loader_t<hb_ot_face_glyf_accelerator_t> glyf;
354 static hb_ot_font_t *
355 _hb_ot_font_create (hb_face_t *face)
357 hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));
359 if (unlikely (!ot_font))
362 ot_font->cmap.init (face);
363 ot_font->h_metrics.init (face, HB_OT_TAG_hhea, HB_OT_TAG_hmtx, HB_OT_TAG_os2);
364 ot_font->v_metrics.init (face, HB_OT_TAG_vhea, HB_OT_TAG_vmtx, HB_TAG_NONE); /* TODO Can we do this lazily? */
365 ot_font->glyf.init (face);
371 _hb_ot_font_destroy (hb_ot_font_t *ot_font)
373 ot_font->cmap.fini ();
374 ot_font->h_metrics.fini ();
375 ot_font->v_metrics.fini ();
376 ot_font->glyf.fini ();
383 hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED,
385 hb_codepoint_t unicode,
386 hb_codepoint_t *glyph,
387 void *user_data HB_UNUSED)
390 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
391 return ot_font->cmap.get_nominal_glyph (unicode, glyph);
395 hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED,
397 hb_codepoint_t unicode,
398 hb_codepoint_t variation_selector,
399 hb_codepoint_t *glyph,
400 void *user_data HB_UNUSED)
402 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
403 return ot_font->cmap.get_variation_glyph (unicode, variation_selector, glyph);
407 hb_ot_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
409 hb_codepoint_t glyph,
410 void *user_data HB_UNUSED)
412 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
413 return font->em_scale_x (ot_font->h_metrics.get_advance (glyph));
417 hb_ot_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
419 hb_codepoint_t glyph,
420 void *user_data HB_UNUSED)
422 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
423 return font->em_scale_y (-(int) ot_font->v_metrics.get_advance (glyph));
427 hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED,
429 hb_codepoint_t glyph,
430 hb_glyph_extents_t *extents,
431 void *user_data HB_UNUSED)
433 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
434 bool ret = ot_font->glyf->get_extents (glyph, extents);
435 extents->x_bearing = font->em_scale_x (extents->x_bearing);
436 extents->y_bearing = font->em_scale_y (extents->y_bearing);
437 extents->width = font->em_scale_x (extents->width);
438 extents->height = font->em_scale_y (extents->height);
443 hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED,
445 hb_font_extents_t *metrics,
446 void *user_data HB_UNUSED)
448 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
449 metrics->ascender = font->em_scale_y (ot_font->h_metrics.ascender);
450 metrics->descender = font->em_scale_y (ot_font->h_metrics.descender);
451 metrics->line_gap = font->em_scale_y (ot_font->h_metrics.line_gap);
456 hb_ot_get_font_v_extents (hb_font_t *font HB_UNUSED,
458 hb_font_extents_t *metrics,
459 void *user_data HB_UNUSED)
461 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
462 metrics->ascender = font->em_scale_x (ot_font->v_metrics.ascender);
463 metrics->descender = font->em_scale_x (ot_font->v_metrics.descender);
464 metrics->line_gap = font->em_scale_x (ot_font->v_metrics.line_gap);
468 static hb_font_funcs_t *static_ot_funcs = NULL;
472 void free_static_ot_funcs (void)
474 hb_font_funcs_destroy (static_ot_funcs);
478 static hb_font_funcs_t *
479 _hb_ot_get_font_funcs (void)
482 hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs);
484 if (unlikely (!funcs))
486 funcs = hb_font_funcs_create ();
488 hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, NULL, NULL);
489 hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, NULL, NULL);
490 hb_font_funcs_set_nominal_glyph_func (funcs, hb_ot_get_nominal_glyph, NULL, NULL);
491 hb_font_funcs_set_variation_glyph_func (funcs, hb_ot_get_variation_glyph, NULL, NULL);
492 hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ot_get_glyph_h_advance, NULL, NULL);
493 hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ot_get_glyph_v_advance, NULL, NULL);
494 //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ot_get_glyph_h_origin, NULL, NULL);
495 //hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ot_get_glyph_v_origin, NULL, NULL);
496 //hb_font_funcs_set_glyph_h_kerning_func (funcs, hb_ot_get_glyph_h_kerning, NULL, NULL); TODO
497 //hb_font_funcs_set_glyph_v_kerning_func (funcs, hb_ot_get_glyph_v_kerning, NULL, NULL);
498 hb_font_funcs_set_glyph_extents_func (funcs, hb_ot_get_glyph_extents, NULL, NULL);
499 //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, NULL, NULL); TODO
500 //hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, NULL, NULL); TODO
501 //hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, NULL, NULL); TODO
503 hb_font_funcs_make_immutable (funcs);
505 if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, NULL, funcs)) {
506 hb_font_funcs_destroy (funcs);
511 atexit (free_static_ot_funcs); /* First person registers atexit() callback. */
520 * hb_ot_font_set_funcs:
525 hb_ot_font_set_funcs (hb_font_t *font)
527 hb_ot_font_t *ot_font = _hb_ot_font_create (font->face);
528 if (unlikely (!ot_font))
531 hb_font_set_funcs (font,
532 _hb_ot_get_font_funcs (),
534 (hb_destroy_func_t) _hb_ot_font_destroy);