1 /*
2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29 #include "hb.hh"
30
31 #include "hb-face.hh"
32 #include "hb-blob.hh"
33 #include "hb-open-file.hh"
34 #include "hb-ot-face.hh"
35 #include "hb-ot-cmap-table.hh"
36
37
38 /**
39 * SECTION:hb-face
40 * @title: hb-face
41 * @short_description: Font face objects
42 * @include: hb.h
43 *
44 * A font face is an object that represents a single face from within a
45 * font family.
46 *
47 * More precisely, a font face represents a single face in a binary font file.
48 * Font faces are typically built from a binary blob and a face index.
49 * Font faces are used to create fonts.
50 *
51 * A font face can be created from a binary blob using hb_face_create().
52 * The face index is used to select a face from a binary blob that contains
53 * multiple faces. For example, a binary blob that contains both a regular
54 * and a bold face can be used to create two font faces, one for each face
55 * index.
56 **/
57
58
59 /**
60 * hb_face_count:
61 * @blob: a blob.
62 *
63 * Fetches the number of faces in a blob.
64 *
65 * Return value: Number of faces in @blob
66 *
67 * Since: 1.7.7
68 **/
69 unsigned int
hb_face_count(hb_blob_t * blob)70 hb_face_count (hb_blob_t *blob)
71 {
72 if (unlikely (!blob))
73 return 0;
74
75 /* TODO We shouldn't be sanitizing blob. Port to run sanitizer and return if not sane. */
76 /* Make API signature const after. */
77 hb_blob_t *sanitized = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
78 const OT::OpenTypeFontFile& ot = *sanitized->as<OT::OpenTypeFontFile> ();
79 unsigned int ret = ot.get_face_count ();
80 hb_blob_destroy (sanitized);
81
82 return ret;
83 }
84
85 /*
86 * hb_face_t
87 */
88
89 DEFINE_NULL_INSTANCE (hb_face_t) =
90 {
91 HB_OBJECT_HEADER_STATIC,
92
93 nullptr, /* reference_table_func */
94 nullptr, /* user_data */
95 nullptr, /* destroy */
96
97 0, /* index */
98 1000, /* upem */
99 0, /* num_glyphs */
100
101 /* Zero for the rest is fine. */
102 };
103
104
105 /**
106 * hb_face_create_for_tables:
107 * @reference_table_func: (closure user_data) (destroy destroy) (scope notified): Table-referencing function
108 * @user_data: A pointer to the user data
109 * @destroy: (nullable): A callback to call when @data is not needed anymore
110 *
111 * Variant of hb_face_create(), built for those cases where it is more
112 * convenient to provide data for individual tables instead of the whole font
113 * data. With the caveat that hb_face_get_table_tags() does not currently work
114 * with faces created this way.
115 *
116 * Creates a new face object from the specified @user_data and @reference_table_func,
117 * with the @destroy callback.
118 *
119 * Return value: (transfer full): The new face object
120 *
121 * Since: 0.9.2
122 **/
123 hb_face_t *
hb_face_create_for_tables(hb_reference_table_func_t reference_table_func,void * user_data,hb_destroy_func_t destroy)124 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
125 void *user_data,
126 hb_destroy_func_t destroy)
127 {
128 hb_face_t *face;
129
130 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
131 if (destroy)
132 destroy (user_data);
133 return hb_face_get_empty ();
134 }
135
136 face->reference_table_func = reference_table_func;
137 face->user_data = user_data;
138 face->destroy = destroy;
139
140 face->num_glyphs = -1;
141
142 face->data.init0 (face);
143 face->table.init0 (face);
144
145 return face;
146 }
147
148
149 typedef struct hb_face_for_data_closure_t {
150 hb_blob_t *blob;
151 uint16_t index;
152 } hb_face_for_data_closure_t;
153
154 static hb_face_for_data_closure_t *
_hb_face_for_data_closure_create(hb_blob_t * blob,unsigned int index)155 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
156 {
157 hb_face_for_data_closure_t *closure;
158
159 closure = (hb_face_for_data_closure_t *) hb_calloc (1, sizeof (hb_face_for_data_closure_t));
160 if (unlikely (!closure))
161 return nullptr;
162
163 closure->blob = blob;
164 closure->index = (uint16_t) (index & 0xFFFFu);
165
166 return closure;
167 }
168
169 static void
_hb_face_for_data_closure_destroy(void * data)170 _hb_face_for_data_closure_destroy (void *data)
171 {
172 hb_face_for_data_closure_t *closure = (hb_face_for_data_closure_t *) data;
173
174 hb_blob_destroy (closure->blob);
175 hb_free (closure);
176 }
177
178 static hb_blob_t *
_hb_face_for_data_reference_table(hb_face_t * face HB_UNUSED,hb_tag_t tag,void * user_data)179 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
180 {
181 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
182
183 if (tag == HB_TAG_NONE)
184 return hb_blob_reference (data->blob);
185
186 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
187 unsigned int base_offset;
188 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index, &base_offset);
189
190 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
191
192 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, base_offset + table.offset, table.length);
193
194 return blob;
195 }
196
197 /**
198 * hb_face_create:
199 * @blob: #hb_blob_t to work upon
200 * @index: The index of the face within @blob
201 *
202 * Constructs a new face object from the specified blob and
203 * a face index into that blob.
204 *
205 * The face index is used for blobs of file formats such as TTC and
206 * DFont that can contain more than one face. Face indices within
207 * such collections are zero-based.
208 *
209 * <note>Note: If the blob font format is not a collection, @index
210 * is ignored. Otherwise, only the lower 16-bits of @index are used.
211 * The unmodified @index can be accessed via hb_face_get_index().</note>
212 *
213 * <note>Note: The high 16-bits of @index, if non-zero, are used by
214 * hb_font_create() to load named-instances in variable fonts. See
215 * hb_font_create() for details.</note>
216 *
217 * Return value: (transfer full): The new face object
218 *
219 * Since: 0.9.2
220 **/
221 hb_face_t *
hb_face_create(hb_blob_t * blob,unsigned int index)222 hb_face_create (hb_blob_t *blob,
223 unsigned int index)
224 {
225 hb_face_t *face;
226
227 if (unlikely (!blob))
228 blob = hb_blob_get_empty ();
229
230 blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
231
232 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (blob, index);
233
234 if (unlikely (!closure))
235 {
236 hb_blob_destroy (blob);
237 return hb_face_get_empty ();
238 }
239
240 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
241 closure,
242 _hb_face_for_data_closure_destroy);
243
244 face->index = index;
245
246 return face;
247 }
248
249 /**
250 * hb_face_get_empty:
251 *
252 * Fetches the singleton empty face object.
253 *
254 * Return value: (transfer full): The empty face object
255 *
256 * Since: 0.9.2
257 **/
258 hb_face_t *
hb_face_get_empty()259 hb_face_get_empty ()
260 {
261 return const_cast<hb_face_t *> (&Null (hb_face_t));
262 }
263
264
265 /**
266 * hb_face_reference: (skip)
267 * @face: A face object
268 *
269 * Increases the reference count on a face object.
270 *
271 * Return value: The @face object
272 *
273 * Since: 0.9.2
274 **/
275 hb_face_t *
hb_face_reference(hb_face_t * face)276 hb_face_reference (hb_face_t *face)
277 {
278 return hb_object_reference (face);
279 }
280
281 /**
282 * hb_face_destroy: (skip)
283 * @face: A face object
284 *
285 * Decreases the reference count on a face object. When the
286 * reference count reaches zero, the face is destroyed,
287 * freeing all memory.
288 *
289 * Since: 0.9.2
290 **/
291 void
hb_face_destroy(hb_face_t * face)292 hb_face_destroy (hb_face_t *face)
293 {
294 if (!hb_object_destroy (face)) return;
295
296 #ifndef HB_NO_SHAPER
297 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
298 {
299 hb_face_t::plan_node_t *next = node->next;
300 hb_shape_plan_destroy (node->shape_plan);
301 hb_free (node);
302 node = next;
303 }
304 #endif
305
306 face->data.fini ();
307 face->table.fini ();
308
309 if (face->destroy)
310 face->destroy (face->user_data);
311
312 hb_free (face);
313 }
314
315 /**
316 * hb_face_set_user_data: (skip)
317 * @face: A face object
318 * @key: The user-data key to set
319 * @data: A pointer to the user data
320 * @destroy: (nullable): A callback to call when @data is not needed anymore
321 * @replace: Whether to replace an existing data with the same key
322 *
323 * Attaches a user-data key/data pair to the given face object.
324 *
325 * Return value: `true` if success, `false` otherwise
326 *
327 * Since: 0.9.2
328 **/
329 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,hb_bool_t replace)330 hb_face_set_user_data (hb_face_t *face,
331 hb_user_data_key_t *key,
332 void * data,
333 hb_destroy_func_t destroy,
334 hb_bool_t replace)
335 {
336 return hb_object_set_user_data (face, key, data, destroy, replace);
337 }
338
339 /**
340 * hb_face_get_user_data: (skip)
341 * @face: A face object
342 * @key: The user-data key to query
343 *
344 * Fetches the user data associated with the specified key,
345 * attached to the specified face object.
346 *
347 * Return value: (transfer none): A pointer to the user data
348 *
349 * Since: 0.9.2
350 **/
351 void *
hb_face_get_user_data(const hb_face_t * face,hb_user_data_key_t * key)352 hb_face_get_user_data (const hb_face_t *face,
353 hb_user_data_key_t *key)
354 {
355 return hb_object_get_user_data (face, key);
356 }
357
358 /**
359 * hb_face_make_immutable:
360 * @face: A face object
361 *
362 * Makes the given face object immutable.
363 *
364 * Since: 0.9.2
365 **/
366 void
hb_face_make_immutable(hb_face_t * face)367 hb_face_make_immutable (hb_face_t *face)
368 {
369 if (hb_object_is_immutable (face))
370 return;
371
372 hb_object_make_immutable (face);
373 }
374
375 /**
376 * hb_face_is_immutable:
377 * @face: A face object
378 *
379 * Tests whether the given face object is immutable.
380 *
381 * Return value: `true` is @face is immutable, `false` otherwise
382 *
383 * Since: 0.9.2
384 **/
385 hb_bool_t
hb_face_is_immutable(const hb_face_t * face)386 hb_face_is_immutable (const hb_face_t *face)
387 {
388 return hb_object_is_immutable (face);
389 }
390
391
392 /**
393 * hb_face_reference_table:
394 * @face: A face object
395 * @tag: The #hb_tag_t of the table to query
396 *
397 * Fetches a reference to the specified table within
398 * the specified face.
399 *
400 * Return value: (transfer full): A pointer to the @tag table within @face
401 *
402 * Since: 0.9.2
403 **/
404 hb_blob_t *
hb_face_reference_table(const hb_face_t * face,hb_tag_t tag)405 hb_face_reference_table (const hb_face_t *face,
406 hb_tag_t tag)
407 {
408 if (unlikely (tag == HB_TAG_NONE))
409 return hb_blob_get_empty ();
410
411 return face->reference_table (tag);
412 }
413
414 /**
415 * hb_face_reference_blob:
416 * @face: A face object
417 *
418 * Fetches a pointer to the binary blob that contains the
419 * specified face. Returns an empty blob if referencing face data is not
420 * possible.
421 *
422 * Return value: (transfer full): A pointer to the blob for @face
423 *
424 * Since: 0.9.2
425 **/
426 hb_blob_t *
hb_face_reference_blob(hb_face_t * face)427 hb_face_reference_blob (hb_face_t *face)
428 {
429 return face->reference_table (HB_TAG_NONE);
430 }
431
432 /**
433 * hb_face_set_index:
434 * @face: A face object
435 * @index: The index to assign
436 *
437 * Assigns the specified face-index to @face. Fails if the
438 * face is immutable.
439 *
440 * <note>Note: changing the index has no effect on the face itself
441 * This only changes the value returned by hb_face_get_index().</note>
442 *
443 * Since: 0.9.2
444 **/
445 void
hb_face_set_index(hb_face_t * face,unsigned int index)446 hb_face_set_index (hb_face_t *face,
447 unsigned int index)
448 {
449 if (hb_object_is_immutable (face))
450 return;
451
452 face->index = index;
453 }
454
455 /**
456 * hb_face_get_index:
457 * @face: A face object
458 *
459 * Fetches the face-index corresponding to the given face.
460 *
461 * <note>Note: face indices within a collection are zero-based.</note>
462 *
463 * Return value: The index of @face.
464 *
465 * Since: 0.9.2
466 **/
467 unsigned int
hb_face_get_index(const hb_face_t * face)468 hb_face_get_index (const hb_face_t *face)
469 {
470 return face->index;
471 }
472
473 /**
474 * hb_face_set_upem:
475 * @face: A face object
476 * @upem: The units-per-em value to assign
477 *
478 * Sets the units-per-em (upem) for a face object to the specified value.
479 *
480 * This API is used in rare circumstances.
481 *
482 * Since: 0.9.2
483 **/
484 void
hb_face_set_upem(hb_face_t * face,unsigned int upem)485 hb_face_set_upem (hb_face_t *face,
486 unsigned int upem)
487 {
488 if (hb_object_is_immutable (face))
489 return;
490
491 face->upem = upem;
492 }
493
494 /**
495 * hb_face_get_upem:
496 * @face: A face object
497 *
498 * Fetches the units-per-em (UPEM) value of the specified face object.
499 *
500 * Typical UPEM values for fonts are 1000, or 2048, but any value
501 * in between 16 and 16,384 is allowed for OpenType fonts.
502 *
503 * Return value: The upem value of @face
504 *
505 * Since: 0.9.2
506 **/
507 unsigned int
hb_face_get_upem(const hb_face_t * face)508 hb_face_get_upem (const hb_face_t *face)
509 {
510 return face->get_upem ();
511 }
512
513 /**
514 * hb_face_set_glyph_count:
515 * @face: A face object
516 * @glyph_count: The glyph-count value to assign
517 *
518 * Sets the glyph count for a face object to the specified value.
519 *
520 * This API is used in rare circumstances.
521 *
522 * Since: 0.9.7
523 **/
524 void
hb_face_set_glyph_count(hb_face_t * face,unsigned int glyph_count)525 hb_face_set_glyph_count (hb_face_t *face,
526 unsigned int glyph_count)
527 {
528 if (hb_object_is_immutable (face))
529 return;
530
531 face->num_glyphs = glyph_count;
532 }
533
534 /**
535 * hb_face_get_glyph_count:
536 * @face: A face object
537 *
538 * Fetches the glyph-count value of the specified face object.
539 *
540 * Return value: The glyph-count value of @face
541 *
542 * Since: 0.9.7
543 **/
544 unsigned int
hb_face_get_glyph_count(const hb_face_t * face)545 hb_face_get_glyph_count (const hb_face_t *face)
546 {
547 return face->get_num_glyphs ();
548 }
549
550 /**
551 * hb_face_get_table_tags:
552 * @face: A face object
553 * @start_offset: The index of first table tag to retrieve
554 * @table_count: (inout): Input = the maximum number of table tags to return;
555 * Output = the actual number of table tags returned (may be zero)
556 * @table_tags: (out) (array length=table_count): The array of table tags found
557 *
558 * Fetches a list of all table tags for a face, if possible. The list returned will
559 * begin at the offset provided
560 *
561 * Return value: Total number of tables, or zero if it is not possible to list
562 *
563 * Since: 1.6.0
564 **/
565 unsigned int
hb_face_get_table_tags(const hb_face_t * face,unsigned int start_offset,unsigned int * table_count,hb_tag_t * table_tags)566 hb_face_get_table_tags (const hb_face_t *face,
567 unsigned int start_offset,
568 unsigned int *table_count, /* IN/OUT */
569 hb_tag_t *table_tags /* OUT */)
570 {
571 if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
572 {
573 if (table_count)
574 *table_count = 0;
575 return 0;
576 }
577
578 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
579
580 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
581 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
582
583 return ot_face.get_table_tags (start_offset, table_count, table_tags);
584 }
585
586
587 /*
588 * Character set.
589 */
590
591
592 #ifndef HB_NO_FACE_COLLECT_UNICODES
593 /**
594 * hb_face_collect_unicodes:
595 * @face: A face object
596 * @out: (out): The set to add Unicode characters to
597 *
598 * Collects all of the Unicode characters covered by @face and adds
599 * them to the #hb_set_t set @out.
600 *
601 * Since: 1.9.0
602 */
603 void
hb_face_collect_unicodes(hb_face_t * face,hb_set_t * out)604 hb_face_collect_unicodes (hb_face_t *face,
605 hb_set_t *out)
606 {
607 face->table.cmap->collect_unicodes (out, face->get_num_glyphs ());
608 }
609 /**
610 * hb_face_collect_nominal_glyph_mapping:
611 * @face: A face object
612 * @mapping: (out): The map to add Unicode-to-glyph mapping to
613 * @unicodes: (nullable) (out): The set to add Unicode characters to, or `NULL`
614 *
615 * Collects the mapping from Unicode characters to nominal glyphs of the @face,
616 * and optionally all of the Unicode characters covered by @face.
617 *
618 * Since: 7.0.0
619 */
620 void
hb_face_collect_nominal_glyph_mapping(hb_face_t * face,hb_map_t * mapping,hb_set_t * unicodes)621 hb_face_collect_nominal_glyph_mapping (hb_face_t *face,
622 hb_map_t *mapping,
623 hb_set_t *unicodes)
624 {
625 hb_set_t stack_unicodes;
626 if (!unicodes)
627 unicodes = &stack_unicodes;
628 face->table.cmap->collect_mapping (unicodes, mapping, face->get_num_glyphs ());
629 }
630 /**
631 * hb_face_collect_variation_selectors:
632 * @face: A face object
633 * @out: (out): The set to add Variation Selector characters to
634 *
635 * Collects all Unicode "Variation Selector" characters covered by @face and adds
636 * them to the #hb_set_t set @out.
637 *
638 * Since: 1.9.0
639 */
640 void
hb_face_collect_variation_selectors(hb_face_t * face,hb_set_t * out)641 hb_face_collect_variation_selectors (hb_face_t *face,
642 hb_set_t *out)
643 {
644 face->table.cmap->collect_variation_selectors (out);
645 }
646 /**
647 * hb_face_collect_variation_unicodes:
648 * @face: A face object
649 * @variation_selector: The Variation Selector to query
650 * @out: (out): The set to add Unicode characters to
651 *
652 * Collects all Unicode characters for @variation_selector covered by @face and adds
653 * them to the #hb_set_t set @out.
654 *
655 * Since: 1.9.0
656 */
657 void
hb_face_collect_variation_unicodes(hb_face_t * face,hb_codepoint_t variation_selector,hb_set_t * out)658 hb_face_collect_variation_unicodes (hb_face_t *face,
659 hb_codepoint_t variation_selector,
660 hb_set_t *out)
661 {
662 face->table.cmap->collect_variation_unicodes (variation_selector, out);
663 }
664 #endif
665