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 return face->reference_table (tag);
374 * hb_face_reference_blob:
379 * Return value: (transfer full):
384 hb_face_reference_blob (hb_face_t *face)
386 return face->reference_table (HB_TAG_NONE);
399 hb_face_set_index (hb_face_t *face,
402 if (hb_object_is_immutable (face))
419 hb_face_get_index (const hb_face_t *face)
434 hb_face_set_upem (hb_face_t *face,
437 if (hb_object_is_immutable (face))
440 face->upem.set_relaxed (upem);
454 hb_face_get_upem (const hb_face_t *face)
456 return face->get_upem ();
460 * hb_face_set_glyph_count:
469 hb_face_set_glyph_count (hb_face_t *face,
470 unsigned int glyph_count)
472 if (hb_object_is_immutable (face))
475 face->num_glyphs.set_relaxed (glyph_count);
479 * hb_face_get_glyph_count:
489 hb_face_get_glyph_count (const hb_face_t *face)
491 return face->get_num_glyphs ();
495 * hb_face_get_table_tags:
497 * @start_offset: index of first tag to return.
498 * @table_count: input length of @table_tags array, output number of items written.
499 * @table_tags: array to write tags into.
501 * Retrieves table tags for a face, if possible.
503 * Return value: total number of tables, or 0 if not possible to list.
508 hb_face_get_table_tags (const hb_face_t *face,
509 unsigned int start_offset,
510 unsigned int *table_count, /* IN/OUT */
511 hb_tag_t *table_tags /* OUT */)
513 if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
520 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
522 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
523 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
525 return ot_face.get_table_tags (start_offset, table_count, table_tags);
535 * hb_face_collect_unicodes:
537 * @out: set to add Unicode characters covered by @face to.
542 hb_face_collect_unicodes (hb_face_t *face,
545 face->table.cmap->collect_unicodes (out);
549 * hb_face_collect_variation_selectors:
551 * @out: set to add Variation Selector characters covered by @face to.
558 hb_face_collect_variation_selectors (hb_face_t *face,
561 face->table.cmap->collect_variation_selectors (out);
565 * hb_face_collect_variation_unicodes:
567 * @out: set to add Unicode characters for @variation_selector covered by @face to.
574 hb_face_collect_variation_unicodes (hb_face_t *face,
575 hb_codepoint_t variation_selector,
578 face->table.cmap->collect_variation_unicodes (variation_selector, out);
584 * face-builder: A face that has add_table().
587 struct hb_face_builder_data_t
591 int cmp (hb_tag_t t) const
593 if (t < tag) return -1;
594 if (t > tag) return -1;
602 hb_vector_t<table_entry_t> tables;
605 static hb_face_builder_data_t *
606 _hb_face_builder_data_create ()
608 hb_face_builder_data_t *data = (hb_face_builder_data_t *) calloc (1, sizeof (hb_face_builder_data_t));
609 if (unlikely (!data))
612 data->tables.init ();
618 _hb_face_builder_data_destroy (void *user_data)
620 hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
622 for (unsigned int i = 0; i < data->tables.length; i++)
623 hb_blob_destroy (data->tables[i].blob);
625 data->tables.fini ();
631 _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data)
634 unsigned int table_count = data->tables.length;
635 unsigned int face_length = table_count * 16 + 12;
637 for (unsigned int i = 0; i < table_count; i++)
638 face_length += hb_ceil_to_4 (hb_blob_get_length (data->tables[i].blob));
640 char *buf = (char *) malloc (face_length);
644 hb_serialize_context_t c (buf, face_length);
645 c.propagate_error (data->tables);
646 OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();
648 bool is_cff = data->tables.lsearch (HB_TAG ('C','F','F',' ')) || data->tables.lsearch (HB_TAG ('C','F','F','2'));
649 hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag;
651 bool ret = f->serialize_single (&c, sfnt_tag, data->tables.as_array ());
661 return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, free);
665 _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
667 hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
670 return _hb_face_builder_data_reference_blob (data);
672 hb_face_builder_data_t::table_entry_t *entry = data->tables.lsearch (tag);
674 return hb_blob_reference (entry->blob);
681 * hb_face_builder_create:
683 * Creates a #hb_face_t that can be used with hb_face_builder_add_table().
684 * After tables are added to the face, it can be compiled to a binary
685 * font file by calling hb_face_reference_blob().
687 * Return value: (transfer full): New face.
692 hb_face_builder_create ()
694 hb_face_builder_data_t *data = _hb_face_builder_data_create ();
695 if (unlikely (!data)) return hb_face_get_empty ();
697 return hb_face_create_for_tables (_hb_face_builder_reference_table,
699 _hb_face_builder_data_destroy);
703 * hb_face_builder_add_table:
705 * Add table for @tag with data provided by @blob to the face. @face must
706 * be created using hb_face_builder_create().
711 hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
713 if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))
716 hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;
717 hb_face_builder_data_t::table_entry_t *entry = data->tables.push ();
720 entry->blob = hb_blob_reference (blob);