Chromium Code Reviews| 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_; | |
| 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 void* mapped_font = fontinfo_with_metrics->default_sysfontinfo_->MapFont( | |
| 340 fontinfo_with_metrics->default_sysfontinfo_, weight, italic, charset, | |
| 341 pitch_family, face, exact); | |
| 342 if (mapped_font && g_engine_for_fontmapper) | |
| 343 g_engine_for_fontmapper->FontSubstituted(); | |
| 344 return mapped_font; | |
| 345 } | |
| 346 | |
| 347 void DeleteFont(FPDF_SYSFONTINFO* sysfontinfo, void* font_id) { | |
| 348 auto* fontinfo_with_metrics = | |
| 349 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 350 fontinfo_with_metrics->default_sysfontinfo_->DeleteFont( | |
| 351 fontinfo_with_metrics->default_sysfontinfo_, font_id); | |
| 352 } | |
| 353 | |
| 354 void EnumFonts(FPDF_SYSFONTINFO* sysfontinfo, void* mapper) { | |
| 355 auto* fontinfo_with_metrics = | |
| 356 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 357 fontinfo_with_metrics->default_sysfontinfo_->EnumFonts( | |
| 358 fontinfo_with_metrics->default_sysfontinfo_, mapper); | |
| 359 } | |
| 360 | |
| 361 unsigned long GetFaceName(FPDF_SYSFONTINFO* sysfontinfo, | |
| 362 void* hFont, | |
| 363 char* buffer, | |
| 364 unsigned long buffer_size) { | |
| 365 auto* fontinfo_with_metrics = | |
| 366 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 367 return fontinfo_with_metrics->default_sysfontinfo_->GetFaceName( | |
| 368 fontinfo_with_metrics->default_sysfontinfo_, hFont, buffer, buffer_size); | |
| 369 } | |
| 370 | |
| 371 void* GetFont(FPDF_SYSFONTINFO* sysfontinfo, const char* face) { | |
| 372 auto* fontinfo_with_metrics = | |
| 373 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 374 return fontinfo_with_metrics->default_sysfontinfo_->GetFont( | |
| 375 fontinfo_with_metrics->default_sysfontinfo_, face); | |
| 376 } | |
| 377 | |
| 378 int GetFontCharset(FPDF_SYSFONTINFO* sysfontinfo, void* hFont) { | |
| 379 auto* fontinfo_with_metrics = | |
| 380 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 381 return fontinfo_with_metrics->default_sysfontinfo_->GetFontCharset( | |
| 382 fontinfo_with_metrics->default_sysfontinfo_, hFont); | |
| 383 } | |
| 384 | |
| 385 unsigned long GetFontData(FPDF_SYSFONTINFO* sysfontinfo, | |
| 386 void* hFont, | |
| 387 unsigned int table, | |
| 388 unsigned char* buffer, | |
| 389 unsigned long buf_size) { | |
| 390 auto* fontinfo_with_metrics = | |
| 391 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 392 return fontinfo_with_metrics->default_sysfontinfo_->GetFontData( | |
| 393 fontinfo_with_metrics->default_sysfontinfo_, hFont, table, buffer, | |
| 394 buf_size); | |
| 395 } | |
| 396 | |
| 397 void Release(FPDF_SYSFONTINFO* sysfontinfo) { | |
| 398 auto* fontinfo_with_metrics = | |
| 399 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo); | |
| 400 fontinfo_with_metrics->default_sysfontinfo_->Release( | |
| 401 fontinfo_with_metrics->default_sysfontinfo_); | |
| 402 } | |
| 308 #endif // defined(OS_LINUX) | 403 #endif // defined(OS_LINUX) |
| 309 | 404 |
| 310 PDFiumEngine* g_engine_for_unsupported = nullptr; | 405 PDFiumEngine* g_engine_for_unsupported = nullptr; |
| 311 | 406 |
| 312 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { | 407 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
| 313 if (!g_engine_for_unsupported) { | 408 if (!g_engine_for_unsupported) { |
| 314 NOTREACHED(); | 409 NOTREACHED(); |
| 315 return; | 410 return; |
| 316 } | 411 } |
| 317 | 412 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 bool InitializeSDK() { | 604 bool InitializeSDK() { |
| 510 SetUpV8(); | 605 SetUpV8(); |
| 511 | 606 |
| 512 FPDF_LIBRARY_CONFIG config; | 607 FPDF_LIBRARY_CONFIG config; |
| 513 config.version = 2; | 608 config.version = 2; |
| 514 config.m_pUserFontPaths = nullptr; | 609 config.m_pUserFontPaths = nullptr; |
| 515 config.m_pIsolate = v8::Isolate::GetCurrent(); | 610 config.m_pIsolate = v8::Isolate::GetCurrent(); |
| 516 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; | 611 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; |
| 517 FPDF_InitLibraryWithConfig(&config); | 612 FPDF_InitLibraryWithConfig(&config); |
| 518 | 613 |
| 519 #if defined(OS_LINUX) | 614 #if !defined(OS_LINUX) |
| 615 g_font_info = | |
| 616 new FPDF_SYSFONTINFO_WITHMETRICS(FPDF_GetDefaultSystemFontInfo()); | |
| 617 if (g_font_info->default_sysfontinfo_->Release) | |
|
Tom Sepez
2016/11/09 17:11:35
nit: this is OK, but I think I'd be happier if we
npm
2016/11/09 19:22:21
Done.
| |
| 618 g_font_info->Release = Release; | |
| 619 if (g_font_info->default_sysfontinfo_->EnumFonts) | |
| 620 g_font_info->EnumFonts = EnumFonts; | |
| 621 // Set new MapFont that calculates metrics | |
| 622 if (g_font_info->default_sysfontinfo_->MapFont) | |
| 623 g_font_info->MapFont = MapFontWithMetrics; | |
| 624 if (g_font_info->default_sysfontinfo_->GetFont) | |
| 625 g_font_info->GetFont = GetFont; | |
| 626 if (g_font_info->default_sysfontinfo_->GetFaceName) | |
| 627 g_font_info->GetFaceName = GetFaceName; | |
| 628 if (g_font_info->default_sysfontinfo_->GetFontCharset) | |
| 629 g_font_info->GetFontCharset = GetFontCharset; | |
| 630 // These are required according to documentation | |
| 631 g_font_info->GetFontData = GetFontData; | |
| 632 g_font_info->DeleteFont = DeleteFont; | |
| 633 FPDF_SetSystemFontInfo(g_font_info); | |
| 634 #else | |
| 520 // Font loading doesn't work in the renderer sandbox in Linux. | 635 // Font loading doesn't work in the renderer sandbox in Linux. |
| 521 FPDF_SetSystemFontInfo(&g_font_info); | 636 FPDF_SetSystemFontInfo(&g_font_info); |
| 522 #endif | 637 #endif |
| 523 | 638 |
| 524 FSDK_SetUnSpObjProcessHandler(&g_unsupported_info); | 639 FSDK_SetUnSpObjProcessHandler(&g_unsupported_info); |
| 525 | 640 |
| 526 return true; | 641 return true; |
| 527 } | 642 } |
| 528 | 643 |
| 529 void ShutdownSDK() { | 644 void ShutdownSDK() { |
| 645 #if !defined(OS_LINUX) | |
| 646 FPDF_FreeDefaultSystemFontInfo(g_font_info); | |
| 647 #endif | |
| 530 FPDF_DestroyLibrary(); | 648 FPDF_DestroyLibrary(); |
| 531 TearDownV8(); | 649 TearDownV8(); |
| 532 } | 650 } |
| 533 | 651 |
| 534 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { | 652 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { |
| 535 return new PDFiumEngine(client); | 653 return new PDFiumEngine(client); |
| 536 } | 654 } |
| 537 | 655 |
| 538 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) | 656 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) |
| 539 : client_(client), | 657 : client_(client), |
| (...skipping 3485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4025 FPDF_DOCUMENT doc = | 4143 FPDF_DOCUMENT doc = |
| 4026 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 4144 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
| 4027 if (!doc) | 4145 if (!doc) |
| 4028 return false; | 4146 return false; |
| 4029 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4147 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
| 4030 FPDF_CloseDocument(doc); | 4148 FPDF_CloseDocument(doc); |
| 4031 return success; | 4149 return success; |
| 4032 } | 4150 } |
| 4033 | 4151 |
| 4034 } // namespace chrome_pdf | 4152 } // namespace chrome_pdf |
| OLD | NEW |