changeset 35:581a03196093

Support rectangle tag of SVG. - Change rectangle in X_main to sh_rect_t.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 06 Aug 2008 02:11:53 +0800
parents 07c523c799f4
children 51a20f240ce3
files src/X_main.c src/event.c src/mb_types.h src/redraw_man.c src/shape_rect.c src/shapes.h
diffstat 6 files changed, 128 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/X_main.c	Wed Aug 06 01:10:32 2008 +0800
+++ b/src/X_main.c	Wed Aug 06 02:11:53 2008 +0800
@@ -56,7 +56,7 @@
     cairo_t *tmpcr;
     cairo_surface_t *tmpsuf;
     redraw_man_t rdman;
-    shape_t *path1, *path2, *path3;
+    shape_t *path1, *path2, *rect;
     coord_t *coord1, *coord2, *coord3;
     paint_t *fill1, *fill2, *fill3;
     paint_t *stroke, *text_stroke;
@@ -115,9 +115,9 @@
     grad_stop_init(fill3_stops + 1, 0.5, 0, 1, 0, 0.5);
     grad_stop_init(fill3_stops + 2, 1, 0, 0, 1, 0.5);
     paint_linear_stops(fill3, 3, fill3_stops);
-    path3 = sh_path_new("M 50,50 L 50,150 L 150,150 L 150,50 z");
-    rdman_paint_fill(&rdman, fill3, path3);
-    rdman_add_shape(&rdman, (shape_t *)path3, rdman.root_coord);
+    rect = sh_rect_new(50, 50, 100, 100, 20, 20);
+    rdman_paint_fill(&rdman, fill3, rect);
+    rdman_add_shape(&rdman, (shape_t *)rect, rdman.root_coord);
 
     rdman_redraw_all(&rdman);
 
@@ -217,6 +217,7 @@
     redraw_man_destroy(&rdman);
     sh_path_free(path1);
     sh_path_free(path2);
+    sh_rect_free(rect);
     sh_text_free(text);
     cairo_destroy(tmpcr);
     cairo_surface_destroy(tmpsuf);
--- a/src/event.c	Wed Aug 06 01:10:32 2008 +0800
+++ b/src/event.c	Wed Aug 06 02:11:53 2008 +0800
@@ -78,6 +78,9 @@
     case SHT_TEXT:
 	sh_text_draw(shape, cr);
 	break;
+    case SHT_RECT:
+	sh_rect_draw(shape, cr);
+	break;
     }
 }
 
--- a/src/mb_types.h	Wed Aug 06 01:10:32 2008 +0800
+++ b/src/mb_types.h	Wed Aug 06 02:11:53 2008 +0800
@@ -120,7 +120,7 @@
     int stroke_linecap;
     int stroke_linejoin;
 };
-enum { SHT_UNKNOW, SHT_PATH, SHT_TEXT };
+enum { SHT_UNKNOW, SHT_PATH, SHT_TEXT, SHT_RECT };
 
 #define sh_attach_geo(sh, g)			\
     do {					\
--- a/src/redraw_man.c	Wed Aug 06 01:10:32 2008 +0800
+++ b/src/redraw_man.c	Wed Aug 06 02:11:53 2008 +0800
@@ -401,6 +401,9 @@
     case SHT_TEXT:
 	sh_text_transform(shape);
 	break;
+    case SHT_RECT:
+	sh_rect_transform(shape);
+	break;
 #ifdef UNITTEST
     default:
 	sh_dummy_transform(shape);
@@ -565,6 +568,9 @@
 	case SHT_TEXT:
 	    sh_text_draw(shape, rdman->cr);
 	    break;
+	case SHT_RECT:
+	    sh_rect_draw(shape, rdman->cr);
+	    break;
 #ifdef UNITTEST
 	default:
 	    sh_dummy_fill(shape, rdman->cr);
--- a/src/shape_rect.c	Wed Aug 06 01:10:32 2008 +0800
+++ b/src/shape_rect.c	Wed Aug 06 02:11:53 2008 +0800
@@ -1,16 +1,115 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include "mb_types.h"
 #include "shapes.h"
 
 typedef struct _sh_rect {
     shape_t shape;
     co_aix x, y;
+    co_aix w, h;
     co_aix rx, ry;
-    co_aix d_x, d_y;
-    co_aix d_rx, d_ry;
+    co_aix poses[12][2];
 } sh_rect_t;
 
-extern void sh_rect_transform(shape_t *shape) {
+shape_t *sh_rect_new(co_aix x, co_aix y, co_aix w, co_aix h,
+		    co_aix rx, co_aix ry) {
+    sh_rect_t *rect;
+
+    rect = (sh_rect_t *)malloc(sizeof(sh_rect_t));
+    if(rect == NULL)
+	return NULL;
+
+    memset(rect, 0, sizeof(sh_rect_t));
+
+    rect->shape.sh_type = SHT_RECT;
+    rect->x = x;
+    rect->y = y;
+    rect->w = w;
+    rect->h = h;
+    rect->rx = rx;
+    rect->ry = ry;
+
+    return (shape_t *)rect;
+}
+
+void sh_rect_free(shape_t *shape) {
+    free(shape);
+}
+
+void sh_rect_transform(shape_t *shape) {
     sh_rect_t *rect = (sh_rect_t *)shape;
+    co_aix x, y, w, h, rx, ry;
+    co_aix (*poses)[2];
+    co_aix width;
+    area_t *area;
+    int i;
 
+    x = rect->x;
+    y = rect->y;
+    w = rect->w;
+    h = rect->h;
+    rx = rect->rx;
+    ry = rect->ry;
+
+    poses = rect->poses;
+
+    poses[0][0] = x + w - rx;
+    poses[0][1] = y;
+    poses[1][0] = x + w;
+    poses[1][1] = y;
+    poses[2][0] = x + w;
+    poses[2][1] = y + ry;
+
+    poses[3][0] = x + w;
+    poses[3][1] = y + h - ry;
+    poses[4][0] = x + w;
+    poses[4][1] = y + h;
+    poses[5][0] = x + w - rx;
+    poses[5][1] = y + h;
+
+    poses[6][0] = x + rx;
+    poses[6][1] = y + h;
+    poses[7][0] = x;
+    poses[7][1] = y + h;
+    poses[8][0] = x;
+    poses[8][1] = y + h - ry;
+
+    poses[9][0] = x;
+    poses[9][1] = y + ry;
+    poses[10][0] = x;
+    poses[10][1] = y;
+    poses[11][0] = x + rx;
+    poses[11][1] = y;
+
+    for(i = 0; i < 12; i++)
+	coord_trans_pos(shape->coord, &poses[i][0], &poses[i][1]);
+
+    geo_from_positions(shape->geo, 12, poses);
+
+    if(shape->stroke) {
+	area = shape->geo->cur_area;
+	width = shape->stroke_width;
+	area->x -= width / 2 + 1;
+	area->y -= width / 2 + 1;
+	area->w += width + 2;
+	area->h += width + 2;
+    }
 }
+
+void sh_rect_draw(shape_t *shape, cairo_t *cr) {
+    sh_rect_t *rect = (sh_rect_t *)shape;
+    int i;
+    co_aix (*poses)[2];
+
+    poses = rect->poses;
+    cairo_move_to(cr, poses[11][0], poses[11][1]);
+    for(i = 0; i < 12; i += 3) {
+	cairo_line_to(cr, poses[i][0], poses[i][1]);
+	cairo_curve_to(cr,
+		       poses[i + 1][0], poses[i + 1][1],
+		       poses[i + 1][0], poses[i + 1][1],
+		       poses[i + 2][0], poses[i + 2][1]);
+    }
+    cairo_close_path(cr);
+}
--- a/src/shapes.h	Wed Aug 06 01:10:32 2008 +0800
+++ b/src/shapes.h	Wed Aug 06 02:11:53 2008 +0800
@@ -8,9 +8,15 @@
  *
  * A shape must include
  * - *_new() and *_free()
+ *   - clear memory for shape_t member.
  * - *_transform()
  * - *_draw()
  * - struct of shape must include an shape_t as type of first member.
+ * 
+ * Must modify
+ * - event.c:draw_shape_path
+ * - redraw_man.c:clean_shape
+ * - redraw_man.c:draw_shape
  */
 
 
@@ -26,5 +32,10 @@
 extern void sh_text_transform(shape_t *shape);
 extern void sh_text_draw(shape_t *shape, cairo_t *cr);
 
+extern shape_t *sh_rect_new(co_aix x, co_aix y, co_aix w, co_aix h,
+			    co_aix rx, co_aix ry);
+extern void sh_rect_free(shape_t *shape);
+extern void sh_rect_transform(shape_t *shape);
+extern void sh_rect_draw(shape_t *shape, cairo_t *cr);
 
 #endif /* __SHAPES_H_ */