OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 #include "pdf/pdfium/pdfium_engine.h" | 5 #include "pdf/pdfium/pdfium_engine.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 page_numbers.push_back(page_number); | 124 page_numbers.push_back(page_number); |
125 } | 125 } |
126 } | 126 } |
127 return page_numbers; | 127 return page_numbers; |
128 } | 128 } |
129 | 129 |
130 #if defined(OS_LINUX) | 130 #if defined(OS_LINUX) |
131 | 131 |
132 PP_Instance g_last_instance_id; | 132 PP_Instance g_last_instance_id; |
133 | 133 |
134 // TODO(npm): Move font stuff to another file to reduce the size of this one | |
134 PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) { | 135 PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) { |
135 static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0, | 136 static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0, |
136 "PP_BrowserFont_Trusted_Weight min"); | 137 "PP_BrowserFont_Trusted_Weight min"); |
137 static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_900 == 8, | 138 static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_900 == 8, |
138 "PP_BrowserFont_Trusted_Weight max"); | 139 "PP_BrowserFont_Trusted_Weight max"); |
139 const int kMinimumWeight = 100; | 140 const int kMinimumWeight = 100; |
140 const int kMaximumWeight = 900; | 141 const int kMaximumWeight = 900; |
141 int normalized_weight = | 142 int normalized_weight = |
142 std::min(std::max(weight, kMinimumWeight), kMaximumWeight); | 143 std::min(std::max(weight, kMinimumWeight), kMaximumWeight); |
143 normalized_weight = (normalized_weight / 100) - 1; | 144 normalized_weight = (normalized_weight / 100) - 1; |
144 return static_cast<PP_BrowserFont_Trusted_Weight>(normalized_weight); | 145 return static_cast<PP_BrowserFont_Trusted_Weight>(normalized_weight); |
145 } | 146 } |
146 | 147 |
147 // This list is for CPWL_FontMap::GetDefaultFontByCharset(). | 148 // This list is for CPWL_FontMap::GetDefaultFontByCharset(). |
148 // We pretend to have these font natively and let the browser (or underlying | 149 // We pretend to have these font natively and let the browser (or underlying |
149 // fontconfig) to pick the proper font on the system. | 150 // fontconfig) to pick the proper font on the system. |
150 void EnumFonts(struct _FPDF_SYSFONTINFO* sysfontinfo, void* mapper) { | 151 void EnumFonts(FPDF_SYSFONTINFO* sysfontinfo, void* mapper) { |
151 FPDF_AddInstalledFont(mapper, "Arial", FXFONT_DEFAULT_CHARSET); | 152 FPDF_AddInstalledFont(mapper, "Arial", FXFONT_DEFAULT_CHARSET); |
152 | 153 |
153 const FPDF_CharsetFontMap* font_map = FPDF_GetDefaultTTFMap(); | 154 const FPDF_CharsetFontMap* font_map = FPDF_GetDefaultTTFMap(); |
154 for (; font_map->charset != -1; ++font_map) { | 155 for (; font_map->charset != -1; ++font_map) { |
155 FPDF_AddInstalledFont(mapper, font_map->fontname, font_map->charset); | 156 FPDF_AddInstalledFont(mapper, font_map->fontname, font_map->charset); |
156 } | 157 } |
157 } | 158 } |
158 | 159 |
159 void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic, | 160 void* MapFont(FPDF_SYSFONTINFO*, |
160 int charset, int pitch_family, const char* face, int* exact) { | 161 int weight, |
162 int italic, | |
163 int charset, | |
164 int pitch_family, | |
165 const char* face, | |
166 int* exact) { | |
161 // Do not attempt to map fonts if pepper is not initialized (for privet local | 167 // Do not attempt to map fonts if pepper is not initialized (for privet local |
162 // printing). | 168 // printing). |
163 // TODO(noamsml): Real font substitution (http://crbug.com/391978) | 169 // TODO(noamsml): Real font substitution (http://crbug.com/391978) |
164 if (!pp::Module::Get()) | 170 if (!pp::Module::Get()) |
165 return nullptr; | 171 return nullptr; |
166 | 172 |
167 pp::BrowserFontDescription description; | 173 pp::BrowserFontDescription description; |
168 | 174 |
169 // Pretend the system does not have the Symbol font to force a fallback to | 175 // Pretend the system does not have the Symbol font to force a fallback to |
170 // the built in Symbol font in CFX_FontMapper::FindSubstFont(). | 176 // the built in Symbol font in CFX_FontMapper::FindSubstFont(). |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 g_engine_for_fontmapper->FontSubstituted(); | 273 g_engine_for_fontmapper->FontSubstituted(); |
268 | 274 |
269 PP_Resource font_resource = pp::PDF::GetFontFileWithFallback( | 275 PP_Resource font_resource = pp::PDF::GetFontFileWithFallback( |
270 pp::InstanceHandle(g_last_instance_id), | 276 pp::InstanceHandle(g_last_instance_id), |
271 &description.pp_font_description(), | 277 &description.pp_font_description(), |
272 static_cast<PP_PrivateFontCharset>(charset)); | 278 static_cast<PP_PrivateFontCharset>(charset)); |
273 long res_id = font_resource; | 279 long res_id = font_resource; |
274 return reinterpret_cast<void*>(res_id); | 280 return reinterpret_cast<void*>(res_id); |
275 } | 281 } |
276 | 282 |
277 unsigned long GetFontData(struct _FPDF_SYSFONTINFO*, void* font_id, | 283 unsigned long GetFontData(FPDF_SYSFONTINFO*, |
278 unsigned int table, unsigned char* buffer, | 284 void* font_id, |
285 unsigned int table, | |
286 unsigned char* buffer, | |
279 unsigned long buf_size) { | 287 unsigned long buf_size) { |
280 if (!pp::PDF::IsAvailable()) { | 288 if (!pp::PDF::IsAvailable()) { |
281 NOTREACHED(); | 289 NOTREACHED(); |
282 return 0; | 290 return 0; |
283 } | 291 } |
284 | 292 |
285 uint32_t size = buf_size; | 293 uint32_t size = buf_size; |
286 long res_id = reinterpret_cast<long>(font_id); | 294 long res_id = reinterpret_cast<long>(font_id); |
287 if (!pp::PDF::GetFontTableForPrivateFontFile(res_id, table, buffer, &size)) | 295 if (!pp::PDF::GetFontTableForPrivateFontFile(res_id, table, buffer, &size)) |
288 return 0; | 296 return 0; |
289 return size; | 297 return size; |
290 } | 298 } |
291 | 299 |
292 void DeleteFont(struct _FPDF_SYSFONTINFO*, void* font_id) { | 300 void DeleteFont(FPDF_SYSFONTINFO*, void* font_id) { |
293 long res_id = reinterpret_cast<long>(font_id); | 301 long res_id = reinterpret_cast<long>(font_id); |
294 pp::Module::Get()->core()->ReleaseResource(res_id); | 302 pp::Module::Get()->core()->ReleaseResource(res_id); |
295 } | 303 } |
296 | 304 |
297 FPDF_SYSFONTINFO g_font_info = { | 305 FPDF_SYSFONTINFO g_font_info = { |
298 1, | 306 1, |
299 0, | 307 0, |
300 EnumFonts, | 308 EnumFonts, |
301 MapFont, | 309 MapFont, |
302 0, | 310 0, |
303 GetFontData, | 311 GetFontData, |
304 0, | 312 0, |
305 0, | 313 0, |
306 DeleteFont | 314 DeleteFont |
307 }; | 315 }; |
316 #else | |
317 struct FPDF_SYSFONTINFO_WITHMETRICS : public FPDF_SYSFONTINFO { | |
318 explicit FPDF_SYSFONTINFO_WITHMETRICS(FPDF_SYSFONTINFO* default_sysfontinfo) { | |
319 version = default_sysfontinfo->version; | |
320 default_sysfontinfo_ = default_sysfontinfo; | |
321 } | |
322 | |
323 ~FPDF_SYSFONTINFO_WITHMETRICS() { delete default_sysfontinfo_; } | |
324 | |
325 FPDF_SYSFONTINFO* default_sysfontinfo_; | |
Lei Zhang
2016/11/09 19:42:56
nit: In structs, no trailing underscore.
npm
2016/11/09 19:58:02
Done.
| |
326 }; | |
327 | |
328 FPDF_SYSFONTINFO_WITHMETRICS* g_font_info = nullptr; | |
329 | |
330 void* MapFontWithMetrics(FPDF_SYSFONTINFO* sysfontinfo, | |
331 int weight, | |
332 int italic, | |
333 int charset, | |
334 int pitch_family, | |
335 const char* face, | |
336 int* exact) { | |
337 auto* fontinfo_with_metrics = | |
338 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
339 if (!fontinfo_with_metrics->default_sysfontinfo_->MapFont) | |
340 return nullptr; | |
341 void* mapped_font = fontinfo_with_metrics->default_sysfontinfo_->MapFont( | |
342 fontinfo_with_metrics->default_sysfontinfo_, weight, italic, charset, | |
343 pitch_family, face, exact); | |
344 if (mapped_font && g_engine_for_fontmapper) | |
345 g_engine_for_fontmapper->FontSubstituted(); | |
346 return mapped_font; | |
347 } | |
348 | |
349 void DeleteFont(FPDF_SYSFONTINFO* sysfontinfo, void* font_id) { | |
350 auto* fontinfo_with_metrics = | |
351 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
352 fontinfo_with_metrics->default_sysfontinfo_->DeleteFont( | |
Lei Zhang
2016/11/09 19:42:56
Should we check the DeleteFont func ptr is not a n
npm
2016/11/09 19:58:02
Well, like I mentioned, implementation is required
| |
353 fontinfo_with_metrics->default_sysfontinfo_, font_id); | |
354 } | |
355 | |
356 void EnumFonts(FPDF_SYSFONTINFO* sysfontinfo, void* mapper) { | |
357 auto* fontinfo_with_metrics = | |
358 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
359 if (!fontinfo_with_metrics->default_sysfontinfo_->EnumFonts) | |
360 return; | |
361 fontinfo_with_metrics->default_sysfontinfo_->EnumFonts( | |
362 fontinfo_with_metrics->default_sysfontinfo_, mapper); | |
363 } | |
364 | |
365 unsigned long GetFaceName(FPDF_SYSFONTINFO* sysfontinfo, | |
366 void* hFont, | |
367 char* buffer, | |
368 unsigned long buffer_size) { | |
369 auto* fontinfo_with_metrics = | |
370 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
371 if (!fontinfo_with_metrics->default_sysfontinfo_->GetFaceName) | |
372 return 0; | |
373 return fontinfo_with_metrics->default_sysfontinfo_->GetFaceName( | |
374 fontinfo_with_metrics->default_sysfontinfo_, hFont, buffer, buffer_size); | |
375 } | |
376 | |
377 void* GetFont(FPDF_SYSFONTINFO* sysfontinfo, const char* face) { | |
378 auto* fontinfo_with_metrics = | |
379 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
380 if (!fontinfo_with_metrics->default_sysfontinfo_->GetFont) | |
381 return nullptr; | |
382 return fontinfo_with_metrics->default_sysfontinfo_->GetFont( | |
383 fontinfo_with_metrics->default_sysfontinfo_, face); | |
384 } | |
385 | |
386 int GetFontCharset(FPDF_SYSFONTINFO* sysfontinfo, void* hFont) { | |
387 auto* fontinfo_with_metrics = | |
388 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
389 if (!fontinfo_with_metrics->default_sysfontinfo_->GetFontCharset) | |
390 return 0; | |
391 return fontinfo_with_metrics->default_sysfontinfo_->GetFontCharset( | |
392 fontinfo_with_metrics->default_sysfontinfo_, hFont); | |
393 } | |
394 | |
395 unsigned long GetFontData(FPDF_SYSFONTINFO* sysfontinfo, | |
396 void* hFont, | |
397 unsigned int table, | |
398 unsigned char* buffer, | |
399 unsigned long buf_size) { | |
400 auto* fontinfo_with_metrics = | |
401 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
402 return fontinfo_with_metrics->default_sysfontinfo_->GetFontData( | |
403 fontinfo_with_metrics->default_sysfontinfo_, hFont, table, buffer, | |
404 buf_size); | |
405 } | |
406 | |
407 void Release(FPDF_SYSFONTINFO* sysfontinfo) { | |
408 auto* fontinfo_with_metrics = | |
409 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
410 if (!fontinfo_with_metrics->default_sysfontinfo_->Release) | |
411 return; | |
412 fontinfo_with_metrics->default_sysfontinfo_->Release( | |
413 fontinfo_with_metrics->default_sysfontinfo_); | |
414 } | |
308 #endif // defined(OS_LINUX) | 415 #endif // defined(OS_LINUX) |
309 | 416 |
310 PDFiumEngine* g_engine_for_unsupported = nullptr; | 417 PDFiumEngine* g_engine_for_unsupported = nullptr; |
311 | 418 |
312 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { | 419 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
313 if (!g_engine_for_unsupported) { | 420 if (!g_engine_for_unsupported) { |
314 NOTREACHED(); | 421 NOTREACHED(); |
315 return; | 422 return; |
316 } | 423 } |
317 | 424 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
509 bool InitializeSDK() { | 616 bool InitializeSDK() { |
510 SetUpV8(); | 617 SetUpV8(); |
511 | 618 |
512 FPDF_LIBRARY_CONFIG config; | 619 FPDF_LIBRARY_CONFIG config; |
513 config.version = 2; | 620 config.version = 2; |
514 config.m_pUserFontPaths = nullptr; | 621 config.m_pUserFontPaths = nullptr; |
515 config.m_pIsolate = v8::Isolate::GetCurrent(); | 622 config.m_pIsolate = v8::Isolate::GetCurrent(); |
516 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; | 623 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; |
517 FPDF_InitLibraryWithConfig(&config); | 624 FPDF_InitLibraryWithConfig(&config); |
518 | 625 |
519 #if defined(OS_LINUX) | 626 #if !defined(OS_LINUX) |
Lei Zhang
2016/11/09 19:42:56
Can you flip this around to #if defined(OS_LINUX)
npm
2016/11/09 19:58:02
Done.
| |
627 g_font_info = | |
628 new FPDF_SYSFONTINFO_WITHMETRICS(FPDF_GetDefaultSystemFontInfo()); | |
629 g_font_info->Release = Release; | |
630 g_font_info->EnumFonts = EnumFonts; | |
631 // Set new MapFont that calculates metrics | |
632 g_font_info->MapFont = MapFontWithMetrics; | |
633 g_font_info->GetFont = GetFont; | |
634 g_font_info->GetFaceName = GetFaceName; | |
635 g_font_info->GetFontCharset = GetFontCharset; | |
636 g_font_info->GetFontData = GetFontData; | |
637 g_font_info->DeleteFont = DeleteFont; | |
638 FPDF_SetSystemFontInfo(g_font_info); | |
639 #else | |
520 // Font loading doesn't work in the renderer sandbox in Linux. | 640 // Font loading doesn't work in the renderer sandbox in Linux. |
521 FPDF_SetSystemFontInfo(&g_font_info); | 641 FPDF_SetSystemFontInfo(&g_font_info); |
522 #endif | 642 #endif |
523 | 643 |
524 FSDK_SetUnSpObjProcessHandler(&g_unsupported_info); | 644 FSDK_SetUnSpObjProcessHandler(&g_unsupported_info); |
525 | 645 |
526 return true; | 646 return true; |
527 } | 647 } |
528 | 648 |
529 void ShutdownSDK() { | 649 void ShutdownSDK() { |
650 #if !defined(OS_LINUX) | |
651 FPDF_FreeDefaultSystemFontInfo(g_font_info); | |
652 #endif | |
530 FPDF_DestroyLibrary(); | 653 FPDF_DestroyLibrary(); |
531 TearDownV8(); | 654 TearDownV8(); |
532 } | 655 } |
533 | 656 |
534 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { | 657 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { |
535 return new PDFiumEngine(client); | 658 return new PDFiumEngine(client); |
536 } | 659 } |
537 | 660 |
538 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) | 661 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) |
539 : client_(client), | 662 : client_(client), |
(...skipping 3485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4025 FPDF_DOCUMENT doc = | 4148 FPDF_DOCUMENT doc = |
4026 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 4149 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
4027 if (!doc) | 4150 if (!doc) |
4028 return false; | 4151 return false; |
4029 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4152 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
4030 FPDF_CloseDocument(doc); | 4153 FPDF_CloseDocument(doc); |
4031 return success; | 4154 return success; |
4032 } | 4155 } |
4033 | 4156 |
4034 } // namespace chrome_pdf | 4157 } // namespace chrome_pdf |
OLD | NEW |