jdk
246 строк · 7.0 Кб
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
33#include "hb-map.hh"34#include "hb-open-file.hh"35#include "hb-serialize.hh"36
37
38/*
39* face-builder: A face that has add_table().
40*/
41
42struct face_table_info_t43{
44hb_blob_t* data;45signed order;46};47
48struct hb_face_builder_data_t49{
50hb_hashmap_t<hb_tag_t, face_table_info_t> tables;51};52
53static int compare_entries (const void* pa, const void* pb)54{
55const auto& a = * (const hb_pair_t<hb_tag_t, face_table_info_t> *) pa;56const auto& b = * (const hb_pair_t<hb_tag_t, face_table_info_t> *) pb;57
58/* Order by blob size first (smallest to largest) and then table tag */59
60if (a.second.order != b.second.order)61return a.second.order < b.second.order ? -1 : +1;62
63if (a.second.data->length != b.second.data->length)64return a.second.data->length < b.second.data->length ? -1 : +1;65
66return a.first < b.first ? -1 : a.first == b.first ? 0 : +1;67}
68
69static hb_face_builder_data_t *70_hb_face_builder_data_create ()71{
72hb_face_builder_data_t *data = (hb_face_builder_data_t *) hb_calloc (1, sizeof (hb_face_builder_data_t));73if (unlikely (!data))74return nullptr;75
76data->tables.init ();77
78return data;79}
80
81static void82_hb_face_builder_data_destroy (void *user_data)83{
84hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;85
86for (auto info : data->tables.values())87hb_blob_destroy (info.data);88
89data->tables.fini ();90
91hb_free (data);92}
93
94static hb_blob_t *95_hb_face_builder_data_reference_blob (hb_face_builder_data_t *data)96{
97
98unsigned int table_count = data->tables.get_population ();99unsigned int face_length = table_count * 16 + 12;100
101for (auto info : data->tables.values())102face_length += hb_ceil_to_4 (hb_blob_get_length (info.data));103
104char *buf = (char *) hb_malloc (face_length);105if (unlikely (!buf))106return nullptr;107
108hb_serialize_context_t c (buf, face_length);109c.propagate_error (data->tables);110OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> ();111
112bool is_cff = (data->tables.has (HB_TAG ('C','F','F',' '))113|| data->tables.has (HB_TAG ('C','F','F','2')));114hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag;115
116// Sort the tags so that produced face is deterministic.117hb_vector_t<hb_pair_t <hb_tag_t, face_table_info_t>> sorted_entries;118data->tables.iter () | hb_sink (sorted_entries);119if (unlikely (sorted_entries.in_error ()))120{121hb_free (buf);122return nullptr;123}124
125sorted_entries.qsort (compare_entries);126
127bool ret = f->serialize_single (&c,128sfnt_tag,129+ sorted_entries.iter()130| hb_map ([&] (hb_pair_t<hb_tag_t, face_table_info_t> _) {131return hb_pair_t<hb_tag_t, hb_blob_t*> (_.first, _.second.data);132}));133
134c.end_serialize ();135
136if (unlikely (!ret))137{138hb_free (buf);139return nullptr;140}141
142return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, hb_free);143}
144
145static hb_blob_t *146_hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)147{
148hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data;149
150if (!tag)151return _hb_face_builder_data_reference_blob (data);152
153return hb_blob_reference (data->tables[tag].data);154}
155
156
157/**
158* hb_face_builder_create:
159*
160* Creates a #hb_face_t that can be used with hb_face_builder_add_table().
161* After tables are added to the face, it can be compiled to a binary
162* font file by calling hb_face_reference_blob().
163*
164* Return value: (transfer full): New face.
165*
166* Since: 1.9.0
167**/
168hb_face_t *169hb_face_builder_create ()170{
171hb_face_builder_data_t *data = _hb_face_builder_data_create ();172if (unlikely (!data)) return hb_face_get_empty ();173
174return hb_face_create_for_tables (_hb_face_builder_reference_table,175data,176_hb_face_builder_data_destroy);177}
178
179/**
180* hb_face_builder_add_table:
181* @face: A face object created with hb_face_builder_create()
182* @tag: The #hb_tag_t of the table to add
183* @blob: The blob containing the table data to add
184*
185* Add table for @tag with data provided by @blob to the face. @face must
186* be created using hb_face_builder_create().
187*
188* Since: 1.9.0
189**/
190hb_bool_t
191hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob)192{
193if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))194return false;195
196if (tag == HB_MAP_VALUE_INVALID)197return false;198
199hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;200
201hb_blob_t* previous = data->tables.get (tag).data;202if (!data->tables.set (tag, face_table_info_t {hb_blob_reference (blob), -1}))203{204hb_blob_destroy (blob);205return false;206}207
208hb_blob_destroy (previous);209return true;210}
211
212/**
213* hb_face_builder_sort_tables:
214* @face: A face object created with hb_face_builder_create()
215* @tags: (array zero-terminated=1): ordered list of table tags terminated by
216* %HB_TAG_NONE
217*
218* Set the ordering of tables for serialization. Any tables not
219* specified in the tags list will be ordered after the tables in
220* tags, ordered by the default sort ordering.
221*
222* Since: 5.3.0
223**/
224void
225hb_face_builder_sort_tables (hb_face_t *face,226const hb_tag_t *tags)227{
228if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy))229return;230
231hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data;232
233// Sort all unspecified tables after any specified tables.234for (auto& info : data->tables.values_ref())235info.order = (unsigned) -1;236
237signed order = 0;238for (const hb_tag_t* tag = tags;239*tag;240tag++)241{242face_table_info_t* info;243if (!data->tables.has (*tag, &info)) continue;244info->order = order++;245}246}
247