Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpdf_simplefont.cpp
Go to the documentation of this file.
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
22namespace {
23
24void GetPredefinedEncoding(const ByteString& value, FontEncoding* basemap) {
26 *basemap = FontEncoding::kWinAnsi;
28 *basemap = FontEncoding::kMacRoman;
30 *basemap = FontEncoding::kMacExpert;
32 *basemap = FontEncoding::kPdfDoc;
33}
34
35} // namespace
36
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
46
47int 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
61void 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) {
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
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
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
154void 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") {
161 } else if (!bEmbedded && m_BaseEncoding == FontEncoding::kBuiltin) {
163 }
164 return;
165 }
166 if (pEncoding->IsName()) {
169 return;
170 }
171 if (FontStyleIsSymbolic(m_Flags) && m_BaseFontName == "Symbol") {
172 if (!bTrueType)
174 return;
175 }
176 ByteString bsEncoding = pEncoding->GetString();
179 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
180 return;
181 }
182
183 const CPDF_Dictionary* pDict = pEncoding->AsDictionary();
184 if (!pDict)
185 return;
186
189 ByteString bsEncoding = pDict->GetByteStringFor("BaseEncoding");
190 if (bTrueType && bsEncoding == pdfium::font_encodings::kMacExpertEncoding)
192 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
193 }
194 if ((!bEmbedded || bTrueType) && m_BaseEncoding == FontEncoding::kBuiltin)
196
198}
199
200int 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
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
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 {
235 }
238 LoadPDFEncoding(!!m_pFontFile, m_Font.IsTTFont());
240 m_CharNames.clear();
241 if (!HasFace()) {
242 return true;
243 }
244
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 }
264 return true;
265}
266
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)
282 }
283 m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
284 m_ItalicAngle, FX_CodePage::kDefANSI, false);
285}
286
292
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
303uint32_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
311 return !m_bUseFontWidth;
312}
fxcrt::ByteString ByteString
Definition bytestring.h:180
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition cpdf_array.h:29
bool KeyExist(const ByteString &key) const
int GetIntegerFor(const ByteString &key) const
ByteString GetByteStringFor(const ByteString &key) const
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
void LoadFontDescriptor(const CPDF_Dictionary *pFontDesc)
virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const
int m_Flags
Definition cpdf_font.h:174
virtual WideString UnicodeFromCharCode(uint32_t charcode) const
bool HasFace() const
Definition cpdf_font.h:109
void CheckFontMetrics()
bool IsEmbedded() const
Definition cpdf_font.h:100
CPDF_SimpleFont(CPDF_Document *pDocument, RetainPtr< CPDF_Dictionary > pFontDict)
~CPDF_SimpleFont() override
int GlyphFromCharCode(uint32_t charcode, bool *pVertGlyph) override
int GetCharWidthF(uint32_t charcode) override
void LoadCharMetrics(int charcode)
uint32_t CharCodeFromUnicode(wchar_t Unicode) const override
bool IsUnicodeCompatible() const override
void LoadCharWidths(const CPDF_Dictionary *font_desc)
FX_RECT GetCharBBox(uint32_t charcode) override
void LoadPDFEncoding(bool bEmbedded, bool bTrueType)
FontEncoding m_BaseEncoding
bool HasFontWidths() const override
void LoadDifferences(const CPDF_Dictionary *encoding)
WideString UnicodeFromCharCode(uint32_t charcode) const override
virtual void LoadGlyphMap()=0
bool operator==(const char *ptr) const
ByteString & operator=(const char *str)
WideString(wchar_t ch)
WideString()=default
FontEncoding
bool FontStyleIsFixedPitch(uint32_t style)
Definition fx_font.h:67
bool FontStyleIsSymbolic(uint32_t style)
Definition fx_font.h:70
bool FontStyleIsAllCaps(uint32_t style)
Definition fx_font.h:76
#define FXFONT_FIXED_PITCH
Definition fx_font.h:30
#define FXFT_Get_Glyph_HoriAdvance(face)
Definition fx_freetype.h:64
struct FT_FaceRec_ FXFT_FaceRec
Definition fx_freetype.h:24
const char kMacExpertEncoding[]
fxcrt::WideString WideString
Definition widestring.h:207