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-matrix-fns.h"
26 #include <libnr/nr-pixops.h>
27 #include <libnr/nr-convert2geom.h>
28 #include <libnr/nr-path.h>
29 #include "helper/geom.h"
31 /*
32 * FIXME: The following #includes and some other content in this file (see below) should actually
33 * be in a separate file called display/canvas-text.cpp. It temporarily had to be moved here
34 * because of linker errors.
35 */
37 /**
38 #include "display-forward.h"
39 #include "sp-canvas-util.h"
40 #include "canvas-text.h"
41 #include "display/inkscape-cairo.h"
42 **/
43 #include <sstream>
44 #include <string.h>
45 #include <desktop.h>
47 /**
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
51 #include <color.h>
53 #include <libnr/nr-matrix-fns.h>
54 #include <libnr/nr-pixops.h>
55 **/
57 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
59 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
60 static void sp_canvas_bpath_init (SPCanvasBPath *path);
61 static void sp_canvas_bpath_destroy (GtkObject *object);
63 static void sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
64 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
65 static double sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item);
67 static SPCanvasItemClass *parent_class;
69 GtkType
70 sp_canvas_bpath_get_type (void)
71 {
72 static GtkType type = 0;
73 if (!type) {
74 GtkTypeInfo info = {
75 (gchar *)"SPCanvasBPath",
76 sizeof (SPCanvasBPath),
77 sizeof (SPCanvasBPathClass),
78 (GtkClassInitFunc) sp_canvas_bpath_class_init,
79 (GtkObjectInitFunc) sp_canvas_bpath_init,
80 NULL, NULL, NULL
81 };
82 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
83 }
84 return type;
85 }
87 static void
88 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
89 {
90 GtkObjectClass *object_class;
91 SPCanvasItemClass *item_class;
93 object_class = GTK_OBJECT_CLASS (klass);
94 item_class = (SPCanvasItemClass *) klass;
96 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
98 object_class->destroy = sp_canvas_bpath_destroy;
100 item_class->update = sp_canvas_bpath_update;
101 item_class->render = sp_canvas_bpath_render;
102 item_class->point = sp_canvas_bpath_point;
103 }
105 static void
106 sp_canvas_bpath_init (SPCanvasBPath * bpath)
107 {
108 bpath->fill_rgba = 0x00000000;
109 bpath->fill_rule = SP_WIND_RULE_EVENODD;
111 bpath->stroke_rgba = 0x00000000;
112 bpath->stroke_width = 1.0;
113 bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
114 bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
115 bpath->stroke_miterlimit = 11.0;
116 }
118 static void
119 sp_canvas_bpath_destroy (GtkObject *object)
120 {
121 SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
123 if (cbp->curve) {
124 cbp->curve = cbp->curve->unref();
125 }
127 if (GTK_OBJECT_CLASS (parent_class)->destroy)
128 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
129 }
131 static void
132 sp_canvas_bpath_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
133 {
134 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
136 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
138 if (((SPCanvasItemClass *) parent_class)->update)
139 ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
141 sp_canvas_item_reset_bounds (item);
143 if (!cbp->curve) return;
145 cbp->affine = affine;
147 Geom::Rect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), to_2geom(affine));
149 item->x1 = (int)bbox.min()[Geom::X] - 1;
150 item->y1 = (int)bbox.min()[Geom::Y] - 1;
151 item->x2 = (int)bbox.max()[Geom::X] + 1;
152 item->y2 = (int)bbox.max()[Geom::Y] + 1;
154 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
155 }
157 static void
158 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
159 {
160 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
162 sp_canvas_prepare_buffer(buf);
164 NR::Rect area (NR::Point(buf->rect.x0, buf->rect.y0), NR::Point(buf->rect.x1, buf->rect.y1));
166 if ( !cbp->curve ||
167 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
168 cbp->curve->get_segment_count() < 1)
169 return;
171 if (!buf->ct)
172 return;
174 bool dofill = ((cbp->fill_rgba & 0xff) != 0);
175 bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
177 cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
178 cairo_new_path(buf->ct);
180 if (!dofill)
181 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, true, 1);
182 else
183 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), to_2geom(cbp->affine), area, false, 1);
185 if (dofill) {
186 // RGB / BGR
187 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));
188 cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
189 : CAIRO_FILL_RULE_WINDING);
190 if (dostroke)
191 cairo_fill_preserve(buf->ct);
192 else
193 cairo_fill(buf->ct);
194 }
196 if (dostroke) {
197 // RGB / BGR
198 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));
199 cairo_set_line_width(buf->ct, 1);
200 cairo_stroke(buf->ct);
201 }
202 }
204 static double
205 sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
206 {
207 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
209 if ( !cbp->curve ||
210 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
211 cbp->curve->get_segment_count() < 1)
212 return NR_HUGE;
214 double width = 0.5;
215 Geom::Rect viewbox = to_2geom(item->canvas->getViewbox());
216 viewbox.expandBy (width);
217 double dist = NR_HUGE;
218 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), to_2geom(cbp->affine), to_2geom(p), NULL, NULL, &dist, 0.5, &viewbox);
220 if (dist <= 1.0) {
221 *actual_item = item;
222 }
224 return dist;
225 }
227 SPCanvasItem *
228 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
229 {
230 g_return_val_if_fail (parent != NULL, NULL);
231 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
233 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
235 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
237 return item;
238 }
240 void
241 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
242 {
243 g_return_if_fail (cbp != NULL);
244 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
246 if (cbp->curve) {
247 cbp->curve = cbp->curve->unref();
248 }
250 if (curve) {
251 cbp->curve = curve->ref();
252 }
254 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
255 }
257 void
258 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
259 {
260 g_return_if_fail (cbp != NULL);
261 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
263 cbp->fill_rgba = rgba;
264 cbp->fill_rule = rule;
266 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
267 }
269 void
270 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap)
271 {
272 g_return_if_fail (cbp != NULL);
273 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
275 cbp->stroke_rgba = rgba;
276 cbp->stroke_width = MAX (width, 0.1);
277 cbp->stroke_linejoin = join;
278 cbp->stroke_linecap = cap;
280 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
281 }
285 /*
286 * FIXME: The following code should actually be in a separate file called
287 * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
288 */
290 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
291 static void sp_canvastext_init (SPCanvasText *canvastext);
292 static void sp_canvastext_destroy (GtkObject *object);
294 static void sp_canvastext_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
295 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
297 static SPCanvasItemClass *parent_class_ct;
299 GtkType
300 sp_canvastext_get_type (void)
301 {
302 static GtkType type = 0;
304 if (!type) {
305 GtkTypeInfo info = {
306 (gchar *)"SPCanvasText",
307 sizeof (SPCanvasText),
308 sizeof (SPCanvasTextClass),
309 (GtkClassInitFunc) sp_canvastext_class_init,
310 (GtkObjectInitFunc) sp_canvastext_init,
311 NULL, NULL, NULL
312 };
313 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
314 }
315 return type;
316 }
318 static void
319 sp_canvastext_class_init (SPCanvasTextClass *klass)
320 {
321 GtkObjectClass *object_class = (GtkObjectClass *) klass;
322 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
324 parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
326 object_class->destroy = sp_canvastext_destroy;
328 item_class->update = sp_canvastext_update;
329 item_class->render = sp_canvastext_render;
330 }
332 static void
333 sp_canvastext_init (SPCanvasText *canvastext)
334 {
335 canvastext->rgba = 0x0000ff7f;
336 canvastext->s[NR::X] = canvastext->s[NR::Y] = 0.0;
337 canvastext->affine = NR::identity();
338 canvastext->fontsize = 10.0;
339 canvastext->item = NULL;
340 canvastext->desktop = NULL;
341 canvastext->text = NULL;
342 }
344 static void
345 sp_canvastext_destroy (GtkObject *object)
346 {
347 g_return_if_fail (object != NULL);
348 g_return_if_fail (SP_IS_CANVASTEXT (object));
350 SPCanvasText *canvastext = SP_CANVASTEXT (object);
352 canvastext->item=NULL;
354 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
355 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
356 }
358 // FIXME: remove this as soon as we know how to correctly determine the text extent
359 static const double arbitrary_factor = 0.7;
361 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
362 static double anchor_offset_x = 0;
363 static double anchor_offset_y = 0;
365 static void
366 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
367 {
368 SPCanvasText *cl = SP_CANVASTEXT (item);
370 if (!buf->ct)
371 return;
373 guint32 rgba = cl->rgba;
374 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));
376 NR::Point s = cl->s * cl->affine;
377 double offsetx = s[NR::X] - buf->rect.x0;
378 double offsety = s[NR::Y] - buf->rect.y0;
379 offsetx -= anchor_offset_x;
380 offsety += anchor_offset_y;
382 cairo_move_to(buf->ct, offsetx, offsety);
383 cairo_set_font_size(buf->ct, cl->fontsize);
384 cairo_show_text(buf->ct, cl->text);
385 cairo_stroke(buf->ct);
387 cairo_new_path(buf->ct);
388 }
390 static void
391 sp_canvastext_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
392 {
393 SPCanvasText *cl = SP_CANVASTEXT (item);
395 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
397 if (parent_class_ct->update)
398 (* parent_class_ct->update) (item, affine, flags);
400 sp_canvas_item_reset_bounds (item);
402 cl->affine = affine;
404 NR::Point s = cl->s * affine;
406 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
407 // method but update() seems to be called before so we don't have the information available when we need it
408 /**
409 cairo_t tmp_buf;
410 cairo_text_extents_t bbox;
411 cairo_text_extents(&tmp_buf, cl->text, &bbox);
412 **/
413 item->x1 = s[NR::X] + 0;
414 item->y1 = s[NR::Y] - cl->fontsize;
415 item->x2 = s[NR::X] + cl->fontsize * strlen(cl->text);
416 item->y2 = s[NR::Y] + cl->fontsize * 0.5; // for letters below the baseline
418 // adjust update region according to anchor shift
419 // FIXME: use the correct text extent
420 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
421 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
422 item->x1 -= anchor_offset_x;
423 item->x2 -= anchor_offset_x;
424 item->y1 += anchor_offset_y;
425 item->y2 += anchor_offset_y;
427 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
428 }
430 SPCanvasItem *
431 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
432 {
433 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
435 SPCanvasText *ct = SP_CANVASTEXT(item);
437 ct->desktop = desktop;
439 ct->s = pos;
440 g_free(ct->text);
441 ct->text = g_strdup(new_text);
443 // TODO: anything else to do?
445 return item;
446 }
449 void
450 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
451 {
452 g_return_if_fail (ct != NULL);
453 g_return_if_fail (SP_IS_CANVASTEXT (ct));
455 if (rgba != ct->rgba) {
456 SPCanvasItem *item;
457 ct->rgba = rgba;
458 item = SP_CANVAS_ITEM (ct);
459 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
460 }
461 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
462 }
464 #define EPSILON 1e-6
465 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
467 void
468 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
469 {
470 sp_canvastext_set_coords(ct, NR::Point(x0, y0));
471 }
473 void
474 sp_canvastext_set_coords (SPCanvasText *ct, const NR::Point start)
475 {
476 NR::Point pos = ct->desktop->doc2dt(start);
478 g_return_if_fail (ct != NULL);
479 g_return_if_fail (SP_IS_CANVASTEXT (ct));
481 if (DIFFER (pos[0], ct->s[NR::X]) || DIFFER (pos[1], ct->s[NR::Y])) {
482 ct->s[NR::X] = pos[0];
483 ct->s[NR::Y] = pos[1];
484 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
485 }
486 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
487 }
489 void
490 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
491 {
492 g_free (ct->text);
493 ct->text = g_strdup(new_text);
494 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
495 }
497 void
498 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
499 {
500 std::ostringstream number;
501 number << num;
502 sp_canvastext_set_text(ct, number.str().c_str());
503 }
505 void
506 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
507 {
508 ct->fontsize = size;
509 }
511 void
512 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
513 {
514 ct->anchor_x = anchor_x;
515 ct->anchor_y = anchor_y;
516 }
518 /*
519 Local Variables:
520 mode:c++
521 c-file-style:"stroustrup"
522 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
523 indent-tabs-mode:nil
524 fill-column:99
525 End:
526 */
527 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :