Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: core/fxge/android/cfpf_skiafontmgr.cpp

Issue 2433543002: Clean up fpf_skiafontmgr (Closed)
Patch Set: Use size_t Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « BUILD.gn ('k') | core/fxge/android/fpf_skiafont.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fxcrt/fx_system.h" 7 #include "core/fxcrt/fx_system.h"
8 8
9 #define FPF_SKIAMATCHWEIGHT_NAME1 62 9 #define FPF_SKIAMATCHWEIGHT_NAME1 62
10 #define FPF_SKIAMATCHWEIGHT_NAME2 60 10 #define FPF_SKIAMATCHWEIGHT_NAME2 60
11 #define FPF_SKIAMATCHWEIGHT_1 16 11 #define FPF_SKIAMATCHWEIGHT_1 16
12 #define FPF_SKIAMATCHWEIGHT_2 8 12 #define FPF_SKIAMATCHWEIGHT_2 8
13 13
14 #include <algorithm>
15
14 #include "core/fxcrt/fx_ext.h" 16 #include "core/fxcrt/fx_ext.h"
17 #include "core/fxcrt/fx_memory.h"
15 #include "core/fxge/android/fpf_skiafont.h" 18 #include "core/fxge/android/fpf_skiafont.h"
16 #include "core/fxge/android/fpf_skiafontmgr.h" 19 #include "core/fxge/android/fpf_skiafontmgr.h"
17 #include "core/fxge/fx_freetype.h" 20 #include "core/fxge/fx_freetype.h"
18 21
19 #ifdef __cplusplus 22 #ifdef __cplusplus
20 extern "C" { 23 extern "C" {
21 #endif 24 #endif
22 static unsigned long FPF_SkiaStream_Read(FXFT_Stream stream, 25 static unsigned long FPF_SkiaStream_Read(FXFT_Stream stream,
23 unsigned long offset, 26 unsigned long offset,
24 unsigned char* buffer, 27 unsigned char* buffer,
25 unsigned long count) { 28 unsigned long count) {
26 IFX_FileRead* pFileRead = (IFX_FileRead*)stream->descriptor.pointer; 29 IFX_FileRead* pFileRead = (IFX_FileRead*)stream->descriptor.pointer;
27 if (!pFileRead) { 30 if (!pFileRead)
28 return 0; 31 return 0;
29 }
30 if (count > 0) { 32 if (count > 0) {
31 if (!pFileRead->ReadBlock(buffer, (FX_FILESIZE)offset, (size_t)count)) { 33 if (!pFileRead->ReadBlock(buffer, (FX_FILESIZE)offset, (size_t)count))
32 return 0; 34 return 0;
33 }
34 } 35 }
35 return count; 36 return count;
36 } 37 }
38
37 static void FPF_SkiaStream_Close(FXFT_Stream stream) {} 39 static void FPF_SkiaStream_Close(FXFT_Stream stream) {}
38 #ifdef __cplusplus 40 #ifdef __cplusplus
39 }; 41 };
40 #endif 42 #endif
43
44 namespace {
45
41 struct FPF_SKIAFONTMAP { 46 struct FPF_SKIAFONTMAP {
42 uint32_t dwFamily; 47 uint32_t dwFamily;
43 uint32_t dwSubSt; 48 uint32_t dwSubSt;
44 }; 49 };
45 static const FPF_SKIAFONTMAP g_SkiaFontmap[] = { 50
51 const FPF_SKIAFONTMAP g_SkiaFontmap[] = {
46 {0x58c5083, 0xc8d2e345}, {0x5dfade2, 0xe1633081}, 52 {0x58c5083, 0xc8d2e345}, {0x5dfade2, 0xe1633081},
47 {0x684317d, 0xe1633081}, {0x14ee2d13, 0xc8d2e345}, 53 {0x684317d, 0xe1633081}, {0x14ee2d13, 0xc8d2e345},
48 {0x3918fe2d, 0xbbeeec72}, {0x3b98b31c, 0xe1633081}, 54 {0x3918fe2d, 0xbbeeec72}, {0x3b98b31c, 0xe1633081},
49 {0x3d49f40e, 0xe1633081}, {0x432c41c5, 0xe1633081}, 55 {0x3d49f40e, 0xe1633081}, {0x432c41c5, 0xe1633081},
50 {0x491b6ad0, 0xe1633081}, {0x5612cab1, 0x59b9f8f1}, 56 {0x491b6ad0, 0xe1633081}, {0x5612cab1, 0x59b9f8f1},
51 {0x779ce19d, 0xc8d2e345}, {0x7cc9510b, 0x59b9f8f1}, 57 {0x779ce19d, 0xc8d2e345}, {0x7cc9510b, 0x59b9f8f1},
52 {0x83746053, 0xbbeeec72}, {0xaaa60c03, 0xbbeeec72}, 58 {0x83746053, 0xbbeeec72}, {0xaaa60c03, 0xbbeeec72},
53 {0xbf85ff26, 0xe1633081}, {0xc04fe601, 0xbbeeec72}, 59 {0xbf85ff26, 0xe1633081}, {0xc04fe601, 0xbbeeec72},
54 {0xca3812d5, 0x59b9f8f1}, {0xca383e15, 0x59b9f8f1}, 60 {0xca3812d5, 0x59b9f8f1}, {0xca383e15, 0x59b9f8f1},
55 {0xcad5eaf6, 0x59b9f8f1}, {0xcb7a04c8, 0xc8d2e345}, 61 {0xcad5eaf6, 0x59b9f8f1}, {0xcb7a04c8, 0xc8d2e345},
56 {0xfb4ce0de, 0xe1633081}, 62 {0xfb4ce0de, 0xe1633081},
57 }; 63 };
58 uint32_t FPF_SkiaGetSubstFont(uint32_t dwHash) { 64
59 int32_t iStart = 0; 65 const FPF_SKIAFONTMAP g_SkiaSansFontMap[] = {
60 int32_t iEnd = sizeof(g_SkiaFontmap) / sizeof(FPF_SKIAFONTMAP);
61 while (iStart <= iEnd) {
62 int32_t iMid = (iStart + iEnd) / 2;
63 const FPF_SKIAFONTMAP* pItem = &g_SkiaFontmap[iMid];
64 if (dwHash < pItem->dwFamily) {
65 iEnd = iMid - 1;
66 } else if (dwHash > pItem->dwFamily) {
67 iStart = iMid + 1;
68 } else {
69 return pItem->dwSubSt;
70 }
71 }
72 return 0;
73 }
74 static const FPF_SKIAFONTMAP g_SkiaSansFontMap[] = {
75 {0x58c5083, 0xd5b8d10f}, {0x14ee2d13, 0xd5b8d10f}, 66 {0x58c5083, 0xd5b8d10f}, {0x14ee2d13, 0xd5b8d10f},
76 {0x779ce19d, 0xd5b8d10f}, {0xcb7a04c8, 0xd5b8d10f}, 67 {0x779ce19d, 0xd5b8d10f}, {0xcb7a04c8, 0xd5b8d10f},
77 {0xfb4ce0de, 0xd5b8d10f}, 68 {0xfb4ce0de, 0xd5b8d10f},
78 }; 69 };
79 uint32_t FPF_SkiaGetSansFont(uint32_t dwHash) { 70
80 int32_t iStart = 0; 71 uint32_t FPF_SkiaGetSubstFont(uint32_t dwHash,
81 int32_t iEnd = sizeof(g_SkiaSansFontMap) / sizeof(FPF_SKIAFONTMAP); 72 const FPF_SKIAFONTMAP* skFontMap,
82 while (iStart <= iEnd) { 73 size_t length) {
83 int32_t iMid = (iStart + iEnd) / 2; 74 const FPF_SKIAFONTMAP* pEnd = skFontMap + length;
84 const FPF_SKIAFONTMAP* pItem = &g_SkiaSansFontMap[iMid]; 75 const FPF_SKIAFONTMAP* pFontMap = std::lower_bound(
85 if (dwHash < pItem->dwFamily) { 76 skFontMap, pEnd, dwHash, [](const FPF_SKIAFONTMAP& item, uint32_t hash) {
86 iEnd = iMid - 1; 77 return item.dwFamily < hash;
87 } else if (dwHash > pItem->dwFamily) { 78 });
88 iStart = iMid + 1; 79 if (pFontMap < pEnd && pFontMap->dwFamily == dwHash)
89 } else { 80 return pFontMap->dwSubSt;
90 return pItem->dwSubSt;
91 }
92 }
93 return 0; 81 return 0;
94 } 82 }
95 static uint32_t FPF_GetHashCode_StringA(const FX_CHAR* pStr, 83
96 int32_t iLength, 84 uint32_t FPF_GetHashCode_StringA(const FX_CHAR* pStr, int32_t iLength) {
97 FX_BOOL bIgnoreCase = FALSE) { 85 if (!pStr)
98 if (!pStr) {
99 return 0; 86 return 0;
100 } 87 if (iLength < 0)
101 if (iLength < 0) {
102 iLength = FXSYS_strlen(pStr); 88 iLength = FXSYS_strlen(pStr);
103 }
104 const FX_CHAR* pStrEnd = pStr + iLength; 89 const FX_CHAR* pStrEnd = pStr + iLength;
105 uint32_t uHashCode = 0; 90 uint32_t uHashCode = 0;
106 if (bIgnoreCase) { 91 while (pStr < pStrEnd)
107 while (pStr < pStrEnd) { 92 uHashCode = 31 * uHashCode + FXSYS_tolower(*pStr++);
108 uHashCode = 31 * uHashCode + FXSYS_tolower(*pStr++);
109 }
110 } else {
111 while (pStr < pStrEnd) {
112 uHashCode = 31 * uHashCode + *pStr++;
113 }
114 }
115 return uHashCode; 93 return uHashCode;
116 } 94 }
95
117 enum FPF_SKIACHARSET { 96 enum FPF_SKIACHARSET {
118 FPF_SKIACHARSET_Ansi = 1 << 0, 97 FPF_SKIACHARSET_Ansi = 1 << 0,
119 FPF_SKIACHARSET_Default = 1 << 1, 98 FPF_SKIACHARSET_Default = 1 << 1,
120 FPF_SKIACHARSET_Symbol = 1 << 2, 99 FPF_SKIACHARSET_Symbol = 1 << 2,
121 FPF_SKIACHARSET_ShiftJIS = 1 << 3, 100 FPF_SKIACHARSET_ShiftJIS = 1 << 3,
122 FPF_SKIACHARSET_Korean = 1 << 4, 101 FPF_SKIACHARSET_Korean = 1 << 4,
123 FPF_SKIACHARSET_Johab = 1 << 5, 102 FPF_SKIACHARSET_Johab = 1 << 5,
124 FPF_SKIACHARSET_GB2312 = 1 << 6, 103 FPF_SKIACHARSET_GB2312 = 1 << 6,
125 FPF_SKIACHARSET_BIG5 = 1 << 7, 104 FPF_SKIACHARSET_BIG5 = 1 << 7,
126 FPF_SKIACHARSET_Greek = 1 << 8, 105 FPF_SKIACHARSET_Greek = 1 << 8,
127 FPF_SKIACHARSET_Turkish = 1 << 9, 106 FPF_SKIACHARSET_Turkish = 1 << 9,
128 FPF_SKIACHARSET_Vietnamese = 1 << 10, 107 FPF_SKIACHARSET_Vietnamese = 1 << 10,
129 FPF_SKIACHARSET_Hebrew = 1 << 11, 108 FPF_SKIACHARSET_Hebrew = 1 << 11,
130 FPF_SKIACHARSET_Arabic = 1 << 12, 109 FPF_SKIACHARSET_Arabic = 1 << 12,
131 FPF_SKIACHARSET_Baltic = 1 << 13, 110 FPF_SKIACHARSET_Baltic = 1 << 13,
132 FPF_SKIACHARSET_Cyrillic = 1 << 14, 111 FPF_SKIACHARSET_Cyrillic = 1 << 14,
133 FPF_SKIACHARSET_Thai = 1 << 15, 112 FPF_SKIACHARSET_Thai = 1 << 15,
134 FPF_SKIACHARSET_EeasternEuropean = 1 << 16, 113 FPF_SKIACHARSET_EeasternEuropean = 1 << 16,
135 FPF_SKIACHARSET_PC = 1 << 17, 114 FPF_SKIACHARSET_PC = 1 << 17,
136 FPF_SKIACHARSET_OEM = 1 << 18, 115 FPF_SKIACHARSET_OEM = 1 << 18,
137 }; 116 };
138 static uint32_t FPF_SkiaGetCharset(uint8_t uCharset) { 117
118 uint32_t FPF_SkiaGetCharset(uint8_t uCharset) {
139 switch (uCharset) { 119 switch (uCharset) {
140 case FXFONT_ANSI_CHARSET: 120 case FXFONT_ANSI_CHARSET:
141 return FPF_SKIACHARSET_Ansi; 121 return FPF_SKIACHARSET_Ansi;
142 case FXFONT_DEFAULT_CHARSET: 122 case FXFONT_DEFAULT_CHARSET:
143 return FPF_SKIACHARSET_Default; 123 return FPF_SKIACHARSET_Default;
144 case FXFONT_SYMBOL_CHARSET: 124 case FXFONT_SYMBOL_CHARSET:
145 return FPF_SKIACHARSET_Symbol; 125 return FPF_SKIACHARSET_Symbol;
146 case FXFONT_SHIFTJIS_CHARSET: 126 case FXFONT_SHIFTJIS_CHARSET:
147 return FPF_SKIACHARSET_ShiftJIS; 127 return FPF_SKIACHARSET_ShiftJIS;
148 case FXFONT_HANGUL_CHARSET: 128 case FXFONT_HANGUL_CHARSET:
(...skipping 14 matching lines...) Expand all
163 return FPF_SKIACHARSET_Baltic; 143 return FPF_SKIACHARSET_Baltic;
164 case FXFONT_RUSSIAN_CHARSET: 144 case FXFONT_RUSSIAN_CHARSET:
165 return FPF_SKIACHARSET_Cyrillic; 145 return FPF_SKIACHARSET_Cyrillic;
166 case FXFONT_THAI_CHARSET: 146 case FXFONT_THAI_CHARSET:
167 return FPF_SKIACHARSET_Thai; 147 return FPF_SKIACHARSET_Thai;
168 case FXFONT_EASTEUROPE_CHARSET: 148 case FXFONT_EASTEUROPE_CHARSET:
169 return FPF_SKIACHARSET_EeasternEuropean; 149 return FPF_SKIACHARSET_EeasternEuropean;
170 } 150 }
171 return FPF_SKIACHARSET_Default; 151 return FPF_SKIACHARSET_Default;
172 } 152 }
173 static uint32_t FPF_SKIANormalizeFontName(const CFX_ByteStringC& bsfamily) { 153
154 uint32_t FPF_SKIANormalizeFontName(const CFX_ByteStringC& bsfamily) {
174 uint32_t dwHash = 0; 155 uint32_t dwHash = 0;
175 int32_t iLength = bsfamily.GetLength(); 156 int32_t iLength = bsfamily.GetLength();
176 const FX_CHAR* pBuffer = bsfamily.c_str(); 157 const FX_CHAR* pBuffer = bsfamily.c_str();
177 for (int32_t i = 0; i < iLength; i++) { 158 for (int32_t i = 0; i < iLength; i++) {
178 FX_CHAR ch = pBuffer[i]; 159 FX_CHAR ch = pBuffer[i];
179 if (ch == ' ' || ch == '-' || ch == ',') { 160 if (ch == ' ' || ch == '-' || ch == ',')
180 continue; 161 continue;
181 }
182 dwHash = 31 * dwHash + FXSYS_tolower(ch); 162 dwHash = 31 * dwHash + FXSYS_tolower(ch);
183 } 163 }
184 return dwHash; 164 return dwHash;
185 } 165 }
186 static uint32_t FPF_SKIAGetFamilyHash(const CFX_ByteStringC& bsFamily, 166
187 uint32_t dwStyle, 167 uint32_t FPF_SKIAGetFamilyHash(const CFX_ByteStringC& bsFamily,
188 uint8_t uCharset) { 168 uint32_t dwStyle,
169 uint8_t uCharset) {
189 CFX_ByteString bsFont(bsFamily); 170 CFX_ByteString bsFont(bsFamily);
190 if (dwStyle & FXFONT_BOLD) { 171 if (dwStyle & FXFONT_BOLD)
191 bsFont += "Bold"; 172 bsFont += "Bold";
192 } 173 if (dwStyle & FXFONT_ITALIC)
193 if (dwStyle & FXFONT_ITALIC) {
194 bsFont += "Italic"; 174 bsFont += "Italic";
195 } 175 if (dwStyle & FXFONT_SERIF)
196 if (dwStyle & FXFONT_SERIF) {
197 bsFont += "Serif"; 176 bsFont += "Serif";
198 }
199 bsFont += uCharset; 177 bsFont += uCharset;
200 return FPF_GetHashCode_StringA(bsFont.c_str(), bsFont.GetLength(), TRUE); 178 return FPF_GetHashCode_StringA(bsFont.c_str(), bsFont.GetLength());
201 } 179 }
202 static FX_BOOL FPF_SkiaIsCJK(uint8_t uCharset) { 180
181 bool FPF_SkiaIsCJK(uint8_t uCharset) {
203 return (uCharset == FXFONT_GB2312_CHARSET) || 182 return (uCharset == FXFONT_GB2312_CHARSET) ||
204 (uCharset == FXFONT_CHINESEBIG5_CHARSET) || 183 (uCharset == FXFONT_CHINESEBIG5_CHARSET) ||
205 (uCharset == FXFONT_HANGUL_CHARSET) || 184 (uCharset == FXFONT_HANGUL_CHARSET) ||
206 (uCharset == FXFONT_SHIFTJIS_CHARSET); 185 (uCharset == FXFONT_SHIFTJIS_CHARSET);
207 } 186 }
208 static FX_BOOL FPF_SkiaMaybeSymbol(const CFX_ByteStringC& bsFacename) { 187
188 bool FPF_SkiaMaybeSymbol(const CFX_ByteStringC& bsFacename) {
209 CFX_ByteString bsName(bsFacename); 189 CFX_ByteString bsName(bsFacename);
210 bsName.MakeLower(); 190 bsName.MakeLower();
211 return bsName.Find("symbol") > -1; 191 return bsName.Find("symbol") > -1;
212 } 192 }
213 static FX_BOOL FPF_SkiaMaybeArabic(const CFX_ByteStringC& bsFacename) { 193
194 bool FPF_SkiaMaybeArabic(const CFX_ByteStringC& bsFacename) {
214 CFX_ByteString bsName(bsFacename); 195 CFX_ByteString bsName(bsFacename);
215 bsName.MakeLower(); 196 bsName.MakeLower();
216 return bsName.Find("arabic") > -1; 197 return bsName.Find("arabic") > -1;
217 } 198 }
218 CFPF_SkiaFontMgr::CFPF_SkiaFontMgr() : m_bLoaded(FALSE), m_FTLibrary(nullptr) {} 199
200 const uint32_t g_FPFSkiaFontCharsets[] = {
201 FPF_SKIACHARSET_Ansi,
202 FPF_SKIACHARSET_EeasternEuropean,
203 FPF_SKIACHARSET_Cyrillic,
204 FPF_SKIACHARSET_Greek,
205 FPF_SKIACHARSET_Turkish,
206 FPF_SKIACHARSET_Hebrew,
207 FPF_SKIACHARSET_Arabic,
208 FPF_SKIACHARSET_Baltic,
209 0,
210 0,
211 0,
212 0,
213 0,
214 0,
215 0,
216 0,
217 FPF_SKIACHARSET_Thai,
218 FPF_SKIACHARSET_ShiftJIS,
219 FPF_SKIACHARSET_GB2312,
220 FPF_SKIACHARSET_Korean,
221 FPF_SKIACHARSET_BIG5,
222 FPF_SKIACHARSET_Johab,
223 0,
224 0,
225 0,
226 0,
227 0,
228 0,
229 0,
230 0,
231 FPF_SKIACHARSET_OEM,
232 FPF_SKIACHARSET_Symbol,
233 };
234
235 uint32_t FPF_SkiaGetFaceCharset(TT_OS2* pOS2) {
236 uint32_t dwCharset = 0;
237 if (pOS2) {
238 for (int32_t i = 0; i < 32; i++) {
239 if (pOS2->ulCodePageRange1 & (1 << i))
240 dwCharset |= g_FPFSkiaFontCharsets[i];
241 }
242 }
243 dwCharset |= FPF_SKIACHARSET_Default;
244 return dwCharset;
245 }
246
247 } // namespace
248
249 CFPF_SkiaFontMgr::CFPF_SkiaFontMgr() : m_bLoaded(false), m_FTLibrary(nullptr) {}
250
219 CFPF_SkiaFontMgr::~CFPF_SkiaFontMgr() { 251 CFPF_SkiaFontMgr::~CFPF_SkiaFontMgr() {
220 for (const auto& pair : m_FamilyFonts) { 252 for (const auto& pair : m_FamilyFonts) {
221 if (pair.second) 253 if (pair.second)
222 pair.second->Release(); 254 pair.second->Release();
223 } 255 }
224 m_FamilyFonts.clear(); 256 m_FamilyFonts.clear();
225 for (auto it = m_FontFaces.rbegin(); it != m_FontFaces.rend(); ++it) { 257 for (auto it = m_FontFaces.begin(); it != m_FontFaces.end(); ++it)
226 delete *it; 258 delete *it;
227 }
228 m_FontFaces.clear(); 259 m_FontFaces.clear();
229 if (m_FTLibrary) { 260 if (m_FTLibrary)
230 FXFT_Done_FreeType(m_FTLibrary); 261 FXFT_Done_FreeType(m_FTLibrary);
231 }
232 } 262 }
233 FX_BOOL CFPF_SkiaFontMgr::InitFTLibrary() { 263
264 bool CFPF_SkiaFontMgr::InitFTLibrary() {
234 if (!m_FTLibrary) 265 if (!m_FTLibrary)
235 FXFT_Init_FreeType(&m_FTLibrary); 266 FXFT_Init_FreeType(&m_FTLibrary);
236 return !!m_FTLibrary; 267 return !!m_FTLibrary;
237 } 268 }
269
238 void CFPF_SkiaFontMgr::LoadSystemFonts() { 270 void CFPF_SkiaFontMgr::LoadSystemFonts() {
239 if (m_bLoaded) { 271 if (m_bLoaded)
240 return; 272 return;
241 }
242 ScanPath("/system/fonts"); 273 ScanPath("/system/fonts");
243 OutputSystemFonts(); 274 m_bLoaded = true;
244 m_bLoaded = TRUE;
245 } 275 }
246 void CFPF_SkiaFontMgr::LoadPrivateFont(IFX_FileRead* pFontFile) {}
247 void CFPF_SkiaFontMgr::LoadPrivateFont(const CFX_ByteStringC& bsFileName) {}
248 void CFPF_SkiaFontMgr::LoadPrivateFont(void* pBuffer, size_t szBuffer) {}
249 276
250 CFPF_SkiaFont* CFPF_SkiaFontMgr::CreateFont(const CFX_ByteStringC& bsFamilyname, 277 CFPF_SkiaFont* CFPF_SkiaFontMgr::CreateFont(const CFX_ByteStringC& bsFamilyname,
251 uint8_t uCharset, 278 uint8_t uCharset,
252 uint32_t dwStyle, 279 uint32_t dwStyle,
253 uint32_t dwMatch) { 280 uint32_t dwMatch) {
254 uint32_t dwHash = FPF_SKIAGetFamilyHash(bsFamilyname, dwStyle, uCharset); 281 uint32_t dwHash = FPF_SKIAGetFamilyHash(bsFamilyname, dwStyle, uCharset);
255 auto it = m_FamilyFonts.find(dwHash); 282 auto it = m_FamilyFonts.find(dwHash);
256 if (it != m_FamilyFonts.end() && it->second) 283 if (it != m_FamilyFonts.end() && it->second)
257 return it->second->Retain(); 284 return it->second->Retain();
258 285
259 uint32_t dwFaceName = FPF_SKIANormalizeFontName(bsFamilyname); 286 uint32_t dwFaceName = FPF_SKIANormalizeFontName(bsFamilyname);
260 uint32_t dwSubst = FPF_SkiaGetSubstFont(dwFaceName); 287 uint32_t dwSubst = FPF_SkiaGetSubstFont(dwFaceName, g_SkiaFontmap,
261 uint32_t dwSubstSans = FPF_SkiaGetSansFont(dwFaceName); 288 FX_ArraySize(g_SkiaFontmap));
262 FX_BOOL bMaybeSymbol = FPF_SkiaMaybeSymbol(bsFamilyname); 289 uint32_t dwSubstSans = FPF_SkiaGetSubstFont(dwFaceName, g_SkiaSansFontMap,
290 FX_ArraySize(g_SkiaSansFontMap));
291 bool bMaybeSymbol = FPF_SkiaMaybeSymbol(bsFamilyname);
263 if (uCharset != FXFONT_ARABIC_CHARSET && FPF_SkiaMaybeArabic(bsFamilyname)) { 292 if (uCharset != FXFONT_ARABIC_CHARSET && FPF_SkiaMaybeArabic(bsFamilyname)) {
264 uCharset = FXFONT_ARABIC_CHARSET; 293 uCharset = FXFONT_ARABIC_CHARSET;
265 } else if (uCharset == FXFONT_ANSI_CHARSET && 294 } else if (uCharset == FXFONT_ANSI_CHARSET &&
266 (dwMatch & FPF_MATCHFONT_REPLACEANSI)) { 295 (dwMatch & FPF_MATCHFONT_REPLACEANSI)) {
267 uCharset = FXFONT_DEFAULT_CHARSET; 296 uCharset = FXFONT_DEFAULT_CHARSET;
268 } 297 }
269 int32_t nExpectVal = FPF_SKIAMATCHWEIGHT_NAME1 + FPF_SKIAMATCHWEIGHT_1 * 3 + 298 int32_t nExpectVal = FPF_SKIAMATCHWEIGHT_NAME1 + FPF_SKIAMATCHWEIGHT_1 * 3 +
270 FPF_SKIAMATCHWEIGHT_2 * 2; 299 FPF_SKIAMATCHWEIGHT_2 * 2;
271 CFPF_SkiaFontDescriptor* pBestFontDes = nullptr; 300 CFPF_SkiaFontDescriptor* pBestFontDes = nullptr;
272 int32_t nMax = -1; 301 int32_t nMax = -1;
273 int32_t nGlyphNum = 0; 302 int32_t nGlyphNum = 0;
274 for (auto it = m_FontFaces.rbegin(); it != m_FontFaces.rend(); ++it) { 303 for (auto it = m_FontFaces.rbegin(); it != m_FontFaces.rend(); ++it) {
275 CFPF_SkiaPathFont* pFontDes = static_cast<CFPF_SkiaPathFont*>(*it); 304 CFPF_SkiaPathFont* pFontDes = static_cast<CFPF_SkiaPathFont*>(*it);
276 if (!(pFontDes->m_dwCharsets & FPF_SkiaGetCharset(uCharset))) { 305 if (!(pFontDes->m_dwCharsets & FPF_SkiaGetCharset(uCharset)))
277 continue; 306 continue;
278 }
279 int32_t nFind = 0; 307 int32_t nFind = 0;
280 uint32_t dwSysFontName = FPF_SKIANormalizeFontName(pFontDes->m_pFamily); 308 uint32_t dwSysFontName = FPF_SKIANormalizeFontName(pFontDes->m_pFamily);
281 if (dwFaceName == dwSysFontName) { 309 if (dwFaceName == dwSysFontName)
282 nFind += FPF_SKIAMATCHWEIGHT_NAME1; 310 nFind += FPF_SKIAMATCHWEIGHT_NAME1;
283 }
284 bool bMatchedName = (nFind == FPF_SKIAMATCHWEIGHT_NAME1); 311 bool bMatchedName = (nFind == FPF_SKIAMATCHWEIGHT_NAME1);
285 if ((dwStyle & FXFONT_BOLD) == (pFontDes->m_dwStyle & FXFONT_BOLD)) { 312 if ((dwStyle & FXFONT_BOLD) == (pFontDes->m_dwStyle & FXFONT_BOLD))
286 nFind += FPF_SKIAMATCHWEIGHT_1; 313 nFind += FPF_SKIAMATCHWEIGHT_1;
287 } 314 if ((dwStyle & FXFONT_ITALIC) == (pFontDes->m_dwStyle & FXFONT_ITALIC))
288 if ((dwStyle & FXFONT_ITALIC) == (pFontDes->m_dwStyle & FXFONT_ITALIC)) {
289 nFind += FPF_SKIAMATCHWEIGHT_1; 315 nFind += FPF_SKIAMATCHWEIGHT_1;
290 }
291 if ((dwStyle & FXFONT_FIXED_PITCH) == 316 if ((dwStyle & FXFONT_FIXED_PITCH) ==
292 (pFontDes->m_dwStyle & FXFONT_FIXED_PITCH)) { 317 (pFontDes->m_dwStyle & FXFONT_FIXED_PITCH)) {
293 nFind += FPF_SKIAMATCHWEIGHT_2; 318 nFind += FPF_SKIAMATCHWEIGHT_2;
294 } 319 }
295 if ((dwStyle & FXFONT_SERIF) == (pFontDes->m_dwStyle & FXFONT_SERIF)) { 320 if ((dwStyle & FXFONT_SERIF) == (pFontDes->m_dwStyle & FXFONT_SERIF))
296 nFind += FPF_SKIAMATCHWEIGHT_1; 321 nFind += FPF_SKIAMATCHWEIGHT_1;
297 } 322 if ((dwStyle & FXFONT_SCRIPT) == (pFontDes->m_dwStyle & FXFONT_SCRIPT))
298 if ((dwStyle & FXFONT_SCRIPT) == (pFontDes->m_dwStyle & FXFONT_SCRIPT)) {
299 nFind += FPF_SKIAMATCHWEIGHT_2; 323 nFind += FPF_SKIAMATCHWEIGHT_2;
300 }
301 if (dwSubst == dwSysFontName || dwSubstSans == dwSysFontName) { 324 if (dwSubst == dwSysFontName || dwSubstSans == dwSysFontName) {
302 nFind += FPF_SKIAMATCHWEIGHT_NAME2; 325 nFind += FPF_SKIAMATCHWEIGHT_NAME2;
303 bMatchedName = true; 326 bMatchedName = true;
304 } 327 }
305 if (uCharset == FXFONT_DEFAULT_CHARSET || bMaybeSymbol) { 328 if (uCharset == FXFONT_DEFAULT_CHARSET || bMaybeSymbol) {
306 if (nFind > nMax && bMatchedName) { 329 if (nFind > nMax && bMatchedName) {
307 nMax = nFind; 330 nMax = nFind;
308 pBestFontDes = *it; 331 pBestFontDes = *it;
309 } 332 }
310 } else if (FPF_SkiaIsCJK(uCharset)) { 333 } else if (FPF_SkiaIsCJK(uCharset)) {
(...skipping 16 matching lines...) Expand all
327 m_FamilyFonts[dwHash] = pFont; 350 m_FamilyFonts[dwHash] = pFont;
328 return pFont->Retain(); 351 return pFont->Retain();
329 } 352 }
330 pFont->Release(); 353 pFont->Release();
331 } 354 }
332 return nullptr; 355 return nullptr;
333 } 356 }
334 357
335 FXFT_Face CFPF_SkiaFontMgr::GetFontFace(IFX_FileRead* pFileRead, 358 FXFT_Face CFPF_SkiaFontMgr::GetFontFace(IFX_FileRead* pFileRead,
336 int32_t iFaceIndex) { 359 int32_t iFaceIndex) {
337 if (!pFileRead) { 360 if (!pFileRead)
338 return nullptr; 361 return nullptr;
339 } 362 if (pFileRead->GetSize() == 0)
340 if (pFileRead->GetSize() == 0) {
341 return nullptr; 363 return nullptr;
342 } 364 if (iFaceIndex < 0)
343 if (iFaceIndex < 0) {
344 return nullptr; 365 return nullptr;
345 }
346 FXFT_StreamRec streamRec; 366 FXFT_StreamRec streamRec;
347 FXSYS_memset(&streamRec, 0, sizeof(FXFT_StreamRec)); 367 FXSYS_memset(&streamRec, 0, sizeof(FXFT_StreamRec));
348 streamRec.size = pFileRead->GetSize(); 368 streamRec.size = pFileRead->GetSize();
349 streamRec.descriptor.pointer = pFileRead; 369 streamRec.descriptor.pointer = pFileRead;
350 streamRec.read = FPF_SkiaStream_Read; 370 streamRec.read = FPF_SkiaStream_Read;
351 streamRec.close = FPF_SkiaStream_Close; 371 streamRec.close = FPF_SkiaStream_Close;
352 FXFT_Open_Args args; 372 FXFT_Open_Args args;
353 args.flags = FT_OPEN_STREAM; 373 args.flags = FT_OPEN_STREAM;
354 args.stream = &streamRec; 374 args.stream = &streamRec;
355 FXFT_Face face; 375 FXFT_Face face;
356 if (FXFT_Open_Face(m_FTLibrary, &args, iFaceIndex, &face)) { 376 if (FXFT_Open_Face(m_FTLibrary, &args, iFaceIndex, &face))
357 return nullptr; 377 return nullptr;
358 }
359 FXFT_Set_Pixel_Sizes(face, 0, 64); 378 FXFT_Set_Pixel_Sizes(face, 0, 64);
360 return face; 379 return face;
361 } 380 }
381
362 FXFT_Face CFPF_SkiaFontMgr::GetFontFace(const CFX_ByteStringC& bsFile, 382 FXFT_Face CFPF_SkiaFontMgr::GetFontFace(const CFX_ByteStringC& bsFile,
363 int32_t iFaceIndex) { 383 int32_t iFaceIndex) {
364 if (bsFile.IsEmpty()) { 384 if (bsFile.IsEmpty())
365 return nullptr; 385 return nullptr;
366 } 386 if (iFaceIndex < 0)
367 if (iFaceIndex < 0) {
368 return nullptr; 387 return nullptr;
369 }
370 FXFT_Open_Args args; 388 FXFT_Open_Args args;
371 args.flags = FT_OPEN_PATHNAME; 389 args.flags = FT_OPEN_PATHNAME;
372 args.pathname = const_cast<FT_String*>(bsFile.c_str()); 390 args.pathname = const_cast<FT_String*>(bsFile.c_str());
373 FXFT_Face face; 391 FXFT_Face face;
374 if (FXFT_Open_Face(m_FTLibrary, &args, iFaceIndex, &face)) { 392 if (FXFT_Open_Face(m_FTLibrary, &args, iFaceIndex, &face))
375 return FALSE; 393 return nullptr;
376 }
377 FXFT_Set_Pixel_Sizes(face, 0, 64); 394 FXFT_Set_Pixel_Sizes(face, 0, 64);
378 return face; 395 return face;
379 } 396 }
397
380 FXFT_Face CFPF_SkiaFontMgr::GetFontFace(const uint8_t* pBuffer, 398 FXFT_Face CFPF_SkiaFontMgr::GetFontFace(const uint8_t* pBuffer,
381 size_t szBuffer, 399 size_t szBuffer,
382 int32_t iFaceIndex) { 400 int32_t iFaceIndex) {
383 if (!pBuffer || szBuffer < 1) { 401 if (!pBuffer || szBuffer < 1)
384 return nullptr; 402 return nullptr;
385 } 403 if (iFaceIndex < 0)
386 if (iFaceIndex < 0) {
387 return nullptr; 404 return nullptr;
388 }
389 FXFT_Open_Args args; 405 FXFT_Open_Args args;
390 args.flags = FT_OPEN_MEMORY; 406 args.flags = FT_OPEN_MEMORY;
391 args.memory_base = pBuffer; 407 args.memory_base = pBuffer;
392 args.memory_size = szBuffer; 408 args.memory_size = szBuffer;
393 FXFT_Face face; 409 FXFT_Face face;
394 if (FXFT_Open_Face(m_FTLibrary, &args, iFaceIndex, &face)) { 410 if (FXFT_Open_Face(m_FTLibrary, &args, iFaceIndex, &face))
395 return FALSE; 411 return nullptr;
396 }
397 FXFT_Set_Pixel_Sizes(face, 0, 64); 412 FXFT_Set_Pixel_Sizes(face, 0, 64);
398 return face; 413 return face;
399 } 414 }
400 415
401 void CFPF_SkiaFontMgr::ScanPath(const CFX_ByteString& path) { 416 void CFPF_SkiaFontMgr::ScanPath(const CFX_ByteString& path) {
402 DIR* handle = FX_OpenFolder(path.c_str()); 417 DIR* handle = FX_OpenFolder(path.c_str());
403 if (!handle) { 418 if (!handle)
404 return; 419 return;
405 }
406 CFX_ByteString filename; 420 CFX_ByteString filename;
407 bool bFolder = false; 421 bool bFolder = false;
408 while (FX_GetNextFile(handle, &filename, &bFolder)) { 422 while (FX_GetNextFile(handle, &filename, &bFolder)) {
409 if (bFolder) { 423 if (bFolder) {
410 if (filename == "." || filename == "..") 424 if (filename == "." || filename == "..")
411 continue; 425 continue;
412 } else { 426 } else {
413 CFX_ByteString ext = filename.Right(4); 427 CFX_ByteString ext = filename.Right(4);
414 ext.MakeLower(); 428 ext.MakeLower();
415 if (ext != ".ttf" && ext != ".ttc" && ext != ".otf") 429 if (ext != ".ttf" && ext != ".ttc" && ext != ".otf")
416 continue; 430 continue;
417 } 431 }
418 CFX_ByteString fullpath(path); 432 CFX_ByteString fullpath(path);
419 fullpath += "/"; 433 fullpath += "/";
420 fullpath += filename; 434 fullpath += filename;
421 if (bFolder) 435 if (bFolder)
422 ScanPath(fullpath); 436 ScanPath(fullpath);
423 else 437 else
424 ScanFile(fullpath); 438 ScanFile(fullpath);
425 } 439 }
426 FX_CloseFolder(handle); 440 FX_CloseFolder(handle);
427 } 441 }
428 442
429 void CFPF_SkiaFontMgr::ScanFile(const CFX_ByteString& file) { 443 void CFPF_SkiaFontMgr::ScanFile(const CFX_ByteString& file) {
430 FXFT_Face face = GetFontFace(file.AsStringC()); 444 FXFT_Face face = GetFontFace(file.AsStringC());
431 if (face) { 445 if (!face)
432 CFPF_SkiaPathFont* pFontDesc = new CFPF_SkiaPathFont; 446 return;
433 pFontDesc->SetPath(file.c_str()); 447 CFPF_SkiaPathFont* pFontDesc = new CFPF_SkiaPathFont;
434 ReportFace(face, pFontDesc); 448 pFontDesc->SetPath(file.c_str());
435 m_FontFaces.push_back(pFontDesc); 449 ReportFace(face, pFontDesc);
436 FXFT_Done_Face(face); 450 m_FontFaces.push_back(pFontDesc);
437 } 451 FXFT_Done_Face(face);
438 }
439
440 static const uint32_t g_FPFSkiaFontCharsets[] = {
441 FPF_SKIACHARSET_Ansi,
442 FPF_SKIACHARSET_EeasternEuropean,
443 FPF_SKIACHARSET_Cyrillic,
444 FPF_SKIACHARSET_Greek,
445 FPF_SKIACHARSET_Turkish,
446 FPF_SKIACHARSET_Hebrew,
447 FPF_SKIACHARSET_Arabic,
448 FPF_SKIACHARSET_Baltic,
449 0,
450 0,
451 0,
452 0,
453 0,
454 0,
455 0,
456 0,
457 FPF_SKIACHARSET_Thai,
458 FPF_SKIACHARSET_ShiftJIS,
459 FPF_SKIACHARSET_GB2312,
460 FPF_SKIACHARSET_Korean,
461 FPF_SKIACHARSET_BIG5,
462 FPF_SKIACHARSET_Johab,
463 0,
464 0,
465 0,
466 0,
467 0,
468 0,
469 0,
470 0,
471 FPF_SKIACHARSET_OEM,
472 FPF_SKIACHARSET_Symbol,
473 };
474
475 static uint32_t FPF_SkiaGetFaceCharset(TT_OS2* pOS2) {
476 uint32_t dwCharset = 0;
477 if (pOS2) {
478 for (int32_t i = 0; i < 32; i++) {
479 if (pOS2->ulCodePageRange1 & (1 << i)) {
480 dwCharset |= g_FPFSkiaFontCharsets[i];
481 }
482 }
483 }
484 dwCharset |= FPF_SKIACHARSET_Default;
485 return dwCharset;
486 } 452 }
487 453
488 void CFPF_SkiaFontMgr::ReportFace(FXFT_Face face, 454 void CFPF_SkiaFontMgr::ReportFace(FXFT_Face face,
489 CFPF_SkiaFontDescriptor* pFontDesc) { 455 CFPF_SkiaFontDescriptor* pFontDesc) {
490 if (!face || !pFontDesc) { 456 if (!face || !pFontDesc)
491 return; 457 return;
492 }
493 pFontDesc->SetFamily(FXFT_Get_Face_Family_Name(face)); 458 pFontDesc->SetFamily(FXFT_Get_Face_Family_Name(face));
494 if (FXFT_Is_Face_Bold(face)) { 459 if (FXFT_Is_Face_Bold(face))
495 pFontDesc->m_dwStyle |= FXFONT_BOLD; 460 pFontDesc->m_dwStyle |= FXFONT_BOLD;
496 } 461 if (FXFT_Is_Face_Italic(face))
497 if (FXFT_Is_Face_Italic(face)) {
498 pFontDesc->m_dwStyle |= FXFONT_ITALIC; 462 pFontDesc->m_dwStyle |= FXFONT_ITALIC;
499 } 463 if (FT_IS_FIXED_WIDTH(face))
500 if (FT_IS_FIXED_WIDTH(face)) {
501 pFontDesc->m_dwStyle |= FXFONT_FIXED_PITCH; 464 pFontDesc->m_dwStyle |= FXFONT_FIXED_PITCH;
502 }
503 TT_OS2* pOS2 = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2); 465 TT_OS2* pOS2 = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
504 if (pOS2) { 466 if (pOS2) {
505 if (pOS2->ulCodePageRange1 & (1 << 31)) { 467 if (pOS2->ulCodePageRange1 & (1 << 31))
506 pFontDesc->m_dwStyle |= FXFONT_SYMBOLIC; 468 pFontDesc->m_dwStyle |= FXFONT_SYMBOLIC;
507 }
508 if (pOS2->panose[0] == 2) { 469 if (pOS2->panose[0] == 2) {
509 uint8_t uSerif = pOS2->panose[1]; 470 uint8_t uSerif = pOS2->panose[1];
510 if ((uSerif > 1 && uSerif < 10) || uSerif > 13) { 471 if ((uSerif > 1 && uSerif < 10) || uSerif > 13)
511 pFontDesc->m_dwStyle |= FXFONT_SERIF; 472 pFontDesc->m_dwStyle |= FXFONT_SERIF;
512 }
513 } 473 }
514 } 474 }
515 if (pOS2 && (pOS2->ulCodePageRange1 & (1 << 31))) { 475 if (pOS2 && (pOS2->ulCodePageRange1 & (1 << 31)))
516 pFontDesc->m_dwStyle |= FXFONT_SYMBOLIC; 476 pFontDesc->m_dwStyle |= FXFONT_SYMBOLIC;
517 }
518 pFontDesc->m_dwCharsets = FPF_SkiaGetFaceCharset(pOS2); 477 pFontDesc->m_dwCharsets = FPF_SkiaGetFaceCharset(pOS2);
519 pFontDesc->m_iFaceIndex = face->face_index; 478 pFontDesc->m_iFaceIndex = face->face_index;
520 pFontDesc->m_iGlyphNum = face->num_glyphs; 479 pFontDesc->m_iGlyphNum = face->num_glyphs;
521 } 480 }
522
523 void CFPF_SkiaFontMgr::OutputSystemFonts() {}
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | core/fxge/android/fpf_skiafont.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698