• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hb-map.hh"
37 
38 
39 /**
40  * SECTION:hb-face
41  * @title: hb-face
42  * @short_description: Font face objects
43  * @include: hb.h
44  *
45  * A font face is an object that represents a single face from within a
46  * font family.
47  *
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.
51  **/
52 
53 
54 /**
55  * hb_face_count:
56  * @blob: a blob.
57  *
58  * Fetches the number of faces in a blob.
59  *
60  * Return value: Number of faces in @blob
61  *
62  * Since: 1.7.7
63  **/
64 unsigned int
hb_face_count(hb_blob_t * blob)65 hb_face_count (hb_blob_t *blob)
66 {
67   if (unlikely (!blob))
68     return 0;
69 
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);
76 
77   return ret;
78 }
79 
80 /*
81  * hb_face_t
82  */
83 
84 DEFINE_NULL_INSTANCE (hb_face_t) =
85 {
86   HB_OBJECT_HEADER_STATIC,
87 
88   nullptr, /* reference_table_func */
89   nullptr, /* user_data */
90   nullptr, /* destroy */
91 
92   0,    /* index */
93   1000, /* upem */
94   0,    /* num_glyphs */
95 
96   /* Zero for the rest is fine. */
97 };
98 
99 
100 /**
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
105  *
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.
110  *
111  * Creates a new face object from the specified @user_data and @reference_table_func,
112  * with the @destroy callback.
113  *
114  * Return value: (transfer full): The new face object
115  *
116  * Since: 0.9.2
117  **/
118 hb_face_t *
hb_face_create_for_tables(hb_reference_table_func_t reference_table_func,void * user_data,hb_destroy_func_t destroy)119 hb_face_create_for_tables (hb_reference_table_func_t  reference_table_func,
120 			   void                      *user_data,
121 			   hb_destroy_func_t          destroy)
122 {
123   hb_face_t *face;
124 
125   if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
126     if (destroy)
127       destroy (user_data);
128     return hb_face_get_empty ();
129   }
130 
131   face->reference_table_func = reference_table_func;
132   face->user_data = user_data;
133   face->destroy = destroy;
134 
135   face->num_glyphs.set_relaxed (-1);
136 
137   face->data.init0 (face);
138   face->table.init0 (face);
139 
140   return face;
141 }
142 
143 
144 typedef struct hb_face_for_data_closure_t {
145   hb_blob_t *blob;
146   unsigned int  index;
147 } hb_face_for_data_closure_t;
148 
149 static hb_face_for_data_closure_t *
_hb_face_for_data_closure_create(hb_blob_t * blob,unsigned int index)150 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
151 {
152   hb_face_for_data_closure_t *closure;
153 
154   closure = (hb_face_for_data_closure_t *) hb_calloc (1, sizeof (hb_face_for_data_closure_t));
155   if (unlikely (!closure))
156     return nullptr;
157 
158   closure->blob = blob;
159   closure->index = index;
160 
161   return closure;
162 }
163 
164 static void
_hb_face_for_data_closure_destroy(void * data)165 _hb_face_for_data_closure_destroy (void *data)
166 {
167   hb_face_for_data_closure_t *closure = (hb_face_for_data_closure_t *) data;
168 
169   hb_blob_destroy (closure->blob);
170   hb_free (closure);
171 }
172 
173 static hb_blob_t *
_hb_face_for_data_reference_table(hb_face_t * face HB_UNUSED,hb_tag_t tag,void * user_data)174 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
175 {
176   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
177 
178   if (tag == HB_TAG_NONE)
179     return hb_blob_reference (data->blob);
180 
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);
184 
185   const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
186 
187   hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, base_offset + table.offset, table.length);
188 
189   return blob;
190 }
191 
192 /**
193  * hb_face_create: (Xconstructor)
194  * @blob: #hb_blob_t to work upon
195  * @index: The index of the face within @blob
196  *
197  * Constructs a new face object from the specified blob and
198  * a face index into that blob. This is used for blobs of
199  * file formats such as Dfont and TTC that can contain more
200  * than one face.
201  *
202  * Return value: (transfer full): The new face object
203  *
204  * Since: 0.9.2
205  **/
206 hb_face_t *
hb_face_create(hb_blob_t * blob,unsigned int index)207 hb_face_create (hb_blob_t    *blob,
208 		unsigned int  index)
209 {
210   hb_face_t *face;
211 
212   if (unlikely (!blob))
213     blob = hb_blob_get_empty ();
214 
215   blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
216 
217   hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (blob, index);
218 
219   if (unlikely (!closure))
220   {
221     hb_blob_destroy (blob);
222     return hb_face_get_empty ();
223   }
224 
225   face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
226 				    closure,
227 				    _hb_face_for_data_closure_destroy);
228 
229   face->index = index;
230 
231   return face;
232 }
233 
234 /**
235  * hb_face_get_empty:
236  *
237  * Fetches the singleton empty face object.
238  *
239  * Return value: (transfer full): The empty face object
240  *
241  * Since: 0.9.2
242  **/
243 hb_face_t *
hb_face_get_empty()244 hb_face_get_empty ()
245 {
246   return const_cast<hb_face_t *> (&Null (hb_face_t));
247 }
248 
249 
250 /**
251  * hb_face_reference: (skip)
252  * @face: A face object
253  *
254  * Increases the reference count on a face object.
255  *
256  * Return value: The @face object
257  *
258  * Since: 0.9.2
259  **/
260 hb_face_t *
hb_face_reference(hb_face_t * face)261 hb_face_reference (hb_face_t *face)
262 {
263   return hb_object_reference (face);
264 }
265 
266 /**
267  * hb_face_destroy: (skip)
268  * @face: A face object
269  *
270  * Decreases the reference count on a face object. When the
271  * reference count reaches zero, the face is destroyed,
272  * freeing all memory.
273  *
274  * Since: 0.9.2
275  **/
276 void
hb_face_destroy(hb_face_t * face)277 hb_face_destroy (hb_face_t *face)
278 {
279   if (!hb_object_destroy (face)) return;
280 
281   for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
282   {
283     hb_face_t::plan_node_t *next = node->next;
284     hb_shape_plan_destroy (node->shape_plan);
285     hb_free (node);
286     node = next;
287   }
288 
289   face->data.fini ();
290   face->table.fini ();
291 
292   if (face->destroy)
293     face->destroy (face->user_data);
294 
295   hb_free (face);
296 }
297 
298 /**
299  * hb_face_set_user_data: (skip)
300  * @face: A face object
301  * @key: The user-data key to set
302  * @data: A pointer to the user data
303  * @destroy: (nullable): A callback to call when @data is not needed anymore
304  * @replace: Whether to replace an existing data with the same key
305  *
306  * Attaches a user-data key/data pair to the given face object.
307  *
308  * Return value: %true if success, %false otherwise
309  *
310  * Since: 0.9.2
311  **/
312 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)313 hb_face_set_user_data (hb_face_t          *face,
314 		       hb_user_data_key_t *key,
315 		       void *              data,
316 		       hb_destroy_func_t   destroy,
317 		       hb_bool_t           replace)
318 {
319   return hb_object_set_user_data (face, key, data, destroy, replace);
320 }
321 
322 /**
323  * hb_face_get_user_data: (skip)
324  * @face: A face object
325  * @key: The user-data key to query
326  *
327  * Fetches the user data associated with the specified key,
328  * attached to the specified face object.
329  *
330  * Return value: (transfer none): A pointer to the user data
331  *
332  * Since: 0.9.2
333  **/
334 void *
hb_face_get_user_data(const hb_face_t * face,hb_user_data_key_t * key)335 hb_face_get_user_data (const hb_face_t    *face,
336 		       hb_user_data_key_t *key)
337 {
338   return hb_object_get_user_data (face, key);
339 }
340 
341 /**
342  * hb_face_make_immutable:
343  * @face: A face object
344  *
345  * Makes the given face object immutable.
346  *
347  * Since: 0.9.2
348  **/
349 void
hb_face_make_immutable(hb_face_t * face)350 hb_face_make_immutable (hb_face_t *face)
351 {
352   if (hb_object_is_immutable (face))
353     return;
354 
355   hb_object_make_immutable (face);
356 }
357 
358 /**
359  * hb_face_is_immutable:
360  * @face: A face object
361  *
362  * Tests whether the given face object is immutable.
363  *
364  * Return value: %true is @face is immutable, %false otherwise
365  *
366  * Since: 0.9.2
367  **/
368 hb_bool_t
hb_face_is_immutable(const hb_face_t * face)369 hb_face_is_immutable (const hb_face_t *face)
370 {
371   return hb_object_is_immutable (face);
372 }
373 
374 
375 /**
376  * hb_face_reference_table:
377  * @face: A face object
378  * @tag: The #hb_tag_t of the table to query
379  *
380  * Fetches a reference to the specified table within
381  * the specified face.
382  *
383  * Return value: (transfer full): A pointer to the @tag table within @face
384  *
385  * Since: 0.9.2
386  **/
387 hb_blob_t *
hb_face_reference_table(const hb_face_t * face,hb_tag_t tag)388 hb_face_reference_table (const hb_face_t *face,
389 			 hb_tag_t tag)
390 {
391   if (unlikely (tag == HB_TAG_NONE))
392     return hb_blob_get_empty ();
393 
394   return face->reference_table (tag);
395 }
396 
397 /**
398  * hb_face_reference_blob:
399  * @face: A face object
400  *
401  * Fetches a pointer to the binary blob that contains the
402  * specified face. Returns an empty blob if referencing face data is not
403  * possible.
404  *
405  * Return value: (transfer full): A pointer to the blob for @face
406  *
407  * Since: 0.9.2
408  **/
409 hb_blob_t *
hb_face_reference_blob(hb_face_t * face)410 hb_face_reference_blob (hb_face_t *face)
411 {
412   return face->reference_table (HB_TAG_NONE);
413 }
414 
415 /**
416  * hb_face_set_index:
417  * @face: A face object
418  * @index: The index to assign
419  *
420  * Assigns the specified face-index to @face. Fails if the
421  * face is immutable.
422  *
423  * <note>Note: face indices within a collection are zero-based.</note>
424  *
425  * Since: 0.9.2
426  **/
427 void
hb_face_set_index(hb_face_t * face,unsigned int index)428 hb_face_set_index (hb_face_t    *face,
429 		   unsigned int  index)
430 {
431   if (hb_object_is_immutable (face))
432     return;
433 
434   face->index = index;
435 }
436 
437 /**
438  * hb_face_get_index:
439  * @face: A face object
440  *
441  * Fetches the face-index corresponding to the given face.
442  *
443  * <note>Note: face indices within a collection are zero-based.</note>
444  *
445  * Return value: The index of @face.
446  *
447  * Since: 0.9.2
448  **/
449 unsigned int
hb_face_get_index(const hb_face_t * face)450 hb_face_get_index (const hb_face_t *face)
451 {
452   return face->index;
453 }
454 
455 /**
456  * hb_face_set_upem:
457  * @face: A face object
458  * @upem: The units-per-em value to assign
459  *
460  * Sets the units-per-em (upem) for a face object to the specified value.
461  *
462  * Since: 0.9.2
463  **/
464 void
hb_face_set_upem(hb_face_t * face,unsigned int upem)465 hb_face_set_upem (hb_face_t    *face,
466 		  unsigned int  upem)
467 {
468   if (hb_object_is_immutable (face))
469     return;
470 
471   face->upem.set_relaxed (upem);
472 }
473 
474 /**
475  * hb_face_get_upem:
476  * @face: A face object
477  *
478  * Fetches the units-per-em (upem) value of the specified face object.
479  *
480  * Return value: The upem value of @face
481  *
482  * Since: 0.9.2
483  **/
484 unsigned int
hb_face_get_upem(const hb_face_t * face)485 hb_face_get_upem (const hb_face_t *face)
486 {
487   return face->get_upem ();
488 }
489 
490 /**
491  * hb_face_set_glyph_count:
492  * @face: A face object
493  * @glyph_count: The glyph-count value to assign
494  *
495  * Sets the glyph count for a face object to the specified value.
496  *
497  * Since: 0.9.7
498  **/
499 void
hb_face_set_glyph_count(hb_face_t * face,unsigned int glyph_count)500 hb_face_set_glyph_count (hb_face_t    *face,
501 			 unsigned int  glyph_count)
502 {
503   if (hb_object_is_immutable (face))
504     return;
505 
506   face->num_glyphs.set_relaxed (glyph_count);
507 }
508 
509 /**
510  * hb_face_get_glyph_count:
511  * @face: A face object
512  *
513  * Fetches the glyph-count value of the specified face object.
514  *
515  * Return value: The glyph-count value of @face
516  *
517  * Since: 0.9.7
518  **/
519 unsigned int
hb_face_get_glyph_count(const hb_face_t * face)520 hb_face_get_glyph_count (const hb_face_t *face)
521 {
522   return face->get_num_glyphs ();
523 }
524 
525 /**
526  * hb_face_get_table_tags:
527  * @face: A face object
528  * @start_offset: The index of first table tag to retrieve
529  * @table_count: (inout): Input = the maximum number of table tags to return;
530  *                Output = the actual number of table tags returned (may be zero)
531  * @table_tags: (out) (array length=table_count): The array of table tags found
532  *
533  * Fetches a list of all table tags for a face, if possible. The list returned will
534  * begin at the offset provided
535  *
536  * Return value: Total number of tables, or zero if it is not possible to list
537  *
538  * Since: 1.6.0
539  **/
540 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)541 hb_face_get_table_tags (const hb_face_t *face,
542 			unsigned int  start_offset,
543 			unsigned int *table_count, /* IN/OUT */
544 			hb_tag_t     *table_tags /* OUT */)
545 {
546   if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
547   {
548     if (table_count)
549       *table_count = 0;
550     return 0;
551   }
552 
553   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
554 
555   const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
556   const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
557 
558   return ot_face.get_table_tags (start_offset, table_count, table_tags);
559 }
560 
561 
562 /*
563  * Character set.
564  */
565 
566 
567 #ifndef HB_NO_FACE_COLLECT_UNICODES
568 /**
569  * hb_face_collect_unicodes:
570  * @face: A face object
571  * @out: The set to add Unicode characters to
572  *
573  * Collects all of the Unicode characters covered by @face and adds
574  * them to the #hb_set_t set @out.
575  *
576  * Since: 1.9.0
577  */
578 void
hb_face_collect_unicodes(hb_face_t * face,hb_set_t * out)579 hb_face_collect_unicodes (hb_face_t *face,
580 			  hb_set_t  *out)
581 {
582   face->table.cmap->collect_unicodes (out, face->get_num_glyphs ());
583 }
584 /**
585  * hb_face_collect_variation_selectors:
586  * @face: A face object
587  * @out: The set to add Variation Selector characters to
588  *
589  * Collects all Unicode "Variation Selector" characters covered by @face and adds
590  * them to the #hb_set_t set @out.
591  *
592  * Since: 1.9.0
593  */
594 void
hb_face_collect_variation_selectors(hb_face_t * face,hb_set_t * out)595 hb_face_collect_variation_selectors (hb_face_t *face,
596 				     hb_set_t  *out)
597 {
598   face->table.cmap->collect_variation_selectors (out);
599 }
600 /**
601  * hb_face_collect_variation_unicodes:
602  * @face: A face object
603  * @variation_selector: The Variation Selector to query
604  * @out: The set to add Unicode characters to
605  *
606  * Collects all Unicode characters for @variation_selector covered by @face and adds
607  * them to the #hb_set_t set @out.
608  *
609  * Since: 1.9.0
610  */
611 void
hb_face_collect_variation_unicodes(hb_face_t * face,hb_codepoint_t variation_selector,hb_set_t * out)612 hb_face_collect_variation_unicodes (hb_face_t *face,
613 				    hb_codepoint_t variation_selector,
614 				    hb_set_t  *out)
615 {
616   face->table.cmap->collect_variation_unicodes (variation_selector, out);
617 }
618 #endif
619 
620 
621 /*
622  * face-builder: A face that has add_table().
623  */
624 
625 struct hb_face_builder_data_t
626 {
627   hb_hashmap_t<hb_tag_t, hb_blob_t *> tables;
628 };
629 
compare_entries(const void * pa,const void * pb)630 static int compare_entries (const void* pa, const void* pb)
631 {
632   const auto& a = * (const hb_pair_t<hb_tag_t, hb_blob_t*> *) pa;
633   const auto& b = * (const hb_pair_t<hb_tag_t, hb_blob_t*> *) pb;
634 
635   /* Order by blob size first (smallest to largest) and then table tag */
636 
637   if (a.second->length != b.second->length)
638     return a.second->length < b.second->length ? -1 : +1;
639 
640   return a.first < b.first ? -1 : a.first == b.first ? 0 : +1;
641 }
642 
643 static hb_face_builder_data_t *
_hb_face_builder_data_create()644 _hb_face_builder_data_create ()
645 {
646   hb_face_builder_data_t *data = (hb_face_builder_data_t *) hb_calloc (1, sizeof (hb_face_builder_data_t));
647   if (unlikely (!data))
648     return nullptr;
649 
650   data->tables.init ();
651 
652   return data;
653 }
654 
655 static void
_hb_face_builder_data_destroy(void * user_data)656 _hb_face_builder_data_destroy (void *user_data)
657 {
658   hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
659 
660   for (hb_blob_t* b : data->tables.values())
661     hb_blob_destroy (b);
662 
663   data->tables.fini ();
664 
665   hb_free (data);
666 }
667 
668 static hb_blob_t *
_hb_face_builder_data_reference_blob(hb_face_builder_data_t * data)669 _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data)
670 {
671 
672   unsigned int table_count = data->tables.get_population ();
673   unsigned int face_length = table_count * 16 + 12;
674 
675   for (hb_blob_t* b : data->tables.values())
676     face_length += hb_ceil_to_4 (hb_blob_get_length (b));
677 
678   char *buf = (char *) hb_malloc (face_length);
679   if (unlikely (!buf))
680     return nullptr;
681 
682   hb_serialize_context_t c (buf, face_length);
683   c.propagate_error (data->tables);
684   OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();
685 
686   bool is_cff = (data->tables.has (HB_TAG ('C','F','F',' '))
687                  || data->tables.has (HB_TAG ('C','F','F','2')));
688   hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag;
689 
690   // Sort the tags so that produced face is deterministic.
691   hb_vector_t<hb_pair_t <hb_tag_t, hb_blob_t*>> sorted_entries;
692   data->tables.iter () | hb_sink (sorted_entries);
693   if (unlikely (sorted_entries.in_error ()))
694   {
695     hb_free (buf);
696     return nullptr;
697   }
698 
699   sorted_entries.qsort (compare_entries);
700   bool ret = f->serialize_single (&c, sfnt_tag, + sorted_entries.iter());
701 
702   c.end_serialize ();
703 
704   if (unlikely (!ret))
705   {
706     hb_free (buf);
707     return nullptr;
708   }
709 
710   return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, hb_free);
711 }
712 
713 static hb_blob_t *
_hb_face_builder_reference_table(hb_face_t * face HB_UNUSED,hb_tag_t tag,void * user_data)714 _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
715 {
716   hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
717 
718   if (!tag)
719     return _hb_face_builder_data_reference_blob (data);
720 
721   return hb_blob_reference (data->tables[tag]);
722 }
723 
724 
725 /**
726  * hb_face_builder_create:
727  *
728  * Creates a #hb_face_t that can be used with hb_face_builder_add_table().
729  * After tables are added to the face, it can be compiled to a binary
730  * font file by calling hb_face_reference_blob().
731  *
732  * Return value: (transfer full): New face.
733  *
734  * Since: 1.9.0
735  **/
736 hb_face_t *
hb_face_builder_create()737 hb_face_builder_create ()
738 {
739   hb_face_builder_data_t *data = _hb_face_builder_data_create ();
740   if (unlikely (!data)) return hb_face_get_empty ();
741 
742   return hb_face_create_for_tables (_hb_face_builder_reference_table,
743 				    data,
744 				    _hb_face_builder_data_destroy);
745 }
746 
747 /**
748  * hb_face_builder_add_table:
749  * @face: A face object created with hb_face_builder_create()
750  * @tag: The #hb_tag_t of the table to add
751  * @blob: The blob containing the table data to add
752  *
753  * Add table for @tag with data provided by @blob to the face.  @face must
754  * be created using hb_face_builder_create().
755  *
756  * Since: 1.9.0
757  **/
758 hb_bool_t
hb_face_builder_add_table(hb_face_t * face,hb_tag_t tag,hb_blob_t * blob)759 hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
760 {
761   if (tag == HB_MAP_VALUE_INVALID)
762     return false;
763 
764   if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))
765     return false;
766 
767   hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;
768 
769   hb_blob_t* previous = data->tables.get (tag);
770   if (!data->tables.set (tag, hb_blob_reference (blob)))
771   {
772     hb_blob_destroy (blob);
773     return false;
774   }
775 
776   hb_blob_destroy (previous);
777   return true;
778 }
779