Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 2 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 3 | * |
| 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 Esfahbod | 7a750ac | 2011-08-17 14:19:59 +0200 | [diff] [blame] | 27 | #ifndef HB_OT_NAME_TABLE_HH |
| 28 | #define HB_OT_NAME_TABLE_HH |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 29 | |
| 30 | #include "hb-open-type-private.hh" |
| 31 | |
| 32 | |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 33 | namespace OT { |
| 34 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 35 | |
| 36 | /* |
Behdad Esfahbod | ae9877d | 2011-08-17 14:43:45 +0200 | [diff] [blame] | 37 | * name -- The Naming Table |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | #define HB_OT_TAG_name HB_TAG('n','a','m','e') |
| 41 | |
Behdad Esfahbod | ae9877d | 2011-08-17 14:43:45 +0200 | [diff] [blame] | 42 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 43 | struct NameRecord |
| 44 | { |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 45 | static int cmp (const NameRecord *a, const NameRecord *b) |
| 46 | { |
| 47 | int ret; |
| 48 | ret = b->platformID.cmp (a->platformID); |
| 49 | if (ret) return ret; |
| 50 | ret = b->encodingID.cmp (a->encodingID); |
| 51 | if (ret) return ret; |
| 52 | ret = b->languageID.cmp (a->languageID); |
| 53 | if (ret) return ret; |
| 54 | ret = b->nameID.cmp (a->nameID); |
| 55 | if (ret) return ret; |
| 56 | return 0; |
| 57 | } |
| 58 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 59 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 60 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 61 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 62 | /* We can check from base all the way up to the end of string... */ |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 63 | return TRACE_RETURN (c->check_struct (this) && c->check_range ((char *) base, (unsigned int) length + offset)); |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | USHORT platformID; /* Platform ID. */ |
| 67 | USHORT encodingID; /* Platform-specific encoding ID. */ |
| 68 | USHORT languageID; /* Language ID. */ |
| 69 | USHORT nameID; /* Name ID. */ |
| 70 | USHORT length; /* String length (in bytes). */ |
| 71 | USHORT offset; /* String offset from start of storage area (in bytes). */ |
| 72 | public: |
| 73 | DEFINE_SIZE_STATIC (12); |
| 74 | }; |
| 75 | |
| 76 | struct name |
| 77 | { |
Behdad Esfahbod | 6c48f20 | 2013-09-09 15:43:10 -0400 | [diff] [blame] | 78 | static const hb_tag_t tableTag = HB_OT_TAG_name; |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 79 | |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 80 | inline unsigned int get_name (unsigned int platform_id, |
| 81 | unsigned int encoding_id, |
| 82 | unsigned int language_id, |
| 83 | unsigned int name_id, |
| 84 | void *buffer, |
| 85 | unsigned int buffer_length) const |
| 86 | { |
| 87 | NameRecord key; |
| 88 | key.platformID.set (platform_id); |
| 89 | key.encodingID.set (encoding_id); |
| 90 | key.languageID.set (language_id); |
| 91 | key.nameID.set (name_id); |
| 92 | NameRecord *match = (NameRecord *) bsearch (&key, nameRecord, count, sizeof (nameRecord[0]), (hb_compare_func_t) NameRecord::cmp); |
| 93 | |
| 94 | if (!match) |
| 95 | return 0; |
| 96 | |
| 97 | unsigned int length = MIN (buffer_length, (unsigned int) match->length); |
Behdad Esfahbod | e9c71fa | 2011-08-07 00:00:27 -0400 | [diff] [blame] | 98 | memcpy (buffer, (char *) this + stringOffset + match->offset, length); |
Behdad Esfahbod | 892eb2e | 2011-08-06 22:06:52 -0400 | [diff] [blame] | 99 | return length; |
| 100 | } |
| 101 | |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 102 | inline unsigned int get_size (void) const |
| 103 | { return min_size + count * nameRecord[0].min_size; } |
| 104 | |
Behdad Esfahbod | 98e3ea8 | 2015-03-04 12:03:39 -0800 | [diff] [blame^] | 105 | inline bool sanitize_records (hb_sanitize_context_t *c) const { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 106 | TRACE_SANITIZE (this); |
Behdad Esfahbod | d6016e4 | 2011-08-17 14:47:41 +0200 | [diff] [blame] | 107 | char *string_pool = (char *) this + stringOffset; |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 108 | unsigned int _count = count; |
| 109 | for (unsigned int i = 0; i < _count; i++) |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 110 | if (!nameRecord[i].sanitize (c, string_pool)) return TRACE_RETURN (false); |
| 111 | return TRACE_RETURN (true); |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 114 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 115 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 116 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 117 | return TRACE_RETURN (c->check_struct (this) && |
| 118 | likely (format == 0 || format == 1) && |
| 119 | c->check_array (nameRecord, nameRecord[0].static_size, count) && |
| 120 | sanitize_records (c)); |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* We only implement format 0 for now. */ |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 124 | USHORT format; /* Format selector (=0/1). */ |
| 125 | USHORT count; /* Number of name records. */ |
Behdad Esfahbod | 8b16ff1 | 2014-07-09 17:40:52 -0400 | [diff] [blame] | 126 | Offset<> stringOffset; /* Offset to start of string storage (from start of table). */ |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 127 | NameRecord nameRecord[VAR]; /* The name records where count is the number of records. */ |
| 128 | public: |
| 129 | DEFINE_SIZE_ARRAY (6, nameRecord); |
| 130 | }; |
| 131 | |
| 132 | |
Behdad Esfahbod | 7d52e66 | 2012-11-16 18:49:54 -0800 | [diff] [blame] | 133 | } /* namespace OT */ |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 134 | |
Behdad Esfahbod | b492299 | 2011-08-05 20:34:50 -0400 | [diff] [blame] | 135 | |
Behdad Esfahbod | 7a750ac | 2011-08-17 14:19:59 +0200 | [diff] [blame] | 136 | #endif /* HB_OT_NAME_TABLE_HH */ |