hb-ot-name-table.hh 9.5 KB
Newer Older
1
/*
2
 * Copyright © 2011,2012  Google, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  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.
 *
 * Google Author(s): Behdad Esfahbod
 */

27 28
#ifndef HB_OT_NAME_TABLE_HH
#define HB_OT_NAME_TABLE_HH
29

30
#include "hb-open-type.hh"
B
Behdad Esfahbod 已提交
31
#include "hb-ot-name-language.hh"
B
Behdad Esfahbod 已提交
32
#include "hb-aat-layout.hh"
33 34


35 36
namespace OT {

37

38 39 40 41
#define entry_score var.u16[0]
#define entry_index var.u16[1]


42
/*
43 44
 * name -- Naming
 * https://docs.microsoft.com/en-us/typography/opentype/spec/name
45 46 47
 */
#define HB_OT_TAG_name HB_TAG('n','a','m','e')

48
#define UNSUPPORTED	42
B
Behdad Esfahbod 已提交
49

50 51
struct NameRecord
{
52
  hb_language_t language (hb_face_t *face) const
53
  {
54
#ifndef HB_NO_OT_NAME_LANGUAGE
55 56 57 58
    unsigned int p = platformID;
    unsigned int l = languageID;

    if (p == 3)
B
Behdad Esfahbod 已提交
59 60 61 62 63
      return _hb_ot_name_language_for_ms_code (l);

    if (p == 1)
      return _hb_ot_name_language_for_mac_code (l);

64
#ifndef HB_NO_OT_NAME_LANGUAGE_AAT
B
Behdad Esfahbod 已提交
65
    if (p == 0)
66
      return face->table.ltag->get_language (l);
B
Behdad Esfahbod 已提交
67
#endif
68

69
#endif
70 71 72
    return HB_LANGUAGE_INVALID;
  }

73
  uint16_t score () const
74
  {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    /* Same order as in cmap::find_best_subtable(). */
    unsigned int p = platformID;
    unsigned int e = encodingID;

    /* 32-bit. */
    if (p == 3 && e == 10) return 0;
    if (p == 0 && e ==  6) return 1;
    if (p == 0 && e ==  4) return 2;

    /* 16-bit. */
    if (p == 3 && e ==  1) return 3;
    if (p == 0 && e ==  3) return 4;
    if (p == 0 && e ==  2) return 5;
    if (p == 0 && e ==  1) return 6;
    if (p == 0 && e ==  0) return 7;

    /* Symbol. */
    if (p == 3 && e ==  0) return 8;

B
Behdad Esfahbod 已提交
94 95
    /* We treat all Mac Latin names as ASCII only. */
    if (p == 1 && e ==  0) return 10; /* 10 is magic number :| */
96 97

    return UNSUPPORTED;
98 99
  }

B
Behdad Esfahbod 已提交
100 101 102 103
  NameRecord* copy (hb_serialize_context_t *c,
		    const void *src_base,
		    const void *dst_base) const
  {
104
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
105
    auto *out = c->embed (this);
106
    if (unlikely (!out)) return_trace (nullptr);
107
    out->offset.serialize_copy (c, offset, src_base, dst_base, length);
108
    return_trace (out);
B
Behdad Esfahbod 已提交
109 110
  }

111
  bool sanitize (hb_sanitize_context_t *c, const void *base) const
B
Behdad Esfahbod 已提交
112
  {
B
Behdad Esfahbod 已提交
113
    TRACE_SANITIZE (this);
B
[name]  
Behdad Esfahbod 已提交
114
    return_trace (c->check_struct (this) && offset.sanitize (c, base, length));
115 116
  }

B
Behdad Esfahbod 已提交
117 118 119 120 121
  HBUINT16	platformID;	/* Platform ID. */
  HBUINT16	encodingID;	/* Platform-specific encoding ID. */
  HBUINT16	languageID;	/* Language ID. */
  HBUINT16	nameID;		/* Name ID. */
  HBUINT16	length;		/* String length (in bytes). */
B
Behdad Esfahbod 已提交
122
  NNOffsetTo<UnsizedArrayOf<HBUINT8>>
B
[name]  
Behdad Esfahbod 已提交
123
		offset;		/* String offset from start of storage area (in bytes). */
124 125 126 127
  public:
  DEFINE_SIZE_STATIC (12);
};

128
static int
B
Behdad Esfahbod 已提交
129
_hb_ot_name_entry_cmp_key (const void *pa, const void *pb)
130 131 132
{
  const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
  const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
133

B
Behdad Esfahbod 已提交
134
  /* Compare by name_id, then language. */
135

136 137
  if (a->name_id != b->name_id)
    return a->name_id < b->name_id ? -1 : +1;
138

139 140 141
  if (a->language == b->language) return 0;
  if (!a->language) return -1;
  if (!b->language) return +1;
B
Behdad Esfahbod 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  return strcmp (hb_language_to_string (a->language),
		 hb_language_to_string (b->language));
}

static int
_hb_ot_name_entry_cmp (const void *pa, const void *pb)
{
  /* Compare by name_id, then language, then score, then index. */

  int v = _hb_ot_name_entry_cmp_key (pa, pb);
  if (v)
    return v;

  const hb_ot_name_entry_t *a = (const hb_ot_name_entry_t *) pa;
  const hb_ot_name_entry_t *b = (const hb_ot_name_entry_t *) pb;
157

158 159
  if (a->entry_score != b->entry_score)
    return a->entry_score < b->entry_score ? -1 : +1;
160

161 162
  if (a->entry_index != b->entry_index)
    return a->entry_index < b->entry_index ? -1 : +1;
163

164 165 166
  return 0;
}

167 168
struct name
{
169
  static constexpr hb_tag_t tableTag = HB_OT_TAG_name;
170

171
  unsigned int get_size () const
B
Behdad Esfahbod 已提交
172
  { return min_size + count * nameRecordZ.item_size; }
B
Behdad Esfahbod 已提交
173

B
Behdad Esfahbod 已提交
174
  template <typename Iterator,
B
Behdad Esfahbod 已提交
175
	    hb_requires (hb_is_source_of (Iterator, const NameRecord &))>
176
  bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
177 178
		  Iterator it,
		  const void *src_string_pool)
179 180 181 182 183
  {
    TRACE_SERIALIZE (this);

    if (unlikely (!c->extend_min ((*this))))  return_trace (false);

184
    this->format = 0;
B
Behdad Esfahbod 已提交
185
    this->count = it.len ();
186

187 188
    auto snap = c->snapshot ();
    this->nameRecordZ.serialize (c, this->count);
Q
Qunxin Liu 已提交
189
    if (unlikely (!c->check_assign (this->stringOffset, c->length ()))) return_trace (false);
190
    c->revert (snap);
191

B
Behdad Esfahbod 已提交
192
    const void *dst_string_pool = &(this + this->stringOffset);
193

B
Behdad Esfahbod 已提交
194
    + it
B
Behdad Esfahbod 已提交
195
    | hb_apply ([=] (const NameRecord& _) { c->copy (_, src_string_pool, dst_string_pool); })
196
    ;
B
Behdad Esfahbod 已提交
197

198
    if (unlikely (c->ran_out_of_room)) return_trace (false);
199

200
    assert (this->stringOffset == c->length ());
201 202

    return_trace (true);
203 204
  }

205
  bool subset (hb_subset_context_t *c) const
206
  {
B
Behdad Esfahbod 已提交
207
    TRACE_SUBSET (this);
208

B
Behdad Esfahbod 已提交
209
    name *name_prime = c->serializer->start_embed<name> ();
B
Behdad Esfahbod 已提交
210
    if (unlikely (!name_prime)) return_trace (false);
B
Behdad Esfahbod 已提交
211 212 213 214 215 216 217

    auto it =
    + nameRecordZ.as_array (count)
    | hb_filter (c->plan->name_ids, &NameRecord::nameID)
    ;

    name_prime->serialize (c->serializer, it, hb_addressof (this + stringOffset));
B
Behdad Esfahbod 已提交
218
    return_trace (name_prime->count);
219
  }
220

221 222
  bool sanitize_records (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
223
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
224
    const void *string_pool = (this+stringOffset).arrayZ;
225
    return_trace (nameRecordZ.sanitize (c, count, string_pool));
226 227
  }

228
  bool sanitize (hb_sanitize_context_t *c) const
B
Behdad Esfahbod 已提交
229
  {
B
Behdad Esfahbod 已提交
230
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
231 232
    return_trace (c->check_struct (this) &&
		  likely (format == 0 || format == 1) &&
233
		  c->check_array (nameRecordZ.arrayZ, count) &&
234 235
		  c->check_range (this, stringOffset) &&
		  sanitize_records (c));
236 237
  }

238 239
  struct accelerator_t
  {
240
    void init (hb_face_t *face)
241
    {
B
Behdad Esfahbod 已提交
242 243
      this->table = hb_sanitize_context_t().reference_table<name> (face);
      assert (this->table.get_length () >= this->table->stringOffset);
244
      this->pool = (const char *) (const void *) (this->table+this->table->stringOffset);
B
Behdad Esfahbod 已提交
245
      this->pool_len = this->table.get_length () - this->table->stringOffset;
246 247
      const hb_array_t<const NameRecord> all_names (this->table->nameRecordZ.arrayZ,
						    this->table->count);
248 249

      this->names.init ();
250
      this->names.alloc (all_names.length);
251

252
      for (unsigned int i = 0; i < all_names.length; i++)
253
      {
254
	hb_ot_name_entry_t *entry = this->names.push ();
255

256
	entry->name_id = all_names[i].nameID;
B
Behdad Esfahbod 已提交
257
	entry->language = all_names[i].language (face);
258 259
	entry->entry_score =  all_names[i].score ();
	entry->entry_index = i;
260 261 262
      }

      this->names.qsort (_hb_ot_name_entry_cmp);
B
Behdad Esfahbod 已提交
263 264
      /* Walk and pick best only for each name_id,language pair,
       * while dropping unsupported encodings. */
265
      unsigned int j = 0;
266
      for (unsigned int i = 0; i < this->names.length; i++)
267
      {
268 269
        if (this->names[i].entry_score == UNSUPPORTED ||
	    this->names[i].language == HB_LANGUAGE_INVALID)
B
Behdad Esfahbod 已提交
270 271 272
	  continue;
        if (i &&
	    this->names[i - 1].name_id  == this->names[i].name_id &&
273 274 275 276 277
	    this->names[i - 1].language == this->names[i].language)
	  continue;
	this->names[j++] = this->names[i];
      }
      this->names.resize (j);
278 279
    }

280
    void fini ()
281 282
    {
      this->names.fini ();
B
Behdad Esfahbod 已提交
283
      this->table.destroy ();
284 285
    }

286
    int get_index (hb_ot_name_id_t   name_id,
287 288
			  hb_language_t     language,
			  unsigned int     *width=nullptr) const
B
Behdad Esfahbod 已提交
289 290 291 292
    {
      const hb_ot_name_entry_t key = {name_id, {0}, language};
      const hb_ot_name_entry_t *entry = (const hb_ot_name_entry_t *)
					hb_bsearch (&key,
293
						    (const hb_ot_name_entry_t *) this->names,
294
						    this->names.length,
B
Behdad Esfahbod 已提交
295 296
						    sizeof (key),
						    _hb_ot_name_entry_cmp_key);
B
Behdad Esfahbod 已提交
297 298 299 300 301 302 303
      if (!entry)
        return -1;

      if (width)
        *width = entry->entry_score < 10 ? 2 : 1;

      return entry->entry_index;
B
Behdad Esfahbod 已提交
304 305
    }

306
    hb_bytes_t get_name (unsigned int idx) const
307 308 309
    {
      const hb_array_t<const NameRecord> all_names (table->nameRecordZ.arrayZ, table->count);
      const NameRecord &record = all_names[idx];
310 311
      const hb_bytes_t string_pool (pool, pool_len);
      return string_pool.sub_array (record.offset, record.length);
312 313
    }

314
    private:
315
    const char *pool;
316
    unsigned int pool_len;
317
    public:
B
Behdad Esfahbod 已提交
318
    hb_blob_ptr_t<name> table;
319 320 321
    hb_vector_t<hb_ot_name_entry_t> names;
  };

322
  /* We only implement format 0 for now. */
B
Behdad Esfahbod 已提交
323 324
  HBUINT16	format;			/* Format selector (=0/1). */
  HBUINT16	count;			/* Number of name records. */
325
  NNOffsetTo<UnsizedArrayOf<HBUINT8>>
B
Behdad Esfahbod 已提交
326
		stringOffset;		/* Offset to start of string storage (from start of table). */
327 328
  UnsizedArrayOf<NameRecord>
		nameRecordZ;		/* The name records where count is the number of records. */
329
  public:
330
  DEFINE_SIZE_ARRAY (6, nameRecordZ);
331 332
};

333
struct name_accelerator_t : name::accelerator_t {};
334

B
Behdad Esfahbod 已提交
335
} /* namespace OT */
336

337

338
#endif /* HB_OT_NAME_TABLE_HH */