hb-ot-name-table.hh revision e9c71fab30fd1d5b163c8a072f9e2d3eb8ba3a92
1b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod/*
2b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * Copyright © 2011  Google, Inc.
3b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod *
4b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
5b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod *
6b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * Permission is hereby granted, without written agreement and without
7b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
8b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * software and its documentation for any purpose, provided that the
9b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
10b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * all copies of this software.
11b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod *
12b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * DAMAGE.
17b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod *
18b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod *
24b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * Google Author(s): Behdad Esfahbod
25b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod */
26b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
27b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod#ifndef HB_OT_NAME_PRIVATE_HH
28b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod#define HB_OT_NAME_PRIVATE_HH
29b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
30b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod#include "hb-open-type-private.hh"
31b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
32b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
33b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
34b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod/*
35b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod * name
36b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod */
37b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
38b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod#define HB_OT_TAG_name HB_TAG('n','a','m','e')
39b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
40b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbodstruct NameRecord
41b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod{
42892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod  static int cmp (const NameRecord *a, const NameRecord *b)
43892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod  {
44892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    int ret;
45892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    ret = b->platformID.cmp (a->platformID);
46892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    if (ret) return ret;
47892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    ret = b->encodingID.cmp (a->encodingID);
48892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    if (ret) return ret;
49892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    ret = b->languageID.cmp (a->languageID);
50892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    if (ret) return ret;
51892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    ret = b->nameID.cmp (a->nameID);
52892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    if (ret) return ret;
53892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    return 0;
54892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod  }
55892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod
56b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *c, void *base) {
57b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    TRACE_SANITIZE ();
58b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    /* We can check from base all the way up to the end of string... */
59b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    return c->check_struct (this) &&
60b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod	   c->check_range ((char *) base, (unsigned int) length + offset);
61b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  }
62b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
63b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	platformID;	/* Platform ID. */
64b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	encodingID;	/* Platform-specific encoding ID. */
65b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	languageID;	/* Language ID. */
66b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	nameID;		/* Name ID. */
67b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	length;		/* String length (in bytes). */
68b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	offset;		/* String offset from start of storage area (in bytes). */
69b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  public:
70b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  DEFINE_SIZE_STATIC (12);
71b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod};
72b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
73b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbodstruct name
74b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod{
75b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  static const hb_tag_t Tag	= HB_OT_TAG_name;
76b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
77892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod  inline unsigned int get_name (unsigned int platform_id,
78892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod				unsigned int encoding_id,
79892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod				unsigned int language_id,
80892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod				unsigned int name_id,
81892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod				void *buffer,
82892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod				unsigned int buffer_length) const
83892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod  {
84892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    NameRecord key;
85892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    key.platformID.set (platform_id);
86892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    key.encodingID.set (encoding_id);
87892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    key.languageID.set (language_id);
88892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    key.nameID.set (name_id);
89892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    NameRecord *match = (NameRecord *) bsearch (&key, nameRecord, count, sizeof (nameRecord[0]), (hb_compare_func_t) NameRecord::cmp);
90892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod
91892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    if (!match)
92892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod      return 0;
93892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod
94892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    unsigned int length = MIN (buffer_length, (unsigned int) match->length);
95e9c71fab30fd1d5b163c8a072f9e2d3eb8ba3a92Behdad Esfahbod    memcpy (buffer, (char *) this + stringOffset + match->offset, length);
96892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    return length;
97892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod  }
98892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod
99b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  inline bool sanitize_records (hb_sanitize_context_t *c) {
100b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    TRACE_SANITIZE ();
101b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    unsigned int _count = count;
102b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    for (unsigned int i = 0; i < _count; i++)
103b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod      if (!nameRecord[i].sanitize (c, this + stringOffset)) return false;
104b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    return true;
105b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  }
106b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
107b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *c) {
108b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    TRACE_SANITIZE ();
109892eb2e462b40451b8f73879eab66310d884386aBehdad Esfahbod    return true;
110b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod    return c->check_struct (this) &&
111b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod	   likely (format == 0 || format == 1) &&
112b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod	   c->check_array (nameRecord, nameRecord[0].static_size, count) &&
113b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod	   sanitize_records (c);
114b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  }
115b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
116b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  /* We only implement format 0 for now. */
117b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  private:
118b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	format;			/* Format selector (=0/1). */
119b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  USHORT	count;			/* Number of name records. */
120b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  Offset	stringOffset;		/* Offset to start of string storage (from start of table). */
121b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  NameRecord	nameRecord[VAR];	/* The name records where count is the number of records. */
122b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  public:
123b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod  DEFINE_SIZE_ARRAY (6, nameRecord);
124b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod};
125b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
126b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
127b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod
128b492299eb3c398701557e452f6c2c9bd370fbbf3Behdad Esfahbod#endif /* HB_OT_NAME_PRIVATE_HH */
129