Code

Fixed NPE
[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 "display/curve.h"
21 #include <libnr/n-art-bpath.h>
22 #include <libnr/nr-path.h>
23 #include <libnr/nr-pixops.h>
24 #include <libnr/nr-matrix-ops.h>
25 #include <libnr/nr-matrix-fns.h>
26 #include <libnr/nr-blit.h>
27 #include <libnr/nr-convert2geom.h>
28 #include <2geom/pathvector.h>
29 #include <livarot/Path.h>
30 #include <livarot/float-line.h>
31 #include <livarot/int-line.h>
32 #include <style.h>
33 #include "prefs-utils.h"
34 #include "inkscape-cairo.h"
36 #include "sp-filter.h"
37 #include "sp-filter-reference.h"
38 #include "display/nr-filter.h"
40 #include <cairo.h>
42 //int  showRuns=0;
43 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
45 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
46 static void nr_arena_shape_init(NRArenaShape *shape);
47 static void nr_arena_shape_finalize(NRObject *object);
49 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
50 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
51 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
52 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
54 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
55 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
56 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
57 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
59 static NRArenaItemClass *shape_parent_class;
61 NRType
62 nr_arena_shape_get_type(void)
63 {
64     static NRType type = 0;
65     if (!type) {
66         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
67                                        "NRArenaShape",
68                                        sizeof(NRArenaShapeClass),
69                                        sizeof(NRArenaShape),
70                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
71                                        (void (*)(NRObject *)) nr_arena_shape_init);
72     }
73     return type;
74 }
76 static void
77 nr_arena_shape_class_init(NRArenaShapeClass *klass)
78 {
79     NRObjectClass *object_class;
80     NRArenaItemClass *item_class;
82     object_class = (NRObjectClass *) klass;
83     item_class = (NRArenaItemClass *) klass;
85     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
87     object_class->finalize = nr_arena_shape_finalize;
88     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
90     item_class->children = nr_arena_shape_children;
91     item_class->add_child = nr_arena_shape_add_child;
92     item_class->set_child_position = nr_arena_shape_set_child_position;
93     item_class->remove_child = nr_arena_shape_remove_child;
94     item_class->update = nr_arena_shape_update;
95     item_class->render = nr_arena_shape_render;
96     item_class->clip = nr_arena_shape_clip;
97     item_class->pick = nr_arena_shape_pick;
98 }
100 /**
101  * Initializes the arena shape, setting all parameters to null, 0, false,
102  * or other defaults
103  */
104 static void
105 nr_arena_shape_init(NRArenaShape *shape)
107     shape->curve = NULL;
108     shape->style = NULL;
109     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
110     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
112     shape->ctm.set_identity();
113     shape->fill_painter = NULL;
114     shape->stroke_painter = NULL;
115     shape->cached_fill = NULL;
116     shape->cached_stroke = NULL;
117     shape->cached_fpartialy = false;
118     shape->cached_spartialy = false;
119     shape->fill_shp = NULL;
120     shape->stroke_shp = NULL;
122     shape->delayed_shp = false;
124     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
125     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
126     shape->cached_fctm.set_identity();
127     shape->cached_sctm.set_identity();
129     shape->markers = NULL;
131     shape->last_pick = NULL;
132     shape->repick_after = 0;
135 static void
136 nr_arena_shape_finalize(NRObject *object)
138     NRArenaShape *shape = (NRArenaShape *) object;
140     if (shape->fill_shp) delete shape->fill_shp;
141     if (shape->stroke_shp) delete shape->stroke_shp;
142     if (shape->cached_fill) delete shape->cached_fill;
143     if (shape->cached_stroke) delete shape->cached_stroke;
144     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
145     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
147     if (shape->style) sp_style_unref(shape->style);
148     if (shape->curve) shape->curve->unref();
150     ((NRObjectClass *) shape_parent_class)->finalize(object);
153 /**
154  * Retrieves the markers from the item
155  */
156 static NRArenaItem *
157 nr_arena_shape_children(NRArenaItem *item)
159     NRArenaShape *shape = (NRArenaShape *) item;
161     return shape->markers;
164 /**
165  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
166  * the prev and next items.  If ref is NULL, then it sets the item's markers
167  * as the next items.
168  */
169 static void
170 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
172     NRArenaShape *shape = (NRArenaShape *) item;
174     if (!ref) {
175         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
176     } else {
177         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
178     }
180     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
183 /**
184  * Removes child from the shape.  If there are no prev items in 
185  * the child, it sets items' markers to the next item in the child.
186  */
187 static void
188 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
190     NRArenaShape *shape = (NRArenaShape *) item;
192     if (child->prev) {
193         nr_arena_item_detach(item, child);
194     } else {
195         shape->markers = nr_arena_item_detach(item, child);
196     }
198     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
201 /**
202  * Detaches child from item, and if there are no previous items in child, it 
203  * sets item's markers to the child.  It then attaches the child back onto the item.
204  * If ref is null, it sets the markers to be the next item, otherwise it uses
205  * the next/prev items in ref.
206  */
207 static void
208 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
210     NRArenaShape *shape = (NRArenaShape *) item;
212     if (child->prev) {
213         nr_arena_item_detach(item, child);
214     } else {
215         shape->markers = nr_arena_item_detach(item, child);
216     }
218     if (!ref) {
219         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
220     } else {
221         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
222     }
224     nr_arena_item_request_render(child);
227 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
228 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
229 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox);
231 /**
232  * Updates the arena shape 'item' and all of its children, including the markers.
233  */
234 static guint
235 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
237     Geom::Rect boundingbox;
239     NRArenaShape *shape = NR_ARENA_SHAPE(item);
241     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
243     unsigned int newstate;
244     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
245         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
246         beststate = beststate & newstate;
247     }
249     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
250         /* We do not have to create rendering structures */
251         shape->ctm = gc->transform;
252         if (state & NR_ARENA_ITEM_STATE_BBOX) {
253             if (shape->curve) {
254                 Geom::PathVector pv = shape->curve->get_pathvector() * to_2geom(gc->transform);
255                 boundingbox = bounds_exact(pv);
256                 item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
257                 item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
258                 item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
259                 item->bbox.y1 = (gint32)(boundingbox[1][1] + 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     boundingbox[0][0] = boundingbox[1][0] = NR_HUGE;
273     boundingbox[0][1] = boundingbox[1][1] = -NR_HUGE;
275     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
277     if (shape->curve) {
278         Geom::PathVector pv = shape->curve->get_pathvector() * to_2geom(gc->transform);
279         boundingbox = bounds_exact(pv);
281         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
282             float width, scale;
283             scale = NR::expansion(gc->transform);
284             width = MAX(0.125, shape->_stroke.width * scale);
285             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
286                 boundingbox.expandBy(width);
287             }
288             // those pesky miters, now
289             float miterMax=width*shape->_stroke.mitre_limit;
290             if ( miterMax > 0.01 ) {
291                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
292                 boundingbox.expandBy(miterMax);
293             }
294         }
295     } else {
296     }
297     shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
298     shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
299     shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
300     shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
301     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
303     /* Release state data */
304     if (TRUE || !NR::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
305         /* Concept test */
306         if (shape->fill_shp) {
307             delete shape->fill_shp;
308             shape->fill_shp = NULL;
309         }
310     }
311     if (shape->stroke_shp) {
312         delete shape->stroke_shp;
313         shape->stroke_shp = NULL;
314     }
315     if (shape->fill_painter) {
316         sp_painter_free(shape->fill_painter);
317         shape->fill_painter = NULL;
318     }
319     if (shape->stroke_painter) {
320         sp_painter_free(shape->stroke_painter);
321         shape->stroke_painter = NULL;
322     }
324     if (!shape->curve || 
325         !shape->style ||
326         shape->curve->is_empty() ||
327         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
328          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
329     {
330         item->bbox = shape->approx_bbox;
331         return NR_ARENA_ITEM_STATE_ALL;
332     }
334     /* Build state data */
335     if ( shape->delayed_shp ) {
336         item->bbox=shape->approx_bbox;
337     } else {
338         nr_arena_shape_update_stroke(shape, gc, area);
339         nr_arena_shape_update_fill(shape, gc, area);
341         boundingbox[0][0] = boundingbox[0][1] = boundingbox[1][0] = boundingbox[1][1] = 0.0;
342         nr_arena_shape_add_bboxes(shape, boundingbox);
344         shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
345         shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
346         shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
347         shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
348     }
350     if (boundingbox.isEmpty())
351         return NR_ARENA_ITEM_STATE_ALL;
353     item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
354     item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
355     item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.0F);
356     item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.0F);
357     nr_arena_request_render_rect(item->arena, &item->bbox);
359     item->render_opacity = TRUE;
360     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
361         if (gc && gc->parent) {
362             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
363                                                               gc->transform, gc->parent->transform,
364                                                               &shape->paintbox);
365         }
366         item->render_opacity = FALSE;
367     }
368     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
369         if (gc && gc->parent) {
370             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
371                                                                 gc->transform, gc->parent->transform,
372                                                                 &shape->paintbox);
373         }
374         item->render_opacity = FALSE;
375     }
376     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
377            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
378           || (shape->markers)
379         )
380     {
381         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
382         item->render_opacity = FALSE;
383     }
385     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
386         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
387             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
388         }
389     }
391     return NR_ARENA_ITEM_STATE_ALL;
394 int matrix_is_isometry(NR::Matrix p) {
395     NR::Matrix   tp;
396     // transposition
397     tp[0]=p[0];
398     tp[1]=p[2];
399     tp[2]=p[1];
400     tp[3]=p[3];
401     for (int i = 4; i < 6; i++) // shut valgrind up :)
402         tp[i] = p[i] = 0;
403     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
404     // Is the adjunct nearly an identity function?
405     if (isom.is_translation(0.01)) {
406         // the transformation is an isometry -> no need to recompute
407         // the uncrossed polygon
408         if ( p.det() < 0 )
409             return -1;
410         else
411             return 1;
412     }
413     return 0;
416 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
417     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
420 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
421 void
422 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
424     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
425         ((shape->curve->get_length() > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
426         if (TRUE || !shape->fill_shp) {
427             NR::Matrix  cached_to_new = NR::identity();
428             int isometry = 0;
429             if ( shape->cached_fill ) {
430                 if (shape->cached_fctm == gc->transform) {
431                     isometry = 2; // identity
432                 } else {
433                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
434                     isometry = matrix_is_isometry(cached_to_new);
435                 }
436                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
437                     isometry = 0;
438             }
439             if ( isometry == 0 ) {
440                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
441                 shape->cached_fill->Reset();
443                 Path*  thePath=new Path;
444                 Shape* theShape=new Shape;
445                 {
446                     NR::Matrix   tempMat(gc->transform);
447                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
448                 }
450                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
451                     thePath->Convert(1.0);
452                     shape->cached_fpartialy = false;
453                 } else {
454                     thePath->Convert(area, 1.0);
455                     shape->cached_fpartialy = true;
456                 }
458                 thePath->Fill(theShape, 0);
460                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
461                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
462                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
463                     //shape->cached_fill->Copy(theShape);
464                 } else {
465                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
466                 }
467                 shape->cached_fctm=gc->transform;
468                 shape->cached_farea = *area;
469                 delete theShape;
470                 delete thePath;
471                 if ( shape->fill_shp == NULL )
472                     shape->fill_shp = new Shape;
474                 shape->fill_shp->Copy(shape->cached_fill);
476             } else if ( 2 == isometry ) {
477                 if ( shape->fill_shp == NULL ) {
478                     shape->fill_shp = new Shape;
479                     shape->fill_shp->Copy(shape->cached_fill);
480                 }
481             } else {
483                 if ( shape->fill_shp == NULL )
484                     shape->fill_shp = new Shape;
486                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
487                                        shape->cached_fill->numberOfEdges());
488                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
489                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
490                 if ( isometry == 1 ) {
491                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
492                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
493                                                  shape->cached_fill->getEdge(i).en);
494                 } else if ( isometry == -1 ) { // need to flip poly.
495                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
496                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
497                                                  shape->cached_fill->getEdge(i).st);
498                 }
499                 shape->fill_shp->ForceToPolygon();
500                 shape->fill_shp->needPointsSorting();
501                 shape->fill_shp->needEdgesSorting();
502             }
503             shape->delayed_shp |= shape->cached_fpartialy;
504         }
505     }
508 void
509 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
511     SPStyle* style = shape->style;
513     float const scale = NR::expansion(gc->transform);
515     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
517     if (outline) {
518         // cairo does not need the livarot path for rendering
519         return; 
520     }
522     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
523     // (since it will only be used for picking, not for rendering)
525     if (outline ||
526         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
527          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
529         float style_width = MAX(0.125, shape->_stroke.width * scale);
530         float width;
531         if (outline) {
532             width = 0.5; // 1 pixel wide, independent of zoom
533         } else {
534             width = style_width;
535         }
537         NR::Matrix  cached_to_new = NR::identity();
539         int isometry = 0;
540         if ( shape->cached_stroke ) {
541             if (shape->cached_sctm == gc->transform) {
542                 isometry = 2; // identity
543             } else {
544                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
545                 isometry = matrix_is_isometry(cached_to_new);
546             }
547             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
548                 isometry = 0;
549             if (0 != isometry && width != shape->cached_width) { 
550                 // if this happens without setting style, we have just switched to outline or back
551                 isometry = 0; 
552             } 
553         }
555         if ( isometry == 0 ) {
556             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
557             shape->cached_stroke->Reset();
558             Path*  thePath = new Path;
559             Shape* theShape = new Shape;
560             {
561                 NR::Matrix   tempMat(gc->transform);
562                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
563             }
565             // add some padding to the rendering area, so clipped path does not go into a render area
566             NRRectL padded_area = *area;
567             padded_area.x0 -= (NR::ICoord)width;
568             padded_area.x1 += (NR::ICoord)width;
569             padded_area.y0 -= (NR::ICoord)width;
570             padded_area.y1 += (NR::ICoord)width;
571             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
572                 thePath->Convert((outline) ? 4.0 : 1.0);
573                 shape->cached_spartialy = false;
574             }
575             else {
576                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
577                 shape->cached_spartialy = true;
578             }
580             if (style->stroke_dash.n_dash && !outline) {
581                 thePath->DashPolylineFromStyle(style, scale, 1.0);
582             }
584             ButtType butt=butt_straight;
585             switch (shape->_stroke.cap) {
586                 case NRArenaShape::BUTT_CAP:
587                     butt = butt_straight;
588                     break;
589                 case NRArenaShape::ROUND_CAP:
590                     butt = butt_round;
591                     break;
592                 case NRArenaShape::SQUARE_CAP:
593                     butt = butt_square;
594                     break;
595             }
596             JoinType join=join_straight;
597             switch (shape->_stroke.join) {
598                 case NRArenaShape::MITRE_JOIN:
599                     join = join_pointy;
600                     break;
601                 case NRArenaShape::ROUND_JOIN:
602                     join = join_round;
603                     break;
604                 case NRArenaShape::BEVEL_JOIN:
605                     join = join_straight;
606                     break;
607             }
609             if (outline) {
610                 butt = butt_straight;
611                 join = join_straight;
612             }
614             thePath->Stroke(theShape, false, 0.5*width, join, butt,
615                             0.5*width*shape->_stroke.mitre_limit);
618             if (outline) {
619                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
620                 shape->cached_stroke->Copy(theShape);
621             } else {
622                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
623             }
625             shape->cached_width = width;
627             shape->cached_sctm=gc->transform;
628             shape->cached_sarea = *area;
629             delete thePath;
630             delete theShape;
631             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
633             shape->stroke_shp->Copy(shape->cached_stroke);
635         } else if ( 2 == isometry ) {
636             if ( shape->stroke_shp == NULL ) {
637                 shape->stroke_shp=new Shape;
638                 shape->stroke_shp->Copy(shape->cached_stroke);
639             }
640         } else {
641             if ( shape->stroke_shp == NULL )
642                 shape->stroke_shp=new Shape;
643             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
644             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
645                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
646             if ( isometry == 1 ) {
647                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
648                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
649                                                shape->cached_stroke->getEdge(i).en);
650             } else if ( isometry == -1 ) {
651                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
652                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
653                                                shape->cached_stroke->getEdge(i).st);
654             }
655             shape->stroke_shp->ForceToPolygon();
656             shape->stroke_shp->needPointsSorting();
657             shape->stroke_shp->needEdgesSorting();
658         }
659         shape->delayed_shp |= shape->cached_spartialy;
660     }
664 void
665 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox)
667     if ( shape->stroke_shp ) {
668         shape->stroke_shp->CalcBBox();
669         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
670         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
671         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
672         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
673         Geom::Rect stroke_bbox( Geom::Interval(shape->stroke_shp->leftX, shape->stroke_shp->rightX),
674                                 Geom::Interval(shape->stroke_shp->topY,  shape->stroke_shp->bottomY) );
675         bbox.unionWith(stroke_bbox);
676     }
678     if ( shape->fill_shp ) {
679         shape->fill_shp->CalcBBox();
680         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
681         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
682         shape->fill_shp->topY=floor(shape->fill_shp->topY);
683         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
684         Shape *larger = shape->stroke_shp ? shape->stroke_shp : shape->fill_shp;
685         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
686                               Geom::Interval(larger->topY,  larger->bottomY) );
687         bbox.unionWith(fill_bbox);
688     }
691 // cairo outline rendering:
692 static unsigned int
693 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
695     NRArenaShape *shape = NR_ARENA_SHAPE(item);
697     if (!ct) 
698         return item->state;
700     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
701     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
702     // by setting color channels in the "wrong" order
703     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));
705     cairo_set_line_width(ct, 0.5);
706     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
707     cairo_new_path(ct);
709     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area, true, 0);
711     cairo_stroke(ct);
713     return item->state;
716 // cairo stroke rendering (flat color only so far!):
717 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
718 // (need to switch them to premul before this can be enabled)
719 void
720 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
722     NRArenaShape *shape = NR_ARENA_SHAPE(item);
723     SPStyle const *style = shape->style;
725     float const scale = NR::expansion(shape->ctm);
727     if (fabs(shape->_stroke.width * scale) < 0.01)
728         return;
730     cairo_t *ct = nr_create_cairo_context (area, pb);
732     if (!ct)
733         return;
735     guint32 rgba;
736     if ( item->render_opacity ) {
737         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
738                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
739     } else {
740         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
741     }
743     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
744     // by setting color channels in the "wrong" order
745     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));
747     float style_width = MAX(0.125, shape->_stroke.width * scale);
748     cairo_set_line_width(ct, style_width);
750     switch (shape->_stroke.cap) {
751         case NRArenaShape::BUTT_CAP:
752             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
753             break;
754         case NRArenaShape::ROUND_CAP:
755             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
756             break;
757         case NRArenaShape::SQUARE_CAP:
758             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
759             break;
760     }
761     switch (shape->_stroke.join) {
762         case NRArenaShape::MITRE_JOIN:
763             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
764             break;
765         case NRArenaShape::ROUND_JOIN:
766             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
767             break;
768         case NRArenaShape::BEVEL_JOIN:
769             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
770             break;
771     }
773     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
775     if (style->stroke_dash.n_dash) {
776         NRVpathDash dash;
777         dash.offset = style->stroke_dash.offset * scale;
778         dash.n_dash = style->stroke_dash.n_dash;
779         dash.dash = g_new(double, dash.n_dash);
780         for (int i = 0; i < dash.n_dash; i++) {
781             dash.dash[i] = style->stroke_dash.dash[i] * scale;
782         }
783         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
784         g_free(dash.dash);
785     }
787     cairo_set_tolerance(ct, 0.1);
788     cairo_new_path(ct);
790     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area->upgrade(), true, style_width);
792     cairo_stroke(ct);
794     cairo_surface_t *cst = cairo_get_target(ct);
795     cairo_destroy (ct);
796     cairo_surface_finish (cst);
797     cairo_surface_destroy (cst);
799     pb->empty = FALSE;
803 /**
804  * Renders the item.  Markers are just composed into the parent buffer.
805  */
806 static unsigned int
807 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
809     NRArenaShape *shape = NR_ARENA_SHAPE(item);
811     if (!shape->curve) return item->state;
812     if (!shape->style) return item->state;
814     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
816     if (outline) { // cairo outline rendering
818         pb->empty = FALSE;
819         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
820         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
822     } else {
824     if ( shape->delayed_shp ) {
825         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
826             NRGC   tempGC(NULL);
827             tempGC.transform=shape->ctm;
828             shape->delayed_shp = false;
829             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
830             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
831 /*      NRRect bbox;
832         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
833         nr_arena_shape_add_bboxes(shape,bbox);
834         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
835         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
836         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
837         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
838         shape->approx_bbox=item->bbox;*/
839         } else {
840             return item->state;
841         }
842     }
844     SPStyle const *style = shape->style;
845     if (shape->fill_shp) {
846         NRPixBlock m;
847         guint32 rgba;
849         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
851         // if memory allocation failed, abort render
852         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
853             nr_pixblock_release (&m);
854             return (item->state);
855         }
857         m.visible_area = pb->visible_area;
858         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
859         m.empty = FALSE;
861         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
862             // do not render fill in any way
863         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
864             if ( item->render_opacity ) {
865                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
866                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
867             } else {
868                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
869             }
870             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
871             pb->empty = FALSE;
872         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
873             if (shape->fill_painter) {
874                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
875             }
876         }
878         nr_pixblock_release(&m);
879     }
881     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
883         // cairo_arena_shape_render_stroke(item, area, pb);
885         guint32 rgba;
886         NRPixBlock m;
888         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
890         // if memory allocation failed, abort render
891         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
892             nr_pixblock_release (&m);
893             return (item->state);
894         }
896         m.visible_area = pb->visible_area;
897         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
898         m.empty = FALSE;
900             if ( item->render_opacity ) {
901                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
902                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
903             } else {
904                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
905             }
906             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
907             pb->empty = FALSE;
909         nr_pixblock_release(&m);
911     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
913         NRPixBlock m;
915         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
917         // if memory allocation failed, abort render
918         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
919             nr_pixblock_release (&m);
920             return (item->state);
921         }
923         m.visible_area = pb->visible_area; 
924         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
925         m.empty = FALSE;
927         if (shape->stroke_painter) {
928             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
929         }
931         nr_pixblock_release(&m);
932     }
934     } // non-cairo non-outline branch
936     /* Render markers into parent buffer */
937     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
938         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
939         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
940     }
942     return item->state;
946 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
947 // reenable this when the bug is fixed and remove the rest of this function
948 // TODO
949 #if defined(DEADCODE) && !defined(DEADCODE)
950 static guint
951 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
953     NRArenaShape *shape = NR_ARENA_SHAPE(item);
954     if (!shape->curve) return item->state;
956         cairo_t *ct = nr_create_cairo_context (area, pb);
958         if (!ct)
959             return item->state;
961         cairo_set_source_rgba(ct, 0, 0, 0, 1);
963         cairo_new_path(ct);
965         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), (area)->upgrade(), false, 0);
967         cairo_fill(ct);
969         cairo_surface_t *cst = cairo_get_target(ct);
970         cairo_destroy (ct);
971         cairo_surface_finish (cst);
972         cairo_surface_destroy (cst);
974         pb->empty = FALSE;
976         return item->state;
978 #endif //defined(DEADCODE) && !defined(DEADCODE)
981 static guint
982 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
984     //return cairo_arena_shape_clip(item, area, pb);
986     NRArenaShape *shape = NR_ARENA_SHAPE(item);
987     if (!shape->curve) return item->state;
989     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
990         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
991             NRGC   tempGC(NULL);
992             tempGC.transform=shape->ctm;
993             shape->delayed_shp = false;
994             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
995         } else {
996             return item->state;
997         }
998     }
1000     if ( shape->fill_shp ) {
1001         NRPixBlock m;
1003         /* fixme: We can OR in one step (Lauris) */
1004         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1006         // if memory allocation failed, abort 
1007         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1008             nr_pixblock_release (&m);
1009             return (item->state);
1010         }
1012         m.visible_area = pb->visible_area; 
1013         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1015         for (int y = area->y0; y < area->y1; y++) {
1016             unsigned char *s, *d;
1017             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1018             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1019             for (int x = area->x0; x < area->x1; x++) {
1020                 *d = NR_COMPOSEA_111(*s, *d);
1021                 d ++;
1022                 s ++;
1023             }
1024         }
1025         nr_pixblock_release(&m);
1026         pb->empty = FALSE;
1027     }
1029     return item->state;
1032 static NRArenaItem *
1033 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1035     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1037     if (shape->repick_after > 0)
1038         shape->repick_after--;
1040     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1041         return shape->last_pick;
1043     if (!shape->curve) return NULL;
1044     if (!shape->style) return NULL;
1045     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1046         return NULL;
1048     GTimeVal tstart, tfinish;
1049     g_get_current_time (&tstart);
1051     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1053     double width;
1054     if (outline) {
1055         width = 0.5;
1056     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1057         float const scale = NR::expansion(shape->ctm);
1058         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1059     } else {
1060         width = 0;
1061     }
1063     const_NRBPath bp;
1064     bp.path = SP_CURVE_BPATH(shape->curve);
1065     double dist = NR_HUGE;
1066     int wind = 0;
1067     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1068              && shape->_fill.opacity > 1e-3 && !outline);
1070     if (item->arena->canvasarena) {
1071         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1072         viewbox.growBy (width);
1073         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1074     } else {
1075         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1076     }
1078     g_get_current_time (&tfinish);
1079     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1080     //g_print ("pick time %lu\n", this_pick);
1082     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1083         shape->repick_after = this_pick / 5000;
1084     }
1086     // covered by fill?
1087     if (needfill) {
1088         if (!shape->style->fill_rule.computed) {
1089             if (wind != 0) {
1090                 shape->last_pick = item;
1091                 return item;
1092             }
1093         } else {
1094             if (wind & 0x1) {
1095                 shape->last_pick = item;
1096                 return item;
1097             }
1098         }
1099     }
1101     // close to the edge, as defined by strokewidth and delta?
1102     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1103     if (needfill || width > 0) { // if either fill or stroke visible,
1104         if ((dist - width) < delta) {
1105             shape->last_pick = item;
1106             return item;
1107         }
1108     }
1110     // if not picked on the shape itself, try its markers
1111     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1112         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1113         if (ret) {
1114             shape->last_pick = item;
1115             return item;
1116         }
1117     }
1119     shape->last_pick = NULL;
1120     return NULL;
1123 /**
1124  *
1125  *  Requests a render of the shape, then if the shape is already a curve it
1126  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1127  *  curve and adds it to the shape.  Finally, it requests an update of the
1128  *  arena for the shape.
1129  */
1130 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1132     g_return_if_fail(shape != NULL);
1133     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1135     if ( justTrans == false ) {
1136         // dirty cached versions
1137         if ( shape->cached_fill ) {
1138             delete shape->cached_fill;
1139             shape->cached_fill=NULL;
1140         }
1141         if ( shape->cached_stroke ) {
1142             delete shape->cached_stroke;
1143             shape->cached_stroke=NULL;
1144         }
1145     }
1147     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1149     if (shape->curve) {
1150         shape->curve->unref();
1151         shape->curve = NULL;
1152     }
1154     if (curve) {
1155         shape->curve = curve;
1156         curve->ref();
1157     }
1159     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1162 void NRArenaShape::setFill(SPPaintServer *server) {
1163     _fill.paint.set(server);
1164     _invalidateCachedFill();
1167 void NRArenaShape::setFill(SPColor const &color) {
1168     _fill.paint.set(color);
1169     _invalidateCachedFill();
1172 void NRArenaShape::setFillOpacity(double opacity) {
1173     _fill.opacity = opacity;
1174     _invalidateCachedFill();
1177 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1178     _fill.rule = rule;
1179     _invalidateCachedFill();
1182 void NRArenaShape::setStroke(SPPaintServer *server) {
1183     _stroke.paint.set(server);
1184     _invalidateCachedStroke();
1187 void NRArenaShape::setStroke(SPColor const &color) {
1188     _stroke.paint.set(color);
1189     _invalidateCachedStroke();
1192 void NRArenaShape::setStrokeOpacity(double opacity) {
1193     _stroke.opacity = opacity;
1194     _invalidateCachedStroke();
1197 void NRArenaShape::setStrokeWidth(double width) {
1198     _stroke.width = width;
1199     _invalidateCachedStroke();
1202 void NRArenaShape::setMitreLimit(double limit) {
1203     _stroke.mitre_limit = limit;
1204     _invalidateCachedStroke();
1207 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1208     _stroke.cap = cap;
1209     _invalidateCachedStroke();
1212 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1213     _stroke.join = join;
1214     _invalidateCachedStroke();
1217 /** nr_arena_shape_set_style
1218  *
1219  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1220  */
1221 void
1222 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1224     g_return_if_fail(shape != NULL);
1225     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1227     if (style) sp_style_ref(style);
1228     if (shape->style) sp_style_unref(shape->style);
1229     shape->style = style;
1231     if ( style->fill.isPaintserver() ) {
1232         shape->setFill(style->getFillPaintServer());
1233     } else if ( style->fill.isColor() ) {
1234         shape->setFill(style->fill.value.color);
1235     } else if ( style->fill.isNone() ) {
1236         shape->setFill(NULL);
1237     } else {
1238         g_assert_not_reached();
1239     }
1240     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1241     switch (style->fill_rule.computed) {
1242         case SP_WIND_RULE_EVENODD: {
1243             shape->setFillRule(NRArenaShape::EVEN_ODD);
1244             break;
1245         }
1246         case SP_WIND_RULE_NONZERO: {
1247             shape->setFillRule(NRArenaShape::NONZERO);
1248             break;
1249         }
1250         default: {
1251             g_assert_not_reached();
1252         }
1253     }
1255     if ( style->stroke.isPaintserver() ) {
1256         shape->setStroke(style->getStrokePaintServer());
1257     } else if ( style->stroke.isColor() ) {
1258         shape->setStroke(style->stroke.value.color);
1259     } else if ( style->stroke.isNone() ) {
1260         shape->setStroke(NULL);
1261     } else {
1262         g_assert_not_reached();
1263     }
1264     shape->setStrokeWidth(style->stroke_width.computed);
1265     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1266     switch (style->stroke_linecap.computed) {
1267         case SP_STROKE_LINECAP_ROUND: {
1268             shape->setLineCap(NRArenaShape::ROUND_CAP);
1269             break;
1270         }
1271         case SP_STROKE_LINECAP_SQUARE: {
1272             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1273             break;
1274         }
1275         case SP_STROKE_LINECAP_BUTT: {
1276             shape->setLineCap(NRArenaShape::BUTT_CAP);
1277             break;
1278         }
1279         default: {
1280             g_assert_not_reached();
1281         }
1282     }
1283     switch (style->stroke_linejoin.computed) {
1284         case SP_STROKE_LINEJOIN_ROUND: {
1285             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1286             break;
1287         }
1288         case SP_STROKE_LINEJOIN_BEVEL: {
1289             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1290             break;
1291         }
1292         case SP_STROKE_LINEJOIN_MITER: {
1293             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1294             break;
1295         }
1296         default: {
1297             g_assert_not_reached();
1298         }
1299     }
1300     shape->setMitreLimit(style->stroke_miterlimit.value);
1302     //if shape has a filter
1303     if (style->filter.set && style->getFilter()) {
1304         if (!shape->filter) {
1305             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1306             shape->filter = new NR::Filter(primitives);
1307         }
1308         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1309     } else {
1310         //no filter set for this shape
1311         delete shape->filter;
1312         shape->filter = NULL;
1313     }
1315     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1318 void
1319 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1321     g_return_if_fail(shape != NULL);
1322     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1323     g_return_if_fail(pbox != NULL);
1325     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1326         shape->paintbox = *pbox;
1327     } else {
1328         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1329         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1330         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1331     }
1333     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1336 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1338     paintbox.x0 = pbox.min()[NR::X];
1339     paintbox.y0 = pbox.min()[NR::Y];
1340     paintbox.x1 = pbox.max()[NR::X];
1341     paintbox.y1 = pbox.max()[NR::Y];
1343     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1346 static void
1347 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1349     if ( st >= en ) return;
1350     if ( vst < 0 ) vst=0;
1351     if ( vst > 1 ) vst=1;
1352     if ( ven < 0 ) ven=0;
1353     if ( ven > 1 ) ven=1;
1354     float   sv=vst;
1355     float   dv=ven-vst;
1356     int     len=en-st;
1357     unsigned char*   d=(unsigned char*)dest.buffer;
1358     d+=(st-dest.startPix);
1359     if ( fabs(dv) < 0.001 ) {
1360         if ( vst > 0.999 ) {
1361             /* Simple copy */
1362             while (len > 0) {
1363                 d[0] = 255;
1364                 d += 1;
1365                 len -= 1;
1366             }
1367         } else {
1368             sv*=256;
1369             unsigned int c0_24=(int)sv;
1370             c0_24&=0xFF;
1371             while (len > 0) {
1372                 /* Draw */
1373                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1374                 d += 1;
1375                 len -= 1;
1376             }
1377         }
1378     } else {
1379         if ( en <= st+1 ) {
1380             sv=0.5*(vst+ven);
1381             sv*=256;
1382             unsigned int c0_24=(int)sv;
1383             c0_24&=0xFF;
1384             /* Draw */
1385             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1386         } else {
1387             dv/=len;
1388             sv+=0.5*dv; // correction trapezoidale
1389             sv*=16777216;
1390             dv*=16777216;
1391             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1392             int s0_24 = static_cast<int>(dv);
1393             while (len > 0) {
1394                 unsigned int ca;
1395                 /* Draw */
1396                 ca = c0_24 >> 16;
1397                 if ( ca > 255 ) ca=255;
1398                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1399                 d += 1;
1400                 c0_24 += s0_24;
1401                 c0_24 = CLAMP(c0_24, 0, 16777216);
1402                 len -= 1;
1403             }
1404         }
1405     }
1408 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1410     theS->CalcBBox();
1411     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1412     int    il,ir,it,ib;
1413     il=(int)floor(l);
1414     ir=(int)ceil(r);
1415     it=(int)floor(t);
1416     ib=(int)ceil(b);
1418     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1419     if ( il < m.area.x0 ) il=m.area.x0;
1420     if ( it < m.area.y0 ) it=m.area.y0;
1421     if ( ir > m.area.x1 ) ir=m.area.x1;
1422     if ( ib > m.area.y1 ) ib=m.area.y1;
1424     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1425     int    curPt;
1426     float  curY;
1427     theS->BeginQuickRaster(curY, curPt);
1429     FloatLigne *theI = new FloatLigne();
1430     IntLigne *theIL = new IntLigne();
1432     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1434     char *mdata = (char*)m.data.px;
1435     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1436     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1437     for (int y = it; y < ib; y++) {
1438         theI->Reset();
1439         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1440         theI->Flatten();
1441         theIL->Copy(theI);
1443         raster_info  dest;
1444         dest.startPix=il;
1445         dest.endPix=ir;
1446         dest.sth=il;
1447         dest.stv=y;
1448         dest.buffer=ligStart;
1449         theIL->Raster(dest, NULL, shape_run_A8_OR);
1450         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1451     }
1452     theS->EndQuickRaster();
1453     delete theI;
1454     delete theIL;
1458 /*
1459   Local Variables:
1460   mode:c++
1461   c-file-style:"stroustrup"
1462   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1463   indent-tabs-mode:nil
1464   fill-column:99
1465   End:
1466 */
1467 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :