Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-font-private.hh
blobaa6c515bcb2fd92bc2ce8ac36c8d5b5cd27b5eb3
1 /*
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
17 * DAMAGE.
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-font.h"
35 #include "hb-object-private.hh"
36 #include "hb-face-private.hh"
37 #include "hb-shaper-private.hh"
42 * hb_font_funcs_t
45 #define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \
46 HB_FONT_FUNC_IMPLEMENT (glyph) \
47 HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \
48 HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \
49 HB_FONT_FUNC_IMPLEMENT (glyph_h_origin) \
50 HB_FONT_FUNC_IMPLEMENT (glyph_v_origin) \
51 HB_FONT_FUNC_IMPLEMENT (glyph_h_kerning) \
52 HB_FONT_FUNC_IMPLEMENT (glyph_v_kerning) \
53 HB_FONT_FUNC_IMPLEMENT (glyph_extents) \
54 HB_FONT_FUNC_IMPLEMENT (glyph_contour_point) \
55 HB_FONT_FUNC_IMPLEMENT (glyph_name) \
56 HB_FONT_FUNC_IMPLEMENT (glyph_from_name) \
57 /* ^--- Add new callbacks here */
59 struct hb_font_funcs_t {
60 hb_object_header_t header;
61 ASSERT_POD ();
63 hb_bool_t immutable;
65 /* Don't access these directly. Call hb_font_get_*() instead. */
67 struct {
68 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_func_t name;
69 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
70 #undef HB_FONT_FUNC_IMPLEMENT
71 } get;
73 struct {
74 #define HB_FONT_FUNC_IMPLEMENT(name) void *name;
75 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
76 #undef HB_FONT_FUNC_IMPLEMENT
77 } user_data;
79 struct {
80 #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
81 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
82 #undef HB_FONT_FUNC_IMPLEMENT
83 } destroy;
89 * hb_font_t
92 struct hb_font_t {
93 hb_object_header_t header;
94 ASSERT_POD ();
96 hb_bool_t immutable;
98 hb_font_t *parent;
99 hb_face_t *face;
101 int x_scale;
102 int y_scale;
104 unsigned int x_ppem;
105 unsigned int y_ppem;
107 hb_font_funcs_t *klass;
108 void *user_data;
109 hb_destroy_func_t destroy;
111 struct hb_shaper_data_t shaper_data;
114 /* Convert from font-space to user-space */
115 inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, this->x_scale); }
116 inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, this->y_scale); }
118 /* Convert from parent-font user-space to our user-space */
119 inline hb_position_t parent_scale_x_distance (hb_position_t v) {
120 if (unlikely (parent && parent->x_scale != x_scale))
121 return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
122 return v;
124 inline hb_position_t parent_scale_y_distance (hb_position_t v) {
125 if (unlikely (parent && parent->y_scale != y_scale))
126 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
127 return v;
129 inline hb_position_t parent_scale_x_position (hb_position_t v) {
130 return parent_scale_x_distance (v);
132 inline hb_position_t parent_scale_y_position (hb_position_t v) {
133 return parent_scale_y_distance (v);
136 inline void parent_scale_distance (hb_position_t *x, hb_position_t *y) {
137 *x = parent_scale_x_distance (*x);
138 *y = parent_scale_y_distance (*y);
140 inline void parent_scale_position (hb_position_t *x, hb_position_t *y) {
141 *x = parent_scale_x_position (*x);
142 *y = parent_scale_y_position (*y);
146 /* Public getters */
148 inline hb_bool_t get_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
149 hb_codepoint_t *glyph)
151 *glyph = 0;
152 return klass->get.glyph (this, user_data,
153 unicode, variation_selector, glyph,
154 klass->user_data.glyph);
157 inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
159 return klass->get.glyph_h_advance (this, user_data,
160 glyph,
161 klass->user_data.glyph_h_advance);
164 inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
166 return klass->get.glyph_v_advance (this, user_data,
167 glyph,
168 klass->user_data.glyph_v_advance);
171 inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
172 hb_position_t *x, hb_position_t *y)
174 *x = *y = 0;
175 return klass->get.glyph_h_origin (this, user_data,
176 glyph, x, y,
177 klass->user_data.glyph_h_origin);
180 inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
181 hb_position_t *x, hb_position_t *y)
183 *x = *y = 0;
184 return klass->get.glyph_v_origin (this, user_data,
185 glyph, x, y,
186 klass->user_data.glyph_v_origin);
189 inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
191 return klass->get.glyph_h_kerning (this, user_data,
192 left_glyph, right_glyph,
193 klass->user_data.glyph_h_kerning);
196 inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
198 return klass->get.glyph_v_kerning (this, user_data,
199 top_glyph, bottom_glyph,
200 klass->user_data.glyph_v_kerning);
203 inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
204 hb_glyph_extents_t *extents)
206 memset (extents, 0, sizeof (*extents));
207 return klass->get.glyph_extents (this, user_data,
208 glyph,
209 extents,
210 klass->user_data.glyph_extents);
213 inline hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
214 hb_position_t *x, hb_position_t *y)
216 *x = *y = 0;
217 return klass->get.glyph_contour_point (this, user_data,
218 glyph, point_index,
219 x, y,
220 klass->user_data.glyph_contour_point);
223 inline hb_bool_t get_glyph_name (hb_codepoint_t glyph,
224 char *name, unsigned int size)
226 if (size) *name = '\0';
227 return klass->get.glyph_name (this, user_data,
228 glyph,
229 name, size,
230 klass->user_data.glyph_name);
233 inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
234 hb_codepoint_t *glyph)
236 *glyph = 0;
237 if (len == -1) len = strlen (name);
238 return klass->get.glyph_from_name (this, user_data,
239 name, len,
240 glyph,
241 klass->user_data.glyph_from_name);
245 /* A bit higher-level, and with fallback */
247 inline void get_glyph_advance_for_direction (hb_codepoint_t glyph,
248 hb_direction_t direction,
249 hb_position_t *x, hb_position_t *y)
251 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
252 *x = get_glyph_h_advance (glyph);
253 *y = 0;
254 } else {
255 *x = 0;
256 *y = get_glyph_v_advance (glyph);
260 /* Internal only */
261 inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
262 hb_position_t *x, hb_position_t *y)
264 *x = get_glyph_h_advance (glyph) / 2;
266 /* TODO use font_metics.ascent */
267 *y = y_scale;
270 inline void get_glyph_origin_for_direction (hb_codepoint_t glyph,
271 hb_direction_t direction,
272 hb_position_t *x, hb_position_t *y)
274 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
276 if (!get_glyph_h_origin (glyph, x, y) &&
277 get_glyph_v_origin (glyph, x, y))
279 hb_position_t dx, dy;
280 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
281 *x -= dx; *y -= dy;
284 else
286 if (!get_glyph_v_origin (glyph, x, y) &&
287 get_glyph_h_origin (glyph, x, y))
289 hb_position_t dx, dy;
290 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
291 *x += dx; *y += dy;
296 inline void add_glyph_origin_for_direction (hb_codepoint_t glyph,
297 hb_direction_t direction,
298 hb_position_t *x, hb_position_t *y)
300 hb_position_t origin_x, origin_y;
302 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
304 *x += origin_x;
305 *y += origin_y;
308 inline void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
309 hb_direction_t direction,
310 hb_position_t *x, hb_position_t *y)
312 hb_position_t origin_x, origin_y;
314 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
316 *x -= origin_x;
317 *y -= origin_y;
320 inline void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
321 hb_direction_t direction,
322 hb_position_t *x, hb_position_t *y)
324 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
325 *x = get_glyph_h_kerning (first_glyph, second_glyph);
326 *y = 0;
327 } else {
328 *x = 0;
329 *y = get_glyph_v_kerning (first_glyph, second_glyph);
333 inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
334 hb_direction_t direction,
335 hb_glyph_extents_t *extents)
337 hb_bool_t ret = get_glyph_extents (glyph, extents);
339 if (ret)
340 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
342 return ret;
345 inline hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
346 hb_direction_t direction,
347 hb_position_t *x, hb_position_t *y)
349 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
351 if (ret)
352 subtract_glyph_origin_for_direction (glyph, direction, x, y);
354 return ret;
357 /* Generates gidDDD if glyph has no name. */
358 inline void
359 glyph_to_string (hb_codepoint_t glyph,
360 char *s, unsigned int size)
362 if (get_glyph_name (glyph, s, size)) return;
364 if (size && snprintf (s, size, "gid%u", glyph) < 0)
365 *s = '\0';
368 /* Parses gidDDD and uniUUUU strings automatically. */
369 inline hb_bool_t
370 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
371 hb_codepoint_t *glyph)
373 if (get_glyph_from_name (s, len, glyph)) return true;
375 if (len == -1) len = strlen (s);
377 /* Straight glyph index. */
378 if (hb_codepoint_parse (s, len, 10, glyph))
379 return true;
381 if (len > 3)
383 /* gidDDD syntax for glyph indices. */
384 if (0 == strncmp (s, "gid", 3) &&
385 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
386 return true;
388 /* uniUUUU and other Unicode character indices. */
389 hb_codepoint_t unichar;
390 if (0 == strncmp (s, "uni", 3) &&
391 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
392 get_glyph (unichar, 0, glyph))
393 return true;
396 return false;
399 private:
400 inline hb_position_t em_scale (int16_t v, int scale) { return (hb_position_t) (v * (int64_t) scale / face->get_upem ()); }
403 #define HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
404 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_PROTOTYPE(shaper, font);
405 #include "hb-shaper-list.hh"
406 #undef HB_SHAPER_IMPLEMENT
407 #undef HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
410 #endif /* HB_FONT_PRIVATE_HH */