hb-font.cc 14.9 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2009  Red Hat, Inc.
3
 *
B
Behdad Esfahbod 已提交
4
 *  This is part of HarfBuzz, a text shaping library.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
 */

27
#include "hb-private.hh"
28

29 30
#include "hb-ot-layout-private.hh"

31
#include "hb-font-private.hh"
B
Behdad Esfahbod 已提交
32
#include "hb-blob.h"
33
#include "hb-open-file-private.hh"
34

B
Behdad Esfahbod 已提交
35 36
#include <string.h>

B
Behdad Esfahbod 已提交
37 38
HB_BEGIN_DECLS

B
Behdad Esfahbod 已提交
39

40
/*
B
Behdad Esfahbod 已提交
41
 * hb_font_funcs_t
42 43
 */

44 45 46 47 48 49 50 51
static hb_bool_t
hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
			       const void *font_data HB_UNUSED,
			       unsigned int point_index HB_UNUSED,
			       hb_codepoint_t glyph HB_UNUSED,
			       hb_position_t *x HB_UNUSED,
			       hb_position_t *y HB_UNUSED,
			       const void *user_data HB_UNUSED)
52 53 54 55 56 57 58 59 60 61
{
  if (font->parent)
    return hb_font_get_contour_point (font->parent,
				      point_index, glyph,
				      x, y);

  *x = *y = 0;

  return false;
}
B
Behdad Esfahbod 已提交
62

63 64
static void
hb_font_get_glyph_advance_nil (hb_font_t *font HB_UNUSED,
65
			       const void *font_data HB_UNUSED,
66
			       hb_codepoint_t glyph HB_UNUSED,
67
			       hb_position_t *x_advance HB_UNUSED,
68 69
			       hb_position_t *y_advance HB_UNUSED,
			       const void *user_data HB_UNUSED)
70 71 72 73 74 75 76 77
{
  if (font->parent) {
    hb_font_get_glyph_advance (font->parent, glyph, x_advance, y_advance);
    return;
  }

  *x_advance = *y_advance = 0;
}
B
Behdad Esfahbod 已提交
78 79

static void
80
hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
81
			       const void *font_data HB_UNUSED,
82
			       hb_codepoint_t glyph HB_UNUSED,
83 84
			       hb_glyph_extents_t *extents HB_UNUSED,
			       const void *user_data HB_UNUSED)
85 86 87 88 89 90 91 92 93
{
  if (font->parent) {
    hb_font_get_glyph_extents (font->parent, glyph, extents);
    return;
  }

  extents->x_bearing = extents->y_bearing = 0;
  extents->width = extents->height = 0;
}
B
Behdad Esfahbod 已提交
94

95 96 97 98 99 100
static hb_codepoint_t
hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
		       const void *font_data HB_UNUSED,
		       hb_codepoint_t unicode HB_UNUSED,
		       hb_codepoint_t variation_selector HB_UNUSED,
		       const void *user_data HB_UNUSED)
101 102 103 104 105 106
{
  if (font->parent)
    return hb_font_get_glyph (font->parent, unicode, variation_selector);

  return 0;
}
107

B
Behdad Esfahbod 已提交
108
static hb_position_t
109
hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
110
			 const void *font_data HB_UNUSED,
111
			 hb_codepoint_t first_glyph HB_UNUSED,
112 113
			 hb_codepoint_t second_glyph HB_UNUSED,
			 const void *user_data HB_UNUSED)
114 115 116 117 118 119
{
  if (font->parent)
    return hb_font_get_kerning (font->parent, first_glyph, second_glyph);

  return 0;
}
B
Behdad Esfahbod 已提交
120

B
Behdad Esfahbod 已提交
121 122

static hb_font_funcs_t _hb_font_funcs_nil = {
123 124
  HB_OBJECT_HEADER_STATIC,

125
  TRUE, /* immutable */
126

127
  {
128
    hb_font_get_contour_point_nil,
129 130
    hb_font_get_glyph_advance_nil,
    hb_font_get_glyph_extents_nil,
131
    hb_font_get_glyph_nil,
132
    hb_font_get_kerning_nil
133
  }
134 135
};

B
Behdad Esfahbod 已提交
136

B
Behdad Esfahbod 已提交
137 138
hb_font_funcs_t *
hb_font_funcs_create (void)
139
{
B
Behdad Esfahbod 已提交
140
  hb_font_funcs_t *ffuncs;
141

142
  if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
B
Behdad Esfahbod 已提交
143
    return &_hb_font_funcs_nil;
144

145
  ffuncs->get = _hb_font_funcs_nil.get;
146

B
Behdad Esfahbod 已提交
147
  return ffuncs;
148 149
}

B
Behdad Esfahbod 已提交
150 151
hb_font_funcs_t *
hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
152
{
153
  return hb_object_reference (ffuncs);
154 155 156
}

void
B
Behdad Esfahbod 已提交
157
hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
158
{
159
  if (!hb_object_destroy (ffuncs)) return;
160

161 162 163 164 165 166 167 168
#define DESTROY(name) if (ffuncs->destroy.name) ffuncs->destroy.name (ffuncs->user_data.name)
  DESTROY (contour_point);
  DESTROY (glyph_advance);
  DESTROY (glyph_extents);
  DESTROY (glyph);
  DESTROY (kerning);
#undef DESTROY

B
Behdad Esfahbod 已提交
169
  free (ffuncs);
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
hb_bool_t
hb_font_funcs_set_user_data (hb_font_funcs_t    *ffuncs,
			     hb_user_data_key_t *key,
			     void *              data,
			     hb_destroy_func_t   destroy)
{
  return hb_object_set_user_data (ffuncs, key, data, destroy);
}

void *
hb_font_funcs_get_user_data (hb_font_funcs_t    *ffuncs,
			     hb_user_data_key_t *key)
{
  return hb_object_get_user_data (ffuncs, key);
}


189 190 191
void
hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
{
192
  if (hb_object_is_inert (ffuncs))
193 194 195
    return;

  ffuncs->immutable = TRUE;
B
Behdad Esfahbod 已提交
196 197 198 199 200
}

hb_bool_t
hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
{
B
Behdad Esfahbod 已提交
201
  return ffuncs->immutable;
202 203
}

204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
#define IMPLEMENT(name)                                                  \
                                                                         \
void                                                                     \
hb_font_funcs_set_##name##_func (hb_font_funcs_t             *ffuncs,    \
                                 hb_font_get_##name##_func_t  func,      \
                                 void                        *user_data, \
                                 hb_destroy_func_t            destroy)   \
{                                                                        \
  if (ffuncs->immutable)                                                 \
    return;                                                              \
                                                                         \
  if (ffuncs->destroy.name)                                              \
    ffuncs->destroy.name (ffuncs->user_data.name);                       \
                                                                         \
  if (func) {                                                            \
    ffuncs->get.name = func;                                             \
    ffuncs->user_data.name = user_data;                                  \
    ffuncs->destroy.name = destroy;                                      \
  } else {                                                               \
    ffuncs->get.name = hb_font_get_##name##_nil;                         \
    ffuncs->user_data.name = NULL;                                       \
    ffuncs->destroy.name = NULL;                                         \
  }                                                                      \
B
Behdad Esfahbod 已提交
228 229
}

230 231 232 233 234
IMPLEMENT (contour_point);
IMPLEMENT (glyph_advance);
IMPLEMENT (glyph_extents);
IMPLEMENT (glyph);
IMPLEMENT (kerning);
B
Behdad Esfahbod 已提交
235

236
#undef IMPLEMENT
B
Behdad Esfahbod 已提交
237

238

239 240 241 242
hb_bool_t
hb_font_get_contour_point (hb_font_t *font,
			   unsigned int point_index,
			   hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
B
Behdad Esfahbod 已提交
243
{
244 245 246 247 248
  *x = 0; *y = 0;
  return font->klass->get.contour_point (font, font->user_data,
					 point_index,
					 glyph, x, y,
					 font->klass->user_data.contour_point);
B
Behdad Esfahbod 已提交
249 250
}

251
void
252
hb_font_get_glyph_advance (hb_font_t *font,
253 254 255 256
			   hb_codepoint_t glyph,
			   hb_position_t *x_advance, hb_position_t *y_advance)
{
  *x_advance = *y_advance = 0;
257 258 259
  return font->klass->get.glyph_advance (font, font->user_data,
					 glyph, x_advance, y_advance,
					 font->klass->user_data.glyph_advance);
260 261 262
}

void
263
hb_font_get_glyph_extents (hb_font_t *font,
264 265 266
			   hb_codepoint_t glyph, hb_glyph_extents_t *extents)
{
  memset (extents, 0, sizeof (*extents));
267 268 269
  return font->klass->get.glyph_extents (font, font->user_data,
					 glyph, extents,
					 font->klass->user_data.glyph_extents);
270 271
}

272 273 274
hb_codepoint_t
hb_font_get_glyph (hb_font_t *font,
		   hb_codepoint_t unicode, hb_codepoint_t variation_selector)
B
Behdad Esfahbod 已提交
275
{
276 277 278
  return font->klass->get.glyph (font, font->user_data,
				 unicode, variation_selector,
				 font->klass->user_data.glyph);
B
Behdad Esfahbod 已提交
279 280 281
}

hb_position_t
282
hb_font_get_kerning (hb_font_t *font,
B
Behdad Esfahbod 已提交
283 284
		     hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
{
285 286 287
  return font->klass->get.kerning (font, font->user_data,
				   first_glyph, second_glyph,
				   font->klass->user_data.kerning);
B
Behdad Esfahbod 已提交
288 289
}

290

291 292 293 294 295
/*
 * hb_face_t
 */

static hb_face_t _hb_face_nil = {
296
  HB_OBJECT_HEADER_STATIC,
297 298 299

  NULL, /* get_table */
  NULL, /* user_data */
300
  NULL, /* destroy */
301

302 303 304
  NULL, /* ot_layout */

  1000
305 306
};

307

308 309
hb_face_t *
hb_face_create_for_tables (hb_get_table_func_t  get_table,
310 311
			   void                *user_data,
			   hb_destroy_func_t    destroy)
312 313 314
{
  hb_face_t *face;

315
  if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
316 317 318 319 320 321 322
    if (destroy)
      destroy (user_data);
    return &_hb_face_nil;
  }

  face->get_table = get_table;
  face->user_data = user_data;
323
  face->destroy = destroy;
324

325
  face->ot_layout = _hb_ot_layout_create (face);
326

327 328
  face->upem = _hb_ot_layout_get_upem (face);

329 330 331
  return face;
}

332 333 334 335 336 337 338 339 340 341 342 343

typedef struct _hb_face_for_data_closure_t {
  hb_blob_t *blob;
  unsigned int  index;
} hb_face_for_data_closure_t;

static hb_face_for_data_closure_t *
_hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
{
  hb_face_for_data_closure_t *closure;

  closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
344
  if (unlikely (!closure))
345
    return NULL;
346

B
Behdad Esfahbod 已提交
347
  closure->blob = blob;
348 349 350 351 352 353 354 355
  closure->index = index;

  return closure;
}

static void
_hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
{
356 357
  hb_blob_destroy (closure->blob);
  free (closure);
358 359 360 361 362 363 364
}

static hb_blob_t *
_hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
{
  hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;

365
  const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
366 367 368 369 370 371 372 373 374
  const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);

  const OpenTypeTable &table = ot_face.get_table_by_tag (tag);

  hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);

  return blob;
}

375 376 377 378
hb_face_t *
hb_face_create_for_data (hb_blob_t    *blob,
			 unsigned int  index)
{
B
Behdad Esfahbod 已提交
379
  if (unlikely (!blob || !hb_blob_get_length (blob)))
380 381
    return &_hb_face_nil;

382
  hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
B
Behdad Esfahbod 已提交
383

384 385
  if (unlikely (!closure))
    return &_hb_face_nil;
386

387
  return hb_face_create_for_tables (_hb_face_for_data_get_table,
388 389
				    closure,
				    (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
390 391
}

392

393 394 395
hb_face_t *
hb_face_reference (hb_face_t *face)
{
396
  return hb_object_reference (face);
397 398 399 400 401
}

void
hb_face_destroy (hb_face_t *face)
{
402
  if (!hb_object_destroy (face)) return;
403

404
  _hb_ot_layout_destroy (face->ot_layout);
B
Behdad Esfahbod 已提交
405

406 407 408 409 410 411
  if (face->destroy)
    face->destroy (face->user_data);

  free (face);
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
hb_bool_t
hb_face_set_user_data (hb_face_t          *face,
		       hb_user_data_key_t *key,
		       void *              data,
		       hb_destroy_func_t   destroy)
{
  return hb_object_set_user_data (face, key, data, destroy);
}

void *
hb_face_get_user_data (hb_face_t          *face,
		       hb_user_data_key_t *key)
{
  return hb_object_get_user_data (face, key);
}


429
hb_blob_t *
430
hb_face_reference_table (hb_face_t *face,
431
			 hb_tag_t   tag)
432
{
433 434
  hb_blob_t *blob;

435
  if (unlikely (!face || !face->get_table))
B
Behdad Esfahbod 已提交
436
    return hb_blob_get_empty ();
437

438
  blob = face->get_table (tag, face->user_data);
439
  if (unlikely (!blob))
B
Behdad Esfahbod 已提交
440
    return hb_blob_get_empty ();
441

442
  return blob;
443
}
444

B
Behdad Esfahbod 已提交
445 446 447
unsigned int
hb_face_get_upem (hb_face_t *face)
{
448
  return _hb_ot_layout_get_upem (face);
B
Behdad Esfahbod 已提交
449 450
}

451 452 453 454 455 456

/*
 * hb_font_t
 */

static hb_font_t _hb_font_nil = {
457
  HB_OBJECT_HEADER_STATIC,
458

459
  TRUE, /* immutable */
460

461
  NULL, /* parent */
462 463
  &_hb_face_nil,

464 465 466 467
  0, /* x_scale */
  0, /* y_scale */

  0, /* x_ppem */
B
Behdad Esfahbod 已提交
468 469
  0, /* y_ppem */

470
  &_hb_font_funcs_nil, /* klass */
471 472
  NULL, /* user_data */
  NULL  /* destroy */
473 474 475
};

hb_font_t *
476
hb_font_create (hb_face_t *face)
477 478 479
{
  hb_font_t *font;

480 481 482 483
  if (unlikely (!face))
    face = &_hb_face_nil;
  if (unlikely (hb_object_is_inert (face)))
    return &_hb_font_nil;
484
  if (!(font = hb_object_create<hb_font_t> ()))
485 486
    return &_hb_font_nil;

487
  font->face = hb_face_reference (face);
B
Behdad Esfahbod 已提交
488 489
  font->klass = &_hb_font_funcs_nil;

490 491 492
  return font;
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
hb_font_t *
hb_font_create_sub_font (hb_font_t *parent)
{
  if (unlikely (!parent))
    return &_hb_font_nil;

  hb_font_t *font = hb_font_create (parent->face);

  if (unlikely (hb_object_is_inert (font)))
    return font;

  hb_font_make_immutable (parent);
  font->parent = hb_font_reference (parent);

  font->x_scale = parent->x_scale;
  font->y_scale = parent->y_scale;
  font->x_ppem = parent->x_ppem;
  font->y_ppem = parent->y_ppem;

  /* We can safely copy user_data from parent since we hold a reference
   * onto it and it's immutable.  We should not copy the destroy notifiers
   * though. */
  font->klass = hb_font_funcs_reference (parent->klass);
  font->user_data = parent->user_data;

  return font;
}

521 522 523
hb_font_t *
hb_font_reference (hb_font_t *font)
{
524
  return hb_object_reference (font);
525 526 527 528 529
}

void
hb_font_destroy (hb_font_t *font)
{
530
  if (!hb_object_destroy (font)) return;
531

532
  hb_font_destroy (font->parent);
533
  hb_face_destroy (font->face);
B
Behdad Esfahbod 已提交
534
  hb_font_funcs_destroy (font->klass);
B
Behdad Esfahbod 已提交
535 536
  if (font->destroy)
    font->destroy (font->user_data);
537 538 539 540

  free (font);
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
hb_bool_t
hb_font_set_user_data (hb_font_t          *font,
		       hb_user_data_key_t *key,
		       void *              data,
		       hb_destroy_func_t   destroy)
{
  return hb_object_set_user_data (font, key, data, destroy);
}

void *
hb_font_get_user_data (hb_font_t          *font,
		       hb_user_data_key_t *key)
{
  return hb_object_get_user_data (font, key);
}

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
void
hb_font_make_immutable (hb_font_t *font)
{
  if (hb_object_is_inert (font))
    return;

  font->immutable = true;
}

hb_bool_t
hb_font_is_immutable (hb_font_t *font)
{
  return font->immutable;
}

572 573 574 575 576
hb_font_t *
hb_font_get_parent (hb_font_t *font)
{
  return font->parent;
}
577

578 579 580 581 582 583 584
hb_face_t *
hb_font_get_face (hb_font_t *font)
{
  return font->face;
}


B
Behdad Esfahbod 已提交
585
void
B
Behdad Esfahbod 已提交
586 587
hb_font_set_funcs (hb_font_t         *font,
		   hb_font_funcs_t   *klass,
588 589
		   void              *user_data,
		   hb_destroy_func_t  destroy)
590
{
591
  if (font->immutable)
B
Behdad Esfahbod 已提交
592
    return;
593

B
Behdad Esfahbod 已提交
594 595 596
  if (font->destroy)
    font->destroy (font->user_data);

B
Behdad Esfahbod 已提交
597 598 599
  if (!klass)
    klass = &_hb_font_funcs_nil;

B
Behdad Esfahbod 已提交
600 601 602
  hb_font_funcs_reference (klass);
  hb_font_funcs_destroy (font->klass);
  font->klass = klass;
B
Behdad Esfahbod 已提交
603
  font->user_data = user_data;
604
  font->destroy = destroy;
605 606
}

B
Behdad Esfahbod 已提交
607

608 609
void
hb_font_set_scale (hb_font_t *font,
610 611
		   int x_scale,
		   int y_scale)
612
{
613
  if (font->immutable)
614 615 616 617 618 619
    return;

  font->x_scale = x_scale;
  font->y_scale = y_scale;
}

B
Behdad Esfahbod 已提交
620 621
void
hb_font_get_scale (hb_font_t *font,
622 623
		   int *x_scale,
		   int *y_scale)
B
Behdad Esfahbod 已提交
624 625 626 627 628
{
  if (x_scale) *x_scale = font->x_scale;
  if (y_scale) *y_scale = font->y_scale;
}

629 630 631 632 633
void
hb_font_set_ppem (hb_font_t *font,
		  unsigned int x_ppem,
		  unsigned int y_ppem)
{
634
  if (font->immutable)
635 636 637 638 639 640
    return;

  font->x_ppem = x_ppem;
  font->y_ppem = y_ppem;
}

B
Behdad Esfahbod 已提交
641 642 643 644 645 646 647 648 649
void
hb_font_get_ppem (hb_font_t *font,
		  unsigned int *x_ppem,
		  unsigned int *y_ppem)
{
  if (x_ppem) *x_ppem = font->x_ppem;
  if (y_ppem) *y_ppem = font->y_ppem;
}

B
Behdad Esfahbod 已提交
650 651

HB_END_DECLS