From ff5fc27a329ff54ca16775c7f1013b37d51a56b5 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 28 Mar 2007 16:46:36 +0000 Subject: [PATCH] Added object ellipse. Extended RptObjImage, RptObjLine, RptObjText and RptObjRect. git-svn-id: svn+ssh://saetta.homelinux.org/svn/libreptool/trunk@9 3191ed1d-3fce-41bb-ab4a-0cebc0943b59 --- data/reptool.dtd | 10 +++++- data/reptool_report.dtd | 10 +++++- src/Makefile.am | 2 ++ src/rptcommon.c | 57 ++++++++++++++++++++++++++----- src/rptcommon.h | 7 ++++ src/rptobjectimage.c | 73 +++++++++++++++++++++++++++++++++++----- src/rptobjectimage.h | 6 ++++ src/rptobjectline.c | 24 +++++++++++-- src/rptobjectrect.c | 39 +++++++++++++++++++-- src/rptobjecttext.c | 4 +-- src/rptprint.c | 51 ++++++++++++++++++++++++++++ src/rptreport.c | 9 +++-- tests/test_report_db.rpt | 3 +- tests/test_rptprint.rptr | 1 + 14 files changed, 267 insertions(+), 29 deletions(-) diff --git a/data/reptool.dtd b/data/reptool.dtd index 35a7f41..a7b7ae1 100644 --- a/data/reptool.dtd +++ b/data/reptool.dtd @@ -1,5 +1,5 @@ + + + + + + 0.0) { xmlSetProp (xnode, "border-top-width", g_strdup_printf ("%f", border.top_width)); - xmlSetProp (xnode, "border-top-color", convert_to_str_color (border.top_color)); + xmlSetProp (xnode, "border-top-color", rpt_common_convert_to_str_color (border.top_color)); } if (border.right_width > 0.0) { xmlSetProp (xnode, "border-right-width", g_strdup_printf ("%f", border.right_width)); - xmlSetProp (xnode, "border-right-color", convert_to_str_color (border.right_color)); + xmlSetProp (xnode, "border-right-color", rpt_common_convert_to_str_color (border.right_color)); } if (border.bottom_width > 0.0) { xmlSetProp (xnode, "border-bottom-width", g_strdup_printf ("%f", border.bottom_width)); - xmlSetProp (xnode, "border-bottom-color", convert_to_str_color (border.bottom_color)); + xmlSetProp (xnode, "border-bottom-color", rpt_common_convert_to_str_color (border.bottom_color)); } if (border.left_width > 0.0) { xmlSetProp (xnode, "border-left-width", g_strdup_printf ("%f", border.left_width)); - xmlSetProp (xnode, "border-left-color", convert_to_str_color (border.left_color)); + xmlSetProp (xnode, "border-left-color", rpt_common_convert_to_str_color (border.left_color)); } } @@ -398,6 +423,22 @@ rpt_common_get_stroke (xmlNode *xnode, RptStroke *stroke) } } +/** + * rpt_common_set_stroke: + * @xnode: + * @stroke: + * + */ +void +rpt_common_set_stroke (xmlNode *xnode, RptStroke stroke) +{ + if (stroke.width != 0.0) + { + xmlSetProp (xnode, "stroke-width", g_strdup_printf ("%f", stroke.width)); + } + xmlSetProp (xnode, "stroke-color", rpt_common_convert_to_str_color (stroke.color)); +} + /** * rpt_common_parse_color: * @str_color: @@ -452,8 +493,8 @@ rpt_common_parse_color (const gchar *str_color, RptColor *color) } } -static gchar * -convert_to_str_color (RptColor color) +gchar * +rpt_common_convert_to_str_color (RptColor color) { gchar *ret = "#"; diff --git a/src/rptcommon.h b/src/rptcommon.h index cde6af7..5b1bd82 100644 --- a/src/rptcommon.h +++ b/src/rptcommon.h @@ -99,8 +99,12 @@ typedef struct void rpt_common_get_position (xmlNode *xnode, RptPoint *position); +void rpt_common_set_position (xmlNode *xnode, + RptPoint position); void rpt_common_get_size (xmlNode *xnode, RptSize *size); +void rpt_common_set_size (xmlNode *xnode, + RptSize size); void rpt_common_get_font (xmlNode *xnode, RptFont *font); void rpt_common_set_font (xmlNode *xnode, @@ -115,8 +119,11 @@ void rpt_common_set_align (xmlNode *xnode, RptAlign align); void rpt_common_get_stroke (xmlNode *xnode, RptStroke *stroke); +void rpt_common_set_stroke (xmlNode *xnode, + RptStroke stroke); void rpt_common_parse_color (const gchar *str_color, RptColor *color); +gchar *rpt_common_convert_to_str_color (RptColor color); G_END_DECLS diff --git a/src/rptobjectimage.c b/src/rptobjectimage.c index 8f201f8..b847d65 100644 --- a/src/rptobjectimage.c +++ b/src/rptobjectimage.c @@ -23,20 +23,22 @@ enum { PROP_0, PROP_SIZE, - PROP_SOURCE + PROP_BORDER, + PROP_SOURCE, + PROP_ADAPT }; static void rpt_obj_image_class_init (RptObjImageClass *klass); static void rpt_obj_image_init (RptObjImage *rpt_obj_image); static void rpt_obj_image_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec); + guint property_id, + const GValue *value, + GParamSpec *pspec); static void rpt_obj_image_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec); + guint property_id, + GValue *value, + GParamSpec *pspec); #define RPT_OBJ_IMAGE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_RPT_OBJ_IMAGE, RptObjImagePrivate)) @@ -45,7 +47,9 @@ typedef struct _RptObjImagePrivate RptObjImagePrivate; struct _RptObjImagePrivate { RptSize *size; + RptBorder *border; gchar *source; + guint adapt; }; GType @@ -94,12 +98,23 @@ rpt_obj_image_class_init (RptObjImageClass *klass) "Size", "The object's size.", G_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_BORDER, + g_param_spec_pointer ("border", + "Border", + "The object's border.", + G_PARAM_READWRITE)); g_object_class_install_property (object_class, PROP_SOURCE, g_param_spec_string ("source", "Source", "The image's source.", "", - G_PARAM_READWRITE)); + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, PROP_ADAPT, + g_param_spec_uint ("adapt", + "Adapt", + "Whether to adapt the image.", + RPT_OBJ_IMAGE_ADAPT_NONE, RPT_OBJ_IMAGE_ADAPT_TO_IMAGE, 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void @@ -111,7 +126,7 @@ rpt_obj_image_init (RptObjImage *rpt_obj_image) priv->size->width = 0.0; priv->size->height = 0.0; - priv->source = g_strdup (""); + priv->border = (RptBorder *)g_malloc0 (sizeof (RptBorder)); } /** @@ -168,8 +183,18 @@ RptObject priv = RPT_OBJ_IMAGE_GET_PRIVATE (rpt_obj_image); rpt_common_get_size (xnode, priv->size); + rpt_common_get_border (xnode, priv->border); priv->source = (gchar *)xmlGetProp (xnode, "source"); + + if (xmlStrcasecmp (xmlGetProp (xnode, "adapt"), (const xmlChar *)"to-box") == 0) + { + priv->adapt = RPT_OBJ_IMAGE_ADAPT_TO_BOX; + } + else if (xmlStrcasecmp (xmlGetProp (xnode, "adapt"), (const xmlChar *)"to-image") == 0) + { + priv->adapt = RPT_OBJ_IMAGE_ADAPT_TO_IMAGE; + } } } @@ -191,7 +216,21 @@ rpt_obj_image_get_xml (RptObject *rpt_objimage, xmlNode *xnode) xmlSetProp (xnode, "width", g_strdup_printf ("%f", priv->size->width)); xmlSetProp (xnode, "height", g_strdup_printf ("%f", priv->size->height)); + + rpt_common_set_border (xnode, *priv->border); + xmlSetProp (xnode, "source", priv->source); + + switch (priv->adapt) + { + case RPT_OBJ_IMAGE_ADAPT_TO_BOX: + xmlSetProp (xnode, "adapt", "to-box"); + break; + + case RPT_OBJ_IMAGE_ADAPT_TO_IMAGE: + xmlSetProp (xnode, "adapt", "to-image"); + break; + } } static void @@ -207,10 +246,18 @@ rpt_obj_image_set_property (GObject *object, guint property_id, const GValue *va priv->size = g_memdup (g_value_get_pointer (value), sizeof (RptSize)); break; + case PROP_BORDER: + priv->border = g_memdup (g_value_get_pointer (value), sizeof (RptBorder)); + break; + case PROP_SOURCE: priv->source = g_strstrip (g_strdup (g_value_get_string (value))); break; + case PROP_ADAPT: + priv->adapt = g_value_get_uint (value); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -230,10 +277,18 @@ rpt_obj_image_get_property (GObject *object, guint property_id, GValue *value, G g_value_set_pointer (value, g_memdup (priv->size, sizeof (RptSize))); break; + case PROP_BORDER: + g_value_set_pointer (value, g_memdup (priv->border, sizeof (RptBorder))); + break; + case PROP_SOURCE: g_value_set_string (value, priv->source); break; + case PROP_ADAPT: + g_value_set_uint (value, priv->adapt); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; diff --git a/src/rptobjectimage.h b/src/rptobjectimage.h index c30bf92..49e84bb 100644 --- a/src/rptobjectimage.h +++ b/src/rptobjectimage.h @@ -52,6 +52,12 @@ struct _RptObjImageClass GType rpt_obj_image_get_type (void) G_GNUC_CONST; +typedef enum +{ + RPT_OBJ_IMAGE_ADAPT_NONE, + RPT_OBJ_IMAGE_ADAPT_TO_BOX, + RPT_OBJ_IMAGE_ADAPT_TO_IMAGE +} eRptObjImageAdapt; RptObject *rpt_obj_image_new (const gchar *name, RptPoint position); RptObject *rpt_obj_image_new_from_xml (xmlNode *xnode); diff --git a/src/rptobjectline.c b/src/rptobjectline.c index 4bfc138..55a5ac7 100644 --- a/src/rptobjectline.c +++ b/src/rptobjectline.c @@ -22,7 +22,8 @@ enum { PROP_0, - PROP_SIZE + PROP_SIZE, + PROP_STROKE }; static void rpt_obj_line_class_init (RptObjLineClass *klass); @@ -44,6 +45,7 @@ typedef struct _RptObjLinePrivate RptObjLinePrivate; struct _RptObjLinePrivate { RptSize *size; + RptStroke *stroke; }; GType @@ -92,6 +94,11 @@ rpt_obj_line_class_init (RptObjLineClass *klass) "Size", "The object's size.", G_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_STROKE, + g_param_spec_pointer ("stroke", + "Stroke", + "The object's stroke.", + G_PARAM_READWRITE)); } static void @@ -102,6 +109,8 @@ rpt_obj_line_init (RptObjLine *rpt_obj_line) priv->size = (RptSize *)g_malloc0 (sizeof (RptSize)); priv->size->width = 0.0; priv->size->height = 0.0; + + priv->stroke = (RptStroke *)g_malloc0 (sizeof (RptStroke)); } /** @@ -158,6 +167,7 @@ RptObject priv = RPT_OBJ_LINE_GET_PRIVATE (rpt_obj_line); rpt_common_get_size (xnode, priv->size); + rpt_common_get_stroke (xnode, priv->stroke); } } @@ -177,8 +187,8 @@ rpt_obj_line_get_xml (RptObject *rpt_objline, xmlNode *xnode) xmlNodeSetName (xnode, "line"); - xmlSetProp (xnode, "width", g_strdup_printf ("%f", priv->size->width)); - xmlSetProp (xnode, "height", g_strdup_printf ("%f", priv->size->height)); + rpt_common_set_size (xnode, *priv->size); + rpt_common_set_stroke (xnode, *priv->stroke); } static void @@ -194,6 +204,10 @@ rpt_obj_line_set_property (GObject *object, guint property_id, const GValue *val priv->size = g_memdup (g_value_get_pointer (value), sizeof (RptSize)); break; + case PROP_STROKE: + priv->stroke = g_memdup (g_value_get_pointer (value), sizeof (RptStroke)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -213,6 +227,10 @@ rpt_obj_line_get_property (GObject *object, guint property_id, GValue *value, GP g_value_set_pointer (value, g_memdup (priv->size, sizeof (RptSize))); break; + case PROP_STROKE: + g_value_set_pointer (value, g_memdup (priv->stroke, sizeof (RptStroke))); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; diff --git a/src/rptobjectrect.c b/src/rptobjectrect.c index a47bc64..f158eb5 100644 --- a/src/rptobjectrect.c +++ b/src/rptobjectrect.c @@ -21,7 +21,8 @@ enum { - PROP_0 + PROP_0, + PROP_FILL_COLOR }; static void rpt_obj_rect_class_init (RptObjRectClass *klass); @@ -42,6 +43,7 @@ static void rpt_obj_rect_get_property (GObject *object, typedef struct _RptObjRectPrivate RptObjRectPrivate; struct _RptObjRectPrivate { + RptColor *fill_color; }; GType @@ -84,12 +86,20 @@ rpt_obj_rect_class_init (RptObjRectClass *klass) object_class->get_property = rpt_obj_rect_get_property; rptobject_class->get_xml = rpt_obj_rect_get_xml; + + g_object_class_install_property (object_class, PROP_FILL_COLOR, + g_param_spec_pointer ("fill-color", + "Fill Color", + "The object's fill color.", + G_PARAM_READWRITE)); } static void rpt_obj_rect_init (RptObjRect *rpt_obj_rect) { RptObjRectPrivate *priv = RPT_OBJ_RECT_GET_PRIVATE (rpt_obj_rect); + + priv->fill_color = (RptColor *)g_malloc0 (sizeof (RptColor)); } /** @@ -143,12 +153,24 @@ RptObject if (rpt_obj_rect != NULL) { + const gchar *prop; RptSize size; + RptStroke stroke; priv = RPT_OBJ_RECT_GET_PRIVATE (rpt_obj_rect); rpt_common_get_size (xnode, &size); - g_object_set (G_OBJECT (rpt_obj_rect), "size", &size, NULL); + rpt_common_get_stroke (xnode, &stroke); + g_object_set (G_OBJECT (rpt_obj_rect), + "size", &size, + "stroke", &stroke, + NULL); + + prop = (const gchar *)xmlGetProp (xnode, "fill-color"); + if (prop != NULL) + { + rpt_common_parse_color (prop, priv->fill_color); + } } } @@ -169,6 +191,11 @@ rpt_obj_rect_get_xml (RptObject *rpt_object, xmlNode *xnode) rpt_obj_line_get_xml (rpt_object, xnode); xmlNodeSetName (xnode, "rect"); + + if (priv->fill_color != NULL) + { + xmlSetProp (xnode, "fill-color", rpt_common_convert_to_str_color (*priv->fill_color)); + } } static void @@ -180,6 +207,10 @@ rpt_obj_rect_set_property (GObject *object, guint property_id, const GValue *val switch (property_id) { + case PROP_FILL_COLOR: + priv->fill_color = g_memdup (g_value_get_pointer (value), sizeof (RptColor)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -195,6 +226,10 @@ rpt_obj_rect_get_property (GObject *object, guint property_id, GValue *value, GP switch (property_id) { + case PROP_FILL_COLOR: + g_value_set_pointer (value, g_memdup (priv->fill_color, sizeof (RptColor))); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; diff --git a/src/rptobjecttext.c b/src/rptobjecttext.c index 56c8b9f..9cb0631 100644 --- a/src/rptobjecttext.c +++ b/src/rptobjecttext.c @@ -120,7 +120,7 @@ rpt_obj_text_class_init (RptObjTextClass *klass) "Source", "The source.", "", - G_PARAM_READWRITE)); + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void @@ -213,7 +213,7 @@ rpt_obj_text_get_xml (RptObject *rpt_objtext, xmlNode *xnode) rpt_common_set_border (xnode, (RptBorder)*priv->border); rpt_common_set_font (xnode, (RptFont)*priv->font); - rpt_common_set_align (xnode, (RptAlign)*priv->font); + rpt_common_set_align (xnode, (RptAlign)*priv->align); } static void diff --git a/src/rptprint.c b/src/rptprint.c index 95baa70..76e60e9 100644 --- a/src/rptprint.c +++ b/src/rptprint.c @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -53,6 +54,8 @@ static void rpt_print_line_xml (RptPrint *rpt_print, xmlNode *xnode); static void rpt_print_rect_xml (RptPrint *rpt_print, xmlNode *xnode); +static void rpt_print_ellipse_xml (RptPrint *rpt_print, + xmlNode *xnode); static void rpt_print_image_xml (RptPrint *rpt_print, xmlNode *xnode); static void rpt_print_line (RptPrint *rpt_print, @@ -314,6 +317,8 @@ rpt_print_get_property (GObject *object, guint property_id, GValue *value, GPara static void rpt_print_page (RptPrint *rpt_print, xmlNode *xnode) { + RptPrintPrivate *priv = RPT_PRINT_GET_PRIVATE (rpt_print); + xmlNode *cur = xnode->children; while (cur != NULL) @@ -330,6 +335,10 @@ rpt_print_page (RptPrint *rpt_print, xmlNode *xnode) { rpt_print_rect_xml (rpt_print, cur); } + else if (strcmp (cur->name, "ellipse") == 0) + { + rpt_print_ellipse_xml (rpt_print, cur); + } else if (strcmp (cur->name, "image") == 0) { rpt_print_image_xml (rpt_print, cur); @@ -538,6 +547,48 @@ rpt_print_rect_xml (RptPrint *rpt_print, xmlNode *xnode) cairo_stroke (priv->cr); } +static void +rpt_print_ellipse_xml (RptPrint *rpt_print, xmlNode *xnode) +{ + RptPoint position; + RptSize size; + RptStroke stroke; + RptColor fill_color; + + RptPrintPrivate *priv = RPT_PRINT_GET_PRIVATE (rpt_print); + + rpt_common_get_position (xnode, &position); + rpt_common_get_size (xnode, &size); + rpt_common_get_stroke (xnode, &stroke); + + gchar *prop = xmlGetProp (xnode, (const xmlChar *)"fill-color"); + if (prop != NULL) + { + fill_color.r = 0.0; + fill_color.g = 0.0; + fill_color.b = 0.0; + fill_color.a = 1.0; + rpt_common_parse_color (prop, &fill_color); + } + + cairo_new_path (priv->cr); + + cairo_save (priv->cr); + cairo_translate (priv->cr, position.x, position.y); + cairo_scale (priv->cr, size.width, size.height); + cairo_arc (priv->cr, 0., 0., 1., 0., 2. * M_PI); + cairo_restore (priv->cr); + + if (prop != NULL) + { + cairo_set_source_rgba (priv->cr, fill_color.r, fill_color.g, fill_color.b, fill_color.a); + cairo_fill_preserve (priv->cr); + } + + cairo_set_source_rgba (priv->cr, stroke.color.r, stroke.color.g, stroke.color.b, stroke.color.a); + cairo_stroke (priv->cr); +} + static void rpt_print_image_xml (RptPrint *rpt_print, xmlNode *xnode) { diff --git a/src/rptreport.c b/src/rptreport.c index aed82f1..f27dc8f 100644 --- a/src/rptreport.c +++ b/src/rptreport.c @@ -32,6 +32,7 @@ #include "rptobjecttext.h" #include "rptobjectline.h" #include "rptobjectrect.h" +#include "rptobjectellipse.h" #include "rptobjectimage.h" typedef enum @@ -533,6 +534,10 @@ rpt_report_xml_parse_section (RptReport *rpt_report, xmlNode *xnode, RptReportSe { rptobj = rpt_obj_rect_new_from_xml (cur); } + else if (strcmp (cur->name, "ellipse") == 0) + { + rptobj = rpt_obj_ellipse_new_from_xml (cur); + } else if (strcmp (cur->name, "image") == 0) { rptobj = rpt_obj_image_new_from_xml (cur); @@ -625,10 +630,10 @@ static xmlNode RptReportPrivate *priv = RPT_REPORT_GET_PRIVATE (rpt_report); xnode = xmlNewNode (NULL, "page"); - xmlSetProp (xnode, "width", g_strdup_printf ("%f", priv->page->size->width)); - xmlSetProp (xnode, "height", g_strdup_printf ("%f", priv->page->size->height)); xmlAddChild (xroot, xnode); + rpt_common_set_size (xnode, *priv->page->size); + return xnode; } diff --git a/tests/test_report_db.rpt b/tests/test_report_db.rpt index b1fb59d..b30d30f 100644 --- a/tests/test_report_db.rpt +++ b/tests/test_report_db.rpt @@ -16,12 +16,13 @@ - + + diff --git a/tests/test_rptprint.rptr b/tests/test_rptprint.rptr index fc5781f..3763a03 100644 --- a/tests/test_rptprint.rptr +++ b/tests/test_rptprint.rptr @@ -10,5 +10,6 @@ Text sample on second page + -- 2.49.0