]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/letest/PortableFontInstance.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / test / letest / PortableFontInstance.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4 *******************************************************************************
5 *
b331163b 6 * Copyright (C) 1999-2015, International Business Machines
b75a7d8f
A
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
57a6839d 24//#include "letest.h"
b75a7d8f
A
25#include "sfnt.h"
26
27#include <string.h>
57a6839d
A
28#include <stdio.h>
29
30#if 0
31static 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
b75a7d8f
A
40
41//
42// Finds the high bit by binary searching
43// through the bits in n.
44//
45le_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
73c04bcf
A
81PortableFontInstance::PortableFontInstance(const char *fileName, float pointSize, LEErrorCode &status)
82 : fFile(NULL), fPointSize(pointSize), fUnitsPerEM(0), fFontChecksum(0), fAscent(0), fDescent(0), fLeading(0),
83 fDirectory(NULL), fNAMETable(NULL), fNameCount(0), fNameStringOffset(0), fCMAPMapper(NULL), fHMTXTable(NULL), fNumGlyphs(0), fNumLongHorMetrics(0)
b75a7d8f
A
84{
85 if (LE_FAILURE(status)) {
86 return;
87 }
88
89 // open the font file
90 fFile = fopen(fileName, "rb");
57a6839d 91 //printf("Open Font: %s\n", fileName);
b75a7d8f
A
92
93 if (fFile == NULL) {
57a6839d 94 printf("%s:%d: %s: FNF\n", __FILE__, __LINE__, fileName);
b75a7d8f
A
95 status = LE_FONT_FILE_NOT_FOUND_ERROR;
96 return;
97 }
98
99 // read in the directory
100 SFNTDirectory tempDir;
101
b331163b
A
102 size_t numRead = fread(&tempDir, sizeof tempDir, 1, fFile);
103 (void)numRead;
b75a7d8f
A
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 = NULL;
109 const HHEATable *hheaTable = NULL;
73c04bcf 110// const NAMETable *nameTable = NULL;
b75a7d8f
A
111 le_uint16 numTables = 0;
112
57a6839d 113 fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize);
b75a7d8f
A
114
115 if (fDirectory == NULL) {
57a6839d 116 printf("%s:%d: %s: malloc err\n", __FILE__, __LINE__, fileName);
b75a7d8f
A
117 status = LE_MEMORY_ALLOCATION_ERROR;
118 goto error_exit;
119 }
120
121 fseek(fFile, 0L, SEEK_SET);
b331163b 122 numRead = fread((void *) fDirectory, sizeof(char), dirSize, fFile);
b75a7d8f
A
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 = (const HEADTable *) readFontTable(headTag);
134
135 if (headTable == NULL) {
136 status = LE_MISSING_FONT_TABLE_ERROR;
57a6839d 137 printf("%s:%d: %s: missing head table\n", __FILE__, __LINE__, fileName);
b75a7d8f
A
138 goto error_exit;
139 }
140
73c04bcf
A
141 fUnitsPerEM = SWAPW(headTable->unitsPerEm);
142 fFontChecksum = SWAPL(headTable->checksumAdjustment);
46f4442e 143 freeFontTable(headTable);
b75a7d8f 144
73c04bcf
A
145 //nameTable = (NAMETable *) readFontTable(nameTag);
146
147 //if (nameTable == NULL) {
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 == NULL) {
155 // status = LE_MISSING_FONT_TABLE_ERROR;
156 // goto error_exit;
157 //}
158
46f4442e 159 //freeFontTable(nameTable);
73c04bcf 160
b75a7d8f
A
161 hheaTable = (HHEATable *) readFontTable(hheaTag);
162
163 if (hheaTable == NULL) {
57a6839d 164 printf("%s:%d: %s: missing hhea table\n", __FILE__, __LINE__, fileName);
b75a7d8f
A
165 status = LE_MISSING_FONT_TABLE_ERROR;
166 goto error_exit;
167 }
168
169 fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent));
170 fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent));
171 fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap));
172
173 fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics);
174
46f4442e 175 freeFontTable((void *) hheaTable);
b75a7d8f
A
176
177 fCMAPMapper = findUnicodeMapper();
178
179 if (fCMAPMapper == NULL) {
57a6839d 180 printf("%s:%d: %s: can't load cmap\n", __FILE__, __LINE__, fileName);
b75a7d8f
A
181 status = LE_MISSING_FONT_TABLE_ERROR;
182 goto error_exit;
183 }
184
185 return;
186
187error_exit:
188 fclose(fFile);
189 fFile = NULL;
190 return;
191}
192
193PortableFontInstance::~PortableFontInstance()
194{
195 if (fFile != NULL) {
196 fclose(fFile);
197
46f4442e
A
198 freeFontTable(fHMTXTable);
199 freeFontTable(fNAMETable);
b75a7d8f
A
200
201 delete fCMAPMapper;
202
57a6839d 203 LE_DELETE_ARRAY(fDirectory);
b75a7d8f 204 }
73c04bcf 205}
b75a7d8f 206
b75a7d8f
A
207const DirectoryEntry *PortableFontInstance::findTable(LETag tag) const
208{
209 if (fDirectory != NULL) {
210 le_uint16 table = 0;
211 le_uint16 probe = fDirPower;
212
213 if (SWAPL(fDirectory->tableDirectory[fDirExtra].tag) <= tag) {
214 table = fDirExtra;
215 }
216
217 while (probe > (1 << 0)) {
218 probe >>= 1;
219
220 if (SWAPL(fDirectory->tableDirectory[table + probe].tag) <= tag) {
221 table += probe;
222 }
223 }
224
225 if (SWAPL(fDirectory->tableDirectory[table].tag) == tag) {
226 return &fDirectory->tableDirectory[table];
227 }
228 }
229
230 return NULL;
231}
232
233const void *PortableFontInstance::readTable(LETag tag, le_uint32 *length) const
234{
235 const DirectoryEntry *entry = findTable(tag);
236
237 if (entry == NULL) {
238 *length = 0;
239 return NULL;
240 }
241
242 *length = SWAPL(entry->length);
243
57a6839d 244 void *table = LE_NEW_ARRAY(char, *length);
b75a7d8f
A
245
246 if (table != NULL) {
247 fseek(fFile, SWAPL(entry->offset), SEEK_SET);
b331163b
A
248 size_t numRead = fread(table, sizeof(char), *length, fFile);
249 (void)numRead;
b75a7d8f
A
250 }
251
252 return table;
253}
254
57a6839d
A
255const void *PortableFontInstance::getFontTable(LETag tableTag, size_t &length) const
256{
257 return FontTableCache::find(tableTag, length);
258}
259
260const void *PortableFontInstance::readFontTable(LETag tableTag, size_t &length) const
b75a7d8f
A
261{
262 le_uint32 len;
263
57a6839d
A
264 const void *data= readTable(tableTag, &len);
265 length = len;
266 //char tag5[5];
267 //printf("Read %s, result %p #%d\n", letagToStr(tableTag,tag5), data,len);
268 return data;
b75a7d8f
A
269}
270
271CMAPMapper *PortableFontInstance::findUnicodeMapper()
272{
273 LETag cmapTag = LE_CMAP_TABLE_TAG;
274 const CMAPTable *cmap = (CMAPTable *) readFontTable(cmapTag);
275
276 if (cmap == NULL) {
277 return NULL;
278 }
279
280 return CMAPMapper::createUnicodeMapper(cmap);
281}
282
73c04bcf
A
283const char *PortableFontInstance::getNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
284{
285 if (fNAMETable == NULL) {
286 LETag nameTag = LE_NAME_TABLE_TAG;
287 PortableFontInstance *realThis = (PortableFontInstance *) this;
288
289 realThis->fNAMETable = (const NAMETable *) readFontTable(nameTag);
290
291 if (realThis->fNAMETable != NULL) {
292 realThis->fNameCount = SWAPW(realThis->fNAMETable->count);
293 realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
294 }
295 }
296
297 for(le_int32 i = 0; i < fNameCount; i += 1) {
298 const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
299
46f4442e 300 if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
73c04bcf
A
301 SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
302 char *name = ((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset);
303 le_uint16 length = SWAPW(nameRecord->length);
57a6839d 304 char *result = LE_NEW_ARRAY(char, length + 2);
73c04bcf 305
57a6839d 306 LE_ARRAY_COPY(result, name, length);
73c04bcf
A
307 result[length] = result[length + 1] = 0;
308
309 return result;
310 }
311 }
312
313 return NULL;
314}
315
46f4442e
A
316const LEUnicode16 *PortableFontInstance::getUnicodeNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
317{
318 if (fNAMETable == NULL) {
319 LETag nameTag = LE_NAME_TABLE_TAG;
320 PortableFontInstance *realThis = (PortableFontInstance *) this;
321
322 realThis->fNAMETable = (const NAMETable *) readFontTable(nameTag);
323
324 if (realThis->fNAMETable != NULL) {
325 realThis->fNameCount = SWAPW(realThis->fNAMETable->count);
326 realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
327 }
328 }
329
330 for(le_int32 i = 0; i < fNameCount; i += 1) {
331 const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
332
333 if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
334 SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
335 LEUnicode16 *name = (LEUnicode16 *) (((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset));
336 le_uint16 length = SWAPW(nameRecord->length) / 2;
57a6839d 337 LEUnicode16 *result = LE_NEW_ARRAY(LEUnicode16, length + 2);
46f4442e
A
338
339 for (le_int32 c = 0; c < length; c += 1) {
340 result[c] = SWAPW(name[c]);
341 }
342
343 result[length] = 0;
344
345 return result;
346 }
347 }
348
349 return NULL;
350}
351
73c04bcf
A
352void PortableFontInstance::deleteNameString(const char *name) const
353{
57a6839d 354 LE_DELETE_ARRAY(name);
73c04bcf 355}
b75a7d8f 356
46f4442e
A
357void PortableFontInstance::deleteNameString(const LEUnicode16 *name) const
358{
57a6839d 359 LE_DELETE_ARRAY(name);
46f4442e
A
360}
361
b75a7d8f
A
362void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
363{
364 TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyph);
365
366 if (fHMTXTable == NULL) {
367 LETag maxpTag = LE_MAXP_TABLE_TAG;
368 LETag hmtxTag = LE_HMTX_TABLE_TAG;
369 const MAXPTable *maxpTable = (MAXPTable *) readFontTable(maxpTag);
370 PortableFontInstance *realThis = (PortableFontInstance *) this;
371
372 if (maxpTable != NULL) {
373 realThis->fNumGlyphs = SWAPW(maxpTable->numGlyphs);
46f4442e 374 freeFontTable(maxpTable);
b75a7d8f
A
375 }
376
377 realThis->fHMTXTable = (const HMTXTable *) readFontTable(hmtxTag);
378 }
379
380 le_uint16 index = ttGlyph;
381
382 if (ttGlyph >= fNumGlyphs || fHMTXTable == NULL) {
383 advance.fX = advance.fY = 0;
384 return;
385 }
386
387 if (ttGlyph >= fNumLongHorMetrics) {
388 index = fNumLongHorMetrics - 1;
389 }
390
391 advance.fX = xUnitsToPoints(SWAPW(fHMTXTable->hMetrics[index].advanceWidth));
392 advance.fY = 0;
393}
394
73c04bcf 395le_bool PortableFontInstance::getGlyphPoint(LEGlyphID /*glyph*/, le_int32 /*pointNumber*/, LEPoint &/*point*/) const
b75a7d8f 396{
374ca955 397 return FALSE;
b75a7d8f
A
398}
399
73c04bcf
A
400le_int32 PortableFontInstance::getUnitsPerEM() const
401{
402 return fUnitsPerEM;
403}
404
405le_uint32 PortableFontInstance::getFontChecksum() const
406{
407 return fFontChecksum;
408}
409
57a6839d
A
410le_uint32 PortableFontInstance::getRawChecksum() const
411{
412 // how big is it?
413 // fseek(fFile, 0L, SEEK_END);
414 // long size = ftell(fFile);
415 le_int32 chksum = 0;
416 // now, calculate
417 fseek(fFile, 0L, SEEK_SET);
418 int r;
419 int count =0;
420 while((r = fgetc(fFile)) != EOF) {
421 chksum += r;
422 count ++;
423 }
424 return (le_uint32) chksum; // cast to signed
425}
426
73c04bcf
A
427le_int32 PortableFontInstance::getAscent() const
428{
429 return fAscent;
430}
431
432le_int32 PortableFontInstance::getDescent() const
433{
434 return fDescent;
435}
436
437le_int32 PortableFontInstance::getLeading() const
438{
439 return fLeading;
440}
441
442// We really want to inherit this method from the superclass, but some compilers
443// issue a warning if we don't implement it...
444LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const
445{
446 return LEFontInstance::mapCharToGlyph(ch, mapper, filterZeroWidth);
447}
448
449// We really want to inherit this method from the superclass, but some compilers
450// issue a warning if we don't implement it...
451LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
452{
453 return LEFontInstance::mapCharToGlyph(ch, mapper);
454}
455
456LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch) const
457{
458 return fCMAPMapper->unicodeToGlyph(ch);
459}
460
461float PortableFontInstance::getXPixelsPerEm() const
462{
463 return fPointSize;
464}
465
466float PortableFontInstance::getYPixelsPerEm() const
467{
468 return fPointSize;
469}
470
471float PortableFontInstance::getScaleFactorX() const
472{
473 return 1.0;
474}
475
476float PortableFontInstance::getScaleFactorY() const
477{
478 return 1.0;
479}