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
29 #ifndef HB_FONT_PRIVATE_HH
30 #define HB_FONT_PRIVATE_HH
32 #include "hb-private.hh"
34 #include "hb-object-private.hh"
35 #include "hb-face-private.hh"
36 #include "hb-shaper-private.hh"
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 */
61 struct hb_font_funcs_t {
62 hb_object_header_t header;
68 #define HB_FONT_FUNC_IMPLEMENT(name) void *name;
69 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
70 #undef HB_FONT_FUNC_IMPLEMENT
74 #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
75 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
76 #undef HB_FONT_FUNC_IMPLEMENT
79 /* Don't access these directly. Call font->get_*() instead. */
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
86 void (*array[VAR]) (void);
97 hb_object_header_t header;
111 /* Font variation coordinates. */
112 unsigned int num_coords;
115 hb_font_funcs_t *klass;
117 hb_destroy_func_t destroy;
129 struct hb_shaper_data_t shaper_data;
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)); }
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);
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);
153 inline hb_position_t parent_scale_x_position (hb_position_t v) {
154 return parent_scale_x_distance (v);
156 inline hb_position_t parent_scale_y_position (hb_position_t v) {
157 return parent_scale_y_distance (v);
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);
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);
172 HB_INTERNAL bool has_func (unsigned int i);
175 #define HB_FONT_FUNC_IMPLEMENT(name) \
177 has_##name##_func (void) \
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); \
183 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
184 #undef HB_FONT_FUNC_IMPLEMENT
186 inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
188 memset (extents, 0, sizeof (*extents));
189 return klass->get.f.font_h_extents (this, user_data,
191 klass->user_data.font_h_extents);
193 inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
195 memset (extents, 0, sizeof (*extents));
196 return klass->get.f.font_v_extents (this, user_data,
198 klass->user_data.font_v_extents);
201 inline bool has_glyph (hb_codepoint_t unicode)
203 hb_codepoint_t glyph;
204 return get_nominal_glyph (unicode, &glyph);
207 inline hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
208 hb_codepoint_t *glyph)
211 return klass->get.f.nominal_glyph (this, user_data,
213 klass->user_data.nominal_glyph);
216 inline hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
217 hb_codepoint_t *glyph)
220 return klass->get.f.variation_glyph (this, user_data,
221 unicode, variation_selector, glyph,
222 klass->user_data.variation_glyph);
225 inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
227 return klass->get.f.glyph_h_advance (this, user_data,
229 klass->user_data.glyph_h_advance);
232 inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
234 return klass->get.f.glyph_v_advance (this, user_data,
236 klass->user_data.glyph_v_advance);
239 inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
240 hb_position_t *x, hb_position_t *y)
243 return klass->get.f.glyph_h_origin (this, user_data,
245 klass->user_data.glyph_h_origin);
248 inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
249 hb_position_t *x, hb_position_t *y)
252 return klass->get.f.glyph_v_origin (this, user_data,
254 klass->user_data.glyph_v_origin);
257 inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
259 return klass->get.f.glyph_h_kerning (this, user_data,
260 left_glyph, right_glyph,
261 klass->user_data.glyph_h_kerning);
264 inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
266 return klass->get.f.glyph_v_kerning (this, user_data,
267 top_glyph, bottom_glyph,
268 klass->user_data.glyph_v_kerning);
271 inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
272 hb_glyph_extents_t *extents)
274 memset (extents, 0, sizeof (*extents));
275 return klass->get.f.glyph_extents (this, user_data,
278 klass->user_data.glyph_extents);
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)
285 return klass->get.f.glyph_contour_point (this, user_data,
288 klass->user_data.glyph_contour_point);
291 inline hb_bool_t get_glyph_name (hb_codepoint_t glyph,
292 char *name, unsigned int size)
294 if (size) *name = '\0';
295 return klass->get.f.glyph_name (this, user_data,
298 klass->user_data.glyph_name);
301 inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
302 hb_codepoint_t *glyph)
305 if (len == -1) len = strlen (name);
306 return klass->get.f.glyph_from_name (this, user_data,
309 klass->user_data.glyph_from_name);
313 /* A bit higher-level, and with fallback */
315 inline void get_h_extents_with_fallback (hb_font_extents_t *extents)
317 if (!get_font_h_extents (extents))
319 extents->ascender = y_scale * .8;
320 extents->descender = extents->ascender - y_scale;
321 extents->line_gap = 0;
324 inline void get_v_extents_with_fallback (hb_font_extents_t *extents)
326 if (!get_font_v_extents (extents))
328 extents->ascender = x_scale / 2;
329 extents->descender = extents->ascender - x_scale;
330 extents->line_gap = 0;
334 inline void get_extents_for_direction (hb_direction_t direction,
335 hb_font_extents_t *extents)
337 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
338 get_h_extents_with_fallback (extents);
340 get_v_extents_with_fallback (extents);
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)
347 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
348 *x = get_glyph_h_advance (glyph);
352 *y = get_glyph_v_advance (glyph);
356 inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
357 hb_position_t *x, hb_position_t *y)
359 *x = get_glyph_h_advance (glyph) / 2;
361 /* TODO cache this somehow?! */
362 hb_font_extents_t extents;
363 get_h_extents_with_fallback (&extents);
364 *y = extents.ascender;
367 inline void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
368 hb_position_t *x, hb_position_t *y)
370 if (!get_glyph_h_origin (glyph, x, y) &&
371 get_glyph_v_origin (glyph, x, y))
373 hb_position_t dx, dy;
374 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
378 inline void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
379 hb_position_t *x, hb_position_t *y)
381 if (!get_glyph_v_origin (glyph, x, y) &&
382 get_glyph_h_origin (glyph, x, y))
384 hb_position_t dx, dy;
385 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
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)
394 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
395 get_glyph_h_origin_with_fallback (glyph, x, y);
397 get_glyph_v_origin_with_fallback (glyph, x, y);
400 inline void add_glyph_h_origin (hb_codepoint_t glyph,
401 hb_position_t *x, hb_position_t *y)
403 hb_position_t origin_x, origin_y;
405 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
410 inline void add_glyph_v_origin (hb_codepoint_t glyph,
411 hb_position_t *x, hb_position_t *y)
413 hb_position_t origin_x, origin_y;
415 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
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)
424 hb_position_t origin_x, origin_y;
426 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
432 inline void subtract_glyph_h_origin (hb_codepoint_t glyph,
433 hb_position_t *x, hb_position_t *y)
435 hb_position_t origin_x, origin_y;
437 get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
442 inline void subtract_glyph_v_origin (hb_codepoint_t glyph,
443 hb_position_t *x, hb_position_t *y)
445 hb_position_t origin_x, origin_y;
447 get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
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)
456 hb_position_t origin_x, origin_y;
458 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
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)
468 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
469 *x = get_glyph_h_kerning (first_glyph, second_glyph);
473 *y = get_glyph_v_kerning (first_glyph, second_glyph);
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)
481 hb_bool_t ret = get_glyph_extents (glyph, extents);
484 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
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)
493 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
496 subtract_glyph_origin_for_direction (glyph, direction, x, y);
501 /* Generates gidDDD if glyph has no name. */
503 glyph_to_string (hb_codepoint_t glyph,
504 char *s, unsigned int size)
506 if (get_glyph_name (glyph, s, size)) return;
508 if (size && snprintf (s, size, "gid%u", glyph) < 0)
512 /* Parses gidDDD and uniUUUU strings automatically. */
514 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
515 hb_codepoint_t *glyph)
517 if (get_glyph_from_name (s, len, glyph)) return true;
519 if (len == -1) len = strlen (s);
521 /* Straight glyph index. */
522 if (hb_codepoint_parse (s, len, 10, glyph))
527 /* gidDDD syntax for glyph indices. */
528 if (0 == strncmp (s, "gid", 3) &&
529 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
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))
543 inline hb_position_t em_scale (int16_t v, int scale)
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);
550 inline hb_position_t em_scalef (float v, int scale)
552 return (hb_position_t) (v * scale / face->get_upem ());
556 HB_MARK_AS_FLAG_T (hb_font_t::dirty_t);
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
565 #endif /* HB_FONT_PRIVATE_HH */