7#include "core/fxge/cfx_font.h"
16#include "build/build_config.h"
17#include "core/fxcrt/check.h"
18#include "core/fxcrt/data_vector.h"
19#include "core/fxcrt/fx_codepage.h"
20#include "core/fxcrt/fx_stream.h"
21#include "core/fxcrt/numerics/safe_conversions.h"
22#include "core/fxcrt/span.h"
23#include "core/fxcrt/unowned_ptr.h"
24#include "core/fxge/cfx_fontcache.h"
25#include "core/fxge/cfx_fontmapper.h"
26#include "core/fxge/cfx_fontmgr.h"
27#include "core/fxge/cfx_gemodule.h"
28#include "core/fxge/cfx_glyphcache.h"
29#include "core/fxge/cfx_path.h"
30#include "core/fxge/cfx_substfont.h"
31#include "core/fxge/fx_font.h"
32#include "core/fxge/scoped_font_transform.h"
44 {
static_cast<
int>(FX_Charset::kMSWin_EasternEuropean),
"Tahoma"},
53FX_RECT FXRectFromFTPos(FT_Pos left, FT_Pos top, FT_Pos right, FT_Pos bottom) {
55 pdfium::checked_cast<int32_t>(top),
56 pdfium::checked_cast<int32_t>(right),
57 pdfium::checked_cast<int32_t>(bottom));
60FX_RECT ScaledFXRectFromFTPos(FT_Pos left,
66 if (x_scale == 0 || y_scale == 0)
67 return FXRectFromFTPos(left, top, right, bottom);
69 return FXRectFromFTPos(left * 1000 / x_scale, top * 1000 / y_scale,
70 right * 1000 / x_scale, bottom * 1000 / y_scale);
74unsigned long FTStreamRead(FXFT_StreamRec* stream,
76 unsigned char* buffer,
77 unsigned long count) {
81 IFX_SeekableReadStream* pFile =
82 static_cast<IFX_SeekableReadStream*>(stream->descriptor.pointer);
85 return pFile && pFile->ReadBlockAtOffset(
86 UNSAFE_BUFFERS(pdfium::make_span(buffer, count)), offset)
91void FTStreamClose(FXFT_StreamRec* stream) {}
94bool ShouldAppendStyle(
const ByteString& style) {
95 return !style.IsEmpty() && style
!= "Regular";
110pdfium::span<
const CFX_Font::CharsetFontMap>
CFX_Font::GetDefaultTTFMapSpan() {
111 auto map_with_sentinel_value =
pdfium::make_span(kDefaultTTFMap);
112 return map_with_sentinel_value.first(map_with_sentinel_value.size() - 1);
117 for (
const auto& entry : GetDefaultTTFMapSpan()) {
118 if (
static_cast<
int>(nCharset) == entry.charset) {
119 return entry.fontname;
132 if ((word >= 0x4E00 && word <= 0x9FA5) ||
133 (word >= 0xE7C7 && word <= 0xE7F3) ||
134 (word >= 0x3000 && word <= 0x303F) ||
135 (word >= 0x2000 && word <= 0x206F)) {
139 if (((word >= 0x3040) && (word <= 0x309F)) ||
140 ((word >= 0x30A0) && (word <= 0x30FF)) ||
141 ((word >= 0x31F0) && (word <= 0x31FF)) ||
142 ((word >= 0xFF00) && (word <= 0xFFEF))) {
146 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
147 ((word >= 0x1100) && (word <= 0x11FF)) ||
148 ((word >= 0x3130) && (word <= 0x318F))) {
152 if (word >= 0x0E00 && word <= 0x0E7F)
155 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
158 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
161 if (word >= 0x0590 && word <= 0x05FF)
164 if (word >= 0x0400 && word <= 0x04FF)
167 if (word >= 0x0100 && word <= 0x024F)
170 if (word >= 0x1E00 && word <= 0x1EFF)
184bool CFX_Font::LoadFile(RetainPtr<IFX_SeekableReadStream> pFile,
188 auto pStreamRec = std::make_unique<FXFT_StreamRec>();
189 pStreamRec->base =
nullptr;
190 pStreamRec->size =
static_cast<
unsigned long>(pFile->GetSize());
192 pStreamRec->descriptor.pointer =
static_cast<
void*>(pFile.Get());
193 pStreamRec->close = FTStreamClose;
194 pStreamRec->read = FTStreamRead;
197 args.flags = FT_OPEN_STREAM;
198 args.stream = pStreamRec.get();
200 m_Face = CFX_Face::Open(CFX_GEModule::Get()->GetFontMgr()->GetFTLibrary(),
205 m_pOwnedFile = std::move(pFile);
206 m_pOwnedStreamRec = std::move(pStreamRec);
207 m_Face->SetPixelSize(0, 64);
211#if !BUILDFLAG(IS_WIN)
212void CFX_Font::SetFace(RetainPtr<CFX_Face> face) {
218void CFX_Font::SetSubstFont(std::unique_ptr<CFX_SubstFont> subst) {
219 m_pSubstFont = std::move(subst);
228#if BUILDFLAG(IS_APPLE)
229 ReleasePlatformResource();
240 m_bVertical = bVertical;
242 m_pSubstFont = std::make_unique<CFX_SubstFont>();
243 m_Face = CFX_GEModule::Get()->GetFontMgr()->GetBuiltinMapper()->FindSubstFont(
244 face_name, bTrueType, flags, weight, italic_angle, code_page,
247 m_FontData = m_Face->GetData();
258 return GetOrCreateGlyphCache()->GetGlyphWidth(
this, glyph_index, dest_width,
268 return m_Face->GetGlyphWidth(glyph_index, dest_width, weight,
274 uint64_t object_tag) {
275 m_bVertical = force_vertical;
276 m_ObjectTag = object_tag;
277 m_FontDataAllocation = DataVector<uint8_t>(src_span.begin(), src_span.end());
278 m_Face = CFX_GEModule::Get()->GetFontMgr()->NewFixedFace(
279 nullptr, m_FontDataAllocation, 0);
280 m_FontData = m_FontDataAllocation;
285 return m_Face && m_Face->IsTtOt();
292 return NormalizeFontMetric(m_Face->GetAscender(), m_Face->GetUnitsPerEm());
299 return NormalizeFontMetric(m_Face->GetDescender(), m_Face->GetUnitsPerEm());
306 if (m_Face->IsTricky()) {
307 int error = FT_Set_Char_Size(m_Face->GetRec(), 0, 1000 * 64, 72, 72);
311 error = FT_Load_Glyph(m_Face->GetRec(), glyph_index,
312 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
317 error = FT_Get_Glyph(m_Face->GetRec()->glyph, &glyph);
322 FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS, &cbox);
323 int pixel_size_x = m_Face->GetRec()->size->metrics.x_ppem;
324 int pixel_size_y = m_Face->GetRec()->size->metrics.y_ppem;
325 FX_RECT result = ScaledFXRectFromFTPos(
326 cbox.xMin, cbox.yMax, cbox.xMax, cbox.yMin, pixel_size_x, pixel_size_y);
327 result.top = std::min(result.top,
static_cast<
int>(m_Face->GetAscender()));
329 std::max(result.bottom,
static_cast<
int>(m_Face->GetDescender()));
330 FT_Done_Glyph(glyph);
331 if (!m_Face->SetPixelSize(0, 64)) {
336 constexpr int kFlag = FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
337 if (FT_Load_Glyph(m_Face->GetRec(), glyph_index, kFlag) != 0)
339 int em = m_Face->GetUnitsPerEm();
352 if (m_Face->IsItalic()) {
358 return str.Contains(
"italic");
362 return m_Face && m_Face->IsBold();
366 return m_Face && m_Face->IsFixedWidth();
369#if defined(PDF_USE_SKIA)
370bool CFX_Font::IsSubstFontBold()
const {
371 CFX_SubstFont* subst_font = GetSubstFont();
372 return subst_font && subst_font->GetOriginalWeight() >= FXFONT_FW_BOLD;
380 ByteString psName = FT_Get_Postscript_Name(m_Face->GetRec());
381 if (psName.IsEmpty())
387 if (!m_Face && !m_pSubstFont)
390 return m_Face->GetFamilyName();
391 return m_pSubstFont->m_Family;
405 ByteString facename = GetFamilyNameOrUntitled();
407 facename.Remove(
' ');
408 if (ShouldAppendStyle(style))
413 return m_pSubstFont->m_Family;
420 return m_Face->GetBBox();
425 if (!result.has_value())
428 int em = m_Face->GetUnitsPerEm();
430 FX_RECT& bbox = result.value();
432 bbox
.top =
pdfium::saturated_cast<int32_t>((bbox
.top * 1000.0f) / em);
441 m_GlyphCache = CFX_GEModule::Get()->GetFontCache()->GetGlyphCache(
this);
446 m_GlyphCache =
nullptr;
450 int dest_width)
const {
454 return m_Face->LoadGlyphPath(glyph_index, dest_width, m_bVertical,
459 uint32_t glyph_index,
465 return GetOrCreateGlyphCache()->LoadGlyphBitmap(
this, glyph_index, bFontStyle,
467 anti_alias, text_options);
471 int dest_width)
const {
472 return GetOrCreateGlyphCache()->LoadGlyphPath(
this, glyph_index, dest_width);
475#if defined(PDF_USE_SKIA)
476CFX_TypeFace* CFX_Font::GetDeviceCache()
const {
477 return GetOrCreateGlyphCache()->GetDeviceCache(
this);
fxcrt::ByteString ByteString
static ByteString GetDefaultFontNameByCharset(FX_Charset nCharset)
int GetSubstFontItalicAngle() const
int GetGlyphWidth(uint32_t glyph_index, int dest_width, int weight) const
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
const CFX_Path * LoadGlyphPath(uint32_t glyph_index, int dest_width) const
std::optional< FX_RECT > GetBBox() const
int GetGlyphWidth(uint32_t glyph_index) const
std::unique_ptr< CFX_Path > LoadGlyphPathImpl(uint32_t glyph_index, int dest_width) const
std::optional< FX_RECT > GetGlyphBBox(uint32_t glyph_index)
ByteString GetPsName() const
ByteString GetBaseFontName() const
bool IsFixedWidth() const
bool LoadEmbedded(pdfium::span< const uint8_t > src_span, bool force_vertical, uint64_t object_tag)
int GetGlyphWidthImpl(uint32_t glyph_index, int dest_width, int weight) const
static const char kDefaultAnsiFontName[]
static const char kUniversalDefaultFontName[]
static FX_Charset GetCharSetFromUnicode(uint16_t word)
static const char kUntitledFontName[]
void LoadSubst(const ByteString &face_name, bool bTrueType, uint32_t flags, int weight, int italic_angle, FX_CodePage code_page, bool bVertical)
CFX_SubstFont * GetSubstFont() const
std::optional< FX_RECT > GetRawBBox() const
ByteString GetFamilyName() const
ByteString & operator+=(const ByteString &str)
ByteString & operator=(const char *str)
bool operator!=(const char *ptr) const
#define FXFT_Get_Glyph_Width(face)
#define FXFT_Get_Glyph_Height(face)
#define FXFT_Get_Glyph_HoriBearingX(face)
#define FXFT_Get_Glyph_HoriBearingY(face)
ByteString operator+(const char *str1, const ByteString &str2)