Code

2e15ac5aa902d580bf840c70d0138161007a74a2
[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  */
17 #include <display/nr-arena.h>
18 #include <display/nr-arena-shape.h>
19 #include "display/nr-filter.h"
20 #include "display/nr-filter-gaussian.h"
21 #include "display/nr-filter-types.h"
22 #include <libnr/n-art-bpath.h>
23 #include <libnr/nr-path.h>
24 #include <libnr/nr-pixops.h>
25 #include <libnr/nr-matrix-ops.h>
26 #include <libnr/nr-matrix-fns.h>
27 #include <libnr/nr-blit.h>
28 #include <livarot/Path.h>
29 #include <livarot/float-line.h>
30 #include <livarot/int-line.h>
31 #include <style.h>
32 #include "prefs-utils.h"
33 #include "sp-filter.h"
34 #include "sp-gaussian-blur.h"
36 #include <cairo.h>
38 //int  showRuns=0;
39 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
41 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
42 static void nr_arena_shape_init(NRArenaShape *shape);
43 static void nr_arena_shape_finalize(NRObject *object);
45 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
46 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
47 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
48 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
50 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
51 static unsigned int nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
52 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
53 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
55 static NRArenaItemClass *shape_parent_class;
57 NRType
58 nr_arena_shape_get_type(void)
59 {
60     static NRType type = 0;
61     if (!type) {
62         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
63                                        "NRArenaShape",
64                                        sizeof(NRArenaShapeClass),
65                                        sizeof(NRArenaShape),
66                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
67                                        (void (*)(NRObject *)) nr_arena_shape_init);
68     }
69     return type;
70 }
72 static void
73 nr_arena_shape_class_init(NRArenaShapeClass *klass)
74 {
75     NRObjectClass *object_class;
76     NRArenaItemClass *item_class;
78     object_class = (NRObjectClass *) klass;
79     item_class = (NRArenaItemClass *) klass;
81     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
83     object_class->finalize = nr_arena_shape_finalize;
84     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
86     item_class->children = nr_arena_shape_children;
87     item_class->add_child = nr_arena_shape_add_child;
88     item_class->set_child_position = nr_arena_shape_set_child_position;
89     item_class->remove_child = nr_arena_shape_remove_child;
90     item_class->update = nr_arena_shape_update;
91     item_class->render = nr_arena_shape_render;
92     item_class->clip = nr_arena_shape_clip;
93     item_class->pick = nr_arena_shape_pick;
94 }
96 /**
97  * Initializes the arena shape, setting all parameters to null, 0, false,
98  * or other defaults
99  */
100 static void
101 nr_arena_shape_init(NRArenaShape *shape)
103     shape->curve = NULL;
104     shape->style = NULL;
105     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
106     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
108     nr_matrix_set_identity(&shape->ctm);
109     shape->fill_painter = NULL;
110     shape->stroke_painter = NULL;
111     shape->cached_fill = NULL;
112     shape->cached_stroke = NULL;
113     shape->cached_fpartialy = false;
114     shape->cached_spartialy = false;
115     shape->fill_shp = NULL;
116     shape->stroke_shp = NULL;
118     shape->delayed_shp = false;
120     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
121     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
122     nr_matrix_set_identity(&shape->cached_fctm);
123     nr_matrix_set_identity(&shape->cached_sctm);
125     shape->markers = NULL;
128 static void
129 nr_arena_shape_finalize(NRObject *object)
131     NRArenaShape *shape = (NRArenaShape *) object;
133     if (shape->fill_shp) delete shape->fill_shp;
134     if (shape->stroke_shp) delete shape->stroke_shp;
135     if (shape->cached_fill) delete shape->cached_fill;
136     if (shape->cached_stroke) delete shape->cached_stroke;
137     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
138     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
140     if (shape->style) sp_style_unref(shape->style);
141     if (shape->curve) sp_curve_unref(shape->curve);
143     ((NRObjectClass *) shape_parent_class)->finalize(object);
146 /**
147  * Retrieves the markers from the item
148  */
149 static NRArenaItem *
150 nr_arena_shape_children(NRArenaItem *item)
152     NRArenaShape *shape = (NRArenaShape *) item;
154     return shape->markers;
157 /**
158  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
159  * the prev and next items.  If ref is NULL, then it sets the item's markers
160  * as the next items.
161  */
162 static void
163 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
165     NRArenaShape *shape = (NRArenaShape *) item;
167     if (!ref) {
168         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
169     } else {
170         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
171     }
173     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
176 /**
177  * Removes child from the shape.  If there are no prev items in 
178  * the child, it sets items' markers to the next item in the child.
179  */
180 static void
181 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
183     NRArenaShape *shape = (NRArenaShape *) item;
185     if (child->prev) {
186         nr_arena_item_detach(item, child);
187     } else {
188         shape->markers = nr_arena_item_detach(item, child);
189     }
191     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
194 /**
195  * Detaches child from item, and if there are no previous items in child, it 
196  * sets item's markers to the child.  It then attaches the child back onto the item.
197  * If ref is null, it sets the markers to be the next item, otherwise it uses
198  * the next/prev items in ref.
199  */
200 static void
201 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
203     NRArenaShape *shape = (NRArenaShape *) item;
205     if (child->prev) {
206         nr_arena_item_detach(item, child);
207     } else {
208         shape->markers = nr_arena_item_detach(item, child);
209     }
211     if (!ref) {
212         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
213     } else {
214         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
215     }
217     nr_arena_item_request_render(child);
220 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
221 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
222 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
224 /**
225  * Updates the arena shape 'item' and all of its children, including the markers.
226  */
227 static guint
228 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
230     NRRect bbox;
232     NRArenaShape *shape = NR_ARENA_SHAPE(item);
234     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
236     unsigned int newstate;
237     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
238         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
239         beststate = beststate & newstate;
240     }
242     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
243         /* We do not have to create rendering structures */
244         shape->ctm = gc->transform;
245         if (state & NR_ARENA_ITEM_STATE_BBOX) {
246             if (shape->curve) {
247                 NRBPath bp;
248                 /* fixme: */
249                 bbox.x0 = bbox.y0 = NR_HUGE;
250                 bbox.x1 = bbox.y1 = -NR_HUGE;
251                 bp.path = SP_CURVE_BPATH(shape->curve);
252                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
253                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
254                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
255                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
256                 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
257             }
258             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
259                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
260                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
261                 }
262             }
263         }
264         return (state | item->state);
265     }
267     shape->delayed_shp=true;
268     shape->ctm = gc->transform;
269     bbox.x0 = bbox.y0 = NR_HUGE;
270     bbox.x1 = bbox.y1 = -NR_HUGE;
272     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
274     if (shape->curve) {
275         NRBPath bp;
276         /* fixme: */
277         bbox.x0 = bbox.y0 = NR_HUGE;
278         bbox.x1 = bbox.y1 = -NR_HUGE;
279         bp.path = SP_CURVE_BPATH(shape->curve);
280         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
281         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
282             float width, scale;
283             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
284             width = MAX(0.125, shape->_stroke.width * scale);
285             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
286                 bbox.x0-=width;
287                 bbox.x1+=width;
288                 bbox.y0-=width;
289                 bbox.y1+=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                 bbox.x0-=miterMax;
296                 bbox.x1+=miterMax;
297                 bbox.y0-=miterMax;
298                 bbox.y1+=miterMax;
299             }
300         }
301     } else {
302     }
303     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
304     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
305     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
306     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
307     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
309     /* Release state data */
310     if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
311         /* Concept test */
312         if (shape->fill_shp) {
313             delete shape->fill_shp;
314             shape->fill_shp = NULL;
315         }
316     }
317     if (shape->stroke_shp) {
318         delete shape->stroke_shp;
319         shape->stroke_shp = NULL;
320     }
321     if (shape->fill_painter) {
322         sp_painter_free(shape->fill_painter);
323         shape->fill_painter = NULL;
324     }
325     if (shape->stroke_painter) {
326         sp_painter_free(shape->stroke_painter);
327         shape->stroke_painter = NULL;
328     }
330     if (!shape->curve || 
331         !shape->style ||
332         sp_curve_is_empty(shape->curve) ||
333         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
334          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
335     {
336         item->bbox = shape->approx_bbox;
337         return NR_ARENA_ITEM_STATE_ALL;
338     }
340     /* Build state data */
341     if ( shape->delayed_shp ) {
342         item->bbox=shape->approx_bbox;
343     } else {
344         nr_arena_shape_update_stroke(shape, gc, area);
345         nr_arena_shape_update_fill(shape, gc, area);
347         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
348         nr_arena_shape_add_bboxes(shape,bbox);
350         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
351         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
352         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
353         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
354     }
356     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
358     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
359     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
360     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
361     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
362     nr_arena_request_render_rect(item->arena, &item->bbox);
364     item->render_opacity = TRUE;
365     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
366         if (gc && gc->parent) {
367             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
368                                                               NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
369                                                               &shape->paintbox);
370         }
371         item->render_opacity = FALSE;
372     }
373     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
374         if (gc && gc->parent) {
375             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
376                                                                 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
377                                                                 &shape->paintbox);
378         }
379         item->render_opacity = FALSE;
380     }
381     if ( item->render_opacity == TRUE
382          && shape->_fill.paint.type()   != NRArenaShape::Paint::NONE
383          && shape->_stroke.paint.type() != NRArenaShape::Paint::NONE )
384     {
385         // don't merge item opacity with paint opacity if there is a stroke on the fill
386         item->render_opacity = FALSE;
387     }
389     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
390         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
391             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
392         }
393     }
395     return NR_ARENA_ITEM_STATE_ALL;
398 int matrix_is_isometry(NR::Matrix p) {
399     NR::Matrix   tp;
400     // transposition
401     tp[0]=p[0];
402     tp[1]=p[2];
403     tp[2]=p[1];
404     tp[3]=p[3];
405     for (int i = 4; i < 6; i++) // shut valgrind up :)
406         tp[i] = p[i] = 0;
407     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
408     // Is the adjunct nearly an identity function?
409     if (isom.is_translation(0.01)) {
410         // the transformation is an isometry -> no need to recompute
411         // the uncrossed polygon
412         if ( p.det() < 0 )
413             return -1;
414         else
415             return 1;
416     }
417     return 0;
420 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
421     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
424 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
425 void
426 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
428     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
429         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
430         if (TRUE || !shape->fill_shp) {
431             NR::Matrix  cached_to_new = NR::identity();
432             int isometry = 0;
433             if ( shape->cached_fill ) {
434                 if (shape->cached_fctm == gc->transform) {
435                     isometry = 2; // identity
436                 } else {
437                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
438                     isometry = matrix_is_isometry(cached_to_new);
439                 }
440                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
441                     isometry = 0;
442             }
443             if ( isometry == 0 ) {
444                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
445                 shape->cached_fill->Reset();
447                 Path*  thePath=new Path;
448                 Shape* theShape=new Shape;
449                 {
450                     NR::Matrix   tempMat(gc->transform);
451                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
452                 }
454                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
455                     thePath->Convert(1.0);
456                     shape->cached_fpartialy = false;
457                 } else {
458                     thePath->Convert(area, 1.0);
459                     shape->cached_fpartialy = true;
460                 }
462                 thePath->Fill(theShape, 0);
464                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
465                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
466                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
467                     //shape->cached_fill->Copy(theShape);
468                 } else {
469                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
470                 }
471                 shape->cached_fctm=gc->transform;
472                 shape->cached_farea = *area;
473                 delete theShape;
474                 delete thePath;
475                 if ( shape->fill_shp == NULL )
476                     shape->fill_shp = new Shape;
478                 shape->fill_shp->Copy(shape->cached_fill);
480             } else if ( 2 == isometry ) {
481                 if ( shape->fill_shp == NULL ) {
482                     shape->fill_shp = new Shape;
483                     shape->fill_shp->Copy(shape->cached_fill);
484                 }
485             } else {
487                 if ( shape->fill_shp == NULL )
488                     shape->fill_shp = new Shape;
490                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
491                                        shape->cached_fill->numberOfEdges());
492                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
493                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
494                 if ( isometry == 1 ) {
495                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
496                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
497                                                  shape->cached_fill->getEdge(i).en);
498                 } else if ( isometry == -1 ) { // need to flip poly.
499                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
500                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
501                                                  shape->cached_fill->getEdge(i).st);
502                 }
503                 shape->fill_shp->ForceToPolygon();
504                 shape->fill_shp->needPointsSorting();
505                 shape->fill_shp->needEdgesSorting();
506             }
507             shape->delayed_shp |= shape->cached_fpartialy;
508         }
509     }
512 void
513 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
515     SPStyle* style = shape->style;
517     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
519     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
521     if (outline) {
522         // cairo does not need the livarot path for rendering... but unfortunately it's still used for picking
523         // FIXME: switch picking to using cairo_in_stroke? 
524         //return; 
525     }
527     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
528     // (since it will only be used for picking, not for rendering)
530     if (outline ||
531         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
532          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
534         float style_width = MAX(0.125, shape->_stroke.width * scale);
535         float width;
536         if (outline) {
537             width = 0.5; // 1 pixel wide, independent of zoom
538         } else {
539             width = style_width;
540         }
542         NR::Matrix  cached_to_new = NR::identity();
544         int isometry = 0;
545         if ( shape->cached_stroke ) {
546             if (shape->cached_sctm == gc->transform) {
547                 isometry = 2; // identity
548             } else {
549                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
550                 isometry = matrix_is_isometry(cached_to_new);
551             }
552             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
553                 isometry = 0;
554             if (0 != isometry && width != shape->cached_width) { 
555                 // if this happens without setting style, we have just switched to outline or back
556                 isometry = 0; 
557             } 
558         }
560         if ( isometry == 0 ) {
561             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
562             shape->cached_stroke->Reset();
563             Path*  thePath = new Path;
564             Shape* theShape = new Shape;
565             {
566                 NR::Matrix   tempMat(gc->transform);
567                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
568             }
570             // add some padding to the rendering area, so clipped path does not go into a render area
571             NRRectL padded_area = *area;
572             padded_area.x0 -= (NR::ICoord)width;
573             padded_area.x1 += (NR::ICoord)width;
574             padded_area.y0 -= (NR::ICoord)width;
575             padded_area.y1 += (NR::ICoord)width;
576             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
577                 thePath->Convert((outline) ? 4.0 : 1.0);
578                 shape->cached_spartialy = false;
579             }
580             else {
581                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
582                 shape->cached_spartialy = true;
583             }
585             if (style->stroke_dash.n_dash && !outline) {
586                 thePath->DashPolylineFromStyle(style, scale, 1.0);
587             }
589             ButtType butt=butt_straight;
590             switch (shape->_stroke.cap) {
591                 case NRArenaShape::BUTT_CAP:
592                     butt = butt_straight;
593                     break;
594                 case NRArenaShape::ROUND_CAP:
595                     butt = butt_round;
596                     break;
597                 case NRArenaShape::SQUARE_CAP:
598                     butt = butt_square;
599                     break;
600             }
601             JoinType join=join_straight;
602             switch (shape->_stroke.join) {
603                 case NRArenaShape::MITRE_JOIN:
604                     join = join_pointy;
605                     break;
606                 case NRArenaShape::ROUND_JOIN:
607                     join = join_round;
608                     break;
609                 case NRArenaShape::BEVEL_JOIN:
610                     join = join_straight;
611                     break;
612             }
614             if (outline) {
615                 butt = butt_straight;
616                 join = join_straight;
617             }
619             thePath->Stroke(theShape, false, 0.5*width, join, butt,
620                             0.5*width*shape->_stroke.mitre_limit);
623             if (outline) {
624                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
625                 shape->cached_stroke->Copy(theShape);
626             } else {
627                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
628             }
630             shape->cached_width = width;
632             shape->cached_sctm=gc->transform;
633             shape->cached_sarea = *area;
634             delete thePath;
635             delete theShape;
636             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
638             shape->stroke_shp->Copy(shape->cached_stroke);
640         } else if ( 2 == isometry ) {
641             if ( shape->stroke_shp == NULL ) {
642                 shape->stroke_shp=new Shape;
643                 shape->stroke_shp->Copy(shape->cached_stroke);
644             }
645         } else {
646             if ( shape->stroke_shp == NULL )
647                 shape->stroke_shp=new Shape;
648             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
649             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
650                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
651             if ( isometry == 1 ) {
652                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
653                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
654                                                shape->cached_stroke->getEdge(i).en);
655             } else if ( isometry == -1 ) {
656                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
657                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
658                                                shape->cached_stroke->getEdge(i).st);
659             }
660             shape->stroke_shp->ForceToPolygon();
661             shape->stroke_shp->needPointsSorting();
662             shape->stroke_shp->needEdgesSorting();
663         }
664         shape->delayed_shp |= shape->cached_spartialy;
665     }
669 void
670 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
672     if ( shape->stroke_shp ) {
673         shape->stroke_shp->CalcBBox();
674         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
675         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
676         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
677         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
678         if ( bbox.x0 >= bbox.x1 ) {
679             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
680                 bbox.x0=shape->stroke_shp->leftX;
681                 bbox.x1=shape->stroke_shp->rightX;
682             }
683         } else {
684             if ( shape->stroke_shp->leftX < bbox.x0 )
685                 bbox.x0=shape->stroke_shp->leftX;
686             if ( shape->stroke_shp->rightX > bbox.x1 )
687                 bbox.x1=shape->stroke_shp->rightX;
688         }
689         if ( bbox.y0 >= bbox.y1 ) {
690             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
691                 bbox.y0=shape->stroke_shp->topY;
692                 bbox.y1=shape->stroke_shp->bottomY;
693             }
694         } else {
695             if ( shape->stroke_shp->topY < bbox.y0 )
696                 bbox.y0=shape->stroke_shp->topY;
697             if ( shape->stroke_shp->bottomY > bbox.y1 )
698                 bbox.y1=shape->stroke_shp->bottomY;
699         }
700     }
701     if ( shape->fill_shp ) {
702         shape->fill_shp->CalcBBox();
703         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
704         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
705         shape->fill_shp->topY=floor(shape->fill_shp->topY);
706         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
707         if ( bbox.x0 >= bbox.x1 ) {
708             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
709                 bbox.x0=shape->fill_shp->leftX;
710                 bbox.x1=shape->fill_shp->rightX;
711             }
712         } else {
713             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
714             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
715         }
716         if ( bbox.y0 >= bbox.y1 ) {
717             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
718                 bbox.y0=shape->fill_shp->topY;
719                 bbox.y1=shape->fill_shp->bottomY;
720             }
721         } else {
722             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
723             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
724         }
725     }
728 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
729 static void
730 feed_curve_to_cairo (cairo_t *ct, SPCurve *curve, NR::Matrix trans, NR::Point shift)
732     NR::Point lastX(0,0);
733     bool  closed = false;
734     NArtBpath *bpath = SP_CURVE_BPATH(curve);
735     for (int i = 0; bpath[i].code != NR_END; i++) {
736         switch (bpath[i].code) {
737             case NR_MOVETO_OPEN:
738             case NR_MOVETO:
739                 if (closed) cairo_close_path(ct);
740                 closed = (bpath[i].code == NR_MOVETO);
741                 lastX[NR::X] = bpath[i].x3;
742                 lastX[NR::Y] = bpath[i].y3;
743                 lastX *= trans;
744                 lastX -= shift;
745                 cairo_move_to(ct, lastX[NR::X], lastX[NR::Y]);
746                 break;
748             case NR_LINETO:
749                 lastX[NR::X] = bpath[i].x3;
750                 lastX[NR::Y] = bpath[i].y3;
751                 lastX *= trans;
752                 lastX -= shift;
753                 cairo_line_to(ct, lastX[NR::X], lastX[NR::Y]);
754                 break;
756             case NR_CURVETO: {
757                 NR::Point  tm1, tm2, tm3;
758                 tm1[0]=bpath[i].x1;
759                 tm1[1]=bpath[i].y1;
760                 tm2[0]=bpath[i].x2;
761                 tm2[1]=bpath[i].y2;
762                 tm3[0]=bpath[i].x3;
763                 tm3[1]=bpath[i].y3;
764                 tm1 *= trans;
765                 tm2 *= trans;
766                 tm3 *= trans;
767                 tm1 -= shift;
768                 tm2 -= shift;
769                 tm3 -= shift;
770                 cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
771                 break;
772             }
774             default:
775                 break;
776         }
777     }
780 /** Creates a cairo context to render to the given pixblock on the given area */
781 cairo_t *
782 nr_create_cairo_context (NRRectL *area, NRPixBlock *pb)
784     if (!nr_rect_l_test_intersect (&pb->area, area)) 
785         return NULL;
787     NRRectL clip;
788     nr_rect_l_intersect (&clip, &pb->area, area);
789     unsigned char *dpx = NR_PIXBLOCK_PX (pb) + (clip.y0 - pb->area.y0) * pb->rs + NR_PIXBLOCK_BPP (pb) * (clip.x0 - pb->area.x0);
790     int width = area->x1 - area->x0;
791     int height = area->y1 - area->y0;
792     // even though cairo cannot draw in nonpremul mode, select ARGB32 for R8G8B8A8N as the closest; later eliminate R8G8B8A8N everywhere
793     cairo_surface_t* cst = cairo_image_surface_create_for_data
794         (dpx,
795          ((pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P || pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N) ? CAIRO_FORMAT_ARGB32 : (pb->mode == NR_PIXBLOCK_MODE_R8G8B8? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_A8)),
796          width,
797          height,
798          pb->rs);
799     cairo_t *ct = cairo_create (cst);
801     return ct;
804 // cairo outline rendering:
805 static unsigned int
806 cairo_arena_shape_render_outline(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
808     NRArenaShape *shape = NR_ARENA_SHAPE(item);
810     cairo_t *ct = nr_create_cairo_context (area, pb);
812     if (!ct) 
813         return item->state;
815     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
816     cairo_set_source_rgba(ct, SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
818     cairo_set_line_width(ct, 0.5);
819     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
820     cairo_new_path(ct);
822     feed_curve_to_cairo (ct, shape->curve, NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
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;
833     return item->state;
836 // cairo stroke rendering (flat color only so far!):
837 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
838 // (need to switch them to premul before this can be enabled)
839 void
840 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
842     NRArenaShape *shape = NR_ARENA_SHAPE(item);
843     SPStyle const *style = shape->style;
845     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
847     if (fabs(shape->_stroke.width * scale) < 0.01)
848         return;
850     cairo_t *ct = nr_create_cairo_context (area, pb);
852     if (!ct) 
853         return;
855     guint32 rgba;
856     if ( item->render_opacity ) {
857         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
858                                           shape->_stroke.opacity *
859                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
860     } else {
861         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
862                                           shape->_stroke.opacity);
863     }
865     // for some reason cairo needs bgra, not rgba
866     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));
868     float style_width = MAX(0.125, shape->_stroke.width * scale);
869     cairo_set_line_width(ct, style_width);
871     switch (shape->_stroke.cap) {
872         case NRArenaShape::BUTT_CAP:
873             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
874             break;
875         case NRArenaShape::ROUND_CAP:
876             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
877             break;
878         case NRArenaShape::SQUARE_CAP:
879             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
880             break;
881     }
882     switch (shape->_stroke.join) {
883         case NRArenaShape::MITRE_JOIN:
884             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
885             break;
886         case NRArenaShape::ROUND_JOIN:
887             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
888             break;
889         case NRArenaShape::BEVEL_JOIN:
890             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
891             break;
892     }
894     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
896     if (style->stroke_dash.n_dash) {
897         NRVpathDash dash;
898         dash.offset = style->stroke_dash.offset * scale;
899         dash.n_dash = style->stroke_dash.n_dash;
900         dash.dash = g_new(double, dash.n_dash);
901         for (int i = 0; i < dash.n_dash; i++) {
902             dash.dash[i] = style->stroke_dash.dash[i] * scale;
903         }
904         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
905         g_free(dash.dash);
906     }
908     cairo_set_tolerance(ct, 0.1);
909     cairo_new_path(ct);
911     feed_curve_to_cairo (ct, shape->curve, NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
913     cairo_stroke(ct);
915     cairo_surface_t *cst = cairo_get_target(ct);
916     cairo_destroy (ct);
917     cairo_surface_finish (cst);
918     cairo_surface_destroy (cst);
920     pb->empty = FALSE;
924 /**
925  * Renders the item.  Markers are just composed into the parent buffer.
926  */
927 static unsigned int
928 nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
930     NRArenaShape *shape = NR_ARENA_SHAPE(item);
932     if (!shape->curve) return item->state;
933     if (!shape->style) return item->state;
935     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
937     if (outline) { 
938         return cairo_arena_shape_render_outline (item, area, pb);
939     }
941     if ( shape->delayed_shp ) {
942         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
943             NRGC   tempGC(NULL);
944             tempGC.transform=shape->ctm;
945             shape->delayed_shp = false;
946             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
947             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
948 /*      NRRect bbox;
949         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
950         nr_arena_shape_add_bboxes(shape,bbox);
951         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
952         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
953         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
954         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
955         shape->approx_bbox=item->bbox;*/
956         } else {
957             return item->state;
958         }
959     }
961     SPStyle const *style = shape->style;
962     if (shape->fill_shp) {
963         NRPixBlock m;
964         guint32 rgba;
966         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
968         // if memory allocation failed, abort render
969         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
970             nr_pixblock_release (&m);
971             return (item->state);
972         }
974         m.visible_area = pb->visible_area; 
975         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
976         m.empty = FALSE;
978         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
979             // do not render fill in any way
980         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
981             if ( item->render_opacity ) {
982                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
983                                                   shape->_fill.opacity *
984                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
985             } else {
986                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
987                                                   shape->_fill.opacity);
988             }
989             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
990             pb->empty = FALSE;
991         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
992             if (shape->fill_painter) {
993                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
994             }
995         }
997         nr_pixblock_release(&m);
998     }
1000     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
1002         // cairo_arena_shape_render_stroke(item, area, pb);
1004         guint32 rgba;
1005         NRPixBlock m;
1007         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1009         // if memory allocation failed, abort render
1010         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1011             nr_pixblock_release (&m);
1012             return (item->state);
1013         }
1015         m.visible_area = pb->visible_area; 
1016         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
1017         m.empty = FALSE;
1019             if ( item->render_opacity ) {
1020                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
1021                                                   shape->_stroke.opacity *
1022                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
1023             } else {
1024                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
1025                                                   shape->_stroke.opacity);
1026             }
1027             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
1028             pb->empty = FALSE;
1030         nr_pixblock_release(&m);
1032     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
1034         NRPixBlock m;
1036         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1038         // if memory allocation failed, abort render
1039         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1040             nr_pixblock_release (&m);
1041             return (item->state);
1042         }
1044         m.visible_area = pb->visible_area; 
1045         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
1046         m.empty = FALSE;
1048         if (shape->stroke_painter) {
1049             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
1050         }
1052         nr_pixblock_release(&m);
1053     }
1055     /* Just compose children into parent buffer */
1056     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1057         unsigned int ret;
1058         ret = nr_arena_item_invoke_render(child, area, pb, flags);
1059         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
1060     }
1062     return item->state;
1066 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
1067 // reenable this when the bug is fixed and remove the rest of this function
1068 static guint
1069 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1071     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1072     if (!shape->curve) return item->state;
1074         cairo_t *ct = nr_create_cairo_context (area, pb);
1076         if (!ct) 
1077             return item->state;
1079         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1081         cairo_new_path(ct);
1083         feed_curve_to_cairo (ct, shape->curve, NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
1085         cairo_fill(ct);
1087         cairo_surface_t *cst = cairo_get_target(ct);
1088         cairo_destroy (ct);
1089         cairo_surface_finish (cst);
1090         cairo_surface_destroy (cst);
1092         pb->empty = FALSE;
1094         return item->state;
1098 static guint
1099 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1101     //return cairo_arena_shape_clip(item, area, pb);
1103     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1104     if (!shape->curve) return item->state;
1106     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1107         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1108             NRGC   tempGC(NULL);
1109             tempGC.transform=shape->ctm;
1110             shape->delayed_shp = false;
1111             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1112         } else {
1113             return item->state;
1114         }
1115     }
1117     if ( shape->fill_shp ) {
1118         NRPixBlock m;
1120         /* fixme: We can OR in one step (Lauris) */
1121         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1123         // if memory allocation failed, abort 
1124         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1125             nr_pixblock_release (&m);
1126             return (item->state);
1127         }
1129         m.visible_area = pb->visible_area; 
1130         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1132         for (int y = area->y0; y < area->y1; y++) {
1133             unsigned char *s, *d;
1134             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1135             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1136             for (int x = area->x0; x < area->x1; x++) {
1137                 *d = NR_COMPOSEA_111(*s, *d);
1138                 d ++;
1139                 s ++;
1140             }
1141         }
1142         nr_pixblock_release(&m);
1143         pb->empty = FALSE;
1144     }
1146     return item->state;
1149 static NRArenaItem *
1150 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1152     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1154     if (!shape->curve) return NULL;
1155     if (!shape->style) return NULL;
1156     if ( shape->delayed_shp ) {
1157         NRRectL  area, updateArea;
1158         area.x0=(int)floor(p[NR::X]);
1159         area.x1=(int)ceil(p[NR::X]);
1160         area.y0=(int)floor(p[NR::Y]);
1161         area.y1=(int)ceil(p[NR::Y]);
1162         int idelta = (int)ceil(delta) + 1;
1163         // njh: inset rect
1164         area.x0-=idelta;
1165         area.x1+=idelta;
1166         area.y0-=idelta;
1167         area.y1+=idelta;
1168         if ( nr_rect_l_test_intersect(&area, &item->bbox) ) {
1169             NRGC   tempGC(NULL);
1170             tempGC.transform=shape->ctm;
1171             updateArea = item->bbox;
1172             if (shape->cached_stroke)
1173                 nr_rect_l_intersect (&updateArea, &updateArea, &shape->cached_sarea);
1175             shape->delayed_shp = false;
1176             nr_arena_shape_update_stroke(shape, &tempGC, &updateArea);
1177             nr_arena_shape_update_fill(shape, &tempGC, &updateArea);
1178             /*      NRRect bbox;
1179                     bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
1180                     nr_arena_shape_add_bboxes(shape,bbox);
1181                     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
1182                     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
1183                     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
1184                     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
1185                     shape->approx_bbox=item->bbox;*/
1186         }
1187     }
1189     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1191     if (item->state & NR_ARENA_ITEM_STATE_RENDER) {
1192         if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1193             if (shape->fill_shp->PtWinding(p) > 0 ) return item;
1194         }
1195         if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1196             if (shape->stroke_shp->PtWinding(p) > 0 ) return item;
1197         }
1198         if (delta > 1e-3) {
1199             if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1200                 if (distanceLessThanOrEqual(shape->fill_shp, p, delta)) return item;
1201             }
1202             if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1203                 if (distanceLessThanOrEqual(shape->stroke_shp, p, delta)) return item;
1204             }
1205         }
1206     } else {
1207         NRBPath bp;
1208         bp.path = SP_CURVE_BPATH(shape->curve);
1209         double dist = NR_HUGE;
1210         int wind = 0;
1211         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, &wind, &dist, NR_EPSILON);
1212         if (shape->_fill.paint.type() != NRArenaShape::Paint::NONE) {
1213             if (!shape->style->fill_rule.computed) {
1214                 if (wind != 0) return item;
1215             } else {
1216                 if (wind & 0x1) return item;
1217             }
1218         }
1219         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1220             /* fixme: We do not take stroke width into account here (Lauris) */
1221             if (dist < delta) return item;
1222         }
1223     }
1225     return NULL;
1228 /**
1229  *
1230  *  Requests a render of the shape, then if the shape is already a curve it
1231  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1232  *  curve and adds it to the shape.  Finally, it requests an update of the
1233  *  arena for the shape.
1234  */
1235 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1237     g_return_if_fail(shape != NULL);
1238     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1240     if ( justTrans == false ) {
1241         // dirty cached versions
1242         if ( shape->cached_fill ) {
1243             delete shape->cached_fill;
1244             shape->cached_fill=NULL;
1245         }
1246         if ( shape->cached_stroke ) {
1247             delete shape->cached_stroke;
1248             shape->cached_stroke=NULL;
1249         }
1250     }
1252     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1254     if (shape->curve) {
1255         sp_curve_unref(shape->curve);
1256         shape->curve = NULL;
1257     }
1259     if (curve) {
1260         shape->curve = curve;
1261         sp_curve_ref(curve);
1262     }
1264     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1267 void NRArenaShape::setFill(SPPaintServer *server) {
1268     _fill.paint.set(server);
1269     _invalidateCachedFill();
1272 void NRArenaShape::setFill(SPColor const &color) {
1273     _fill.paint.set(color);
1274     _invalidateCachedFill();
1277 void NRArenaShape::setFillOpacity(double opacity) {
1278     _fill.opacity = opacity;
1279     _invalidateCachedFill();
1282 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1283     _fill.rule = rule;
1284     _invalidateCachedFill();
1287 void NRArenaShape::setStroke(SPPaintServer *server) {
1288     _stroke.paint.set(server);
1289     _invalidateCachedStroke();
1292 void NRArenaShape::setStroke(SPColor const &color) {
1293     _stroke.paint.set(color);
1294     _invalidateCachedStroke();
1297 void NRArenaShape::setStrokeOpacity(double opacity) {
1298     _stroke.opacity = opacity;
1299     _invalidateCachedStroke();
1302 void NRArenaShape::setStrokeWidth(double width) {
1303     _stroke.width = width;
1304     _invalidateCachedStroke();
1307 void NRArenaShape::setMitreLimit(double limit) {
1308     _stroke.mitre_limit = limit;
1309     _invalidateCachedStroke();
1312 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1313     _stroke.cap = cap;
1314     _invalidateCachedStroke();
1317 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1318     _stroke.join = join;
1319     _invalidateCachedStroke();
1322 /** nr_arena_shape_set_style
1323  *
1324  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1325  */
1326 void
1327 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1329     g_return_if_fail(shape != NULL);
1330     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1332     if (style) sp_style_ref(style);
1333     if (shape->style) sp_style_unref(shape->style);
1334     shape->style = style;
1336     switch (style->fill.type) {
1337         case SP_PAINT_TYPE_NONE: {
1338             shape->setFill(NULL);
1339             break;
1340         }
1341         case SP_PAINT_TYPE_COLOR: {
1342             shape->setFill(style->fill.value.color);
1343             break;
1344         }
1345         case SP_PAINT_TYPE_PAINTSERVER: {
1346             shape->setFill(style->fill.value.paint.server);
1347             break;
1348         }
1349         default: {
1350             g_assert_not_reached();
1351         }
1352     }
1353     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1354     switch (style->fill_rule.computed) {
1355         case SP_WIND_RULE_EVENODD: {
1356             shape->setFillRule(NRArenaShape::EVEN_ODD);
1357             break;
1358         }
1359         case SP_WIND_RULE_NONZERO: {
1360             shape->setFillRule(NRArenaShape::NONZERO);
1361             break;
1362         }
1363         default: {
1364             g_assert_not_reached();
1365         }
1366     }
1368     switch (style->stroke.type) {
1369         case SP_PAINT_TYPE_NONE: {
1370             shape->setStroke(NULL);
1371             break;
1372         }
1373         case SP_PAINT_TYPE_COLOR: {
1374             shape->setStroke(style->stroke.value.color);
1375             break;
1376         }
1377         case SP_PAINT_TYPE_PAINTSERVER: {
1378             shape->setStroke(style->stroke.value.paint.server);
1379             break;
1380         }
1381         default: {
1382             g_assert_not_reached();
1383         }
1384     }
1385     shape->setStrokeWidth(style->stroke_width.computed);
1386     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1387     switch (style->stroke_linecap.computed) {
1388         case SP_STROKE_LINECAP_ROUND: {
1389             shape->setLineCap(NRArenaShape::ROUND_CAP);
1390             break;
1391         }
1392         case SP_STROKE_LINECAP_SQUARE: {
1393             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1394             break;
1395         }
1396         case SP_STROKE_LINECAP_BUTT: {
1397             shape->setLineCap(NRArenaShape::BUTT_CAP);
1398             break;
1399         }
1400         default: {
1401             g_assert_not_reached();
1402         }
1403     }
1404     switch (style->stroke_linejoin.computed) {
1405         case SP_STROKE_LINEJOIN_ROUND: {
1406             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1407             break;
1408         }
1409         case SP_STROKE_LINEJOIN_BEVEL: {
1410             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1411             break;
1412         }
1413         case SP_STROKE_LINEJOIN_MITER: {
1414             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1415             break;
1416         }
1417         default: {
1418             g_assert_not_reached();
1419         }
1420     }
1421     shape->setMitreLimit(style->stroke_miterlimit.value);
1423     //if shape has a filter
1424     if (style->filter.set && style->filter.filter) 
1425     {
1426         shape->filter = new NR::Filter();
1427         shape->filter->set_x(style->filter.filter->x);
1428         shape->filter->set_y(style->filter.filter->y);
1429         shape->filter->set_width(style->filter.filter->width);
1430         shape->filter->set_height(style->filter.filter->height);
1431         
1432         //go through all SP filter primitives
1433         for(int i=0; i<style->filter.filter->_primitive_count; i++)
1434         {
1435             SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1436             //if primitive is gaussianblur
1437             if(SP_IS_GAUSSIANBLUR(primitive)) {
1438                 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1439                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1440                 float num = spblur->stdDeviation.getNumber();
1441                 if( num>=0.0 )
1442                 {
1443                     float optnum = spblur->stdDeviation.getOptNumber();
1444                     if( optnum>=0.0 )
1445                         gaussian->set_deviation((double) num, (double) optnum);
1446                     else
1447                         gaussian->set_deviation((double) num);
1448                 }
1449             }
1450         }
1451     }
1452     else
1453     {
1454         //no filter set for this shape
1455         shape->filter = NULL;
1456     }
1458     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1461 void
1462 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1464     g_return_if_fail(shape != NULL);
1465     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1466     g_return_if_fail(pbox != NULL);
1468     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1469         shape->paintbox = *pbox;
1470     } else {
1471         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1472         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1473         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1474     }
1476     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1479 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1481     paintbox.x0 = pbox.min()[NR::X];
1482     paintbox.y0 = pbox.min()[NR::Y];
1483     paintbox.x1 = pbox.max()[NR::X];
1484     paintbox.y1 = pbox.max()[NR::Y];
1486     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1489 static void
1490 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1492     if ( st >= en ) return;
1493     if ( vst < 0 ) vst=0;
1494     if ( vst > 1 ) vst=1;
1495     if ( ven < 0 ) ven=0;
1496     if ( ven > 1 ) ven=1;
1497     float   sv=vst;
1498     float   dv=ven-vst;
1499     int     len=en-st;
1500     unsigned char*   d=(unsigned char*)dest.buffer;
1501     d+=(st-dest.startPix);
1502     if ( fabs(dv) < 0.001 ) {
1503         if ( vst > 0.999 ) {
1504             /* Simple copy */
1505             while (len > 0) {
1506                 d[0] = 255;
1507                 d += 1;
1508                 len -= 1;
1509             }
1510         } else {
1511             sv*=256;
1512             unsigned int c0_24=(int)sv;
1513             c0_24&=0xFF;
1514             while (len > 0) {
1515                 /* Draw */
1516                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1517                 d += 1;
1518                 len -= 1;
1519             }
1520         }
1521     } else {
1522         if ( en <= st+1 ) {
1523             sv=0.5*(vst+ven);
1524             sv*=256;
1525             unsigned int c0_24=(int)sv;
1526             c0_24&=0xFF;
1527             /* Draw */
1528             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1529         } else {
1530             dv/=len;
1531             sv+=0.5*dv; // correction trapezoidale
1532             sv*=16777216;
1533             dv*=16777216;
1534             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1535             int s0_24 = static_cast<int>(dv);
1536             while (len > 0) {
1537                 unsigned int ca;
1538                 /* Draw */
1539                 ca = c0_24 >> 16;
1540                 if ( ca > 255 ) ca=255;
1541                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1542                 d += 1;
1543                 c0_24 += s0_24;
1544                 c0_24 = CLAMP(c0_24, 0, 16777216);
1545                 len -= 1;
1546             }
1547         }
1548     }
1551 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1553     theS->CalcBBox();
1554     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1555     int    il,ir,it,ib;
1556     il=(int)floor(l);
1557     ir=(int)ceil(r);
1558     it=(int)floor(t);
1559     ib=(int)ceil(b);
1561     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1562     if ( il < m.area.x0 ) il=m.area.x0;
1563     if ( it < m.area.y0 ) it=m.area.y0;
1564     if ( ir > m.area.x1 ) ir=m.area.x1;
1565     if ( ib > m.area.y1 ) ib=m.area.y1;
1567     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1568     int    curPt;
1569     float  curY;
1570     theS->BeginQuickRaster(curY, curPt);
1572     FloatLigne *theI = new FloatLigne();
1573     IntLigne *theIL = new IntLigne();
1575     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1577     char *mdata = (char*)m.data.px;
1578     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1579     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1580     for (int y = it; y < ib; y++) {
1581         theI->Reset();
1582         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1583         theI->Flatten();
1584         theIL->Copy(theI);
1586         raster_info  dest;
1587         dest.startPix=il;
1588         dest.endPix=ir;
1589         dest.sth=il;
1590         dest.stv=y;
1591         dest.buffer=ligStart;
1592         theIL->Raster(dest, NULL, shape_run_A8_OR);
1593         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1594     }
1595     theS->EndQuickRaster();
1596     delete theI;
1597     delete theIL;
1601 /*
1602   Local Variables:
1603   mode:c++
1604   c-file-style:"stroustrup"
1605   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1606   indent-tabs-mode:nil
1607   fill-column:99
1608   End:
1609 */
1610 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :