Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2009 Red Hat, Inc. |
michael@0 | 3 | * Copyright © 2012 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 | #include "hb-private.hh" |
michael@0 | 30 | |
michael@0 | 31 | #include "hb-ot-layout-private.hh" |
michael@0 | 32 | |
michael@0 | 33 | #include "hb-font-private.hh" |
michael@0 | 34 | #include "hb-open-file-private.hh" |
michael@0 | 35 | #include "hb-ot-head-table.hh" |
michael@0 | 36 | #include "hb-ot-maxp-table.hh" |
michael@0 | 37 | |
michael@0 | 38 | #include "hb-cache-private.hh" |
michael@0 | 39 | |
michael@0 | 40 | #include <string.h> |
michael@0 | 41 | |
michael@0 | 42 | |
michael@0 | 43 | /* |
michael@0 | 44 | * hb_face_t |
michael@0 | 45 | */ |
michael@0 | 46 | |
michael@0 | 47 | const hb_face_t _hb_face_nil = { |
michael@0 | 48 | HB_OBJECT_HEADER_STATIC, |
michael@0 | 49 | |
michael@0 | 50 | true, /* immutable */ |
michael@0 | 51 | |
michael@0 | 52 | NULL, /* reference_table_func */ |
michael@0 | 53 | NULL, /* user_data */ |
michael@0 | 54 | NULL, /* destroy */ |
michael@0 | 55 | |
michael@0 | 56 | 0, /* index */ |
michael@0 | 57 | 1000, /* upem */ |
michael@0 | 58 | 0, /* num_glyphs */ |
michael@0 | 59 | |
michael@0 | 60 | { |
michael@0 | 61 | #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID, |
michael@0 | 62 | #include "hb-shaper-list.hh" |
michael@0 | 63 | #undef HB_SHAPER_IMPLEMENT |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | NULL, /* shape_plans */ |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | /** |
michael@0 | 71 | * hb_face_create_for_tables: |
michael@0 | 72 | * @reference_table_func: (closure user_data) (destroy destroy) (scope notified): |
michael@0 | 73 | * @user_data: |
michael@0 | 74 | * @destroy: |
michael@0 | 75 | * |
michael@0 | 76 | * |
michael@0 | 77 | * |
michael@0 | 78 | * Return value: (transfer full) |
michael@0 | 79 | * |
michael@0 | 80 | * Since: 1.0 |
michael@0 | 81 | **/ |
michael@0 | 82 | hb_face_t * |
michael@0 | 83 | hb_face_create_for_tables (hb_reference_table_func_t reference_table_func, |
michael@0 | 84 | void *user_data, |
michael@0 | 85 | hb_destroy_func_t destroy) |
michael@0 | 86 | { |
michael@0 | 87 | hb_face_t *face; |
michael@0 | 88 | |
michael@0 | 89 | if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) { |
michael@0 | 90 | if (destroy) |
michael@0 | 91 | destroy (user_data); |
michael@0 | 92 | return hb_face_get_empty (); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | face->reference_table_func = reference_table_func; |
michael@0 | 96 | face->user_data = user_data; |
michael@0 | 97 | face->destroy = destroy; |
michael@0 | 98 | |
michael@0 | 99 | face->upem = 0; |
michael@0 | 100 | face->num_glyphs = (unsigned int) -1; |
michael@0 | 101 | |
michael@0 | 102 | return face; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | |
michael@0 | 106 | typedef struct hb_face_for_data_closure_t { |
michael@0 | 107 | hb_blob_t *blob; |
michael@0 | 108 | unsigned int index; |
michael@0 | 109 | } hb_face_for_data_closure_t; |
michael@0 | 110 | |
michael@0 | 111 | static hb_face_for_data_closure_t * |
michael@0 | 112 | _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index) |
michael@0 | 113 | { |
michael@0 | 114 | hb_face_for_data_closure_t *closure; |
michael@0 | 115 | |
michael@0 | 116 | closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t)); |
michael@0 | 117 | if (unlikely (!closure)) |
michael@0 | 118 | return NULL; |
michael@0 | 119 | |
michael@0 | 120 | closure->blob = blob; |
michael@0 | 121 | closure->index = index; |
michael@0 | 122 | |
michael@0 | 123 | return closure; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | static void |
michael@0 | 127 | _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure) |
michael@0 | 128 | { |
michael@0 | 129 | hb_blob_destroy (closure->blob); |
michael@0 | 130 | free (closure); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | static hb_blob_t * |
michael@0 | 134 | _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) |
michael@0 | 135 | { |
michael@0 | 136 | hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data; |
michael@0 | 137 | |
michael@0 | 138 | if (tag == HB_TAG_NONE) |
michael@0 | 139 | return hb_blob_reference (data->blob); |
michael@0 | 140 | |
michael@0 | 141 | const OT::OpenTypeFontFile &ot_file = *OT::Sanitizer<OT::OpenTypeFontFile>::lock_instance (data->blob); |
michael@0 | 142 | const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index); |
michael@0 | 143 | |
michael@0 | 144 | const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag); |
michael@0 | 145 | |
michael@0 | 146 | hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length); |
michael@0 | 147 | |
michael@0 | 148 | return blob; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * hb_face_create: (Xconstructor) |
michael@0 | 153 | * @blob: |
michael@0 | 154 | * @index: |
michael@0 | 155 | * |
michael@0 | 156 | * |
michael@0 | 157 | * |
michael@0 | 158 | * Return value: (transfer full): |
michael@0 | 159 | * |
michael@0 | 160 | * Since: 1.0 |
michael@0 | 161 | **/ |
michael@0 | 162 | hb_face_t * |
michael@0 | 163 | hb_face_create (hb_blob_t *blob, |
michael@0 | 164 | unsigned int index) |
michael@0 | 165 | { |
michael@0 | 166 | hb_face_t *face; |
michael@0 | 167 | |
michael@0 | 168 | if (unlikely (!blob || !hb_blob_get_length (blob))) |
michael@0 | 169 | return hb_face_get_empty (); |
michael@0 | 170 | |
michael@0 | 171 | hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (OT::Sanitizer<OT::OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index); |
michael@0 | 172 | |
michael@0 | 173 | if (unlikely (!closure)) |
michael@0 | 174 | return hb_face_get_empty (); |
michael@0 | 175 | |
michael@0 | 176 | face = hb_face_create_for_tables (_hb_face_for_data_reference_table, |
michael@0 | 177 | closure, |
michael@0 | 178 | (hb_destroy_func_t) _hb_face_for_data_closure_destroy); |
michael@0 | 179 | |
michael@0 | 180 | hb_face_set_index (face, index); |
michael@0 | 181 | |
michael@0 | 182 | return face; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * hb_face_get_empty: |
michael@0 | 187 | * |
michael@0 | 188 | * |
michael@0 | 189 | * |
michael@0 | 190 | * Return value: (transfer full) |
michael@0 | 191 | * |
michael@0 | 192 | * Since: 1.0 |
michael@0 | 193 | **/ |
michael@0 | 194 | hb_face_t * |
michael@0 | 195 | hb_face_get_empty (void) |
michael@0 | 196 | { |
michael@0 | 197 | return const_cast<hb_face_t *> (&_hb_face_nil); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | |
michael@0 | 201 | /** |
michael@0 | 202 | * hb_face_reference: (skip) |
michael@0 | 203 | * @face: a face. |
michael@0 | 204 | * |
michael@0 | 205 | * |
michael@0 | 206 | * |
michael@0 | 207 | * Return value: |
michael@0 | 208 | * |
michael@0 | 209 | * Since: 1.0 |
michael@0 | 210 | **/ |
michael@0 | 211 | hb_face_t * |
michael@0 | 212 | hb_face_reference (hb_face_t *face) |
michael@0 | 213 | { |
michael@0 | 214 | return hb_object_reference (face); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | /** |
michael@0 | 218 | * hb_face_destroy: (skip) |
michael@0 | 219 | * @face: a face. |
michael@0 | 220 | * |
michael@0 | 221 | * |
michael@0 | 222 | * |
michael@0 | 223 | * Since: 1.0 |
michael@0 | 224 | **/ |
michael@0 | 225 | void |
michael@0 | 226 | hb_face_destroy (hb_face_t *face) |
michael@0 | 227 | { |
michael@0 | 228 | if (!hb_object_destroy (face)) return; |
michael@0 | 229 | |
michael@0 | 230 | for (hb_face_t::plan_node_t *node = face->shape_plans; node; ) |
michael@0 | 231 | { |
michael@0 | 232 | hb_face_t::plan_node_t *next = node->next; |
michael@0 | 233 | hb_shape_plan_destroy (node->shape_plan); |
michael@0 | 234 | free (node); |
michael@0 | 235 | node = next; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, face); |
michael@0 | 239 | #include "hb-shaper-list.hh" |
michael@0 | 240 | #undef HB_SHAPER_IMPLEMENT |
michael@0 | 241 | |
michael@0 | 242 | if (face->destroy) |
michael@0 | 243 | face->destroy (face->user_data); |
michael@0 | 244 | |
michael@0 | 245 | free (face); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | /** |
michael@0 | 249 | * hb_face_set_user_data: (skip) |
michael@0 | 250 | * @face: a face. |
michael@0 | 251 | * @key: |
michael@0 | 252 | * @data: |
michael@0 | 253 | * @destroy: |
michael@0 | 254 | * @replace: |
michael@0 | 255 | * |
michael@0 | 256 | * |
michael@0 | 257 | * |
michael@0 | 258 | * Return value: |
michael@0 | 259 | * |
michael@0 | 260 | * Since: 1.0 |
michael@0 | 261 | **/ |
michael@0 | 262 | hb_bool_t |
michael@0 | 263 | hb_face_set_user_data (hb_face_t *face, |
michael@0 | 264 | hb_user_data_key_t *key, |
michael@0 | 265 | void * data, |
michael@0 | 266 | hb_destroy_func_t destroy, |
michael@0 | 267 | hb_bool_t replace) |
michael@0 | 268 | { |
michael@0 | 269 | return hb_object_set_user_data (face, key, data, destroy, replace); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | /** |
michael@0 | 273 | * hb_face_get_user_data: (skip) |
michael@0 | 274 | * @face: a face. |
michael@0 | 275 | * @key: |
michael@0 | 276 | * |
michael@0 | 277 | * |
michael@0 | 278 | * |
michael@0 | 279 | * Return value: (transfer none): |
michael@0 | 280 | * |
michael@0 | 281 | * Since: 1.0 |
michael@0 | 282 | **/ |
michael@0 | 283 | void * |
michael@0 | 284 | hb_face_get_user_data (hb_face_t *face, |
michael@0 | 285 | hb_user_data_key_t *key) |
michael@0 | 286 | { |
michael@0 | 287 | return hb_object_get_user_data (face, key); |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | /** |
michael@0 | 291 | * hb_face_make_immutable: |
michael@0 | 292 | * @face: a face. |
michael@0 | 293 | * |
michael@0 | 294 | * |
michael@0 | 295 | * |
michael@0 | 296 | * Since: 1.0 |
michael@0 | 297 | **/ |
michael@0 | 298 | void |
michael@0 | 299 | hb_face_make_immutable (hb_face_t *face) |
michael@0 | 300 | { |
michael@0 | 301 | if (hb_object_is_inert (face)) |
michael@0 | 302 | return; |
michael@0 | 303 | |
michael@0 | 304 | face->immutable = true; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | /** |
michael@0 | 308 | * hb_face_is_immutable: |
michael@0 | 309 | * @face: a face. |
michael@0 | 310 | * |
michael@0 | 311 | * |
michael@0 | 312 | * |
michael@0 | 313 | * Return value: |
michael@0 | 314 | * |
michael@0 | 315 | * Since: 1.0 |
michael@0 | 316 | **/ |
michael@0 | 317 | hb_bool_t |
michael@0 | 318 | hb_face_is_immutable (hb_face_t *face) |
michael@0 | 319 | { |
michael@0 | 320 | return face->immutable; |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | |
michael@0 | 324 | /** |
michael@0 | 325 | * hb_face_reference_table: |
michael@0 | 326 | * @face: a face. |
michael@0 | 327 | * @tag: |
michael@0 | 328 | * |
michael@0 | 329 | * |
michael@0 | 330 | * |
michael@0 | 331 | * Return value: (transfer full): |
michael@0 | 332 | * |
michael@0 | 333 | * Since: 1.0 |
michael@0 | 334 | **/ |
michael@0 | 335 | hb_blob_t * |
michael@0 | 336 | hb_face_reference_table (hb_face_t *face, |
michael@0 | 337 | hb_tag_t tag) |
michael@0 | 338 | { |
michael@0 | 339 | return face->reference_table (tag); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | /** |
michael@0 | 343 | * hb_face_reference_blob: |
michael@0 | 344 | * @face: a face. |
michael@0 | 345 | * |
michael@0 | 346 | * |
michael@0 | 347 | * |
michael@0 | 348 | * Return value: (transfer full): |
michael@0 | 349 | * |
michael@0 | 350 | * Since: 1.0 |
michael@0 | 351 | **/ |
michael@0 | 352 | hb_blob_t * |
michael@0 | 353 | hb_face_reference_blob (hb_face_t *face) |
michael@0 | 354 | { |
michael@0 | 355 | return face->reference_table (HB_TAG_NONE); |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | /** |
michael@0 | 359 | * hb_face_set_index: |
michael@0 | 360 | * @face: a face. |
michael@0 | 361 | * @index: |
michael@0 | 362 | * |
michael@0 | 363 | * |
michael@0 | 364 | * |
michael@0 | 365 | * Since: 1.0 |
michael@0 | 366 | **/ |
michael@0 | 367 | void |
michael@0 | 368 | hb_face_set_index (hb_face_t *face, |
michael@0 | 369 | unsigned int index) |
michael@0 | 370 | { |
michael@0 | 371 | if (hb_object_is_inert (face)) |
michael@0 | 372 | return; |
michael@0 | 373 | |
michael@0 | 374 | face->index = index; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | /** |
michael@0 | 378 | * hb_face_get_index: |
michael@0 | 379 | * @face: a face. |
michael@0 | 380 | * |
michael@0 | 381 | * |
michael@0 | 382 | * |
michael@0 | 383 | * Return value: |
michael@0 | 384 | * |
michael@0 | 385 | * Since: 1.0 |
michael@0 | 386 | **/ |
michael@0 | 387 | unsigned int |
michael@0 | 388 | hb_face_get_index (hb_face_t *face) |
michael@0 | 389 | { |
michael@0 | 390 | return face->index; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | /** |
michael@0 | 394 | * hb_face_set_upem: |
michael@0 | 395 | * @face: a face. |
michael@0 | 396 | * @upem: |
michael@0 | 397 | * |
michael@0 | 398 | * |
michael@0 | 399 | * |
michael@0 | 400 | * Since: 1.0 |
michael@0 | 401 | **/ |
michael@0 | 402 | void |
michael@0 | 403 | hb_face_set_upem (hb_face_t *face, |
michael@0 | 404 | unsigned int upem) |
michael@0 | 405 | { |
michael@0 | 406 | if (hb_object_is_inert (face)) |
michael@0 | 407 | return; |
michael@0 | 408 | |
michael@0 | 409 | face->upem = upem; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | /** |
michael@0 | 413 | * hb_face_get_upem: |
michael@0 | 414 | * @face: a face. |
michael@0 | 415 | * |
michael@0 | 416 | * |
michael@0 | 417 | * |
michael@0 | 418 | * Return value: |
michael@0 | 419 | * |
michael@0 | 420 | * Since: 1.0 |
michael@0 | 421 | **/ |
michael@0 | 422 | unsigned int |
michael@0 | 423 | hb_face_get_upem (hb_face_t *face) |
michael@0 | 424 | { |
michael@0 | 425 | return face->get_upem (); |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | void |
michael@0 | 429 | hb_face_t::load_upem (void) const |
michael@0 | 430 | { |
michael@0 | 431 | hb_blob_t *head_blob = OT::Sanitizer<OT::head>::sanitize (reference_table (HB_OT_TAG_head)); |
michael@0 | 432 | const OT::head *head_table = OT::Sanitizer<OT::head>::lock_instance (head_blob); |
michael@0 | 433 | upem = head_table->get_upem (); |
michael@0 | 434 | hb_blob_destroy (head_blob); |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | /** |
michael@0 | 438 | * hb_face_set_glyph_count: |
michael@0 | 439 | * @face: a face. |
michael@0 | 440 | * @glyph_count: |
michael@0 | 441 | * |
michael@0 | 442 | * |
michael@0 | 443 | * |
michael@0 | 444 | * Since: 1.0 |
michael@0 | 445 | **/ |
michael@0 | 446 | void |
michael@0 | 447 | hb_face_set_glyph_count (hb_face_t *face, |
michael@0 | 448 | unsigned int glyph_count) |
michael@0 | 449 | { |
michael@0 | 450 | if (hb_object_is_inert (face)) |
michael@0 | 451 | return; |
michael@0 | 452 | |
michael@0 | 453 | face->num_glyphs = glyph_count; |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | /** |
michael@0 | 457 | * hb_face_get_glyph_count: |
michael@0 | 458 | * @face: a face. |
michael@0 | 459 | * |
michael@0 | 460 | * |
michael@0 | 461 | * |
michael@0 | 462 | * Return value: |
michael@0 | 463 | * |
michael@0 | 464 | * Since: 1.0 |
michael@0 | 465 | **/ |
michael@0 | 466 | unsigned int |
michael@0 | 467 | hb_face_get_glyph_count (hb_face_t *face) |
michael@0 | 468 | { |
michael@0 | 469 | return face->get_num_glyphs (); |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | void |
michael@0 | 473 | hb_face_t::load_num_glyphs (void) const |
michael@0 | 474 | { |
michael@0 | 475 | hb_blob_t *maxp_blob = OT::Sanitizer<OT::maxp>::sanitize (reference_table (HB_OT_TAG_maxp)); |
michael@0 | 476 | const OT::maxp *maxp_table = OT::Sanitizer<OT::maxp>::lock_instance (maxp_blob); |
michael@0 | 477 | num_glyphs = maxp_table->get_num_glyphs (); |
michael@0 | 478 | hb_blob_destroy (maxp_blob); |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 |