Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions fpdfsdk/epdf_annot_embeddertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions fpdfsdk/epdf_c_api_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions fpdfsdk/epdf_font_embeddertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
126 changes: 126 additions & 0 deletions fpdfsdk/epdf_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ struct FieldRecord {
RetainPtr<const CPDF_Dictionary> 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<OptionRecord> options;
std::vector<WidgetRecord> widgets;
std::array<epdf::ActionModelDataPtr, 4> actions;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2464,6 +2477,62 @@ 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<RetainPtr<const CPDF_Dictionary>> FieldWidgetKids(
CPDF_Document* doc,
const CPDF_Dictionary* field) {
std::vector<RetainPtr<const CPDF_Dictionary>> widgets;
RetainPtr<const CPDF_Array> kids =
field->GetArrayFor(pdfium::form_fields::kKids);
if (!kids) {
return widgets;
}
for (size_t i = 0; i < kids->size(); ++i) {
RetainPtr<const CPDF_Object> element = kids->GetObjectAt(i);
const CPDF_Reference* ref = element ? element->AsReference() : nullptr;
RetainPtr<const CPDF_Dictionary> 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, 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<CPDF_Array> OwnToggleValuesList(
CPDF_Dictionary* field,
const CPDF_Array* list,
const std::vector<RetainPtr<const CPDF_Dictionary>>& widgets) {
RetainPtr<CPDF_Array> own = ToArray(field->GetMutableObjectFor("Opt"));
if (!own) {
own = pdfium::MakeRetain<CPDF_Array>();
for (size_t i = 0; i < list->size(); ++i) {
RetainPtr<const CPDF_Object> entry = list->GetDirectObjectAt(i);
if (entry) {
own->Append(entry->Clone());
} else {
own->AppendNew<CPDF_String>();
}
}
field->SetFor("Opt", own);
}
for (size_t i = own->size(); i < widgets.size(); ++i) {
own->AppendNew<CPDF_String>(ReadWidgetOnState(widgets[i].Get()));
}
return own;
}

} // namespace

FPDF_EXPORT uint32_t FPDF_CALLCONV
Expand Down Expand Up @@ -2592,6 +2661,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();
Expand All @@ -2612,6 +2682,25 @@ 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<const CPDF_Array> values_list =
toggle ? ToArray(CPDF_FormField::GetFieldAttrForDict(field.Get(), "Opt"))
: 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;
}

RetainPtr<const CPDF_Dictionary> widget =
ToDictionary(doc->GetOrParseIndirectObject(widget_objnum));
Expand Down Expand Up @@ -2703,6 +2792,21 @@ EPDFForm_AttachWidget(FPDF_DOCUMENT document,
return false;
}
kids->AppendNew<CPDF_Reference>(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<RetainPtr<const CPDF_Dictionary>> widgets =
values_list ? FieldWidgetKids(doc, mutable_field.Get())
: std::vector<RetainPtr<const CPDF_Dictionary>>();
if (!widgets.empty()) {
RetainPtr<CPDF_Array> own =
OwnToggleValuesList(mutable_field.Get(), values_list.Get(), widgets);
const size_t place = widgets.size() - 1;
if (export_value) {
own->SetNewAt<CPDF_String>(place, new_value.AsStringView());
} else {
own->SetNewAt<CPDF_String>(place, state);
}
}

if (toggle) {
mutable_widget->SetNewFor<CPDF_Name>("AS", kOffState);
Expand Down Expand Up @@ -2741,6 +2845,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<const CPDF_Array> values_list =
IsToggleField(field.Get())
? ToArray(CPDF_FormField::GetFieldAttrForDict(field.Get(), "Opt"))
: nullptr;
std::optional<size_t> listed_place;
if (values_list) {
const std::vector<RetainPtr<const CPDF_Dictionary>> 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<CPDF_Dictionary> mutable_field =
ToDictionary(doc->GetMutableIndirectObject(field_objnum));
Expand All @@ -2756,6 +2878,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()) {
Expand Down
Loading