hb-font.cc revision b435ab7e29c388e3b100f729957319931625a3a8
1/* 2 * Copyright (C) 2009 Red Hat, Inc. 3 * 4 * This is part of HarfBuzz, a text shaping library. 5 * 6 * Permission is hereby granted, without written agreement and without 7 * license or royalty fees, to use, copy, modify, and distribute this 8 * software and its documentation for any purpose, provided that the 9 * above copyright notice and the following two paragraphs appear in 10 * all copies of this software. 11 * 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 16 * DAMAGE. 17 * 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 23 * 24 * Red Hat Author(s): Behdad Esfahbod 25 */ 26 27#include "hb-private.h" 28 29#include "hb-font-private.h" 30#include "hb-blob-private.h" 31#include "hb-open-file-private.hh" 32 33#include "hb-ot-layout-private.h" 34 35#include <string.h> 36 37 38/* 39 * hb_font_funcs_t 40 */ 41 42static hb_codepoint_t 43hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED, 44 hb_face_t *face HB_UNUSED, 45 const void *user_data HB_UNUSED, 46 hb_codepoint_t unicode HB_UNUSED, 47 hb_codepoint_t variation_selector HB_UNUSED) 48{ return 0; } 49 50static hb_bool_t 51hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED, 52 hb_face_t *face HB_UNUSED, 53 const void *user_data HB_UNUSED, 54 unsigned int point_index HB_UNUSED, 55 hb_codepoint_t glyph HB_UNUSED, 56 hb_position_t *x HB_UNUSED, 57 hb_position_t *y HB_UNUSED) 58{ return false; } 59 60static void 61hb_font_get_glyph_metrics_nil (hb_font_t *font HB_UNUSED, 62 hb_face_t *face HB_UNUSED, 63 const void *user_data HB_UNUSED, 64 hb_codepoint_t glyph HB_UNUSED, 65 hb_glyph_metrics_t *metrics HB_UNUSED) 66{ } 67 68static hb_position_t 69hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED, 70 hb_face_t *face HB_UNUSED, 71 const void *user_data HB_UNUSED, 72 hb_codepoint_t first_glyph HB_UNUSED, 73 hb_codepoint_t second_glyph HB_UNUSED) 74{ return 0; } 75 76hb_font_funcs_t _hb_font_funcs_nil = { 77 HB_REFERENCE_COUNT_INVALID, /* ref_count */ 78 79 TRUE, /* immutable */ 80 81 hb_font_get_glyph_nil, 82 hb_font_get_contour_point_nil, 83 hb_font_get_glyph_metrics_nil, 84 hb_font_get_kerning_nil 85}; 86 87hb_font_funcs_t * 88hb_font_funcs_create (void) 89{ 90 hb_font_funcs_t *ffuncs; 91 92 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs)) 93 return &_hb_font_funcs_nil; 94 95 return ffuncs; 96} 97 98hb_font_funcs_t * 99hb_font_funcs_reference (hb_font_funcs_t *ffuncs) 100{ 101 HB_OBJECT_DO_REFERENCE (ffuncs); 102} 103 104unsigned int 105hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs) 106{ 107 HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs); 108} 109 110void 111hb_font_funcs_destroy (hb_font_funcs_t *ffuncs) 112{ 113 HB_OBJECT_DO_DESTROY (ffuncs); 114 115 free (ffuncs); 116} 117 118hb_font_funcs_t * 119hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs) 120{ 121 hb_font_funcs_t *ffuncs; 122 123 if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs)) 124 return &_hb_font_funcs_nil; 125 126 *ffuncs = *other_ffuncs; 127 128 /* re-init refcount */ 129 HB_OBJECT_DO_INIT (ffuncs); 130 ffuncs->immutable = FALSE; 131 132 return ffuncs; 133} 134 135void 136hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs) 137{ 138 if (HB_OBJECT_IS_INERT (ffuncs)) 139 return; 140 141 ffuncs->immutable = TRUE; 142} 143 144 145void 146hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs, 147 hb_font_get_glyph_func_t glyph_func) 148{ 149 if (ffuncs->immutable) 150 return; 151 152 ffuncs->get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil; 153} 154 155void 156hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs, 157 hb_font_get_contour_point_func_t contour_point_func) 158{ 159 if (ffuncs->immutable) 160 return; 161 162 ffuncs->get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil; 163} 164 165void 166hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs, 167 hb_font_get_glyph_metrics_func_t glyph_metrics_func) 168{ 169 if (ffuncs->immutable) 170 return; 171 172 ffuncs->get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil; 173} 174 175void 176hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs, 177 hb_font_get_kerning_func_t kerning_func) 178{ 179 if (ffuncs->immutable) 180 return; 181 182 ffuncs->get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil; 183} 184 185 186hb_codepoint_t 187hb_font_get_glyph (hb_font_t *font, hb_face_t *face, 188 hb_codepoint_t unicode, hb_codepoint_t variation_selector) 189{ 190 return font->klass->get_glyph (font, face, font->user_data, 191 unicode, variation_selector); 192} 193 194hb_bool_t 195hb_font_get_contour_point (hb_font_t *font, hb_face_t *face, 196 unsigned int point_index, 197 hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y) 198{ 199 *x = 0; *y = 0; 200 return font->klass->get_contour_point (font, face, font->user_data, 201 point_index, 202 glyph, x, y); 203} 204 205void 206hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face, 207 hb_codepoint_t glyph, hb_glyph_metrics_t *metrics) 208{ 209 memset (metrics, 0, sizeof (*metrics)); 210 return font->klass->get_glyph_metrics (font, face, font->user_data, 211 glyph, metrics); 212} 213 214hb_position_t 215hb_font_get_kerning (hb_font_t *font, hb_face_t *face, 216 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph) 217{ 218 return font->klass->get_kerning (font, face, font->user_data, 219 first_glyph, second_glyph); 220} 221 222 223/* 224 * hb_face_t 225 */ 226 227static hb_face_t _hb_face_nil = { 228 HB_REFERENCE_COUNT_INVALID, /* ref_count */ 229 230 NULL, /* get_table */ 231 NULL, /* destroy */ 232 NULL, /* user_data */ 233 234 {} /* ot_layout */ 235}; 236 237 238hb_face_t * 239hb_face_create_for_tables (hb_get_table_func_t get_table, 240 hb_destroy_func_t destroy, 241 void *user_data) 242{ 243 hb_face_t *face; 244 245 if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) { 246 if (destroy) 247 destroy (user_data); 248 return &_hb_face_nil; 249 } 250 251 face->get_table = get_table; 252 face->destroy = destroy; 253 face->user_data = user_data; 254 255 _hb_ot_layout_init (face); 256 257 return face; 258} 259 260 261typedef struct _hb_face_for_data_closure_t { 262 hb_blob_t *blob; 263 unsigned int index; 264} hb_face_for_data_closure_t; 265 266static hb_face_for_data_closure_t _hb_face_for_data_closure_nil = { 267 &_hb_blob_nil, 268 0 269}; 270 271static hb_face_for_data_closure_t * 272_hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index) 273{ 274 hb_face_for_data_closure_t *closure; 275 276 closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t)); 277 if (unlikely (!closure)) 278 return &_hb_face_for_data_closure_nil; 279 280 closure->blob = hb_blob_reference (blob); 281 closure->index = index; 282 283 return closure; 284} 285 286static void 287_hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure) 288{ 289 if (likely (closure != &_hb_face_for_data_closure_nil)) { 290 hb_blob_destroy (closure->blob); 291 free (closure); 292 } 293} 294 295static hb_blob_t * 296_hb_face_for_data_get_table (hb_tag_t tag, void *user_data) 297{ 298 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data; 299 300 const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob); 301 const OpenTypeFontFace &ot_face = ot_file.get_face (data->index); 302 303 const OpenTypeTable &table = ot_face.get_table_by_tag (tag); 304 305 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length); 306 307 hb_blob_unlock (data->blob); 308 309 return blob; 310} 311 312hb_face_t * 313hb_face_create_for_data (hb_blob_t *blob, 314 unsigned int index) 315{ 316 hb_face_t *face; 317 318 if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) 319 return &_hb_face_nil; 320 321 face->get_table = _hb_face_for_data_get_table; 322 face->destroy = (hb_destroy_func_t) _hb_face_for_data_closure_destroy; 323 hb_blob_reference (blob); 324 face->user_data = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (blob), index); 325 hb_blob_destroy (blob); 326 327 _hb_ot_layout_init (face); 328 329 return face; 330} 331 332 333hb_face_t * 334hb_face_reference (hb_face_t *face) 335{ 336 HB_OBJECT_DO_REFERENCE (face); 337} 338 339unsigned int 340hb_face_get_reference_count (hb_face_t *face) 341{ 342 HB_OBJECT_DO_GET_REFERENCE_COUNT (face); 343} 344 345void 346hb_face_destroy (hb_face_t *face) 347{ 348 HB_OBJECT_DO_DESTROY (face); 349 350 _hb_ot_layout_fini (face); 351 352 if (face->destroy) 353 face->destroy (face->user_data); 354 355 free (face); 356} 357 358hb_blob_t * 359hb_face_get_table (hb_face_t *face, 360 hb_tag_t tag) 361{ 362 hb_blob_t *blob; 363 364 if (unlikely (!face || !face->get_table)) 365 return &_hb_blob_nil; 366 367 blob = face->get_table (tag, face->user_data); 368 369 return blob? blob : &_hb_blob_nil; 370} 371 372 373/* 374 * hb_font_t 375 */ 376 377static hb_font_t _hb_font_nil = { 378 HB_REFERENCE_COUNT_INVALID, /* ref_count */ 379 380 0, /* x_scale */ 381 0, /* y_scale */ 382 383 0, /* x_ppem */ 384 0, /* y_ppem */ 385 386 NULL, /* klass */ 387 NULL, /* destroy */ 388 NULL /* user_data */ 389}; 390 391hb_font_t * 392hb_font_create (void) 393{ 394 hb_font_t *font; 395 396 if (!HB_OBJECT_DO_CREATE (hb_font_t, font)) 397 return &_hb_font_nil; 398 399 font->klass = &_hb_font_funcs_nil; 400 401 return font; 402} 403 404hb_font_t * 405hb_font_reference (hb_font_t *font) 406{ 407 HB_OBJECT_DO_REFERENCE (font); 408} 409 410unsigned int 411hb_font_get_reference_count (hb_font_t *font) 412{ 413 HB_OBJECT_DO_GET_REFERENCE_COUNT (font); 414} 415 416void 417hb_font_destroy (hb_font_t *font) 418{ 419 HB_OBJECT_DO_DESTROY (font); 420 421 hb_font_funcs_destroy (font->klass); 422 if (font->destroy) 423 font->destroy (font->user_data); 424 425 free (font); 426} 427 428void 429hb_font_set_funcs (hb_font_t *font, 430 hb_font_funcs_t *klass, 431 hb_destroy_func_t destroy, 432 void *user_data) 433{ 434 if (HB_OBJECT_IS_INERT (font)) 435 return; 436 437 if (font->destroy) 438 font->destroy (font->user_data); 439 440 if (!klass) 441 klass = &_hb_font_funcs_nil; 442 443 hb_font_funcs_reference (klass); 444 hb_font_funcs_destroy (font->klass); 445 font->klass = klass; 446 font->destroy = destroy; 447 font->user_data = user_data; 448} 449 450void 451hb_font_set_scale (hb_font_t *font, 452 hb_16dot16_t x_scale, 453 hb_16dot16_t y_scale) 454{ 455 if (HB_OBJECT_IS_INERT (font)) 456 return; 457 458 font->x_scale = x_scale; 459 font->y_scale = y_scale; 460} 461 462void 463hb_font_set_ppem (hb_font_t *font, 464 unsigned int x_ppem, 465 unsigned int y_ppem) 466{ 467 if (HB_OBJECT_IS_INERT (font)) 468 return; 469 470 font->x_ppem = x_ppem; 471 font->y_ppem = y_ppem; 472} 473 474