Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / src / hb-face.cc
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
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 *
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   uint16_t  index;
147 } hb_face_for_data_closure_t;
148
149 static hb_face_for_data_closure_t *
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 = (uint16_t) (index & 0xFFFFu);
160
161   return closure;
162 }
163
164 static void
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 *
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.
199  *
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.
203  *
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>
207  *
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>
211  *
212  * Return value: (transfer full): The new face object
213  *
214  * Since: 0.9.2
215  **/
216 hb_face_t *
217 hb_face_create (hb_blob_t    *blob,
218                 unsigned int  index)
219 {
220   hb_face_t *face;
221
222   if (unlikely (!blob))
223     blob = hb_blob_get_empty ();
224
225   blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (hb_blob_reference (blob));
226
227   hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (blob, index);
228
229   if (unlikely (!closure))
230   {
231     hb_blob_destroy (blob);
232     return hb_face_get_empty ();
233   }
234
235   face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
236                                     closure,
237                                     _hb_face_for_data_closure_destroy);
238
239   face->index = index;
240
241   return face;
242 }
243
244 /**
245  * hb_face_get_empty:
246  *
247  * Fetches the singleton empty face object.
248  *
249  * Return value: (transfer full): The empty face object
250  *
251  * Since: 0.9.2
252  **/
253 hb_face_t *
254 hb_face_get_empty ()
255 {
256   return const_cast<hb_face_t *> (&Null (hb_face_t));
257 }
258
259
260 /**
261  * hb_face_reference: (skip)
262  * @face: A face object
263  *
264  * Increases the reference count on a face object.
265  *
266  * Return value: The @face object
267  *
268  * Since: 0.9.2
269  **/
270 hb_face_t *
271 hb_face_reference (hb_face_t *face)
272 {
273   return hb_object_reference (face);
274 }
275
276 /**
277  * hb_face_destroy: (skip)
278  * @face: A face object
279  *
280  * Decreases the reference count on a face object. When the
281  * reference count reaches zero, the face is destroyed,
282  * freeing all memory.
283  *
284  * Since: 0.9.2
285  **/
286 void
287 hb_face_destroy (hb_face_t *face)
288 {
289   if (!hb_object_destroy (face)) return;
290
291   for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
292   {
293     hb_face_t::plan_node_t *next = node->next;
294     hb_shape_plan_destroy (node->shape_plan);
295     hb_free (node);
296     node = next;
297   }
298
299   face->data.fini ();
300   face->table.fini ();
301
302   if (face->destroy)
303     face->destroy (face->user_data);
304
305   hb_free (face);
306 }
307
308 /**
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
315  *
316  * Attaches a user-data key/data pair to the given face object.
317  *
318  * Return value: %true if success, %false otherwise
319  *
320  * Since: 0.9.2
321  **/
322 hb_bool_t
323 hb_face_set_user_data (hb_face_t          *face,
324                        hb_user_data_key_t *key,
325                        void *              data,
326                        hb_destroy_func_t   destroy,
327                        hb_bool_t           replace)
328 {
329   return hb_object_set_user_data (face, key, data, destroy, replace);
330 }
331
332 /**
333  * hb_face_get_user_data: (skip)
334  * @face: A face object
335  * @key: The user-data key to query
336  *
337  * Fetches the user data associated with the specified key,
338  * attached to the specified face object.
339  *
340  * Return value: (transfer none): A pointer to the user data
341  *
342  * Since: 0.9.2
343  **/
344 void *
345 hb_face_get_user_data (const hb_face_t    *face,
346                        hb_user_data_key_t *key)
347 {
348   return hb_object_get_user_data (face, key);
349 }
350
351 /**
352  * hb_face_make_immutable:
353  * @face: A face object
354  *
355  * Makes the given face object immutable.
356  *
357  * Since: 0.9.2
358  **/
359 void
360 hb_face_make_immutable (hb_face_t *face)
361 {
362   if (hb_object_is_immutable (face))
363     return;
364
365   hb_object_make_immutable (face);
366 }
367
368 /**
369  * hb_face_is_immutable:
370  * @face: A face object
371  *
372  * Tests whether the given face object is immutable.
373  *
374  * Return value: %true is @face is immutable, %false otherwise
375  *
376  * Since: 0.9.2
377  **/
378 hb_bool_t
379 hb_face_is_immutable (const hb_face_t *face)
380 {
381   return hb_object_is_immutable (face);
382 }
383
384
385 /**
386  * hb_face_reference_table:
387  * @face: A face object
388  * @tag: The #hb_tag_t of the table to query
389  *
390  * Fetches a reference to the specified table within
391  * the specified face.
392  *
393  * Return value: (transfer full): A pointer to the @tag table within @face
394  *
395  * Since: 0.9.2
396  **/
397 hb_blob_t *
398 hb_face_reference_table (const hb_face_t *face,
399                          hb_tag_t tag)
400 {
401   if (unlikely (tag == HB_TAG_NONE))
402     return hb_blob_get_empty ();
403
404   return face->reference_table (tag);
405 }
406
407 /**
408  * hb_face_reference_blob:
409  * @face: A face object
410  *
411  * Fetches a pointer to the binary blob that contains the
412  * specified face. Returns an empty blob if referencing face data is not
413  * possible.
414  *
415  * Return value: (transfer full): A pointer to the blob for @face
416  *
417  * Since: 0.9.2
418  **/
419 hb_blob_t *
420 hb_face_reference_blob (hb_face_t *face)
421 {
422   return face->reference_table (HB_TAG_NONE);
423 }
424
425 /**
426  * hb_face_set_index:
427  * @face: A face object
428  * @index: The index to assign
429  *
430  * Assigns the specified face-index to @face. Fails if the
431  * face is immutable.
432  *
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>
435  *
436  * Since: 0.9.2
437  **/
438 void
439 hb_face_set_index (hb_face_t    *face,
440                    unsigned int  index)
441 {
442   if (hb_object_is_immutable (face))
443     return;
444
445   face->index = index;
446 }
447
448 /**
449  * hb_face_get_index:
450  * @face: A face object
451  *
452  * Fetches the face-index corresponding to the given face.
453  *
454  * <note>Note: face indices within a collection are zero-based.</note>
455  *
456  * Return value: The index of @face.
457  *
458  * Since: 0.9.2
459  **/
460 unsigned int
461 hb_face_get_index (const hb_face_t *face)
462 {
463   return face->index;
464 }
465
466 /**
467  * hb_face_set_upem:
468  * @face: A face object
469  * @upem: The units-per-em value to assign
470  *
471  * Sets the units-per-em (upem) for a face object to the specified value.
472  *
473  * Since: 0.9.2
474  **/
475 void
476 hb_face_set_upem (hb_face_t    *face,
477                   unsigned int  upem)
478 {
479   if (hb_object_is_immutable (face))
480     return;
481
482   face->upem.set_relaxed (upem);
483 }
484
485 /**
486  * hb_face_get_upem:
487  * @face: A face object
488  *
489  * Fetches the units-per-em (upem) value of the specified face object.
490  *
491  * Return value: The upem value of @face
492  *
493  * Since: 0.9.2
494  **/
495 unsigned int
496 hb_face_get_upem (const hb_face_t *face)
497 {
498   return face->get_upem ();
499 }
500
501 /**
502  * hb_face_set_glyph_count:
503  * @face: A face object
504  * @glyph_count: The glyph-count value to assign
505  *
506  * Sets the glyph count for a face object to the specified value.
507  *
508  * Since: 0.9.7
509  **/
510 void
511 hb_face_set_glyph_count (hb_face_t    *face,
512                          unsigned int  glyph_count)
513 {
514   if (hb_object_is_immutable (face))
515     return;
516
517   face->num_glyphs.set_relaxed (glyph_count);
518 }
519
520 /**
521  * hb_face_get_glyph_count:
522  * @face: A face object
523  *
524  * Fetches the glyph-count value of the specified face object.
525  *
526  * Return value: The glyph-count value of @face
527  *
528  * Since: 0.9.7
529  **/
530 unsigned int
531 hb_face_get_glyph_count (const hb_face_t *face)
532 {
533   return face->get_num_glyphs ();
534 }
535
536 /**
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
543  *
544  * Fetches a list of all table tags for a face, if possible. The list returned will
545  * begin at the offset provided
546  *
547  * Return value: Total number of tables, or zero if it is not possible to list
548  *
549  * Since: 1.6.0
550  **/
551 unsigned int
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 */)
556 {
557   if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
558   {
559     if (table_count)
560       *table_count = 0;
561     return 0;
562   }
563
564   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
565
566   const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
567   const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
568
569   return ot_face.get_table_tags (start_offset, table_count, table_tags);
570 }
571
572
573 /*
574  * Character set.
575  */
576
577
578 #ifndef HB_NO_FACE_COLLECT_UNICODES
579 /**
580  * hb_face_collect_unicodes:
581  * @face: A face object
582  * @out: The set to add Unicode characters to
583  *
584  * Collects all of the Unicode characters covered by @face and adds
585  * them to the #hb_set_t set @out.
586  *
587  * Since: 1.9.0
588  */
589 void
590 hb_face_collect_unicodes (hb_face_t *face,
591                           hb_set_t  *out)
592 {
593   face->table.cmap->collect_unicodes (out, face->get_num_glyphs ());
594 }
595 /**
596  * hb_face_collect_variation_selectors:
597  * @face: A face object
598  * @out: The set to add Variation Selector characters to
599  *
600  * Collects all Unicode "Variation Selector" characters covered by @face and adds
601  * them to the #hb_set_t set @out.
602  *
603  * Since: 1.9.0
604  */
605 void
606 hb_face_collect_variation_selectors (hb_face_t *face,
607                                      hb_set_t  *out)
608 {
609   face->table.cmap->collect_variation_selectors (out);
610 }
611 /**
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
616  *
617  * Collects all Unicode characters for @variation_selector covered by @face and adds
618  * them to the #hb_set_t set @out.
619  *
620  * Since: 1.9.0
621  */
622 void
623 hb_face_collect_variation_unicodes (hb_face_t *face,
624                                     hb_codepoint_t variation_selector,
625                                     hb_set_t  *out)
626 {
627   face->table.cmap->collect_variation_unicodes (variation_selector, out);
628 }
629 #endif
630
631
632 /*
633  * face-builder: A face that has add_table().
634  */
635
636 struct hb_face_builder_data_t
637 {
638   hb_hashmap_t<hb_tag_t, hb_blob_t *> tables;
639 };
640
641 static int compare_entries (const void* pa, const void* pb)
642 {
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;
645
646   /* Order by blob size first (smallest to largest) and then table tag */
647
648   if (a.second->length != b.second->length)
649     return a.second->length < b.second->length ? -1 : +1;
650
651   return a.first < b.first ? -1 : a.first == b.first ? 0 : +1;
652 }
653
654 static hb_face_builder_data_t *
655 _hb_face_builder_data_create ()
656 {
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))
659     return nullptr;
660
661   data->tables.init ();
662
663   return data;
664 }
665
666 static void
667 _hb_face_builder_data_destroy (void *user_data)
668 {
669   hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
670
671   for (hb_blob_t* b : data->tables.values())
672     hb_blob_destroy (b);
673
674   data->tables.fini ();
675
676   hb_free (data);
677 }
678
679 static hb_blob_t *
680 _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data)
681 {
682
683   unsigned int table_count = data->tables.get_population ();
684   unsigned int face_length = table_count * 16 + 12;
685
686   for (hb_blob_t* b : data->tables.values())
687     face_length += hb_ceil_to_4 (hb_blob_get_length (b));
688
689   char *buf = (char *) hb_malloc (face_length);
690   if (unlikely (!buf))
691     return nullptr;
692
693   hb_serialize_context_t c (buf, face_length);
694   c.propagate_error (data->tables);
695   OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();
696
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;
700
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 ()))
705   {
706     hb_free (buf);
707     return nullptr;
708   }
709
710   sorted_entries.qsort (compare_entries);
711   bool ret = f->serialize_single (&c, sfnt_tag, + sorted_entries.iter());
712
713   c.end_serialize ();
714
715   if (unlikely (!ret))
716   {
717     hb_free (buf);
718     return nullptr;
719   }
720
721   return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, hb_free);
722 }
723
724 static hb_blob_t *
725 _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
726 {
727   hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;
728
729   if (!tag)
730     return _hb_face_builder_data_reference_blob (data);
731
732   return hb_blob_reference (data->tables[tag]);
733 }
734
735
736 /**
737  * hb_face_builder_create:
738  *
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().
742  *
743  * Return value: (transfer full): New face.
744  *
745  * Since: 1.9.0
746  **/
747 hb_face_t *
748 hb_face_builder_create ()
749 {
750   hb_face_builder_data_t *data = _hb_face_builder_data_create ();
751   if (unlikely (!data)) return hb_face_get_empty ();
752
753   return hb_face_create_for_tables (_hb_face_builder_reference_table,
754                                     data,
755                                     _hb_face_builder_data_destroy);
756 }
757
758 /**
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
763  *
764  * Add table for @tag with data provided by @blob to the face.  @face must
765  * be created using hb_face_builder_create().
766  *
767  * Since: 1.9.0
768  **/
769 hb_bool_t
770 hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)
771 {
772   if (tag == HB_MAP_VALUE_INVALID)
773     return false;
774
775   if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))
776     return false;
777
778   hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;
779
780   hb_blob_t* previous = data->tables.get (tag);
781   if (!data->tables.set (tag, hb_blob_reference (blob)))
782   {
783     hb_blob_destroy (blob);
784     return false;
785   }
786
787   hb_blob_destroy (previous);
788   return true;
789 }