Skip to content

Commit f41c1bd

Browse files
committed
bug: not all true types can use winansiencoding. need to check cmaps
1 parent 339db4f commit f41c1bd

12 files changed

+183
-63
lines changed

PDFWriter/AbstractWrittenFont.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636

3737
using namespace PDFHummus;
3838

39-
AbstractWrittenFont::AbstractWrittenFont(ObjectsContext* inObjectsContext)
39+
AbstractWrittenFont::AbstractWrittenFont(ObjectsContext* inObjectsContext, FreeTypeFaceWrapper* inFontInfo)
4040
{
4141
mObjectsContext = inObjectsContext;
42+
mFontInfo = inFontInfo;
4243
mCIDRepresentation = NULL;
4344
mANSIRepresentation = NULL;
4445
}

PDFWriter/AbstractWrittenFont.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PDFParser;
3232
class AbstractWrittenFont : public IWrittenFont
3333
{
3434
public:
35-
AbstractWrittenFont(ObjectsContext* inObjectsContext);
35+
AbstractWrittenFont(ObjectsContext* inObjectsContext, FreeTypeFaceWrapper* inFontInfo);
3636
virtual ~AbstractWrittenFont(void);
3737

3838
virtual void AppendGlyphs(const GlyphUnicodeMappingList& inGlyphsList,
@@ -47,6 +47,7 @@ class AbstractWrittenFont : public IWrittenFont
4747
WrittenFontRepresentation* mCIDRepresentation;
4848
WrittenFontRepresentation* mANSIRepresentation;
4949
ObjectsContext* mObjectsContext;
50+
FreeTypeFaceWrapper* mFontInfo;
5051

5152
PDFHummus::EStatusCode WriteStateInDictionary(ObjectsContext* inStateWriter,DictionaryContext* inDerivedObjectDictionary);
5253
PDFHummus::EStatusCode WriteStateAfterDictionary(ObjectsContext* inStateWriter);

PDFWriter/FreeTypeFaceWrapper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,11 @@ IWrittenFont* FreeTypeFaceWrapper::CreateWrittenFontObject(ObjectsContext* inObj
655655
if(FT_Get_CID_Is_Internally_CID_Keyed(mFace,&isCID) != 0)
656656
isCID = false;
657657

658-
result = new WrittenFontCFF(inObjectsContext,isCID != 0, inFontIsToBeEmbedded); // CFF fonts should know if font is to be embedded, as the embedding code involves re-encoding of glyphs
658+
result = new WrittenFontCFF(inObjectsContext, this,isCID != 0, inFontIsToBeEmbedded); // CFF fonts should know if font is to be embedded, as the embedding code involves re-encoding of glyphs
659659
}
660660
else if(strcmp(fontFormat,scTrueType) == 0)
661661
{
662-
result = new WrittenFontTrueType(inObjectsContext);
662+
result = new WrittenFontTrueType(inObjectsContext, this);
663663
}
664664
else
665665
{

PDFWriter/IWrittenFont.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class IWrittenFont
6161
/*
6262
Write a font definition using the glyphs appended.
6363
*/
64-
virtual PDFHummus::EStatusCode WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo,bool inEmbedFont) = 0;
64+
virtual PDFHummus::EStatusCode WriteFontDefinition(bool inEmbedFont) = 0;
6565

6666
// state read and write
6767
virtual PDFHummus::EStatusCode WriteState(ObjectsContext* inStateWriter,ObjectIDType inObjectID) = 0;

PDFWriter/PDFUsedFont.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ EStatusCode PDFUsedFont::WriteFontDefinition()
122122
if(!mWrittenFont)
123123
return eSuccess;
124124
else
125-
return mWrittenFont->WriteFontDefinition(mFaceWrapper, mEmbedFont);
125+
return mWrittenFont->WriteFontDefinition(mEmbedFont);
126126
}
127127

128128
EStatusCode PDFUsedFont::WriteState(ObjectsContext* inStateWriter,ObjectIDType inObjectID)

PDFWriter/WrittenFontCFF.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
using namespace PDFHummus;
3636

37-
WrittenFontCFF::WrittenFontCFF(ObjectsContext* inObjectsContext,bool inIsCID, bool inFontWillBeEmbedded):AbstractWrittenFont(inObjectsContext)
37+
WrittenFontCFF::WrittenFontCFF(ObjectsContext* inObjectsContext, FreeTypeFaceWrapper* inFontInfo, bool inIsCID, bool inFontWillBeEmbedded):AbstractWrittenFont(inObjectsContext, inFontInfo)
3838
{
3939
mAvailablePositionsCount = 255;
4040
mFreeList.push_back(UCharAndUChar(1,255));
@@ -161,7 +161,7 @@ unsigned char WrittenFontCFF::AllocateFromFreeList(unsigned int inGlyph)
161161
return result;
162162
}
163163

164-
EStatusCode WrittenFontCFF::WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo,bool inEmbedFont)
164+
EStatusCode WrittenFontCFF::WriteFontDefinition(bool inEmbedFont)
165165
{
166166
EStatusCode status = PDFHummus::eSuccess;
167167
do
@@ -170,7 +170,7 @@ EStatusCode WrittenFontCFF::WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo,
170170
{
171171
CFFANSIFontWriter fontWriter;
172172

173-
status = fontWriter.WriteFont(inFontInfo, mANSIRepresentation, mObjectsContext, inEmbedFont);
173+
status = fontWriter.WriteFont(*mFontInfo, mANSIRepresentation, mObjectsContext, inEmbedFont);
174174
if(status != PDFHummus::eSuccess)
175175
{
176176
TRACE_LOG("WrittenFontCFF::WriteFontDefinition, Failed to write Ansi font definition");
@@ -184,7 +184,7 @@ EStatusCode WrittenFontCFF::WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo,
184184
CIDFontWriter fontWriter;
185185
CFFDescendentFontWriter descendentFontWriter;
186186

187-
status = fontWriter.WriteFont(inFontInfo, mCIDRepresentation, mObjectsContext, &descendentFontWriter, inEmbedFont);
187+
status = fontWriter.WriteFont(*mFontInfo, mCIDRepresentation, mObjectsContext, &descendentFontWriter, inEmbedFont);
188188
if(status != PDFHummus::eSuccess)
189189
{
190190
TRACE_LOG("WrittenFontCFF::WriteFontDefinition, Failed to write CID font definition");

PDFWriter/WrittenFontCFF.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ typedef std::list<UCharAndUChar> UCharAndUCharList;
3333
class WrittenFontCFF : public AbstractWrittenFont
3434
{
3535
public:
36-
WrittenFontCFF(ObjectsContext* inObjectsContext,bool inIsCID, bool inFontWillBeEmbedded);
36+
WrittenFontCFF(ObjectsContext* inObjectsContext, FreeTypeFaceWrapper* inFontInfo, bool inIsCID, bool inFontWillBeEmbedded);
3737
virtual ~WrittenFontCFF(void);
3838

3939

40-
virtual PDFHummus::EStatusCode WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo, bool inEmbedFont);
40+
virtual PDFHummus::EStatusCode WriteFontDefinition(bool inEmbedFont);
4141

4242
virtual PDFHummus::EStatusCode WriteState(ObjectsContext* inStateWriter,ObjectIDType inObjectId);
4343
virtual PDFHummus::EStatusCode ReadState(PDFParser* inStateReader,ObjectIDType inObjectID);

PDFWriter/WrittenFontTrueType.cpp

+55-49
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,33 @@
2929
#include "PDFObjectCast.h"
3030
#include "PDFParser.h"
3131
#include "PDFDictionary.h"
32+
#include "FreeTypeFaceWrapper.h"
33+
34+
#include <ft2build.h>
35+
#include FT_FREETYPE_H
3236

3337
using namespace PDFHummus;
3438

35-
WrittenFontTrueType::WrittenFontTrueType(ObjectsContext* inObjectsContext):AbstractWrittenFont(inObjectsContext)
39+
bool FontHasCmapsForWinAnsiEncoding(FT_Face font) {
40+
// See PDF Reference 5.5.5 Character Encoding, Encodings for TrueType Fonts. When is mapping from glyph name is possible, and so non CID encoding can be built - only if font contains either of
41+
// two possible cmaps: win unicode bmp or macintosh roman. so check if got either of those.
42+
for(FT_Int i = 0; i < font->num_charmaps; ++i) {
43+
FT_CharMap charmap = font->charmaps[i];
44+
45+
if (charmap->platform_id == 3 && charmap->encoding_id == 1)
46+
return true; // Windows Unicode BMP
47+
48+
if (charmap->platform_id == 1 && charmap->encoding_id == 0)
49+
return true; // Macintosh Roman
50+
51+
}
52+
53+
return false;
54+
}
55+
56+
WrittenFontTrueType::WrittenFontTrueType(ObjectsContext* inObjectsContext, FreeTypeFaceWrapper* inFontInfo):AbstractWrittenFont(inObjectsContext, inFontInfo)
3657
{
58+
fontSupportsWinAnsiEncoding = FontHasCmapsForWinAnsiEncoding(*inFontInfo);
3759
}
3860

3961
WrittenFontTrueType::~WrittenFontTrueType(void)
@@ -43,18 +65,19 @@ WrittenFontTrueType::~WrittenFontTrueType(void)
4365
/*
4466
here's what i'm deciding on:
4567
1. Can encoding if/f all text codes are available through WinAnsiEncoding.
46-
[maybe should also make sure that the font has the relevant cmaps?! Or maybe I'm just assuming that...]
68+
[maybe should also make sure that the font has the relevant cmaps?! Or maybe I'm just assuming that...] [That's what FontHasCmapsForWinAnsiEncoding and fontSupportsWinAnsiEncoding are for]
4769
2. While encoding use WinAnsiEncoding values, of course. This will necasserily work
4870
3. While writing the font description simply write the WinAnsiEncoding glyph name, and pray.*/
4971

50-
bool WrittenFontTrueType::AddToANSIRepresentation( const GlyphUnicodeMappingList& inGlyphsList,
51-
UShortList& outEncodedCharacters)
52-
{
72+
bool WrittenFontTrueType::AddANSICandidates(const GlyphUnicodeMappingList& inGlyphsList, UShortList& ioCandidates) {
73+
if(!fontSupportsWinAnsiEncoding) {
74+
return false;
75+
}
76+
5377
// i'm totally relying on the text here, which is fine till i'll do ligatures, in which case
5478
// i'll need to make something different out of the text.
5579
// as you can see this has little to do with glyphs (mainly cause i can't use FreeType to map the glyphs
5680
// back to the rleevant unicode values...but no need anyways...that's why i carry the text).
57-
UShortList candidates;
5881
BoolAndByte encodingResult(true,0);
5982
WinAnsiEncoding winAnsiEncoding;
6083
GlyphUnicodeMappingList::const_iterator it = inGlyphsList.begin();
@@ -81,11 +104,20 @@ bool WrittenFontTrueType::AddToANSIRepresentation( const GlyphUnicodeMappingList
81104
{
82105
encodingResult = winAnsiEncoding.Encode(it->mUnicodeValues.front());
83106
if(encodingResult.first)
84-
candidates.push_back(encodingResult.second);
107+
ioCandidates.push_back(encodingResult.second);
85108
}
86109
}
87110

88-
if(encodingResult.first)
111+
return encodingResult.first;
112+
}
113+
114+
bool WrittenFontTrueType::AddToANSIRepresentation(const GlyphUnicodeMappingList& inGlyphsList, UShortList& outEncodedCharacters)
115+
{
116+
UShortList candidates;
117+
118+
bool result = AddANSICandidates(inGlyphsList, candidates);
119+
120+
if(result)
89121
{
90122
// for the first time, add also 0,0 mapping
91123
if(mANSIRepresentation->mGlyphIDToEncodedChar.size() == 0)
@@ -104,11 +136,11 @@ bool WrittenFontTrueType::AddToANSIRepresentation( const GlyphUnicodeMappingList
104136
outEncodedCharacters = candidates;
105137
}
106138

107-
return encodingResult.first;
139+
return result;
108140
}
109141

110142

111-
EStatusCode WrittenFontTrueType::WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo,bool inEmbedFont)
143+
EStatusCode WrittenFontTrueType::WriteFontDefinition(bool inEmbedFont)
112144
{
113145
EStatusCode status = PDFHummus::eSuccess;
114146
do
@@ -117,7 +149,7 @@ EStatusCode WrittenFontTrueType::WriteFontDefinition(FreeTypeFaceWrapper& inFont
117149
{
118150
TrueTypeANSIFontWriter fontWriter;
119151

120-
status = fontWriter.WriteFont(inFontInfo, mANSIRepresentation, mObjectsContext, inEmbedFont);
152+
status = fontWriter.WriteFont(*mFontInfo, mANSIRepresentation, mObjectsContext, inEmbedFont);
121153
if(status != PDFHummus::eSuccess)
122154
{
123155
TRACE_LOG("WrittenFontTrueType::WriteFontDefinition, Failed to write Ansi font definition");
@@ -131,7 +163,7 @@ EStatusCode WrittenFontTrueType::WriteFontDefinition(FreeTypeFaceWrapper& inFont
131163
CIDFontWriter fontWriter;
132164
TrueTypeDescendentFontWriter descendentFontWriter;
133165

134-
status = fontWriter.WriteFont(inFontInfo, mCIDRepresentation, mObjectsContext, &descendentFontWriter, inEmbedFont);
166+
status = fontWriter.WriteFont(*mFontInfo, mCIDRepresentation, mObjectsContext, &descendentFontWriter, inEmbedFont);
135167
if(status != PDFHummus::eSuccess)
136168
{
137169
TRACE_LOG("WrittenFontTrueType::WriteFontDefinition, Failed to write CID font definition");
@@ -150,46 +182,20 @@ bool WrittenFontTrueType::AddToANSIRepresentation( const GlyphUnicodeMappingList
150182
UShortListList candidatesList;
151183
UShortList candidates;
152184
BoolAndByte encodingResult(true,0);
153-
WinAnsiEncoding winAnsiEncoding;
154185
GlyphUnicodeMappingListList::const_iterator itList = inGlyphsList.begin();
155-
GlyphUnicodeMappingList::const_iterator it;
186+
bool result = true;
156187

157-
for(; itList != inGlyphsList.end() && encodingResult.first; ++itList)
188+
for(; itList != inGlyphsList.end() && result; ++itList)
158189
{
159-
it = itList->begin();
160-
for(; it != itList->end() && encodingResult.first; ++it)
161-
{
162-
// don't bother with characters of more or less than one unicode
163-
if(it->mUnicodeValues.size() != 1)
164-
{
165-
encodingResult.first = false;
166-
}
167-
else if(0x2022 == it->mUnicodeValues.front())
168-
{
169-
// From the reference:
170-
// In WinAnsiEncoding, all unused codes greater than 40 map to the bullet character.
171-
// However, only code 225 is specifically assigned to the bullet character; other codes are subject to future reassignment.
172-
173-
// now i don't know if it's related or not...but acrobat isn't happy when i'm using winansi with bullet. and text coming after that bullet may be
174-
// corrupted.
175-
// so i'm forcing CID if i hit bullet till i know better.
176-
encodingResult.first = false;
177-
}
178-
else
179-
{
180-
encodingResult = winAnsiEncoding.Encode(it->mUnicodeValues.front());
181-
if(encodingResult.first)
182-
candidates.push_back(encodingResult.second);
183-
}
184-
}
185-
if(encodingResult.first)
190+
result = AddANSICandidates(*itList, candidates);
191+
if(result)
186192
{
187193
candidatesList.push_back(candidates);
188194
candidates.clear();
189195
}
190196
}
191197

192-
if(encodingResult.first)
198+
if(result)
193199
{
194200
// for the first time, add also 0,0 mapping
195201
if(mANSIRepresentation->mGlyphIDToEncodedChar.size() == 0)
@@ -216,7 +222,7 @@ bool WrittenFontTrueType::AddToANSIRepresentation( const GlyphUnicodeMappingList
216222
outEncodedCharacters = candidatesList;
217223
}
218224

219-
return encodingResult.first;
225+
return result;
220226
}
221227

222228
EStatusCode WrittenFontTrueType::WriteState(ObjectsContext* inStateWriter,ObjectIDType inObjectID)
@@ -246,9 +252,9 @@ EStatusCode WrittenFontTrueType::ReadState(PDFParser* inStateReader,ObjectIDType
246252
return AbstractWrittenFont::ReadStateFromObject(inStateReader,writtenFontState.GetPtr());
247253
}
248254

249-
unsigned short WrittenFontTrueType::EncodeCIDGlyph(unsigned int inGlyphId) {
250-
// Gal 26/8/2017: Most of the times, the glyph IDs are CIDs. this is to retain a few requirements of True type fonts, and the case of fonts when they are not embedded.
251-
// However, when CFF fonts are embedded, the matching code actually recreates a font from just the subset, and renumbers them based on the order
252-
// of them joining the font. Hence, we need a slight difference for this case, and an override is provided
253-
return (unsigned short)inGlyphId;
255+
unsigned short WrittenFontTrueType::EncodeCIDGlyph(unsigned int inGlyphId) {
256+
// Gal 26/8/2017: Most of the times, the glyph IDs are CIDs. this is to retain a few requirements of True type fonts, and the case of fonts when they are not embedded.
257+
// However, when CFF fonts are embedded, the matching code actually recreates a font from just the subset, and renumbers them based on the order
258+
// of them joining the font. Hence, we need a slight difference for this case, and an override is provided
259+
return (unsigned short)inGlyphId;
254260
}

PDFWriter/WrittenFontTrueType.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
class WrittenFontTrueType : public AbstractWrittenFont
2525
{
2626
public:
27-
WrittenFontTrueType(ObjectsContext* inObjectsContext);
27+
WrittenFontTrueType(ObjectsContext* inObjectsContext, FreeTypeFaceWrapper* inFontInfo);
2828
~WrittenFontTrueType(void);
2929

30-
virtual PDFHummus::EStatusCode WriteFontDefinition(FreeTypeFaceWrapper& inFontInfo,bool inEmbedFont);
30+
virtual PDFHummus::EStatusCode WriteFontDefinition(bool inEmbedFont);
3131

3232
virtual PDFHummus::EStatusCode WriteState(ObjectsContext* inStateWriter,ObjectIDType inObjectId);
3333
virtual PDFHummus::EStatusCode ReadState(PDFParser* inStateReader,ObjectIDType inObjectID);
3434

3535

3636
private:
37+
bool fontSupportsWinAnsiEncoding;
38+
3739
virtual bool AddToANSIRepresentation( const GlyphUnicodeMappingList& inGlyphsList,
3840
UShortList& outEncodedCharacters);
3941

@@ -44,4 +46,8 @@ class WrittenFontTrueType : public AbstractWrittenFont
4446

4547
virtual unsigned short EncodeCIDGlyph(unsigned int inGlyphId);
4648

49+
bool AddANSICandidates(const GlyphUnicodeMappingList& inGlyphsList, UShortList& ioCandidates);
50+
51+
52+
4753
};

PDFWriterTesting/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ create_test_sourcelist (Tests
6767
TIFFImageTest.cpp
6868
TiffSpecialsTest.cpp
6969
TimerTest.cpp
70+
TrueTypeAnsiWriteBug.cpp
7071
TrueTypeTest.cpp
7172
TTCTest.cpp
7273
Type1Test.cpp
Binary file not shown.

0 commit comments

Comments
 (0)