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
cfx_font.cpp
Go to the documentation of this file.
1// Copyright 2014 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/fxge/cfx_font.h"
8
9#include <stdint.h>
10
11#include <algorithm>
12#include <limits>
13#include <memory>
14#include <utility>
15
16#include "build/build_config.h"
17#include "core/fxcrt/data_vector.h"
18#include "core/fxcrt/fx_codepage.h"
19#include "core/fxcrt/fx_stream.h"
20#include "core/fxcrt/unowned_ptr.h"
21#include "core/fxge/cfx_fontcache.h"
22#include "core/fxge/cfx_fontmapper.h"
23#include "core/fxge/cfx_fontmgr.h"
24#include "core/fxge/cfx_gemodule.h"
25#include "core/fxge/cfx_glyphcache.h"
26#include "core/fxge/cfx_path.h"
27#include "core/fxge/cfx_substfont.h"
28#include "core/fxge/fx_font.h"
29#include "core/fxge/scoped_font_transform.h"
30#include "third_party/base/check.h"
31#include "third_party/base/containers/span.h"
32#include "third_party/base/numerics/safe_conversions.h"
33
34namespace {
35
36FX_RECT FXRectFromFTPos(FT_Pos left, FT_Pos top, FT_Pos right, FT_Pos bottom) {
37 return FX_RECT(pdfium::base::checked_cast<int32_t>(left),
38 pdfium::base::checked_cast<int32_t>(top),
39 pdfium::base::checked_cast<int32_t>(right),
40 pdfium::base::checked_cast<int32_t>(bottom));
41}
42
43FX_RECT ScaledFXRectFromFTPos(FT_Pos left,
44 FT_Pos top,
45 FT_Pos right,
46 FT_Pos bottom,
47 int x_scale,
48 int y_scale) {
49 if (x_scale == 0 || y_scale == 0)
50 return FXRectFromFTPos(left, top, right, bottom);
51
52 return FXRectFromFTPos(left * 1000 / x_scale, top * 1000 / y_scale,
53 right * 1000 / x_scale, bottom * 1000 / y_scale);
54}
55
56#ifdef PDF_ENABLE_XFA
57unsigned long FTStreamRead(FXFT_StreamRec* stream,
58 unsigned long offset,
59 unsigned char* buffer,
60 unsigned long count) {
61 if (count == 0)
62 return 0;
63
64 IFX_SeekableReadStream* pFile =
65 static_cast<IFX_SeekableReadStream*>(stream->descriptor.pointer);
66 return pFile && pFile->ReadBlockAtOffset({buffer, count}, offset) ? count : 0;
67}
68
69void FTStreamClose(FXFT_StreamRec* stream) {}
70#endif // PDF_ENABLE_XFA
71
72bool ShouldAppendStyle(const ByteString& style) {
73 return !style.IsEmpty() && style != "Regular";
74}
75
76} // namespace
77
79 {static_cast<int>(FX_Charset::kANSI), kDefaultAnsiFontName},
80 {static_cast<int>(FX_Charset::kChineseSimplified), "SimSun"},
81 {static_cast<int>(FX_Charset::kChineseTraditional), "MingLiU"},
82 {static_cast<int>(FX_Charset::kShiftJIS), "MS Gothic"},
83 {static_cast<int>(FX_Charset::kHangul), "Batang"},
84 {static_cast<int>(FX_Charset::kMSWin_Cyrillic), "Arial"},
85#if BUILDFLAG(IS_WIN)
86 {static_cast<int>(FX_Charset::kMSWin_EasternEuropean), "Tahoma"},
87#else
88 {static_cast<int>(FX_Charset::kMSWin_EasternEuropean), "Arial"},
89#endif
90 {static_cast<int>(FX_Charset::kMSWin_Arabic), "Arial"},
91 {-1, nullptr}};
92
93// static
94const char CFX_Font::kUntitledFontName[] = "Untitled";
95
96// static
97const char CFX_Font::kDefaultAnsiFontName[] = "Helvetica";
98
99// static
100const char CFX_Font::kUniversalDefaultFontName[] = "Arial Unicode MS";
101
102// static
104 for (size_t i = 0; i < std::size(kDefaultTTFMap) - 1; ++i) {
105 if (static_cast<int>(nCharset) == kDefaultTTFMap[i].charset)
106 return kDefaultTTFMap[i].fontname;
107 }
109}
110
111// static
113 // to avoid CJK Font to show ASCII
114 if (word < 0x7F)
115 return FX_Charset::kANSI;
116
117 // find new charset
118 if ((word >= 0x4E00 && word <= 0x9FA5) ||
119 (word >= 0xE7C7 && word <= 0xE7F3) ||
120 (word >= 0x3000 && word <= 0x303F) ||
121 (word >= 0x2000 && word <= 0x206F)) {
123 }
124
125 if (((word >= 0x3040) && (word <= 0x309F)) ||
126 ((word >= 0x30A0) && (word <= 0x30FF)) ||
127 ((word >= 0x31F0) && (word <= 0x31FF)) ||
128 ((word >= 0xFF00) && (word <= 0xFFEF))) {
130 }
131
132 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
133 ((word >= 0x1100) && (word <= 0x11FF)) ||
134 ((word >= 0x3130) && (word <= 0x318F))) {
135 return FX_Charset::kHangul;
136 }
137
138 if (word >= 0x0E00 && word <= 0x0E7F)
139 return FX_Charset::kThai;
140
141 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
143
144 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
146
147 if (word >= 0x0590 && word <= 0x05FF)
149
150 if (word >= 0x0400 && word <= 0x04FF)
152
153 if (word >= 0x0100 && word <= 0x024F)
155
156 if (word >= 0x1E00 && word <= 0x1EFF)
158
159 return FX_Charset::kANSI;
160}
161
162CFX_Font::CFX_Font() = default;
163
165 CFX_SubstFont* subst_font = GetSubstFont();
166 return subst_font ? subst_font->m_ItalicAngle : 0;
167}
168
169#ifdef PDF_ENABLE_XFA
170bool CFX_Font::LoadFile(RetainPtr<IFX_SeekableReadStream> pFile,
171 int nFaceIndex) {
172 m_bEmbedded = false;
173 m_ObjectTag = 0;
174
175 auto pStreamRec = std::make_unique<FXFT_StreamRec>();
176 pStreamRec->base = nullptr;
177 pStreamRec->size = static_cast<unsigned long>(pFile->GetSize());
178 pStreamRec->pos = 0;
179 pStreamRec->descriptor.pointer = static_cast<void*>(pFile.Get());
180 pStreamRec->close = FTStreamClose;
181 pStreamRec->read = FTStreamRead;
182
183 FT_Open_Args args;
184 args.flags = FT_OPEN_STREAM;
185 args.stream = pStreamRec.get();
186
187 m_Face = CFX_Face::Open(CFX_GEModule::Get()->GetFontMgr()->GetFTLibrary(),
188 &args, nFaceIndex);
189 if (!m_Face)
190 return false;
191
192 m_pOwnedFile = std::move(pFile);
193 m_pOwnedStreamRec = std::move(pStreamRec);
194 FT_Set_Pixel_Sizes(m_Face->GetRec(), 0, 64);
195 return true;
196}
197
198#if !BUILDFLAG(IS_WIN)
199void CFX_Font::SetFace(RetainPtr<CFX_Face> face) {
200 ClearGlyphCache();
201 m_ObjectTag = 0;
202 m_Face = face;
203}
204
205void CFX_Font::SetSubstFont(std::unique_ptr<CFX_SubstFont> subst) {
206 m_pSubstFont = std::move(subst);
207}
208#endif // !BUILDFLAG(IS_WIN)
209#endif // PDF_ENABLE_XFA
210
212 m_FontData = {}; // m_FontData can't outive m_Face.
213 m_Face.Reset();
214
215#if BUILDFLAG(IS_APPLE)
216 ReleasePlatformResource();
217#endif
218}
219
220void CFX_Font::LoadSubst(const ByteString& face_name,
221 bool bTrueType,
222 uint32_t flags,
223 int weight,
224 int italic_angle,
225 FX_CodePage code_page,
226 bool bVertical) {
227 m_bEmbedded = false;
228 m_bVertical = bVertical;
229 m_ObjectTag = 0;
230 m_pSubstFont = std::make_unique<CFX_SubstFont>();
231 m_Face = CFX_GEModule::Get()->GetFontMgr()->GetBuiltinMapper()->FindSubstFont(
232 face_name, bTrueType, flags, weight, italic_angle, code_page,
233 m_pSubstFont.get());
234 if (m_Face) {
235 m_FontData = m_Face->GetData();
236 }
237}
238
239int CFX_Font::GetGlyphWidth(uint32_t glyph_index) const {
240 return GetGlyphWidth(glyph_index, 0, 0);
241}
242
243int CFX_Font::GetGlyphWidth(uint32_t glyph_index,
244 int dest_width,
245 int weight) const {
246 return GetOrCreateGlyphCache()->GetGlyphWidth(this, glyph_index, dest_width,
247 weight);
248}
249
250int CFX_Font::GetGlyphWidthImpl(uint32_t glyph_index,
251 int dest_width,
252 int weight) const {
253 if (!m_Face)
254 return 0;
255
256 return m_Face->GetGlyphWidth(glyph_index, dest_width, weight,
257 m_pSubstFont.get());
258}
259
260bool CFX_Font::LoadEmbedded(pdfium::span<const uint8_t> src_span,
261 bool force_vertical,
262 uint64_t object_tag) {
263 m_bVertical = force_vertical;
264 m_ObjectTag = object_tag;
265 m_FontDataAllocation = DataVector<uint8_t>(src_span.begin(), src_span.end());
266 m_Face = CFX_GEModule::Get()->GetFontMgr()->NewFixedFace(
267 nullptr, m_FontDataAllocation, 0);
268 m_bEmbedded = true;
269 m_FontData = m_FontDataAllocation;
270 return !!m_Face;
271}
272
273bool CFX_Font::IsTTFont() const {
274 return m_Face && m_Face->IsTtOt();
275}
276
277int CFX_Font::GetAscent() const {
278 return m_Face ? m_Face->GetAdjustedAscender() : 0;
279}
280
281int CFX_Font::GetDescent() const {
282 return m_Face ? m_Face->GetAdjustedDescender() : 0;
283}
284
285absl::optional<FX_RECT> CFX_Font::GetGlyphBBox(uint32_t glyph_index) {
286 if (!m_Face)
287 return absl::nullopt;
288
289 if (m_Face->IsTricky()) {
290 int error = FT_Set_Char_Size(m_Face->GetRec(), 0, 1000 * 64, 72, 72);
291 if (error)
292 return absl::nullopt;
293
294 error = FT_Load_Glyph(m_Face->GetRec(), glyph_index,
295 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
296 if (error)
297 return absl::nullopt;
298
299 FT_Glyph glyph;
300 error = FT_Get_Glyph(m_Face->GetRec()->glyph, &glyph);
301 if (error)
302 return absl::nullopt;
303
304 FT_BBox cbox;
305 FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &cbox);
306 int pixel_size_x = m_Face->GetRec()->size->metrics.x_ppem;
307 int pixel_size_y = m_Face->GetRec()->size->metrics.y_ppem;
308 FX_RECT result = ScaledFXRectFromFTPos(
309 cbox.xMin, cbox.yMax, cbox.xMax, cbox.yMin, pixel_size_x, pixel_size_y);
310 result.top = std::min(result.top, static_cast<int>(m_Face->GetAscender()));
311 result.bottom =
312 std::max(result.bottom, static_cast<int>(m_Face->GetDescender()));
313 FT_Done_Glyph(glyph);
314 if (FT_Set_Pixel_Sizes(m_Face->GetRec(), 0, 64) != 0)
315 return absl::nullopt;
316 return result;
317 }
318 constexpr int kFlag = FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
319 if (FT_Load_Glyph(m_Face->GetRec(), glyph_index, kFlag) != 0)
320 return absl::nullopt;
321 int em = m_Face->GetUnitsPerEm();
322 return ScaledFXRectFromFTPos(FXFT_Get_Glyph_HoriBearingX(m_Face->GetRec()),
323 FXFT_Get_Glyph_HoriBearingY(m_Face->GetRec()) -
324 FXFT_Get_Glyph_Height(m_Face->GetRec()),
325 FXFT_Get_Glyph_HoriBearingX(m_Face->GetRec()) +
326 FXFT_Get_Glyph_Width(m_Face->GetRec()),
327 FXFT_Get_Glyph_HoriBearingY(m_Face->GetRec()),
328 em, em);
329}
330
331bool CFX_Font::IsItalic() const {
332 if (!m_Face)
333 return false;
334 if (m_Face->IsItalic()) {
335 return true;
336 }
337
338 ByteString str = m_Face->GetStyleName();
339 str.MakeLower();
340 return str.Contains("italic");
341}
342
343bool CFX_Font::IsBold() const {
344 return m_Face && m_Face->IsBold();
345}
346
347bool CFX_Font::IsFixedWidth() const {
348 return m_Face && m_Face->IsFixedWidth();
349}
350
351#if defined(PDF_USE_SKIA)
352bool CFX_Font::IsSubstFontBold() const {
353 CFX_SubstFont* subst_font = GetSubstFont();
354 return subst_font && subst_font->GetOriginalWeight() >= FXFONT_FW_BOLD;
355}
356#endif
357
358ByteString CFX_Font::GetPsName() const {
359 if (!m_Face)
360 return ByteString();
361
362 ByteString psName = FT_Get_Postscript_Name(m_Face->GetRec());
363 if (psName.IsEmpty())
364 psName = kUntitledFontName;
365 return psName;
366}
367
368ByteString CFX_Font::GetFamilyName() const {
369 if (!m_Face && !m_pSubstFont)
370 return ByteString();
371 if (m_Face)
372 return m_Face->GetFamilyName();
373 return m_pSubstFont->m_Family;
374}
375
376ByteString CFX_Font::GetFamilyNameOrUntitled() const {
377 ByteString facename = GetFamilyName();
378 return facename.IsEmpty() ? kUntitledFontName : facename;
379}
380
381ByteString CFX_Font::GetFaceName() const {
382 if (!m_Face && !m_pSubstFont)
383 return ByteString();
384 if (m_Face) {
385 ByteString style = m_Face->GetStyleName();
386 ByteString facename = GetFamilyNameOrUntitled();
387 if (ShouldAppendStyle(style))
388 facename += " " + style;
389 return facename;
390 }
391 return m_pSubstFont->m_Family;
392}
393
394ByteString CFX_Font::GetBaseFontName() const {
395 ByteString psname = GetPsName();
396 if (!psname.IsEmpty() && psname != kUntitledFontName)
397 return psname;
398 if (m_Face) {
399 ByteString style = m_Face->GetStyleName();
400 ByteString facename = GetFamilyNameOrUntitled();
401 if (IsTTFont())
402 facename.Remove(' ');
403 if (ShouldAppendStyle(style))
404 facename += (IsTTFont() ? "," : " ") + style;
405 return facename;
406 }
407 if (m_pSubstFont)
408 return m_pSubstFont->m_Family;
409 return ByteString();
410}
411
413 if (!m_Face)
414 return absl::nullopt;
415 return m_Face->GetBBox();
416}
417
419 absl::optional<FX_RECT> result = GetRawBBox();
420 if (!result.has_value())
421 return result;
422
423 int em = m_Face->GetUnitsPerEm();
424 if (em != 0) {
425 FX_RECT& bbox = result.value();
426 bbox.left = (bbox.left * 1000) / em;
427 bbox.top = (bbox.top * 1000) / em;
428 bbox.right = (bbox.right * 1000) / em;
429 bbox.bottom = (bbox.bottom * 1000) / em;
430 }
431 return result;
432}
433
434RetainPtr<CFX_GlyphCache> CFX_Font::GetOrCreateGlyphCache() const {
435 if (!m_GlyphCache)
436 m_GlyphCache = CFX_GEModule::Get()->GetFontCache()->GetGlyphCache(this);
437 return m_GlyphCache;
438}
439
440void CFX_Font::ClearGlyphCache() {
441 m_GlyphCache = nullptr;
442}
443
445 int dest_width) const {
446 if (!m_Face)
447 return nullptr;
448
449 return m_Face->LoadGlyphPath(glyph_index, dest_width, m_bVertical,
450 m_pSubstFont.get());
451}
452
454 uint32_t glyph_index,
455 bool bFontStyle,
456 const CFX_Matrix& matrix,
457 int dest_width,
458 int anti_alias,
459 CFX_TextRenderOptions* text_options) const {
460 return GetOrCreateGlyphCache()->LoadGlyphBitmap(this, glyph_index, bFontStyle,
461 matrix, dest_width,
462 anti_alias, text_options);
463}
464
465const CFX_Path* CFX_Font::LoadGlyphPath(uint32_t glyph_index,
466 int dest_width) const {
467 return GetOrCreateGlyphCache()->LoadGlyphPath(this, glyph_index, dest_width);
468}
469
470#if defined(PDF_USE_SKIA)
471CFX_TypeFace* CFX_Font::GetDeviceCache() const {
472 return GetOrCreateGlyphCache()->GetDeviceCache(this);
473}
474#endif
static ByteString GetDefaultFontNameByCharset(FX_Charset nCharset)
Definition cfx_font.cpp:103
int GetSubstFontItalicAngle() const
Definition cfx_font.cpp:164
int GetGlyphWidth(uint32_t glyph_index, int dest_width, int weight) const
Definition cfx_font.cpp:243
const CFX_GlyphBitmap * LoadGlyphBitmap(uint32_t glyph_index, bool bFontStyle, const CFX_Matrix &matrix, int dest_width, int anti_alias, CFX_TextRenderOptions *text_options) const
Definition cfx_font.cpp:453
const CFX_Path * LoadGlyphPath(uint32_t glyph_index, int dest_width) const
Definition cfx_font.cpp:465
absl::optional< FX_RECT > GetBBox() const
Definition cfx_font.cpp:418
int GetGlyphWidth(uint32_t glyph_index) const
Definition cfx_font.cpp:239
static const CharsetFontMap kDefaultTTFMap[]
Definition cfx_font.h:78
std::unique_ptr< CFX_Path > LoadGlyphPathImpl(uint32_t glyph_index, int dest_width) const
Definition cfx_font.cpp:444
absl::optional< FX_RECT > GetRawBBox() const
Definition cfx_font.cpp:412
ByteString GetPsName() const
Definition cfx_font.cpp:358
int GetDescent() const
Definition cfx_font.cpp:281
ByteString GetBaseFontName() const
Definition cfx_font.cpp:394
bool IsItalic() const
Definition cfx_font.cpp:331
bool IsTTFont() const
Definition cfx_font.cpp:273
absl::optional< FX_RECT > GetGlyphBBox(uint32_t glyph_index)
Definition cfx_font.cpp:285
bool IsFixedWidth() const
Definition cfx_font.cpp:347
bool LoadEmbedded(pdfium::span< const uint8_t > src_span, bool force_vertical, uint64_t object_tag)
Definition cfx_font.cpp:260
int GetGlyphWidthImpl(uint32_t glyph_index, int dest_width, int weight) const
Definition cfx_font.cpp:250
static const char kDefaultAnsiFontName[]
Definition cfx_font.h:58
ByteString GetFaceName() const
Definition cfx_font.cpp:381
static const char kUniversalDefaultFontName[]
Definition cfx_font.h:59
static FX_Charset GetCharSetFromUnicode(uint16_t word)
Definition cfx_font.cpp:112
static const char kUntitledFontName[]
Definition cfx_font.h:56
int GetAscent() const
Definition cfx_font.cpp:277
void LoadSubst(const ByteString &face_name, bool bTrueType, uint32_t flags, int weight, int italic_angle, FX_CodePage code_page, bool bVertical)
Definition cfx_font.cpp:220
CFX_SubstFont * GetSubstFont() const
Definition cfx_font.h:82
bool IsBold() const
Definition cfx_font.cpp:343
ByteString GetFamilyName() const
Definition cfx_font.cpp:368
ByteString & operator+=(const ByteString &str)
ByteString & operator=(const char *str)
bool IsEmpty() const
Definition bytestring.h:119
bool operator!=(const char *ptr) const
Definition bytestring.h:130
FX_Charset
Definition fx_codepage.h:70
@ kChineseTraditional
@ kMSWin_EasternEuropean
@ kChineseSimplified
FX_CodePage
Definition fx_codepage.h:18
#define FXFT_Get_Glyph_Width(face)
Definition fx_freetype.h:58
#define FXFT_Get_Glyph_Height(face)
Definition fx_freetype.h:59
#define FXFT_Get_Glyph_HoriBearingX(face)
Definition fx_freetype.h:56
#define FXFT_Get_Glyph_HoriBearingY(face)
Definition fx_freetype.h:57
ByteString operator+(const char *str1, const ByteString &str2)
Definition bytestring.h:282
int32_t bottom
int32_t right
int32_t top
int32_t left