2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2011 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
35 #include "hb-shaper.hh"
42 #define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \
43 HB_FONT_FUNC_IMPLEMENT (font_h_extents) \
44 HB_FONT_FUNC_IMPLEMENT (font_v_extents) \
45 HB_FONT_FUNC_IMPLEMENT (nominal_glyph) \
46 HB_FONT_FUNC_IMPLEMENT (nominal_glyphs) \
47 HB_FONT_FUNC_IMPLEMENT (variation_glyph) \
48 HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \
49 HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \
50 HB_FONT_FUNC_IMPLEMENT (glyph_h_advances) \
51 HB_FONT_FUNC_IMPLEMENT (glyph_v_advances) \
52 HB_FONT_FUNC_IMPLEMENT (glyph_h_origin) \
53 HB_FONT_FUNC_IMPLEMENT (glyph_v_origin) \
54 HB_FONT_FUNC_IMPLEMENT (glyph_h_kerning) \
55 HB_FONT_FUNC_IMPLEMENT (glyph_v_kerning) \
56 HB_FONT_FUNC_IMPLEMENT (glyph_extents) \
57 HB_FONT_FUNC_IMPLEMENT (glyph_contour_point) \
58 HB_FONT_FUNC_IMPLEMENT (glyph_name) \
59 HB_FONT_FUNC_IMPLEMENT (glyph_from_name) \
60 /* ^--- Add new callbacks here */
62 struct hb_font_funcs_t
64 hb_object_header_t header;
67 #define HB_FONT_FUNC_IMPLEMENT(name) void *name;
68 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
69 #undef HB_FONT_FUNC_IMPLEMENT
73 #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
74 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
75 #undef HB_FONT_FUNC_IMPLEMENT
78 /* Don't access these directly. Call font->get_*() instead. */
81 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_func_t name;
82 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
83 #undef HB_FONT_FUNC_IMPLEMENT
86 #define HB_FONT_FUNC_IMPLEMENT(name) +1
87 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
88 #undef HB_FONT_FUNC_IMPLEMENT
92 DECLARE_NULL_INSTANCE (hb_font_funcs_t);
99 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INSTANTIATE_SHAPERS(shaper, font);
100 #include "hb-shaper-list.hh"
101 #undef HB_SHAPER_IMPLEMENT
105 hb_object_header_t header;
118 /* Font variation coordinates. */
119 unsigned int num_coords;
122 hb_font_funcs_t *klass;
124 hb_destroy_func_t destroy;
126 hb_shaper_object_dataset_t<hb_font_t> data; /* Various shaper data. */
129 /* Convert from font-space to user-space */
130 int dir_scale (hb_direction_t direction)
131 { return HB_DIRECTION_IS_VERTICAL(direction) ? y_scale : x_scale; }
132 hb_position_t em_scale_x (int16_t v) { return em_scale (v, x_scale); }
133 hb_position_t em_scale_y (int16_t v) { return em_scale (v, y_scale); }
134 hb_position_t em_scalef_x (float v) { return em_scalef (v, this->x_scale); }
135 hb_position_t em_scalef_y (float v) { return em_scalef (v, this->y_scale); }
136 float em_fscale_x (int16_t v) { return em_fscale (v, x_scale); }
137 float em_fscale_y (int16_t v) { return em_fscale (v, y_scale); }
138 hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
139 { return em_scale (v, dir_scale (direction)); }
141 /* Convert from parent-font user-space to our user-space */
142 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);
148 hb_position_t parent_scale_y_distance (hb_position_t v)
150 if (unlikely (parent && parent->y_scale != y_scale))
151 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
154 hb_position_t parent_scale_x_position (hb_position_t v)
155 { return parent_scale_x_distance (v); }
156 hb_position_t parent_scale_y_position (hb_position_t v)
157 { return parent_scale_y_distance (v); }
159 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);
164 void parent_scale_position (hb_position_t *x, hb_position_t *y)
166 *x = parent_scale_x_position (*x);
167 *y = parent_scale_y_position (*y);
173 HB_INTERNAL bool has_func (unsigned int i);
174 HB_INTERNAL bool has_func_set (unsigned int i);
177 #define HB_FONT_FUNC_IMPLEMENT(name) \
179 has_##name##_func () \
181 hb_font_funcs_t *funcs = this->klass; \
182 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
183 return has_func (i); \
186 has_##name##_func_set () \
188 hb_font_funcs_t *funcs = this->klass; \
189 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
190 return has_func_set (i); \
192 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
193 #undef HB_FONT_FUNC_IMPLEMENT
195 hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
197 memset (extents, 0, sizeof (*extents));
198 return klass->get.f.font_h_extents (this, user_data,
200 klass->user_data.font_h_extents);
202 hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
204 memset (extents, 0, sizeof (*extents));
205 return klass->get.f.font_v_extents (this, user_data,
207 klass->user_data.font_v_extents);
210 bool has_glyph (hb_codepoint_t unicode)
212 hb_codepoint_t glyph;
213 return get_nominal_glyph (unicode, &glyph);
216 hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
217 hb_codepoint_t *glyph)
220 return klass->get.f.nominal_glyph (this, user_data,
222 klass->user_data.nominal_glyph);
224 unsigned int get_nominal_glyphs (unsigned int count,
225 const hb_codepoint_t *first_unicode,
226 unsigned int unicode_stride,
227 hb_codepoint_t *first_glyph,
228 unsigned int glyph_stride)
230 return klass->get.f.nominal_glyphs (this, user_data,
232 first_unicode, unicode_stride,
233 first_glyph, glyph_stride,
234 klass->user_data.nominal_glyphs);
237 hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
238 hb_codepoint_t *glyph)
241 return klass->get.f.variation_glyph (this, user_data,
242 unicode, variation_selector, glyph,
243 klass->user_data.variation_glyph);
246 hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
248 return klass->get.f.glyph_h_advance (this, user_data,
250 klass->user_data.glyph_h_advance);
253 hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
255 return klass->get.f.glyph_v_advance (this, user_data,
257 klass->user_data.glyph_v_advance);
260 void get_glyph_h_advances (unsigned int count,
261 const hb_codepoint_t *first_glyph,
262 unsigned int glyph_stride,
263 hb_position_t *first_advance,
264 unsigned int advance_stride)
266 return klass->get.f.glyph_h_advances (this, user_data,
268 first_glyph, glyph_stride,
269 first_advance, advance_stride,
270 klass->user_data.glyph_h_advances);
273 void get_glyph_v_advances (unsigned int count,
274 const hb_codepoint_t *first_glyph,
275 unsigned int glyph_stride,
276 hb_position_t *first_advance,
277 unsigned int advance_stride)
279 return klass->get.f.glyph_v_advances (this, user_data,
281 first_glyph, glyph_stride,
282 first_advance, advance_stride,
283 klass->user_data.glyph_v_advances);
286 hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
287 hb_position_t *x, hb_position_t *y)
290 return klass->get.f.glyph_h_origin (this, user_data,
292 klass->user_data.glyph_h_origin);
295 hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
296 hb_position_t *x, hb_position_t *y)
299 return klass->get.f.glyph_v_origin (this, user_data,
301 klass->user_data.glyph_v_origin);
304 hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph,
305 hb_codepoint_t right_glyph)
307 return klass->get.f.glyph_h_kerning (this, user_data,
308 left_glyph, right_glyph,
309 klass->user_data.glyph_h_kerning);
312 hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph,
313 hb_codepoint_t bottom_glyph)
315 return klass->get.f.glyph_v_kerning (this, user_data,
316 top_glyph, bottom_glyph,
317 klass->user_data.glyph_v_kerning);
320 hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
321 hb_glyph_extents_t *extents)
323 memset (extents, 0, sizeof (*extents));
324 return klass->get.f.glyph_extents (this, user_data,
327 klass->user_data.glyph_extents);
330 hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
331 hb_position_t *x, hb_position_t *y)
334 return klass->get.f.glyph_contour_point (this, user_data,
337 klass->user_data.glyph_contour_point);
340 hb_bool_t get_glyph_name (hb_codepoint_t glyph,
341 char *name, unsigned int size)
343 if (size) *name = '\0';
344 return klass->get.f.glyph_name (this, user_data,
347 klass->user_data.glyph_name);
350 hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
351 hb_codepoint_t *glyph)
354 if (len == -1) len = strlen (name);
355 return klass->get.f.glyph_from_name (this, user_data,
358 klass->user_data.glyph_from_name);
362 /* A bit higher-level, and with fallback */
364 void get_h_extents_with_fallback (hb_font_extents_t *extents)
366 if (!get_font_h_extents (extents))
368 extents->ascender = y_scale * .8;
369 extents->descender = extents->ascender - y_scale;
370 extents->line_gap = 0;
373 void get_v_extents_with_fallback (hb_font_extents_t *extents)
375 if (!get_font_v_extents (extents))
377 extents->ascender = x_scale / 2;
378 extents->descender = extents->ascender - x_scale;
379 extents->line_gap = 0;
383 void get_extents_for_direction (hb_direction_t direction,
384 hb_font_extents_t *extents)
386 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
387 get_h_extents_with_fallback (extents);
389 get_v_extents_with_fallback (extents);
392 void get_glyph_advance_for_direction (hb_codepoint_t glyph,
393 hb_direction_t direction,
394 hb_position_t *x, hb_position_t *y)
397 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
398 *x = get_glyph_h_advance (glyph);
400 *y = get_glyph_v_advance (glyph);
402 void get_glyph_advances_for_direction (hb_direction_t direction,
404 const hb_codepoint_t *first_glyph,
405 unsigned glyph_stride,
406 hb_position_t *first_advance,
407 unsigned advance_stride)
409 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
410 get_glyph_h_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
412 get_glyph_v_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
415 void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
416 hb_position_t *x, hb_position_t *y)
418 *x = get_glyph_h_advance (glyph) / 2;
420 /* TODO cache this somehow?! */
421 hb_font_extents_t extents;
422 get_h_extents_with_fallback (&extents);
423 *y = extents.ascender;
426 void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
427 hb_position_t *x, hb_position_t *y)
429 if (!get_glyph_h_origin (glyph, x, y) &&
430 get_glyph_v_origin (glyph, x, y))
432 hb_position_t dx, dy;
433 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
437 void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
438 hb_position_t *x, hb_position_t *y)
440 if (!get_glyph_v_origin (glyph, x, y) &&
441 get_glyph_h_origin (glyph, x, y))
443 hb_position_t dx, dy;
444 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
449 void get_glyph_origin_for_direction (hb_codepoint_t glyph,
450 hb_direction_t direction,
451 hb_position_t *x, hb_position_t *y)
453 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
454 get_glyph_h_origin_with_fallback (glyph, x, y);
456 get_glyph_v_origin_with_fallback (glyph, x, y);
459 void add_glyph_h_origin (hb_codepoint_t glyph,
460 hb_position_t *x, hb_position_t *y)
462 hb_position_t origin_x, origin_y;
464 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
469 void add_glyph_v_origin (hb_codepoint_t glyph,
470 hb_position_t *x, hb_position_t *y)
472 hb_position_t origin_x, origin_y;
474 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
479 void add_glyph_origin_for_direction (hb_codepoint_t glyph,
480 hb_direction_t direction,
481 hb_position_t *x, hb_position_t *y)
483 hb_position_t origin_x, origin_y;
485 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
491 void subtract_glyph_h_origin (hb_codepoint_t glyph,
492 hb_position_t *x, hb_position_t *y)
494 hb_position_t origin_x, origin_y;
496 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
501 void subtract_glyph_v_origin (hb_codepoint_t glyph,
502 hb_position_t *x, hb_position_t *y)
504 hb_position_t origin_x, origin_y;
506 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
511 void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
512 hb_direction_t direction,
513 hb_position_t *x, hb_position_t *y)
515 hb_position_t origin_x, origin_y;
517 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
523 void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
524 hb_direction_t direction,
525 hb_position_t *x, hb_position_t *y)
527 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
529 *x = get_glyph_h_kerning (first_glyph, second_glyph);
532 *y = get_glyph_v_kerning (first_glyph, second_glyph);
536 hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
537 hb_direction_t direction,
538 hb_glyph_extents_t *extents)
540 hb_bool_t ret = get_glyph_extents (glyph, extents);
543 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
548 hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
549 hb_direction_t direction,
550 hb_position_t *x, hb_position_t *y)
552 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
555 subtract_glyph_origin_for_direction (glyph, direction, x, y);
560 /* Generates gidDDD if glyph has no name. */
562 glyph_to_string (hb_codepoint_t glyph,
563 char *s, unsigned int size)
565 if (get_glyph_name (glyph, s, size)) return;
567 if (size && snprintf (s, size, "gid%u", glyph) < 0)
571 /* Parses gidDDD and uniUUUU strings automatically. */
573 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
574 hb_codepoint_t *glyph)
576 if (get_glyph_from_name (s, len, glyph)) return true;
578 if (len == -1) len = strlen (s);
580 /* Straight glyph index. */
581 if (hb_codepoint_parse (s, len, 10, glyph))
586 /* gidDDD syntax for glyph indices. */
587 if (0 == strncmp (s, "gid", 3) &&
588 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
591 /* uniUUUU and other Unicode character indices. */
592 hb_codepoint_t unichar;
593 if (0 == strncmp (s, "uni", 3) &&
594 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
595 get_nominal_glyph (unichar, glyph))
602 hb_position_t em_scale (int16_t v, int scale)
604 int upem = face->get_upem ();
605 int64_t scaled = v * (int64_t) scale;
606 scaled += scaled >= 0 ? upem/2 : -upem/2; /* Round. */
607 return (hb_position_t) (scaled / upem);
609 hb_position_t em_scalef (float v, int scale)
610 { return (hb_position_t) round (v * scale / face->get_upem ()); }
611 float em_fscale (int16_t v, int scale)
612 { return (float) v * scale / face->get_upem (); }
614 DECLARE_NULL_INSTANCE (hb_font_t);
617 #endif /* HB_FONT_HH */