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"
42 * @short_description: Font face objects
45 * A font face is an object that represents a single face from within a
48 * More precisely, a font face represents a single face in a binary font file.
49 * Font faces are typically built from a binary blob and a face index.
50 * Font faces are used to create fonts.
58 * Fetches the number of faces in a blob.
60 * Return value: Number of faces in @blob
65 hb_face_count (hb_blob_t *blob)
70 /* TODO We shouldn't be sanitizing blob. Port to run sanitizer and return if not sane. */
71 /* Make API signature const after. */
72 hb_blob_t *sanitized = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
73 const OT::OpenTypeFontFile& ot = *sanitized->as<OT::OpenTypeFontFile> ();
74 unsigned int ret = ot.get_face_count ();
75 hb_blob_destroy (sanitized);
84 DEFINE_NULL_INSTANCE (hb_face_t) =
86 HB_OBJECT_HEADER_STATIC,
88 nullptr, /* reference_table_func */
89 nullptr, /* user_data */
90 nullptr, /* destroy */
96 /* Zero for the rest is fine. */
101 * hb_face_create_for_tables:
102 * @reference_table_func: (closure user_data) (destroy destroy) (scope notified): Table-referencing function
103 * @user_data: A pointer to the user data
104 * @destroy: (nullable): A callback to call when @data is not needed anymore
106 * Variant of hb_face_create(), built for those cases where it is more
107 * convenient to provide data for individual tables instead of the whole font
108 * data. With the caveat that hb_face_get_table_tags() does not currently work
109 * with faces created this way.
111 * Creates a new face object from the specified @user_data and @reference_table_func,
112 * with the @destroy callback.
114 * Return value: (transfer full): The new face object
119 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
121 hb_destroy_func_t destroy)
125 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
128 return hb_face_get_empty ();
131 face->reference_table_func = reference_table_func;
132 face->user_data = user_data;
133 face->destroy = destroy;
135 face->num_glyphs.set_relaxed (-1);
137 face->data.init0 (face);
138 face->table.init0 (face);
144 typedef struct hb_face_for_data_closure_t {
147 } hb_face_for_data_closure_t;
149 static hb_face_for_data_closure_t *
150 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
152 hb_face_for_data_closure_t *closure;
154 closure = (hb_face_for_data_closure_t *) hb_calloc (1, sizeof (hb_face_for_data_closure_t));
155 if (unlikely (!closure))
158 closure->blob = blob;
159 closure->index = (uint16_t) (index & 0xFFFFu);
165 _hb_face_for_data_closure_destroy (void *data)
167 hb_face_for_data_closure_t *closure = (hb_face_for_data_closure_t *) data;
169 hb_blob_destroy (closure->blob);
174 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
176 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
178 if (tag == HB_TAG_NONE)
179 return hb_blob_reference (data->blob);
181 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
182 unsigned int base_offset;
183 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index, &base_offset);
185 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
187 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, base_offset + table.offset, table.length);
193 * hb_face_create: (Xconstructor)
194 * @blob: #hb_blob_t to work upon
195 * @index: The index of the face within @blob
197 * Constructs a new face object from the specified blob and
198 * a face index into that blob.
200 * The face index is used for blobs of file formats such as TTC and
201 * and DFont that can contain more than one face. Face indices within
202 * such collections are zero-based.
204 * <note>Note: If the blob font format is not a collection, @index
205 * is ignored. Otherwise, only the lower 16-bits of @index are used.
206 * The unmodified @index can be accessed via hb_face_get_index().</note>
208 * <note>Note: The high 16-bits of @index, if non-zero, are used by
209 * hb_font_create() to load named-instances in variable fonts. See
210 * hb_font_create() for details.</note>
212 * Return value: (transfer full): The new face object
217 hb_face_create (hb_blob_t *blob,
222 if (unlikely (!blob))
223 blob = hb_blob_get_empty ();
225 blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
227 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (blob, index);
229 if (unlikely (!closure))
231 hb_blob_destroy (blob);
232 return hb_face_get_empty ();
235 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
237 _hb_face_for_data_closure_destroy);
247 * Fetches the singleton empty face object.
249 * Return value: (transfer full): The empty face object
256 return const_cast<hb_face_t *> (&Null (hb_face_t));
261 * hb_face_reference: (skip)
262 * @face: A face object
264 * Increases the reference count on a face object.
266 * Return value: The @face object
271 hb_face_reference (hb_face_t *face)
273 return hb_object_reference (face);
277 * hb_face_destroy: (skip)
278 * @face: A face object
280 * Decreases the reference count on a face object. When the
281 * reference count reaches zero, the face is destroyed,
282 * freeing all memory.
287 hb_face_destroy (hb_face_t *face)
289 if (!hb_object_destroy (face)) return;
291 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
293 hb_face_t::plan_node_t *next = node->next;
294 hb_shape_plan_destroy (node->shape_plan);
303 face->destroy (face->user_data);
309 * hb_face_set_user_data: (skip)
310 * @face: A face object
311 * @key: The user-data key to set
312 * @data: A pointer to the user data
313 * @destroy: (nullable): A callback to call when @data is not needed anymore
314 * @replace: Whether to replace an existing data with the same key
316 * Attaches a user-data key/data pair to the given face object.
318 * Return value: %true if success, %false otherwise
323 hb_face_set_user_data (hb_face_t *face,
324 hb_user_data_key_t *key,
326 hb_destroy_func_t destroy,
329 return hb_object_set_user_data (face, key, data, destroy, replace);
333 * hb_face_get_user_data: (skip)
334 * @face: A face object
335 * @key: The user-data key to query
337 * Fetches the user data associated with the specified key,
338 * attached to the specified face object.
340 * Return value: (transfer none): A pointer to the user data
345 hb_face_get_user_data (const hb_face_t *face,
346 hb_user_data_key_t *key)
348 return hb_object_get_user_data (face, key);
352 * hb_face_make_immutable:
353 * @face: A face object
355 * Makes the given face object immutable.
360 hb_face_make_immutable (hb_face_t *face)
362 if (hb_object_is_immutable (face))
365 hb_object_make_immutable (face);
369 * hb_face_is_immutable:
370 * @face: A face object
372 * Tests whether the given face object is immutable.
374 * Return value: %true is @face is immutable, %false otherwise
379 hb_face_is_immutable (const hb_face_t *face)
381 return hb_object_is_immutable (face);
386 * hb_face_reference_table:
387 * @face: A face object
388 * @tag: The #hb_tag_t of the table to query
390 * Fetches a reference to the specified table within
391 * the specified face.
393 * Return value: (transfer full): A pointer to the @tag table within @face
398 hb_face_reference_table (const hb_face_t *face,
401 if (unlikely (tag == HB_TAG_NONE))
402 return hb_blob_get_empty ();
404 return face->reference_table (tag);
408 * hb_face_reference_blob:
409 * @face: A face object
411 * Fetches a pointer to the binary blob that contains the
412 * specified face. Returns an empty blob if referencing face data is not
415 * Return value: (transfer full): A pointer to the blob for @face
420 hb_face_reference_blob (hb_face_t *face)
422 return face->reference_table (HB_TAG_NONE);
427 * @face: A face object
428 * @index: The index to assign
430 * Assigns the specified face-index to @face. Fails if the
433 * <note>Note: changing the index has no effect on the face itself
434 * This only changes the value returned by hb_face_get_index().</note>
439 hb_face_set_index (hb_face_t *face,
442 if (hb_object_is_immutable (face))
450 * @face: A face object
452 * Fetches the face-index corresponding to the given face.
454 * <note>Note: face indices within a collection are zero-based.</note>
456 * Return value: The index of @face.
461 hb_face_get_index (const hb_face_t *face)
468 * @face: A face object
469 * @upem: The units-per-em value to assign
471 * Sets the units-per-em (upem) for a face object to the specified value.
476 hb_face_set_upem (hb_face_t *face,
479 if (hb_object_is_immutable (face))
482 face->upem.set_relaxed (upem);
487 * @face: A face object
489 * Fetches the units-per-em (upem) value of the specified face object.
491 * Return value: The upem value of @face
496 hb_face_get_upem (const hb_face_t *face)
498 return face->get_upem ();
502 * hb_face_set_glyph_count:
503 * @face: A face object
504 * @glyph_count: The glyph-count value to assign
506 * Sets the glyph count for a face object to the specified value.
511 hb_face_set_glyph_count (hb_face_t *face,
512 unsigned int glyph_count)
514 if (hb_object_is_immutable (face))
517 face->num_glyphs.set_relaxed (glyph_count);
521 * hb_face_get_glyph_count:
522 * @face: A face object
524 * Fetches the glyph-count value of the specified face object.
526 * Return value: The glyph-count value of @face
531 hb_face_get_glyph_count (const hb_face_t *face)
533 return face->get_num_glyphs ();
537 * hb_face_get_table_tags:
538 * @face: A face object
539 * @start_offset: The index of first table tag to retrieve
540 * @table_count: (inout): Input = the maximum number of table tags to return;
541 * Output = the actual number of table tags returned (may be zero)
542 * @table_tags: (out) (array length=table_count): The array of table tags found
544 * Fetches a list of all table tags for a face, if possible. The list returned will
545 * begin at the offset provided
547 * Return value: Total number of tables, or zero if it is not possible to list
552 hb_face_get_table_tags (const hb_face_t *face,
553 unsigned int start_offset,
554 unsigned int *table_count, /* IN/OUT */
555 hb_tag_t *table_tags /* OUT */)
557 if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
564 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
566 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
567 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
569 return ot_face.get_table_tags (start_offset, table_count, table_tags);
578 #ifndef HB_NO_FACE_COLLECT_UNICODES
580 * hb_face_collect_unicodes:
581 * @face: A face object
582 * @out: The set to add Unicode characters to
584 * Collects all of the Unicode characters covered by @face and adds
585 * them to the #hb_set_t set @out.
590 hb_face_collect_unicodes (hb_face_t *face,
593 face->table.cmap->collect_unicodes (out, face->get_num_glyphs ());
596 * hb_face_collect_variation_selectors:
597 * @face: A face object
598 * @out: The set to add Variation Selector characters to
600 * Collects all Unicode "Variation Selector" characters covered by @face and adds
601 * them to the #hb_set_t set @out.
606 hb_face_collect_variation_selectors (hb_face_t *face,
609 face->table.cmap->collect_variation_selectors (out);
612 * hb_face_collect_variation_unicodes:
613 * @face: A face object
614 * @variation_selector: The Variation Selector to query
615 * @out: The set to add Unicode characters to
617 * Collects all Unicode characters for @variation_selector covered by @face and adds
618 * them to the #hb_set_t set @out.
623 hb_face_collect_variation_unicodes (hb_face_t *face,
624 hb_codepoint_t variation_selector,
627 face->table.cmap->collect_variation_unicodes (variation_selector, out);
633 * face-builder: A face that has add_table().
636 struct hb_face_builder_data_t
638 hb_hashmap_t<hb_tag_t, hb_blob_t *> tables;
641 static int compare_entries (const void* pa, const void* pb)
643 const auto& a = * (const hb_pair_t<hb_tag_t, hb_blob_t*> *) pa;
644 const auto& b = * (const hb_pair_t<hb_tag_t, hb_blob_t*> *) pb;
646 /* Order by blob size first (smallest to largest) and then table tag */
648 if (a.second->length != b.second->length)
649 return a.second->length < b.second->length ? -1 : +1;
651 return a.first < b.first ? -1 : a.first == b.first ? 0 : +1;
654 static hb_face_builder_data_t *
655 _hb_face_builder_data_create ()
657 hb_face_builder_data_t *data = (hb_face_builder_data_t *) hb_calloc (1, sizeof (hb_face_builder_data_t));
658 if (unlikely (!data))
661 data->tables.init ();
667 _hb_face_builder_data_destroy (void *user_data)
669 hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
671 for (hb_blob_t* b : data->tables.values())
674 data->tables.fini ();
680 _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data)
683 unsigned int table_count = data->tables.get_population ();
684 unsigned int face_length = table_count * 16 + 12;
686 for (hb_blob_t* b : data->tables.values())
687 face_length += hb_ceil_to_4 (hb_blob_get_length (b));
689 char *buf = (char *) hb_malloc (face_length);
693 hb_serialize_context_t c (buf, face_length);
694 c.propagate_error (data->tables);
695 OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();
697 bool is_cff = (data->tables.has (HB_TAG ('C','F','F',' '))
698 || data->tables.has (HB_TAG ('C','F','F','2')));
699 hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag;
701 // Sort the tags so that produced face is deterministic.
702 hb_vector_t<hb_pair_t <hb_tag_t, hb_blob_t*>> sorted_entries;
703 data->tables.iter () | hb_sink (sorted_entries);
704 if (unlikely (sorted_entries.in_error ()))
710 sorted_entries.qsort (compare_entries);
711 bool ret = f->serialize_single (&c, sfnt_tag, + sorted_entries.iter());
721 return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, hb_free);
725 _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
727 hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
730 return _hb_face_builder_data_reference_blob (data);
732 return hb_blob_reference (data->tables[tag]);
737 * hb_face_builder_create:
739 * Creates a #hb_face_t that can be used with hb_face_builder_add_table().
740 * After tables are added to the face, it can be compiled to a binary
741 * font file by calling hb_face_reference_blob().
743 * Return value: (transfer full): New face.
748 hb_face_builder_create ()
750 hb_face_builder_data_t *data = _hb_face_builder_data_create ();
751 if (unlikely (!data)) return hb_face_get_empty ();
753 return hb_face_create_for_tables (_hb_face_builder_reference_table,
755 _hb_face_builder_data_destroy);
759 * hb_face_builder_add_table:
760 * @face: A face object created with hb_face_builder_create()
761 * @tag: The #hb_tag_t of the table to add
762 * @blob: The blob containing the table data to add
764 * Add table for @tag with data provided by @blob to the face. @face must
765 * be created using hb_face_builder_create().
770 hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
772 if (tag == HB_MAP_VALUE_INVALID)
775 if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))
778 hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;
780 hb_blob_t* previous = data->tables.get (tag);
781 if (!data->tables.set (tag, hb_blob_reference (blob)))
783 hb_blob_destroy (blob);
787 hb_blob_destroy (previous);