hb-subset.cc 9.3 KB
Newer Older
1
/*
B
Minor  
Behdad Esfahbod 已提交
2
 * Copyright © 2018  Google, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
R
Rod Sheeter 已提交
24
 * Google Author(s): Garret Rieger, Rod Sheeter, Behdad Esfahbod
25 26
 */

27 28
#include "hb.hh"
#include "hb-open-type.hh"
29

30
#include "hb-subset.hh"
31

32
#include "hb-open-file.hh"
33
#include "hb-ot-cmap-table.hh"
34
#include "hb-ot-glyf-table.hh"
35
#include "hb-ot-hdmx-table.hh"
36
#include "hb-ot-head-table.hh"
R
Rod Sheeter 已提交
37 38
#include "hb-ot-hhea-table.hh"
#include "hb-ot-hmtx-table.hh"
39
#include "hb-ot-maxp-table.hh"
40
#include "hb-ot-os2-table.hh"
41
#include "hb-ot-post-table.hh"
42
#include "hb-ot-cff1-table.hh"
M
Michiharu Ariza 已提交
43
#include "hb-ot-cff2-table.hh"
44
#include "hb-ot-vorg-table.hh"
45
#include "hb-ot-name-table.hh"
46 47
#include "hb-ot-layout-gsub-table.hh"
#include "hb-ot-layout-gpos-table.hh"
48

49

50
HB_UNUSED static inline unsigned int
51
_plan_estimate_subset_table_size (hb_subset_plan_t *plan,
52 53 54 55
				  unsigned int table_len);
static inline unsigned int
_plan_estimate_subset_table_size (hb_subset_plan_t *plan,
				  unsigned int table_len)
56
{
57
  unsigned int src_glyphs = plan->source->get_num_glyphs ();
58
  unsigned int dst_glyphs = plan->glyphset ()->get_population ();
59

B
Behdad Esfahbod 已提交
60 61 62
  if (unlikely (!src_glyphs))
    return 512 + table_len;

63
  return 512 + (unsigned int) (table_len * sqrt ((double) dst_glyphs / src_glyphs));
64 65
}

66 67 68
template<typename TableType>
static bool
_subset2 (hb_subset_plan_t *plan)
69
{
70
  bool result = false;
71 72
  hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
  const TableType *table = source_blob->as<TableType> ();
73

74 75 76
  hb_tag_t tag = TableType::tableTag;
  if (source_blob->data)
  {
77
    hb_vector_t<char> buf;
B
Behdad Esfahbod 已提交
78 79
    /* TODO Not all tables are glyph-related.  'name' table size for example should not be
     * affected by number of glyphs.  Accommodate that. */
80
    unsigned int buf_size = _plan_estimate_subset_table_size (plan, source_blob->length);
E
Ebrahim Byagowi 已提交
81
    DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c initial estimated table size: %u bytes.", HB_UNTAG (tag), buf_size);
82 83
    if (unlikely (!buf.alloc (buf_size)))
    {
E
Ebrahim Byagowi 已提交
84
      DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c failed to allocate %u bytes.", HB_UNTAG (tag), buf_size);
85 86 87
      return false;
    }
  retry:
88
    hb_serialize_context_t serializer ((void *) buf, buf_size);
89
    serializer.start_serialize<TableType> ();
90
    hb_subset_context_t c (plan, &serializer);
91
    bool needed = table->subset (&c);
92
    if (serializer.ran_out_of_room)
93 94
    {
      buf_size += (buf_size >> 1) + 32;
E
Ebrahim Byagowi 已提交
95
      DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c ran out of room; reallocating to %u bytes.", HB_UNTAG (tag), buf_size);
96 97
      if (unlikely (!buf.alloc (buf_size)))
      {
E
Ebrahim Byagowi 已提交
98
	DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c failed to reallocate %u bytes.", HB_UNTAG (tag), buf_size);
99 100 101 102
	return false;
      }
      goto retry;
    }
103 104
    serializer.end_serialize ();

105
    result = !serializer.in_error ();
106

107 108
    if (result)
    {
109 110 111 112 113 114 115 116 117 118 119
      if (needed)
      {
	hb_blob_t *dest_blob = serializer.copy_blob ();
	DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c final subset table size: %u bytes.", HB_UNTAG (tag), dest_blob->length);
	result = c.plan->add_table (tag, dest_blob);
	hb_blob_destroy (dest_blob);
      }
      else
      {
	DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset table subsetted to empty.", HB_UNTAG (tag));
      }
120 121 122
    }
  }
  else
E
Ebrahim Byagowi 已提交
123
    DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
124 125

  hb_blob_destroy (source_blob);
E
Ebrahim Byagowi 已提交
126
  DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
127
  return result;
128 129
}

130
template<typename TableType>
131
static bool
R
Rod Sheeter 已提交
132
_subset (hb_subset_plan_t *plan)
133
{
134
  hb_blob_t *source_blob = hb_sanitize_context_t ().reference_table<TableType> (plan->source);
135
  const TableType *table = source_blob->as<TableType> ();
R
Rod Sheeter 已提交
136

137
  hb_tag_t tag = TableType::tableTag;
138
  hb_bool_t result = false;
139
  if (source_blob->data)
140 141
    result = table->subset (plan);
  else
E
Ebrahim Byagowi 已提交
142
    DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset sanitize failed on source table.", HB_UNTAG (tag));
143 144

  hb_blob_destroy (source_blob);
E
Ebrahim Byagowi 已提交
145
  DEBUG_MSG(SUBSET, nullptr, "OT::%c%c%c%c::subset %s", HB_UNTAG (tag), result ? "success" : "FAILED!");
146
  return result;
147
}
148

B
Behdad Esfahbod 已提交
149

B
Behdad Esfahbod 已提交
150
static bool
151
_subset_table (hb_subset_plan_t *plan,
E
Ebrahim Byagowi 已提交
152
	       hb_tag_t          tag)
153
{
E
Ebrahim Byagowi 已提交
154
  DEBUG_MSG(SUBSET, nullptr, "begin subset %c%c%c%c", HB_UNTAG (tag));
R
Rod Sheeter 已提交
155
  bool result = true;
156 157
  switch (tag) {
    case HB_OT_TAG_glyf:
158
      result = _subset2<const OT::glyf> (plan);
R
Rod Sheeter 已提交
159
      break;
G
Garret Rieger 已提交
160
    case HB_OT_TAG_hdmx:
G
Garret Rieger 已提交
161
      result = _subset2<const OT::hdmx> (plan);
G
Garret Rieger 已提交
162
      break;
163
    case HB_OT_TAG_name:
164
      result = _subset2<const OT::name> (plan);
165
      break;
166
    case HB_OT_TAG_head:
167 168
      // TODO that won't work well if there is no glyf
      DEBUG_MSG(SUBSET, nullptr, "skip head, handled by glyf");
R
Rod Sheeter 已提交
169 170 171
      result = true;
      break;
    case HB_OT_TAG_hhea:
172
      DEBUG_MSG(SUBSET, nullptr, "skip hhea handled by hmtx");
173
      return true;
R
Rod Sheeter 已提交
174
    case HB_OT_TAG_hmtx:
175
      result = _subset2<const OT::hmtx> (plan);
R
Rod Sheeter 已提交
176
      break;
G
Garret Rieger 已提交
177
    case HB_OT_TAG_vhea:
G
Garret Rieger 已提交
178
      DEBUG_MSG(SUBSET, nullptr, "skip vhea handled by vmtx");
G
Garret Rieger 已提交
179
      return true;
G
Garret Rieger 已提交
180
    case HB_OT_TAG_vmtx:
181
      result = _subset2<const OT::vmtx> (plan);
G
Garret Rieger 已提交
182
      break;
183
    case HB_OT_TAG_maxp:
Q
Qunxin Liu 已提交
184
      result = _subset2<const OT::maxp> (plan);
185
      break;
186
    case HB_OT_TAG_loca:
187
      DEBUG_MSG(SUBSET, nullptr, "skip loca handled by glyf");
188 189
      return true;
    case HB_OT_TAG_cmap:
190
      result = _subset2<const OT::cmap> (plan);
R
Rod Sheeter 已提交
191
      break;
192
    case HB_OT_TAG_OS2:
G
Garret Rieger 已提交
193
      result = _subset2<const OT::OS2> (plan);
194
      break;
195
    case HB_OT_TAG_post:
Q
Qunxin Liu 已提交
196
      result = _subset2<const OT::post> (plan);
197
      break;
B
Behdad Esfahbod 已提交
198 199

#ifndef HB_NO_SUBSET_CFF
200 201
    case HB_OT_TAG_cff1:
      result = _subset<const OT::cff1> (plan);
M
Michiharu Ariza 已提交
202
      break;
M
Michiharu Ariza 已提交
203 204 205
    case HB_OT_TAG_cff2:
      result = _subset<const OT::cff2> (plan);
      break;
206
    case HB_OT_TAG_VORG:
207
      result = _subset2<const OT::VORG> (plan);
208
      break;
B
Behdad Esfahbod 已提交
209
#endif
B
Behdad Esfahbod 已提交
210

B
Minor  
Behdad Esfahbod 已提交
211
#ifndef HB_NO_SUBSET_LAYOUT
B
Behdad Esfahbod 已提交
212 213 214
    case HB_OT_TAG_GDEF:
      result = _subset2<const OT::GDEF> (plan);
      break;
215 216 217 218 219 220
    case HB_OT_TAG_GSUB:
      result = _subset2<const OT::GSUB> (plan);
      break;
    case HB_OT_TAG_GPOS:
      result = _subset2<const OT::GPOS> (plan);
      break;
B
Behdad Esfahbod 已提交
221
#endif
222

223
    default:
E
Ebrahim Byagowi 已提交
224
      hb_blob_t *source_table = hb_face_reference_table (plan->source, tag);
225
      if (likely (source_table))
E
Ebrahim Byagowi 已提交
226
	result = plan->add_table (tag, source_table);
R
Rod Sheeter 已提交
227
      else
E
Ebrahim Byagowi 已提交
228
	result = false;
229
      hb_blob_destroy (source_table);
R
Rod Sheeter 已提交
230
      break;
231
  }
E
Ebrahim Byagowi 已提交
232
  DEBUG_MSG(SUBSET, nullptr, "subset %c%c%c%c %s", HB_UNTAG (tag), result ? "ok" : "FAILED");
233
  return result;
234 235
}

R
Rod Sheeter 已提交
236
static bool
E
Ebrahim Byagowi 已提交
237
_should_drop_table (hb_subset_plan_t *plan, hb_tag_t tag)
R
Rod Sheeter 已提交
238
{
239 240 241
  if (plan->drop_tables->has (tag))
    return true;

242 243 244 245 246 247 248 249
  switch (tag) {
    case HB_TAG ('c', 'v', 'a', 'r'): /* hint table, fallthrough */
    case HB_TAG ('c', 'v', 't', ' '): /* hint table, fallthrough */
    case HB_TAG ('f', 'p', 'g', 'm'): /* hint table, fallthrough */
    case HB_TAG ('p', 'r', 'e', 'p'): /* hint table, fallthrough */
    case HB_TAG ('h', 'd', 'm', 'x'): /* hint table, fallthrough */
    case HB_TAG ('V', 'D', 'M', 'X'): /* hint table, fallthrough */
      return plan->drop_hints;
B
Behdad Esfahbod 已提交
250

251
#ifdef HB_NO_SUBSET_LAYOUT
252
    // Drop Layout Tables if requested.
B
Behdad Esfahbod 已提交
253 254 255
    case HB_OT_TAG_GDEF:
    case HB_OT_TAG_GPOS:
    case HB_OT_TAG_GSUB:
256 257 258 259
    case HB_TAG ('m', 'o', 'r', 'x'):
    case HB_TAG ('m', 'o', 'r', 't'):
    case HB_TAG ('k', 'e', 'r', 'x'):
    case HB_TAG ('k', 'e', 'r', 'n'):
B
Behdad Esfahbod 已提交
260 261 262
      return true;
#endif

263 264
    default:
      return false;
265
  }
R
Rod Sheeter 已提交
266 267
}

268 269
/**
 * hb_subset:
270
 * @source: font face data to be subset.
271 272
 * @input: input to use for the subsetting.
 *
273
 * Subsets a font according to provided input.
274
 **/
275 276
hb_face_t *
hb_subset (hb_face_t *source,
E
Ebrahim Byagowi 已提交
277
	   hb_subset_input_t *input)
278
{
E
Ebrahim Byagowi 已提交
279
  if (unlikely (!input || !source)) return hb_face_get_empty ();
280

281
  hb_subset_plan_t *plan = hb_subset_plan_create (source, input);
282

283 284
  hb_tag_t table_tags[32];
  unsigned int offset = 0, count;
R
Rod Sheeter 已提交
285
  bool success = true;
286 287 288 289 290 291
  do {
    count = ARRAY_LENGTH (table_tags);
    hb_face_get_table_tags (source, offset, &count, table_tags);
    for (unsigned int i = 0; i < count; i++)
    {
      hb_tag_t tag = table_tags[i];
E
Ebrahim Byagowi 已提交
292
      if (_should_drop_table (plan, tag))
R
Rod Sheeter 已提交
293
      {
E
Ebrahim Byagowi 已提交
294 295
	DEBUG_MSG(SUBSET, nullptr, "drop %c%c%c%c", HB_UNTAG (tag));
	continue;
R
Rod Sheeter 已提交
296
      }
R
Rod Sheeter 已提交
297
      success = success && _subset_table (plan, tag);
298
    }
299
    offset += count;
300
  } while (success && count == ARRAY_LENGTH (table_tags));
R
Rod Sheeter 已提交
301

E
Ebrahim Byagowi 已提交
302
  hb_face_t *result = success ? hb_face_reference (plan->dest) : hb_face_get_empty ();
303
  hb_subset_plan_destroy (plan);
R
Rod Sheeter 已提交
304
  return result;
305
}