1 /*
2    Source File : FreeTypeType1Wrapper.cpp
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 #include "FreeTypeType1Wrapper.h"
22 #include "InputFile.h"
23 #include "Trace.h"
24 
25 
26 
FreeTypeType1Wrapper(FT_Face inFace,const std::string & inFontFilePath,const std::string & inPFMFilePath)27 FreeTypeType1Wrapper::FreeTypeType1Wrapper(FT_Face inFace,const std::string& inFontFilePath,const std::string& inPFMFilePath)
28 {
29 	if(FT_Get_PS_Font_Info(inFace,&mPSFontInfo) != 0)
30 	{
31 		TRACE_LOG("Unexpected failure in FreeTypeType1Wrapper::FreeTypeType1Wrapper. could not retrieve PS font info");
32 		mPSavailable = false; // this is obviousy an exception
33 	}
34 	else
35 		mPSavailable = true;
36 
37 
38 	if(FT_Get_PS_Font_Private(inFace,&mPrivateInfo) != 0)
39 	{
40 		TRACE_LOG("Unexpected failure in FreeTypeType1Wrapper::FreeTypeType1Wrapper. could not retrieve PS private font info");
41 		mPSPrivateAvailable = false; // this is obviousy an exception
42 	}
43 	else
44 		mPSPrivateAvailable = true;
45 
46     T1_EncodingType encodingType;
47     FT_Get_PS_Font_Value(inFace, PS_DICT_ENCODING_TYPE, 0, (void*)&encodingType, sizeof(encodingType));
48     mIsCustomEncoding = encodingType == T1_ENCODING_TYPE_ARRAY;
49 
50 	mPFMFileInfoRelevant =
51 		(inPFMFilePath.size() != 0 && mPFMReader.Read(inPFMFilePath) != PDFHummus::eFailure);
52 
53     // parse type 1 input file (my own parsing), to get extra info about encoding
54     if(inFontFilePath.size() != 0)
55     {
56         InputFile type1File;
57 
58         type1File.OpenFile(inFontFilePath);
59         mType1File.ReadType1File(type1File.GetInputStream());
60 
61         type1File.CloseFile();
62     }
63 
64     mFace = inFace;
65 }
66 
~FreeTypeType1Wrapper(void)67 FreeTypeType1Wrapper::~FreeTypeType1Wrapper(void)
68 {
69 }
70 
GetItalicAngle()71 double FreeTypeType1Wrapper::GetItalicAngle()
72 {
73 	return mPSavailable ? (double)mPSFontInfo.italic_angle : 0;
74 }
75 
GetCapHeight()76 BoolAndFTShort FreeTypeType1Wrapper::GetCapHeight()
77 {
78 	if(mPFMFileInfoRelevant)
79 		return BoolAndFTShort(true,mPFMReader.ExtendedFontMetrics.CapHeight);
80 	else
81 		return BoolAndFTShort(false,0);
82 }
83 
GetxHeight()84 BoolAndFTShort FreeTypeType1Wrapper::GetxHeight()
85 {
86 	if(mPFMFileInfoRelevant)
87 		return BoolAndFTShort(true,mPFMReader.ExtendedFontMetrics.XHeight);
88 	else
89 		return BoolAndFTShort(false,0);
90 }
91 
GetStemV()92 FT_UShort FreeTypeType1Wrapper::GetStemV()
93 {
94 	// StemV of PDF matches the StdWV of type 1...so piece of cake here
95 	return mPSPrivateAvailable ? mPrivateInfo.standard_width[0]:0;
96 }
97 
GetFontStretch()98 EFontStretch FreeTypeType1Wrapper::GetFontStretch()
99 {
100 	return eFontStretchUknown;
101 }
102 
GetFontWeight()103 FT_UShort FreeTypeType1Wrapper::GetFontWeight()
104 {
105 	// ahhh. using the weight factor is as good as analyzing the name...whatever. so just return the default value
106 	return 1000;
107 }
108 
HasSerifs()109 bool FreeTypeType1Wrapper::HasSerifs()
110 {
111 	if(mPFMFileInfoRelevant)
112 		return (mPFMReader.Header.PitchAndFamily & 16) != 0;
113 	else
114 		return false;
115 
116 }
117 
IsScript()118 bool FreeTypeType1Wrapper::IsScript()
119 {
120 	if(mPFMFileInfoRelevant)
121 		return (mPFMReader.Header.PitchAndFamily & 64) != 0;
122 	else
123 		return false;
124 }
125 
IsForceBold()126 bool FreeTypeType1Wrapper::IsForceBold()
127 {
128 	return mPSPrivateAvailable ? (1 == mPrivateInfo.force_bold) : false;
129 }
130 
HasPrivateEncoding()131 bool FreeTypeType1Wrapper::HasPrivateEncoding()
132 {
133     return mIsCustomEncoding;
134 }
135 
GetGlyphForUnicodeChar(unsigned long inChar)136 unsigned int FreeTypeType1Wrapper::GetGlyphForUnicodeChar(unsigned long inChar)
137 {
138     return (unsigned int)inChar; // will run only if custom encoding, in which case input value should be output value
139 }
140 
141 
GetPrivateGlyphName(unsigned int inGlyphIndex)142 std::string FreeTypeType1Wrapper::GetPrivateGlyphName(unsigned int inGlyphIndex)
143 {
144     return mType1File.GetGlyphCharStringName(inGlyphIndex);
145 }
146 
GetFreeTypeGlyphIndexFromEncodingGlyphIndex(unsigned int inGlyphIndex)147 unsigned int FreeTypeType1Wrapper::GetFreeTypeGlyphIndexFromEncodingGlyphIndex(unsigned int inGlyphIndex)
148 {
149     return FT_Get_Name_Index(mFace,(FT_String*)(GetPrivateGlyphName(inGlyphIndex).c_str()));
150 }
151 
152