hb-font.cc 15.3 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
static hb_bool_t
hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
46
			       void *font_data HB_UNUSED,
47 48 49 50
			       hb_codepoint_t glyph,
			       unsigned int point_index,
			       hb_position_t *x,
			       hb_position_t *y,
51
			       void *user_data HB_UNUSED)
52
{
53 54 55
  if (font->parent) {
    hb_bool_t ret;
    ret = hb_font_get_contour_point (font->parent,
56
				     glyph, point_index,
57 58 59 60
				     x, y);
    font->parent_scale_position (x, y);
    return ret;
  }
61 62 63 64 65

  *x = *y = 0;

  return false;
}
B
Behdad Esfahbod 已提交
66

67 68
static void
hb_font_get_glyph_advance_nil (hb_font_t *font HB_UNUSED,
69
			       void *font_data HB_UNUSED,
70 71 72
			       hb_codepoint_t glyph,
			       hb_position_t *x_advance,
			       hb_position_t *y_advance,
73
			       void *user_data HB_UNUSED)
74 75 76
{
  if (font->parent) {
    hb_font_get_glyph_advance (font->parent, glyph, x_advance, y_advance);
77
    font->parent_scale_distance (x_advance, y_advance);
78 79 80 81 82
    return;
  }

  *x_advance = *y_advance = 0;
}
B
Behdad Esfahbod 已提交
83 84

static void
85
hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
86
			       void *font_data HB_UNUSED,
87 88
			       hb_codepoint_t glyph,
			       hb_glyph_extents_t *extents,
89
			       void *user_data HB_UNUSED)
90 91 92
{
  if (font->parent) {
    hb_font_get_glyph_extents (font->parent, glyph, extents);
93 94
    font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
    font->parent_scale_distance (&extents->width, &extents->height);
95 96 97 98 99 100
    return;
  }

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

102 103
static hb_codepoint_t
hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
104
		       void *font_data HB_UNUSED,
105 106
		       hb_codepoint_t unicode,
		       hb_codepoint_t variation_selector,
107
		       void *user_data HB_UNUSED)
108 109 110 111 112 113
{
  if (font->parent)
    return hb_font_get_glyph (font->parent, unicode, variation_selector);

  return 0;
}
114

115
static void
116
hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
117
			 void *font_data HB_UNUSED,
118 119 120 121
			 hb_codepoint_t first_glyph,
			 hb_codepoint_t second_glyph,
			 hb_position_t *x_kern,
			 hb_position_t *y_kern,
122
			 void *user_data HB_UNUSED)
123
{
124
  if (font->parent) {
125 126 127
    hb_font_get_kerning (font->parent, first_glyph, second_glyph, x_kern, y_kern);
    font->parent_scale_distance (x_kern, y_kern);
    return;
128
  }
129

130
  *x_kern = *y_kern = 0;
131
}
B
Behdad Esfahbod 已提交
132

B
Behdad Esfahbod 已提交
133 134

static hb_font_funcs_t _hb_font_funcs_nil = {
135 136
  HB_OBJECT_HEADER_STATIC,

137
  TRUE, /* immutable */
138

139
  {
140
    hb_font_get_contour_point_nil,
141 142
    hb_font_get_glyph_advance_nil,
    hb_font_get_glyph_extents_nil,
143
    hb_font_get_glyph_nil,
144
    hb_font_get_kerning_nil
145
  }
146 147
};

B
Behdad Esfahbod 已提交
148

B
Behdad Esfahbod 已提交
149 150
hb_font_funcs_t *
hb_font_funcs_create (void)
151
{
B
Behdad Esfahbod 已提交
152
  hb_font_funcs_t *ffuncs;
153

154
  if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
B
Behdad Esfahbod 已提交
155
    return &_hb_font_funcs_nil;
156

157
  ffuncs->get = _hb_font_funcs_nil.get;
158

B
Behdad Esfahbod 已提交
159
  return ffuncs;
160 161
}

162 163 164 165 166 167
hb_font_funcs_t *
hb_font_funcs_get_empty (void)
{
  return &_hb_font_funcs_nil;
}

B
Behdad Esfahbod 已提交
168 169
hb_font_funcs_t *
hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
170
{
171
  return hb_object_reference (ffuncs);
172 173 174
}

void
B
Behdad Esfahbod 已提交
175
hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
176
{
177
  if (!hb_object_destroy (ffuncs)) return;
178

179 180 181 182 183 184 185 186
#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 已提交
187
  free (ffuncs);
188 189
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
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);
}


207 208 209
void
hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
{
210
  if (hb_object_is_inert (ffuncs))
211 212 213
    return;

  ffuncs->immutable = TRUE;
B
Behdad Esfahbod 已提交
214 215 216 217 218
}

hb_bool_t
hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
{
B
Behdad Esfahbod 已提交
219
  return ffuncs->immutable;
220 221
}

222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#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 已提交
246 247
}

248 249 250 251 252
IMPLEMENT (contour_point);
IMPLEMENT (glyph_advance);
IMPLEMENT (glyph_extents);
IMPLEMENT (glyph);
IMPLEMENT (kerning);
B
Behdad Esfahbod 已提交
253

254
#undef IMPLEMENT
B
Behdad Esfahbod 已提交
255

256

257 258
hb_bool_t
hb_font_get_contour_point (hb_font_t *font,
259 260
			   hb_codepoint_t glyph, unsigned int point_index,
			   hb_position_t *x, hb_position_t *y)
B
Behdad Esfahbod 已提交
261
{
262 263
  *x = 0; *y = 0;
  return font->klass->get.contour_point (font, font->user_data,
264 265
					 glyph, point_index,
					 x, y,
266
					 font->klass->user_data.contour_point);
B
Behdad Esfahbod 已提交
267 268
}

269
void
270
hb_font_get_glyph_advance (hb_font_t *font,
271 272 273 274
			   hb_codepoint_t glyph,
			   hb_position_t *x_advance, hb_position_t *y_advance)
{
  *x_advance = *y_advance = 0;
275 276 277
  return font->klass->get.glyph_advance (font, font->user_data,
					 glyph, x_advance, y_advance,
					 font->klass->user_data.glyph_advance);
278 279 280
}

void
281
hb_font_get_glyph_extents (hb_font_t *font,
282 283 284
			   hb_codepoint_t glyph, hb_glyph_extents_t *extents)
{
  memset (extents, 0, sizeof (*extents));
285 286 287
  return font->klass->get.glyph_extents (font, font->user_data,
					 glyph, extents,
					 font->klass->user_data.glyph_extents);
288 289
}

290 291 292
hb_codepoint_t
hb_font_get_glyph (hb_font_t *font,
		   hb_codepoint_t unicode, hb_codepoint_t variation_selector)
B
Behdad Esfahbod 已提交
293
{
294 295 296
  return font->klass->get.glyph (font, font->user_data,
				 unicode, variation_selector,
				 font->klass->user_data.glyph);
B
Behdad Esfahbod 已提交
297 298
}

299
void
300
hb_font_get_kerning (hb_font_t *font,
301 302
		     hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
		     hb_position_t *x_kern, hb_position_t *y_kern)
B
Behdad Esfahbod 已提交
303
{
304
  *x_kern = *y_kern = 0;
305 306
  return font->klass->get.kerning (font, font->user_data,
				   first_glyph, second_glyph,
307
				   x_kern, y_kern,
308
				   font->klass->user_data.kerning);
B
Behdad Esfahbod 已提交
309 310
}

311

312 313 314 315 316
/*
 * hb_face_t
 */

static hb_face_t _hb_face_nil = {
317
  HB_OBJECT_HEADER_STATIC,
318 319 320

  NULL, /* get_table */
  NULL, /* user_data */
321
  NULL, /* destroy */
322

323 324 325
  NULL, /* ot_layout */

  1000
326 327
};

328

329 330
hb_face_t *
hb_face_create_for_tables (hb_get_table_func_t  get_table,
331 332
			   void                *user_data,
			   hb_destroy_func_t    destroy)
333 334 335
{
  hb_face_t *face;

336
  if (!get_table || !(face = hb_object_create<hb_face_t> ())) {
337 338 339 340 341 342 343
    if (destroy)
      destroy (user_data);
    return &_hb_face_nil;
  }

  face->get_table = get_table;
  face->user_data = user_data;
344
  face->destroy = destroy;
345

346
  face->ot_layout = _hb_ot_layout_create (face);
347

348 349
  face->upem = _hb_ot_layout_get_upem (face);

350 351 352
  return face;
}

353 354 355 356 357 358 359 360 361 362 363 364

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));
365
  if (unlikely (!closure))
366
    return NULL;
367

B
Behdad Esfahbod 已提交
368
  closure->blob = blob;
369 370 371 372 373 374 375 376
  closure->index = index;

  return closure;
}

static void
_hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
{
377 378
  hb_blob_destroy (closure->blob);
  free (closure);
379 380 381
}

static hb_blob_t *
B
Behdad Esfahbod 已提交
382
_hb_face_for_data_get_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
383 384 385
{
  hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;

386
  const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
387 388 389 390 391 392 393 394 395
  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;
}

396
hb_face_t *
397 398
hb_face_create (hb_blob_t    *blob,
		unsigned int  index)
399
{
B
Behdad Esfahbod 已提交
400
  if (unlikely (!blob || !hb_blob_get_length (blob)))
401 402
    return &_hb_face_nil;

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

405 406
  if (unlikely (!closure))
    return &_hb_face_nil;
407

408
  return hb_face_create_for_tables (_hb_face_for_data_get_table,
409 410
				    closure,
				    (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
411 412
}

413 414 415 416 417 418
hb_face_t *
hb_face_get_empty (void)
{
  return &_hb_face_nil;
}

419

420 421 422
hb_face_t *
hb_face_reference (hb_face_t *face)
{
423
  return hb_object_reference (face);
424 425 426 427 428
}

void
hb_face_destroy (hb_face_t *face)
{
429
  if (!hb_object_destroy (face)) return;
430

431
  _hb_ot_layout_destroy (face->ot_layout);
B
Behdad Esfahbod 已提交
432

433 434 435 436 437 438
  if (face->destroy)
    face->destroy (face->user_data);

  free (face);
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
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);
}


456
hb_blob_t *
457
hb_face_reference_table (hb_face_t *face,
458
			 hb_tag_t   tag)
459
{
460 461
  hb_blob_t *blob;

462
  if (unlikely (!face || !face->get_table))
B
Behdad Esfahbod 已提交
463
    return hb_blob_get_empty ();
464

B
Behdad Esfahbod 已提交
465
  blob = face->get_table (face, tag, face->user_data);
466
  if (unlikely (!blob))
B
Behdad Esfahbod 已提交
467
    return hb_blob_get_empty ();
468

469
  return blob;
470
}
471

B
Behdad Esfahbod 已提交
472 473 474
unsigned int
hb_face_get_upem (hb_face_t *face)
{
475
  return _hb_ot_layout_get_upem (face);
B
Behdad Esfahbod 已提交
476 477
}

478 479 480 481 482 483

/*
 * hb_font_t
 */

static hb_font_t _hb_font_nil = {
484
  HB_OBJECT_HEADER_STATIC,
485

486
  TRUE, /* immutable */
487

488
  NULL, /* parent */
489 490
  &_hb_face_nil,

491 492 493 494
  0, /* x_scale */
  0, /* y_scale */

  0, /* x_ppem */
B
Behdad Esfahbod 已提交
495 496
  0, /* y_ppem */

497
  &_hb_font_funcs_nil, /* klass */
498 499
  NULL, /* user_data */
  NULL  /* destroy */
500 501 502
};

hb_font_t *
503
hb_font_create (hb_face_t *face)
504 505 506
{
  hb_font_t *font;

507 508 509 510
  if (unlikely (!face))
    face = &_hb_face_nil;
  if (unlikely (hb_object_is_inert (face)))
    return &_hb_font_nil;
511
  if (!(font = hb_object_create<hb_font_t> ()))
512 513
    return &_hb_font_nil;

514
  font->face = hb_face_reference (face);
B
Behdad Esfahbod 已提交
515 516
  font->klass = &_hb_font_funcs_nil;

517 518 519
  return font;
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
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;

B
Behdad Esfahbod 已提交
539
  font->klass = &_hb_font_funcs_nil;
540 541 542 543

  return font;
}

544 545 546 547 548 549
hb_font_t *
hb_font_get_empty (void)
{
  return &_hb_font_nil;
}

550 551 552
hb_font_t *
hb_font_reference (hb_font_t *font)
{
553
  return hb_object_reference (font);
554 555 556 557 558
}

void
hb_font_destroy (hb_font_t *font)
{
559
  if (!hb_object_destroy (font)) return;
560

561
  hb_font_destroy (font->parent);
562
  hb_face_destroy (font->face);
B
Behdad Esfahbod 已提交
563
  hb_font_funcs_destroy (font->klass);
B
Behdad Esfahbod 已提交
564 565
  if (font->destroy)
    font->destroy (font->user_data);
566 567 568 569

  free (font);
}

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
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);
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
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;
}

601 602 603 604 605
hb_font_t *
hb_font_get_parent (hb_font_t *font)
{
  return font->parent;
}
606

607 608 609 610 611 612 613
hb_face_t *
hb_font_get_face (hb_font_t *font)
{
  return font->face;
}


B
Behdad Esfahbod 已提交
614
void
B
Behdad Esfahbod 已提交
615 616
hb_font_set_funcs (hb_font_t         *font,
		   hb_font_funcs_t   *klass,
617 618
		   void              *user_data,
		   hb_destroy_func_t  destroy)
619
{
620
  if (font->immutable)
B
Behdad Esfahbod 已提交
621
    return;
622

B
Behdad Esfahbod 已提交
623 624 625
  if (font->destroy)
    font->destroy (font->user_data);

B
Behdad Esfahbod 已提交
626 627 628
  if (!klass)
    klass = &_hb_font_funcs_nil;

B
Behdad Esfahbod 已提交
629 630 631
  hb_font_funcs_reference (klass);
  hb_font_funcs_destroy (font->klass);
  font->klass = klass;
B
Behdad Esfahbod 已提交
632
  font->user_data = user_data;
633
  font->destroy = destroy;
634 635
}

B
Behdad Esfahbod 已提交
636

637 638
void
hb_font_set_scale (hb_font_t *font,
639 640
		   int x_scale,
		   int y_scale)
641
{
642
  if (font->immutable)
643 644 645 646 647 648
    return;

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

B
Behdad Esfahbod 已提交
649 650
void
hb_font_get_scale (hb_font_t *font,
651 652
		   int *x_scale,
		   int *y_scale)
B
Behdad Esfahbod 已提交
653 654 655 656 657
{
  if (x_scale) *x_scale = font->x_scale;
  if (y_scale) *y_scale = font->y_scale;
}

658 659 660 661 662
void
hb_font_set_ppem (hb_font_t *font,
		  unsigned int x_ppem,
		  unsigned int y_ppem)
{
663
  if (font->immutable)
664 665 666 667 668 669
    return;

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

B
Behdad Esfahbod 已提交
670 671 672 673 674 675 676 677 678
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 已提交
679 680

HB_END_DECLS