sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Symbian implementation of LEFontInstance sl@0: * sl@0: */ sl@0: sl@0: // Symbian includes sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // Icu includes sl@0: #include "layout/LETypes.h" sl@0: #include "layout/LEFontInstance.h" sl@0: #include "layout/LESwaps.h" sl@0: sl@0: #include "SymbianFontInstance.h" sl@0: sl@0: // sl@0: // Finds the high bit by binary searching sl@0: // through the bits in n. sl@0: // sl@0: le_int8 SymbianFontInstance::highBit(le_int32 value) sl@0: { sl@0: if (value <= 0) sl@0: { sl@0: return -32; sl@0: } sl@0: sl@0: le_uint8 bit = 0; sl@0: sl@0: if (value >= 1 << 16) sl@0: { sl@0: value >>= 16; sl@0: bit += 16; sl@0: } sl@0: sl@0: if (value >= 1 << 8) sl@0: { sl@0: value >>= 8; sl@0: bit += 8; sl@0: } sl@0: sl@0: if (value >= 1 << 4) sl@0: { sl@0: value >>= 4; sl@0: bit += 4; sl@0: } sl@0: sl@0: if (value >= 1 << 2) sl@0: { sl@0: value >>= 2; sl@0: bit += 2; sl@0: } sl@0: sl@0: if (value >= 1 << 1) sl@0: { sl@0: value >>= 1; sl@0: bit += 1; sl@0: } sl@0: sl@0: return bit; sl@0: } sl@0: sl@0: // This is used to build a shaper which does not require the rasteriser sl@0: // to support table access. This is required for interworking testing. sl@0: // #define TRUETYPE_EXTENSION_NOT_SUPPORTED sl@0: #ifndef TRUETYPE_EXTENSION_NOT_SUPPORTED sl@0: sl@0: sl@0: SymbianFontInstance::SymbianFontInstance(CBitmapFont *aBitmapFont, sl@0: LEErrorCode &status, le_bool aKeepGlyphOfZWJ) sl@0: : fFile(NULL), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0), sl@0: fDirectory(NULL), fCMAPMapper(NULL), fHMTXTable(0), fNumGlyphs(0), sl@0: fNumLongHorMetrics(0), iFont(aBitmapFont), iKeepGlyphOfZWJ(aKeepGlyphOfZWJ) sl@0: sl@0: { sl@0: // get the OpenFont details sl@0: if (!aBitmapFont->IsOpenFont()) sl@0: { sl@0: // the font supplied was not a Openfont so .. sl@0: status = LE_ILLEGAL_ARGUMENT_ERROR; sl@0: return; sl@0: } sl@0: sl@0: COpenFont* font = aBitmapFont->OpenFont(); sl@0: sl@0: // get the extended interface sl@0: TAny* ext = 0; sl@0: font->ExtendedInterface(KUidOpenFontShapingExtension, ext); sl@0: MOpenFontShapingExtension* extensionInterface sl@0: = reinterpret_cast(ext); sl@0: sl@0: font->ExtendedInterface(KUidOpenFontTrueTypeExtension, ext); sl@0: MOpenFontTrueTypeExtension* trueTypeExtensionInterface sl@0: = reinterpret_cast(ext); sl@0: sl@0: /* Currently if the trueTypeExtensionInterface is not available the sl@0: truetype tables are accessed directly rather than via the rasteriser */ sl@0: if (!extensionInterface /* || !trueTypeExtensionInterface*/) sl@0: { sl@0: // this rasterizer does not support the required interface sl@0: // so do not bother to shape sl@0: status = LE_ILLEGAL_ARGUMENT_ERROR; sl@0: return ; sl@0: } sl@0: sl@0: iTrueTypeExtensionInterface = trueTypeExtensionInterface; sl@0: iExtensionInterface = extensionInterface; sl@0: sl@0: // get the font file name from the COpenFontFile in the COpenFont sl@0: TBuf8 fontFileName; sl@0: fontFileName.Copy(font->File()->FileName()); sl@0: fontFileName.ZeroTerminate(); sl@0: sl@0: /* get the font metrics via the rasterizer */ sl@0: MOpenFontShapingExtension::TExtensionFontMetrics fontMetrics; sl@0: extensionInterface->GetExtensionFontMetrics(fontMetrics); sl@0: sl@0: /* The number of font design units per em. */ sl@0: fUnitsPerEM = fontMetrics.iUnitsPerEm; sl@0: sl@0: /* The size of the font's em square in pixels. */ sl@0: fXPixelsPerEm = fontMetrics.iXPixelsPerEm; sl@0: fYPixelsPerEm = fontMetrics.iYPixelsPerEm; sl@0: sl@0: /* Scaling factors, pixels per font unit. */ sl@0: fDeviceScaleY = fontMetrics.iYScaleFactor; sl@0: fDeviceScaleX = fontMetrics.iXScaleFactor; sl@0: sl@0: // open the font file sl@0: fFile = fopen( (char *)fontFileName.Ptr(), "rb"); sl@0: if (fFile == 0) sl@0: { sl@0: status = LE_FONT_FILE_NOT_FOUND_ERROR; sl@0: return; sl@0: } sl@0: sl@0: // read in the directory sl@0: SFNTDirectory tempDir; sl@0: sl@0: fread(&tempDir, sizeof tempDir, 1, fFile); sl@0: sl@0: le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry)); sl@0: const LETag headTag = LE_HEAD_TABLE_TAG; sl@0: const LETag hheaTag = LE_HHEA_TABLE_TAG; sl@0: const HEADTable *headTable = NULL; sl@0: const HHEATable *hheaTable = NULL; sl@0: le_uint16 numTables = 0; sl@0: sl@0: //coverity[incorrect_multiplication] sl@0: //coverity[buffer_alloc] sl@0: // dirSize is The actually sizoe of fDirectory which indeed contains more data thant SFNTDirectory defined. sl@0: fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize); sl@0: sl@0: if (fDirectory == NULL) sl@0: { sl@0: status = LE_MEMORY_ALLOCATION_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: fseek(fFile, 0L, SEEK_SET); sl@0: fread((void *) fDirectory, sizeof(char), dirSize, fFile); sl@0: sl@0: // sl@0: // We calculate these numbers 'cause some fonts sl@0: // have bogus values for them in the directory header. sl@0: // sl@0: numTables = SWAPW(fDirectory->numTables); sl@0: fDirPower = 1 << highBit(numTables); sl@0: fDirExtra = numTables - fDirPower; sl@0: sl@0: // read unitsPerEm from 'head' table sl@0: headTable = (const HEADTable *) readFontTable(headTag); sl@0: sl@0: if (headTable == NULL) sl@0: { sl@0: status = LE_MISSING_FONT_TABLE_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: fUnitsPerEM = SWAPW(headTable->unitsPerEm); sl@0: deleteTable(headTable); sl@0: sl@0: hheaTable = (HHEATable *) readFontTable(hheaTag); sl@0: sl@0: if (hheaTable == NULL) sl@0: { sl@0: status = LE_MISSING_FONT_TABLE_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent)); sl@0: fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent)); sl@0: fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap)); sl@0: sl@0: fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics); sl@0: sl@0: deleteTable((void *) hheaTable); sl@0: sl@0: fCMAPMapper = findUnicodeMapper(); sl@0: sl@0: if (fCMAPMapper == NULL) sl@0: { sl@0: status = LE_MISSING_FONT_TABLE_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: return; sl@0: sl@0: error_exit: sl@0: fclose(fFile); sl@0: fFile = NULL; sl@0: return; sl@0: } sl@0: sl@0: SymbianFontInstance::~SymbianFontInstance() sl@0: { sl@0: if (fFile != NULL) sl@0: { sl@0: fclose(fFile); sl@0: deleteTable(fHMTXTable); sl@0: delete fCMAPMapper; sl@0: LE_DELETE_ARRAY(fDirectory); sl@0: } sl@0: }; sl@0: sl@0: #else sl@0: /* read the font directly. This is used if rasterizers do not support sl@0: the trueTypeExtensionInterface */ sl@0: sl@0: SymbianFontInstance::SymbianFontInstance(CBitmapFont *aBitmapFont, sl@0: LEErrorCode &status) sl@0: : fFile(NULL), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0), sl@0: fDirectory(NULL), fCMAPMapper(NULL), fHMTXTable(0), fNumGlyphs(0), sl@0: fNumLongHorMetrics(0), iFont(aBitmapFont) sl@0: sl@0: { sl@0: // get the OpenFont details sl@0: if (!aBitmapFont->IsOpenFont()) sl@0: { sl@0: // the font supplied was not a Openfont so .. sl@0: status = LE_ILLEGAL_ARGUMENT_ERROR; sl@0: return; sl@0: } sl@0: sl@0: COpenFont* font = aBitmapFont->OpenFont(); sl@0: sl@0: // get the extended interface sl@0: TAny* ext = 0; sl@0: font->ExtendedInterface(KUidOpenFontShapingExtension, ext); sl@0: iExtensionInterface = reinterpret_cast(ext); sl@0: sl@0: // do not use the true type interface sl@0: iTrueTypeExtensionInterface = 0; sl@0: sl@0: if (!iExtensionInterface ) sl@0: { sl@0: // this rasterizer does not support the required interface sl@0: // so do not bother to shape sl@0: status = LE_ILLEGAL_ARGUMENT_ERROR; sl@0: return ; sl@0: } sl@0: sl@0: // get the font file name from the COpenFontFile in the COpenFont sl@0: TBuf8 fontFileName; sl@0: fontFileName.Copy(font->File()->FileName()); sl@0: fontFileName.ZeroTerminate(); sl@0: sl@0: /* get the font metrics via the rasterizer */ sl@0: MOpenFontShapingExtension::TExtensionFontMetrics fontMetrics; sl@0: iExtensionInterface->GetExtensionFontMetrics(fontMetrics); sl@0: sl@0: /* The number of font design units per em. */ sl@0: fUnitsPerEM = fontMetrics.iUnitsPerEm; sl@0: sl@0: /* The size of the font's em square in pixels. */ sl@0: fXPixelsPerEm = fontMetrics.iXPixelsPerEm; sl@0: fYPixelsPerEm = fontMetrics.iYPixelsPerEm; sl@0: sl@0: /* Scaling factors, pixels per font unit. */ sl@0: fDeviceScaleY = fontMetrics.iYScaleFactor; sl@0: fDeviceScaleX = fontMetrics.iXScaleFactor; sl@0: sl@0: // open the font file sl@0: fFile = fopen( (char *)fontFileName.Ptr(), "rb"); sl@0: if (fFile == 0) sl@0: { sl@0: status = LE_FONT_FILE_NOT_FOUND_ERROR; sl@0: return; sl@0: } sl@0: sl@0: // read in the directory sl@0: SFNTDirectory tempDir; sl@0: sl@0: fread(&tempDir, sizeof tempDir, 1, fFile); sl@0: sl@0: le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry)); sl@0: const LETag headTag = LE_HEAD_TABLE_TAG; sl@0: const LETag hheaTag = LE_HHEA_TABLE_TAG; sl@0: const HEADTable *headTable = NULL; sl@0: const HHEATable *hheaTable = NULL; sl@0: le_uint16 numTables = 0; sl@0: sl@0: fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize); sl@0: sl@0: if (fDirectory == NULL) sl@0: { sl@0: status = LE_MEMORY_ALLOCATION_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: fseek(fFile, 0L, SEEK_SET); sl@0: fread((void *) fDirectory, sizeof(char), dirSize, fFile); sl@0: sl@0: // sl@0: // We calculate these numbers 'cause some fonts sl@0: // have bogus values for them in the directory header. sl@0: // sl@0: numTables = SWAPW(fDirectory->numTables); sl@0: fDirPower = 1 << highBit(numTables); sl@0: fDirExtra = numTables - fDirPower; sl@0: sl@0: // read unitsPerEm from 'head' table sl@0: headTable = (const HEADTable *) readFontTable(headTag); sl@0: sl@0: if (headTable == NULL) sl@0: { sl@0: status = LE_MISSING_FONT_TABLE_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: fUnitsPerEM = SWAPW(headTable->unitsPerEm); sl@0: deleteTable(headTable); sl@0: sl@0: hheaTable = (HHEATable *) readFontTable(hheaTag); sl@0: sl@0: if (hheaTable == NULL) sl@0: { sl@0: status = LE_MISSING_FONT_TABLE_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent)); sl@0: fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent)); sl@0: fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap)); sl@0: sl@0: fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics); sl@0: sl@0: deleteTable((void *) hheaTable); sl@0: sl@0: fCMAPMapper = findUnicodeMapper(); sl@0: sl@0: if (fCMAPMapper == NULL) sl@0: { sl@0: status = LE_MISSING_FONT_TABLE_ERROR; sl@0: goto error_exit; sl@0: } sl@0: sl@0: return; sl@0: sl@0: error_exit: sl@0: fclose(fFile); sl@0: fFile = NULL; sl@0: return; sl@0: } sl@0: sl@0: SymbianFontInstance::~SymbianFontInstance() sl@0: { sl@0: if (fFile != NULL) sl@0: { sl@0: fclose(fFile); sl@0: sl@0: deleteTable(fHMTXTable); sl@0: sl@0: delete fCMAPMapper; sl@0: sl@0: LE_DELETE_ARRAY(fDirectory); sl@0: } sl@0: }; sl@0: sl@0: #endif sl@0: sl@0: sl@0: void SymbianFontInstance::deleteTable(const void *table) const sl@0: { sl@0: LE_DELETE_ARRAY(table); sl@0: } sl@0: sl@0: const DirectoryEntry *SymbianFontInstance::findTable(LETag tag) const sl@0: { sl@0: if (fDirectory != NULL) sl@0: { sl@0: le_uint16 table = 0; sl@0: le_uint16 probe = fDirPower; sl@0: sl@0: if (SWAPL(fDirectory->tableDirectory[fDirExtra].tag) <= tag) sl@0: { sl@0: table = fDirExtra; sl@0: } sl@0: sl@0: while (probe > (1 << 0)) sl@0: { sl@0: probe >>= 1; sl@0: sl@0: if (SWAPL(fDirectory->tableDirectory[table + probe].tag) <= tag) sl@0: { sl@0: table += probe; sl@0: } sl@0: } sl@0: sl@0: if (SWAPL(fDirectory->tableDirectory[table].tag) == tag) sl@0: { sl@0: return &fDirectory->tableDirectory[table]; sl@0: } sl@0: } sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: const void *SymbianFontInstance::readTable(LETag aTag, le_uint32 *aLength) const sl@0: { sl@0: void *table = NULL; sl@0: sl@0: /* If the current rasteriser supports the TrueTypeExtensionInterface sl@0: use it to access the true Type tables */ sl@0: if (iTrueTypeExtensionInterface) sl@0: { sl@0: TInt error; sl@0: TInt length; sl@0: TAny * trueTypeTable = iTrueTypeExtensionInterface-> sl@0: GetTrueTypeTable(error, aTag, &length); sl@0: sl@0: if (!trueTypeTable) sl@0: { sl@0: *aLength = 0; sl@0: return 0; sl@0: } sl@0: sl@0: if (aLength) sl@0: *aLength = length; sl@0: sl@0: // allocate some memory for the table & copy table into it sl@0: table = LE_NEW_ARRAY(char, length); sl@0: if (table) sl@0: { sl@0: // copy the table from the rasteriser sl@0: Mem::Copy(table, (char *) trueTypeTable, length); sl@0: } sl@0: sl@0: iTrueTypeExtensionInterface->ReleaseTrueTypeTable(trueTypeTable); sl@0: } sl@0: else sl@0: { sl@0: // old method, read the table directly sl@0: const DirectoryEntry *entry = findTable(aTag); sl@0: sl@0: if (entry == NULL) sl@0: { sl@0: if (aLength) sl@0: *aLength = 0; sl@0: return NULL; sl@0: } sl@0: sl@0: TInt length = SWAPL(entry->length); sl@0: if (aLength) sl@0: *aLength = length; sl@0: sl@0: table = LE_NEW_ARRAY(char, length); sl@0: sl@0: if (table != NULL) sl@0: { sl@0: fseek(fFile, SWAPL(entry->offset), SEEK_SET); sl@0: fread(table, sizeof(char), length, fFile); sl@0: } sl@0: } sl@0: sl@0: return table; sl@0: } sl@0: sl@0: const void *SymbianFontInstance::getFontTable(LETag tableTag) const sl@0: { sl@0: if (iTrueTypeExtensionInterface) sl@0: { sl@0: TInt error; sl@0: return iTrueTypeExtensionInterface->GetTrueTypeTable(error, tableTag, 0); sl@0: } sl@0: return FontTableCache::find(tableTag); sl@0: } sl@0: sl@0: const void *SymbianFontInstance::readFontTable(LETag tableTag) const sl@0: { sl@0: return readTable(tableTag, 0); sl@0: } sl@0: sl@0: CMAPMapper *SymbianFontInstance::findUnicodeMapper() sl@0: { sl@0: LETag cmapTag = LE_CMAP_TABLE_TAG; sl@0: const CMAPTable *cmap = (CMAPTable *) readFontTable(cmapTag); sl@0: sl@0: return cmap ? CMAPMapper::createUnicodeMapper(cmap) : NULL; sl@0: } sl@0: sl@0: void SymbianFontInstance::getGlyphAdvance( sl@0: LEGlyphID aGlyph, LEPoint &aAdvance) const sl@0: { sl@0: TInt glyph = LE_GET_GLYPH(aGlyph) | 0x80000000; sl@0: TOpenFontCharMetrics metrics; sl@0: const TUint8* bitmap = 0; sl@0: sl@0: // If an FFFF glyph code is received, avoid trying to rasterize it as there sl@0: // is no character data associated with FFFF. sl@0: // This will avoid the overhead of trying to rasterize it. sl@0: if (glyph == 0x8000FFFF) sl@0: { sl@0: aAdvance.fX = 0; sl@0: aAdvance.fY = 0; sl@0: return; sl@0: } sl@0: sl@0: if (!iFont->GetCharacterData(iSessionHandle, glyph, metrics, bitmap)) sl@0: { sl@0: // Glyph not yet rasterized; rasterize it ourselves sl@0: iFont->Rasterize(iSessionHandle, glyph, 0); sl@0: if (!iFont->GetCharacterData(iSessionHandle, glyph, metrics, bitmap)) sl@0: { sl@0: aAdvance.fX = 0; sl@0: aAdvance.fY = 0; sl@0: return; sl@0: } sl@0: } sl@0: aAdvance.fX = metrics.HorizAdvance(); sl@0: aAdvance.fY = 0; sl@0: return; sl@0: } sl@0: sl@0: le_bool SymbianFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const sl@0: { sl@0: TReal x; sl@0: TReal y; sl@0: if (!iExtensionInterface->GlyphPointInHintedPixels( sl@0: glyph, pointNumber, x, y )) sl@0: return FALSE; sl@0: point.fX = x; sl@0: point.fY = y; sl@0: return TRUE; sl@0: } sl@0: sl@0: void SymbianFontInstance::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const sl@0: { sl@0: pixels.fX = xFunits / fUnitsPerEM * fXPixelsPerEm; sl@0: pixels.fY = yFunits / fUnitsPerEM * fYPixelsPerEm; sl@0: } sl@0: sl@0: float SymbianFontInstance::xUnitsToPoints(float xUnits) const sl@0: { sl@0: // Seems like this function should have been called xUnitsToPixels?! sl@0: return xUnits / fUnitsPerEM * fXPixelsPerEm; sl@0: } sl@0: sl@0: float SymbianFontInstance::yUnitsToPoints(float yUnits) const sl@0: { sl@0: // Seems like this function should have been called yUnitsToPixels?! sl@0: return yUnits / fUnitsPerEM * fYPixelsPerEm; sl@0: } sl@0: sl@0: float SymbianFontInstance::getXPixelsPerEm() const sl@0: { sl@0: return fXPixelsPerEm; sl@0: } sl@0: sl@0: float SymbianFontInstance::getYPixelsPerEm() const sl@0: { sl@0: return fYPixelsPerEm; sl@0: } sl@0: sl@0: float SymbianFontInstance::getScaleFactorX() const sl@0: { sl@0: return fDeviceScaleX; sl@0: } sl@0: sl@0: float SymbianFontInstance::getScaleFactorY() const sl@0: { sl@0: return fDeviceScaleY; sl@0: }