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

Side by Side Diff: core/fpdfapi/fpdf_font/cpdf_cidfont.cpp

Issue 2392773003: Move core/fpdfapi/fpdf_font to core/fpdfapi/font (Closed)
Patch Set: Created 4 years, 2 months 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 | « core/fpdfapi/fpdf_font/cpdf_cidfont.h ('k') | core/fpdfapi/fpdf_font/cpdf_font.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_cidfont.h"
8
9 #include <algorithm>
10 #include <vector>
11
12 #include "core/fpdfapi/cmaps/cmap_int.h"
13 #include "core/fpdfapi/cpdf_modulemgr.h"
14 #include "core/fpdfapi/fpdf_font/cpdf_fontencoding.h"
15 #include "core/fpdfapi/fpdf_font/font_int.h"
16 #include "core/fpdfapi/fpdf_font/ttgsubtable.h"
17 #include "core/fpdfapi/fpdf_page/cpdf_pagemodule.h"
18 #include "core/fpdfapi/fpdf_parser/cpdf_array.h"
19 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h"
20 #include "core/fpdfapi/fpdf_parser/cpdf_stream_acc.h"
21 #include "third_party/base/numerics/safe_math.h"
22
23 namespace {
24
25 const uint16_t g_CharsetCPs[CIDSET_NUM_SETS] = {0, 936, 950, 932, 949, 1200};
26
27 const struct CIDTransform {
28 uint16_t cid;
29 uint8_t a;
30 uint8_t b;
31 uint8_t c;
32 uint8_t d;
33 uint8_t e;
34 uint8_t f;
35 } g_Japan1_VertCIDs[] = {
36 {97, 129, 0, 0, 127, 55, 0}, {7887, 127, 0, 0, 127, 76, 89},
37 {7888, 127, 0, 0, 127, 79, 94}, {7889, 0, 129, 127, 0, 17, 127},
38 {7890, 0, 129, 127, 0, 17, 127}, {7891, 0, 129, 127, 0, 17, 127},
39 {7892, 0, 129, 127, 0, 17, 127}, {7893, 0, 129, 127, 0, 17, 127},
40 {7894, 0, 129, 127, 0, 17, 127}, {7895, 0, 129, 127, 0, 17, 127},
41 {7896, 0, 129, 127, 0, 17, 127}, {7897, 0, 129, 127, 0, 17, 127},
42 {7898, 0, 129, 127, 0, 17, 127}, {7899, 0, 129, 127, 0, 17, 104},
43 {7900, 0, 129, 127, 0, 17, 127}, {7901, 0, 129, 127, 0, 17, 104},
44 {7902, 0, 129, 127, 0, 17, 127}, {7903, 0, 129, 127, 0, 17, 127},
45 {7904, 0, 129, 127, 0, 17, 127}, {7905, 0, 129, 127, 0, 17, 114},
46 {7906, 0, 129, 127, 0, 17, 127}, {7907, 0, 129, 127, 0, 17, 127},
47 {7908, 0, 129, 127, 0, 17, 127}, {7909, 0, 129, 127, 0, 17, 127},
48 {7910, 0, 129, 127, 0, 17, 127}, {7911, 0, 129, 127, 0, 17, 127},
49 {7912, 0, 129, 127, 0, 17, 127}, {7913, 0, 129, 127, 0, 17, 127},
50 {7914, 0, 129, 127, 0, 17, 127}, {7915, 0, 129, 127, 0, 17, 114},
51 {7916, 0, 129, 127, 0, 17, 127}, {7917, 0, 129, 127, 0, 17, 127},
52 {7918, 127, 0, 0, 127, 18, 25}, {7919, 127, 0, 0, 127, 18, 25},
53 {7920, 127, 0, 0, 127, 18, 25}, {7921, 127, 0, 0, 127, 18, 25},
54 {7922, 127, 0, 0, 127, 18, 25}, {7923, 127, 0, 0, 127, 18, 25},
55 {7924, 127, 0, 0, 127, 18, 25}, {7925, 127, 0, 0, 127, 18, 25},
56 {7926, 127, 0, 0, 127, 18, 25}, {7927, 127, 0, 0, 127, 18, 25},
57 {7928, 127, 0, 0, 127, 18, 25}, {7929, 127, 0, 0, 127, 18, 25},
58 {7930, 127, 0, 0, 127, 18, 25}, {7931, 127, 0, 0, 127, 18, 25},
59 {7932, 127, 0, 0, 127, 18, 25}, {7933, 127, 0, 0, 127, 18, 25},
60 {7934, 127, 0, 0, 127, 18, 25}, {7935, 127, 0, 0, 127, 18, 25},
61 {7936, 127, 0, 0, 127, 18, 25}, {7937, 127, 0, 0, 127, 18, 25},
62 {7938, 127, 0, 0, 127, 18, 25}, {7939, 127, 0, 0, 127, 18, 25},
63 {8720, 0, 129, 127, 0, 19, 102}, {8721, 0, 129, 127, 0, 13, 127},
64 {8722, 0, 129, 127, 0, 19, 108}, {8723, 0, 129, 127, 0, 19, 102},
65 {8724, 0, 129, 127, 0, 19, 102}, {8725, 0, 129, 127, 0, 19, 102},
66 {8726, 0, 129, 127, 0, 19, 102}, {8727, 0, 129, 127, 0, 19, 102},
67 {8728, 0, 129, 127, 0, 19, 114}, {8729, 0, 129, 127, 0, 19, 114},
68 {8730, 0, 129, 127, 0, 38, 108}, {8731, 0, 129, 127, 0, 13, 108},
69 {8732, 0, 129, 127, 0, 19, 108}, {8733, 0, 129, 127, 0, 19, 108},
70 {8734, 0, 129, 127, 0, 19, 108}, {8735, 0, 129, 127, 0, 19, 108},
71 {8736, 0, 129, 127, 0, 19, 102}, {8737, 0, 129, 127, 0, 19, 102},
72 {8738, 0, 129, 127, 0, 19, 102}, {8739, 0, 129, 127, 0, 19, 102},
73 {8740, 0, 129, 127, 0, 19, 102}, {8741, 0, 129, 127, 0, 19, 102},
74 {8742, 0, 129, 127, 0, 19, 102}, {8743, 0, 129, 127, 0, 19, 102},
75 {8744, 0, 129, 127, 0, 19, 102}, {8745, 0, 129, 127, 0, 19, 102},
76 {8746, 0, 129, 127, 0, 19, 114}, {8747, 0, 129, 127, 0, 19, 114},
77 {8748, 0, 129, 127, 0, 19, 102}, {8749, 0, 129, 127, 0, 19, 102},
78 {8750, 0, 129, 127, 0, 19, 102}, {8751, 0, 129, 127, 0, 19, 102},
79 {8752, 0, 129, 127, 0, 19, 102}, {8753, 0, 129, 127, 0, 19, 102},
80 {8754, 0, 129, 127, 0, 19, 102}, {8755, 0, 129, 127, 0, 19, 102},
81 {8756, 0, 129, 127, 0, 19, 102}, {8757, 0, 129, 127, 0, 19, 102},
82 {8758, 0, 129, 127, 0, 19, 102}, {8759, 0, 129, 127, 0, 19, 102},
83 {8760, 0, 129, 127, 0, 19, 102}, {8761, 0, 129, 127, 0, 19, 102},
84 {8762, 0, 129, 127, 0, 19, 102}, {8763, 0, 129, 127, 0, 19, 102},
85 {8764, 0, 129, 127, 0, 19, 102}, {8765, 0, 129, 127, 0, 19, 102},
86 {8766, 0, 129, 127, 0, 19, 102}, {8767, 0, 129, 127, 0, 19, 102},
87 {8768, 0, 129, 127, 0, 19, 102}, {8769, 0, 129, 127, 0, 19, 102},
88 {8770, 0, 129, 127, 0, 19, 102}, {8771, 0, 129, 127, 0, 19, 102},
89 {8772, 0, 129, 127, 0, 19, 102}, {8773, 0, 129, 127, 0, 19, 102},
90 {8774, 0, 129, 127, 0, 19, 102}, {8775, 0, 129, 127, 0, 19, 102},
91 {8776, 0, 129, 127, 0, 19, 102}, {8777, 0, 129, 127, 0, 19, 102},
92 {8778, 0, 129, 127, 0, 19, 102}, {8779, 0, 129, 127, 0, 19, 114},
93 {8780, 0, 129, 127, 0, 19, 108}, {8781, 0, 129, 127, 0, 19, 114},
94 {8782, 0, 129, 127, 0, 13, 114}, {8783, 0, 129, 127, 0, 19, 108},
95 {8784, 0, 129, 127, 0, 13, 114}, {8785, 0, 129, 127, 0, 19, 108},
96 {8786, 0, 129, 127, 0, 19, 108}, {8787, 0, 129, 127, 0, 19, 108},
97 {8788, 0, 129, 127, 0, 19, 108}, {8789, 0, 129, 127, 0, 19, 108},
98 {8790, 0, 129, 127, 0, 19, 108}, {8791, 0, 129, 127, 0, 19, 108},
99 {8792, 0, 129, 127, 0, 19, 108}, {8793, 0, 129, 127, 0, 19, 108},
100 {8794, 0, 129, 127, 0, 19, 108}, {8795, 0, 129, 127, 0, 19, 108},
101 {8796, 0, 129, 127, 0, 19, 108}, {8797, 0, 129, 127, 0, 19, 108},
102 {8798, 0, 129, 127, 0, 19, 108}, {8799, 0, 129, 127, 0, 19, 108},
103 {8800, 0, 129, 127, 0, 19, 108}, {8801, 0, 129, 127, 0, 19, 108},
104 {8802, 0, 129, 127, 0, 19, 108}, {8803, 0, 129, 127, 0, 19, 108},
105 {8804, 0, 129, 127, 0, 19, 108}, {8805, 0, 129, 127, 0, 19, 108},
106 {8806, 0, 129, 127, 0, 19, 108}, {8807, 0, 129, 127, 0, 19, 108},
107 {8808, 0, 129, 127, 0, 19, 108}, {8809, 0, 129, 127, 0, 19, 108},
108 {8810, 0, 129, 127, 0, 19, 108}, {8811, 0, 129, 127, 0, 19, 114},
109 {8812, 0, 129, 127, 0, 19, 102}, {8813, 0, 129, 127, 0, 19, 114},
110 {8814, 0, 129, 127, 0, 76, 102}, {8815, 0, 129, 127, 0, 13, 121},
111 {8816, 0, 129, 127, 0, 19, 114}, {8817, 0, 129, 127, 0, 19, 127},
112 {8818, 0, 129, 127, 0, 19, 114}, {8819, 0, 129, 127, 0, 218, 108},
113 };
114
115 CPDF_FontGlobals* GetFontGlobals() {
116 return CPDF_ModuleMgr::Get()->GetPageModule()->GetFontGlobals();
117 }
118
119 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
120
121 bool IsValidEmbeddedCharcodeFromUnicodeCharset(CIDSet charset) {
122 switch (charset) {
123 case CIDSET_GB1:
124 case CIDSET_CNS1:
125 case CIDSET_JAPAN1:
126 case CIDSET_KOREA1:
127 return true;
128
129 default:
130 return false;
131 }
132 }
133
134 FX_WCHAR EmbeddedUnicodeFromCharcode(const FXCMAP_CMap* pEmbedMap,
135 CIDSet charset,
136 uint32_t charcode) {
137 if (!IsValidEmbeddedCharcodeFromUnicodeCharset(charset))
138 return 0;
139
140 uint16_t cid = FPDFAPI_CIDFromCharCode(pEmbedMap, charcode);
141 const auto& codes = GetFontGlobals()->m_EmbeddedToUnicodes[charset];
142 if (codes.m_pMap && cid && cid < codes.m_Count)
143 return codes.m_pMap[cid];
144 return 0;
145 }
146
147 uint32_t EmbeddedCharcodeFromUnicode(const FXCMAP_CMap* pEmbedMap,
148 CIDSet charset,
149 FX_WCHAR unicode) {
150 if (!IsValidEmbeddedCharcodeFromUnicodeCharset(charset))
151 return 0;
152
153 const auto& codes = GetFontGlobals()->m_EmbeddedToUnicodes[charset];
154 const uint16_t* pCodes = codes.m_pMap;
155 if (!pCodes)
156 return 0;
157
158 for (uint32_t i = 0; i < codes.m_Count; ++i) {
159 if (pCodes[i] == unicode) {
160 uint32_t CharCode = FPDFAPI_CharCodeFromCID(pEmbedMap, i);
161 if (CharCode)
162 return CharCode;
163 }
164 }
165 return 0;
166 }
167
168 #endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
169
170 void FT_UseCIDCharmap(FXFT_Face face, int coding) {
171 int encoding;
172 switch (coding) {
173 case CIDCODING_GB:
174 encoding = FXFT_ENCODING_GB2312;
175 break;
176 case CIDCODING_BIG5:
177 encoding = FXFT_ENCODING_BIG5;
178 break;
179 case CIDCODING_JIS:
180 encoding = FXFT_ENCODING_SJIS;
181 break;
182 case CIDCODING_KOREA:
183 encoding = FXFT_ENCODING_JOHAB;
184 break;
185 default:
186 encoding = FXFT_ENCODING_UNICODE;
187 }
188 int err = FXFT_Select_Charmap(face, encoding);
189 if (err)
190 err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE);
191 if (err && FXFT_Get_Face_Charmaps(face))
192 FXFT_Set_Charmap(face, *FXFT_Get_Face_Charmaps(face));
193 }
194
195 bool IsMetricForCID(const uint32_t* pEntry, uint16_t CID) {
196 return pEntry[0] <= CID && pEntry[1] >= CID;
197 }
198
199 } // namespace
200
201 CPDF_CIDFont::CPDF_CIDFont()
202 : m_pCMap(nullptr),
203 m_pCID2UnicodeMap(nullptr),
204 m_bCIDIsGID(false),
205 m_bAnsiWidthsFixed(false),
206 m_bAdobeCourierStd(false) {
207 for (size_t i = 0; i < FX_ArraySize(m_CharBBox); ++i)
208 m_CharBBox[i] = FX_RECT(-1, -1, -1, -1);
209 }
210
211 CPDF_CIDFont::~CPDF_CIDFont() {}
212
213 bool CPDF_CIDFont::IsCIDFont() const {
214 return true;
215 }
216
217 const CPDF_CIDFont* CPDF_CIDFont::AsCIDFont() const {
218 return this;
219 }
220
221 CPDF_CIDFont* CPDF_CIDFont::AsCIDFont() {
222 return this;
223 }
224
225 uint16_t CPDF_CIDFont::CIDFromCharCode(uint32_t charcode) const {
226 return m_pCMap ? m_pCMap->CIDFromCharCode(charcode)
227 : static_cast<uint16_t>(charcode);
228 }
229
230 bool CPDF_CIDFont::IsVertWriting() const {
231 return m_pCMap && m_pCMap->IsVertWriting();
232 }
233
234 CFX_WideString CPDF_CIDFont::UnicodeFromCharCode(uint32_t charcode) const {
235 CFX_WideString str = CPDF_Font::UnicodeFromCharCode(charcode);
236 if (!str.IsEmpty())
237 return str;
238 FX_WCHAR ret = GetUnicodeFromCharCode(charcode);
239 return ret ? ret : CFX_WideString();
240 }
241
242 FX_WCHAR CPDF_CIDFont::GetUnicodeFromCharCode(uint32_t charcode) const {
243 switch (m_pCMap->m_Coding) {
244 case CIDCODING_UCS2:
245 case CIDCODING_UTF16:
246 return static_cast<FX_WCHAR>(charcode);
247 case CIDCODING_CID:
248 if (!m_pCID2UnicodeMap || !m_pCID2UnicodeMap->IsLoaded())
249 return 0;
250 return m_pCID2UnicodeMap->UnicodeFromCID(static_cast<uint16_t>(charcode));
251 }
252 if (m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded() && m_pCMap->IsLoaded())
253 return m_pCID2UnicodeMap->UnicodeFromCID(CIDFromCharCode(charcode));
254
255 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
256 FX_WCHAR unicode;
257 int charsize = 1;
258 if (charcode > 255) {
259 charcode = (charcode % 256) * 256 + (charcode / 256);
260 charsize = 2;
261 }
262 int ret = FXSYS_MultiByteToWideChar(
263 g_CharsetCPs[m_pCMap->m_Coding], 0,
264 reinterpret_cast<const FX_CHAR*>(&charcode), charsize, &unicode, 1);
265 return ret == 1 ? unicode : 0;
266 #else
267 if (!m_pCMap->m_pEmbedMap)
268 return 0;
269 return EmbeddedUnicodeFromCharcode(m_pCMap->m_pEmbedMap, m_pCMap->m_Charset,
270 charcode);
271 #endif
272 }
273
274 uint32_t CPDF_CIDFont::CharCodeFromUnicode(FX_WCHAR unicode) const {
275 uint32_t charcode = CPDF_Font::CharCodeFromUnicode(unicode);
276 if (charcode)
277 return charcode;
278 switch (m_pCMap->m_Coding) {
279 case CIDCODING_UNKNOWN:
280 return 0;
281 case CIDCODING_UCS2:
282 case CIDCODING_UTF16:
283 return unicode;
284 case CIDCODING_CID: {
285 if (!m_pCID2UnicodeMap || !m_pCID2UnicodeMap->IsLoaded())
286 return 0;
287 uint32_t CID = 0;
288 while (CID < 65536) {
289 FX_WCHAR this_unicode =
290 m_pCID2UnicodeMap->UnicodeFromCID(static_cast<uint16_t>(CID));
291 if (this_unicode == unicode)
292 return CID;
293 CID++;
294 }
295 break;
296 }
297 }
298
299 if (unicode < 0x80)
300 return static_cast<uint32_t>(unicode);
301 if (m_pCMap->m_Coding == CIDCODING_CID)
302 return 0;
303 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
304 uint8_t buffer[32];
305 int ret = FXSYS_WideCharToMultiByte(
306 g_CharsetCPs[m_pCMap->m_Coding], 0, &unicode, 1,
307 reinterpret_cast<char*>(buffer), 4, nullptr, nullptr);
308 if (ret == 1)
309 return buffer[0];
310 if (ret == 2)
311 return buffer[0] * 256 + buffer[1];
312 #else
313 if (m_pCMap->m_pEmbedMap) {
314 return EmbeddedCharcodeFromUnicode(m_pCMap->m_pEmbedMap, m_pCMap->m_Charset,
315 unicode);
316 }
317 #endif
318 return 0;
319 }
320
321 bool CPDF_CIDFont::Load() {
322 if (m_pFontDict->GetStringFor("Subtype") == "TrueType") {
323 LoadGB2312();
324 return true;
325 }
326
327 CPDF_Array* pFonts = m_pFontDict->GetArrayFor("DescendantFonts");
328 if (!pFonts || pFonts->GetCount() != 1)
329 return false;
330
331 CPDF_Dictionary* pCIDFontDict = pFonts->GetDictAt(0);
332 if (!pCIDFontDict)
333 return false;
334
335 m_BaseFont = pCIDFontDict->GetStringFor("BaseFont");
336 if ((m_BaseFont.Compare("CourierStd") == 0 ||
337 m_BaseFont.Compare("CourierStd-Bold") == 0 ||
338 m_BaseFont.Compare("CourierStd-BoldOblique") == 0 ||
339 m_BaseFont.Compare("CourierStd-Oblique") == 0) &&
340 !IsEmbedded()) {
341 m_bAdobeCourierStd = true;
342 }
343 CPDF_Dictionary* pFontDesc = pCIDFontDict->GetDictFor("FontDescriptor");
344 if (pFontDesc)
345 LoadFontDescriptor(pFontDesc);
346
347 CPDF_Object* pEncoding = m_pFontDict->GetDirectObjectFor("Encoding");
348 if (!pEncoding)
349 return false;
350
351 CFX_ByteString subtype = pCIDFontDict->GetStringFor("Subtype");
352 m_bType1 = (subtype == "CIDFontType0");
353
354 CPDF_CMapManager& manager = GetFontGlobals()->m_CMapManager;
355 if (pEncoding->IsName()) {
356 CFX_ByteString cmap = pEncoding->GetString();
357 bool bPromptCJK = m_pFontFile && m_bType1;
358 m_pCMap = manager.GetPredefinedCMap(cmap, bPromptCJK);
359 if (!m_pCMap)
360 return false;
361 } else if (CPDF_Stream* pStream = pEncoding->AsStream()) {
362 m_pCMap = new CPDF_CMap;
363 m_pAllocatedCMap.reset(m_pCMap);
364 CPDF_StreamAcc acc;
365 acc.LoadAllData(pStream, FALSE);
366 m_pCMap->LoadEmbedded(acc.GetData(), acc.GetSize());
367 } else {
368 return false;
369 }
370
371 m_Charset = m_pCMap->m_Charset;
372 if (m_Charset == CIDSET_UNKNOWN) {
373 CPDF_Dictionary* pCIDInfo = pCIDFontDict->GetDictFor("CIDSystemInfo");
374 if (pCIDInfo) {
375 m_Charset =
376 CharsetFromOrdering(pCIDInfo->GetStringFor("Ordering").AsStringC());
377 }
378 }
379 if (m_Charset != CIDSET_UNKNOWN) {
380 bool bPromptCJK = !m_pFontFile && (m_pCMap->m_Coding == CIDCODING_CID ||
381 pCIDFontDict->KeyExist("W"));
382 m_pCID2UnicodeMap = manager.GetCID2UnicodeMap(m_Charset, bPromptCJK);
383 }
384 if (m_Font.GetFace()) {
385 if (m_bType1)
386 FXFT_Select_Charmap(m_Font.GetFace(), FXFT_ENCODING_UNICODE);
387 else
388 FT_UseCIDCharmap(m_Font.GetFace(), m_pCMap->m_Coding);
389 }
390 m_DefaultWidth = pCIDFontDict->GetIntegerFor("DW", 1000);
391 CPDF_Array* pWidthArray = pCIDFontDict->GetArrayFor("W");
392 if (pWidthArray)
393 LoadMetricsArray(pWidthArray, m_WidthList, 1);
394 if (!IsEmbedded())
395 LoadSubstFont();
396
397 if (m_pFontFile || (GetSubstFont()->m_SubstFlags & FXFONT_SUBST_EXACT)) {
398 CPDF_Object* pmap = pCIDFontDict->GetDirectObjectFor("CIDToGIDMap");
399 if (pmap) {
400 if (CPDF_Stream* pStream = pmap->AsStream()) {
401 m_pStreamAcc.reset(new CPDF_StreamAcc);
402 m_pStreamAcc->LoadAllData(pStream, FALSE);
403 } else if (pmap->GetString() == "Identity") {
404 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
405 if (m_pFontFile)
406 m_bCIDIsGID = true;
407 #else
408 m_bCIDIsGID = true;
409 #endif
410 }
411 }
412 }
413
414 CheckFontMetrics();
415 if (IsVertWriting()) {
416 pWidthArray = pCIDFontDict->GetArrayFor("W2");
417 if (pWidthArray)
418 LoadMetricsArray(pWidthArray, m_VertMetrics, 3);
419 CPDF_Array* pDefaultArray = pCIDFontDict->GetArrayFor("DW2");
420 if (pDefaultArray) {
421 m_DefaultVY = pDefaultArray->GetIntegerAt(0);
422 m_DefaultW1 = pDefaultArray->GetIntegerAt(1);
423 } else {
424 m_DefaultVY = 880;
425 m_DefaultW1 = -1000;
426 }
427 }
428 return true;
429 }
430
431 FX_RECT CPDF_CIDFont::GetCharBBox(uint32_t charcode) {
432 if (charcode < 256 && m_CharBBox[charcode].right != -1)
433 return m_CharBBox[charcode];
434
435 FX_RECT rect;
436 bool bVert = false;
437 int glyph_index = GlyphFromCharCode(charcode, &bVert);
438 FXFT_Face face = m_Font.GetFace();
439 if (face) {
440 if (FXFT_Is_Face_Tricky(face)) {
441 int err = FXFT_Load_Glyph(face, glyph_index,
442 FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
443 if (!err) {
444 FXFT_BBox cbox;
445 FXFT_Glyph glyph;
446 err = FXFT_Get_Glyph(((FXFT_Face)face)->glyph, &glyph);
447 if (!err) {
448 FXFT_Glyph_Get_CBox(glyph, FXFT_GLYPH_BBOX_PIXELS, &cbox);
449 int pixel_size_x = ((FXFT_Face)face)->size->metrics.x_ppem;
450 int pixel_size_y = ((FXFT_Face)face)->size->metrics.y_ppem;
451 if (pixel_size_x == 0 || pixel_size_y == 0) {
452 rect = FX_RECT(cbox.xMin, cbox.yMax, cbox.xMax, cbox.yMin);
453 } else {
454 rect = FX_RECT(cbox.xMin * 1000 / pixel_size_x,
455 cbox.yMax * 1000 / pixel_size_y,
456 cbox.xMax * 1000 / pixel_size_x,
457 cbox.yMin * 1000 / pixel_size_y);
458 }
459 rect.top = std::min(rect.top,
460 static_cast<int>(FXFT_Get_Face_Ascender(face)));
461 rect.bottom = std::max(
462 rect.bottom, static_cast<int>(FXFT_Get_Face_Descender(face)));
463 FXFT_Done_Glyph(glyph);
464 }
465 }
466 } else {
467 int err = FXFT_Load_Glyph(face, glyph_index, FXFT_LOAD_NO_SCALE);
468 if (err == 0) {
469 rect = FX_RECT(TT2PDF(FXFT_Get_Glyph_HoriBearingX(face), face),
470 TT2PDF(FXFT_Get_Glyph_HoriBearingY(face), face),
471 TT2PDF(FXFT_Get_Glyph_HoriBearingX(face) +
472 FXFT_Get_Glyph_Width(face),
473 face),
474 TT2PDF(FXFT_Get_Glyph_HoriBearingY(face) -
475 FXFT_Get_Glyph_Height(face),
476 face));
477 rect.top += rect.top / 64;
478 }
479 }
480 }
481 if (!m_pFontFile && m_Charset == CIDSET_JAPAN1) {
482 uint16_t CID = CIDFromCharCode(charcode);
483 const uint8_t* pTransform = GetCIDTransform(CID);
484 if (pTransform && !bVert) {
485 CFX_Matrix matrix(CIDTransformToFloat(pTransform[0]),
486 CIDTransformToFloat(pTransform[1]),
487 CIDTransformToFloat(pTransform[2]),
488 CIDTransformToFloat(pTransform[3]),
489 CIDTransformToFloat(pTransform[4]) * 1000,
490 CIDTransformToFloat(pTransform[5]) * 1000);
491 CFX_FloatRect rect_f(rect);
492 rect_f.Transform(&matrix);
493 rect = rect_f.GetOuterRect();
494 }
495 }
496 if (charcode < 256)
497 m_CharBBox[charcode] = rect;
498
499 return rect;
500 }
501
502 int CPDF_CIDFont::GetCharWidthF(uint32_t charcode) {
503 if (charcode < 0x80 && m_bAnsiWidthsFixed)
504 return (charcode >= 32 && charcode < 127) ? 500 : 0;
505
506 uint16_t cid = CIDFromCharCode(charcode);
507 int size = m_WidthList.GetSize();
508 const uint32_t* pList = m_WidthList.GetData();
509 for (int i = 0; i < size; i += 3) {
510 const uint32_t* pEntry = pList + i;
511 if (IsMetricForCID(pEntry, cid))
512 return static_cast<int>(pEntry[2]);
513 }
514 return m_DefaultWidth;
515 }
516
517 short CPDF_CIDFont::GetVertWidth(uint16_t CID) const {
518 uint32_t vertsize = m_VertMetrics.GetSize() / 5;
519 if (vertsize) {
520 const uint32_t* pTable = m_VertMetrics.GetData();
521 for (uint32_t i = 0; i < vertsize; i++) {
522 const uint32_t* pEntry = pTable + (i * 5);
523 if (IsMetricForCID(pEntry, CID))
524 return static_cast<short>(pEntry[2]);
525 }
526 }
527 return m_DefaultW1;
528 }
529
530 void CPDF_CIDFont::GetVertOrigin(uint16_t CID, short& vx, short& vy) const {
531 uint32_t vertsize = m_VertMetrics.GetSize() / 5;
532 if (vertsize) {
533 const uint32_t* pTable = m_VertMetrics.GetData();
534 for (uint32_t i = 0; i < vertsize; i++) {
535 const uint32_t* pEntry = pTable + (i * 5);
536 if (IsMetricForCID(pEntry, CID)) {
537 vx = static_cast<short>(pEntry[3]);
538 vy = static_cast<short>(pEntry[4]);
539 return;
540 }
541 }
542 }
543 uint32_t dwWidth = m_DefaultWidth;
544 int size = m_WidthList.GetSize();
545 const uint32_t* pList = m_WidthList.GetData();
546 for (int i = 0; i < size; i += 3) {
547 const uint32_t* pEntry = pList + i;
548 if (IsMetricForCID(pEntry, CID)) {
549 dwWidth = pEntry[2];
550 break;
551 }
552 }
553 vx = static_cast<short>(dwWidth) / 2;
554 vy = m_DefaultVY;
555 }
556
557 int CPDF_CIDFont::GetGlyphIndex(uint32_t unicode, bool* pVertGlyph) {
558 if (pVertGlyph)
559 *pVertGlyph = false;
560
561 FXFT_Face face = m_Font.GetFace();
562 int index = FXFT_Get_Char_Index(face, unicode);
563 if (unicode == 0x2502)
564 return index;
565
566 if (!index || !IsVertWriting())
567 return index;
568
569 if (m_pTTGSUBTable)
570 return GetVerticalGlyph(index, pVertGlyph);
571
572 if (!m_Font.GetSubData()) {
573 unsigned long length = 0;
574 int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0,
575 nullptr, &length);
576 if (!error)
577 m_Font.SetSubData(FX_Alloc(uint8_t, length));
578 }
579 int error = FXFT_Load_Sfnt_Table(face, FT_MAKE_TAG('G', 'S', 'U', 'B'), 0,
580 m_Font.GetSubData(), nullptr);
581 if (error || !m_Font.GetSubData())
582 return index;
583
584 m_pTTGSUBTable.reset(new CFX_CTTGSUBTable);
585 m_pTTGSUBTable->LoadGSUBTable((FT_Bytes)m_Font.GetSubData());
586 return GetVerticalGlyph(index, pVertGlyph);
587 }
588
589 int CPDF_CIDFont::GetVerticalGlyph(int index, bool* pVertGlyph) {
590 uint32_t vindex = 0;
591 m_pTTGSUBTable->GetVerticalGlyph(index, &vindex);
592 if (!vindex)
593 return index;
594
595 index = vindex;
596 if (pVertGlyph)
597 *pVertGlyph = true;
598 return index;
599 }
600
601 int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) {
602 if (pVertGlyph)
603 *pVertGlyph = FALSE;
604
605 if (!m_pFontFile && !m_pStreamAcc) {
606 uint16_t cid = CIDFromCharCode(charcode);
607 FX_WCHAR unicode = 0;
608 if (m_bCIDIsGID) {
609 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
610 return cid;
611 #else
612 if (m_Flags & PDFFONT_SYMBOLIC)
613 return cid;
614
615 CFX_WideString uni_str = UnicodeFromCharCode(charcode);
616 if (uni_str.IsEmpty())
617 return cid;
618
619 unicode = uni_str.GetAt(0);
620 #endif
621 } else {
622 if (cid && m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded())
623 unicode = m_pCID2UnicodeMap->UnicodeFromCID(cid);
624 if (unicode == 0)
625 unicode = GetUnicodeFromCharCode(charcode);
626 if (unicode == 0) {
627 CFX_WideString unicode_str = UnicodeFromCharCode(charcode);
628 if (!unicode_str.IsEmpty())
629 unicode = unicode_str.GetAt(0);
630 }
631 }
632 FXFT_Face face = m_Font.GetFace();
633 if (unicode == 0) {
634 if (!m_bAdobeCourierStd)
635 return charcode ? static_cast<int>(charcode) : -1;
636
637 charcode += 31;
638 bool bMSUnicode = FT_UseTTCharmap(face, 3, 1);
639 bool bMacRoman = !bMSUnicode && FT_UseTTCharmap(face, 1, 0);
640 int iBaseEncoding = PDFFONT_ENCODING_STANDARD;
641 if (bMSUnicode)
642 iBaseEncoding = PDFFONT_ENCODING_WINANSI;
643 else if (bMacRoman)
644 iBaseEncoding = PDFFONT_ENCODING_MACROMAN;
645 const FX_CHAR* name = GetAdobeCharName(
646 iBaseEncoding, std::vector<CFX_ByteString>(), charcode);
647 if (!name)
648 return charcode ? static_cast<int>(charcode) : -1;
649
650 int index = 0;
651 uint16_t name_unicode = PDF_UnicodeFromAdobeName(name);
652 if (!name_unicode)
653 return charcode ? static_cast<int>(charcode) : -1;
654
655 if (iBaseEncoding == PDFFONT_ENCODING_STANDARD)
656 return FXFT_Get_Char_Index(face, name_unicode);
657
658 if (iBaseEncoding == PDFFONT_ENCODING_WINANSI) {
659 index = FXFT_Get_Char_Index(face, name_unicode);
660 } else {
661 ASSERT(iBaseEncoding == PDFFONT_ENCODING_MACROMAN);
662 uint32_t maccode =
663 FT_CharCodeFromUnicode(FXFT_ENCODING_APPLE_ROMAN, name_unicode);
664 index = maccode ? FXFT_Get_Char_Index(face, maccode)
665 : FXFT_Get_Name_Index(face, const_cast<char*>(name));
666 }
667 if (index == 0 || index == 0xffff)
668 return charcode ? static_cast<int>(charcode) : -1;
669 return index;
670 }
671 if (m_Charset == CIDSET_JAPAN1) {
672 if (unicode == '\\') {
673 unicode = '/';
674 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
675 } else if (unicode == 0xa5) {
676 unicode = 0x5c;
677 #endif
678 }
679 }
680 if (!face)
681 return unicode;
682
683 int err = FXFT_Select_Charmap(face, FXFT_ENCODING_UNICODE);
684 if (err) {
685 int i;
686 for (i = 0; i < FXFT_Get_Face_CharmapCount(face); i++) {
687 uint32_t ret = FT_CharCodeFromUnicode(
688 FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmaps(face)[i]),
689 static_cast<FX_WCHAR>(charcode));
690 if (ret == 0)
691 continue;
692 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[i]);
693 unicode = static_cast<FX_WCHAR>(ret);
694 break;
695 }
696 if (i == FXFT_Get_Face_CharmapCount(face) && i) {
697 FXFT_Set_Charmap(face, FXFT_Get_Face_Charmaps(face)[0]);
698 unicode = static_cast<FX_WCHAR>(charcode);
699 }
700 }
701 if (FXFT_Get_Face_Charmap(face)) {
702 int index = GetGlyphIndex(unicode, pVertGlyph);
703 return index != 0 ? index : -1;
704 }
705 return unicode;
706 }
707
708 if (!m_Font.GetFace())
709 return -1;
710
711 uint16_t cid = CIDFromCharCode(charcode);
712 if (!m_pStreamAcc) {
713 if (m_bType1)
714 return cid;
715
716 if (m_pFontFile && !m_pCMap->m_pMapping)
717 return cid;
718 if (m_pCMap->m_Coding == CIDCODING_UNKNOWN ||
719 !FXFT_Get_Face_Charmap(m_Font.GetFace())) {
720 return cid;
721 }
722 if (FXFT_Get_Charmap_Encoding(FXFT_Get_Face_Charmap(m_Font.GetFace())) ==
723 FXFT_ENCODING_UNICODE) {
724 CFX_WideString unicode_str = UnicodeFromCharCode(charcode);
725 if (unicode_str.IsEmpty())
726 return -1;
727
728 charcode = unicode_str.GetAt(0);
729 }
730 return GetGlyphIndex(charcode, pVertGlyph);
731 }
732 uint32_t byte_pos = cid * 2;
733 if (byte_pos + 2 > m_pStreamAcc->GetSize())
734 return -1;
735
736 const uint8_t* pdata = m_pStreamAcc->GetData() + byte_pos;
737 return pdata[0] * 256 + pdata[1];
738 }
739
740 uint32_t CPDF_CIDFont::GetNextChar(const FX_CHAR* pString,
741 int nStrLen,
742 int& offset) const {
743 return m_pCMap->GetNextChar(pString, nStrLen, offset);
744 }
745
746 int CPDF_CIDFont::GetCharSize(uint32_t charcode) const {
747 return m_pCMap->GetCharSize(charcode);
748 }
749
750 int CPDF_CIDFont::CountChar(const FX_CHAR* pString, int size) const {
751 return m_pCMap->CountChar(pString, size);
752 }
753
754 int CPDF_CIDFont::AppendChar(FX_CHAR* str, uint32_t charcode) const {
755 return m_pCMap->AppendChar(str, charcode);
756 }
757
758 bool CPDF_CIDFont::IsUnicodeCompatible() const {
759 if (m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded() && m_pCMap->IsLoaded())
760 return true;
761 return m_pCMap->m_Coding != CIDCODING_UNKNOWN;
762 }
763
764 void CPDF_CIDFont::LoadSubstFont() {
765 pdfium::base::CheckedNumeric<int> safeStemV(m_StemV);
766 safeStemV *= 5;
767 m_Font.LoadSubst(m_BaseFont, !m_bType1, m_Flags,
768 safeStemV.ValueOrDefault(FXFONT_FW_NORMAL), m_ItalicAngle,
769 g_CharsetCPs[m_Charset], IsVertWriting());
770 }
771
772 void CPDF_CIDFont::LoadMetricsArray(CPDF_Array* pArray,
773 CFX_ArrayTemplate<uint32_t>& result,
774 int nElements) {
775 int width_status = 0;
776 int iCurElement = 0;
777 int first_code = 0;
778 int last_code = 0;
779 for (size_t i = 0; i < pArray->GetCount(); i++) {
780 CPDF_Object* pObj = pArray->GetDirectObjectAt(i);
781 if (!pObj)
782 continue;
783
784 if (CPDF_Array* pObjArray = pObj->AsArray()) {
785 if (width_status != 1)
786 return;
787
788 for (size_t j = 0; j < pObjArray->GetCount(); j += nElements) {
789 result.Add(first_code);
790 result.Add(first_code);
791 for (int k = 0; k < nElements; k++)
792 result.Add(pObjArray->GetIntegerAt(j + k));
793 first_code++;
794 }
795 width_status = 0;
796 } else {
797 if (width_status == 0) {
798 first_code = pObj->GetInteger();
799 width_status = 1;
800 } else if (width_status == 1) {
801 last_code = pObj->GetInteger();
802 width_status = 2;
803 iCurElement = 0;
804 } else {
805 if (!iCurElement) {
806 result.Add(first_code);
807 result.Add(last_code);
808 }
809 result.Add(pObj->GetInteger());
810 iCurElement++;
811 if (iCurElement == nElements)
812 width_status = 0;
813 }
814 }
815 }
816 }
817
818 // static
819 FX_FLOAT CPDF_CIDFont::CIDTransformToFloat(uint8_t ch) {
820 return (ch < 128 ? ch : ch - 255) * (1.0f / 127);
821 }
822
823 void CPDF_CIDFont::LoadGB2312() {
824 m_BaseFont = m_pFontDict->GetStringFor("BaseFont");
825 CPDF_Dictionary* pFontDesc = m_pFontDict->GetDictFor("FontDescriptor");
826 if (pFontDesc)
827 LoadFontDescriptor(pFontDesc);
828
829 m_Charset = CIDSET_GB1;
830 m_bType1 = false;
831 CPDF_CMapManager& manager = GetFontGlobals()->m_CMapManager;
832 m_pCMap = manager.GetPredefinedCMap("GBK-EUC-H", false);
833 m_pCID2UnicodeMap = manager.GetCID2UnicodeMap(m_Charset, false);
834 if (!IsEmbedded())
835 LoadSubstFont();
836
837 CheckFontMetrics();
838 m_DefaultWidth = 1000;
839 m_bAnsiWidthsFixed = true;
840 }
841
842 const uint8_t* CPDF_CIDFont::GetCIDTransform(uint16_t CID) const {
843 if (m_Charset != CIDSET_JAPAN1 || m_pFontFile)
844 return nullptr;
845
846 const auto* pEnd = g_Japan1_VertCIDs + FX_ArraySize(g_Japan1_VertCIDs);
847 const auto* pTransform = std::lower_bound(
848 g_Japan1_VertCIDs, pEnd, CID,
849 [](const CIDTransform& entry, uint16_t cid) { return entry.cid < cid; });
850 return (pTransform < pEnd && CID == pTransform->cid) ? &pTransform->a
851 : nullptr;
852 }
OLDNEW
« no previous file with comments | « core/fpdfapi/fpdf_font/cpdf_cidfont.h ('k') | core/fpdfapi/fpdf_font/cpdf_font.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698