From 79b18e75e04cc6aedd1c2370859804d5287119b6 Mon Sep 17 00:00:00 2001 From: Anuj Khare Date: Mon, 21 Jul 2014 00:03:11 +0530 Subject: [PATCH 1/4] glib: Extend FreeText annotation support This patch allows adding new freetext annotations, their intent, quadding and callout lines. --- glib/poppler-annot.cc | 279 +++++++++++++++++++++++++++++++++++++++++++++++++- glib/poppler-annot.h | 25 +++++ 2 files changed, 302 insertions(+), 2 deletions(-) diff --git a/glib/poppler-annot.cc b/glib/poppler-annot.cc index 13bc768..4b6df2d 100644 --- a/glib/poppler-annot.cc +++ b/glib/poppler-annot.cc @@ -41,6 +41,9 @@ typedef struct _PopplerAnnotLineClass PopplerAnnotLineClass; typedef struct _PopplerAnnotCircleClass PopplerAnnotCircleClass; typedef struct _PopplerAnnotSquareClass PopplerAnnotSquareClass; +static PopplerColor *create_poppler_color_from_annot_color (AnnotColor *color); +static AnnotColor *create_annot_color_from_poppler_color (PopplerColor *poppler_color); + struct _PopplerAnnotClass { GObjectClass parent_class; @@ -79,6 +82,9 @@ struct _PopplerAnnotTextMarkupClass struct _PopplerAnnotFreeText { PopplerAnnotMarkup parent_instance; + + gdouble font_size; + PopplerColor *font_color; }; struct _PopplerAnnotFreeTextClass @@ -470,10 +476,91 @@ poppler_annot_free_text_class_init (PopplerAnnotFreeTextClass *klass) { } +static GooString *create_appearance_string (gdouble font_size, + PopplerColor *poppler_color) +{ + /* Core does not parse the font name as of yet. */ + GooString *s = GooString::format("/Invalid_font {0:f} Tf", font_size); + + if (poppler_color) { + AnnotColor *annot_color; + const double *values; + + annot_color = create_annot_color_from_poppler_color (poppler_color); + values = annot_color->getValues (); + + s->appendf (" {0:.2f} {1:.2f} {2:.2f} rg", values[0], values[1], values[2]); + delete annot_color; + } + + return s; +} + PopplerAnnot * _poppler_annot_free_text_new (Annot *annot) { - return _poppler_create_annot (POPPLER_TYPE_ANNOT_FREE_TEXT, annot); + AnnotFreeText *annot_ftext; + PopplerAnnot *poppler_annot; + GooString *da; + AnnotColor *annot_color; + PopplerAnnotFreeText *poppler_ftext; + + poppler_annot = _poppler_create_annot (POPPLER_TYPE_ANNOT_FREE_TEXT, annot); + poppler_ftext = POPPLER_ANNOT_FREE_TEXT (poppler_annot); + + annot_ftext = static_cast(annot); + da = annot_ftext->getAppearanceString(); + + g_assert (da!=NULL && da->getLength()!=0); + + annot_ftext->parseAppearanceString (da, poppler_ftext->font_size, annot_color); + poppler_ftext->font_color = create_poppler_color_from_annot_color (annot_color); + delete annot_color; + + return poppler_annot; +} + +/** + * poppler_annot_free_text_new: + * @doc: a #PopplerDocument + * @rect: a #PopplerRectangle + * @font_size: a #gdouble + * @font_color: a #PopplerColor + * + * Creates a Free Text annotation with font size @font_size and font + * color @font_color that will be located on @rect when added to a page. + * See poppler_page_add_annot(). + * Annotation takes ownership of @font_color. + * + * Return value: A newly created #PopplerAnnotFreeText annotation + * + * Since: 0.28 + */ +PopplerAnnot * +poppler_annot_free_text_new (PopplerDocument *doc, + PopplerRectangle *rect, + gdouble font_size, + PopplerColor *font_color) +{ + PopplerAnnot *poppler_annot; + AnnotFreeText *annot; + GooString *goo_tmp; + PopplerAnnotFreeText *poppler_ftext; + + PDFRectangle pdf_rect (rect->x1, rect->y1, + rect->x2, rect->y2); + + goo_tmp = create_appearance_string (font_size, font_color); + annot = new AnnotFreeText (doc->doc, &pdf_rect, goo_tmp); + delete goo_tmp; + + poppler_annot = _poppler_create_annot (POPPLER_TYPE_ANNOT_FREE_TEXT, annot); + poppler_ftext = POPPLER_ANNOT_FREE_TEXT (poppler_annot); + + poppler_ftext->font_size = font_size; + poppler_ftext->font_color = font_color; + + return poppler_annot; } static void @@ -1659,6 +1746,66 @@ poppler_annot_free_text_get_quadding (PopplerAnnotFreeText *poppler_annot) } /** + * poppler_annot_free_text_set_quadding: + * @poppler_annot: a #PopplerAnnotFreeText + * @quadding: a #PopplerAnnotFreeTextQuadding + * + * Sets the justification of the text of @poppler_annot. + * + * Since: 0.28 + **/ +void +poppler_annot_free_text_set_quadding (PopplerAnnotFreeText *poppler_annot, + PopplerAnnotFreeTextQuadding quadding) +{ + AnnotFreeText *annot; + + g_return_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot)); + + annot = static_cast(POPPLER_ANNOT (poppler_annot)->annot); + annot->setQuadding ((AnnotFreeText::AnnotFreeTextQuadding) quadding); +} + +/** + * poppler_annot_free_text_get_intent: + * @poppler_annot: a #PopplerAnnotFreeText + * @intent: a #PopplerAnnotFreeTextIntent + * + * Returns: the intent of the text of @poppler_annot. + * Since: 0.28 + **/ +PopplerAnnotFreeTextIntent +poppler_annot_free_text_get_intent (PopplerAnnotFreeText *poppler_annot) +{ + AnnotFreeText *annot; + + g_return_val_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot), POPPLER_ANNOT_FREE_TEXT_INTENT_FREE_TEXT); + + annot = static_cast(POPPLER_ANNOT (poppler_annot)->annot); + return (PopplerAnnotFreeTextIntent) annot->getIntent (); +} + +/** + * poppler_annot_free_text_set_intent: + * @poppler_annot: a #PopplerAnnotFreeText + * @intent: a #PopplerAnnotFreeTextIntent + * + * Sets the intent of the text of @poppler_annot. + * Since: 0.28 + **/ +void +poppler_annot_free_text_set_intent (PopplerAnnotFreeText *poppler_annot, + PopplerAnnotFreeTextIntent intent) +{ + AnnotFreeText *annot; + + g_return_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot)); + + annot = static_cast(POPPLER_ANNOT (poppler_annot)->annot); + annot->setIntent ((AnnotFreeText::AnnotFreeTextIntent) intent); +} + +/** * poppler_annot_free_text_get_callout_line: * @poppler_annot: a #PopplerAnnotFreeText * @@ -1702,6 +1849,135 @@ poppler_annot_free_text_get_callout_line (PopplerAnnotFreeText *poppler_annot) return NULL; } +/** + * poppler_annot_free_text_set_callout_line: + * @poppler_annot: a #PopplerAnnotFreeText + * @callout: (allow-none): a #PopplerAnnotCalloutLine + * + * Sets the callout line of the text of @poppler_annot to @callout, + * or removes the callout line if %NULL + * + * Since: 0.28 + **/ +void +poppler_annot_free_text_set_callout_line (PopplerAnnotFreeText *poppler_annot, + PopplerAnnotCalloutLine *callout) +{ + AnnotFreeText *annot; + AnnotCalloutLine *line = NULL; + + g_return_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot)); + + if (callout != NULL) { + if (callout->multiline) { + line = new AnnotCalloutMultiLine (callout->x1, callout->y1, + callout->x2, callout->y2, + callout->x3, callout->y3); + } else { + line = new AnnotCalloutLine (callout->x1, callout->y1, + callout->x2, callout->y2); + } + } + + annot = static_cast(POPPLER_ANNOT (poppler_annot)->annot); + annot->setCalloutLine (line); + if (line) + delete line; +} + +static void +poppler_annot_free_text_set_appearance_string (PopplerAnnotFreeText *poppler_annot) +{ + AnnotFreeText *annot; + GooString *da; + + da = create_appearance_string (poppler_annot->font_size, + poppler_annot->font_color); + annot = static_cast(POPPLER_ANNOT (poppler_annot)->annot); + annot->setAppearanceString (da); + delete da; +} + +/** + * poppler_annot_free_text_get_font_size: + * @poppler_annot: a #PopplerAnnotFreeText + * + * Retrieves the font size of @poppler_annot. + * + * Return value: The font size of the @poppler_annot. + * Since: 0.28 + **/ +gdouble +poppler_annot_free_text_get_font_size (PopplerAnnotFreeText *poppler_annot) +{ + g_return_val_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot), -1); + + return poppler_annot->font_size; +} + +/** + * poppler_annot_free_text_set_font_size: + * @poppler_annot: a #PopplerAnnotFreeText + * @size: a gdouble + * + * Sets the font size of the @poppler_annot to @size. + * + * Since: 0.28 + **/ +void +poppler_annot_free_text_set_font_size (PopplerAnnotFreeText *poppler_annot, + gdouble size) +{ + g_return_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot)); + + poppler_annot->font_size = size; + poppler_annot_free_text_set_appearance_string (poppler_annot); +} + +/** + * poppler_annot_free_text_get_font_color: + * @poppler_annot: a #PopplerAnnotFreeText + * + * Retrieves the font color of @poppler_annot. + * + * Return value: (transfer none): a #PopplerColor with the font color + * values of @poppler_annot, or %NULL. + * Since: 0.28 + **/ +PopplerColor * +poppler_annot_free_text_get_font_color (PopplerAnnotFreeText *poppler_annot) +{ + g_return_val_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot), NULL); + + return poppler_annot->font_color; +} + +/** + * poppler_annot_free_text_set_font_color: + * @poppler_annot: a #PopplerAnnotFreeText + * @poppler_color: (allow-none): a #PopplerColor, or %NULL + * + * Sets the color of @poppler_annot to @poppler_color, or if %NULL, + * sets the color to the default value. + * + * Since: 0.28 + */ +void +poppler_annot_free_text_set_color (PopplerAnnotFreeText *poppler_annot, + PopplerColor *poppler_color) +{ + g_return_if_fail (POPPLER_IS_ANNOT_FREE_TEXT (poppler_annot)); + + if (!poppler_color) { + g_free (poppler_annot->font_color); + poppler_annot->font_color = NULL; + } else { + *poppler_annot->font_color = *poppler_color; + } + + poppler_annot_free_text_set_appearance_string (poppler_annot); +} + /* PopplerAnnotFileAttachment */ /** * poppler_annot_file_attachment_get_attachment: @@ -1808,7 +2084,6 @@ poppler_annot_callout_line_free (PopplerAnnotCalloutLine *callout) g_free (callout); } - /* PopplerAnnotMovie */ /** * poppler_annot_movie_get_title: diff --git a/glib/poppler-annot.h b/glib/poppler-annot.h index 441563e..d0e413b 100644 --- a/glib/poppler-annot.h +++ b/glib/poppler-annot.h @@ -159,6 +159,13 @@ typedef enum POPPLER_ANNOT_FREE_TEXT_QUADDING_RIGHT_JUSTIFIED } PopplerAnnotFreeTextQuadding; +typedef enum +{ + POPPLER_ANNOT_FREE_TEXT_INTENT_FREE_TEXT, + POPPLER_ANNOT_FREE_TEXT_INTENT_CALLOUT, + POPPLER_ANNOT_FREE_TEXT_INTENT_TYPE_WRITER +} PopplerAnnotFreeTextIntent; + struct _PopplerAnnotCalloutLine { gboolean multiline; @@ -243,7 +250,25 @@ GArray *poppler_annot_text_markup_get_quadrilaterals ( /* PopplerAnnotFreeText */ GType poppler_annot_free_text_get_type (void) G_GNUC_CONST; PopplerAnnotFreeTextQuadding poppler_annot_free_text_get_quadding (PopplerAnnotFreeText *poppler_annot); +void poppler_annot_free_text_set_quadding (PopplerAnnotFreeText *poppler_annot, + PopplerAnnotFreeTextQuadding quadding); + +PopplerAnnotFreeTextIntent poppler_annot_free_text_get_intent (PopplerAnnotFreeText *poppler_annot); +void poppler_annot_free_text_set_intent (PopplerAnnotFreeText *poppler_annot, + PopplerAnnotFreeTextIntent intent); PopplerAnnotCalloutLine *poppler_annot_free_text_get_callout_line (PopplerAnnotFreeText *poppler_annot); +void poppler_annot_free_text_set_callout_line (PopplerAnnotFreeText *poppler_annot, + PopplerAnnotCalloutLine *callout); +PopplerAnnot *poppler_annot_free_text_new (PopplerDocument *doc, + PopplerRectangle *rect, + gdouble font_size, + PopplerColor *color); +gdouble poppler_annot_free_text_get_font_size (PopplerAnnotFreeText *poppler_annot); +void poppler_annot_free_text_set_font_size (PopplerAnnotFreeText *poppler_annot, + gdouble size); +PopplerColor *poppler_annot_free_text_get_font_color (PopplerAnnotFreeText *poppler_annot); +void poppler_annot_free_text_set_font_color (PopplerAnnotFreeText *poppler_annot, + PopplerColor *poppler_color); /* PopplerAnnotFileAttachment */ GType poppler_annot_file_attachment_get_type (void) G_GNUC_CONST; -- 1.9.1