From d3eb3513dcb3879fdab81d81ee9ddb68992af447 Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Tue, 1 Dec 2026 11:48:53 +0200 Subject: [PATCH 1/3] Give a widget joining a group its /Opt value, and read which groups have the list A checkbox or radio group may name its values in a list in the field (/Opt), as Acrobat writes values that repeat or aren't plain names: a widget's value is the list's entry at its place among the field's widgets, and its on-state name (/0, /1) only a key. EPDFForm_AttachWidget appended the new widget to /Kids but wrote no entry, so it read back Yes and never went on with its value. EPDFForm_DetachWidget left the list as it was, so every widget after the one that left read the value of the widget before it. EPDFForm_AttachWidget takes an export value before split_objnum (NULL: the on-state name). When the field has a list, its own or a parent's, the field gets a direct list of its own with the value at the new widget's place; a place the list lacked before it gets that widget's on-state name, its value without a list. A list that is an object of its own, or a parent's, is copied, never written. Without a list, a value other than the on-state is refused and nothing is written; a value for a field that isn't a checkbox or radio group is refused. EPDFForm_DetachWidget takes the widget's entry out of the list. Places are counted as CPDF_InteractiveForm counts a field's controls (/Kids entries that are widget dictionaries). EPDFForm_HasFieldValuesList(model, field_index): the form model records whether a checkbox or radio group has such a list. The engine could only guess before, from a widget's value differing from its on-state name, which misses a list that repeats the names (/Opt [(0) (1)]) or names Yes (/Opt [(Yes)]). Tests: eight new EPDFFormEmbedderTest cases on testing/resources/toggle_values_list.in (a group /Opt [(pick-a) (pick-b)] with states /0 /1, a unison group whose list is an object of its own, a merged checkbox whose value is not Latin), one of them in PDFium's own form filler on the saved file. The seven attach and detach tests fail with the writes switched off. All eight are in the layer-mode writers list. Every other attach in the tests passes nullptr. --- fpdfsdk/epdf_annot_embeddertest.cpp | 22 +- fpdfsdk/epdf_c_api_test.c | 1 + fpdfsdk/epdf_font_embeddertest.cpp | 10 +- fpdfsdk/epdf_form.cpp | 122 +++++++ fpdfsdk/epdf_form_embeddertest.cpp | 347 ++++++++++++++++++- fpdfsdk/epdf_object_number_embeddertest.cpp | 10 +- fpdfsdk/epdf_signature_embeddertest.cpp | 5 +- fpdfsdk/epdf_widget_drawing_embeddertest.cpp | 3 +- public/epdf_form.h | 25 +- testing/embedpdf_layer_mode_writers.txt | 8 + testing/resources/toggle_values_list.in | 153 ++++++++ testing/resources/toggle_values_list.pdf | Bin 0 -> 3053 bytes 12 files changed, 669 insertions(+), 37 deletions(-) create mode 100644 testing/resources/toggle_values_list.in create mode 100644 testing/resources/toggle_values_list.pdf diff --git a/fpdfsdk/epdf_annot_embeddertest.cpp b/fpdfsdk/epdf_annot_embeddertest.cpp index 271a464ef..8319c90b1 100644 --- a/fpdfsdk/epdf_annot_embeddertest.cpp +++ b/fpdfsdk/epdf_annot_embeddertest.cpp @@ -2594,8 +2594,9 @@ TEST_F(EPDFAnnotEmbedderTest, WidgetRegisteredFontInstallsRealDrEntry) { const uint32_t field = EPDFForm_CreateField( document(), 4 /* EPDF_FORMFIELD_FAMILY_TEXT */, field_name.get(), 0); ASSERT_GT(field, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget( - document(), field, EPDFAnnot_GetObjectNumber(annot.get()), nullptr, 0)); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, + EPDFAnnot_GetObjectNumber(annot.get()), + nullptr, nullptr, 0)); ASSERT_TRUE(embedpdf_test::FillTextField(document(), field, L"hello")); ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(annot.get())); @@ -3644,7 +3645,7 @@ TEST_F(EPDFAnnotEmbedderTest, FontEmbeddingPolicyGovernsRegisteredResources) { ASSERT_GT(field, 0u); ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, EPDFAnnot_GetObjectNumber(widget.get()), - nullptr, 0)); + nullptr, nullptr, 0)); ASSERT_TRUE(embedpdf_test::FillTextField(document(), field, L"hello")); ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(widget.get())); EXPECT_TRUE(AppearanceFontHasEmbeddedSubset( @@ -5632,8 +5633,9 @@ TEST_F(EPDFAnnotEmbedderTest, TextFieldKoreanUsesRegisteredDroidFallbackFont) { const uint32_t field = EPDFForm_CreateField( document(), 4 /* EPDF_FORMFIELD_FAMILY_TEXT */, field_name.get(), 0); ASSERT_GT(field, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget( - document(), field, EPDFAnnot_GetObjectNumber(annot.get()), nullptr, 0)); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, + EPDFAnnot_GetObjectNumber(annot.get()), + nullptr, nullptr, 0)); ASSERT_TRUE( embedpdf_test::FillTextField(document(), field, L"\xD55C\xAE00")); @@ -5670,8 +5672,9 @@ TEST_F(EPDFAnnotEmbedderTest, ComboBoxKoreanUsesRegisteredDroidFallbackFont) { EPDFForm_CreateField(document(), 5 /* EPDF_FORMFIELD_FAMILY_COMBOBOX */, field_name.get(), 0); ASSERT_GT(field, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget( - document(), field, EPDFAnnot_GetObjectNumber(annot.get()), nullptr, 0)); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, + EPDFAnnot_GetObjectNumber(annot.get()), + nullptr, nullptr, 0)); ScopedFPDFWideString latin_option = GetFPDFWideString(L"Latin"); ScopedFPDFWideString korean_option = GetFPDFWideString(L"\xD55C\xAE00"); @@ -5710,8 +5713,9 @@ TEST_F(EPDFAnnotEmbedderTest, ListBoxKoreanUsesRegisteredDroidFallbackFont) { const uint32_t field = EPDFForm_CreateField( document(), 6 /* EPDF_FORMFIELD_FAMILY_LISTBOX */, field_name.get(), 0); ASSERT_GT(field, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget( - document(), field, EPDFAnnot_GetObjectNumber(annot.get()), nullptr, 0)); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, + EPDFAnnot_GetObjectNumber(annot.get()), + nullptr, nullptr, 0)); ScopedFPDFWideString latin_option = GetFPDFWideString(L"Latin"); ScopedFPDFWideString korean_option = GetFPDFWideString(L"\xD55C\xAE00"); diff --git a/fpdfsdk/epdf_c_api_test.c b/fpdfsdk/epdf_c_api_test.c index 0c89a5f79..4b81fcc18 100644 --- a/fpdfsdk/epdf_c_api_test.c +++ b/fpdfsdk/epdf_c_api_test.c @@ -402,6 +402,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFForm_GetFormKind); CHK(EPDFForm_GetNeedAppearances); CHK(EPDFForm_HasFieldEmbedMetadata); + CHK(EPDFForm_HasFieldValuesList); CHK(EPDFForm_IsFieldOptionSelected); CHK(EPDFForm_IsFieldWidgetChecked); CHK(EPDFForm_LoadModel); diff --git a/fpdfsdk/epdf_font_embeddertest.cpp b/fpdfsdk/epdf_font_embeddertest.cpp index cfaef1d45..04f2bde3f 100644 --- a/fpdfsdk/epdf_font_embeddertest.cpp +++ b/fpdfsdk/epdf_font_embeddertest.cpp @@ -316,8 +316,9 @@ class EPDFFontEmbedderTest : public EmbedderTest { const uint32_t field = EPDFForm_CreateField( document, 4 /* EPDF_FORMFIELD_FAMILY_TEXT */, field_name.get(), 0); EXPECT_GT(field, 0u); - EXPECT_TRUE(EPDFForm_AttachWidget( - document, field, EPDFAnnot_GetObjectNumber(annot), nullptr, 0)); + EXPECT_TRUE(EPDFForm_AttachWidget(document, field, + EPDFAnnot_GetObjectNumber(annot), nullptr, + nullptr, 0)); EXPECT_TRUE(embedpdf_test::FillTextField(document, field, value)); EXPECT_TRUE(EPDFAnnot_GenerateFormFieldAP(annot)); return field; @@ -1790,8 +1791,9 @@ TEST_F(EPDFFontEmbedderTest, PasswordFieldWritesOnlyStars) { const uint32_t field = EPDFForm_CreateField( document(), 4 /* EPDF_FORMFIELD_FAMILY_TEXT */, name.get(), 0); ASSERT_GT(field, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget( - document(), field, EPDFAnnot_GetObjectNumber(annot.get()), nullptr, 0)); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, + EPDFAnnot_GetObjectNumber(annot.get()), + nullptr, nullptr, 0)); ASSERT_TRUE(EPDFForm_SetFieldFlags(document(), field, 1u << 13, 0)); ASSERT_TRUE(embedpdf_test::FillTextField(document(), field, L"hunter2")); ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(annot.get())); diff --git a/fpdfsdk/epdf_form.cpp b/fpdfsdk/epdf_form.cpp index 01b7120d1..f4a4e4e70 100644 --- a/fpdfsdk/epdf_form.cpp +++ b/fpdfsdk/epdf_form.cpp @@ -96,6 +96,8 @@ struct FieldRecord { RetainPtr embed_metadata; FieldValueRecord value; FieldValueRecord default_value; + // A checkbox or radio group whose widgets' values a list (/Opt) names. + bool values_listed = false; std::vector options; std::vector widgets; std::array actions; @@ -274,6 +276,11 @@ FieldRecord SnapshotField( } } + if (IsToggleFamily(record.family)) { + record.values_listed = + !!ToArray(CPDF_FormField::GetFieldAttrForDict(field_dict, "Opt")); + } + const int control_count = field->CountControls(); record.widgets.reserve(control_count); for (int i = 0; i < control_count; ++i) { @@ -1409,6 +1416,12 @@ EPDFForm_HasFieldEmbedMetadata(EPDF_FORM_MODEL model, int field_index) { return field && field->embed_metadata; } +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV +EPDFForm_HasFieldValuesList(EPDF_FORM_MODEL model, int field_index) { + const FieldRecord* field = GetFieldRecord(model, field_index); + return field && field->values_listed; +} + FPDF_EXPORT unsigned long FPDF_CALLCONV EPDFForm_GetFieldEmbedMetadataString(EPDF_FORM_MODEL model, int field_index, @@ -2464,6 +2477,61 @@ void BakeWidgetAppearance(CPDF_Document* doc, } } +// The widgets of a terminal field, in the order of its /Kids: the places a +// checkbox's or radio group's values list (/Opt) names, counted as +// CPDF_InteractiveForm counts a field's controls (a kid that is no widget +// dictionary holds no place). +std::vector> FieldWidgetKids( + CPDF_Document* doc, + const CPDF_Dictionary* field) { + std::vector> widgets; + RetainPtr kids = + field->GetArrayFor(pdfium::form_fields::kKids); + if (!kids) { + return widgets; + } + for (size_t i = 0; i < kids->size(); ++i) { + RetainPtr element = kids->GetObjectAt(i); + const CPDF_Reference* ref = element ? element->AsReference() : nullptr; + RetainPtr kid = + ref ? ToDictionary(doc->GetOrParseIndirectObject(ref->GetRefObjNum())) + : ToDictionary(element); + if (kid && kid->GetNameFor(pdfium::annotation::kSubtype) == "Widget") { + widgets.push_back(std::move(kid)); + } + } + return widgets; +} + +// The values list (/Opt) of the toggle field |field|, |list|, as the field's +// own direct array, to write. A list that is an object of its own, or a +// parent's, stays as it is for whoever else names it: the field gets a copy. +// The result has an entry for each of |widgets|: an entry the list lacks is +// the on-state name of the widget at that place, which is that widget's +// value without a list. +RetainPtr OwnToggleValuesList( + CPDF_Dictionary* field, + const CPDF_Array* list, + const std::vector>& widgets) { + RetainPtr own = ToArray(field->GetMutableObjectFor("Opt")); + if (!own) { + own = pdfium::MakeRetain(); + for (size_t i = 0; i < list->size(); ++i) { + RetainPtr entry = list->GetDirectObjectAt(i); + if (entry) { + own->Append(entry->Clone()); + } else { + own->AppendNew(); + } + } + field->SetFor("Opt", own); + } + for (size_t i = own->size(); i < widgets.size(); ++i) { + own->AppendNew(ReadWidgetOnState(widgets[i].Get())); + } + return own; +} + } // namespace FPDF_EXPORT uint32_t FPDF_CALLCONV @@ -2592,6 +2660,7 @@ EPDFForm_AttachWidget(FPDF_DOCUMENT document, uint32_t field_objnum, uint32_t widget_objnum, FPDF_BYTESTRING on_state, + FPDF_WIDESTRING export_value, uint32_t split_objnum) { ScopedFPDFDocumentView document_view(document); CPDF_Document* doc = document_view.Get(); @@ -2612,6 +2681,22 @@ EPDFForm_AttachWidget(FPDF_DOCUMENT document, if (toggle && (state.IsEmpty() || state == kOffState)) { return false; // toggles need a real on-state name } + if (export_value && !toggle) { + return false; // only a checkbox's or radio button's value is its own + } + // A list in the field (/Opt) names each toggle widget's value by its place: + // the new widget's entry is |export_value|, or its on-state name. Without a + // list the value is the on-state name, so a value that differs can't be + // kept. + RetainPtr values_list = + toggle ? ToArray(CPDF_FormField::GetFieldAttrForDict(field.Get(), "Opt")) + : nullptr; + const WideString new_value = + export_value ? WideStringFromFPDFWideString(export_value) : WideString(); + if (export_value && !values_list && + new_value != PDF_DecodeText(state.unsigned_span())) { + return false; + } RetainPtr widget = ToDictionary(doc->GetOrParseIndirectObject(widget_objnum)); @@ -2703,6 +2788,21 @@ EPDFForm_AttachWidget(FPDF_DOCUMENT document, return false; } kids->AppendNew(doc, widget_objnum); + // The new widget is the last of the field's widgets: its entry goes at that + // place, and a place the list lacks before it is filled. + const std::vector> widgets = + values_list ? FieldWidgetKids(doc, mutable_field.Get()) + : std::vector>(); + if (!widgets.empty()) { + RetainPtr own = + OwnToggleValuesList(mutable_field.Get(), values_list.Get(), widgets); + const size_t place = widgets.size() - 1; + if (export_value) { + own->SetNewAt(place, new_value.AsStringView()); + } else { + own->SetNewAt(place, state); + } + } if (toggle) { mutable_widget->SetNewFor("AS", kOffState); @@ -2741,6 +2841,24 @@ EPDFForm_DetachWidget(FPDF_DOCUMENT document, if (!ArrayReferencesDict(kids.Get(), widget_objnum, widget.Get())) { return false; } + // A checkbox's or radio group's values list (/Opt) names each widget's + // value by its place: the widget's entry goes with it, so the widgets after + // it keep theirs. + RetainPtr values_list = + IsToggleField(field.Get()) + ? ToArray(CPDF_FormField::GetFieldAttrForDict(field.Get(), "Opt")) + : nullptr; + std::optional listed_place; + if (values_list) { + const std::vector> widgets = + FieldWidgetKids(doc, field.Get()); + for (size_t i = 0; i < widgets.size() && i < values_list->size(); ++i) { + if (widgets[i]->GetObjNum() == widget_objnum) { + listed_place = i; + break; + } + } + } RetainPtr mutable_field = ToDictionary(doc->GetMutableIndirectObject(field_objnum)); @@ -2756,6 +2874,10 @@ EPDFForm_DetachWidget(FPDF_DOCUMENT document, !RemoveObjNumFromMutableArray(mutable_kids.Get(), widget_objnum)) { return false; } + if (listed_place.has_value()) { + OwnToggleValuesList(mutable_field.Get(), values_list.Get(), {}) + ->RemoveAt(listed_place.value()); + } // An empty /Kids array would make CPDF_InteractiveForm skip the field // entirely; drop the key so the field stays visible as "unplaced". if (mutable_kids->IsEmpty()) { diff --git a/fpdfsdk/epdf_form_embeddertest.cpp b/fpdfsdk/epdf_form_embeddertest.cpp index e5e05a529..35f399939 100644 --- a/fpdfsdk/epdf_form_embeddertest.cpp +++ b/fpdfsdk/epdf_form_embeddertest.cpp @@ -1535,6 +1535,39 @@ uint32_t CreateWidgetAnnot(FPDF_PAGE page, return EPDFAnnot_GetObjectNumber(annot.get()); } +// The entries of a toggle field's own values list (/Opt), as text. +std::vector OwnValuesList(FPDF_DOCUMENT document, + uint32_t field_objnum) { + std::vector entries; + RetainPtr field = + GetEffectiveIndirectDictionary(document, field_objnum); + RetainPtr list = + field ? field->GetArrayFor("Opt") : nullptr; + for (size_t i = 0; list && i < list->size(); ++i) { + const WideString entry = list->GetUnicodeTextAt(i); + entries.emplace_back(entry.c_str(), entry.GetLength()); + } + return entries; +} + +// Each widget's export value and on-state name of the field named |name|. +std::vector> ToggleWidgets( + FPDF_DOCUMENT document, + const wchar_t* name) { + std::vector> widgets; + EPDF_FORM_MODEL model = EPDFForm_LoadModel(document); + const int index = model ? FieldIndexByName(model, name) : -1; + for (int i = 0; index >= 0 && i < EPDFForm_CountFieldWidgets(model, index); + ++i) { + widgets.emplace_back(GetWidgetExportValue(model, index, i), + GetWidgetOnState(model, index, i)); + } + if (model) { + EPDFForm_CloseModel(model); + } + return widgets; +} + } // namespace TEST_F(EPDFFormEmbedderTest, CreateUnplacedFieldBootstrapsAcroForm) { @@ -1589,7 +1622,8 @@ TEST_F(EPDFFormEmbedderTest, AFieldWithoutWidgetsGetsNoDrawing) { 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( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); ASSERT_TRUE(embedpdf_test::FillTextField(document(), field, L"Placed")); field_dict = GetEffectiveIndirectDictionary(document(), field); ASSERT_TRUE(field_dict); @@ -1612,12 +1646,15 @@ TEST_F(EPDFFormEmbedderTest, AttachWidgetsFormsARadioGroup) { ASSERT_GT(w1, 0u); ASSERT_GT(w2, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, w1, "male", 0)); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, w2, "female", 0)); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, w1, "male", nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, w2, "female", nullptr, 0)); // Re-attaching an already attached widget fails. - EXPECT_FALSE(EPDFForm_AttachWidget(document(), field, w1, "male", 0)); + EXPECT_FALSE( + EPDFForm_AttachWidget(document(), field, w1, "male", nullptr, 0)); // Toggles demand a usable on-state name. - EXPECT_FALSE(EPDFForm_AttachWidget(document(), field, w1, nullptr, 0)); + EXPECT_FALSE( + EPDFForm_AttachWidget(document(), field, w1, nullptr, nullptr, 0)); EPDF_FORM_MODEL model = EPDFForm_LoadModel(document()); ASSERT_TRUE(model); @@ -1642,6 +1679,273 @@ TEST_F(EPDFFormEmbedderTest, AttachWidgetsFormsARadioGroup) { UnloadPage(page); } +using ToggleWidgetList = std::vector>; + +// The model says which checkbox or radio group names its values in a list +// (/Opt), also when the list repeats the on-state names, so its values read +// the same either way; a choice field's options are no such list. +TEST_F(EPDFFormEmbedderTest, ModelTellsAGroupWhoseValuesAListNames) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + { + RetainPtr field = + GetMutableIndirectDictionary(document(), 4u); + ASSERT_TRUE(field); + auto list = field->SetNewFor("Opt"); + list->AppendNew("0"); + list->AppendNew("1"); + } + EPDF_FORM_MODEL model = EPDFForm_LoadModel(document()); + ASSERT_TRUE(model); + for (const wchar_t* name : {L"choice", L"unison", L"agree"}) { + SCOPED_TRACE(testing::Message() << name); + const int index = FieldIndexByName(model, name); + ASSERT_GE(index, 0); + EXPECT_TRUE(EPDFForm_HasFieldValuesList(model, index)); + } + const int choice = FieldIndexByName(model, L"choice"); + EXPECT_EQ(L"0", GetWidgetExportValue(model, choice, 0)); + EXPECT_EQ("0", GetWidgetOnState(model, choice, 0)); + EXPECT_FALSE(EPDFForm_HasFieldValuesList(model, -1)); + EPDFForm_CloseModel(model); + CloseDocument(); + + ASSERT_TRUE(OpenDocument("toggle_fields.pdf")); + model = EPDFForm_LoadModel(document()); + ASSERT_TRUE(model); + EXPECT_FALSE(EPDFForm_HasFieldValuesList( + model, FieldIndexByName(model, L"ntto_radio"))); + EXPECT_TRUE(EPDFForm_HasFieldValuesList( + model, FieldIndexByName(model, L"opt_check"))); + EPDFForm_CloseModel(model); + CloseDocument(); + + ASSERT_TRUE(OpenDocument("combobox_form.pdf")); + model = EPDFForm_LoadModel(document()); + ASSERT_TRUE(model); + ASSERT_GT(EPDFForm_CountFields(model), 0); + for (int i = 0; i < EPDFForm_CountFields(model); ++i) { + EXPECT_FALSE(EPDFForm_HasFieldValuesList(model, i)); + } + EPDFForm_CloseModel(model); +} + +// A radio group whose values a list in the field names (/Opt, as Acrobat +// writes repeated or non-Latin values): a new button's value is the list's +// entry at its place, so attaching writes it there. The on-state name is the +// caller's key: a button that repeats one shares its name. +TEST_F(EPDFFormEmbedderTest, AWidgetJoiningAListedGroupGetsItsValue) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + ASSERT_EQ((ToggleWidgetList{{L"pick-a", "0"}, {L"pick-b", "1"}}), + ToggleWidgets(document(), L"choice")); + + const uint32_t repeat = CreateWidgetAnnot(page, 20, 100, 40, 120); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, repeat, "0", + GetFPDFWideString(L"pick-a").get(), 0)); + const uint32_t added = CreateWidgetAnnot(page, 60, 100, 80, 120); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, added, "2", + GetFPDFWideString(L"pick-c").get(), 0)); + // No value: the on-state name is the value, as without a list. + const uint32_t named = CreateWidgetAnnot(page, 100, 100, 120, 120); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, named, "3", nullptr, 0)); + + EXPECT_EQ((ToggleWidgetList{{L"pick-a", "0"}, + {L"pick-b", "1"}, + {L"pick-a", "0"}, + {L"pick-c", "2"}, + {L"3", "3"}}), + ToggleWidgets(document(), L"choice")); + EXPECT_EQ((std::vector{L"pick-a", L"pick-b", L"pick-a", + L"pick-c", L"3"}), + OwnValuesList(document(), 4u)); + UnloadPage(page); +} + +// A list that is an object of its own may be named by more than the field: +// it stays as it is, and the field gets a list of its own. +TEST_F(EPDFFormEmbedderTest, AListThatIsAnObjectStaysAsItIs) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + + const uint32_t widget = CreateWidgetAnnot(page, 100, 200, 120, 220); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 8u, widget, "0", + GetFPDFWideString(L"same").get(), 0)); + + EXPECT_EQ((ToggleWidgetList{{L"same", "0"}, {L"same", "0"}, {L"same", "0"}}), + ToggleWidgets(document(), L"unison")); + RetainPtr field = + GetEffectiveIndirectDictionary(document(), 8u); + ASSERT_TRUE(field); + RetainPtr list = field->GetObjectFor("Opt"); + ASSERT_TRUE(list); + ASSERT_TRUE(list->IsArray()); // direct, the field's own + EXPECT_EQ(3u, list->AsArray()->size()); + RetainPtr shared = ToArray( + CPDFDocumentFromFPDFDocument(document())->GetOrParseIndirectObject(7u)); + ASSERT_TRUE(shared); + EXPECT_EQ(2u, shared->size()); + UnloadPage(page); +} + +// A list with fewer entries than the field has widgets: each place it lacks +// before the new widget's gets that widget's on-state name, its value +// without a list. +TEST_F(EPDFFormEmbedderTest, AShortListIsFilledBeforeTheNewPlace) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + { + RetainPtr field = + GetMutableIndirectDictionary(document(), 4u); + ASSERT_TRUE(field); + auto list = field->SetNewFor("Opt"); + list->AppendNew("pick-a"); + } + ASSERT_EQ(L"Yes", ToggleWidgets(document(), L"choice")[1].first); + + const uint32_t widget = CreateWidgetAnnot(page, 20, 100, 40, 120); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, widget, "2", + GetFPDFWideString(L"pick-c").get(), 0)); + EXPECT_EQ((std::vector{L"pick-a", L"1", L"pick-c"}), + OwnValuesList(document(), 4u)); + EXPECT_EQ((ToggleWidgetList{{L"pick-a", "0"}, {L"1", "1"}, {L"pick-c", "2"}}), + ToggleWidgets(document(), L"choice")); + UnloadPage(page); +} + +// Without a list a widget's value is its on-state name: a value that differs +// can't be kept, and the call writes nothing. Only a toggle takes a value. +TEST_F(EPDFFormEmbedderTest, AFieldWithoutAListTakesNoOtherValue) { + ASSERT_TRUE(OpenDocument("hello_world.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + const uint32_t radio = EPDFForm_CreateField( + document(), 3 /* radio */, GetFPDFWideString(L"gender").get(), 0); + ASSERT_GT(radio, 0u); + const uint32_t widget = CreateWidgetAnnot(page, 20, 200, 40, 220); + ASSERT_GT(widget, 0u); + + EXPECT_FALSE(EPDFForm_AttachWidget(document(), radio, widget, "male", + GetFPDFWideString(L"female").get(), 0)); + RetainPtr field_dict = + GetEffectiveIndirectDictionary(document(), radio); + ASSERT_TRUE(field_dict); + EXPECT_FALSE(field_dict->KeyExist("Kids")); + EXPECT_FALSE(GetEffectiveIndirectDictionary(document(), widget) + ->KeyExist(pdfium::form_fields::kParent)); + + ASSERT_TRUE(EPDFForm_AttachWidget(document(), radio, widget, "male", + GetFPDFWideString(L"male").get(), 0)); + field_dict = GetEffectiveIndirectDictionary(document(), radio); + EXPECT_FALSE(field_dict->KeyExist("Opt")); + EXPECT_EQ((ToggleWidgetList{{L"male", "male"}}), + ToggleWidgets(document(), L"gender")); + + const uint32_t text = EPDFForm_CreateField( + document(), 4 /* text */, GetFPDFWideString(L"name").get(), 0); + const uint32_t box = CreateWidgetAnnot(page, 20, 100, 180, 140); + EXPECT_FALSE(EPDFForm_AttachWidget(document(), text, box, nullptr, + GetFPDFWideString(L"x").get(), 0)); + UnloadPage(page); +} + +// A checkbox merged with its widget splits when a second widget joins it; +// the widget it was keeps the first entry of the list, and the new one +// shares its on-state name and its value. +TEST_F(EPDFFormEmbedderTest, AListedMergedCheckboxSplitsAndKeepsItsValue) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + const std::wstring agree = L"Ja \x2713"; + ASSERT_EQ((ToggleWidgetList{{agree, "0"}}), + ToggleWidgets(document(), L"agree")); + + const uint32_t widget = CreateWidgetAnnot(page, 60, 150, 80, 170); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 11u, widget, "0", + GetFPDFWideString(agree).get(), 0)); + RetainPtr field = + GetEffectiveIndirectDictionary(document(), 11u); + ASSERT_TRUE(field); + RetainPtr kids = field->GetArrayFor("Kids"); + ASSERT_TRUE(kids); + EXPECT_EQ(2u, kids->size()); + EXPECT_EQ(widget, kids->GetObjectAt(1)->AsReference()->GetRefObjNum()); + EXPECT_EQ((ToggleWidgetList{{agree, "0"}, {agree, "0"}}), + ToggleWidgets(document(), L"agree")); + EXPECT_EQ((std::vector{agree, agree}), + OwnValuesList(document(), 11u)); + UnloadPage(page); +} + +// A widget leaving a listed field takes its entry with it: the widgets after +// it keep their values. +TEST_F(EPDFFormEmbedderTest, ADetachedWidgetTakesItsListEntry) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + const uint32_t widget = CreateWidgetAnnot(page, 20, 100, 40, 120); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, widget, "2", + GetFPDFWideString(L"pick-c").get(), 0)); + + ASSERT_TRUE(EPDFForm_DetachWidget(document(), 4u, 5u)); + EXPECT_EQ((std::vector{L"pick-b", L"pick-c"}), + OwnValuesList(document(), 4u)); + EXPECT_EQ((ToggleWidgetList{{L"pick-b", "1"}, {L"pick-c", "2"}}), + ToggleWidgets(document(), L"choice")); + + // A list that is an object of its own stays as it is. + ASSERT_TRUE(EPDFForm_DetachWidget(document(), 8u, 9u)); + EXPECT_EQ((std::vector{L"same"}), + OwnValuesList(document(), 8u)); + RetainPtr shared = ToArray( + CPDFDocumentFromFPDFDocument(document())->GetOrParseIndirectObject(7u)); + ASSERT_TRUE(shared); + EXPECT_EQ(2u, shared->size()); + UnloadPage(page); +} + +// In the saved file, PDFium's own form filler switches a new button of a +// radio group in unison with the buttons of its value, as it reads the value +// from the list: a press on the new one turns all three on. +TEST_F(EPDFFormEmbedderTest, AFormFillerSwitchesANewListedButtonInUnison) { + ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + const uint32_t widget = CreateWidgetAnnot(page, 100, 200, 120, 220); + ASSERT_TRUE(EPDFForm_AttachWidget(document(), 8u, widget, "0", + GetFPDFWideString(L"same").get(), 0)); + UnloadPage(page); + ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, FPDF_NO_INCREMENTAL)); + const std::string bytes = GetString(); + + ScopedFPDFDocument saved( + FPDF_LoadMemDocument(bytes.data(), bytes.size(), nullptr)); + ASSERT_TRUE(saved); + FPDF_FORMHANDLE form = SetupFormFillEnvironment( + saved.get(), JavaScriptOption::kDisableJavaScript); + ASSERT_TRUE(form); + { + ScopedFPDFPage saved_page(FPDF_LoadPage(saved.get(), 0)); + ASSERT_TRUE(saved_page); + FORM_OnAfterLoadPage(saved_page.get(), form); + FORM_OnMouseMove(form, saved_page.get(), 0, 110, 210); + FORM_OnLButtonDown(form, saved_page.get(), 0, 110, 210); + FORM_OnLButtonUp(form, saved_page.get(), 0, 110, 210); + FORM_OnBeforeClosePage(saved_page.get(), form); + } + CPDF_Document* saved_doc = CPDFDocumentFromFPDFDocument(saved.get()); + for (uint32_t button : {9u, 10u, widget}) { + SCOPED_TRACE(button); + RetainPtr dict = + ToDictionary(saved_doc->GetOrParseIndirectObject(button)); + ASSERT_TRUE(dict); + EXPECT_EQ("0", dict->GetNameFor("AS")); + } + FPDFDOC_ExitFormFillEnvironment(form); +} + TEST_F(EPDFFormEmbedderTest, AttachToSignatureFieldDrawsItsBox) { ASSERT_TRUE(OpenDocument("hello_world.pdf")); FPDF_PAGE page = LoadPage(0); @@ -1659,7 +1963,8 @@ TEST_F(EPDFFormEmbedderTest, AttachToSignatureFieldDrawsItsBox) { ASSERT_TRUE(EPDFAnnot_SetMKColor(annot.get(), EPDF_MK_COLOR_BG, 255, 0, 0)); ASSERT_TRUE(EPDFAnnot_SetMKColor(annot.get(), EPDF_MK_COLOR_BC, 0, 0, 255)); } - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); // The box: the background filled, the border stroked inside it. const std::wstring box = GetEffectiveWidgetAppearance(document(), widget); @@ -1681,7 +1986,8 @@ TEST_F(EPDFFormEmbedderTest, SignatureFieldBoxRedrawsInItsNewStyle) { ASSERT_GT(field, 0u); 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( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); // No colours: an empty box. EXPECT_EQ(std::wstring::npos, GetEffectiveWidgetAppearance(document(), widget).find(L"rg")); @@ -1733,7 +2039,8 @@ TEST_F(EPDFFormEmbedderTest, AttachToLegacyMergedFieldKeepsFieldId) { // maxlen_text (object 4) is a merged field/widget. const uint32_t widget = CreateWidgetAnnot(page, 20, 20, 280, 36); ASSERT_GT(widget, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), 4u, widget, nullptr, nullptr, 0)); EPDF_FORM_MODEL model = EPDFForm_LoadModel(document()); ASSERT_TRUE(model); @@ -1765,7 +2072,8 @@ TEST_F(EPDFFormEmbedderTest, DetachWidgetKeepsFieldVisible) { const uint32_t field = EPDFForm_CreateField(document(), 4, GetFPDFWideString(L"note").get(), 0); const uint32_t widget = CreateWidgetAnnot(page, 20, 200, 200, 220); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); ASSERT_TRUE(EPDFForm_DetachWidget(document(), field, widget)); // Detaching twice fails (no longer attached). EXPECT_FALSE(EPDFForm_DetachWidget(document(), field, widget)); @@ -1789,7 +2097,8 @@ TEST_F(EPDFFormEmbedderTest, DeleteFieldDetachesAndPrunesAncestors) { const uint32_t field = EPDFForm_CreateField( document(), 4, GetFPDFWideString(L"billing.name").get(), 0); const uint32_t widget = CreateWidgetAnnot(page, 20, 200, 200, 220); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); uint32_t detached[4] = {}; unsigned long detached_count = 0; @@ -1958,7 +2267,8 @@ TEST_F(EPDFFormEmbedderTest, AuthoringOnLayerIsDurable) { ASSERT_TRUE(page); const uint32_t widget = CreateWidgetAnnot(page, 20, 200, 200, 220); ASSERT_GT(widget, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget(doc.layer, field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(doc.layer, field, widget, nullptr, nullptr, 0)); FPDF_ClosePage(page); // Duplicate create fails without growing the delta. @@ -2360,7 +2670,8 @@ TEST_F(EPDFFormEmbedderTest, SplitKeepsEachPlanesActions) { ASSERT_TRUE(page); const uint32_t widget = CreateWidgetAnnot(page, 20, 100, 280, 130); ASSERT_GT(widget, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), 4u, widget, nullptr, nullptr, 0)); UnloadPage(page); EXPECT_EQ(L"formatted = 1;", @@ -2425,7 +2736,8 @@ TEST_F(EPDFFormEmbedderTest, NewFieldAppearanceIsAFormXObject) { ASSERT_GT(field, 0u); const uint32_t widget = CreateWidgetAnnot(page, 20, 200, 180, 230); ASSERT_GT(widget, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); ASSERT_TRUE(SetOneValue(document(), field, L"Hello")); unsigned long changed_count = 0; ASSERT_TRUE(EPDFForm_RedrawFieldWidgets(document(), field, nullptr, 0, @@ -2494,7 +2806,8 @@ TEST_F(EPDFFormEmbedderTest, RedrawIsANewStreamEveryTime) { CreateWidgetAnnot(page, 20, bottom, 180, bottom + 30); ASSERT_GT(widget, 0u); bottom += 40; - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); if (family != EPDF_FORMFIELD_FAMILY_TEXT) { ASSERT_TRUE( EPDFForm_SetFieldOptions(document(), field, options, options, 2)); @@ -2539,7 +2852,8 @@ TEST_F(EPDFFormEmbedderTest, RedrawLeavesASharedAppearanceAlone) { CreateWidgetAnnot(page, 20, 240, 180, 270)}; for (uint32_t widget : widgets) { ASSERT_GT(widget, 0u); - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); } // One stream, and one /AP naming it that both widgets name. @@ -2619,7 +2933,8 @@ TEST_F(EPDFFormEmbedderTest, PushButtonIsAuthorableAndShowsItsCaption) { EXPECT_FALSE(EPDFAnnot_SetMKTextPosition(annot.get(), 7)); ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(annot.get(), 0)); } - ASSERT_TRUE(EPDFForm_AttachWidget(document(), field, widget, nullptr, 0)); + ASSERT_TRUE( + EPDFForm_AttachWidget(document(), field, widget, nullptr, nullptr, 0)); EPDF_FORM_MODEL model = EPDFForm_LoadModel(document()); ASSERT_TRUE(model); diff --git a/fpdfsdk/epdf_object_number_embeddertest.cpp b/fpdfsdk/epdf_object_number_embeddertest.cpp index c0e68b26b..e42fa2d24 100644 --- a/fpdfsdk/epdf_object_number_embeddertest.cpp +++ b/fpdfsdk/epdf_object_number_embeddertest.cpp @@ -540,7 +540,7 @@ TEST_F(EPDFObjectNumberEmbedderTest, FieldAndWidgetTakeTheChosenNumbers) { EXPECT_EQ(10u, CreateTextField(doc.get(), L"billing.name", 10)); EXPECT_EQ(kRaised + 1, ReferenceIn(doc.get(), 10, "Parent")); // The field isn't merged: the split number goes unused and stays free. - ASSERT_TRUE(EPDFForm_AttachWidget(doc.get(), 10, 11, nullptr, 12)); + ASSERT_TRUE(EPDFForm_AttachWidget(doc.get(), 10, 11, nullptr, nullptr, 12)); EXPECT_EQ(12u, CreateAnnot(doc.get(), 0, 12)); ASSERT_TRUE(EPDFLayer_CommitTransaction(doc.get())); @@ -556,13 +556,15 @@ TEST_F(EPDFObjectNumberEmbedderTest, MergedFieldSplitsToTheChosenNumber) { ASSERT_TRUE(doc); ASSERT_TRUE(EPDFLayer_BeginTransaction(doc.get())); ASSERT_TRUE(CreateWidget(doc.get(), 0, 11)); + EXPECT_FALSE(EPDFForm_AttachWidget(doc.get(), kMergedField, 11, nullptr, + nullptr, kSecondPage)); EXPECT_FALSE( - EPDFForm_AttachWidget(doc.get(), kMergedField, 11, nullptr, kSecondPage)); - EXPECT_FALSE(EPDFForm_AttachWidget(doc.get(), kMergedField, 11, nullptr, 11)); + EPDFForm_AttachWidget(doc.get(), kMergedField, 11, nullptr, nullptr, 11)); EXPECT_THAT(Annots(doc.get(), 0), ElementsAre(kMergedField, 11u)); EXPECT_THAT(WidgetsOf(doc.get(), kMergedField), ElementsAre(kMergedField)); - ASSERT_TRUE(EPDFForm_AttachWidget(doc.get(), kMergedField, 11, nullptr, 12)); + ASSERT_TRUE( + EPDFForm_AttachWidget(doc.get(), kMergedField, 11, nullptr, nullptr, 12)); ASSERT_TRUE(EPDFLayer_CommitTransaction(doc.get())); EXPECT_THAT(WidgetsOf(doc.get(), kMergedField), ElementsAre(12u, 11u)); EXPECT_THAT(Annots(doc.get(), 0), UnorderedElementsAre(11u, 12u)); diff --git a/fpdfsdk/epdf_signature_embeddertest.cpp b/fpdfsdk/epdf_signature_embeddertest.cpp index a3b7acbd3..094700593 100644 --- a/fpdfsdk/epdf_signature_embeddertest.cpp +++ b/fpdfsdk/epdf_signature_embeddertest.cpp @@ -658,7 +658,7 @@ uint32_t CreateSignatureField(FPDF_DOCUMENT doc, FPDF_PAGE page, const wchar_t* ADD_FAILURE() << "widget has no object number"; return 0; } - if (!EPDFForm_AttachWidget(doc, field, widget_objnum, nullptr, 0)) { + if (!EPDFForm_AttachWidget(doc, field, widget_objnum, nullptr, nullptr, 0)) { ADD_FAILURE() << "EPDFForm_AttachWidget failed for widget " << widget_objnum; return 0; } @@ -936,7 +936,8 @@ TEST_F(EPDFSignatureEmbedderTest, EXPECT_TRUE(EPDFAnnot_SetRect(widget.get(), &rect)); const unsigned int number = EPDFAnnot_GetObjectNumber(widget.get()); widget.reset(); - EXPECT_TRUE(EPDFForm_AttachWidget(document(), field, number, nullptr, 0)); + EXPECT_TRUE( + EPDFForm_AttachWidget(document(), field, number, nullptr, nullptr, 0)); return number; }; const unsigned int first_button = new_button(L"initials", 50); diff --git a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp index 188badaf4..836251652 100644 --- a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp +++ b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp @@ -747,7 +747,8 @@ class EPDFWidgetDrawingEmbedderTest : public EmbedderTest { } number = EPDFAnnot_GetObjectNumber(widget.get()); } - EXPECT_TRUE(EPDFForm_AttachWidget(document, field, number, on_state, 0)); + EXPECT_TRUE( + EPDFForm_AttachWidget(document, field, number, on_state, nullptr, 0)); return ScopedFPDFAnnotation(EPDFPage_GetAnnotByObjectNumber(page, number)); } diff --git a/public/epdf_form.h b/public/epdf_form.h index 032a87add..fa2560275 100644 --- a/public/epdf_form.h +++ b/public/epdf_form.h @@ -298,6 +298,15 @@ EPDFForm_IsFieldOptionSelected(EPDF_FORM_MODEL model, int field_index, int option_index); +// Experimental EmbedPDF Extension API. +// Return whether a checkbox or radio group names its widgets' values in a +// list (/Opt, its own or a parent's), as Acrobat does for values that repeat +// or aren't plain names: each widget's value is then the list's entry at its +// place among the field's widgets (EPDFForm_GetFieldWidgetExportValue()), and +// its on-state name only a key. False for other families. +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV +EPDFForm_HasFieldValuesList(EPDF_FORM_MODEL model, int field_index); + // Experimental EmbedPDF Extension API. // Return the number of widget annotations bound to the field. A merged // field/widget dictionary counts as one widget whose object number equals @@ -649,18 +658,32 @@ EPDFForm_CreateField(FPDF_DOCUMENT document, // the object number that new kid gets, or 0 for the next free one; it is used // only when the field is merged. A number other than 0 needs a layer // transaction and must be free (EPDFLayer_RaiseLastObjectNumber()). +// +// |export_value| is a checkbox's or radio button's value, or NULL for its +// on-state name. A toggle field may name its widgets' values in a list +// (/Opt, its own or a parent's), as Acrobat does for repeated or non-Latin +// values: a widget's value is then the list's entry at its place among the +// field's widgets, and its on-state name only a key, which widgets of one +// value share to go on together. The field then gets a list of its own with +// |export_value| at the new widget's place; a place the list lacked before +// it gets that widget's on-state name, its value without a list. For a field +// without a list, an |export_value| other than |on_state| fails. NULL for +// other families. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFForm_AttachWidget(FPDF_DOCUMENT document, uint32_t field_objnum, uint32_t widget_objnum, FPDF_BYTESTRING on_state, + FPDF_WIDESTRING export_value, uint32_t split_objnum); // Experimental EmbedPDF Extension API. // Detach a widget from its field. The widget keeps its page placement and // last appearance but becomes an ordinary inert annotation (deletable via // the annotation APIs). The field survives, "unplaced" when this was its -// last widget. +// last widget. A checkbox's or radio group's values list (/Opt, see +// EPDFForm_AttachWidget()) loses the widget's entry, so the widgets after it +// keep their values; the field then has a list of its own. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFForm_DetachWidget(FPDF_DOCUMENT document, uint32_t field_objnum, diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index c215a4930..84a47c1ca 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -228,9 +228,16 @@ EPDFFontEmbedderTest.UnauthorizedRegisteredFontDoesNotTakeOverABox 27 EPDFFontEmbedderTest.WidgetDrawsEachClusterInAFontThatHasIt 22 EPDFFontEmbedderTest.WidgetDrawsWhatNoFontHasWithTheLastResort 22 EPDFFontEmbedderTest.WidgetWritesWithADocumentProgram 58 +EPDFFormEmbedderTest.ADetachedWidgetTakesItsListEntry 8 +EPDFFormEmbedderTest.AFieldWithoutAListTakesNoOtherValue 10 EPDFFormEmbedderTest.AFieldWithoutWidgetsGetsNoDrawing 8 EPDFFormEmbedderTest.AFillMakesAnInlineFormFontAnObject 6 +EPDFFormEmbedderTest.AFormFillerSwitchesANewListedButtonInUnison 5 +EPDFFormEmbedderTest.AListThatIsAnObjectStaysAsItIs 5 +EPDFFormEmbedderTest.AListedMergedCheckboxSplitsAndKeepsItsValue 6 EPDFFormEmbedderTest.ANegativeWidgetRotationIsDrawnAsItsTurn 3 +EPDFFormEmbedderTest.AShortListIsFilledBeforeTheNewPlace 5 +EPDFFormEmbedderTest.AWidgetJoiningAListedGroupGetsItsValue 11 EPDFFormEmbedderTest.AttachToLegacyMergedFieldKeepsFieldId 7 EPDFFormEmbedderTest.AttachToSignatureFieldDrawsItsBox 7 EPDFFormEmbedderTest.AttachWidgetsFormsARadioGroup 11 @@ -253,6 +260,7 @@ EPDFFormEmbedderTest.GenerateFormFieldAPRefusesAnotherSubtype 2 EPDFFormEmbedderTest.ListBoxTopIndexIsRead 1 EPDFFormEmbedderTest.ListBoxValuesAreAnArray 1 EPDFFormEmbedderTest.MalformedDefaultReadsAsUnsupported 1 +EPDFFormEmbedderTest.ModelTellsAGroupWhoseValuesAListNames 1 EPDFFormEmbedderTest.NewFieldAppearanceIsAFormXObject 8 EPDFFormEmbedderTest.NoneShadowsAParentFieldsValue 2 EPDFFormEmbedderTest.PushButtonIsAuthorableAndShowsItsCaption 8 diff --git a/testing/resources/toggle_values_list.in b/testing/resources/toggle_values_list.in new file mode 100644 index 000000000..86314e689 --- /dev/null +++ b/testing/resources/toggle_values_list.in @@ -0,0 +1,153 @@ +{{header}} +{{object 1 0}} << + /Type /Catalog + /Pages 2 0 R + /AcroForm << + /Fields [4 0 R 8 0 R 11 0 R] + /DR << /Font << /Helv 15 0 R /ZaDb 16 0 R >> >> + /DA (/Helv 0 Tf 0 g) + >> +>> +endobj +{{object 2 0}} << + /Type /Pages + /Count 1 + /Kids [3 0 R] +>> +endobj +{{object 3 0}} << + /Type /Page + /Parent 2 0 R + /MediaBox [0 0 300 300] + /Annots [5 0 R 6 0 R 9 0 R 10 0 R 11 0 R] +>> +endobj +% A radio group as Acrobat writes one whose values are no names: the values +% are a list in the field (/Opt), and each button's on-state name (/0, /1) +% is its place (Ff = radio 32768 + no-toggle-to-off 16384). +{{object 4 0}} << + /FT /Btn + /T (choice) + /Ff 49152 + /Opt [(pick-a) (pick-b)] + /V /1 + /Kids [5 0 R 6 0 R] +>> +endobj +{{object 5 0}} << + /Type /Annot + /Subtype /Widget + /Parent 4 0 R + /P 3 0 R + /F 4 + /AS /Off + /Rect [20 250 40 270] + /MK << /CA (l) >> + /AP << /N << /0 13 0 R /Off 14 0 R >> >> +>> +endobj +{{object 6 0}} << + /Type /Annot + /Subtype /Widget + /Parent 4 0 R + /P 3 0 R + /F 4 + /AS /1 + /Rect [60 250 80 270] + /MK << /CA (l) >> + /AP << /N << /1 13 0 R /Off 14 0 R >> >> +>> +endobj +% The list of a radio group in unison, as an object of its own. +{{object 7 0}} [(same) (same)] +endobj +% A radio group in unison (Ff = radio 32768 + no-toggle-to-off 16384 + +% radios-in-unison 33554432): its two buttons have one value and share the +% on-state /0, so they go on together. +{{object 8 0}} << + /FT /Btn + /T (unison) + /Ff 33603584 + /Opt 7 0 R + /V /Off + /Kids [9 0 R 10 0 R] +>> +endobj +{{object 9 0}} << + /Type /Annot + /Subtype /Widget + /Parent 8 0 R + /P 3 0 R + /F 4 + /AS /Off + /Rect [20 200 40 220] + /MK << /CA (l) >> + /AP << /N << /0 13 0 R /Off 14 0 R >> >> +>> +endobj +{{object 10 0}} << + /Type /Annot + /Subtype /Widget + /Parent 8 0 R + /P 3 0 R + /F 4 + /AS /Off + /Rect [60 200 80 220] + /MK << /CA (l) >> + /AP << /N << /0 13 0 R /Off 14 0 R >> >> +>> +endobj +% A checkbox merged with its widget, whose value is no Latin name +% (Ja and a check mark, U+2713, in UTF-16BE), so Acrobat names it in a list. +{{object 11 0}} << + /Type /Annot + /Subtype /Widget + /FT /Btn + /T (agree) + /Opt [] + /V /Off + /AS /Off + /P 3 0 R + /F 4 + /Rect [20 150 40 170] + /MK << /CA (4) >> + /AP << /N << /0 13 0 R /Off 14 0 R >> >> +>> +endobj +{{object 13 0}} << + /Type /XObject + /Subtype /Form + /BBox [0 0 20 20] + {{streamlen}} +>> +stream +0 g 5 5 10 10 re f +endstream +endobj +{{object 14 0}} << + /Type /XObject + /Subtype /Form + /BBox [0 0 20 20] + {{streamlen}} +>> +stream +q Q +endstream +endobj +{{object 15 0}} << + /Type /Font + /Subtype /Type1 + /BaseFont /Helvetica + /Encoding /WinAnsiEncoding +>> +endobj +{{object 16 0}} << + /Type /Font + /Subtype /Type1 + /BaseFont /ZapfDingbats +>> +endobj +{{xref}} +{{trailer}} +{{startxref}} +%%EOF diff --git a/testing/resources/toggle_values_list.pdf b/testing/resources/toggle_values_list.pdf new file mode 100644 index 0000000000000000000000000000000000000000..210ca96ba727e1148c1b89376405a88032c3b2bc GIT binary patch literal 3053 zcmcIm+j8PY5PjEI^a~YJ2BXmp*m7KjFH|bK8#^|eO|8ohgBXd4@eGj$vzxEUL%uMT z4@h^5iwu6SQx3*RYW4J-={bF-8&~JUwm9l~jX(eX>mN^m4|e<;93OiCozb_sg3g(A za%OWfT*+LO5TMgFJr2^s4sG#JxuapI)ofD2ZA2c><6l8BukX+w9nP=O7yYbpeDi~v zJ%WhYvGYZqk3l5-aB>1CC#x|7IOwpW52Fm<=1qKoZ}F?l1mSvwXcsnPC6s4&fwKv^ zeXj{!NHg5khLhpB;WXq*77B-N*8EXTv^=$6;nv4dq0j%cy3W8D>o9z^nraz+nhFHD z;$@YIE$#*kppX-7AusG=4zdKw=~y~g7TRI1tx>R?+ET%zoGs9RaKKnF@<2ZTMA2`+?cJoV#WVq?v${=@iBb=dAgTqPI&Y9Y(}(=;ybfBbpdWm!NSi z^O;Q1KFr|z70xi|COtSr2yJKce5UZywpoVz7WSg%kw;s<&E;?eol|EhJtH_sr&gzm zcCUjWqrQj(x<_lmL`CZ@0f;4U51mt10Z%I6y;lGiF9F_w5mJx2W;2jaSd8Rb7+qR} zltaQAS|Mc-9f?$SX^y<^YT>sBB~lM}oiBImg5E8RO~_Y}5e|{aTwP^bn|1{W!Z41b zC=8nK7>rxmRXLO}m5+)Gj!TWprJPc|VU=O1O|ej!l$NaDAh&1-ECq#+#gRvvx82aE z^sf-C=^uuPAI3fA8&v>hgjE$ezv5oS`nrbHX;1d;R4=AItMV&oj|xY$58jgYEQtRz zc49taC+5E?cA{oFRq6d04}k|&j!flr7|)>6EpWlbHg_M~&8m!i^3v9FiWf7eTv)ZNwoddd1ioZhHC zwIyUY-5k{<;T4BJD3jxUi5|58r7M(t@UW|Z82^x&_{EyfNQzc3S6v2c_!~_g*5>{l zetu@M=T0^1o1QF_s-D&URF;anD(;k&)2SrK3zOQ3HaT=YYcnvVUK?uN4|iO<%X-O7 zU*tSH$Kcqml(l$Y3zcC$Mc4B;A4uXjj3I-Kjha6CfZ5nYAGK-679`j-?bzZz9;`dI zuD?IF*Wa5%hzJkjr(*>Q@t!S8P>*-~l6Ze?zqdCBkJ?#p5CnUegD~2|97OTnyg{7Q r#=1i4nJTcwp#8nJ7F*l($#J9qP?UG3wJaR(M36+G*JxZ^4!wT?^^OY2 literal 0 HcmV?d00001 From ead95f3a8c28cc13890021071f55896849864038 Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Tue, 1 Dec 2026 12:06:04 +0200 Subject: [PATCH 2/3] Refuse an empty /Opt value, and keep what Acrobat reads in a short list A review of pull request 139 found two things in the attach. An empty export value was written as () in the field's list. An empty entry reads back as another value (PDFium's GetExportValue() gives Yes), so the widget never had the value asked for. EPDFForm_AttachWidget now refuses an empty export value before it writes anything. NULL still means the on-state name. A list may have fewer entries than the field has widgets. The attach fills each place the list lacks with that widget's on-state name. Acrobat reads a widget the list has no entry for by its on-state name (checked in Acrobat 26.002: a group cut to /Opt [(pick-a)] reads its second button as 1), as the fork's value transactions do, so the value Acrobat shows stays the same. GetExportValue() reads Yes there, which is why the review saw the value change; filling with Yes would change it in Acrobat instead. The comment and the header now say so. Tests: AWidgetJoiningAListedGroupGetsItsValue refuses an empty value and checks nothing was written. AShortListIsFilledBeforeTheNewPlace checks that the button that is on stays on with its value, after the attach and after the new widget leaves again. --- fpdfsdk/epdf_form.cpp | 8 ++++++-- fpdfsdk/epdf_form_embeddertest.cpp | 30 +++++++++++++++++++++++++++--- public/epdf_form.h | 7 ++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/fpdfsdk/epdf_form.cpp b/fpdfsdk/epdf_form.cpp index f4a4e4e70..4be6c33a0 100644 --- a/fpdfsdk/epdf_form.cpp +++ b/fpdfsdk/epdf_form.cpp @@ -2507,8 +2507,9 @@ std::vector> FieldWidgetKids( // own direct array, to write. A list that is an object of its own, or a // parent's, stays as it is for whoever else names it: the field gets a copy. // The result has an entry for each of |widgets|: an entry the list lacks is -// the on-state name of the widget at that place, which is that widget's -// value without a list. +// the on-state name of the widget at that place, the value Acrobat reads for +// a widget the list has no entry for, so the value it shows stays the same. +// (CPDF_FormControl::GetExportValue() reads "Yes" there.) RetainPtr OwnToggleValuesList( CPDF_Dictionary* field, const CPDF_Array* list, @@ -2693,6 +2694,9 @@ EPDFForm_AttachWidget(FPDF_DOCUMENT document, : nullptr; const WideString new_value = export_value ? WideStringFromFPDFWideString(export_value) : WideString(); + if (export_value && new_value.IsEmpty()) { + return false; // an empty entry reads back as another value ("Yes") + } if (export_value && !values_list && new_value != PDF_DecodeText(state.unsigned_span())) { return false; diff --git a/fpdfsdk/epdf_form_embeddertest.cpp b/fpdfsdk/epdf_form_embeddertest.cpp index 35f399939..1b843e260 100644 --- a/fpdfsdk/epdf_form_embeddertest.cpp +++ b/fpdfsdk/epdf_form_embeddertest.cpp @@ -1749,6 +1749,12 @@ TEST_F(EPDFFormEmbedderTest, AWidgetJoiningAListedGroupGetsItsValue) { // No value: the on-state name is the value, as without a list. const uint32_t named = CreateWidgetAnnot(page, 100, 100, 120, 120); ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, named, "3", nullptr, 0)); + // An empty value reads back as another one: refused, nothing written. + const uint32_t empty = CreateWidgetAnnot(page, 140, 100, 160, 120); + EXPECT_FALSE(EPDFForm_AttachWidget(document(), 4u, empty, "4", + GetFPDFWideString(L"").get(), 0)); + EXPECT_FALSE(GetEffectiveIndirectDictionary(document(), empty) + ->KeyExist(pdfium::form_fields::kParent)); EXPECT_EQ((ToggleWidgetList{{L"pick-a", "0"}, {L"pick-b", "1"}, @@ -1789,9 +1795,12 @@ TEST_F(EPDFFormEmbedderTest, AListThatIsAnObjectStaysAsItIs) { UnloadPage(page); } -// A list with fewer entries than the field has widgets: each place it lacks -// before the new widget's gets that widget's on-state name, its value -// without a list. +// A list with fewer entries than the field has widgets. Acrobat reads a +// widget the list has no entry for by its on-state name, where +// CPDF_FormControl::GetExportValue() reads "Yes": each place the list lacks +// before the new widget's gets that widget's on-state name, so the value +// Acrobat shows stays the same, for the button that is on too, and after the +// new widget leaves again. TEST_F(EPDFFormEmbedderTest, AShortListIsFilledBeforeTheNewPlace) { ASSERT_TRUE(OpenDocument("toggle_values_list.pdf")); FPDF_PAGE page = LoadPage(0); @@ -1804,6 +1813,13 @@ TEST_F(EPDFFormEmbedderTest, AShortListIsFilledBeforeTheNewPlace) { list->AppendNew("pick-a"); } ASSERT_EQ(L"Yes", ToggleWidgets(document(), L"choice")[1].first); + const auto expect_second_on = [this] { + EXPECT_EQ("1", + GetEffectiveIndirectDictionary(document(), 4u)->GetNameFor("V")); + EXPECT_EQ("1", + GetEffectiveIndirectDictionary(document(), 6u)->GetNameFor("AS")); + }; + expect_second_on(); const uint32_t widget = CreateWidgetAnnot(page, 20, 100, 40, 120); ASSERT_TRUE(EPDFForm_AttachWidget(document(), 4u, widget, "2", @@ -1812,6 +1828,14 @@ TEST_F(EPDFFormEmbedderTest, AShortListIsFilledBeforeTheNewPlace) { OwnValuesList(document(), 4u)); EXPECT_EQ((ToggleWidgetList{{L"pick-a", "0"}, {L"1", "1"}, {L"pick-c", "2"}}), ToggleWidgets(document(), L"choice")); + expect_second_on(); + + ASSERT_TRUE(EPDFForm_DetachWidget(document(), 4u, widget)); + EXPECT_EQ((std::vector{L"pick-a", L"1"}), + OwnValuesList(document(), 4u)); + EXPECT_EQ((ToggleWidgetList{{L"pick-a", "0"}, {L"1", "1"}}), + ToggleWidgets(document(), L"choice")); + expect_second_on(); UnloadPage(page); } diff --git a/public/epdf_form.h b/public/epdf_form.h index fa2560275..bbf9d539b 100644 --- a/public/epdf_form.h +++ b/public/epdf_form.h @@ -666,9 +666,10 @@ EPDFForm_CreateField(FPDF_DOCUMENT document, // field's widgets, and its on-state name only a key, which widgets of one // value share to go on together. The field then gets a list of its own with // |export_value| at the new widget's place; a place the list lacked before -// it gets that widget's on-state name, its value without a list. For a field -// without a list, an |export_value| other than |on_state| fails. NULL for -// other families. +// it gets that widget's on-state name, the value Acrobat reads for a widget +// the list has no entry for. For a field without a list, an |export_value| +// other than |on_state| fails. An empty |export_value| fails: an empty entry +// reads back as another value. NULL for other families. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFForm_AttachWidget(FPDF_DOCUMENT document, uint32_t field_objnum, From 7fb0b9631986381e5eaa740cc69caa2ba616da2d Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Tue, 1 Dec 2026 12:09:51 +0200 Subject: [PATCH 3/3] List the empty-value check's widget in the layer-mode writers count AWidgetJoiningAListedGroupGetsItsValue makes one more widget for the refused empty value, so it leaves 12 objects in the layer, not 11. --- testing/embedpdf_layer_mode_writers.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 84a47c1ca..8b0831fdc 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -237,7 +237,7 @@ EPDFFormEmbedderTest.AListThatIsAnObjectStaysAsItIs 5 EPDFFormEmbedderTest.AListedMergedCheckboxSplitsAndKeepsItsValue 6 EPDFFormEmbedderTest.ANegativeWidgetRotationIsDrawnAsItsTurn 3 EPDFFormEmbedderTest.AShortListIsFilledBeforeTheNewPlace 5 -EPDFFormEmbedderTest.AWidgetJoiningAListedGroupGetsItsValue 11 +EPDFFormEmbedderTest.AWidgetJoiningAListedGroupGetsItsValue 12 EPDFFormEmbedderTest.AttachToLegacyMergedFieldKeepsFieldId 7 EPDFFormEmbedderTest.AttachToSignatureFieldDrawsItsBox 7 EPDFFormEmbedderTest.AttachWidgetsFormsARadioGroup 11