michael@0: /* michael@0: * Copyright © 2011,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OT_NAME_TABLE_HH michael@0: #define HB_OT_NAME_TABLE_HH michael@0: michael@0: #include "hb-open-type-private.hh" michael@0: michael@0: michael@0: namespace OT { michael@0: michael@0: michael@0: /* michael@0: * name -- The Naming Table michael@0: */ michael@0: michael@0: #define HB_OT_TAG_name HB_TAG('n','a','m','e') michael@0: michael@0: michael@0: struct NameRecord michael@0: { michael@0: static int cmp (const NameRecord *a, const NameRecord *b) michael@0: { michael@0: int ret; michael@0: ret = b->platformID.cmp (a->platformID); michael@0: if (ret) return ret; michael@0: ret = b->encodingID.cmp (a->encodingID); michael@0: if (ret) return ret; michael@0: ret = b->languageID.cmp (a->languageID); michael@0: if (ret) return ret; michael@0: ret = b->nameID.cmp (a->nameID); michael@0: if (ret) return ret; michael@0: return 0; michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c, void *base) { michael@0: TRACE_SANITIZE (this); michael@0: /* We can check from base all the way up to the end of string... */ michael@0: return TRACE_RETURN (c->check_struct (this) && c->check_range ((char *) base, (unsigned int) length + offset)); michael@0: } michael@0: michael@0: USHORT platformID; /* Platform ID. */ michael@0: USHORT encodingID; /* Platform-specific encoding ID. */ michael@0: USHORT languageID; /* Language ID. */ michael@0: USHORT nameID; /* Name ID. */ michael@0: USHORT length; /* String length (in bytes). */ michael@0: USHORT offset; /* String offset from start of storage area (in bytes). */ michael@0: public: michael@0: DEFINE_SIZE_STATIC (12); michael@0: }; michael@0: michael@0: struct name michael@0: { michael@0: static const hb_tag_t tableTag = HB_OT_TAG_name; michael@0: michael@0: inline unsigned int get_name (unsigned int platform_id, michael@0: unsigned int encoding_id, michael@0: unsigned int language_id, michael@0: unsigned int name_id, michael@0: void *buffer, michael@0: unsigned int buffer_length) const michael@0: { michael@0: NameRecord key; michael@0: key.platformID.set (platform_id); michael@0: key.encodingID.set (encoding_id); michael@0: key.languageID.set (language_id); michael@0: key.nameID.set (name_id); michael@0: NameRecord *match = (NameRecord *) bsearch (&key, nameRecord, count, sizeof (nameRecord[0]), (hb_compare_func_t) NameRecord::cmp); michael@0: michael@0: if (!match) michael@0: return 0; michael@0: michael@0: unsigned int length = MIN (buffer_length, (unsigned int) match->length); michael@0: memcpy (buffer, (char *) this + stringOffset + match->offset, length); michael@0: return length; michael@0: } michael@0: michael@0: inline unsigned int get_size (void) const michael@0: { return min_size + count * nameRecord[0].min_size; } michael@0: michael@0: inline bool sanitize_records (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: char *string_pool = (char *) this + stringOffset; michael@0: unsigned int _count = count; michael@0: for (unsigned int i = 0; i < _count; i++) michael@0: if (!nameRecord[i].sanitize (c, string_pool)) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this) && michael@0: likely (format == 0 || format == 1) && michael@0: c->check_array (nameRecord, nameRecord[0].static_size, count) && michael@0: sanitize_records (c)); michael@0: } michael@0: michael@0: /* We only implement format 0 for now. */ michael@0: USHORT format; /* Format selector (=0/1). */ michael@0: USHORT count; /* Number of name records. */ michael@0: Offset stringOffset; /* Offset to start of string storage (from start of table). */ michael@0: NameRecord nameRecord[VAR]; /* The name records where count is the number of records. */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (6, nameRecord); michael@0: }; michael@0: michael@0: michael@0: } /* namespace OT */ michael@0: michael@0: michael@0: #endif /* HB_OT_NAME_TABLE_HH */