blob: f0f3fb83dc5ce958faba06c52b1d480e4e459095 [file] [log] [blame]
npmf73893a2016-07-27 13:54:25 -07001// Copyright 2016 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
dsinclair74a34fc2016-09-29 16:41:42 -07007#ifndef CORE_FXGE_CFX_FONTMAPPER_H_
8#define CORE_FXGE_CFX_FONTMAPPER_H_
npmf73893a2016-07-27 13:54:25 -07009
10#include <memory>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -050011#include <utility>
npmf73893a2016-07-27 13:54:25 -070012#include <vector>
13
Tom Sepez0c036ac2021-04-26 17:36:28 +000014#include "build/build_config.h"
Tom Sepez1e151c62021-11-22 23:46:09 +000015#include "core/fxcrt/bytestring.h"
Tom Sepez6c57cda2021-07-23 19:41:57 +000016#include "core/fxcrt/fx_codepage_forward.h"
Lei Zhangff87ac92021-04-13 17:46:26 +000017#include "core/fxcrt/retain_ptr.h"
Lei Zhang75892802020-09-24 21:22:56 +000018#include "core/fxcrt/unowned_ptr.h"
Tom Sepez3512b962019-06-12 17:19:56 +000019#include "core/fxge/cfx_face.h"
Lei Zhang455cf862021-10-08 01:01:50 +000020#include "third_party/abseil-cpp/absl/types/optional.h"
npmf73893a2016-07-27 13:54:25 -070021
Lei Zhangd8948372022-09-15 21:24:21 +000022#ifdef PDF_ENABLE_XFA
23#include "core/fxcrt/fixed_uninit_data_vector.h"
24#endif
25
Lei Zhang96d6b4d2018-01-11 14:31:41 +000026class CFX_FontMgr;
npm81ee14d2016-08-29 09:35:12 -070027class CFX_SubstFont;
Dan Sinclairf8af5652018-03-06 18:56:33 +000028class SystemFontInfoIface;
npm81ee14d2016-08-29 09:35:12 -070029
npmf73893a2016-07-27 13:54:25 -070030class CFX_FontMapper {
31 public:
Lei Zhangc4e1ba62020-05-18 20:58:10 +000032 enum StandardFont : uint8_t {
Tom Sepezee894b62019-06-17 21:40:09 +000033 kCourier = 0,
34 kCourierBold,
35 kCourierBoldOblique,
36 kCourierOblique,
37 kHelvetica,
38 kHelveticaBold,
39 kHelveticaBoldOblique,
40 kHelveticaOblique,
41 kTimes,
42 kTimesBold,
43 kTimesBoldOblique,
44 kTimesOblique,
45 kSymbol,
46 kDingbats,
Lei Zhang2b682592021-04-19 18:27:03 +000047 kLast = kDingbats
Tom Sepezee894b62019-06-17 21:40:09 +000048 };
Lei Zhanga2378492021-10-26 17:13:22 +000049 static constexpr int kNumStandardFonts = 14;
Tom Sepezee894b62019-06-17 21:40:09 +000050
npmf73893a2016-07-27 13:54:25 -070051 explicit CFX_FontMapper(CFX_FontMgr* mgr);
52 ~CFX_FontMapper();
53
Lei Zhang2c495302021-10-07 23:13:30 +000054 static absl::optional<StandardFont> GetStandardFontName(ByteString* name);
Lei Zhang2b682592021-04-19 18:27:03 +000055 static bool IsStandardFontName(const ByteString& name);
Tom Sepezee894b62019-06-17 21:40:09 +000056 static bool IsSymbolicFont(StandardFont font);
57 static bool IsFixedFont(StandardFont font);
Lei Zhangaddc9bf2019-12-19 18:37:06 +000058 static constexpr uint32_t MakeTag(char c1, char c2, char c3, char c4) {
59 return static_cast<uint8_t>(c1) << 24 | static_cast<uint8_t>(c2) << 16 |
60 static_cast<uint8_t>(c3) << 8 | static_cast<uint8_t>(c4);
61 }
Lei Zhang1bdc49a2019-02-07 01:23:14 +000062
Dan Sinclairf8af5652018-03-06 18:56:33 +000063 void SetSystemFontInfo(std::unique_ptr<SystemFontInfoIface> pFontInfo);
Tom Andersond4fe5f72021-12-03 20:52:52 +000064 std::unique_ptr<SystemFontInfoIface> TakeSystemFontInfo();
Tom Sepezd6839862021-07-26 22:49:03 +000065 void AddInstalledFont(const ByteString& name, FX_Charset charset);
npmf73893a2016-07-27 13:54:25 -070066 void LoadInstalledFonts();
67
Tom Sepez3512b962019-06-12 17:19:56 +000068 RetainPtr<CFX_Face> FindSubstFont(const ByteString& face_name,
Lei Zhang4aba5752022-04-01 18:47:50 +000069 bool is_truetype,
Tom Sepez3512b962019-06-12 17:19:56 +000070 uint32_t flags,
71 int weight,
72 int italic_angle,
Tom Sepez6c57cda2021-07-23 19:41:57 +000073 FX_CodePage code_page,
Lei Zhang4aba5752022-04-01 18:47:50 +000074 CFX_SubstFont* subst_font);
Tom Sepezdf93cd32018-05-25 22:58:39 +000075
Lei Zhangd5c4e5e2021-10-21 21:58:18 +000076 size_t GetFaceSize() const;
Lei Zhangc1d4c192022-03-29 00:53:10 +000077 // `index` must be less than GetFaceSize().
78 ByteString GetFaceName(size_t index) const;
Tom Sepez0c036ac2021-04-26 17:36:28 +000079 bool HasInstalledFont(ByteStringView name) const;
80 bool HasLocalizedFont(ByteStringView name) const;
81
Lei Zhangf997abe2022-01-12 19:14:23 +000082#if BUILDFLAG(IS_WIN)
Lei Zhang2c495302021-10-07 23:13:30 +000083 absl::optional<ByteString> InstalledFontNameStartingWith(
Tom Sepez0c036ac2021-04-26 17:36:28 +000084 const ByteString& name) const;
Lei Zhang2c495302021-10-07 23:13:30 +000085 absl::optional<ByteString> LocalizedFontNameStartingWith(
Tom Sepez0c036ac2021-04-26 17:36:28 +000086 const ByteString& name) const;
Lei Zhangf997abe2022-01-12 19:14:23 +000087#endif // BUILDFLAG(IS_WIN)
npmf73893a2016-07-27 13:54:25 -070088
Tom Sepez8fcf5d42020-01-24 22:51:50 +000089#ifdef PDF_ENABLE_XFA
Lei Zhangc1d4c192022-03-29 00:53:10 +000090 // `index` must be less than GetFaceSize().
Lei Zhangd8948372022-09-15 21:24:21 +000091 FixedUninitDataVector<uint8_t> RawBytesForIndex(size_t index);
Tom Sepez8fcf5d42020-01-24 22:51:50 +000092#endif // PDF_ENABLE_XFA
93
npmf73893a2016-07-27 13:54:25 -070094 private:
Lei Zhang72312d22022-10-14 10:32:10 +000095 friend class TestFontMapper;
96
Lei Zhang4aba5752022-04-01 18:47:50 +000097 uint32_t GetChecksumFromTT(void* font_handle);
98 ByteString GetPSNameFromTT(void* font_handle);
Ryan Harrison275e2602017-09-18 14:23:18 -040099 ByteString MatchInstalledFonts(const ByteString& norm_name);
Lei Zhang4aba5752022-04-01 18:47:50 +0000100 RetainPtr<CFX_Face> UseInternalSubst(int base_font,
Tom Sepez3512b962019-06-12 17:19:56 +0000101 int weight,
Lei Zhang4aba5752022-04-01 18:47:50 +0000102 int italic_angle,
103 int pitch_family,
104 CFX_SubstFont* subst_font);
Lei Zhangbc2fde82022-04-01 19:43:50 +0000105 RetainPtr<CFX_Face> UseExternalSubst(void* font_handle,
106 ByteString face_name,
107 int weight,
108 bool is_italic,
109 int italic_angle,
110 FX_Charset charset,
111 CFX_SubstFont* subst_font);
Lei Zhang4aba5752022-04-01 18:47:50 +0000112 RetainPtr<CFX_Face> GetCachedTTCFace(void* font_handle,
Tom Sepez6d041c72022-03-02 20:20:39 +0000113 size_t ttc_size,
114 size_t data_size);
Lei Zhang4aba5752022-04-01 18:47:50 +0000115 RetainPtr<CFX_Face> GetCachedFace(void* font_handle,
116 ByteString subst_name,
Tom Sepez3512b962019-06-12 17:19:56 +0000117 int weight,
Lei Zhang4aba5752022-04-01 18:47:50 +0000118 bool is_italic,
Tom Sepez6d041c72022-03-02 20:20:39 +0000119 size_t data_size);
npmf73893a2016-07-27 13:54:25 -0700120
121 struct FaceData {
Ryan Harrison275e2602017-09-18 14:23:18 -0400122 ByteString name;
npmf73893a2016-07-27 13:54:25 -0700123 uint32_t charset;
124 };
125
Tom Sepezba8e7542019-06-07 22:58:40 +0000126 bool m_bListLoaded = false;
Ryan Harrison275e2602017-09-18 14:23:18 -0400127 ByteString m_LastFamily;
npmf73893a2016-07-27 13:54:25 -0700128 std::vector<FaceData> m_FaceArray;
Dan Sinclairf8af5652018-03-06 18:56:33 +0000129 std::unique_ptr<SystemFontInfoIface> m_pFontInfo;
Dan Sinclairaee0db02017-09-21 16:53:58 -0400130 UnownedPtr<CFX_FontMgr> const m_pFontMgr;
Tom Sepez0c036ac2021-04-26 17:36:28 +0000131 std::vector<ByteString> m_InstalledTTFonts;
132 std::vector<std::pair<ByteString, ByteString>> m_LocalizedTTFonts;
Lei Zhanga2378492021-10-26 17:13:22 +0000133 RetainPtr<CFX_Face> m_StandardFaces[kNumStandardFonts];
134 RetainPtr<CFX_Face> m_GenericSansFace;
135 RetainPtr<CFX_Face> m_GenericSerifFace;
npmf73893a2016-07-27 13:54:25 -0700136};
137
dsinclair74a34fc2016-09-29 16:41:42 -0700138#endif // CORE_FXGE_CFX_FONTMAPPER_H_