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;
123 float *design_coords;
125 hb_font_funcs_t *klass;
127 hb_destroy_func_t destroy;
129 hb_shaper_object_dataset_t<hb_font_t> data; /* Various shaper data. */
132 /* Convert from font-space to user-space */
133 int64_t dir_mult (hb_direction_t direction)
134 { return HB_DIRECTION_IS_VERTICAL(direction) ? y_mult : x_mult; }
135 hb_position_t em_scale_x (int16_t v) { return em_mult (v, x_mult); }
136 hb_position_t em_scale_y (int16_t v) { return em_mult (v, y_mult); }
137 hb_position_t em_scalef_x (float v) { return em_scalef (v, x_scale); }
138 hb_position_t em_scalef_y (float v) { return em_scalef (v, y_scale); }
139 float em_fscale_x (int16_t v) { return em_fscale (v, x_scale); }
140 float em_fscale_y (int16_t v) { return em_fscale (v, y_scale); }
141 hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
142 { return em_mult (v, dir_mult (direction)); }
144 /* Convert from parent-font user-space to our user-space */
145 hb_position_t parent_scale_x_distance (hb_position_t v)
147 if (unlikely (parent && parent->x_scale != x_scale))
148 return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
151 hb_position_t parent_scale_y_distance (hb_position_t v)
153 if (unlikely (parent && parent->y_scale != y_scale))
154 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
157 hb_position_t parent_scale_x_position (hb_position_t v)
158 { return parent_scale_x_distance (v); }
159 hb_position_t parent_scale_y_position (hb_position_t v)
160 { return parent_scale_y_distance (v); }
162 void parent_scale_distance (hb_position_t *x, hb_position_t *y)
164 *x = parent_scale_x_distance (*x);
165 *y = parent_scale_y_distance (*y);
167 void parent_scale_position (hb_position_t *x, hb_position_t *y)
169 *x = parent_scale_x_position (*x);
170 *y = parent_scale_y_position (*y);
176 HB_INTERNAL bool has_func (unsigned int i);
177 HB_INTERNAL bool has_func_set (unsigned int i);
180 #define HB_FONT_FUNC_IMPLEMENT(name) \
182 has_##name##_func () \
184 hb_font_funcs_t *funcs = this->klass; \
185 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
186 return has_func (i); \
189 has_##name##_func_set () \
191 hb_font_funcs_t *funcs = this->klass; \
192 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
193 return has_func_set (i); \
195 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
196 #undef HB_FONT_FUNC_IMPLEMENT
198 hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
200 memset (extents, 0, sizeof (*extents));
201 return klass->get.f.font_h_extents (this, user_data,
203 klass->user_data.font_h_extents);
205 hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
207 memset (extents, 0, sizeof (*extents));
208 return klass->get.f.font_v_extents (this, user_data,
210 klass->user_data.font_v_extents);
213 bool has_glyph (hb_codepoint_t unicode)
215 hb_codepoint_t glyph;
216 return get_nominal_glyph (unicode, &glyph);
219 hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
220 hb_codepoint_t *glyph)
223 return klass->get.f.nominal_glyph (this, user_data,
225 klass->user_data.nominal_glyph);
227 unsigned int get_nominal_glyphs (unsigned int count,
228 const hb_codepoint_t *first_unicode,
229 unsigned int unicode_stride,
230 hb_codepoint_t *first_glyph,
231 unsigned int glyph_stride)
233 return klass->get.f.nominal_glyphs (this, user_data,
235 first_unicode, unicode_stride,
236 first_glyph, glyph_stride,
237 klass->user_data.nominal_glyphs);
240 hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
241 hb_codepoint_t *glyph)
244 return klass->get.f.variation_glyph (this, user_data,
245 unicode, variation_selector, glyph,
246 klass->user_data.variation_glyph);
249 hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
251 return klass->get.f.glyph_h_advance (this, user_data,
253 klass->user_data.glyph_h_advance);
256 hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
258 return klass->get.f.glyph_v_advance (this, user_data,
260 klass->user_data.glyph_v_advance);
263 void get_glyph_h_advances (unsigned int count,
264 const hb_codepoint_t *first_glyph,
265 unsigned int glyph_stride,
266 hb_position_t *first_advance,
267 unsigned int advance_stride)
269 return klass->get.f.glyph_h_advances (this, user_data,
271 first_glyph, glyph_stride,
272 first_advance, advance_stride,
273 klass->user_data.glyph_h_advances);
276 void get_glyph_v_advances (unsigned int count,
277 const hb_codepoint_t *first_glyph,
278 unsigned int glyph_stride,
279 hb_position_t *first_advance,
280 unsigned int advance_stride)
282 return klass->get.f.glyph_v_advances (this, user_data,
284 first_glyph, glyph_stride,
285 first_advance, advance_stride,
286 klass->user_data.glyph_v_advances);
289 hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
290 hb_position_t *x, hb_position_t *y)
293 return klass->get.f.glyph_h_origin (this, user_data,
295 klass->user_data.glyph_h_origin);
298 hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
299 hb_position_t *x, hb_position_t *y)
302 return klass->get.f.glyph_v_origin (this, user_data,
304 klass->user_data.glyph_v_origin);
307 hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph,
308 hb_codepoint_t right_glyph)
310 #ifdef HB_DISABLE_DEPRECATED
313 return klass->get.f.glyph_h_kerning (this, user_data,
314 left_glyph, right_glyph,
315 klass->user_data.glyph_h_kerning);
319 hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph,
320 hb_codepoint_t bottom_glyph)
322 #ifdef HB_DISABLE_DEPRECATED
325 return klass->get.f.glyph_v_kerning (this, user_data,
326 top_glyph, bottom_glyph,
327 klass->user_data.glyph_v_kerning);
331 hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
332 hb_glyph_extents_t *extents)
334 memset (extents, 0, sizeof (*extents));
335 return klass->get.f.glyph_extents (this, user_data,
338 klass->user_data.glyph_extents);
341 hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
342 hb_position_t *x, hb_position_t *y)
345 return klass->get.f.glyph_contour_point (this, user_data,
348 klass->user_data.glyph_contour_point);
351 hb_bool_t get_glyph_name (hb_codepoint_t glyph,
352 char *name, unsigned int size)
354 if (size) *name = '\0';
355 return klass->get.f.glyph_name (this, user_data,
358 klass->user_data.glyph_name);
361 hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
362 hb_codepoint_t *glyph)
365 if (len == -1) len = strlen (name);
366 return klass->get.f.glyph_from_name (this, user_data,
369 klass->user_data.glyph_from_name);
373 /* A bit higher-level, and with fallback */
375 void get_h_extents_with_fallback (hb_font_extents_t *extents)
377 if (!get_font_h_extents (extents))
379 extents->ascender = y_scale * .8;
380 extents->descender = extents->ascender - y_scale;
381 extents->line_gap = 0;
384 void get_v_extents_with_fallback (hb_font_extents_t *extents)
386 if (!get_font_v_extents (extents))
388 extents->ascender = x_scale / 2;
389 extents->descender = extents->ascender - x_scale;
390 extents->line_gap = 0;
394 void get_extents_for_direction (hb_direction_t direction,
395 hb_font_extents_t *extents)
397 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
398 get_h_extents_with_fallback (extents);
400 get_v_extents_with_fallback (extents);
403 void get_glyph_advance_for_direction (hb_codepoint_t glyph,
404 hb_direction_t direction,
405 hb_position_t *x, hb_position_t *y)
408 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
409 *x = get_glyph_h_advance (glyph);
411 *y = get_glyph_v_advance (glyph);
413 void get_glyph_advances_for_direction (hb_direction_t direction,
415 const hb_codepoint_t *first_glyph,
416 unsigned glyph_stride,
417 hb_position_t *first_advance,
418 unsigned advance_stride)
420 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
421 get_glyph_h_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
423 get_glyph_v_advances (count, first_glyph, glyph_stride, first_advance, advance_stride);
426 void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
427 hb_position_t *x, hb_position_t *y)
429 *x = get_glyph_h_advance (glyph) / 2;
431 /* TODO cache this somehow?! */
432 hb_font_extents_t extents;
433 get_h_extents_with_fallback (&extents);
434 *y = extents.ascender;
437 void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
438 hb_position_t *x, hb_position_t *y)
440 if (!get_glyph_h_origin (glyph, x, y) &&
441 get_glyph_v_origin (glyph, x, y))
443 hb_position_t dx, dy;
444 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
448 void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
449 hb_position_t *x, hb_position_t *y)
451 if (!get_glyph_v_origin (glyph, x, y) &&
452 get_glyph_h_origin (glyph, x, y))
454 hb_position_t dx, dy;
455 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
460 void get_glyph_origin_for_direction (hb_codepoint_t glyph,
461 hb_direction_t direction,
462 hb_position_t *x, hb_position_t *y)
464 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
465 get_glyph_h_origin_with_fallback (glyph, x, y);
467 get_glyph_v_origin_with_fallback (glyph, x, y);
470 void add_glyph_h_origin (hb_codepoint_t glyph,
471 hb_position_t *x, hb_position_t *y)
473 hb_position_t origin_x, origin_y;
475 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
480 void add_glyph_v_origin (hb_codepoint_t glyph,
481 hb_position_t *x, hb_position_t *y)
483 hb_position_t origin_x, origin_y;
485 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
490 void add_glyph_origin_for_direction (hb_codepoint_t glyph,
491 hb_direction_t direction,
492 hb_position_t *x, hb_position_t *y)
494 hb_position_t origin_x, origin_y;
496 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
502 void subtract_glyph_h_origin (hb_codepoint_t glyph,
503 hb_position_t *x, hb_position_t *y)
505 hb_position_t origin_x, origin_y;
507 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
512 void subtract_glyph_v_origin (hb_codepoint_t glyph,
513 hb_position_t *x, hb_position_t *y)
515 hb_position_t origin_x, origin_y;
517 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
522 void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
523 hb_direction_t direction,
524 hb_position_t *x, hb_position_t *y)
526 hb_position_t origin_x, origin_y;
528 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
534 void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
535 hb_direction_t direction,
536 hb_position_t *x, hb_position_t *y)
538 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
540 *x = get_glyph_h_kerning (first_glyph, second_glyph);
543 *y = get_glyph_v_kerning (first_glyph, second_glyph);
547 hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
548 hb_direction_t direction,
549 hb_glyph_extents_t *extents)
551 hb_bool_t ret = get_glyph_extents (glyph, extents);
554 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
559 hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
560 hb_direction_t direction,
561 hb_position_t *x, hb_position_t *y)
563 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
566 subtract_glyph_origin_for_direction (glyph, direction, x, y);
571 /* Generates gidDDD if glyph has no name. */
573 glyph_to_string (hb_codepoint_t glyph,
574 char *s, unsigned int size)
576 if (get_glyph_name (glyph, s, size)) return;
578 if (size && snprintf (s, size, "gid%u", glyph) < 0)
582 /* Parses gidDDD and uniUUUU strings automatically. */
584 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
585 hb_codepoint_t *glyph)
587 if (get_glyph_from_name (s, len, glyph)) return true;
589 if (len == -1) len = strlen (s);
591 /* Straight glyph index. */
592 if (hb_codepoint_parse (s, len, 10, glyph))
597 /* gidDDD syntax for glyph indices. */
598 if (0 == strncmp (s, "gid", 3) &&
599 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
602 /* uniUUUU and other Unicode character indices. */
603 hb_codepoint_t unichar;
604 if (0 == strncmp (s, "uni", 3) &&
605 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
606 get_nominal_glyph (unichar, glyph))
613 void mults_changed ()
615 signed upem = face->get_upem ();
616 x_mult = ((int64_t) x_scale << 16) / upem;
617 y_mult = ((int64_t) y_scale << 16) / upem;
620 hb_position_t em_mult (int16_t v, int64_t mult)
622 return (hb_position_t) ((v * mult) >> 16);
624 hb_position_t em_scalef (float v, int scale)
625 { return (hb_position_t) roundf (v * scale / face->get_upem ()); }
626 float em_fscale (int16_t v, int scale)
627 { return (float) v * scale / face->get_upem (); }
629 DECLARE_NULL_INSTANCE (hb_font_t);
632 #endif /* HB_FONT_HH */