1 #define __SP_CANVAS_BPATH_C__
3 /*
4 * Simple bezier bpath CanvasItem for inkscape
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@ximian.com>
8 *
9 * Copyright (C) 2001 Lauris Kaplinski and Ximian, Inc.
10 *
11 * Released under GNU GPL
12 *
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "color.h"
19 #include "sp-canvas-util.h"
20 #include "inkscape-cairo.h"
21 #include "canvas-bpath.h"
22 #include "display/display-forward.h"
23 #include "display/curve.h"
24 #include "display/inkscape-cairo.h"
25 #include <libnr/nr-pixops.h>
26 #include "helper/geom.h"
28 /*
29 * FIXME: The following #includes and some other content in this file (see below) should actually
30 * be in a separate file called display/canvas-text.cpp. It temporarily had to be moved here
31 * because of linker errors.
32 */
34 /**
35 #include "display-forward.h"
36 #include "sp-canvas-util.h"
37 #include "canvas-text.h"
38 #include "display/inkscape-cairo.h"
39 **/
40 #include <sstream>
41 #include <string.h>
42 #include <desktop.h>
44 /**
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48 #include <color.h>
50 #include <libnr/nr-pixops.h>
51 **/
53 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
55 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
56 static void sp_canvas_bpath_init (SPCanvasBPath *path);
57 static void sp_canvas_bpath_destroy (GtkObject *object);
59 static void sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
60 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
61 static double sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
63 static SPCanvasItemClass *parent_class;
65 GtkType
66 sp_canvas_bpath_get_type (void)
67 {
68 static GtkType type = 0;
69 if (!type) {
70 GtkTypeInfo info = {
71 (gchar *)"SPCanvasBPath",
72 sizeof (SPCanvasBPath),
73 sizeof (SPCanvasBPathClass),
74 (GtkClassInitFunc) sp_canvas_bpath_class_init,
75 (GtkObjectInitFunc) sp_canvas_bpath_init,
76 NULL, NULL, NULL
77 };
78 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
79 }
80 return type;
81 }
83 static void
84 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
85 {
86 GtkObjectClass *object_class;
87 SPCanvasItemClass *item_class;
89 object_class = GTK_OBJECT_CLASS (klass);
90 item_class = (SPCanvasItemClass *) klass;
92 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
94 object_class->destroy = sp_canvas_bpath_destroy;
96 item_class->update = sp_canvas_bpath_update;
97 item_class->render = sp_canvas_bpath_render;
98 item_class->point = sp_canvas_bpath_point;
99 }
101 static void
102 sp_canvas_bpath_init (SPCanvasBPath * bpath)
103 {
104 bpath->fill_rgba = 0x00000000;
105 bpath->fill_rule = SP_WIND_RULE_EVENODD;
107 bpath->stroke_rgba = 0x00000000;
108 bpath->stroke_width = 1.0;
109 bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
110 bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
111 bpath->stroke_miterlimit = 11.0;
112 }
114 static void
115 sp_canvas_bpath_destroy (GtkObject *object)
116 {
117 SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
119 if (cbp->curve) {
120 cbp->curve = cbp->curve->unref();
121 }
123 if (GTK_OBJECT_CLASS (parent_class)->destroy)
124 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
125 }
127 static void
128 sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
129 {
130 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
132 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
134 if (((SPCanvasItemClass *) parent_class)->update)
135 ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
137 sp_canvas_item_reset_bounds (item);
139 if (!cbp->curve) return;
141 cbp->affine = affine;
143 Geom::OptRect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
145 if (bbox) {
146 item->x1 = (int)bbox->min()[Geom::X] - 1;
147 item->y1 = (int)bbox->min()[Geom::Y] - 1;
148 item->x2 = (int)bbox->max()[Geom::X] + 1;
149 item->y2 = (int)bbox->max()[Geom::Y] + 1;
150 } else {
151 item->x1 = 0;
152 item->y1 = 0;
153 item->x2 = 0;
154 item->y2 = 0;
155 }
156 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
157 }
159 static void
160 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
161 {
162 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
164 sp_canvas_prepare_buffer(buf);
166 Geom::Rect area (Geom::Point(buf->rect.x0, buf->rect.y0), Geom::Point(buf->rect.x1, buf->rect.y1));
168 if ( !cbp->curve ||
169 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
170 cbp->curve->get_segment_count() < 1)
171 return;
173 if (!buf->ct)
174 return;
176 bool dofill = ((cbp->fill_rgba & 0xff) != 0);
177 bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
179 cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
180 cairo_new_path(buf->ct);
182 if (!dofill)
183 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
184 else
185 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
187 if (dofill) {
188 // RGB / BGR
189 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cbp->fill_rgba), SP_RGBA32_G_F(cbp->fill_rgba), SP_RGBA32_R_F(cbp->fill_rgba), SP_RGBA32_A_F(cbp->fill_rgba));
190 cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
191 : CAIRO_FILL_RULE_WINDING);
192 if (dostroke)
193 cairo_fill_preserve(buf->ct);
194 else
195 cairo_fill(buf->ct);
196 }
198 if (dostroke) {
199 // RGB / BGR
200 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(cbp->stroke_rgba), SP_RGBA32_G_F(cbp->stroke_rgba), SP_RGBA32_R_F(cbp->stroke_rgba), SP_RGBA32_A_F(cbp->stroke_rgba));
201 cairo_set_line_width(buf->ct, 1);
202 if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
203 cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
204 }
205 cairo_stroke(buf->ct);
206 }
207 }
209 static double
210 sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
211 {
212 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
214 if ( !cbp->curve ||
215 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
216 cbp->curve->get_segment_count() < 1)
217 return NR_HUGE;
219 double width = 0.5;
220 Geom::Rect viewbox = item->canvas->getViewbox();
221 viewbox.expandBy (width);
222 double dist = NR_HUGE;
223 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
225 if (dist <= 1.0) {
226 *actual_item = item;
227 }
229 return dist;
230 }
232 SPCanvasItem *
233 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
234 {
235 g_return_val_if_fail (parent != NULL, NULL);
236 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
238 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
240 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
242 return item;
243 }
245 void
246 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
247 {
248 g_return_if_fail (cbp != NULL);
249 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
251 if (cbp->curve) {
252 cbp->curve = cbp->curve->unref();
253 }
255 if (curve) {
256 cbp->curve = curve->ref();
257 }
259 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
260 }
262 void
263 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
264 {
265 g_return_if_fail (cbp != NULL);
266 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
268 cbp->fill_rgba = rgba;
269 cbp->fill_rule = rule;
271 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
272 }
274 void
275 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
276 {
277 g_return_if_fail (cbp != NULL);
278 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
280 cbp->stroke_rgba = rgba;
281 cbp->stroke_width = MAX (width, 0.1);
282 cbp->stroke_linejoin = join;
283 cbp->stroke_linecap = cap;
284 cbp->dashes[0] = dash;
285 cbp->dashes[1] = gap;
287 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
288 }
292 /*
293 * FIXME: The following code should actually be in a separate file called
294 * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
295 */
297 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
298 static void sp_canvastext_init (SPCanvasText *canvastext);
299 static void sp_canvastext_destroy (GtkObject *object);
301 static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
302 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
304 static SPCanvasItemClass *parent_class_ct;
306 GtkType
307 sp_canvastext_get_type (void)
308 {
309 static GtkType type = 0;
311 if (!type) {
312 GtkTypeInfo info = {
313 (gchar *)"SPCanvasText",
314 sizeof (SPCanvasText),
315 sizeof (SPCanvasTextClass),
316 (GtkClassInitFunc) sp_canvastext_class_init,
317 (GtkObjectInitFunc) sp_canvastext_init,
318 NULL, NULL, NULL
319 };
320 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
321 }
322 return type;
323 }
325 static void
326 sp_canvastext_class_init (SPCanvasTextClass *klass)
327 {
328 GtkObjectClass *object_class = (GtkObjectClass *) klass;
329 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
331 parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
333 object_class->destroy = sp_canvastext_destroy;
335 item_class->update = sp_canvastext_update;
336 item_class->render = sp_canvastext_render;
337 }
339 static void
340 sp_canvastext_init (SPCanvasText *canvastext)
341 {
342 canvastext->rgba = 0x0000ff7f;
343 canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
344 canvastext->affine = Geom::identity();
345 canvastext->fontsize = 10.0;
346 canvastext->item = NULL;
347 canvastext->desktop = NULL;
348 canvastext->text = NULL;
349 }
351 static void
352 sp_canvastext_destroy (GtkObject *object)
353 {
354 g_return_if_fail (object != NULL);
355 g_return_if_fail (SP_IS_CANVASTEXT (object));
357 SPCanvasText *canvastext = SP_CANVASTEXT (object);
359 canvastext->item=NULL;
361 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
362 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
363 }
365 // FIXME: remove this as soon as we know how to correctly determine the text extent
366 static const double arbitrary_factor = 0.7;
368 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
369 static double anchor_offset_x = 0;
370 static double anchor_offset_y = 0;
372 static void
373 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
374 {
375 SPCanvasText *cl = SP_CANVASTEXT (item);
377 if (!buf->ct)
378 return;
380 guint32 rgba = cl->rgba;
381 cairo_set_source_rgba(buf->ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
383 Geom::Point s = cl->s * cl->affine;
384 double offsetx = s[Geom::X] - buf->rect.x0;
385 double offsety = s[Geom::Y] - buf->rect.y0;
386 offsetx -= anchor_offset_x;
387 offsety += anchor_offset_y;
389 cairo_move_to(buf->ct, offsetx, offsety);
390 cairo_set_font_size(buf->ct, cl->fontsize);
391 cairo_show_text(buf->ct, cl->text);
392 cairo_stroke(buf->ct);
394 cairo_new_path(buf->ct);
395 }
397 static void
398 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
399 {
400 SPCanvasText *cl = SP_CANVASTEXT (item);
402 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
404 if (parent_class_ct->update)
405 (* parent_class_ct->update) (item, affine, flags);
407 sp_canvas_item_reset_bounds (item);
409 cl->affine = affine;
411 Geom::Point s = cl->s * affine;
413 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
414 // method but update() seems to be called before so we don't have the information available when we need it
415 /**
416 cairo_t tmp_buf;
417 cairo_text_extents_t bbox;
418 cairo_text_extents(&tmp_buf, cl->text, &bbox);
419 **/
420 item->x1 = s[Geom::X] + 0;
421 item->y1 = s[Geom::Y] - cl->fontsize;
422 item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
423 item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
425 // adjust update region according to anchor shift
426 // FIXME: use the correct text extent
427 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
428 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
429 item->x1 -= anchor_offset_x;
430 item->x2 -= anchor_offset_x;
431 item->y1 += anchor_offset_y;
432 item->y2 += anchor_offset_y;
434 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
435 }
437 SPCanvasItem *
438 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
439 {
440 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
442 SPCanvasText *ct = SP_CANVASTEXT(item);
444 ct->desktop = desktop;
446 ct->s = pos;
447 g_free(ct->text);
448 ct->text = g_strdup(new_text);
450 // TODO: anything else to do?
452 return item;
453 }
456 void
457 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
458 {
459 g_return_if_fail (ct != NULL);
460 g_return_if_fail (SP_IS_CANVASTEXT (ct));
462 if (rgba != ct->rgba) {
463 SPCanvasItem *item;
464 ct->rgba = rgba;
465 item = SP_CANVAS_ITEM (ct);
466 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
467 }
468 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
469 }
471 #define EPSILON 1e-6
472 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
474 void
475 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
476 {
477 sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
478 }
480 void
481 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
482 {
483 Geom::Point pos = ct->desktop->doc2dt(start);
485 g_return_if_fail (ct != NULL);
486 g_return_if_fail (SP_IS_CANVASTEXT (ct));
488 if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
489 ct->s[Geom::X] = pos[0];
490 ct->s[Geom::Y] = pos[1];
491 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
492 }
493 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
494 }
496 void
497 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
498 {
499 g_free (ct->text);
500 ct->text = g_strdup(new_text);
501 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
502 }
504 void
505 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
506 {
507 std::ostringstream number;
508 number << num;
509 sp_canvastext_set_text(ct, number.str().c_str());
510 }
512 void
513 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
514 {
515 ct->fontsize = size;
516 }
518 void
519 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
520 {
521 ct->anchor_x = anchor_x;
522 ct->anchor_y = anchor_y;
523 }
525 /*
526 Local Variables:
527 mode:c++
528 c-file-style:"stroustrup"
529 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
530 indent-tabs-mode:nil
531 fill-column:99
532 End:
533 */
534 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :