blob: bb49c2cb0d2fefd4dfed9d8859a8d93dbbf578f4 [file] [log] [blame]
Behdad Esfahbodb4922992011-08-05 20:34:50 -04001/*
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02002 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbodb4922992011-08-05 20:34:50 -04003 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020027#ifndef HB_OT_NAME_TABLE_HH
28#define HB_OT_NAME_TABLE_HH
Behdad Esfahbodb4922992011-08-05 20:34:50 -040029
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-open-type.hh"
Behdad Esfahbodb4922992011-08-05 20:34:50 -040031
32
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040033namespace OT {
34
Behdad Esfahbodb4922992011-08-05 20:34:50 -040035
36/*
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +043037 * name -- Naming
38 * https://docs.microsoft.com/en-us/typography/opentype/spec/name
Behdad Esfahbodb4922992011-08-05 20:34:50 -040039 */
Behdad Esfahbodb4922992011-08-05 20:34:50 -040040#define HB_OT_TAG_name HB_TAG('n','a','m','e')
41
Behdad Esfahbodae9877d2011-08-17 14:43:45 +020042
Behdad Esfahbodb4922992011-08-05 20:34:50 -040043struct NameRecord
44{
Behdad Esfahbodec86cc52017-10-30 14:11:59 -060045 static int cmp (const void *pa, const void *pb)
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040046 {
Behdad Esfahbodec86cc52017-10-30 14:11:59 -060047 const NameRecord *a = (const NameRecord *) pa;
48 const NameRecord *b = (const NameRecord *) pb;
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040049 int ret;
50 ret = b->platformID.cmp (a->platformID);
51 if (ret) return ret;
52 ret = b->encodingID.cmp (a->encodingID);
53 if (ret) return ret;
54 ret = b->languageID.cmp (a->languageID);
55 if (ret) return ret;
56 ret = b->nameID.cmp (a->nameID);
57 if (ret) return ret;
58 return 0;
59 }
60
Behdad Esfahbodde2118e2015-02-17 17:27:44 +030061 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
62 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -050063 TRACE_SANITIZE (this);
Behdad Esfahbodb4922992011-08-05 20:34:50 -040064 /* We can check from base all the way up to the end of string... */
Behdad Esfahbodb4715902015-09-29 14:57:02 +010065 return_trace (c->check_struct (this) && c->check_range ((char *) base, (unsigned int) length + offset));
Behdad Esfahbodb4922992011-08-05 20:34:50 -040066 }
67
Behdad Esfahbod6b191782018-01-10 03:07:30 +010068 HBUINT16 platformID; /* Platform ID. */
69 HBUINT16 encodingID; /* Platform-specific encoding ID. */
70 HBUINT16 languageID; /* Language ID. */
71 HBUINT16 nameID; /* Name ID. */
72 HBUINT16 length; /* String length (in bytes). */
73 HBUINT16 offset; /* String offset from start of storage area (in bytes). */
Behdad Esfahbodb4922992011-08-05 20:34:50 -040074 public:
75 DEFINE_SIZE_STATIC (12);
76};
77
78struct name
79{
Behdad Esfahbod6c48f202013-09-09 15:43:10 -040080 static const hb_tag_t tableTag = HB_OT_TAG_name;
Behdad Esfahbodb4922992011-08-05 20:34:50 -040081
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040082 inline unsigned int get_name (unsigned int platform_id,
83 unsigned int encoding_id,
84 unsigned int language_id,
85 unsigned int name_id,
86 void *buffer,
87 unsigned int buffer_length) const
88 {
89 NameRecord key;
90 key.platformID.set (platform_id);
91 key.encodingID.set (encoding_id);
92 key.languageID.set (language_id);
93 key.nameID.set (name_id);
Behdad Esfahboddff2c452018-09-10 23:29:26 +020094 NameRecord *match = (NameRecord *) bsearch (&key, nameRecordZ.arrayZ, count, sizeof (nameRecordZ[0]), NameRecord::cmp);
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040095
96 if (!match)
97 return 0;
98
99 unsigned int length = MIN (buffer_length, (unsigned int) match->length);
Behdad Esfahbode9c71fa2011-08-07 00:00:27 -0400100 memcpy (buffer, (char *) this + stringOffset + match->offset, length);
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -0400101 return length;
102 }
103
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400104 inline unsigned int get_size (void) const
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200105 { return min_size + count * nameRecordZ[0].min_size; }
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400106
Behdad Esfahbod98e3ea82015-03-04 12:03:39 -0800107 inline bool sanitize_records (hb_sanitize_context_t *c) const {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500108 TRACE_SANITIZE (this);
Behdad Esfahbodd6016e42011-08-17 14:47:41 +0200109 char *string_pool = (char *) this + stringOffset;
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400110 unsigned int _count = count;
111 for (unsigned int i = 0; i < _count; i++)
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200112 if (!nameRecordZ[i].sanitize (c, string_pool)) return_trace (false);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100113 return_trace (true);
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400114 }
115
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300116 inline bool sanitize (hb_sanitize_context_t *c) const
117 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500118 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100119 return_trace (c->check_struct (this) &&
120 likely (format == 0 || format == 1) &&
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200121 c->check_array (nameRecordZ.arrayZ, count) &&
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100122 sanitize_records (c));
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400123 }
124
125 /* We only implement format 0 for now. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100126 HBUINT16 format; /* Format selector (=0/1). */
127 HBUINT16 count; /* Number of name records. */
Behdad Esfahbodc6173a32017-11-14 21:09:03 -0800128 Offset16 stringOffset; /* Offset to start of string storage (from start of table). */
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200129 UnsizedArrayOf<NameRecord>
130 nameRecordZ; /* The name records where count is the number of records. */
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400131 public:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200132 DEFINE_SIZE_ARRAY (6, nameRecordZ);
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400133};
134
135
Behdad Esfahbod7d52e662012-11-16 18:49:54 -0800136} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -0400137
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400138
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +0200139#endif /* HB_OT_NAME_TABLE_HH */