Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / display / nr-arena-shape.cpp
1 /*
2  * RGBA display list system for inkscape
3  *
4  * Author:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *
7  * Copyright (C) 2001-2002 Lauris Kaplinski
8  * Copyright (C) 2001 Ximian, Inc.
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include <glib.h>
14 #include <fenv.h>
15 #include <typeinfo>
16 #include <cairo.h>
18 #include <2geom/svg-path.h>
19 #include <2geom/svg-path-parser.h>
20 #include <2geom/pathvector.h>
21 #include <2geom/curves.h>
22 #include "display/sp-canvas.h"
23 #include "display/canvas-arena.h"
24 #include "display/nr-arena.h"
25 #include "display/nr-arena-shape.h"
26 #include "display/curve.h"
27 #include "libnr/nr-pixops.h"
28 #include "libnr/nr-blit.h"
29 #include "libnr/nr-convert2geom.h"
30 #include "livarot/Path.h"
31 #include "livarot/float-line.h"
32 #include "livarot/int-line.h"
33 #include "style.h"
34 #include "inkscape-cairo.h"
35 #include "helper/geom.h"
36 #include "helper/geom-curves.h"
37 #include "sp-filter.h"
38 #include "sp-filter-reference.h"
39 #include "display/nr-filter.h"
40 #include "preferences.h"
42 #include "svg/svg.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;
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)
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;
137 static void
138 nr_arena_shape_finalize(NRObject *object)
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);
155 /**
156  * Retrieves the markers from the item
157  */
158 static NRArenaItem *
159 nr_arena_shape_children(NRArenaItem *item)
161     NRArenaShape *shape = (NRArenaShape *) item;
163     return shape->markers;
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)
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);
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)
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);
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)
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);
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)
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                 if (boundingbox) {
258                     item->bbox.x0 = static_cast<NR::ICoord>(floor((*boundingbox)[0][0])); // Floor gives the coordinate in which the point resides
259                     item->bbox.y0 = static_cast<NR::ICoord>(floor((*boundingbox)[1][0]));
260                     item->bbox.x1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[0][1])); // Ceil gives the first coordinate beyond the point
261                     item->bbox.y1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[1][1]));
262                 } else {
263                     item->bbox = NR_RECT_L_EMPTY;
264                 }
265             }
266             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
267                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
268                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
269                 }
270             }
271         }
272         return (state | item->state);
273     }
275     shape->delayed_shp=true;
276     shape->ctm = gc->transform;
277     boundingbox = Geom::OptRect();
279     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
281     if (shape->curve) {
282         boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
284         if (boundingbox && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
285             float width, scale;
286             scale = gc->transform.descrim();
287             width = MAX(0.125, shape->_stroke.width * scale);
288             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
289                 boundingbox->expandBy(width);
290             }
291             // those pesky miters, now
292             float miterMax=width*shape->_stroke.mitre_limit;
293             if ( miterMax > 0.01 ) {
294                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
295                 boundingbox->expandBy(miterMax);
296             }
297         }
298     } 
300     /// \todo  just write item->bbox = boundingbox
301     if (boundingbox) {
302         shape->approx_bbox.x0 = static_cast<NR::ICoord>(floor((*boundingbox)[0][0]));
303         shape->approx_bbox.y0 = static_cast<NR::ICoord>(floor((*boundingbox)[1][0]));
304         shape->approx_bbox.x1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[0][1]));
305         shape->approx_bbox.y1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[1][1]));
306     } else {
307         shape->approx_bbox = NR_RECT_L_EMPTY;
308     }
309     if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
311     /* Release state data */
312     if (shape->fill_shp) {
313         delete shape->fill_shp;
314         shape->fill_shp = NULL;
315     }
316     if (shape->stroke_shp) {
317         delete shape->stroke_shp;
318         shape->stroke_shp = NULL;
319     }
320     if (shape->fill_painter) {
321         sp_painter_free(shape->fill_painter);
322         shape->fill_painter = NULL;
323     }
324     if (shape->stroke_painter) {
325         sp_painter_free(shape->stroke_painter);
326         shape->stroke_painter = NULL;
327     }
329     if (!shape->curve || 
330         !shape->style ||
331         shape->curve->is_empty() ||
332         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
333          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
334     {
335         item->bbox = shape->approx_bbox;
336         return NR_ARENA_ITEM_STATE_ALL;
337     }
339     /* Build state data */
340     if ( shape->delayed_shp ) {
341         item->bbox=shape->approx_bbox;
342     } else {
343         nr_arena_shape_update_stroke(shape, gc, area);
344         nr_arena_shape_update_fill(shape, gc, area);
346         boundingbox = Geom::OptRect();
347         nr_arena_shape_add_bboxes(shape, boundingbox);
349         /// \todo  just write shape->approx_bbox = boundingbox
350         if (boundingbox) {
351             shape->approx_bbox.x0 = static_cast<NR::ICoord>(floor((*boundingbox)[0][0]));
352             shape->approx_bbox.y0 = static_cast<NR::ICoord>(floor((*boundingbox)[1][0]));
353             shape->approx_bbox.x1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[0][1]));
354             shape->approx_bbox.y1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[1][1]));
355         } else {
356             shape->approx_bbox = NR_RECT_L_EMPTY;
357         }
358     }
360     if (!boundingbox)
361         return NR_ARENA_ITEM_STATE_ALL;
363     /// \todo  just write item->bbox = boundingbox
364     item->bbox.x0 = static_cast<NR::ICoord>(floor((*boundingbox)[0][0]));
365     item->bbox.y0 = static_cast<NR::ICoord>(floor((*boundingbox)[1][0]));
366     item->bbox.x1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[0][1]));
367     item->bbox.y1 = static_cast<NR::ICoord>(ceil ((*boundingbox)[1][1]));
369     item->render_opacity = TRUE;
370     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
371         if (gc && gc->parent) {
372             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
373                                                               gc->transform, gc->parent->transform,
374                                                               &shape->paintbox);
375         }
376         item->render_opacity = FALSE;
377     }
378     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
379         if (gc && gc->parent) {
380             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
381                                                                 gc->transform, gc->parent->transform,
382                                                                 &shape->paintbox);
383         }
384         item->render_opacity = FALSE;
385     }
386     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
387            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
388           || (shape->markers)
389         )
390     {
391         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
392         item->render_opacity = FALSE;
393     }
395     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
396         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
397             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
398         }
399     }
401     return NR_ARENA_ITEM_STATE_ALL;
404 int matrix_is_isometry(Geom::Matrix p) {
405     Geom::Matrix   tp;
406     // transposition
407     tp[0]=p[0];
408     tp[1]=p[2];
409     tp[2]=p[1];
410     tp[3]=p[3];
411     for (int i = 4; i < 6; i++) // shut valgrind up :)
412         tp[i] = p[i] = 0;
413     Geom::Matrix   isom = tp*p; // A^T * A = adjunct?
414     // Is the adjunct nearly an identity function?
415     if (isom.isTranslation(0.01)) {
416         // the transformation is an isometry -> no need to recompute
417         // the uncrossed polygon
418         if ( p.det() < 0 )
419             return -1;
420         else
421             return 1;
422     }
423     return 0;
426 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
427     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
430 /* returns true if the pathvector has a region that needs fill.
431  * is for optimizing purposes, so should be fast and can falsely return true. 
432  * CANNOT falsely return false. */
433 static bool has_inner_area(Geom::PathVector const & pv) {
434     // return false for the cases where there is surely no region to be filled
435     if (pv.empty())
436         return false;
438     if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
439         // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
440         if ( is_straight_curve(pv.front().front()) )
441         {
442             return false;
443         }
444     }
446     return true; //too costly to see if it has region to be filled, so return true.
449 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
450 void
451 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
453     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
454           has_inner_area(shape->curve->get_pathvector()) ) {
456             Geom::Matrix  cached_to_new = Geom::identity();
457             int isometry = 0;
458             if ( shape->cached_fill ) {
459                 if (shape->cached_fctm == gc->transform) {
460                     isometry = 2; // identity
461                 } else {
462                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
463                     isometry = matrix_is_isometry(cached_to_new);
464                 }
465                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
466                     isometry = 0;
467             }
468             if ( isometry == 0 ) {
469                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
470                 shape->cached_fill->Reset();
472                 Path*  thePath=new Path;
473                 Shape* theShape=new Shape;
474                 {
475                     Geom::Matrix tempMat(gc->transform);
476                     thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
477                 }
479                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
480                     thePath->Convert(1.0);
481                     shape->cached_fpartialy = false;
482                 } else {
483                     thePath->Convert(area, 1.0);
484                     shape->cached_fpartialy = true;
485                 }
487                 thePath->Fill(theShape, 0);
489                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
490                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
491                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
492                     //shape->cached_fill->Copy(theShape);
493                 } else {
494                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
495                 }
496                 shape->cached_fctm=gc->transform;
497                 shape->cached_farea = *area;
498                 delete theShape;
499                 delete thePath;
500                 if ( shape->fill_shp == NULL )
501                     shape->fill_shp = new Shape;
503                 shape->fill_shp->Copy(shape->cached_fill);
505             } else if ( 2 == isometry ) {
506                 if ( shape->fill_shp == NULL ) {
507                     shape->fill_shp = new Shape;
508                     shape->fill_shp->Copy(shape->cached_fill);
509                 }
510             } else {
512                 if ( shape->fill_shp == NULL )
513                     shape->fill_shp = new Shape;
515                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
516                                        shape->cached_fill->numberOfEdges());
517                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
518                     shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
519                 if ( isometry == 1 ) {
520                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
521                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
522                                                  shape->cached_fill->getEdge(i).en);
523                 } else if ( isometry == -1 ) { // need to flip poly.
524                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
525                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
526                                                  shape->cached_fill->getEdge(i).st);
527                 }
528                 shape->fill_shp->ForceToPolygon();
529                 shape->fill_shp->needPointsSorting();
530                 shape->fill_shp->needEdgesSorting();
531             }
532             shape->delayed_shp |= shape->cached_fpartialy;
533     }
536 void
537 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
539     SPStyle* style = shape->style;
541     float const scale = gc->transform.descrim();
543     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
545     if (outline) {
546         // cairo does not need the livarot path for rendering
547         return; 
548     }
550     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
551     // (since it will only be used for picking, not for rendering)
553     if (outline ||
554         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
555          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
557         float style_width = MAX(0.125, shape->_stroke.width * scale);
558         float width;
559         if (outline) {
560             width = 0.5; // 1 pixel wide, independent of zoom
561         } else {
562             width = style_width;
563         }
565         Geom::Matrix  cached_to_new = Geom::identity();
567         int isometry = 0;
568         if ( shape->cached_stroke ) {
569             if (shape->cached_sctm == gc->transform) {
570                 isometry = 2; // identity
571             } else {
572                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
573                 isometry = matrix_is_isometry(cached_to_new);
574             }
575             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
576                 isometry = 0;
577             if (0 != isometry && width != shape->cached_width) { 
578                 // if this happens without setting style, we have just switched to outline or back
579                 isometry = 0; 
580             } 
581         }
583         if ( isometry == 0 ) {
584             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
585             shape->cached_stroke->Reset();
586             Path*  thePath = new Path;
587             Shape* theShape = new Shape;
588             {
589                 Geom::Matrix   tempMat( gc->transform );
590                 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
591             }
593             // add some padding to the rendering area, so clipped path does not go into a render area
594             NRRectL padded_area = *area;
595             padded_area.x0 -= (NR::ICoord)width;
596             padded_area.x1 += (NR::ICoord)width;
597             padded_area.y0 -= (NR::ICoord)width;
598             padded_area.y1 += (NR::ICoord)width;
599             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
600                 thePath->Convert((outline) ? 4.0 : 1.0);
601                 shape->cached_spartialy = false;
602             }
603             else {
604                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
605                 shape->cached_spartialy = true;
606             }
608             if (style->stroke_dash.n_dash && !outline) {
609                 thePath->DashPolylineFromStyle(style, scale, 1.0);
610             }
612             ButtType butt=butt_straight;
613             switch (shape->_stroke.cap) {
614                 case NRArenaShape::BUTT_CAP:
615                     butt = butt_straight;
616                     break;
617                 case NRArenaShape::ROUND_CAP:
618                     butt = butt_round;
619                     break;
620                 case NRArenaShape::SQUARE_CAP:
621                     butt = butt_square;
622                     break;
623             }
624             JoinType join=join_straight;
625             switch (shape->_stroke.join) {
626                 case NRArenaShape::MITRE_JOIN:
627                     join = join_pointy;
628                     break;
629                 case NRArenaShape::ROUND_JOIN:
630                     join = join_round;
631                     break;
632                 case NRArenaShape::BEVEL_JOIN:
633                     join = join_straight;
634                     break;
635             }
637             if (outline) {
638                 butt = butt_straight;
639                 join = join_straight;
640             }
642             thePath->Stroke(theShape, false, 0.5*width, join, butt,
643                             0.5*width*shape->_stroke.mitre_limit);
646             if (outline) {
647                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
648                 shape->cached_stroke->Copy(theShape);
649             } else {
650                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
651             }
653             shape->cached_width = width;
655             shape->cached_sctm=gc->transform;
656             shape->cached_sarea = *area;
657             delete thePath;
658             delete theShape;
659             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
661             shape->stroke_shp->Copy(shape->cached_stroke);
663         } else if ( 2 == isometry ) {
664             if ( shape->stroke_shp == NULL ) {
665                 shape->stroke_shp=new Shape;
666                 shape->stroke_shp->Copy(shape->cached_stroke);
667             }
668         } else {
669             if ( shape->stroke_shp == NULL )
670                 shape->stroke_shp=new Shape;
671             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
672             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
673                 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
674             if ( isometry == 1 ) {
675                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
676                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
677                                                shape->cached_stroke->getEdge(i).en);
678             } else if ( isometry == -1 ) {
679                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
680                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
681                                                shape->cached_stroke->getEdge(i).st);
682             }
683             shape->stroke_shp->ForceToPolygon();
684             shape->stroke_shp->needPointsSorting();
685             shape->stroke_shp->needEdgesSorting();
686         }
687         shape->delayed_shp |= shape->cached_spartialy;
688     }
692 void
693 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox)
695     /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
696      * if so, then this can be written much more compact ! */
698     if ( shape->stroke_shp ) {
699         Shape *larger = shape->stroke_shp;
700         larger->CalcBBox();
701         larger->leftX   = floor(larger->leftX);
702         larger->rightX  = ceil(larger->rightX);
703         larger->topY    = floor(larger->topY);
704         larger->bottomY = ceil(larger->bottomY);
705         Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
706                                 Geom::Interval(larger->topY,  larger->bottomY) );
707         bbox.unionWith(stroke_bbox);
708     }
710     if ( shape->fill_shp ) {
711         Shape *larger = shape->fill_shp;
712         larger->CalcBBox();
713         larger->leftX   = floor(larger->leftX);
714         larger->rightX  = ceil(larger->rightX);
715         larger->topY    = floor(larger->topY);
716         larger->bottomY = ceil(larger->bottomY);
717         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
718                               Geom::Interval(larger->topY,  larger->bottomY) );
719         bbox.unionWith(fill_bbox);
720     }
723 // cairo outline rendering:
724 static unsigned int
725 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, Geom::OptRect area)
727     NRArenaShape *shape = NR_ARENA_SHAPE(item);
729     if (!ct) 
730         return item->state;
732     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
733     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
734     // by setting color channels in the "wrong" order
735     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));
737     cairo_set_line_width(ct, 0.5);
738     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
739     cairo_new_path(ct);
741     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
743     cairo_stroke(ct);
745     return item->state;
748 // cairo stroke rendering (flat color only so far!):
749 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
750 // (need to switch them to premul before this can be enabled)
751 void
752 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
754     NRArenaShape *shape = NR_ARENA_SHAPE(item);
755     SPStyle const *style = shape->style;
757     float const scale = shape->ctm.descrim();
759     if (fabs(shape->_stroke.width * scale) < 0.01)
760         return;
762     cairo_t *ct = nr_create_cairo_context (area, pb);
764     if (!ct)
765         return;
767     guint32 rgba;
768     if ( item->render_opacity ) {
769         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
770                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
771     } else {
772         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
773     }
775     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
776     // by setting color channels in the "wrong" order
777     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));
779     float style_width = MAX(0.125, shape->_stroke.width * scale);
780     cairo_set_line_width(ct, style_width);
782     switch (shape->_stroke.cap) {
783         case NRArenaShape::BUTT_CAP:
784             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
785             break;
786         case NRArenaShape::ROUND_CAP:
787             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
788             break;
789         case NRArenaShape::SQUARE_CAP:
790             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
791             break;
792     }
793     switch (shape->_stroke.join) {
794         case NRArenaShape::MITRE_JOIN:
795             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
796             break;
797         case NRArenaShape::ROUND_JOIN:
798             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
799             break;
800         case NRArenaShape::BEVEL_JOIN:
801             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
802             break;
803     }
805     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
807     if (style->stroke_dash.n_dash) {
808         NRVpathDash dash;
809         dash.offset = style->stroke_dash.offset * scale;
810         dash.n_dash = style->stroke_dash.n_dash;
811         dash.dash = g_new(double, dash.n_dash);
812         for (int i = 0; i < dash.n_dash; i++) {
813             dash.dash[i] = style->stroke_dash.dash[i] * scale;
814         }
815         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
816         g_free(dash.dash);
817     }
819     cairo_set_tolerance(ct, 0.1);
820     cairo_new_path(ct);
822     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
824     cairo_stroke(ct);
826     cairo_surface_t *cst = cairo_get_target(ct);
827     cairo_destroy (ct);
828     cairo_surface_finish (cst);
829     cairo_surface_destroy (cst);
831     pb->empty = FALSE;
834 /**
835  * Renders the item.  Markers are just composed into the parent buffer.
836  */
837 static unsigned int
838 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
840     NRArenaShape *shape = NR_ARENA_SHAPE(item);
842     if (!shape->curve) return item->state;
843     if (!shape->style) return item->state;
845     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
846     bool print_colors_preview = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_PRINT_COLORS_PREVIEW);
848     if (outline) { // cairo outline rendering
850         pb->empty = FALSE;
851         unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
852         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
854     } else {
856     if ( shape->delayed_shp ) {
857         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
858             NRGC   tempGC(NULL);
859             tempGC.transform=shape->ctm;
860             shape->delayed_shp = false;
861             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
862             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
863 /*      NRRect bbox;
864         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
865         nr_arena_shape_add_bboxes(shape,bbox);
866         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
867         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
868         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
869         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
870         shape->approx_bbox=item->bbox;*/
871         } else {
872             return item->state;
873         }
874     }
876     SPStyle const *style = shape->style;
878     if (shape->fill_shp) {
879         NRPixBlock m;
880         guint32 rgba;
882         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
884         // if memory allocation failed, abort render
885         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
886             nr_pixblock_release (&m);
887             return (item->state);
888         }
890         m.visible_area = pb->visible_area;
891         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
892         m.empty = FALSE;
894         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
895             // do not render fill in any way
896         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
898             const SPColor* fill_color = &shape->_fill.paint.color();
899             if ( item->render_opacity ) {
900                 rgba = fill_color->toRGBA32( shape->_fill.opacity *
901                                         SP_SCALE24_TO_FLOAT(style->opacity.value) );
902             } else {
903                 rgba = fill_color->toRGBA32( shape->_fill.opacity );
904             }
906             if (print_colors_preview)
907                 nr_arena_separate_color_plates(&rgba);
909             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
910             pb->empty = FALSE;
911         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
912             if (shape->fill_painter) {
913                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
914             }
915         }
917         nr_pixblock_release(&m);
918     }
920     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
922         // cairo_arena_shape_render_stroke(item, area, pb);
924         guint32 rgba;
925         NRPixBlock m;
927         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
929         // if memory allocation failed, abort render
930         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
931             nr_pixblock_release (&m);
932             return (item->state);
933         }
935         m.visible_area = pb->visible_area;
936         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
937         m.empty = FALSE;
939         const SPColor* stroke_color = &shape->_stroke.paint.color();
940         if ( item->render_opacity ) {
941             rgba = stroke_color->toRGBA32( shape->_stroke.opacity *
942                                     SP_SCALE24_TO_FLOAT(style->opacity.value) );
943         } else {
944             rgba = stroke_color->toRGBA32( shape->_stroke.opacity );
945         }
947         if (print_colors_preview)
948             nr_arena_separate_color_plates(&rgba);
950         nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
951         pb->empty = FALSE;
953         nr_pixblock_release(&m);
955     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
957         NRPixBlock m;
959         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
961         // if memory allocation failed, abort render
962         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
963             nr_pixblock_release (&m);
964             return (item->state);
965         }
967         m.visible_area = pb->visible_area; 
968         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
969         m.empty = FALSE;
971         if (shape->stroke_painter) {
972             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
973         }
975         nr_pixblock_release(&m);
976     }
978     } // non-cairo non-outline branch
980     /* Render markers into parent buffer */
981     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
982         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
983         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
984     }
986     return item->state;
990 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
991 // reenable this when the bug is fixed and remove the rest of this function
992 // TODO
993 #if defined(DEADCODE) && !defined(DEADCODE)
994 static guint
995 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
997     NRArenaShape *shape = NR_ARENA_SHAPE(item);
998     if (!shape->curve) return item->state;
1000         cairo_t *ct = nr_create_cairo_context (area, pb);
1002         if (!ct)
1003             return item->state;
1005         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1007         cairo_new_path(ct);
1009         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
1011         cairo_fill(ct);
1013         cairo_surface_t *cst = cairo_get_target(ct);
1014         cairo_destroy (ct);
1015         cairo_surface_finish (cst);
1016         cairo_surface_destroy (cst);
1018         pb->empty = FALSE;
1020         return item->state;
1022 #endif //defined(DEADCODE) && !defined(DEADCODE)
1025 static guint
1026 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1028     //return cairo_arena_shape_clip(item, area, pb);
1030     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1031     if (!shape->curve) return item->state;
1033     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1034         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1035             NRGC   tempGC(NULL);
1036             tempGC.transform=shape->ctm;
1037             shape->delayed_shp = false;
1038             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1039         } else {
1040             return item->state;
1041         }
1042     }
1044     if ( shape->fill_shp ) {
1045         NRPixBlock m;
1047         /* fixme: We can OR in one step (Lauris) */
1048         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1050         // if memory allocation failed, abort 
1051         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1052             nr_pixblock_release (&m);
1053             return (item->state);
1054         }
1056         m.visible_area = pb->visible_area; 
1057         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1059         for (int y = area->y0; y < area->y1; y++) {
1060             unsigned char *s, *d;
1061             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1062             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1063             for (int x = area->x0; x < area->x1; x++) {
1064                 *d = NR_COMPOSEA_111(*s, *d);
1065                 d ++;
1066                 s ++;
1067             }
1068         }
1069         nr_pixblock_release(&m);
1070         pb->empty = FALSE;
1071     }
1073     return item->state;
1076 static NRArenaItem *
1077 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1079     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1081     if (shape->repick_after > 0)
1082         shape->repick_after--;
1084     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1085         return shape->last_pick;
1087     if (!shape->curve) return NULL;
1088     if (!shape->style) return NULL;
1090     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1092     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
1093         // fully transparent, no pick unless outline mode
1094         return NULL;
1096     GTimeVal tstart, tfinish;
1097     g_get_current_time (&tstart);
1099     double width;
1100     if (outline) {
1101         width = 0.5;
1102     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1103         float const scale = shape->ctm.descrim();
1104         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1105     } else {
1106         width = 0;
1107     }
1109     double dist = NR_HUGE;
1110     int wind = 0;
1111     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1112              && shape->_fill.opacity > 1e-3 && !outline);
1114     if (item->arena->canvasarena) {
1115         Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1116         viewbox.expandBy (width);
1117         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1118     } else {
1119         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1120     }
1122     g_get_current_time (&tfinish);
1123     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1124     //g_print ("pick time %lu\n", this_pick);
1126     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1127         shape->repick_after = this_pick / 5000;
1128     }
1130     // covered by fill?
1131     if (needfill) {
1132         if (!shape->style->fill_rule.computed) {
1133             if (wind != 0) {
1134                 shape->last_pick = item;
1135                 return item;
1136             }
1137         } else {
1138             if (wind & 0x1) {
1139                 shape->last_pick = item;
1140                 return item;
1141             }
1142         }
1143     }
1145     // close to the edge, as defined by strokewidth and delta?
1146     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1147     if (needfill || width > 0) { // if either fill or stroke visible,
1148         if ((dist - width) < delta) {
1149             shape->last_pick = item;
1150             return item;
1151         }
1152     }
1154     // if not picked on the shape itself, try its markers
1155     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1156         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1157         if (ret) {
1158             shape->last_pick = item;
1159             return item;
1160         }
1161     }
1163     shape->last_pick = NULL;
1164     return NULL;
1167 /**
1168  *
1169  *  Requests a render of the shape, then if the shape is already a curve it
1170  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1171  *  curve and adds it to the shape.  Finally, it requests an update of the
1172  *  arena for the shape.
1173  */
1174 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1176     g_return_if_fail(shape != NULL);
1177     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1179     if ( justTrans == false ) {
1180         // dirty cached versions
1181         if ( shape->cached_fill ) {
1182             delete shape->cached_fill;
1183             shape->cached_fill=NULL;
1184         }
1185         if ( shape->cached_stroke ) {
1186             delete shape->cached_stroke;
1187             shape->cached_stroke=NULL;
1188         }
1189     }
1191     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1193     if (shape->curve) {
1194         shape->curve->unref();
1195         shape->curve = NULL;
1196     }
1198     if (curve) {
1199         shape->curve = curve;
1200         curve->ref();
1201     }
1203     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1206 void NRArenaShape::setFill(SPPaintServer *server) {
1207     _fill.paint.set(server);
1208     _invalidateCachedFill();
1211 void NRArenaShape::setFill(SPColor const &color) {
1212     _fill.paint.set(color);
1213     _invalidateCachedFill();
1216 void NRArenaShape::setFillOpacity(double opacity) {
1217     _fill.opacity = opacity;
1218     _invalidateCachedFill();
1221 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1222     _fill.rule = rule;
1223     _invalidateCachedFill();
1226 void NRArenaShape::setStroke(SPPaintServer *server) {
1227     _stroke.paint.set(server);
1228     _invalidateCachedStroke();
1231 void NRArenaShape::setStroke(SPColor const &color) {
1232     _stroke.paint.set(color);
1233     _invalidateCachedStroke();
1236 void NRArenaShape::setStrokeOpacity(double opacity) {
1237     _stroke.opacity = opacity;
1238     _invalidateCachedStroke();
1241 void NRArenaShape::setStrokeWidth(double width) {
1242     _stroke.width = width;
1243     _invalidateCachedStroke();
1246 void NRArenaShape::setMitreLimit(double limit) {
1247     _stroke.mitre_limit = limit;
1248     _invalidateCachedStroke();
1251 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1252     _stroke.cap = cap;
1253     _invalidateCachedStroke();
1256 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1257     _stroke.join = join;
1258     _invalidateCachedStroke();
1261 /** nr_arena_shape_set_style
1262  *
1263  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1264  */
1265 void
1266 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1268     g_return_if_fail(shape != NULL);
1269     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1271     if (style) sp_style_ref(style);
1272     if (shape->style) sp_style_unref(shape->style);
1273     shape->style = style;
1275     if ( style->fill.isPaintserver() ) {
1276         shape->setFill(style->getFillPaintServer());
1277     } else if ( style->fill.isColor() ) {
1278         shape->setFill(style->fill.value.color);
1279     } else if ( style->fill.isNone() ) {
1280         shape->setFill(NULL);
1281     } else {
1282         g_assert_not_reached();
1283     }
1284     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1285     switch (style->fill_rule.computed) {
1286         case SP_WIND_RULE_EVENODD: {
1287             shape->setFillRule(NRArenaShape::EVEN_ODD);
1288             break;
1289         }
1290         case SP_WIND_RULE_NONZERO: {
1291             shape->setFillRule(NRArenaShape::NONZERO);
1292             break;
1293         }
1294         default: {
1295             g_assert_not_reached();
1296         }
1297     }
1299     if ( style->stroke.isPaintserver() ) {
1300         shape->setStroke(style->getStrokePaintServer());
1301     } else if ( style->stroke.isColor() ) {
1302         shape->setStroke(style->stroke.value.color);
1303     } else if ( style->stroke.isNone() ) {
1304         shape->setStroke(NULL);
1305     } else {
1306         g_assert_not_reached();
1307     }
1308     shape->setStrokeWidth(style->stroke_width.computed);
1309     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1310     switch (style->stroke_linecap.computed) {
1311         case SP_STROKE_LINECAP_ROUND: {
1312             shape->setLineCap(NRArenaShape::ROUND_CAP);
1313             break;
1314         }
1315         case SP_STROKE_LINECAP_SQUARE: {
1316             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1317             break;
1318         }
1319         case SP_STROKE_LINECAP_BUTT: {
1320             shape->setLineCap(NRArenaShape::BUTT_CAP);
1321             break;
1322         }
1323         default: {
1324             g_assert_not_reached();
1325         }
1326     }
1327     switch (style->stroke_linejoin.computed) {
1328         case SP_STROKE_LINEJOIN_ROUND: {
1329             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1330             break;
1331         }
1332         case SP_STROKE_LINEJOIN_BEVEL: {
1333             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1334             break;
1335         }
1336         case SP_STROKE_LINEJOIN_MITER: {
1337             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1338             break;
1339         }
1340         default: {
1341             g_assert_not_reached();
1342         }
1343     }
1344     shape->setMitreLimit(style->stroke_miterlimit.value);
1346     //if shape has a filter
1347     if (style->filter.set && style->getFilter()) {
1348         if (!shape->filter) {
1349             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1350             shape->filter = new Inkscape::Filters::Filter(primitives);
1351         }
1352         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1353     } else {
1354         //no filter set for this shape
1355         delete shape->filter;
1356         shape->filter = NULL;
1357     }
1359     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1362 void
1363 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1365     g_return_if_fail(shape != NULL);
1366     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1367     g_return_if_fail(pbox != NULL);
1369     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1370         shape->paintbox = *pbox;
1371     } else {
1372         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1373         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1374         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1375     }
1377     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1380 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1382     paintbox.x0 = pbox.min()[Geom::X];
1383     paintbox.y0 = pbox.min()[Geom::Y];
1384     paintbox.x1 = pbox.max()[Geom::X];
1385     paintbox.y1 = pbox.max()[Geom::Y];
1387     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1390 static void
1391 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1393     if ( st >= en ) return;
1394     if ( vst < 0 ) vst=0;
1395     if ( vst > 1 ) vst=1;
1396     if ( ven < 0 ) ven=0;
1397     if ( ven > 1 ) ven=1;
1398     float   sv=vst;
1399     float   dv=ven-vst;
1400     int     len=en-st;
1401     unsigned char*   d=(unsigned char*)dest.buffer;
1402     d+=(st-dest.startPix);
1403     if ( fabs(dv) < 0.001 ) {
1404         if ( vst > 0.999 ) {
1405             /* Simple copy */
1406             while (len > 0) {
1407                 d[0] = 255;
1408                 d += 1;
1409                 len -= 1;
1410             }
1411         } else {
1412             sv*=256;
1413             unsigned int c0_24=(int)sv;
1414             c0_24&=0xFF;
1415             while (len > 0) {
1416                 /* Draw */
1417                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1418                 d += 1;
1419                 len -= 1;
1420             }
1421         }
1422     } else {
1423         if ( en <= st+1 ) {
1424             sv=0.5*(vst+ven);
1425             sv*=256;
1426             unsigned int c0_24=(int)sv;
1427             c0_24&=0xFF;
1428             /* Draw */
1429             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1430         } else {
1431             dv/=len;
1432             sv+=0.5*dv; // correction trapezoidale
1433             sv*=16777216;
1434             dv*=16777216;
1435             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1436             int s0_24 = static_cast<int>(dv);
1437             while (len > 0) {
1438                 unsigned int ca;
1439                 /* Draw */
1440                 ca = c0_24 >> 16;
1441                 if ( ca > 255 ) ca=255;
1442                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1443                 d += 1;
1444                 c0_24 += s0_24;
1445                 c0_24 = CLAMP(c0_24, 0, 16777216);
1446                 len -= 1;
1447             }
1448         }
1449     }
1452 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1454     theS->CalcBBox();
1455     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1456     int    il,ir,it,ib;
1457     il=(int)floor(l);
1458     ir=(int)ceil(r);
1459     it=(int)floor(t);
1460     ib=(int)ceil(b);
1462     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1463     if ( il < m.area.x0 ) il=m.area.x0;
1464     if ( it < m.area.y0 ) it=m.area.y0;
1465     if ( ir > m.area.x1 ) ir=m.area.x1;
1466     if ( ib > m.area.y1 ) ib=m.area.y1;
1468     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1469     int    curPt;
1470     float  curY;
1471     theS->BeginQuickRaster(curY, curPt);
1473     FloatLigne *theI = new FloatLigne();
1474     IntLigne *theIL = new IntLigne();
1476     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1478     char *mdata = (char*)m.data.px;
1479     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1480     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1481     for (int y = it; y < ib; y++) {
1482         theI->Reset();
1483         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1484         theI->Flatten();
1485         theIL->Copy(theI);
1487         raster_info  dest;
1488         dest.startPix=il;
1489         dest.endPix=ir;
1490         dest.sth=il;
1491         dest.stv=y;
1492         dest.buffer=ligStart;
1493         theIL->Raster(dest, NULL, shape_run_A8_OR);
1494         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1495     }
1496     theS->EndQuickRaster();
1497     delete theI;
1498     delete theIL;
1502 /*
1503   Local Variables:
1504   mode:c++
1505   c-file-style:"stroustrup"
1506   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1507   indent-tabs-mode:nil
1508   fill-column:99
1509   End:
1510 */
1511 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :