Commit bf7e0e98 authored by Albert Astals Cid's avatar Albert Astals Cid

Merge xpdf302branch in HEAD as noone vetoed it.

Testing more than welcome
parent ba74bb3b
2007-04-25 Albert Astals Cid <aacid@kde.org>
* ChangeLog:
* configure.ac:
* fofi/FoFiTrueType.cc:
* fofi/FoFiTrueType.h:
* fofi/FoFiType1.cc:
* fofi/FoFiType1C.cc:
* fofi/FoFiType1C.h:
* glib/poppler-action.cc:
* glib/poppler-document.cc:
* glib/poppler-page.cc:
* goo/FixedPoint.cc:
* goo/FixedPoint.h:
* goo/GooString.cc:
* goo/GooString.h:
* goo/Makefile.am:
* goo/gfile.cc:
* goo/gmem.cc:
* goo/gmem.h:
* poppler/ABWOutputDev.cc:
* poppler/ABWOutputDev.h:
* poppler/Annot.cc:
* poppler/Annot.h:
* poppler/ArthurOutputDev.cc:
* poppler/Catalog.cc:
* poppler/Catalog.h:
* poppler/CharCodeToUnicode.cc:
* poppler/DCTStream.cc:
* poppler/DCTStream.h:
* poppler/Decrypt.cc:
* poppler/Decrypt.h:
* poppler/Dict.cc:
* poppler/Dict.h:
* poppler/FontInfo.cc:
* poppler/Form.cc:
* poppler/Form.h:
* poppler/Function.cc:
* poppler/Function.h:
* poppler/Gfx.cc:
* poppler/Gfx.h:
* poppler/GfxFont.cc:
* poppler/GfxFont.h:
* poppler/GfxState.cc:
* poppler/GfxState.h:
* poppler/GlobalParams.cc:
* poppler/GlobalParams.h:
* poppler/JBIG2Stream.cc:
* poppler/JBIG2Stream.h:
* poppler/JPXStream.cc:
* poppler/JPXStream.h:
* poppler/Lexer.cc:
* poppler/Link.cc:
* poppler/Link.h:
* poppler/Makefile.am:
* poppler/Object.cc:
* poppler/Object.h:
* poppler/Outline.cc:
* poppler/OutputDev.cc:
* poppler/OutputDev.h:
* poppler/PDFDoc.cc:
* poppler/PDFDoc.h:
* poppler/PSOutputDev.cc:
* poppler/PSOutputDev.h:
* poppler/PSTokenizer.cc:
* poppler/Page.cc:
* poppler/Page.h:
* poppler/PageLabelInfo.cc:
* poppler/Parser.cc:
* poppler/Parser.h:
* poppler/PreScanOutputDev.cc:
* poppler/PreScanOutputDev.h:
* poppler/SecurityHandler.cc:
* poppler/SecurityHandler.h:
* poppler/SplashOutputDev.cc:
* poppler/SplashOutputDev.h:
* poppler/Stream.cc:
* poppler/Stream.h:
* poppler/TextOutputDev.cc:
* poppler/TextOutputDev.h:
* poppler/XRef.cc:
* poppler/XRef.h:
* qt/poppler-document.cc:
* qt/poppler-page-transition.cc:
* qt/poppler-page.cc:
* qt/poppler-private.h:
* qt4/src/poppler-annotation-helper.h:
* qt4/src/poppler-document.cc:
* qt4/src/poppler-embeddedfile.cc:
* qt4/src/poppler-form.cc:
* qt4/src/poppler-page.cc:
* qt4/src/poppler-private.h:
* splash/Splash.cc:
* splash/Splash.h:
* splash/SplashBitmap.cc:
* splash/SplashBitmap.h:
* splash/SplashClip.cc:
* splash/SplashClip.h:
* splash/SplashFTFont.cc:
* splash/SplashFTFont.h:
* splash/SplashFTFontEngine.cc:
* splash/SplashFTFontEngine.h:
* splash/SplashFTFontFile.cc:
* splash/SplashFTFontFile.h:
* splash/SplashFont.cc:
* splash/SplashFont.h:
* splash/SplashFontEngine.cc:
* splash/SplashFontEngine.h:
* splash/SplashFontFile.cc:
* splash/SplashFontFile.h:
* splash/SplashMath.h:
* splash/SplashPath.cc:
* splash/SplashPath.h:
* splash/SplashPattern.cc:
* splash/SplashPattern.h:
* splash/SplashScreen.cc:
* splash/SplashScreen.h:
* splash/SplashState.cc:
* splash/SplashState.h:
* splash/SplashT1Font.cc:
* splash/SplashT1Font.h:
* splash/SplashT1FontEngine.cc:
* splash/SplashT1FontFile.cc:
* splash/SplashT1FontFile.h:
* splash/SplashTypes.h:
* splash/SplashXPath.cc:
* splash/SplashXPath.h:
* splash/SplashXPathScanner.cc:
* splash/SplashXPathScanner.h:
* utils/HtmlOutputDev.cc:
* utils/HtmlOutputDev.h:
* utils/pdffonts.cc:
* utils/pdfinfo.cc:
* utils/pdftoabw.cc:
* utils/pdftohtml.cc:
* utils/pdftoppm.cc:
* utils/pdftops.cc:
* utils/pdftotext.cc:
Merge xpdf 3.02 changes
2007-04-15 Pino Toscano <pino@kde.org>
reviewed by: Albert Astals Cid <aacid@kde.org>
......
......@@ -40,6 +40,10 @@ dnl ##### Checks for header files.
AC_PATH_XTRA
AC_HEADER_DIRENT
AC_ARG_ENABLE(exceptions,
[ --enable-exceptions use C++ exceptions],
AC_DEFINE([USE_EXCEPTIONS], [], [Throw exceptions to deal with not enough memory and similar problems]))
dnl ##### Switch over to C++. This will make the checks below a little
dnl ##### bit stricter (requiring function prototypes in include files).
dnl ##### (99% of xpdf is written in C++.)
......
......@@ -18,6 +18,7 @@
#include "goo/gmem.h"
#include "goo/GooString.h"
#include "goo/GooHash.h"
#include "FoFiType1C.h"
#include "FoFiTrueType.h"
//
......@@ -108,8 +109,11 @@ struct TrueTypeLoca {
#define cmapTag 0x636d6170
#define glyfTag 0x676c7966
#define headTag 0x68656164
#define hheaTag 0x68686561
#define hmtxTag 0x686d7478
#define locaTag 0x6c6f6361
#define nameTag 0x6e616d65
#define os2Tag 0x4f532f32
#define postTag 0x706f7374
static int cmpTrueTypeLocaOffset(const void *p1, const void *p2) {
......@@ -392,6 +396,28 @@ int FoFiTrueType::mapNameToGID(char *name) {
return nameToGID->lookupInt(name);
}
Gushort *FoFiTrueType::getCIDToGIDMap(int *nCIDs) {
FoFiType1C *ff;
Gushort *map;
int i;
*nCIDs = 0;
if (!openTypeCFF) {
return NULL;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return NULL;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return NULL;
}
map = ff->getCIDToGIDMap(nCIDs);
delete ff;
return map;
}
int FoFiTrueType::getEmbeddingRights() {
int i, fsType;
GBool ok;
......@@ -420,13 +446,19 @@ void FoFiTrueType::convertToType42(char *psName, char **encoding,
Gushort *codeToGID,
FoFiOutputFunc outputFunc,
void *outputStream) {
char buf[512];
GooString *buf;
GBool ok;
if (openTypeCFF) {
return;
}
// write the header
ok = gTrue;
sprintf(buf, "%%!PS-TrueTypeFont-%g\n", (double)getS32BE(0, &ok) / 65536.0);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("%!PS-TrueTypeFont-{0:2g}\n",
(double)getS32BE(0, &ok) / 65536.0);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
// begin the font dictionary
(*outputFunc)(outputStream, "10 dict begin\n", 14);
......@@ -435,9 +467,10 @@ void FoFiTrueType::convertToType42(char *psName, char **encoding,
(*outputFunc)(outputStream, " def\n", 5);
(*outputFunc)(outputStream, "/FontType 42 def\n", 17);
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
sprintf(buf, "/FontBBox [%d %d %d %d] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17);
// write the guts of the dictionary
......@@ -449,20 +482,47 @@ void FoFiTrueType::convertToType42(char *psName, char **encoding,
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
}
void FoFiTrueType::convertToType1(char *psName, char **newEncoding,
GBool ascii, FoFiOutputFunc outputFunc,
void *outputStream) {
FoFiType1C *ff;
int i;
if (!openTypeCFF) {
return;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return;
}
ff->convertToType1(psName, newEncoding, ascii, outputFunc, outputStream);
delete ff;
}
void FoFiTrueType::convertToCIDType2(char *psName,
Gushort *cidMap, int nCIDs,
GBool needVerticalMetrics,
FoFiOutputFunc outputFunc,
void *outputStream) {
char buf[512];
GooString *buf;
Gushort cid;
GBool ok;
int i, j, k;
if (openTypeCFF) {
return;
}
// write the header
ok = gTrue;
sprintf(buf, "%%!PS-TrueTypeFont-%g\n", (double)getS32BE(0, &ok) / 65536.0);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("%!PS-TrueTypeFont-{0:2g}\n",
(double)getS32BE(0, &ok) / 65536.0);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
// begin the font dictionary
(*outputFunc)(outputStream, "20 dict begin\n", 14);
......@@ -478,8 +538,9 @@ void FoFiTrueType::convertToCIDType2(char *psName,
(*outputFunc)(outputStream, " end def\n", 10);
(*outputFunc)(outputStream, "/GDBytes 2 def\n", 15);
if (cidMap) {
sprintf(buf, "/CIDCount %d def\n", nCIDs);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/CIDCount {0:d} def\n", nCIDs);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
if (nCIDs > 32767) {
(*outputFunc)(outputStream, "/CIDMap [", 9);
for (i = 0; i < nCIDs; i += 32768 - 16) {
......@@ -488,8 +549,10 @@ void FoFiTrueType::convertToCIDType2(char *psName,
(*outputFunc)(outputStream, " ", 2);
for (k = 0; k < 16 && i+j+k < nCIDs; ++k) {
cid = cidMap[i+j+k];
sprintf(buf, "%02x%02x", (cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("{0:02x}{1:02x}",
(cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "\n", 1);
}
......@@ -503,8 +566,10 @@ void FoFiTrueType::convertToCIDType2(char *psName,
(*outputFunc)(outputStream, " ", 2);
for (j = 0; j < 16 && i+j < nCIDs; ++j) {
cid = cidMap[i+j];
sprintf(buf, "%02x%02x", (cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("{0:02x}{1:02x}",
(cid >> 8) & 0xff, cid & 0xff);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "\n", 1);
}
......@@ -512,27 +577,34 @@ void FoFiTrueType::convertToCIDType2(char *psName,
}
} else {
// direct mapping - just fill the string(s) with s[i]=i
sprintf(buf, "/CIDCount %d def\n", nGlyphs);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/CIDCount {0:d} def\n", nGlyphs);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
if (nGlyphs > 32767) {
(*outputFunc)(outputStream, "/CIDMap [\n", 10);
for (i = 0; i < nGlyphs; i += 32767) {
j = nGlyphs - i < 32767 ? nGlyphs - i : 32767;
sprintf(buf, " %d string 0 1 %d {\n", 2 * j, j - 1);
(*outputFunc)(outputStream, buf, strlen(buf));
sprintf(buf, " 2 copy dup 2 mul exch %d add -8 bitshift put\n", i);
(*outputFunc)(outputStream, buf, strlen(buf));
sprintf(buf, " 1 index exch dup 2 mul 1 add exch %d add"
" 255 and put\n", i);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format(" {0:d} string 0 1 {1:d} {{\n", 2 * j, j - 1);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
buf = GooString::format(" 2 copy dup 2 mul exch {0:d} add -8 bitshift put\n",
i);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
buf = GooString::format(" 1 index exch dup 2 mul 1 add exch {0:d} add"
" 255 and put\n", i);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, " } for\n", 8);
}
(*outputFunc)(outputStream, "] def\n", 6);
} else {
sprintf(buf, "/CIDMap %d string\n", 2 * nGlyphs);
(*outputFunc)(outputStream, buf, strlen(buf));
sprintf(buf, " 0 1 %d {\n", nGlyphs - 1);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/CIDMap {0:d} string\n", 2 * nGlyphs);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
buf = GooString::format(" 0 1 {0:d} {{\n", nGlyphs - 1);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream,
" 2 copy dup 2 mul exch -8 bitshift put\n", 42);
(*outputFunc)(outputStream,
......@@ -542,9 +614,10 @@ void FoFiTrueType::convertToCIDType2(char *psName,
}
}
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
sprintf(buf, "/FontBBox [%d %d %d %d] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17);
(*outputFunc)(outputStream, "/Encoding [] readonly def\n", 26);
(*outputFunc)(outputStream, "/CharStrings 1 dict dup begin\n", 30);
......@@ -560,14 +633,39 @@ void FoFiTrueType::convertToCIDType2(char *psName,
56);
}
void FoFiTrueType::convertToCIDType0(char *psName,
FoFiOutputFunc outputFunc,
void *outputStream) {
FoFiType1C *ff;
int i;
if (!openTypeCFF) {
return;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return;
}
ff->convertToCIDType0(psName, outputFunc, outputStream);
delete ff;
}
void FoFiTrueType::convertToType0(char *psName, Gushort *cidMap, int nCIDs,
GBool needVerticalMetrics,
FoFiOutputFunc outputFunc,
void *outputStream) {
char buf[512];
GooString *buf;
GooString *sfntsName;
int n, i, j;
if (openTypeCFF) {
return;
}
// write the Type 42 sfnts array
sfntsName = (new GooString(psName))->append("_sfnts");
cvtSfnts(outputFunc, outputStream, sfntsName, needVerticalMetrics);
......@@ -579,28 +677,33 @@ void FoFiTrueType::convertToType0(char *psName, Gushort *cidMap, int nCIDs,
(*outputFunc)(outputStream, "10 dict begin\n", 14);
(*outputFunc)(outputStream, "/FontName /", 11);
(*outputFunc)(outputStream, psName, strlen(psName));
sprintf(buf, "_%02x def\n", i >> 8);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("_{0:02x} def\n", i >> 8);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/FontType 42 def\n", 17);
(*outputFunc)(outputStream, "/FontMatrix [1 0 0 1 0 0] def\n", 30);
sprintf(buf, "/FontBBox [%d %d %d %d] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/FontBBox [{0:d} {1:d} {2:d} {3:d}] def\n",
bbox[0], bbox[1], bbox[2], bbox[3]);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
(*outputFunc)(outputStream, "/PaintType 0 def\n", 17);
(*outputFunc)(outputStream, "/sfnts ", 7);
(*outputFunc)(outputStream, psName, strlen(psName));
(*outputFunc)(outputStream, "_sfnts def\n", 11);
(*outputFunc)(outputStream, "/Encoding 256 array\n", 20);
for (j = 0; j < 256 && i+j < n; ++j) {
sprintf(buf, "dup %d /c%02x put\n", j, j);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("dup {0:d} /c{1:02x} put\n", j, j);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "readonly def\n", 13);
(*outputFunc)(outputStream, "/CharStrings 257 dict dup begin\n", 32);
(*outputFunc)(outputStream, "/.notdef 0 def\n", 15);
for (j = 0; j < 256 && i+j < n; ++j) {
sprintf(buf, "/c%02x %d def\n", j, cidMap ? cidMap[i+j] : i+j);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("/c{0:02x} {1:d} def\n",
j, cidMap ? cidMap[i+j] : i+j);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "end readonly def\n", 17);
(*outputFunc)(outputStream,
......@@ -617,21 +720,44 @@ void FoFiTrueType::convertToType0(char *psName, Gushort *cidMap, int nCIDs,
(*outputFunc)(outputStream, "/FMapType 2 def\n", 16);
(*outputFunc)(outputStream, "/Encoding [\n", 12);
for (i = 0; i < n; i += 256) {
sprintf(buf, "%d\n", i >> 8);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("{0:d}\n", i >> 8);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "] def\n", 6);
(*outputFunc)(outputStream, "/FDepVector [\n", 14);
for (i = 0; i < n; i += 256) {
(*outputFunc)(outputStream, "/", 1);
(*outputFunc)(outputStream, psName, strlen(psName));
sprintf(buf, "_%02x findfont\n", i >> 8);
(*outputFunc)(outputStream, buf, strlen(buf));
buf = GooString::format("_{0:02x} findfont\n", i >> 8);
(*outputFunc)(outputStream, buf->getCString(), buf->getLength());
delete buf;
}
(*outputFunc)(outputStream, "] def\n", 6);
(*outputFunc)(outputStream, "FontName currentdict end definefont pop\n", 40);
}
void FoFiTrueType::convertToType0(char *psName,
FoFiOutputFunc outputFunc,
void *outputStream) {
FoFiType1C *ff;
int i;
if (!openTypeCFF) {
return;
}
i = seekTable("CFF ");
if (!checkRegion(tables[i].offset, tables[i].len)) {
return;
}
if (!(ff = FoFiType1C::make((char *)file + tables[i].offset,
tables[i].len))) {
return;
}
ff->convertToType0(psName, outputFunc, outputStream);
delete ff;
}
void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
void *outputStream, char *name,
Gushort *codeToGID) {
......@@ -673,12 +799,50 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
0, 0, 0, 0, // min Type 1 memory
0, 0, 0, 0 // max Type 1 memory
};
GBool missingCmap, missingName, missingPost, unsortedLoca, badCmapLen;
static char os2Tab[86] = {
0, 1, // version
0, 1, // xAvgCharWidth
0, 0, // usWeightClass
0, 0, // usWidthClass
0, 0, // fsType
0, 0, // ySubscriptXSize
0, 0, // ySubscriptYSize
0, 0, // ySubscriptXOffset
0, 0, // ySubscriptYOffset
0, 0, // ySuperscriptXSize
0, 0, // ySuperscriptYSize
0, 0, // ySuperscriptXOffset
0, 0, // ySuperscriptYOffset
0, 0, // yStrikeoutSize
0, 0, // yStrikeoutPosition
0, 0, // sFamilyClass
0, 0, 0, 0, 0, // panose
0, 0, 0, 0, 0,
0, 0, 0, 0, // ulUnicodeRange1
0, 0, 0, 0, // ulUnicodeRange2
0, 0, 0, 0, // ulUnicodeRange3
0, 0, 0, 0, // ulUnicodeRange4
0, 0, 0, 0, // achVendID
0, 0, // fsSelection
0, 0, // usFirstCharIndex
0, 0, // usLastCharIndex
0, 0, // sTypoAscender
0, 0, // sTypoDescender
0, 0, // sTypoLineGap
0, 0, // usWinAscent
0, 0, // usWinDescent
0, 0, 0, 0, // ulCodePageRange1
0, 0, 0, 0 // ulCodePageRange2
};
GBool missingCmap, missingName, missingPost, missingOS2;
GBool unsortedLoca, badCmapLen, abbrevHMTX;
int nZeroLengthTables;
int nHMetrics, advWidth, lsb;
TrueTypeLoca *locaTable;
TrueTypeTable *newTables;
char *newNameTab, *newCmapTab;
char *newNameTab, *newCmapTab, *newHHEATab, *newHMTXTab;
int nNewTables, cmapIdx, cmapLen, glyfLen, newNameLen, newCmapLen, next;
int newHHEALen, newHMTXLen;
Guint locaChecksum, glyfChecksum, fileChecksum;
char *tableDir;
char locaBuf[4], checksumBuf[4];
......@@ -686,10 +850,20 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
Guint t;
int pos, i, j, k, n;
if (openTypeCFF) {
return;
}
// check for missing tables
// (Note: if the OS/2 table is missing, the Microsoft PCL5 driver
// will embed a PCL TrueType font with the pitch field set to zero,
// which apparently causes divide-by-zero errors. As far as I can
// tell, the only important field in the OS/2 table is
// xAvgCharWidth.)
missingCmap = (cmapIdx = seekTable("cmap")) < 0;
missingName = seekTable("name") < 0;
missingPost = seekTable("post") < 0;
missingOS2 = seekTable("OS/2") < 0;
// read the loca table, check to see if it's sorted
locaTable = (TrueTypeLoca *)gmallocn(nGlyphs + 1, sizeof(TrueTypeLoca));
......@@ -706,6 +880,19 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
if (i > 0 && locaTable[i].origOffset < locaTable[i-1].origOffset) {
unsortedLoca = gTrue;
}
// glyph descriptions must be at least 12 bytes long (nContours,
// xMin, yMin, xMax, yMax, instructionLength - two bytes each);
// invalid glyph descriptions (even if they're never used) make
// Windows choke, so we work around that problem here (ideally,
// this would parse the glyph descriptions in the glyf table and
// remove any that were invalid, but this quick test is a decent
// start)
if (i > 0 &&
locaTable[i].origOffset - locaTable[i-1].origOffset > 0 &&
locaTable[i].origOffset - locaTable[i-1].origOffset < 12) {
locaTable[i-1].origOffset = locaTable[i].origOffset;
unsortedLoca = gTrue;
}
locaTable[i].idx = i;
}
......@@ -733,9 +920,17 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
}
}
// check for an abbreviated hmtx table (this is completely legal,
// but confuses the Microsoft PCL5 printer driver, which generates
// embedded fonts with the pitch field set to zero)
i = seekTable("hhea");
nHMetrics = getU16BE(tables[i].offset + 34, &ok);
abbrevHMTX = nHMetrics < nGlyphs;
// if nothing is broken, just write the TTF file as is
if (!missingCmap && !missingName && !missingPost && !unsortedLoca &&
!badCmapLen && nZeroLengthTables == 0 && !name && !codeToGID) {
if (!missingCmap && !missingName && !missingPost && !missingOS2 &&
!unsortedLoca && !badCmapLen && !abbrevHMTX && nZeroLengthTables == 0 &&
!name && !codeToGID) {
(*outputFunc)(outputStream, (char *)file, len);
goto done1;
}
......@@ -893,6 +1088,44 @@ void FoFiTrueType::writeTTF(FoFiOutputFunc outputFunc,
newCmapTab = NULL;
}
// generate the new hmtx table and the updated hhea table
if (abbrevHMTX) {
i = seekTable("hhea");
pos = tables[i].offset;
newHHEALen = 36;
newHHEATab = (char *)gmalloc(newHHEALen);
for (i = 0; i < newHHEALen; ++i) {
newHHEATab[i] = getU8(pos++, &ok);
}
newHHEATab[34] = nGlyphs >> 8;
newHHEATab[35] = nGlyphs & 0xff;
i = seekTable("hmtx");
pos = tables[i].offset;
newHMTXLen = 4 * nGlyphs;
newHMTXTab = (char *)gmalloc(newHMTXLen);
advWidth = 0;
for (i = 0; i < nHMetrics; ++i) {
advWidth = getU16BE(pos, &ok);
lsb = getU16BE(pos + 2, &ok);
pos += 4;
newHMTXTab[4*i ] = advWidth >> 8;
newHMTXTab[4*i + 1] = advWidth & 0xff;
newHMTXTab[4*i + 2] = lsb >> 8;
newHMTXTab[4*i + 3] = lsb & 0xff;
}
for (; i < nGlyphs; ++i) {
lsb = getU16BE(pos, &ok);
pos += 2;
newHMTXTab[4*i ] = advWidth >> 8;
newHMTXTab[4*i + 1] = advWidth & 0xff;
newHMTXTab[4*i + 2] = lsb >> 8;
newHMTXTab[4*i + 3] = lsb & 0xff;
}
} else {
newHHEATab = newHMTXTab = NULL;
newHHEALen = newHMTXLen = 0; // make gcc happy
}