From 83a7b085942de959b1bcda4e14001eafd36876e3 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 18 Oct 2011 11:45:08 +0200 Subject: [PATCH] Added, managed and tested property ellipsize to text objects (closes #64). --- data/reptool.dtd | 1 + data/reptool_report.dtd | 1 + src/rptcommon.c | 81 +++++++++++++++++++++++++++++++++++++++++ src/rptcommon.h | 11 ++++++ src/rptobjecttext.c | 69 +++++++++++++++++++---------------- src/rptprint.c | 31 +++++++++++++++- src/rptreport.c | 1 + 7 files changed, 163 insertions(+), 32 deletions(-) diff --git a/data/reptool.dtd b/data/reptool.dtd index c809b7c..ba0534a 100644 --- a/data/reptool.dtd +++ b/data/reptool.dtd @@ -71,6 +71,7 @@ padding-right CDATA #IMPLIED padding-bottom CDATA #IMPLIED padding-left CDATA #IMPLIED + ellipsize (none | start | middle | end) > diff --git a/data/reptool_report.dtd b/data/reptool_report.dtd index 90842c7..6a3f633 100644 --- a/data/reptool_report.dtd +++ b/data/reptool_report.dtd @@ -65,6 +65,7 @@ padding-right CDATA #IMPLIED padding-bottom CDATA #IMPLIED padding-left CDATA #IMPLIED + ellipsize (none | start | middle | end) > diff --git a/src/rptcommon.c b/src/rptcommon.c index af2ecaf..db73db0 100644 --- a/src/rptcommon.c +++ b/src/rptcommon.c @@ -184,6 +184,87 @@ const gchar return ret; } +/** + * rpt_common_strellipsize_to_enum: + * @ellipsize: + * + * Returns: the enum value that match the string @ellipsize. + */ +eRptEllipsize +rpt_common_strellipsize_to_enum (const gchar *ellipsize) +{ + eRptEllipsize ret; + + gchar *real_ellipsize; + + ret = RPT_ELLIPSIZE_NONE; + + if (ellipsize != NULL) + { + real_ellipsize = g_strstrip (g_strdup (ellipsize)); + if (g_ascii_strcasecmp (real_ellipsize, "none") == 0) + { + /* already setted */ + } + else if (g_ascii_strcasecmp (real_ellipsize, "start") == 0) + { + ret = RPT_ELLIPSIZE_START; + } + else if (g_ascii_strcasecmp (real_ellipsize, "middle") == 0) + { + ret = RPT_ELLIPSIZE_MIDDLE; + } + else if (g_ascii_strcasecmp (real_ellipsize, "end") == 0) + { + ret = RPT_ELLIPSIZE_END; + } + else + { + g_warning ("Ellipsize type «%s» not available.", real_ellipsize); + } + } + + return ret; +} + +/** + * rpt_common_enum_to_strellipsize: + * @unit: + * + * Returns: the string value that represents then enum value @unit. + */ +const gchar +*rpt_common_enum_to_strellipsize (eRptEllipsize ellipsize) +{ + gchar *ret; + + switch (ellipsize) + { + case RPT_ELLIPSIZE_NONE: + ret = g_strdup ("none"); + break; + + case RPT_ELLIPSIZE_START: + ret = g_strdup ("start"); + break; + + case RPT_ELLIPSIZE_MIDDLE: + ret = g_strdup ("middle"); + break; + + case RPT_ELLIPSIZE_END: + ret = g_strdup ("end"); + break; + + default: + g_warning ("Ellipsize type «%d» not available.", ellipsize); + ret = g_strdup ("none"); + break; + } + + return ret; +} + /** * rpt_common_stroutputtype_to_enum: * @output_type: diff --git a/src/rptcommon.h b/src/rptcommon.h index 0622d2a..5239878 100644 --- a/src/rptcommon.h +++ b/src/rptcommon.h @@ -183,6 +183,14 @@ struct _RptStroke }; typedef struct _RptStroke RptStroke; +typedef enum +{ + RPT_ELLIPSIZE_NONE, + RPT_ELLIPSIZE_START, + RPT_ELLIPSIZE_MIDDLE, + RPT_ELLIPSIZE_END +} eRptEllipsize; + gdouble rpt_common_value_to_points (eRptUnitLength unit, gdouble value); gdouble rpt_common_points_to_value (eRptUnitLength unit, gdouble value); @@ -193,6 +201,9 @@ const gchar *rpt_common_enum_to_strunit (eRptUnitLength unit); eRptOutputType rpt_common_stroutputtype_to_enum (const gchar *output_type); const gchar *rpt_common_enum_to_stroutputtype (eRptOutputType output_type); +eRptEllipsize rpt_common_strellipsize_to_enum (const gchar *ellipsize); +const gchar *rpt_common_enum_to_strellipsize (eRptEllipsize ellipsize); + RptPoint *rpt_common_rptpoint_new (void); RptPoint *rpt_common_rptpoint_new_with_values (gdouble x, gdouble y); RptPoint *rpt_common_get_position (xmlNode *xnode); diff --git a/src/rptobjecttext.c b/src/rptobjecttext.c index 30152b2..d1eaf57 100644 --- a/src/rptobjecttext.c +++ b/src/rptobjecttext.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2010 Andrea Zagli + * Copyright (C) 2006-2011 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -32,7 +32,8 @@ enum PROP_PADDING_TOP, PROP_PADDING_RIGHT, PROP_PADDING_BOTTOM, - PROP_PADDING_LEFT + PROP_PADDING_LEFT, + PROP_ELLIPSIZE }; static void rpt_obj_text_class_init (RptObjTextClass *klass); @@ -64,35 +65,10 @@ struct _RptObjTextPrivate gdouble padding_right; gdouble padding_bottom; gdouble padding_left; + eRptEllipsize ellipsize; }; -GType -rpt_obj_text_get_type (void) -{ - static GType rpt_obj_text_type = 0; - - if (!rpt_obj_text_type) - { - static const GTypeInfo rpt_obj_text_info = - { - sizeof (RptObjTextClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) rpt_obj_text_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (RptObjText), - 0, /* n_preallocs */ - (GInstanceInitFunc) rpt_obj_text_init, - NULL - }; - - rpt_obj_text_type = g_type_register_static (TYPE_RPT_OBJECT, "RptObjText", - &rpt_obj_text_info, 0); - } - - return rpt_obj_text_type; -} +G_DEFINE_TYPE (RptObjText, rpt_obj_text, TYPE_RPT_OBJECT) static void rpt_obj_text_class_init (RptObjTextClass *klass) @@ -167,6 +143,12 @@ rpt_obj_text_class_init (RptObjTextClass *klass) "Padding Left", 0, G_MAXDOUBLE, 0, G_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_ELLIPSIZE, + g_param_spec_uint ("ellipsize", + "Ellipsize", + "Ellipsize", + RPT_ELLIPSIZE_NONE, RPT_ELLIPSIZE_END, RPT_ELLIPSIZE_NONE, + G_PARAM_READWRITE)); } static void @@ -194,6 +176,8 @@ rpt_obj_text_init (RptObjText *rpt_obj_text) priv->font = font; priv->align = NULL; priv->background_color = NULL; + + priv->ellipsize = RPT_ELLIPSIZE_NONE; } /** @@ -261,6 +245,7 @@ RptObject prop = (gchar *)xmlGetProp (xnode, "source"); g_object_set (rpt_obj_text, "source", prop, NULL); + if (prop != NULL) xmlFree (prop); prop = (gchar *)xmlGetProp (xnode, "background-color"); if (prop != NULL) @@ -269,27 +254,39 @@ RptObject color = rpt_common_parse_color (g_strstrip (prop)); g_object_set (rpt_obj_text, "background-color", color, NULL); + + xmlFree (prop); } prop = (gchar *)xmlGetProp (xnode, "padding-top"); if (prop != NULL) { g_object_set (rpt_obj_text, "padding-top", strtod (prop, NULL), NULL); + xmlFree (prop); } prop = (gchar *)xmlGetProp (xnode, "padding-right"); if (prop != NULL) { g_object_set (rpt_obj_text, "padding-right", strtod (prop, NULL), NULL); + xmlFree (prop); } prop = (gchar *)xmlGetProp (xnode, "padding-bottom"); if (prop != NULL) { g_object_set (rpt_obj_text, "padding-bottom", strtod (prop, NULL), NULL); + xmlFree (prop); } prop = (gchar *)xmlGetProp (xnode, "padding-left"); if (prop != NULL) { g_object_set (rpt_obj_text, "padding-left", strtod (prop, NULL), NULL); + xmlFree (prop); + } + prop = (gchar *)xmlGetProp (xnode, "ellipsize"); + if (prop != NULL) + { + g_object_set (rpt_obj_text, "ellipsize", rpt_common_strellipsize_to_enum (prop), NULL); + xmlFree (prop); } } } @@ -339,13 +336,16 @@ rpt_obj_text_get_xml (RptObject *rpt_objtext, xmlNode *xnode) { xmlSetProp (xnode, "padding-left", g_strdup_printf ("%f", priv->padding_left)); } + if (priv->ellipsize > RPT_ELLIPSIZE_NONE) + { + xmlSetProp (xnode, "ellipsize", rpt_common_enum_to_strellipsize (priv->ellipsize)); + } } static void rpt_obj_text_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { RptObjText *rpt_obj_text = RPT_OBJ_TEXT (object); - RptObjTextPrivate *priv = RPT_OBJ_TEXT_GET_PRIVATE (rpt_obj_text); switch (property_id) @@ -394,6 +394,10 @@ rpt_obj_text_set_property (GObject *object, guint property_id, const GValue *val priv->padding_left = g_value_get_double (value); break; + case PROP_ELLIPSIZE: + priv->ellipsize = g_value_get_uint (value); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -404,7 +408,6 @@ static void rpt_obj_text_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { RptObjText *rpt_obj_text = RPT_OBJ_TEXT (object); - RptObjTextPrivate *priv = RPT_OBJ_TEXT_GET_PRIVATE (rpt_obj_text); switch (property_id) @@ -453,6 +456,10 @@ rpt_obj_text_get_property (GObject *object, guint property_id, GValue *value, GP g_value_set_double (value, priv->padding_left); break; + case PROP_ELLIPSIZE: + g_value_set_uint (value, priv->ellipsize); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; diff --git a/src/rptprint.c b/src/rptprint.c index dab0b9e..e0fbac7 100644 --- a/src/rptprint.c +++ b/src/rptprint.c @@ -851,7 +851,7 @@ rpt_print_text_xml (RptPrint *rpt_print, xmlNode *xnode) } else { - pango_font_description_set_size (pfdesc, 12); + pango_font_description_set_size (pfdesc, 12 * PANGO_SCALE); } pango_layout_set_font_description (playout, pfdesc); @@ -910,6 +910,7 @@ rpt_print_text_xml (RptPrint *rpt_print, xmlNode *xnode) cairo_set_source_rgba (priv->cr, color->r, color->g, color->b, color->a); cairo_fill_preserve (priv->cr); } + if (prop != NULL) xmlFree (prop); /* drawing border */ rpt_print_border (rpt_print, position, size, border, rotation); @@ -958,6 +959,34 @@ rpt_print_text_xml (RptPrint *rpt_print, xmlNode *xnode) cairo_clip (priv->cr); } + /* ellipsize */ + prop = xmlGetProp (xnode, (const xmlChar *)"ellipsize"); + if (prop != NULL) + { + eRptEllipsize ellipsize = rpt_common_strellipsize_to_enum (prop); + if (ellipsize > RPT_ELLIPSIZE_NONE) + { + switch (ellipsize) + { + case RPT_ELLIPSIZE_START: + pango_layout_set_ellipsize (playout, PANGO_ELLIPSIZE_START); + break; + + case RPT_ELLIPSIZE_MIDDLE: + pango_layout_set_ellipsize (playout, PANGO_ELLIPSIZE_MIDDLE); + break; + + case RPT_ELLIPSIZE_END: + pango_layout_set_ellipsize (playout, PANGO_ELLIPSIZE_END); + break; + + default: + break; + } + } + xmlFree (prop); + } + /* drawing text */ if (font != NULL) { diff --git a/src/rptreport.c b/src/rptreport.c index caf144d..577b372 100644 --- a/src/rptreport.c +++ b/src/rptreport.c @@ -655,6 +655,7 @@ RptReport "source", g_strdup_printf ("[%s]", field_name), "size", size, "font", font, + "ellipsize", RPT_ELLIPSIZE_END, NULL); rpt_report_add_object_to_section (ret, obj, RPTREPORT_SECTION_BODY); -- 2.49.0