From b5fff8e8574e14f4fc579ac41289f80d9fd204a3 Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Thu, 26 Sep 2013 18:46:04 +0300 Subject: [PATCH v10 09/12] glib: Accessors for form fields of structure elements Implements two methods which can be used on PopplerStructureElement objects of type POPPLER_STRUCTURE_ELEMENT_FORM: - poppler_structure_element_get_form_field() - poppler_structure_element_get_form_field_mapping() The only difference is that the later returns a PopplerFormFieldMapping, which itself contains the PopplerFormField, plus the rectangle covered by the form widget in the page. --- glib/poppler-structure-element.cc | 65 +++++++++++++++++++++++++++++++++++++++ glib/poppler-structure-element.h | 2 ++ 2 files changed, 67 insertions(+) diff --git a/glib/poppler-structure-element.cc b/glib/poppler-structure-element.cc index a7d5c77..00e0ac3 100644 --- a/glib/poppler-structure-element.cc +++ b/glib/poppler-structure-element.cc @@ -859,6 +859,71 @@ poppler_structure_element_get_text_spans (PopplerStructureElement *poppler_struc } /** + * poppler_structure_element_get_form_field: + * @poppler_structure_element: A #PopplerStructureElement + * + * Return value: (transfer full): A #PopplerFormField, or %NULL if + * the element is not a %POPPLER_STRUCTURE_ELEMENT_FORM. + */ +PopplerFormField * +poppler_structure_element_get_form_field (PopplerStructureElement *poppler_structure_element) +{ + g_return_val_if_fail (POPPLER_IS_STRUCTURE_ELEMENT (poppler_structure_element), NULL); + g_assert (poppler_structure_element->elem); + + if (poppler_structure_element->elem->getType () != StructElement::Form) + return NULL; + + // TODO Handle elements which have a Role attribute (used sometimes for + // non-editable widgets, to describe their appearance). Editable + // fields have only a single child, with the field identifier. + if (poppler_structure_element->elem->getNumElements () != 1) + return NULL; + + gint field_id = -1; + const StructElement *child = poppler_structure_element->elem->getElement (0); + if (child->isContent ()) + { + if (child->isObjectRef ()) + { + // TODO Handle this case -- I have yet to see a PDF using this. + } + else + { + // Element contains the form field ID as the MCID attribute. + field_id = child->getMCID (); + } + } + + if (field_id < 0) + return NULL; + + return (field_id < 0) ? NULL : poppler_document_get_form_field (poppler_structure_element->document, + field_id); +} + +/** + * poppler_structure_element_get_form_field_mapping: + * @poppler_structure_element: a #PopplerStructureElement + * + * Return value: (transfer full): A #PopplerFormFieldMapping, or %NULL if + * the element is not a %POPPLER_STRUCTURE_ELEMENT_FORM + */ +PopplerFormFieldMapping * +poppler_structure_element_get_form_field_mapping (PopplerStructureElement *poppler_structure_element) +{ + g_return_val_if_fail (POPPLER_IS_STRUCTURE_ELEMENT (poppler_structure_element), NULL); + g_assert (poppler_structure_element->elem); + + PopplerFormField *field = poppler_structure_element_get_form_field (poppler_structure_element); + if (field == NULL) + return NULL; + + gint page_num = poppler_structure_element_get_page (poppler_structure_element); + return _poppler_form_field_mapping_new_from_form_field (field, page_num); +} + +/** * poppler_text_span_copy: * @poppler_text_span: a #PopplerTextSpan * diff --git a/glib/poppler-structure-element.h b/glib/poppler-structure-element.h index fee8b12..5ed1720 100644 --- a/glib/poppler-structure-element.h +++ b/glib/poppler-structure-element.h @@ -100,6 +100,8 @@ gchar *poppler_structure_element_get_text gchar *poppler_structure_element_get_alt_text (PopplerStructureElement *poppler_structure_element); gchar *poppler_structure_element_get_actual_text (PopplerStructureElement *poppler_structure_element); GList *poppler_structure_element_get_text_spans (PopplerStructureElement *poppler_structure_element); +PopplerFormField *poppler_structure_element_get_form_field (PopplerStructureElement *poppler_structure_element); +PopplerFormFieldMapping *poppler_structure_element_get_form_field_mapping (PopplerStructureElement *poppler_structure_element); #define POPPLER_TYPE_STRUCTURE_ELEMENT_ITER (poppler_structure_element_iter_get_type ()) GType poppler_structure_element_iter_get_type (void) G_GNUC_CONST; -- 1.8.4.2