1 /*
2    Source File : Type1Input.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 #include "EStatusCode.h"
23 #include "InputPFBDecodeStream.h"
24 #include "IType1InterpreterImplementation.h"
25 
26 #include <string>
27 #include <map>
28 #include <vector>
29 #include <set>
30 
31 
32 
33 enum EType1EncodingType
34 {
35 	eType1EncodingTypeStandardEncoding,
36 	eType1EncodingTypeCustom
37 };
38 
39 struct Type1Encoding
40 {
41 	EType1EncodingType EncodingType;
42 	std::string mCustomEncoding[256];
43 };
44 
45 struct Type1FontDictionary
46 {
47 	std::string FontName;
48 	int PaintType;
49 	int FontType;
50 	double FontMatrix[6];
51 	double FontBBox[4];
52 	int UniqueID;
53 	// Metrics ignored
54 	double StrokeWidth;
55 	bool FSTypeValid;
56 	unsigned short fsType;
57 };
58 
59 struct Type1FontInfoDictionary
60 {
61 	std::string version;
62 	std::string Notice;
63 	std::string Copyright;
64 	std::string FullName;
65 	std::string FamilyName;
66 	std::string Weight;
67 	double ItalicAngle;
68 	bool isFixedPitch;
69 	double UnderlinePosition;
70 	double UnderlineThickness;
71 	bool FSTypeValid;
72 	unsigned short fsType;
73 };
74 
75 struct Type1PrivateDictionary
76 {
77 	int UniqueID;
78 	std::vector<int> BlueValues;
79 	std::vector<int> OtherBlues;
80 	std::vector<int> FamilyBlues;
81 	std::vector<int> FamilyOtherBlues;
82 	double BlueScale;
83 	int BlueShift;
84 	int BlueFuzz;
85 	double StdHW;
86 	double StdVW;
87 	std::vector<double> StemSnapH;
88 	std::vector<double> StemSnapV;
89 	bool ForceBold;
90 	int LanguageGroup;
91 	int lenIV;
92 	bool RndStemUp;
93 };
94 
95 
96 typedef std::set<Byte> ByteSet;
97 typedef std::set<unsigned short> UShortSet;
98 
99 struct CharString1Dependencies
100 {
101 	ByteSet mCharCodes; // from seac operator
102 	UShortSet mOtherSubrs; // from callothersubr
103 	UShortSet mSubrs; // from callsubr
104 };
105 
106 
107 typedef std::vector<Type1CharString> Type1CharStringVector;
108 typedef std::map<std::string,Type1CharString> StringToType1CharStringMap;
109 typedef std::map<std::string,Byte> StringToByteMap;
110 
111 class IByteReaderWithPosition;
112 
113 class Type1Input : public Type1InterpreterImplementationAdapter
114 {
115 public:
116 	Type1Input(void);
117 	~Type1Input(void);
118 
119 	PDFHummus::EStatusCode ReadType1File(IByteReaderWithPosition* inType1File);
120 	PDFHummus::EStatusCode CalculateDependenciesForCharIndex(Byte inCharStringIndex,
121 												  CharString1Dependencies& ioDependenciesInfo);
122 	PDFHummus::EStatusCode CalculateDependenciesForCharIndex(const std::string& inCharStringName,
123 												  CharString1Dependencies& ioDependenciesInfo);
124 	void Reset();
125 	Type1CharString* GetGlyphCharString(const std::string& inCharStringName);
126 	Type1CharString* GetGlyphCharString(Byte inCharStringIndex);
127 	std::string GetGlyphCharStringName(Byte inCharStringIndex);
128 	bool IsValidGlyphIndex(Byte inCharStringIndex);
129 	Byte GetEncoding(const std::string& inCharStringName);
130     bool IsCustomEncoding();
131 
132 	// some structs for you all laddies and lasses
133 	Type1FontDictionary mFontDictionary;
134 	Type1FontInfoDictionary mFontInfoDictionary;
135 	Type1PrivateDictionary mPrivateDictionary;
136 
137 	// IType1InterpreterImplementation overrides
138 	virtual Type1CharString* GetSubr(long inSubrIndex);
139 	virtual PDFHummus::EStatusCode Type1Seac(const LongList& inOperandList);
140 	virtual bool IsOtherSubrSupported(long inOtherSubrsIndex);
141 	virtual unsigned long GetLenIV();
142 
143 private:
144 	Type1Encoding mEncoding;
145 	StringToByteMap mReverseEncoding;
146 	long mSubrsCount;
147 	Type1CharString* mSubrs;
148 	StringToType1CharStringMap mCharStrings;
149 
150 	InputPFBDecodeStream mPFBDecoder;
151 
152 	CharString1Dependencies* mCurrentDependencies;
153 
154 
155 	void FreeTables();
156 	bool IsComment(const std::string& inToken);
157 	PDFHummus::EStatusCode ReadFontDictionary();
158 	PDFHummus::EStatusCode ReadFontInfoDictionary();
159 	std::string FromPSName(const std::string& inPostScriptName);
160 	PDFHummus::EStatusCode ParseEncoding();
161 	PDFHummus::EStatusCode ReadPrivateDictionary();
162 	PDFHummus::EStatusCode ParseIntVector(std::vector<int>& inVector);
163 	PDFHummus::EStatusCode ParseDoubleVector(std::vector<double>& inVector);
164 	PDFHummus::EStatusCode ParseSubrs();
165 	PDFHummus::EStatusCode ParseCharstrings();
166 	PDFHummus::EStatusCode ParseDoubleArray(double* inArray,int inArraySize);
167 	std::string FromPSString(const std::string& inPSString);
168 	void CalculateReverseEncoding();
169 };
170