• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2024  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  * Author(s): Behdad Esfahbod
25  */
26 
27 #include "hb.hh"
28 
29 #ifdef HAVE_CORETEXT
30 
31 #include "hb-coretext.h"
32 
33 #include "hb-draw.hh"
34 #include "hb-font.hh"
35 #include "hb-machinery.hh"
36 
37 #if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1080) \
38     || (defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 60000) \
39     || (defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 90000)
40 #  define kCTFontOrientationDefault kCTFontDefaultOrientation
41 #  define kCTFontOrientationHorizontal kCTFontHorizontalOrientation
42 #  define kCTFontOrientationVertical kCTFontVerticalOrientation
43 #endif
44 
45 #define MAX_GLYPHS 64u
46 
47 static void
_hb_coretext_font_destroy(void * font_data)48 _hb_coretext_font_destroy (void *font_data)
49 {
50   CTFontRef ct_font = (CTFontRef) font_data;
51 
52   CFRelease (ct_font);
53 }
54 
55 static hb_bool_t
hb_coretext_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)56 hb_coretext_get_nominal_glyph (hb_font_t *font HB_UNUSED,
57 			       void *font_data,
58 			       hb_codepoint_t unicode,
59 			       hb_codepoint_t *glyph,
60 			       void *user_data HB_UNUSED)
61 {
62   CTFontRef ct_font = (CTFontRef) font_data;
63   UniChar ch = unicode;
64   CGGlyph cg_glyph;
65   if (CTFontGetGlyphsForCharacters (ct_font, &ch, &cg_glyph, 1))
66   {
67     *glyph = cg_glyph;
68     return true;
69   }
70   return false;
71 }
72 
73 static unsigned int
hb_coretext_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)74 hb_coretext_get_nominal_glyphs (hb_font_t *font HB_UNUSED,
75 				void *font_data,
76 				unsigned int count,
77 				const hb_codepoint_t *first_unicode,
78 				unsigned int unicode_stride,
79 				hb_codepoint_t *first_glyph,
80 				unsigned int glyph_stride,
81 				void *user_data HB_UNUSED)
82 {
83   CTFontRef ct_font = (CTFontRef) font_data;
84 
85   UniChar ch[MAX_GLYPHS];
86   CGGlyph cg_glyph[MAX_GLYPHS];
87   for (unsigned i = 0; i < count; i += MAX_GLYPHS)
88   {
89     unsigned c = (unsigned) hb_min ((int) MAX_GLYPHS, (int) count - (int) i);
90     for (unsigned j = 0; j < c; j++)
91     {
92       ch[j] = *first_unicode;
93       first_unicode = &StructAtOffset<const hb_codepoint_t> (first_unicode, unicode_stride);
94     }
95     CTFontGetGlyphsForCharacters (ct_font, ch, cg_glyph, c);
96     for (unsigned j = 0; j < c; j++)
97     {
98       *first_glyph = cg_glyph[j];
99       first_glyph = &StructAtOffset<hb_codepoint_t> (first_glyph, glyph_stride);
100     }
101   }
102 
103   return count;
104 }
105 
106 static hb_bool_t
hb_coretext_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)107 hb_coretext_get_variation_glyph (hb_font_t *font HB_UNUSED,
108 				 void *font_data,
109 				 hb_codepoint_t unicode,
110 				 hb_codepoint_t variation_selector,
111 				 hb_codepoint_t *glyph,
112 				 void *user_data HB_UNUSED)
113 {
114   CTFontRef ct_font = (CTFontRef) font_data;
115 
116   UniChar ch[2] = { unicode, variation_selector };
117   CGGlyph cg_glyph[2];
118 
119   CTFontGetGlyphsForCharacters (ct_font, ch, cg_glyph, 2);
120 
121   if (cg_glyph[1])
122     return false;
123 
124   *glyph = cg_glyph[0];
125   return true;
126 }
127 
128 static void
hb_coretext_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)129 hb_coretext_get_glyph_h_advances (hb_font_t* font, void* font_data,
130 				  unsigned count,
131 				  const hb_codepoint_t *first_glyph,
132 				  unsigned glyph_stride,
133 				  hb_position_t *first_advance,
134 				  unsigned advance_stride,
135 				  void *user_data HB_UNUSED)
136 {
137   CTFontRef ct_font = (CTFontRef) font_data;
138 
139   CGFloat ct_font_size = CTFontGetSize (ct_font);
140   CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
141 
142   CGGlyph cg_glyph[MAX_GLYPHS];
143   CGSize advances[MAX_GLYPHS];
144   for (unsigned i = 0; i < count; i += MAX_GLYPHS)
145   {
146     unsigned c = (unsigned) hb_min ((int) MAX_GLYPHS, (int) count - (int) i);
147     for (unsigned j = 0; j < c; j++)
148     {
149       cg_glyph[j] = *first_glyph;
150       first_glyph = &StructAtOffset<const hb_codepoint_t> (first_glyph, glyph_stride);
151     }
152     CTFontGetAdvancesForGlyphs (ct_font, kCTFontOrientationHorizontal, cg_glyph, advances, c);
153     for (unsigned j = 0; j < c; j++)
154     {
155       *first_advance = round (advances[j].width * x_mult);
156       first_advance = &StructAtOffset<hb_position_t> (first_advance, advance_stride);
157     }
158   }
159 }
160 
161 #ifndef HB_NO_VERTICAL
162 static void
hb_coretext_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)163 hb_coretext_get_glyph_v_advances (hb_font_t* font, void* font_data,
164 				  unsigned count,
165 				  const hb_codepoint_t *first_glyph,
166 				  unsigned glyph_stride,
167 				  hb_position_t *first_advance,
168 				  unsigned advance_stride,
169 				  void *user_data HB_UNUSED)
170 {
171   CTFontRef ct_font = (CTFontRef) font_data;
172 
173   CGFloat ct_font_size = CTFontGetSize (ct_font);
174   CGFloat y_mult = (CGFloat) -font->y_scale / ct_font_size;
175 
176   CGGlyph cg_glyph[MAX_GLYPHS];
177   CGSize advances[MAX_GLYPHS];
178   for (unsigned i = 0; i < count; i += MAX_GLYPHS)
179   {
180     unsigned c = (unsigned) hb_min ((int) MAX_GLYPHS, (int) count - (int) i);
181     for (unsigned j = 0; j < c; j++)
182     {
183       cg_glyph[j] = *first_glyph;
184       first_glyph = &StructAtOffset<const hb_codepoint_t> (first_glyph, glyph_stride);
185     }
186     CTFontGetAdvancesForGlyphs (ct_font, kCTFontOrientationVertical, cg_glyph, advances, c);
187     for (unsigned j = 0; j < c; j++)
188     {
189       *first_advance = round (advances[j].width * y_mult);
190       first_advance = &StructAtOffset<hb_position_t> (first_advance, advance_stride);
191     }
192   }
193 }
194 #endif
195 
196 #ifndef HB_NO_VERTICAL
197 static hb_bool_t
hb_coretext_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)198 hb_coretext_get_glyph_v_origin (hb_font_t *font,
199 				void *font_data,
200 				hb_codepoint_t glyph,
201 				hb_position_t *x,
202 				hb_position_t *y,
203 				void *user_data HB_UNUSED)
204 {
205   CTFontRef ct_font = (CTFontRef) font_data;
206 
207   CGFloat ct_font_size = CTFontGetSize (ct_font);
208   CGFloat x_mult = (CGFloat) -font->x_scale / ct_font_size;
209   CGFloat y_mult = (CGFloat) -font->y_scale / ct_font_size;
210 
211   const CGGlyph glyphs = glyph;
212   CGSize origin;
213   CTFontGetVerticalTranslationsForGlyphs (ct_font, &glyphs, &origin, 1);
214 
215   *x = round (x_mult * origin.width);
216   *y = round (y_mult * origin.height);
217 
218   return true;
219 }
220 #endif
221 
222 static hb_bool_t
hb_coretext_get_glyph_extents(hb_font_t * font,void * font_data,hb_codepoint_t glyph,hb_glyph_extents_t * extents,void * user_data HB_UNUSED)223 hb_coretext_get_glyph_extents (hb_font_t *font,
224 			       void *font_data,
225 			       hb_codepoint_t glyph,
226 			       hb_glyph_extents_t *extents,
227 			       void *user_data HB_UNUSED)
228 {
229   CTFontRef ct_font = (CTFontRef) font_data;
230 
231   CGFloat ct_font_size = CTFontGetSize (ct_font);
232   CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
233   CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
234 
235   CGGlyph glyphs[1] = { glyph };
236   CGRect bounds = ::CTFontGetBoundingRectsForGlyphs(ct_font,
237 						    kCTFontOrientationDefault, glyphs, NULL, 1);
238 
239   extents->x_bearing = round (bounds.origin.x * x_mult);
240   extents->y_bearing = round (bounds.origin.y * y_mult);
241   extents->width = round (bounds.size.width * x_mult);
242   extents->height = round (bounds.size.height * y_mult);
243 
244   return true;
245 }
246 
247 static hb_bool_t
hb_coretext_get_font_h_extents(hb_font_t * font,void * font_data,hb_font_extents_t * metrics,void * user_data HB_UNUSED)248 hb_coretext_get_font_h_extents (hb_font_t *font,
249 				void *font_data,
250 				hb_font_extents_t *metrics,
251 				void *user_data HB_UNUSED)
252 {
253   CTFontRef ct_font = (CTFontRef) font_data;
254   CGFloat ct_font_size = CTFontGetSize (ct_font);
255   CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
256 
257   metrics->ascender = round (CTFontGetAscent (ct_font) * y_mult);
258   metrics->descender = -round (CTFontGetDescent (ct_font) * y_mult);
259   metrics->line_gap = round (CTFontGetLeading (ct_font) * y_mult);
260 
261   return true;
262 }
263 
264 #ifndef HB_NO_DRAW
265 
266 static void
ct_apply_func(void * info,const CGPathElement * element)267 ct_apply_func (void *info, const CGPathElement *element)
268 {
269   hb_draw_session_t *draws = (hb_draw_session_t *) info;
270 
271   switch (element->type)
272   {
273   case kCGPathElementMoveToPoint:
274     draws->move_to (element->points[0].x, element->points[0].y);
275     break;
276   case kCGPathElementAddLineToPoint:
277     draws->line_to (element->points[0].x, element->points[0].y);
278     break;
279   case kCGPathElementAddQuadCurveToPoint:
280     draws->quadratic_to (element->points[0].x, element->points[0].y,
281 			 element->points[1].x, element->points[1].y);
282     break;
283   case kCGPathElementAddCurveToPoint:
284     draws->cubic_to (element->points[0].x, element->points[0].y,
285 		     element->points[1].x, element->points[1].y,
286 		     element->points[2].x, element->points[2].y);
287     break;
288   case kCGPathElementCloseSubpath:
289     draws->close_path ();
290     break;
291   }
292 }
293 
294 static void
hb_coretext_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)295 hb_coretext_draw_glyph (hb_font_t *font,
296 			void *font_data HB_UNUSED,
297 			hb_codepoint_t glyph,
298 			hb_draw_funcs_t *draw_funcs, void *draw_data,
299 			void *user_data)
300 {
301   CTFontRef ct_font = (CTFontRef) font_data;
302 
303   CGFloat ct_font_size = CTFontGetSize (ct_font);
304   CGFloat x_mult = (CGFloat) font->x_scale / ct_font_size;
305   CGFloat y_mult = (CGFloat) font->y_scale / ct_font_size;
306 
307   CGAffineTransform transform = CGAffineTransformIdentity;
308   transform = CGAffineTransformScale (transform, x_mult, y_mult);
309 
310   CGPathRef path = CTFontCreatePathForGlyph (ct_font, glyph, &transform);
311   if (!path)
312     return;
313 
314   hb_draw_session_t drawing = {draw_funcs, draw_data, font->slant};
315 
316   CGPathApply (path, &drawing, ct_apply_func);
317 
318   CFRelease (path);
319 }
320 #endif
321 
322 static hb_bool_t
hb_coretext_get_glyph_name(hb_font_t * font,void * font_data HB_UNUSED,hb_codepoint_t glyph,char * name,unsigned int size,void * user_data HB_UNUSED)323 hb_coretext_get_glyph_name (hb_font_t *font,
324 			    void *font_data HB_UNUSED,
325 			    hb_codepoint_t glyph,
326 			    char *name, unsigned int size,
327 			    void *user_data HB_UNUSED)
328 {
329   CGFontRef cg_font = (CGFontRef) (const void *) font->face->data.coretext;
330 
331   CGGlyph cg_glyph = glyph;
332   CFStringRef cf_name = CGFontCopyGlyphNameForGlyph (cg_font, cg_glyph);
333   if (!cf_name)
334     return false;
335 
336   CFIndex len = CFStringGetLength (cf_name);
337   if (len > size - 1)
338     len = size - 1;
339 
340   CFStringGetBytes (cf_name, CFRangeMake (0, len),
341 		    kCFStringEncodingUTF8, 0, false,
342 		    (UInt8 *) name, size, &len);
343 
344   name[len] = '\0';
345   return true;
346 }
347 
348 static hb_bool_t
hb_coretext_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)349 hb_coretext_get_glyph_from_name (hb_font_t *font HB_UNUSED,
350 				 void *font_data,
351 				 const char *name, int len,
352 				 hb_codepoint_t *glyph,
353 				 void *user_data HB_UNUSED)
354 {
355   CTFontRef ct_font = (CTFontRef) font_data;
356 
357   if (len == -1)
358     len = strlen (name);
359 
360   CFStringRef cf_name = CFStringCreateWithBytes (kCFAllocatorDefault,
361 						 (const UInt8 *) name, len,
362 						 kCFStringEncodingUTF8, false);
363   CGGlyph cg_glyph = CTFontGetGlyphWithName (ct_font, cf_name);
364   *glyph = cg_glyph;
365 
366   CFRelease (cf_name);
367 
368   // TODO Return true for .notdef; hb-ft does that.
369 
370   return cg_glyph != 0;
371 }
372 
373 
374 static inline void free_static_coretext_funcs ();
375 
376 static struct hb_coretext_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_coretext_font_funcs_lazy_loader_t>
377 {
createhb_coretext_font_funcs_lazy_loader_t378   static hb_font_funcs_t *create ()
379   {
380     hb_font_funcs_t *funcs = hb_font_funcs_create ();
381 
382     hb_font_funcs_set_nominal_glyph_func (funcs, hb_coretext_get_nominal_glyph, nullptr, nullptr);
383     hb_font_funcs_set_nominal_glyphs_func (funcs, hb_coretext_get_nominal_glyphs, nullptr, nullptr);
384     hb_font_funcs_set_variation_glyph_func (funcs, hb_coretext_get_variation_glyph, nullptr, nullptr);
385 
386     hb_font_funcs_set_font_h_extents_func (funcs, hb_coretext_get_font_h_extents, nullptr, nullptr);
387     hb_font_funcs_set_glyph_h_advances_func (funcs, hb_coretext_get_glyph_h_advances, nullptr, nullptr);
388     //hb_font_funcs_set_glyph_h_origin_func (funcs, hb_coretext_get_glyph_h_origin, nullptr, nullptr);
389 
390 #ifndef HB_NO_VERTICAL
391     //hb_font_funcs_set_font_v_extents_func (funcs, hb_coretext_get_font_v_extents, nullptr, nullptr);
392     hb_font_funcs_set_glyph_v_advances_func (funcs, hb_coretext_get_glyph_v_advances, nullptr, nullptr);
393     hb_font_funcs_set_glyph_v_origin_func (funcs, hb_coretext_get_glyph_v_origin, nullptr, nullptr);
394 #endif
395 
396 #ifndef HB_NO_DRAW
397     hb_font_funcs_set_draw_glyph_func (funcs, hb_coretext_draw_glyph, nullptr, nullptr);
398 #endif
399 
400     hb_font_funcs_set_glyph_extents_func (funcs, hb_coretext_get_glyph_extents, nullptr, nullptr);
401 
402 #ifndef HB_NO_OT_FONT_GLYPH_NAMES
403     hb_font_funcs_set_glyph_name_func (funcs, hb_coretext_get_glyph_name, nullptr, nullptr);
404     hb_font_funcs_set_glyph_from_name_func (funcs, hb_coretext_get_glyph_from_name, nullptr, nullptr);
405 #endif
406 
407     hb_font_funcs_make_immutable (funcs);
408 
409     hb_atexit (free_static_coretext_funcs);
410 
411     return funcs;
412   }
413 } static_coretext_funcs;
414 
415 static inline
free_static_coretext_funcs()416 void free_static_coretext_funcs ()
417 {
418   static_coretext_funcs.free_instance ();
419 }
420 
421 static hb_font_funcs_t *
_hb_coretext_get_font_funcs()422 _hb_coretext_get_font_funcs ()
423 {
424   return static_coretext_funcs.get_unconst ();
425 }
426 
427 
428 /**
429  * hb_coretext_font_set_funcs:
430  * @font: #hb_font_t to work upon
431  *
432  * Configures the font-functions structure of the specified
433  * #hb_font_t font object to use CoreText font functions.
434  *
435  * In particular, you can use this function to configure an
436  * existing #hb_face_t face object for use with CoreText font
437  * functions even if that #hb_face_t face object was initially
438  * created with hb_face_create(), and therefore was not
439  * initially configured to use CoreText font functions.
440  *
441  * An #hb_font_t object created with hb_coretext_font_create()
442  * is preconfigured for CoreText font functions and does not
443  * require this function to be used.
444  *
445  * <note>Note: Internally, this function creates a CTFont.
446 * </note>
447  *
448  * Since: 10.1.0
449  **/
450 void
hb_coretext_font_set_funcs(hb_font_t * font)451 hb_coretext_font_set_funcs (hb_font_t *font)
452 {
453   CTFontRef ct_font = hb_coretext_font_get_ct_font (font);
454   if (unlikely (!ct_font))
455     return;
456 
457   hb_font_set_funcs (font,
458 		     _hb_coretext_get_font_funcs (),
459 		     (void *) CFRetain (ct_font),
460 		     _hb_coretext_font_destroy);
461 }
462 
463 #undef MAX_GLYPHS
464 
465 #endif
466