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 hb_font_funcs_t *klass;
113 hb_destroy_func_t destroy;
115 struct hb_shaper_data_t shaper_data;
118 /* Convert from font-space to user-space */
119 inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, this->x_scale); }
120 inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, this->y_scale); }
122 /* Convert from parent-font user-space to our user-space */
123 inline hb_position_t parent_scale_x_distance (hb_position_t v) {
124 if (unlikely (parent && parent->x_scale != x_scale))
125 return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
128 inline hb_position_t parent_scale_y_distance (hb_position_t v) {
129 if (unlikely (parent && parent->y_scale != y_scale))
130 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
133 inline hb_position_t parent_scale_x_position (hb_position_t v) {
134 return parent_scale_x_distance (v);
136 inline hb_position_t parent_scale_y_position (hb_position_t v) {
137 return parent_scale_y_distance (v);
140 inline void parent_scale_distance (hb_position_t *x, hb_position_t *y) {
141 *x = parent_scale_x_distance (*x);
142 *y = parent_scale_y_distance (*y);
144 inline void parent_scale_position (hb_position_t *x, hb_position_t *y) {
145 *x = parent_scale_x_position (*x);
146 *y = parent_scale_y_position (*y);
152 HB_INTERNAL bool has_func (unsigned int i);
155 #define HB_FONT_FUNC_IMPLEMENT(name) \
157 has_##name##_func (void) \
159 hb_font_funcs_t *funcs = this->klass; \
160 unsigned int i = offsetof (hb_font_funcs_t::get_t::get_funcs_t, name) / sizeof (funcs->get.array[0]); \
161 return has_func (i); \
163 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
164 #undef HB_FONT_FUNC_IMPLEMENT
166 inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
168 memset (extents, 0, sizeof (*extents));
169 return klass->get.f.font_h_extents (this, user_data,
171 klass->user_data.font_h_extents);
173 inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
175 memset (extents, 0, sizeof (*extents));
176 return klass->get.f.font_v_extents (this, user_data,
178 klass->user_data.font_v_extents);
181 inline bool has_glyph (hb_codepoint_t unicode)
183 hb_codepoint_t glyph;
184 return get_nominal_glyph (unicode, &glyph);
187 inline hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
188 hb_codepoint_t *glyph)
191 return klass->get.f.nominal_glyph (this, user_data,
193 klass->user_data.nominal_glyph);
196 inline hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
197 hb_codepoint_t *glyph)
200 return klass->get.f.variation_glyph (this, user_data,
201 unicode, variation_selector, glyph,
202 klass->user_data.variation_glyph);
205 inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
207 return klass->get.f.glyph_h_advance (this, user_data,
209 klass->user_data.glyph_h_advance);
212 inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
214 return klass->get.f.glyph_v_advance (this, user_data,
216 klass->user_data.glyph_v_advance);
219 inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
220 hb_position_t *x, hb_position_t *y)
223 return klass->get.f.glyph_h_origin (this, user_data,
225 klass->user_data.glyph_h_origin);
228 inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
229 hb_position_t *x, hb_position_t *y)
232 return klass->get.f.glyph_v_origin (this, user_data,
234 klass->user_data.glyph_v_origin);
237 inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
239 return klass->get.f.glyph_h_kerning (this, user_data,
240 left_glyph, right_glyph,
241 klass->user_data.glyph_h_kerning);
244 inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
246 return klass->get.f.glyph_v_kerning (this, user_data,
247 top_glyph, bottom_glyph,
248 klass->user_data.glyph_v_kerning);
251 inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
252 hb_glyph_extents_t *extents)
254 memset (extents, 0, sizeof (*extents));
255 return klass->get.f.glyph_extents (this, user_data,
258 klass->user_data.glyph_extents);
261 inline hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
262 hb_position_t *x, hb_position_t *y)
265 return klass->get.f.glyph_contour_point (this, user_data,
268 klass->user_data.glyph_contour_point);
271 inline hb_bool_t get_glyph_name (hb_codepoint_t glyph,
272 char *name, unsigned int size)
274 if (size) *name = '\0';
275 return klass->get.f.glyph_name (this, user_data,
278 klass->user_data.glyph_name);
281 inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
282 hb_codepoint_t *glyph)
285 if (len == -1) len = strlen (name);
286 return klass->get.f.glyph_from_name (this, user_data,
289 klass->user_data.glyph_from_name);
293 /* A bit higher-level, and with fallback */
295 inline void get_extents_for_direction (hb_direction_t direction,
296 hb_font_extents_t *extents)
298 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
299 if (!get_font_h_extents (extents))
301 extents->ascender = y_scale * .8;
302 extents->descender = y_scale - extents->ascender;
303 extents->line_gap = 0;
306 if (!get_font_v_extents (extents))
308 extents->ascender = x_scale / 2;
309 extents->descender = x_scale - extents->ascender;
310 extents->line_gap = 0;
315 inline void get_glyph_advance_for_direction (hb_codepoint_t glyph,
316 hb_direction_t direction,
317 hb_position_t *x, hb_position_t *y)
319 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
320 *x = get_glyph_h_advance (glyph);
324 *y = get_glyph_v_advance (glyph);
329 inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
330 hb_position_t *x, hb_position_t *y)
332 *x = get_glyph_h_advance (glyph) / 2;
334 /* TODO use font_extents.ascender */
338 inline void get_glyph_origin_for_direction (hb_codepoint_t glyph,
339 hb_direction_t direction,
340 hb_position_t *x, hb_position_t *y)
342 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
344 if (!get_glyph_h_origin (glyph, x, y) &&
345 get_glyph_v_origin (glyph, x, y))
347 hb_position_t dx, dy;
348 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
354 if (!get_glyph_v_origin (glyph, x, y) &&
355 get_glyph_h_origin (glyph, x, y))
357 hb_position_t dx, dy;
358 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
364 inline void add_glyph_h_origin (hb_codepoint_t glyph,
365 hb_position_t *x, hb_position_t *y)
367 hb_position_t origin_x, origin_y;
369 get_glyph_h_origin (glyph, &origin_x, &origin_y);
374 inline void add_glyph_v_origin (hb_codepoint_t glyph,
375 hb_position_t *x, hb_position_t *y)
377 hb_position_t origin_x, origin_y;
379 get_glyph_v_origin (glyph, &origin_x, &origin_y);
384 inline void add_glyph_origin_for_direction (hb_codepoint_t glyph,
385 hb_direction_t direction,
386 hb_position_t *x, hb_position_t *y)
388 hb_position_t origin_x, origin_y;
390 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
396 inline void subtract_glyph_h_origin (hb_codepoint_t glyph,
397 hb_position_t *x, hb_position_t *y)
399 hb_position_t origin_x, origin_y;
401 get_glyph_h_origin (glyph, &origin_x, &origin_y);
406 inline void subtract_glyph_v_origin (hb_codepoint_t glyph,
407 hb_position_t *x, hb_position_t *y)
409 hb_position_t origin_x, origin_y;
411 get_glyph_v_origin (glyph, &origin_x, &origin_y);
416 inline void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
417 hb_direction_t direction,
418 hb_position_t *x, hb_position_t *y)
420 hb_position_t origin_x, origin_y;
422 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
428 inline void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
429 hb_direction_t direction,
430 hb_position_t *x, hb_position_t *y)
432 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
433 *x = get_glyph_h_kerning (first_glyph, second_glyph);
437 *y = get_glyph_v_kerning (first_glyph, second_glyph);
441 inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
442 hb_direction_t direction,
443 hb_glyph_extents_t *extents)
445 hb_bool_t ret = get_glyph_extents (glyph, extents);
448 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
453 inline hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
454 hb_direction_t direction,
455 hb_position_t *x, hb_position_t *y)
457 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
460 subtract_glyph_origin_for_direction (glyph, direction, x, y);
465 /* Generates gidDDD if glyph has no name. */
467 glyph_to_string (hb_codepoint_t glyph,
468 char *s, unsigned int size)
470 if (get_glyph_name (glyph, s, size)) return;
472 if (size && snprintf (s, size, "gid%u", glyph) < 0)
476 /* Parses gidDDD and uniUUUU strings automatically. */
478 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
479 hb_codepoint_t *glyph)
481 if (get_glyph_from_name (s, len, glyph)) return true;
483 if (len == -1) len = strlen (s);
485 /* Straight glyph index. */
486 if (hb_codepoint_parse (s, len, 10, glyph))
491 /* gidDDD syntax for glyph indices. */
492 if (0 == strncmp (s, "gid", 3) &&
493 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
496 /* uniUUUU and other Unicode character indices. */
497 hb_codepoint_t unichar;
498 if (0 == strncmp (s, "uni", 3) &&
499 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
500 get_nominal_glyph (unichar, glyph))
508 inline hb_position_t em_scale (int16_t v, int scale) { return (hb_position_t) (v * (int64_t) scale / face->get_upem ()); }
511 #define HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
512 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_PROTOTYPE(shaper, font);
513 #include "hb-shaper-list.hh"
514 #undef HB_SHAPER_IMPLEMENT
515 #undef HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
518 #endif /* HB_FONT_PRIVATE_HH */