39fc849a9b0b82dd080572dc95e2fbd1ac20fffd
[platform/upstream/harfbuzz.git] / src / hb-ot-font.cc
1 /*
2  * Copyright © 2011,2014  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
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.
11  *
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
16  * DAMAGE.
17  *
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.
23  *
24  * Google Author(s): Behdad Esfahbod, Roozbeh Pournader
25  */
26
27 #include "hb-private.hh"
28
29 #include "hb-ot.h"
30
31 #include "hb-font-private.hh"
32
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"
40
41
42 struct hb_ot_face_metrics_accelerator_t
43 {
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;
50
51   const OT::_mtx *table;
52   hb_blob_t *blob;
53
54   inline void init (hb_face_t *face,
55                     hb_tag_t _hea_tag,
56                     hb_tag_t _mtx_tag,
57                     hb_tag_t os2_tag)
58   {
59     this->default_advance = face->get_upem ();
60
61     bool got_font_extents = false;
62     if (os2_tag)
63     {
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))
68       {
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;
73       }
74       hb_blob_destroy (os2_blob);
75     }
76
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)
81     {
82       this->ascender = _hea->ascender;
83       this->descender = _hea->descender;
84       this->line_gap = _hea->lineGap;
85     }
86     hb_blob_destroy (_hea_blob);
87
88     this->blob = OT::Sanitizer<OT::_mtx>::sanitize (face->reference_table (_mtx_tag));
89
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;
95
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))
99     {
100       this->num_metrics = this->num_advances = 0;
101       hb_blob_destroy (this->blob);
102       this->blob = hb_blob_get_empty ();
103     }
104     this->table = OT::Sanitizer<OT::_mtx>::lock_instance (this->blob);
105   }
106
107   inline void fini (void)
108   {
109     hb_blob_destroy (this->blob);
110   }
111
112   inline unsigned int get_advance (hb_codepoint_t glyph) const
113   {
114     if (unlikely (glyph >= this->num_metrics))
115     {
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)
120         return 0;
121       else
122         return this->default_advance;
123     }
124
125     if (glyph >= this->num_advances)
126       glyph = this->num_advances - 1;
127
128     return this->table->longMetric[glyph].advance;
129   }
130 };
131
132 struct hb_ot_face_glyf_accelerator_t
133 {
134   bool short_offset;
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;
141
142   inline void init (hb_face_t *face)
143   {
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)
147     {
148       /* Unknown format.  Leave num_glyphs=0, that takes care of disabling us. */
149       hb_blob_destroy (head_blob);
150       return;
151     }
152     this->short_offset = 0 == head->indexToLocFormat;
153     hb_blob_destroy (head_blob);
154
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);
159
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);
162   }
163
164   inline void fini (void)
165   {
166     hb_blob_destroy (this->loca_blob);
167     hb_blob_destroy (this->glyf_blob);
168   }
169
170   inline bool get_extents (hb_codepoint_t glyph,
171                            hb_glyph_extents_t *extents) const
172   {
173     if (unlikely (glyph >= this->num_glyphs))
174       return false;
175
176     unsigned int start_offset, end_offset;
177     if (this->short_offset)
178     {
179       start_offset = 2 * this->loca->u.shortsZ[glyph];
180       end_offset   = 2 * this->loca->u.shortsZ[glyph + 1];
181     }
182     else
183     {
184       start_offset = this->loca->u.longsZ[glyph];
185       end_offset   = this->loca->u.longsZ[glyph + 1];
186     }
187
188     if (start_offset > end_offset || end_offset > this->glyf_len)
189       return false;
190
191     if (end_offset - start_offset < OT::glyfGlyphHeader::static_size)
192       return true; /* Empty glyph; zero extents. */
193
194     const OT::glyfGlyphHeader &glyph_header = OT::StructAtOffset<OT::glyfGlyphHeader> (this->glyf, start_offset);
195
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;
200
201     return true;
202   }
203 };
204
205 typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
206                                           hb_codepoint_t codepoint,
207                                           hb_codepoint_t *glyph);
208
209 template <typename Type>
210 static inline bool get_glyph_from (const void *obj,
211                                    hb_codepoint_t codepoint,
212                                    hb_codepoint_t *glyph)
213 {
214   const Type *typed_obj = (const Type *) obj;
215   return typed_obj->get_glyph (codepoint, glyph);
216 }
217
218 struct hb_ot_face_cmap_accelerator_t
219 {
220   hb_cmap_get_glyph_func_t get_glyph_func;
221   const void *get_glyph_data;
222   OT::CmapSubtableFormat4::accelerator_t format4_accel;
223
224   const OT::CmapSubtableFormat14 *uvs_table;
225   hb_blob_t *blob;
226
227   inline void init (hb_face_t *face)
228   {
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;
233
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);
245     /* Meh. */
246     if (!subtable) subtable = &OT::Null(OT::CmapSubtable);
247
248     /* UVS subtable. */
249     if (!subtable_uvs)
250     {
251       const OT::CmapSubtable *st = cmap->find_subtable (0, 5);
252       if (st && st->u.format == 14)
253         subtable_uvs = &st->u.format14;
254     }
255     /* Meh. */
256     if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtableFormat14);
257
258     this->uvs_table = subtable_uvs;
259
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;
265     case  4:
266       {
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;
270       }
271       break;
272     }
273   }
274
275   inline void fini (void)
276   {
277     hb_blob_destroy (this->blob);
278   }
279
280   inline bool get_nominal_glyph (hb_codepoint_t  unicode,
281                                  hb_codepoint_t *glyph) const
282   {
283     return this->get_glyph_func (this->get_glyph_data, unicode, glyph);
284   }
285
286   inline bool get_variation_glyph (hb_codepoint_t  unicode,
287                                    hb_codepoint_t  variation_selector,
288                                    hb_codepoint_t *glyph) const
289   {
290     switch (this->uvs_table->get_glyph_variant (unicode,
291                                                 variation_selector,
292                                                 glyph))
293     {
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;
297     }
298
299     return get_nominal_glyph (unicode, glyph);
300   }
301 };
302
303 template <typename T>
304 struct hb_lazy_loader_t
305 {
306   inline void init (hb_face_t *face_)
307   {
308     face = face_;
309     instance = NULL;
310   }
311
312   inline void fini (void)
313   {
314     if (instance && instance != &OT::Null(T))
315     {
316       instance->fini();
317       free (instance);
318     }
319   }
320
321   inline const T* operator-> (void) const
322   {
323   retry:
324     T *p = (T *) hb_atomic_ptr_get (&instance);
325     if (unlikely (!p))
326     {
327       p = (T *) calloc (1, sizeof (T));
328       if (unlikely (!p))
329         return &OT::Null(T);
330       p->init (face);
331       if (unlikely (!hb_atomic_ptr_cmpexch (const_cast<T **>(&instance), NULL, p)))
332       {
333         p->fini ();
334         goto retry;
335       }
336     }
337     return p;
338   }
339
340   private:
341   hb_face_t *face;
342   T *instance;
343 };
344
345 struct hb_ot_font_t
346 {
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;
351 };
352
353
354 static hb_ot_font_t *
355 _hb_ot_font_create (hb_face_t *face)
356 {
357   hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t));
358
359   if (unlikely (!ot_font))
360     return NULL;
361
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);
366
367   return ot_font;
368 }
369
370 static void
371 _hb_ot_font_destroy (hb_ot_font_t *ot_font)
372 {
373   ot_font->cmap.fini ();
374   ot_font->h_metrics.fini ();
375   ot_font->v_metrics.fini ();
376   ot_font->glyf.fini ();
377
378   free (ot_font);
379 }
380
381
382 static hb_bool_t
383 hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED,
384                          void *font_data,
385                          hb_codepoint_t unicode,
386                          hb_codepoint_t *glyph,
387                          void *user_data HB_UNUSED)
388
389 {
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);
392 }
393
394 static hb_bool_t
395 hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED,
396                            void *font_data,
397                            hb_codepoint_t unicode,
398                            hb_codepoint_t variation_selector,
399                            hb_codepoint_t *glyph,
400                            void *user_data HB_UNUSED)
401 {
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);
404 }
405
406 static hb_position_t
407 hb_ot_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
408                            void *font_data,
409                            hb_codepoint_t glyph,
410                            void *user_data HB_UNUSED)
411 {
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));
414 }
415
416 static hb_position_t
417 hb_ot_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
418                            void *font_data,
419                            hb_codepoint_t glyph,
420                            void *user_data HB_UNUSED)
421 {
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));
424 }
425
426 static hb_bool_t
427 hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED,
428                          void *font_data,
429                          hb_codepoint_t glyph,
430                          hb_glyph_extents_t *extents,
431                          void *user_data HB_UNUSED)
432 {
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);
439   return ret;
440 }
441
442 static hb_bool_t
443 hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED,
444                           void *font_data,
445                           hb_font_extents_t *metrics,
446                           void *user_data HB_UNUSED)
447 {
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);
452   return true;
453 }
454
455 static hb_bool_t
456 hb_ot_get_font_v_extents (hb_font_t *font HB_UNUSED,
457                           void *font_data,
458                           hb_font_extents_t *metrics,
459                           void *user_data HB_UNUSED)
460 {
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);
465   return true;
466 }
467
468 static hb_font_funcs_t *static_ot_funcs = NULL;
469
470 #ifdef HB_USE_ATEXIT
471 static
472 void free_static_ot_funcs (void)
473 {
474   hb_font_funcs_destroy (static_ot_funcs);
475 }
476 #endif
477
478 static hb_font_funcs_t *
479 _hb_ot_get_font_funcs (void)
480 {
481 retry:
482   hb_font_funcs_t *funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs);
483
484   if (unlikely (!funcs))
485   {
486     funcs = hb_font_funcs_create ();
487
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
502
503     hb_font_funcs_make_immutable (funcs);
504
505     if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, NULL, funcs)) {
506       hb_font_funcs_destroy (funcs);
507       goto retry;
508     }
509
510 #ifdef HB_USE_ATEXIT
511     atexit (free_static_ot_funcs); /* First person registers atexit() callback. */
512 #endif
513   };
514
515   return funcs;
516 }
517
518
519 /**
520  * hb_ot_font_set_funcs:
521  *
522  * Since: 0.9.28
523  **/
524 void
525 hb_ot_font_set_funcs (hb_font_t *font)
526 {
527   hb_ot_font_t *ot_font = _hb_ot_font_create (font->face);
528   if (unlikely (!ot_font))
529     return;
530
531   hb_font_set_funcs (font,
532                      _hb_ot_get_font_funcs (),
533                      ot_font,
534                      (hb_destroy_func_t) _hb_ot_font_destroy);
535 }