1 // Copyright 2016 The PDFium Authors
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 <algorithm>
10 #include <iterator>
11 #include <utility>
12
13 #include "constants/font_encodings.h"
14 #include "core/fpdfapi/parser/cpdf_array.h"
15 #include "core/fpdfapi/parser/cpdf_dictionary.h"
16 #include "core/fpdfapi/parser/cpdf_name.h"
17 #include "core/fxcrt/fx_codepage.h"
18 #include "core/fxge/freetype/fx_freetype.h"
19 #include "core/fxge/fx_font.h"
20 #include "third_party/base/numerics/safe_math.h"
21
22 namespace {
23
GetPredefinedEncoding(const ByteString & value,FontEncoding * basemap)24 void GetPredefinedEncoding(const ByteString& value, FontEncoding* basemap) {
25 if (value == pdfium::font_encodings::kWinAnsiEncoding)
26 *basemap = FontEncoding::kWinAnsi;
27 else if (value == pdfium::font_encodings::kMacRomanEncoding)
28 *basemap = FontEncoding::kMacRoman;
29 else if (value == pdfium::font_encodings::kMacExpertEncoding)
30 *basemap = FontEncoding::kMacExpert;
31 else if (value == pdfium::font_encodings::kPDFDocEncoding)
32 *basemap = FontEncoding::kPdfDoc;
33 }
34
35 } // namespace
36
CPDF_SimpleFont(CPDF_Document * pDocument,RetainPtr<CPDF_Dictionary> pFontDict)37 CPDF_SimpleFont::CPDF_SimpleFont(CPDF_Document* pDocument,
38 RetainPtr<CPDF_Dictionary> pFontDict)
39 : CPDF_Font(pDocument, std::move(pFontDict)) {
40 memset(m_CharWidth, 0xff, sizeof(m_CharWidth));
41 memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex));
42 for (size_t i = 0; i < std::size(m_CharBBox); ++i)
43 m_CharBBox[i] = FX_RECT(-1, -1, -1, -1);
44 }
45
46 CPDF_SimpleFont::~CPDF_SimpleFont() = default;
47
GlyphFromCharCode(uint32_t charcode,bool * pVertGlyph)48 int CPDF_SimpleFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) {
49 if (pVertGlyph)
50 *pVertGlyph = false;
51
52 if (charcode > 0xff)
53 return -1;
54
55 int index = m_GlyphIndex[charcode];
56 if (index == 0xffff)
57 return -1;
58
59 return index;
60 }
61
LoadCharMetrics(int charcode)62 void CPDF_SimpleFont::LoadCharMetrics(int charcode) {
63 if (!m_Font.GetFaceRec())
64 return;
65
66 if (charcode < 0 || charcode > 0xff) {
67 return;
68 }
69 int glyph_index = m_GlyphIndex[charcode];
70 if (glyph_index == 0xffff) {
71 if (!m_pFontFile && charcode != 32) {
72 LoadCharMetrics(32);
73 m_CharBBox[charcode] = m_CharBBox[32];
74 if (m_bUseFontWidth) {
75 m_CharWidth[charcode] = m_CharWidth[32];
76 }
77 }
78 return;
79 }
80 FXFT_FaceRec* face = m_Font.GetFaceRec();
81 int err =
82 FT_Load_Glyph(face, glyph_index,
83 FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
84 if (err)
85 return;
86
87 FT_Pos iHoriBearingX = FXFT_Get_Glyph_HoriBearingX(face);
88 FT_Pos iHoriBearingY = FXFT_Get_Glyph_HoriBearingY(face);
89 m_CharBBox[charcode] =
90 FX_RECT(TT2PDF(iHoriBearingX, face), TT2PDF(iHoriBearingY, face),
91 TT2PDF(iHoriBearingX + FXFT_Get_Glyph_Width(face), face),
92 TT2PDF(iHoriBearingY - FXFT_Get_Glyph_Height(face), face));
93
94 if (m_bUseFontWidth) {
95 int TT_Width = TT2PDF(FXFT_Get_Glyph_HoriAdvance(face), face);
96 if (m_CharWidth[charcode] == 0xffff) {
97 m_CharWidth[charcode] = TT_Width;
98 } else if (TT_Width && !IsEmbedded()) {
99 m_CharBBox[charcode].right =
100 m_CharBBox[charcode].right * m_CharWidth[charcode] / TT_Width;
101 m_CharBBox[charcode].left =
102 m_CharBBox[charcode].left * m_CharWidth[charcode] / TT_Width;
103 }
104 }
105 }
106
LoadCharWidths(const CPDF_Dictionary * font_desc)107 void CPDF_SimpleFont::LoadCharWidths(const CPDF_Dictionary* font_desc) {
108 RetainPtr<const CPDF_Array> width_array = m_pFontDict->GetArrayFor("Widths");
109 m_bUseFontWidth = !width_array;
110 if (!width_array)
111 return;
112
113 if (font_desc && font_desc->KeyExist("MissingWidth")) {
114 int missing_width = font_desc->GetIntegerFor("MissingWidth");
115 std::fill(std::begin(m_CharWidth), std::end(m_CharWidth), missing_width);
116 }
117
118 size_t width_start = m_pFontDict->GetIntegerFor("FirstChar", 0);
119 size_t width_end = m_pFontDict->GetIntegerFor("LastChar", 0);
120 if (width_start > 255)
121 return;
122
123 if (width_end == 0 || width_end >= width_start + width_array->size())
124 width_end = width_start + width_array->size() - 1;
125 if (width_end > 255)
126 width_end = 255;
127 for (size_t i = width_start; i <= width_end; i++)
128 m_CharWidth[i] = width_array->GetIntegerAt(i - width_start);
129 }
130
LoadDifferences(const CPDF_Dictionary * encoding)131 void CPDF_SimpleFont::LoadDifferences(const CPDF_Dictionary* encoding) {
132 RetainPtr<const CPDF_Array> diffs = encoding->GetArrayFor("Differences");
133 if (!diffs)
134 return;
135
136 m_CharNames.resize(kInternalTableSize);
137 uint32_t cur_code = 0;
138 for (uint32_t i = 0; i < diffs->size(); i++) {
139 RetainPtr<const CPDF_Object> element = diffs->GetDirectObjectAt(i);
140 if (!element)
141 continue;
142
143 const CPDF_Name* name = element->AsName();
144 if (name) {
145 if (cur_code < m_CharNames.size())
146 m_CharNames[cur_code] = name->GetString();
147 cur_code++;
148 } else {
149 cur_code = element->GetInteger();
150 }
151 }
152 }
153
LoadPDFEncoding(bool bEmbedded,bool bTrueType)154 void CPDF_SimpleFont::LoadPDFEncoding(bool bEmbedded, bool bTrueType) {
155 RetainPtr<const CPDF_Object> pEncoding =
156 m_pFontDict->GetDirectObjectFor("Encoding");
157 if (!pEncoding) {
158 if (m_BaseFontName == "Symbol") {
159 m_BaseEncoding =
160 bTrueType ? FontEncoding::kMsSymbol : FontEncoding::kAdobeSymbol;
161 } else if (!bEmbedded && m_BaseEncoding == FontEncoding::kBuiltin) {
162 m_BaseEncoding = FontEncoding::kWinAnsi;
163 }
164 return;
165 }
166 if (pEncoding->IsName()) {
167 if (m_BaseEncoding == FontEncoding::kAdobeSymbol ||
168 m_BaseEncoding == FontEncoding::kZapfDingbats) {
169 return;
170 }
171 if (FontStyleIsSymbolic(m_Flags) && m_BaseFontName == "Symbol") {
172 if (!bTrueType)
173 m_BaseEncoding = FontEncoding::kAdobeSymbol;
174 return;
175 }
176 ByteString bsEncoding = pEncoding->GetString();
177 if (bsEncoding == pdfium::font_encodings::kMacExpertEncoding)
178 bsEncoding = pdfium::font_encodings::kWinAnsiEncoding;
179 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
180 return;
181 }
182
183 const CPDF_Dictionary* pDict = pEncoding->AsDictionary();
184 if (!pDict)
185 return;
186
187 if (m_BaseEncoding != FontEncoding::kAdobeSymbol &&
188 m_BaseEncoding != FontEncoding::kZapfDingbats) {
189 ByteString bsEncoding = pDict->GetByteStringFor("BaseEncoding");
190 if (bTrueType && bsEncoding == pdfium::font_encodings::kMacExpertEncoding)
191 bsEncoding = pdfium::font_encodings::kWinAnsiEncoding;
192 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
193 }
194 if ((!bEmbedded || bTrueType) && m_BaseEncoding == FontEncoding::kBuiltin)
195 m_BaseEncoding = FontEncoding::kStandard;
196
197 LoadDifferences(pDict);
198 }
199
GetCharWidthF(uint32_t charcode)200 int CPDF_SimpleFont::GetCharWidthF(uint32_t charcode) {
201 if (charcode > 0xff)
202 charcode = 0;
203
204 if (m_CharWidth[charcode] == 0xffff) {
205 LoadCharMetrics(charcode);
206 if (m_CharWidth[charcode] == 0xffff) {
207 m_CharWidth[charcode] = 0;
208 }
209 }
210 return m_CharWidth[charcode];
211 }
212
GetCharBBox(uint32_t charcode)213 FX_RECT CPDF_SimpleFont::GetCharBBox(uint32_t charcode) {
214 if (charcode > 0xff)
215 charcode = 0;
216
217 if (m_CharBBox[charcode].left == -1)
218 LoadCharMetrics(charcode);
219
220 return m_CharBBox[charcode];
221 }
222
LoadCommon()223 bool CPDF_SimpleFont::LoadCommon() {
224 RetainPtr<const CPDF_Dictionary> pFontDesc =
225 m_pFontDict->GetDictFor("FontDescriptor");
226 if (pFontDesc)
227 LoadFontDescriptor(pFontDesc.Get());
228 LoadCharWidths(pFontDesc.Get());
229 if (m_pFontFile) {
230 if (m_BaseFontName.GetLength() > 8 && m_BaseFontName[7] == '+')
231 m_BaseFontName = m_BaseFontName.Last(m_BaseFontName.GetLength() - 8);
232 } else {
233 LoadSubstFont();
234 }
235 if (!FontStyleIsSymbolic(m_Flags))
236 m_BaseEncoding = FontEncoding::kStandard;
237 LoadPDFEncoding(!!m_pFontFile, m_Font.IsTTFont());
238 LoadGlyphMap();
239 m_CharNames.clear();
240 if (!m_Font.GetFaceRec())
241 return true;
242
243 if (FontStyleIsAllCaps(m_Flags)) {
244 static const unsigned char kLowercases[][2] = {
245 {'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}};
246 for (size_t range = 0; range < std::size(kLowercases); ++range) {
247 const auto& lower = kLowercases[range];
248 for (int i = lower[0]; i <= lower[1]; ++i) {
249 if (m_GlyphIndex[i] != 0xffff && m_pFontFile)
250 continue;
251
252 int j = i - 32;
253 m_GlyphIndex[i] = m_GlyphIndex[j];
254 if (m_CharWidth[j]) {
255 m_CharWidth[i] = m_CharWidth[j];
256 m_CharBBox[i] = m_CharBBox[j];
257 }
258 }
259 }
260 }
261 CheckFontMetrics();
262 return true;
263 }
264
LoadSubstFont()265 void CPDF_SimpleFont::LoadSubstFont() {
266 if (!m_bUseFontWidth && !FontStyleIsFixedPitch(m_Flags)) {
267 int width = 0;
268 size_t i;
269 for (i = 0; i < kInternalTableSize; i++) {
270 if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff)
271 continue;
272
273 if (width == 0)
274 width = m_CharWidth[i];
275 else if (width != m_CharWidth[i])
276 break;
277 }
278 if (i == kInternalTableSize && width)
279 m_Flags |= FXFONT_FIXED_PITCH;
280 }
281 m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
282 m_ItalicAngle, FX_CodePage::kDefANSI, false);
283 }
284
IsUnicodeCompatible() const285 bool CPDF_SimpleFont::IsUnicodeCompatible() const {
286 return m_BaseEncoding != FontEncoding::kBuiltin &&
287 m_BaseEncoding != FontEncoding::kAdobeSymbol &&
288 m_BaseEncoding != FontEncoding::kZapfDingbats;
289 }
290
UnicodeFromCharCode(uint32_t charcode) const291 WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const {
292 WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode);
293 if (!unicode.IsEmpty())
294 return unicode;
295 wchar_t ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
296 if (ret == 0)
297 return WideString();
298 return WideString(ret);
299 }
300
CharCodeFromUnicode(wchar_t unicode) const301 uint32_t CPDF_SimpleFont::CharCodeFromUnicode(wchar_t unicode) const {
302 uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode);
303 if (ret)
304 return ret;
305 return m_Encoding.CharCodeFromUnicode(unicode);
306 }
307
HasFontWidths() const308 bool CPDF_SimpleFont::HasFontWidths() const {
309 return !m_bUseFontWidth;
310 }
311