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/fxge/fx_freetype.h"
12 #include "third_party/base/numerics/safe_math.h"
13
CPDF_SimpleFont()14 CPDF_SimpleFont::CPDF_SimpleFont() : m_BaseEncoding(PDFFONT_ENCODING_BUILTIN) {
15 memset(m_CharWidth, 0xff, sizeof(m_CharWidth));
16 memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex));
17 memset(m_ExtGID, 0xff, sizeof(m_ExtGID));
18 for (size_t i = 0; i < FX_ArraySize(m_CharBBox); ++i)
19 m_CharBBox[i] = FX_RECT(-1, -1, -1, -1);
20 }
21
~CPDF_SimpleFont()22 CPDF_SimpleFont::~CPDF_SimpleFont() {}
23
GlyphFromCharCode(uint32_t charcode,bool * pVertGlyph)24 int CPDF_SimpleFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) {
25 if (pVertGlyph)
26 *pVertGlyph = false;
27
28 if (charcode > 0xff)
29 return -1;
30
31 int index = m_GlyphIndex[charcode];
32 if (index == 0xffff || (index == 0 && IsTrueTypeFont()))
33 return -1;
34
35 return index;
36 }
37
LoadCharMetrics(int charcode)38 void CPDF_SimpleFont::LoadCharMetrics(int charcode) {
39 if (!m_Font.GetFace())
40 return;
41
42 if (charcode < 0 || charcode > 0xff) {
43 return;
44 }
45 int glyph_index = m_GlyphIndex[charcode];
46 if (glyph_index == 0xffff) {
47 if (!m_pFontFile && charcode != 32) {
48 LoadCharMetrics(32);
49 m_CharBBox[charcode] = m_CharBBox[32];
50 if (m_bUseFontWidth) {
51 m_CharWidth[charcode] = m_CharWidth[32];
52 }
53 }
54 return;
55 }
56 FXFT_Face face = m_Font.GetFace();
57 int err = FXFT_Load_Glyph(
58 face, glyph_index,
59 FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
60 if (err)
61 return;
62
63 int iHoriBearingX = FXFT_Get_Glyph_HoriBearingX(face);
64 int iHoriBearingY = FXFT_Get_Glyph_HoriBearingY(face);
65 m_CharBBox[charcode] =
66 FX_RECT(TT2PDF(iHoriBearingX, face), TT2PDF(iHoriBearingY, face),
67 TT2PDF(iHoriBearingX + FXFT_Get_Glyph_Width(face), face),
68 TT2PDF(iHoriBearingY - FXFT_Get_Glyph_Height(face), face));
69
70 if (m_bUseFontWidth) {
71 int TT_Width = TT2PDF(FXFT_Get_Glyph_HoriAdvance(face), face);
72 if (m_CharWidth[charcode] == 0xffff) {
73 m_CharWidth[charcode] = TT_Width;
74 } else if (TT_Width && !IsEmbedded()) {
75 m_CharBBox[charcode].right =
76 m_CharBBox[charcode].right * m_CharWidth[charcode] / TT_Width;
77 m_CharBBox[charcode].left =
78 m_CharBBox[charcode].left * m_CharWidth[charcode] / TT_Width;
79 }
80 }
81 }
82
GetCharWidthF(uint32_t charcode)83 int CPDF_SimpleFont::GetCharWidthF(uint32_t charcode) {
84 if (charcode > 0xff)
85 charcode = 0;
86
87 if (m_CharWidth[charcode] == 0xffff) {
88 LoadCharMetrics(charcode);
89 if (m_CharWidth[charcode] == 0xffff) {
90 m_CharWidth[charcode] = 0;
91 }
92 }
93 return m_CharWidth[charcode];
94 }
95
GetCharBBox(uint32_t charcode)96 FX_RECT CPDF_SimpleFont::GetCharBBox(uint32_t charcode) {
97 if (charcode > 0xff)
98 charcode = 0;
99
100 if (m_CharBBox[charcode].left == -1)
101 LoadCharMetrics(charcode);
102
103 return m_CharBBox[charcode];
104 }
105
LoadCommon()106 bool CPDF_SimpleFont::LoadCommon() {
107 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictFor("FontDescriptor");
108 if (pFontDesc) {
109 LoadFontDescriptor(pFontDesc);
110 }
111 CPDF_Array* pWidthArray = m_pFontDict->GetArrayFor("Widths");
112 m_bUseFontWidth = !pWidthArray;
113 if (pWidthArray) {
114 if (pFontDesc && pFontDesc->KeyExist("MissingWidth")) {
115 int MissingWidth = pFontDesc->GetIntegerFor("MissingWidth");
116 for (int i = 0; i < 256; i++) {
117 m_CharWidth[i] = MissingWidth;
118 }
119 }
120 size_t width_start = m_pFontDict->GetIntegerFor("FirstChar", 0);
121 size_t width_end = m_pFontDict->GetIntegerFor("LastChar", 0);
122 if (width_start <= 255) {
123 if (width_end == 0 || width_end >= width_start + pWidthArray->GetCount())
124 width_end = width_start + pWidthArray->GetCount() - 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] = pWidthArray->GetIntegerAt(i - width_start);
129 }
130 }
131 if (m_pFontFile) {
132 if (m_BaseFont.GetLength() > 8 && m_BaseFont[7] == '+')
133 m_BaseFont = m_BaseFont.Right(m_BaseFont.GetLength() - 8);
134 } else {
135 LoadSubstFont();
136 }
137 if (!FontStyleIsSymbolic(m_Flags))
138 m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
139 CPDF_Object* pEncoding = m_pFontDict->GetDirectObjectFor("Encoding");
140 LoadPDFEncoding(pEncoding, m_BaseEncoding, &m_CharNames, !!m_pFontFile,
141 m_Font.IsTTFont());
142 LoadGlyphMap();
143 m_CharNames.clear();
144 if (!m_Font.GetFace())
145 return true;
146
147 if (FontStyleIsAllCaps(m_Flags)) {
148 unsigned char kLowercases[][2] = {{'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}};
149 for (size_t range = 0; range < FX_ArraySize(kLowercases); ++range) {
150 const auto& lower = kLowercases[range];
151 for (int i = lower[0]; i <= lower[1]; ++i) {
152 if (m_GlyphIndex[i] != 0xffff && m_pFontFile)
153 continue;
154
155 int j = i - 32;
156 m_GlyphIndex[i] = m_GlyphIndex[j];
157 if (m_CharWidth[j]) {
158 m_CharWidth[i] = m_CharWidth[j];
159 m_CharBBox[i] = m_CharBBox[j];
160 }
161 }
162 }
163 }
164 CheckFontMetrics();
165 return true;
166 }
167
LoadSubstFont()168 void CPDF_SimpleFont::LoadSubstFont() {
169 if (!m_bUseFontWidth && !FontStyleIsFixedPitch(m_Flags)) {
170 int width = 0, i;
171 for (i = 0; i < 256; i++) {
172 if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff)
173 continue;
174
175 if (width == 0)
176 width = m_CharWidth[i];
177 else if (width != m_CharWidth[i])
178 break;
179 }
180 if (i == 256 && width)
181 m_Flags |= FXFONT_FIXED_PITCH;
182 }
183 pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
184 if (m_StemV < 140)
185 safeStemV *= 5;
186 else
187 safeStemV = safeStemV * 4 + 140;
188 m_Font.LoadSubst(m_BaseFont, IsTrueTypeFont(), m_Flags,
189 safeStemV.ValueOrDefault(FXFONT_FW_NORMAL), m_ItalicAngle, 0,
190 false);
191 }
192
IsUnicodeCompatible() const193 bool CPDF_SimpleFont::IsUnicodeCompatible() const {
194 return m_BaseEncoding != PDFFONT_ENCODING_BUILTIN &&
195 m_BaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL &&
196 m_BaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS;
197 }
198
UnicodeFromCharCode(uint32_t charcode) const199 WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const {
200 WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode);
201 if (!unicode.IsEmpty())
202 return unicode;
203 wchar_t ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
204 if (ret == 0)
205 return WideString();
206 return ret;
207 }
208
CharCodeFromUnicode(wchar_t unicode) const209 uint32_t CPDF_SimpleFont::CharCodeFromUnicode(wchar_t unicode) const {
210 uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode);
211 if (ret)
212 return ret;
213 return m_Encoding.CharCodeFromUnicode(unicode);
214 }
215
HasFontWidths() const216 bool CPDF_SimpleFont::HasFontWidths() const {
217 return !m_bUseFontWidth;
218 }
219