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_IF_NOT_DEPRECATED (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;
122 /* Font variation coordinates. */
123 unsigned int num_coords;
125 float *design_coords;
127 hb_font_funcs_t *klass;
129 hb_destroy_func_t destroy;
131 hb_shaper_object_dataset_t<hb_font_t> data; /* Various shaper data. */
134 /* Convert from font-space to user-space */
135 int64_t dir_mult (hb_direction_t direction)
136 { return HB_DIRECTION_IS_VERTICAL(direction) ? y_mult : x_mult; }
137 hb_position_t em_scale_x (int16_t v) { return em_mult (v, x_mult); }
138 hb_position_t em_scale_y (int16_t v) { return em_mult (v, y_mult); }
139 hb_position_t em_scalef_x (float v) { return em_scalef (v, x_scale); }
140 hb_position_t em_scalef_y (float v) { return em_scalef (v, y_scale); }
141 float em_fscale_x (int16_t v) { return em_fscale (v, x_scale); }
142 float em_fscale_y (int16_t v) { return em_fscale (v, y_scale); }
143 hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
144 { return em_mult (v, dir_mult (direction)); }
146 /* Convert from parent-font user-space to our user-space */
147 hb_position_t parent_scale_x_distance (hb_position_t v)
149 if (unlikely (parent && parent->x_scale != x_scale))
150 return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
153 hb_position_t parent_scale_y_distance (hb_position_t v)
155 if (unlikely (parent && parent->y_scale != y_scale))
156 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
159 hb_position_t parent_scale_x_position (hb_position_t v)
160 { return parent_scale_x_distance (v); }
161 hb_position_t parent_scale_y_position (hb_position_t v)
162 { return parent_scale_y_distance (v); }
164 void parent_scale_distance (hb_position_t *x, hb_position_t *y)
166 *x = parent_scale_x_distance (*x);
167 *y = parent_scale_y_distance (*y);
169 void parent_scale_position (hb_position_t *x, hb_position_t *y)
171 *x = parent_scale_x_position (*x);
172 *y = parent_scale_y_position (*y);
178 HB_INTERNAL bool has_func (unsigned int i);
179 HB_INTERNAL bool has_func_set (unsigned int i);
182 #define HB_FONT_FUNC_IMPLEMENT(name) \
184 has_##name##_func () \
186 hb_font_funcs_t *funcs = this->klass; \
187 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
188 return has_func (i); \
191 has_##name##_func_set () \
193 hb_font_funcs_t *funcs = this->klass; \
194 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
195 return has_func_set (i); \
197 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
198 #undef HB_FONT_FUNC_IMPLEMENT
200 hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
202 memset (extents, 0, sizeof (*extents));
203 return klass->get.f.font_h_extents (this, user_data,
205 klass->user_data.font_h_extents);
207 hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
209 memset (extents, 0, sizeof (*extents));
210 return klass->get.f.font_v_extents (this, user_data,
212 klass->user_data.font_v_extents);
215 bool has_glyph (hb_codepoint_t unicode)
217 hb_codepoint_t glyph;
218 return get_nominal_glyph (unicode, &glyph);
221 hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
222 hb_codepoint_t *glyph,
223 hb_codepoint_t not_found = 0)
226 return klass->get.f.nominal_glyph (this, user_data,
228 klass->user_data.nominal_glyph);
230 unsigned int get_nominal_glyphs (unsigned int count,
231 const hb_codepoint_t *first_unicode,
232 unsigned int unicode_stride,
233 hb_codepoint_t *first_glyph,
234 unsigned int glyph_stride)
236 return klass->get.f.nominal_glyphs (this, user_data,
238 first_unicode, unicode_stride,
239 first_glyph, glyph_stride,
240 klass->user_data.nominal_glyphs);
243 hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
244 hb_codepoint_t *glyph,
245 hb_codepoint_t not_found = 0)
248 return klass->get.f.variation_glyph (this, user_data,
249 unicode, variation_selector, glyph,
250 klass->user_data.variation_glyph);
253 hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
255 return klass->get.f.glyph_h_advance (this, user_data,
257 klass->user_data.glyph_h_advance);
260 hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
262 return klass->get.f.glyph_v_advance (this, user_data,
264 klass->user_data.glyph_v_advance);
267 void get_glyph_h_advances (unsigned int count,
268 const hb_codepoint_t *first_glyph,
269 unsigned int glyph_stride,
270 hb_position_t *first_advance,
271 unsigned int advance_stride)
273 return klass->get.f.glyph_h_advances (this, user_data,
275 first_glyph, glyph_stride,
276 first_advance, advance_stride,
277 klass->user_data.glyph_h_advances);
280 void get_glyph_v_advances (unsigned int count,
281 const hb_codepoint_t *first_glyph,
282 unsigned int glyph_stride,
283 hb_position_t *first_advance,
284 unsigned int advance_stride)
286 return klass->get.f.glyph_v_advances (this, user_data,
288 first_glyph, glyph_stride,
289 first_advance, advance_stride,
290 klass->user_data.glyph_v_advances);
293 hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
294 hb_position_t *x, hb_position_t *y)
297 return klass->get.f.glyph_h_origin (this, user_data,
299 klass->user_data.glyph_h_origin);
302 hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
303 hb_position_t *x, hb_position_t *y)
306 return klass->get.f.glyph_v_origin (this, user_data,
308 klass->user_data.glyph_v_origin);
311 hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph,
312 hb_codepoint_t right_glyph)
314 #ifdef HB_DISABLE_DEPRECATED
317 return klass->get.f.glyph_h_kerning (this, user_data,
318 left_glyph, right_glyph,
319 klass->user_data.glyph_h_kerning);
323 hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph,
324 hb_codepoint_t bottom_glyph)
326 #ifdef HB_DISABLE_DEPRECATED
329 return klass->get.f.glyph_v_kerning (this, user_data,
330 top_glyph, bottom_glyph,
331 klass->user_data.glyph_v_kerning);
335 hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
336 hb_glyph_extents_t *extents)
338 memset (extents, 0, sizeof (*extents));
339 return klass->get.f.glyph_extents (this, user_data,
342 klass->user_data.glyph_extents);
345 hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
346 hb_position_t *x, hb_position_t *y)
349 return klass->get.f.glyph_contour_point (this, user_data,
352 klass->user_data.glyph_contour_point);
355 hb_bool_t get_glyph_name (hb_codepoint_t glyph,
356 char *name, unsigned int size)
358 if (size) *name = '\0';
359 return klass->get.f.glyph_name (this, user_data,
362 klass->user_data.glyph_name);
365 hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
366 hb_codepoint_t *glyph)
369 if (len == -1) len = strlen (name);
370 return klass->get.f.glyph_from_name (this, user_data,
373 klass->user_data.glyph_from_name);
377 /* A bit higher-level, and with fallback */
379 void get_h_extents_with_fallback (hb_font_extents_t *extents)
381 if (!get_font_h_extents (extents))
383 extents->ascender = y_scale * .8;
384 extents->descender = extents->ascender - y_scale;
385 extents->line_gap = 0;
388 void get_v_extents_with_fallback (hb_font_extents_t *extents)
390 if (!get_font_v_extents (extents))
392 extents->ascender = x_scale / 2;
393 extents->descender = extents->ascender - x_scale;
394 extents->line_gap = 0;
398 void get_extents_for_direction (hb_direction_t direction,
399 hb_font_extents_t *extents)
401 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
402 get_h_extents_with_fallback (extents);
404 get_v_extents_with_fallback (extents);
407 void get_glyph_advance_for_direction (hb_codepoint_t glyph,
408 hb_direction_t direction,
409 hb_position_t *x, hb_position_t *y)
412 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
413 *x = get_glyph_h_advance (glyph);
415 *y = get_glyph_v_advance (glyph);
417 void get_glyph_advances_for_direction (hb_direction_t direction,
419 const hb_codepoint_t *first_glyph,
420 unsigned glyph_stride,
421 hb_position_t *first_advance,
422 unsigned advance_stride)
424 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
425 get_glyph_h_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
427 get_glyph_v_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
430 void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
431 hb_position_t *x, hb_position_t *y)
433 *x = get_glyph_h_advance (glyph) / 2;
435 /* TODO cache this somehow?! */
436 hb_font_extents_t extents;
437 get_h_extents_with_fallback (&extents);
438 *y = extents.ascender;
441 void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
442 hb_position_t *x, hb_position_t *y)
444 if (!get_glyph_h_origin (glyph, x, y) &&
445 get_glyph_v_origin (glyph, x, y))
447 hb_position_t dx, dy;
448 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
452 void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
453 hb_position_t *x, hb_position_t *y)
455 if (!get_glyph_v_origin (glyph, x, y) &&
456 get_glyph_h_origin (glyph, x, y))
458 hb_position_t dx, dy;
459 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
464 void get_glyph_origin_for_direction (hb_codepoint_t glyph,
465 hb_direction_t direction,
466 hb_position_t *x, hb_position_t *y)
468 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
469 get_glyph_h_origin_with_fallback (glyph, x, y);
471 get_glyph_v_origin_with_fallback (glyph, x, y);
474 void add_glyph_h_origin (hb_codepoint_t glyph,
475 hb_position_t *x, hb_position_t *y)
477 hb_position_t origin_x, origin_y;
479 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
484 void add_glyph_v_origin (hb_codepoint_t glyph,
485 hb_position_t *x, hb_position_t *y)
487 hb_position_t origin_x, origin_y;
489 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
494 void add_glyph_origin_for_direction (hb_codepoint_t glyph,
495 hb_direction_t direction,
496 hb_position_t *x, hb_position_t *y)
498 hb_position_t origin_x, origin_y;
500 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
506 void subtract_glyph_h_origin (hb_codepoint_t glyph,
507 hb_position_t *x, hb_position_t *y)
509 hb_position_t origin_x, origin_y;
511 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
516 void subtract_glyph_v_origin (hb_codepoint_t glyph,
517 hb_position_t *x, hb_position_t *y)
519 hb_position_t origin_x, origin_y;
521 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
526 void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
527 hb_direction_t direction,
528 hb_position_t *x, hb_position_t *y)
530 hb_position_t origin_x, origin_y;
532 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
538 void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
539 hb_direction_t direction,
540 hb_position_t *x, hb_position_t *y)
542 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
544 *x = get_glyph_h_kerning (first_glyph, second_glyph);
547 *y = get_glyph_v_kerning (first_glyph, second_glyph);
551 hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
552 hb_direction_t direction,
553 hb_glyph_extents_t *extents)
555 hb_bool_t ret = get_glyph_extents (glyph, extents);
558 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
563 hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
564 hb_direction_t direction,
565 hb_position_t *x, hb_position_t *y)
567 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
570 subtract_glyph_origin_for_direction (glyph, direction, x, y);
575 /* Generates gidDDD if glyph has no name. */
577 glyph_to_string (hb_codepoint_t glyph,
578 char *s, unsigned int size)
580 if (get_glyph_name (glyph, s, size)) return;
582 if (size && snprintf (s, size, "gid%u", glyph) < 0)
586 /* Parses gidDDD and uniUUUU strings automatically. */
588 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
589 hb_codepoint_t *glyph)
591 if (get_glyph_from_name (s, len, glyph)) return true;
593 if (len == -1) len = strlen (s);
595 /* Straight glyph index. */
596 if (hb_codepoint_parse (s, len, 10, glyph))
601 /* gidDDD syntax for glyph indices. */
602 if (0 == strncmp (s, "gid", 3) &&
603 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
606 /* uniUUUU and other Unicode character indices. */
607 hb_codepoint_t unichar;
608 if (0 == strncmp (s, "uni", 3) &&
609 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
610 get_nominal_glyph (unichar, glyph))
617 void mults_changed ()
619 signed upem = face->get_upem ();
620 x_mult = ((int64_t) x_scale << 16) / upem;
621 y_mult = ((int64_t) y_scale << 16) / upem;
622 slant_xy = y_scale ? slant * x_scale / y_scale : 0.f;
625 hb_position_t em_mult (int16_t v, int64_t mult)
626 { return (hb_position_t) ((v * mult + 32768) >> 16); }
627 hb_position_t em_scalef (float v, int scale)
628 { return (hb_position_t) roundf (v * scale / face->get_upem ()); }
629 float em_fscale (int16_t v, int scale)
630 { return (float) v * scale / face->get_upem (); }
632 DECLARE_NULL_INSTANCE (hb_font_t);
635 #endif /* HB_FONT_HH */