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 hb_font_funcs_t *klass; 112 void *user_data; 113 hb_destroy_func_t destroy; 114 115 struct hb_shaper_data_t shaper_data; 116 117 118 /* Convert from font-space to user-space */ em_scale_xhb_font_t119 inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, this->x_scale); } em_scale_yhb_font_t120 inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, this->y_scale); } 121 122 /* Convert from parent-font user-space to our user-space */ parent_scale_x_distancehb_font_t123 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); 126 return v; 127 } parent_scale_y_distancehb_font_t128 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); 131 return v; 132 } parent_scale_x_positionhb_font_t133 inline hb_position_t parent_scale_x_position (hb_position_t v) { 134 return parent_scale_x_distance (v); 135 } parent_scale_y_positionhb_font_t136 inline hb_position_t parent_scale_y_position (hb_position_t v) { 137 return parent_scale_y_distance (v); 138 } 139 parent_scale_distancehb_font_t140 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); 143 } parent_scale_positionhb_font_t144 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); 147 } 148 149 150 /* Public getters */ 151 152 HB_INTERNAL bool has_func (unsigned int i); 153 154 /* has_* ... */ 155 #define HB_FONT_FUNC_IMPLEMENT(name) \ 156 bool \ 157 has_##name##_func (void) \ 158 { \ 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); \ 162 } 163 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS 164 #undef HB_FONT_FUNC_IMPLEMENT 165 get_font_h_extentshb_font_t166 inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents) 167 { 168 memset (extents, 0, sizeof (*extents)); 169 return klass->get.f.font_h_extents (this, user_data, 170 extents, 171 klass->user_data.font_h_extents); 172 } get_font_v_extentshb_font_t173 inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents) 174 { 175 memset (extents, 0, sizeof (*extents)); 176 return klass->get.f.font_v_extents (this, user_data, 177 extents, 178 klass->user_data.font_v_extents); 179 } 180 has_glyphhb_font_t181 inline bool has_glyph (hb_codepoint_t unicode) 182 { 183 hb_codepoint_t glyph; 184 return get_nominal_glyph (unicode, &glyph); 185 } 186 get_nominal_glyphhb_font_t187 inline hb_bool_t get_nominal_glyph (hb_codepoint_t unicode, 188 hb_codepoint_t *glyph) 189 { 190 *glyph = 0; 191 return klass->get.f.nominal_glyph (this, user_data, 192 unicode, glyph, 193 klass->user_data.nominal_glyph); 194 } 195 get_variation_glyphhb_font_t196 inline hb_bool_t get_variation_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector, 197 hb_codepoint_t *glyph) 198 { 199 *glyph = 0; 200 return klass->get.f.variation_glyph (this, user_data, 201 unicode, variation_selector, glyph, 202 klass->user_data.variation_glyph); 203 } 204 get_glyph_h_advancehb_font_t205 inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph) 206 { 207 return klass->get.f.glyph_h_advance (this, user_data, 208 glyph, 209 klass->user_data.glyph_h_advance); 210 } 211 get_glyph_v_advancehb_font_t212 inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph) 213 { 214 return klass->get.f.glyph_v_advance (this, user_data, 215 glyph, 216 klass->user_data.glyph_v_advance); 217 } 218 get_glyph_h_originhb_font_t219 inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph, 220 hb_position_t *x, hb_position_t *y) 221 { 222 *x = *y = 0; 223 return klass->get.f.glyph_h_origin (this, user_data, 224 glyph, x, y, 225 klass->user_data.glyph_h_origin); 226 } 227 get_glyph_v_originhb_font_t228 inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph, 229 hb_position_t *x, hb_position_t *y) 230 { 231 *x = *y = 0; 232 return klass->get.f.glyph_v_origin (this, user_data, 233 glyph, x, y, 234 klass->user_data.glyph_v_origin); 235 } 236 get_glyph_h_kerninghb_font_t237 inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph) 238 { 239 return klass->get.f.glyph_h_kerning (this, user_data, 240 left_glyph, right_glyph, 241 klass->user_data.glyph_h_kerning); 242 } 243 get_glyph_v_kerninghb_font_t244 inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph) 245 { 246 return klass->get.f.glyph_v_kerning (this, user_data, 247 top_glyph, bottom_glyph, 248 klass->user_data.glyph_v_kerning); 249 } 250 get_glyph_extentshb_font_t251 inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph, 252 hb_glyph_extents_t *extents) 253 { 254 memset (extents, 0, sizeof (*extents)); 255 return klass->get.f.glyph_extents (this, user_data, 256 glyph, 257 extents, 258 klass->user_data.glyph_extents); 259 } 260 get_glyph_contour_pointhb_font_t261 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) 263 { 264 *x = *y = 0; 265 return klass->get.f.glyph_contour_point (this, user_data, 266 glyph, point_index, 267 x, y, 268 klass->user_data.glyph_contour_point); 269 } 270 get_glyph_namehb_font_t271 inline hb_bool_t get_glyph_name (hb_codepoint_t glyph, 272 char *name, unsigned int size) 273 { 274 if (size) *name = '\0'; 275 return klass->get.f.glyph_name (this, user_data, 276 glyph, 277 name, size, 278 klass->user_data.glyph_name); 279 } 280 get_glyph_from_namehb_font_t281 inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */ 282 hb_codepoint_t *glyph) 283 { 284 *glyph = 0; 285 if (len == -1) len = strlen (name); 286 return klass->get.f.glyph_from_name (this, user_data, 287 name, len, 288 glyph, 289 klass->user_data.glyph_from_name); 290 } 291 292 293 /* A bit higher-level, and with fallback */ 294 get_extents_for_directionhb_font_t295 inline void get_extents_for_direction (hb_direction_t direction, 296 hb_font_extents_t *extents) 297 { 298 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) { 299 if (!get_font_h_extents (extents)) 300 { 301 extents->ascender = y_scale * .8; 302 extents->descender = y_scale - extents->ascender; 303 extents->line_gap = 0; 304 } 305 } else { 306 if (!get_font_v_extents (extents)) 307 { 308 extents->ascender = x_scale / 2; 309 extents->descender = x_scale - extents->ascender; 310 extents->line_gap = 0; 311 } 312 } 313 } 314 get_glyph_advance_for_directionhb_font_t315 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) 318 { 319 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) { 320 *x = get_glyph_h_advance (glyph); 321 *y = 0; 322 } else { 323 *x = 0; 324 *y = get_glyph_v_advance (glyph); 325 } 326 } 327 328 /* Internal only */ guess_v_origin_minus_h_originhb_font_t329 inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph, 330 hb_position_t *x, hb_position_t *y) 331 { 332 *x = get_glyph_h_advance (glyph) / 2; 333 334 /* TODO use font_extents.ascender */ 335 *y = y_scale; 336 } 337 get_glyph_origin_for_directionhb_font_t338 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) 341 { 342 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) 343 { 344 if (!get_glyph_h_origin (glyph, x, y) && 345 get_glyph_v_origin (glyph, x, y)) 346 { 347 hb_position_t dx, dy; 348 guess_v_origin_minus_h_origin (glyph, &dx, &dy); 349 *x -= dx; *y -= dy; 350 } 351 } 352 else 353 { 354 if (!get_glyph_v_origin (glyph, x, y) && 355 get_glyph_h_origin (glyph, x, y)) 356 { 357 hb_position_t dx, dy; 358 guess_v_origin_minus_h_origin (glyph, &dx, &dy); 359 *x += dx; *y += dy; 360 } 361 } 362 } 363 add_glyph_h_originhb_font_t364 inline void add_glyph_h_origin (hb_codepoint_t glyph, 365 hb_position_t *x, hb_position_t *y) 366 { 367 hb_position_t origin_x, origin_y; 368 369 get_glyph_h_origin (glyph, &origin_x, &origin_y); 370 371 *x += origin_x; 372 *y += origin_y; 373 } add_glyph_v_originhb_font_t374 inline void add_glyph_v_origin (hb_codepoint_t glyph, 375 hb_position_t *x, hb_position_t *y) 376 { 377 hb_position_t origin_x, origin_y; 378 379 get_glyph_v_origin (glyph, &origin_x, &origin_y); 380 381 *x += origin_x; 382 *y += origin_y; 383 } add_glyph_origin_for_directionhb_font_t384 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) 387 { 388 hb_position_t origin_x, origin_y; 389 390 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y); 391 392 *x += origin_x; 393 *y += origin_y; 394 } 395 subtract_glyph_h_originhb_font_t396 inline void subtract_glyph_h_origin (hb_codepoint_t glyph, 397 hb_position_t *x, hb_position_t *y) 398 { 399 hb_position_t origin_x, origin_y; 400 401 get_glyph_h_origin (glyph, &origin_x, &origin_y); 402 403 *x -= origin_x; 404 *y -= origin_y; 405 } subtract_glyph_v_originhb_font_t406 inline void subtract_glyph_v_origin (hb_codepoint_t glyph, 407 hb_position_t *x, hb_position_t *y) 408 { 409 hb_position_t origin_x, origin_y; 410 411 get_glyph_v_origin (glyph, &origin_x, &origin_y); 412 413 *x -= origin_x; 414 *y -= origin_y; 415 } subtract_glyph_origin_for_directionhb_font_t416 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) 419 { 420 hb_position_t origin_x, origin_y; 421 422 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y); 423 424 *x -= origin_x; 425 *y -= origin_y; 426 } 427 get_glyph_kerning_for_directionhb_font_t428 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) 431 { 432 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) { 433 *x = get_glyph_h_kerning (first_glyph, second_glyph); 434 *y = 0; 435 } else { 436 *x = 0; 437 *y = get_glyph_v_kerning (first_glyph, second_glyph); 438 } 439 } 440 get_glyph_extents_for_originhb_font_t441 inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph, 442 hb_direction_t direction, 443 hb_glyph_extents_t *extents) 444 { 445 hb_bool_t ret = get_glyph_extents (glyph, extents); 446 447 if (ret) 448 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing); 449 450 return ret; 451 } 452 get_glyph_contour_point_for_originhb_font_t453 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) 456 { 457 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y); 458 459 if (ret) 460 subtract_glyph_origin_for_direction (glyph, direction, x, y); 461 462 return ret; 463 } 464 465 /* Generates gidDDD if glyph has no name. */ 466 inline void glyph_to_stringhb_font_t467 glyph_to_string (hb_codepoint_t glyph, 468 char *s, unsigned int size) 469 { 470 if (get_glyph_name (glyph, s, size)) return; 471 472 if (size && snprintf (s, size, "gid%u", glyph) < 0) 473 *s = '\0'; 474 } 475 476 /* Parses gidDDD and uniUUUU strings automatically. */ 477 inline hb_bool_t glyph_from_stringhb_font_t478 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */ 479 hb_codepoint_t *glyph) 480 { 481 if (get_glyph_from_name (s, len, glyph)) return true; 482 483 if (len == -1) len = strlen (s); 484 485 /* Straight glyph index. */ 486 if (hb_codepoint_parse (s, len, 10, glyph)) 487 return true; 488 489 if (len > 3) 490 { 491 /* gidDDD syntax for glyph indices. */ 492 if (0 == strncmp (s, "gid", 3) && 493 hb_codepoint_parse (s + 3, len - 3, 10, glyph)) 494 return true; 495 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)) 501 return true; 502 } 503 504 return false; 505 } 506 507 private: em_scalehb_font_t508 inline hb_position_t em_scale (int16_t v, int scale) { return (hb_position_t) (v * (int64_t) scale / face->get_upem ()); } 509 }; 510 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 516 517 518 #endif /* HB_FONT_PRIVATE_HH */ 519