aa4b40e175568999ddb07b52a01162792a967752
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-matrix-fns.h>
51 #include <libnr/nr-pixops.h>
52 **/
54 void nr_pixblock_render_bpath_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride);
56 static void sp_canvas_bpath_class_init (SPCanvasBPathClass *klass);
57 static void sp_canvas_bpath_init (SPCanvasBPath *path);
58 static void sp_canvas_bpath_destroy (GtkObject *object);
60 static void sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
61 static void sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf);
62 static double sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item);
64 static SPCanvasItemClass *parent_class;
66 GtkType
67 sp_canvas_bpath_get_type (void)
68 {
69 static GtkType type = 0;
70 if (!type) {
71 GtkTypeInfo info = {
72 (gchar *)"SPCanvasBPath",
73 sizeof (SPCanvasBPath),
74 sizeof (SPCanvasBPathClass),
75 (GtkClassInitFunc) sp_canvas_bpath_class_init,
76 (GtkObjectInitFunc) sp_canvas_bpath_init,
77 NULL, NULL, NULL
78 };
79 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
80 }
81 return type;
82 }
84 static void
85 sp_canvas_bpath_class_init (SPCanvasBPathClass *klass)
86 {
87 GtkObjectClass *object_class;
88 SPCanvasItemClass *item_class;
90 object_class = GTK_OBJECT_CLASS (klass);
91 item_class = (SPCanvasItemClass *) klass;
93 parent_class = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
95 object_class->destroy = sp_canvas_bpath_destroy;
97 item_class->update = sp_canvas_bpath_update;
98 item_class->render = sp_canvas_bpath_render;
99 item_class->point = sp_canvas_bpath_point;
100 }
102 static void
103 sp_canvas_bpath_init (SPCanvasBPath * bpath)
104 {
105 bpath->fill_rgba = 0x00000000;
106 bpath->fill_rule = SP_WIND_RULE_EVENODD;
108 bpath->stroke_rgba = 0x00000000;
109 bpath->stroke_width = 1.0;
110 bpath->stroke_linejoin = SP_STROKE_LINEJOIN_MITER;
111 bpath->stroke_linecap = SP_STROKE_LINECAP_BUTT;
112 bpath->stroke_miterlimit = 11.0;
113 }
115 static void
116 sp_canvas_bpath_destroy (GtkObject *object)
117 {
118 SPCanvasBPath *cbp = SP_CANVAS_BPATH (object);
120 if (cbp->curve) {
121 cbp->curve = cbp->curve->unref();
122 }
124 if (GTK_OBJECT_CLASS (parent_class)->destroy)
125 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
126 }
128 static void
129 sp_canvas_bpath_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
130 {
131 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
133 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
135 if (((SPCanvasItemClass *) parent_class)->update)
136 ((SPCanvasItemClass *) parent_class)->update (item, affine, flags);
138 sp_canvas_item_reset_bounds (item);
140 if (!cbp->curve) return;
142 cbp->affine = affine;
144 Geom::OptRect bbox = bounds_exact_transformed(cbp->curve->get_pathvector(), affine);
146 if (bbox) {
147 item->x1 = (int)bbox->min()[Geom::X] - 1;
148 item->y1 = (int)bbox->min()[Geom::Y] - 1;
149 item->x2 = (int)bbox->max()[Geom::X] + 1;
150 item->y2 = (int)bbox->max()[Geom::Y] + 1;
151 } else {
152 item->x1 = 0;
153 item->y1 = 0;
154 item->x2 = 0;
155 item->y2 = 0;
156 }
157 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
158 }
160 static void
161 sp_canvas_bpath_render (SPCanvasItem *item, SPCanvasBuf *buf)
162 {
163 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
165 sp_canvas_prepare_buffer(buf);
167 Geom::Rect area (Geom::Point(buf->rect.x0, buf->rect.y0), Geom::Point(buf->rect.x1, buf->rect.y1));
169 if ( !cbp->curve ||
170 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
171 cbp->curve->get_segment_count() < 1)
172 return;
174 if (!buf->ct)
175 return;
177 bool dofill = ((cbp->fill_rgba & 0xff) != 0);
178 bool dostroke = ((cbp->stroke_rgba & 0xff) != 0);
180 cairo_set_tolerance(buf->ct, 1.25); // low quality, but good enough for canvas items
181 cairo_new_path(buf->ct);
183 if (!dofill)
184 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, true, 1);
185 else
186 feed_pathvector_to_cairo (buf->ct, cbp->curve->get_pathvector(), cbp->affine, area, false, 1);
188 if (dofill) {
189 // RGB / BGR
190 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));
191 cairo_set_fill_rule(buf->ct, cbp->fill_rule == SP_WIND_RULE_EVENODD? CAIRO_FILL_RULE_EVEN_ODD
192 : CAIRO_FILL_RULE_WINDING);
193 if (dostroke)
194 cairo_fill_preserve(buf->ct);
195 else
196 cairo_fill(buf->ct);
197 }
199 if (dostroke) {
200 // RGB / BGR
201 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));
202 cairo_set_line_width(buf->ct, 1);
203 if (cbp->dashes[0] != 0 && cbp->dashes[1] != 0) {
204 cairo_set_dash (buf->ct, cbp->dashes, 2, 0);
205 }
206 cairo_stroke(buf->ct);
207 }
208 }
210 static double
211 sp_canvas_bpath_point (SPCanvasItem *item, Geom::Point p, SPCanvasItem **actual_item)
212 {
213 SPCanvasBPath *cbp = SP_CANVAS_BPATH (item);
215 if ( !cbp->curve ||
216 ((cbp->stroke_rgba & 0xff) == 0 && (cbp->fill_rgba & 0xff) == 0 ) ||
217 cbp->curve->get_segment_count() < 1)
218 return NR_HUGE;
220 double width = 0.5;
221 Geom::Rect viewbox = item->canvas->getViewbox();
222 viewbox.expandBy (width);
223 double dist = NR_HUGE;
224 pathv_matrix_point_bbox_wind_distance(cbp->curve->get_pathvector(), cbp->affine, p, NULL, NULL, &dist, 0.5, &viewbox);
226 if (dist <= 1.0) {
227 *actual_item = item;
228 }
230 return dist;
231 }
233 SPCanvasItem *
234 sp_canvas_bpath_new (SPCanvasGroup *parent, SPCurve *curve)
235 {
236 g_return_val_if_fail (parent != NULL, NULL);
237 g_return_val_if_fail (SP_IS_CANVAS_GROUP (parent), NULL);
239 SPCanvasItem *item = sp_canvas_item_new (parent, SP_TYPE_CANVAS_BPATH, NULL);
241 sp_canvas_bpath_set_bpath (SP_CANVAS_BPATH (item), curve);
243 return item;
244 }
246 void
247 sp_canvas_bpath_set_bpath (SPCanvasBPath *cbp, SPCurve *curve)
248 {
249 g_return_if_fail (cbp != NULL);
250 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
252 if (cbp->curve) {
253 cbp->curve = cbp->curve->unref();
254 }
256 if (curve) {
257 cbp->curve = curve->ref();
258 }
260 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
261 }
263 void
264 sp_canvas_bpath_set_fill (SPCanvasBPath *cbp, guint32 rgba, SPWindRule rule)
265 {
266 g_return_if_fail (cbp != NULL);
267 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
269 cbp->fill_rgba = rgba;
270 cbp->fill_rule = rule;
272 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
273 }
275 void
276 sp_canvas_bpath_set_stroke (SPCanvasBPath *cbp, guint32 rgba, gdouble width, SPStrokeJoinType join, SPStrokeCapType cap, double dash, double gap)
277 {
278 g_return_if_fail (cbp != NULL);
279 g_return_if_fail (SP_IS_CANVAS_BPATH (cbp));
281 cbp->stroke_rgba = rgba;
282 cbp->stroke_width = MAX (width, 0.1);
283 cbp->stroke_linejoin = join;
284 cbp->stroke_linecap = cap;
285 cbp->dashes[0] = dash;
286 cbp->dashes[1] = gap;
288 sp_canvas_item_request_update (SP_CANVAS_ITEM (cbp));
289 }
293 /*
294 * FIXME: The following code should actually be in a separate file called
295 * display/canvas-text.cpp. It temporarily had to be moved here because of linker errors.
296 */
298 static void sp_canvastext_class_init (SPCanvasTextClass *klass);
299 static void sp_canvastext_init (SPCanvasText *canvastext);
300 static void sp_canvastext_destroy (GtkObject *object);
302 static void sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags);
303 static void sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf);
305 static SPCanvasItemClass *parent_class_ct;
307 GtkType
308 sp_canvastext_get_type (void)
309 {
310 static GtkType type = 0;
312 if (!type) {
313 GtkTypeInfo info = {
314 (gchar *)"SPCanvasText",
315 sizeof (SPCanvasText),
316 sizeof (SPCanvasTextClass),
317 (GtkClassInitFunc) sp_canvastext_class_init,
318 (GtkObjectInitFunc) sp_canvastext_init,
319 NULL, NULL, NULL
320 };
321 type = gtk_type_unique (SP_TYPE_CANVAS_ITEM, &info);
322 }
323 return type;
324 }
326 static void
327 sp_canvastext_class_init (SPCanvasTextClass *klass)
328 {
329 GtkObjectClass *object_class = (GtkObjectClass *) klass;
330 SPCanvasItemClass *item_class = (SPCanvasItemClass *) klass;
332 parent_class_ct = (SPCanvasItemClass*)gtk_type_class (SP_TYPE_CANVAS_ITEM);
334 object_class->destroy = sp_canvastext_destroy;
336 item_class->update = sp_canvastext_update;
337 item_class->render = sp_canvastext_render;
338 }
340 static void
341 sp_canvastext_init (SPCanvasText *canvastext)
342 {
343 canvastext->rgba = 0x0000ff7f;
344 canvastext->s[Geom::X] = canvastext->s[Geom::Y] = 0.0;
345 canvastext->affine = Geom::identity();
346 canvastext->fontsize = 10.0;
347 canvastext->item = NULL;
348 canvastext->desktop = NULL;
349 canvastext->text = NULL;
350 }
352 static void
353 sp_canvastext_destroy (GtkObject *object)
354 {
355 g_return_if_fail (object != NULL);
356 g_return_if_fail (SP_IS_CANVASTEXT (object));
358 SPCanvasText *canvastext = SP_CANVASTEXT (object);
360 canvastext->item=NULL;
362 if (GTK_OBJECT_CLASS (parent_class_ct)->destroy)
363 (* GTK_OBJECT_CLASS (parent_class_ct)->destroy) (object);
364 }
366 // FIXME: remove this as soon as we know how to correctly determine the text extent
367 static const double arbitrary_factor = 0.7;
369 // these are set in sp_canvastext_update() and then re-used in sp_canvastext_render(), which is called afterwards
370 static double anchor_offset_x = 0;
371 static double anchor_offset_y = 0;
373 static void
374 sp_canvastext_render (SPCanvasItem *item, SPCanvasBuf *buf)
375 {
376 SPCanvasText *cl = SP_CANVASTEXT (item);
378 if (!buf->ct)
379 return;
381 guint32 rgba = cl->rgba;
382 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));
384 Geom::Point s = cl->s * cl->affine;
385 double offsetx = s[Geom::X] - buf->rect.x0;
386 double offsety = s[Geom::Y] - buf->rect.y0;
387 offsetx -= anchor_offset_x;
388 offsety += anchor_offset_y;
390 cairo_move_to(buf->ct, offsetx, offsety);
391 cairo_set_font_size(buf->ct, cl->fontsize);
392 cairo_show_text(buf->ct, cl->text);
393 cairo_stroke(buf->ct);
395 cairo_new_path(buf->ct);
396 }
398 static void
399 sp_canvastext_update (SPCanvasItem *item, Geom::Matrix const &affine, unsigned int flags)
400 {
401 SPCanvasText *cl = SP_CANVASTEXT (item);
403 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
405 if (parent_class_ct->update)
406 (* parent_class_ct->update) (item, affine, flags);
408 sp_canvas_item_reset_bounds (item);
410 cl->affine = affine;
412 Geom::Point s = cl->s * affine;
414 // set up a temporary cairo_t to measure the text extents; it would be better to compute this in the render()
415 // method but update() seems to be called before so we don't have the information available when we need it
416 /**
417 cairo_t tmp_buf;
418 cairo_text_extents_t bbox;
419 cairo_text_extents(&tmp_buf, cl->text, &bbox);
420 **/
421 item->x1 = s[Geom::X] + 0;
422 item->y1 = s[Geom::Y] - cl->fontsize;
423 item->x2 = s[Geom::X] + cl->fontsize * strlen(cl->text);
424 item->y2 = s[Geom::Y] + cl->fontsize * 0.5; // for letters below the baseline
426 // adjust update region according to anchor shift
427 // FIXME: use the correct text extent
428 anchor_offset_x = arbitrary_factor * cl->fontsize * strlen(cl->text) * (cl->anchor_x + 1.0) / 2.0;
429 anchor_offset_y = cl->fontsize * (cl->anchor_y + 1.0) / 2.0;
430 item->x1 -= anchor_offset_x;
431 item->x2 -= anchor_offset_x;
432 item->y1 += anchor_offset_y;
433 item->y2 += anchor_offset_y;
435 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
436 }
438 SPCanvasItem *
439 sp_canvastext_new(SPCanvasGroup *parent, SPDesktop *desktop, Geom::Point pos, gchar const *new_text)
440 {
441 SPCanvasItem *item = sp_canvas_item_new(parent, SP_TYPE_CANVASTEXT, NULL);
443 SPCanvasText *ct = SP_CANVASTEXT(item);
445 ct->desktop = desktop;
447 ct->s = pos;
448 g_free(ct->text);
449 ct->text = g_strdup(new_text);
451 // TODO: anything else to do?
453 return item;
454 }
457 void
458 sp_canvastext_set_rgba32 (SPCanvasText *ct, guint32 rgba)
459 {
460 g_return_if_fail (ct != NULL);
461 g_return_if_fail (SP_IS_CANVASTEXT (ct));
463 if (rgba != ct->rgba) {
464 SPCanvasItem *item;
465 ct->rgba = rgba;
466 item = SP_CANVAS_ITEM (ct);
467 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
468 }
469 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
470 }
472 #define EPSILON 1e-6
473 #define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
475 void
476 sp_canvastext_set_coords (SPCanvasText *ct, gdouble x0, gdouble y0)
477 {
478 sp_canvastext_set_coords(ct, Geom::Point(x0, y0));
479 }
481 void
482 sp_canvastext_set_coords (SPCanvasText *ct, const Geom::Point start)
483 {
484 Geom::Point pos = ct->desktop->doc2dt(start);
486 g_return_if_fail (ct != NULL);
487 g_return_if_fail (SP_IS_CANVASTEXT (ct));
489 if (DIFFER (pos[0], ct->s[Geom::X]) || DIFFER (pos[1], ct->s[Geom::Y])) {
490 ct->s[Geom::X] = pos[0];
491 ct->s[Geom::Y] = pos[1];
492 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
493 }
494 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
495 }
497 void
498 sp_canvastext_set_text (SPCanvasText *ct, gchar const * new_text)
499 {
500 g_free (ct->text);
501 ct->text = g_strdup(new_text);
502 sp_canvas_item_request_update (SP_CANVAS_ITEM (ct));
503 }
505 void
506 sp_canvastext_set_number_as_text (SPCanvasText *ct, int num)
507 {
508 std::ostringstream number;
509 number << num;
510 sp_canvastext_set_text(ct, number.str().c_str());
511 }
513 void
514 sp_canvastext_set_fontsize (SPCanvasText *ct, double size)
515 {
516 ct->fontsize = size;
517 }
519 void
520 sp_canvastext_set_anchor (SPCanvasText *ct, double anchor_x, double anchor_y)
521 {
522 ct->anchor_x = anchor_x;
523 ct->anchor_y = anchor_y;
524 }
526 /*
527 Local Variables:
528 mode:c++
529 c-file-style:"stroustrup"
530 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
531 indent-tabs-mode:nil
532 fill-column:99
533 End:
534 */
535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :