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;
120 /* Font variation coordinates. */
121 unsigned int num_coords;
124 hb_font_funcs_t *klass;
126 hb_destroy_func_t destroy;
128 hb_shaper_object_dataset_t<hb_font_t> data; /* Various shaper data. */
131 /* Convert from font-space to user-space */
132 int64_t dir_mult (hb_direction_t direction)
133 { return HB_DIRECTION_IS_VERTICAL(direction) ? y_mult : x_mult; }
134 hb_position_t em_scale_x (int16_t v) { return em_mult (v, x_mult); }
135 hb_position_t em_scale_y (int16_t v) { return em_mult (v, y_mult); }
136 hb_position_t em_scalef_x (float v) { return em_scalef (v, x_scale); }
137 hb_position_t em_scalef_y (float v) { return em_scalef (v, y_scale); }
138 float em_fscale_x (int16_t v) { return em_fscale (v, x_scale); }
139 float em_fscale_y (int16_t v) { return em_fscale (v, y_scale); }
140 hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
141 { return em_mult (v, dir_mult (direction)); }
143 /* Convert from parent-font user-space to our user-space */
144 hb_position_t parent_scale_x_distance (hb_position_t v)
146 if (unlikely (parent && parent->x_scale != x_scale))
147 return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
150 hb_position_t parent_scale_y_distance (hb_position_t v)
152 if (unlikely (parent && parent->y_scale != y_scale))
153 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
156 hb_position_t parent_scale_x_position (hb_position_t v)
157 { return parent_scale_x_distance (v); }
158 hb_position_t parent_scale_y_position (hb_position_t v)
159 { return parent_scale_y_distance (v); }
161 void parent_scale_distance (hb_position_t *x, hb_position_t *y)
163 *x = parent_scale_x_distance (*x);
164 *y = parent_scale_y_distance (*y);
166 void parent_scale_position (hb_position_t *x, hb_position_t *y)
168 *x = parent_scale_x_position (*x);
169 *y = parent_scale_y_position (*y);
175 HB_INTERNAL bool has_func (unsigned int i);
176 HB_INTERNAL bool has_func_set (unsigned int i);
179 #define HB_FONT_FUNC_IMPLEMENT(name) \
181 has_##name##_func () \
183 hb_font_funcs_t *funcs = this->klass; \
184 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
185 return has_func (i); \
188 has_##name##_func_set () \
190 hb_font_funcs_t *funcs = this->klass; \
191 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
192 return has_func_set (i); \
194 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
195 #undef HB_FONT_FUNC_IMPLEMENT
197 hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
199 memset (extents, 0, sizeof (*extents));
200 return klass->get.f.font_h_extents (this, user_data,
202 klass->user_data.font_h_extents);
204 hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
206 memset (extents, 0, sizeof (*extents));
207 return klass->get.f.font_v_extents (this, user_data,
209 klass->user_data.font_v_extents);
212 bool has_glyph (hb_codepoint_t unicode)
214 hb_codepoint_t glyph;
215 return get_nominal_glyph (unicode, &glyph);
218 hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
219 hb_codepoint_t *glyph)
222 return klass->get.f.nominal_glyph (this, user_data,
224 klass->user_data.nominal_glyph);
226 unsigned int get_nominal_glyphs (unsigned int count,
227 const hb_codepoint_t *first_unicode,
228 unsigned int unicode_stride,
229 hb_codepoint_t *first_glyph,
230 unsigned int glyph_stride)
232 return klass->get.f.nominal_glyphs (this, user_data,
234 first_unicode, unicode_stride,
235 first_glyph, glyph_stride,
236 klass->user_data.nominal_glyphs);
239 hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
240 hb_codepoint_t *glyph)
243 return klass->get.f.variation_glyph (this, user_data,
244 unicode, variation_selector, glyph,
245 klass->user_data.variation_glyph);
248 hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
250 return klass->get.f.glyph_h_advance (this, user_data,
252 klass->user_data.glyph_h_advance);
255 hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
257 return klass->get.f.glyph_v_advance (this, user_data,
259 klass->user_data.glyph_v_advance);
262 void get_glyph_h_advances (unsigned int count,
263 const hb_codepoint_t *first_glyph,
264 unsigned int glyph_stride,
265 hb_position_t *first_advance,
266 unsigned int advance_stride)
268 return klass->get.f.glyph_h_advances (this, user_data,
270 first_glyph, glyph_stride,
271 first_advance, advance_stride,
272 klass->user_data.glyph_h_advances);
275 void get_glyph_v_advances (unsigned int count,
276 const hb_codepoint_t *first_glyph,
277 unsigned int glyph_stride,
278 hb_position_t *first_advance,
279 unsigned int advance_stride)
281 return klass->get.f.glyph_v_advances (this, user_data,
283 first_glyph, glyph_stride,
284 first_advance, advance_stride,
285 klass->user_data.glyph_v_advances);
288 hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
289 hb_position_t *x, hb_position_t *y)
292 return klass->get.f.glyph_h_origin (this, user_data,
294 klass->user_data.glyph_h_origin);
297 hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
298 hb_position_t *x, hb_position_t *y)
301 return klass->get.f.glyph_v_origin (this, user_data,
303 klass->user_data.glyph_v_origin);
306 hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph,
307 hb_codepoint_t right_glyph)
309 #ifdef HB_DISABLE_DEPRECATED
312 return klass->get.f.glyph_h_kerning (this, user_data,
313 left_glyph, right_glyph,
314 klass->user_data.glyph_h_kerning);
318 hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph,
319 hb_codepoint_t bottom_glyph)
321 #ifdef HB_DISABLE_DEPRECATED
324 return klass->get.f.glyph_v_kerning (this, user_data,
325 top_glyph, bottom_glyph,
326 klass->user_data.glyph_v_kerning);
330 hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
331 hb_glyph_extents_t *extents)
333 memset (extents, 0, sizeof (*extents));
334 return klass->get.f.glyph_extents (this, user_data,
337 klass->user_data.glyph_extents);
340 hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
341 hb_position_t *x, hb_position_t *y)
344 return klass->get.f.glyph_contour_point (this, user_data,
347 klass->user_data.glyph_contour_point);
350 hb_bool_t get_glyph_name (hb_codepoint_t glyph,
351 char *name, unsigned int size)
353 if (size) *name = '\0';
354 return klass->get.f.glyph_name (this, user_data,
357 klass->user_data.glyph_name);
360 hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
361 hb_codepoint_t *glyph)
364 if (len == -1) len = strlen (name);
365 return klass->get.f.glyph_from_name (this, user_data,
368 klass->user_data.glyph_from_name);
372 /* A bit higher-level, and with fallback */
374 void get_h_extents_with_fallback (hb_font_extents_t *extents)
376 if (!get_font_h_extents (extents))
378 extents->ascender = y_scale * .8;
379 extents->descender = extents->ascender - y_scale;
380 extents->line_gap = 0;
383 void get_v_extents_with_fallback (hb_font_extents_t *extents)
385 if (!get_font_v_extents (extents))
387 extents->ascender = x_scale / 2;
388 extents->descender = extents->ascender - x_scale;
389 extents->line_gap = 0;
393 void get_extents_for_direction (hb_direction_t direction,
394 hb_font_extents_t *extents)
396 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
397 get_h_extents_with_fallback (extents);
399 get_v_extents_with_fallback (extents);
402 void get_glyph_advance_for_direction (hb_codepoint_t glyph,
403 hb_direction_t direction,
404 hb_position_t *x, hb_position_t *y)
407 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
408 *x = get_glyph_h_advance (glyph);
410 *y = get_glyph_v_advance (glyph);
412 void get_glyph_advances_for_direction (hb_direction_t direction,
414 const hb_codepoint_t *first_glyph,
415 unsigned glyph_stride,
416 hb_position_t *first_advance,
417 unsigned advance_stride)
419 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
420 get_glyph_h_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
422 get_glyph_v_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
425 void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
426 hb_position_t *x, hb_position_t *y)
428 *x = get_glyph_h_advance (glyph) / 2;
430 /* TODO cache this somehow?! */
431 hb_font_extents_t extents;
432 get_h_extents_with_fallback (&extents);
433 *y = extents.ascender;
436 void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
437 hb_position_t *x, hb_position_t *y)
439 if (!get_glyph_h_origin (glyph, x, y) &&
440 get_glyph_v_origin (glyph, x, y))
442 hb_position_t dx, dy;
443 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
447 void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
448 hb_position_t *x, hb_position_t *y)
450 if (!get_glyph_v_origin (glyph, x, y) &&
451 get_glyph_h_origin (glyph, x, y))
453 hb_position_t dx, dy;
454 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
459 void get_glyph_origin_for_direction (hb_codepoint_t glyph,
460 hb_direction_t direction,
461 hb_position_t *x, hb_position_t *y)
463 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
464 get_glyph_h_origin_with_fallback (glyph, x, y);
466 get_glyph_v_origin_with_fallback (glyph, x, y);
469 void add_glyph_h_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_h_origin_with_fallback (glyph, &origin_x, &origin_y);
479 void add_glyph_v_origin (hb_codepoint_t glyph,
480 hb_position_t *x, hb_position_t *y)
482 hb_position_t origin_x, origin_y;
484 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
489 void add_glyph_origin_for_direction (hb_codepoint_t glyph,
490 hb_direction_t direction,
491 hb_position_t *x, hb_position_t *y)
493 hb_position_t origin_x, origin_y;
495 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
501 void subtract_glyph_h_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_h_origin_with_fallback (glyph, &origin_x, &origin_y);
511 void subtract_glyph_v_origin (hb_codepoint_t glyph,
512 hb_position_t *x, hb_position_t *y)
514 hb_position_t origin_x, origin_y;
516 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
521 void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
522 hb_direction_t direction,
523 hb_position_t *x, hb_position_t *y)
525 hb_position_t origin_x, origin_y;
527 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
533 void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
534 hb_direction_t direction,
535 hb_position_t *x, hb_position_t *y)
537 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
539 *x = get_glyph_h_kerning (first_glyph, second_glyph);
542 *y = get_glyph_v_kerning (first_glyph, second_glyph);
546 hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
547 hb_direction_t direction,
548 hb_glyph_extents_t *extents)
550 hb_bool_t ret = get_glyph_extents (glyph, extents);
553 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
558 hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
559 hb_direction_t direction,
560 hb_position_t *x, hb_position_t *y)
562 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
565 subtract_glyph_origin_for_direction (glyph, direction, x, y);
570 /* Generates gidDDD if glyph has no name. */
572 glyph_to_string (hb_codepoint_t glyph,
573 char *s, unsigned int size)
575 if (get_glyph_name (glyph, s, size)) return;
577 if (size && snprintf (s, size, "gid%u", glyph) < 0)
581 /* Parses gidDDD and uniUUUU strings automatically. */
583 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
584 hb_codepoint_t *glyph)
586 if (get_glyph_from_name (s, len, glyph)) return true;
588 if (len == -1) len = strlen (s);
590 /* Straight glyph index. */
591 if (hb_codepoint_parse (s, len, 10, glyph))
596 /* gidDDD syntax for glyph indices. */
597 if (0 == strncmp (s, "gid", 3) &&
598 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
601 /* uniUUUU and other Unicode character indices. */
602 hb_codepoint_t unichar;
603 if (0 == strncmp (s, "uni", 3) &&
604 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
605 get_nominal_glyph (unichar, glyph))
612 void mults_changed ()
614 signed upem = face->get_upem ();
615 x_mult = ((int64_t) x_scale << 16) / upem;
616 y_mult = ((int64_t) y_scale << 16) / upem;
619 hb_position_t em_mult (int16_t v, int64_t mult)
621 return (hb_position_t) ((v * mult) >> 16);
623 hb_position_t em_scalef (float v, int scale)
624 { return (hb_position_t) roundf (v * scale / face->get_upem ()); }
625 float em_fscale (int16_t v, int scale)
626 { return (float) v * scale / face->get_upem (); }
628 DECLARE_NULL_INSTANCE (hb_font_t);
631 #endif /* HB_FONT_HH */