xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-ot-font.cc (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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.hh"
28 
29 #ifndef HB_NO_OT_FONT
30 
31 #include "hb-ot.h"
32 
33 #include "hb-cache.hh"
34 #include "hb-font.hh"
35 #include "hb-machinery.hh"
36 #include "hb-ot-face.hh"
37 #include "hb-outline.hh"
38 
39 #include "hb-ot-cmap-table.hh"
40 #include "hb-ot-glyf-table.hh"
41 #include "hb-ot-cff2-table.hh"
42 #include "hb-ot-cff1-table.hh"
43 #include "hb-ot-hmtx-table.hh"
44 #include "hb-ot-post-table.hh"
45 #include "hb-ot-stat-table.hh" // Just so we compile it; unused otherwise.
46 #include "hb-ot-var-varc-table.hh"
47 #include "hb-ot-vorg-table.hh"
48 #include "OT/Color/CBDT/CBDT.hh"
49 #include "OT/Color/COLR/COLR.hh"
50 #include "OT/Color/sbix/sbix.hh"
51 #include "OT/Color/svg/svg.hh"
52 
53 
54 /**
55  * SECTION:hb-ot-font
56  * @title: hb-ot-font
57  * @short_description: OpenType font implementation
58  * @include: hb-ot.h
59  *
60  * Functions for using OpenType fonts with hb_shape().  Note that fonts returned
61  * by hb_font_create() default to using these functions, so most clients would
62  * never need to call these functions directly.
63  **/
64 
65 using hb_ot_font_cmap_cache_t    = hb_cache_t<21, 16, 8, true>;
66 using hb_ot_font_advance_cache_t = hb_cache_t<24, 16, 8, true>;
67 
68 #ifndef HB_NO_OT_FONT_CMAP_CACHE
69 static hb_user_data_key_t hb_ot_font_cmap_cache_user_data_key;
70 #endif
71 
72 struct hb_ot_font_t
73 {
74   const hb_ot_face_t *ot_face;
75 
76 #ifndef HB_NO_OT_FONT_CMAP_CACHE
77   hb_ot_font_cmap_cache_t *cmap_cache;
78 #endif
79 
80   /* h_advance caching */
81   mutable hb_atomic_int_t cached_coords_serial;
82   mutable hb_atomic_ptr_t<hb_ot_font_advance_cache_t> advance_cache;
83 };
84 
85 static hb_ot_font_t *
_hb_ot_font_create(hb_font_t * font)86 _hb_ot_font_create (hb_font_t *font)
87 {
88   hb_ot_font_t *ot_font = (hb_ot_font_t *) hb_calloc (1, sizeof (hb_ot_font_t));
89   if (unlikely (!ot_font))
90     return nullptr;
91 
92   ot_font->ot_face = &font->face->table;
93 
94 #ifndef HB_NO_OT_FONT_CMAP_CACHE
95   // retry:
96   auto *cmap_cache  = (hb_ot_font_cmap_cache_t *) hb_face_get_user_data (font->face,
97 									 &hb_ot_font_cmap_cache_user_data_key);
98   if (!cmap_cache)
99   {
100     cmap_cache = (hb_ot_font_cmap_cache_t *) hb_malloc (sizeof (hb_ot_font_cmap_cache_t));
101     if (unlikely (!cmap_cache)) goto out;
102     new (cmap_cache) hb_ot_font_cmap_cache_t ();
103     if (unlikely (!hb_face_set_user_data (font->face,
104 					  &hb_ot_font_cmap_cache_user_data_key,
105 					  cmap_cache,
106 					  hb_free,
107 					  false)))
108     {
109       hb_free (cmap_cache);
110       cmap_cache = nullptr;
111       /* Normally we would retry here, but that would
112        * infinite-loop if the face is the empty-face.
113        * Just let it go and this font will be uncached if it
114        * happened to collide with another thread creating the
115        * cache at the same time. */
116       // goto retry;
117     }
118   }
119   out:
120   ot_font->cmap_cache = cmap_cache;
121 #endif
122 
123   return ot_font;
124 }
125 
126 static void
_hb_ot_font_destroy(void * font_data)127 _hb_ot_font_destroy (void *font_data)
128 {
129   hb_ot_font_t *ot_font = (hb_ot_font_t *) font_data;
130 
131   auto *cache = ot_font->advance_cache.get_relaxed ();
132   hb_free (cache);
133 
134   hb_free (ot_font);
135 }
136 
137 static hb_bool_t
hb_ot_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)138 hb_ot_get_nominal_glyph (hb_font_t *font HB_UNUSED,
139 			 void *font_data,
140 			 hb_codepoint_t unicode,
141 			 hb_codepoint_t *glyph,
142 			 void *user_data HB_UNUSED)
143 {
144   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
145   const hb_ot_face_t *ot_face = ot_font->ot_face;
146   hb_ot_font_cmap_cache_t *cmap_cache = nullptr;
147 #ifndef HB_NO_OT_FONT_CMAP_CACHE
148   cmap_cache = ot_font->cmap_cache;
149 #endif
150   return ot_face->cmap->get_nominal_glyph (unicode, glyph, cmap_cache);
151 }
152 
153 static unsigned int
hb_ot_get_nominal_glyphs(hb_font_t * font HB_UNUSED,void * font_data,unsigned int count,const hb_codepoint_t * first_unicode,unsigned int unicode_stride,hb_codepoint_t * first_glyph,unsigned int glyph_stride,void * user_data HB_UNUSED)154 hb_ot_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
155 			  void *font_data,
156 			  unsigned int count,
157 			  const hb_codepoint_t *first_unicode,
158 			  unsigned int unicode_stride,
159 			  hb_codepoint_t *first_glyph,
160 			  unsigned int glyph_stride,
161 			  void *user_data HB_UNUSED)
162 {
163   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
164   const hb_ot_face_t *ot_face = ot_font->ot_face;
165   hb_ot_font_cmap_cache_t *cmap_cache = nullptr;
166 #ifndef HB_NO_OT_FONT_CMAP_CACHE
167   cmap_cache = ot_font->cmap_cache;
168 #endif
169   return ot_face->cmap->get_nominal_glyphs (count,
170 					    first_unicode, unicode_stride,
171 					    first_glyph, glyph_stride,
172 					    cmap_cache);
173 }
174 
175 static hb_bool_t
hb_ot_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)176 hb_ot_get_variation_glyph (hb_font_t *font HB_UNUSED,
177 			   void *font_data,
178 			   hb_codepoint_t unicode,
179 			   hb_codepoint_t variation_selector,
180 			   hb_codepoint_t *glyph,
181 			   void *user_data HB_UNUSED)
182 {
183   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
184   const hb_ot_face_t *ot_face = ot_font->ot_face;
185   hb_ot_font_cmap_cache_t *cmap_cache = nullptr;
186 #ifndef HB_NO_OT_FONT_CMAP_CACHE
187   cmap_cache = ot_font->cmap_cache;
188 #endif
189   return ot_face->cmap->get_variation_glyph (unicode,
190                                              variation_selector, glyph,
191                                              cmap_cache);
192 }
193 
194 static void
hb_ot_get_glyph_h_advances(hb_font_t * font,void * font_data,unsigned count,const hb_codepoint_t * first_glyph,unsigned glyph_stride,hb_position_t * first_advance,unsigned advance_stride,void * user_data HB_UNUSED)195 hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
196 			    unsigned count,
197 			    const hb_codepoint_t *first_glyph,
198 			    unsigned glyph_stride,
199 			    hb_position_t *first_advance,
200 			    unsigned advance_stride,
201 			    void *user_data HB_UNUSED)
202 {
203 
204   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
205   const hb_ot_face_t *ot_face = ot_font->ot_face;
206   const OT::hmtx_accelerator_t &hmtx = *ot_face->hmtx;
207 
208   hb_position_t *orig_first_advance = first_advance;
209 
210 #if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_ADVANCE_CACHE)
211   const OT::HVAR &HVAR = *hmtx.var_table;
212   const OT::ItemVariationStore &varStore = &HVAR + HVAR.varStore;
213   OT::ItemVariationStore::cache_t *varStore_cache = font->num_coords * count >= 128 ? varStore.create_cache () : nullptr;
214 
215   bool use_cache = font->num_coords;
216 #else
217   OT::ItemVariationStore::cache_t *varStore_cache = nullptr;
218   bool use_cache = false;
219 #endif
220 
221   hb_ot_font_advance_cache_t *cache = nullptr;
222   if (use_cache)
223   {
224   retry:
225     cache = ot_font->advance_cache.get_acquire ();
226     if (unlikely (!cache))
227     {
228       cache = (hb_ot_font_advance_cache_t *) hb_malloc (sizeof (hb_ot_font_advance_cache_t));
229       if (unlikely (!cache))
230       {
231 	use_cache = false;
232 	goto out;
233       }
234       new (cache) hb_ot_font_advance_cache_t;
235 
236       if (unlikely (!ot_font->advance_cache.cmpexch (nullptr, cache)))
237       {
238 	hb_free (cache);
239 	goto retry;
240       }
241       ot_font->cached_coords_serial.set_release (font->serial_coords);
242     }
243   }
244   out:
245 
246   if (!use_cache)
247   {
248     for (unsigned int i = 0; i < count; i++)
249     {
250       *first_advance = font->em_scale_x (hmtx.get_advance_with_var_unscaled (*first_glyph, font, varStore_cache));
251       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
252       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
253     }
254   }
255   else
256   { /* Use cache. */
257     if (ot_font->cached_coords_serial.get_acquire () != (int) font->serial_coords)
258     {
259       ot_font->advance_cache->clear ();
260       ot_font->cached_coords_serial.set_release (font->serial_coords);
261     }
262 
263     for (unsigned int i = 0; i < count; i++)
264     {
265       hb_position_t v;
266       unsigned cv;
267       if (ot_font->advance_cache->get (*first_glyph, &cv))
268 	v = cv;
269       else
270       {
271         v = hmtx.get_advance_with_var_unscaled (*first_glyph, font, varStore_cache);
272 	ot_font->advance_cache->set (*first_glyph, v);
273       }
274       *first_advance = font->em_scale_x (v);
275       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
276       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
277     }
278   }
279 
280 #if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_ADVANCE_CACHE)
281   OT::ItemVariationStore::destroy_cache (varStore_cache);
282 #endif
283 
284   if (font->x_strength && !font->embolden_in_place)
285   {
286     /* Emboldening. */
287     hb_position_t x_strength = font->x_scale >= 0 ? font->x_strength : -font->x_strength;
288     first_advance = orig_first_advance;
289     for (unsigned int i = 0; i < count; i++)
290     {
291       *first_advance += *first_advance ? x_strength : 0;
292       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
293     }
294   }
295 }
296 
297 #ifndef HB_NO_VERTICAL
298 static void
hb_ot_get_glyph_v_advances(hb_font_t * font,void * font_data,unsigned count,const hb_codepoint_t * first_glyph,unsigned glyph_stride,hb_position_t * first_advance,unsigned advance_stride,void * user_data HB_UNUSED)299 hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data,
300 			    unsigned count,
301 			    const hb_codepoint_t *first_glyph,
302 			    unsigned glyph_stride,
303 			    hb_position_t *first_advance,
304 			    unsigned advance_stride,
305 			    void *user_data HB_UNUSED)
306 {
307   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
308   const hb_ot_face_t *ot_face = ot_font->ot_face;
309   const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
310 
311   hb_position_t *orig_first_advance = first_advance;
312 
313   if (vmtx.has_data ())
314   {
315 #if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_ADVANCE_CACHE)
316     const OT::VVAR &VVAR = *vmtx.var_table;
317     const OT::ItemVariationStore &varStore = &VVAR + VVAR.varStore;
318     OT::ItemVariationStore::cache_t *varStore_cache = font->num_coords ? varStore.create_cache () : nullptr;
319 #else
320     OT::ItemVariationStore::cache_t *varStore_cache = nullptr;
321 #endif
322 
323     for (unsigned int i = 0; i < count; i++)
324     {
325       *first_advance = font->em_scale_y (-(int) vmtx.get_advance_with_var_unscaled (*first_glyph, font, varStore_cache));
326       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
327       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
328     }
329 
330 #if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_ADVANCE_CACHE)
331     OT::ItemVariationStore::destroy_cache (varStore_cache);
332 #endif
333   }
334   else
335   {
336     hb_font_extents_t font_extents;
337     font->get_h_extents_with_fallback (&font_extents);
338     hb_position_t advance = -(font_extents.ascender - font_extents.descender);
339 
340     for (unsigned int i = 0; i < count; i++)
341     {
342       *first_advance = advance;
343       first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
344       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
345     }
346   }
347 
348   if (font->y_strength && !font->embolden_in_place)
349   {
350     /* Emboldening. */
351     hb_position_t y_strength = font->y_scale >= 0 ? font->y_strength : -font->y_strength;
352     first_advance = orig_first_advance;
353     for (unsigned int i = 0; i < count; i++)
354     {
355       *first_advance += *first_advance ? y_strength : 0;
356       first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
357     }
358   }
359 }
360 #endif
361 
362 #ifndef HB_NO_VERTICAL
363 static hb_bool_t
hb_ot_get_glyph_v_origin(hb_font_t * font,void * font_data,hb_codepoint_t glyph,hb_position_t * x,hb_position_t * y,void * user_data HB_UNUSED)364 hb_ot_get_glyph_v_origin (hb_font_t *font,
365 			  void *font_data,
366 			  hb_codepoint_t glyph,
367 			  hb_position_t *x,
368 			  hb_position_t *y,
369 			  void *user_data HB_UNUSED)
370 {
371   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
372   const hb_ot_face_t *ot_face = ot_font->ot_face;
373 
374   *x = font->get_glyph_h_advance (glyph) / 2;
375 
376   const OT::VORG &VORG = *ot_face->VORG;
377   if (VORG.has_data ())
378   {
379     float delta = 0;
380 
381 #ifndef HB_NO_VAR
382     const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
383     const OT::VVAR &VVAR = *vmtx.var_table;
384     if (font->num_coords)
385       VVAR.get_vorg_delta_unscaled (glyph,
386 				    font->coords, font->num_coords,
387 				    &delta);
388 #endif
389 
390     *y = font->em_scalef_y (VORG.get_y_origin (glyph) + delta);
391     return true;
392   }
393 
394   hb_glyph_extents_t extents = {0};
395   if (ot_face->glyf->get_extents (font, glyph, &extents))
396   {
397     const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
398     int tsb = 0;
399     if (vmtx.get_leading_bearing_with_var_unscaled (font, glyph, &tsb))
400     {
401       *y = extents.y_bearing + font->em_scale_y (tsb);
402       return true;
403     }
404 
405     hb_font_extents_t font_extents;
406     font->get_h_extents_with_fallback (&font_extents);
407     hb_position_t advance = font_extents.ascender - font_extents.descender;
408     int diff = advance - -extents.height;
409     *y = extents.y_bearing + (diff >> 1);
410     return true;
411   }
412 
413   hb_font_extents_t font_extents;
414   font->get_h_extents_with_fallback (&font_extents);
415   *y = font_extents.ascender;
416 
417   return true;
418 }
419 #endif
420 
421 static hb_bool_t
hb_ot_get_glyph_extents(hb_font_t * font,void * font_data,hb_codepoint_t glyph,hb_glyph_extents_t * extents,void * user_data HB_UNUSED)422 hb_ot_get_glyph_extents (hb_font_t *font,
423 			 void *font_data,
424 			 hb_codepoint_t glyph,
425 			 hb_glyph_extents_t *extents,
426 			 void *user_data HB_UNUSED)
427 {
428   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
429   const hb_ot_face_t *ot_face = ot_font->ot_face;
430 
431 #if !defined(HB_NO_OT_FONT_BITMAP) && !defined(HB_NO_COLOR)
432   if (ot_face->sbix->get_extents (font, glyph, extents)) return true;
433   if (ot_face->CBDT->get_extents (font, glyph, extents)) return true;
434 #endif
435 #if !defined(HB_NO_COLOR) && !defined(HB_NO_PAINT)
436   if (ot_face->COLR->get_extents (font, glyph, extents)) return true;
437 #endif
438   if (ot_face->glyf->get_extents (font, glyph, extents)) return true;
439 #ifndef HB_NO_OT_FONT_CFF
440   if (ot_face->cff2->get_extents (font, glyph, extents)) return true;
441   if (ot_face->cff1->get_extents (font, glyph, extents)) return true;
442 #endif
443 
444   return false;
445 }
446 
447 #ifndef HB_NO_OT_FONT_GLYPH_NAMES
448 static hb_bool_t
hb_ot_get_glyph_name(hb_font_t * font HB_UNUSED,void * font_data,hb_codepoint_t glyph,char * name,unsigned int size,void * user_data HB_UNUSED)449 hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED,
450 		      void *font_data,
451 		      hb_codepoint_t glyph,
452 		      char *name, unsigned int size,
453 		      void *user_data HB_UNUSED)
454 {
455   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
456   const hb_ot_face_t *ot_face = ot_font->ot_face;
457 
458   if (ot_face->post->get_glyph_name (glyph, name, size)) return true;
459 #ifndef HB_NO_OT_FONT_CFF
460   if (ot_face->cff1->get_glyph_name (glyph, name, size)) return true;
461 #endif
462   return false;
463 }
464 static hb_bool_t
hb_ot_get_glyph_from_name(hb_font_t * font HB_UNUSED,void * font_data,const char * name,int len,hb_codepoint_t * glyph,void * user_data HB_UNUSED)465 hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED,
466 			   void *font_data,
467 			   const char *name, int len,
468 			   hb_codepoint_t *glyph,
469 			   void *user_data HB_UNUSED)
470 {
471   const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
472   const hb_ot_face_t *ot_face = ot_font->ot_face;
473 
474   if (ot_face->post->get_glyph_from_name (name, len, glyph)) return true;
475 #ifndef HB_NO_OT_FONT_CFF
476     if (ot_face->cff1->get_glyph_from_name (name, len, glyph)) return true;
477 #endif
478   return false;
479 }
480 #endif
481 
482 static hb_bool_t
hb_ot_get_font_h_extents(hb_font_t * font,void * font_data HB_UNUSED,hb_font_extents_t * metrics,void * user_data HB_UNUSED)483 hb_ot_get_font_h_extents (hb_font_t *font,
484 			  void *font_data HB_UNUSED,
485 			  hb_font_extents_t *metrics,
486 			  void *user_data HB_UNUSED)
487 {
488   bool ret = _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, &metrics->ascender) &&
489 	     _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_HORIZONTAL_DESCENDER, &metrics->descender) &&
490 	     _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_HORIZONTAL_LINE_GAP, &metrics->line_gap);
491 
492   /* Embolden */
493   int y_shift = font->y_strength;
494   if (font->y_scale < 0) y_shift = -y_shift;
495   metrics->ascender += y_shift;
496 
497   return ret;
498 }
499 
500 #ifndef HB_NO_VERTICAL
501 static hb_bool_t
hb_ot_get_font_v_extents(hb_font_t * font,void * font_data HB_UNUSED,hb_font_extents_t * metrics,void * user_data HB_UNUSED)502 hb_ot_get_font_v_extents (hb_font_t *font,
503 			  void *font_data HB_UNUSED,
504 			  hb_font_extents_t *metrics,
505 			  void *user_data HB_UNUSED)
506 {
507   return _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_VERTICAL_ASCENDER, &metrics->ascender) &&
508 	 _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_VERTICAL_DESCENDER, &metrics->descender) &&
509 	 _hb_ot_metrics_get_position_common (font, HB_OT_METRICS_TAG_VERTICAL_LINE_GAP, &metrics->line_gap);
510 }
511 #endif
512 
513 #ifndef HB_NO_DRAW
514 static void
hb_ot_draw_glyph(hb_font_t * font,void * font_data HB_UNUSED,hb_codepoint_t glyph,hb_draw_funcs_t * draw_funcs,void * draw_data,void * user_data)515 hb_ot_draw_glyph (hb_font_t *font,
516 		  void *font_data HB_UNUSED,
517 		  hb_codepoint_t glyph,
518 		  hb_draw_funcs_t *draw_funcs, void *draw_data,
519 		  void *user_data)
520 {
521   bool embolden = font->x_strength || font->y_strength;
522   hb_outline_t outline;
523 
524   { // Need draw_session to be destructed before emboldening.
525     hb_draw_session_t draw_session (embolden ? hb_outline_recording_pen_get_funcs () : draw_funcs,
526 				    embolden ? &outline : draw_data, font->slant_xy);
527 #ifndef HB_NO_VAR_COMPOSITES
528     if (!font->face->table.VARC->get_path (font, glyph, draw_session))
529 #endif
530     // Keep the following in synch with VARC::get_path_at()
531     if (!font->face->table.glyf->get_path (font, glyph, draw_session))
532 #ifndef HB_NO_CFF
533     if (!font->face->table.cff2->get_path (font, glyph, draw_session))
534     if (!font->face->table.cff1->get_path (font, glyph, draw_session))
535 #endif
536     {}
537   }
538 
539   if (embolden)
540   {
541     float x_shift = font->embolden_in_place ? 0 : (float) font->x_strength / 2;
542     float y_shift = (float) font->y_strength / 2;
543     if (font->x_scale < 0) x_shift = -x_shift;
544     if (font->y_scale < 0) y_shift = -y_shift;
545     outline.embolden (font->x_strength, font->y_strength,
546 		      x_shift, y_shift);
547 
548     outline.replay (draw_funcs, draw_data);
549   }
550 }
551 #endif
552 
553 #ifndef HB_NO_PAINT
554 static void
hb_ot_paint_glyph(hb_font_t * font,void * font_data,hb_codepoint_t glyph,hb_paint_funcs_t * paint_funcs,void * paint_data,unsigned int palette,hb_color_t foreground,void * user_data)555 hb_ot_paint_glyph (hb_font_t *font,
556                    void *font_data,
557                    hb_codepoint_t glyph,
558                    hb_paint_funcs_t *paint_funcs, void *paint_data,
559                    unsigned int palette,
560                    hb_color_t foreground,
561                    void *user_data)
562 {
563 #ifndef HB_NO_COLOR
564   if (font->face->table.COLR->paint_glyph (font, glyph, paint_funcs, paint_data, palette, foreground)) return;
565   if (font->face->table.SVG->paint_glyph (font, glyph, paint_funcs, paint_data)) return;
566 #ifndef HB_NO_OT_FONT_BITMAP
567   if (font->face->table.CBDT->paint_glyph (font, glyph, paint_funcs, paint_data)) return;
568   if (font->face->table.sbix->paint_glyph (font, glyph, paint_funcs, paint_data)) return;
569 #endif
570 #endif
571 #ifndef HB_NO_VAR_COMPOSITES
572   if (font->face->table.VARC->paint_glyph (font, glyph, paint_funcs, paint_data, foreground)) return;
573 #endif
574   if (font->face->table.glyf->paint_glyph (font, glyph, paint_funcs, paint_data, foreground)) return;
575 #ifndef HB_NO_CFF
576   if (font->face->table.cff2->paint_glyph (font, glyph, paint_funcs, paint_data, foreground)) return;
577   if (font->face->table.cff1->paint_glyph (font, glyph, paint_funcs, paint_data, foreground)) return;
578 #endif
579 }
580 #endif
581 
582 static inline void free_static_ot_funcs ();
583 
584 static struct hb_ot_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ot_font_funcs_lazy_loader_t>
585 {
createhb_ot_font_funcs_lazy_loader_t586   static hb_font_funcs_t *create ()
587   {
588     hb_font_funcs_t *funcs = hb_font_funcs_create ();
589 
590     hb_font_funcs_set_nominal_glyph_func (funcs, hb_ot_get_nominal_glyph, nullptr, nullptr);
591     hb_font_funcs_set_nominal_glyphs_func (funcs, hb_ot_get_nominal_glyphs, nullptr, nullptr);
592     hb_font_funcs_set_variation_glyph_func (funcs, hb_ot_get_variation_glyph, nullptr, nullptr);
593 
594     hb_font_funcs_set_font_h_extents_func (funcs, hb_ot_get_font_h_extents, nullptr, nullptr);
595     hb_font_funcs_set_glyph_h_advances_func (funcs, hb_ot_get_glyph_h_advances, nullptr, nullptr);
596     //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_ot_get_glyph_h_origin, nullptr, nullptr);
597 
598 #ifndef HB_NO_VERTICAL
599     hb_font_funcs_set_font_v_extents_func (funcs, hb_ot_get_font_v_extents, nullptr, nullptr);
600     hb_font_funcs_set_glyph_v_advances_func (funcs, hb_ot_get_glyph_v_advances, nullptr, nullptr);
601     hb_font_funcs_set_glyph_v_origin_func (funcs, hb_ot_get_glyph_v_origin, nullptr, nullptr);
602 #endif
603 
604 #ifndef HB_NO_DRAW
605     hb_font_funcs_set_draw_glyph_func (funcs, hb_ot_draw_glyph, nullptr, nullptr);
606 #endif
607 
608 #ifndef HB_NO_PAINT
609     hb_font_funcs_set_paint_glyph_func (funcs, hb_ot_paint_glyph, nullptr, nullptr);
610 #endif
611 
612     hb_font_funcs_set_glyph_extents_func (funcs, hb_ot_get_glyph_extents, nullptr, nullptr);
613     //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr);
614 
615 #ifndef HB_NO_OT_FONT_GLYPH_NAMES
616     hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr);
617     hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr);
618 #endif
619 
620     hb_font_funcs_make_immutable (funcs);
621 
622     hb_atexit (free_static_ot_funcs);
623 
624     return funcs;
625   }
626 } static_ot_funcs;
627 
628 static inline
free_static_ot_funcs()629 void free_static_ot_funcs ()
630 {
631   static_ot_funcs.free_instance ();
632 }
633 
634 static hb_font_funcs_t *
_hb_ot_get_font_funcs()635 _hb_ot_get_font_funcs ()
636 {
637   return static_ot_funcs.get_unconst ();
638 }
639 
640 
641 /**
642  * hb_ot_font_set_funcs:
643  * @font: #hb_font_t to work upon
644  *
645  * Sets the font functions to use when working with @font to
646  * the HarfBuzz's native implementation. This is the default
647  * for fonts newly created.
648  *
649  * Since: 0.9.28
650  **/
651 void
hb_ot_font_set_funcs(hb_font_t * font)652 hb_ot_font_set_funcs (hb_font_t *font)
653 {
654   hb_ot_font_t *ot_font = _hb_ot_font_create (font);
655   if (unlikely (!ot_font))
656     return;
657 
658   hb_font_set_funcs (font,
659 		     _hb_ot_get_font_funcs (),
660 		     ot_font,
661 		     _hb_ot_font_destroy);
662 }
663 
664 #endif
665