hb-font.cc 23.3 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 239 240
 *
 * 
 *
 * Return value: (transfer full): 
 *
 * Since: 1.0
 **/
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 261 262
/**
 * hb_font_funcs_get_empty:
 *
 * 
 *
 * Return value: (transfer full): 
 *
 * Since: 1.0
 **/
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 277 278
/**
 * hb_font_funcs_reference: (skip)
 * @ffuncs: font functions.
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 291 292
/**
 * hb_font_funcs_destroy: (skip)
 * @ffuncs: font functions.
 *
 * 
 *
 * Since: 1.0
 **/
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 318 319
/**
 * hb_font_funcs_set_user_data: (skip)
 * @ffuncs: font functions.
 * @key: 
 * @data: 
 * @destroy: 
 * @replace: 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 339 340
/**
 * hb_font_funcs_get_user_data: (skip)
 * @ffuncs: font functions.
 * @key: 
 *
 * 
 *
 * Return value: (transfer none): 
 *
 * Since: 1.0
 **/
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 355 356
/**
 * hb_font_funcs_make_immutable:
 * @ffuncs: font functions.
 *
 * 
 *
 * Since: 1.0
 **/
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 374 375
/**
 * hb_font_funcs_is_immutable:
 * @ffuncs: font functions.
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 428 429
/**
 * hb_font_get_glyph:
 * @font: a font.
 * @unicode: 
 * @variation_selector: 
 * @glyph: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 447 448
/**
 * hb_font_get_glyph_h_advance:
 * @font: a font.
 * @glyph: 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 465 466
/**
 * hb_font_get_glyph_v_advance:
 * @font: a font.
 * @glyph: 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 485 486
/**
 * hb_font_get_glyph_h_origin:
 * @font: a font.
 * @glyph: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 506 507
/**
 * hb_font_get_glyph_v_origin:
 * @font: a font.
 * @glyph: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 526 527
/**
 * hb_font_get_glyph_h_kerning:
 * @font: a font.
 * @left_glyph: 
 * @right_glyph: 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 545 546
/**
 * hb_font_get_glyph_v_kerning:
 * @font: a font.
 * @top_glyph: 
 * @bottom_glyph: 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 564 565
/**
 * hb_font_get_glyph_extents:
 * @font: a font.
 * @glyph: 
 * @extents: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 586 587
/**
 * hb_font_get_glyph_contour_point:
 * @font: a font.
 * @glyph: 
 * @point_index: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 651 652
/**
 * hb_font_get_glyph_advance_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
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 672 673
/**
 * hb_font_get_glyph_origin_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
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 693 694
/**
 * hb_font_add_glyph_origin_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
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 714 715
/**
 * hb_font_subtract_glyph_origin_for_direction:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
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 736 737
/**
 * hb_font_get_glyph_kerning_for_direction:
 * @font: a font.
 * @first_glyph: 
 * @second_glyph: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
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 758 759
/**
 * hb_font_get_glyph_extents_for_origin:
 * @font: a font.
 * @glyph: 
 * @direction: 
 * @extents: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 782 783
/**
 * hb_font_get_glyph_contour_point_for_origin:
 * @font: a font.
 * @glyph: 
 * @point_index: 
 * @direction: 
 * @x: (out): 
 * @y: (out): 
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
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 848 849
 * @face: a face.
 *
 * 
 *
 * Return value: (transfer full): 
 *
 * Since: 1.0
 **/
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

864 865 866
  return font;
}

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

  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;

  return font;
}

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

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

    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 */
927 928 929 930 931 932 933
    NULL, /* destroy */

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

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

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

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

968 969 970 971 972 973 974
#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);

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

  free (font);
}

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

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

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

  font->immutable = true;
}

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
/**
 * hb_font_is_immutable:
 * @font: a font.
 *
 * 
 *
 * Return value: 
 *
 * Since: 1.0
 **/
1051 1052 1053 1054 1055 1056
hb_bool_t
hb_font_is_immutable (hb_font_t *font)
{
  return font->immutable;
}

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
/**
 * hb_font_get_parent:
 * @font: a font.
 *
 * 
 *
 * Return value: (transfer none): 
 *
 * Since: 1.0
 **/
1067 1068 1069 1070 1071
hb_font_t *
hb_font_get_parent (hb_font_t *font)
{
  return font->parent;
}
1072

1073 1074 1075 1076 1077 1078 1079 1080
/**
 * hb_font_get_face:
 * @font: a font.
 *
 * 
 *
 * Return value: (transfer none): 
 *
S
Sascha Brawer 已提交
1081
 * Since: 0.9.2
1082
 **/
1083 1084 1085 1086 1087 1088 1089
hb_face_t *
hb_font_get_face (hb_font_t *font)
{
  return font->face;
}


1090 1091 1092
/**
 * hb_font_set_funcs:
 * @font: a font.
1093
 * @klass: (closure font_data) (destroy destroy) (scope notified):
1094
 * @font_data: 
1095 1096 1097 1098
 * @destroy: 
 *
 * 
 *
S
Sascha Brawer 已提交
1099
 * Since: 0.9.2
1100
 **/
B
Behdad Esfahbod 已提交
1101
void
B
Behdad Esfahbod 已提交
1102 1103
hb_font_set_funcs (hb_font_t         *font,
		   hb_font_funcs_t   *klass,
1104
		   void              *font_data,
1105
		   hb_destroy_func_t  destroy)
1106
{
B
Behdad Esfahbod 已提交
1107 1108
  if (font->immutable) {
    if (destroy)
1109
      destroy (font_data);
B
Behdad Esfahbod 已提交
1110
    return;
B
Behdad Esfahbod 已提交
1111
  }
1112

B
Behdad Esfahbod 已提交
1113 1114 1115
  if (font->destroy)
    font->destroy (font->user_data);

B
Behdad Esfahbod 已提交
1116
  if (!klass)
1117
    klass = hb_font_funcs_get_empty ();
B
Behdad Esfahbod 已提交
1118

B
Behdad Esfahbod 已提交
1119 1120 1121
  hb_font_funcs_reference (klass);
  hb_font_funcs_destroy (font->klass);
  font->klass = klass;
1122
  font->user_data = font_data;
1123
  font->destroy = destroy;
1124 1125
}

1126 1127 1128
/**
 * hb_font_set_funcs_data:
 * @font: a font.
1129
 * @font_data: (destroy destroy) (scope notified):
1130 1131 1132 1133
 * @destroy: 
 *
 * 
 *
S
Sascha Brawer 已提交
1134
 * Since: 0.9.2
1135
 **/
1136 1137
void
hb_font_set_funcs_data (hb_font_t         *font,
1138
		        void              *font_data,
1139 1140
		        hb_destroy_func_t  destroy)
{
B
Behdad Esfahbod 已提交
1141 1142 1143
  /* Destroy user_data? */
  if (font->immutable) {
    if (destroy)
1144
      destroy (font_data);
1145
    return;
B
Behdad Esfahbod 已提交
1146
  }
1147 1148 1149 1150

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

1151
  font->user_data = font_data;
1152 1153 1154
  font->destroy = destroy;
}

B
Behdad Esfahbod 已提交
1155

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
/**
 * hb_font_set_scale:
 * @font: a font.
 * @x_scale: 
 * @y_scale: 
 *
 * 
 *
 * Since: 1.0
 **/
1166 1167
void
hb_font_set_scale (hb_font_t *font,
1168 1169
		   int x_scale,
		   int y_scale)
1170
{
1171
  if (font->immutable)
1172 1173 1174 1175 1176 1177
    return;

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

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/**
 * hb_font_get_scale:
 * @font: a font.
 * @x_scale: (out): 
 * @y_scale: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
B
Behdad Esfahbod 已提交
1188 1189
void
hb_font_get_scale (hb_font_t *font,
1190 1191
		   int *x_scale,
		   int *y_scale)
B
Behdad Esfahbod 已提交
1192 1193 1194 1195 1196
{
  if (x_scale) *x_scale = font->x_scale;
  if (y_scale) *y_scale = font->y_scale;
}

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
/**
 * hb_font_set_ppem:
 * @font: a font.
 * @x_ppem: 
 * @y_ppem: 
 *
 * 
 *
 * Since: 1.0
 **/
1207 1208 1209 1210 1211
void
hb_font_set_ppem (hb_font_t *font,
		  unsigned int x_ppem,
		  unsigned int y_ppem)
{
1212
  if (font->immutable)
1213 1214 1215 1216 1217 1218
    return;

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

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
/**
 * hb_font_get_ppem:
 * @font: a font.
 * @x_ppem: (out): 
 * @y_ppem: (out): 
 *
 * 
 *
 * Since: 1.0
 **/
B
Behdad Esfahbod 已提交
1229 1230 1231 1232 1233 1234 1235 1236
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;
}