2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
33 #include "hb-open-file.hh"
34 #include "hb-ot-face.hh"
35 #include "hb-ot-cmap-table.hh"
41 * @short_description: Font face objects
44 * A font face is an object that represents a single face from within a
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.
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
63 * Fetches the number of faces in a blob.
65 * Return value: Number of faces in @blob
70 hb_face_count (hb_blob_t *blob)
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);
89 DEFINE_NULL_INSTANCE (hb_face_t) =
91 HB_OBJECT_HEADER_STATIC,
93 nullptr, /* reference_table_func */
94 nullptr, /* user_data */
95 nullptr, /* destroy */
101 /* Zero for the rest is fine. */
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
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.
116 * Creates a new face object from the specified @user_data and @reference_table_func,
117 * with the @destroy callback.
119 * Return value: (transfer full): The new face object
124 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
126 hb_destroy_func_t destroy)
130 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
133 return hb_face_get_empty ();
136 face->reference_table_func = reference_table_func;
137 face->user_data = user_data;
138 face->destroy = destroy;
140 face->num_glyphs = -1;
142 face->data.init0 (face);
143 face->table.init0 (face);
149 typedef struct hb_face_for_data_closure_t {
152 } hb_face_for_data_closure_t;
154 static hb_face_for_data_closure_t *
155 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
157 hb_face_for_data_closure_t *closure;
159 closure = (hb_face_for_data_closure_t *) hb_calloc (1, sizeof (hb_face_for_data_closure_t));
160 if (unlikely (!closure))
163 closure->blob = blob;
164 closure->index = (uint16_t) (index & 0xFFFFu);
170 _hb_face_for_data_closure_destroy (void *data)
172 hb_face_for_data_closure_t *closure = (hb_face_for_data_closure_t *) data;
174 hb_blob_destroy (closure->blob);
179 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
181 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
183 if (tag == HB_TAG_NONE)
184 return hb_blob_reference (data->blob);
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);
190 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
192 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, base_offset + table.offset, table.length);
199 * @blob: #hb_blob_t to work upon
200 * @index: The index of the face within @blob
202 * Constructs a new face object from the specified blob and
203 * a face index into that blob.
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.
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>
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>
217 * Return value: (transfer full): The new face object
222 hb_face_create (hb_blob_t *blob,
227 if (unlikely (!blob))
228 blob = hb_blob_get_empty ();
230 blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
232 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (blob, index);
234 if (unlikely (!closure))
236 hb_blob_destroy (blob);
237 return hb_face_get_empty ();
240 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
242 _hb_face_for_data_closure_destroy);
252 * Fetches the singleton empty face object.
254 * Return value: (transfer full): The empty face object
261 return const_cast<hb_face_t *> (&Null (hb_face_t));
266 * hb_face_reference: (skip)
267 * @face: A face object
269 * Increases the reference count on a face object.
271 * Return value: The @face object
276 hb_face_reference (hb_face_t *face)
278 return hb_object_reference (face);
282 * hb_face_destroy: (skip)
283 * @face: A face object
285 * Decreases the reference count on a face object. When the
286 * reference count reaches zero, the face is destroyed,
287 * freeing all memory.
292 hb_face_destroy (hb_face_t *face)
294 if (!hb_object_destroy (face)) return;
297 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
299 hb_face_t::plan_node_t *next = node->next;
300 hb_shape_plan_destroy (node->shape_plan);
310 face->destroy (face->user_data);
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
323 * Attaches a user-data key/data pair to the given face object.
325 * Return value: `true` if success, `false` otherwise
330 hb_face_set_user_data (hb_face_t *face,
331 hb_user_data_key_t *key,
333 hb_destroy_func_t destroy,
336 return hb_object_set_user_data (face, key, data, destroy, replace);
340 * hb_face_get_user_data: (skip)
341 * @face: A face object
342 * @key: The user-data key to query
344 * Fetches the user data associated with the specified key,
345 * attached to the specified face object.
347 * Return value: (transfer none): A pointer to the user data
352 hb_face_get_user_data (const hb_face_t *face,
353 hb_user_data_key_t *key)
355 return hb_object_get_user_data (face, key);
359 * hb_face_make_immutable:
360 * @face: A face object
362 * Makes the given face object immutable.
367 hb_face_make_immutable (hb_face_t *face)
369 if (hb_object_is_immutable (face))
372 hb_object_make_immutable (face);
376 * hb_face_is_immutable:
377 * @face: A face object
379 * Tests whether the given face object is immutable.
381 * Return value: `true` is @face is immutable, `false` otherwise
386 hb_face_is_immutable (const hb_face_t *face)
388 return hb_object_is_immutable (face);
393 * hb_face_reference_table:
394 * @face: A face object
395 * @tag: The #hb_tag_t of the table to query
397 * Fetches a reference to the specified table within
398 * the specified face.
400 * Return value: (transfer full): A pointer to the @tag table within @face
405 hb_face_reference_table (const hb_face_t *face,
408 if (unlikely (tag == HB_TAG_NONE))
409 return hb_blob_get_empty ();
411 return face->reference_table (tag);
415 * hb_face_reference_blob:
416 * @face: A face object
418 * Fetches a pointer to the binary blob that contains the
419 * specified face. Returns an empty blob if referencing face data is not
422 * Return value: (transfer full): A pointer to the blob for @face
427 hb_face_reference_blob (hb_face_t *face)
429 return face->reference_table (HB_TAG_NONE);
434 * @face: A face object
435 * @index: The index to assign
437 * Assigns the specified face-index to @face. Fails if the
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>
446 hb_face_set_index (hb_face_t *face,
449 if (hb_object_is_immutable (face))
457 * @face: A face object
459 * Fetches the face-index corresponding to the given face.
461 * <note>Note: face indices within a collection are zero-based.</note>
463 * Return value: The index of @face.
468 hb_face_get_index (const hb_face_t *face)
475 * @face: A face object
476 * @upem: The units-per-em value to assign
478 * Sets the units-per-em (upem) for a face object to the specified value.
480 * This API is used in rare circumstances.
485 hb_face_set_upem (hb_face_t *face,
488 if (hb_object_is_immutable (face))
496 * @face: A face object
498 * Fetches the units-per-em (UPEM) value of the specified face object.
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.
503 * Return value: The upem value of @face
508 hb_face_get_upem (const hb_face_t *face)
510 return face->get_upem ();
514 * hb_face_set_glyph_count:
515 * @face: A face object
516 * @glyph_count: The glyph-count value to assign
518 * Sets the glyph count for a face object to the specified value.
520 * This API is used in rare circumstances.
525 hb_face_set_glyph_count (hb_face_t *face,
526 unsigned int glyph_count)
528 if (hb_object_is_immutable (face))
531 face->num_glyphs = glyph_count;
535 * hb_face_get_glyph_count:
536 * @face: A face object
538 * Fetches the glyph-count value of the specified face object.
540 * Return value: The glyph-count value of @face
545 hb_face_get_glyph_count (const hb_face_t *face)
547 return face->get_num_glyphs ();
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
558 * Fetches a list of all table tags for a face, if possible. The list returned will
559 * begin at the offset provided
561 * Return value: Total number of tables, or zero if it is not possible to list
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 */)
571 if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
578 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
580 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
581 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
583 return ot_face.get_table_tags (start_offset, table_count, table_tags);
592 #ifndef HB_NO_FACE_COLLECT_UNICODES
594 * hb_face_collect_unicodes:
595 * @face: A face object
596 * @out: (out): The set to add Unicode characters to
598 * Collects all of the Unicode characters covered by @face and adds
599 * them to the #hb_set_t set @out.
604 hb_face_collect_unicodes (hb_face_t *face,
607 face->table.cmap->collect_unicodes (out, face->get_num_glyphs ());
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`
615 * Collects the mapping from Unicode characters to nominal glyphs of the @face,
616 * and optionally all of the Unicode characters covered by @face.
621 hb_face_collect_nominal_glyph_mapping (hb_face_t *face,
625 hb_set_t stack_unicodes;
627 unicodes = &stack_unicodes;
628 face->table.cmap->collect_mapping (unicodes, mapping, face->get_num_glyphs ());
631 * hb_face_collect_variation_selectors:
632 * @face: A face object
633 * @out: (out): The set to add Variation Selector characters to
635 * Collects all Unicode "Variation Selector" characters covered by @face and adds
636 * them to the #hb_set_t set @out.
641 hb_face_collect_variation_selectors (hb_face_t *face,
644 face->table.cmap->collect_variation_selectors (out);
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
652 * Collects all Unicode characters for @variation_selector covered by @face and adds
653 * them to the #hb_set_t set @out.
658 hb_face_collect_variation_unicodes (hb_face_t *face,
659 hb_codepoint_t variation_selector,
662 face->table.cmap->collect_variation_unicodes (variation_selector, out);