1 /*
2    Source File : CFFEmbeddedFontWriter.h
3 
4 
5    Copyright 2011 Gal Kahana PDFWriter
6 
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10 
11        http://www.apache.org/licenses/LICENSE-2.0
12 
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 
19 
20 */
21 #pragma once
22 
23 #include "EStatusCode.h"
24 #include "ObjectsBasicTypes.h"
25 #include "OpenTypeFileInput.h"
26 #include "MyStringBuf.h"
27 #include "InputFile.h"
28 #include "CFFPrimitiveWriter.h"
29 #include "OutputStringBufferStream.h"
30 #include "IOBasicTypes.h"
31 
32 #include <vector>
33 #include <string>
34 #include <set>
35 #include <map>
36 
37 
38 using namespace IOBasicTypes;
39 
40 typedef std::vector<unsigned int> UIntVector;
41 typedef std::set<unsigned int> UIntSet;
42 typedef std::vector<unsigned short> UShortVector;
43 typedef std::map<FontDictInfo*,Byte> FontDictInfoToByteMap;
44 
45 
46 class FreeTypeFaceWrapper;
47 class ObjectsContext;
48 
49 class CFFEmbeddedFontWriter
50 {
51 public:
52 	CFFEmbeddedFontWriter(void);
53 	~CFFEmbeddedFontWriter(void);
54 
55 	PDFHummus::EStatusCode WriteEmbeddedFont(	FreeTypeFaceWrapper& inFontInfo,
56 									const UIntVector& inSubsetGlyphIDs,
57 									const std::string& inFontFile3SubType,
58 									const std::string& inSubsetFontName,
59 									ObjectsContext* inObjectsContext,
60 									ObjectIDType& outEmbeddedFontObjectID);
61 
62 	// the optional inCIDMapping parameter provides a vector ordered in the same
63 	// way as the glyph IDs. for each position in the CID mapping vector there's the matching CID
64 	// for the GID in the same position in the subset glyph IDs.
65 	// use it when the CFF origin is from a subset font, and the GID->CID mapping is not simply
66 	// identity
67 	PDFHummus::EStatusCode WriteEmbeddedFont(	FreeTypeFaceWrapper& inFontInfo,
68 									const UIntVector& inSubsetGlyphIDs,
69 									const std::string& inFontFile3SubType,
70 									const std::string& inSubsetFontName,
71 									ObjectsContext* inObjectsContext,
72 									UShortVector* inCIDMapping,
73 									ObjectIDType& outEmbeddedFontObjectID);
74 
75 
76 private:
77 	OpenTypeFileInput mOpenTypeInput;
78 	InputFile mOpenTypeFile;
79 	CFFPrimitiveWriter mPrimitivesWriter;
80 	OutputStringBufferStream mFontFileStream;
81 	bool mIsCID;
82 	std::string mOptionalEmbeddedPostscript;
83 
84 	// placeholders positions
85 	LongFilePositionType mCharsetPlaceHolderPosition;
86 	LongFilePositionType mEncodingPlaceHolderPosition;
87 	LongFilePositionType mCharstringsPlaceHolderPosition;
88 	LongFilePositionType mPrivatePlaceHolderPosition;
89 	LongFilePositionType mFDArrayPlaceHolderPosition;
90 	LongFilePositionType mFDSelectPlaceHolderPosition;
91 
92 	LongFilePositionType mEncodingPosition;
93 	LongFilePositionType mCharsetPosition;
94 	LongFilePositionType mCharStringPosition;
95 	LongFilePositionType mPrivatePosition;
96 	LongFilePositionType mPrivateSize;
97 	LongFilePositionType mFDArrayPosition;
98 	LongFilePositionType mFDSelectPosition;
99 
100 	PDFHummus::EStatusCode CreateCFFSubset(
101 					FreeTypeFaceWrapper& inFontInfo,
102 					const UIntVector& inSubsetGlyphIDs,
103 					UShortVector* inCIDMapping,
104 					const std::string& inSubsetFontName,
105 					bool& outNotEmbedded,
106 					MyStringBuf& outFontProgram);
107 	PDFHummus::EStatusCode AddDependentGlyphs(UIntVector& ioSubsetGlyphIDs);
108 	PDFHummus::EStatusCode AddComponentGlyphs(unsigned int inGlyphID,UIntSet& ioComponents,bool &outFoundComponents);
109 	PDFHummus::EStatusCode WriteCFFHeader();
110 	PDFHummus::EStatusCode WriteName(const std::string& inSubsetFontName);
111 	PDFHummus::EStatusCode WriteTopIndex();
112 	Byte GetMostCompressedOffsetSize(unsigned long inOffset);
113 	PDFHummus::EStatusCode WriteTopDictSegment(MyStringBuf& ioTopDictSegment);
114 	PDFHummus::EStatusCode WriteStringIndex();
115 	PDFHummus::EStatusCode WriteGlobalSubrsIndex();
116 	PDFHummus::EStatusCode WriteEncodings(const UIntVector& inSubsetGlyphIDs);
117 	PDFHummus::EStatusCode WriteCharsets(const UIntVector& inSubsetGlyphIDs,UShortVector* inCIDMapping);
118 	PDFHummus::EStatusCode WriteCharStrings(const UIntVector& inSubsetGlyphIDs);
119 	PDFHummus::EStatusCode WritePrivateDictionary();
120 
121 	PDFHummus::EStatusCode WriteFDArray(const UIntVector& inSubsetGlyphIDs,const FontDictInfoToByteMap& inNewFontDictsIndexes);
122 	PDFHummus::EStatusCode WriteFDSelect(const UIntVector& inSubsetGlyphIDs,const FontDictInfoToByteMap& inNewFontDictsIndexes);
123 	PDFHummus::EStatusCode WritePrivateDictionaryBody(	const PrivateDictInfo& inPrivateDictionary,
124 											LongFilePositionType& outWriteSize,
125 											LongFilePositionType& outWritePosition);
126 	PDFHummus::EStatusCode UpdateIndexesAtTopDict();
127 
128 	void DetermineFDArrayIndexes(const UIntVector& inSubsetGlyphIDs,FontDictInfoToByteMap& outNewFontDictsIndexes);
129 
130 };
131