diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 1cf240109..be7532f56 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -258,6 +258,12 @@ bool CPDF_Font::ShouldApplyGlyphSpacingHeuristic( } CFX_SubstFont* subst_font = current_font->GetSubstFont(); + // EmbedPDF(fonts): a registered fallback font is loaded from its own file, + // not through the font mapper, so it has no substitution record. It stands + // in for the font the document names. + if (!subst_font) { + return true; + } if (subst_font->IsBuiltInGenericFont()) { return false; } diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 49730a179..2635fb7b5 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -465,7 +465,8 @@ selective flattening. ### fonts **Rule.** Registered fonts, fallbacks, subsetting, CID-keyed CFF and font embedding policies. A standard font (the 14, on an ANSI code page) draws with PDFium's built-in face on every platform, -never a system font. +never a system font. A character the substitute for a font the document does not hold lacks is +drawn in the registered fallback font that has it, at the widths the document gives. **Why.** Text the user types must render and embed with the right font, and a document must draw the same in every engine, WebAssembly (which has no system fonts) included. **Where.** `cfx_fontregistry` (ours), `cpdf_font*`, `cfx_fontmapper.cpp`. Upstream's embedder tests @@ -480,6 +481,7 @@ which the rich text layout breaks a field's lines with; and `CFX_Face::LoadUnsca and `GetMappedCodePointRanges`, defined in our `cfx_glyphoutline.cpp` and `cfx_codepointrange.cpp`. **Guarded by.** The font embedder tests; `cfx_fontmapper_unittest.cpp` and `EPDFViewEmbedderTest.StandardFontsDrawWithBuiltInFaces` for standard fonts; +`EPDFAnnotEmbedderTest.RegisteredFallbackFont*` for the page's own text; `epdf_font_embeddertest.cpp` and `epdf_richtextlayout_embeddertest.cpp` (`FieldMatchesCpvt` for the line breaks) for a document's fonts and the layout. **When upstream touches this.** Font loading and mapping. A new upstream test that expects the test @@ -487,7 +489,9 @@ fonts for a standard font passes as it is; one of ours that expects the built-in switch off. Our font code calls FreeType directly (`CFX_Face::GetFsTypeFlags()`, CID-keyed CFF, the registry), so our builds keep `pdf_enable_freetype` on; a Fontations-only build would need Skrifa versions of them. When upstream changes CPVT's word breaking, the layout follows on purpose: -a field breaks its lines where upstream's would. Keep `font_index_` declared after +a field breaks its lines where upstream's would. A registered fallback font has no +`CFX_SubstFont`, so code that reads one from a font `GetFontFallback()` returns must allow for +null. Keep `font_index_` declared after `epdf_attachment_` in `CPDF_Document`, so the index goes before the attachment and the parser. ### measurement diff --git a/fpdfsdk/epdf_annot_embeddertest.cpp b/fpdfsdk/epdf_annot_embeddertest.cpp index c02c76d42..8bd5ec5a5 100644 --- a/fpdfsdk/epdf_annot_embeddertest.cpp +++ b/fpdfsdk/epdf_annot_embeddertest.cpp @@ -46,6 +46,7 @@ #include "core/fpdfdoc/cpdf_annotfontmap.h" #include "core/fpdfdoc/cpdf_annotfontsubset.h" #include "core/fpdfdoc/cpdf_richtextwriter.h" +#include "core/fxcrt/compiler_specific.h" #include "core/fxcrt/containers/contains.h" #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_safe_types.h" @@ -53,7 +54,11 @@ #include "core/fxcrt/span.h" #include "core/fxge/cfx_face.h" #include "core/fxge/cfx_font.h" +#include "core/fxge/cfx_fontmapper.h" +#include "core/fxge/cfx_fontmgr.h" #include "core/fxge/cfx_fontregistry.h" +#include "core/fxge/cfx_gemodule.h" +#include "core/fxge/systemfontinfo_iface.h" #include "fpdfsdk/cpdfsdk_helpers.h" #include "fpdfsdk/epdf_edit_test_util.h" #include "public/cpp/fpdf_scopers.h" @@ -247,6 +252,22 @@ class ScopedRegisteredFonts { ~ScopedRegisteredFonts() { EPDFFont_ClearRegisteredFonts(); } }; +// Substitutes from PDFium's built-in faces only, as WebAssembly does. +class ScopedNoSystemFonts { + public: + ScopedNoSystemFonts() : system_fonts_(mapper()->TakeSystemFontInfo()) {} + ~ScopedNoSystemFonts() { + mapper()->SetSystemFontInfo(std::move(system_fonts_)); + } + + private: + static CFX_FontMapper* mapper() { + return CFX_GEModule::Get()->GetFontMgr()->GetBuiltinMapper(); + } + + std::unique_ptr system_fonts_; +}; + using embedpdf_test::MemoryFileAccess; ByteString RegisteredFontAlias(EPDF_FONT_ID font_id) { @@ -552,6 +573,27 @@ std::string RichTextJsonFor(const char* family, int size, const char* text) { "},\"paragraphs\":[{\"runs\":[{\"text\":\"" + text + "\"}]}]}"; } +// Makes `page` show `text`, a string operand, at 72 points in the font +// `font_objnum`, from (40, 100). +void SetOneFontTextPageContent(CPDF_Document* cpdf_doc, + CPDF_Page* cpdf_page, + uint32_t font_objnum, + const char* text) { + RetainPtr page_dict = cpdf_page->GetMutableDict(); + RetainPtr resources = + page_dict->GetOrCreateDictFor("Resources"); + RetainPtr font_resources = + resources->GetOrCreateDictFor("Font"); + font_resources->SetNewFor("F1", cpdf_doc, font_objnum); + + const ByteString content = + ByteString::Format("BT\n/F1 72 Tf\n40 100 Td\n%s Tj\nET\n", text); + RetainPtr contents = + cpdf_doc->NewIndirect(content.unsigned_span()); + page_dict->SetNewFor("Contents", cpdf_doc, + contents->GetObjNum()); +} + void AddBrokenTrueTypeCjkTextPageContent(FPDF_DOCUMENT doc, FPDF_PAGE page) { CPDF_Document* cpdf_doc = CPDFDocumentFromFPDFDocument(doc); CPDF_Page* cpdf_page = CPDFPageFromFPDFPage(page); @@ -571,24 +613,75 @@ void AddBrokenTrueTypeCjkTextPageContent(FPDF_DOCUMENT doc, FPDF_PAGE page) { encoding_dict->SetFor("Differences", std::move(differences)); font_dict->SetFor("Encoding", std::move(encoding_dict)); - RetainPtr page_dict = cpdf_page->GetMutableDict(); - RetainPtr resources = - page_dict->GetOrCreateDictFor("Resources"); - RetainPtr font_resources = - resources->GetOrCreateDictFor("Font"); - font_resources->SetNewFor("F1", cpdf_doc, - font_dict->GetObjNum()); - - const ByteString kContent = - "BT\n" - "/F1 72 Tf\n" - "40 100 Td\n" - "<41> Tj\n" - "ET\n"; - RetainPtr contents = - cpdf_doc->NewIndirect(kContent.unsigned_span()); - page_dict->SetNewFor("Contents", cpdf_doc, - contents->GetObjNum()); + SetOneFontTextPageContent(cpdf_doc, cpdf_page, font_dict->GetObjNum(), + "<41>"); +} + +// Writes U+8FD9 on `page` in SimSun, a font the document names but does not +// hold, as a Type0 font of the Adobe-GB1 collection. `width` is the width the +// font gives the character, in thousandths of the font size. +void AddUnembeddedType0CjkTextPageContent(FPDF_DOCUMENT doc, + FPDF_PAGE page, + int width) { + CPDF_Document* cpdf_doc = CPDFDocumentFromFPDFDocument(doc); + CPDF_Page* cpdf_page = CPDFPageFromFPDFPage(page); + ASSERT_TRUE(cpdf_doc); + ASSERT_TRUE(cpdf_page); + + auto descriptor = cpdf_doc->NewIndirect(); + descriptor->SetNewFor("Type", "FontDescriptor"); + descriptor->SetNewFor("FontName", "SimSun"); + descriptor->SetNewFor("Flags", 32); + descriptor->SetNewFor("StemV", 0); + + auto cid_font = cpdf_doc->NewIndirect(); + cid_font->SetNewFor("Type", "Font"); + cid_font->SetNewFor("Subtype", "CIDFontType2"); + cid_font->SetNewFor("BaseFont", "SimSun"); + cid_font->SetNewFor("DW", width); + cid_font->SetNewFor("FontDescriptor", cpdf_doc, + descriptor->GetObjNum()); + auto system_info = cid_font->SetNewFor("CIDSystemInfo"); + system_info->SetNewFor("Registry", "Adobe"); + system_info->SetNewFor("Ordering", "GB1"); + system_info->SetNewFor("Supplement", 2); + + auto font_dict = cpdf_doc->NewIndirect(); + font_dict->SetNewFor("Type", "Font"); + font_dict->SetNewFor("Subtype", "Type0"); + font_dict->SetNewFor("BaseFont", "SimSun"); + font_dict->SetNewFor("Encoding", "UniGB-UCS2-H"); + auto descendants = font_dict->SetNewFor("DescendantFonts"); + descendants->AppendNew(cpdf_doc, cid_font->GetObjNum()); + + SetOneFontTextPageContent(cpdf_doc, cpdf_page, font_dict->GetObjNum(), + "<8FD9>"); +} + +// The same as a simple TrueType font. +void AddUnembeddedTrueTypeCjkTextPageContent(FPDF_DOCUMENT doc, + FPDF_PAGE page, + int width) { + CPDF_Document* cpdf_doc = CPDFDocumentFromFPDFDocument(doc); + CPDF_Page* cpdf_page = CPDFPageFromFPDFPage(page); + ASSERT_TRUE(cpdf_doc); + ASSERT_TRUE(cpdf_page); + + auto font_dict = cpdf_doc->NewIndirect(); + font_dict->SetNewFor("Type", "Font"); + font_dict->SetNewFor("Subtype", "TrueType"); + font_dict->SetNewFor("BaseFont", "SimSun"); + font_dict->SetNewFor("FirstChar", 65); + font_dict->SetNewFor("LastChar", 65); + font_dict->SetNewFor("Widths")->AppendNew(width); + auto encoding_dict = font_dict->SetNewFor("Encoding"); + encoding_dict->SetNewFor("Type", "Encoding"); + auto differences = encoding_dict->SetNewFor("Differences"); + differences->AppendNew(65); + differences->AppendNew("uni8FD9"); + + SetOneFontTextPageContent(cpdf_doc, cpdf_page, font_dict->GetObjNum(), + "<41>"); } // Applies the one REDACT annotation `annot` of `page`. @@ -5719,6 +5812,66 @@ TEST_F(EPDFAnnotEmbedderTest, RegisteredFallbackFontRendersPageMissingGlyph) { EXPECT_NE(without_registered_fallback, BitmapChecksum(bitmap.get())); } +// A font the document names but does not hold is drawn in the registered +// fallback font, at the widths the document gives: a glyph wider than its +// width is drawn narrower, so it stays off the next one. +TEST_F(EPDFAnnotEmbedderTest, RegisteredFallbackFontRendersUnembeddedFont) { + constexpr int kPageSize = 200; + auto painted_width = [](FPDF_BITMAP bitmap) { + int first = kPageSize; + int last = -1; + const int stride = FPDFBitmap_GetStride(bitmap); + auto pixels = UNSAFE_TODO( + pdfium::span(static_cast(FPDFBitmap_GetBuffer(bitmap)), + static_cast(stride) * kPageSize)); + for (int y = 0; y < kPageSize; ++y) { + for (int x = 0; x < kPageSize; ++x) { + if (pixels[y * stride + x * 4] != 0xFF) { + first = std::min(first, x); + last = std::max(last, x); + } + } + } + return last < first ? 0 : last - first + 1; + }; + + std::vector font_data = LoadNotoSansSCFontData(); + ASSERT_FALSE(font_data.empty()); + + for (bool type0 : {true, false}) { + SCOPED_TRACE(type0 ? "Type0" : "TrueType"); + auto render = [&](int width) { + embedpdf_test::ScopedTestDocument doc(NewTestDocument()); + ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, kPageSize, kPageSize)); + if (type0) { + AddUnembeddedType0CjkTextPageContent(doc.get(), page.get(), width); + } else { + AddUnembeddedTrueTypeCjkTextPageContent(doc.get(), page.get(), width); + } + page.reset(); + page.reset(FPDF_LoadPage(doc.get(), 0)); + return RenderPageOnWhite(page.get(), kPageSize, kPageSize); + }; + + ScopedNoSystemFonts no_system_fonts; + ScopedRegisteredFonts scoped_fonts; + EPDF_FONT_ID font_id = + EPDFFont_RegisterMemFont64("NotoSansSC", /*weight=*/400, /*italic=*/0, + font_data.data(), font_data.size()); + ASSERT_NE(0u, font_id); + ASSERT_TRUE(EPDFFont_AddFallbackFont(font_id, /*tier=*/0)); + + ScopedFPDFBitmap full = render(/*width=*/1000); + ScopedFPDFBitmap half = render(/*width=*/500); + const int full_width = painted_width(full.get()); + const int half_width = painted_width(half.get()); + EXPECT_GT(full_width, 50); + EXPECT_LE(full_width, 72); + EXPECT_GT(half_width, 20); + EXPECT_LE(half_width, 36); + } +} + TEST_F(EPDFAnnotEmbedderTest, EmbedMetadata) { ASSERT_TRUE(OpenDocument("text_form_multiple.pdf")); ScopedPage page = LoadScopedPage(0); diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 0d765ca09..474b5cf50 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -143,6 +143,7 @@ EPDFAnnotEmbedderTest.RefitWrapsAStampAcrobatTurnedUpright 5 EPDFAnnotEmbedderTest.RegisteredCffFontEmitsCidFontType0 13 EPDFAnnotEmbedderTest.RegisteredCidKeyedCffKeepsCidsInAppearance 13 EPDFAnnotEmbedderTest.RegisteredFallbackFontRendersPageMissingGlyph 5 +EPDFAnnotEmbedderTest.RegisteredFallbackFontRendersUnembeddedFont 7 EPDFAnnotEmbedderTest.RegisteredFontResolutionPrefersStoredFamily 9 EPDFAnnotEmbedderTest.RegisteredFontResolutionRefusesConflictingHint 6 EPDFAnnotEmbedderTest.RegistryHonoursFsTypeEmbeddingPermissions 13