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