7#include "core/fpdfapi/font/cpdf_font.h"
14#include "build/build_config.h"
15#include "constants/font_encodings.h"
16#include "core/fpdfapi/font/cpdf_cidfont.h"
17#include "core/fpdfapi/font/cpdf_fontencoding.h"
18#include "core/fpdfapi/font/cpdf_fontglobals.h"
19#include "core/fpdfapi/font/cpdf_tounicodemap.h"
20#include "core/fpdfapi/font/cpdf_truetypefont.h"
21#include "core/fpdfapi/font/cpdf_type1font.h"
22#include "core/fpdfapi/font/cpdf_type3font.h"
23#include "core/fpdfapi/parser/cpdf_array.h"
24#include "core/fpdfapi/parser/cpdf_dictionary.h"
25#include "core/fpdfapi/parser/cpdf_document.h"
26#include "core/fpdfapi/parser/cpdf_name.h"
27#include "core/fpdfapi/parser/cpdf_stream.h"
28#include "core/fpdfapi/parser/cpdf_stream_acc.h"
29#include "core/fxcrt/fx_codepage.h"
30#include "core/fxcrt/fx_safe_types.h"
31#include "core/fxcrt/stl_util.h"
32#include "core/fxge/cfx_fontmapper.h"
33#include "core/fxge/cfx_substfont.h"
34#include "core/fxge/freetype/fx_freetype.h"
35#include "core/fxge/fx_font.h"
36#include "third_party/base/check.h"
37#include "third_party/base/numerics/clamped_math.h"
38#include "third_party/base/numerics/safe_conversions.h"
42constexpr size_t kChineseFontNameSize = 4;
43const uint8_t kChineseFontNames[][kChineseFontNameSize] = {
44 {0xCB, 0xCE, 0xCC, 0xE5},
45 {0xBF, 0xAC, 0xCC, 0xE5},
46 {0xBA, 0xDA, 0xCC, 0xE5},
47 {0xB7, 0xC2, 0xCB, 0xCE},
48 {0xD0, 0xC2, 0xCB, 0xCE}};
60 m_pDocument->MaybePurgeFontFileStreamAcc(std::move(m_pFontFile));
112 return pString.GetLength();
115#if BUILDFLAG(IS_APPLE)
116int CPDF_Font::GlyphFromCharCodeExt(uint32_t charcode) {
117 return GlyphFromCharCode(charcode,
nullptr);
125 return pCIDFont ? pCIDFont->IsVertWriting() : m_Font.IsVertical();
129 *buf =
static_cast<
char>(charcode);
136 *str += ByteStringView(buf, len);
143 return m_pToUnicodeMap ? m_pToUnicodeMap->Lookup(charcode) : WideString();
150 return m_pToUnicodeMap ? m_pToUnicodeMap->ReverseLookup(unicode) : 0;
160 bool bExistItalicAngle =
false;
163 bExistItalicAngle =
true;
165 if (ItalicAngle < 0) {
169 bool bExistStemV =
false;
174 bool bExistAscent =
false;
179 bool bExistDescent =
false;
182 bExistDescent =
true;
184 bool bExistCapHeight =
false;
186 bExistCapHeight =
true;
187 if (bExistItalicAngle && bExistAscent && bExistCapHeight && bExistDescent &&
193 RetainPtr<
const CPDF_Array> pBBox = pFontDesc->GetArrayFor(
"FontBBox");
201 RetainPtr<
const CPDF_Stream> pFontFile = pFontDesc->GetStreamFor(
"FontFile");
209 const uint64_t key = pFontFile->KeyForCache();
210 m_pFontFile = m_pDocument->GetFontFileStreamAcc(std::move(pFontFile));
214 if (!m_Font.LoadEmbedded(m_pFontFile->GetSpan(), IsVertWriting(), key))
215 m_pDocument->MaybePurgeFontFileStreamAcc(std::move(m_pFontFile));
221 RetainPtr<CFX_Face> face = m_Font.GetFace();
224 const FX_RECT raw_bbox = face->GetBBox();
229 m_Ascent = TT2PDF(face->GetAscender(), face);
230 m_Descent = TT2PDF(face->GetDescender(), face);
233 for (
int i = 0; i < 256; i++) {
260 RetainPtr<
const CPDF_Stream> pStream = m_pFontDict->GetStreamFor(
"ToUnicode");
264 m_pToUnicodeMap = std::make_unique<CPDF_ToUnicodeMap>(std::move(pStream));
270 while (offset < pString.GetLength())
271 width += GetCharWidthF(GetNextChar(pString, &offset));
277 ByteStringView name) {
278 ByteString fontname(name);
279 absl::optional<CFX_FontMapper::StandardFont> font_id =
280 CFX_FontMapper::GetStandardFontName(&fontname);
281 if (!font_id.has_value())
289 auto pDict = pDoc->New<CPDF_Dictionary>();
290 pDict->SetNewFor<CPDF_Name>(
"Type",
"Font");
291 pDict->SetNewFor<CPDF_Name>(
"Subtype",
"Type1");
292 pDict->SetNewFor<CPDF_Name>(
"BaseFont", fontname);
293 pDict->SetNewFor<CPDF_Name>(
"Encoding",
296 pFontGlobals->Set(pDoc, font_id.value(), pFont);
304 ByteString type = pFontDict->GetByteStringFor(
"Subtype");
306 if (type
== "TrueType") {
307 ByteString tag = pFontDict->GetByteStringFor(
"BaseFont").First(4);
308 for (size_t i = 0; i <
std::size(kChineseFontNames); ++i) {
309 if (tag
== ByteString(kChineseFontNames[i], kChineseFontNameSize)) {
310 RetainPtr<
const CPDF_Dictionary> pFontDesc =
311 pFontDict->GetDictFor(
"FontDescriptor");
312 if (!pFontDesc || !pFontDesc->KeyExist(
"FontFile2"))
313 pFont = pdfium::MakeRetain<CPDF_CIDFont>(pDoc,
std::move(pFontDict));
318 pFont = pdfium::MakeRetain<CPDF_TrueTypeFont>(pDoc,
std::move(pFontDict));
319 }
else if (type
== "Type3") {
320 pFont = pdfium::MakeRetain<CPDF_Type3Font>(pDoc,
std::move(pFontDict),
322 }
else if (type
== "Type0") {
323 pFont = pdfium::MakeRetain<CPDF_CIDFont>(pDoc,
std::move(pFontDict));
325 pFont = pdfium::MakeRetain<CPDF_Type1Font>(pDoc,
std::move(pFontDict));
334 if (pString.IsEmpty())
337 size_t& offset = *pOffset;
338 return offset < pString.GetLength() ? pString[offset++] : pString.Back();
352 return absl::nullopt;
359 const std::vector<ByteString>& charnames,
364 if (!charnames.empty() && !charnames[charcode].IsEmpty())
365 return charnames[charcode].c_str();
367 const char* name =
nullptr;
378 if (m_FontFallbacks.empty()) {
379 m_FontFallbacks.push_back(std::make_unique<CFX_Font>());
380 FX_SAFE_INT32 safeWeight = m_StemV;
382 m_FontFallbacks[0]->LoadSubst(
"Arial", IsTrueTypeFont(), m_Flags,
384 m_ItalicAngle, FX_CodePage::kDefANSI,
391 if (!fxcrt::IndexInBounds(m_FontFallbacks, fallbackFont))
396 int glyph = m_FontFallbacks[fallbackFont]->GetFace()->GetCharIndex(unicode);
404 if (position < 0 ||
static_cast<size_t>(position) >= m_FontFallbacks.size())
406 return m_FontFallbacks[position].get();
411 int upm = face->GetUnitsPerEm();
413 return pdfium::base::saturated_cast<
int>(m);
415 const double dm = (m * 1000.0 + upm / 2) / upm;
416 return pdfium::base::saturated_cast<
int>(dm);
421 FXFT_FaceRec* rec = face->GetRec();
424 return FX_RECT(TT2PDF(left, face), TT2PDF(top, face),
433 for (size_t i = 0; i < face->GetCharMapCount(); i++) {
434 if (face->GetCharMapPlatformIdByIndex(i) == platform_id &&
435 face->GetCharMapEncodingIdByIndex(i) == encoding_id) {
436 face->SetCharMapByIndex(i);
444 FX_SAFE_INT32 safeStemV(m_StemV);
448 safeStemV = safeStemV * 4 + 140;
bool KeyExist(const ByteString &key) const
int GetIntegerFor(const ByteString &key) const
RetainPtr< const CPDF_Stream > GetStreamFor(const ByteString &key) const
int GetIntegerFor(const ByteString &key, int default_int) const
static CPDF_FontGlobals * GetInstance()
virtual FX_RECT GetCharBBox(uint32_t charcode)=0
virtual bool IsTrueTypeFont() const
virtual const CPDF_CIDFont * AsCIDFont() const
virtual void WillBeDestroyed()
int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode)
void LoadFontDescriptor(const CPDF_Dictionary *pFontDesc)
virtual CPDF_CIDFont * AsCIDFont()
absl::optional< FX_Charset > GetSubstFontCharset() const
virtual bool IsType1Font() const
virtual bool IsVertWriting() const
CPDF_Font(CPDF_Document *pDocument, RetainPtr< CPDF_Dictionary > pFontDict)
void LoadUnicodeMap() const
static RetainPtr< CPDF_Font > Create(CPDF_Document *pDoc, RetainPtr< CPDF_Dictionary > pFontDict, FormFactoryIface *pFactory)
virtual uint32_t CharCodeFromUnicode(wchar_t Unicode) const
virtual const CPDF_TrueTypeFont * AsTrueTypeFont() const
virtual const CPDF_Type1Font * AsType1Font() const
virtual bool IsCIDFont() const
virtual WideString UnicodeFromCharCode(uint32_t charcode) const
virtual const CPDF_Type3Font * AsType3Font() const
static RetainPtr< CPDF_Font > GetStockFont(CPDF_Document *pDoc, ByteStringView fontname)
static bool UseTTCharmap(const RetainPtr< CFX_Face > &face, int platform_id, int encoding_id)
virtual size_t CountChar(ByteStringView pString) const
void AppendChar(ByteString *str, uint32_t charcode) const
int GetStringWidth(ByteStringView pString)
virtual CPDF_Type1Font * AsType1Font()
virtual bool HasFontWidths() const
virtual bool IsType3Font() const
virtual CPDF_Type3Font * AsType3Font()
virtual CPDF_TrueTypeFont * AsTrueTypeFont()
int GetFontWeight() const
virtual int AppendChar(char *buf, uint32_t charcode) const
virtual uint32_t GetNextChar(ByteStringView pString, size_t *pOffset) const
uint32_t FallbackFontFromCharcode(uint32_t charcode)
bool IsStandardFont() const
static FX_RECT GetCharBBoxForFace(const RetainPtr< CFX_Face > &face)
CFX_Font * GetFontFallback(int position)
bool IsBase14Font() const
bool operator==(const ByteString &other) const
bool operator==(const char *ptr) const
CharType operator[](const size_t index) const
const char * CharNameFromPredefinedCharSet(FontEncoding encoding, uint8_t charcode)
#define FXFONT_USEEXTERNATTR
#define FXFONT_NONSYMBOLIC
#define FXFT_Get_Glyph_Width(face)
#define FXFT_Get_Glyph_Height(face)
#define FXFT_Get_Glyph_HoriBearingX(face)
#define FXFT_Get_Glyph_HoriBearingY(face)
const char kWinAnsiEncoding[]
FX_RECT & operator=(const FX_RECT &that)=default