1 #define __NR_ARENA_SHAPE_C__
3 /*
4 * RGBA display list system for inkscape
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <2geom/svg-path.h>
16 #include <2geom/svg-path-parser.h>
17 #include <display/canvas-arena.h>
18 #include <display/nr-arena.h>
19 #include <display/nr-arena-shape.h>
20 #include "display/curve.h"
21 #include <libnr/nr-pixops.h>
22 #include <libnr/nr-blit.h>
23 #include <libnr/nr-convert2geom.h>
24 #include <2geom/pathvector.h>
25 #include <2geom/curves.h>
26 #include <livarot/Path.h>
27 #include <livarot/float-line.h>
28 #include <livarot/int-line.h>
29 #include <style.h>
30 #include "inkscape-cairo.h"
31 #include "helper/geom.h"
32 #include "helper/geom-curves.h"
33 #include "sp-filter.h"
34 #include "sp-filter-reference.h"
35 #include "display/nr-filter.h"
36 #include <typeinfo>
37 #include <cairo.h>
38 #include "preferences.h"
40 #include <glib.h>
41 #include "svg/svg.h"
42 #include <fenv.h>
44 //int showRuns=0;
45 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
47 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
48 static void nr_arena_shape_init(NRArenaShape *shape);
49 static void nr_arena_shape_finalize(NRObject *object);
51 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
52 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
53 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
54 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
56 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
57 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
58 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
59 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int sticky);
61 static NRArenaItemClass *shape_parent_class;
63 NRType
64 nr_arena_shape_get_type(void)
65 {
66 static NRType type = 0;
67 if (!type) {
68 type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
69 "NRArenaShape",
70 sizeof(NRArenaShapeClass),
71 sizeof(NRArenaShape),
72 (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
73 (void (*)(NRObject *)) nr_arena_shape_init);
74 }
75 return type;
76 }
78 static void
79 nr_arena_shape_class_init(NRArenaShapeClass *klass)
80 {
81 NRObjectClass *object_class;
82 NRArenaItemClass *item_class;
84 object_class = (NRObjectClass *) klass;
85 item_class = (NRArenaItemClass *) klass;
87 shape_parent_class = (NRArenaItemClass *) ((NRObjectClass *) klass)->parent;
89 object_class->finalize = nr_arena_shape_finalize;
90 object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
92 item_class->children = nr_arena_shape_children;
93 item_class->add_child = nr_arena_shape_add_child;
94 item_class->set_child_position = nr_arena_shape_set_child_position;
95 item_class->remove_child = nr_arena_shape_remove_child;
96 item_class->update = nr_arena_shape_update;
97 item_class->render = nr_arena_shape_render;
98 item_class->clip = nr_arena_shape_clip;
99 item_class->pick = nr_arena_shape_pick;
100 }
102 /**
103 * Initializes the arena shape, setting all parameters to null, 0, false,
104 * or other defaults
105 */
106 static void
107 nr_arena_shape_init(NRArenaShape *shape)
108 {
109 shape->curve = NULL;
110 shape->style = NULL;
111 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
112 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
114 shape->ctm.setIdentity();
115 shape->fill_painter = NULL;
116 shape->stroke_painter = NULL;
117 shape->cached_fill = NULL;
118 shape->cached_stroke = NULL;
119 shape->cached_fpartialy = false;
120 shape->cached_spartialy = false;
121 shape->fill_shp = NULL;
122 shape->stroke_shp = NULL;
124 shape->delayed_shp = false;
126 shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
127 shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
128 shape->cached_fctm.setIdentity();
129 shape->cached_sctm.setIdentity();
131 shape->markers = NULL;
133 shape->last_pick = NULL;
134 shape->repick_after = 0;
135 }
137 static void
138 nr_arena_shape_finalize(NRObject *object)
139 {
140 NRArenaShape *shape = (NRArenaShape *) object;
142 if (shape->fill_shp) delete shape->fill_shp;
143 if (shape->stroke_shp) delete shape->stroke_shp;
144 if (shape->cached_fill) delete shape->cached_fill;
145 if (shape->cached_stroke) delete shape->cached_stroke;
146 if (shape->fill_painter) sp_painter_free(shape->fill_painter);
147 if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
149 if (shape->style) sp_style_unref(shape->style);
150 if (shape->curve) shape->curve->unref();
152 ((NRObjectClass *) shape_parent_class)->finalize(object);
153 }
155 /**
156 * Retrieves the markers from the item
157 */
158 static NRArenaItem *
159 nr_arena_shape_children(NRArenaItem *item)
160 {
161 NRArenaShape *shape = (NRArenaShape *) item;
163 return shape->markers;
164 }
166 /**
167 * Attaches child to item, and if ref is not NULL, sets it and ref->next as
168 * the prev and next items. If ref is NULL, then it sets the item's markers
169 * as the next items.
170 */
171 static void
172 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
173 {
174 NRArenaShape *shape = (NRArenaShape *) item;
176 if (!ref) {
177 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
178 } else {
179 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
180 }
182 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
183 }
185 /**
186 * Removes child from the shape. If there are no prev items in
187 * the child, it sets items' markers to the next item in the child.
188 */
189 static void
190 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
191 {
192 NRArenaShape *shape = (NRArenaShape *) item;
194 if (child->prev) {
195 nr_arena_item_detach(item, child);
196 } else {
197 shape->markers = nr_arena_item_detach(item, child);
198 }
200 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
201 }
203 /**
204 * Detaches child from item, and if there are no previous items in child, it
205 * sets item's markers to the child. It then attaches the child back onto the item.
206 * If ref is null, it sets the markers to be the next item, otherwise it uses
207 * the next/prev items in ref.
208 */
209 static void
210 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
211 {
212 NRArenaShape *shape = (NRArenaShape *) item;
214 if (child->prev) {
215 nr_arena_item_detach(item, child);
216 } else {
217 shape->markers = nr_arena_item_detach(item, child);
218 }
220 if (!ref) {
221 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
222 } else {
223 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
224 }
226 nr_arena_item_request_render(child);
227 }
229 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
230 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
231 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox);
233 /**
234 * Updates the arena shape 'item' and all of its children, including the markers.
235 */
236 static guint
237 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
238 {
239 Geom::OptRect boundingbox;
241 NRArenaShape *shape = NR_ARENA_SHAPE(item);
243 unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
245 unsigned int newstate;
246 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
247 newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
248 beststate = beststate & newstate;
249 }
251 if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
252 /* We do not have to create rendering structures */
253 shape->ctm = gc->transform;
254 if (state & NR_ARENA_ITEM_STATE_BBOX) {
255 if (shape->curve) {
256 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
257 /// \todo just write item->bbox = boundingbox
258 if (boundingbox) {
259 item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
260 item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
261 item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
262 item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
263 } else {
264 item->bbox = NR_RECT_L_EMPTY;
265 }
266 }
267 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
268 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
269 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
270 }
271 }
272 }
273 return (state | item->state);
274 }
276 shape->delayed_shp=true;
277 shape->ctm = gc->transform;
278 boundingbox = Geom::OptRect();
280 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
282 if (shape->curve) {
283 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
285 if (boundingbox && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
286 float width, scale;
287 scale = gc->transform.descrim();
288 width = MAX(0.125, shape->_stroke.width * scale);
289 if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
290 boundingbox->expandBy(width);
291 }
292 // those pesky miters, now
293 float miterMax=width*shape->_stroke.mitre_limit;
294 if ( miterMax > 0.01 ) {
295 // grunt mode. we should compute the various miters instead (one for each point on the curve)
296 boundingbox->expandBy(miterMax);
297 }
298 }
299 }
301 /// \todo just write item->bbox = boundingbox
302 if (boundingbox) {
303 shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
304 shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
305 shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
306 shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
307 } else {
308 shape->approx_bbox = NR_RECT_L_EMPTY;
309 }
310 if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
312 /* Release state data */
313 if (shape->fill_shp) {
314 delete shape->fill_shp;
315 shape->fill_shp = NULL;
316 }
317 if (shape->stroke_shp) {
318 delete shape->stroke_shp;
319 shape->stroke_shp = NULL;
320 }
321 if (shape->fill_painter) {
322 sp_painter_free(shape->fill_painter);
323 shape->fill_painter = NULL;
324 }
325 if (shape->stroke_painter) {
326 sp_painter_free(shape->stroke_painter);
327 shape->stroke_painter = NULL;
328 }
330 if (!shape->curve ||
331 !shape->style ||
332 shape->curve->is_empty() ||
333 (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
334 ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
335 {
336 item->bbox = shape->approx_bbox;
337 return NR_ARENA_ITEM_STATE_ALL;
338 }
340 /* Build state data */
341 if ( shape->delayed_shp ) {
342 item->bbox=shape->approx_bbox;
343 } else {
344 nr_arena_shape_update_stroke(shape, gc, area);
345 nr_arena_shape_update_fill(shape, gc, area);
347 boundingbox = Geom::OptRect();
348 nr_arena_shape_add_bboxes(shape, boundingbox);
350 /// \todo just write shape->approx_bbox = boundingbox
351 if (boundingbox) {
352 shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
353 shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
354 shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
355 shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
356 } else {
357 shape->approx_bbox = NR_RECT_L_EMPTY;
358 }
359 }
361 if (!boundingbox)
362 return NR_ARENA_ITEM_STATE_ALL;
364 /// \todo just write item->bbox = boundingbox
365 item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
366 item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
367 item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.0F);
368 item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.0F);
370 item->render_opacity = TRUE;
371 if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
372 if (gc && gc->parent) {
373 shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
374 gc->transform, gc->parent->transform,
375 &shape->paintbox);
376 }
377 item->render_opacity = FALSE;
378 }
379 if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
380 if (gc && gc->parent) {
381 shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
382 gc->transform, gc->parent->transform,
383 &shape->paintbox);
384 }
385 item->render_opacity = FALSE;
386 }
387 if ( (shape->_fill.paint.type() != NRArenaShape::Paint::NONE &&
388 shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
389 || (shape->markers)
390 )
391 {
392 // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
393 item->render_opacity = FALSE;
394 }
396 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
397 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
398 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
399 }
400 }
402 return NR_ARENA_ITEM_STATE_ALL;
403 }
405 int matrix_is_isometry(Geom::Matrix p) {
406 Geom::Matrix tp;
407 // transposition
408 tp[0]=p[0];
409 tp[1]=p[2];
410 tp[2]=p[1];
411 tp[3]=p[3];
412 for (int i = 4; i < 6; i++) // shut valgrind up :)
413 tp[i] = p[i] = 0;
414 Geom::Matrix isom = tp*p; // A^T * A = adjunct?
415 // Is the adjunct nearly an identity function?
416 if (isom.isTranslation(0.01)) {
417 // the transformation is an isometry -> no need to recompute
418 // the uncrossed polygon
419 if ( p.det() < 0 )
420 return -1;
421 else
422 return 1;
423 }
424 return 0;
425 }
427 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
428 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
429 }
431 /* returns true if the pathvector has a region that needs fill.
432 * is for optimizing purposes, so should be fast and can falsely return true.
433 * CANNOT falsely return false. */
434 static bool has_inner_area(Geom::PathVector const & pv) {
435 // return false for the cases where there is surely no region to be filled
436 if (pv.empty())
437 return false;
439 if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
440 // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
441 if ( is_straight_curve(pv.front().front()) )
442 {
443 return false;
444 }
445 }
447 return true; //too costly to see if it has region to be filled, so return true.
448 }
450 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
451 void
452 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
453 {
454 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
455 has_inner_area(shape->curve->get_pathvector()) ) {
457 Geom::Matrix cached_to_new = Geom::identity();
458 int isometry = 0;
459 if ( shape->cached_fill ) {
460 if (shape->cached_fctm == gc->transform) {
461 isometry = 2; // identity
462 } else {
463 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
464 isometry = matrix_is_isometry(cached_to_new);
465 }
466 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
467 isometry = 0;
468 }
469 if ( isometry == 0 ) {
470 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
471 shape->cached_fill->Reset();
473 Path* thePath=new Path;
474 Shape* theShape=new Shape;
475 {
476 Geom::Matrix tempMat(gc->transform);
477 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
478 }
480 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
481 thePath->Convert(1.0);
482 shape->cached_fpartialy = false;
483 } else {
484 thePath->Convert(area, 1.0);
485 shape->cached_fpartialy = true;
486 }
488 thePath->Fill(theShape, 0);
490 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
491 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
492 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
493 //shape->cached_fill->Copy(theShape);
494 } else {
495 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
496 }
497 shape->cached_fctm=gc->transform;
498 shape->cached_farea = *area;
499 delete theShape;
500 delete thePath;
501 if ( shape->fill_shp == NULL )
502 shape->fill_shp = new Shape;
504 shape->fill_shp->Copy(shape->cached_fill);
506 } else if ( 2 == isometry ) {
507 if ( shape->fill_shp == NULL ) {
508 shape->fill_shp = new Shape;
509 shape->fill_shp->Copy(shape->cached_fill);
510 }
511 } else {
513 if ( shape->fill_shp == NULL )
514 shape->fill_shp = new Shape;
516 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
517 shape->cached_fill->numberOfEdges());
518 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
519 shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
520 if ( isometry == 1 ) {
521 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
522 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
523 shape->cached_fill->getEdge(i).en);
524 } else if ( isometry == -1 ) { // need to flip poly.
525 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
526 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
527 shape->cached_fill->getEdge(i).st);
528 }
529 shape->fill_shp->ForceToPolygon();
530 shape->fill_shp->needPointsSorting();
531 shape->fill_shp->needEdgesSorting();
532 }
533 shape->delayed_shp |= shape->cached_fpartialy;
534 }
535 }
537 void
538 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
539 {
540 SPStyle* style = shape->style;
542 float const scale = gc->transform.descrim();
544 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
546 if (outline) {
547 // cairo does not need the livarot path for rendering
548 return;
549 }
551 // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes
552 // (since it will only be used for picking, not for rendering)
554 if (outline ||
555 ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
556 ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
558 float style_width = MAX(0.125, shape->_stroke.width * scale);
559 float width;
560 if (outline) {
561 width = 0.5; // 1 pixel wide, independent of zoom
562 } else {
563 width = style_width;
564 }
566 Geom::Matrix cached_to_new = Geom::identity();
568 int isometry = 0;
569 if ( shape->cached_stroke ) {
570 if (shape->cached_sctm == gc->transform) {
571 isometry = 2; // identity
572 } else {
573 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
574 isometry = matrix_is_isometry(cached_to_new);
575 }
576 if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
577 isometry = 0;
578 if (0 != isometry && width != shape->cached_width) {
579 // if this happens without setting style, we have just switched to outline or back
580 isometry = 0;
581 }
582 }
584 if ( isometry == 0 ) {
585 if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
586 shape->cached_stroke->Reset();
587 Path* thePath = new Path;
588 Shape* theShape = new Shape;
589 {
590 Geom::Matrix tempMat( gc->transform );
591 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
592 }
594 // add some padding to the rendering area, so clipped path does not go into a render area
595 NRRectL padded_area = *area;
596 padded_area.x0 -= (NR::ICoord)width;
597 padded_area.x1 += (NR::ICoord)width;
598 padded_area.y0 -= (NR::ICoord)width;
599 padded_area.y1 += (NR::ICoord)width;
600 if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
601 thePath->Convert((outline) ? 4.0 : 1.0);
602 shape->cached_spartialy = false;
603 }
604 else {
605 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
606 shape->cached_spartialy = true;
607 }
609 if (style->stroke_dash.n_dash && !outline) {
610 thePath->DashPolylineFromStyle(style, scale, 1.0);
611 }
613 ButtType butt=butt_straight;
614 switch (shape->_stroke.cap) {
615 case NRArenaShape::BUTT_CAP:
616 butt = butt_straight;
617 break;
618 case NRArenaShape::ROUND_CAP:
619 butt = butt_round;
620 break;
621 case NRArenaShape::SQUARE_CAP:
622 butt = butt_square;
623 break;
624 }
625 JoinType join=join_straight;
626 switch (shape->_stroke.join) {
627 case NRArenaShape::MITRE_JOIN:
628 join = join_pointy;
629 break;
630 case NRArenaShape::ROUND_JOIN:
631 join = join_round;
632 break;
633 case NRArenaShape::BEVEL_JOIN:
634 join = join_straight;
635 break;
636 }
638 if (outline) {
639 butt = butt_straight;
640 join = join_straight;
641 }
643 thePath->Stroke(theShape, false, 0.5*width, join, butt,
644 0.5*width*shape->_stroke.mitre_limit);
647 if (outline) {
648 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
649 shape->cached_stroke->Copy(theShape);
650 } else {
651 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
652 }
654 shape->cached_width = width;
656 shape->cached_sctm=gc->transform;
657 shape->cached_sarea = *area;
658 delete thePath;
659 delete theShape;
660 if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
662 shape->stroke_shp->Copy(shape->cached_stroke);
664 } else if ( 2 == isometry ) {
665 if ( shape->stroke_shp == NULL ) {
666 shape->stroke_shp=new Shape;
667 shape->stroke_shp->Copy(shape->cached_stroke);
668 }
669 } else {
670 if ( shape->stroke_shp == NULL )
671 shape->stroke_shp=new Shape;
672 shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
673 for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
674 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
675 if ( isometry == 1 ) {
676 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
677 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
678 shape->cached_stroke->getEdge(i).en);
679 } else if ( isometry == -1 ) {
680 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
681 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
682 shape->cached_stroke->getEdge(i).st);
683 }
684 shape->stroke_shp->ForceToPolygon();
685 shape->stroke_shp->needPointsSorting();
686 shape->stroke_shp->needEdgesSorting();
687 }
688 shape->delayed_shp |= shape->cached_spartialy;
689 }
690 }
693 void
694 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox)
695 {
696 /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
697 * if so, then this can be written much more compact ! */
699 if ( shape->stroke_shp ) {
700 Shape *larger = shape->stroke_shp;
701 larger->CalcBBox();
702 larger->leftX = floor(larger->leftX);
703 larger->rightX = ceil(larger->rightX);
704 larger->topY = floor(larger->topY);
705 larger->bottomY = ceil(larger->bottomY);
706 Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
707 Geom::Interval(larger->topY, larger->bottomY) );
708 bbox.unionWith(stroke_bbox);
709 }
711 if ( shape->fill_shp ) {
712 Shape *larger = shape->fill_shp;
713 larger->CalcBBox();
714 larger->leftX = floor(larger->leftX);
715 larger->rightX = ceil(larger->rightX);
716 larger->topY = floor(larger->topY);
717 larger->bottomY = ceil(larger->bottomY);
718 Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
719 Geom::Interval(larger->topY, larger->bottomY) );
720 bbox.unionWith(fill_bbox);
721 }
722 }
724 // cairo outline rendering:
725 static unsigned int
726 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, Geom::OptRect area)
727 {
728 NRArenaShape *shape = NR_ARENA_SHAPE(item);
730 if (!ct)
731 return item->state;
733 guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
734 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
735 // by setting color channels in the "wrong" order
736 cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
738 cairo_set_line_width(ct, 0.5);
739 cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
740 cairo_new_path(ct);
742 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
744 cairo_stroke(ct);
746 return item->state;
747 }
749 // cairo stroke rendering (flat color only so far!):
750 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
751 // (need to switch them to premul before this can be enabled)
752 void
753 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
754 {
755 NRArenaShape *shape = NR_ARENA_SHAPE(item);
756 SPStyle const *style = shape->style;
758 float const scale = shape->ctm.descrim();
760 if (fabs(shape->_stroke.width * scale) < 0.01)
761 return;
763 cairo_t *ct = nr_create_cairo_context (area, pb);
765 if (!ct)
766 return;
768 guint32 rgba;
769 if ( item->render_opacity ) {
770 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
771 SP_SCALE24_TO_FLOAT(style->opacity.value) );
772 } else {
773 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
774 }
776 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
777 // by setting color channels in the "wrong" order
778 cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
780 float style_width = MAX(0.125, shape->_stroke.width * scale);
781 cairo_set_line_width(ct, style_width);
783 switch (shape->_stroke.cap) {
784 case NRArenaShape::BUTT_CAP:
785 cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
786 break;
787 case NRArenaShape::ROUND_CAP:
788 cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
789 break;
790 case NRArenaShape::SQUARE_CAP:
791 cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
792 break;
793 }
794 switch (shape->_stroke.join) {
795 case NRArenaShape::MITRE_JOIN:
796 cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
797 break;
798 case NRArenaShape::ROUND_JOIN:
799 cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
800 break;
801 case NRArenaShape::BEVEL_JOIN:
802 cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
803 break;
804 }
806 cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
808 if (style->stroke_dash.n_dash) {
809 NRVpathDash dash;
810 dash.offset = style->stroke_dash.offset * scale;
811 dash.n_dash = style->stroke_dash.n_dash;
812 dash.dash = g_new(double, dash.n_dash);
813 for (int i = 0; i < dash.n_dash; i++) {
814 dash.dash[i] = style->stroke_dash.dash[i] * scale;
815 }
816 cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
817 g_free(dash.dash);
818 }
820 cairo_set_tolerance(ct, 0.1);
821 cairo_new_path(ct);
823 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
825 cairo_stroke(ct);
827 cairo_surface_t *cst = cairo_get_target(ct);
828 cairo_destroy (ct);
829 cairo_surface_finish (cst);
830 cairo_surface_destroy (cst);
832 pb->empty = FALSE;
833 }
835 /**
836 * Renders the item. Markers are just composed into the parent buffer.
837 */
838 static unsigned int
839 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
840 {
841 NRArenaShape *shape = NR_ARENA_SHAPE(item);
843 if (!shape->curve) return item->state;
844 if (!shape->style) return item->state;
846 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
847 bool print_colors_preview = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_PRINT_COLORS_PREVIEW);
849 if (outline) { // cairo outline rendering
851 pb->empty = FALSE;
852 unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
853 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
855 } else {
857 if ( shape->delayed_shp ) {
858 if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
859 NRGC tempGC(NULL);
860 tempGC.transform=shape->ctm;
861 shape->delayed_shp = false;
862 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
863 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
864 /* NRRect bbox;
865 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
866 nr_arena_shape_add_bboxes(shape,bbox);
867 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
868 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
869 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
870 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
871 shape->approx_bbox=item->bbox;*/
872 } else {
873 return item->state;
874 }
875 }
877 SPStyle const *style = shape->style;
879 if (shape->fill_shp) {
880 NRPixBlock m;
881 guint32 rgba;
883 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
885 // if memory allocation failed, abort render
886 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
887 nr_pixblock_release (&m);
888 return (item->state);
889 }
891 m.visible_area = pb->visible_area;
892 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
893 m.empty = FALSE;
895 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
896 // do not render fill in any way
897 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
899 const SPColor* fill_color = &shape->_fill.paint.color();
900 if ( item->render_opacity ) {
901 rgba = fill_color->toRGBA32( shape->_fill.opacity *
902 SP_SCALE24_TO_FLOAT(style->opacity.value) );
903 } else {
904 rgba = fill_color->toRGBA32( shape->_fill.opacity );
905 }
907 if (print_colors_preview)
908 nr_arena_separate_color_plates(&rgba);
910 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
911 pb->empty = FALSE;
912 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
913 if (shape->fill_painter) {
914 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
915 }
916 }
918 nr_pixblock_release(&m);
919 }
921 if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
923 // cairo_arena_shape_render_stroke(item, area, pb);
925 guint32 rgba;
926 NRPixBlock m;
928 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
930 // if memory allocation failed, abort render
931 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
932 nr_pixblock_release (&m);
933 return (item->state);
934 }
936 m.visible_area = pb->visible_area;
937 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
938 m.empty = FALSE;
940 const SPColor* stroke_color = &shape->_stroke.paint.color();
941 if ( item->render_opacity ) {
942 rgba = stroke_color->toRGBA32( shape->_stroke.opacity *
943 SP_SCALE24_TO_FLOAT(style->opacity.value) );
944 } else {
945 rgba = stroke_color->toRGBA32( shape->_stroke.opacity );
946 }
948 if (print_colors_preview)
949 nr_arena_separate_color_plates(&rgba);
951 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
952 pb->empty = FALSE;
954 nr_pixblock_release(&m);
956 } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
958 NRPixBlock m;
960 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
962 // if memory allocation failed, abort render
963 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
964 nr_pixblock_release (&m);
965 return (item->state);
966 }
968 m.visible_area = pb->visible_area;
969 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
970 m.empty = FALSE;
972 if (shape->stroke_painter) {
973 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
974 }
976 nr_pixblock_release(&m);
977 }
979 } // non-cairo non-outline branch
981 /* Render markers into parent buffer */
982 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
983 unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
984 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
985 }
987 return item->state;
988 }
991 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
992 // reenable this when the bug is fixed and remove the rest of this function
993 // TODO
994 #if defined(DEADCODE) && !defined(DEADCODE)
995 static guint
996 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
997 {
998 NRArenaShape *shape = NR_ARENA_SHAPE(item);
999 if (!shape->curve) return item->state;
1001 cairo_t *ct = nr_create_cairo_context (area, pb);
1003 if (!ct)
1004 return item->state;
1006 cairo_set_source_rgba(ct, 0, 0, 0, 1);
1008 cairo_new_path(ct);
1010 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
1012 cairo_fill(ct);
1014 cairo_surface_t *cst = cairo_get_target(ct);
1015 cairo_destroy (ct);
1016 cairo_surface_finish (cst);
1017 cairo_surface_destroy (cst);
1019 pb->empty = FALSE;
1021 return item->state;
1022 }
1023 #endif //defined(DEADCODE) && !defined(DEADCODE)
1026 static guint
1027 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1028 {
1029 //return cairo_arena_shape_clip(item, area, pb);
1031 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1032 if (!shape->curve) return item->state;
1034 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1035 if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1036 NRGC tempGC(NULL);
1037 tempGC.transform=shape->ctm;
1038 shape->delayed_shp = false;
1039 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1040 } else {
1041 return item->state;
1042 }
1043 }
1045 if ( shape->fill_shp ) {
1046 NRPixBlock m;
1048 /* fixme: We can OR in one step (Lauris) */
1049 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1051 // if memory allocation failed, abort
1052 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1053 nr_pixblock_release (&m);
1054 return (item->state);
1055 }
1057 m.visible_area = pb->visible_area;
1058 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1060 for (int y = area->y0; y < area->y1; y++) {
1061 unsigned char *s, *d;
1062 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1063 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1064 for (int x = area->x0; x < area->x1; x++) {
1065 *d = NR_COMPOSEA_111(*s, *d);
1066 d ++;
1067 s ++;
1068 }
1069 }
1070 nr_pixblock_release(&m);
1071 pb->empty = FALSE;
1072 }
1074 return item->state;
1075 }
1077 static NRArenaItem *
1078 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1079 {
1080 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1082 if (shape->repick_after > 0)
1083 shape->repick_after--;
1085 if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1086 return shape->last_pick;
1088 if (!shape->curve) return NULL;
1089 if (!shape->style) return NULL;
1091 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1093 if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline)
1094 // fully transparent, no pick unless outline mode
1095 return NULL;
1097 GTimeVal tstart, tfinish;
1098 g_get_current_time (&tstart);
1100 double width;
1101 if (outline) {
1102 width = 0.5;
1103 } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1104 float const scale = shape->ctm.descrim();
1105 width = MAX(0.125, shape->_stroke.width * scale) / 2;
1106 } else {
1107 width = 0;
1108 }
1110 double dist = NR_HUGE;
1111 int wind = 0;
1112 bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE
1113 && shape->_fill.opacity > 1e-3 && !outline);
1115 if (item->arena->canvasarena) {
1116 Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1117 viewbox.expandBy (width);
1118 pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1119 } else {
1120 pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1121 }
1123 g_get_current_time (&tfinish);
1124 glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1125 //g_print ("pick time %lu\n", this_pick);
1127 if (this_pick > 10000) { // slow picking, remember to skip several new picks
1128 shape->repick_after = this_pick / 5000;
1129 }
1131 // covered by fill?
1132 if (needfill) {
1133 if (!shape->style->fill_rule.computed) {
1134 if (wind != 0) {
1135 shape->last_pick = item;
1136 return item;
1137 }
1138 } else {
1139 if (wind & 0x1) {
1140 shape->last_pick = item;
1141 return item;
1142 }
1143 }
1144 }
1146 // close to the edge, as defined by strokewidth and delta?
1147 // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1148 if (needfill || width > 0) { // if either fill or stroke visible,
1149 if ((dist - width) < delta) {
1150 shape->last_pick = item;
1151 return item;
1152 }
1153 }
1155 // if not picked on the shape itself, try its markers
1156 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1157 NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1158 if (ret) {
1159 shape->last_pick = item;
1160 return item;
1161 }
1162 }
1164 shape->last_pick = NULL;
1165 return NULL;
1166 }
1168 /**
1169 *
1170 * Requests a render of the shape, then if the shape is already a curve it
1171 * unrefs the old curve; if the new curve is valid it creates a copy of the
1172 * curve and adds it to the shape. Finally, it requests an update of the
1173 * arena for the shape.
1174 */
1175 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1176 {
1177 g_return_if_fail(shape != NULL);
1178 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1180 if ( justTrans == false ) {
1181 // dirty cached versions
1182 if ( shape->cached_fill ) {
1183 delete shape->cached_fill;
1184 shape->cached_fill=NULL;
1185 }
1186 if ( shape->cached_stroke ) {
1187 delete shape->cached_stroke;
1188 shape->cached_stroke=NULL;
1189 }
1190 }
1192 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1194 if (shape->curve) {
1195 shape->curve->unref();
1196 shape->curve = NULL;
1197 }
1199 if (curve) {
1200 shape->curve = curve;
1201 curve->ref();
1202 }
1204 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1205 }
1207 void NRArenaShape::setFill(SPPaintServer *server) {
1208 _fill.paint.set(server);
1209 _invalidateCachedFill();
1210 }
1212 void NRArenaShape::setFill(SPColor const &color) {
1213 _fill.paint.set(color);
1214 _invalidateCachedFill();
1215 }
1217 void NRArenaShape::setFillOpacity(double opacity) {
1218 _fill.opacity = opacity;
1219 _invalidateCachedFill();
1220 }
1222 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1223 _fill.rule = rule;
1224 _invalidateCachedFill();
1225 }
1227 void NRArenaShape::setStroke(SPPaintServer *server) {
1228 _stroke.paint.set(server);
1229 _invalidateCachedStroke();
1230 }
1232 void NRArenaShape::setStroke(SPColor const &color) {
1233 _stroke.paint.set(color);
1234 _invalidateCachedStroke();
1235 }
1237 void NRArenaShape::setStrokeOpacity(double opacity) {
1238 _stroke.opacity = opacity;
1239 _invalidateCachedStroke();
1240 }
1242 void NRArenaShape::setStrokeWidth(double width) {
1243 _stroke.width = width;
1244 _invalidateCachedStroke();
1245 }
1247 void NRArenaShape::setMitreLimit(double limit) {
1248 _stroke.mitre_limit = limit;
1249 _invalidateCachedStroke();
1250 }
1252 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1253 _stroke.cap = cap;
1254 _invalidateCachedStroke();
1255 }
1257 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1258 _stroke.join = join;
1259 _invalidateCachedStroke();
1260 }
1262 /** nr_arena_shape_set_style
1263 *
1264 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1265 */
1266 void
1267 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1268 {
1269 g_return_if_fail(shape != NULL);
1270 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1272 if (style) sp_style_ref(style);
1273 if (shape->style) sp_style_unref(shape->style);
1274 shape->style = style;
1276 if ( style->fill.isPaintserver() ) {
1277 shape->setFill(style->getFillPaintServer());
1278 } else if ( style->fill.isColor() ) {
1279 shape->setFill(style->fill.value.color);
1280 } else if ( style->fill.isNone() ) {
1281 shape->setFill(NULL);
1282 } else {
1283 g_assert_not_reached();
1284 }
1285 shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1286 switch (style->fill_rule.computed) {
1287 case SP_WIND_RULE_EVENODD: {
1288 shape->setFillRule(NRArenaShape::EVEN_ODD);
1289 break;
1290 }
1291 case SP_WIND_RULE_NONZERO: {
1292 shape->setFillRule(NRArenaShape::NONZERO);
1293 break;
1294 }
1295 default: {
1296 g_assert_not_reached();
1297 }
1298 }
1300 if ( style->stroke.isPaintserver() ) {
1301 shape->setStroke(style->getStrokePaintServer());
1302 } else if ( style->stroke.isColor() ) {
1303 shape->setStroke(style->stroke.value.color);
1304 } else if ( style->stroke.isNone() ) {
1305 shape->setStroke(NULL);
1306 } else {
1307 g_assert_not_reached();
1308 }
1309 shape->setStrokeWidth(style->stroke_width.computed);
1310 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1311 switch (style->stroke_linecap.computed) {
1312 case SP_STROKE_LINECAP_ROUND: {
1313 shape->setLineCap(NRArenaShape::ROUND_CAP);
1314 break;
1315 }
1316 case SP_STROKE_LINECAP_SQUARE: {
1317 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1318 break;
1319 }
1320 case SP_STROKE_LINECAP_BUTT: {
1321 shape->setLineCap(NRArenaShape::BUTT_CAP);
1322 break;
1323 }
1324 default: {
1325 g_assert_not_reached();
1326 }
1327 }
1328 switch (style->stroke_linejoin.computed) {
1329 case SP_STROKE_LINEJOIN_ROUND: {
1330 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1331 break;
1332 }
1333 case SP_STROKE_LINEJOIN_BEVEL: {
1334 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1335 break;
1336 }
1337 case SP_STROKE_LINEJOIN_MITER: {
1338 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1339 break;
1340 }
1341 default: {
1342 g_assert_not_reached();
1343 }
1344 }
1345 shape->setMitreLimit(style->stroke_miterlimit.value);
1347 //if shape has a filter
1348 if (style->filter.set && style->getFilter()) {
1349 if (!shape->filter) {
1350 int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1351 shape->filter = new Inkscape::Filters::Filter(primitives);
1352 }
1353 sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1354 } else {
1355 //no filter set for this shape
1356 delete shape->filter;
1357 shape->filter = NULL;
1358 }
1360 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1361 }
1363 void
1364 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1365 {
1366 g_return_if_fail(shape != NULL);
1367 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1368 g_return_if_fail(pbox != NULL);
1370 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1371 shape->paintbox = *pbox;
1372 } else {
1373 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1374 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1375 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1376 }
1378 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1379 }
1381 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1382 {
1383 paintbox.x0 = pbox.min()[Geom::X];
1384 paintbox.y0 = pbox.min()[Geom::Y];
1385 paintbox.x1 = pbox.max()[Geom::X];
1386 paintbox.y1 = pbox.max()[Geom::Y];
1388 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1389 }
1391 static void
1392 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1393 {
1394 if ( st >= en ) return;
1395 if ( vst < 0 ) vst=0;
1396 if ( vst > 1 ) vst=1;
1397 if ( ven < 0 ) ven=0;
1398 if ( ven > 1 ) ven=1;
1399 float sv=vst;
1400 float dv=ven-vst;
1401 int len=en-st;
1402 unsigned char* d=(unsigned char*)dest.buffer;
1403 d+=(st-dest.startPix);
1404 if ( fabs(dv) < 0.001 ) {
1405 if ( vst > 0.999 ) {
1406 /* Simple copy */
1407 while (len > 0) {
1408 d[0] = 255;
1409 d += 1;
1410 len -= 1;
1411 }
1412 } else {
1413 sv*=256;
1414 unsigned int c0_24=(int)sv;
1415 c0_24&=0xFF;
1416 while (len > 0) {
1417 /* Draw */
1418 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1419 d += 1;
1420 len -= 1;
1421 }
1422 }
1423 } else {
1424 if ( en <= st+1 ) {
1425 sv=0.5*(vst+ven);
1426 sv*=256;
1427 unsigned int c0_24=(int)sv;
1428 c0_24&=0xFF;
1429 /* Draw */
1430 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1431 } else {
1432 dv/=len;
1433 sv+=0.5*dv; // correction trapezoidale
1434 sv*=16777216;
1435 dv*=16777216;
1436 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1437 int s0_24 = static_cast<int>(dv);
1438 while (len > 0) {
1439 unsigned int ca;
1440 /* Draw */
1441 ca = c0_24 >> 16;
1442 if ( ca > 255 ) ca=255;
1443 d[0] = NR_COMPOSEA_111(ca,d[0]);
1444 d += 1;
1445 c0_24 += s0_24;
1446 c0_24 = CLAMP(c0_24, 0, 16777216);
1447 len -= 1;
1448 }
1449 }
1450 }
1451 }
1453 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1454 {
1455 theS->CalcBBox();
1456 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1457 int il,ir,it,ib;
1458 il=(int)floor(l);
1459 ir=(int)ceil(r);
1460 it=(int)floor(t);
1461 ib=(int)ceil(b);
1463 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1464 if ( il < m.area.x0 ) il=m.area.x0;
1465 if ( it < m.area.y0 ) it=m.area.y0;
1466 if ( ir > m.area.x1 ) ir=m.area.x1;
1467 if ( ib > m.area.y1 ) ib=m.area.y1;
1469 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1470 int curPt;
1471 float curY;
1472 theS->BeginQuickRaster(curY, curPt);
1474 FloatLigne *theI = new FloatLigne();
1475 IntLigne *theIL = new IntLigne();
1477 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1479 char *mdata = (char*)m.data.px;
1480 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1481 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1482 for (int y = it; y < ib; y++) {
1483 theI->Reset();
1484 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1485 theI->Flatten();
1486 theIL->Copy(theI);
1488 raster_info dest;
1489 dest.startPix=il;
1490 dest.endPix=ir;
1491 dest.sth=il;
1492 dest.stv=y;
1493 dest.buffer=ligStart;
1494 theIL->Raster(dest, NULL, shape_run_A8_OR);
1495 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1496 }
1497 theS->EndQuickRaster();
1498 delete theI;
1499 delete theIL;
1500 }
1503 /*
1504 Local Variables:
1505 mode:c++
1506 c-file-style:"stroustrup"
1507 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1508 indent-tabs-mode:nil
1509 fill-column:99
1510 End:
1511 */
1512 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :