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

Unified Diff: pdf/pdfium/pdfium_engine.cc

Issue 2479313005: Add font substituted metrics for OS other than Linux (Closed)
Patch Set: Move ifs inside methods 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pdf/out_of_process_instance.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pdf/pdfium/pdfium_engine.cc
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc
index 475d56933053765ad2f460b8ca1d7a89df84a095..e0b586267d560b0a957734afb7c1aff9cf698994 100644
--- a/pdf/pdfium/pdfium_engine.cc
+++ b/pdf/pdfium/pdfium_engine.cc
@@ -131,6 +131,7 @@ std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange(
PP_Instance g_last_instance_id;
+// TODO(npm): Move font stuff to another file to reduce the size of this one
PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) {
static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0,
"PP_BrowserFont_Trusted_Weight min");
@@ -147,7 +148,7 @@ PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) {
// This list is for CPWL_FontMap::GetDefaultFontByCharset().
// We pretend to have these font natively and let the browser (or underlying
// fontconfig) to pick the proper font on the system.
-void EnumFonts(struct _FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
+void EnumFonts(FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
FPDF_AddInstalledFont(mapper, "Arial", FXFONT_DEFAULT_CHARSET);
const FPDF_CharsetFontMap* font_map = FPDF_GetDefaultTTFMap();
@@ -156,8 +157,13 @@ void EnumFonts(struct _FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
}
}
-void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic,
- int charset, int pitch_family, const char* face, int* exact) {
+void* MapFont(FPDF_SYSFONTINFO*,
+ int weight,
+ int italic,
+ int charset,
+ int pitch_family,
+ const char* face,
+ int* exact) {
// Do not attempt to map fonts if pepper is not initialized (for privet local
// printing).
// TODO(noamsml): Real font substitution (http://crbug.com/391978)
@@ -274,8 +280,10 @@ void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic,
return reinterpret_cast<void*>(res_id);
}
-unsigned long GetFontData(struct _FPDF_SYSFONTINFO*, void* font_id,
- unsigned int table, unsigned char* buffer,
+unsigned long GetFontData(FPDF_SYSFONTINFO*,
+ void* font_id,
+ unsigned int table,
+ unsigned char* buffer,
unsigned long buf_size) {
if (!pp::PDF::IsAvailable()) {
NOTREACHED();
@@ -289,7 +297,7 @@ unsigned long GetFontData(struct _FPDF_SYSFONTINFO*, void* font_id,
return size;
}
-void DeleteFont(struct _FPDF_SYSFONTINFO*, void* font_id) {
+void DeleteFont(FPDF_SYSFONTINFO*, void* font_id) {
long res_id = reinterpret_cast<long>(font_id);
pp::Module::Get()->core()->ReleaseResource(res_id);
}
@@ -305,6 +313,105 @@ FPDF_SYSFONTINFO g_font_info = {
0,
DeleteFont
};
+#else
+struct FPDF_SYSFONTINFO_WITHMETRICS : public FPDF_SYSFONTINFO {
+ explicit FPDF_SYSFONTINFO_WITHMETRICS(FPDF_SYSFONTINFO* default_sysfontinfo) {
+ version = default_sysfontinfo->version;
+ default_sysfontinfo_ = default_sysfontinfo;
+ }
+
+ ~FPDF_SYSFONTINFO_WITHMETRICS() { delete default_sysfontinfo_; }
+
+ 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.
+};
+
+FPDF_SYSFONTINFO_WITHMETRICS* g_font_info = nullptr;
+
+void* MapFontWithMetrics(FPDF_SYSFONTINFO* sysfontinfo,
+ int weight,
+ int italic,
+ int charset,
+ int pitch_family,
+ const char* face,
+ int* exact) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ if (!fontinfo_with_metrics->default_sysfontinfo_->MapFont)
+ return nullptr;
+ void* mapped_font = fontinfo_with_metrics->default_sysfontinfo_->MapFont(
+ fontinfo_with_metrics->default_sysfontinfo_, weight, italic, charset,
+ pitch_family, face, exact);
+ if (mapped_font && g_engine_for_fontmapper)
+ g_engine_for_fontmapper->FontSubstituted();
+ return mapped_font;
+}
+
+void DeleteFont(FPDF_SYSFONTINFO* sysfontinfo, void* font_id) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ 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
+ fontinfo_with_metrics->default_sysfontinfo_, font_id);
+}
+
+void EnumFonts(FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ if (!fontinfo_with_metrics->default_sysfontinfo_->EnumFonts)
+ return;
+ fontinfo_with_metrics->default_sysfontinfo_->EnumFonts(
+ fontinfo_with_metrics->default_sysfontinfo_, mapper);
+}
+
+unsigned long GetFaceName(FPDF_SYSFONTINFO* sysfontinfo,
+ void* hFont,
+ char* buffer,
+ unsigned long buffer_size) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ if (!fontinfo_with_metrics->default_sysfontinfo_->GetFaceName)
+ return 0;
+ return fontinfo_with_metrics->default_sysfontinfo_->GetFaceName(
+ fontinfo_with_metrics->default_sysfontinfo_, hFont, buffer, buffer_size);
+}
+
+void* GetFont(FPDF_SYSFONTINFO* sysfontinfo, const char* face) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ if (!fontinfo_with_metrics->default_sysfontinfo_->GetFont)
+ return nullptr;
+ return fontinfo_with_metrics->default_sysfontinfo_->GetFont(
+ fontinfo_with_metrics->default_sysfontinfo_, face);
+}
+
+int GetFontCharset(FPDF_SYSFONTINFO* sysfontinfo, void* hFont) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ if (!fontinfo_with_metrics->default_sysfontinfo_->GetFontCharset)
+ return 0;
+ return fontinfo_with_metrics->default_sysfontinfo_->GetFontCharset(
+ fontinfo_with_metrics->default_sysfontinfo_, hFont);
+}
+
+unsigned long GetFontData(FPDF_SYSFONTINFO* sysfontinfo,
+ void* hFont,
+ unsigned int table,
+ unsigned char* buffer,
+ unsigned long buf_size) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ return fontinfo_with_metrics->default_sysfontinfo_->GetFontData(
+ fontinfo_with_metrics->default_sysfontinfo_, hFont, table, buffer,
+ buf_size);
+}
+
+void Release(FPDF_SYSFONTINFO* sysfontinfo) {
+ auto* fontinfo_with_metrics =
+ static_cast<FPDF_SYSFONTINFO_WITHMETRICS*>(sysfontinfo);
+ if (!fontinfo_with_metrics->default_sysfontinfo_->Release)
+ return;
+ fontinfo_with_metrics->default_sysfontinfo_->Release(
+ fontinfo_with_metrics->default_sysfontinfo_);
+}
#endif // defined(OS_LINUX)
PDFiumEngine* g_engine_for_unsupported = nullptr;
@@ -516,7 +623,20 @@ bool InitializeSDK() {
config.m_v8EmbedderSlot = gin::kEmbedderPDFium;
FPDF_InitLibraryWithConfig(&config);
-#if defined(OS_LINUX)
+#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.
+ g_font_info =
+ new FPDF_SYSFONTINFO_WITHMETRICS(FPDF_GetDefaultSystemFontInfo());
+ g_font_info->Release = Release;
+ g_font_info->EnumFonts = EnumFonts;
+ // Set new MapFont that calculates metrics
+ g_font_info->MapFont = MapFontWithMetrics;
+ g_font_info->GetFont = GetFont;
+ g_font_info->GetFaceName = GetFaceName;
+ g_font_info->GetFontCharset = GetFontCharset;
+ g_font_info->GetFontData = GetFontData;
+ g_font_info->DeleteFont = DeleteFont;
+ FPDF_SetSystemFontInfo(g_font_info);
+#else
// Font loading doesn't work in the renderer sandbox in Linux.
FPDF_SetSystemFontInfo(&g_font_info);
#endif
@@ -527,6 +647,9 @@ bool InitializeSDK() {
}
void ShutdownSDK() {
+#if !defined(OS_LINUX)
+ FPDF_FreeDefaultSystemFontInfo(g_font_info);
+#endif
FPDF_DestroyLibrary();
TearDownV8();
}
« 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