diff --git a/pdf/pdfium/pdfium_page.cc b/pdf/pdfium/pdfium_page.cc index cf8d31cc5854e..24899ca5a2b9b 100644 --- a/pdf/pdfium/pdfium_page.cc +++ b/pdf/pdfium/pdfium_page.cc @@ -1169,11 +1169,7 @@ void PDFiumPage::PopulateAnnotations() { break; } case FPDF_ANNOT_WIDGET: { - // TODO(crbug.com/1030242): Populate other types of form fields too. - if (FPDFAnnot_GetFormFieldType(engine_->form(), annot.get()) == - FPDF_FORMFIELD_TEXTFIELD) { - PopulateTextField(annot.get()); - } + PopulateFormField(annot.get()); break; } default: @@ -1234,23 +1230,48 @@ void PDFiumPage::PopulateTextField(FPDF_ANNOTATION annot) { DCHECK_EQ(FPDFAnnot_GetFormFieldType(form_handle, annot), FPDF_FORMFIELD_TEXTFIELD); - FS_RECTF rect; - if (!FPDFAnnot_GetRect(annot, &rect)) + TextField text_field; + if (!PopulateFormFieldProperties(annot, &text_field)) return; - TextField text_field; - // We use the bounding box of the text field as the bounding rect. - text_field.bounding_rect = - PageToScreen(pp::Point(), 1.0, rect.left, rect.top, rect.right, - rect.bottom, PageOrientation::kOriginal); text_field.value = base::UTF16ToUTF8(CallPDFiumWideStringBufferApi( base::BindRepeating(&FPDFAnnot_GetFormFieldValue, form_handle, annot), /*check_expected_size=*/true)); - text_field.name = base::UTF16ToUTF8(CallPDFiumWideStringBufferApi( + text_fields_.push_back(std::move(text_field)); +} + +void PDFiumPage::PopulateFormField(FPDF_ANNOTATION annot) { + DCHECK_EQ(FPDFAnnot_GetSubtype(annot), FPDF_ANNOT_WIDGET); + int form_field_type = FPDFAnnot_GetFormFieldType(engine_->form(), annot); + + // TODO(crbug.com/1030242): Populate other types of form fields too. + switch (form_field_type) { + case FPDF_FORMFIELD_TEXTFIELD: { + PopulateTextField(annot); + break; + } + default: + break; + } +} + +bool PDFiumPage::PopulateFormFieldProperties(FPDF_ANNOTATION annot, + FormField* form_field) { + DCHECK(annot); + FS_RECTF rect; + if (!FPDFAnnot_GetRect(annot, &rect)) + return false; + + // We use the bounding box of the form field as the bounding rect. + form_field->bounding_rect = + PageToScreen(pp::Point(), 1.0, rect.left, rect.top, rect.right, + rect.bottom, PageOrientation::kOriginal); + FPDF_FORMHANDLE form_handle = engine_->form(); + form_field->name = base::UTF16ToUTF8(CallPDFiumWideStringBufferApi( base::BindRepeating(&FPDFAnnot_GetFormFieldName, form_handle, annot), /*check_expected_size=*/true)); - text_field.flags = FPDFAnnot_GetFormFieldFlags(form_handle, annot); - text_fields_.push_back(std::move(text_field)); + form_field->flags = FPDFAnnot_GetFormFieldFlags(form_handle, annot); + return true; } bool PDFiumPage::GetUnderlyingTextRangeForRect(const pp::FloatRect& rect, @@ -1382,6 +1403,12 @@ PDFiumPage::Highlight::Highlight(const Highlight& that) = default; PDFiumPage::Highlight::~Highlight() = default; +PDFiumPage::FormField::FormField() = default; + +PDFiumPage::FormField::FormField(const FormField& that) = default; + +PDFiumPage::FormField::~FormField() = default; + PDFiumPage::TextField::TextField() = default; PDFiumPage::TextField::TextField(const TextField& that) = default; diff --git a/pdf/pdfium/pdfium_page.h b/pdf/pdfium/pdfium_page.h index e3353d547e081..6f69e450d10de 100644 --- a/pdf/pdfium/pdfium_page.h +++ b/pdf/pdfium/pdfium_page.h @@ -235,18 +235,26 @@ class PDFiumPage { std::string note_text; }; + // Represents a form field within the page. + struct FormField { + FormField(); + FormField(const FormField& other); + ~FormField(); + + pp::Rect bounding_rect; + // Represents the name of form field as defined in the field dictionary. + std::string name; + // Represents the flags of form field as defined in the field dictionary. + int flags; + }; + // Represents a text field within the page. - struct TextField { + struct TextField : FormField { TextField(); TextField(const TextField& other); ~TextField(); - // Represents the name of form field as defined in the field dictionary. - std::string name; std::string value; - pp::Rect bounding_rect; - // Represents the flags of form field as defined in the field dictionary. - int flags; }; // Returns a link index if the given character index is over a link, or -1 @@ -266,6 +274,8 @@ class PDFiumPage { void PopulateHighlight(FPDF_ANNOTATION annot); // Populate |text_fields_| with |annot|. void PopulateTextField(FPDF_ANNOTATION annot); + // Populate form fields like text field on the page. + void PopulateFormField(FPDF_ANNOTATION annot); // Returns link type and fills target associated with a destination. Returns // NONSELECTABLE_AREA if detection failed. Area GetDestinationTarget(FPDF_DEST destination, LinkTarget* target); @@ -305,6 +315,8 @@ class PDFiumPage { static uint32_t CountLinkHighlightOverlaps( const std::vector<Link>& links, const std::vector<Highlight>& highlights); + bool PopulateFormFieldProperties(FPDF_ANNOTATION annot, + FormField* form_field); PDFiumEngine* engine_; ScopedFPDFPage page_;