0

[unseasoned-pdf] Move Pepper Linux font code to pdf/ppapi_migration/.

Move Pepper-specific Linux font code from pdf/pdfium/ to
pdf/ppapi_migration/. In pdf/pdfium/, start using
blink::WebFontDescription in place of pp::BrowserFontDescription. In
pdf/ppapi_migration/, translate blink::WebFontDescription to
pp::BrowserFontDescription.

Bug: 1139079
Change-Id: Ie5e8fa1bb1e06c73ed0ee28f848ead90ec7d0523
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2853426
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Dominik Röttsches <drott@chromium.org>
Reviewed-by: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#886555}
This commit is contained in:
Lei Zhang
2021-05-26 01:33:27 +00:00
committed by Chromium LUCI CQ
parent 26d7ea11b2
commit 5c6fa34bd1
5 changed files with 126 additions and 60 deletions

@ -179,6 +179,7 @@ if (enable_pdf) {
"//ppapi/cpp:objects",
"//ppapi/cpp/private:internal_module",
"//printing",
"//third_party/blink/public:blink_headers",
"//third_party/blink/public/common:headers",
"//third_party/icu",
"//third_party/pdfium",

@ -14,27 +14,22 @@
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "pdf/ppapi_migration/pdfium_font_linux.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/pdf.h"
#include "ppapi/cpp/trusted/browser_font_trusted.h"
#include "third_party/blink/public/platform/web_font_description.h"
#include "third_party/pdfium/public/fpdf_sysfontinfo.h"
namespace chrome_pdf {
namespace {
PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) {
static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0,
"PP_BrowserFont_Trusted_Weight min");
static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_900 == 8,
"PP_BrowserFont_Trusted_Weight max");
blink::WebFontDescription::Weight WeightToBlinkWeight(int weight) {
static_assert(blink::WebFontDescription::kWeight100 == 0, "Blink Weight min");
static_assert(blink::WebFontDescription::kWeight900 == 8, "Blink Weight max");
constexpr int kMinimumWeight = 100;
constexpr int kMaximumWeight = 900;
int normalized_weight =
base::ClampToRange(weight, kMinimumWeight, kMaximumWeight);
normalized_weight = (normalized_weight / 100) - 1;
return static_cast<PP_BrowserFont_Trusted_Weight>(normalized_weight);
return static_cast<blink::WebFontDescription::Weight>(normalized_weight);
}
// This list is for CPWL_FontMap::GetDefaultFontByCharset().
@ -56,23 +51,19 @@ void* MapFont(FPDF_SYSFONTINFO*,
int pitch_family,
const char* face,
int* exact) {
// Do not attempt to map fonts if PPAPI is not initialized (for Privet local
// printing).
// TODO(noamsml): Real font substitution (http://crbug.com/391978)
if (!pp::Module::Get())
return nullptr;
pp::BrowserFontDescription description;
// Pretend the system does not have the Symbol font to force a fallback to
// the built in Symbol font in CFX_FontMapper::FindSubstFont().
if (strcmp(face, "Symbol") == 0)
return nullptr;
blink::WebFontDescription desc;
if (pitch_family & FXFONT_FF_FIXEDPITCH) {
description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
desc.generic_family = blink::WebFontDescription::kGenericFamilyMonospace;
} else if (pitch_family & FXFONT_FF_ROMAN) {
description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF);
desc.generic_family = blink::WebFontDescription::kGenericFamilySerif;
} else {
desc.generic_family = blink::WebFontDescription::kGenericFamilyStandard;
}
static const struct {
@ -124,17 +115,17 @@ void* MapFont(FPDF_SYSFONTINFO*,
size_t i;
for (i = 0; i < base::size(kPdfFontSubstitutions); ++i) {
if (strcmp(face, kPdfFontSubstitutions[i].pdf_name) == 0) {
description.set_face(kPdfFontSubstitutions[i].face);
desc.family = blink::WebString::FromUTF8(kPdfFontSubstitutions[i].face);
if (kPdfFontSubstitutions[i].bold)
description.set_weight(PP_BROWSERFONT_TRUSTED_WEIGHT_BOLD);
desc.weight = blink::WebFontDescription::kWeightBold;
if (kPdfFontSubstitutions[i].italic)
description.set_italic(true);
desc.italic = true;
break;
}
}
if (i == base::size(kPdfFontSubstitutions)) {
// Convert to UTF-8 before calling set_face().
// Convert to UTF-8 and make sure it is valid.
std::string face_utf8;
if (base::IsStringUTF8(face)) {
face_utf8 = face;
@ -149,22 +140,12 @@ void* MapFont(FPDF_SYSFONTINFO*,
if (face_utf8.empty())
return nullptr;
description.set_face(face_utf8);
description.set_weight(WeightToBrowserFontTrustedWeight(weight));
description.set_italic(italic > 0);
desc.family = blink::WebString::FromUTF8(face_utf8);
desc.weight = WeightToBlinkWeight(weight);
desc.italic = italic > 0;
}
if (!pp::PDF::IsAvailable()) {
NOTREACHED();
return nullptr;
}
PP_Resource font_resource = pp::PDF::GetFontFileWithFallback(
pp::InstanceHandle(GetLastPepperInstance()),
&description.pp_font_description(),
static_cast<PP_PrivateFontCharset>(charset));
long res_id = font_resource;
return reinterpret_cast<void*>(res_id);
return MapPepperFont(desc, charset);
}
unsigned long GetFontData(FPDF_SYSFONTINFO*,
@ -172,21 +153,11 @@ unsigned long GetFontData(FPDF_SYSFONTINFO*,
unsigned int table,
unsigned char* buffer,
unsigned long buf_size) {
if (!pp::PDF::IsAvailable()) {
NOTREACHED();
return 0;
}
uint32_t size = buf_size;
long res_id = reinterpret_cast<long>(font_id);
if (!pp::PDF::GetFontTableForPrivateFontFile(res_id, table, buffer, &size))
return 0;
return size;
return GetPepperFontData(font_id, table, buffer, buf_size);
}
void DeleteFont(FPDF_SYSFONTINFO*, void* font_id) {
long res_id = reinterpret_cast<long>(font_id);
pp::Module::Get()->core()->ReleaseResource(res_id);
DeletePepperFont(font_id);
}
FPDF_SYSFONTINFO g_font_info = {1, 0, EnumFonts, MapFont, 0,

@ -7,9 +7,11 @@
namespace chrome_pdf {
// Initializes a Linux-specific font mapper that proxies font requests via
// PPAPI. This is necessary because font loading does not work in the sandbox on
// Linux.
// Initializes a Linux-specific font mapper.
// This is necessary because font loading does not work in the sandbox on Linux.
// Depending on how this process is initialized, it either:
// a) sends font requests to Blink.
// b) proxies font requests via PPAPI.
void InitializeLinuxFontMapper();
} // namespace chrome_pdf

@ -4,7 +4,12 @@
#include "pdf/ppapi_migration/pdfium_font_linux.h"
#include "base/notreached.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/pdf.h"
#include "ppapi/cpp/trusted/browser_font_trusted.h"
#include "third_party/blink/public/platform/web_font_description.h"
namespace chrome_pdf {
@ -12,15 +17,86 @@ namespace {
PP_Instance g_last_instance_id;
constexpr PP_BrowserFont_Trusted_Weight ToPepperWeight(
blink::WebFontDescription::Weight w) {
return static_cast<PP_BrowserFont_Trusted_Weight>(w);
}
#define FONT_WEIGHT_MATCH_ASSERT(x) \
static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_##x == \
ToPepperWeight(blink::WebFontDescription::kWeight##x), \
"Font weight mismatch.")
FONT_WEIGHT_MATCH_ASSERT(100);
FONT_WEIGHT_MATCH_ASSERT(200);
FONT_WEIGHT_MATCH_ASSERT(300);
FONT_WEIGHT_MATCH_ASSERT(400);
FONT_WEIGHT_MATCH_ASSERT(500);
FONT_WEIGHT_MATCH_ASSERT(600);
FONT_WEIGHT_MATCH_ASSERT(700);
FONT_WEIGHT_MATCH_ASSERT(800);
FONT_WEIGHT_MATCH_ASSERT(900);
#undef FONT_WEIGHT_MATCH_ASSERT
} // namespace
void* MapPepperFont(const blink::WebFontDescription& desc, int charset) {
// Do not attempt to map fonts if PPAPI is not initialized (for Privet local
// printing).
// TODO(noamsml): Real font substitution (http://crbug.com/391978)
if (!pp::Module::Get())
return nullptr;
if (!pp::PDF::IsAvailable()) {
NOTREACHED();
return nullptr;
}
pp::BrowserFontDescription description;
if (desc.generic_family ==
blink::WebFontDescription::kGenericFamilyMonospace) {
description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
} else if (desc.generic_family ==
blink::WebFontDescription::kGenericFamilySerif) {
description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF);
}
description.set_face(desc.family.Utf8());
description.set_weight(ToPepperWeight(desc.weight));
description.set_italic(desc.italic);
PP_Resource font_resource = pp::PDF::GetFontFileWithFallback(
pp::InstanceHandle(g_last_instance_id),
&description.pp_font_description(),
static_cast<PP_PrivateFontCharset>(charset));
long res_id = font_resource;
return reinterpret_cast<void*>(res_id);
}
unsigned long GetPepperFontData(void* font_id,
unsigned int table,
unsigned char* buffer,
unsigned long buf_size) {
if (!pp::PDF::IsAvailable()) {
NOTREACHED();
return 0;
}
uint32_t size = buf_size;
long res_id = reinterpret_cast<long>(font_id);
if (!pp::PDF::GetFontTableForPrivateFontFile(res_id, table, buffer, &size))
return 0;
return size;
}
void DeletePepperFont(void* font_id) {
long res_id = reinterpret_cast<long>(font_id);
pp::Module::Get()->core()->ReleaseResource(res_id);
}
void SetLastPepperInstance(pp::Instance* last_instance) {
if (last_instance)
g_last_instance_id = last_instance->pp_instance();
}
PP_Instance GetLastPepperInstance() {
return g_last_instance_id;
}
} // namespace chrome_pdf

@ -5,7 +5,9 @@
#ifndef PDF_PPAPI_MIGRATION_PDFIUM_FONT_LINUX_H_
#define PDF_PPAPI_MIGRATION_PDFIUM_FONT_LINUX_H_
#include "ppapi/c/pp_instance.h"
namespace blink {
struct WebFontDescription;
}
namespace pp {
class Instance;
@ -13,12 +15,26 @@ class Instance;
namespace chrome_pdf {
// Returns a handle to the font mapped based on `desc` and `charset`, for use
// as the font_id in GetPepperFontData() and DeletePepperFont() below. Returns
// nullptr on failure.
void* MapPepperFont(const blink::WebFontDescription& desc, int charset);
// Reads data from the `font_id` handle for `table` into a `buffer` of
// `buf_size`. Returns the amount of data read on success, or 0 on failure. If
// `buffer` is null, then just return the required size for the buffer.
unsigned long GetPepperFontData(void* font_id,
unsigned int table,
unsigned char* buffer,
unsigned long buf_size);
// Releases resources allocated by MapPepperFont().
void DeletePepperFont(void* font_id);
// Keeps track of the most recently used plugin instance. This is a no-op if
// `last_instance` is null.
void SetLastPepperInstance(pp::Instance* last_instance);
PP_Instance GetLastPepperInstance();
} // namespace chrome_pdf
#endif // PDF_PPAPI_MIGRATION_PDFIUM_FONT_LINUX_H_