Code

4c988be46b5153689f1461c966e6bbfbcfae99c9
[inkscape.git] / src / display / nr-arena-shape.cpp
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-matrix-ops.h>
23 #include <libnr/nr-matrix-fns.h>
24 #include <libnr/nr-blit.h>
25 #include <libnr/nr-convert2geom.h>
26 #include <2geom/pathvector.h>
27 #include <2geom/curves.h>
28 #include <livarot/Path.h>
29 #include <livarot/float-line.h>
30 #include <livarot/int-line.h>
31 #include <style.h>
32 #include "inkscape-cairo.h"
33 #include "helper/geom.h"
34 #include "helper/geom-curves.h"
35 #include "sp-filter.h"
36 #include "sp-filter-reference.h"
37 #include "display/nr-filter.h"
38 #include <typeinfo>
39 #include <cairo.h>
41 #include <glib.h>
42 #include "svg/svg.h"
43 #include <fenv.h>
45 //int  showRuns=0;
46 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
48 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
49 static void nr_arena_shape_init(NRArenaShape *shape);
50 static void nr_arena_shape_finalize(NRObject *object);
52 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
53 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
54 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
55 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
57 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
58 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
59 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
60 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int sticky);
62 static NRArenaItemClass *shape_parent_class;
64 NRType
65 nr_arena_shape_get_type(void)
66 {
67     static NRType type = 0;
68     if (!type) {
69         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
70                                        "NRArenaShape",
71                                        sizeof(NRArenaShapeClass),
72                                        sizeof(NRArenaShape),
73                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
74                                        (void (*)(NRObject *)) nr_arena_shape_init);
75     }
76     return type;
77 }
79 static void
80 nr_arena_shape_class_init(NRArenaShapeClass *klass)
81 {
82     NRObjectClass *object_class;
83     NRArenaItemClass *item_class;
85     object_class = (NRObjectClass *) klass;
86     item_class = (NRArenaItemClass *) klass;
88     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
90     object_class->finalize = nr_arena_shape_finalize;
91     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
93     item_class->children = nr_arena_shape_children;
94     item_class->add_child = nr_arena_shape_add_child;
95     item_class->set_child_position = nr_arena_shape_set_child_position;
96     item_class->remove_child = nr_arena_shape_remove_child;
97     item_class->update = nr_arena_shape_update;
98     item_class->render = nr_arena_shape_render;
99     item_class->clip = nr_arena_shape_clip;
100     item_class->pick = nr_arena_shape_pick;
103 /**
104  * Initializes the arena shape, setting all parameters to null, 0, false,
105  * or other defaults
106  */
107 static void
108 nr_arena_shape_init(NRArenaShape *shape)
110     shape->curve = NULL;
111     shape->style = NULL;
112     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
113     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
115     shape->ctm.setIdentity();
116     shape->fill_painter = NULL;
117     shape->stroke_painter = NULL;
118     shape->cached_fill = NULL;
119     shape->cached_stroke = NULL;
120     shape->cached_fpartialy = false;
121     shape->cached_spartialy = false;
122     shape->fill_shp = NULL;
123     shape->stroke_shp = NULL;
125     shape->delayed_shp = false;
127     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
128     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
129     shape->cached_fctm.setIdentity();
130     shape->cached_sctm.setIdentity();
132     shape->markers = NULL;
134     shape->last_pick = NULL;
135     shape->repick_after = 0;
138 static void
139 nr_arena_shape_finalize(NRObject *object)
141     NRArenaShape *shape = (NRArenaShape *) object;
143     if (shape->fill_shp) delete shape->fill_shp;
144     if (shape->stroke_shp) delete shape->stroke_shp;
145     if (shape->cached_fill) delete shape->cached_fill;
146     if (shape->cached_stroke) delete shape->cached_stroke;
147     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
148     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
150     if (shape->style) sp_style_unref(shape->style);
151     if (shape->curve) shape->curve->unref();
153     ((NRObjectClass *) shape_parent_class)->finalize(object);
156 /**
157  * Retrieves the markers from the item
158  */
159 static NRArenaItem *
160 nr_arena_shape_children(NRArenaItem *item)
162     NRArenaShape *shape = (NRArenaShape *) item;
164     return shape->markers;
167 /**
168  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
169  * the prev and next items.  If ref is NULL, then it sets the item's markers
170  * as the next items.
171  */
172 static void
173 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
175     NRArenaShape *shape = (NRArenaShape *) item;
177     if (!ref) {
178         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
179     } else {
180         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
181     }
183     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
186 /**
187  * Removes child from the shape.  If there are no prev items in 
188  * the child, it sets items' markers to the next item in the child.
189  */
190 static void
191 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
193     NRArenaShape *shape = (NRArenaShape *) item;
195     if (child->prev) {
196         nr_arena_item_detach(item, child);
197     } else {
198         shape->markers = nr_arena_item_detach(item, child);
199     }
201     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
204 /**
205  * Detaches child from item, and if there are no previous items in child, it 
206  * sets item's markers to the child.  It then attaches the child back onto the item.
207  * If ref is null, it sets the markers to be the next item, otherwise it uses
208  * the next/prev items in ref.
209  */
210 static void
211 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
213     NRArenaShape *shape = (NRArenaShape *) item;
215     if (child->prev) {
216         nr_arena_item_detach(item, child);
217     } else {
218         shape->markers = nr_arena_item_detach(item, child);
219     }
221     if (!ref) {
222         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
223     } else {
224         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
225     }
227     nr_arena_item_request_render(child);
230 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
231 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
232 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox);
234 /**
235  * Updates the arena shape 'item' and all of its children, including the markers.
236  */
237 static guint
238 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
240     Geom::Rect boundingbox;
242     NRArenaShape *shape = NR_ARENA_SHAPE(item);
244     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
246     unsigned int newstate;
247     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
248         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
249         beststate = beststate & newstate;
250     }
252     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
253         /* We do not have to create rendering structures */
254         shape->ctm = gc->transform;
255         if (state & NR_ARENA_ITEM_STATE_BBOX) {
256             if (shape->curve) {
257                 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
258                 item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
259                 item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
260                 item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
261                 item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
262             }
263             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
264                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
265                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
266                 }
267             }
268         }
269         return (state | item->state);
270     }
272     shape->delayed_shp=true;
273     shape->ctm = gc->transform;
274     boundingbox[0][0] = boundingbox[1][0] = NR_HUGE;
275     boundingbox[0][1] = boundingbox[1][1] = -NR_HUGE;
277     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
279     if (shape->curve) {
280         boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
282         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
283             float width, scale;
284             scale = gc->transform.descrim();
285             width = MAX(0.125, shape->_stroke.width * scale);
286             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
287                 boundingbox.expandBy(width);
288             }
289             // those pesky miters, now
290             float miterMax=width*shape->_stroke.mitre_limit;
291             if ( miterMax > 0.01 ) {
292                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
293                 boundingbox.expandBy(miterMax);
294             }
295         }
296     } else {
297     }
298     shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
299     shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
300     shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
301     shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
302     if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
304     /* Release state data */
305     if (TRUE || !Geom::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
306         /* Concept test */
307         if (shape->fill_shp) {
308             delete shape->fill_shp;
309             shape->fill_shp = NULL;
310         }
311     }
312     if (shape->stroke_shp) {
313         delete shape->stroke_shp;
314         shape->stroke_shp = NULL;
315     }
316     if (shape->fill_painter) {
317         sp_painter_free(shape->fill_painter);
318         shape->fill_painter = NULL;
319     }
320     if (shape->stroke_painter) {
321         sp_painter_free(shape->stroke_painter);
322         shape->stroke_painter = NULL;
323     }
325     if (!shape->curve || 
326         !shape->style ||
327         shape->curve->is_empty() ||
328         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
329          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
330     {
331         item->bbox = shape->approx_bbox;
332         return NR_ARENA_ITEM_STATE_ALL;
333     }
335     /* Build state data */
336     if ( shape->delayed_shp ) {
337         item->bbox=shape->approx_bbox;
338     } else {
339         nr_arena_shape_update_stroke(shape, gc, area);
340         nr_arena_shape_update_fill(shape, gc, area);
342         boundingbox[0][0] = boundingbox[0][1] = boundingbox[1][0] = boundingbox[1][1] = 0.0;
343         nr_arena_shape_add_bboxes(shape, boundingbox);
345         shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
346         shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
347         shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
348         shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
349     }
351     if (boundingbox.isEmpty())
352         return NR_ARENA_ITEM_STATE_ALL;
354     item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
355     item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
356     item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.0F);
357     item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.0F);
358     nr_arena_request_render_rect(item->arena, &item->bbox);
360     item->render_opacity = TRUE;
361     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
362         if (gc && gc->parent) {
363             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
364                                                               gc->transform, gc->parent->transform,
365                                                               &shape->paintbox);
366         }
367         item->render_opacity = FALSE;
368     }
369     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
370         if (gc && gc->parent) {
371             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
372                                                                 gc->transform, gc->parent->transform,
373                                                                 &shape->paintbox);
374         }
375         item->render_opacity = FALSE;
376     }
377     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
378            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
379           || (shape->markers)
380         )
381     {
382         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
383         item->render_opacity = FALSE;
384     }
386     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
387         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
388             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
389         }
390     }
392     return NR_ARENA_ITEM_STATE_ALL;
395 int matrix_is_isometry(Geom::Matrix p) {
396     Geom::Matrix   tp;
397     // transposition
398     tp[0]=p[0];
399     tp[1]=p[2];
400     tp[2]=p[1];
401     tp[3]=p[3];
402     for (int i = 4; i < 6; i++) // shut valgrind up :)
403         tp[i] = p[i] = 0;
404     Geom::Matrix   isom = tp*p; // A^T * A = adjunct?
405     // Is the adjunct nearly an identity function?
406     if (isom.isTranslation(0.01)) {
407         // the transformation is an isometry -> no need to recompute
408         // the uncrossed polygon
409         if ( p.det() < 0 )
410             return -1;
411         else
412             return 1;
413     }
414     return 0;
417 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
418     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
421 /* returns true if the pathvector has a region that needs fill.
422  * is for optimizing purposes, so should be fast and can falsely return true. 
423  * CANNOT falsely return false. */
424 static bool has_inner_area(Geom::PathVector const & pv) {
425     // return false for the cases where there is surely no region to be filled
426     if (pv.empty())
427         return false;
429     if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
430         // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
431         if ( is_straight_curve(pv.front().front()) )
432         {
433             return false;
434         }
435     }
437     return true; //too costly to see if it has region to be filled, so return true.
440 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
441 void
442 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
444     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
445 //        ((shape->curve->get_length() > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {  // <-- this used to be the old code, i think it has to determine that the path has a sort of 'internal region' where fill would occur
446           has_inner_area(shape->curve->get_pathvector()) ) {
447         if (TRUE || !shape->fill_shp) {
448             Geom::Matrix  cached_to_new = Geom::identity();
449             int isometry = 0;
450             if ( shape->cached_fill ) {
451                 if (shape->cached_fctm == gc->transform) {
452                     isometry = 2; // identity
453                 } else {
454                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
455                     isometry = matrix_is_isometry(cached_to_new);
456                 }
457                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
458                     isometry = 0;
459             }
460             if ( isometry == 0 ) {
461                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
462                 shape->cached_fill->Reset();
464                 Path*  thePath=new Path;
465                 Shape* theShape=new Shape;
466                 {
467                     Geom::Matrix tempMat(gc->transform);
468                     thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
469                 }
471                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
472                     thePath->Convert(1.0);
473                     shape->cached_fpartialy = false;
474                 } else {
475                     thePath->Convert(area, 1.0);
476                     shape->cached_fpartialy = true;
477                 }
479                 thePath->Fill(theShape, 0);
481                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
482                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
483                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
484                     //shape->cached_fill->Copy(theShape);
485                 } else {
486                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
487                 }
488                 shape->cached_fctm=gc->transform;
489                 shape->cached_farea = *area;
490                 delete theShape;
491                 delete thePath;
492                 if ( shape->fill_shp == NULL )
493                     shape->fill_shp = new Shape;
495                 shape->fill_shp->Copy(shape->cached_fill);
497             } else if ( 2 == isometry ) {
498                 if ( shape->fill_shp == NULL ) {
499                     shape->fill_shp = new Shape;
500                     shape->fill_shp->Copy(shape->cached_fill);
501                 }
502             } else {
504                 if ( shape->fill_shp == NULL )
505                     shape->fill_shp = new Shape;
507                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
508                                        shape->cached_fill->numberOfEdges());
509                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
510                     shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
511                 if ( isometry == 1 ) {
512                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
513                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
514                                                  shape->cached_fill->getEdge(i).en);
515                 } else if ( isometry == -1 ) { // need to flip poly.
516                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
517                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
518                                                  shape->cached_fill->getEdge(i).st);
519                 }
520                 shape->fill_shp->ForceToPolygon();
521                 shape->fill_shp->needPointsSorting();
522                 shape->fill_shp->needEdgesSorting();
523             }
524             shape->delayed_shp |= shape->cached_fpartialy;
525         }
526     }
529 void
530 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
532     SPStyle* style = shape->style;
534     float const scale = gc->transform.descrim();
536     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
538     if (outline) {
539         // cairo does not need the livarot path for rendering
540         return; 
541     }
543     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
544     // (since it will only be used for picking, not for rendering)
546     if (outline ||
547         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
548          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
550         float style_width = MAX(0.125, shape->_stroke.width * scale);
551         float width;
552         if (outline) {
553             width = 0.5; // 1 pixel wide, independent of zoom
554         } else {
555             width = style_width;
556         }
558         Geom::Matrix  cached_to_new = Geom::identity();
560         int isometry = 0;
561         if ( shape->cached_stroke ) {
562             if (shape->cached_sctm == gc->transform) {
563                 isometry = 2; // identity
564             } else {
565                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
566                 isometry = matrix_is_isometry(cached_to_new);
567             }
568             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
569                 isometry = 0;
570             if (0 != isometry && width != shape->cached_width) { 
571                 // if this happens without setting style, we have just switched to outline or back
572                 isometry = 0; 
573             } 
574         }
576         if ( isometry == 0 ) {
577             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
578             shape->cached_stroke->Reset();
579             Path*  thePath = new Path;
580             Shape* theShape = new Shape;
581             {
582                 Geom::Matrix   tempMat( gc->transform );
583                 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
584             }
586             // add some padding to the rendering area, so clipped path does not go into a render area
587             NRRectL padded_area = *area;
588             padded_area.x0 -= (NR::ICoord)width;
589             padded_area.x1 += (NR::ICoord)width;
590             padded_area.y0 -= (NR::ICoord)width;
591             padded_area.y1 += (NR::ICoord)width;
592             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
593                 thePath->Convert((outline) ? 4.0 : 1.0);
594                 shape->cached_spartialy = false;
595             }
596             else {
597                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
598                 shape->cached_spartialy = true;
599             }
601             if (style->stroke_dash.n_dash && !outline) {
602                 thePath->DashPolylineFromStyle(style, scale, 1.0);
603             }
605             ButtType butt=butt_straight;
606             switch (shape->_stroke.cap) {
607                 case NRArenaShape::BUTT_CAP:
608                     butt = butt_straight;
609                     break;
610                 case NRArenaShape::ROUND_CAP:
611                     butt = butt_round;
612                     break;
613                 case NRArenaShape::SQUARE_CAP:
614                     butt = butt_square;
615                     break;
616             }
617             JoinType join=join_straight;
618             switch (shape->_stroke.join) {
619                 case NRArenaShape::MITRE_JOIN:
620                     join = join_pointy;
621                     break;
622                 case NRArenaShape::ROUND_JOIN:
623                     join = join_round;
624                     break;
625                 case NRArenaShape::BEVEL_JOIN:
626                     join = join_straight;
627                     break;
628             }
630             if (outline) {
631                 butt = butt_straight;
632                 join = join_straight;
633             }
635             thePath->Stroke(theShape, false, 0.5*width, join, butt,
636                             0.5*width*shape->_stroke.mitre_limit);
639             if (outline) {
640                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
641                 shape->cached_stroke->Copy(theShape);
642             } else {
643                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
644             }
646             shape->cached_width = width;
648             shape->cached_sctm=gc->transform;
649             shape->cached_sarea = *area;
650             delete thePath;
651             delete theShape;
652             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
654             shape->stroke_shp->Copy(shape->cached_stroke);
656         } else if ( 2 == isometry ) {
657             if ( shape->stroke_shp == NULL ) {
658                 shape->stroke_shp=new Shape;
659                 shape->stroke_shp->Copy(shape->cached_stroke);
660             }
661         } else {
662             if ( shape->stroke_shp == NULL )
663                 shape->stroke_shp=new Shape;
664             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
665             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
666                 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
667             if ( isometry == 1 ) {
668                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
669                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
670                                                shape->cached_stroke->getEdge(i).en);
671             } else if ( isometry == -1 ) {
672                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
673                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
674                                                shape->cached_stroke->getEdge(i).st);
675             }
676             shape->stroke_shp->ForceToPolygon();
677             shape->stroke_shp->needPointsSorting();
678             shape->stroke_shp->needEdgesSorting();
679         }
680         shape->delayed_shp |= shape->cached_spartialy;
681     }
685 void
686 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox)
688     /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
689      * if so, then this can be written much more compact ! */
691     if ( shape->stroke_shp ) {
692         Shape *larger = shape->stroke_shp;
693         larger->CalcBBox();
694         larger->leftX   = floor(larger->leftX);
695         larger->rightX  = ceil(larger->rightX);
696         larger->topY    = floor(larger->topY);
697         larger->bottomY = ceil(larger->bottomY);
698         Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
699                                 Geom::Interval(larger->topY,  larger->bottomY) );
700         bbox.unionWith(stroke_bbox);
701     }
703     if ( shape->fill_shp ) {
704         Shape *larger = shape->fill_shp;
705         larger->CalcBBox();
706         larger->leftX   = floor(larger->leftX);
707         larger->rightX  = ceil(larger->rightX);
708         larger->topY    = floor(larger->topY);
709         larger->bottomY = ceil(larger->bottomY);
710         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
711                               Geom::Interval(larger->topY,  larger->bottomY) );
712         bbox.unionWith(fill_bbox);
713     }
716 // cairo outline rendering:
717 static unsigned int
718 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, boost::optional<Geom::Rect> area)
720     NRArenaShape *shape = NR_ARENA_SHAPE(item);
722     if (!ct) 
723         return item->state;
725     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
726     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
727     // by setting color channels in the "wrong" order
728     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));
730     cairo_set_line_width(ct, 0.5);
731     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
732     cairo_new_path(ct);
734     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
736     cairo_stroke(ct);
738     return item->state;
741 // cairo stroke rendering (flat color only so far!):
742 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
743 // (need to switch them to premul before this can be enabled)
744 void
745 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
747     NRArenaShape *shape = NR_ARENA_SHAPE(item);
748     SPStyle const *style = shape->style;
750     float const scale = shape->ctm.descrim();
752     if (fabs(shape->_stroke.width * scale) < 0.01)
753         return;
755     cairo_t *ct = nr_create_cairo_context (area, pb);
757     if (!ct)
758         return;
760     guint32 rgba;
761     if ( item->render_opacity ) {
762         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
763                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
764     } else {
765         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
766     }
768     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
769     // by setting color channels in the "wrong" order
770     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));
772     float style_width = MAX(0.125, shape->_stroke.width * scale);
773     cairo_set_line_width(ct, style_width);
775     switch (shape->_stroke.cap) {
776         case NRArenaShape::BUTT_CAP:
777             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
778             break;
779         case NRArenaShape::ROUND_CAP:
780             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
781             break;
782         case NRArenaShape::SQUARE_CAP:
783             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
784             break;
785     }
786     switch (shape->_stroke.join) {
787         case NRArenaShape::MITRE_JOIN:
788             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
789             break;
790         case NRArenaShape::ROUND_JOIN:
791             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
792             break;
793         case NRArenaShape::BEVEL_JOIN:
794             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
795             break;
796     }
798     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
800     if (style->stroke_dash.n_dash) {
801         NRVpathDash dash;
802         dash.offset = style->stroke_dash.offset * scale;
803         dash.n_dash = style->stroke_dash.n_dash;
804         dash.dash = g_new(double, dash.n_dash);
805         for (int i = 0; i < dash.n_dash; i++) {
806             dash.dash[i] = style->stroke_dash.dash[i] * scale;
807         }
808         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
809         g_free(dash.dash);
810     }
812     cairo_set_tolerance(ct, 0.1);
813     cairo_new_path(ct);
815     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
817     cairo_stroke(ct);
819     cairo_surface_t *cst = cairo_get_target(ct);
820     cairo_destroy (ct);
821     cairo_surface_finish (cst);
822     cairo_surface_destroy (cst);
824     pb->empty = FALSE;
828 /**
829  * Renders the item.  Markers are just composed into the parent buffer.
830  */
831 static unsigned int
832 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
834     NRArenaShape *shape = NR_ARENA_SHAPE(item);
836     if (!shape->curve) return item->state;
837     if (!shape->style) return item->state;
839     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
841     if (outline) { // cairo outline rendering
843         pb->empty = FALSE;
844         unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
845         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
847     } else {
849     if ( shape->delayed_shp ) {
850         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
851             NRGC   tempGC(NULL);
852             tempGC.transform=shape->ctm;
853             shape->delayed_shp = false;
854             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
855             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
856 /*      NRRect bbox;
857         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
858         nr_arena_shape_add_bboxes(shape,bbox);
859         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
860         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
861         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
862         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
863         shape->approx_bbox=item->bbox;*/
864         } else {
865             return item->state;
866         }
867     }
869     SPStyle const *style = shape->style;
870     if (shape->fill_shp) {
871         NRPixBlock m;
872         guint32 rgba;
874         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
876         // if memory allocation failed, abort render
877         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
878             nr_pixblock_release (&m);
879             return (item->state);
880         }
882         m.visible_area = pb->visible_area;
883         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
884         m.empty = FALSE;
886         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
887             // do not render fill in any way
888         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
889             if ( item->render_opacity ) {
890                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
891                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
892             } else {
893                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
894             }
895             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
896             pb->empty = FALSE;
897         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
898             if (shape->fill_painter) {
899                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
900             }
901         }
903         nr_pixblock_release(&m);
904     }
906     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
908         // cairo_arena_shape_render_stroke(item, area, pb);
910         guint32 rgba;
911         NRPixBlock m;
913         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
915         // if memory allocation failed, abort render
916         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
917             nr_pixblock_release (&m);
918             return (item->state);
919         }
921         m.visible_area = pb->visible_area;
922         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
923         m.empty = FALSE;
925             if ( item->render_opacity ) {
926                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
927                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
928             } else {
929                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
930             }
931             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
932             pb->empty = FALSE;
934         nr_pixblock_release(&m);
936     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
938         NRPixBlock m;
940         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
942         // if memory allocation failed, abort render
943         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
944             nr_pixblock_release (&m);
945             return (item->state);
946         }
948         m.visible_area = pb->visible_area; 
949         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
950         m.empty = FALSE;
952         if (shape->stroke_painter) {
953             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
954         }
956         nr_pixblock_release(&m);
957     }
959     } // non-cairo non-outline branch
961     /* Render markers into parent buffer */
962     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
963         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
964         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
965     }
967     return item->state;
971 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
972 // reenable this when the bug is fixed and remove the rest of this function
973 // TODO
974 #if defined(DEADCODE) && !defined(DEADCODE)
975 static guint
976 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
978     NRArenaShape *shape = NR_ARENA_SHAPE(item);
979     if (!shape->curve) return item->state;
981         cairo_t *ct = nr_create_cairo_context (area, pb);
983         if (!ct)
984             return item->state;
986         cairo_set_source_rgba(ct, 0, 0, 0, 1);
988         cairo_new_path(ct);
990         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
992         cairo_fill(ct);
994         cairo_surface_t *cst = cairo_get_target(ct);
995         cairo_destroy (ct);
996         cairo_surface_finish (cst);
997         cairo_surface_destroy (cst);
999         pb->empty = FALSE;
1001         return item->state;
1003 #endif //defined(DEADCODE) && !defined(DEADCODE)
1006 static guint
1007 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1009     //return cairo_arena_shape_clip(item, area, pb);
1011     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1012     if (!shape->curve) return item->state;
1014     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1015         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1016             NRGC   tempGC(NULL);
1017             tempGC.transform=shape->ctm;
1018             shape->delayed_shp = false;
1019             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1020         } else {
1021             return item->state;
1022         }
1023     }
1025     if ( shape->fill_shp ) {
1026         NRPixBlock m;
1028         /* fixme: We can OR in one step (Lauris) */
1029         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1031         // if memory allocation failed, abort 
1032         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1033             nr_pixblock_release (&m);
1034             return (item->state);
1035         }
1037         m.visible_area = pb->visible_area; 
1038         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1040         for (int y = area->y0; y < area->y1; y++) {
1041             unsigned char *s, *d;
1042             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1043             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1044             for (int x = area->x0; x < area->x1; x++) {
1045                 *d = NR_COMPOSEA_111(*s, *d);
1046                 d ++;
1047                 s ++;
1048             }
1049         }
1050         nr_pixblock_release(&m);
1051         pb->empty = FALSE;
1052     }
1054     return item->state;
1057 static NRArenaItem *
1058 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1060     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1062     if (shape->repick_after > 0)
1063         shape->repick_after--;
1065     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1066         return shape->last_pick;
1068     if (!shape->curve) return NULL;
1069     if (!shape->style) return NULL;
1071     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1073     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
1074         // fully transparent, no pick unless outline mode
1075         return NULL;
1077     GTimeVal tstart, tfinish;
1078     g_get_current_time (&tstart);
1080     double width;
1081     if (outline) {
1082         width = 0.5;
1083     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1084         float const scale = shape->ctm.descrim();
1085         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1086     } else {
1087         width = 0;
1088     }
1090     double dist = NR_HUGE;
1091     int wind = 0;
1092     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1093              && shape->_fill.opacity > 1e-3 && !outline);
1095     if (item->arena->canvasarena) {
1096         Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1097         viewbox.expandBy (width);
1098         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1099     } else {
1100         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1101     }
1103     g_get_current_time (&tfinish);
1104     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1105     //g_print ("pick time %lu\n", this_pick);
1107     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1108         shape->repick_after = this_pick / 5000;
1109     }
1111     // covered by fill?
1112     if (needfill) {
1113         if (!shape->style->fill_rule.computed) {
1114             if (wind != 0) {
1115                 shape->last_pick = item;
1116                 return item;
1117             }
1118         } else {
1119             if (wind & 0x1) {
1120                 shape->last_pick = item;
1121                 return item;
1122             }
1123         }
1124     }
1126     // close to the edge, as defined by strokewidth and delta?
1127     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1128     if (needfill || width > 0) { // if either fill or stroke visible,
1129         if ((dist - width) < delta) {
1130             shape->last_pick = item;
1131             return item;
1132         }
1133     }
1135     // if not picked on the shape itself, try its markers
1136     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1137         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1138         if (ret) {
1139             shape->last_pick = item;
1140             return item;
1141         }
1142     }
1144     shape->last_pick = NULL;
1145     return NULL;
1148 /**
1149  *
1150  *  Requests a render of the shape, then if the shape is already a curve it
1151  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1152  *  curve and adds it to the shape.  Finally, it requests an update of the
1153  *  arena for the shape.
1154  */
1155 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1157     g_return_if_fail(shape != NULL);
1158     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1160     if ( justTrans == false ) {
1161         // dirty cached versions
1162         if ( shape->cached_fill ) {
1163             delete shape->cached_fill;
1164             shape->cached_fill=NULL;
1165         }
1166         if ( shape->cached_stroke ) {
1167             delete shape->cached_stroke;
1168             shape->cached_stroke=NULL;
1169         }
1170     }
1172     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1174     if (shape->curve) {
1175         shape->curve->unref();
1176         shape->curve = NULL;
1177     }
1179     if (curve) {
1180         shape->curve = curve;
1181         curve->ref();
1182     }
1184     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1187 void NRArenaShape::setFill(SPPaintServer *server) {
1188     _fill.paint.set(server);
1189     _invalidateCachedFill();
1192 void NRArenaShape::setFill(SPColor const &color) {
1193     _fill.paint.set(color);
1194     _invalidateCachedFill();
1197 void NRArenaShape::setFillOpacity(double opacity) {
1198     _fill.opacity = opacity;
1199     _invalidateCachedFill();
1202 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1203     _fill.rule = rule;
1204     _invalidateCachedFill();
1207 void NRArenaShape::setStroke(SPPaintServer *server) {
1208     _stroke.paint.set(server);
1209     _invalidateCachedStroke();
1212 void NRArenaShape::setStroke(SPColor const &color) {
1213     _stroke.paint.set(color);
1214     _invalidateCachedStroke();
1217 void NRArenaShape::setStrokeOpacity(double opacity) {
1218     _stroke.opacity = opacity;
1219     _invalidateCachedStroke();
1222 void NRArenaShape::setStrokeWidth(double width) {
1223     _stroke.width = width;
1224     _invalidateCachedStroke();
1227 void NRArenaShape::setMitreLimit(double limit) {
1228     _stroke.mitre_limit = limit;
1229     _invalidateCachedStroke();
1232 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1233     _stroke.cap = cap;
1234     _invalidateCachedStroke();
1237 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1238     _stroke.join = join;
1239     _invalidateCachedStroke();
1242 /** nr_arena_shape_set_style
1243  *
1244  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1245  */
1246 void
1247 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1249     g_return_if_fail(shape != NULL);
1250     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1252     if (style) sp_style_ref(style);
1253     if (shape->style) sp_style_unref(shape->style);
1254     shape->style = style;
1256     if ( style->fill.isPaintserver() ) {
1257         shape->setFill(style->getFillPaintServer());
1258     } else if ( style->fill.isColor() ) {
1259         shape->setFill(style->fill.value.color);
1260     } else if ( style->fill.isNone() ) {
1261         shape->setFill(NULL);
1262     } else {
1263         g_assert_not_reached();
1264     }
1265     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1266     switch (style->fill_rule.computed) {
1267         case SP_WIND_RULE_EVENODD: {
1268             shape->setFillRule(NRArenaShape::EVEN_ODD);
1269             break;
1270         }
1271         case SP_WIND_RULE_NONZERO: {
1272             shape->setFillRule(NRArenaShape::NONZERO);
1273             break;
1274         }
1275         default: {
1276             g_assert_not_reached();
1277         }
1278     }
1280     if ( style->stroke.isPaintserver() ) {
1281         shape->setStroke(style->getStrokePaintServer());
1282     } else if ( style->stroke.isColor() ) {
1283         shape->setStroke(style->stroke.value.color);
1284     } else if ( style->stroke.isNone() ) {
1285         shape->setStroke(NULL);
1286     } else {
1287         g_assert_not_reached();
1288     }
1289     shape->setStrokeWidth(style->stroke_width.computed);
1290     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1291     switch (style->stroke_linecap.computed) {
1292         case SP_STROKE_LINECAP_ROUND: {
1293             shape->setLineCap(NRArenaShape::ROUND_CAP);
1294             break;
1295         }
1296         case SP_STROKE_LINECAP_SQUARE: {
1297             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1298             break;
1299         }
1300         case SP_STROKE_LINECAP_BUTT: {
1301             shape->setLineCap(NRArenaShape::BUTT_CAP);
1302             break;
1303         }
1304         default: {
1305             g_assert_not_reached();
1306         }
1307     }
1308     switch (style->stroke_linejoin.computed) {
1309         case SP_STROKE_LINEJOIN_ROUND: {
1310             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1311             break;
1312         }
1313         case SP_STROKE_LINEJOIN_BEVEL: {
1314             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1315             break;
1316         }
1317         case SP_STROKE_LINEJOIN_MITER: {
1318             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1319             break;
1320         }
1321         default: {
1322             g_assert_not_reached();
1323         }
1324     }
1325     shape->setMitreLimit(style->stroke_miterlimit.value);
1327     //if shape has a filter
1328     if (style->filter.set && style->getFilter()) {
1329         if (!shape->filter) {
1330             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1331             shape->filter = new NR::Filter(primitives);
1332         }
1333         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1334     } else {
1335         //no filter set for this shape
1336         delete shape->filter;
1337         shape->filter = NULL;
1338     }
1340     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1343 void
1344 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1346     g_return_if_fail(shape != NULL);
1347     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1348     g_return_if_fail(pbox != NULL);
1350     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1351         shape->paintbox = *pbox;
1352     } else {
1353         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1354         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1355         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1356     }
1358     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1361 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1363     paintbox.x0 = pbox.min()[Geom::X];
1364     paintbox.y0 = pbox.min()[Geom::Y];
1365     paintbox.x1 = pbox.max()[Geom::X];
1366     paintbox.y1 = pbox.max()[Geom::Y];
1368     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1371 static void
1372 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1374     if ( st >= en ) return;
1375     if ( vst < 0 ) vst=0;
1376     if ( vst > 1 ) vst=1;
1377     if ( ven < 0 ) ven=0;
1378     if ( ven > 1 ) ven=1;
1379     float   sv=vst;
1380     float   dv=ven-vst;
1381     int     len=en-st;
1382     unsigned char*   d=(unsigned char*)dest.buffer;
1383     d+=(st-dest.startPix);
1384     if ( fabs(dv) < 0.001 ) {
1385         if ( vst > 0.999 ) {
1386             /* Simple copy */
1387             while (len > 0) {
1388                 d[0] = 255;
1389                 d += 1;
1390                 len -= 1;
1391             }
1392         } else {
1393             sv*=256;
1394             unsigned int c0_24=(int)sv;
1395             c0_24&=0xFF;
1396             while (len > 0) {
1397                 /* Draw */
1398                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1399                 d += 1;
1400                 len -= 1;
1401             }
1402         }
1403     } else {
1404         if ( en <= st+1 ) {
1405             sv=0.5*(vst+ven);
1406             sv*=256;
1407             unsigned int c0_24=(int)sv;
1408             c0_24&=0xFF;
1409             /* Draw */
1410             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1411         } else {
1412             dv/=len;
1413             sv+=0.5*dv; // correction trapezoidale
1414             sv*=16777216;
1415             dv*=16777216;
1416             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1417             int s0_24 = static_cast<int>(dv);
1418             while (len > 0) {
1419                 unsigned int ca;
1420                 /* Draw */
1421                 ca = c0_24 >> 16;
1422                 if ( ca > 255 ) ca=255;
1423                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1424                 d += 1;
1425                 c0_24 += s0_24;
1426                 c0_24 = CLAMP(c0_24, 0, 16777216);
1427                 len -= 1;
1428             }
1429         }
1430     }
1433 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1435     theS->CalcBBox();
1436     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1437     int    il,ir,it,ib;
1438     il=(int)floor(l);
1439     ir=(int)ceil(r);
1440     it=(int)floor(t);
1441     ib=(int)ceil(b);
1443     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1444     if ( il < m.area.x0 ) il=m.area.x0;
1445     if ( it < m.area.y0 ) it=m.area.y0;
1446     if ( ir > m.area.x1 ) ir=m.area.x1;
1447     if ( ib > m.area.y1 ) ib=m.area.y1;
1449     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1450     int    curPt;
1451     float  curY;
1452     theS->BeginQuickRaster(curY, curPt);
1454     FloatLigne *theI = new FloatLigne();
1455     IntLigne *theIL = new IntLigne();
1457     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1459     char *mdata = (char*)m.data.px;
1460     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1461     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1462     for (int y = it; y < ib; y++) {
1463         theI->Reset();
1464         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1465         theI->Flatten();
1466         theIL->Copy(theI);
1468         raster_info  dest;
1469         dest.startPix=il;
1470         dest.endPix=ir;
1471         dest.sth=il;
1472         dest.stv=y;
1473         dest.buffer=ligStart;
1474         theIL->Raster(dest, NULL, shape_run_A8_OR);
1475         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1476     }
1477     theS->EndQuickRaster();
1478     delete theI;
1479     delete theIL;
1483 /*
1484   Local Variables:
1485   mode:c++
1486   c-file-style:"stroustrup"
1487   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1488   indent-tabs-mode:nil
1489   fill-column:99
1490   End:
1491 */
1492 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :