OLD | NEW |
1 /* | 1 /* |
2 * Copyright © 2011,2014 Google, Inc. | 2 * Copyright © 2011,2014 Google, Inc. |
3 * | 3 * |
4 * This is part of HarfBuzz, a text shaping library. | 4 * This is part of HarfBuzz, a text shaping library. |
5 * | 5 * |
6 * Permission is hereby granted, without written agreement and without | 6 * Permission is hereby granted, without written agreement and without |
7 * license or royalty fees, to use, copy, modify, and distribute this | 7 * license or royalty fees, to use, copy, modify, and distribute this |
8 * software and its documentation for any purpose, provided that the | 8 * software and its documentation for any purpose, provided that the |
9 * above copyright notice and the following two paragraphs appear in | 9 * above copyright notice and the following two paragraphs appear in |
10 * all copies of this software. | 10 * all copies of this software. |
(...skipping 13 matching lines...) Expand all Loading... |
24 * Google Author(s): Behdad Esfahbod, Roozbeh Pournader | 24 * Google Author(s): Behdad Esfahbod, Roozbeh Pournader |
25 */ | 25 */ |
26 | 26 |
27 #include "hb-private.hh" | 27 #include "hb-private.hh" |
28 | 28 |
29 #include "hb-ot.h" | 29 #include "hb-ot.h" |
30 | 30 |
31 #include "hb-font-private.hh" | 31 #include "hb-font-private.hh" |
32 | 32 |
33 #include "hb-ot-cmap-table.hh" | 33 #include "hb-ot-cmap-table.hh" |
| 34 #include "hb-ot-glyf-table.hh" |
| 35 #include "hb-ot-head-table.hh" |
34 #include "hb-ot-hhea-table.hh" | 36 #include "hb-ot-hhea-table.hh" |
35 #include "hb-ot-hmtx-table.hh" | 37 #include "hb-ot-hmtx-table.hh" |
36 | 38 |
37 | 39 |
38 struct hb_ot_face_metrics_accelerator_t | 40 struct hb_ot_face_metrics_accelerator_t |
39 { | 41 { |
40 unsigned int num_metrics; | 42 unsigned int num_metrics; |
41 unsigned int num_advances; | 43 unsigned int num_advances; |
42 unsigned int default_advance; | 44 unsigned int default_advance; |
43 const OT::_mtx *table; | 45 const OT::_mtx *table; |
(...skipping 25 matching lines...) Expand all Loading... |
69 inline void fini (void) | 71 inline void fini (void) |
70 { | 72 { |
71 hb_blob_destroy (this->blob); | 73 hb_blob_destroy (this->blob); |
72 } | 74 } |
73 | 75 |
74 inline unsigned int get_advance (hb_codepoint_t glyph) const | 76 inline unsigned int get_advance (hb_codepoint_t glyph) const |
75 { | 77 { |
76 if (unlikely (glyph >= this->num_metrics)) | 78 if (unlikely (glyph >= this->num_metrics)) |
77 { | 79 { |
78 /* If this->num_metrics is zero, it means we don't have the metrics table | 80 /* If this->num_metrics is zero, it means we don't have the metrics table |
79 * for this direction: return one EM. Otherwise, it means that the glyph | 81 * for this direction: return default advance. Otherwise, it means that t
he |
80 * index is out of bound: return zero. */ | 82 * glyph index is out of bound: return zero. */ |
81 if (this->num_metrics) | 83 if (this->num_metrics) |
82 return 0; | 84 return 0; |
83 else | 85 else |
84 return this->default_advance; | 86 return this->default_advance; |
85 } | 87 } |
86 | 88 |
87 if (glyph >= this->num_advances) | 89 if (glyph >= this->num_advances) |
88 glyph = this->num_advances - 1; | 90 glyph = this->num_advances - 1; |
89 | 91 |
90 return this->table->longMetric[glyph].advance; | 92 return this->table->longMetric[glyph].advance; |
91 } | 93 } |
92 }; | 94 }; |
93 | 95 |
| 96 struct hb_ot_face_glyf_accelerator_t |
| 97 { |
| 98 bool short_offset; |
| 99 unsigned int num_glyphs; |
| 100 const OT::loca *loca; |
| 101 const OT::glyf *glyf; |
| 102 hb_blob_t *loca_blob; |
| 103 hb_blob_t *glyf_blob; |
| 104 unsigned int glyf_len; |
| 105 |
| 106 inline void init (hb_face_t *face) |
| 107 { |
| 108 hb_blob_t *head_blob = OT::Sanitizer<OT::head>::sanitize (face->reference_ta
ble (HB_OT_TAG_head)); |
| 109 const OT::head *head = OT::Sanitizer<OT::head>::lock_instance (head_blob); |
| 110 if ((unsigned int) head->indexToLocFormat > 1 || head->glyphDataFormat != 0) |
| 111 { |
| 112 /* Unknown format. Leave num_glyphs=0, that takes care of disabling us. *
/ |
| 113 hb_blob_destroy (head_blob); |
| 114 return; |
| 115 } |
| 116 this->short_offset = 0 == head->indexToLocFormat; |
| 117 hb_blob_destroy (head_blob); |
| 118 |
| 119 this->loca_blob = OT::Sanitizer<OT::loca>::sanitize (face->reference_table (
HB_OT_TAG_loca)); |
| 120 this->loca = OT::Sanitizer<OT::loca>::lock_instance (this->loca_blob); |
| 121 this->glyf_blob = OT::Sanitizer<OT::glyf>::sanitize (face->reference_table (
HB_OT_TAG_glyf)); |
| 122 this->glyf = OT::Sanitizer<OT::glyf>::lock_instance (this->glyf_blob); |
| 123 |
| 124 this->num_glyphs = MAX (1u, hb_blob_get_length (this->loca_blob) / (this->sh
ort_offset ? 2 : 4)) - 1; |
| 125 this->glyf_len = hb_blob_get_length (this->glyf_blob); |
| 126 } |
| 127 |
| 128 inline void fini (void) |
| 129 { |
| 130 hb_blob_destroy (this->loca_blob); |
| 131 hb_blob_destroy (this->glyf_blob); |
| 132 } |
| 133 |
| 134 inline bool get_extents (hb_codepoint_t glyph, |
| 135 hb_glyph_extents_t *extents) const |
| 136 { |
| 137 if (unlikely (glyph >= this->num_glyphs)) |
| 138 return false; |
| 139 |
| 140 unsigned int start_offset, end_offset; |
| 141 if (this->short_offset) |
| 142 { |
| 143 start_offset = 2 * this->loca->u.shortsZ[glyph]; |
| 144 end_offset = 2 * this->loca->u.shortsZ[glyph + 1]; |
| 145 } |
| 146 else |
| 147 { |
| 148 start_offset = this->loca->u.longsZ[glyph]; |
| 149 end_offset = this->loca->u.longsZ[glyph + 1]; |
| 150 } |
| 151 |
| 152 if (start_offset > end_offset || end_offset > this->glyf_len) |
| 153 return false; |
| 154 |
| 155 if (end_offset - start_offset < OT::glyfGlyphHeader::static_size) |
| 156 return true; /* Empty glyph; zero extents. */ |
| 157 |
| 158 const OT::glyfGlyphHeader &glyph_header = OT::StructAtOffset<OT::glyfGlyphHe
ader> (this->glyf, start_offset); |
| 159 |
| 160 extents->x_bearing = MIN (glyph_header.xMin, glyph_header.xMax); |
| 161 extents->y_bearing = MAX (glyph_header.yMin, glyph_header.yMax); |
| 162 extents->width = MAX (glyph_header.xMin, glyph_header.xMax) - extents->x
_bearing; |
| 163 extents->height = MIN (glyph_header.yMin, glyph_header.yMax) - extents->y
_bearing; |
| 164 |
| 165 return true; |
| 166 } |
| 167 }; |
| 168 |
94 struct hb_ot_face_cmap_accelerator_t | 169 struct hb_ot_face_cmap_accelerator_t |
95 { | 170 { |
96 const OT::CmapSubtable *table; | 171 const OT::CmapSubtable *table; |
97 const OT::CmapSubtable *uvs_table; | 172 const OT::CmapSubtable *uvs_table; |
98 hb_blob_t *blob; | 173 hb_blob_t *blob; |
99 | 174 |
100 inline void init (hb_face_t *face) | 175 inline void init (hb_face_t *face) |
101 { | 176 { |
102 this->blob = OT::Sanitizer<OT::cmap>::sanitize (face->reference_table (HB_OT
_TAG_cmap)); | 177 this->blob = OT::Sanitizer<OT::cmap>::sanitize (face->reference_table (HB_OT
_TAG_cmap)); |
103 const OT::cmap *cmap = OT::Sanitizer<OT::cmap>::lock_instance (this->blob); | 178 const OT::cmap *cmap = OT::Sanitizer<OT::cmap>::lock_instance (this->blob); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 return this->table->get_glyph (unicode, glyph); | 226 return this->table->get_glyph (unicode, glyph); |
152 } | 227 } |
153 }; | 228 }; |
154 | 229 |
155 | 230 |
156 struct hb_ot_font_t | 231 struct hb_ot_font_t |
157 { | 232 { |
158 hb_ot_face_cmap_accelerator_t cmap; | 233 hb_ot_face_cmap_accelerator_t cmap; |
159 hb_ot_face_metrics_accelerator_t h_metrics; | 234 hb_ot_face_metrics_accelerator_t h_metrics; |
160 hb_ot_face_metrics_accelerator_t v_metrics; | 235 hb_ot_face_metrics_accelerator_t v_metrics; |
| 236 hb_ot_face_glyf_accelerator_t glyf; |
161 }; | 237 }; |
162 | 238 |
163 | 239 |
164 static hb_ot_font_t * | 240 static hb_ot_font_t * |
165 _hb_ot_font_create (hb_font_t *font) | 241 _hb_ot_font_create (hb_face_t *face) |
166 { | 242 { |
167 hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t)); | 243 hb_ot_font_t *ot_font = (hb_ot_font_t *) calloc (1, sizeof (hb_ot_font_t)); |
168 hb_face_t *face = font->face; | |
169 | 244 |
170 if (unlikely (!ot_font)) | 245 if (unlikely (!ot_font)) |
171 return NULL; | 246 return NULL; |
172 | 247 |
173 unsigned int upem = face->get_upem (); | 248 unsigned int upem = face->get_upem (); |
174 | 249 |
175 ot_font->cmap.init (face); | 250 ot_font->cmap.init (face); |
176 ot_font->h_metrics.init (face, HB_OT_TAG_hhea, HB_OT_TAG_hmtx, upem>>1); | 251 ot_font->h_metrics.init (face, HB_OT_TAG_hhea, HB_OT_TAG_hmtx, upem>>1); |
177 ot_font->v_metrics.init (face, HB_OT_TAG_vhea, HB_OT_TAG_vmtx, upem); /* TODO
Can we do this lazily? */ | 252 ot_font->v_metrics.init (face, HB_OT_TAG_vhea, HB_OT_TAG_vmtx, upem); /* TODO
Can we do this lazily? */ |
| 253 ot_font->glyf.init (face); |
178 | 254 |
179 return ot_font; | 255 return ot_font; |
180 } | 256 } |
181 | 257 |
182 static void | 258 static void |
183 _hb_ot_font_destroy (hb_ot_font_t *ot_font) | 259 _hb_ot_font_destroy (hb_ot_font_t *ot_font) |
184 { | 260 { |
185 ot_font->cmap.fini (); | 261 ot_font->cmap.fini (); |
186 ot_font->h_metrics.fini (); | 262 ot_font->h_metrics.fini (); |
187 ot_font->v_metrics.fini (); | 263 ot_font->v_metrics.fini (); |
| 264 ot_font->glyf.fini (); |
188 | 265 |
189 free (ot_font); | 266 free (ot_font); |
190 } | 267 } |
191 | 268 |
192 | 269 |
193 static hb_bool_t | 270 static hb_bool_t |
194 hb_ot_get_glyph (hb_font_t *font HB_UNUSED, | 271 hb_ot_get_glyph (hb_font_t *font HB_UNUSED, |
195 void *font_data, | 272 void *font_data, |
196 hb_codepoint_t unicode, | 273 hb_codepoint_t unicode, |
197 hb_codepoint_t variation_selector, | 274 hb_codepoint_t variation_selector, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 return 0; | 346 return 0; |
270 } | 347 } |
271 | 348 |
272 static hb_bool_t | 349 static hb_bool_t |
273 hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED, | 350 hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED, |
274 void *font_data, | 351 void *font_data, |
275 hb_codepoint_t glyph, | 352 hb_codepoint_t glyph, |
276 hb_glyph_extents_t *extents, | 353 hb_glyph_extents_t *extents, |
277 void *user_data HB_UNUSED) | 354 void *user_data HB_UNUSED) |
278 { | 355 { |
279 /* TODO */ | 356 const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; |
280 return false; | 357 bool ret = ot_font->glyf.get_extents (glyph, extents); |
| 358 extents->x_bearing = font->em_scale_x (extents->x_bearing); |
| 359 extents->y_bearing = font->em_scale_y (extents->y_bearing); |
| 360 extents->width = font->em_scale_x (extents->width); |
| 361 extents->height = font->em_scale_y (extents->height); |
| 362 return ret; |
281 } | 363 } |
282 | 364 |
283 static hb_bool_t | 365 static hb_bool_t |
284 hb_ot_get_glyph_contour_point (hb_font_t *font HB_UNUSED, | 366 hb_ot_get_glyph_contour_point (hb_font_t *font HB_UNUSED, |
285 void *font_data, | 367 void *font_data, |
286 hb_codepoint_t glyph, | 368 hb_codepoint_t glyph, |
287 unsigned int point_index, | 369 unsigned int point_index, |
288 hb_position_t *x, | 370 hb_position_t *x, |
289 hb_position_t *y, | 371 hb_position_t *y, |
290 void *user_data HB_UNUSED) | 372 void *user_data HB_UNUSED) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 return const_cast<hb_font_funcs_t *> (&ot_ffuncs); | 416 return const_cast<hb_font_funcs_t *> (&ot_ffuncs); |
335 } | 417 } |
336 | 418 |
337 | 419 |
338 /** | 420 /** |
339 * Since: 0.9.28 | 421 * Since: 0.9.28 |
340 **/ | 422 **/ |
341 void | 423 void |
342 hb_ot_font_set_funcs (hb_font_t *font) | 424 hb_ot_font_set_funcs (hb_font_t *font) |
343 { | 425 { |
344 hb_ot_font_t *ot_font = _hb_ot_font_create (font); | 426 hb_ot_font_t *ot_font = _hb_ot_font_create (font->face); |
345 if (unlikely (!ot_font)) | 427 if (unlikely (!ot_font)) |
346 return; | 428 return; |
347 | 429 |
348 hb_font_set_funcs (font, | 430 hb_font_set_funcs (font, |
349 _hb_ot_get_font_funcs (), | 431 _hb_ot_get_font_funcs (), |
350 ot_font, | 432 ot_font, |
351 (hb_destroy_func_t) _hb_ot_font_destroy); | 433 (hb_destroy_func_t) _hb_ot_font_destroy); |
352 } | 434 } |
OLD | NEW |