summaryrefslogtreecommitdiffstats
path: root/gfx/2d/SFNTNameTable.cpp
blob: a30c6bd97aa4ffb81d3bb254c9075d83413d9dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SFNTNameTable.h"

#include "BigEndianInts.h"
#include "Logging.h"
#include "mozilla/Move.h"

#if defined(XP_MACOSX)
#include <CoreFoundation/CoreFoundation.h>
#endif

namespace mozilla {
namespace gfx {

static const BigEndianUint16 FORMAT_0 = 0;

static const BigEndianUint16 NAME_ID_FAMILY = 1;
static const BigEndianUint16 NAME_ID_STYLE = 2;
static const BigEndianUint16 NAME_ID_FULL = 4;

static const BigEndianUint16 PLATFORM_ID_UNICODE = 0;
static const BigEndianUint16 PLATFORM_ID_MAC = 1;
static const BigEndianUint16 PLATFORM_ID_MICROSOFT = 3;

static const BigEndianUint16 ENCODING_ID_MICROSOFT_SYMBOL = 0;
static const BigEndianUint16 ENCODING_ID_MICROSOFT_UNICODEBMP = 1;
static const BigEndianUint16 ENCODING_ID_MICROSOFT_UNICODEFULL = 10;

static const BigEndianUint16 ENCODING_ID_MAC_ROMAN = 0;

static const BigEndianUint16 LANG_ID_MAC_ENGLISH = 0;

static const BigEndianUint16 LANG_ID_MICROSOFT_EN_US = 0x0409;

#pragma pack(push, 1)

// Name table has a header, followed by name records, followed by string data.
struct NameHeader
{
  BigEndianUint16 format;       // Format selector (=0).
  BigEndianUint16 count;        // Number of name records.
  BigEndianUint16 stringOffset; // Offset to string storage from start of table.
};

struct NameRecord
{
  BigEndianUint16 platformID;
  BigEndianUint16 encodingID; // Platform-specific encoding ID
  BigEndianUint16 languageID;
  BigEndianUint16 nameID;
  BigEndianUint16 length;     // String length in bytes.
  BigEndianUint16 offset;     // String offset from start of storage in bytes.
};

#pragma pack(pop)

enum ENameDecoder : int
{
  eNameDecoderUTF16,
#if defined(XP_MACOSX)
  eNameDecoderMacRoman,
#endif
  eNameDecoderNone
};

/* static */
UniquePtr<SFNTNameTable>
SFNTNameTable::Create(const uint8_t *aNameData, uint32_t aDataLength)
{
  MOZ_ASSERT(aNameData);

  if (aDataLength < sizeof(NameHeader)) {
    gfxWarning() << "Name data too short to contain NameHeader.";
    return nullptr;
  }

  const NameHeader *nameHeader = reinterpret_cast<const NameHeader*>(aNameData);
  if (nameHeader->format != FORMAT_0) {
    gfxWarning() << "Only Name Table Format 0 is supported.";
    return nullptr;
  }

  uint16_t stringOffset = nameHeader->stringOffset;

  if (stringOffset !=
      sizeof(NameHeader) + (nameHeader->count * sizeof(NameRecord))) {
    gfxWarning() << "Name table string offset is incorrect.";
    return nullptr;
  }

  if (aDataLength < stringOffset) {
    gfxWarning() << "Name data too short to contain name records.";
    return nullptr;
  }

  return UniquePtr<SFNTNameTable>(
    new SFNTNameTable(nameHeader, aNameData, aDataLength));
}

SFNTNameTable::SFNTNameTable(const NameHeader *aNameHeader,
                             const uint8_t *aNameData, uint32_t aDataLength)
  : mFirstRecord(reinterpret_cast<const NameRecord*>(aNameData
                                                     + sizeof(NameHeader)))
  , mEndOfRecords(mFirstRecord + aNameHeader->count)
  , mStringData(aNameData + aNameHeader->stringOffset)
  , mStringDataLength(aDataLength - aNameHeader->stringOffset)
{
  MOZ_ASSERT(reinterpret_cast<const uint8_t*>(aNameHeader) == aNameData);
}

static bool
IsUTF16Encoding(const NameRecord *aNameRecord)
{
  if (aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
      (aNameRecord->encodingID == ENCODING_ID_MICROSOFT_UNICODEBMP ||
       aNameRecord->encodingID == ENCODING_ID_MICROSOFT_SYMBOL)) {
    return true;
  }

  if (aNameRecord->platformID == PLATFORM_ID_UNICODE) {
    return true;
  }

  return false;
}

#if defined(XP_MACOSX)
static bool
IsMacRomanEncoding(const NameRecord *aNameRecord)
{
  if (aNameRecord->platformID == PLATFORM_ID_MAC &&
      aNameRecord->encodingID == ENCODING_ID_MAC_ROMAN) {
    return true;
  }

  return false;
}
#endif

static NameRecordMatchers*
CreateCanonicalMatchers(const BigEndianUint16& aNameID)
{
  // For Windows, we return only Microsoft platform name record
  // matchers. On Mac, we return matchers for both Microsoft platform
  // records and Mac platform records.
  NameRecordMatchers *matchers = new NameRecordMatchers();

#if defined(XP_MACOSX)
  // First, look for the English name.
  if (!matchers->append(
    [=](const NameRecord *aNameRecord) {
        if (aNameRecord->nameID == aNameID &&
            aNameRecord->languageID == LANG_ID_MAC_ENGLISH &&
            aNameRecord->platformID == PLATFORM_ID_MAC &&
            IsMacRomanEncoding(aNameRecord)) {
          return eNameDecoderMacRoman;
        } else  {
          return eNameDecoderNone;
        }
    })) {
    MOZ_CRASH();
  }

  // Second, look for all languages.
  if (!matchers->append(
    [=](const NameRecord *aNameRecord) {
        if (aNameRecord->nameID == aNameID &&
            aNameRecord->platformID == PLATFORM_ID_MAC &&
            IsMacRomanEncoding(aNameRecord)) {
          return eNameDecoderMacRoman;
        } else  {
          return eNameDecoderNone;
        }
    })) {
    MOZ_CRASH();
  }
#endif /* defined(XP_MACOSX) */

  // First, look for the English name (this will normally succeed).
  if (!matchers->append(
    [=](const NameRecord *aNameRecord) {
        if (aNameRecord->nameID == aNameID &&
            aNameRecord->languageID == LANG_ID_MICROSOFT_EN_US &&
            aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
            IsUTF16Encoding(aNameRecord)) {
          return eNameDecoderUTF16;
        } else {
          return eNameDecoderNone;
        }
    })) {
    MOZ_CRASH();
  }

  // Second, look for all languages.
  if (!matchers->append(
    [=](const NameRecord *aNameRecord) {
        if (aNameRecord->nameID == aNameID &&
            aNameRecord->platformID == PLATFORM_ID_MICROSOFT &&
            IsUTF16Encoding(aNameRecord)) {
          return eNameDecoderUTF16;
        } else {
          return eNameDecoderNone;
        }
    })) {
    MOZ_CRASH();
  }

  return matchers;
}

static const NameRecordMatchers&
FullNameMatchers()
{
  static const NameRecordMatchers *sFullNameMatchers =
    CreateCanonicalMatchers(NAME_ID_FULL);
  return *sFullNameMatchers;
}

static const NameRecordMatchers&
FamilyMatchers()
{
  static const NameRecordMatchers *sFamilyMatchers =
    CreateCanonicalMatchers(NAME_ID_FAMILY);
  return *sFamilyMatchers;
}

static const NameRecordMatchers&
StyleMatchers()
{
  static const NameRecordMatchers *sStyleMatchers =
    CreateCanonicalMatchers(NAME_ID_STYLE);
  return *sStyleMatchers;
}

bool
SFNTNameTable::GetU16FullName(mozilla::u16string& aU16FullName)
{
  if (ReadU16Name(FullNameMatchers(), aU16FullName)) {
    return true;
  }

  // If the full name record doesn't exist create the name from the family space
  // concatenated with the style.
  mozilla::u16string familyName;
  if (!ReadU16Name(FamilyMatchers(), familyName)) {
    return false;
  }

  mozilla::u16string styleName;
  if (!ReadU16Name(StyleMatchers(), styleName)) {
    return false;
  }

  aU16FullName.assign(Move(familyName));
  aU16FullName.append(u" ");
  aU16FullName.append(styleName);
  return true;
}

bool
SFNTNameTable::ReadU16Name(const NameRecordMatchers& aMatchers,
                           mozilla::u16string& aU16Name)
{
  MOZ_ASSERT(!aMatchers.empty());

  for (size_t i = 0; i < aMatchers.length(); ++i) {
    const NameRecord* record = mFirstRecord;
    while (record != mEndOfRecords) {
      switch (aMatchers[i](record)) {
        case eNameDecoderUTF16:
          return ReadU16NameFromU16Record(record, aU16Name);
#if defined(XP_MACOSX)
        case eNameDecoderMacRoman:
          return ReadU16NameFromMacRomanRecord(record, aU16Name);
#endif
        case eNameDecoderNone:
          break;
        default:
          MOZ_CRASH("Invalid matcher encoding type");
          break;
      }
      ++record;
    }
  }

  return false;
}

bool
SFNTNameTable::ReadU16NameFromU16Record(const NameRecord *aNameRecord,
                                        mozilla::u16string& aU16Name)
{
  uint32_t offset = aNameRecord->offset;
  uint32_t length = aNameRecord->length;
  if (mStringDataLength < offset + length) {
    gfxWarning() << "Name data too short to contain name string.";
    return false;
  }

  const uint8_t *startOfName = mStringData + offset;
  size_t actualLength = length / sizeof(char16_t);
  UniquePtr<char16_t[]> nameData(new char16_t[actualLength]);
  NativeEndian::copyAndSwapFromBigEndian(nameData.get(), startOfName,
                                         actualLength);

  aU16Name.assign(nameData.get(), actualLength);
  return true;
}

#if defined(XP_MACOSX)
bool
SFNTNameTable::ReadU16NameFromMacRomanRecord(const NameRecord *aNameRecord,
                                             mozilla::u16string& aU16Name)
{
  uint32_t offset = aNameRecord->offset;
  uint32_t length = aNameRecord->length;
  if (mStringDataLength < offset + length) {
    gfxWarning() << "Name data too short to contain name string.";
    return false;
  }
  if (length > INT_MAX) {
    gfxWarning() << "Name record too long to decode.";
    return false;
  }

  // pointer to the Mac Roman encoded string in the name record
  const uint8_t *encodedStr = mStringData + offset;

  CFStringRef cfString;
  cfString = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, encodedStr,
                                           length, kCFStringEncodingMacRoman,
                                           false, kCFAllocatorNull);

  // length (in UTF-16 code pairs) of the decoded string
  CFIndex decodedLength = CFStringGetLength(cfString);

  // temporary buffer
  UniquePtr<UniChar[]> u16Buffer = MakeUnique<UniChar[]>(decodedLength);

  CFStringGetCharacters(cfString, CFRangeMake(0, decodedLength),
                        u16Buffer.get());

  CFRelease(cfString);

  aU16Name.assign(reinterpret_cast<char16_t*>(u16Buffer.get()), decodedLength);

  return true;
}
#endif

} // gfx
} // mozilla