1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/font/cpdf_simplefont.h"
8
9 #include "core/fpdfapi/parser/cpdf_array.h"
10 #include "core/fpdfapi/parser/cpdf_dictionary.h"
11 #include "core/fpdfapi/parser/cpdf_name.h"
12 #include "core/fxge/fx_font.h"
13 #include "core/fxge/fx_freetype.h"
14 #include "third_party/base/numerics/safe_math.h"
15 #include "third_party/base/stl_util.h"
16
17 namespace {
18
GetPredefinedEncoding(const ByteString & value,int * basemap)19 void GetPredefinedEncoding(const ByteString& value, int* basemap) {
20 if (value == "WinAnsiEncoding")
21 *basemap = PDFFONT_ENCODING_WINANSI;
22 else if (value == "MacRomanEncoding")
23 *basemap = PDFFONT_ENCODING_MACROMAN;
24 else if (value == "MacExpertEncoding")
25 *basemap = PDFFONT_ENCODING_MACEXPERT;
26 else if (value == "PDFDocEncoding")
27 *basemap = PDFFONT_ENCODING_PDFDOC;
28 }
29
30 } // namespace
31
CPDF_SimpleFont(CPDF_Document * pDocument,CPDF_Dictionary * pFontDict)32 CPDF_SimpleFont::CPDF_SimpleFont(CPDF_Document* pDocument,
33 CPDF_Dictionary* pFontDict)
34 : CPDF_Font(pDocument, pFontDict) {
35 memset(m_CharWidth, 0xff, sizeof(m_CharWidth));
36 memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex));
37 for (size_t i = 0; i < pdfium::size(m_CharBBox); ++i)
38 m_CharBBox[i] = FX_RECT(-1, -1, -1, -1);
39 }
40
41 CPDF_SimpleFont::~CPDF_SimpleFont() = default;
42
GlyphFromCharCode(uint32_t charcode,bool * pVertGlyph)43 int CPDF_SimpleFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) {
44 if (pVertGlyph)
45 *pVertGlyph = false;
46
47 if (charcode > 0xff)
48 return -1;
49
50 int index = m_GlyphIndex[charcode];
51 if (index == 0xffff)
52 return -1;
53
54 return index;
55 }
56
LoadCharMetrics(int charcode)57 void CPDF_SimpleFont::LoadCharMetrics(int charcode) {
58 if (!m_Font.GetFaceRec())
59 return;
60
61 if (charcode < 0 || charcode > 0xff) {
62 return;
63 }
64 int glyph_index = m_GlyphIndex[charcode];
65 if (glyph_index == 0xffff) {
66 if (!m_pFontFile && charcode != 32) {
67 LoadCharMetrics(32);
68 m_CharBBox[charcode] = m_CharBBox[32];
69 if (m_bUseFontWidth) {
70 m_CharWidth[charcode] = m_CharWidth[32];
71 }
72 }
73 return;
74 }
75 FXFT_FaceRec* face = m_Font.GetFaceRec();
76 int err =
77 FT_Load_Glyph(face, glyph_index,
78 FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
79 if (err)
80 return;
81
82 int iHoriBearingX = FXFT_Get_Glyph_HoriBearingX(face);
83 int iHoriBearingY = FXFT_Get_Glyph_HoriBearingY(face);
84 m_CharBBox[charcode] =
85 FX_RECT(TT2PDF(iHoriBearingX, face), TT2PDF(iHoriBearingY, face),
86 TT2PDF(iHoriBearingX + FXFT_Get_Glyph_Width(face), face),
87 TT2PDF(iHoriBearingY - FXFT_Get_Glyph_Height(face), face));
88
89 if (m_bUseFontWidth) {
90 int TT_Width = TT2PDF(FXFT_Get_Glyph_HoriAdvance(face), face);
91 if (m_CharWidth[charcode] == 0xffff) {
92 m_CharWidth[charcode] = TT_Width;
93 } else if (TT_Width && !IsEmbedded()) {
94 m_CharBBox[charcode].right =
95 m_CharBBox[charcode].right * m_CharWidth[charcode] / TT_Width;
96 m_CharBBox[charcode].left =
97 m_CharBBox[charcode].left * m_CharWidth[charcode] / TT_Width;
98 }
99 }
100 }
101
LoadPDFEncoding(bool bEmbedded,bool bTrueType)102 void CPDF_SimpleFont::LoadPDFEncoding(bool bEmbedded, bool bTrueType) {
103 const CPDF_Object* pEncoding = m_pFontDict->GetDirectObjectFor("Encoding");
104 if (!pEncoding) {
105 if (m_BaseFontName == "Symbol") {
106 m_BaseEncoding = bTrueType ? PDFFONT_ENCODING_MS_SYMBOL
107 : PDFFONT_ENCODING_ADOBE_SYMBOL;
108 } else if (!bEmbedded && m_BaseEncoding == PDFFONT_ENCODING_BUILTIN) {
109 m_BaseEncoding = PDFFONT_ENCODING_WINANSI;
110 }
111 return;
112 }
113 if (pEncoding->IsName()) {
114 if (m_BaseEncoding == PDFFONT_ENCODING_ADOBE_SYMBOL ||
115 m_BaseEncoding == PDFFONT_ENCODING_ZAPFDINGBATS) {
116 return;
117 }
118 if (FontStyleIsSymbolic(m_Flags) && m_BaseFontName == "Symbol") {
119 if (!bTrueType)
120 m_BaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL;
121 return;
122 }
123 ByteString bsEncoding = pEncoding->GetString();
124 if (bsEncoding.Compare("MacExpertEncoding") == 0) {
125 bsEncoding = "WinAnsiEncoding";
126 }
127 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
128 return;
129 }
130
131 const CPDF_Dictionary* pDict = pEncoding->AsDictionary();
132 if (!pDict)
133 return;
134
135 if (m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL &&
136 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS) {
137 ByteString bsEncoding = pDict->GetStringFor("BaseEncoding");
138 if (bTrueType && bsEncoding.Compare("MacExpertEncoding") == 0)
139 bsEncoding = "WinAnsiEncoding";
140 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
141 }
142 if ((!bEmbedded || bTrueType) && m_BaseEncoding == PDFFONT_ENCODING_BUILTIN)
143 m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
144
145 const CPDF_Array* pDiffs = pDict->GetArrayFor("Differences");
146 if (!pDiffs)
147 return;
148
149 m_CharNames.resize(256);
150 uint32_t cur_code = 0;
151 for (uint32_t i = 0; i < pDiffs->size(); i++) {
152 const CPDF_Object* pElement = pDiffs->GetDirectObjectAt(i);
153 if (!pElement)
154 continue;
155
156 const CPDF_Name* pName = pElement->AsName();
157 if (pName) {
158 if (cur_code < 256)
159 m_CharNames[cur_code] = pName->GetString();
160 cur_code++;
161 } else {
162 cur_code = pElement->GetInteger();
163 }
164 }
165 }
166
GetCharWidthF(uint32_t charcode)167 int CPDF_SimpleFont::GetCharWidthF(uint32_t charcode) {
168 if (charcode > 0xff)
169 charcode = 0;
170
171 if (m_CharWidth[charcode] == 0xffff) {
172 LoadCharMetrics(charcode);
173 if (m_CharWidth[charcode] == 0xffff) {
174 m_CharWidth[charcode] = 0;
175 }
176 }
177 return m_CharWidth[charcode];
178 }
179
GetCharBBox(uint32_t charcode)180 FX_RECT CPDF_SimpleFont::GetCharBBox(uint32_t charcode) {
181 if (charcode > 0xff)
182 charcode = 0;
183
184 if (m_CharBBox[charcode].left == -1)
185 LoadCharMetrics(charcode);
186
187 return m_CharBBox[charcode];
188 }
189
LoadCommon()190 bool CPDF_SimpleFont::LoadCommon() {
191 const CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictFor("FontDescriptor");
192 if (pFontDesc) {
193 LoadFontDescriptor(pFontDesc);
194 }
195 const CPDF_Array* pWidthArray = m_pFontDict->GetArrayFor("Widths");
196 m_bUseFontWidth = !pWidthArray;
197 if (pWidthArray) {
198 if (pFontDesc && pFontDesc->KeyExist("MissingWidth")) {
199 int MissingWidth = pFontDesc->GetIntegerFor("MissingWidth");
200 for (int i = 0; i < 256; i++) {
201 m_CharWidth[i] = MissingWidth;
202 }
203 }
204 size_t width_start = m_pFontDict->GetIntegerFor("FirstChar", 0);
205 size_t width_end = m_pFontDict->GetIntegerFor("LastChar", 0);
206 if (width_start <= 255) {
207 if (width_end == 0 || width_end >= width_start + pWidthArray->size())
208 width_end = width_start + pWidthArray->size() - 1;
209 if (width_end > 255)
210 width_end = 255;
211 for (size_t i = width_start; i <= width_end; i++)
212 m_CharWidth[i] = pWidthArray->GetIntegerAt(i - width_start);
213 }
214 }
215 if (m_pFontFile) {
216 if (m_BaseFontName.GetLength() > 8 && m_BaseFontName[7] == '+')
217 m_BaseFontName = m_BaseFontName.Last(m_BaseFontName.GetLength() - 8);
218 } else {
219 LoadSubstFont();
220 }
221 if (!FontStyleIsSymbolic(m_Flags))
222 m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
223 LoadPDFEncoding(!!m_pFontFile, m_Font.IsTTFont());
224 LoadGlyphMap();
225 m_CharNames.clear();
226 if (!m_Font.GetFaceRec())
227 return true;
228
229 if (FontStyleIsAllCaps(m_Flags)) {
230 static const unsigned char kLowercases[][2] = {
231 {'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}};
232 for (size_t range = 0; range < pdfium::size(kLowercases); ++range) {
233 const auto& lower = kLowercases[range];
234 for (int i = lower[0]; i <= lower[1]; ++i) {
235 if (m_GlyphIndex[i] != 0xffff && m_pFontFile)
236 continue;
237
238 int j = i - 32;
239 m_GlyphIndex[i] = m_GlyphIndex[j];
240 if (m_CharWidth[j]) {
241 m_CharWidth[i] = m_CharWidth[j];
242 m_CharBBox[i] = m_CharBBox[j];
243 }
244 }
245 }
246 }
247 CheckFontMetrics();
248 return true;
249 }
250
LoadSubstFont()251 void CPDF_SimpleFont::LoadSubstFont() {
252 if (!m_bUseFontWidth && !FontStyleIsFixedPitch(m_Flags)) {
253 int width = 0, i;
254 for (i = 0; i < 256; i++) {
255 if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff)
256 continue;
257
258 if (width == 0)
259 width = m_CharWidth[i];
260 else if (width != m_CharWidth[i])
261 break;
262 }
263 if (i == 256 && width)
264 m_Flags |= FXFONT_FIXED_PITCH;
265 }
266 m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
267 m_ItalicAngle, 0, false);
268 }
269
IsUnicodeCompatible() const270 bool CPDF_SimpleFont::IsUnicodeCompatible() const {
271 return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN &&
272 m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL &&
273 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS;
274 }
275
UnicodeFromCharCode(uint32_t charcode) const276 WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const {
277 WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode);
278 if (!unicode.IsEmpty())
279 return unicode;
280 wchar_t ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
281 if (ret == 0)
282 return WideString();
283 return ret;
284 }
285
CharCodeFromUnicode(wchar_t unicode) const286 uint32_t CPDF_SimpleFont::CharCodeFromUnicode(wchar_t unicode) const {
287 uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode);
288 if (ret)
289 return ret;
290 return m_Encoding.CharCodeFromUnicode(unicode);
291 }
292
HasFontWidths() const293 bool CPDF_SimpleFont::HasFontWidths() const {
294 return !m_bUseFontWidth;
295 }
296