Code

c09bdb9dd9e1a405937d9a5f6edcb8a946c7640e
[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 "display/nr-filter.h"
36 #include <cairo.h>
38 //int  showRuns=0;
39 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
41 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
42 static void nr_arena_shape_init(NRArenaShape *shape);
43 static void nr_arena_shape_finalize(NRObject *object);
45 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
46 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
47 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
48 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
50 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
51 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
52 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
53 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
55 static NRArenaItemClass *shape_parent_class;
57 NRType
58 nr_arena_shape_get_type(void)
59 {
60     static NRType type = 0;
61     if (!type) {
62         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
63                                        "NRArenaShape",
64                                        sizeof(NRArenaShapeClass),
65                                        sizeof(NRArenaShape),
66                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
67                                        (void (*)(NRObject *)) nr_arena_shape_init);
68     }
69     return type;
70 }
72 static void
73 nr_arena_shape_class_init(NRArenaShapeClass *klass)
74 {
75     NRObjectClass *object_class;
76     NRArenaItemClass *item_class;
78     object_class = (NRObjectClass *) klass;
79     item_class = (NRArenaItemClass *) klass;
81     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
83     object_class->finalize = nr_arena_shape_finalize;
84     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
86     item_class->children = nr_arena_shape_children;
87     item_class->add_child = nr_arena_shape_add_child;
88     item_class->set_child_position = nr_arena_shape_set_child_position;
89     item_class->remove_child = nr_arena_shape_remove_child;
90     item_class->update = nr_arena_shape_update;
91     item_class->render = nr_arena_shape_render;
92     item_class->clip = nr_arena_shape_clip;
93     item_class->pick = nr_arena_shape_pick;
94 }
96 /**
97  * Initializes the arena shape, setting all parameters to null, 0, false,
98  * or other defaults
99  */
100 static void
101 nr_arena_shape_init(NRArenaShape *shape)
103     shape->curve = NULL;
104     shape->style = NULL;
105     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
106     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
108     nr_matrix_set_identity(&shape->ctm);
109     shape->fill_painter = NULL;
110     shape->stroke_painter = NULL;
111     shape->cached_fill = NULL;
112     shape->cached_stroke = NULL;
113     shape->cached_fpartialy = false;
114     shape->cached_spartialy = false;
115     shape->fill_shp = NULL;
116     shape->stroke_shp = NULL;
118     shape->delayed_shp = false;
120     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
121     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
122     nr_matrix_set_identity(&shape->cached_fctm);
123     nr_matrix_set_identity(&shape->cached_sctm);
125     shape->markers = NULL;
127     shape->last_pick = NULL;
128     shape->repick_after = 0;
131 static void
132 nr_arena_shape_finalize(NRObject *object)
134     NRArenaShape *shape = (NRArenaShape *) object;
136     if (shape->fill_shp) delete shape->fill_shp;
137     if (shape->stroke_shp) delete shape->stroke_shp;
138     if (shape->cached_fill) delete shape->cached_fill;
139     if (shape->cached_stroke) delete shape->cached_stroke;
140     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
141     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
143     if (shape->style) sp_style_unref(shape->style);
144     if (shape->curve) sp_curve_unref(shape->curve);
146     ((NRObjectClass *) shape_parent_class)->finalize(object);
149 /**
150  * Retrieves the markers from the item
151  */
152 static NRArenaItem *
153 nr_arena_shape_children(NRArenaItem *item)
155     NRArenaShape *shape = (NRArenaShape *) item;
157     return shape->markers;
160 /**
161  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
162  * the prev and next items.  If ref is NULL, then it sets the item's markers
163  * as the next items.
164  */
165 static void
166 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
168     NRArenaShape *shape = (NRArenaShape *) item;
170     if (!ref) {
171         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
172     } else {
173         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
174     }
176     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
179 /**
180  * Removes child from the shape.  If there are no prev items in 
181  * the child, it sets items' markers to the next item in the child.
182  */
183 static void
184 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
186     NRArenaShape *shape = (NRArenaShape *) item;
188     if (child->prev) {
189         nr_arena_item_detach(item, child);
190     } else {
191         shape->markers = nr_arena_item_detach(item, child);
192     }
194     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
197 /**
198  * Detaches child from item, and if there are no previous items in child, it 
199  * sets item's markers to the child.  It then attaches the child back onto the item.
200  * If ref is null, it sets the markers to be the next item, otherwise it uses
201  * the next/prev items in ref.
202  */
203 static void
204 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
206     NRArenaShape *shape = (NRArenaShape *) item;
208     if (child->prev) {
209         nr_arena_item_detach(item, child);
210     } else {
211         shape->markers = nr_arena_item_detach(item, child);
212     }
214     if (!ref) {
215         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
216     } else {
217         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
218     }
220     nr_arena_item_request_render(child);
223 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
224 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
225 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
227 /**
228  * Updates the arena shape 'item' and all of its children, including the markers.
229  */
230 static guint
231 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
233     NRRect bbox;
235     NRArenaShape *shape = NR_ARENA_SHAPE(item);
237     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
239     unsigned int newstate;
240     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
241         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
242         beststate = beststate & newstate;
243     }
245     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
246         /* We do not have to create rendering structures */
247         shape->ctm = gc->transform;
248         if (state & NR_ARENA_ITEM_STATE_BBOX) {
249             if (shape->curve) {
250                 NRBPath bp;
251                 /* fixme: */
252                 bbox.x0 = bbox.y0 = NR_HUGE;
253                 bbox.x1 = bbox.y1 = -NR_HUGE;
254                 bp.path = SP_CURVE_BPATH(shape->curve);
255                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
256                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
257                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
258                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
259                 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
260             }
261             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
262                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
263                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
264                 }
265             }
266         }
267         return (state | item->state);
268     }
270     shape->delayed_shp=true;
271     shape->ctm = gc->transform;
272     bbox.x0 = bbox.y0 = NR_HUGE;
273     bbox.x1 = bbox.y1 = -NR_HUGE;
275     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
277     if (shape->curve) {
278         NRBPath bp;
279         /* fixme: */
280         bbox.x0 = bbox.y0 = NR_HUGE;
281         bbox.x1 = bbox.y1 = -NR_HUGE;
282         bp.path = SP_CURVE_BPATH(shape->curve);
283         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
284         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
285             float width, scale;
286             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
287             width = MAX(0.125, shape->_stroke.width * scale);
288             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
289                 bbox.x0-=width;
290                 bbox.x1+=width;
291                 bbox.y0-=width;
292                 bbox.y1+=width;
293             }
294             // those pesky miters, now
295             float miterMax=width*shape->_stroke.mitre_limit;
296             if ( miterMax > 0.01 ) {
297                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
298                 bbox.x0-=miterMax;
299                 bbox.x1+=miterMax;
300                 bbox.y0-=miterMax;
301                 bbox.y1+=miterMax;
302             }
303         }
304     } else {
305     }
306     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
307     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
308     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
309     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
310     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
312     /* Release state data */
313     if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
314         /* Concept test */
315         if (shape->fill_shp) {
316             delete shape->fill_shp;
317             shape->fill_shp = NULL;
318         }
319     }
320     if (shape->stroke_shp) {
321         delete shape->stroke_shp;
322         shape->stroke_shp = NULL;
323     }
324     if (shape->fill_painter) {
325         sp_painter_free(shape->fill_painter);
326         shape->fill_painter = NULL;
327     }
328     if (shape->stroke_painter) {
329         sp_painter_free(shape->stroke_painter);
330         shape->stroke_painter = NULL;
331     }
333     if (!shape->curve || 
334         !shape->style ||
335         sp_curve_is_empty(shape->curve) ||
336         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
337          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
338     {
339         item->bbox = shape->approx_bbox;
340         return NR_ARENA_ITEM_STATE_ALL;
341     }
343     /* Build state data */
344     if ( shape->delayed_shp ) {
345         item->bbox=shape->approx_bbox;
346     } else {
347         nr_arena_shape_update_stroke(shape, gc, area);
348         nr_arena_shape_update_fill(shape, gc, area);
350         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
351         nr_arena_shape_add_bboxes(shape,bbox);
353         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
354         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
355         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
356         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
357     }
359     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
361     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
362     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
363     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
364     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
365     nr_arena_request_render_rect(item->arena, &item->bbox);
367     item->render_opacity = TRUE;
368     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
369         if (gc && gc->parent) {
370             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
371                                                               NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
372                                                               &shape->paintbox);
373         }
374         item->render_opacity = FALSE;
375     }
376     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
377         if (gc && gc->parent) {
378             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
379                                                                 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
380                                                                 &shape->paintbox);
381         }
382         item->render_opacity = FALSE;
383     }
384     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
385            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
386           || (shape->markers)
387         )
388     {
389         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
390         item->render_opacity = FALSE;
391     }
393     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
394         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
395             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
396         }
397     }
399     return NR_ARENA_ITEM_STATE_ALL;
402 int matrix_is_isometry(NR::Matrix p) {
403     NR::Matrix   tp;
404     // transposition
405     tp[0]=p[0];
406     tp[1]=p[2];
407     tp[2]=p[1];
408     tp[3]=p[3];
409     for (int i = 4; i < 6; i++) // shut valgrind up :)
410         tp[i] = p[i] = 0;
411     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
412     // Is the adjunct nearly an identity function?
413     if (isom.is_translation(0.01)) {
414         // the transformation is an isometry -> no need to recompute
415         // the uncrossed polygon
416         if ( p.det() < 0 )
417             return -1;
418         else
419             return 1;
420     }
421     return 0;
424 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
425     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
428 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
429 void
430 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
432     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
433         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
434         if (TRUE || !shape->fill_shp) {
435             NR::Matrix  cached_to_new = NR::identity();
436             int isometry = 0;
437             if ( shape->cached_fill ) {
438                 if (shape->cached_fctm == gc->transform) {
439                     isometry = 2; // identity
440                 } else {
441                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
442                     isometry = matrix_is_isometry(cached_to_new);
443                 }
444                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
445                     isometry = 0;
446             }
447             if ( isometry == 0 ) {
448                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
449                 shape->cached_fill->Reset();
451                 Path*  thePath=new Path;
452                 Shape* theShape=new Shape;
453                 {
454                     NR::Matrix   tempMat(gc->transform);
455                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
456                 }
458                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
459                     thePath->Convert(1.0);
460                     shape->cached_fpartialy = false;
461                 } else {
462                     thePath->Convert(area, 1.0);
463                     shape->cached_fpartialy = true;
464                 }
466                 thePath->Fill(theShape, 0);
468                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
469                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
470                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
471                     //shape->cached_fill->Copy(theShape);
472                 } else {
473                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
474                 }
475                 shape->cached_fctm=gc->transform;
476                 shape->cached_farea = *area;
477                 delete theShape;
478                 delete thePath;
479                 if ( shape->fill_shp == NULL )
480                     shape->fill_shp = new Shape;
482                 shape->fill_shp->Copy(shape->cached_fill);
484             } else if ( 2 == isometry ) {
485                 if ( shape->fill_shp == NULL ) {
486                     shape->fill_shp = new Shape;
487                     shape->fill_shp->Copy(shape->cached_fill);
488                 }
489             } else {
491                 if ( shape->fill_shp == NULL )
492                     shape->fill_shp = new Shape;
494                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
495                                        shape->cached_fill->numberOfEdges());
496                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
497                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
498                 if ( isometry == 1 ) {
499                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
500                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
501                                                  shape->cached_fill->getEdge(i).en);
502                 } else if ( isometry == -1 ) { // need to flip poly.
503                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
504                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
505                                                  shape->cached_fill->getEdge(i).st);
506                 }
507                 shape->fill_shp->ForceToPolygon();
508                 shape->fill_shp->needPointsSorting();
509                 shape->fill_shp->needEdgesSorting();
510             }
511             shape->delayed_shp |= shape->cached_fpartialy;
512         }
513     }
516 void
517 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
519     SPStyle* style = shape->style;
521     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
523     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
525     if (outline) {
526         // cairo does not need the livarot path for rendering
527         return; 
528     }
530     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
531     // (since it will only be used for picking, not for rendering)
533     if (outline ||
534         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
535          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
537         float style_width = MAX(0.125, shape->_stroke.width * scale);
538         float width;
539         if (outline) {
540             width = 0.5; // 1 pixel wide, independent of zoom
541         } else {
542             width = style_width;
543         }
545         NR::Matrix  cached_to_new = NR::identity();
547         int isometry = 0;
548         if ( shape->cached_stroke ) {
549             if (shape->cached_sctm == gc->transform) {
550                 isometry = 2; // identity
551             } else {
552                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
553                 isometry = matrix_is_isometry(cached_to_new);
554             }
555             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
556                 isometry = 0;
557             if (0 != isometry && width != shape->cached_width) { 
558                 // if this happens without setting style, we have just switched to outline or back
559                 isometry = 0; 
560             } 
561         }
563         if ( isometry == 0 ) {
564             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
565             shape->cached_stroke->Reset();
566             Path*  thePath = new Path;
567             Shape* theShape = new Shape;
568             {
569                 NR::Matrix   tempMat(gc->transform);
570                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
571             }
573             // add some padding to the rendering area, so clipped path does not go into a render area
574             NRRectL padded_area = *area;
575             padded_area.x0 -= (NR::ICoord)width;
576             padded_area.x1 += (NR::ICoord)width;
577             padded_area.y0 -= (NR::ICoord)width;
578             padded_area.y1 += (NR::ICoord)width;
579             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
580                 thePath->Convert((outline) ? 4.0 : 1.0);
581                 shape->cached_spartialy = false;
582             }
583             else {
584                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
585                 shape->cached_spartialy = true;
586             }
588             if (style->stroke_dash.n_dash && !outline) {
589                 thePath->DashPolylineFromStyle(style, scale, 1.0);
590             }
592             ButtType butt=butt_straight;
593             switch (shape->_stroke.cap) {
594                 case NRArenaShape::BUTT_CAP:
595                     butt = butt_straight;
596                     break;
597                 case NRArenaShape::ROUND_CAP:
598                     butt = butt_round;
599                     break;
600                 case NRArenaShape::SQUARE_CAP:
601                     butt = butt_square;
602                     break;
603             }
604             JoinType join=join_straight;
605             switch (shape->_stroke.join) {
606                 case NRArenaShape::MITRE_JOIN:
607                     join = join_pointy;
608                     break;
609                 case NRArenaShape::ROUND_JOIN:
610                     join = join_round;
611                     break;
612                 case NRArenaShape::BEVEL_JOIN:
613                     join = join_straight;
614                     break;
615             }
617             if (outline) {
618                 butt = butt_straight;
619                 join = join_straight;
620             }
622             thePath->Stroke(theShape, false, 0.5*width, join, butt,
623                             0.5*width*shape->_stroke.mitre_limit);
626             if (outline) {
627                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
628                 shape->cached_stroke->Copy(theShape);
629             } else {
630                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
631             }
633             shape->cached_width = width;
635             shape->cached_sctm=gc->transform;
636             shape->cached_sarea = *area;
637             delete thePath;
638             delete theShape;
639             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
641             shape->stroke_shp->Copy(shape->cached_stroke);
643         } else if ( 2 == isometry ) {
644             if ( shape->stroke_shp == NULL ) {
645                 shape->stroke_shp=new Shape;
646                 shape->stroke_shp->Copy(shape->cached_stroke);
647             }
648         } else {
649             if ( shape->stroke_shp == NULL )
650                 shape->stroke_shp=new Shape;
651             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
652             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
653                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
654             if ( isometry == 1 ) {
655                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
656                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
657                                                shape->cached_stroke->getEdge(i).en);
658             } else if ( isometry == -1 ) {
659                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
660                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
661                                                shape->cached_stroke->getEdge(i).st);
662             }
663             shape->stroke_shp->ForceToPolygon();
664             shape->stroke_shp->needPointsSorting();
665             shape->stroke_shp->needEdgesSorting();
666         }
667         shape->delayed_shp |= shape->cached_spartialy;
668     }
672 void
673 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
675     if ( shape->stroke_shp ) {
676         shape->stroke_shp->CalcBBox();
677         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
678         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
679         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
680         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
681         if ( bbox.x0 >= bbox.x1 ) {
682             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
683                 bbox.x0=shape->stroke_shp->leftX;
684                 bbox.x1=shape->stroke_shp->rightX;
685             }
686         } else {
687             if ( shape->stroke_shp->leftX < bbox.x0 )
688                 bbox.x0=shape->stroke_shp->leftX;
689             if ( shape->stroke_shp->rightX > bbox.x1 )
690                 bbox.x1=shape->stroke_shp->rightX;
691         }
692         if ( bbox.y0 >= bbox.y1 ) {
693             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
694                 bbox.y0=shape->stroke_shp->topY;
695                 bbox.y1=shape->stroke_shp->bottomY;
696             }
697         } else {
698             if ( shape->stroke_shp->topY < bbox.y0 )
699                 bbox.y0=shape->stroke_shp->topY;
700             if ( shape->stroke_shp->bottomY > bbox.y1 )
701                 bbox.y1=shape->stroke_shp->bottomY;
702         }
703     }
704     if ( shape->fill_shp ) {
705         shape->fill_shp->CalcBBox();
706         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
707         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
708         shape->fill_shp->topY=floor(shape->fill_shp->topY);
709         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
710         if ( bbox.x0 >= bbox.x1 ) {
711             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
712                 bbox.x0=shape->fill_shp->leftX;
713                 bbox.x1=shape->fill_shp->rightX;
714             }
715         } else {
716             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
717             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
718         }
719         if ( bbox.y0 >= bbox.y1 ) {
720             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
721                 bbox.y0=shape->fill_shp->topY;
722                 bbox.y1=shape->fill_shp->bottomY;
723             }
724         } else {
725             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
726             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
727         }
728     }
731 // cairo outline rendering:
732 static unsigned int
733 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
735     NRArenaShape *shape = NR_ARENA_SHAPE(item);
737     if (!ct) 
738         return item->state;
740     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
741     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
742     // by setting color channels in the "wrong" order
743     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));
745     cairo_set_line_width(ct, 0.5);
746     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
747     cairo_new_path(ct);
749     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
751     cairo_stroke(ct);
753     return item->state;
756 // cairo stroke rendering (flat color only so far!):
757 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
758 // (need to switch them to premul before this can be enabled)
759 void
760 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
762     NRArenaShape *shape = NR_ARENA_SHAPE(item);
763     SPStyle const *style = shape->style;
765     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
767     if (fabs(shape->_stroke.width * scale) < 0.01)
768         return;
770     cairo_t *ct = nr_create_cairo_context (area, pb);
772     if (!ct) 
773         return;
775     guint32 rgba;
776     if ( item->render_opacity ) {
777         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
778                                           shape->_stroke.opacity *
779                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
780     } else {
781         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
782                                           shape->_stroke.opacity);
783     }
785     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
786     // by setting color channels in the "wrong" order
787     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));
789     float style_width = MAX(0.125, shape->_stroke.width * scale);
790     cairo_set_line_width(ct, style_width);
792     switch (shape->_stroke.cap) {
793         case NRArenaShape::BUTT_CAP:
794             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
795             break;
796         case NRArenaShape::ROUND_CAP:
797             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
798             break;
799         case NRArenaShape::SQUARE_CAP:
800             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
801             break;
802     }
803     switch (shape->_stroke.join) {
804         case NRArenaShape::MITRE_JOIN:
805             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
806             break;
807         case NRArenaShape::ROUND_JOIN:
808             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
809             break;
810         case NRArenaShape::BEVEL_JOIN:
811             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
812             break;
813     }
815     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
817     if (style->stroke_dash.n_dash) {
818         NRVpathDash dash;
819         dash.offset = style->stroke_dash.offset * scale;
820         dash.n_dash = style->stroke_dash.n_dash;
821         dash.dash = g_new(double, dash.n_dash);
822         for (int i = 0; i < dash.n_dash; i++) {
823             dash.dash[i] = style->stroke_dash.dash[i] * scale;
824         }
825         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
826         g_free(dash.dash);
827     }
829     cairo_set_tolerance(ct, 0.1);
830     cairo_new_path(ct);
832     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
834     cairo_stroke(ct);
836     cairo_surface_t *cst = cairo_get_target(ct);
837     cairo_destroy (ct);
838     cairo_surface_finish (cst);
839     cairo_surface_destroy (cst);
841     pb->empty = FALSE;
845 /**
846  * Renders the item.  Markers are just composed into the parent buffer.
847  */
848 static unsigned int
849 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
851     NRArenaShape *shape = NR_ARENA_SHAPE(item);
853     if (!shape->curve) return item->state;
854     if (!shape->style) return item->state;
856     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
858     if (outline) { // cairo outline rendering
860         pb->empty = FALSE;
861         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
862         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
864     } else {
866     if ( shape->delayed_shp ) {
867         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
868             NRGC   tempGC(NULL);
869             tempGC.transform=shape->ctm;
870             shape->delayed_shp = false;
871             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
872             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
873 /*      NRRect bbox;
874         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
875         nr_arena_shape_add_bboxes(shape,bbox);
876         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
877         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
878         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
879         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
880         shape->approx_bbox=item->bbox;*/
881         } else {
882             return item->state;
883         }
884     }
886     SPStyle const *style = shape->style;
887     if (shape->fill_shp) {
888         NRPixBlock m;
889         guint32 rgba;
891         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
893         // if memory allocation failed, abort render
894         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
895             nr_pixblock_release (&m);
896             return (item->state);
897         }
899         m.visible_area = pb->visible_area; 
900         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
901         m.empty = FALSE;
903         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
904             // do not render fill in any way
905         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
906             if ( item->render_opacity ) {
907                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
908                                                   shape->_fill.opacity *
909                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
910             } else {
911                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
912                                                   shape->_fill.opacity);
913             }
914             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
915             pb->empty = FALSE;
916         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
917             if (shape->fill_painter) {
918                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
919             }
920         }
922         nr_pixblock_release(&m);
923     }
925     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
927         // cairo_arena_shape_render_stroke(item, area, pb);
929         guint32 rgba;
930         NRPixBlock m;
932         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
934         // if memory allocation failed, abort render
935         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
936             nr_pixblock_release (&m);
937             return (item->state);
938         }
940         m.visible_area = pb->visible_area; 
941         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
942         m.empty = FALSE;
944             if ( item->render_opacity ) {
945                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
946                                                   shape->_stroke.opacity *
947                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
948             } else {
949                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
950                                                   shape->_stroke.opacity);
951             }
952             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
953             pb->empty = FALSE;
955         nr_pixblock_release(&m);
957     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
959         NRPixBlock m;
961         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
963         // if memory allocation failed, abort render
964         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
965             nr_pixblock_release (&m);
966             return (item->state);
967         }
969         m.visible_area = pb->visible_area; 
970         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
971         m.empty = FALSE;
973         if (shape->stroke_painter) {
974             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
975         }
977         nr_pixblock_release(&m);
978     }
980     } // non-cairo non-outline branch
982     /* Render markers into parent buffer */
983     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
984         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
985         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
986     }
988     return item->state;
992 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
993 // reenable this when the bug is fixed and remove the rest of this function
994 static guint
995 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
997     NRArenaShape *shape = NR_ARENA_SHAPE(item);
998     if (!shape->curve) return item->state;
1000         cairo_t *ct = nr_create_cairo_context (area, pb);
1002         if (!ct) 
1003             return item->state;
1005         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1007         cairo_new_path(ct);
1009         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1011         cairo_fill(ct);
1013         cairo_surface_t *cst = cairo_get_target(ct);
1014         cairo_destroy (ct);
1015         cairo_surface_finish (cst);
1016         cairo_surface_destroy (cst);
1018         pb->empty = FALSE;
1020         return item->state;
1024 static guint
1025 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1027     //return cairo_arena_shape_clip(item, area, pb);
1029     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1030     if (!shape->curve) return item->state;
1032     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1033         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1034             NRGC   tempGC(NULL);
1035             tempGC.transform=shape->ctm;
1036             shape->delayed_shp = false;
1037             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1038         } else {
1039             return item->state;
1040         }
1041     }
1043     if ( shape->fill_shp ) {
1044         NRPixBlock m;
1046         /* fixme: We can OR in one step (Lauris) */
1047         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1049         // if memory allocation failed, abort 
1050         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1051             nr_pixblock_release (&m);
1052             return (item->state);
1053         }
1055         m.visible_area = pb->visible_area; 
1056         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1058         for (int y = area->y0; y < area->y1; y++) {
1059             unsigned char *s, *d;
1060             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1061             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1062             for (int x = area->x0; x < area->x1; x++) {
1063                 *d = NR_COMPOSEA_111(*s, *d);
1064                 d ++;
1065                 s ++;
1066             }
1067         }
1068         nr_pixblock_release(&m);
1069         pb->empty = FALSE;
1070     }
1072     return item->state;
1075 static NRArenaItem *
1076 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1078     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1080     if (shape->repick_after > 0)
1081         shape->repick_after--;
1083     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1084         return shape->last_pick;
1086     if (!shape->curve) return NULL;
1087     if (!shape->style) return NULL;
1088     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1089         return NULL;
1091     GTimeVal tstart, tfinish;
1092     g_get_current_time (&tstart);
1094     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1096     double width;
1097     if (outline) {
1098         width = 0.5;
1099     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1100         float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1101         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1102     } else {
1103         width = 0;
1104     }
1106     NRBPath bp;
1107     bp.path = SP_CURVE_BPATH(shape->curve);
1108     double dist = NR_HUGE;
1109     int wind = 0;
1110     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1111              && shape->_fill.opacity > 1e-3 && !outline);
1113     if (item->arena->canvasarena) {
1114         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1115         viewbox.growBy (width);
1116         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1117     } else {
1118         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1119     }
1121     g_get_current_time (&tfinish);
1122     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1123     //g_print ("pick time %lu\n", this_pick);
1125     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1126         shape->repick_after = this_pick / 5000;
1127     }
1129     // covered by fill?
1130     if (needfill) {
1131         if (!shape->style->fill_rule.computed) {
1132             if (wind != 0) {
1133                 shape->last_pick = item;
1134                 return item;
1135             }
1136         } else {
1137             if (wind & 0x1) {
1138                 shape->last_pick = item;
1139                 return item;
1140             }
1141         }
1142     }
1144     // close to the edge, as defined by strokewidth and delta?
1145     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1146     if (needfill || width > 0) { // if either fill or stroke visible,
1147         if ((dist - width) < delta) {
1148             shape->last_pick = item;
1149             return item;
1150         }
1151     }
1153     // if not picked on the shape itself, try its markers
1154     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1155         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1156         if (ret) {
1157             shape->last_pick = item;
1158             return item;
1159         }
1160     }
1162     shape->last_pick = NULL;
1163     return NULL;
1166 /**
1167  *
1168  *  Requests a render of the shape, then if the shape is already a curve it
1169  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1170  *  curve and adds it to the shape.  Finally, it requests an update of the
1171  *  arena for the shape.
1172  */
1173 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1175     g_return_if_fail(shape != NULL);
1176     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1178     if ( justTrans == false ) {
1179         // dirty cached versions
1180         if ( shape->cached_fill ) {
1181             delete shape->cached_fill;
1182             shape->cached_fill=NULL;
1183         }
1184         if ( shape->cached_stroke ) {
1185             delete shape->cached_stroke;
1186             shape->cached_stroke=NULL;
1187         }
1188     }
1190     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1192     if (shape->curve) {
1193         sp_curve_unref(shape->curve);
1194         shape->curve = NULL;
1195     }
1197     if (curve) {
1198         shape->curve = curve;
1199         sp_curve_ref(curve);
1200     }
1202     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1205 void NRArenaShape::setFill(SPPaintServer *server) {
1206     _fill.paint.set(server);
1207     _invalidateCachedFill();
1210 void NRArenaShape::setFill(SPColor const &color) {
1211     _fill.paint.set(color);
1212     _invalidateCachedFill();
1215 void NRArenaShape::setFillOpacity(double opacity) {
1216     _fill.opacity = opacity;
1217     _invalidateCachedFill();
1220 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1221     _fill.rule = rule;
1222     _invalidateCachedFill();
1225 void NRArenaShape::setStroke(SPPaintServer *server) {
1226     _stroke.paint.set(server);
1227     _invalidateCachedStroke();
1230 void NRArenaShape::setStroke(SPColor const &color) {
1231     _stroke.paint.set(color);
1232     _invalidateCachedStroke();
1235 void NRArenaShape::setStrokeOpacity(double opacity) {
1236     _stroke.opacity = opacity;
1237     _invalidateCachedStroke();
1240 void NRArenaShape::setStrokeWidth(double width) {
1241     _stroke.width = width;
1242     _invalidateCachedStroke();
1245 void NRArenaShape::setMitreLimit(double limit) {
1246     _stroke.mitre_limit = limit;
1247     _invalidateCachedStroke();
1250 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1251     _stroke.cap = cap;
1252     _invalidateCachedStroke();
1255 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1256     _stroke.join = join;
1257     _invalidateCachedStroke();
1260 /** nr_arena_shape_set_style
1261  *
1262  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1263  */
1264 void
1265 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1267     g_return_if_fail(shape != NULL);
1268     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1270     if (style) sp_style_ref(style);
1271     if (shape->style) sp_style_unref(shape->style);
1272     shape->style = style;
1274     switch (style->fill.type) {
1275         case SP_PAINT_TYPE_NONE: {
1276             shape->setFill(NULL);
1277             break;
1278         }
1279         case SP_PAINT_TYPE_COLOR: {
1280             shape->setFill(style->fill.value.color);
1281             break;
1282         }
1283         case SP_PAINT_TYPE_PAINTSERVER: {
1284             shape->setFill(style->fill.value.paint.server);
1285             break;
1286         }
1287         default: {
1288             g_assert_not_reached();
1289         }
1290     }
1291     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1292     switch (style->fill_rule.computed) {
1293         case SP_WIND_RULE_EVENODD: {
1294             shape->setFillRule(NRArenaShape::EVEN_ODD);
1295             break;
1296         }
1297         case SP_WIND_RULE_NONZERO: {
1298             shape->setFillRule(NRArenaShape::NONZERO);
1299             break;
1300         }
1301         default: {
1302             g_assert_not_reached();
1303         }
1304     }
1306     switch (style->stroke.type) {
1307         case SP_PAINT_TYPE_NONE: {
1308             shape->setStroke(NULL);
1309             break;
1310         }
1311         case SP_PAINT_TYPE_COLOR: {
1312             shape->setStroke(style->stroke.value.color);
1313             break;
1314         }
1315         case SP_PAINT_TYPE_PAINTSERVER: {
1316             shape->setStroke(style->stroke.value.paint.server);
1317             break;
1318         }
1319         default: {
1320             g_assert_not_reached();
1321         }
1322     }
1323     shape->setStrokeWidth(style->stroke_width.computed);
1324     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1325     switch (style->stroke_linecap.computed) {
1326         case SP_STROKE_LINECAP_ROUND: {
1327             shape->setLineCap(NRArenaShape::ROUND_CAP);
1328             break;
1329         }
1330         case SP_STROKE_LINECAP_SQUARE: {
1331             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1332             break;
1333         }
1334         case SP_STROKE_LINECAP_BUTT: {
1335             shape->setLineCap(NRArenaShape::BUTT_CAP);
1336             break;
1337         }
1338         default: {
1339             g_assert_not_reached();
1340         }
1341     }
1342     switch (style->stroke_linejoin.computed) {
1343         case SP_STROKE_LINEJOIN_ROUND: {
1344             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1345             break;
1346         }
1347         case SP_STROKE_LINEJOIN_BEVEL: {
1348             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1349             break;
1350         }
1351         case SP_STROKE_LINEJOIN_MITER: {
1352             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1353             break;
1354         }
1355         default: {
1356             g_assert_not_reached();
1357         }
1358     }
1359     shape->setMitreLimit(style->stroke_miterlimit.value);
1361     //if shape has a filter
1362     if (style->filter.set && style->filter.filter) {
1363         if (!shape->filter) {
1364             int primitives = sp_filter_primitive_count(style->filter.filter);
1365             shape->filter = new NR::Filter(primitives);
1366         }
1367         sp_filter_build_renderer(style->filter.filter, shape->filter);
1368     } else {
1369         //no filter set for this shape
1370         delete shape->filter;
1371         shape->filter = NULL;
1372     }
1374     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1377 void
1378 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1380     g_return_if_fail(shape != NULL);
1381     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1382     g_return_if_fail(pbox != NULL);
1384     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1385         shape->paintbox = *pbox;
1386     } else {
1387         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1388         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1389         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1390     }
1392     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1395 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1397     paintbox.x0 = pbox.min()[NR::X];
1398     paintbox.y0 = pbox.min()[NR::Y];
1399     paintbox.x1 = pbox.max()[NR::X];
1400     paintbox.y1 = pbox.max()[NR::Y];
1402     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1405 static void
1406 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1408     if ( st >= en ) return;
1409     if ( vst < 0 ) vst=0;
1410     if ( vst > 1 ) vst=1;
1411     if ( ven < 0 ) ven=0;
1412     if ( ven > 1 ) ven=1;
1413     float   sv=vst;
1414     float   dv=ven-vst;
1415     int     len=en-st;
1416     unsigned char*   d=(unsigned char*)dest.buffer;
1417     d+=(st-dest.startPix);
1418     if ( fabs(dv) < 0.001 ) {
1419         if ( vst > 0.999 ) {
1420             /* Simple copy */
1421             while (len > 0) {
1422                 d[0] = 255;
1423                 d += 1;
1424                 len -= 1;
1425             }
1426         } else {
1427             sv*=256;
1428             unsigned int c0_24=(int)sv;
1429             c0_24&=0xFF;
1430             while (len > 0) {
1431                 /* Draw */
1432                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1433                 d += 1;
1434                 len -= 1;
1435             }
1436         }
1437     } else {
1438         if ( en <= st+1 ) {
1439             sv=0.5*(vst+ven);
1440             sv*=256;
1441             unsigned int c0_24=(int)sv;
1442             c0_24&=0xFF;
1443             /* Draw */
1444             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1445         } else {
1446             dv/=len;
1447             sv+=0.5*dv; // correction trapezoidale
1448             sv*=16777216;
1449             dv*=16777216;
1450             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1451             int s0_24 = static_cast<int>(dv);
1452             while (len > 0) {
1453                 unsigned int ca;
1454                 /* Draw */
1455                 ca = c0_24 >> 16;
1456                 if ( ca > 255 ) ca=255;
1457                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1458                 d += 1;
1459                 c0_24 += s0_24;
1460                 c0_24 = CLAMP(c0_24, 0, 16777216);
1461                 len -= 1;
1462             }
1463         }
1464     }
1467 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1469     theS->CalcBBox();
1470     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1471     int    il,ir,it,ib;
1472     il=(int)floor(l);
1473     ir=(int)ceil(r);
1474     it=(int)floor(t);
1475     ib=(int)ceil(b);
1477     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1478     if ( il < m.area.x0 ) il=m.area.x0;
1479     if ( it < m.area.y0 ) it=m.area.y0;
1480     if ( ir > m.area.x1 ) ir=m.area.x1;
1481     if ( ib > m.area.y1 ) ib=m.area.y1;
1483     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1484     int    curPt;
1485     float  curY;
1486     theS->BeginQuickRaster(curY, curPt);
1488     FloatLigne *theI = new FloatLigne();
1489     IntLigne *theIL = new IntLigne();
1491     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1493     char *mdata = (char*)m.data.px;
1494     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1495     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1496     for (int y = it; y < ib; y++) {
1497         theI->Reset();
1498         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1499         theI->Flatten();
1500         theIL->Copy(theI);
1502         raster_info  dest;
1503         dest.startPix=il;
1504         dest.endPix=ir;
1505         dest.sth=il;
1506         dest.stv=y;
1507         dest.buffer=ligStart;
1508         theIL->Raster(dest, NULL, shape_run_A8_OR);
1509         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1510     }
1511     theS->EndQuickRaster();
1512     delete theI;
1513     delete theIL;
1517 /*
1518   Local Variables:
1519   mode:c++
1520   c-file-style:"stroustrup"
1521   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1522   indent-tabs-mode:nil
1523   fill-column:99
1524   End:
1525 */
1526 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :