hb-font.cc 23.8 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2009  Red Hat, Inc.
3
 * Copyright © 2012  Google, Inc.
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
26
 * Google Author(s): Behdad Esfahbod
27 28
 */

29
#include "hb-private.hh"
30

31 32
#include "hb-ot-layout-private.hh"

33
#include "hb-font-private.hh"
34
#include "hb-open-file-private.hh"
35
#include "hb-ot-head-table.hh"
B
Behdad Esfahbod 已提交
36
#include "hb-ot-maxp-table.hh"
37

38 39
#include "hb-cache-private.hh"

B
Behdad Esfahbod 已提交
40 41
#include <string.h>

B
Behdad Esfahbod 已提交
42

43
/*
B
Behdad Esfahbod 已提交
44
 * hb_font_funcs_t
45 46
 */

47
static hb_bool_t
48
hb_font_get_glyph_nil (hb_font_t *font,
49 50 51 52 53 54 55
		       void *font_data HB_UNUSED,
		       hb_codepoint_t unicode,
		       hb_codepoint_t variation_selector,
		       hb_codepoint_t *glyph,
		       void *user_data HB_UNUSED)
{
  if (font->parent)
B
Behdad Esfahbod 已提交
56
    return font->parent->get_glyph (unicode, variation_selector, glyph);
57 58

  *glyph = 0;
59
  return false;
60 61
}

62
static hb_position_t
63
hb_font_get_glyph_h_advance_nil (hb_font_t *font,
64 65 66
				 void *font_data HB_UNUSED,
				 hb_codepoint_t glyph,
				 void *user_data HB_UNUSED)
67
{
68
  if (font->parent)
B
Behdad Esfahbod 已提交
69
    return font->parent_scale_x_distance (font->parent->get_glyph_h_advance (glyph));
70

71
  return font->x_scale;
72
}
B
Behdad Esfahbod 已提交
73

74
static hb_position_t
75
hb_font_get_glyph_v_advance_nil (hb_font_t *font,
76 77 78
				 void *font_data HB_UNUSED,
				 hb_codepoint_t glyph,
				 void *user_data HB_UNUSED)
79
{
80
  if (font->parent)
B
Behdad Esfahbod 已提交
81
    return font->parent_scale_y_distance (font->parent->get_glyph_v_advance (glyph));
82

83
  return font->y_scale;
84 85
}

B
Behdad Esfahbod 已提交
86
static hb_bool_t
87
hb_font_get_glyph_h_origin_nil (hb_font_t *font,
B
Behdad Esfahbod 已提交
88 89
				void *font_data HB_UNUSED,
				hb_codepoint_t glyph,
B
Behdad Esfahbod 已提交
90 91
				hb_position_t *x,
				hb_position_t *y,
B
Behdad Esfahbod 已提交
92 93 94
				void *user_data HB_UNUSED)
{
  if (font->parent) {
B
Behdad Esfahbod 已提交
95
    hb_bool_t ret = font->parent->get_glyph_h_origin (glyph, x, y);
96 97
    if (ret)
      font->parent_scale_position (x, y);
B
Behdad Esfahbod 已提交
98 99 100
    return ret;
  }

B
Behdad Esfahbod 已提交
101
  *x = *y = 0;
102
  return false;
B
Behdad Esfahbod 已提交
103 104
}

105
static hb_bool_t
106
hb_font_get_glyph_v_origin_nil (hb_font_t *font,
107 108
				void *font_data HB_UNUSED,
				hb_codepoint_t glyph,
B
Behdad Esfahbod 已提交
109 110
				hb_position_t *x,
				hb_position_t *y,
111 112 113
				void *user_data HB_UNUSED)
{
  if (font->parent) {
B
Behdad Esfahbod 已提交
114
    hb_bool_t ret = font->parent->get_glyph_v_origin (glyph, x, y);
115 116
    if (ret)
      font->parent_scale_position (x, y);
117 118 119
    return ret;
  }

B
Behdad Esfahbod 已提交
120
  *x = *y = 0;
121
  return false;
122 123
}

124
static hb_position_t
125
hb_font_get_glyph_h_kerning_nil (hb_font_t *font,
B
Behdad Esfahbod 已提交
126 127 128 129
				 void *font_data HB_UNUSED,
				 hb_codepoint_t left_glyph,
				 hb_codepoint_t right_glyph,
				 void *user_data HB_UNUSED)
130
{
131
  if (font->parent)
B
Behdad Esfahbod 已提交
132
    return font->parent_scale_x_distance (font->parent->get_glyph_h_kerning (left_glyph, right_glyph));
133

134
  return 0;
135 136
}

137
static hb_position_t
138
hb_font_get_glyph_v_kerning_nil (hb_font_t *font,
B
Behdad Esfahbod 已提交
139 140 141 142
				 void *font_data HB_UNUSED,
				 hb_codepoint_t top_glyph,
				 hb_codepoint_t bottom_glyph,
				 void *user_data HB_UNUSED)
143
{
144
  if (font->parent)
B
Behdad Esfahbod 已提交
145
    return font->parent_scale_y_distance (font->parent->get_glyph_v_kerning (top_glyph, bottom_glyph));
146

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

150
static hb_bool_t
151
hb_font_get_glyph_extents_nil (hb_font_t *font,
152
			       void *font_data HB_UNUSED,
153 154
			       hb_codepoint_t glyph,
			       hb_glyph_extents_t *extents,
155
			       void *user_data HB_UNUSED)
156 157
{
  if (font->parent) {
B
Behdad Esfahbod 已提交
158
    hb_bool_t ret = font->parent->get_glyph_extents (glyph, extents);
159 160 161 162
    if (ret) {
      font->parent_scale_position (&extents->x_bearing, &extents->y_bearing);
      font->parent_scale_distance (&extents->width, &extents->height);
    }
163
    return ret;
164 165
  }

166
  memset (extents, 0, sizeof (*extents));
167
  return false;
168
}
169

170
static hb_bool_t
171
hb_font_get_glyph_contour_point_nil (hb_font_t *font,
B
Behdad Esfahbod 已提交
172 173 174 175 176 177
				     void *font_data HB_UNUSED,
				     hb_codepoint_t glyph,
				     unsigned int point_index,
				     hb_position_t *x,
				     hb_position_t *y,
				     void *user_data HB_UNUSED)
178
{
179
  if (font->parent) {
B
Behdad Esfahbod 已提交
180
    hb_bool_t ret = font->parent->get_glyph_contour_point (glyph, point_index, x, y);
181 182
    if (ret)
      font->parent_scale_position (x, y);
183
    return ret;
184
  }
185

186
  *x = *y = 0;
187
  return false;
188
}
B
Behdad Esfahbod 已提交
189

190 191 192 193 194 195 196 197
static hb_bool_t
hb_font_get_glyph_name_nil (hb_font_t *font,
			    void *font_data HB_UNUSED,
			    hb_codepoint_t glyph,
			    char *name, unsigned int size,
			    void *user_data HB_UNUSED)
{
  if (font->parent)
B
Behdad Esfahbod 已提交
198
    return font->parent->get_glyph_name (glyph, name, size);
199

200
  if (size) *name = '\0';
201
  return false;
202 203 204 205 206 207 208 209 210 211
}

static hb_bool_t
hb_font_get_glyph_from_name_nil (hb_font_t *font,
				 void *font_data HB_UNUSED,
				 const char *name, int len, /* -1 means nul-terminated */
				 hb_codepoint_t *glyph,
				 void *user_data HB_UNUSED)
{
  if (font->parent)
B
Behdad Esfahbod 已提交
212
    return font->parent->get_glyph_from_name (name, len, glyph);
213 214

  *glyph = 0;
215
  return false;
216 217
}

B
Behdad Esfahbod 已提交
218

219
static const hb_font_funcs_t _hb_font_funcs_nil = {
220 221
  HB_OBJECT_HEADER_STATIC,

222
  true, /* immutable */
223

224
  {
225 226 227
#define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_nil,
    HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_FONT_FUNC_IMPLEMENT
228
  }
229 230
};

B
Behdad Esfahbod 已提交
231

232
/**
233
 * hb_font_funcs_create: (Xconstructor)
234 235 236 237 238
 *
 * 
 *
 * Return value: (transfer full): 
 *
239
 * Since: 0.9.2
240
 **/
B
Behdad Esfahbod 已提交
241 242
hb_font_funcs_t *
hb_font_funcs_create (void)
243
{
B
Behdad Esfahbod 已提交
244
  hb_font_funcs_t *ffuncs;
245

246
  if (!(ffuncs = hb_object_create<hb_font_funcs_t> ()))
247
    return hb_font_funcs_get_empty ();
248

249
  ffuncs->get = _hb_font_funcs_nil.get;
250

B
Behdad Esfahbod 已提交
251
  return ffuncs;
252 253
}

254 255 256 257 258 259 260
/**
 * hb_font_funcs_get_empty:
 *
 * 
 *
 * Return value: (transfer full): 
 *
261
 * Since: 0.9.2
262
 **/
263 264 265
hb_font_funcs_t *
hb_font_funcs_get_empty (void)
{
266
  return const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil);
267 268
}

269 270 271 272 273 274 275 276
/**
 * hb_font_funcs_reference: (skip)
 * @ffuncs: font functions.
 *
 * 
 *
 * Return value: 
 *
277
 * Since: 0.9.2
278
 **/
B
Behdad Esfahbod 已提交
279 280
hb_font_funcs_t *
hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
281
{
282
  return hb_object_reference (ffuncs);
283 284
}

285 286 287 288 289 290
/**
 * hb_font_funcs_destroy: (skip)
 * @ffuncs: font functions.
 *
 * 
 *
291
 * Since: 0.9.2
292
 **/
293
void
B
Behdad Esfahbod 已提交
294
hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
295
{
296
  if (!hb_object_destroy (ffuncs)) return;
297

B
Behdad Esfahbod 已提交
298 299
#define HB_FONT_FUNC_IMPLEMENT(name) if (ffuncs->destroy.name) \
  ffuncs->destroy.name (ffuncs->user_data.name);
300 301
  HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_FONT_FUNC_IMPLEMENT
302

B
Behdad Esfahbod 已提交
303
  free (ffuncs);
304 305
}

306 307 308 309 310 311 312 313 314 315 316 317
/**
 * hb_font_funcs_set_user_data: (skip)
 * @ffuncs: font functions.
 * @key: 
 * @data: 
 * @destroy: 
 * @replace: 
 *
 * 
 *
 * Return value: 
 *
318
 * Since: 0.9.2
319
 **/
320 321 322 323
hb_bool_t
hb_font_funcs_set_user_data (hb_font_funcs_t    *ffuncs,
			     hb_user_data_key_t *key,
			     void *              data,
324 325
			     hb_destroy_func_t   destroy,
			     hb_bool_t           replace)
326
{
327
  return hb_object_set_user_data (ffuncs, key, data, destroy, replace);
328 329
}

330 331 332 333 334 335 336 337 338
/**
 * hb_font_funcs_get_user_data: (skip)
 * @ffuncs: font functions.
 * @key: 
 *
 * 
 *
 * Return value: (transfer none): 
 *
339
 * Since: 0.9.2
340
 **/
341 342 343 344 345 346 347 348
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);
}


349 350 351 352 353 354
/**
 * hb_font_funcs_make_immutable:
 * @ffuncs: font functions.
 *
 * 
 *
355
 * Since: 0.9.2
356
 **/
357 358 359
void
hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
{
360
  if (unlikely (hb_object_is_inert (ffuncs)))
361 362
    return;

363
  ffuncs->immutable = true;
B
Behdad Esfahbod 已提交
364 365
}

366 367 368 369 370 371 372 373
/**
 * hb_font_funcs_is_immutable:
 * @ffuncs: font functions.
 *
 * 
 *
 * Return value: 
 *
374
 * Since: 0.9.2
375
 **/
B
Behdad Esfahbod 已提交
376 377 378
hb_bool_t
hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
{
B
Behdad Esfahbod 已提交
379
  return ffuncs->immutable;
380 381
}

382

383
#define HB_FONT_FUNC_IMPLEMENT(name) \
384 385 386 387 388 389 390
                                                                         \
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)   \
{                                                                        \
B
Behdad Esfahbod 已提交
391 392 393
  if (ffuncs->immutable) {                                               \
    if (destroy)                                                         \
      destroy (user_data);                                               \
394
    return;                                                              \
B
Behdad Esfahbod 已提交
395
  }                                                                      \
396 397 398 399 400 401 402 403 404 405 406 407 408
                                                                         \
  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 已提交
409 410
}

411 412 413 414
HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_FONT_FUNC_IMPLEMENT


B
Behdad Esfahbod 已提交
415 416
/* Public getters */

417 418 419 420 421 422 423 424 425 426 427
/**
 * hb_font_get_glyph:
 * @font: a font.
 * @unicode: 
 * @variation_selector: 
 * @glyph: (out): 
 *
 * 
 *
 * Return value: 
 *
428
 * Since: 0.9.2
429
 **/
430 431 432 433 434
hb_bool_t
hb_font_get_glyph (hb_font_t *font,
		   hb_codepoint_t unicode, hb_codepoint_t variation_selector,
		   hb_codepoint_t *glyph)
{
B
Behdad Esfahbod 已提交
435
  return font->get_glyph (unicode, variation_selector, glyph);
436 437
}

438 439 440 441 442 443 444 445 446
/**
 * hb_font_get_glyph_h_advance:
 * @font: a font.
 * @glyph: 
 *
 * 
 *
 * Return value: 
 *
447
 * Since: 0.9.2
448
 **/
449
hb_position_t
450
hb_font_get_glyph_h_advance (hb_font_t *font,
451
			     hb_codepoint_t glyph)
452
{
B
Behdad Esfahbod 已提交
453
  return font->get_glyph_h_advance (glyph);
454 455
}

456 457 458 459 460 461 462 463 464
/**
 * hb_font_get_glyph_v_advance:
 * @font: a font.
 * @glyph: 
 *
 * 
 *
 * Return value: 
 *
465
 * Since: 0.9.2
466
 **/
467
hb_position_t
468
hb_font_get_glyph_v_advance (hb_font_t *font,
469
			     hb_codepoint_t glyph)
470
{
B
Behdad Esfahbod 已提交
471
  return font->get_glyph_v_advance (glyph);
472 473
}

474 475 476 477 478 479 480 481 482 483 484
/**
 * hb_font_get_glyph_h_origin:
 * @font: a font.
 * @glyph: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
485
 * Since: 0.9.2
486
 **/
B
Behdad Esfahbod 已提交
487 488 489
hb_bool_t
hb_font_get_glyph_h_origin (hb_font_t *font,
			    hb_codepoint_t glyph,
B
Behdad Esfahbod 已提交
490
			    hb_position_t *x, hb_position_t *y)
B
Behdad Esfahbod 已提交
491
{
B
Behdad Esfahbod 已提交
492
  return font->get_glyph_h_origin (glyph, x, y);
B
Behdad Esfahbod 已提交
493 494
}

495 496 497 498 499 500 501 502 503 504 505
/**
 * hb_font_get_glyph_v_origin:
 * @font: a font.
 * @glyph: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
506
 * Since: 0.9.2
507
 **/
508 509 510
hb_bool_t
hb_font_get_glyph_v_origin (hb_font_t *font,
			    hb_codepoint_t glyph,
B
Behdad Esfahbod 已提交
511
			    hb_position_t *x, hb_position_t *y)
512
{
B
Behdad Esfahbod 已提交
513
  return font->get_glyph_v_origin (glyph, x, y);
514 515
}

516 517 518 519 520 521 522 523 524 525
/**
 * hb_font_get_glyph_h_kerning:
 * @font: a font.
 * @left_glyph: 
 * @right_glyph: 
 *
 * 
 *
 * Return value: 
 *
526
 * Since: 0.9.2
527
 **/
528
hb_position_t
B
Behdad Esfahbod 已提交
529
hb_font_get_glyph_h_kerning (hb_font_t *font,
530
			     hb_codepoint_t left_glyph, hb_codepoint_t right_glyph)
531
{
B
Behdad Esfahbod 已提交
532
  return font->get_glyph_h_kerning (left_glyph, right_glyph);
533
}
B
Behdad Esfahbod 已提交
534

535 536 537 538 539 540 541 542 543 544
/**
 * hb_font_get_glyph_v_kerning:
 * @font: a font.
 * @top_glyph: 
 * @bottom_glyph: 
 *
 * 
 *
 * Return value: 
 *
545
 * Since: 0.9.2
546
 **/
547
hb_position_t
B
Behdad Esfahbod 已提交
548
hb_font_get_glyph_v_kerning (hb_font_t *font,
549
			     hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph)
550
{
551
  return font->get_glyph_v_kerning (top_glyph, bottom_glyph);
552
}
B
Behdad Esfahbod 已提交
553

554 555 556 557 558 559 560 561 562 563
/**
 * hb_font_get_glyph_extents:
 * @font: a font.
 * @glyph: 
 * @extents: (out): 
 *
 * 
 *
 * Return value: 
 *
564
 * Since: 0.9.2
565
 **/
566 567 568 569 570
hb_bool_t
hb_font_get_glyph_extents (hb_font_t *font,
			   hb_codepoint_t glyph,
			   hb_glyph_extents_t *extents)
{
B
Behdad Esfahbod 已提交
571
  return font->get_glyph_extents (glyph, extents);
572
}
573

574 575 576 577 578 579 580 581 582 583 584 585
/**
 * hb_font_get_glyph_contour_point:
 * @font: a font.
 * @glyph: 
 * @point_index: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
586
 * Since: 0.9.2
587
 **/
588
hb_bool_t
B
Behdad Esfahbod 已提交
589 590 591
hb_font_get_glyph_contour_point (hb_font_t *font,
				 hb_codepoint_t glyph, unsigned int point_index,
				 hb_position_t *x, hb_position_t *y)
B
Behdad Esfahbod 已提交
592
{
B
Behdad Esfahbod 已提交
593
  return font->get_glyph_contour_point (glyph, point_index, x, y);
B
Behdad Esfahbod 已提交
594 595
}

596 597 598 599 600 601 602 603 604 605 606
/**
 * hb_font_get_glyph_name:
 * @font: a font.
 * @glyph: 
 * @name: (array length=size): 
 * @size: 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
607
 * Since: 0.9.2
608
 **/
609 610 611 612 613
hb_bool_t
hb_font_get_glyph_name (hb_font_t *font,
			hb_codepoint_t glyph,
			char *name, unsigned int size)
{
614
  return font->get_glyph_name (glyph, name, size);
615 616
}

617 618 619 620 621 622 623 624 625 626 627
/**
 * hb_font_get_glyph_from_name:
 * @font: a font.
 * @name: (array length=len): 
 * @len: 
 * @glyph: (out): 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
628
 * Since: 0.9.2
629
 **/
630 631 632 633 634
hb_bool_t
hb_font_get_glyph_from_name (hb_font_t *font,
			     const char *name, int len, /* -1 means nul-terminated */
			     hb_codepoint_t *glyph)
{
B
Behdad Esfahbod 已提交
635
  return font->get_glyph_from_name (name, len, glyph);
636 637
}

638 639 640

/* A bit higher-level, and with fallback */

641 642 643 644 645 646 647 648 649 650
/**
 * hb_font_get_glyph_advance_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
651
 * Since: 0.9.2
652
 **/
653
void
654 655 656
hb_font_get_glyph_advance_for_direction (hb_font_t *font,
					 hb_codepoint_t glyph,
					 hb_direction_t direction,
B
Behdad Esfahbod 已提交
657
					 hb_position_t *x, hb_position_t *y)
658
{
B
Behdad Esfahbod 已提交
659
  return font->get_glyph_advance_for_direction (glyph, direction, x, y);
660 661
}

662 663 664 665 666 667 668 669 670 671
/**
 * hb_font_get_glyph_origin_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
672
 * Since: 0.9.2
673
 **/
674
void
B
Behdad Esfahbod 已提交
675 676 677
hb_font_get_glyph_origin_for_direction (hb_font_t *font,
					hb_codepoint_t glyph,
					hb_direction_t direction,
B
Behdad Esfahbod 已提交
678
					hb_position_t *x, hb_position_t *y)
B
Behdad Esfahbod 已提交
679
{
B
Behdad Esfahbod 已提交
680
  return font->get_glyph_origin_for_direction (glyph, direction, x, y);
B
Behdad Esfahbod 已提交
681 682
}

683 684 685 686 687 688 689 690 691 692
/**
 * hb_font_add_glyph_origin_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
693
 * Since: 0.9.2
694
 **/
B
Behdad Esfahbod 已提交
695 696 697 698 699 700
void
hb_font_add_glyph_origin_for_direction (hb_font_t *font,
					hb_codepoint_t glyph,
					hb_direction_t direction,
					hb_position_t *x, hb_position_t *y)
{
B
Behdad Esfahbod 已提交
701
  return font->add_glyph_origin_for_direction (glyph, direction, x, y);
B
Behdad Esfahbod 已提交
702 703
}

704 705 706 707 708 709 710 711 712 713
/**
 * hb_font_subtract_glyph_origin_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
714
 * Since: 0.9.2
715
 **/
B
Behdad Esfahbod 已提交
716 717 718 719 720 721
void
hb_font_subtract_glyph_origin_for_direction (hb_font_t *font,
					     hb_codepoint_t glyph,
					     hb_direction_t direction,
					     hb_position_t *x, hb_position_t *y)
{
B
Behdad Esfahbod 已提交
722
  return font->subtract_glyph_origin_for_direction (glyph, direction, x, y);
B
Behdad Esfahbod 已提交
723 724
}

725 726 727 728 729 730 731 732 733 734 735
/**
 * hb_font_get_glyph_kerning_for_direction:
 * @font: a font.
 * @first_glyph: 
 * @second_glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
736
 * Since: 0.9.2
737
 **/
B
Behdad Esfahbod 已提交
738 739 740 741
void
hb_font_get_glyph_kerning_for_direction (hb_font_t *font,
					 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
					 hb_direction_t direction,
B
Behdad Esfahbod 已提交
742
					 hb_position_t *x, hb_position_t *y)
743
{
B
Behdad Esfahbod 已提交
744
  return font->get_glyph_kerning_for_direction (first_glyph, second_glyph, direction, x, y);
745 746
}

747 748 749 750 751 752 753 754 755 756 757
/**
 * hb_font_get_glyph_extents_for_origin:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @extents: (out): 
 *
 * 
 *
 * Return value: 
 *
758
 * Since: 0.9.2
759
 **/
760 761 762 763 764
hb_bool_t
hb_font_get_glyph_extents_for_origin (hb_font_t *font,
				      hb_codepoint_t glyph,
				      hb_direction_t direction,
				      hb_glyph_extents_t *extents)
B
Behdad Esfahbod 已提交
765
{
B
Behdad Esfahbod 已提交
766
  return font->get_glyph_extents_for_origin (glyph, direction, extents);
B
Behdad Esfahbod 已提交
767 768
}

769 770 771 772 773 774 775 776 777 778 779 780 781
/**
 * hb_font_get_glyph_contour_point_for_origin:
 * @font: a font.
 * @glyph: 
 * @point_index: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
782
 * Since: 0.9.2
783
 **/
784
hb_bool_t
785 786 787 788
hb_font_get_glyph_contour_point_for_origin (hb_font_t *font,
					    hb_codepoint_t glyph, unsigned int point_index,
					    hb_direction_t direction,
					    hb_position_t *x, hb_position_t *y)
B
Behdad Esfahbod 已提交
789
{
B
Behdad Esfahbod 已提交
790
  return font->get_glyph_contour_point_for_origin (glyph, point_index, direction, x, y);
B
Behdad Esfahbod 已提交
791 792
}

793
/* Generates gidDDD if glyph has no name. */
794 795 796 797 798 799 800 801 802
/**
 * hb_font_glyph_to_string:
 * @font: a font.
 * @glyph: 
 * @s: (array length=size): 
 * @size: 
 *
 * 
 *
S
Sascha Brawer 已提交
803
 * Since: 0.9.2
804
 **/
805 806 807 808 809 810 811 812 813
void
hb_font_glyph_to_string (hb_font_t *font,
			 hb_codepoint_t glyph,
			 char *s, unsigned int size)
{
  font->glyph_to_string (glyph, s, size);
}

/* Parses gidDDD and uniUUUU strings automatically. */
814 815 816
/**
 * hb_font_glyph_from_string:
 * @font: a font.
817
 * @s: (array length=len) (element-type uint8_t): 
818 819 820 821 822 823 824
 * @len: 
 * @glyph: (out): 
 *
 * 
 *
 * Return value: 
 *
S
Sascha Brawer 已提交
825
 * Since: 0.9.2
826
 **/
827 828 829 830 831 832 833 834
hb_bool_t
hb_font_glyph_from_string (hb_font_t *font,
			   const char *s, int len, /* -1 means nul-terminated */
			   hb_codepoint_t *glyph)
{
  return font->glyph_from_string (s, len, glyph);
}

835

836 837 838 839
/*
 * hb_font_t
 */

840
/**
841
 * hb_font_create: (Xconstructor)
842 843 844 845 846 847
 * @face: a face.
 *
 * 
 *
 * Return value: (transfer full): 
 *
848
 * Since: 0.9.2
849
 **/
850
hb_font_t *
851
hb_font_create (hb_face_t *face)
852 853 854
{
  hb_font_t *font;

855
  if (unlikely (!face))
856
    face = hb_face_get_empty ();
857
  if (!(font = hb_object_create<hb_font_t> ()))
858
    return hb_font_get_empty ();
859

860
  hb_face_make_immutable (face);
861
  font->face = hb_face_reference (face);
862
  font->klass = hb_font_funcs_get_empty ();
B
Behdad Esfahbod 已提交
863

B
Behdad Esfahbod 已提交
864 865
  font->x_scale = font->y_scale = hb_face_get_upem (face);

866 867 868
  return font;
}

869 870 871 872 873 874 875 876
/**
 * hb_font_create_sub_font:
 * @parent: parent font.
 *
 * 
 *
 * Return value: (transfer full): 
 *
877
 * Since: 0.9.2
878
 **/
879 880 881 882
hb_font_t *
hb_font_create_sub_font (hb_font_t *parent)
{
  if (unlikely (!parent))
B
Behdad Esfahbod 已提交
883
    parent = hb_font_get_empty ();
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

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

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

  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;

  return font;
}

900 901 902 903 904 905 906
/**
 * hb_font_get_empty:
 *
 * 
 *
 * Return value: (transfer full)
 *
907
 * Since: 0.9.2
908
 **/
909 910 911
hb_font_t *
hb_font_get_empty (void)
{
912 913 914
  static const hb_font_t _hb_font_nil = {
    HB_OBJECT_HEADER_STATIC,

915
    true, /* immutable */
916 917 918 919 920 921 922 923 924 925 926 927

    NULL, /* parent */
    const_cast<hb_face_t *> (&_hb_face_nil),

    0, /* x_scale */
    0, /* y_scale */

    0, /* x_ppem */
    0, /* y_ppem */

    const_cast<hb_font_funcs_t *> (&_hb_font_funcs_nil), /* klass */
    NULL, /* user_data */
928 929 930 931 932 933 934
    NULL, /* destroy */

    {
#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT
    }
935 936 937
  };

  return const_cast<hb_font_t *> (&_hb_font_nil);
938 939
}

940 941 942 943 944 945 946 947
/**
 * hb_font_reference: (skip)
 * @font: a font.
 *
 * 
 *
 * Return value: (transfer full): 
 *
948
 * Since: 0.9.2
949
 **/
950 951 952
hb_font_t *
hb_font_reference (hb_font_t *font)
{
953
  return hb_object_reference (font);
954 955
}

956 957 958 959 960 961
/**
 * hb_font_destroy: (skip)
 * @font: a font.
 *
 * 
 *
962
 * Since: 0.9.2
963
 **/
964 965 966
void
hb_font_destroy (hb_font_t *font)
{
967
  if (!hb_object_destroy (font)) return;
968

969 970 971 972 973 974 975
#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, font);
#include "hb-shaper-list.hh"
#undef HB_SHAPER_IMPLEMENT

  if (font->destroy)
    font->destroy (font->user_data);

976
  hb_font_destroy (font->parent);
977
  hb_face_destroy (font->face);
B
Behdad Esfahbod 已提交
978
  hb_font_funcs_destroy (font->klass);
979 980 981 982

  free (font);
}

983 984 985 986 987 988 989 990 991 992 993 994
/**
 * hb_font_set_user_data: (skip)
 * @font: a font.
 * @key: 
 * @data: 
 * @destroy: 
 * @replace: 
 *
 * 
 *
 * Return value: 
 *
995
 * Since: 0.9.2
996
 **/
997 998 999 1000
hb_bool_t
hb_font_set_user_data (hb_font_t          *font,
		       hb_user_data_key_t *key,
		       void *              data,
1001 1002
		       hb_destroy_func_t   destroy,
		       hb_bool_t           replace)
1003
{
1004
  return hb_object_set_user_data (font, key, data, destroy, replace);
1005 1006
}

1007 1008 1009 1010 1011 1012 1013 1014 1015
/**
 * hb_font_get_user_data: (skip)
 * @font: a font.
 * @key: 
 *
 * 
 *
 * Return value: (transfer none): 
 *
1016
 * Since: 0.9.2
1017
 **/
1018 1019 1020 1021 1022 1023 1024
void *
hb_font_get_user_data (hb_font_t          *font,
		       hb_user_data_key_t *key)
{
  return hb_object_get_user_data (font, key);
}

1025 1026 1027 1028 1029 1030
/**
 * hb_font_make_immutable:
 * @font: a font.
 *
 * 
 *
1031
 * Since: 0.9.2
1032
 **/
1033 1034 1035
void
hb_font_make_immutable (hb_font_t *font)
{
1036
  if (unlikely (hb_object_is_inert (font)))
1037 1038
    return;

1039 1040 1041
  if (font->parent)
    hb_font_make_immutable (font->parent);

1042 1043 1044
  font->immutable = true;
}

1045 1046 1047 1048 1049 1050 1051 1052
/**
 * hb_font_is_immutable:
 * @font: a font.
 *
 * 
 *
 * Return value: 
 *
1053
 * Since: 0.9.2
1054
 **/
1055 1056 1057 1058 1059 1060
hb_bool_t
hb_font_is_immutable (hb_font_t *font)
{
  return font->immutable;
}

B
Behdad Esfahbod 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
/**
 * hb_font_set_parent:
 * @font: a font.
 * @parent: new parent.
 *
 * Sets parent font of @font.
 *
 * Since: 1.0.5
 **/
void
hb_font_set_parent (hb_font_t *font,
		    hb_font_t *parent)
{
  if (font->immutable)
    return;

  if (!parent)
    parent = hb_font_get_empty ();

  hb_font_t *old = font->parent;

  font->parent = hb_font_reference (parent);

  hb_font_destroy (old);
}

1087 1088 1089 1090 1091 1092 1093 1094
/**
 * hb_font_get_parent:
 * @font: a font.
 *
 * 
 *
 * Return value: (transfer none): 
 *
1095
 * Since: 0.9.2
1096
 **/
1097 1098 1099 1100 1101
hb_font_t *
hb_font_get_parent (hb_font_t *font)
{
  return font->parent;
}
1102

1103 1104 1105 1106 1107 1108 1109 1110
/**
 * hb_font_get_face:
 * @font: a font.
 *
 * 
 *
 * Return value: (transfer none): 
 *
S
Sascha Brawer 已提交
1111
 * Since: 0.9.2
1112
 **/
1113 1114 1115 1116 1117 1118 1119
hb_face_t *
hb_font_get_face (hb_font_t *font)
{
  return font->face;
}


1120 1121 1122
/**
 * hb_font_set_funcs:
 * @font: a font.
1123
 * @klass: (closure font_data) (destroy destroy) (scope notified):
1124
 * @font_data: 
1125 1126 1127 1128
 * @destroy: 
 *
 * 
 *
S
Sascha Brawer 已提交
1129
 * Since: 0.9.2
1130
 **/
B
Behdad Esfahbod 已提交
1131
void
B
Behdad Esfahbod 已提交
1132 1133
hb_font_set_funcs (hb_font_t         *font,
		   hb_font_funcs_t   *klass,
1134
		   void              *font_data,
1135
		   hb_destroy_func_t  destroy)
1136
{
B
Behdad Esfahbod 已提交
1137 1138
  if (font->immutable) {
    if (destroy)
1139
      destroy (font_data);
B
Behdad Esfahbod 已提交
1140
    return;
B
Behdad Esfahbod 已提交
1141
  }
1142

B
Behdad Esfahbod 已提交
1143 1144 1145
  if (font->destroy)
    font->destroy (font->user_data);

B
Behdad Esfahbod 已提交
1146
  if (!klass)
1147
    klass = hb_font_funcs_get_empty ();
B
Behdad Esfahbod 已提交
1148

B
Behdad Esfahbod 已提交
1149 1150 1151
  hb_font_funcs_reference (klass);
  hb_font_funcs_destroy (font->klass);
  font->klass = klass;
1152
  font->user_data = font_data;
1153
  font->destroy = destroy;
1154 1155
}

1156 1157 1158
/**
 * hb_font_set_funcs_data:
 * @font: a font.
1159
 * @font_data: (destroy destroy) (scope notified):
1160 1161 1162 1163
 * @destroy: 
 *
 * 
 *
S
Sascha Brawer 已提交
1164
 * Since: 0.9.2
1165
 **/
1166 1167
void
hb_font_set_funcs_data (hb_font_t         *font,
1168
		        void              *font_data,
1169 1170
		        hb_destroy_func_t  destroy)
{
B
Behdad Esfahbod 已提交
1171 1172 1173
  /* Destroy user_data? */
  if (font->immutable) {
    if (destroy)
1174
      destroy (font_data);
1175
    return;
B
Behdad Esfahbod 已提交
1176
  }
1177 1178 1179 1180

  if (font->destroy)
    font->destroy (font->user_data);

1181
  font->user_data = font_data;
1182 1183 1184
  font->destroy = destroy;
}

B
Behdad Esfahbod 已提交
1185

1186 1187 1188 1189 1190 1191 1192 1193
/**
 * hb_font_set_scale:
 * @font: a font.
 * @x_scale: 
 * @y_scale: 
 *
 * 
 *
1194
 * Since: 0.9.2
1195
 **/
1196 1197
void
hb_font_set_scale (hb_font_t *font,
1198 1199
		   int x_scale,
		   int y_scale)
1200
{
1201
  if (font->immutable)
1202 1203 1204 1205 1206 1207
    return;

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

1208 1209 1210 1211 1212 1213 1214 1215
/**
 * hb_font_get_scale:
 * @font: a font.
 * @x_scale: (out): 
 * @y_scale: (out): 
 *
 * 
 *
1216
 * Since: 0.9.2
1217
 **/
B
Behdad Esfahbod 已提交
1218 1219
void
hb_font_get_scale (hb_font_t *font,
1220 1221
		   int *x_scale,
		   int *y_scale)
B
Behdad Esfahbod 已提交
1222 1223 1224 1225 1226
{
  if (x_scale) *x_scale = font->x_scale;
  if (y_scale) *y_scale = font->y_scale;
}

1227 1228 1229 1230 1231 1232 1233 1234
/**
 * hb_font_set_ppem:
 * @font: a font.
 * @x_ppem: 
 * @y_ppem: 
 *
 * 
 *
1235
 * Since: 0.9.2
1236
 **/
1237 1238 1239 1240 1241
void
hb_font_set_ppem (hb_font_t *font,
		  unsigned int x_ppem,
		  unsigned int y_ppem)
{
1242
  if (font->immutable)
1243 1244 1245 1246 1247 1248
    return;

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

1249 1250 1251 1252 1253 1254 1255 1256
/**
 * hb_font_get_ppem:
 * @font: a font.
 * @x_ppem: (out): 
 * @y_ppem: (out): 
 *
 * 
 *
1257
 * Since: 0.9.2
1258
 **/
B
Behdad Esfahbod 已提交
1259 1260 1261 1262 1263 1264 1265 1266
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;
}