0

[unseasoned-pdf] Implement Blink-based Linux font mapping.

Talk directly to FontService, instead of proxying the request through
PPAPI.

Bug: 1139079
Change-Id: I42627fc7ebdab74884cfd593ca1208cdbe78c754
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2853614
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: K. Moon <kmoon@chromium.org>
Reviewed-by: Dominik Röttsches <drott@chromium.org>
Cr-Commit-Position: refs/heads/master@{#894358}
This commit is contained in:
Lei Zhang
2021-06-21 19:46:30 +00:00
committed by Chromium LUCI CQ
parent e78cfb404f
commit bd7ca12e08
3 changed files with 93 additions and 8 deletions

@ -209,7 +209,10 @@ if (enable_pdf) {
"pdfium/pdfium_font_linux.h",
]
deps += [ ":font_table_linux" ]
deps += [
":font_table_linux",
"//components/services/font/public/cpp",
]
}
}

@ -1,4 +1,5 @@
include_rules = [
"+components/services/font/public/cpp",
"+third_party/pdfium/public",
"+ui/gfx/codec",
"+v8/include/cppgc/platform.h",

@ -4,15 +4,19 @@
#include "pdf/pdfium/pdfium_font_linux.h"
#include <algorithm>
#include <memory>
#include <string>
#include "base/files/file.h"
#include "base/i18n/encoding_detection.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/notreached.h"
#include "base/no_destructor.h"
#include "base/numerics/ranges.h"
#include "base/sequence_checker.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "components/services/font/public/cpp/font_loader.h"
#include "pdf/font_table_linux.h"
#include "pdf/pdfium/pdfium_engine.h"
#include "pdf/ppapi_migration/pdfium_font_linux.h"
#include "third_party/blink/public/platform/web_font_description.h"
@ -22,6 +26,85 @@ namespace chrome_pdf {
namespace {
// Maps font description and charset to `FontId` as requested by PDFium, with
// `FontId` as an opaque type that PDFium works with. Based on the `FontId`,
// PDFium can read from the font files using GetFontData(). Properly frees the
// underlying resource type when PDFium is done with the mapped font.
class BlinkFontMapper {
public:
// Defined as the type most convenient for use with PDFium's
// `FPDF_SYSFONTINFO` functions.
using FontId = void*;
BlinkFontMapper() = default;
~BlinkFontMapper() = delete;
// Returns a handle to the font mapped based on `desc` and `charset`, for use
// as the `font_id` in GetFontData() and DeleteFont() below. Returns nullptr
// on failure.
// TODO(thestig): Document how this handles TTC files.
FontId MapFont(const blink::WebFontDescription& desc, int charset) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto font_file = std::make_unique<base::File>();
// In RendererBlinkPlatform, SkFontConfigInterface::SetGlobal() only ever
// sets the global to a FontLoader. Thus it is safe to assume the returned
// result is just that.
auto* font_loader = reinterpret_cast<font_service::FontLoader*>(
SkFontConfigInterface::RefGlobal().get());
font_loader->MatchFontWithFallback(
desc.family.Utf8(),
desc.weight >= blink::WebFontDescription::kWeightBold, desc.italic,
charset, blink::WebFontDescription::kGenericFamilyStandard,
font_file.get());
if (!font_file->IsValid())
return nullptr;
// Release to PDFium. PDFium will free `font_file` in DeleteFont() below.
return font_file.release();
}
// Releases the font file that `font_id` points to.
void DeleteFont(FontId font_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
delete FileFromFontId(font_id);
}
// 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.
// See content::GetFontTable() for information on the `table_tag` parameter.
unsigned long GetFontData(FontId font_id,
unsigned int table_tag,
unsigned char* buffer,
unsigned long buf_size) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(thestig): cache?
base::PlatformFile platform_file =
FileFromFontId(font_id)->GetPlatformFile();
size_t size = buf_size;
if (!pdf::GetFontTable(platform_file, table_tag, /*offset=*/0, buffer,
&size)) {
return 0;
}
return size;
}
private:
static base::File* FileFromFontId(FontId font_id) {
return reinterpret_cast<base::File*>(font_id);
}
SEQUENCE_CHECKER(sequence_checker_);
};
BlinkFontMapper& GetBlinkFontMapper() {
static base::NoDestructor<BlinkFontMapper> mapper;
return *mapper;
}
bool UsePepperMapping() {
return PDFiumEngine::GetFontMappingMode() != FontMappingMode::kBlink;
}
@ -153,8 +236,7 @@ void* MapFont(FPDF_SYSFONTINFO*,
if (UsePepperMapping())
return MapPepperFont(desc, charset);
NOTIMPLEMENTED();
return nullptr;
return GetBlinkFontMapper().MapFont(desc, charset);
}
unsigned long GetFontData(FPDF_SYSFONTINFO*,
@ -165,8 +247,7 @@ unsigned long GetFontData(FPDF_SYSFONTINFO*,
if (UsePepperMapping())
return GetPepperFontData(font_id, table, buffer, buf_size);
NOTIMPLEMENTED();
return 0;
return GetBlinkFontMapper().GetFontData(font_id, table, buffer, buf_size);
}
void DeleteFont(FPDF_SYSFONTINFO*, void* font_id) {
@ -175,7 +256,7 @@ void DeleteFont(FPDF_SYSFONTINFO*, void* font_id) {
return;
}
NOTIMPLEMENTED();
GetBlinkFontMapper().DeleteFont(font_id);
}
FPDF_SYSFONTINFO g_font_info = {1, 0, EnumFonts, MapFont, 0,