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 * Font face is objects represent a single face in a font family.
45 * More exactly, a font face represents a single face in a binary font file.
46 * Font faces are typically built from a binary blob and a face index.
47 * Font faces are used to create fonts.
55 * Get number of faces in a blob.
57 * Return value: Number of faces in @blob
62 hb_face_count (hb_blob_t *blob)
67 /* TODO We shouldn't be sanitizing blob. Port to run sanitizer and return if not sane. */
68 /* Make API signature const after. */
69 hb_blob_t *sanitized = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
70 const OT::OpenTypeFontFile& ot = *sanitized->as<OT::OpenTypeFontFile> ();
71 unsigned int ret = ot.get_face_count ();
72 hb_blob_destroy (sanitized);
81 DEFINE_NULL_INSTANCE (hb_face_t) =
83 HB_OBJECT_HEADER_STATIC,
85 nullptr, /* reference_table_func */
86 nullptr, /* user_data */
87 nullptr, /* destroy */
90 HB_ATOMIC_INT_INIT (1000), /* upem */
91 HB_ATOMIC_INT_INIT (0), /* num_glyphs */
93 /* Zero for the rest is fine. */
98 * hb_face_create_for_tables:
99 * @reference_table_func: (closure user_data) (destroy destroy) (scope notified):
105 * Return value: (transfer full)
110 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
112 hb_destroy_func_t destroy)
116 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
119 return hb_face_get_empty ();
122 face->reference_table_func = reference_table_func;
123 face->user_data = user_data;
124 face->destroy = destroy;
126 face->num_glyphs.set_relaxed (-1);
128 face->data.init0 (face);
129 face->table.init0 (face);
135 typedef struct hb_face_for_data_closure_t {
138 } hb_face_for_data_closure_t;
140 static hb_face_for_data_closure_t *
141 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
143 hb_face_for_data_closure_t *closure;
145 closure = (hb_face_for_data_closure_t *) calloc (1, sizeof (hb_face_for_data_closure_t));
146 if (unlikely (!closure))
149 closure->blob = blob;
150 closure->index = index;
156 _hb_face_for_data_closure_destroy (void *data)
158 hb_face_for_data_closure_t *closure = (hb_face_for_data_closure_t *) data;
160 hb_blob_destroy (closure->blob);
165 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
167 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
169 if (tag == HB_TAG_NONE)
170 return hb_blob_reference (data->blob);
172 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
173 unsigned int base_offset;
174 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index, &base_offset);
176 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
178 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, base_offset + table.offset, table.length);
184 * hb_face_create: (Xconstructor)
190 * Return value: (transfer full):
195 hb_face_create (hb_blob_t *blob,
200 if (unlikely (!blob))
201 blob = hb_blob_get_empty ();
203 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob)), index);
205 if (unlikely (!closure))
206 return hb_face_get_empty ();
208 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
210 _hb_face_for_data_closure_destroy);
222 * Return value: (transfer full)
229 return const_cast<hb_face_t *> (&Null (hb_face_t));
234 * hb_face_reference: (skip)
244 hb_face_reference (hb_face_t *face)
246 return hb_object_reference (face);
250 * hb_face_destroy: (skip)
258 hb_face_destroy (hb_face_t *face)
260 if (!hb_object_destroy (face)) return;
262 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
264 hb_face_t::plan_node_t *next = node->next;
265 hb_shape_plan_destroy (node->shape_plan);
274 face->destroy (face->user_data);
280 * hb_face_set_user_data: (skip)
294 hb_face_set_user_data (hb_face_t *face,
295 hb_user_data_key_t *key,
297 hb_destroy_func_t destroy,
300 return hb_object_set_user_data (face, key, data, destroy, replace);
304 * hb_face_get_user_data: (skip)
310 * Return value: (transfer none):
315 hb_face_get_user_data (const hb_face_t *face,
316 hb_user_data_key_t *key)
318 return hb_object_get_user_data (face, key);
322 * hb_face_make_immutable:
330 hb_face_make_immutable (hb_face_t *face)
332 if (hb_object_is_immutable (face))
335 hb_object_make_immutable (face);
339 * hb_face_is_immutable:
349 hb_face_is_immutable (const hb_face_t *face)
351 return hb_object_is_immutable (face);
356 * hb_face_reference_table:
362 * Return value: (transfer full):
367 hb_face_reference_table (const hb_face_t *face,
370 if (unlikely (tag == HB_TAG_NONE))
371 return hb_blob_get_empty ();
373 return face->reference_table (tag);
377 * hb_face_reference_blob:
382 * Return value: (transfer full):
387 hb_face_reference_blob (hb_face_t *face)
389 return face->reference_table (HB_TAG_NONE);
402 hb_face_set_index (hb_face_t *face,
405 if (hb_object_is_immutable (face))
422 hb_face_get_index (const hb_face_t *face)
437 hb_face_set_upem (hb_face_t *face,
440 if (hb_object_is_immutable (face))
443 face->upem.set_relaxed (upem);
457 hb_face_get_upem (const hb_face_t *face)
459 return face->get_upem ();
463 * hb_face_set_glyph_count:
472 hb_face_set_glyph_count (hb_face_t *face,
473 unsigned int glyph_count)
475 if (hb_object_is_immutable (face))
478 face->num_glyphs.set_relaxed (glyph_count);
482 * hb_face_get_glyph_count:
492 hb_face_get_glyph_count (const hb_face_t *face)
494 return face->get_num_glyphs ();
498 * hb_face_get_table_tags:
500 * @start_offset: index of first tag to return.
501 * @table_count: input length of @table_tags array, output number of items written.
502 * @table_tags: array to write tags into.
504 * Retrieves table tags for a face, if possible.
506 * Return value: total number of tables, or 0 if not possible to list.
511 hb_face_get_table_tags (const hb_face_t *face,
512 unsigned int start_offset,
513 unsigned int *table_count, /* IN/OUT */
514 hb_tag_t *table_tags /* OUT */)
516 if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
523 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
525 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
526 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
528 return ot_face.get_table_tags (start_offset, table_count, table_tags);
537 #ifndef HB_NO_FACE_COLLECT_UNICODES
539 * hb_face_collect_unicodes:
541 * @out: set to add Unicode characters covered by @face to.
546 hb_face_collect_unicodes (hb_face_t *face,
549 face->table.cmap->collect_unicodes (out, face->get_num_glyphs ());
552 * hb_face_collect_variation_selectors:
554 * @out: set to add Variation Selector characters covered by @face to.
561 hb_face_collect_variation_selectors (hb_face_t *face,
564 face->table.cmap->collect_variation_selectors (out);
567 * hb_face_collect_variation_unicodes:
569 * @out: set to add Unicode characters for @variation_selector covered by @face to.
576 hb_face_collect_variation_unicodes (hb_face_t *face,
577 hb_codepoint_t variation_selector,
580 face->table.cmap->collect_variation_unicodes (variation_selector, out);
586 * face-builder: A face that has add_table().
589 struct hb_face_builder_data_t
593 int cmp (hb_tag_t t) const
595 if (t < tag) return -1;
596 if (t > tag) return -1;
604 hb_vector_t<table_entry_t> tables;
607 static hb_face_builder_data_t *
608 _hb_face_builder_data_create ()
610 hb_face_builder_data_t *data = (hb_face_builder_data_t *) calloc (1, sizeof (hb_face_builder_data_t));
611 if (unlikely (!data))
614 data->tables.init ();
620 _hb_face_builder_data_destroy (void *user_data)
622 hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
624 for (unsigned int i = 0; i < data->tables.length; i++)
625 hb_blob_destroy (data->tables[i].blob);
627 data->tables.fini ();
633 _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data)
636 unsigned int table_count = data->tables.length;
637 unsigned int face_length = table_count * 16 + 12;
639 for (unsigned int i = 0; i < table_count; i++)
640 face_length += hb_ceil_to_4 (hb_blob_get_length (data->tables[i].blob));
642 char *buf = (char *) malloc (face_length);
646 hb_serialize_context_t c (buf, face_length);
647 c.propagate_error (data->tables);
648 OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();
650 bool is_cff = data->tables.lsearch (HB_TAG ('C','F','F',' ')) || data->tables.lsearch (HB_TAG ('C','F','F','2'));
651 hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag;
653 bool ret = f->serialize_single (&c, sfnt_tag, data->tables.as_array ());
663 return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, free);
667 _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
669 hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
672 return _hb_face_builder_data_reference_blob (data);
674 hb_face_builder_data_t::table_entry_t *entry = data->tables.lsearch (tag);
676 return hb_blob_reference (entry->blob);
683 * hb_face_builder_create:
685 * Creates a #hb_face_t that can be used with hb_face_builder_add_table().
686 * After tables are added to the face, it can be compiled to a binary
687 * font file by calling hb_face_reference_blob().
689 * Return value: (transfer full): New face.
694 hb_face_builder_create ()
696 hb_face_builder_data_t *data = _hb_face_builder_data_create ();
697 if (unlikely (!data)) return hb_face_get_empty ();
699 return hb_face_create_for_tables (_hb_face_builder_reference_table,
701 _hb_face_builder_data_destroy);
705 * hb_face_builder_add_table:
707 * Add table for @tag with data provided by @blob to the face. @face must
708 * be created using hb_face_builder_create().
713 hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
715 if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))
718 hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;
719 hb_face_builder_data_t::table_entry_t *entry = data->tables.push ();
722 entry->blob = hb_blob_reference (blob);