From 994f408b9644af63321421d8afae835c66b4e26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Poo-Caama=C3=B1o?= Date: Wed, 20 Nov 2013 10:52:42 -0800 Subject: [PATCH] glib-demo: Add support for PopplerTextAnnotMarkup * The subtypes are: Highlihght, Squiggly, StrikeOut and Underline. * Use ScrolledWindow for annotation properties to make room to show the Quadrilaterals of TextMarkup annotations. https://bugs.freedesktop.org/show_bug.cgi?id=51487 --- glib/demo/annots.c | 151 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 129 insertions(+), 22 deletions(-) diff --git a/glib/demo/annots.c b/glib/demo/annots.c index 3d3b1c9..d9c3887 100644 --- a/glib/demo/annots.c +++ b/glib/demo/annots.c @@ -38,6 +38,23 @@ enum { SELECTED_N_COLUMNS }; +typedef struct +{ + const guint type; + const gchar *label; +} Annotations; + +static Annotations supported_annots[] = { + { POPPLER_ANNOT_TEXT, "Text" }, + { POPPLER_ANNOT_LINE, "Line" }, + { POPPLER_ANNOT_SQUARE, "Square" }, + { POPPLER_ANNOT_CIRCLE, "Circle" }, + { POPPLER_ANNOT_HIGHLIGHT, "Highlight" }, + { POPPLER_ANNOT_UNDERLINE, "Underline" }, + { POPPLER_ANNOT_SQUIGGLY, "Squiggly" }, + { POPPLER_ANNOT_STRIKE_OUT, "Strike Out" }, +}; + typedef enum { MODE_NORMAL, /* Regular use as pointer in the page */ MODE_ADD, /* To add simple annotations */ @@ -449,6 +466,42 @@ pgd_annot_color_changed (GtkButton *button, } static void +pgd_annot_view_set_annot_text_markup (GtkWidget *table, + PopplerAnnotTextMarkup *annot, + gint *row) +{ + gint i; + gchar *text = NULL; + gchar *prev_text = NULL; + GArray *quads_array = NULL; + PopplerQuadrilateral quadrilateral; + + quads_array = poppler_annot_text_markup_get_quadrilaterals (annot); + + prev_text = g_strdup (""); + for (i = 0; i < quads_array->len; i++) { + quadrilateral = g_array_index (quads_array, PopplerQuadrilateral, i); + + text = g_strdup_printf ("%s%2d:(%.2f,%.2f) (%.2f,%.2f)\n" + " (%.2f,%.2f) (%.2f,%.2f)\n", + prev_text, i+1, + quadrilateral.p1.x, quadrilateral.p1.y, + quadrilateral.p2.x, quadrilateral.p2.y, + quadrilateral.p3.x, quadrilateral.p3.y, + quadrilateral.p4.x, quadrilateral.p4.y); + g_free (prev_text); + prev_text = text; + } + + text = g_strchomp (text); + pgd_table_add_property (GTK_GRID (table), "Quadrilaterals:", text, row); + + g_array_free (quads_array, TRUE); + g_free (text); +} + + +static void pgd_annot_view_set_annot_free_text (GtkWidget *table, PopplerAnnotFreeText *annot, gint *row) @@ -619,6 +672,12 @@ pgd_annot_view_set_annot (PgdAnnotsDemo *demo, case POPPLER_ANNOT_TEXT: pgd_annot_view_set_annot_text (table, POPPLER_ANNOT_TEXT (annot), &row); break; + case POPPLER_ANNOT_HIGHLIGHT: + case POPPLER_ANNOT_UNDERLINE: + case POPPLER_ANNOT_SQUIGGLY: + case POPPLER_ANNOT_STRIKE_OUT: + pgd_annot_view_set_annot_text_markup (table, POPPLER_ANNOT_TEXT_MARKUP (annot), &row); + break; case POPPLER_ANNOT_FREE_TEXT: pgd_annot_view_set_annot_free_text (table, POPPLER_ANNOT_FREE_TEXT (annot), &row); break; @@ -859,6 +918,18 @@ pgd_annots_add_annot (PgdAnnotsDemo *demo) annot = poppler_annot_circle_new (demo->doc, &rect); poppler_annot_set_border_width (annot, 1.5); break; + case POPPLER_ANNOT_HIGHLIGHT: + annot = poppler_annot_text_markup_new_highlight (demo->doc, &rect); + break; + case POPPLER_ANNOT_UNDERLINE: + annot = poppler_annot_text_markup_new_underline (demo->doc, &rect); + break; + case POPPLER_ANNOT_SQUIGGLY: + annot = poppler_annot_text_markup_new_squiggly (demo->doc, &rect); + break; + case POPPLER_ANNOT_STRIKE_OUT: + annot = poppler_annot_text_markup_new_strikeout (demo->doc, &rect); + break; default: g_assert_not_reached (); } @@ -884,6 +955,54 @@ pgd_annots_finish_add_annot (PgdAnnotsDemo *demo) gtk_label_set_text (GTK_LABEL (demo->timer_label), NULL); } +static void +pgd_annots_update_selected_text (PgdAnnotsDemo *demo) +{ + PopplerRectangle doc_area, *rect; + gdouble width, height; + PopplerQuadrilateral quadrilateral; + GList *list, *region; + GArray *quads_array = NULL; + + poppler_page_get_size (demo->page, &width, &height); + + doc_area.x1 = demo->start.x; + doc_area.y1 = demo->start.y; + doc_area.x2 = demo->stop.x; + doc_area.y2 = demo->stop.y; + + region = poppler_page_get_selection_region (demo->page, 1.0, + POPPLER_SELECTION_GLYPH, + &doc_area); + + if (region == NULL) + return; + + quads_array = g_array_sized_new (FALSE, FALSE, + sizeof (PopplerQuadrilateral), + g_list_length (region)); + + for (list = region; list; list = list->next) { + rect = (PopplerRectangle *) list->data; + + quadrilateral.p1.x = rect->x1; + quadrilateral.p1.y = height - rect->y1; + quadrilateral.p2.x = rect->x2; + quadrilateral.p2.y = height - rect->y1; + quadrilateral.p3.x = rect->x1; + quadrilateral.p3.y = height - rect->y2; + quadrilateral.p4.x = rect->x2; + quadrilateral.p4.y = height - rect->y2; + + g_array_append_val (quads_array, quadrilateral); + } + + poppler_page_selection_region_free (region); + + poppler_annot_text_markup_set_quadrilaterals (POPPLER_ANNOT_TEXT_MARKUP (demo->active_annot), quads_array); + g_array_free (quads_array, TRUE); +} + /* Render area */ static cairo_surface_t * pgd_annots_render_page (PgdAnnotsDemo *demo) @@ -1023,6 +1142,9 @@ pgd_annots_drawing_area_motion_notify (GtkWidget *area, poppler_annot_line_set_vertices (POPPLER_ANNOT_LINE (demo->active_annot), &start, &end); + if (POPPLER_IS_ANNOT_TEXT_MARKUP (demo->active_annot)) + pgd_annots_update_selected_text (demo); + pgd_annot_view_set_annot (demo, demo->active_annot); pgd_annots_viewer_queue_redraw (demo); @@ -1110,29 +1232,14 @@ pgd_annots_create_widget (PopplerDocument *document) model = gtk_list_store_new(SELECTED_N_COLUMNS, G_TYPE_INT, G_TYPE_STRING); - gtk_list_store_append (model, &iter); - gtk_list_store_set (model, &iter, - SELECTED_TYPE_COLUMN, POPPLER_ANNOT_TEXT, - SELECTED_LABEL_COLUMN, "Text", - -1); - - gtk_list_store_append (model, &iter); - gtk_list_store_set (model, &iter, - SELECTED_TYPE_COLUMN, POPPLER_ANNOT_LINE, - SELECTED_LABEL_COLUMN, "Line", - -1); - - gtk_list_store_append (model, &iter); - gtk_list_store_set (model, &iter, - SELECTED_TYPE_COLUMN, POPPLER_ANNOT_SQUARE, - SELECTED_LABEL_COLUMN, "Square", - -1); - gtk_list_store_append (model, &iter); - gtk_list_store_set (model, &iter, - SELECTED_TYPE_COLUMN, POPPLER_ANNOT_CIRCLE, - SELECTED_LABEL_COLUMN, "Circle", - -1); + for (gint i = 0; i < G_N_ELEMENTS (supported_annots); i++) { + gtk_list_store_append (model, &iter); + gtk_list_store_set (model, &iter, + SELECTED_TYPE_COLUMN, supported_annots[i].type, + SELECTED_LABEL_COLUMN, supported_annots[i].label, + -1); + } demo->type_selector = gtk_combo_box_new_with_model (GTK_TREE_MODEL (model)); g_object_unref (model); -- 1.7.9.5