gfx/harfbuzz/src/hb-font-private.hh

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright © 2009 Red Hat, Inc.
michael@0 3 * Copyright © 2011 Google, Inc.
michael@0 4 *
michael@0 5 * This is part of HarfBuzz, a text shaping library.
michael@0 6 *
michael@0 7 * Permission is hereby granted, without written agreement and without
michael@0 8 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 9 * software and its documentation for any purpose, provided that the
michael@0 10 * above copyright notice and the following two paragraphs appear in
michael@0 11 * all copies of this software.
michael@0 12 *
michael@0 13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 17 * DAMAGE.
michael@0 18 *
michael@0 19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 24 *
michael@0 25 * Red Hat Author(s): Behdad Esfahbod
michael@0 26 * Google Author(s): Behdad Esfahbod
michael@0 27 */
michael@0 28
michael@0 29 #ifndef HB_FONT_PRIVATE_HH
michael@0 30 #define HB_FONT_PRIVATE_HH
michael@0 31
michael@0 32 #include "hb-private.hh"
michael@0 33
michael@0 34 #include "hb-object-private.hh"
michael@0 35 #include "hb-face-private.hh"
michael@0 36 #include "hb-shaper-private.hh"
michael@0 37
michael@0 38
michael@0 39
michael@0 40 /*
michael@0 41 * hb_font_funcs_t
michael@0 42 */
michael@0 43
michael@0 44 #define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \
michael@0 45 HB_FONT_FUNC_IMPLEMENT (glyph) \
michael@0 46 HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \
michael@0 47 HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \
michael@0 48 HB_FONT_FUNC_IMPLEMENT (glyph_h_origin) \
michael@0 49 HB_FONT_FUNC_IMPLEMENT (glyph_v_origin) \
michael@0 50 HB_FONT_FUNC_IMPLEMENT (glyph_h_kerning) \
michael@0 51 HB_FONT_FUNC_IMPLEMENT (glyph_v_kerning) \
michael@0 52 HB_FONT_FUNC_IMPLEMENT (glyph_extents) \
michael@0 53 HB_FONT_FUNC_IMPLEMENT (glyph_contour_point) \
michael@0 54 HB_FONT_FUNC_IMPLEMENT (glyph_name) \
michael@0 55 HB_FONT_FUNC_IMPLEMENT (glyph_from_name) \
michael@0 56 /* ^--- Add new callbacks here */
michael@0 57
michael@0 58 struct hb_font_funcs_t {
michael@0 59 hb_object_header_t header;
michael@0 60 ASSERT_POD ();
michael@0 61
michael@0 62 hb_bool_t immutable;
michael@0 63
michael@0 64 /* Don't access these directly. Call hb_font_get_*() instead. */
michael@0 65
michael@0 66 struct {
michael@0 67 #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_func_t name;
michael@0 68 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
michael@0 69 #undef HB_FONT_FUNC_IMPLEMENT
michael@0 70 } get;
michael@0 71
michael@0 72 struct {
michael@0 73 #define HB_FONT_FUNC_IMPLEMENT(name) void *name;
michael@0 74 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
michael@0 75 #undef HB_FONT_FUNC_IMPLEMENT
michael@0 76 } user_data;
michael@0 77
michael@0 78 struct {
michael@0 79 #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
michael@0 80 HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
michael@0 81 #undef HB_FONT_FUNC_IMPLEMENT
michael@0 82 } destroy;
michael@0 83 };
michael@0 84
michael@0 85
michael@0 86
michael@0 87 /*
michael@0 88 * hb_font_t
michael@0 89 */
michael@0 90
michael@0 91 struct hb_font_t {
michael@0 92 hb_object_header_t header;
michael@0 93 ASSERT_POD ();
michael@0 94
michael@0 95 hb_bool_t immutable;
michael@0 96
michael@0 97 hb_font_t *parent;
michael@0 98 hb_face_t *face;
michael@0 99
michael@0 100 int x_scale;
michael@0 101 int y_scale;
michael@0 102
michael@0 103 unsigned int x_ppem;
michael@0 104 unsigned int y_ppem;
michael@0 105
michael@0 106 hb_font_funcs_t *klass;
michael@0 107 void *user_data;
michael@0 108 hb_destroy_func_t destroy;
michael@0 109
michael@0 110 struct hb_shaper_data_t shaper_data;
michael@0 111
michael@0 112
michael@0 113 /* Convert from font-space to user-space */
michael@0 114 inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, this->x_scale); }
michael@0 115 inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, this->y_scale); }
michael@0 116
michael@0 117 /* Convert from parent-font user-space to our user-space */
michael@0 118 inline hb_position_t parent_scale_x_distance (hb_position_t v) {
michael@0 119 if (unlikely (parent && parent->x_scale != x_scale))
michael@0 120 return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale);
michael@0 121 return v;
michael@0 122 }
michael@0 123 inline hb_position_t parent_scale_y_distance (hb_position_t v) {
michael@0 124 if (unlikely (parent && parent->y_scale != y_scale))
michael@0 125 return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale);
michael@0 126 return v;
michael@0 127 }
michael@0 128 inline hb_position_t parent_scale_x_position (hb_position_t v) {
michael@0 129 return parent_scale_x_distance (v);
michael@0 130 }
michael@0 131 inline hb_position_t parent_scale_y_position (hb_position_t v) {
michael@0 132 return parent_scale_y_distance (v);
michael@0 133 }
michael@0 134
michael@0 135 inline void parent_scale_distance (hb_position_t *x, hb_position_t *y) {
michael@0 136 *x = parent_scale_x_distance (*x);
michael@0 137 *y = parent_scale_y_distance (*y);
michael@0 138 }
michael@0 139 inline void parent_scale_position (hb_position_t *x, hb_position_t *y) {
michael@0 140 *x = parent_scale_x_position (*x);
michael@0 141 *y = parent_scale_y_position (*y);
michael@0 142 }
michael@0 143
michael@0 144
michael@0 145 /* Public getters */
michael@0 146
michael@0 147 inline hb_bool_t has_glyph (hb_codepoint_t unicode)
michael@0 148 {
michael@0 149 hb_codepoint_t glyph;
michael@0 150 return get_glyph (unicode, 0, &glyph);
michael@0 151 }
michael@0 152
michael@0 153 inline hb_bool_t get_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector,
michael@0 154 hb_codepoint_t *glyph)
michael@0 155 {
michael@0 156 *glyph = 0;
michael@0 157 return klass->get.glyph (this, user_data,
michael@0 158 unicode, variation_selector, glyph,
michael@0 159 klass->user_data.glyph);
michael@0 160 }
michael@0 161
michael@0 162 inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph)
michael@0 163 {
michael@0 164 return klass->get.glyph_h_advance (this, user_data,
michael@0 165 glyph,
michael@0 166 klass->user_data.glyph_h_advance);
michael@0 167 }
michael@0 168
michael@0 169 inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph)
michael@0 170 {
michael@0 171 return klass->get.glyph_v_advance (this, user_data,
michael@0 172 glyph,
michael@0 173 klass->user_data.glyph_v_advance);
michael@0 174 }
michael@0 175
michael@0 176 inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph,
michael@0 177 hb_position_t *x, hb_position_t *y)
michael@0 178 {
michael@0 179 *x = *y = 0;
michael@0 180 return klass->get.glyph_h_origin (this, user_data,
michael@0 181 glyph, x, y,
michael@0 182 klass->user_data.glyph_h_origin);
michael@0 183 }
michael@0 184
michael@0 185 inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph,
michael@0 186 hb_position_t *x, hb_position_t *y)
michael@0 187 {
michael@0 188 *x = *y = 0;
michael@0 189 return klass->get.glyph_v_origin (this, user_data,
michael@0 190 glyph, x, y,
michael@0 191 klass->user_data.glyph_v_origin);
michael@0 192 }
michael@0 193
michael@0 194 inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
michael@0 195 {
michael@0 196 return klass->get.glyph_h_kerning (this, user_data,
michael@0 197 left_glyph, right_glyph,
michael@0 198 klass->user_data.glyph_h_kerning);
michael@0 199 }
michael@0 200
michael@0 201 inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
michael@0 202 {
michael@0 203 return klass->get.glyph_v_kerning (this, user_data,
michael@0 204 top_glyph, bottom_glyph,
michael@0 205 klass->user_data.glyph_v_kerning);
michael@0 206 }
michael@0 207
michael@0 208 inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph,
michael@0 209 hb_glyph_extents_t *extents)
michael@0 210 {
michael@0 211 memset (extents, 0, sizeof (*extents));
michael@0 212 return klass->get.glyph_extents (this, user_data,
michael@0 213 glyph,
michael@0 214 extents,
michael@0 215 klass->user_data.glyph_extents);
michael@0 216 }
michael@0 217
michael@0 218 inline hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index,
michael@0 219 hb_position_t *x, hb_position_t *y)
michael@0 220 {
michael@0 221 *x = *y = 0;
michael@0 222 return klass->get.glyph_contour_point (this, user_data,
michael@0 223 glyph, point_index,
michael@0 224 x, y,
michael@0 225 klass->user_data.glyph_contour_point);
michael@0 226 }
michael@0 227
michael@0 228 inline hb_bool_t get_glyph_name (hb_codepoint_t glyph,
michael@0 229 char *name, unsigned int size)
michael@0 230 {
michael@0 231 if (size) *name = '\0';
michael@0 232 return klass->get.glyph_name (this, user_data,
michael@0 233 glyph,
michael@0 234 name, size,
michael@0 235 klass->user_data.glyph_name);
michael@0 236 }
michael@0 237
michael@0 238 inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */
michael@0 239 hb_codepoint_t *glyph)
michael@0 240 {
michael@0 241 *glyph = 0;
michael@0 242 if (len == -1) len = strlen (name);
michael@0 243 return klass->get.glyph_from_name (this, user_data,
michael@0 244 name, len,
michael@0 245 glyph,
michael@0 246 klass->user_data.glyph_from_name);
michael@0 247 }
michael@0 248
michael@0 249
michael@0 250 /* A bit higher-level, and with fallback */
michael@0 251
michael@0 252 inline void get_glyph_advance_for_direction (hb_codepoint_t glyph,
michael@0 253 hb_direction_t direction,
michael@0 254 hb_position_t *x, hb_position_t *y)
michael@0 255 {
michael@0 256 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
michael@0 257 *x = get_glyph_h_advance (glyph);
michael@0 258 *y = 0;
michael@0 259 } else {
michael@0 260 *x = 0;
michael@0 261 *y = get_glyph_v_advance (glyph);
michael@0 262 }
michael@0 263 }
michael@0 264
michael@0 265 /* Internal only */
michael@0 266 inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph,
michael@0 267 hb_position_t *x, hb_position_t *y)
michael@0 268 {
michael@0 269 *x = get_glyph_h_advance (glyph) / 2;
michael@0 270
michael@0 271 /* TODO use font_metics.ascent */
michael@0 272 *y = y_scale;
michael@0 273 }
michael@0 274
michael@0 275 inline void get_glyph_origin_for_direction (hb_codepoint_t glyph,
michael@0 276 hb_direction_t direction,
michael@0 277 hb_position_t *x, hb_position_t *y)
michael@0 278 {
michael@0 279 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
michael@0 280 {
michael@0 281 if (!get_glyph_h_origin (glyph, x, y) &&
michael@0 282 get_glyph_v_origin (glyph, x, y))
michael@0 283 {
michael@0 284 hb_position_t dx, dy;
michael@0 285 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
michael@0 286 *x -= dx; *y -= dy;
michael@0 287 }
michael@0 288 }
michael@0 289 else
michael@0 290 {
michael@0 291 if (!get_glyph_v_origin (glyph, x, y) &&
michael@0 292 get_glyph_h_origin (glyph, x, y))
michael@0 293 {
michael@0 294 hb_position_t dx, dy;
michael@0 295 guess_v_origin_minus_h_origin (glyph, &dx, &dy);
michael@0 296 *x += dx; *y += dy;
michael@0 297 }
michael@0 298 }
michael@0 299 }
michael@0 300
michael@0 301 inline void add_glyph_origin_for_direction (hb_codepoint_t glyph,
michael@0 302 hb_direction_t direction,
michael@0 303 hb_position_t *x, hb_position_t *y)
michael@0 304 {
michael@0 305 hb_position_t origin_x, origin_y;
michael@0 306
michael@0 307 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
michael@0 308
michael@0 309 *x += origin_x;
michael@0 310 *y += origin_y;
michael@0 311 }
michael@0 312
michael@0 313 inline void subtract_glyph_origin_for_direction (hb_codepoint_t glyph,
michael@0 314 hb_direction_t direction,
michael@0 315 hb_position_t *x, hb_position_t *y)
michael@0 316 {
michael@0 317 hb_position_t origin_x, origin_y;
michael@0 318
michael@0 319 get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y);
michael@0 320
michael@0 321 *x -= origin_x;
michael@0 322 *y -= origin_y;
michael@0 323 }
michael@0 324
michael@0 325 inline void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
michael@0 326 hb_direction_t direction,
michael@0 327 hb_position_t *x, hb_position_t *y)
michael@0 328 {
michael@0 329 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
michael@0 330 *x = get_glyph_h_kerning (first_glyph, second_glyph);
michael@0 331 *y = 0;
michael@0 332 } else {
michael@0 333 *x = 0;
michael@0 334 *y = get_glyph_v_kerning (first_glyph, second_glyph);
michael@0 335 }
michael@0 336 }
michael@0 337
michael@0 338 inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph,
michael@0 339 hb_direction_t direction,
michael@0 340 hb_glyph_extents_t *extents)
michael@0 341 {
michael@0 342 hb_bool_t ret = get_glyph_extents (glyph, extents);
michael@0 343
michael@0 344 if (ret)
michael@0 345 subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing);
michael@0 346
michael@0 347 return ret;
michael@0 348 }
michael@0 349
michael@0 350 inline hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index,
michael@0 351 hb_direction_t direction,
michael@0 352 hb_position_t *x, hb_position_t *y)
michael@0 353 {
michael@0 354 hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y);
michael@0 355
michael@0 356 if (ret)
michael@0 357 subtract_glyph_origin_for_direction (glyph, direction, x, y);
michael@0 358
michael@0 359 return ret;
michael@0 360 }
michael@0 361
michael@0 362 /* Generates gidDDD if glyph has no name. */
michael@0 363 inline void
michael@0 364 glyph_to_string (hb_codepoint_t glyph,
michael@0 365 char *s, unsigned int size)
michael@0 366 {
michael@0 367 if (get_glyph_name (glyph, s, size)) return;
michael@0 368
michael@0 369 if (size && snprintf (s, size, "gid%u", glyph) < 0)
michael@0 370 *s = '\0';
michael@0 371 }
michael@0 372
michael@0 373 /* Parses gidDDD and uniUUUU strings automatically. */
michael@0 374 inline hb_bool_t
michael@0 375 glyph_from_string (const char *s, int len, /* -1 means nul-terminated */
michael@0 376 hb_codepoint_t *glyph)
michael@0 377 {
michael@0 378 if (get_glyph_from_name (s, len, glyph)) return true;
michael@0 379
michael@0 380 if (len == -1) len = strlen (s);
michael@0 381
michael@0 382 /* Straight glyph index. */
michael@0 383 if (hb_codepoint_parse (s, len, 10, glyph))
michael@0 384 return true;
michael@0 385
michael@0 386 if (len > 3)
michael@0 387 {
michael@0 388 /* gidDDD syntax for glyph indices. */
michael@0 389 if (0 == strncmp (s, "gid", 3) &&
michael@0 390 hb_codepoint_parse (s + 3, len - 3, 10, glyph))
michael@0 391 return true;
michael@0 392
michael@0 393 /* uniUUUU and other Unicode character indices. */
michael@0 394 hb_codepoint_t unichar;
michael@0 395 if (0 == strncmp (s, "uni", 3) &&
michael@0 396 hb_codepoint_parse (s + 3, len - 3, 16, &unichar) &&
michael@0 397 get_glyph (unichar, 0, glyph))
michael@0 398 return true;
michael@0 399 }
michael@0 400
michael@0 401 return false;
michael@0 402 }
michael@0 403
michael@0 404 private:
michael@0 405 inline hb_position_t em_scale (int16_t v, int scale) { return (hb_position_t) (v * (int64_t) scale / face->get_upem ()); }
michael@0 406 };
michael@0 407
michael@0 408 #define HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
michael@0 409 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_PROTOTYPE(shaper, font);
michael@0 410 #include "hb-shaper-list.hh"
michael@0 411 #undef HB_SHAPER_IMPLEMENT
michael@0 412 #undef HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS
michael@0 413
michael@0 414
michael@0 415 #endif /* HB_FONT_PRIVATE_HH */

mercurial