2 *******************************************************************************
4 * Copyright (C) 1999-2008, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: PortableFontInstance.cpp
10 * created on: 11/22/1999
11 * created by: Eric R. Mader
16 #include "layout/LETypes.h"
17 #include "layout/LEFontInstance.h"
18 #include "layout/LESwaps.h"
20 #include "PortableFontInstance.h"
28 PortableFontInstance::PortableFontInstance(hb_face_t *face, float xScale, float yScale, LEErrorCode &status)
29 : fFace(face), fXScale(xScale), fYScale(yScale), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0),
30 fNAMETable(NULL), fNameCount(0), fNameStringOffset(0), fCMAPMapper(NULL), fHMTXTable(NULL), fNumGlyphs(0), fNumLongHorMetrics(0)
32 if (LE_FAILURE(status)) {
36 const LETag hheaTag = LE_HHEA_TABLE_TAG;
37 const HHEATable *hheaTable = NULL;
39 fUnitsPerEM = hb_face_get_upem (face);
41 hheaTable = (HHEATable *) getFontTable(hheaTag);
43 if (hheaTable == NULL) {
44 status = LE_MISSING_FONT_TABLE_ERROR;
48 fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent));
49 fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent));
50 fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap));
52 fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics);
54 fCMAPMapper = findUnicodeMapper();
56 if (fCMAPMapper == NULL) {
57 status = LE_MISSING_FONT_TABLE_ERROR;
62 PortableFontInstance::~PortableFontInstance()
68 const void *PortableFontInstance::getFontTable(LETag tableTag) const
70 return FontTableCache::find(tableTag);
73 hb_blob_t *PortableFontInstance::readFontTable(LETag tableTag) const
75 return hb_face_reference_table(fFace, tableTag);
78 CMAPMapper *PortableFontInstance::findUnicodeMapper()
80 LETag cmapTag = LE_CMAP_TABLE_TAG;
81 const CMAPTable *cmap = (CMAPTable *) getFontTable(cmapTag);
87 return CMAPMapper::createUnicodeMapper(cmap);
90 const char *PortableFontInstance::getNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
92 if (fNAMETable == NULL) {
93 LETag nameTag = LE_NAME_TABLE_TAG;
94 PortableFontInstance *realThis = (PortableFontInstance *) this;
96 realThis->fNAMETable = (const NAMETable *) getFontTable(nameTag);
98 if (realThis->fNAMETable != NULL) {
99 realThis->fNameCount = SWAPW(realThis->fNAMETable->count);
100 realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
104 for(le_int32 i = 0; i < fNameCount; i += 1) {
105 const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
107 if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
108 SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
109 char *name = ((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset);
110 le_uint16 length = SWAPW(nameRecord->length);
111 char *result = NEW_ARRAY(char, length + 2);
113 ARRAY_COPY(result, name, length);
114 result[length] = result[length + 1] = 0;
123 const LEUnicode16 *PortableFontInstance::getUnicodeNameString(le_uint16 nameID, le_uint16 platformID, le_uint16 encodingID, le_uint16 languageID) const
125 if (fNAMETable == NULL) {
126 LETag nameTag = LE_NAME_TABLE_TAG;
127 PortableFontInstance *realThis = (PortableFontInstance *) this;
129 realThis->fNAMETable = (const NAMETable *) getFontTable(nameTag);
131 if (realThis->fNAMETable != NULL) {
132 realThis->fNameCount = SWAPW(realThis->fNAMETable->count);
133 realThis->fNameStringOffset = SWAPW(realThis->fNAMETable->stringOffset);
137 for(le_int32 i = 0; i < fNameCount; i += 1) {
138 const NameRecord *nameRecord = &fNAMETable->nameRecords[i];
140 if (SWAPW(nameRecord->platformID) == platformID && SWAPW(nameRecord->encodingID) == encodingID &&
141 SWAPW(nameRecord->languageID) == languageID && SWAPW(nameRecord->nameID) == nameID) {
142 LEUnicode16 *name = (LEUnicode16 *) (((char *) fNAMETable) + fNameStringOffset + SWAPW(nameRecord->offset));
143 le_uint16 length = SWAPW(nameRecord->length) / 2;
144 LEUnicode16 *result = NEW_ARRAY(LEUnicode16, length + 2);
146 for (le_int32 c = 0; c < length; c += 1) {
147 result[c] = SWAPW(name[c]);
159 void PortableFontInstance::deleteNameString(const char *name) const
164 void PortableFontInstance::deleteNameString(const LEUnicode16 *name) const
169 void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
171 TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyph);
173 if (fHMTXTable == NULL) {
174 LETag maxpTag = LE_MAXP_TABLE_TAG;
175 LETag hmtxTag = LE_HMTX_TABLE_TAG;
176 const MAXPTable *maxpTable = (MAXPTable *) getFontTable(maxpTag);
177 PortableFontInstance *realThis = (PortableFontInstance *) this;
179 if (maxpTable != NULL) {
180 realThis->fNumGlyphs = SWAPW(maxpTable->numGlyphs);
183 realThis->fHMTXTable = (const HMTXTable *) getFontTable(hmtxTag);
186 le_uint16 index = ttGlyph;
188 if (ttGlyph >= fNumGlyphs || fHMTXTable == NULL) {
189 advance.fX = advance.fY = 0;
193 if (ttGlyph >= fNumLongHorMetrics) {
194 index = fNumLongHorMetrics - 1;
197 advance.fX = xUnitsToPoints(SWAPW(fHMTXTable->hMetrics[index].advanceWidth));
201 le_bool PortableFontInstance::getGlyphPoint(LEGlyphID /*glyph*/, le_int32 /*pointNumber*/, LEPoint &/*point*/) const
206 le_int32 PortableFontInstance::getUnitsPerEM() const
211 le_uint32 PortableFontInstance::getFontChecksum() const
216 le_int32 PortableFontInstance::getAscent() const
221 le_int32 PortableFontInstance::getDescent() const
226 le_int32 PortableFontInstance::getLeading() const
231 // We really want to inherit this method from the superclass, but some compilers
232 // issue a warning if we don't implement it...
233 LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper, le_bool filterZeroWidth) const
235 return LEFontInstance::mapCharToGlyph(ch, mapper, filterZeroWidth);
238 // We really want to inherit this method from the superclass, but some compilers
239 // issue a warning if we don't implement it...
240 LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch, const LECharMapper *mapper) const
242 return LEFontInstance::mapCharToGlyph(ch, mapper);
245 LEGlyphID PortableFontInstance::mapCharToGlyph(LEUnicode32 ch) const
247 return fCMAPMapper->unicodeToGlyph(ch);
250 float PortableFontInstance::getXPixelsPerEm() const
255 float PortableFontInstance::getYPixelsPerEm() const
260 float PortableFontInstance::getScaleFactorX() const
265 float PortableFontInstance::getScaleFactorY() const