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(), 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(), cbp->affine, area, true, 1);
182 else
183 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), 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 if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
201 cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
202 }
203 cairo_stroke(buf->ct);
204 }
205 }
207 static double
208 sp_canvas_bpath_point (SPCanvasItem *item, NR::Point p, SPCanvasItem **actual_item)
209 {
210 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
212 if ( !cbp->curve ||
213 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
214 cbp->curve->get_segment_count() < 1)
215 return NR_HUGE;
217 double width = 0.5;
218 Geom::Rect viewbox = to_2geom(item->canvas->getViewbox());
219 viewbox.expandBy (width);
220 double dist = NR_HUGE;
221 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
223 if (dist <= 1.0) {
224 *actual_item = item;
225 }
227 return dist;
228 }
230 SPCanvasItem *
231 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
232 {
233 g_return_val_if_fail (parent != NULL, NULL);
234 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
236 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
238 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
240 return item;
241 }
243 void
244 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
245 {
246 g_return_if_fail (cbp != NULL);
247 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
249 if (cbp->curve) {
250 cbp->curve = cbp->curve->unref();
251 }
253 if (curve) {
254 cbp->curve = curve->ref();
255 }
257 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
258 }
260 void
261 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
262 {
263 g_return_if_fail (cbp != NULL);
264 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
266 cbp->fill_rgba = rgba;
267 cbp->fill_rule = rule;
269 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
270 }
272 void
273 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
274 {
275 g_return_if_fail (cbp != NULL);
276 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
278 cbp->stroke_rgba = rgba;
279 cbp->stroke_width = MAX (width, 0.1);
280 cbp->stroke_linejoin = join;
281 cbp->stroke_linecap = cap;
282 cbp->dashes[0] = dash;
283 cbp->dashes[1] = gap;
285 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
286 }
290 /*
291 * FIXME: The following code should actually be in a separate file called
292 * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
293 */
295 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
296 static void sp_canvastext_init (SPCanvasText *canvastext);
297 static void sp_canvastext_destroy (GtkObject *object);
299 static void sp_canvastext_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags);
300 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
302 static SPCanvasItemClass *parent_class_ct;
304 GtkType
305 sp_canvastext_get_type (void)
306 {
307 static GtkType type = 0;
309 if (!type) {
310 GtkTypeInfo info = {
311 (gchar *)"SPCanvasText",
312 sizeof (SPCanvasText),
313 sizeof (SPCanvasTextClass),
314 (GtkClassInitFunc) sp_canvastext_class_init,
315 (GtkObjectInitFunc) sp_canvastext_init,
316 NULL, NULL, NULL
317 };
318 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
319 }
320 return type;
321 }
323 static void
324 sp_canvastext_class_init (SPCanvasTextClass *klass)
325 {
326 GtkObjectClass *object_class = (GtkObjectClass *) klass;
327 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
329 parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
331 object_class->destroy = sp_canvastext_destroy;
333 item_class->update = sp_canvastext_update;
334 item_class->render = sp_canvastext_render;
335 }
337 static void
338 sp_canvastext_init (SPCanvasText *canvastext)
339 {
340 canvastext->rgba = 0x0000ff7f;
341 canvastext->s[NR::X] = canvastext->s[NR::Y] = 0.0;
342 canvastext->affine = NR::identity();
343 canvastext->fontsize = 10.0;
344 canvastext->item = NULL;
345 canvastext->desktop = NULL;
346 canvastext->text = NULL;
347 }
349 static void
350 sp_canvastext_destroy (GtkObject *object)
351 {
352 g_return_if_fail (object != NULL);
353 g_return_if_fail (SP_IS_CANVASTEXT (object));
355 SPCanvasText *canvastext = SP_CANVASTEXT (object);
357 canvastext->item=NULL;
359 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
360 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
361 }
363 // FIXME: remove this as soon as we know how to correctly determine the text extent
364 static const double arbitrary_factor = 0.7;
366 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
367 static double anchor_offset_x = 0;
368 static double anchor_offset_y = 0;
370 static void
371 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
372 {
373 SPCanvasText *cl = SP_CANVASTEXT (item);
375 if (!buf->ct)
376 return;
378 guint32 rgba = cl->rgba;
379 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));
381 NR::Point s = cl->s * cl->affine;
382 double offsetx = s[NR::X] - buf->rect.x0;
383 double offsety = s[NR::Y] - buf->rect.y0;
384 offsetx -= anchor_offset_x;
385 offsety += anchor_offset_y;
387 cairo_move_to(buf->ct, offsetx, offsety);
388 cairo_set_font_size(buf->ct, cl->fontsize);
389 cairo_show_text(buf->ct, cl->text);
390 cairo_stroke(buf->ct);
392 cairo_new_path(buf->ct);
393 }
395 static void
396 sp_canvastext_update (SPCanvasItem *item, NR::Matrix const &affine, unsigned int flags)
397 {
398 SPCanvasText *cl = SP_CANVASTEXT (item);
400 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
402 if (parent_class_ct->update)
403 (* parent_class_ct->update) (item, affine, flags);
405 sp_canvas_item_reset_bounds (item);
407 cl->affine = affine;
409 NR::Point s = cl->s * affine;
411 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
412 // method but update() seems to be called before so we don't have the information available when we need it
413 /**
414 cairo_t tmp_buf;
415 cairo_text_extents_t bbox;
416 cairo_text_extents(&tmp_buf, cl->text, &bbox);
417 **/
418 item->x1 = s[NR::X] + 0;
419 item->y1 = s[NR::Y] - cl->fontsize;
420 item->x2 = s[NR::X] + cl->fontsize * strlen(cl->text);
421 item->y2 = s[NR::Y] + cl->fontsize * 0.5; // for letters below the baseline
423 // adjust update region according to anchor shift
424 // FIXME: use the correct text extent
425 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
426 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
427 item->x1 -= anchor_offset_x;
428 item->x2 -= anchor_offset_x;
429 item->y1 += anchor_offset_y;
430 item->y2 += anchor_offset_y;
432 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
433 }
435 SPCanvasItem *
436 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
437 {
438 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
440 SPCanvasText *ct = SP_CANVASTEXT(item);
442 ct->desktop = desktop;
444 ct->s = pos;
445 g_free(ct->text);
446 ct->text = g_strdup(new_text);
448 // TODO: anything else to do?
450 return item;
451 }
454 void
455 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
456 {
457 g_return_if_fail (ct != NULL);
458 g_return_if_fail (SP_IS_CANVASTEXT (ct));
460 if (rgba != ct->rgba) {
461 SPCanvasItem *item;
462 ct->rgba = rgba;
463 item = SP_CANVAS_ITEM (ct);
464 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
465 }
466 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
467 }
469 #define EPSILON 1e-6
470 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
472 void
473 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
474 {
475 sp_canvastext_set_coords(ct, NR::Point(x0, y0));
476 }
478 void
479 sp_canvastext_set_coords (SPCanvasText *ct, const NR::Point start)
480 {
481 NR::Point pos = ct->desktop->doc2dt(start);
483 g_return_if_fail (ct != NULL);
484 g_return_if_fail (SP_IS_CANVASTEXT (ct));
486 if (DIFFER (pos[0], ct->s[NR::X]) || DIFFER (pos[1], ct->s[NR::Y])) {
487 ct->s[NR::X] = pos[0];
488 ct->s[NR::Y] = pos[1];
489 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
490 }
491 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
492 }
494 void
495 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
496 {
497 g_free (ct->text);
498 ct->text = g_strdup(new_text);
499 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
500 }
502 void
503 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
504 {
505 std::ostringstream number;
506 number << num;
507 sp_canvastext_set_text(ct, number.str().c_str());
508 }
510 void
511 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
512 {
513 ct->fontsize = size;
514 }
516 void
517 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
518 {
519 ct->anchor_x = anchor_x;
520 ct->anchor_y = anchor_y;
521 }
523 /*
524 Local Variables:
525 mode:c++
526 c-file-style:"stroustrup"
527 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
528 indent-tabs-mode:nil
529 fill-column:99
530 End:
531 */
532 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :