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_fontmgr.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/fxge/cfx_fontmgr.h"
8
9#include <iterator>
10#include <memory>
11#include <utility>
12
13#include "core/fxcrt/fixed_size_data_vector.h"
14#include "core/fxge/cfx_fontmapper.h"
15#include "core/fxge/cfx_substfont.h"
16#include "core/fxge/fontdata/chromefontdata/chromefontdata.h"
17#include "core/fxge/fx_font.h"
18#include "core/fxge/systemfontinfo_iface.h"
19#include "third_party/base/check_op.h"
20
21namespace {
22
23struct BuiltinFont {
24 const uint8_t* m_pFontData; // Raw, POD struct.
25 uint32_t m_dwSize;
26};
27
28constexpr BuiltinFont kFoxitFonts[] = {
29 {kFoxitFixedFontData, 17597},
33 {kFoxitSansFontData, 15025},
37 {kFoxitSerifFontData, 19469},
41 {kFoxitSymbolFontData, 16729},
43};
44static_assert(std::size(kFoxitFonts) == CFX_FontMapper::kNumStandardFonts,
45 "Wrong font count");
46
47constexpr BuiltinFont kGenericSansFont = {kFoxitSansMMFontData, 66919};
48constexpr BuiltinFont kGenericSerifFont = {kFoxitSerifMMFontData, 113417};
49
50FXFT_LibraryRec* FTLibraryInitHelper() {
51 FXFT_LibraryRec* pLibrary = nullptr;
52 FT_Init_FreeType(&pLibrary);
53 return pLibrary;
54}
55
56} // namespace
57
58CFX_FontMgr::FontDesc::FontDesc(FixedSizeDataVector<uint8_t> data)
59 : m_pFontData(std::move(data)) {}
60
61CFX_FontMgr::FontDesc::~FontDesc() = default;
62
63void CFX_FontMgr::FontDesc::SetFace(size_t index, CFX_Face* face) {
64 CHECK_LT(index, std::size(m_TTCFaces));
65 m_TTCFaces[index].Reset(face);
66}
67
68CFX_Face* CFX_FontMgr::FontDesc::GetFace(size_t index) const {
69 CHECK_LT(index, std::size(m_TTCFaces));
70 return m_TTCFaces[index].Get();
71}
72
76 m_FTLibrarySupportsHinting(SetLcdFilterMode() ||
77 FreeTypeVersionSupportsHinting()) {}
78
79CFX_FontMgr::~CFX_FontMgr() = default;
80
82 const ByteString& face_name,
83 int weight,
84 bool bItalic) {
85 auto it = m_FaceMap.find({face_name, weight, bItalic});
86 return it != m_FaceMap.end() ? pdfium::WrapRetain(it->second.Get()) : nullptr;
87}
88
90 const ByteString& face_name,
91 int weight,
92 bool bItalic,
93 FixedSizeDataVector<uint8_t> data) {
94 auto pFontDesc = pdfium::MakeRetain<FontDesc>(std::move(data));
95 m_FaceMap[{face_name, weight, bItalic}].Reset(pFontDesc.Get());
96 return pFontDesc;
97}
98
100 size_t ttc_size,
101 uint32_t checksum) {
102 auto it = m_TTCFaceMap.find({ttc_size, checksum});
103 return it != m_TTCFaceMap.end() ? pdfium::WrapRetain(it->second.Get())
104 : nullptr;
105}
106
108 size_t ttc_size,
109 uint32_t checksum,
110 FixedSizeDataVector<uint8_t> data) {
111 auto pNewDesc = pdfium::MakeRetain<FontDesc>(std::move(data));
112 m_TTCFaceMap[{ttc_size, checksum}].Reset(pNewDesc.Get());
113 return pNewDesc;
114}
115
116RetainPtr<CFX_Face> CFX_FontMgr::NewFixedFace(RetainPtr<FontDesc> pDesc,
117 pdfium::span<const uint8_t> span,
118 size_t face_index) {
119 RetainPtr<CFX_Face> face =
120 CFX_Face::New(m_FTLibrary.get(), std::move(pDesc), span,
121 static_cast<FT_Long>(face_index));
122 if (!face)
123 return nullptr;
124
125 if (FT_Set_Pixel_Sizes(face->GetRec(), 64, 64) != 0)
126 return nullptr;
127
128 return face;
129}
130
131// static
132pdfium::span<const uint8_t> CFX_FontMgr::GetStandardFont(size_t index) {
133 CHECK_LT(index, std::size(kFoxitFonts));
134 return pdfium::make_span(kFoxitFonts[index].m_pFontData,
135 kFoxitFonts[index].m_dwSize);
136}
137
138// static
139pdfium::span<const uint8_t> CFX_FontMgr::GetGenericSansFont() {
140 return pdfium::make_span(kGenericSansFont.m_pFontData,
141 kGenericSansFont.m_dwSize);
142}
143
144// static
145pdfium::span<const uint8_t> CFX_FontMgr::GetGenericSerifFont() {
146 return pdfium::make_span(kGenericSerifFont.m_pFontData,
147 kGenericSerifFont.m_dwSize);
148}
149
150bool CFX_FontMgr::FreeTypeVersionSupportsHinting() const {
151 FT_Int major;
152 FT_Int minor;
153 FT_Int patch;
154 FT_Library_Version(m_FTLibrary.get(), &major, &minor, &patch);
155 // Freetype versions >= 2.8.1 support hinting even if subpixel rendering is
156 // disabled. https://sourceforge.net/projects/freetype/files/freetype2/2.8.1/
157 return major > 2 || (major == 2 && minor > 8) ||
158 (major == 2 && minor == 8 && patch >= 1);
159}
160
161bool CFX_FontMgr::SetLcdFilterMode() const {
162 return FT_Library_SetLcdFilter(m_FTLibrary.get(), FT_LCD_FILTER_DEFAULT) !=
163 FT_Err_Unimplemented_Feature;
164}
const unsigned char kFoxitSerifMMFontData[113417]
const unsigned char kFoxitSerifFontData[19469]
Definition FoxitSerif.cpp:9
const unsigned char kFoxitSerifBoldFontData[19395]
const unsigned char kFoxitSansFontData[15025]
Definition FoxitSans.cpp:9
const unsigned char kFoxitSansMMFontData[66919]
const unsigned char kFoxitFixedItalicFontData[18746]
const unsigned char kFoxitSansBoldItalicFontData[16418]
const unsigned char kFoxitSansBoldFontData[16344]
const unsigned char kFoxitFixedBoldFontData[18055]
const unsigned char kFoxitSansItalicFontData[16339]
const unsigned char kFoxitSymbolFontData[16729]
const unsigned char kFoxitFixedFontData[17597]
Definition FoxitFixed.cpp:9
const unsigned char kFoxitDingbatsFontData[29513]
const unsigned char kFoxitSerifBoldItalicFontData[20733]
const unsigned char kFoxitSerifItalicFontData[21227]
const unsigned char kFoxitFixedBoldItalicFontData[19151]
static constexpr int kNumStandardFonts
CFX_Face * GetFace(size_t index) const
void SetFace(size_t index, CFX_Face *face)
RetainPtr< FontDesc > AddCachedFontDesc(const ByteString &face_name, int weight, bool bItalic, FixedSizeDataVector< uint8_t > data)
RetainPtr< FontDesc > GetCachedFontDesc(const ByteString &face_name, int weight, bool bItalic)
RetainPtr< FontDesc > AddCachedTTCFontDesc(size_t ttc_size, uint32_t checksum, FixedSizeDataVector< uint8_t > data)
static pdfium::span< const uint8_t > GetGenericSerifFont()
RetainPtr< CFX_Face > NewFixedFace(RetainPtr< FontDesc > pDesc, pdfium::span< const uint8_t > span, size_t face_index)
RetainPtr< FontDesc > GetCachedTTCFontDesc(size_t ttc_size, uint32_t checksum)
static pdfium::span< const uint8_t > GetGenericSansFont()