OLD | NEW |
| (Empty) |
1 // 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 | |
7 #include "core/fpdfapi/fpdf_font/cpdf_type1font.h" | |
8 | |
9 #include "core/fpdfapi/fpdf_font/font_int.h" | |
10 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" | |
11 #include "core/fxge/cfx_gemodule.h" | |
12 #include "core/fxge/fx_freetype.h" | |
13 | |
14 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
15 #include "core/fxge/apple/apple_int.h" | |
16 #endif | |
17 | |
18 namespace { | |
19 | |
20 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
21 struct GlyphNameMap { | |
22 const FX_CHAR* m_pStrAdobe; | |
23 const FX_CHAR* m_pStrUnicode; | |
24 }; | |
25 | |
26 const GlyphNameMap g_GlyphNameSubsts[] = {{"ff", "uniFB00"}, | |
27 {"ffi", "uniFB03"}, | |
28 {"ffl", "uniFB04"}, | |
29 {"fi", "uniFB01"}, | |
30 {"fl", "uniFB02"}}; | |
31 | |
32 int compareString(const void* key, const void* element) { | |
33 return FXSYS_stricmp(static_cast<const FX_CHAR*>(key), | |
34 static_cast<const GlyphNameMap*>(element)->m_pStrAdobe); | |
35 } | |
36 | |
37 const FX_CHAR* GlyphNameRemap(const FX_CHAR* pStrAdobe) { | |
38 const GlyphNameMap* found = static_cast<const GlyphNameMap*>(FXSYS_bsearch( | |
39 pStrAdobe, g_GlyphNameSubsts, FX_ArraySize(g_GlyphNameSubsts), | |
40 sizeof(GlyphNameMap), compareString)); | |
41 return found ? found->m_pStrUnicode : nullptr; | |
42 } | |
43 | |
44 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
45 | |
46 FX_BOOL FT_UseType1Charmap(FXFT_Face face) { | |
47 if (FXFT_Get_Face_CharmapCount(face) == 0) { | |
48 return FALSE; | |
49 } | |
50 if (FXFT_Get_Face_CharmapCount(face) == 1 && | |
51 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) == | |
52 FXFT_ENCODING_UNICODE) { | |
53 return FALSE; | |
54 } | |
55 if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[0]) == | |
56 FXFT_ENCODING_UNICODE) { | |
57 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[1]); | |
58 } else { | |
59 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[0]); | |
60 } | |
61 return TRUE; | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 CPDF_Type1Font::CPDF_Type1Font() : m_Base14Font(-1) {} | |
67 | |
68 bool CPDF_Type1Font::IsType1Font() const { | |
69 return true; | |
70 } | |
71 | |
72 const CPDF_Type1Font* CPDF_Type1Font::AsType1Font() const { | |
73 return this; | |
74 } | |
75 | |
76 CPDF_Type1Font* CPDF_Type1Font::AsType1Font() { | |
77 return this; | |
78 } | |
79 | |
80 bool CPDF_Type1Font::Load() { | |
81 m_Base14Font = PDF_GetStandardFontName(&m_BaseFont); | |
82 if (m_Base14Font >= 0) { | |
83 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictFor("FontDescriptor"); | |
84 if (pFontDesc && pFontDesc->KeyExist("Flags")) | |
85 m_Flags = pFontDesc->GetIntegerFor("Flags"); | |
86 else | |
87 m_Flags = m_Base14Font >= 12 ? PDFFONT_SYMBOLIC : PDFFONT_NONSYMBOLIC; | |
88 | |
89 if (m_Base14Font < 4) { | |
90 for (int i = 0; i < 256; i++) | |
91 m_CharWidth[i] = 600; | |
92 } | |
93 if (m_Base14Font == 12) | |
94 m_BaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL; | |
95 else if (m_Base14Font == 13) | |
96 m_BaseEncoding = PDFFONT_ENCODING_ZAPFDINGBATS; | |
97 else if (m_Flags & PDFFONT_NONSYMBOLIC) | |
98 m_BaseEncoding = PDFFONT_ENCODING_STANDARD; | |
99 } | |
100 return LoadCommon(); | |
101 } | |
102 | |
103 int CPDF_Type1Font::GlyphFromCharCodeExt(uint32_t charcode) { | |
104 if (charcode > 0xff) { | |
105 return -1; | |
106 } | |
107 int index = m_ExtGID[(uint8_t)charcode]; | |
108 if (index == 0xffff) { | |
109 return -1; | |
110 } | |
111 return index; | |
112 } | |
113 | |
114 void CPDF_Type1Font::LoadGlyphMap() { | |
115 if (!m_Font.GetFace()) | |
116 return; | |
117 | |
118 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
119 bool bCoreText = true; | |
120 CQuartz2D& quartz2d = | |
121 static_cast<CApplePlatform*>(CFX_GEModule::Get()->GetPlatformData()) | |
122 ->m_quartz2d; | |
123 if (!m_Font.GetPlatformFont()) { | |
124 if (m_Font.GetPsName() == "DFHeiStd-W5") | |
125 bCoreText = false; | |
126 | |
127 m_Font.SetPlatformFont( | |
128 quartz2d.CreateFont(m_Font.GetFontData(), m_Font.GetSize())); | |
129 if (!m_Font.GetPlatformFont()) | |
130 bCoreText = false; | |
131 } | |
132 #endif | |
133 if (!IsEmbedded() && (m_Base14Font < 12) && m_Font.IsTTFont()) { | |
134 if (FT_UseTTCharmap(m_Font.GetFace(), 3, 0)) { | |
135 FX_BOOL bGotOne = FALSE; | |
136 for (int charcode = 0; charcode < 256; charcode++) { | |
137 const uint8_t prefix[4] = {0x00, 0xf0, 0xf1, 0xf2}; | |
138 for (int j = 0; j < 4; j++) { | |
139 uint16_t unicode = prefix[j] * 256 + charcode; | |
140 m_GlyphIndex[charcode] = | |
141 FXFT_Get_Char_Index(m_Font.GetFace(), unicode); | |
142 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
143 FX_CHAR name_glyph[256]; | |
144 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], | |
145 name_glyph, 256); | |
146 name_glyph[255] = 0; | |
147 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
148 kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, | |
149 kCFAllocatorNull); | |
150 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
151 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
152 if (name_ct) { | |
153 CFRelease(name_ct); | |
154 } | |
155 #endif | |
156 if (m_GlyphIndex[charcode]) { | |
157 bGotOne = TRUE; | |
158 break; | |
159 } | |
160 } | |
161 } | |
162 if (bGotOne) { | |
163 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
164 if (!bCoreText) | |
165 FXSYS_memcpy(m_ExtGID, m_GlyphIndex, 256); | |
166 #endif | |
167 return; | |
168 } | |
169 } | |
170 FXFT_Select_Charmap(m_Font.GetFace(), FXFT_ENCODING_UNICODE); | |
171 if (m_BaseEncoding == 0) { | |
172 m_BaseEncoding = PDFFONT_ENCODING_STANDARD; | |
173 } | |
174 for (int charcode = 0; charcode < 256; charcode++) { | |
175 const FX_CHAR* name = | |
176 GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode); | |
177 if (!name) | |
178 continue; | |
179 | |
180 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name); | |
181 m_GlyphIndex[charcode] = FXFT_Get_Char_Index( | |
182 m_Font.GetFace(), m_Encoding.m_Unicodes[charcode]); | |
183 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
184 FX_CHAR name_glyph[256]; | |
185 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], name_glyph, | |
186 256); | |
187 name_glyph[255] = 0; | |
188 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
189 kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, | |
190 kCFAllocatorNull); | |
191 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
192 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
193 if (name_ct) { | |
194 CFRelease(name_ct); | |
195 } | |
196 #endif | |
197 if (m_GlyphIndex[charcode] == 0 && FXSYS_strcmp(name, ".notdef") == 0) { | |
198 m_Encoding.m_Unicodes[charcode] = 0x20; | |
199 m_GlyphIndex[charcode] = FXFT_Get_Char_Index(m_Font.GetFace(), 0x20); | |
200 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
201 FX_CHAR name_glyph[256]; | |
202 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], | |
203 name_glyph, 256); | |
204 name_glyph[255] = 0; | |
205 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
206 kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, | |
207 kCFAllocatorNull); | |
208 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
209 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
210 if (name_ct) { | |
211 CFRelease(name_ct); | |
212 } | |
213 #endif | |
214 } | |
215 } | |
216 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
217 if (!bCoreText) | |
218 FXSYS_memcpy(m_ExtGID, m_GlyphIndex, 256); | |
219 #endif | |
220 return; | |
221 } | |
222 FT_UseType1Charmap(m_Font.GetFace()); | |
223 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
224 if (bCoreText) { | |
225 if (m_Flags & PDFFONT_SYMBOLIC) { | |
226 for (int charcode = 0; charcode < 256; charcode++) { | |
227 const FX_CHAR* name = | |
228 GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode); | |
229 if (name) { | |
230 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name); | |
231 m_GlyphIndex[charcode] = | |
232 FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name); | |
233 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
234 kCFAllocatorDefault, name, kCFStringEncodingASCII, | |
235 kCFAllocatorNull); | |
236 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
237 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
238 if (name_ct) { | |
239 CFRelease(name_ct); | |
240 } | |
241 } else { | |
242 m_GlyphIndex[charcode] = | |
243 FXFT_Get_Char_Index(m_Font.GetFace(), charcode); | |
244 FX_WCHAR unicode = 0; | |
245 if (m_GlyphIndex[charcode]) { | |
246 unicode = | |
247 FT_UnicodeFromCharCode(PDFFONT_ENCODING_STANDARD, charcode); | |
248 } | |
249 FX_CHAR name_glyph[256]; | |
250 FXSYS_memset(name_glyph, 0, sizeof(name_glyph)); | |
251 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], | |
252 name_glyph, 256); | |
253 name_glyph[255] = 0; | |
254 if (unicode == 0 && name_glyph[0] != 0) { | |
255 unicode = PDF_UnicodeFromAdobeName(name_glyph); | |
256 } | |
257 m_Encoding.m_Unicodes[charcode] = unicode; | |
258 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
259 kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, | |
260 kCFAllocatorNull); | |
261 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
262 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
263 if (name_ct) { | |
264 CFRelease(name_ct); | |
265 } | |
266 } | |
267 } | |
268 return; | |
269 } | |
270 FX_BOOL bUnicode = FALSE; | |
271 if (0 == FXFT_Select_Charmap(m_Font.GetFace(), FXFT_ENCODING_UNICODE)) { | |
272 bUnicode = TRUE; | |
273 } | |
274 for (int charcode = 0; charcode < 256; charcode++) { | |
275 const FX_CHAR* name = | |
276 GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode); | |
277 if (!name) { | |
278 continue; | |
279 } | |
280 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name); | |
281 const FX_CHAR* pStrUnicode = GlyphNameRemap(name); | |
282 if (pStrUnicode && | |
283 0 == FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name)) { | |
284 name = pStrUnicode; | |
285 } | |
286 m_GlyphIndex[charcode] = | |
287 FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name); | |
288 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
289 kCFAllocatorDefault, name, kCFStringEncodingASCII, kCFAllocatorNull); | |
290 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
291 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
292 if (name_ct) { | |
293 CFRelease(name_ct); | |
294 } | |
295 if (m_GlyphIndex[charcode] == 0) { | |
296 if (FXSYS_strcmp(name, ".notdef") != 0 && | |
297 FXSYS_strcmp(name, "space") != 0) { | |
298 m_GlyphIndex[charcode] = FXFT_Get_Char_Index( | |
299 m_Font.GetFace(), | |
300 bUnicode ? m_Encoding.m_Unicodes[charcode] : charcode); | |
301 FX_CHAR name_glyph[256]; | |
302 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], | |
303 name_glyph, 256); | |
304 name_glyph[255] = 0; | |
305 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
306 kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, | |
307 kCFAllocatorNull); | |
308 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
309 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
310 if (name_ct) { | |
311 CFRelease(name_ct); | |
312 } | |
313 } else { | |
314 m_Encoding.m_Unicodes[charcode] = 0x20; | |
315 m_GlyphIndex[charcode] = | |
316 bUnicode ? FXFT_Get_Char_Index(m_Font.GetFace(), 0x20) : 0xffff; | |
317 FX_CHAR name_glyph[256]; | |
318 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], | |
319 name_glyph, 256); | |
320 name_glyph[255] = 0; | |
321 CFStringRef name_ct = CFStringCreateWithCStringNoCopy( | |
322 kCFAllocatorDefault, name_glyph, kCFStringEncodingASCII, | |
323 kCFAllocatorNull); | |
324 m_ExtGID[charcode] = CGFontGetGlyphWithGlyphName( | |
325 (CGFontRef)m_Font.GetPlatformFont(), name_ct); | |
326 if (name_ct) { | |
327 CFRelease(name_ct); | |
328 } | |
329 } | |
330 } | |
331 } | |
332 return; | |
333 } | |
334 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
335 if (m_Flags & PDFFONT_SYMBOLIC) { | |
336 for (int charcode = 0; charcode < 256; charcode++) { | |
337 const FX_CHAR* name = | |
338 GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode); | |
339 if (name) { | |
340 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name); | |
341 m_GlyphIndex[charcode] = | |
342 FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name); | |
343 } else { | |
344 m_GlyphIndex[charcode] = | |
345 FXFT_Get_Char_Index(m_Font.GetFace(), charcode); | |
346 if (m_GlyphIndex[charcode]) { | |
347 FX_WCHAR unicode = | |
348 FT_UnicodeFromCharCode(PDFFONT_ENCODING_STANDARD, charcode); | |
349 if (unicode == 0) { | |
350 FX_CHAR name_glyph[256]; | |
351 FXSYS_memset(name_glyph, 0, sizeof(name_glyph)); | |
352 FXFT_Get_Glyph_Name(m_Font.GetFace(), m_GlyphIndex[charcode], | |
353 name_glyph, 256); | |
354 name_glyph[255] = 0; | |
355 if (name_glyph[0] != 0) { | |
356 unicode = PDF_UnicodeFromAdobeName(name_glyph); | |
357 } | |
358 } | |
359 m_Encoding.m_Unicodes[charcode] = unicode; | |
360 } | |
361 } | |
362 } | |
363 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
364 if (!bCoreText) | |
365 FXSYS_memcpy(m_ExtGID, m_GlyphIndex, 256); | |
366 | |
367 #endif | |
368 return; | |
369 } | |
370 FX_BOOL bUnicode = FALSE; | |
371 if (0 == FXFT_Select_Charmap(m_Font.GetFace(), FXFT_ENCODING_UNICODE)) { | |
372 bUnicode = TRUE; | |
373 } | |
374 for (int charcode = 0; charcode < 256; charcode++) { | |
375 const FX_CHAR* name = | |
376 GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode); | |
377 if (!name) { | |
378 continue; | |
379 } | |
380 m_Encoding.m_Unicodes[charcode] = PDF_UnicodeFromAdobeName(name); | |
381 m_GlyphIndex[charcode] = FXFT_Get_Name_Index(m_Font.GetFace(), (char*)name); | |
382 if (m_GlyphIndex[charcode] == 0) { | |
383 if (FXSYS_strcmp(name, ".notdef") != 0 && | |
384 FXSYS_strcmp(name, "space") != 0) { | |
385 m_GlyphIndex[charcode] = FXFT_Get_Char_Index( | |
386 m_Font.GetFace(), | |
387 bUnicode ? m_Encoding.m_Unicodes[charcode] : charcode); | |
388 } else { | |
389 m_Encoding.m_Unicodes[charcode] = 0x20; | |
390 m_GlyphIndex[charcode] = 0xffff; | |
391 } | |
392 } | |
393 } | |
394 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
395 if (!bCoreText) | |
396 FXSYS_memcpy(m_ExtGID, m_GlyphIndex, 256); | |
397 #endif | |
398 } | |
OLD | NEW |