From 3deb152c81e47800b17ae87703775ec39f82f129 Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Thu, 26 Sep 2013 18:46:04 +0300 Subject: [PATCH v20 4/5] 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_form_get_field() - poppler_page_get_form_field_mapping_for_structure_element() 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-page.cc | 32 +++++++++++++++++++++++++ glib/poppler-page.h | 3 +++ glib/poppler-structure-element.cc | 47 +++++++++++++++++++++++++++++++++++++ glib/poppler-structure-element.h | 1 + glib/reference/poppler-sections.txt | 2 ++ 5 files changed, 85 insertions(+) diff --git a/glib/poppler-page.cc b/glib/poppler-page.cc index f5271b5..dd1796e 100644 --- a/glib/poppler-page.cc +++ b/glib/poppler-page.cc @@ -1380,6 +1380,38 @@ poppler_page_free_form_field_mapping (GList *list) } /** + * poppler_page_get_form_field_mapping_for_structure_element: + * @page: A #PopplerPage. + * @structure_element: a #PopplerStructureElement. + * + * Obtains the #PopplerFormFieldMapping for a single form field associated + * with a structure element of type %POPPLER_STRUCTURE_ELEMENT_FORM. The + * returned value must be freed with poppler_form_field_mapping_free(). + * + * Return value: (transfer full): A #PopplerFormFieldMapping; or %NULL if + * the element is not a %POPPLER_STRUCTURE_ELEMENT_FORM or if the given + * structure element does not have content in the page. + * + * Since: 0.26 + */ +PopplerFormFieldMapping * +poppler_page_get_form_field_mapping_for_structure_element (PopplerPage *page, + PopplerStructureElement *structure_element) +{ + g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL); + g_return_val_if_fail (POPPLER_IS_STRUCTURE_ELEMENT (structure_element), NULL); + + if (page->index != poppler_structure_element_get_page (structure_element)) + return NULL; + + PopplerFormField *field = poppler_structure_element_form_get_field (structure_element); + if (field == NULL) + return NULL; + + return _poppler_form_field_mapping_new_from_form_field (field, page->index); +} + +/** * poppler_page_get_annot_mapping: * @page: A #PopplerPage * diff --git a/glib/poppler-page.h b/glib/poppler-page.h index 63fe362..f611b2d 100644 --- a/glib/poppler-page.h +++ b/glib/poppler-page.h @@ -89,6 +89,9 @@ void poppler_page_free_image_mapping (GList *li cairo_surface_t *poppler_page_get_image (PopplerPage *page, gint image_id); GList *poppler_page_get_form_field_mapping (PopplerPage *page); +PopplerFormFieldMapping * +poppler_page_get_form_field_mapping_for_structure_element(PopplerPage *page, + PopplerStructureElement *structure_element); void poppler_page_free_form_field_mapping (GList *list); GList *poppler_page_get_annot_mapping (PopplerPage *page); void poppler_page_free_annot_mapping (GList *list); diff --git a/glib/poppler-structure-element.cc b/glib/poppler-structure-element.cc index 9e86304..3888a6d 100644 --- a/glib/poppler-structure-element.cc +++ b/glib/poppler-structure-element.cc @@ -960,6 +960,53 @@ text_span_poppler_text_span (const TextSpan& span) } /** + * poppler_structure_element_form_get_field: + * @poppler_structure_element: A #PopplerStructureElement + * + * Obtains the #PopplerFormField out of an element of type + * %POPPLER_STRUCTURE_ELEMENT_FORM. The returned value must be freed + * with g_object_unref(). Note that there is one structure element + * per form field. + * + * Return value: (transfer full): A #PopplerFormField, or %NULL if + * the element is not a %POPPLER_STRUCTURE_ELEMENT_FORM. + * + * Since: 0.26 + */ +PopplerFormField * +poppler_structure_element_form_get_field (PopplerStructureElement *poppler_structure_element) +{ + g_return_val_if_fail (POPPLER_IS_STRUCTURE_ELEMENT (poppler_structure_element), NULL); + g_return_val_if_fail (poppler_structure_element->elem != NULL, NULL); + g_return_val_if_fail (poppler_structure_element->elem->getType () != StructElement::Form, NULL); + + switch (poppler_structure_element_form_get_role (poppler_structure_element)) + { + case POPPLER_STRUCTURE_FORM_ROLE_RADIO_BUTTON: + case POPPLER_STRUCTURE_FORM_ROLE_PUSH_BUTTON: + case POPPLER_STRUCTURE_FORM_ROLE_TEXT_VALUE: + case POPPLER_STRUCTURE_FORM_ROLE_CHECKBOX: + /* TODO Handle read-only form items with a Role attribute. */ + return NULL; + + case POPPLER_STRUCTURE_FORM_ROLE_UNDEFINED: { + g_assert (poppler_structure_element->elem->getNumElements () == 1); + StructElement *child = poppler_structure_element->elem->getElement (0); + g_assert (child->isObjectRef ()); + + FormWidget *widget = poppler_structure_element->document->doc->getCatalog () + ->getForm ()->findWidgetByRef (child->getObjectRef ()); + + return poppler_document_get_form_field (poppler_structure_element->document, + widget->getID ()); + break; + } + default: + g_assert_not_reached (); + } +} + +/** * poppler_text_span_copy: * @poppler_text_span: a #PopplerTextSpan * diff --git a/glib/poppler-structure-element.h b/glib/poppler-structure-element.h index 380158d..2204060 100644 --- a/glib/poppler-structure-element.h +++ b/glib/poppler-structure-element.h @@ -311,6 +311,7 @@ gdouble *poppler_structure_element_get_column_widths gchar **poppler_structure_element_get_headers (PopplerStructureElement *poppler_structure_element); PopplerStructureFormState poppler_structure_element_form_get_state (PopplerStructureElement *poppler_structure_element); PopplerStructureFormRole poppler_structure_element_form_get_role (PopplerStructureElement *poppler_structure_element); +PopplerFormField *poppler_structure_element_form_get_field (PopplerStructureElement *poppler_structure_element); void poppler_structure_element_table_get_padding (PopplerStructureElement *poppler_structure_element, gdouble *paddings); gboolean poppler_structure_element_table_get_bounding_box (PopplerStructureElement *poppler_structure_element, diff --git a/glib/reference/poppler-sections.txt b/glib/reference/poppler-sections.txt index 62a4199..873a611 100644 --- a/glib/reference/poppler-sections.txt +++ b/glib/reference/poppler-sections.txt @@ -73,6 +73,7 @@ poppler_annot_mapping_free poppler_text_attributes_new poppler_text_attributes_copy poppler_text_attributes_free +poppler_page_get_form_field_mapping_for_structure_element POPPLER_PAGE @@ -663,6 +664,7 @@ poppler_structure_element_get_border_color poppler_structure_element_get_headers poppler_structure_element_form_get_role poppler_structure_element_form_get_state +poppler_structure_element_form_get_field poppler_structure_element_table_get_padding poppler_structure_element_table_get_border_style poppler_structure_element_table_get_bounding_box -- 1.9.0