Code

fix bug where master opacity did not affect markers
[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(cairo_t *ct, 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 (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
383            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
384           || (shape->markers)
385         )
386     {
387         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
388         item->render_opacity = FALSE;
389     }
391     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
392         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
393             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
394         }
395     }
397     return NR_ARENA_ITEM_STATE_ALL;
400 int matrix_is_isometry(NR::Matrix p) {
401     NR::Matrix   tp;
402     // transposition
403     tp[0]=p[0];
404     tp[1]=p[2];
405     tp[2]=p[1];
406     tp[3]=p[3];
407     for (int i = 4; i < 6; i++) // shut valgrind up :)
408         tp[i] = p[i] = 0;
409     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
410     // Is the adjunct nearly an identity function?
411     if (isom.is_translation(0.01)) {
412         // the transformation is an isometry -> no need to recompute
413         // the uncrossed polygon
414         if ( p.det() < 0 )
415             return -1;
416         else
417             return 1;
418     }
419     return 0;
422 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
423     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
426 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
427 void
428 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
430     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
431         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
432         if (TRUE || !shape->fill_shp) {
433             NR::Matrix  cached_to_new = NR::identity();
434             int isometry = 0;
435             if ( shape->cached_fill ) {
436                 if (shape->cached_fctm == gc->transform) {
437                     isometry = 2; // identity
438                 } else {
439                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
440                     isometry = matrix_is_isometry(cached_to_new);
441                 }
442                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
443                     isometry = 0;
444             }
445             if ( isometry == 0 ) {
446                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
447                 shape->cached_fill->Reset();
449                 Path*  thePath=new Path;
450                 Shape* theShape=new Shape;
451                 {
452                     NR::Matrix   tempMat(gc->transform);
453                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
454                 }
456                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
457                     thePath->Convert(1.0);
458                     shape->cached_fpartialy = false;
459                 } else {
460                     thePath->Convert(area, 1.0);
461                     shape->cached_fpartialy = true;
462                 }
464                 thePath->Fill(theShape, 0);
466                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
467                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
468                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
469                     //shape->cached_fill->Copy(theShape);
470                 } else {
471                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
472                 }
473                 shape->cached_fctm=gc->transform;
474                 shape->cached_farea = *area;
475                 delete theShape;
476                 delete thePath;
477                 if ( shape->fill_shp == NULL )
478                     shape->fill_shp = new Shape;
480                 shape->fill_shp->Copy(shape->cached_fill);
482             } else if ( 2 == isometry ) {
483                 if ( shape->fill_shp == NULL ) {
484                     shape->fill_shp = new Shape;
485                     shape->fill_shp->Copy(shape->cached_fill);
486                 }
487             } else {
489                 if ( shape->fill_shp == NULL )
490                     shape->fill_shp = new Shape;
492                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
493                                        shape->cached_fill->numberOfEdges());
494                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
495                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
496                 if ( isometry == 1 ) {
497                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
498                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
499                                                  shape->cached_fill->getEdge(i).en);
500                 } else if ( isometry == -1 ) { // need to flip poly.
501                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
502                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
503                                                  shape->cached_fill->getEdge(i).st);
504                 }
505                 shape->fill_shp->ForceToPolygon();
506                 shape->fill_shp->needPointsSorting();
507                 shape->fill_shp->needEdgesSorting();
508             }
509             shape->delayed_shp |= shape->cached_fpartialy;
510         }
511     }
514 void
515 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
517     SPStyle* style = shape->style;
519     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
521     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
523     if (outline) {
524         // cairo does not need the livarot path for rendering
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(cairo_t *ct, NRArenaItem *item, NR::Point shift)
733     NRArenaShape *shape = NR_ARENA_SHAPE(item);
735     if (!ct) 
736         return item->state;
738     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
739     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
740     // by setting color channels in the "wrong" order
741     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));
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), shift);
749     cairo_stroke(ct);
751     return item->state;
754 // cairo stroke rendering (flat color only so far!):
755 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
756 // (need to switch them to premul before this can be enabled)
757 void
758 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
760     NRArenaShape *shape = NR_ARENA_SHAPE(item);
761     SPStyle const *style = shape->style;
763     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
765     if (fabs(shape->_stroke.width * scale) < 0.01)
766         return;
768     cairo_t *ct = nr_create_cairo_context (area, pb);
770     if (!ct) 
771         return;
773     guint32 rgba;
774     if ( item->render_opacity ) {
775         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
776                                           shape->_stroke.opacity *
777                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
778     } else {
779         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
780                                           shape->_stroke.opacity);
781     }
783     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
784     // by setting color channels in the "wrong" order
785     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));
787     float style_width = MAX(0.125, shape->_stroke.width * scale);
788     cairo_set_line_width(ct, style_width);
790     switch (shape->_stroke.cap) {
791         case NRArenaShape::BUTT_CAP:
792             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
793             break;
794         case NRArenaShape::ROUND_CAP:
795             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
796             break;
797         case NRArenaShape::SQUARE_CAP:
798             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
799             break;
800     }
801     switch (shape->_stroke.join) {
802         case NRArenaShape::MITRE_JOIN:
803             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
804             break;
805         case NRArenaShape::ROUND_JOIN:
806             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
807             break;
808         case NRArenaShape::BEVEL_JOIN:
809             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
810             break;
811     }
813     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
815     if (style->stroke_dash.n_dash) {
816         NRVpathDash dash;
817         dash.offset = style->stroke_dash.offset * scale;
818         dash.n_dash = style->stroke_dash.n_dash;
819         dash.dash = g_new(double, dash.n_dash);
820         for (int i = 0; i < dash.n_dash; i++) {
821             dash.dash[i] = style->stroke_dash.dash[i] * scale;
822         }
823         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
824         g_free(dash.dash);
825     }
827     cairo_set_tolerance(ct, 0.1);
828     cairo_new_path(ct);
830     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
832     cairo_stroke(ct);
834     cairo_surface_t *cst = cairo_get_target(ct);
835     cairo_destroy (ct);
836     cairo_surface_finish (cst);
837     cairo_surface_destroy (cst);
839     pb->empty = FALSE;
843 /**
844  * Renders the item.  Markers are just composed into the parent buffer.
845  */
846 static unsigned int
847 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
849     NRArenaShape *shape = NR_ARENA_SHAPE(item);
851     if (!shape->curve) return item->state;
852     if (!shape->style) return item->state;
854     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
856     if (outline) { // cairo outline rendering
858         pb->empty = FALSE;
859         unsigned int ret = cairo_arena_shape_render_outline (ct, item, NR::Point(pb->area.x0, pb->area.y0));
860         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
862     } else {
864     if ( shape->delayed_shp ) {
865         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
866             NRGC   tempGC(NULL);
867             tempGC.transform=shape->ctm;
868             shape->delayed_shp = false;
869             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
870             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
871 /*      NRRect bbox;
872         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
873         nr_arena_shape_add_bboxes(shape,bbox);
874         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
875         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
876         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
877         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
878         shape->approx_bbox=item->bbox;*/
879         } else {
880             return item->state;
881         }
882     }
884     SPStyle const *style = shape->style;
885     if (shape->fill_shp) {
886         NRPixBlock m;
887         guint32 rgba;
889         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
891         // if memory allocation failed, abort render
892         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
893             nr_pixblock_release (&m);
894             return (item->state);
895         }
897         m.visible_area = pb->visible_area; 
898         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
899         m.empty = FALSE;
901         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
902             // do not render fill in any way
903         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
904             if ( item->render_opacity ) {
905                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
906                                                   shape->_fill.opacity *
907                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
908             } else {
909                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
910                                                   shape->_fill.opacity);
911             }
912             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
913             pb->empty = FALSE;
914         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
915             if (shape->fill_painter) {
916                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
917             }
918         }
920         nr_pixblock_release(&m);
921     }
923     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
925         // cairo_arena_shape_render_stroke(item, area, pb);
927         guint32 rgba;
928         NRPixBlock m;
930         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
932         // if memory allocation failed, abort render
933         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
934             nr_pixblock_release (&m);
935             return (item->state);
936         }
938         m.visible_area = pb->visible_area; 
939         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
940         m.empty = FALSE;
942             if ( item->render_opacity ) {
943                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
944                                                   shape->_stroke.opacity *
945                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
946             } else {
947                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
948                                                   shape->_stroke.opacity);
949             }
950             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
951             pb->empty = FALSE;
953         nr_pixblock_release(&m);
955     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
957         NRPixBlock m;
959         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
961         // if memory allocation failed, abort render
962         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
963             nr_pixblock_release (&m);
964             return (item->state);
965         }
967         m.visible_area = pb->visible_area; 
968         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
969         m.empty = FALSE;
971         if (shape->stroke_painter) {
972             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
973         }
975         nr_pixblock_release(&m);
976     }
978     } // non-cairo non-outline branch
980     /* Render markers into parent buffer */
981     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
982         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
983         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
984     }
986     return item->state;
990 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
991 // reenable this when the bug is fixed and remove the rest of this function
992 static guint
993 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
995     NRArenaShape *shape = NR_ARENA_SHAPE(item);
996     if (!shape->curve) return item->state;
998         cairo_t *ct = nr_create_cairo_context (area, pb);
1000         if (!ct) 
1001             return item->state;
1003         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1005         cairo_new_path(ct);
1007         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
1009         cairo_fill(ct);
1011         cairo_surface_t *cst = cairo_get_target(ct);
1012         cairo_destroy (ct);
1013         cairo_surface_finish (cst);
1014         cairo_surface_destroy (cst);
1016         pb->empty = FALSE;
1018         return item->state;
1022 static guint
1023 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1025     //return cairo_arena_shape_clip(item, area, pb);
1027     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1028     if (!shape->curve) return item->state;
1030     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1031         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1032             NRGC   tempGC(NULL);
1033             tempGC.transform=shape->ctm;
1034             shape->delayed_shp = false;
1035             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1036         } else {
1037             return item->state;
1038         }
1039     }
1041     if ( shape->fill_shp ) {
1042         NRPixBlock m;
1044         /* fixme: We can OR in one step (Lauris) */
1045         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1047         // if memory allocation failed, abort 
1048         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1049             nr_pixblock_release (&m);
1050             return (item->state);
1051         }
1053         m.visible_area = pb->visible_area; 
1054         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1056         for (int y = area->y0; y < area->y1; y++) {
1057             unsigned char *s, *d;
1058             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1059             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1060             for (int x = area->x0; x < area->x1; x++) {
1061                 *d = NR_COMPOSEA_111(*s, *d);
1062                 d ++;
1063                 s ++;
1064             }
1065         }
1066         nr_pixblock_release(&m);
1067         pb->empty = FALSE;
1068     }
1070     return item->state;
1073 static NRArenaItem *
1074 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1076     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1077     if (!shape->curve) return NULL;
1078     if (!shape->style) return NULL;
1080     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1082     float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1083     double width;
1084     if (outline) {
1085         width = 0.5;
1086     } else {
1087         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1088     }
1090     NRBPath bp;
1091     bp.path = SP_CURVE_BPATH(shape->curve);
1092     double dist = NR_HUGE;
1093     int wind = 0;
1094     nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, &wind, &dist, NR_EPSILON);
1096     // pick fill
1097     if (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && !outline) {
1098         if (!shape->style->fill_rule.computed) {
1099             if (wind != 0) return item;
1100         } else {
1101             if (wind & 0x1) return item;
1102         }
1103     }
1105     // pick stroke
1106     if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1107         // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1108         if ((dist - width) < delta) return item;
1109     }
1111     // if not picked on the shape itself, try its markers
1112     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1113         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1114         if (ret)
1115             return ret;
1116     }
1118     return NULL;
1121 /**
1122  *
1123  *  Requests a render of the shape, then if the shape is already a curve it
1124  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1125  *  curve and adds it to the shape.  Finally, it requests an update of the
1126  *  arena for the shape.
1127  */
1128 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1130     g_return_if_fail(shape != NULL);
1131     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1133     if ( justTrans == false ) {
1134         // dirty cached versions
1135         if ( shape->cached_fill ) {
1136             delete shape->cached_fill;
1137             shape->cached_fill=NULL;
1138         }
1139         if ( shape->cached_stroke ) {
1140             delete shape->cached_stroke;
1141             shape->cached_stroke=NULL;
1142         }
1143     }
1145     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1147     if (shape->curve) {
1148         sp_curve_unref(shape->curve);
1149         shape->curve = NULL;
1150     }
1152     if (curve) {
1153         shape->curve = curve;
1154         sp_curve_ref(curve);
1155     }
1157     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1160 void NRArenaShape::setFill(SPPaintServer *server) {
1161     _fill.paint.set(server);
1162     _invalidateCachedFill();
1165 void NRArenaShape::setFill(SPColor const &color) {
1166     _fill.paint.set(color);
1167     _invalidateCachedFill();
1170 void NRArenaShape::setFillOpacity(double opacity) {
1171     _fill.opacity = opacity;
1172     _invalidateCachedFill();
1175 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1176     _fill.rule = rule;
1177     _invalidateCachedFill();
1180 void NRArenaShape::setStroke(SPPaintServer *server) {
1181     _stroke.paint.set(server);
1182     _invalidateCachedStroke();
1185 void NRArenaShape::setStroke(SPColor const &color) {
1186     _stroke.paint.set(color);
1187     _invalidateCachedStroke();
1190 void NRArenaShape::setStrokeOpacity(double opacity) {
1191     _stroke.opacity = opacity;
1192     _invalidateCachedStroke();
1195 void NRArenaShape::setStrokeWidth(double width) {
1196     _stroke.width = width;
1197     _invalidateCachedStroke();
1200 void NRArenaShape::setMitreLimit(double limit) {
1201     _stroke.mitre_limit = limit;
1202     _invalidateCachedStroke();
1205 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1206     _stroke.cap = cap;
1207     _invalidateCachedStroke();
1210 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1211     _stroke.join = join;
1212     _invalidateCachedStroke();
1215 /** nr_arena_shape_set_style
1216  *
1217  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1218  */
1219 void
1220 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1222     g_return_if_fail(shape != NULL);
1223     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1225     if (style) sp_style_ref(style);
1226     if (shape->style) sp_style_unref(shape->style);
1227     shape->style = style;
1229     switch (style->fill.type) {
1230         case SP_PAINT_TYPE_NONE: {
1231             shape->setFill(NULL);
1232             break;
1233         }
1234         case SP_PAINT_TYPE_COLOR: {
1235             shape->setFill(style->fill.value.color);
1236             break;
1237         }
1238         case SP_PAINT_TYPE_PAINTSERVER: {
1239             shape->setFill(style->fill.value.paint.server);
1240             break;
1241         }
1242         default: {
1243             g_assert_not_reached();
1244         }
1245     }
1246     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1247     switch (style->fill_rule.computed) {
1248         case SP_WIND_RULE_EVENODD: {
1249             shape->setFillRule(NRArenaShape::EVEN_ODD);
1250             break;
1251         }
1252         case SP_WIND_RULE_NONZERO: {
1253             shape->setFillRule(NRArenaShape::NONZERO);
1254             break;
1255         }
1256         default: {
1257             g_assert_not_reached();
1258         }
1259     }
1261     switch (style->stroke.type) {
1262         case SP_PAINT_TYPE_NONE: {
1263             shape->setStroke(NULL);
1264             break;
1265         }
1266         case SP_PAINT_TYPE_COLOR: {
1267             shape->setStroke(style->stroke.value.color);
1268             break;
1269         }
1270         case SP_PAINT_TYPE_PAINTSERVER: {
1271             shape->setStroke(style->stroke.value.paint.server);
1272             break;
1273         }
1274         default: {
1275             g_assert_not_reached();
1276         }
1277     }
1278     shape->setStrokeWidth(style->stroke_width.computed);
1279     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1280     switch (style->stroke_linecap.computed) {
1281         case SP_STROKE_LINECAP_ROUND: {
1282             shape->setLineCap(NRArenaShape::ROUND_CAP);
1283             break;
1284         }
1285         case SP_STROKE_LINECAP_SQUARE: {
1286             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1287             break;
1288         }
1289         case SP_STROKE_LINECAP_BUTT: {
1290             shape->setLineCap(NRArenaShape::BUTT_CAP);
1291             break;
1292         }
1293         default: {
1294             g_assert_not_reached();
1295         }
1296     }
1297     switch (style->stroke_linejoin.computed) {
1298         case SP_STROKE_LINEJOIN_ROUND: {
1299             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1300             break;
1301         }
1302         case SP_STROKE_LINEJOIN_BEVEL: {
1303             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1304             break;
1305         }
1306         case SP_STROKE_LINEJOIN_MITER: {
1307             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1308             break;
1309         }
1310         default: {
1311             g_assert_not_reached();
1312         }
1313     }
1314     shape->setMitreLimit(style->stroke_miterlimit.value);
1316     //if shape has a filter
1317     if (style->filter.set && style->filter.filter) 
1318     {
1319         shape->filter = new NR::Filter();
1320         shape->filter->set_x(style->filter.filter->x);
1321         shape->filter->set_y(style->filter.filter->y);
1322         shape->filter->set_width(style->filter.filter->width);
1323         shape->filter->set_height(style->filter.filter->height);
1324         
1325         //go through all SP filter primitives
1326         for(int i=0; i<style->filter.filter->_primitive_count; i++)
1327         {
1328             SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1329             //if primitive is gaussianblur
1330             if(SP_IS_GAUSSIANBLUR(primitive)) {
1331                 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1332                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1333                 float num = spblur->stdDeviation.getNumber();
1334                 if( num>=0.0 )
1335                 {
1336                     float optnum = spblur->stdDeviation.getOptNumber();
1337                     if( optnum>=0.0 )
1338                         gaussian->set_deviation((double) num, (double) optnum);
1339                     else
1340                         gaussian->set_deviation((double) num);
1341                 }
1342             }
1343         }
1344     }
1345     else
1346     {
1347         //no filter set for this shape
1348         shape->filter = NULL;
1349     }
1351     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1354 void
1355 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1357     g_return_if_fail(shape != NULL);
1358     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1359     g_return_if_fail(pbox != NULL);
1361     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1362         shape->paintbox = *pbox;
1363     } else {
1364         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1365         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1366         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1367     }
1369     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1372 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1374     paintbox.x0 = pbox.min()[NR::X];
1375     paintbox.y0 = pbox.min()[NR::Y];
1376     paintbox.x1 = pbox.max()[NR::X];
1377     paintbox.y1 = pbox.max()[NR::Y];
1379     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1382 static void
1383 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1385     if ( st >= en ) return;
1386     if ( vst < 0 ) vst=0;
1387     if ( vst > 1 ) vst=1;
1388     if ( ven < 0 ) ven=0;
1389     if ( ven > 1 ) ven=1;
1390     float   sv=vst;
1391     float   dv=ven-vst;
1392     int     len=en-st;
1393     unsigned char*   d=(unsigned char*)dest.buffer;
1394     d+=(st-dest.startPix);
1395     if ( fabs(dv) < 0.001 ) {
1396         if ( vst > 0.999 ) {
1397             /* Simple copy */
1398             while (len > 0) {
1399                 d[0] = 255;
1400                 d += 1;
1401                 len -= 1;
1402             }
1403         } else {
1404             sv*=256;
1405             unsigned int c0_24=(int)sv;
1406             c0_24&=0xFF;
1407             while (len > 0) {
1408                 /* Draw */
1409                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1410                 d += 1;
1411                 len -= 1;
1412             }
1413         }
1414     } else {
1415         if ( en <= st+1 ) {
1416             sv=0.5*(vst+ven);
1417             sv*=256;
1418             unsigned int c0_24=(int)sv;
1419             c0_24&=0xFF;
1420             /* Draw */
1421             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1422         } else {
1423             dv/=len;
1424             sv+=0.5*dv; // correction trapezoidale
1425             sv*=16777216;
1426             dv*=16777216;
1427             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1428             int s0_24 = static_cast<int>(dv);
1429             while (len > 0) {
1430                 unsigned int ca;
1431                 /* Draw */
1432                 ca = c0_24 >> 16;
1433                 if ( ca > 255 ) ca=255;
1434                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1435                 d += 1;
1436                 c0_24 += s0_24;
1437                 c0_24 = CLAMP(c0_24, 0, 16777216);
1438                 len -= 1;
1439             }
1440         }
1441     }
1444 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1446     theS->CalcBBox();
1447     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1448     int    il,ir,it,ib;
1449     il=(int)floor(l);
1450     ir=(int)ceil(r);
1451     it=(int)floor(t);
1452     ib=(int)ceil(b);
1454     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1455     if ( il < m.area.x0 ) il=m.area.x0;
1456     if ( it < m.area.y0 ) it=m.area.y0;
1457     if ( ir > m.area.x1 ) ir=m.area.x1;
1458     if ( ib > m.area.y1 ) ib=m.area.y1;
1460     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1461     int    curPt;
1462     float  curY;
1463     theS->BeginQuickRaster(curY, curPt);
1465     FloatLigne *theI = new FloatLigne();
1466     IntLigne *theIL = new IntLigne();
1468     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1470     char *mdata = (char*)m.data.px;
1471     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1472     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1473     for (int y = it; y < ib; y++) {
1474         theI->Reset();
1475         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1476         theI->Flatten();
1477         theIL->Copy(theI);
1479         raster_info  dest;
1480         dest.startPix=il;
1481         dest.endPix=ir;
1482         dest.sth=il;
1483         dest.stv=y;
1484         dest.buffer=ligStart;
1485         theIL->Raster(dest, NULL, shape_run_A8_OR);
1486         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1487     }
1488     theS->EndQuickRaster();
1489     delete theI;
1490     delete theIL;
1494 /*
1495   Local Variables:
1496   mode:c++
1497   c-file-style:"stroustrup"
1498   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1499   indent-tabs-mode:nil
1500   fill-column:99
1501   End:
1502 */
1503 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :