Code

Purged fill type enum
[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/canvas-arena.h>
18 #include <display/nr-arena.h>
19 #include <display/nr-arena-shape.h>
20 #include <libnr/n-art-bpath.h>
21 #include <libnr/nr-path.h>
22 #include <libnr/nr-pixops.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-fns.h>
25 #include <libnr/nr-blit.h>
26 #include <livarot/Path.h>
27 #include <livarot/float-line.h>
28 #include <livarot/int-line.h>
29 #include <style.h>
30 #include "prefs-utils.h"
31 #include "inkscape-cairo.h"
33 #include "sp-filter.h"
34 #include "sp-filter-reference.h"
35 #include "display/nr-filter.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;
128     shape->last_pick = NULL;
129     shape->repick_after = 0;
132 static void
133 nr_arena_shape_finalize(NRObject *object)
135     NRArenaShape *shape = (NRArenaShape *) object;
137     if (shape->fill_shp) delete shape->fill_shp;
138     if (shape->stroke_shp) delete shape->stroke_shp;
139     if (shape->cached_fill) delete shape->cached_fill;
140     if (shape->cached_stroke) delete shape->cached_stroke;
141     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
142     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
144     if (shape->style) sp_style_unref(shape->style);
145     if (shape->curve) sp_curve_unref(shape->curve);
147     ((NRObjectClass *) shape_parent_class)->finalize(object);
150 /**
151  * Retrieves the markers from the item
152  */
153 static NRArenaItem *
154 nr_arena_shape_children(NRArenaItem *item)
156     NRArenaShape *shape = (NRArenaShape *) item;
158     return shape->markers;
161 /**
162  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
163  * the prev and next items.  If ref is NULL, then it sets the item's markers
164  * as the next items.
165  */
166 static void
167 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
169     NRArenaShape *shape = (NRArenaShape *) item;
171     if (!ref) {
172         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
173     } else {
174         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
175     }
177     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
180 /**
181  * Removes child from the shape.  If there are no prev items in 
182  * the child, it sets items' markers to the next item in the child.
183  */
184 static void
185 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
187     NRArenaShape *shape = (NRArenaShape *) item;
189     if (child->prev) {
190         nr_arena_item_detach(item, child);
191     } else {
192         shape->markers = nr_arena_item_detach(item, child);
193     }
195     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
198 /**
199  * Detaches child from item, and if there are no previous items in child, it 
200  * sets item's markers to the child.  It then attaches the child back onto the item.
201  * If ref is null, it sets the markers to be the next item, otherwise it uses
202  * the next/prev items in ref.
203  */
204 static void
205 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
207     NRArenaShape *shape = (NRArenaShape *) item;
209     if (child->prev) {
210         nr_arena_item_detach(item, child);
211     } else {
212         shape->markers = nr_arena_item_detach(item, child);
213     }
215     if (!ref) {
216         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
217     } else {
218         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
219     }
221     nr_arena_item_request_render(child);
224 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
225 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
226 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
228 /**
229  * Updates the arena shape 'item' and all of its children, including the markers.
230  */
231 static guint
232 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
234     NRRect bbox;
236     NRArenaShape *shape = NR_ARENA_SHAPE(item);
238     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
240     unsigned int newstate;
241     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
242         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
243         beststate = beststate & newstate;
244     }
246     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
247         /* We do not have to create rendering structures */
248         shape->ctm = gc->transform;
249         if (state & NR_ARENA_ITEM_STATE_BBOX) {
250             if (shape->curve) {
251                 NRBPath bp;
252                 /* fixme: */
253                 bbox.x0 = bbox.y0 = NR_HUGE;
254                 bbox.x1 = bbox.y1 = -NR_HUGE;
255                 bp.path = SP_CURVE_BPATH(shape->curve);
256                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
257                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
258                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
259                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
260                 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
261             }
262             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
263                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
264                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
265                 }
266             }
267         }
268         return (state | item->state);
269     }
271     shape->delayed_shp=true;
272     shape->ctm = gc->transform;
273     bbox.x0 = bbox.y0 = NR_HUGE;
274     bbox.x1 = bbox.y1 = -NR_HUGE;
276     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
278     if (shape->curve) {
279         NRBPath bp;
280         /* fixme: */
281         bbox.x0 = bbox.y0 = NR_HUGE;
282         bbox.x1 = bbox.y1 = -NR_HUGE;
283         bp.path = SP_CURVE_BPATH(shape->curve);
284         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
285         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
286             float width, scale;
287             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
288             width = MAX(0.125, shape->_stroke.width * scale);
289             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
290                 bbox.x0-=width;
291                 bbox.x1+=width;
292                 bbox.y0-=width;
293                 bbox.y1+=width;
294             }
295             // those pesky miters, now
296             float miterMax=width*shape->_stroke.mitre_limit;
297             if ( miterMax > 0.01 ) {
298                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
299                 bbox.x0-=miterMax;
300                 bbox.x1+=miterMax;
301                 bbox.y0-=miterMax;
302                 bbox.y1+=miterMax;
303             }
304         }
305     } else {
306     }
307     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
308     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
309     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
310     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
311     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
313     /* Release state data */
314     if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
315         /* Concept test */
316         if (shape->fill_shp) {
317             delete shape->fill_shp;
318             shape->fill_shp = NULL;
319         }
320     }
321     if (shape->stroke_shp) {
322         delete shape->stroke_shp;
323         shape->stroke_shp = NULL;
324     }
325     if (shape->fill_painter) {
326         sp_painter_free(shape->fill_painter);
327         shape->fill_painter = NULL;
328     }
329     if (shape->stroke_painter) {
330         sp_painter_free(shape->stroke_painter);
331         shape->stroke_painter = NULL;
332     }
334     if (!shape->curve || 
335         !shape->style ||
336         sp_curve_is_empty(shape->curve) ||
337         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
338          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
339     {
340         item->bbox = shape->approx_bbox;
341         return NR_ARENA_ITEM_STATE_ALL;
342     }
344     /* Build state data */
345     if ( shape->delayed_shp ) {
346         item->bbox=shape->approx_bbox;
347     } else {
348         nr_arena_shape_update_stroke(shape, gc, area);
349         nr_arena_shape_update_fill(shape, gc, area);
351         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
352         nr_arena_shape_add_bboxes(shape,bbox);
354         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
355         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
356         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
357         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
358     }
360     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
362     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
363     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
364     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
365     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
366     nr_arena_request_render_rect(item->arena, &item->bbox);
368     item->render_opacity = TRUE;
369     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
370         if (gc && gc->parent) {
371             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
372                                                               NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
373                                                               &shape->paintbox);
374         }
375         item->render_opacity = FALSE;
376     }
377     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
378         if (gc && gc->parent) {
379             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
380                                                                 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
381                                                                 &shape->paintbox);
382         }
383         item->render_opacity = FALSE;
384     }
385     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
386            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
387           || (shape->markers)
388         )
389     {
390         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
391         item->render_opacity = FALSE;
392     }
394     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
395         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
396             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
397         }
398     }
400     return NR_ARENA_ITEM_STATE_ALL;
403 int matrix_is_isometry(NR::Matrix p) {
404     NR::Matrix   tp;
405     // transposition
406     tp[0]=p[0];
407     tp[1]=p[2];
408     tp[2]=p[1];
409     tp[3]=p[3];
410     for (int i = 4; i < 6; i++) // shut valgrind up :)
411         tp[i] = p[i] = 0;
412     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
413     // Is the adjunct nearly an identity function?
414     if (isom.is_translation(0.01)) {
415         // the transformation is an isometry -> no need to recompute
416         // the uncrossed polygon
417         if ( p.det() < 0 )
418             return -1;
419         else
420             return 1;
421     }
422     return 0;
425 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
426     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
429 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
430 void
431 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
433     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
434         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
435         if (TRUE || !shape->fill_shp) {
436             NR::Matrix  cached_to_new = NR::identity();
437             int isometry = 0;
438             if ( shape->cached_fill ) {
439                 if (shape->cached_fctm == gc->transform) {
440                     isometry = 2; // identity
441                 } else {
442                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
443                     isometry = matrix_is_isometry(cached_to_new);
444                 }
445                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
446                     isometry = 0;
447             }
448             if ( isometry == 0 ) {
449                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
450                 shape->cached_fill->Reset();
452                 Path*  thePath=new Path;
453                 Shape* theShape=new Shape;
454                 {
455                     NR::Matrix   tempMat(gc->transform);
456                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
457                 }
459                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
460                     thePath->Convert(1.0);
461                     shape->cached_fpartialy = false;
462                 } else {
463                     thePath->Convert(area, 1.0);
464                     shape->cached_fpartialy = true;
465                 }
467                 thePath->Fill(theShape, 0);
469                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
470                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
471                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
472                     //shape->cached_fill->Copy(theShape);
473                 } else {
474                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
475                 }
476                 shape->cached_fctm=gc->transform;
477                 shape->cached_farea = *area;
478                 delete theShape;
479                 delete thePath;
480                 if ( shape->fill_shp == NULL )
481                     shape->fill_shp = new Shape;
483                 shape->fill_shp->Copy(shape->cached_fill);
485             } else if ( 2 == isometry ) {
486                 if ( shape->fill_shp == NULL ) {
487                     shape->fill_shp = new Shape;
488                     shape->fill_shp->Copy(shape->cached_fill);
489                 }
490             } else {
492                 if ( shape->fill_shp == NULL )
493                     shape->fill_shp = new Shape;
495                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
496                                        shape->cached_fill->numberOfEdges());
497                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
498                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
499                 if ( isometry == 1 ) {
500                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
501                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
502                                                  shape->cached_fill->getEdge(i).en);
503                 } else if ( isometry == -1 ) { // need to flip poly.
504                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
505                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
506                                                  shape->cached_fill->getEdge(i).st);
507                 }
508                 shape->fill_shp->ForceToPolygon();
509                 shape->fill_shp->needPointsSorting();
510                 shape->fill_shp->needEdgesSorting();
511             }
512             shape->delayed_shp |= shape->cached_fpartialy;
513         }
514     }
517 void
518 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
520     SPStyle* style = shape->style;
522     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
524     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
526     if (outline) {
527         // cairo does not need the livarot path for rendering
528         return; 
529     }
531     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
532     // (since it will only be used for picking, not for rendering)
534     if (outline ||
535         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
536          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
538         float style_width = MAX(0.125, shape->_stroke.width * scale);
539         float width;
540         if (outline) {
541             width = 0.5; // 1 pixel wide, independent of zoom
542         } else {
543             width = style_width;
544         }
546         NR::Matrix  cached_to_new = NR::identity();
548         int isometry = 0;
549         if ( shape->cached_stroke ) {
550             if (shape->cached_sctm == gc->transform) {
551                 isometry = 2; // identity
552             } else {
553                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
554                 isometry = matrix_is_isometry(cached_to_new);
555             }
556             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
557                 isometry = 0;
558             if (0 != isometry && width != shape->cached_width) { 
559                 // if this happens without setting style, we have just switched to outline or back
560                 isometry = 0; 
561             } 
562         }
564         if ( isometry == 0 ) {
565             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
566             shape->cached_stroke->Reset();
567             Path*  thePath = new Path;
568             Shape* theShape = new Shape;
569             {
570                 NR::Matrix   tempMat(gc->transform);
571                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
572             }
574             // add some padding to the rendering area, so clipped path does not go into a render area
575             NRRectL padded_area = *area;
576             padded_area.x0 -= (NR::ICoord)width;
577             padded_area.x1 += (NR::ICoord)width;
578             padded_area.y0 -= (NR::ICoord)width;
579             padded_area.y1 += (NR::ICoord)width;
580             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
581                 thePath->Convert((outline) ? 4.0 : 1.0);
582                 shape->cached_spartialy = false;
583             }
584             else {
585                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
586                 shape->cached_spartialy = true;
587             }
589             if (style->stroke_dash.n_dash && !outline) {
590                 thePath->DashPolylineFromStyle(style, scale, 1.0);
591             }
593             ButtType butt=butt_straight;
594             switch (shape->_stroke.cap) {
595                 case NRArenaShape::BUTT_CAP:
596                     butt = butt_straight;
597                     break;
598                 case NRArenaShape::ROUND_CAP:
599                     butt = butt_round;
600                     break;
601                 case NRArenaShape::SQUARE_CAP:
602                     butt = butt_square;
603                     break;
604             }
605             JoinType join=join_straight;
606             switch (shape->_stroke.join) {
607                 case NRArenaShape::MITRE_JOIN:
608                     join = join_pointy;
609                     break;
610                 case NRArenaShape::ROUND_JOIN:
611                     join = join_round;
612                     break;
613                 case NRArenaShape::BEVEL_JOIN:
614                     join = join_straight;
615                     break;
616             }
618             if (outline) {
619                 butt = butt_straight;
620                 join = join_straight;
621             }
623             thePath->Stroke(theShape, false, 0.5*width, join, butt,
624                             0.5*width*shape->_stroke.mitre_limit);
627             if (outline) {
628                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
629                 shape->cached_stroke->Copy(theShape);
630             } else {
631                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
632             }
634             shape->cached_width = width;
636             shape->cached_sctm=gc->transform;
637             shape->cached_sarea = *area;
638             delete thePath;
639             delete theShape;
640             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
642             shape->stroke_shp->Copy(shape->cached_stroke);
644         } else if ( 2 == isometry ) {
645             if ( shape->stroke_shp == NULL ) {
646                 shape->stroke_shp=new Shape;
647                 shape->stroke_shp->Copy(shape->cached_stroke);
648             }
649         } else {
650             if ( shape->stroke_shp == NULL )
651                 shape->stroke_shp=new Shape;
652             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
653             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
654                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
655             if ( isometry == 1 ) {
656                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
657                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
658                                                shape->cached_stroke->getEdge(i).en);
659             } else if ( isometry == -1 ) {
660                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
661                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
662                                                shape->cached_stroke->getEdge(i).st);
663             }
664             shape->stroke_shp->ForceToPolygon();
665             shape->stroke_shp->needPointsSorting();
666             shape->stroke_shp->needEdgesSorting();
667         }
668         shape->delayed_shp |= shape->cached_spartialy;
669     }
673 void
674 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
676     if ( shape->stroke_shp ) {
677         shape->stroke_shp->CalcBBox();
678         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
679         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
680         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
681         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
682         if ( bbox.x0 >= bbox.x1 ) {
683             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
684                 bbox.x0=shape->stroke_shp->leftX;
685                 bbox.x1=shape->stroke_shp->rightX;
686             }
687         } else {
688             if ( shape->stroke_shp->leftX < bbox.x0 )
689                 bbox.x0=shape->stroke_shp->leftX;
690             if ( shape->stroke_shp->rightX > bbox.x1 )
691                 bbox.x1=shape->stroke_shp->rightX;
692         }
693         if ( bbox.y0 >= bbox.y1 ) {
694             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
695                 bbox.y0=shape->stroke_shp->topY;
696                 bbox.y1=shape->stroke_shp->bottomY;
697             }
698         } else {
699             if ( shape->stroke_shp->topY < bbox.y0 )
700                 bbox.y0=shape->stroke_shp->topY;
701             if ( shape->stroke_shp->bottomY > bbox.y1 )
702                 bbox.y1=shape->stroke_shp->bottomY;
703         }
704     }
705     if ( shape->fill_shp ) {
706         shape->fill_shp->CalcBBox();
707         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
708         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
709         shape->fill_shp->topY=floor(shape->fill_shp->topY);
710         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
711         if ( bbox.x0 >= bbox.x1 ) {
712             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
713                 bbox.x0=shape->fill_shp->leftX;
714                 bbox.x1=shape->fill_shp->rightX;
715             }
716         } else {
717             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
718             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
719         }
720         if ( bbox.y0 >= bbox.y1 ) {
721             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
722                 bbox.y0=shape->fill_shp->topY;
723                 bbox.y1=shape->fill_shp->bottomY;
724             }
725         } else {
726             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
727             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
728         }
729     }
732 // cairo outline rendering:
733 static unsigned int
734 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
736     NRArenaShape *shape = NR_ARENA_SHAPE(item);
738     if (!ct) 
739         return item->state;
741     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
742     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
743     // by setting color channels in the "wrong" order
744     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));
746     cairo_set_line_width(ct, 0.5);
747     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
748     cairo_new_path(ct);
750     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
752     cairo_stroke(ct);
754     return item->state;
757 // cairo stroke rendering (flat color only so far!):
758 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
759 // (need to switch them to premul before this can be enabled)
760 void
761 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
763     NRArenaShape *shape = NR_ARENA_SHAPE(item);
764     SPStyle const *style = shape->style;
766     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
768     if (fabs(shape->_stroke.width * scale) < 0.01)
769         return;
771     cairo_t *ct = nr_create_cairo_context (area, pb);
773     if (!ct) 
774         return;
776     guint32 rgba;
777     if ( item->render_opacity ) {
778         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
779                                           shape->_stroke.opacity *
780                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
781     } else {
782         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
783                                           shape->_stroke.opacity);
784     }
786     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
787     // by setting color channels in the "wrong" order
788     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));
790     float style_width = MAX(0.125, shape->_stroke.width * scale);
791     cairo_set_line_width(ct, style_width);
793     switch (shape->_stroke.cap) {
794         case NRArenaShape::BUTT_CAP:
795             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
796             break;
797         case NRArenaShape::ROUND_CAP:
798             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
799             break;
800         case NRArenaShape::SQUARE_CAP:
801             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
802             break;
803     }
804     switch (shape->_stroke.join) {
805         case NRArenaShape::MITRE_JOIN:
806             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
807             break;
808         case NRArenaShape::ROUND_JOIN:
809             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
810             break;
811         case NRArenaShape::BEVEL_JOIN:
812             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
813             break;
814     }
816     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
818     if (style->stroke_dash.n_dash) {
819         NRVpathDash dash;
820         dash.offset = style->stroke_dash.offset * scale;
821         dash.n_dash = style->stroke_dash.n_dash;
822         dash.dash = g_new(double, dash.n_dash);
823         for (int i = 0; i < dash.n_dash; i++) {
824             dash.dash[i] = style->stroke_dash.dash[i] * scale;
825         }
826         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
827         g_free(dash.dash);
828     }
830     cairo_set_tolerance(ct, 0.1);
831     cairo_new_path(ct);
833     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
835     cairo_stroke(ct);
837     cairo_surface_t *cst = cairo_get_target(ct);
838     cairo_destroy (ct);
839     cairo_surface_finish (cst);
840     cairo_surface_destroy (cst);
842     pb->empty = FALSE;
846 /**
847  * Renders the item.  Markers are just composed into the parent buffer.
848  */
849 static unsigned int
850 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
852     NRArenaShape *shape = NR_ARENA_SHAPE(item);
854     if (!shape->curve) return item->state;
855     if (!shape->style) return item->state;
857     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
859     if (outline) { // cairo outline rendering
861         pb->empty = FALSE;
862         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
863         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
865     } else {
867     if ( shape->delayed_shp ) {
868         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
869             NRGC   tempGC(NULL);
870             tempGC.transform=shape->ctm;
871             shape->delayed_shp = false;
872             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
873             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
874 /*      NRRect bbox;
875         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
876         nr_arena_shape_add_bboxes(shape,bbox);
877         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
878         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
879         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
880         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
881         shape->approx_bbox=item->bbox;*/
882         } else {
883             return item->state;
884         }
885     }
887     SPStyle const *style = shape->style;
888     if (shape->fill_shp) {
889         NRPixBlock m;
890         guint32 rgba;
892         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
894         // if memory allocation failed, abort render
895         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
896             nr_pixblock_release (&m);
897             return (item->state);
898         }
900         m.visible_area = pb->visible_area; 
901         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
902         m.empty = FALSE;
904         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
905             // do not render fill in any way
906         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
907             if ( item->render_opacity ) {
908                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
909                                                   shape->_fill.opacity *
910                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
911             } else {
912                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
913                                                   shape->_fill.opacity);
914             }
915             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
916             pb->empty = FALSE;
917         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
918             if (shape->fill_painter) {
919                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
920             }
921         }
923         nr_pixblock_release(&m);
924     }
926     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
928         // cairo_arena_shape_render_stroke(item, area, pb);
930         guint32 rgba;
931         NRPixBlock m;
933         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
935         // if memory allocation failed, abort render
936         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
937             nr_pixblock_release (&m);
938             return (item->state);
939         }
941         m.visible_area = pb->visible_area; 
942         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
943         m.empty = FALSE;
945             if ( item->render_opacity ) {
946                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
947                                                   shape->_stroke.opacity *
948                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
949             } else {
950                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
951                                                   shape->_stroke.opacity);
952             }
953             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
954             pb->empty = FALSE;
956         nr_pixblock_release(&m);
958     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
960         NRPixBlock m;
962         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
964         // if memory allocation failed, abort render
965         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
966             nr_pixblock_release (&m);
967             return (item->state);
968         }
970         m.visible_area = pb->visible_area; 
971         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
972         m.empty = FALSE;
974         if (shape->stroke_painter) {
975             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
976         }
978         nr_pixblock_release(&m);
979     }
981     } // non-cairo non-outline branch
983     /* Render markers into parent buffer */
984     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
985         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
986         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
987     }
989     return item->state;
993 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
994 // reenable this when the bug is fixed and remove the rest of this function
995 static guint
996 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
998     NRArenaShape *shape = NR_ARENA_SHAPE(item);
999     if (!shape->curve) return item->state;
1001         cairo_t *ct = nr_create_cairo_context (area, pb);
1003         if (!ct) 
1004             return item->state;
1006         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1008         cairo_new_path(ct);
1010         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1012         cairo_fill(ct);
1014         cairo_surface_t *cst = cairo_get_target(ct);
1015         cairo_destroy (ct);
1016         cairo_surface_finish (cst);
1017         cairo_surface_destroy (cst);
1019         pb->empty = FALSE;
1021         return item->state;
1025 static guint
1026 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1028     //return cairo_arena_shape_clip(item, area, pb);
1030     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1031     if (!shape->curve) return item->state;
1033     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1034         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1035             NRGC   tempGC(NULL);
1036             tempGC.transform=shape->ctm;
1037             shape->delayed_shp = false;
1038             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1039         } else {
1040             return item->state;
1041         }
1042     }
1044     if ( shape->fill_shp ) {
1045         NRPixBlock m;
1047         /* fixme: We can OR in one step (Lauris) */
1048         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1050         // if memory allocation failed, abort 
1051         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1052             nr_pixblock_release (&m);
1053             return (item->state);
1054         }
1056         m.visible_area = pb->visible_area; 
1057         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1059         for (int y = area->y0; y < area->y1; y++) {
1060             unsigned char *s, *d;
1061             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1062             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1063             for (int x = area->x0; x < area->x1; x++) {
1064                 *d = NR_COMPOSEA_111(*s, *d);
1065                 d ++;
1066                 s ++;
1067             }
1068         }
1069         nr_pixblock_release(&m);
1070         pb->empty = FALSE;
1071     }
1073     return item->state;
1076 static NRArenaItem *
1077 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1079     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1081     if (shape->repick_after > 0)
1082         shape->repick_after--;
1084     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1085         return shape->last_pick;
1087     if (!shape->curve) return NULL;
1088     if (!shape->style) return NULL;
1089     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1090         return NULL;
1092     GTimeVal tstart, tfinish;
1093     g_get_current_time (&tstart);
1095     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1097     double width;
1098     if (outline) {
1099         width = 0.5;
1100     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1101         float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1102         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1103     } else {
1104         width = 0;
1105     }
1107     NRBPath bp;
1108     bp.path = SP_CURVE_BPATH(shape->curve);
1109     double dist = NR_HUGE;
1110     int wind = 0;
1111     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1112              && shape->_fill.opacity > 1e-3 && !outline);
1114     if (item->arena->canvasarena) {
1115         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1116         viewbox.growBy (width);
1117         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1118     } else {
1119         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1120     }
1122     g_get_current_time (&tfinish);
1123     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1124     //g_print ("pick time %lu\n", this_pick);
1126     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1127         shape->repick_after = this_pick / 5000;
1128     }
1130     // covered by fill?
1131     if (needfill) {
1132         if (!shape->style->fill_rule.computed) {
1133             if (wind != 0) {
1134                 shape->last_pick = item;
1135                 return item;
1136             }
1137         } else {
1138             if (wind & 0x1) {
1139                 shape->last_pick = item;
1140                 return item;
1141             }
1142         }
1143     }
1145     // close to the edge, as defined by strokewidth and delta?
1146     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1147     if (needfill || width > 0) { // if either fill or stroke visible,
1148         if ((dist - width) < delta) {
1149             shape->last_pick = item;
1150             return item;
1151         }
1152     }
1154     // if not picked on the shape itself, try its markers
1155     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1156         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1157         if (ret) {
1158             shape->last_pick = item;
1159             return item;
1160         }
1161     }
1163     shape->last_pick = NULL;
1164     return NULL;
1167 /**
1168  *
1169  *  Requests a render of the shape, then if the shape is already a curve it
1170  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1171  *  curve and adds it to the shape.  Finally, it requests an update of the
1172  *  arena for the shape.
1173  */
1174 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1176     g_return_if_fail(shape != NULL);
1177     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1179     if ( justTrans == false ) {
1180         // dirty cached versions
1181         if ( shape->cached_fill ) {
1182             delete shape->cached_fill;
1183             shape->cached_fill=NULL;
1184         }
1185         if ( shape->cached_stroke ) {
1186             delete shape->cached_stroke;
1187             shape->cached_stroke=NULL;
1188         }
1189     }
1191     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1193     if (shape->curve) {
1194         sp_curve_unref(shape->curve);
1195         shape->curve = NULL;
1196     }
1198     if (curve) {
1199         shape->curve = curve;
1200         sp_curve_ref(curve);
1201     }
1203     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1206 void NRArenaShape::setFill(SPPaintServer *server) {
1207     _fill.paint.set(server);
1208     _invalidateCachedFill();
1211 void NRArenaShape::setFill(SPColor const &color) {
1212     _fill.paint.set(color);
1213     _invalidateCachedFill();
1216 void NRArenaShape::setFillOpacity(double opacity) {
1217     _fill.opacity = opacity;
1218     _invalidateCachedFill();
1221 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1222     _fill.rule = rule;
1223     _invalidateCachedFill();
1226 void NRArenaShape::setStroke(SPPaintServer *server) {
1227     _stroke.paint.set(server);
1228     _invalidateCachedStroke();
1231 void NRArenaShape::setStroke(SPColor const &color) {
1232     _stroke.paint.set(color);
1233     _invalidateCachedStroke();
1236 void NRArenaShape::setStrokeOpacity(double opacity) {
1237     _stroke.opacity = opacity;
1238     _invalidateCachedStroke();
1241 void NRArenaShape::setStrokeWidth(double width) {
1242     _stroke.width = width;
1243     _invalidateCachedStroke();
1246 void NRArenaShape::setMitreLimit(double limit) {
1247     _stroke.mitre_limit = limit;
1248     _invalidateCachedStroke();
1251 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1252     _stroke.cap = cap;
1253     _invalidateCachedStroke();
1256 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1257     _stroke.join = join;
1258     _invalidateCachedStroke();
1261 /** nr_arena_shape_set_style
1262  *
1263  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1264  */
1265 void
1266 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1268     g_return_if_fail(shape != NULL);
1269     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1271     if (style) sp_style_ref(style);
1272     if (shape->style) sp_style_unref(shape->style);
1273     shape->style = style;
1275     if ( style->fill.isPaintserver() ) {
1276         shape->setFill(style->getFillPaintServer());
1277     } else if ( style->fill.isColor() ) {
1278         shape->setFill(style->fill.value.color);
1279     } else if ( style->fill.isNone() ) {
1280         shape->setFill(NULL);
1281     } else {
1282         g_assert_not_reached();
1283     }
1284     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1285     switch (style->fill_rule.computed) {
1286         case SP_WIND_RULE_EVENODD: {
1287             shape->setFillRule(NRArenaShape::EVEN_ODD);
1288             break;
1289         }
1290         case SP_WIND_RULE_NONZERO: {
1291             shape->setFillRule(NRArenaShape::NONZERO);
1292             break;
1293         }
1294         default: {
1295             g_assert_not_reached();
1296         }
1297     }
1299     if ( style->stroke.isPaintserver() ) {
1300         shape->setStroke(style->getStrokePaintServer());
1301     } else if ( style->stroke.isColor() ) {
1302         shape->setStroke(style->stroke.value.color);
1303     } else if ( style->stroke.isNone() ) {
1304         shape->setStroke(NULL);
1305     } else {
1306         g_assert_not_reached();
1307     }
1308     shape->setStrokeWidth(style->stroke_width.computed);
1309     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1310     switch (style->stroke_linecap.computed) {
1311         case SP_STROKE_LINECAP_ROUND: {
1312             shape->setLineCap(NRArenaShape::ROUND_CAP);
1313             break;
1314         }
1315         case SP_STROKE_LINECAP_SQUARE: {
1316             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1317             break;
1318         }
1319         case SP_STROKE_LINECAP_BUTT: {
1320             shape->setLineCap(NRArenaShape::BUTT_CAP);
1321             break;
1322         }
1323         default: {
1324             g_assert_not_reached();
1325         }
1326     }
1327     switch (style->stroke_linejoin.computed) {
1328         case SP_STROKE_LINEJOIN_ROUND: {
1329             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1330             break;
1331         }
1332         case SP_STROKE_LINEJOIN_BEVEL: {
1333             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1334             break;
1335         }
1336         case SP_STROKE_LINEJOIN_MITER: {
1337             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1338             break;
1339         }
1340         default: {
1341             g_assert_not_reached();
1342         }
1343     }
1344     shape->setMitreLimit(style->stroke_miterlimit.value);
1346     //if shape has a filter
1347     if (style->filter.set && style->getFilter()) {
1348         if (!shape->filter) {
1349             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1350             shape->filter = new NR::Filter(primitives);
1351         }
1352         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1353     } else {
1354         //no filter set for this shape
1355         delete shape->filter;
1356         shape->filter = NULL;
1357     }
1359     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1362 void
1363 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1365     g_return_if_fail(shape != NULL);
1366     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1367     g_return_if_fail(pbox != NULL);
1369     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1370         shape->paintbox = *pbox;
1371     } else {
1372         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1373         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1374         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1375     }
1377     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1380 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1382     paintbox.x0 = pbox.min()[NR::X];
1383     paintbox.y0 = pbox.min()[NR::Y];
1384     paintbox.x1 = pbox.max()[NR::X];
1385     paintbox.y1 = pbox.max()[NR::Y];
1387     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1390 static void
1391 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1393     if ( st >= en ) return;
1394     if ( vst < 0 ) vst=0;
1395     if ( vst > 1 ) vst=1;
1396     if ( ven < 0 ) ven=0;
1397     if ( ven > 1 ) ven=1;
1398     float   sv=vst;
1399     float   dv=ven-vst;
1400     int     len=en-st;
1401     unsigned char*   d=(unsigned char*)dest.buffer;
1402     d+=(st-dest.startPix);
1403     if ( fabs(dv) < 0.001 ) {
1404         if ( vst > 0.999 ) {
1405             /* Simple copy */
1406             while (len > 0) {
1407                 d[0] = 255;
1408                 d += 1;
1409                 len -= 1;
1410             }
1411         } else {
1412             sv*=256;
1413             unsigned int c0_24=(int)sv;
1414             c0_24&=0xFF;
1415             while (len > 0) {
1416                 /* Draw */
1417                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1418                 d += 1;
1419                 len -= 1;
1420             }
1421         }
1422     } else {
1423         if ( en <= st+1 ) {
1424             sv=0.5*(vst+ven);
1425             sv*=256;
1426             unsigned int c0_24=(int)sv;
1427             c0_24&=0xFF;
1428             /* Draw */
1429             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1430         } else {
1431             dv/=len;
1432             sv+=0.5*dv; // correction trapezoidale
1433             sv*=16777216;
1434             dv*=16777216;
1435             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1436             int s0_24 = static_cast<int>(dv);
1437             while (len > 0) {
1438                 unsigned int ca;
1439                 /* Draw */
1440                 ca = c0_24 >> 16;
1441                 if ( ca > 255 ) ca=255;
1442                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1443                 d += 1;
1444                 c0_24 += s0_24;
1445                 c0_24 = CLAMP(c0_24, 0, 16777216);
1446                 len -= 1;
1447             }
1448         }
1449     }
1452 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1454     theS->CalcBBox();
1455     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1456     int    il,ir,it,ib;
1457     il=(int)floor(l);
1458     ir=(int)ceil(r);
1459     it=(int)floor(t);
1460     ib=(int)ceil(b);
1462     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1463     if ( il < m.area.x0 ) il=m.area.x0;
1464     if ( it < m.area.y0 ) it=m.area.y0;
1465     if ( ir > m.area.x1 ) ir=m.area.x1;
1466     if ( ib > m.area.y1 ) ib=m.area.y1;
1468     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1469     int    curPt;
1470     float  curY;
1471     theS->BeginQuickRaster(curY, curPt);
1473     FloatLigne *theI = new FloatLigne();
1474     IntLigne *theIL = new IntLigne();
1476     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1478     char *mdata = (char*)m.data.px;
1479     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1480     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1481     for (int y = it; y < ib; y++) {
1482         theI->Reset();
1483         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1484         theI->Flatten();
1485         theIL->Copy(theI);
1487         raster_info  dest;
1488         dest.startPix=il;
1489         dest.endPix=ir;
1490         dest.sth=il;
1491         dest.stv=y;
1492         dest.buffer=ligStart;
1493         theIL->Raster(dest, NULL, shape_run_A8_OR);
1494         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1495     }
1496     theS->EndQuickRaster();
1497     delete theI;
1498     delete theIL;
1502 /*
1503   Local Variables:
1504   mode:c++
1505   c-file-style:"stroustrup"
1506   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1507   indent-tabs-mode:nil
1508   fill-column:99
1509   End:
1510 */
1511 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :