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

Side by Side Diff: pdf/pdfium/pdfium_engine.cc

Issue 2479313005: Add font substituted metrics for OS other than Linux (Closed)
Patch Set: Fix Delete! 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 | « pdf/out_of_process_instance.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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* sysfontinfo) {
319 version = sysfontinfo->version;
320 default_sysfontinfo = 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 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 if (!fontinfo_with_metrics->default_sysfontinfo->DeleteFont)
353 return;
354 fontinfo_with_metrics->default_sysfontinfo->DeleteFont(
355 fontinfo_with_metrics->default_sysfontinfo, font_id);
356 }
357
358 void EnumFonts(FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
359 auto* fontinfo_with_metrics =
360 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
361 if (!fontinfo_with_metrics->default_sysfontinfo->EnumFonts)
362 return;
363 fontinfo_with_metrics->default_sysfontinfo->EnumFonts(
364 fontinfo_with_metrics->default_sysfontinfo, mapper);
365 }
366
367 unsigned long GetFaceName(FPDF_SYSFONTINFO* sysfontinfo,
368 void* hFont,
369 char* buffer,
370 unsigned long buffer_size) {
371 auto* fontinfo_with_metrics =
372 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
373 if (!fontinfo_with_metrics->default_sysfontinfo->GetFaceName)
374 return 0;
375 return fontinfo_with_metrics->default_sysfontinfo->GetFaceName(
376 fontinfo_with_metrics->default_sysfontinfo, hFont, buffer, buffer_size);
377 }
378
379 void* GetFont(FPDF_SYSFONTINFO* sysfontinfo, const char* face) {
380 auto* fontinfo_with_metrics =
381 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
382 if (!fontinfo_with_metrics->default_sysfontinfo->GetFont)
383 return nullptr;
384 return fontinfo_with_metrics->default_sysfontinfo->GetFont(
385 fontinfo_with_metrics->default_sysfontinfo, face);
386 }
387
388 int GetFontCharset(FPDF_SYSFONTINFO* sysfontinfo, void* hFont) {
389 auto* fontinfo_with_metrics =
390 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
391 if (!fontinfo_with_metrics->default_sysfontinfo->GetFontCharset)
392 return 0;
393 return fontinfo_with_metrics->default_sysfontinfo->GetFontCharset(
394 fontinfo_with_metrics->default_sysfontinfo, hFont);
395 }
396
397 unsigned long GetFontData(FPDF_SYSFONTINFO* sysfontinfo,
398 void* hFont,
399 unsigned int table,
400 unsigned char* buffer,
401 unsigned long buf_size) {
402 auto* fontinfo_with_metrics =
403 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
404 if (!fontinfo_with_metrics->default_sysfontinfo->GetFontData)
405 return 0;
406 return fontinfo_with_metrics->default_sysfontinfo->GetFontData(
407 fontinfo_with_metrics->default_sysfontinfo, hFont, table, buffer,
408 buf_size);
409 }
410
411 void Release(FPDF_SYSFONTINFO* sysfontinfo) {
412 auto* fontinfo_with_metrics =
413 static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
414 if (!fontinfo_with_metrics->default_sysfontinfo->Release)
415 return;
416 fontinfo_with_metrics->default_sysfontinfo->Release(
417 fontinfo_with_metrics->default_sysfontinfo);
418 }
308 #endif // defined(OS_LINUX) 419 #endif // defined(OS_LINUX)
309 420
310 PDFiumEngine* g_engine_for_unsupported = nullptr; 421 PDFiumEngine* g_engine_for_unsupported = nullptr;
311 422
312 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { 423 void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
313 if (!g_engine_for_unsupported) { 424 if (!g_engine_for_unsupported) {
314 NOTREACHED(); 425 NOTREACHED();
315 return; 426 return;
316 } 427 }
317 428
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 FPDF_LIBRARY_CONFIG config; 623 FPDF_LIBRARY_CONFIG config;
513 config.version = 2; 624 config.version = 2;
514 config.m_pUserFontPaths = nullptr; 625 config.m_pUserFontPaths = nullptr;
515 config.m_pIsolate = v8::Isolate::GetCurrent(); 626 config.m_pIsolate = v8::Isolate::GetCurrent();
516 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; 627 config.m_v8EmbedderSlot = gin::kEmbedderPDFium;
517 FPDF_InitLibraryWithConfig(&config); 628 FPDF_InitLibraryWithConfig(&config);
518 629
519 #if defined(OS_LINUX) 630 #if defined(OS_LINUX)
520 // Font loading doesn't work in the renderer sandbox in Linux. 631 // Font loading doesn't work in the renderer sandbox in Linux.
521 FPDF_SetSystemFontInfo(&g_font_info); 632 FPDF_SetSystemFontInfo(&g_font_info);
633 #else
634 g_font_info =
635 new FPDF_SYSFONTINFO_WITHMETRICS(FPDF_GetDefaultSystemFontInfo());
636 g_font_info->Release = Release;
637 g_font_info->EnumFonts = EnumFonts;
638 // Set new MapFont that calculates metrics
639 g_font_info->MapFont = MapFontWithMetrics;
640 g_font_info->GetFont = GetFont;
641 g_font_info->GetFaceName = GetFaceName;
642 g_font_info->GetFontCharset = GetFontCharset;
643 g_font_info->GetFontData = GetFontData;
644 g_font_info->DeleteFont = DeleteFont;
645 FPDF_SetSystemFontInfo(g_font_info);
522 #endif 646 #endif
523 647
524 FSDK_SetUnSpObjProcessHandler(&g_unsupported_info); 648 FSDK_SetUnSpObjProcessHandler(&g_unsupported_info);
525 649
526 return true; 650 return true;
527 } 651 }
528 652
529 void ShutdownSDK() { 653 void ShutdownSDK() {
654 #if !defined(OS_LINUX)
655 FPDF_FreeDefaultSystemFontInfo(g_font_info->default_sysfontinfo);
656 delete g_font_info;
657 #endif
530 FPDF_DestroyLibrary(); 658 FPDF_DestroyLibrary();
531 TearDownV8(); 659 TearDownV8();
532 } 660 }
533 661
534 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { 662 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) {
535 return new PDFiumEngine(client); 663 return new PDFiumEngine(client);
536 } 664 }
537 665
538 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) 666 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
539 : client_(client), 667 : client_(client),
(...skipping 3485 matching lines...) Expand 10 before | Expand all | Expand 10 after
4025 FPDF_DOCUMENT doc = 4153 FPDF_DOCUMENT doc =
4026 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4154 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4027 if (!doc) 4155 if (!doc)
4028 return false; 4156 return false;
4029 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4157 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4030 FPDF_CloseDocument(doc); 4158 FPDF_CloseDocument(doc);
4031 return success; 4159 return success;
4032 } 4160 }
4033 4161
4034 } // namespace chrome_pdf 4162 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/out_of_process_instance.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698