Commit bb2b7fb4 authored by Carlos Garcia Campos's avatar Carlos Garcia Campos
Browse files

glib: Add API to get text, text layout and text attributes for a given area

parent f662973b
......@@ -850,6 +850,28 @@ poppler_page_get_text (PopplerPage *page)
return poppler_page_get_selected_text (page, POPPLER_SELECTION_GLYPH, &rectangle);
}
/**
* poppler_page_get_text_for_area:
* @page: a #PopplerPage
* @area: a #PopplerRectangle
*
* Retrieves the text of @page contained in @area.
*
* Return value: a pointer to the text as a string
*
* Since: 0.26
**/
char *
poppler_page_get_text_for_area (PopplerPage *page,
PopplerRectangle *area)
{
g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL);
g_return_val_if_fail (area != NULL, NULL);
return poppler_page_get_selected_text (page, POPPLER_SELECTION_GLYPH, area);
}
/**
* poppler_page_find_text_with_options:
* @page: a #PopplerPage
......@@ -2018,14 +2040,16 @@ poppler_page_get_crop_box (PopplerPage *page, PopplerRectangle *rect)
* poppler_page_get_text_layout:
* @page: A #PopplerPage
* @rectangles: (out) (array length=n_rectangles) (transfer container): return location for an array of #PopplerRectangle
* @n_rectangles: (out) length of returned array
* @n_rectangles: (out): length of returned array
*
* Obtains the layout of the text as a list of #PopplerRectangle
* This array must be freed with g_free () when done.
* This array must be freed with g_free() when done.
*
* The position in the array represents an offset in the text returned by
* poppler_page_get_text()
*
* See also poppler_page_get_text_layout_for_area().
*
* Return value: %TRUE if the page contains text, %FALSE otherwise
*
* Since: 0.16
......@@ -2034,6 +2058,38 @@ gboolean
poppler_page_get_text_layout (PopplerPage *page,
PopplerRectangle **rectangles,
guint *n_rectangles)
{
PopplerRectangle selection = {0, 0, 0, 0};
g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
poppler_page_get_size (page, &selection.x2, &selection.y2);
return poppler_page_get_text_layout_for_area (page, &selection, rectangles, n_rectangles);
}
/**
* poppler_page_get_text_layout_for_area:
* @page: A #PopplerPage
* @area: a #PopplerRectangle
* @rectangles: (out) (array length=n_rectangles) (transfer container): return location for an array of #PopplerRectangle
* @n_rectangles: (out): length of returned array
*
* Obtains the layout of the text contained in @area as a list of #PopplerRectangle
* This array must be freed with g_free() when done.
*
* The position in the array represents an offset in the text returned by
* poppler_page_get_text_for_area()
*
* Return value: %TRUE if the page contains text, %FALSE otherwise
*
* Since: 0.26
**/
gboolean
poppler_page_get_text_layout_for_area (PopplerPage *page,
PopplerRectangle *area,
PopplerRectangle **rectangles,
guint *n_rectangles)
{
TextPage *text;
PopplerRectangle *rect;
......@@ -2047,10 +2103,15 @@ poppler_page_get_text_layout (PopplerPage *page,
int n_lines;
g_return_val_if_fail (POPPLER_IS_PAGE (page), FALSE);
g_return_val_if_fail (area != NULL, FALSE);
*n_rectangles = 0;
poppler_page_get_size (page, &selection.x2, &selection.y2);
selection.x1 = area->x1;
selection.y1 = area->y1;
selection.x2 = area->x2;
selection.y2 = area->y2;
text = poppler_page_get_text_page (page);
word_list = text->getSelectionWords (&selection, selectionStyleGlyph, &n_lines);
if (!word_list)
......@@ -2170,19 +2231,50 @@ word_text_attributes_equal (TextWord *a, gint ai, TextWord *b, gint bi)
* poppler_page_get_text_attributes:
* @page: A #PopplerPage
*
* Obtains the attributes of the text as a GList of #PopplerTextAttributes.
* Obtains the attributes of the text as a #GList of #PopplerTextAttributes.
* This list must be freed with poppler_page_free_text_attributes() when done.
*
* Each list element is a #PopplerTextAttributes struct where start_index and
* end_index indicates the range of text (as returned by poppler_page_get_text())
* to which text attributes apply.
*
* See also poppler_page_get_text_attributes_for_area()
*
* Return value: (element-type PopplerTextAttributes) (transfer full): A #GList of #PopplerTextAttributes
*
* Since: 0.18
**/
GList *
poppler_page_get_text_attributes (PopplerPage *page)
{
PopplerRectangle selection = {0, 0, 0, 0};
g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL);
poppler_page_get_size (page, &selection.x2, &selection.y2);
return poppler_page_get_text_attributes_for_area (page, &selection);
}
/**
* poppler_page_get_text_attributes_for_area:
* @page: A #PopplerPage
* @area: a #PopplerRectangle
*
* Obtains the attributes of the text in @area as a #GList of #PopplerTextAttributes.
* This list must be freed with poppler_page_free_text_attributes() when done.
*
* Each list element is a #PopplerTextAttributes struct where start_index and
* end_index indicates the range of text (as returned by poppler_page_get_text_for_area())
* to which text attributes apply.
*
* Return value: (element-type PopplerTextAttributes) (transfer full): A #GList of #PopplerTextAttributes
*
* Since: 0.26
**/
GList *
poppler_page_get_text_attributes_for_area (PopplerPage *page,
PopplerRectangle *area)
{
TextPage *text;
PDFRectangle selection;
......@@ -2196,8 +2288,13 @@ poppler_page_get_text_attributes (PopplerPage *page)
GList *attributes = NULL;
g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL);
g_return_val_if_fail (area != NULL, FALSE);
selection.x1 = area->x1;
selection.y1 = area->y1;
selection.x2 = area->x2;
selection.y2 = area->y2;
poppler_page_get_size (page, &selection.x2, &selection.y2);
text = poppler_page_get_text_page (page);
word_list = text->getSelectionWords (&selection, selectionStyleGlyph, &n_lines);
if (!word_list)
......
......@@ -68,6 +68,8 @@ GList *poppler_page_find_text (PopplerPage *page,
void poppler_page_render_to_ps (PopplerPage *page,
PopplerPSFile *ps_file);
char *poppler_page_get_text (PopplerPage *page);
char *poppler_page_get_text_for_area (PopplerPage *page,
PopplerRectangle *area);
char *poppler_page_get_selected_text (PopplerPage *page,
PopplerSelectionStyle style,
PopplerRectangle *selection);
......@@ -99,8 +101,14 @@ void poppler_page_get_crop_box (PopplerPage *page,
gboolean poppler_page_get_text_layout (PopplerPage *page,
PopplerRectangle **rectangles,
guint *n_rectangles);
gboolean poppler_page_get_text_layout_for_area (PopplerPage *page,
PopplerRectangle *area,
PopplerRectangle **rectangles,
guint *n_rectangles);
GList *poppler_page_get_text_attributes (PopplerPage *page);
void poppler_page_free_text_attributes (GList *list);
GList * poppler_page_get_text_attributes_for_area (PopplerPage *page,
PopplerRectangle *area);
/* A rectangle on a page, with coordinates in PDF points. */
#define POPPLER_TYPE_RECTANGLE (poppler_rectangle_get_type ())
......
......@@ -35,8 +35,11 @@ poppler_page_get_selected_text
poppler_page_find_text
poppler_page_find_text_with_options
poppler_page_get_text
poppler_page_get_text_for_area
poppler_page_get_text_layout
poppler_page_get_text_layout_for_area
poppler_page_get_text_attributes
poppler_page_get_text_attributes_for_area
poppler_page_free_text_attributes
poppler_page_get_link_mapping
poppler_page_free_link_mapping
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment