Line data Source code
1 : /*
2 : * Copyright © 2009 Red Hat, Inc.
3 : * Copyright © 2011 Google, Inc.
4 : *
5 : * This is part of HarfBuzz, a text shaping library.
6 : *
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.
12 : *
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.
18 : *
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.
24 : *
25 : * Red Hat Author(s): Behdad Esfahbod
26 : * Google Author(s): Behdad Esfahbod
27 : */
28 :
29 : #ifndef HB_FONT_PRIVATE_HH
30 : #define HB_FONT_PRIVATE_HH
31 :
32 : #include "hb-private.hh"
33 :
34 : #include "hb-object-private.hh"
35 : #include "hb-face-private.hh"
36 : #include "hb-shaper-private.hh"
37 :
38 :
39 :
40 : /*
41 : * hb_font_funcs_t
42 : */
43 :
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 */
60 :
61 : struct hb_font_funcs_t {
62 : hb_object_header_t header;
63 : ASSERT_POD ();
64 :
65 : hb_bool_t immutable;
66 :
67 : struct {
68 : #define HB_FONT_FUNC_IMPLEMENT(name) void *name;
69 : HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
70 : #undef HB_FONT_FUNC_IMPLEMENT
71 : } user_data;
72 :
73 : struct {
74 : #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
75 : HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
76 : #undef HB_FONT_FUNC_IMPLEMENT
77 : } destroy;
78 :
79 : /* Don't access these directly. Call font->get_*() instead. */
80 : union get_t {
81 : struct get_funcs_t {
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
85 : } f;
86 : void (*array[VAR]) (void);
87 : } get;
88 : };
89 :
90 :
91 :
92 : /*
93 : * hb_font_t
94 : */
95 :
96 : struct hb_font_t {
97 : hb_object_header_t header;
98 : ASSERT_POD ();
99 :
100 : hb_bool_t immutable;
101 :
102 : hb_font_t *parent;
103 : hb_face_t *face;
104 :
105 : int x_scale;
106 : int y_scale;
107 :
108 : unsigned int x_ppem;
109 : unsigned int y_ppem;
110 :
111 : /* Font variation coordinates. */
112 : unsigned int num_coords;
113 : int *coords;
114 :
115 : hb_font_funcs_t *klass;
116 : void *user_data;
117 : hb_destroy_func_t destroy;
118 :
119 : enum dirty_t {
120 : NOTHING = 0x0000,
121 : FACE = 0x0001,
122 : PARENT = 0x0002,
123 : FUNCS = 0x0004,
124 : SCALE = 0x0008,
125 : PPEM = 0x0010,
126 : VARIATIONS = 0x0020,
127 : } dirty;
128 :
129 : struct hb_shaper_data_t shaper_data;
130 :
131 :
132 : /* Convert from font-space to user-space */
133 0 : inline int dir_scale (hb_direction_t direction)
134 0 : { return HB_DIRECTION_IS_VERTICAL(direction) ? y_scale : x_scale; }
135 185 : inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, x_scale); }
136 0 : inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, y_scale); }
137 0 : inline hb_position_t em_scalef_x (float v) { return em_scalef (v, this->x_scale); }
138 0 : inline hb_position_t em_scalef_y (float v) { return em_scalef (v, this->y_scale); }
139 0 : inline hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
140 0 : { return em_scale (v, dir_scale (direction)); }
141 :
142 : /* Convert from parent-font user-space to our user-space */
143 0 : inline hb_position_t parent_scale_x_distance (hb_position_t v) {
144 0 : if (unlikely (parent && parent->x_scale != x_scale))
145 0 : return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
146 0 : return v;
147 : }
148 0 : inline hb_position_t parent_scale_y_distance (hb_position_t v) {
149 0 : if (unlikely (parent && parent->y_scale != y_scale))
150 0 : return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
151 0 : return v;
152 : }
153 0 : inline hb_position_t parent_scale_x_position (hb_position_t v) {
154 0 : return parent_scale_x_distance (v);
155 : }
156 0 : inline hb_position_t parent_scale_y_position (hb_position_t v) {
157 0 : return parent_scale_y_distance (v);
158 : }
159 :
160 0 : inline void parent_scale_distance (hb_position_t *x, hb_position_t *y) {
161 0 : *x = parent_scale_x_distance (*x);
162 0 : *y = parent_scale_y_distance (*y);
163 0 : }
164 0 : inline void parent_scale_position (hb_position_t *x, hb_position_t *y) {
165 0 : *x = parent_scale_x_position (*x);
166 0 : *y = parent_scale_y_position (*y);
167 0 : }
168 :
169 :
170 : /* Public getters */
171 :
172 : HB_INTERNAL bool has_func (unsigned int i);
173 :
174 : /* has_* ... */
175 : #define HB_FONT_FUNC_IMPLEMENT(name) \
176 : bool \
177 : has_##name##_func (void) \
178 : { \
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); \
182 : }
183 102 : HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
184 : #undef HB_FONT_FUNC_IMPLEMENT
185 :
186 0 : inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
187 : {
188 0 : memset (extents, 0, sizeof (*extents));
189 0 : return klass->get.f.font_h_extents (this, user_data,
190 : extents,
191 0 : klass->user_data.font_h_extents);
192 : }
193 0 : inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
194 : {
195 0 : memset (extents, 0, sizeof (*extents));
196 0 : return klass->get.f.font_v_extents (this, user_data,
197 : extents,
198 0 : klass->user_data.font_v_extents);
199 : }
200 :
201 0 : inline bool has_glyph (hb_codepoint_t unicode)
202 : {
203 : hb_codepoint_t glyph;
204 0 : return get_nominal_glyph (unicode, &glyph);
205 : }
206 :
207 265 : inline hb_bool_t get_nominal_glyph (hb_codepoint_t unicode,
208 : hb_codepoint_t *glyph)
209 : {
210 265 : *glyph = 0;
211 530 : return klass->get.f.nominal_glyph (this, user_data,
212 : unicode, glyph,
213 530 : klass->user_data.nominal_glyph);
214 : }
215 :
216 0 : inline hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
217 : hb_codepoint_t *glyph)
218 : {
219 0 : *glyph = 0;
220 0 : return klass->get.f.variation_glyph (this, user_data,
221 : unicode, variation_selector, glyph,
222 0 : klass->user_data.variation_glyph);
223 : }
224 :
225 265 : inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
226 : {
227 530 : return klass->get.f.glyph_h_advance (this, user_data,
228 : glyph,
229 530 : klass->user_data.glyph_h_advance);
230 : }
231 :
232 0 : inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
233 : {
234 0 : return klass->get.f.glyph_v_advance (this, user_data,
235 : glyph,
236 0 : klass->user_data.glyph_v_advance);
237 : }
238 :
239 0 : inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
240 : hb_position_t *x, hb_position_t *y)
241 : {
242 0 : *x = *y = 0;
243 0 : return klass->get.f.glyph_h_origin (this, user_data,
244 : glyph, x, y,
245 0 : klass->user_data.glyph_h_origin);
246 : }
247 :
248 0 : inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
249 : hb_position_t *x, hb_position_t *y)
250 : {
251 0 : *x = *y = 0;
252 0 : return klass->get.f.glyph_v_origin (this, user_data,
253 : glyph, x, y,
254 0 : klass->user_data.glyph_v_origin);
255 : }
256 :
257 0 : inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
258 : {
259 0 : return klass->get.f.glyph_h_kerning (this, user_data,
260 : left_glyph, right_glyph,
261 0 : klass->user_data.glyph_h_kerning);
262 : }
263 :
264 0 : inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
265 : {
266 0 : return klass->get.f.glyph_v_kerning (this, user_data,
267 : top_glyph, bottom_glyph,
268 0 : klass->user_data.glyph_v_kerning);
269 : }
270 :
271 0 : inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
272 : hb_glyph_extents_t *extents)
273 : {
274 0 : memset (extents, 0, sizeof (*extents));
275 0 : return klass->get.f.glyph_extents (this, user_data,
276 : glyph,
277 : extents,
278 0 : klass->user_data.glyph_extents);
279 : }
280 :
281 0 : 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)
283 : {
284 0 : *x = *y = 0;
285 0 : return klass->get.f.glyph_contour_point (this, user_data,
286 : glyph, point_index,
287 : x, y,
288 0 : klass->user_data.glyph_contour_point);
289 : }
290 :
291 0 : inline hb_bool_t get_glyph_name (hb_codepoint_t glyph,
292 : char *name, unsigned int size)
293 : {
294 0 : if (size) *name = '\0';
295 0 : return klass->get.f.glyph_name (this, user_data,
296 : glyph,
297 : name, size,
298 0 : klass->user_data.glyph_name);
299 : }
300 :
301 0 : inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
302 : hb_codepoint_t *glyph)
303 : {
304 0 : *glyph = 0;
305 0 : if (len == -1) len = strlen (name);
306 0 : return klass->get.f.glyph_from_name (this, user_data,
307 : name, len,
308 : glyph,
309 0 : klass->user_data.glyph_from_name);
310 : }
311 :
312 :
313 : /* A bit higher-level, and with fallback */
314 :
315 0 : inline void get_h_extents_with_fallback (hb_font_extents_t *extents)
316 : {
317 0 : if (!get_font_h_extents (extents))
318 : {
319 0 : extents->ascender = y_scale * .8;
320 0 : extents->descender = extents->ascender - y_scale;
321 0 : extents->line_gap = 0;
322 : }
323 0 : }
324 0 : inline void get_v_extents_with_fallback (hb_font_extents_t *extents)
325 : {
326 0 : if (!get_font_v_extents (extents))
327 : {
328 0 : extents->ascender = x_scale / 2;
329 0 : extents->descender = extents->ascender - x_scale;
330 0 : extents->line_gap = 0;
331 : }
332 0 : }
333 :
334 0 : inline void get_extents_for_direction (hb_direction_t direction,
335 : hb_font_extents_t *extents)
336 : {
337 0 : if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
338 0 : get_h_extents_with_fallback (extents);
339 : else
340 0 : get_v_extents_with_fallback (extents);
341 0 : }
342 :
343 0 : 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)
346 : {
347 0 : if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
348 0 : *x = get_glyph_h_advance (glyph);
349 0 : *y = 0;
350 : } else {
351 0 : *x = 0;
352 0 : *y = get_glyph_v_advance (glyph);
353 : }
354 0 : }
355 :
356 0 : inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
357 : hb_position_t *x, hb_position_t *y)
358 : {
359 0 : *x = get_glyph_h_advance (glyph) / 2;
360 :
361 : /* TODO cache this somehow?! */
362 : hb_font_extents_t extents;
363 0 : get_h_extents_with_fallback (&extents);
364 0 : *y = extents.ascender;
365 0 : }
366 :
367 0 : inline void get_glyph_h_origin_with_fallback (hb_codepoint_t glyph,
368 : hb_position_t *x, hb_position_t *y)
369 : {
370 0 : if (!get_glyph_h_origin (glyph, x, y) &&
371 0 : get_glyph_v_origin (glyph, x, y))
372 : {
373 : hb_position_t dx, dy;
374 0 : guess_v_origin_minus_h_origin (glyph, &dx, &dy);
375 0 : *x -= dx; *y -= dy;
376 : }
377 0 : }
378 0 : inline void get_glyph_v_origin_with_fallback (hb_codepoint_t glyph,
379 : hb_position_t *x, hb_position_t *y)
380 : {
381 0 : if (!get_glyph_v_origin (glyph, x, y) &&
382 0 : get_glyph_h_origin (glyph, x, y))
383 : {
384 : hb_position_t dx, dy;
385 0 : guess_v_origin_minus_h_origin (glyph, &dx, &dy);
386 0 : *x += dx; *y += dy;
387 : }
388 0 : }
389 :
390 0 : 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)
393 : {
394 0 : if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
395 0 : get_glyph_h_origin_with_fallback (glyph, x, y);
396 : else
397 0 : get_glyph_v_origin_with_fallback (glyph, x, y);
398 0 : }
399 :
400 0 : inline void add_glyph_h_origin (hb_codepoint_t glyph,
401 : hb_position_t *x, hb_position_t *y)
402 : {
403 : hb_position_t origin_x, origin_y;
404 :
405 0 : get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
406 :
407 0 : *x += origin_x;
408 0 : *y += origin_y;
409 0 : }
410 : inline void add_glyph_v_origin (hb_codepoint_t glyph,
411 : hb_position_t *x, hb_position_t *y)
412 : {
413 : hb_position_t origin_x, origin_y;
414 :
415 : get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
416 :
417 : *x += origin_x;
418 : *y += origin_y;
419 : }
420 0 : 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)
423 : {
424 : hb_position_t origin_x, origin_y;
425 :
426 0 : get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
427 :
428 0 : *x += origin_x;
429 0 : *y += origin_y;
430 0 : }
431 :
432 0 : inline void subtract_glyph_h_origin (hb_codepoint_t glyph,
433 : hb_position_t *x, hb_position_t *y)
434 : {
435 : hb_position_t origin_x, origin_y;
436 :
437 0 : get_glyph_h_origin_with_fallback (glyph, &origin_x, &origin_y);
438 :
439 0 : *x -= origin_x;
440 0 : *y -= origin_y;
441 0 : }
442 0 : inline void subtract_glyph_v_origin (hb_codepoint_t glyph,
443 : hb_position_t *x, hb_position_t *y)
444 : {
445 : hb_position_t origin_x, origin_y;
446 :
447 0 : get_glyph_v_origin_with_fallback (glyph, &origin_x, &origin_y);
448 :
449 0 : *x -= origin_x;
450 0 : *y -= origin_y;
451 0 : }
452 0 : 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)
455 : {
456 : hb_position_t origin_x, origin_y;
457 :
458 0 : get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
459 :
460 0 : *x -= origin_x;
461 0 : *y -= origin_y;
462 0 : }
463 :
464 0 : 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)
467 : {
468 0 : if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
469 0 : *x = get_glyph_h_kerning (first_glyph, second_glyph);
470 0 : *y = 0;
471 : } else {
472 0 : *x = 0;
473 0 : *y = get_glyph_v_kerning (first_glyph, second_glyph);
474 : }
475 0 : }
476 :
477 0 : inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
478 : hb_direction_t direction,
479 : hb_glyph_extents_t *extents)
480 : {
481 0 : hb_bool_t ret = get_glyph_extents (glyph, extents);
482 :
483 0 : if (ret)
484 0 : subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
485 :
486 0 : return ret;
487 : }
488 :
489 0 : 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)
492 : {
493 0 : hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
494 :
495 0 : if (ret)
496 0 : subtract_glyph_origin_for_direction (glyph, direction, x, y);
497 :
498 0 : return ret;
499 : }
500 :
501 : /* Generates gidDDD if glyph has no name. */
502 : inline void
503 0 : glyph_to_string (hb_codepoint_t glyph,
504 : char *s, unsigned int size)
505 : {
506 0 : if (get_glyph_name (glyph, s, size)) return;
507 :
508 0 : if (size && snprintf (s, size, "gid%u", glyph) < 0)
509 0 : *s = '\0';
510 : }
511 :
512 : /* Parses gidDDD and uniUUUU strings automatically. */
513 : inline hb_bool_t
514 0 : glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
515 : hb_codepoint_t *glyph)
516 : {
517 0 : if (get_glyph_from_name (s, len, glyph)) return true;
518 :
519 0 : if (len == -1) len = strlen (s);
520 :
521 : /* Straight glyph index. */
522 0 : if (hb_codepoint_parse (s, len, 10, glyph))
523 0 : return true;
524 :
525 0 : if (len > 3)
526 : {
527 : /* gidDDD syntax for glyph indices. */
528 0 : if (0 == strncmp (s, "gid", 3) &&
529 0 : hb_codepoint_parse (s + 3, len - 3, 10, glyph))
530 0 : return true;
531 :
532 : /* uniUUUU and other Unicode character indices. */
533 : hb_codepoint_t unichar;
534 0 : if (0 == strncmp (s, "uni", 3) &&
535 0 : hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
536 0 : get_nominal_glyph (unichar, glyph))
537 0 : return true;
538 : }
539 :
540 0 : return false;
541 : }
542 :
543 185 : inline hb_position_t em_scale (int16_t v, int scale)
544 : {
545 185 : int upem = face->get_upem ();
546 185 : int64_t scaled = v * (int64_t) scale;
547 185 : scaled += scaled >= 0 ? upem/2 : -upem/2; /* Round. */
548 185 : return (hb_position_t) (scaled / upem);
549 : }
550 0 : inline hb_position_t em_scalef (float v, int scale)
551 : {
552 0 : return (hb_position_t) (v * scale / face->get_upem ());
553 : }
554 : };
555 :
556 12 : HB_MARK_AS_FLAG_T (hb_font_t::dirty_t);
557 :
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
563 :
564 :
565 : #endif /* HB_FONT_PRIVATE_HH */
|