7#include "core/fxge/cfx_font.h"
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"
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));
43FX_RECT ScaledFXRectFromFTPos(FT_Pos left,
49 if (x_scale == 0 || y_scale == 0)
50 return FXRectFromFTPos(left, top, right, bottom);
52 return FXRectFromFTPos(left * 1000 / x_scale, top * 1000 / y_scale,
53 right * 1000 / x_scale, bottom * 1000 / y_scale);
57unsigned long FTStreamRead(FXFT_StreamRec* stream,
59 unsigned char* buffer,
60 unsigned long count) {
64 IFX_SeekableReadStream* pFile =
65 static_cast<IFX_SeekableReadStream*>(stream->descriptor.pointer);
66 return pFile && pFile->ReadBlockAtOffset({buffer, count}, offset) ? count : 0;
69void FTStreamClose(FXFT_StreamRec* stream) {}
72bool ShouldAppendStyle(
const ByteString& style) {
86 {
static_cast<
int>(FX_Charset::kMSWin_EasternEuropean),
"Tahoma"},
118 if ((word >= 0x4E00 && word <= 0x9FA5) ||
119 (word >= 0xE7C7 && word <= 0xE7F3) ||
120 (word >= 0x3000 && word <= 0x303F) ||
121 (word >= 0x2000 && word <= 0x206F)) {
125 if (((word >= 0x3040) && (word <= 0x309F)) ||
126 ((word >= 0x30A0) && (word <= 0x30FF)) ||
127 ((word >= 0x31F0) && (word <= 0x31FF)) ||
128 ((word >= 0xFF00) && (word <= 0xFFEF))) {
132 if (((word >= 0xAC00) && (word <= 0xD7AF)) ||
133 ((word >= 0x1100) && (word <= 0x11FF)) ||
134 ((word >= 0x3130) && (word <= 0x318F))) {
138 if (word >= 0x0E00 && word <= 0x0E7F)
141 if ((word >= 0x0370 && word <= 0x03FF) || (word >= 0x1F00 && word <= 0x1FFF))
144 if ((word >= 0x0600 && word <= 0x06FF) || (word >= 0xFB50 && word <= 0xFEFC))
147 if (word >= 0x0590 && word <= 0x05FF)
150 if (word >= 0x0400 && word <= 0x04FF)
153 if (word >= 0x0100 && word <= 0x024F)
156 if (word >= 0x1E00 && word <= 0x1EFF)
170bool CFX_Font::LoadFile(RetainPtr<IFX_SeekableReadStream> pFile,
175 auto pStreamRec = std::make_unique<FXFT_StreamRec>();
176 pStreamRec->base =
nullptr;
177 pStreamRec->size =
static_cast<
unsigned long>(pFile->GetSize());
179 pStreamRec->descriptor.pointer =
static_cast<
void*>(pFile.Get());
180 pStreamRec->close = FTStreamClose;
181 pStreamRec->read = FTStreamRead;
184 args.flags = FT_OPEN_STREAM;
185 args.stream = pStreamRec.get();
187 m_Face = CFX_Face::Open(CFX_GEModule::Get()->GetFontMgr()->GetFTLibrary(),
192 m_pOwnedFile = std::move(pFile);
193 m_pOwnedStreamRec = std::move(pStreamRec);
194 FT_Set_Pixel_Sizes(m_Face->GetRec(), 0, 64);
198#if !BUILDFLAG(IS_WIN)
199void CFX_Font::SetFace(RetainPtr<CFX_Face> face) {
205void CFX_Font::SetSubstFont(std::unique_ptr<CFX_SubstFont> subst) {
206 m_pSubstFont = std::move(subst);
215#if BUILDFLAG(IS_APPLE)
216 ReleasePlatformResource();
228 m_bVertical = bVertical;
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,
235 m_FontData = m_Face->GetData();
246 return GetOrCreateGlyphCache()->GetGlyphWidth(
this, glyph_index, dest_width,
256 return m_Face->GetGlyphWidth(glyph_index, dest_width, weight,
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);
269 m_FontData = m_FontDataAllocation;
274 return m_Face && m_Face->IsTtOt();
278 return m_Face ? m_Face->GetAdjustedAscender() : 0;
282 return m_Face ? m_Face->GetAdjustedDescender() : 0;
287 return absl::nullopt;
289 if (m_Face->IsTricky()) {
290 int error = FT_Set_Char_Size(m_Face->GetRec(), 0, 1000 * 64, 72, 72);
292 return absl::nullopt;
294 error = FT_Load_Glyph(m_Face->GetRec(), glyph_index,
295 FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
297 return absl::nullopt;
300 error = FT_Get_Glyph(m_Face->GetRec()->glyph, &glyph);
302 return absl::nullopt;
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()));
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;
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();
334 if (m_Face->IsItalic()) {
338 ByteString str = m_Face->GetStyleName();
340 return str.Contains(
"italic");
344 return m_Face && m_Face->IsBold();
348 return m_Face && m_Face->IsFixedWidth();
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;
362 ByteString psName = FT_Get_Postscript_Name(m_Face->GetRec());
369 if (!m_Face && !m_pSubstFont)
372 return m_Face->GetFamilyName();
373 return m_pSubstFont->m_Family;
376ByteString
CFX_Font::GetFamilyNameOrUntitled()
const {
382 if (!m_Face && !m_pSubstFont)
385 ByteString style = m_Face->GetStyleName();
386 ByteString facename = GetFamilyNameOrUntitled();
387 if (ShouldAppendStyle(style))
388 facename
+= " " + style;
391 return m_pSubstFont->m_Family;
399 ByteString style = m_Face->GetStyleName();
400 ByteString facename = GetFamilyNameOrUntitled();
402 facename.Remove(
' ');
403 if (ShouldAppendStyle(style))
408 return m_pSubstFont->m_Family;
414 return absl::nullopt;
415 return m_Face->GetBBox();
419 absl::optional<FX_RECT> result = GetRawBBox();
420 if (!result.has_value())
423 int em = m_Face->GetUnitsPerEm();
425 FX_RECT& bbox = result.value();
436 m_GlyphCache = CFX_GEModule::Get()->GetFontCache()->GetGlyphCache(
this);
441 m_GlyphCache =
nullptr;
445 int dest_width)
const {
449 return m_Face->LoadGlyphPath(glyph_index, dest_width, m_bVertical,
454 uint32_t glyph_index,
460 return GetOrCreateGlyphCache()->LoadGlyphBitmap(
this, glyph_index, bFontStyle,
462 anti_alias, text_options);
466 int dest_width)
const {
467 return GetOrCreateGlyphCache()->LoadGlyphPath(
this, glyph_index, dest_width);
470#if defined(PDF_USE_SKIA)
471CFX_TypeFace* CFX_Font::GetDeviceCache()
const {
472 return GetOrCreateGlyphCache()->GetDeviceCache(
this);
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
absl::optional< FX_RECT > GetBBox() const
int GetGlyphWidth(uint32_t glyph_index) const
static const CharsetFontMap kDefaultTTFMap[]
std::unique_ptr< CFX_Path > LoadGlyphPathImpl(uint32_t glyph_index, int dest_width) const
absl::optional< FX_RECT > GetRawBBox() const
ByteString GetPsName() const
ByteString GetBaseFontName() const
absl::optional< FX_RECT > GetGlyphBBox(uint32_t glyph_index)
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[]
ByteString GetFaceName() const
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
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)