1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 1999-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: PortableFontInstance.cpp
11 *
12 * created on: 11/22/1999
13 * created by: Eric R. Mader
14 */
15
16 #include <stdio.h>
17
18 #include "layout/LETypes.h"
19 #include "layout/LEFontInstance.h"
20 #include "layout/LESwaps.h"
21
22 #include "PortableFontInstance.h"
23
24 //#include "letest.h"
25 #include "sfnt.h"
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #if 0
31 static const char *letagToStr(LETag tag, char *str) {
32 str[0]= 0xFF & (tag>>24);
33 str[1]= 0xFF & (tag>>16);
34 str[2]= 0xFF & (tag>>8);
35 str[3]= 0xFF & (tag>>0);
36 str[4]= 0;
37 return str;
38 }
39 #endif
40
41 //
42 // Finds the high bit by binary searching
43 // through the bits in n.
44 //
highBit(le_int32 value)45 le_int8 PortableFontInstance::highBit(le_int32 value)
46 {
47 if (value <= 0) {
48 return -32;
49 }
50
51 le_uint8 bit = 0;
52
53 if (value >= 1 << 16) {
54 value >>= 16;
55 bit += 16;
56 }
57
58 if (value >= 1 << 8) {
59 value >>= 8;
60 bit += 8;
61 }
62
63 if (value >= 1 << 4) {
64 value >>= 4;
65 bit += 4;
66 }
67
68 if (value >= 1 << 2) {
69 value >>= 2;
70 bit += 2;
71 }
72
73 if (value >= 1 << 1) {
74 value >>= 1;
75 bit += 1;
76 }
77
78 return bit;
79 }
80
PortableFontInstance(const char * fileName,float pointSize,LEErrorCode & status)81 PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize, LEErrorCode &status)
82 : fFile(nullptr), fPointSize(pointSize), fUnitsPerEM(0), fFontChecksum(0), fAscent(0), fDescent(0), fLeading(0),
83 fDirectory(nullptr), fNAMETable(nullptr), fNameCount(0), fNameStringOffset(0), fCMAPMapper(nullptr), fHMTXTable(nullptr), fNumGlyphs(0), fNumLongHorMetrics(0)
84 {
85 if (LE_FAILURE(status)) {
86 return;
87 }
88
89 // open the font file
90 fFile = fopen(fileName, "rb");
91 //printf("Open Font: %s\n", fileName);
92
93 if (fFile == nullptr) {
94 printf("%s:%d: %s: FNF\n", __FILE__, __LINE__, fileName);
95 status = LE_FONT_FILE_NOT_FOUND_ERROR;
96 return;
97 }
98
99 // read in the directory
100 SFNTDirectory tempDir;
101
102 size_t numRead = fread(&tempDir, sizeof tempDir, 1, fFile);
103 (void)numRead;
104
105 le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry));
106 const LETag headTag = LE_HEAD_TABLE_TAG;
107 const LETag hheaTag = LE_HHEA_TABLE_TAG;
108 const HEADTable *headTable = nullptr;
109 const HHEATable *hheaTable = nullptr;
110 // const NAMETable *nameTable = nullptr;
111 le_uint16 numTables = 0;
112
113 fDirectory = reinterpret_cast<const SFNTDirectory*>(LE_NEW_ARRAY(char, dirSize));
114
115 if (fDirectory == nullptr) {
116 printf("%s:%d: %s: malloc err\n", __FILE__, __LINE__, fileName);
117 status = LE_MEMORY_ALLOCATION_ERROR;
118 goto error_exit;
119 }
120
121 fseek(fFile, 0L, SEEK_SET);
122 numRead = fread((void *) fDirectory, sizeof(char), dirSize, fFile);
123
124 //
125 // We calculate these numbers 'cause some fonts
126 // have bogus values for them in the directory header.
127 //
128 numTables = SWAPW(fDirectory->numTables);
129 fDirPower = 1 << highBit(numTables);
130 fDirExtra = numTables - fDirPower;
131
132 // read unitsPerEm from 'head' table
133 headTable = static_cast<const HEADTable*>(readFontTable(headTag));
134
135 if (headTable == nullptr) {
136 status = LE_MISSING_FONT_TABLE_ERROR;
137 printf("%s:%d: %s: missing head table\n", __FILE__, __LINE__, fileName);
138 goto error_exit;
139 }
140
141 fUnitsPerEM = SWAPW(headTable->unitsPerEm);
142 fFontChecksum = SWAPL(headTable->checksumAdjustment);
143 freeFontTable(headTable);
144
145 //nameTable = (NAMETable *) readFontTable(nameTag);
146
147 //if (nameTable == nullptr) {
148 // status = LE_MISSING_FONT_TABLE_ERROR;
149 // goto error_exit;
150 //}
151
152 //fFontVersionString = findName(nameTable, NAME_VERSION_STRING, PLATFORM_MACINTOSH, MACINTOSH_ROMAN, MACINTOSH_ENGLISH);
153
154 //if (fFontVersionString == nullptr) {
155 // status = LE_MISSING_FONT_TABLE_ERROR;
156 // goto error_exit;
157 //}
158
159 //freeFontTable(nameTable);
160
161 hheaTable = (HHEATable *) readFontTable(hheaTag);
162
163 if (hheaTable == nullptr) {
164 printf("%s:%d: %s: missing hhea table\n", __FILE__, __LINE__, fileName);
165 status = LE_MISSING_FONT_TABLE_ERROR;
166 goto error_exit;
167 }
168
169 fAscent = static_cast<le_int32>(yUnitsToPoints(static_cast<float>(SWAPW(hheaTable->ascent))));
170 fDescent = static_cast<le_int32>(yUnitsToPoints(static_cast<float>(SWAPW(hheaTable->descent))));
171 fLeading = static_cast<le_int32>(yUnitsToPoints(static_cast<float>(SWAPW(hheaTable->lineGap))));
172
173 fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics);
174
175 freeFontTable((void *) hheaTable);
176
177 fCMAPMapper = findUnicodeMapper();
178
179 if (fCMAPMapper == nullptr) {
180 printf("%s:%d: %s: can't load cmap\n", __FILE__, __LINE__, fileName);
181 status = LE_MISSING_FONT_TABLE_ERROR;
182 goto error_exit;
183 }
184
185 return;
186
187 error_exit:
188 fclose(fFile);
189 fFile = nullptr;
190 }
191
~PortableFontInstance()192 PortableFontInstance::~PortableFontInstance()
193 {
194 if (fFile != nullptr) {
195 fclose(fFile);
196
197 freeFontTable(fHMTXTable);
198 freeFontTable(fNAMETable);
199
200 delete fCMAPMapper;
201
202 LE_DELETE_ARRAY(fDirectory);
203 }
204 }
205
findTable(LETag tag) const206 const DirectoryEntry *PortableFontInstance::findTable(LETag tag) const
207 {
208 if (fDirectory != nullptr) {
209 le_uint16 table = 0;
210 le_uint16 probe = fDirPower;
211
212 if (SWAPL(fDirectory->tableDirectory[fDirExtra].tag) <= tag) {
213 table = fDirExtra;
214 }
215
216 while (probe > (1 << 0)) {
217 probe >>= 1;
218
219 if (SWAPL(fDirectory->tableDirectory[table + probe].tag) <= tag) {
220 table += probe;
221 }
222 }
223
224 if (SWAPL(fDirectory->tableDirectory[table].tag) == tag) {
225 return &fDirectory->tableDirectory[table];
226 }
227 }
228
229 return nullptr;
230 }
231
readTable(LETag tag,le_uint32 * length) const232 const void *PortableFontInstance::readTable(LETag tag, le_uint32 *length) const
233 {
234 const DirectoryEntry *entry = findTable(tag);
235
236 if (entry == nullptr) {
237 *length = 0;
238 return nullptr;
239 }
240
241 *length = SWAPL(entry->length);
242
243 void *table = LE_NEW_ARRAY(char, *length);
244
245 if (table != nullptr) {
246 fseek(fFile, SWAPL(entry->offset), SEEK_SET);
247 size_t numRead = fread(table, sizeof(char), *length, fFile);
248 (void)numRead;
249 }
250
251 return table;
252 }
253
getFontTable(LETag tableTag,size_t & length) const254 const void *PortableFontInstance::getFontTable(LETag tableTag, size_t &length) const
255 {
256 return FontTableCache::find(tableTag, length);
257 }
258
readFontTable(LETag tableTag,size_t & length) const259 const void *PortableFontInstance::readFontTable(LETag tableTag, size_t &length) const
260 {
261 le_uint32 len;
262
263 const void *data= readTable(tableTag, &len);
264 length = len;
265 //char tag5[5];
266 //printf("Read %s, result %p #%d\n", letagToStr(tableTag,tag5), data,len);
267 return data;
268 }
269
findUnicodeMapper()270 CMAPMapper *PortableFontInstance::findUnicodeMapper()
271 {
272 LETag cmapTag = LE_CMAP_TABLE_TAG;
273 const CMAPTable *cmap = (CMAPTable *) readFontTable(cmapTag);
274
275 if (cmap == nullptr) {
276 return nullptr;
277 }
278
279 return CMAPMapper::createUnicodeMapper(cmap);
280 }
281
getNameString(le_uint16 nameID,le_uint16 platformID,le_uint16 encodingID,le_uint16 languageID) const282 const char *PortableFontInstance::getNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
283 {
284 if (fNAMETable == nullptr) {
285 LETag nameTag = LE_NAME_TABLE_TAG;
286 PortableFontInstance* realThis = const_cast<PortableFontInstance*>(this);
287
288 realThis->fNAMETable = static_cast<const NAMETable*>(readFontTable(nameTag));
289
290 if (realThis->fNAMETable != nullptr) {
291 realThis->fNameCount = SWAPW(realThis->fNAMETable->count);
292 realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
293 }
294 }
295
296 for(le_int32 i = 0; i < fNameCount; i += 1) {
297 const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
298
299 if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
300 SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
301 char *name = ((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset);
302 le_uint16 length = SWAPW(nameRecord->length);
303 char *result = LE_NEW_ARRAY(char, length + 2);
304
305 LE_ARRAY_COPY(result, name, length);
306 result[length] = result[length + 1] = 0;
307
308 return result;
309 }
310 }
311
312 return nullptr;
313 }
314
getUnicodeNameString(le_uint16 nameID,le_uint16 platformID,le_uint16 encodingID,le_uint16 languageID) const315 const LEUnicode16 *PortableFontInstance::getUnicodeNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
316 {
317 if (fNAMETable == nullptr) {
318 LETag nameTag = LE_NAME_TABLE_TAG;
319 PortableFontInstance* realThis = const_cast<PortableFontInstance*>(this);
320
321 realThis->fNAMETable = static_cast<const NAMETable*>(readFontTable(nameTag));
322
323 if (realThis->fNAMETable != nullptr) {
324 realThis->fNameCount = SWAPW(realThis->fNAMETable->count);
325 realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
326 }
327 }
328
329 for(le_int32 i = 0; i < fNameCount; i += 1) {
330 const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
331
332 if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
333 SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
334 const LEUnicode16* name = reinterpret_cast<const LEUnicode16*>(reinterpret_cast<const char*>(fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset));
335 le_uint16 length = SWAPW(nameRecord->length) / 2;
336 LEUnicode16 *result = LE_NEW_ARRAY(LEUnicode16, length + 2);
337
338 for (le_int32 c = 0; c < length; c += 1) {
339 result[c] = SWAPW(name[c]);
340 }
341
342 result[length] = 0;
343
344 return result;
345 }
346 }
347
348 return nullptr;
349 }
350
deleteNameString(const char * name) const351 void PortableFontInstance::deleteNameString(const char *name) const
352 {
353 LE_DELETE_ARRAY(name);
354 }
355
deleteNameString(const LEUnicode16 * name) const356 void PortableFontInstance::deleteNameString(const LEUnicode16 *name) const
357 {
358 LE_DELETE_ARRAY(name);
359 }
360
getGlyphAdvance(LEGlyphID glyph,LEPoint & advance) const361 void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
362 {
363 TTGlyphID ttGlyph = static_cast<TTGlyphID>(LE_GET_GLYPH(glyph));
364
365 if (fHMTXTable == nullptr) {
366 LETag maxpTag = LE_MAXP_TABLE_TAG;
367 LETag hmtxTag = LE_HMTX_TABLE_TAG;
368 const MAXPTable *maxpTable = (MAXPTable *) readFontTable(maxpTag);
369 PortableFontInstance* realThis = const_cast<PortableFontInstance*>(this);
370
371 if (maxpTable != nullptr) {
372 realThis->fNumGlyphs = SWAPW(maxpTable->numGlyphs);
373 freeFontTable(maxpTable);
374 }
375
376 realThis->fHMTXTable = static_cast<const HMTXTable*>(readFontTable(hmtxTag));
377 }
378
379 le_uint16 index = ttGlyph;
380
381 if (ttGlyph >= fNumGlyphs || fHMTXTable == nullptr) {
382 advance.fX = advance.fY = 0;
383 return;
384 }
385
386 if (ttGlyph >= fNumLongHorMetrics) {
387 index = fNumLongHorMetrics - 1;
388 }
389
390 advance.fX = xUnitsToPoints(SWAPW(fHMTXTable->hMetrics[index].advanceWidth));
391 advance.fY = 0;
392 }
393
getGlyphPoint(LEGlyphID,le_int32,LEPoint &) const394 le_bool PortableFontInstance::getGlyphPoint(LEGlyphID /*glyph*/, le_int32 /*pointNumber*/, LEPoint &/*point*/) const
395 {
396 return false;
397 }
398
getUnitsPerEM() const399 le_int32 PortableFontInstance::getUnitsPerEM() const
400 {
401 return fUnitsPerEM;
402 }
403
getFontChecksum() const404 le_uint32 PortableFontInstance::getFontChecksum() const
405 {
406 return fFontChecksum;
407 }
408
getRawChecksum() const409 le_uint32 PortableFontInstance::getRawChecksum() const
410 {
411 // how big is it?
412 // fseek(fFile, 0L, SEEK_END);
413 // long size = ftell(fFile);
414 le_int32 chksum = 0;
415 // now, calculate
416 fseek(fFile, 0L, SEEK_SET);
417 int r;
418 while((r = fgetc(fFile)) != EOF) {
419 chksum += r;
420 }
421 return static_cast<le_uint32>(chksum); // cast to signed
422 }
423
getAscent() const424 le_int32 PortableFontInstance::getAscent() const
425 {
426 return fAscent;
427 }
428
getDescent() const429 le_int32 PortableFontInstance::getDescent() const
430 {
431 return fDescent;
432 }
433
getLeading() const434 le_int32 PortableFontInstance::getLeading() const
435 {
436 return fLeading;
437 }
438
439 // We really want to inherit this method from the superclass, but some compilers
440 // issue a warning if we don't implement it...
mapCharToGlyph(LEUnicode32 ch,const LECharMapper * mapper,le_bool filterZeroWidth) const441 LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const
442 {
443 return LEFontInstance::mapCharToGlyph(ch, mapper, filterZeroWidth);
444 }
445
446 // We really want to inherit this method from the superclass, but some compilers
447 // issue a warning if we don't implement it...
mapCharToGlyph(LEUnicode32 ch,const LECharMapper * mapper) const448 LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
449 {
450 return LEFontInstance::mapCharToGlyph(ch, mapper);
451 }
452
mapCharToGlyph(LEUnicode32 ch) const453 LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch) const
454 {
455 return fCMAPMapper->unicodeToGlyph(ch);
456 }
457
getXPixelsPerEm() const458 float PortableFontInstance::getXPixelsPerEm() const
459 {
460 return fPointSize;
461 }
462
getYPixelsPerEm() const463 float PortableFontInstance::getYPixelsPerEm() const
464 {
465 return fPointSize;
466 }
467
getScaleFactorX() const468 float PortableFontInstance::getScaleFactorX() const
469 {
470 return 1.0;
471 }
472
getScaleFactorY() const473 float PortableFontInstance::getScaleFactorY() const
474 {
475 return 1.0;
476 }
477