2001-10-03 21:48:10 +00:00
|
|
|
/*
|
2001-11-08 00:22:25 +00:00
|
|
|
*******************************************************************************
|
2001-10-03 21:48:10 +00:00
|
|
|
*
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 1999-2003, International Business Machines
|
2001-11-08 00:22:25 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
2001-10-03 21:48:10 +00:00
|
|
|
*
|
2001-11-08 00:22:25 +00:00
|
|
|
*******************************************************************************
|
|
|
|
* file name: PortableFontInstance.cpp
|
2001-10-03 21:48:10 +00:00
|
|
|
*
|
2001-11-08 00:22:25 +00:00
|
|
|
* created on: 11/22/1999
|
|
|
|
* created by: Eric R. Mader
|
2001-10-03 21:48:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
#include "layout/LETypes.h"
|
|
|
|
#include "layout/LEFontInstance.h"
|
|
|
|
#include "layout/LESwaps.h"
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
#include "PortableFontInstance.h"
|
|
|
|
|
|
|
|
#include "sfnt.h"
|
|
|
|
|
2002-03-27 23:24:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2002-03-20 22:23:14 +00:00
|
|
|
//
|
|
|
|
// Finds the high bit by binary searching
|
|
|
|
// through the bits in n.
|
|
|
|
//
|
|
|
|
le_int8 PortableFontInstance::highBit(le_int32 value)
|
|
|
|
{
|
|
|
|
if (value <= 0) {
|
|
|
|
return -32;
|
|
|
|
}
|
|
|
|
|
|
|
|
le_uint8 bit = 0;
|
|
|
|
|
|
|
|
if (value >= 1 << 16) {
|
|
|
|
value >>= 16;
|
|
|
|
bit += 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value >= 1 << 8) {
|
|
|
|
value >>= 8;
|
|
|
|
bit += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value >= 1 << 4) {
|
|
|
|
value >>= 4;
|
|
|
|
bit += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value >= 1 << 2) {
|
|
|
|
value >>= 2;
|
|
|
|
bit += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value >= 1 << 1) {
|
|
|
|
value >>= 1;
|
|
|
|
bit += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
PortableFontInstance::PortableFontInstance(char *fileName, float pointSize, LEErrorCode &status)
|
|
|
|
: fFile(NULL), fUnitsPerEM(0), fPointSize(pointSize), fAscent(0), fDescent(0), fLeading(0),
|
|
|
|
fDirectory(NULL), fCMAPMapper(NULL), fHMTXTable(NULL), fNumGlyphs(0), fNumLongHorMetrics(0)
|
|
|
|
{
|
2001-10-03 21:48:10 +00:00
|
|
|
if (LE_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
// open the font file
|
|
|
|
fFile = fopen(fileName, "rb");
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (fFile == NULL) {
|
2003-03-03 22:54:57 +00:00
|
|
|
status = LE_FONT_FILE_NOT_FOUND_ERROR;
|
2002-04-02 02:55:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
// read in the directory
|
|
|
|
SFNTDirectory tempDir;
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
fread(&tempDir, sizeof tempDir, 1, fFile);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry));
|
2003-05-20 23:39:30 +00:00
|
|
|
const LETag headTag = LE_HEAD_TABLE_TAG;
|
|
|
|
const LETag hheaTag = LE_HHEA_TABLE_TAG;
|
2002-04-02 02:55:31 +00:00
|
|
|
const HEADTable *headTable = NULL;
|
2003-03-03 22:54:57 +00:00
|
|
|
const HHEATable *hheaTable = NULL;
|
2002-03-20 22:23:14 +00:00
|
|
|
le_uint16 numTables = 0;
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
if (fDirectory == NULL) {
|
2003-03-03 22:54:57 +00:00
|
|
|
status = LE_MEMORY_ALLOCATION_ERROR;
|
2001-10-03 21:48:10 +00:00
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
fseek(fFile, 0L, SEEK_SET);
|
|
|
|
fread((void *) fDirectory, sizeof(char), dirSize, fFile);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-03-20 22:23:14 +00:00
|
|
|
//
|
|
|
|
// We calculate these numbers 'cause some fonts
|
|
|
|
// have bogus values for them in the directory header.
|
|
|
|
//
|
|
|
|
numTables = SWAPW(fDirectory->numTables);
|
|
|
|
fDirPower = 1 << highBit(numTables);
|
|
|
|
fDirExtra = numTables - fDirPower;
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
// read unitsPerEm from 'head' table
|
2003-03-03 22:54:57 +00:00
|
|
|
headTable = (const HEADTable *) readFontTable(headTag);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
if (headTable == NULL) {
|
2003-03-03 22:54:57 +00:00
|
|
|
status = LE_MISSING_FONT_TABLE_ERROR;
|
2001-10-03 21:48:10 +00:00
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
fUnitsPerEM = SWAPW(headTable->unitsPerEm);
|
2002-04-02 02:55:31 +00:00
|
|
|
deleteTable(headTable);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
hheaTable = (HHEATable *) readFontTable(hheaTag);
|
|
|
|
|
|
|
|
if (hheaTable == NULL) {
|
|
|
|
status = LE_MISSING_FONT_TABLE_ERROR;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
2003-05-15 20:42:27 +00:00
|
|
|
fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent));
|
|
|
|
fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent));
|
|
|
|
fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap));
|
2003-03-03 22:54:57 +00:00
|
|
|
|
|
|
|
fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics);
|
|
|
|
|
|
|
|
deleteTable((void *) hheaTable);
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
fCMAPMapper = findUnicodeMapper();
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
if (fCMAPMapper == NULL) {
|
2003-03-03 22:54:57 +00:00
|
|
|
status = LE_MISSING_FONT_TABLE_ERROR;
|
2001-10-03 21:48:10 +00:00
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
fclose(fFile);
|
|
|
|
fFile = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PortableFontInstance::~PortableFontInstance()
|
|
|
|
{
|
|
|
|
if (fFile != NULL) {
|
2002-04-02 02:55:31 +00:00
|
|
|
fclose(fFile);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
deleteTable(fHMTXTable);
|
2002-03-27 23:24:17 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
delete fCMAPMapper;
|
2002-03-27 23:24:17 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
LE_DELETE_ARRAY(fDirectory);
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
2005-01-14 18:49:35 +00:00
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
void PortableFontInstance::deleteTable(const void *table) const
|
|
|
|
{
|
2003-03-03 22:54:57 +00:00
|
|
|
LE_DELETE_ARRAY(table);
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const DirectoryEntry *PortableFontInstance::findTable(LETag tag) const
|
|
|
|
{
|
|
|
|
if (fDirectory != NULL) {
|
2002-04-02 02:55:31 +00:00
|
|
|
le_uint16 table = 0;
|
|
|
|
le_uint16 probe = fDirPower;
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (SWAPL(fDirectory->tableDirectory[fDirExtra].tag) <= tag) {
|
|
|
|
table = fDirExtra;
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
while (probe > (1 << 0)) {
|
|
|
|
probe >>= 1;
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (SWAPL(fDirectory->tableDirectory[table + probe].tag) <= tag) {
|
|
|
|
table += probe;
|
|
|
|
}
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (SWAPL(fDirectory->tableDirectory[table].tag) == tag) {
|
|
|
|
return &fDirectory->tableDirectory[table];
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
return NULL;
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const void *PortableFontInstance::readTable(LETag tag, le_uint32 *length) const
|
|
|
|
{
|
2002-04-02 02:55:31 +00:00
|
|
|
const DirectoryEntry *entry = findTable(tag);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (entry == NULL) {
|
|
|
|
*length = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
*length = SWAPL(entry->length);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
void *table = LE_NEW_ARRAY(char, *length);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
if (table != NULL) {
|
2002-04-02 02:55:31 +00:00
|
|
|
fseek(fFile, SWAPL(entry->offset), SEEK_SET);
|
|
|
|
fread(table, sizeof(char), *length, fFile);
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
return table;
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2002-03-27 23:24:17 +00:00
|
|
|
const void *PortableFontInstance::getFontTable(LETag tableTag) const
|
|
|
|
{
|
2003-03-03 22:54:57 +00:00
|
|
|
return FontTableCache::find(tableTag);
|
|
|
|
}
|
2002-03-27 23:24:17 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
const void *PortableFontInstance::readFontTable(LETag tableTag) const
|
|
|
|
{
|
|
|
|
le_uint32 len;
|
2002-03-27 23:24:17 +00:00
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
return readTable(tableTag, &len);
|
|
|
|
}
|
2002-03-27 23:24:17 +00:00
|
|
|
|
2001-10-03 21:48:10 +00:00
|
|
|
CMAPMapper *PortableFontInstance::findUnicodeMapper()
|
|
|
|
{
|
2003-05-20 23:39:30 +00:00
|
|
|
LETag cmapTag = LE_CMAP_TABLE_TAG;
|
2003-03-03 22:54:57 +00:00
|
|
|
const CMAPTable *cmap = (CMAPTable *) readFontTable(cmapTag);
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (cmap == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
return CMAPMapper::createUnicodeMapper(cmap);
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PortableFontInstance::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
|
|
|
|
{
|
2003-02-05 00:12:26 +00:00
|
|
|
TTGlyphID ttGlyph = (TTGlyphID) LE_GET_GLYPH(glyph);
|
|
|
|
|
2002-04-02 02:55:31 +00:00
|
|
|
if (fHMTXTable == NULL) {
|
2003-05-20 23:39:30 +00:00
|
|
|
LETag maxpTag = LE_MAXP_TABLE_TAG;
|
|
|
|
LETag hmtxTag = LE_HMTX_TABLE_TAG;
|
2003-03-03 22:54:57 +00:00
|
|
|
const MAXPTable *maxpTable = (MAXPTable *) readFontTable(maxpTag);
|
2002-04-02 02:55:31 +00:00
|
|
|
PortableFontInstance *realThis = (PortableFontInstance *) this;
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
if (maxpTable != NULL) {
|
2002-04-02 02:55:31 +00:00
|
|
|
realThis->fNumGlyphs = SWAPW(maxpTable->numGlyphs);
|
|
|
|
deleteTable(maxpTable);
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2003-03-03 22:54:57 +00:00
|
|
|
realThis->fHMTXTable = (const HMTXTable *) readFontTable(hmtxTag);
|
2002-04-02 02:55:31 +00:00
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-02-05 00:12:26 +00:00
|
|
|
le_uint16 index = ttGlyph;
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-02-05 00:12:26 +00:00
|
|
|
if (ttGlyph >= fNumGlyphs || fHMTXTable == NULL) {
|
2002-04-02 02:55:31 +00:00
|
|
|
advance.fX = advance.fY = 0;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
2003-02-05 00:12:26 +00:00
|
|
|
if (ttGlyph >= fNumLongHorMetrics) {
|
2002-04-02 02:55:31 +00:00
|
|
|
index = fNumLongHorMetrics - 1;
|
|
|
|
}
|
2001-10-03 21:48:10 +00:00
|
|
|
|
|
|
|
advance.fX = xUnitsToPoints(SWAPW(fHMTXTable->hMetrics[index].advanceWidth));
|
|
|
|
advance.fY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
le_bool PortableFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
|
|
|
|
{
|
2003-12-08 22:43:41 +00:00
|
|
|
return FALSE;
|
2001-10-03 21:48:10 +00:00
|
|
|
}
|
|
|
|
|