1 // Copyright 2014 PDFium Authors. All rights reserved. 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 #ifndef CORE_INCLUDE_FXGE_FX_FONT_H_ 8 #define CORE_INCLUDE_FXGE_FX_FONT_H_ 9 10 #include <map> 11 #include <memory> 12 13 #include "core/include/fxcrt/fx_system.h" 14 #include "fx_dib.h" 15 16 typedef struct FT_FaceRec_* FXFT_Face; 17 typedef void* FXFT_Library; 18 19 class CFX_FaceCache; 20 class CFX_FontFaceInfo; 21 class CFX_FontMapper; 22 class CFX_PathData; 23 class CFX_SizeGlyphCache; 24 class CFX_SubstFont; 25 class CTTFontDesc; 26 class IFX_FontEncoding; 27 class IFX_SystemFontInfo; 28 29 #define FXFONT_FIXED_PITCH 0x01 30 #define FXFONT_SERIF 0x02 31 #define FXFONT_SYMBOLIC 0x04 32 #define FXFONT_SCRIPT 0x08 33 #define FXFONT_ITALIC 0x40 34 #define FXFONT_BOLD 0x40000 35 #define FXFONT_USEEXTERNATTR 0x80000 36 #define FXFONT_CIDFONT 0x100000 37 #ifdef PDF_ENABLE_XFA 38 #define FXFONT_EXACTMATCH 0x80000000 39 #endif // PDF_ENABLE_XFA 40 #define FXFONT_ANSI_CHARSET 0 41 #define FXFONT_DEFAULT_CHARSET 1 42 #define FXFONT_SYMBOL_CHARSET 2 43 #define FXFONT_SHIFTJIS_CHARSET 128 44 #define FXFONT_HANGEUL_CHARSET 129 45 #define FXFONT_GB2312_CHARSET 134 46 #define FXFONT_CHINESEBIG5_CHARSET 136 47 #define FXFONT_THAI_CHARSET 222 48 #define FXFONT_EASTEUROPE_CHARSET 238 49 #define FXFONT_RUSSIAN_CHARSET 204 50 #define FXFONT_GREEK_CHARSET 161 51 #define FXFONT_TURKISH_CHARSET 162 52 #define FXFONT_HEBREW_CHARSET 177 53 #define FXFONT_ARABIC_CHARSET 178 54 #define FXFONT_BALTIC_CHARSET 186 55 #define FXFONT_FF_FIXEDPITCH 1 56 #define FXFONT_FF_ROMAN (1 << 4) 57 #define FXFONT_FF_SCRIPT (4 << 4) 58 #define FXFONT_FW_NORMAL 400 59 #define FXFONT_FW_BOLD 700 60 61 class CFX_Font { 62 public: 63 CFX_Font(); 64 ~CFX_Font(); 65 66 void LoadSubst(const CFX_ByteString& face_name, 67 FX_BOOL bTrueType, 68 FX_DWORD flags, 69 int weight, 70 int italic_angle, 71 int CharsetCP, 72 FX_BOOL bVertical = FALSE); 73 FX_BOOL LoadEmbedded(const uint8_t* data, FX_DWORD size); 74 FXFT_Face GetFace() const { return m_Face; } 75 76 #ifdef PDF_ENABLE_XFA 77 FX_BOOL LoadFile(IFX_FileRead* pFile, 78 int nFaceIndex = 0, 79 int* pFaceCount = NULL); 80 81 FX_BOOL LoadClone(const CFX_Font* pFont); 82 CFX_SubstFont* GetSubstFont() const { return m_pSubstFont; } 83 void SetFace(FXFT_Face face) { m_Face = face; } 84 void SetSubstFont(CFX_SubstFont* subst) { m_pSubstFont = subst; } 85 #else // PDF_ENABLE_XFA 86 const CFX_SubstFont* GetSubstFont() const { return m_pSubstFont; } 87 #endif // PDF_ENABLE_XFA 88 89 CFX_PathData* LoadGlyphPath(FX_DWORD glyph_index, int dest_width = 0); 90 int GetGlyphWidth(FX_DWORD glyph_index); 91 int GetAscent() const; 92 int GetDescent() const; 93 FX_BOOL GetGlyphBBox(FX_DWORD glyph_index, FX_RECT& bbox); 94 FX_BOOL IsItalic() const; 95 FX_BOOL IsBold() const; 96 FX_BOOL IsFixedWidth() const; 97 FX_BOOL IsVertical() const { return m_bVertical; } 98 CFX_WideString GetPsName() const; 99 CFX_ByteString GetFamilyName() const; 100 CFX_ByteString GetFaceName() const; 101 FX_BOOL IsTTFont() const; 102 FX_BOOL GetBBox(FX_RECT& bbox); 103 int GetHeight() const; 104 int GetULPos() const; 105 int GetULthickness() const; 106 int GetMaxAdvanceWidth() const; 107 FX_BOOL IsEmbedded() const { return m_bEmbedded; } 108 uint8_t* GetSubData() const { return m_pGsubData; } 109 void SetSubData(uint8_t* data) { m_pGsubData = data; } 110 void* GetPlatformFont() const { return m_pPlatformFont; } 111 void SetPlatformFont(void* font) { m_pPlatformFont = font; } 112 uint8_t* GetFontData() const { return m_pFontData; } 113 FX_DWORD GetSize() const { return m_dwSize; } 114 void AdjustMMParams(int glyph_index, int width, int weight); 115 116 private: 117 void ReleasePlatformResource(); 118 void DeleteFace(); 119 120 FXFT_Face m_Face; 121 CFX_SubstFont* m_pSubstFont; 122 uint8_t* m_pFontDataAllocation; 123 uint8_t* m_pFontData; 124 uint8_t* m_pGsubData; 125 FX_DWORD m_dwSize; 126 CFX_BinaryBuf m_OtfFontData; 127 void* m_hHandle; 128 void* m_pPlatformFont; 129 void* m_pPlatformFontCollection; 130 void* m_pDwFont; 131 FX_BOOL m_bDwLoaded; 132 FX_BOOL m_bEmbedded; 133 FX_BOOL m_bVertical; 134 135 #ifdef PDF_ENABLE_XFA 136 protected: 137 FX_BOOL m_bLogic; 138 void* m_pOwnedStream; 139 #endif // PDF_ENABLE_XFA 140 }; 141 142 #define ENCODING_INTERNAL 0 143 #define ENCODING_UNICODE 1 144 145 #ifdef PDF_ENABLE_XFA 146 #define FXFM_ENC_TAG(a, b, c, d) \ 147 (((FX_DWORD)(a) << 24) | ((FX_DWORD)(b) << 16) | ((FX_DWORD)(c) << 8) | \ 148 (FX_DWORD)(d)) 149 #define FXFM_ENCODING_NONE FXFM_ENC_TAG(0, 0, 0, 0) 150 #define FXFM_ENCODING_MS_SYMBOL FXFM_ENC_TAG('s', 'y', 'm', 'b') 151 #define FXFM_ENCODING_UNICODE FXFM_ENC_TAG('u', 'n', 'i', 'c') 152 #define FXFM_ENCODING_MS_SJIS FXFM_ENC_TAG('s', 'j', 'i', 's') 153 #define FXFM_ENCODING_MS_GB2312 FXFM_ENC_TAG('g', 'b', ' ', ' ') 154 #define FXFM_ENCODING_MS_BIG5 FXFM_ENC_TAG('b', 'i', 'g', '5') 155 #define FXFM_ENCODING_MS_WANSUNG FXFM_ENC_TAG('w', 'a', 'n', 's') 156 #define FXFM_ENCODING_MS_JOHAB FXFM_ENC_TAG('j', 'o', 'h', 'a') 157 #define FXFM_ENCODING_ADOBE_STANDARD FXFM_ENC_TAG('A', 'D', 'O', 'B') 158 #define FXFM_ENCODING_ADOBE_EXPERT FXFM_ENC_TAG('A', 'D', 'B', 'E') 159 #define FXFM_ENCODING_ADOBE_CUSTOM FXFM_ENC_TAG('A', 'D', 'B', 'C') 160 #define FXFM_ENCODING_ADOBE_LATIN_1 FXFM_ENC_TAG('l', 'a', 't', '1') 161 #define FXFM_ENCODING_OLD_LATIN_2 FXFM_ENC_TAG('l', 'a', 't', '2') 162 #define FXFM_ENCODING_APPLE_ROMAN FXFM_ENC_TAG('a', 'r', 'm', 'n') 163 #endif // PDF_ENABLE_XFA 164 165 class CFX_UnicodeEncoding { 166 public: 167 explicit CFX_UnicodeEncoding(CFX_Font* pFont); 168 virtual ~CFX_UnicodeEncoding(); 169 170 virtual FX_DWORD GlyphFromCharCode(FX_DWORD charcode); 171 172 protected: 173 // Unowned, not nullptr. 174 CFX_Font* m_pFont; 175 }; 176 177 #ifdef PDF_ENABLE_XFA 178 class CFX_UnicodeEncodingEx : public CFX_UnicodeEncoding { 179 public: 180 CFX_UnicodeEncodingEx(CFX_Font* pFont, FX_DWORD EncodingID); 181 ~CFX_UnicodeEncodingEx() override; 182 183 // CFX_UnicodeEncoding: 184 FX_DWORD GlyphFromCharCode(FX_DWORD charcode) override; 185 186 FX_DWORD CharCodeFromUnicode(FX_WCHAR Unicode) const; 187 188 private: 189 FX_DWORD m_nEncodingID; 190 }; 191 CFX_UnicodeEncodingEx* FX_CreateFontEncodingEx( 192 CFX_Font* pFont, 193 FX_DWORD nEncodingID = FXFM_ENCODING_NONE); 194 #endif // PDF_ENABLE_XFA 195 196 #define FXFONT_SUBST_MM 0x01 197 #define FXFONT_SUBST_GLYPHPATH 0x04 198 #define FXFONT_SUBST_CLEARTYPE 0x08 199 #define FXFONT_SUBST_TRANSFORM 0x10 200 #define FXFONT_SUBST_NONSYMBOL 0x20 201 #define FXFONT_SUBST_EXACT 0x40 202 #define FXFONT_SUBST_STANDARD 0x80 203 204 class CFX_SubstFont { 205 public: 206 CFX_SubstFont(); 207 208 void* m_ExtHandle; 209 210 CFX_ByteString m_Family; 211 212 int m_Charset; 213 214 FX_DWORD m_SubstFlags; 215 216 int m_Weight; 217 218 int m_ItalicAngle; 219 220 FX_BOOL m_bSubstOfCJK; 221 222 int m_WeightCJK; 223 224 FX_BOOL m_bItlicCJK; 225 }; 226 #define FX_FONT_FLAG_SERIF 0x01 227 #define FX_FONT_FLAG_FIXEDPITCH 0x02 228 #define FX_FONT_FLAG_ITALIC 0x04 229 #define FX_FONT_FLAG_BOLD 0x08 230 #define FX_FONT_FLAG_SYMBOLIC_SYMBOL 0x10 231 #define FX_FONT_FLAG_SYMBOLIC_DINGBATS 0x20 232 #define FX_FONT_FLAG_MULTIPLEMASTER 0x40 233 234 class CFX_FontMgr { 235 public: 236 CFX_FontMgr(); 237 ~CFX_FontMgr(); 238 239 void InitFTLibrary(); 240 241 FXFT_Face GetCachedFace(const CFX_ByteString& face_name, 242 int weight, 243 FX_BOOL bItalic, 244 uint8_t*& pFontData); 245 FXFT_Face AddCachedFace(const CFX_ByteString& face_name, 246 int weight, 247 FX_BOOL bItalic, 248 uint8_t* pData, 249 FX_DWORD size, 250 int face_index); 251 FXFT_Face GetCachedTTCFace(int ttc_size, 252 FX_DWORD checksum, 253 int font_offset, 254 uint8_t*& pFontData); 255 FXFT_Face AddCachedTTCFace(int ttc_size, 256 FX_DWORD checksum, 257 uint8_t* pData, 258 FX_DWORD size, 259 int font_offset); 260 FXFT_Face GetFileFace(const FX_CHAR* filename, int face_index); 261 FXFT_Face GetFixedFace(const uint8_t* pData, FX_DWORD size, int face_index); 262 void ReleaseFace(FXFT_Face face); 263 void SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo); 264 FXFT_Face FindSubstFont(const CFX_ByteString& face_name, 265 FX_BOOL bTrueType, 266 FX_DWORD flags, 267 int weight, 268 int italic_angle, 269 int CharsetCP, 270 CFX_SubstFont* pSubstFont); 271 bool GetBuiltinFont(size_t index, const uint8_t** pFontData, FX_DWORD* size); 272 CFX_FontMapper* GetBuiltinMapper() const { return m_pBuiltinMapper.get(); } 273 FXFT_Library GetFTLibrary() const { return m_FTLibrary; } 274 275 private: 276 std::unique_ptr<CFX_FontMapper> m_pBuiltinMapper; 277 std::map<CFX_ByteString, CTTFontDesc*> m_FaceMap; 278 FXFT_Library m_FTLibrary; 279 }; 280 281 class IFX_FontEnumerator { 282 public: 283 virtual void HitFont() = 0; 284 285 virtual void Finish() = 0; 286 287 protected: 288 virtual ~IFX_FontEnumerator() {} 289 }; 290 291 class IFX_AdditionalFontEnum { 292 public: 293 virtual int CountFiles() = 0; 294 virtual IFX_FileStream* GetFontFile(int index) = 0; 295 296 protected: 297 virtual ~IFX_AdditionalFontEnum() {} 298 }; 299 300 class CFX_FontMapper { 301 public: 302 explicit CFX_FontMapper(CFX_FontMgr* mgr); 303 ~CFX_FontMapper(); 304 305 void SetSystemFontInfo(IFX_SystemFontInfo* pFontInfo); 306 IFX_SystemFontInfo* GetSystemFontInfo() { return m_pFontInfo; } 307 void AddInstalledFont(const CFX_ByteString& name, int charset); 308 void LoadInstalledFonts(); 309 CFX_ByteStringArray m_InstalledTTFonts; 310 void SetFontEnumerator(IFX_FontEnumerator* pFontEnumerator) { 311 m_pFontEnumerator = pFontEnumerator; 312 } 313 IFX_FontEnumerator* GetFontEnumerator() const { return m_pFontEnumerator; } 314 FXFT_Face FindSubstFont(const CFX_ByteString& face_name, 315 FX_BOOL bTrueType, 316 FX_DWORD flags, 317 int weight, 318 int italic_angle, 319 int CharsetCP, 320 CFX_SubstFont* pSubstFont); 321 #ifdef PDF_ENABLE_XFA 322 FXFT_Face FindSubstFontByUnicode(FX_DWORD dwUnicode, 323 FX_DWORD flags, 324 int weight, 325 int italic_angle); 326 #endif // PDF_ENABLE_XFA 327 FX_BOOL IsBuiltinFace(const FXFT_Face face) const; 328 329 private: 330 static const size_t MM_FACE_COUNT = 2; 331 static const size_t FOXIT_FACE_COUNT = 14; 332 333 CFX_ByteString GetPSNameFromTT(void* hFont); 334 CFX_ByteString MatchInstalledFonts(const CFX_ByteString& norm_name); 335 FXFT_Face UseInternalSubst(CFX_SubstFont* pSubstFont, 336 int iBaseFont, 337 int italic_angle, 338 int weight, 339 int picthfamily); 340 341 FX_BOOL m_bListLoaded; 342 FXFT_Face m_MMFaces[MM_FACE_COUNT]; 343 CFX_ByteString m_LastFamily; 344 CFX_DWordArray m_CharsetArray; 345 CFX_ByteStringArray m_FaceArray; 346 IFX_SystemFontInfo* m_pFontInfo; 347 FXFT_Face m_FoxitFaces[FOXIT_FACE_COUNT]; 348 IFX_FontEnumerator* m_pFontEnumerator; 349 CFX_FontMgr* const m_pFontMgr; 350 }; 351 352 class IFX_SystemFontInfo { 353 public: 354 static IFX_SystemFontInfo* CreateDefault(const char** pUserPaths); 355 virtual void Release() = 0; 356 357 virtual FX_BOOL EnumFontList(CFX_FontMapper* pMapper) = 0; 358 virtual void* MapFont(int weight, 359 FX_BOOL bItalic, 360 int charset, 361 int pitch_family, 362 const FX_CHAR* face, 363 int& iExact) = 0; 364 #ifdef PDF_ENABLE_XFA 365 virtual void* MapFontByUnicode(FX_DWORD dwUnicode, 366 int weight, 367 FX_BOOL bItalic, 368 int pitch_family) { 369 return NULL; 370 } 371 #endif // PDF_ENABLE_XFA 372 virtual void* GetFont(const FX_CHAR* face) = 0; 373 virtual FX_DWORD GetFontData(void* hFont, 374 FX_DWORD table, 375 uint8_t* buffer, 376 FX_DWORD size) = 0; 377 virtual FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) = 0; 378 virtual FX_BOOL GetFontCharset(void* hFont, int& charset) = 0; 379 virtual int GetFaceIndex(void* hFont) { return 0; } 380 virtual void DeleteFont(void* hFont) = 0; 381 virtual void* RetainFont(void* hFont) { return NULL; } 382 383 protected: 384 virtual ~IFX_SystemFontInfo() {} 385 }; 386 387 class CFX_FolderFontInfo : public IFX_SystemFontInfo { 388 public: 389 CFX_FolderFontInfo(); 390 ~CFX_FolderFontInfo() override; 391 void AddPath(const CFX_ByteStringC& path); 392 393 // IFX_SytemFontInfo: 394 void Release() override; 395 FX_BOOL EnumFontList(CFX_FontMapper* pMapper) override; 396 void* MapFont(int weight, 397 FX_BOOL bItalic, 398 int charset, 399 int pitch_family, 400 const FX_CHAR* face, 401 int& bExact) override; 402 #ifdef PDF_ENABLE_XFA 403 void* MapFontByUnicode(FX_DWORD dwUnicode, 404 int weight, 405 FX_BOOL bItalic, 406 int pitch_family) override; 407 #endif // PDF_ENABLE_XFA 408 void* GetFont(const FX_CHAR* face) override; 409 FX_DWORD GetFontData(void* hFont, 410 FX_DWORD table, 411 uint8_t* buffer, 412 FX_DWORD size) override; 413 void DeleteFont(void* hFont) override; 414 FX_BOOL GetFaceName(void* hFont, CFX_ByteString& name) override; 415 FX_BOOL GetFontCharset(void* hFont, int& charset) override; 416 417 protected: 418 std::map<CFX_ByteString, CFX_FontFaceInfo*> m_FontList; 419 CFX_ByteStringArray m_PathList; 420 CFX_FontMapper* m_pMapper; 421 void ScanPath(CFX_ByteString& path); 422 void ScanFile(CFX_ByteString& path); 423 void ReportFace(CFX_ByteString& path, 424 FXSYS_FILE* pFile, 425 FX_DWORD filesize, 426 FX_DWORD offset); 427 void* GetSubstFont(const CFX_ByteString& face); 428 void* FindFont(int weight, 429 FX_BOOL bItalic, 430 int charset, 431 int pitch_family, 432 const FX_CHAR* family, 433 FX_BOOL bMatchName); 434 }; 435 class CFX_CountedFaceCache { 436 public: 437 CFX_FaceCache* m_Obj; 438 FX_DWORD m_nCount; 439 }; 440 441 class CFX_FontCache { 442 public: 443 ~CFX_FontCache(); 444 CFX_FaceCache* GetCachedFace(CFX_Font* pFont); 445 void ReleaseCachedFace(CFX_Font* pFont); 446 void FreeCache(FX_BOOL bRelease = FALSE); 447 448 private: 449 using CFX_FTCacheMap = std::map<FXFT_Face, CFX_CountedFaceCache*>; 450 CFX_FTCacheMap m_FTFaceMap; 451 CFX_FTCacheMap m_ExtFaceMap; 452 }; 453 class CFX_AutoFontCache { 454 public: 455 CFX_AutoFontCache(CFX_FontCache* pFontCache, CFX_Font* pFont) 456 : m_pFontCache(pFontCache), m_pFont(pFont) {} 457 ~CFX_AutoFontCache() { m_pFontCache->ReleaseCachedFace(m_pFont); } 458 CFX_FontCache* m_pFontCache; 459 CFX_Font* m_pFont; 460 }; 461 #define FX_FONTCACHE_DEFINE(pFontCache, pFont) \ 462 CFX_AutoFontCache autoFontCache((pFontCache), (pFont)) 463 class CFX_GlyphBitmap { 464 public: 465 int m_Top; 466 int m_Left; 467 CFX_DIBitmap m_Bitmap; 468 }; 469 class CFX_FaceCache { 470 public: 471 explicit CFX_FaceCache(FXFT_Face face); 472 ~CFX_FaceCache(); 473 const CFX_GlyphBitmap* LoadGlyphBitmap(CFX_Font* pFont, 474 FX_DWORD glyph_index, 475 FX_BOOL bFontStyle, 476 const CFX_Matrix* pMatrix, 477 int dest_width, 478 int anti_alias, 479 int& text_flags); 480 const CFX_PathData* LoadGlyphPath(CFX_Font* pFont, 481 FX_DWORD glyph_index, 482 int dest_width); 483 484 private: 485 CFX_GlyphBitmap* RenderGlyph(CFX_Font* pFont, 486 FX_DWORD glyph_index, 487 FX_BOOL bFontStyle, 488 const CFX_Matrix* pMatrix, 489 int dest_width, 490 int anti_alias); 491 CFX_GlyphBitmap* RenderGlyph_Nativetext(CFX_Font* pFont, 492 FX_DWORD glyph_index, 493 const CFX_Matrix* pMatrix, 494 int dest_width, 495 int anti_alias); 496 CFX_GlyphBitmap* LookUpGlyphBitmap(CFX_Font* pFont, 497 const CFX_Matrix* pMatrix, 498 CFX_ByteStringC& FaceGlyphsKey, 499 FX_DWORD glyph_index, 500 FX_BOOL bFontStyle, 501 int dest_width, 502 int anti_alias); 503 void InitPlatform(); 504 void DestroyPlatform(); 505 506 FXFT_Face const m_Face; 507 std::map<CFX_ByteString, CFX_SizeGlyphCache*> m_SizeMap; 508 std::map<FX_DWORD, CFX_PathData*> m_PathMap; 509 CFX_DIBitmap* m_pBitmap; 510 }; 511 512 struct FXTEXT_GLYPHPOS { 513 const CFX_GlyphBitmap* m_pGlyph; 514 int m_OriginX; 515 int m_OriginY; 516 FX_FLOAT m_fOriginX; 517 FX_FLOAT m_fOriginY; 518 }; 519 520 FX_RECT FXGE_GetGlyphsBBox(FXTEXT_GLYPHPOS* pGlyphAndPos, 521 int nChars, 522 int anti_alias, 523 FX_FLOAT retinaScaleX = 1.0f, 524 FX_FLOAT retinaScaleY = 1.0f); 525 526 class IFX_GSUBTable { 527 public: 528 static IFX_GSUBTable* Create(CFX_Font* pFont); 529 virtual FX_BOOL GetVerticalGlyph(FX_DWORD glyphnum, FX_DWORD* vglyphnum) = 0; 530 531 protected: 532 virtual ~IFX_GSUBTable() {} 533 }; 534 535 CFX_ByteString GetNameFromTT(const uint8_t* name_table, FX_DWORD name); 536 537 int PDF_GetStandardFontName(CFX_ByteString* name); 538 539 #endif // CORE_INCLUDE_FXGE_FX_FONT_H_ 540