diff --git a/core/fpdfdoc/cpdf_annotfontmap.cpp b/core/fpdfdoc/cpdf_annotfontmap.cpp index 10f271b38..775eb3462 100644 --- a/core/fpdfdoc/cpdf_annotfontmap.cpp +++ b/core/fpdfdoc/cpdf_annotfontmap.cpp @@ -611,9 +611,21 @@ CPDF_AnnotFontMap::PrepareFontResources() { if (font_obj_num != 0) { prepared.font_resources->SetNewFor(entry.alias, doc_, font_obj_num); - } else { - prepared.font_resources->SetFor(entry.alias, font_dict->Clone()); + continue; + } + + if (install_dr_entry_ && static_cast(i) == da_entry_ && + entry.source == Source::kDocumentFontDict) { + RetainPtr dr_fonts = GetDrFontDict(doc_); + if (dr_fonts && + dr_fonts->GetObjectFor(entry.alias.AsStringView()) == font_dict) { + prepared.inline_dr_alias = entry.alias; + prepared.font_resources->SetFor(entry.alias, + font_dict->CloneForHolder(doc_)); + continue; + } } + prepared.font_resources->SetFor(entry.alias, font_dict->Clone()); } return prepared; } @@ -623,6 +635,15 @@ RetainPtr CPDF_AnnotFontMap::PublishFontResources( if (!doc_ || !prepared.font_resources) { return nullptr; } + // Acrobat needs the /DR font to be indirect. Give the staged copy its + // number only when publishing, and share it with the form. + const ByteString& alias = prepared.inline_dr_alias; + if (!alias.IsEmpty()) { + prepared.font_resources->ConvertToIndirectObjectFor(alias, doc_); + RetainPtr font = + prepared.font_resources->GetDictFor(alias.AsStringView()); + InstallDrEntry(alias, font.Get()); + } bool da_published = false; for (PreparedFontResources::StagedEntry& staged : prepared.registered) { RetainPtr font_resource = diff --git a/core/fpdfdoc/cpdf_annotfontmap.h b/core/fpdfdoc/cpdf_annotfontmap.h index 264ffd606..2a29e7f80 100644 --- a/core/fpdfdoc/cpdf_annotfontmap.h +++ b/core/fpdfdoc/cpdf_annotfontmap.h @@ -101,6 +101,9 @@ class CPDF_AnnotFontMap final { // Direct. Foreign (document) fonts are already in it; registered fonts // are added at Publish. RetainPtr font_resources; + // A copy of the inline /DR font lives at this key until Publish gives it + // an object number and installs the reference in both dictionaries. + ByteString inline_dr_alias; std::vector registered; }; std::optional PrepareFontResources(); diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 2635fb7b5..c6a5cf5f6 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -365,7 +365,10 @@ a picture the button showed, so it is never written into, and the icon its resou with it. Before a form drawer replaces an appearance, a widget whose `/AP` is an indirect object gets a copy of its own (`OwnAppearanceDict`), and `epdf_annot.cpp` does the same for `/MK` and `/BS` before a setter writes one (`OwnMKForWriting` and `BorderStyleDictForWrite`, both through -`OwnDictEntry`): another annotation may name the same dictionary. A colour is +`OwnDictEntry`): another annotation may name the same dictionary. A `/DR` font that an appearance +written into the file names, when the file holds it inline (a dictionary inside `/DR`), gets +its object number in `CPDF_AnnotFontMap::PublishFontResources`. A failed appearance leaves it +inline. On success the form and appearance share the font so Acrobat displays the field. A colour is set as a new array, never written into the one the entry named, which may be an object another `/MK` names too. An `/MK` setter that changes nothing writes nothing, and it changes nothing only when the entry is well formed and reads as the value (three numbers from 0 to 1, a string, an @@ -379,7 +382,10 @@ upstream's commits to its generator. `epdf_widget_drawing_embeddertest.cpp` for its `AButtonIsSharedAsTheFormFillerSharesIt` runs upstream's form filler on the same buttons and compares the two. `CPDFGenerateAPTest.BorderArrayDrawsAsBorderStyleForEveryKind` draws every kind that has a border from both keys; the border setters' tests are in `epdf_annot_embeddertest.cpp` -(`ABorderCallThatChangesNothingWritesNothing`, `ASharedBorderStyleIsNotWrittenInto`). +(`ABorderCallThatChangesNothingWritesNothing`, `ASharedBorderStyleIsNotWrittenInto`). An inline +`/DR` font: `EPDFFormEmbedderTest.AFillMakesAnInlineFormFontAnObject` and +`AnInlineFormFontBecomesAnObjectInATransaction`. The failure path is covered by +`EPDFAnnotEmbedderTest.FailedFreeTextAppearanceKeepsFormFontInline`. **When upstream touches this.** A commit to `cpdf_generateap.*`: port it to ours or skip it, with the reason in the sync report. A new upstream caller of `CPDF_GenerateAP::`: route it to ours, or accept it in the report. A change to what `CPDF_IconFit`'s readers return (`GetScaleMethod`, diff --git a/fpdfsdk/epdf_annot_embeddertest.cpp b/fpdfsdk/epdf_annot_embeddertest.cpp index 8bd5ec5a5..e4f65fb49 100644 --- a/fpdfsdk/epdf_annot_embeddertest.cpp +++ b/fpdfsdk/epdf_annot_embeddertest.cpp @@ -3388,6 +3388,43 @@ std::vector WrapAsCollection(pdfium::span sfnt) { } // namespace +TEST_F(EPDFAnnotEmbedderTest, FailedFreeTextAppearanceKeepsFormFontInline) { + ASSERT_TRUE(OpenDocumentFromMemory(MakePdf({ + "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [] /DR << /Font " + "<< /Helv << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> " + ">> >> >> >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] " + "/Annots [4 0 R] >>", + "<< /Type /Annot /Subtype /FreeText /Rect [20 20 20 50] " + "/DA (/Helv 10 Tf 0 g) /Contents (Label) >>", + }))); + ScopedFPDFPage page(FPDF_LoadPage(document(), 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page.get(), 0)); + ASSERT_TRUE(annot); + RetainPtr font = GetDrFontEntry(document(), "Helv"); + ASSERT_TRUE(font); + ASSERT_TRUE(font->IsInline()); + const CPDF_Document* doc = CPDFDocumentFromFPDFDocument(document()); + const uint32_t last_obj = doc->GetLastObjNum(); + const std::string dr_before = SerializeDrDict(document()); + + EXPECT_FALSE(EPDFAnnot_GenerateAppearance(annot.get())); + EXPECT_EQ(dr_before, SerializeDrDict(document())); + EXPECT_EQ(last_obj, doc->GetLastObjNum()); + EXPECT_FALSE(FPDFAnnot_HasKey(annot.get(), "AP")); + + // With a drawable box, the appearance and /DR share the published font. + const FS_RECTF rect{20, 50, 180, 20}; + ASSERT_TRUE(FPDFAnnot_SetRect(annot.get(), &rect)); + ASSERT_TRUE(EPDFAnnot_GenerateAppearance(annot.get())); + font = GetDrFontEntry(document(), "Helv"); + ASSERT_TRUE(font); + EXPECT_FALSE(font->IsInline()); + EXPECT_EQ(font.Get(), GetAppearanceFontDict(annot.get(), "Helv").Get()); +} + // D8 (Phase C note ยง6): a font resource that fails to stage after another // was staged leaves the document exactly as it was. The FreeText below needs // two registered fonts (the /DA font and a CJK fallback); the injection makes diff --git a/fpdfsdk/epdf_form.cpp b/fpdfsdk/epdf_form.cpp index 72f517344..01b7120d1 100644 --- a/fpdfsdk/epdf_form.cpp +++ b/fpdfsdk/epdf_form.cpp @@ -426,7 +426,10 @@ void FinishTxnControl(const CPDF_Array* opt_array, // Resolve the widgets of a terminal field from its own dictionary, each // through the document so layer promotions win. Fails when a kid carries // /T: the target is a non-terminal field and value transactions must -// address terminal fields. +// address terminal fields. A field with no /Kids is its own widget only +// when it says so (/Subtype /Widget), as CPDF_InteractiveForm reads it; +// otherwise it has no widget yet (a field made before its widgets are +// attached): no control, so nothing is drawn on the field itself. bool CollectRawTxnControls(CPDF_Document* doc, const CPDF_Dictionary* field_dict, uint32_t field_objnum, @@ -440,6 +443,9 @@ bool CollectRawTxnControls(CPDF_Document* doc, RetainPtr kids = field_dict->GetArrayFor(pdfium::form_fields::kKids); if (!kids) { + if (field_dict->GetNameFor(pdfium::annotation::kSubtype) != "Widget") { + return true; + } TxnControl control; control.merged = true; control.objnum = field_objnum; diff --git a/fpdfsdk/epdf_form_embeddertest.cpp b/fpdfsdk/epdf_form_embeddertest.cpp index 4a5488728..0407a963c 100644 --- a/fpdfsdk/epdf_form_embeddertest.cpp +++ b/fpdfsdk/epdf_form_embeddertest.cpp @@ -14,9 +14,11 @@ #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/parser/cpdf_name.h" #include "core/fpdfapi/parser/cpdf_number.h" +#include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_string.h" #include "fpdfsdk/cpdfsdk_helpers.h" +#include "fpdfsdk/epdf_edit_test_util.h" #include "public/epdf_action.h" #include "public/epdf_annot.h" #include "public/epdf_layer.h" @@ -198,6 +200,16 @@ class EPDFFormEmbedderTest : public EmbedderTest { return false; } out->bytes = GetFileContents(file_path.c_str()); + return OpenLayerOverBytes(out); + } + + bool OpenLayerFromMemory(const std::string& contents, LayerDoc* out) { + out->bytes.assign(contents.begin(), contents.end()); + return OpenLayerOverBytes(out); + } + + private: + bool OpenLayerOverBytes(LayerDoc* out) { if (out->bytes.empty()) { return false; } @@ -739,6 +751,124 @@ TEST_F(EPDFFormEmbedderTest, TextValueOnLayerIsWrittenWhole) { EXPECT_EQ(promoted, EPDFLayer_GetPromotedObjectCount(doc.layer)); } +namespace { + +// A form of two text fields, objects 4 and 5, each its own widget, whose /DR +// font /Helv is written inline (a dictionary inside /DR), as hand-written +// forms often have it. +std::string FormWithInlineFont() { + return MakePdf({ + "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R 5 0 R] " + "/DA (/Helv 10 Tf 0 g) /DR << /Font << /Helv << /Type /Font " + "/Subtype /Type1 /BaseFont /Helvetica >> >> >> >> >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 300] " + "/Annots [4 0 R 5 0 R] >>", + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (first) " + "/Rect [20 240 380 260] /F 4 /P 3 0 R >>", + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (second) " + "/Rect [20 180 380 200] /F 4 /P 3 0 R >>", + }); +} + +// The object a font entry names: 0 when the font is written inline, or is +// not there. +uint32_t FontObjNum(const CPDF_Dictionary* fonts, const char* name) { + RetainPtr font = + fonts ? fonts->GetObjectFor(name) : nullptr; + const CPDF_Reference* ref = font ? font->AsReference() : nullptr; + return ref ? ref->GetRefObjNum() : 0; +} + +// The object /AcroForm /DR /Font names as `name` (the catalog is object 1). +uint32_t FormFontObjNum(FPDF_DOCUMENT document, const char* name) { + RetainPtr catalog = + GetEffectiveIndirectDictionary(document, 1u); + RetainPtr acroform = + catalog ? catalog->GetDictFor("AcroForm") : nullptr; + RetainPtr dr = + acroform ? acroform->GetDictFor("DR") : nullptr; + return FontObjNum(dr ? dr->GetDictFor("Font").Get() : nullptr, name); +} + +// The object the widget's normal appearance names as its font `name`. +uint32_t AppearanceFontObjNum(FPDF_DOCUMENT document, + uint32_t widget_object_number, + const char* name) { + RetainPtr widget = + GetEffectiveIndirectDictionary(document, widget_object_number); + RetainPtr appearance = + widget ? widget->GetDictFor("AP") : nullptr; + RetainPtr normal = + appearance ? appearance->GetStreamFor("N") : nullptr; + RetainPtr resources = + normal ? normal->GetDict()->GetDictFor("Resources") : nullptr; + return FontObjNum(resources ? resources->GetDictFor("Font").Get() : nullptr, + name); +} + +} // namespace + +// A form font written inline in /DR: the first fill makes it an object of its +// own, which /DR and the field's appearance both name, and the next fill names +// the same object. Acrobat shows no editable field whose appearance names a +// font that /DR holds inline. +TEST_F(EPDFFormEmbedderTest, AFillMakesAnInlineFormFontAnObject) { + ASSERT_TRUE(OpenDocumentFromMemory(FormWithInlineFont())); + ASSERT_EQ(0u, FormFontObjNum(document(), "Helv")); + + ASSERT_TRUE(embedpdf_test::FillTextField(document(), 4u, L"First")); + const uint32_t font = FormFontObjNum(document(), "Helv"); + ASSERT_NE(0u, font); + RetainPtr font_dict = + GetEffectiveIndirectDictionary(document(), font); + ASSERT_TRUE(font_dict); + EXPECT_EQ("Helvetica", font_dict->GetNameFor("BaseFont")); + EXPECT_EQ(font, AppearanceFontObjNum(document(), 4u, "Helv")); + EXPECT_NE(std::wstring::npos, + GetEffectiveWidgetAppearance(document(), 4u).find(L"First")); + + ASSERT_TRUE(embedpdf_test::FillTextField(document(), 5u, L"Second")); + EXPECT_EQ(font, FormFontObjNum(document(), "Helv")); + EXPECT_EQ(font, AppearanceFontObjNum(document(), 5u, "Helv")); + + // The saved file holds them so. + ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, FPDF_NO_INCREMENTAL)); + const std::string bytes = GetString(); + ScopedFPDFDocument saved(FPDF_LoadMemDocument( + bytes.data(), static_cast(bytes.size()), nullptr)); + ASSERT_TRUE(saved); + const uint32_t saved_font = FormFontObjNum(saved.get(), "Helv"); + EXPECT_NE(0u, saved_font); + EXPECT_EQ(saved_font, AppearanceFontObjNum(saved.get(), 4u, "Helv")); + EXPECT_EQ(saved_font, AppearanceFontObjNum(saved.get(), 5u, "Helv")); +} + +// The same in a layer transaction, as the engine writes. +TEST_F(EPDFFormEmbedderTest, AnInlineFormFontBecomesAnObjectInATransaction) { + LayerDoc doc; + ASSERT_TRUE(OpenLayerFromMemory(FormWithInlineFont(), &doc)); + ASSERT_TRUE(EPDFLayer_BeginTransaction(doc.layer)); + ASSERT_TRUE(embedpdf_test::FillTextField(doc.layer, 4u, L"First")); + ASSERT_TRUE(EPDFLayer_CommitTransaction(doc.layer)); + + const uint32_t font = FormFontObjNum(doc.layer, "Helv"); + ASSERT_NE(0u, font); + EXPECT_EQ(font, AppearanceFontObjNum(doc.layer, 4u, "Helv")); + EXPECT_TRUE(EPDFLayer_IsObjectPromoted(doc.layer, 1u)); // /AcroForm /DR +} + +// A form font that is an object of its own is named as it is, and /DR is not +// written. +TEST_F(EPDFFormEmbedderTest, AFormFontThatIsAnObjectIsNamedAsItIs) { + LayerDoc doc; + ASSERT_TRUE(OpenLayer("text_form.pdf", &doc)); // /DR 5, its /Font 6, /F1 7 + ASSERT_TRUE(embedpdf_test::FillTextField(doc.layer, 4u, L"Text")); + EXPECT_EQ(7u, AppearanceFontObjNum(doc.layer, 4u, "F1")); + EXPECT_FALSE(EPDFLayer_IsObjectPromoted(doc.layer, 5u)); + EXPECT_FALSE(EPDFLayer_IsObjectPromoted(doc.layer, 6u)); +} + TEST_F(EPDFFormEmbedderTest, SetFieldDisplayOnLayerIsDurable) { LayerDoc doc; ASSERT_TRUE(OpenLayer("text_form.pdf", &doc)); @@ -1435,6 +1565,38 @@ TEST_F(EPDFFormEmbedderTest, CreateUnplacedFieldBootstrapsAcroForm) { EPDFForm_CloseModel(model); } +// A field made before its widgets (the engine creates a field, sets its flags, +// then attaches its widgets) has nothing to draw on: a flag or a value drawn +// then puts no appearance on the field itself. Its widget, once attached, is +// drawn. +TEST_F(EPDFFormEmbedderTest, AFieldWithoutWidgetsGetsNoDrawing) { + ASSERT_TRUE(OpenDocument("hello_world.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + + const uint32_t field = EPDFForm_CreateField( + document(), 4 /* text */, GetFPDFWideString(L"notes").get(), 0); + ASSERT_GT(field, 0u); + ASSERT_TRUE(EPDFForm_SetFieldFlags(document(), field, + pdfium::form_flags::kReadOnly, 0)); + ASSERT_TRUE(embedpdf_test::FillTextField(document(), field, L"Unplaced")); + RetainPtr field_dict = + GetEffectiveIndirectDictionary(document(), field); + ASSERT_TRUE(field_dict); + EXPECT_FALSE(field_dict->KeyExist("AP")); + + const uint32_t widget = CreateWidgetAnnot(page, 20, 200, 180, 240); + ASSERT_GT(widget, 0u); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE(embedpdf_test::FillTextField(document(), field, L"Placed")); + field_dict = GetEffectiveIndirectDictionary(document(), field); + ASSERT_TRUE(field_dict); + EXPECT_FALSE(field_dict->KeyExist("AP")); + EXPECT_NE(std::wstring::npos, + GetEffectiveWidgetAppearance(document(), widget).find(L"Placed")); + UnloadPage(page); +} + TEST_F(EPDFFormEmbedderTest, AttachWidgetsFormsARadioGroup) { ASSERT_TRUE(OpenDocument("hello_world.pdf")); FPDF_PAGE page = LoadPage(0); diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 474b5cf50..e6a7f8591 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -97,6 +97,7 @@ EPDFAnnotEmbedderTest.DrawingRectIsWhereTheAppearanceIsDrawn 6 EPDFAnnotEmbedderTest.EmbedMetadata 1 EPDFAnnotEmbedderTest.EmbedSetRectPreservesRotatedAppearance 3 EPDFAnnotEmbedderTest.ExplicitGenerateAppearanceAllowed 2 +EPDFAnnotEmbedderTest.FailedFreeTextAppearanceKeepsFormFontInline 4 EPDFAnnotEmbedderTest.FontEmbeddingPolicyGovernsRegisteredResources 53 EPDFAnnotEmbedderTest.FreeTextAllFallbackTextKeepsRegisteredDaFont 18 EPDFAnnotEmbedderTest.FreeTextAppearanceFallsBackToRegisteredFont 13 @@ -226,6 +227,8 @@ EPDFFontEmbedderTest.UnauthorizedRegisteredFontDoesNotTakeOverABox 25 EPDFFontEmbedderTest.WidgetDrawsEachClusterInAFontThatHasIt 20 EPDFFontEmbedderTest.WidgetDrawsWhatNoFontHasWithTheLastResort 20 EPDFFontEmbedderTest.WidgetWritesWithADocumentProgram 52 +EPDFFormEmbedderTest.AFieldWithoutWidgetsGetsNoDrawing 7 +EPDFFormEmbedderTest.AFillMakesAnInlineFormFontAnObject 6 EPDFFormEmbedderTest.ANegativeWidgetRotationIsDrawnAsItsTurn 2 EPDFFormEmbedderTest.AttachToLegacyMergedFieldKeepsFieldId 6 EPDFFormEmbedderTest.AttachToSignatureFieldDrawsItsBox 7