Code

render text in outline mode via cairo (still no endian-safe); factor out helper functions
[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"
35 #include "inkscape-cairo.h"
37 #include <cairo.h>
39 //int  showRuns=0;
40 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
42 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
43 static void nr_arena_shape_init(NRArenaShape *shape);
44 static void nr_arena_shape_finalize(NRObject *object);
46 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
47 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
48 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
49 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
51 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
52 static unsigned int nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
53 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
54 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
56 static NRArenaItemClass *shape_parent_class;
58 NRType
59 nr_arena_shape_get_type(void)
60 {
61     static NRType type = 0;
62     if (!type) {
63         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
64                                        "NRArenaShape",
65                                        sizeof(NRArenaShapeClass),
66                                        sizeof(NRArenaShape),
67                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
68                                        (void (*)(NRObject *)) nr_arena_shape_init);
69     }
70     return type;
71 }
73 static void
74 nr_arena_shape_class_init(NRArenaShapeClass *klass)
75 {
76     NRObjectClass *object_class;
77     NRArenaItemClass *item_class;
79     object_class = (NRObjectClass *) klass;
80     item_class = (NRArenaItemClass *) klass;
82     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
84     object_class->finalize = nr_arena_shape_finalize;
85     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
87     item_class->children = nr_arena_shape_children;
88     item_class->add_child = nr_arena_shape_add_child;
89     item_class->set_child_position = nr_arena_shape_set_child_position;
90     item_class->remove_child = nr_arena_shape_remove_child;
91     item_class->update = nr_arena_shape_update;
92     item_class->render = nr_arena_shape_render;
93     item_class->clip = nr_arena_shape_clip;
94     item_class->pick = nr_arena_shape_pick;
95 }
97 /**
98  * Initializes the arena shape, setting all parameters to null, 0, false,
99  * or other defaults
100  */
101 static void
102 nr_arena_shape_init(NRArenaShape *shape)
104     shape->curve = NULL;
105     shape->style = NULL;
106     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
107     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
109     nr_matrix_set_identity(&shape->ctm);
110     shape->fill_painter = NULL;
111     shape->stroke_painter = NULL;
112     shape->cached_fill = NULL;
113     shape->cached_stroke = NULL;
114     shape->cached_fpartialy = false;
115     shape->cached_spartialy = false;
116     shape->fill_shp = NULL;
117     shape->stroke_shp = NULL;
119     shape->delayed_shp = false;
121     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
122     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
123     nr_matrix_set_identity(&shape->cached_fctm);
124     nr_matrix_set_identity(&shape->cached_sctm);
126     shape->markers = NULL;
129 static void
130 nr_arena_shape_finalize(NRObject *object)
132     NRArenaShape *shape = (NRArenaShape *) object;
134     if (shape->fill_shp) delete shape->fill_shp;
135     if (shape->stroke_shp) delete shape->stroke_shp;
136     if (shape->cached_fill) delete shape->cached_fill;
137     if (shape->cached_stroke) delete shape->cached_stroke;
138     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
139     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
141     if (shape->style) sp_style_unref(shape->style);
142     if (shape->curve) sp_curve_unref(shape->curve);
144     ((NRObjectClass *) shape_parent_class)->finalize(object);
147 /**
148  * Retrieves the markers from the item
149  */
150 static NRArenaItem *
151 nr_arena_shape_children(NRArenaItem *item)
153     NRArenaShape *shape = (NRArenaShape *) item;
155     return shape->markers;
158 /**
159  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
160  * the prev and next items.  If ref is NULL, then it sets the item's markers
161  * as the next items.
162  */
163 static void
164 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
166     NRArenaShape *shape = (NRArenaShape *) item;
168     if (!ref) {
169         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
170     } else {
171         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
172     }
174     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
177 /**
178  * Removes child from the shape.  If there are no prev items in 
179  * the child, it sets items' markers to the next item in the child.
180  */
181 static void
182 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
184     NRArenaShape *shape = (NRArenaShape *) item;
186     if (child->prev) {
187         nr_arena_item_detach(item, child);
188     } else {
189         shape->markers = nr_arena_item_detach(item, child);
190     }
192     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
195 /**
196  * Detaches child from item, and if there are no previous items in child, it 
197  * sets item's markers to the child.  It then attaches the child back onto the item.
198  * If ref is null, it sets the markers to be the next item, otherwise it uses
199  * the next/prev items in ref.
200  */
201 static void
202 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
204     NRArenaShape *shape = (NRArenaShape *) item;
206     if (child->prev) {
207         nr_arena_item_detach(item, child);
208     } else {
209         shape->markers = nr_arena_item_detach(item, child);
210     }
212     if (!ref) {
213         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
214     } else {
215         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
216     }
218     nr_arena_item_request_render(child);
221 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
222 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
223 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
225 /**
226  * Updates the arena shape 'item' and all of its children, including the markers.
227  */
228 static guint
229 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
231     NRRect bbox;
233     NRArenaShape *shape = NR_ARENA_SHAPE(item);
235     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
237     unsigned int newstate;
238     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
239         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
240         beststate = beststate & newstate;
241     }
243     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
244         /* We do not have to create rendering structures */
245         shape->ctm = gc->transform;
246         if (state & NR_ARENA_ITEM_STATE_BBOX) {
247             if (shape->curve) {
248                 NRBPath bp;
249                 /* fixme: */
250                 bbox.x0 = bbox.y0 = NR_HUGE;
251                 bbox.x1 = bbox.y1 = -NR_HUGE;
252                 bp.path = SP_CURVE_BPATH(shape->curve);
253                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
254                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
255                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
256                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
257                 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
258             }
259             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
260                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
261                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
262                 }
263             }
264         }
265         return (state | item->state);
266     }
268     shape->delayed_shp=true;
269     shape->ctm = gc->transform;
270     bbox.x0 = bbox.y0 = NR_HUGE;
271     bbox.x1 = bbox.y1 = -NR_HUGE;
273     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
275     if (shape->curve) {
276         NRBPath bp;
277         /* fixme: */
278         bbox.x0 = bbox.y0 = NR_HUGE;
279         bbox.x1 = bbox.y1 = -NR_HUGE;
280         bp.path = SP_CURVE_BPATH(shape->curve);
281         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
282         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
283             float width, scale;
284             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
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                 bbox.x0-=width;
288                 bbox.x1+=width;
289                 bbox.y0-=width;
290                 bbox.y1+=width;
291             }
292             // those pesky miters, now
293             float miterMax=width*shape->_stroke.mitre_limit;
294             if ( miterMax > 0.01 ) {
295                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
296                 bbox.x0-=miterMax;
297                 bbox.x1+=miterMax;
298                 bbox.y0-=miterMax;
299                 bbox.y1+=miterMax;
300             }
301         }
302     } else {
303     }
304     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
305     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
306     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
307     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
308     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
310     /* Release state data */
311     if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
312         /* Concept test */
313         if (shape->fill_shp) {
314             delete shape->fill_shp;
315             shape->fill_shp = NULL;
316         }
317     }
318     if (shape->stroke_shp) {
319         delete shape->stroke_shp;
320         shape->stroke_shp = NULL;
321     }
322     if (shape->fill_painter) {
323         sp_painter_free(shape->fill_painter);
324         shape->fill_painter = NULL;
325     }
326     if (shape->stroke_painter) {
327         sp_painter_free(shape->stroke_painter);
328         shape->stroke_painter = NULL;
329     }
331     if (!shape->curve || 
332         !shape->style ||
333         sp_curve_is_empty(shape->curve) ||
334         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
335          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
336     {
337         item->bbox = shape->approx_bbox;
338         return NR_ARENA_ITEM_STATE_ALL;
339     }
341     /* Build state data */
342     if ( shape->delayed_shp ) {
343         item->bbox=shape->approx_bbox;
344     } else {
345         nr_arena_shape_update_stroke(shape, gc, area);
346         nr_arena_shape_update_fill(shape, gc, area);
348         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
349         nr_arena_shape_add_bboxes(shape,bbox);
351         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
352         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
353         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
354         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
355     }
357     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
359     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
360     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
361     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
362     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
363     nr_arena_request_render_rect(item->arena, &item->bbox);
365     item->render_opacity = TRUE;
366     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
367         if (gc && gc->parent) {
368             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
369                                                               NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
370                                                               &shape->paintbox);
371         }
372         item->render_opacity = FALSE;
373     }
374     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
375         if (gc && gc->parent) {
376             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
377                                                                 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
378                                                                 &shape->paintbox);
379         }
380         item->render_opacity = FALSE;
381     }
382     if ( item->render_opacity == TRUE
383          && shape->_fill.paint.type()   != NRArenaShape::Paint::NONE
384          && shape->_stroke.paint.type() != NRArenaShape::Paint::NONE )
385     {
386         // don't merge item opacity with paint opacity if there is a stroke on the fill
387         item->render_opacity = FALSE;
388     }
390     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
391         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
392             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
393         }
394     }
396     return NR_ARENA_ITEM_STATE_ALL;
399 int matrix_is_isometry(NR::Matrix p) {
400     NR::Matrix   tp;
401     // transposition
402     tp[0]=p[0];
403     tp[1]=p[2];
404     tp[2]=p[1];
405     tp[3]=p[3];
406     for (int i = 4; i < 6; i++) // shut valgrind up :)
407         tp[i] = p[i] = 0;
408     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
409     // Is the adjunct nearly an identity function?
410     if (isom.is_translation(0.01)) {
411         // the transformation is an isometry -> no need to recompute
412         // the uncrossed polygon
413         if ( p.det() < 0 )
414             return -1;
415         else
416             return 1;
417     }
418     return 0;
421 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
422     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
425 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
426 void
427 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
429     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
430         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
431         if (TRUE || !shape->fill_shp) {
432             NR::Matrix  cached_to_new = NR::identity();
433             int isometry = 0;
434             if ( shape->cached_fill ) {
435                 if (shape->cached_fctm == gc->transform) {
436                     isometry = 2; // identity
437                 } else {
438                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
439                     isometry = matrix_is_isometry(cached_to_new);
440                 }
441                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
442                     isometry = 0;
443             }
444             if ( isometry == 0 ) {
445                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
446                 shape->cached_fill->Reset();
448                 Path*  thePath=new Path;
449                 Shape* theShape=new Shape;
450                 {
451                     NR::Matrix   tempMat(gc->transform);
452                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
453                 }
455                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
456                     thePath->Convert(1.0);
457                     shape->cached_fpartialy = false;
458                 } else {
459                     thePath->Convert(area, 1.0);
460                     shape->cached_fpartialy = true;
461                 }
463                 thePath->Fill(theShape, 0);
465                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
466                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
467                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
468                     //shape->cached_fill->Copy(theShape);
469                 } else {
470                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
471                 }
472                 shape->cached_fctm=gc->transform;
473                 shape->cached_farea = *area;
474                 delete theShape;
475                 delete thePath;
476                 if ( shape->fill_shp == NULL )
477                     shape->fill_shp = new Shape;
479                 shape->fill_shp->Copy(shape->cached_fill);
481             } else if ( 2 == isometry ) {
482                 if ( shape->fill_shp == NULL ) {
483                     shape->fill_shp = new Shape;
484                     shape->fill_shp->Copy(shape->cached_fill);
485                 }
486             } else {
488                 if ( shape->fill_shp == NULL )
489                     shape->fill_shp = new Shape;
491                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
492                                        shape->cached_fill->numberOfEdges());
493                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
494                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
495                 if ( isometry == 1 ) {
496                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
497                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
498                                                  shape->cached_fill->getEdge(i).en);
499                 } else if ( isometry == -1 ) { // need to flip poly.
500                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
501                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
502                                                  shape->cached_fill->getEdge(i).st);
503                 }
504                 shape->fill_shp->ForceToPolygon();
505                 shape->fill_shp->needPointsSorting();
506                 shape->fill_shp->needEdgesSorting();
507             }
508             shape->delayed_shp |= shape->cached_fpartialy;
509         }
510     }
513 void
514 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
516     SPStyle* style = shape->style;
518     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
520     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
522     if (outline) {
523         // cairo does not need the livarot path for rendering... but unfortunately it's still used for picking
524         // FIXME: switch picking to using cairo_in_stroke? 
525         //return; 
526     }
528     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
529     // (since it will only be used for picking, not for rendering)
531     if (outline ||
532         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
533          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
535         float style_width = MAX(0.125, shape->_stroke.width * scale);
536         float width;
537         if (outline) {
538             width = 0.5; // 1 pixel wide, independent of zoom
539         } else {
540             width = style_width;
541         }
543         NR::Matrix  cached_to_new = NR::identity();
545         int isometry = 0;
546         if ( shape->cached_stroke ) {
547             if (shape->cached_sctm == gc->transform) {
548                 isometry = 2; // identity
549             } else {
550                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
551                 isometry = matrix_is_isometry(cached_to_new);
552             }
553             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
554                 isometry = 0;
555             if (0 != isometry && width != shape->cached_width) { 
556                 // if this happens without setting style, we have just switched to outline or back
557                 isometry = 0; 
558             } 
559         }
561         if ( isometry == 0 ) {
562             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
563             shape->cached_stroke->Reset();
564             Path*  thePath = new Path;
565             Shape* theShape = new Shape;
566             {
567                 NR::Matrix   tempMat(gc->transform);
568                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
569             }
571             // add some padding to the rendering area, so clipped path does not go into a render area
572             NRRectL padded_area = *area;
573             padded_area.x0 -= (NR::ICoord)width;
574             padded_area.x1 += (NR::ICoord)width;
575             padded_area.y0 -= (NR::ICoord)width;
576             padded_area.y1 += (NR::ICoord)width;
577             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
578                 thePath->Convert((outline) ? 4.0 : 1.0);
579                 shape->cached_spartialy = false;
580             }
581             else {
582                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
583                 shape->cached_spartialy = true;
584             }
586             if (style->stroke_dash.n_dash && !outline) {
587                 thePath->DashPolylineFromStyle(style, scale, 1.0);
588             }
590             ButtType butt=butt_straight;
591             switch (shape->_stroke.cap) {
592                 case NRArenaShape::BUTT_CAP:
593                     butt = butt_straight;
594                     break;
595                 case NRArenaShape::ROUND_CAP:
596                     butt = butt_round;
597                     break;
598                 case NRArenaShape::SQUARE_CAP:
599                     butt = butt_square;
600                     break;
601             }
602             JoinType join=join_straight;
603             switch (shape->_stroke.join) {
604                 case NRArenaShape::MITRE_JOIN:
605                     join = join_pointy;
606                     break;
607                 case NRArenaShape::ROUND_JOIN:
608                     join = join_round;
609                     break;
610                 case NRArenaShape::BEVEL_JOIN:
611                     join = join_straight;
612                     break;
613             }
615             if (outline) {
616                 butt = butt_straight;
617                 join = join_straight;
618             }
620             thePath->Stroke(theShape, false, 0.5*width, join, butt,
621                             0.5*width*shape->_stroke.mitre_limit);
624             if (outline) {
625                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
626                 shape->cached_stroke->Copy(theShape);
627             } else {
628                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
629             }
631             shape->cached_width = width;
633             shape->cached_sctm=gc->transform;
634             shape->cached_sarea = *area;
635             delete thePath;
636             delete theShape;
637             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
639             shape->stroke_shp->Copy(shape->cached_stroke);
641         } else if ( 2 == isometry ) {
642             if ( shape->stroke_shp == NULL ) {
643                 shape->stroke_shp=new Shape;
644                 shape->stroke_shp->Copy(shape->cached_stroke);
645             }
646         } else {
647             if ( shape->stroke_shp == NULL )
648                 shape->stroke_shp=new Shape;
649             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
650             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
651                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
652             if ( isometry == 1 ) {
653                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
654                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
655                                                shape->cached_stroke->getEdge(i).en);
656             } else if ( isometry == -1 ) {
657                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
658                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
659                                                shape->cached_stroke->getEdge(i).st);
660             }
661             shape->stroke_shp->ForceToPolygon();
662             shape->stroke_shp->needPointsSorting();
663             shape->stroke_shp->needEdgesSorting();
664         }
665         shape->delayed_shp |= shape->cached_spartialy;
666     }
670 void
671 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
673     if ( shape->stroke_shp ) {
674         shape->stroke_shp->CalcBBox();
675         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
676         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
677         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
678         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
679         if ( bbox.x0 >= bbox.x1 ) {
680             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
681                 bbox.x0=shape->stroke_shp->leftX;
682                 bbox.x1=shape->stroke_shp->rightX;
683             }
684         } else {
685             if ( shape->stroke_shp->leftX < bbox.x0 )
686                 bbox.x0=shape->stroke_shp->leftX;
687             if ( shape->stroke_shp->rightX > bbox.x1 )
688                 bbox.x1=shape->stroke_shp->rightX;
689         }
690         if ( bbox.y0 >= bbox.y1 ) {
691             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
692                 bbox.y0=shape->stroke_shp->topY;
693                 bbox.y1=shape->stroke_shp->bottomY;
694             }
695         } else {
696             if ( shape->stroke_shp->topY < bbox.y0 )
697                 bbox.y0=shape->stroke_shp->topY;
698             if ( shape->stroke_shp->bottomY > bbox.y1 )
699                 bbox.y1=shape->stroke_shp->bottomY;
700         }
701     }
702     if ( shape->fill_shp ) {
703         shape->fill_shp->CalcBBox();
704         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
705         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
706         shape->fill_shp->topY=floor(shape->fill_shp->topY);
707         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
708         if ( bbox.x0 >= bbox.x1 ) {
709             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
710                 bbox.x0=shape->fill_shp->leftX;
711                 bbox.x1=shape->fill_shp->rightX;
712             }
713         } else {
714             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
715             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
716         }
717         if ( bbox.y0 >= bbox.y1 ) {
718             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
719                 bbox.y0=shape->fill_shp->topY;
720                 bbox.y1=shape->fill_shp->bottomY;
721             }
722         } else {
723             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
724             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
725         }
726     }
729 // cairo outline rendering:
730 static unsigned int
731 cairo_arena_shape_render_outline(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
733     NRArenaShape *shape = NR_ARENA_SHAPE(item);
735     cairo_t *ct = nr_create_cairo_context (area, pb);
737     if (!ct) 
738         return item->state;
740     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
741     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));
743     cairo_set_line_width(ct, 0.5);
744     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
745     cairo_new_path(ct);
747     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
749     cairo_stroke(ct);
751     cairo_surface_t *cst = cairo_get_target(ct);
752     cairo_destroy (ct);
753     cairo_surface_finish (cst);
754     cairo_surface_destroy (cst);
756     pb->empty = FALSE;
758     return item->state;
761 // cairo stroke rendering (flat color only so far!):
762 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
763 // (need to switch them to premul before this can be enabled)
764 void
765 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
767     NRArenaShape *shape = NR_ARENA_SHAPE(item);
768     SPStyle const *style = shape->style;
770     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
772     if (fabs(shape->_stroke.width * scale) < 0.01)
773         return;
775     cairo_t *ct = nr_create_cairo_context (area, pb);
777     if (!ct) 
778         return;
780     guint32 rgba;
781     if ( item->render_opacity ) {
782         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
783                                           shape->_stroke.opacity *
784                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
785     } else {
786         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
787                                           shape->_stroke.opacity);
788     }
790     // for some reason cairo needs bgra, not rgba
791     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));
793     float style_width = MAX(0.125, shape->_stroke.width * scale);
794     cairo_set_line_width(ct, style_width);
796     switch (shape->_stroke.cap) {
797         case NRArenaShape::BUTT_CAP:
798             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
799             break;
800         case NRArenaShape::ROUND_CAP:
801             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
802             break;
803         case NRArenaShape::SQUARE_CAP:
804             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
805             break;
806     }
807     switch (shape->_stroke.join) {
808         case NRArenaShape::MITRE_JOIN:
809             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
810             break;
811         case NRArenaShape::ROUND_JOIN:
812             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
813             break;
814         case NRArenaShape::BEVEL_JOIN:
815             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
816             break;
817     }
819     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
821     if (style->stroke_dash.n_dash) {
822         NRVpathDash dash;
823         dash.offset = style->stroke_dash.offset * scale;
824         dash.n_dash = style->stroke_dash.n_dash;
825         dash.dash = g_new(double, dash.n_dash);
826         for (int i = 0; i < dash.n_dash; i++) {
827             dash.dash[i] = style->stroke_dash.dash[i] * scale;
828         }
829         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
830         g_free(dash.dash);
831     }
833     cairo_set_tolerance(ct, 0.1);
834     cairo_new_path(ct);
836     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
838     cairo_stroke(ct);
840     cairo_surface_t *cst = cairo_get_target(ct);
841     cairo_destroy (ct);
842     cairo_surface_finish (cst);
843     cairo_surface_destroy (cst);
845     pb->empty = FALSE;
849 /**
850  * Renders the item.  Markers are just composed into the parent buffer.
851  */
852 static unsigned int
853 nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
855     NRArenaShape *shape = NR_ARENA_SHAPE(item);
857     if (!shape->curve) return item->state;
858     if (!shape->style) return item->state;
860     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
862     if (outline) { 
863         return cairo_arena_shape_render_outline (item, area, pb);
864     }
866     if ( shape->delayed_shp ) {
867         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
868             NRGC   tempGC(NULL);
869             tempGC.transform=shape->ctm;
870             shape->delayed_shp = false;
871             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
872             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
873 /*      NRRect bbox;
874         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
875         nr_arena_shape_add_bboxes(shape,bbox);
876         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
877         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
878         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
879         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
880         shape->approx_bbox=item->bbox;*/
881         } else {
882             return item->state;
883         }
884     }
886     SPStyle const *style = shape->style;
887     if (shape->fill_shp) {
888         NRPixBlock m;
889         guint32 rgba;
891         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
893         // if memory allocation failed, abort render
894         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
895             nr_pixblock_release (&m);
896             return (item->state);
897         }
899         m.visible_area = pb->visible_area; 
900         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
901         m.empty = FALSE;
903         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
904             // do not render fill in any way
905         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
906             if ( item->render_opacity ) {
907                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
908                                                   shape->_fill.opacity *
909                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
910             } else {
911                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
912                                                   shape->_fill.opacity);
913             }
914             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
915             pb->empty = FALSE;
916         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
917             if (shape->fill_painter) {
918                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
919             }
920         }
922         nr_pixblock_release(&m);
923     }
925     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
927         // cairo_arena_shape_render_stroke(item, area, pb);
929         guint32 rgba;
930         NRPixBlock m;
932         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
934         // if memory allocation failed, abort render
935         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
936             nr_pixblock_release (&m);
937             return (item->state);
938         }
940         m.visible_area = pb->visible_area; 
941         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
942         m.empty = FALSE;
944             if ( item->render_opacity ) {
945                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
946                                                   shape->_stroke.opacity *
947                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
948             } else {
949                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
950                                                   shape->_stroke.opacity);
951             }
952             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
953             pb->empty = FALSE;
955         nr_pixblock_release(&m);
957     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
959         NRPixBlock m;
961         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
963         // if memory allocation failed, abort render
964         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
965             nr_pixblock_release (&m);
966             return (item->state);
967         }
969         m.visible_area = pb->visible_area; 
970         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
971         m.empty = FALSE;
973         if (shape->stroke_painter) {
974             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
975         }
977         nr_pixblock_release(&m);
978     }
980     /* Just compose children into parent buffer */
981     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
982         unsigned int ret;
983         ret = nr_arena_item_invoke_render(child, area, pb, flags);
984         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
985     }
987     return item->state;
991 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
992 // reenable this when the bug is fixed and remove the rest of this function
993 static guint
994 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
996     NRArenaShape *shape = NR_ARENA_SHAPE(item);
997     if (!shape->curve) return item->state;
999         cairo_t *ct = nr_create_cairo_context (area, pb);
1001         if (!ct) 
1002             return item->state;
1004         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1006         cairo_new_path(ct);
1008         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
1010         cairo_fill(ct);
1012         cairo_surface_t *cst = cairo_get_target(ct);
1013         cairo_destroy (ct);
1014         cairo_surface_finish (cst);
1015         cairo_surface_destroy (cst);
1017         pb->empty = FALSE;
1019         return item->state;
1023 static guint
1024 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1026     //return cairo_arena_shape_clip(item, area, pb);
1028     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1029     if (!shape->curve) return item->state;
1031     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1032         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1033             NRGC   tempGC(NULL);
1034             tempGC.transform=shape->ctm;
1035             shape->delayed_shp = false;
1036             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1037         } else {
1038             return item->state;
1039         }
1040     }
1042     if ( shape->fill_shp ) {
1043         NRPixBlock m;
1045         /* fixme: We can OR in one step (Lauris) */
1046         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1048         // if memory allocation failed, abort 
1049         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1050             nr_pixblock_release (&m);
1051             return (item->state);
1052         }
1054         m.visible_area = pb->visible_area; 
1055         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1057         for (int y = area->y0; y < area->y1; y++) {
1058             unsigned char *s, *d;
1059             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1060             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1061             for (int x = area->x0; x < area->x1; x++) {
1062                 *d = NR_COMPOSEA_111(*s, *d);
1063                 d ++;
1064                 s ++;
1065             }
1066         }
1067         nr_pixblock_release(&m);
1068         pb->empty = FALSE;
1069     }
1071     return item->state;
1074 static NRArenaItem *
1075 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1077     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1079     if (!shape->curve) return NULL;
1080     if (!shape->style) return NULL;
1081     if ( shape->delayed_shp ) {
1082         NRRectL  area, updateArea;
1083         area.x0=(int)floor(p[NR::X]);
1084         area.x1=(int)ceil(p[NR::X]);
1085         area.y0=(int)floor(p[NR::Y]);
1086         area.y1=(int)ceil(p[NR::Y]);
1087         int idelta = (int)ceil(delta) + 1;
1088         // njh: inset rect
1089         area.x0-=idelta;
1090         area.x1+=idelta;
1091         area.y0-=idelta;
1092         area.y1+=idelta;
1093         if ( nr_rect_l_test_intersect(&area, &item->bbox) ) {
1094             NRGC   tempGC(NULL);
1095             tempGC.transform=shape->ctm;
1096             updateArea = item->bbox;
1097             if (shape->cached_stroke)
1098                 nr_rect_l_intersect (&updateArea, &updateArea, &shape->cached_sarea);
1100             shape->delayed_shp = false;
1101             nr_arena_shape_update_stroke(shape, &tempGC, &updateArea);
1102             nr_arena_shape_update_fill(shape, &tempGC, &updateArea);
1103             /*      NRRect bbox;
1104                     bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
1105                     nr_arena_shape_add_bboxes(shape,bbox);
1106                     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
1107                     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
1108                     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
1109                     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
1110                     shape->approx_bbox=item->bbox;*/
1111         }
1112     }
1114     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1116     if (item->state & NR_ARENA_ITEM_STATE_RENDER) {
1117         if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1118             if (shape->fill_shp->PtWinding(p) > 0 ) return item;
1119         }
1120         if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1121             if (shape->stroke_shp->PtWinding(p) > 0 ) return item;
1122         }
1123         if (delta > 1e-3) {
1124             if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1125                 if (distanceLessThanOrEqual(shape->fill_shp, p, delta)) return item;
1126             }
1127             if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1128                 if (distanceLessThanOrEqual(shape->stroke_shp, p, delta)) return item;
1129             }
1130         }
1131     } else {
1132         NRBPath bp;
1133         bp.path = SP_CURVE_BPATH(shape->curve);
1134         double dist = NR_HUGE;
1135         int wind = 0;
1136         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, &wind, &dist, NR_EPSILON);
1137         if (shape->_fill.paint.type() != NRArenaShape::Paint::NONE) {
1138             if (!shape->style->fill_rule.computed) {
1139                 if (wind != 0) return item;
1140             } else {
1141                 if (wind & 0x1) return item;
1142             }
1143         }
1144         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1145             /* fixme: We do not take stroke width into account here (Lauris) */
1146             if (dist < delta) return item;
1147         }
1148     }
1150     return NULL;
1153 /**
1154  *
1155  *  Requests a render of the shape, then if the shape is already a curve it
1156  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1157  *  curve and adds it to the shape.  Finally, it requests an update of the
1158  *  arena for the shape.
1159  */
1160 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1162     g_return_if_fail(shape != NULL);
1163     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1165     if ( justTrans == false ) {
1166         // dirty cached versions
1167         if ( shape->cached_fill ) {
1168             delete shape->cached_fill;
1169             shape->cached_fill=NULL;
1170         }
1171         if ( shape->cached_stroke ) {
1172             delete shape->cached_stroke;
1173             shape->cached_stroke=NULL;
1174         }
1175     }
1177     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1179     if (shape->curve) {
1180         sp_curve_unref(shape->curve);
1181         shape->curve = NULL;
1182     }
1184     if (curve) {
1185         shape->curve = curve;
1186         sp_curve_ref(curve);
1187     }
1189     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1192 void NRArenaShape::setFill(SPPaintServer *server) {
1193     _fill.paint.set(server);
1194     _invalidateCachedFill();
1197 void NRArenaShape::setFill(SPColor const &color) {
1198     _fill.paint.set(color);
1199     _invalidateCachedFill();
1202 void NRArenaShape::setFillOpacity(double opacity) {
1203     _fill.opacity = opacity;
1204     _invalidateCachedFill();
1207 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1208     _fill.rule = rule;
1209     _invalidateCachedFill();
1212 void NRArenaShape::setStroke(SPPaintServer *server) {
1213     _stroke.paint.set(server);
1214     _invalidateCachedStroke();
1217 void NRArenaShape::setStroke(SPColor const &color) {
1218     _stroke.paint.set(color);
1219     _invalidateCachedStroke();
1222 void NRArenaShape::setStrokeOpacity(double opacity) {
1223     _stroke.opacity = opacity;
1224     _invalidateCachedStroke();
1227 void NRArenaShape::setStrokeWidth(double width) {
1228     _stroke.width = width;
1229     _invalidateCachedStroke();
1232 void NRArenaShape::setMitreLimit(double limit) {
1233     _stroke.mitre_limit = limit;
1234     _invalidateCachedStroke();
1237 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1238     _stroke.cap = cap;
1239     _invalidateCachedStroke();
1242 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1243     _stroke.join = join;
1244     _invalidateCachedStroke();
1247 /** nr_arena_shape_set_style
1248  *
1249  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1250  */
1251 void
1252 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1254     g_return_if_fail(shape != NULL);
1255     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1257     if (style) sp_style_ref(style);
1258     if (shape->style) sp_style_unref(shape->style);
1259     shape->style = style;
1261     switch (style->fill.type) {
1262         case SP_PAINT_TYPE_NONE: {
1263             shape->setFill(NULL);
1264             break;
1265         }
1266         case SP_PAINT_TYPE_COLOR: {
1267             shape->setFill(style->fill.value.color);
1268             break;
1269         }
1270         case SP_PAINT_TYPE_PAINTSERVER: {
1271             shape->setFill(style->fill.value.paint.server);
1272             break;
1273         }
1274         default: {
1275             g_assert_not_reached();
1276         }
1277     }
1278     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1279     switch (style->fill_rule.computed) {
1280         case SP_WIND_RULE_EVENODD: {
1281             shape->setFillRule(NRArenaShape::EVEN_ODD);
1282             break;
1283         }
1284         case SP_WIND_RULE_NONZERO: {
1285             shape->setFillRule(NRArenaShape::NONZERO);
1286             break;
1287         }
1288         default: {
1289             g_assert_not_reached();
1290         }
1291     }
1293     switch (style->stroke.type) {
1294         case SP_PAINT_TYPE_NONE: {
1295             shape->setStroke(NULL);
1296             break;
1297         }
1298         case SP_PAINT_TYPE_COLOR: {
1299             shape->setStroke(style->stroke.value.color);
1300             break;
1301         }
1302         case SP_PAINT_TYPE_PAINTSERVER: {
1303             shape->setStroke(style->stroke.value.paint.server);
1304             break;
1305         }
1306         default: {
1307             g_assert_not_reached();
1308         }
1309     }
1310     shape->setStrokeWidth(style->stroke_width.computed);
1311     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1312     switch (style->stroke_linecap.computed) {
1313         case SP_STROKE_LINECAP_ROUND: {
1314             shape->setLineCap(NRArenaShape::ROUND_CAP);
1315             break;
1316         }
1317         case SP_STROKE_LINECAP_SQUARE: {
1318             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1319             break;
1320         }
1321         case SP_STROKE_LINECAP_BUTT: {
1322             shape->setLineCap(NRArenaShape::BUTT_CAP);
1323             break;
1324         }
1325         default: {
1326             g_assert_not_reached();
1327         }
1328     }
1329     switch (style->stroke_linejoin.computed) {
1330         case SP_STROKE_LINEJOIN_ROUND: {
1331             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1332             break;
1333         }
1334         case SP_STROKE_LINEJOIN_BEVEL: {
1335             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1336             break;
1337         }
1338         case SP_STROKE_LINEJOIN_MITER: {
1339             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1340             break;
1341         }
1342         default: {
1343             g_assert_not_reached();
1344         }
1345     }
1346     shape->setMitreLimit(style->stroke_miterlimit.value);
1348     //if shape has a filter
1349     if (style->filter.set && style->filter.filter) 
1350     {
1351         shape->filter = new NR::Filter();
1352         shape->filter->set_x(style->filter.filter->x);
1353         shape->filter->set_y(style->filter.filter->y);
1354         shape->filter->set_width(style->filter.filter->width);
1355         shape->filter->set_height(style->filter.filter->height);
1356         
1357         //go through all SP filter primitives
1358         for(int i=0; i<style->filter.filter->_primitive_count; i++)
1359         {
1360             SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1361             //if primitive is gaussianblur
1362             if(SP_IS_GAUSSIANBLUR(primitive)) {
1363                 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1364                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1365                 float num = spblur->stdDeviation.getNumber();
1366                 if( num>=0.0 )
1367                 {
1368                     float optnum = spblur->stdDeviation.getOptNumber();
1369                     if( optnum>=0.0 )
1370                         gaussian->set_deviation((double) num, (double) optnum);
1371                     else
1372                         gaussian->set_deviation((double) num);
1373                 }
1374             }
1375         }
1376     }
1377     else
1378     {
1379         //no filter set for this shape
1380         shape->filter = NULL;
1381     }
1383     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1386 void
1387 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1389     g_return_if_fail(shape != NULL);
1390     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1391     g_return_if_fail(pbox != NULL);
1393     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1394         shape->paintbox = *pbox;
1395     } else {
1396         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1397         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1398         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1399     }
1401     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1404 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1406     paintbox.x0 = pbox.min()[NR::X];
1407     paintbox.y0 = pbox.min()[NR::Y];
1408     paintbox.x1 = pbox.max()[NR::X];
1409     paintbox.y1 = pbox.max()[NR::Y];
1411     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1414 static void
1415 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1417     if ( st >= en ) return;
1418     if ( vst < 0 ) vst=0;
1419     if ( vst > 1 ) vst=1;
1420     if ( ven < 0 ) ven=0;
1421     if ( ven > 1 ) ven=1;
1422     float   sv=vst;
1423     float   dv=ven-vst;
1424     int     len=en-st;
1425     unsigned char*   d=(unsigned char*)dest.buffer;
1426     d+=(st-dest.startPix);
1427     if ( fabs(dv) < 0.001 ) {
1428         if ( vst > 0.999 ) {
1429             /* Simple copy */
1430             while (len > 0) {
1431                 d[0] = 255;
1432                 d += 1;
1433                 len -= 1;
1434             }
1435         } else {
1436             sv*=256;
1437             unsigned int c0_24=(int)sv;
1438             c0_24&=0xFF;
1439             while (len > 0) {
1440                 /* Draw */
1441                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1442                 d += 1;
1443                 len -= 1;
1444             }
1445         }
1446     } else {
1447         if ( en <= st+1 ) {
1448             sv=0.5*(vst+ven);
1449             sv*=256;
1450             unsigned int c0_24=(int)sv;
1451             c0_24&=0xFF;
1452             /* Draw */
1453             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1454         } else {
1455             dv/=len;
1456             sv+=0.5*dv; // correction trapezoidale
1457             sv*=16777216;
1458             dv*=16777216;
1459             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1460             int s0_24 = static_cast<int>(dv);
1461             while (len > 0) {
1462                 unsigned int ca;
1463                 /* Draw */
1464                 ca = c0_24 >> 16;
1465                 if ( ca > 255 ) ca=255;
1466                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1467                 d += 1;
1468                 c0_24 += s0_24;
1469                 c0_24 = CLAMP(c0_24, 0, 16777216);
1470                 len -= 1;
1471             }
1472         }
1473     }
1476 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1478     theS->CalcBBox();
1479     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1480     int    il,ir,it,ib;
1481     il=(int)floor(l);
1482     ir=(int)ceil(r);
1483     it=(int)floor(t);
1484     ib=(int)ceil(b);
1486     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1487     if ( il < m.area.x0 ) il=m.area.x0;
1488     if ( it < m.area.y0 ) it=m.area.y0;
1489     if ( ir > m.area.x1 ) ir=m.area.x1;
1490     if ( ib > m.area.y1 ) ib=m.area.y1;
1492     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1493     int    curPt;
1494     float  curY;
1495     theS->BeginQuickRaster(curY, curPt);
1497     FloatLigne *theI = new FloatLigne();
1498     IntLigne *theIL = new IntLigne();
1500     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1502     char *mdata = (char*)m.data.px;
1503     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1504     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1505     for (int y = it; y < ib; y++) {
1506         theI->Reset();
1507         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1508         theI->Flatten();
1509         theIL->Copy(theI);
1511         raster_info  dest;
1512         dest.startPix=il;
1513         dest.endPix=ir;
1514         dest.sth=il;
1515         dest.stv=y;
1516         dest.buffer=ligStart;
1517         theIL->Raster(dest, NULL, shape_run_A8_OR);
1518         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1519     }
1520     theS->EndQuickRaster();
1521     delete theI;
1522     delete theIL;
1526 /*
1527   Local Variables:
1528   mode:c++
1529   c-file-style:"stroustrup"
1530   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1531   indent-tabs-mode:nil
1532   fill-column:99
1533   End:
1534 */
1535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :