fbb16a0b72f3421ba7490adf80807c37aa26b250
[platform/upstream/harfbuzz.git] / src / hb-font-private.hh
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2011  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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.
12  *
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.
18  *
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.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_FONT_PRIVATE_HH
30 #define HB_FONT_PRIVATE_HH
31
32 #include "hb-private.hh"
33
34 #include "hb-object-private.hh"
35 #include "hb-face-private.hh"
36 #include "hb-shaper-private.hh"
37
38
39
40 /*
41  * hb_font_funcs_t
42  */
43
44 #define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \
45   HB_FONT_FUNC_IMPLEMENT (font_h_extents) \
46   HB_FONT_FUNC_IMPLEMENT (font_v_extents) \
47   HB_FONT_FUNC_IMPLEMENT (nominal_glyph) \
48   HB_FONT_FUNC_IMPLEMENT (variation_glyph) \
49   HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \
50   HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \
51   HB_FONT_FUNC_IMPLEMENT (glyph_h_origin) \
52   HB_FONT_FUNC_IMPLEMENT (glyph_v_origin) \
53   HB_FONT_FUNC_IMPLEMENT (glyph_h_kerning) \
54   HB_FONT_FUNC_IMPLEMENT (glyph_v_kerning) \
55   HB_FONT_FUNC_IMPLEMENT (glyph_extents) \
56   HB_FONT_FUNC_IMPLEMENT (glyph_contour_point) \
57   HB_FONT_FUNC_IMPLEMENT (glyph_name) \
58   HB_FONT_FUNC_IMPLEMENT (glyph_from_name) \
59   /* ^--- Add new callbacks here */
60
61 struct hb_font_funcs_t {
62   hb_object_header_t header;
63   ASSERT_POD ();
64
65   hb_bool_t immutable;
66
67   struct {
68 #define HB_FONT_FUNC_IMPLEMENT(name) void *name;
69     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
70 #undef HB_FONT_FUNC_IMPLEMENT
71   } user_data;
72
73   struct {
74 #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
75     HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
76 #undef HB_FONT_FUNC_IMPLEMENT
77   } destroy;
78
79   /* Don't access these directly.  Call font->get_*() instead. */
80   union get_t {
81     struct get_funcs_t {
82 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_func_t name;
83       HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
84 #undef HB_FONT_FUNC_IMPLEMENT
85     } f;
86     void (*array[VAR]) (void);
87   } get;
88 };
89
90
91
92 /*
93  * hb_font_t
94  */
95
96 struct hb_font_t {
97   hb_object_header_t header;
98   ASSERT_POD ();
99
100   hb_bool_t immutable;
101
102   hb_font_t *parent;
103   hb_face_t *face;
104
105   int x_scale;
106   int y_scale;
107
108   unsigned int x_ppem;
109   unsigned int y_ppem;
110
111   /* Font variation coordinates. */
112   unsigned int num_coords;
113   int *coords;
114
115   hb_font_funcs_t   *klass;
116   void              *user_data;
117   hb_destroy_func_t  destroy;
118
119   enum dirty_t {
120     NOTHING     = 0x0000,
121     FACE        = 0x0001,
122     PARENT      = 0x0002,
123     FUNCS       = 0x0004,
124     SCALE       = 0x0008,
125     PPEM        = 0x0010,
126     VARIATIONS  = 0x0020,
127   } dirty;
128
129   struct hb_shaper_data_t shaper_data;
130
131
132   /* Convert from font-space to user-space */
133   inline int dir_scale (hb_direction_t direction)
134   { return HB_DIRECTION_IS_VERTICAL(direction) ? y_scale : x_scale; }
135   inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, x_scale); }
136   inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, y_scale); }
137   inline hb_position_t em_scalef_x (float v) { return em_scalef (v, this->x_scale); }
138   inline hb_position_t em_scalef_y (float v) { return em_scalef (v, this->y_scale); }
139   inline hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
140   { return em_scale (v, dir_scale (direction)); }
141
142   /* Convert from parent-font user-space to our user-space */
143   inline hb_position_t parent_scale_x_distance (hb_position_t v) {
144     if (unlikely (parent && parent->x_scale != x_scale))
145       return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
146     return v;
147   }
148   inline hb_position_t parent_scale_y_distance (hb_position_t v) {
149     if (unlikely (parent && parent->y_scale != y_scale))
150       return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
151     return v;
152   }
153   inline hb_position_t parent_scale_x_position (hb_position_t v) {
154     return parent_scale_x_distance (v);
155   }
156   inline hb_position_t parent_scale_y_position (hb_position_t v) {
157     return parent_scale_y_distance (v);
158   }
159
160   inline void parent_scale_distance (hb_position_t *x, hb_position_t *y) {
161     *x = parent_scale_x_distance (*x);
162     *y = parent_scale_y_distance (*y);
163   }
164   inline void parent_scale_position (hb_position_t *x, hb_position_t *y) {
165     *x = parent_scale_x_position (*x);
166     *y = parent_scale_y_position (*y);
167   }
168
169
170   /* Public getters */
171
172   HB_INTERNAL bool has_func (unsigned int i);
173
174   /* has_* ... */
175 #define HB_FONT_FUNC_IMPLEMENT(name) \
176   bool \
177   has_##name##_func (void) \
178   { \
179     hb_font_funcs_t *funcs = this->klass; \
180     unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
181     return has_func (i); \
182   }
183   HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
184 #undef HB_FONT_FUNC_IMPLEMENT
185
186   inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
187   {
188     memset (extents, 0, sizeof (*extents));
189     return klass->get.f.font_h_extents (this, user_data,
190                                         extents,
191                                         klass->user_data.font_h_extents);
192   }
193   inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
194   {
195     memset (extents, 0, sizeof (*extents));
196     return klass->get.f.font_v_extents (this, user_data,
197                                         extents,
198                                         klass->user_data.font_v_extents);
199   }
200
201   inline bool has_glyph (hb_codepoint_t unicode)
202   {
203     hb_codepoint_t glyph;
204     return get_nominal_glyph (unicode, &glyph);
205   }
206
207   inline hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
208                                       hb_codepoint_t *glyph)
209   {
210     *glyph = 0;
211     return klass->get.f.nominal_glyph (this, user_data,
212                                        unicode, glyph,
213                                        klass->user_data.nominal_glyph);
214   }
215
216   inline hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
217                                         hb_codepoint_t *glyph)
218   {
219     *glyph = 0;
220     return klass->get.f.variation_glyph (this, user_data,
221                                          unicode, variation_selector, glyph,
222                                          klass->user_data.variation_glyph);
223   }
224
225   inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
226   {
227     return klass->get.f.glyph_h_advance (this, user_data,
228                                          glyph,
229                                          klass->user_data.glyph_h_advance);
230   }
231
232   inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
233   {
234     return klass->get.f.glyph_v_advance (this, user_data,
235                                          glyph,
236                                          klass->user_data.glyph_v_advance);
237   }
238
239   inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
240                                        hb_position_t *x, hb_position_t *y)
241   {
242     *x = *y = 0;
243     return klass->get.f.glyph_h_origin (this, user_data,
244                                         glyph, x, y,
245                                         klass->user_data.glyph_h_origin);
246   }
247
248   inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
249                                        hb_position_t *x, hb_position_t *y)
250   {
251     *x = *y = 0;
252     return klass->get.f.glyph_v_origin (this, user_data,
253                                         glyph, x, y,
254                                         klass->user_data.glyph_v_origin);
255   }
256
257   inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
258   {
259     return klass->get.f.glyph_h_kerning (this, user_data,
260                                          left_glyph, right_glyph,
261                                          klass->user_data.glyph_h_kerning);
262   }
263
264   inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
265   {
266     return klass->get.f.glyph_v_kerning (this, user_data,
267                                          top_glyph, bottom_glyph,
268                                          klass->user_data.glyph_v_kerning);
269   }
270
271   inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
272                                       hb_glyph_extents_t *extents)
273   {
274     memset (extents, 0, sizeof (*extents));
275     return klass->get.f.glyph_extents (this, user_data,
276                                        glyph,
277                                        extents,
278                                        klass->user_data.glyph_extents);
279   }
280
281   inline hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
282                                             hb_position_t *x, hb_position_t *y)
283   {
284     *x = *y = 0;
285     return klass->get.f.glyph_contour_point (this, user_data,
286                                              glyph, point_index,
287                                              x, y,
288                                              klass->user_data.glyph_contour_point);
289   }
290
291   inline hb_bool_t get_glyph_name (hb_codepoint_t glyph,
292                                    char *name, unsigned int size)
293   {
294     if (size) *name = '\0';
295     return klass->get.f.glyph_name (this, user_data,
296                                     glyph,
297                                     name, size,
298                                     klass->user_data.glyph_name);
299   }
300
301   inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
302                                         hb_codepoint_t *glyph)
303   {
304     *glyph = 0;
305     if (len == -1) len = strlen (name);
306     return klass->get.f.glyph_from_name (this, user_data,
307                                          name, len,
308                                          glyph,
309                                          klass->user_data.glyph_from_name);
310   }
311
312
313   /* A bit higher-level, and with fallback */
314
315   inline void get_h_extents_with_fallback (hb_font_extents_t *extents)
316   {
317     if (!get_font_h_extents (extents))
318     {
319       extents->ascender = y_scale * .8;
320       extents->descender = extents->ascender - y_scale;
321       extents->line_gap = 0;
322     }
323   }
324   inline void get_v_extents_with_fallback (hb_font_extents_t *extents)
325   {
326     if (!get_font_v_extents (extents))
327     {
328       extents->ascender = x_scale / 2;
329       extents->descender = extents->ascender - x_scale;
330       extents->line_gap = 0;
331     }
332   }
333
334   inline void get_extents_for_direction (hb_direction_t direction,
335                                          hb_font_extents_t *extents)
336   {
337     if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
338       get_h_extents_with_fallback (extents);
339     else
340       get_v_extents_with_fallback (extents);
341   }
342
343   inline void get_glyph_advance_for_direction (hb_codepoint_t glyph,
344                                                hb_direction_t direction,
345                                                hb_position_t *x, hb_position_t *y)
346   {
347     if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
348       *x = get_glyph_h_advance (glyph);
349       *y = 0;
350     } else {
351       *x = 0;
352       *y = get_glyph_v_advance (glyph);
353     }
354   }
355
356   inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
357                                              hb_position_t *x, hb_position_t *y)
358   {
359     *x = get_glyph_h_advance (glyph) / 2;
360
361     /* TODO cache this somehow?! */
362     hb_font_extents_t extents;
363     get_h_extents_with_fallback (&extents);
364     *y = extents.ascender;
365   }
366
367   inline void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
368                                                 hb_position_t *x, hb_position_t *y)
369   {
370     if (!get_glyph_h_origin (glyph, x, y) &&
371          get_glyph_v_origin (glyph, x, y))
372     {
373       hb_position_t dx, dy;
374       guess_v_origin_minus_h_origin (glyph, &dx, &dy);
375       *x -= dx; *y -= dy;
376     }
377   }
378   inline void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
379                                                 hb_position_t *x, hb_position_t *y)
380   {
381     if (!get_glyph_v_origin (glyph, x, y) &&
382          get_glyph_h_origin (glyph, x, y))
383     {
384       hb_position_t dx, dy;
385       guess_v_origin_minus_h_origin (glyph, &dx, &dy);
386       *x += dx; *y += dy;
387     }
388   }
389
390   inline void get_glyph_origin_for_direction (hb_codepoint_t glyph,
391                                               hb_direction_t direction,
392                                               hb_position_t *x, hb_position_t *y)
393   {
394     if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
395       get_glyph_h_origin_with_fallback (glyph, x, y);
396     else
397       get_glyph_v_origin_with_fallback (glyph, x, y);
398   }
399
400   inline void add_glyph_h_origin (hb_codepoint_t glyph,
401                                   hb_position_t *x, hb_position_t *y)
402   {
403     hb_position_t origin_x, origin_y;
404
405     get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
406
407     *x += origin_x;
408     *y += origin_y;
409   }
410   inline void add_glyph_v_origin (hb_codepoint_t glyph,
411                                   hb_position_t *x, hb_position_t *y)
412   {
413     hb_position_t origin_x, origin_y;
414
415     get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
416
417     *x += origin_x;
418     *y += origin_y;
419   }
420   inline void add_glyph_origin_for_direction (hb_codepoint_t glyph,
421                                               hb_direction_t direction,
422                                               hb_position_t *x, hb_position_t *y)
423   {
424     hb_position_t origin_x, origin_y;
425
426     get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
427
428     *x += origin_x;
429     *y += origin_y;
430   }
431
432   inline void subtract_glyph_h_origin (hb_codepoint_t glyph,
433                                        hb_position_t *x, hb_position_t *y)
434   {
435     hb_position_t origin_x, origin_y;
436
437     get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
438
439     *x -= origin_x;
440     *y -= origin_y;
441   }
442   inline void subtract_glyph_v_origin (hb_codepoint_t glyph,
443                                        hb_position_t *x, hb_position_t *y)
444   {
445     hb_position_t origin_x, origin_y;
446
447     get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
448
449     *x -= origin_x;
450     *y -= origin_y;
451   }
452   inline void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
453                                                    hb_direction_t direction,
454                                                    hb_position_t *x, hb_position_t *y)
455   {
456     hb_position_t origin_x, origin_y;
457
458     get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
459
460     *x -= origin_x;
461     *y -= origin_y;
462   }
463
464   inline void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
465                                                hb_direction_t direction,
466                                                hb_position_t *x, hb_position_t *y)
467   {
468     if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
469       *x = get_glyph_h_kerning (first_glyph, second_glyph);
470       *y = 0;
471     } else {
472       *x = 0;
473       *y = get_glyph_v_kerning (first_glyph, second_glyph);
474     }
475   }
476
477   inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
478                                                  hb_direction_t direction,
479                                                  hb_glyph_extents_t *extents)
480   {
481     hb_bool_t ret = get_glyph_extents (glyph, extents);
482
483     if (ret)
484       subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
485
486     return ret;
487   }
488
489   inline hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
490                                                        hb_direction_t direction,
491                                                        hb_position_t *x, hb_position_t *y)
492   {
493     hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
494
495     if (ret)
496       subtract_glyph_origin_for_direction (glyph, direction, x, y);
497
498     return ret;
499   }
500
501   /* Generates gidDDD if glyph has no name. */
502   inline void
503   glyph_to_string (hb_codepoint_t glyph,
504                    char *s, unsigned int size)
505   {
506     if (get_glyph_name (glyph, s, size)) return;
507
508     if (size && snprintf (s, size, "gid%u", glyph) < 0)
509       *s = '\0';
510   }
511
512   /* Parses gidDDD and uniUUUU strings automatically. */
513   inline hb_bool_t
514   glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
515                      hb_codepoint_t *glyph)
516   {
517     if (get_glyph_from_name (s, len, glyph)) return true;
518
519     if (len == -1) len = strlen (s);
520
521     /* Straight glyph index. */
522     if (hb_codepoint_parse (s, len, 10, glyph))
523       return true;
524
525     if (len > 3)
526     {
527       /* gidDDD syntax for glyph indices. */
528       if (0 == strncmp (s, "gid", 3) &&
529           hb_codepoint_parse (s + 3, len - 3, 10, glyph))
530         return true;
531
532       /* uniUUUU and other Unicode character indices. */
533       hb_codepoint_t unichar;
534       if (0 == strncmp (s, "uni", 3) &&
535           hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
536           get_nominal_glyph (unichar, glyph))
537         return true;
538     }
539
540     return false;
541   }
542
543   inline hb_position_t em_scale (int16_t v, int scale)
544   {
545     int upem = face->get_upem ();
546     int64_t scaled = v * (int64_t) scale;
547     scaled += scaled >= 0 ? upem/2 : -upem/2; /* Round. */
548     return (hb_position_t) (scaled / upem);
549   }
550   inline hb_position_t em_scalef (float v, int scale)
551   {
552     return (hb_position_t) (v * scale / face->get_upem ());
553   }
554 };
555
556 HB_MARK_AS_FLAG_T (hb_font_t::dirty_t);
557
558 #define HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
559 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_PROTOTYPE(shaper, font);
560 #include "hb-shaper-list.hh"
561 #undef HB_SHAPER_IMPLEMENT
562 #undef HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
563
564
565 #endif /* HB_FONT_PRIVATE_HH */