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