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/fxge/freetype/fx_freetype.h"
19#include "core/fxge/fx_font.h"
20
21namespace {
22
23void GetPredefinedEncoding(const ByteString& value, FontEncoding* basemap) {
25 *basemap = FontEncoding::kWinAnsi;
26 else if (value == pdfium::font_encodings::kMacRomanEncoding)
27 *basemap = FontEncoding::kMacRoman;
28 else if (value == pdfium::font_encodings::kMacExpertEncoding)
29 *basemap = FontEncoding::kMacExpert;
30 else if (value == pdfium::font_encodings::kPDFDocEncoding)
31 *basemap = FontEncoding::kPdfDoc;
32}
33
34} // namespace
35
37 RetainPtr<CPDF_Dictionary> pFontDict)
38 : CPDF_Font(pDocument, std::move(pFontDict)) {
39 memset(m_CharWidth, 0xff, sizeof(m_CharWidth));
40 memset(m_GlyphIndex, 0xff, sizeof(m_GlyphIndex));
41 for (size_t i = 0; i < std::size(m_CharBBox); ++i)
42 m_CharBBox[i] = 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] = GetCharBBoxForFace(face);
92
93 if (m_bUseFontWidth) {
94 int TT_Width = TT2PDF(FXFT_Get_Glyph_HoriAdvance(face_rec), face);
95 if (m_CharWidth[charcode] == 0xffff) {
96 m_CharWidth[charcode] = TT_Width;
97 } else if (TT_Width && !IsEmbedded()) {
98 m_CharBBox[charcode].right =
99 m_CharBBox[charcode].right * m_CharWidth[charcode] / TT_Width;
100 m_CharBBox[charcode].left =
101 m_CharBBox[charcode].left * m_CharWidth[charcode] / TT_Width;
102 }
103 }
104}
105
106void CPDF_SimpleFont::LoadCharWidths(const CPDF_Dictionary* font_desc) {
107 RetainPtr<const CPDF_Array> width_array = m_pFontDict->GetArrayFor("Widths");
108 m_bUseFontWidth = !width_array;
109 if (!width_array)
110 return;
111
112 if (font_desc && font_desc->KeyExist("MissingWidth")) {
113 int missing_width = font_desc->GetIntegerFor("MissingWidth");
114 std::fill(std::begin(m_CharWidth), std::end(m_CharWidth), missing_width);
115 }
116
117 size_t width_start = m_pFontDict->GetIntegerFor("FirstChar", 0);
118 size_t width_end = m_pFontDict->GetIntegerFor("LastChar", 0);
119 if (width_start > 255)
120 return;
121
122 if (width_end == 0 || width_end >= width_start + width_array->size())
123 width_end = width_start + width_array->size() - 1;
124 if (width_end > 255)
125 width_end = 255;
126 for (size_t i = width_start; i <= width_end; i++)
127 m_CharWidth[i] = width_array->GetIntegerAt(i - width_start);
128}
129
130void CPDF_SimpleFont::LoadDifferences(const CPDF_Dictionary* encoding) {
131 RetainPtr<const CPDF_Array> diffs = encoding->GetArrayFor("Differences");
132 if (!diffs)
133 return;
134
135 m_CharNames.resize(kInternalTableSize);
136 uint32_t cur_code = 0;
137 for (uint32_t i = 0; i < diffs->size(); i++) {
138 RetainPtr<const CPDF_Object> element = diffs->GetDirectObjectAt(i);
139 if (!element)
140 continue;
141
142 const CPDF_Name* name = element->AsName();
143 if (name) {
144 if (cur_code < m_CharNames.size())
145 m_CharNames[cur_code] = name->GetString();
146 cur_code++;
147 } else {
148 cur_code = element->GetInteger();
149 }
150 }
151}
152
153void CPDF_SimpleFont::LoadPDFEncoding(bool bEmbedded, bool bTrueType) {
154 RetainPtr<const CPDF_Object> pEncoding =
155 m_pFontDict->GetDirectObjectFor("Encoding");
156 if (!pEncoding) {
157 if (m_BaseFontName == "Symbol") {
160 } else if (!bEmbedded && m_BaseEncoding == FontEncoding::kBuiltin) {
162 }
163 return;
164 }
165 if (pEncoding->IsName()) {
168 return;
169 }
170 if (FontStyleIsSymbolic(m_Flags) && m_BaseFontName == "Symbol") {
171 if (!bTrueType)
173 return;
174 }
175 ByteString bsEncoding = pEncoding->GetString();
176 if (bsEncoding == pdfium::font_encodings::kMacExpertEncoding)
177 bsEncoding = pdfium::font_encodings::kWinAnsiEncoding;
178 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
179 return;
180 }
181
182 const CPDF_Dictionary* pDict = pEncoding->AsDictionary();
183 if (!pDict)
184 return;
185
188 ByteString bsEncoding = pDict->GetByteStringFor("BaseEncoding");
189 if (bTrueType && bsEncoding == pdfium::font_encodings::kMacExpertEncoding)
190 bsEncoding = pdfium::font_encodings::kWinAnsiEncoding;
191 GetPredefinedEncoding(bsEncoding, &m_BaseEncoding);
192 }
193 if ((!bEmbedded || bTrueType) && m_BaseEncoding == FontEncoding::kBuiltin)
195
197}
198
199int CPDF_SimpleFont::GetCharWidthF(uint32_t charcode) {
200 if (charcode > 0xff)
201 charcode = 0;
202
203 if (m_CharWidth[charcode] == 0xffff) {
204 LoadCharMetrics(charcode);
205 if (m_CharWidth[charcode] == 0xffff) {
206 m_CharWidth[charcode] = 0;
207 }
208 }
209 return m_CharWidth[charcode];
210}
211
213 if (charcode > 0xff)
214 charcode = 0;
215
216 if (m_CharBBox[charcode].left == -1)
217 LoadCharMetrics(charcode);
218
219 return m_CharBBox[charcode];
220}
221
223 RetainPtr<const CPDF_Dictionary> pFontDesc =
224 m_pFontDict->GetDictFor("FontDescriptor");
225 if (pFontDesc)
226 LoadFontDescriptor(pFontDesc.Get());
227 LoadCharWidths(pFontDesc.Get());
228 if (m_pFontFile) {
229 if (m_BaseFontName.GetLength() > 7 && m_BaseFontName[6] == '+') {
230 m_BaseFontName = m_BaseFontName.Last(m_BaseFontName.GetLength() - 7);
231 }
232 } else {
234 }
237 LoadPDFEncoding(!!m_pFontFile, m_Font.IsTTFont());
239 m_CharNames.clear();
240 if (!HasFace()) {
241 return true;
242 }
243
245 static const unsigned char kLowercases[][2] = {
246 {'a', 'z'}, {0xe0, 0xf6}, {0xf8, 0xfd}};
247 for (size_t range = 0; range < std::size(kLowercases); ++range) {
248 const auto& lower = kLowercases[range];
249 for (int i = lower[0]; i <= lower[1]; ++i) {
250 if (m_GlyphIndex[i] != 0xffff && m_pFontFile)
251 continue;
252
253 int j = i - 32;
254 m_GlyphIndex[i] = m_GlyphIndex[j];
255 if (m_CharWidth[j]) {
256 m_CharWidth[i] = m_CharWidth[j];
257 m_CharBBox[i] = m_CharBBox[j];
258 }
259 }
260 }
261 }
263 return true;
264}
265
268 int width = 0;
269 size_t i;
270 for (i = 0; i < kInternalTableSize; i++) {
271 if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff)
272 continue;
273
274 if (width == 0)
275 width = m_CharWidth[i];
276 else if (width != m_CharWidth[i])
277 break;
278 }
279 if (i == kInternalTableSize && width)
281 }
282 m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
283 m_ItalicAngle, FX_CodePage::kDefANSI, false);
284}
285
291
292WideString CPDF_SimpleFont::UnicodeFromCharCode(uint32_t charcode) const {
293 WideString unicode = CPDF_Font::UnicodeFromCharCode(charcode);
294 if (!unicode.IsEmpty())
295 return unicode;
296 wchar_t ret = m_Encoding.UnicodeFromCharCode((uint8_t)charcode);
297 if (ret == 0)
298 return WideString();
299 return WideString(ret);
300}
301
302uint32_t CPDF_SimpleFont::CharCodeFromUnicode(wchar_t unicode) const {
303 uint32_t ret = CPDF_Font::CharCodeFromUnicode(unicode);
304 if (ret)
305 return ret;
306 return m_Encoding.CharCodeFromUnicode(unicode);
307}
308
310 return !m_bUseFontWidth;
311}
bool KeyExist(const ByteString &key) const
int GetIntegerFor(const ByteString &key) const
ByteString GetByteStringFor(const ByteString &key) const
void LoadFontDescriptor(const CPDF_Dictionary *pFontDesc)
virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const
int m_Flags
Definition cpdf_font.h:178
virtual WideString UnicodeFromCharCode(uint32_t charcode) const
bool HasFace() const
Definition cpdf_font.h:110
void CheckFontMetrics()
bool IsEmbedded() const
Definition cpdf_font.h:101
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)
bool IsEmpty() const
Definition widestring.h:118
FontEncoding
bool FontStyleIsFixedPitch(uint32_t style)
Definition fx_font.h:65
bool FontStyleIsSymbolic(uint32_t style)
Definition fx_font.h:68
bool FontStyleIsAllCaps(uint32_t style)
Definition fx_font.h:74
#define FXFONT_FIXED_PITCH
Definition fx_font.h:28
#define FXFT_Get_Glyph_HoriAdvance(face)
Definition fx_freetype.h:60
const char kMacExpertEncoding[]