• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fxcrt/stl_util.h"
19 #include "core/fxge/freetype/fx_freetype.h"
20 #include "core/fxge/fx_font.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   m_CharWidth.fill(0xffff);
41   m_GlyphIndex.fill(0xffff);
42   m_CharBBox.fill(FX_RECT(-1, -1, -1, -1));
43 }
44 
45 CPDF_SimpleFont::~CPDF_SimpleFont() = default;
46 
GlyphFromCharCode(uint32_t charcode,bool * pVertGlyph)47 int CPDF_SimpleFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) {
48   if (pVertGlyph)
49     *pVertGlyph = false;
50 
51   if (charcode > 0xff)
52     return -1;
53 
54   int index = m_GlyphIndex[charcode];
55   if (index == 0xffff)
56     return -1;
57 
58   return index;
59 }
60 
LoadCharMetrics(int charcode)61 void CPDF_SimpleFont::LoadCharMetrics(int charcode) {
62   if (!m_Font.GetFaceRec())
63     return;
64 
65   if (charcode < 0 || charcode > 0xff) {
66     return;
67   }
68   int glyph_index = m_GlyphIndex[charcode];
69   if (glyph_index == 0xffff) {
70     if (!m_pFontFile && charcode != 32) {
71       LoadCharMetrics(32);
72       m_CharBBox[charcode] = m_CharBBox[32];
73       if (m_bUseFontWidth) {
74         m_CharWidth[charcode] = m_CharWidth[32];
75       }
76     }
77     return;
78   }
79   RetainPtr<CFX_Face> face = m_Font.GetFace();
80   if (!face) {
81     return;
82   }
83 
84   FXFT_FaceRec* face_rec = face->GetRec();
85   int err =
86       FT_Load_Glyph(face_rec, glyph_index,
87                     FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
88   if (err)
89     return;
90 
91   m_CharBBox[charcode] = face->GetGlyphBBox();
92 
93   if (m_bUseFontWidth) {
94     int TT_Width = NormalizeFontMetric(FXFT_Get_Glyph_HoriAdvance(face_rec),
95                                        face->GetUnitsPerEm());
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() > 7 && m_BaseFontName[6] == '+') {
231       m_BaseFontName = m_BaseFontName.Last(m_BaseFontName.GetLength() - 7);
232     }
233   } else {
234     LoadSubstFont();
235   }
236   if (!FontStyleIsSymbolic(m_Flags))
237     m_BaseEncoding = FontEncoding::kStandard;
238   LoadPDFEncoding(!!m_pFontFile, m_Font.IsTTFont());
239   LoadGlyphMap();
240   m_CharNames.clear();
241   if (!HasFace()) {
242     return true;
243   }
244 
245   if (FontStyleIsAllCaps(m_Flags)) {
246     static const auto kLowercases =
247         fxcrt::ToArray<std::pair<const uint8_t, const uint8_t>>(
248             {{'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}});
249     for (const auto& lower : kLowercases) {
250       for (int i = lower.first; i <= lower.second; ++i) {
251         if (m_GlyphIndex[i] != 0xffff && m_pFontFile) {
252           continue;
253         }
254         int j = i - 32;
255         m_GlyphIndex[i] = m_GlyphIndex[j];
256         if (m_CharWidth[j]) {
257           m_CharWidth[i] = m_CharWidth[j];
258           m_CharBBox[i] = m_CharBBox[j];
259         }
260       }
261     }
262   }
263   CheckFontMetrics();
264   return true;
265 }
266 
LoadSubstFont()267 void CPDF_SimpleFont::LoadSubstFont() {
268   if (!m_bUseFontWidth && !FontStyleIsFixedPitch(m_Flags)) {
269     int width = 0;
270     size_t i;
271     for (i = 0; i < kInternalTableSize; i++) {
272       if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff)
273         continue;
274 
275       if (width == 0)
276         width = m_CharWidth[i];
277       else if (width != m_CharWidth[i])
278         break;
279     }
280     if (i == kInternalTableSize && width)
281       m_Flags |= FXFONT_FIXED_PITCH;
282   }
283   m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
284                    m_ItalicAngle, FX_CodePage::kDefANSI, false);
285 }
286 
IsUnicodeCompatible() const287 bool CPDF_SimpleFont::IsUnicodeCompatible() const {
288   return m_BaseEncoding != FontEncoding::kBuiltin &&
289          m_BaseEncoding != FontEncoding::kAdobeSymbol &&
290          m_BaseEncoding != FontEncoding::kZapfDingbats;
291 }
292 
UnicodeFromCharCode(uint32_t charcode) const293 WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const {
294   WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode);
295   if (!unicode.IsEmpty())
296     return unicode;
297   wchar_t ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
298   if (ret == 0)
299     return WideString();
300   return WideString(ret);
301 }
302 
CharCodeFromUnicode(wchar_t unicode) const303 uint32_t CPDF_SimpleFont::CharCodeFromUnicode(wchar_t unicode) const {
304   uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode);
305   if (ret)
306     return ret;
307   return m_Encoding.CharCodeFromUnicode(unicode);
308 }
309 
HasFontWidths() const310 bool CPDF_SimpleFont::HasFontWidths() const {
311   return !m_bUseFontWidth;
312 }
313