Code

Merging from trunk
[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  */
15 #include <2geom/svg-path.h>
16 #include <2geom/svg-path-parser.h>
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/nr-pixops.h>
22 #include <libnr/nr-matrix-ops.h>
23 #include <libnr/nr-matrix-fns.h>
24 #include <libnr/nr-blit.h>
25 #include <libnr/nr-convert2geom.h>
26 #include <2geom/pathvector.h>
27 #include <2geom/curves.h>
28 #include <livarot/Path.h>
29 #include <livarot/float-line.h>
30 #include <livarot/int-line.h>
31 #include <style.h>
32 #include "prefs-utils.h"
33 #include "inkscape-cairo.h"
34 #include "helper/geom.h"
35 #include "helper/geom-curves.h"
36 #include "sp-filter.h"
37 #include "sp-filter-reference.h"
38 #include "display/nr-filter.h"
39 #include <typeinfo>
40 #include <cairo.h>
42 #include <glib.h>
43 #include "svg/svg.h"
44 #include <fenv.h>
46 //int  showRuns=0;
47 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
49 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
50 static void nr_arena_shape_init(NRArenaShape *shape);
51 static void nr_arena_shape_finalize(NRObject *object);
53 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
54 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
55 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
56 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
58 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
59 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
60 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
61 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int sticky);
63 static NRArenaItemClass *shape_parent_class;
65 NRType
66 nr_arena_shape_get_type(void)
67 {
68     static NRType type = 0;
69     if (!type) {
70         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
71                                        "NRArenaShape",
72                                        sizeof(NRArenaShapeClass),
73                                        sizeof(NRArenaShape),
74                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
75                                        (void (*)(NRObject *)) nr_arena_shape_init);
76     }
77     return type;
78 }
80 static void
81 nr_arena_shape_class_init(NRArenaShapeClass *klass)
82 {
83     NRObjectClass *object_class;
84     NRArenaItemClass *item_class;
86     object_class = (NRObjectClass *) klass;
87     item_class = (NRArenaItemClass *) klass;
89     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
91     object_class->finalize = nr_arena_shape_finalize;
92     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
94     item_class->children = nr_arena_shape_children;
95     item_class->add_child = nr_arena_shape_add_child;
96     item_class->set_child_position = nr_arena_shape_set_child_position;
97     item_class->remove_child = nr_arena_shape_remove_child;
98     item_class->update = nr_arena_shape_update;
99     item_class->render = nr_arena_shape_render;
100     item_class->clip = nr_arena_shape_clip;
101     item_class->pick = nr_arena_shape_pick;
104 /**
105  * Initializes the arena shape, setting all parameters to null, 0, false,
106  * or other defaults
107  */
108 static void
109 nr_arena_shape_init(NRArenaShape *shape)
111     shape->curve = NULL;
112     shape->style = NULL;
113     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
114     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
116     shape->ctm.setIdentity();
117     shape->fill_painter = NULL;
118     shape->stroke_painter = NULL;
119     shape->cached_fill = NULL;
120     shape->cached_stroke = NULL;
121     shape->cached_fpartialy = false;
122     shape->cached_spartialy = false;
123     shape->fill_shp = NULL;
124     shape->stroke_shp = NULL;
126     shape->delayed_shp = false;
128     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
129     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
130     shape->cached_fctm.setIdentity();
131     shape->cached_sctm.setIdentity();
133     shape->markers = NULL;
135     shape->last_pick = NULL;
136     shape->repick_after = 0;
139 static void
140 nr_arena_shape_finalize(NRObject *object)
142     NRArenaShape *shape = (NRArenaShape *) object;
144     if (shape->fill_shp) delete shape->fill_shp;
145     if (shape->stroke_shp) delete shape->stroke_shp;
146     if (shape->cached_fill) delete shape->cached_fill;
147     if (shape->cached_stroke) delete shape->cached_stroke;
148     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
149     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
151     if (shape->style) sp_style_unref(shape->style);
152     if (shape->curve) shape->curve->unref();
154     ((NRObjectClass *) shape_parent_class)->finalize(object);
157 /**
158  * Retrieves the markers from the item
159  */
160 static NRArenaItem *
161 nr_arena_shape_children(NRArenaItem *item)
163     NRArenaShape *shape = (NRArenaShape *) item;
165     return shape->markers;
168 /**
169  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
170  * the prev and next items.  If ref is NULL, then it sets the item's markers
171  * as the next items.
172  */
173 static void
174 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
176     NRArenaShape *shape = (NRArenaShape *) item;
178     if (!ref) {
179         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
180     } else {
181         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
182     }
184     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
187 /**
188  * Removes child from the shape.  If there are no prev items in 
189  * the child, it sets items' markers to the next item in the child.
190  */
191 static void
192 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
194     NRArenaShape *shape = (NRArenaShape *) item;
196     if (child->prev) {
197         nr_arena_item_detach(item, child);
198     } else {
199         shape->markers = nr_arena_item_detach(item, child);
200     }
202     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
205 /**
206  * Detaches child from item, and if there are no previous items in child, it 
207  * sets item's markers to the child.  It then attaches the child back onto the item.
208  * If ref is null, it sets the markers to be the next item, otherwise it uses
209  * the next/prev items in ref.
210  */
211 static void
212 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
214     NRArenaShape *shape = (NRArenaShape *) item;
216     if (child->prev) {
217         nr_arena_item_detach(item, child);
218     } else {
219         shape->markers = nr_arena_item_detach(item, child);
220     }
222     if (!ref) {
223         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
224     } else {
225         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
226     }
228     nr_arena_item_request_render(child);
231 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
232 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
233 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox);
235 /**
236  * Updates the arena shape 'item' and all of its children, including the markers.
237  */
238 static guint
239 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
241     Geom::Rect boundingbox;
243     NRArenaShape *shape = NR_ARENA_SHAPE(item);
245     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
247     unsigned int newstate;
248     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
249         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
250         beststate = beststate & newstate;
251     }
253     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
254         /* We do not have to create rendering structures */
255         shape->ctm = gc->transform;
256         if (state & NR_ARENA_ITEM_STATE_BBOX) {
257             if (shape->curve) {
258                 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
259                 item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
260                 item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
261                 item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
262                 item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
263             }
264             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
265                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
266                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
267                 }
268             }
269         }
270         return (state | item->state);
271     }
273     shape->delayed_shp=true;
274     shape->ctm = gc->transform;
275     boundingbox[0][0] = boundingbox[1][0] = NR_HUGE;
276     boundingbox[0][1] = boundingbox[1][1] = -NR_HUGE;
278     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
280     if (shape->curve) {
281         boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
283         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
284             float width, scale;
285             scale = gc->transform.descrim();
286             width = MAX(0.125, shape->_stroke.width * scale);
287             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
288                 boundingbox.expandBy(width);
289             }
290             // those pesky miters, now
291             float miterMax=width*shape->_stroke.mitre_limit;
292             if ( miterMax > 0.01 ) {
293                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
294                 boundingbox.expandBy(miterMax);
295             }
296         }
297     } else {
298     }
299     shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
300     shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
301     shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
302     shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
303     if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
305     /* Release state data */
306     if (TRUE || !Geom::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
307         /* Concept test */
308         if (shape->fill_shp) {
309             delete shape->fill_shp;
310             shape->fill_shp = NULL;
311         }
312     }
313     if (shape->stroke_shp) {
314         delete shape->stroke_shp;
315         shape->stroke_shp = NULL;
316     }
317     if (shape->fill_painter) {
318         sp_painter_free(shape->fill_painter);
319         shape->fill_painter = NULL;
320     }
321     if (shape->stroke_painter) {
322         sp_painter_free(shape->stroke_painter);
323         shape->stroke_painter = NULL;
324     }
326     if (!shape->curve || 
327         !shape->style ||
328         shape->curve->is_empty() ||
329         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
330          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
331     {
332         item->bbox = shape->approx_bbox;
333         return NR_ARENA_ITEM_STATE_ALL;
334     }
336     /* Build state data */
337     if ( shape->delayed_shp ) {
338         item->bbox=shape->approx_bbox;
339     } else {
340         nr_arena_shape_update_stroke(shape, gc, area);
341         nr_arena_shape_update_fill(shape, gc, area);
343         boundingbox[0][0] = boundingbox[0][1] = boundingbox[1][0] = boundingbox[1][1] = 0.0;
344         nr_arena_shape_add_bboxes(shape, boundingbox);
346         shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
347         shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
348         shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
349         shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
350     }
352     if (boundingbox.isEmpty())
353         return NR_ARENA_ITEM_STATE_ALL;
355     item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
356     item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
357     item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.0F);
358     item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.0F);
359     nr_arena_request_render_rect(item->arena, &item->bbox);
361     item->render_opacity = TRUE;
362     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
363         if (gc && gc->parent) {
364             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
365                                                               gc->transform, gc->parent->transform,
366                                                               &shape->paintbox);
367         }
368         item->render_opacity = FALSE;
369     }
370     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
371         if (gc && gc->parent) {
372             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
373                                                                 gc->transform, gc->parent->transform,
374                                                                 &shape->paintbox);
375         }
376         item->render_opacity = FALSE;
377     }
378     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
379            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
380           || (shape->markers)
381         )
382     {
383         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
384         item->render_opacity = FALSE;
385     }
387     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
388         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
389             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
390         }
391     }
393     return NR_ARENA_ITEM_STATE_ALL;
396 int matrix_is_isometry(Geom::Matrix p) {
397     Geom::Matrix   tp;
398     // transposition
399     tp[0]=p[0];
400     tp[1]=p[2];
401     tp[2]=p[1];
402     tp[3]=p[3];
403     for (int i = 4; i < 6; i++) // shut valgrind up :)
404         tp[i] = p[i] = 0;
405     Geom::Matrix   isom = tp*p; // A^T * A = adjunct?
406     // Is the adjunct nearly an identity function?
407     if (isom.isTranslation(0.01)) {
408         // the transformation is an isometry -> no need to recompute
409         // the uncrossed polygon
410         if ( p.det() < 0 )
411             return -1;
412         else
413             return 1;
414     }
415     return 0;
418 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
419     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
422 /* returns true if the pathvector has a region that needs fill.
423  * is for optimizing purposes, so should be fast and can falsely return true. 
424  * CANNOT falsely return false. */
425 static bool has_inner_area(Geom::PathVector const & pv) {
426     // return false for the cases where there is surely no region to be filled
427     if (pv.empty())
428         return false;
430     if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
431         // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
432         if ( is_straight_curve(pv.front().front()) )
433         {
434             return false;
435         }
436     }
438     return true; //too costly to see if it has region to be filled, so return true.
441 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
442 void
443 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
445     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
446 //        ((shape->curve->get_length() > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {  // <-- this used to be the old code, i think it has to determine that the path has a sort of 'internal region' where fill would occur
447           has_inner_area(shape->curve->get_pathvector()) ) {
448         if (TRUE || !shape->fill_shp) {
449             Geom::Matrix  cached_to_new = Geom::identity();
450             int isometry = 0;
451             if ( shape->cached_fill ) {
452                 if (shape->cached_fctm == gc->transform) {
453                     isometry = 2; // identity
454                 } else {
455                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
456                     isometry = matrix_is_isometry(cached_to_new);
457                 }
458                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
459                     isometry = 0;
460             }
461             if ( isometry == 0 ) {
462                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
463                 shape->cached_fill->Reset();
465                 Path*  thePath=new Path;
466                 Shape* theShape=new Shape;
467                 {
468                     Geom::Matrix tempMat(gc->transform);
469                     thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
470                 }
472                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
473                     thePath->Convert(1.0);
474                     shape->cached_fpartialy = false;
475                 } else {
476                     thePath->Convert(area, 1.0);
477                     shape->cached_fpartialy = true;
478                 }
480                 thePath->Fill(theShape, 0);
482                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
483                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
484                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
485                     //shape->cached_fill->Copy(theShape);
486                 } else {
487                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
488                 }
489                 shape->cached_fctm=gc->transform;
490                 shape->cached_farea = *area;
491                 delete theShape;
492                 delete thePath;
493                 if ( shape->fill_shp == NULL )
494                     shape->fill_shp = new Shape;
496                 shape->fill_shp->Copy(shape->cached_fill);
498             } else if ( 2 == isometry ) {
499                 if ( shape->fill_shp == NULL ) {
500                     shape->fill_shp = new Shape;
501                     shape->fill_shp->Copy(shape->cached_fill);
502                 }
503             } else {
505                 if ( shape->fill_shp == NULL )
506                     shape->fill_shp = new Shape;
508                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
509                                        shape->cached_fill->numberOfEdges());
510                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
511                     shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
512                 if ( isometry == 1 ) {
513                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
514                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
515                                                  shape->cached_fill->getEdge(i).en);
516                 } else if ( isometry == -1 ) { // need to flip poly.
517                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
518                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
519                                                  shape->cached_fill->getEdge(i).st);
520                 }
521                 shape->fill_shp->ForceToPolygon();
522                 shape->fill_shp->needPointsSorting();
523                 shape->fill_shp->needEdgesSorting();
524             }
525             shape->delayed_shp |= shape->cached_fpartialy;
526         }
527     }
530 void
531 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
533     SPStyle* style = shape->style;
535     float const scale = gc->transform.descrim();
537     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
539     if (outline) {
540         // cairo does not need the livarot path for rendering
541         return; 
542     }
544     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
545     // (since it will only be used for picking, not for rendering)
547     if (outline ||
548         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
549          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
551         float style_width = MAX(0.125, shape->_stroke.width * scale);
552         float width;
553         if (outline) {
554             width = 0.5; // 1 pixel wide, independent of zoom
555         } else {
556             width = style_width;
557         }
559         Geom::Matrix  cached_to_new = Geom::identity();
561         int isometry = 0;
562         if ( shape->cached_stroke ) {
563             if (shape->cached_sctm == gc->transform) {
564                 isometry = 2; // identity
565             } else {
566                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
567                 isometry = matrix_is_isometry(cached_to_new);
568             }
569             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
570                 isometry = 0;
571             if (0 != isometry && width != shape->cached_width) { 
572                 // if this happens without setting style, we have just switched to outline or back
573                 isometry = 0; 
574             } 
575         }
577         if ( isometry == 0 ) {
578             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
579             shape->cached_stroke->Reset();
580             Path*  thePath = new Path;
581             Shape* theShape = new Shape;
582             {
583                 Geom::Matrix   tempMat( gc->transform );
584                 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
585             }
587             // add some padding to the rendering area, so clipped path does not go into a render area
588             NRRectL padded_area = *area;
589             padded_area.x0 -= (NR::ICoord)width;
590             padded_area.x1 += (NR::ICoord)width;
591             padded_area.y0 -= (NR::ICoord)width;
592             padded_area.y1 += (NR::ICoord)width;
593             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
594                 thePath->Convert((outline) ? 4.0 : 1.0);
595                 shape->cached_spartialy = false;
596             }
597             else {
598                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
599                 shape->cached_spartialy = true;
600             }
602             if (style->stroke_dash.n_dash && !outline) {
603                 thePath->DashPolylineFromStyle(style, scale, 1.0);
604             }
606             ButtType butt=butt_straight;
607             switch (shape->_stroke.cap) {
608                 case NRArenaShape::BUTT_CAP:
609                     butt = butt_straight;
610                     break;
611                 case NRArenaShape::ROUND_CAP:
612                     butt = butt_round;
613                     break;
614                 case NRArenaShape::SQUARE_CAP:
615                     butt = butt_square;
616                     break;
617             }
618             JoinType join=join_straight;
619             switch (shape->_stroke.join) {
620                 case NRArenaShape::MITRE_JOIN:
621                     join = join_pointy;
622                     break;
623                 case NRArenaShape::ROUND_JOIN:
624                     join = join_round;
625                     break;
626                 case NRArenaShape::BEVEL_JOIN:
627                     join = join_straight;
628                     break;
629             }
631             if (outline) {
632                 butt = butt_straight;
633                 join = join_straight;
634             }
636             thePath->Stroke(theShape, false, 0.5*width, join, butt,
637                             0.5*width*shape->_stroke.mitre_limit);
640             if (outline) {
641                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
642                 shape->cached_stroke->Copy(theShape);
643             } else {
644                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
645             }
647             shape->cached_width = width;
649             shape->cached_sctm=gc->transform;
650             shape->cached_sarea = *area;
651             delete thePath;
652             delete theShape;
653             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
655             shape->stroke_shp->Copy(shape->cached_stroke);
657         } else if ( 2 == isometry ) {
658             if ( shape->stroke_shp == NULL ) {
659                 shape->stroke_shp=new Shape;
660                 shape->stroke_shp->Copy(shape->cached_stroke);
661             }
662         } else {
663             if ( shape->stroke_shp == NULL )
664                 shape->stroke_shp=new Shape;
665             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
666             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
667                 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
668             if ( isometry == 1 ) {
669                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
670                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
671                                                shape->cached_stroke->getEdge(i).en);
672             } else if ( isometry == -1 ) {
673                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
674                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
675                                                shape->cached_stroke->getEdge(i).st);
676             }
677             shape->stroke_shp->ForceToPolygon();
678             shape->stroke_shp->needPointsSorting();
679             shape->stroke_shp->needEdgesSorting();
680         }
681         shape->delayed_shp |= shape->cached_spartialy;
682     }
686 void
687 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox)
689     /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
690      * if so, then this can be written much more compact ! */
692     if ( shape->stroke_shp ) {
693         Shape *larger = shape->stroke_shp;
694         larger->CalcBBox();
695         larger->leftX   = floor(larger->leftX);
696         larger->rightX  = ceil(larger->rightX);
697         larger->topY    = floor(larger->topY);
698         larger->bottomY = ceil(larger->bottomY);
699         Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
700                                 Geom::Interval(larger->topY,  larger->bottomY) );
701         bbox.unionWith(stroke_bbox);
702     }
704     if ( shape->fill_shp ) {
705         Shape *larger = shape->fill_shp;
706         larger->CalcBBox();
707         larger->leftX   = floor(larger->leftX);
708         larger->rightX  = ceil(larger->rightX);
709         larger->topY    = floor(larger->topY);
710         larger->bottomY = ceil(larger->bottomY);
711         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
712                               Geom::Interval(larger->topY,  larger->bottomY) );
713         bbox.unionWith(fill_bbox);
714     }
717 // cairo outline rendering:
718 static unsigned int
719 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, boost::optional<Geom::Rect> area)
721     NRArenaShape *shape = NR_ARENA_SHAPE(item);
723     if (!ct) 
724         return item->state;
726     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
727     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
728     // by setting color channels in the "wrong" order
729     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));
731     cairo_set_line_width(ct, 0.5);
732     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
733     cairo_new_path(ct);
735     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
737     cairo_stroke(ct);
739     return item->state;
742 // cairo stroke rendering (flat color only so far!):
743 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
744 // (need to switch them to premul before this can be enabled)
745 void
746 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
748     NRArenaShape *shape = NR_ARENA_SHAPE(item);
749     SPStyle const *style = shape->style;
751     float const scale = shape->ctm.descrim();
753     if (fabs(shape->_stroke.width * scale) < 0.01)
754         return;
756     cairo_t *ct = nr_create_cairo_context (area, pb);
758     if (!ct)
759         return;
761     guint32 rgba;
762     if ( item->render_opacity ) {
763         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
764                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
765     } else {
766         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
767     }
769     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
770     // by setting color channels in the "wrong" order
771     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));
773     float style_width = MAX(0.125, shape->_stroke.width * scale);
774     cairo_set_line_width(ct, style_width);
776     switch (shape->_stroke.cap) {
777         case NRArenaShape::BUTT_CAP:
778             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
779             break;
780         case NRArenaShape::ROUND_CAP:
781             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
782             break;
783         case NRArenaShape::SQUARE_CAP:
784             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
785             break;
786     }
787     switch (shape->_stroke.join) {
788         case NRArenaShape::MITRE_JOIN:
789             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
790             break;
791         case NRArenaShape::ROUND_JOIN:
792             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
793             break;
794         case NRArenaShape::BEVEL_JOIN:
795             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
796             break;
797     }
799     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
801     if (style->stroke_dash.n_dash) {
802         NRVpathDash dash;
803         dash.offset = style->stroke_dash.offset * scale;
804         dash.n_dash = style->stroke_dash.n_dash;
805         dash.dash = g_new(double, dash.n_dash);
806         for (int i = 0; i < dash.n_dash; i++) {
807             dash.dash[i] = style->stroke_dash.dash[i] * scale;
808         }
809         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
810         g_free(dash.dash);
811     }
813     cairo_set_tolerance(ct, 0.1);
814     cairo_new_path(ct);
816     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
818     cairo_stroke(ct);
820     cairo_surface_t *cst = cairo_get_target(ct);
821     cairo_destroy (ct);
822     cairo_surface_finish (cst);
823     cairo_surface_destroy (cst);
825     pb->empty = FALSE;
829 /**
830  * Renders the item.  Markers are just composed into the parent buffer.
831  */
832 static unsigned int
833 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
835     NRArenaShape *shape = NR_ARENA_SHAPE(item);
837     if (!shape->curve) return item->state;
838     if (!shape->style) return item->state;
840     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
842     if (outline) { // cairo outline rendering
844         pb->empty = FALSE;
845         unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
846         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
848     } else {
850     if ( shape->delayed_shp ) {
851         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
852             NRGC   tempGC(NULL);
853             tempGC.transform=shape->ctm;
854             shape->delayed_shp = false;
855             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
856             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
857 /*      NRRect bbox;
858         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
859         nr_arena_shape_add_bboxes(shape,bbox);
860         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
861         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
862         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
863         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
864         shape->approx_bbox=item->bbox;*/
865         } else {
866             return item->state;
867         }
868     }
870     SPStyle const *style = shape->style;
871     if (shape->fill_shp) {
872         NRPixBlock m;
873         guint32 rgba;
875         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
877         // if memory allocation failed, abort render
878         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
879             nr_pixblock_release (&m);
880             return (item->state);
881         }
883         m.visible_area = pb->visible_area;
884         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
885         m.empty = FALSE;
887         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
888             // do not render fill in any way
889         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
890             if ( item->render_opacity ) {
891                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
892                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
893             } else {
894                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
895             }
896             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
897             pb->empty = FALSE;
898         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
899             if (shape->fill_painter) {
900                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
901             }
902         }
904         nr_pixblock_release(&m);
905     }
907     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
909         // cairo_arena_shape_render_stroke(item, area, pb);
911         guint32 rgba;
912         NRPixBlock m;
914         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
916         // if memory allocation failed, abort render
917         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
918             nr_pixblock_release (&m);
919             return (item->state);
920         }
922         m.visible_area = pb->visible_area;
923         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
924         m.empty = FALSE;
926             if ( item->render_opacity ) {
927                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
928                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
929             } else {
930                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
931             }
932             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
933             pb->empty = FALSE;
935         nr_pixblock_release(&m);
937     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
939         NRPixBlock m;
941         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
943         // if memory allocation failed, abort render
944         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
945             nr_pixblock_release (&m);
946             return (item->state);
947         }
949         m.visible_area = pb->visible_area; 
950         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
951         m.empty = FALSE;
953         if (shape->stroke_painter) {
954             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
955         }
957         nr_pixblock_release(&m);
958     }
960     } // non-cairo non-outline branch
962     /* Render markers into parent buffer */
963     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
964         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
965         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
966     }
968     return item->state;
972 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
973 // reenable this when the bug is fixed and remove the rest of this function
974 // TODO
975 #if defined(DEADCODE) && !defined(DEADCODE)
976 static guint
977 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
979     NRArenaShape *shape = NR_ARENA_SHAPE(item);
980     if (!shape->curve) return item->state;
982         cairo_t *ct = nr_create_cairo_context (area, pb);
984         if (!ct)
985             return item->state;
987         cairo_set_source_rgba(ct, 0, 0, 0, 1);
989         cairo_new_path(ct);
991         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
993         cairo_fill(ct);
995         cairo_surface_t *cst = cairo_get_target(ct);
996         cairo_destroy (ct);
997         cairo_surface_finish (cst);
998         cairo_surface_destroy (cst);
1000         pb->empty = FALSE;
1002         return item->state;
1004 #endif //defined(DEADCODE) && !defined(DEADCODE)
1007 static guint
1008 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1010     //return cairo_arena_shape_clip(item, area, pb);
1012     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1013     if (!shape->curve) return item->state;
1015     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1016         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1017             NRGC   tempGC(NULL);
1018             tempGC.transform=shape->ctm;
1019             shape->delayed_shp = false;
1020             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1021         } else {
1022             return item->state;
1023         }
1024     }
1026     if ( shape->fill_shp ) {
1027         NRPixBlock m;
1029         /* fixme: We can OR in one step (Lauris) */
1030         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1032         // if memory allocation failed, abort 
1033         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1034             nr_pixblock_release (&m);
1035             return (item->state);
1036         }
1038         m.visible_area = pb->visible_area; 
1039         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1041         for (int y = area->y0; y < area->y1; y++) {
1042             unsigned char *s, *d;
1043             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1044             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1045             for (int x = area->x0; x < area->x1; x++) {
1046                 *d = NR_COMPOSEA_111(*s, *d);
1047                 d ++;
1048                 s ++;
1049             }
1050         }
1051         nr_pixblock_release(&m);
1052         pb->empty = FALSE;
1053     }
1055     return item->state;
1058 static NRArenaItem *
1059 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1061     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1063     if (shape->repick_after > 0)
1064         shape->repick_after--;
1066     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1067         return shape->last_pick;
1069     if (!shape->curve) return NULL;
1070     if (!shape->style) return NULL;
1072     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1074     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
1075         // fully transparent, no pick unless outline mode
1076         return NULL;
1078     GTimeVal tstart, tfinish;
1079     g_get_current_time (&tstart);
1081     double width;
1082     if (outline) {
1083         width = 0.5;
1084     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1085         float const scale = shape->ctm.descrim();
1086         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1087     } else {
1088         width = 0;
1089     }
1091     double dist = NR_HUGE;
1092     int wind = 0;
1093     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1094              && shape->_fill.opacity > 1e-3 && !outline);
1096     if (item->arena->canvasarena) {
1097         Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1098         viewbox.expandBy (width);
1099         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1100     } else {
1101         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1102     }
1104     g_get_current_time (&tfinish);
1105     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1106     //g_print ("pick time %lu\n", this_pick);
1108     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1109         shape->repick_after = this_pick / 5000;
1110     }
1112     // covered by fill?
1113     if (needfill) {
1114         if (!shape->style->fill_rule.computed) {
1115             if (wind != 0) {
1116                 shape->last_pick = item;
1117                 return item;
1118             }
1119         } else {
1120             if (wind & 0x1) {
1121                 shape->last_pick = item;
1122                 return item;
1123             }
1124         }
1125     }
1127     // close to the edge, as defined by strokewidth and delta?
1128     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1129     if (needfill || width > 0) { // if either fill or stroke visible,
1130         if ((dist - width) < delta) {
1131             shape->last_pick = item;
1132             return item;
1133         }
1134     }
1136     // if not picked on the shape itself, try its markers
1137     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1138         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1139         if (ret) {
1140             shape->last_pick = item;
1141             return item;
1142         }
1143     }
1145     shape->last_pick = NULL;
1146     return NULL;
1149 /**
1150  *
1151  *  Requests a render of the shape, then if the shape is already a curve it
1152  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1153  *  curve and adds it to the shape.  Finally, it requests an update of the
1154  *  arena for the shape.
1155  */
1156 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1158     g_return_if_fail(shape != NULL);
1159     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1161     if ( justTrans == false ) {
1162         // dirty cached versions
1163         if ( shape->cached_fill ) {
1164             delete shape->cached_fill;
1165             shape->cached_fill=NULL;
1166         }
1167         if ( shape->cached_stroke ) {
1168             delete shape->cached_stroke;
1169             shape->cached_stroke=NULL;
1170         }
1171     }
1173     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1175     if (shape->curve) {
1176         shape->curve->unref();
1177         shape->curve = NULL;
1178     }
1180     if (curve) {
1181         shape->curve = curve;
1182         curve->ref();
1183     }
1185     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1188 void NRArenaShape::setFill(SPPaintServer *server) {
1189     _fill.paint.set(server);
1190     _invalidateCachedFill();
1193 void NRArenaShape::setFill(SPColor const &color) {
1194     _fill.paint.set(color);
1195     _invalidateCachedFill();
1198 void NRArenaShape::setFillOpacity(double opacity) {
1199     _fill.opacity = opacity;
1200     _invalidateCachedFill();
1203 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1204     _fill.rule = rule;
1205     _invalidateCachedFill();
1208 void NRArenaShape::setStroke(SPPaintServer *server) {
1209     _stroke.paint.set(server);
1210     _invalidateCachedStroke();
1213 void NRArenaShape::setStroke(SPColor const &color) {
1214     _stroke.paint.set(color);
1215     _invalidateCachedStroke();
1218 void NRArenaShape::setStrokeOpacity(double opacity) {
1219     _stroke.opacity = opacity;
1220     _invalidateCachedStroke();
1223 void NRArenaShape::setStrokeWidth(double width) {
1224     _stroke.width = width;
1225     _invalidateCachedStroke();
1228 void NRArenaShape::setMitreLimit(double limit) {
1229     _stroke.mitre_limit = limit;
1230     _invalidateCachedStroke();
1233 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1234     _stroke.cap = cap;
1235     _invalidateCachedStroke();
1238 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1239     _stroke.join = join;
1240     _invalidateCachedStroke();
1243 /** nr_arena_shape_set_style
1244  *
1245  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1246  */
1247 void
1248 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1250     g_return_if_fail(shape != NULL);
1251     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1253     if (style) sp_style_ref(style);
1254     if (shape->style) sp_style_unref(shape->style);
1255     shape->style = style;
1257     if ( style->fill.isPaintserver() ) {
1258         shape->setFill(style->getFillPaintServer());
1259     } else if ( style->fill.isColor() ) {
1260         shape->setFill(style->fill.value.color);
1261     } else if ( style->fill.isNone() ) {
1262         shape->setFill(NULL);
1263     } else {
1264         g_assert_not_reached();
1265     }
1266     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1267     switch (style->fill_rule.computed) {
1268         case SP_WIND_RULE_EVENODD: {
1269             shape->setFillRule(NRArenaShape::EVEN_ODD);
1270             break;
1271         }
1272         case SP_WIND_RULE_NONZERO: {
1273             shape->setFillRule(NRArenaShape::NONZERO);
1274             break;
1275         }
1276         default: {
1277             g_assert_not_reached();
1278         }
1279     }
1281     if ( style->stroke.isPaintserver() ) {
1282         shape->setStroke(style->getStrokePaintServer());
1283     } else if ( style->stroke.isColor() ) {
1284         shape->setStroke(style->stroke.value.color);
1285     } else if ( style->stroke.isNone() ) {
1286         shape->setStroke(NULL);
1287     } else {
1288         g_assert_not_reached();
1289     }
1290     shape->setStrokeWidth(style->stroke_width.computed);
1291     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1292     switch (style->stroke_linecap.computed) {
1293         case SP_STROKE_LINECAP_ROUND: {
1294             shape->setLineCap(NRArenaShape::ROUND_CAP);
1295             break;
1296         }
1297         case SP_STROKE_LINECAP_SQUARE: {
1298             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1299             break;
1300         }
1301         case SP_STROKE_LINECAP_BUTT: {
1302             shape->setLineCap(NRArenaShape::BUTT_CAP);
1303             break;
1304         }
1305         default: {
1306             g_assert_not_reached();
1307         }
1308     }
1309     switch (style->stroke_linejoin.computed) {
1310         case SP_STROKE_LINEJOIN_ROUND: {
1311             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1312             break;
1313         }
1314         case SP_STROKE_LINEJOIN_BEVEL: {
1315             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1316             break;
1317         }
1318         case SP_STROKE_LINEJOIN_MITER: {
1319             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1320             break;
1321         }
1322         default: {
1323             g_assert_not_reached();
1324         }
1325     }
1326     shape->setMitreLimit(style->stroke_miterlimit.value);
1328     //if shape has a filter
1329     if (style->filter.set && style->getFilter()) {
1330         if (!shape->filter) {
1331             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1332             shape->filter = new NR::Filter(primitives);
1333         }
1334         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1335     } else {
1336         //no filter set for this shape
1337         delete shape->filter;
1338         shape->filter = NULL;
1339     }
1341     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1344 void
1345 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1347     g_return_if_fail(shape != NULL);
1348     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1349     g_return_if_fail(pbox != NULL);
1351     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1352         shape->paintbox = *pbox;
1353     } else {
1354         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1355         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1356         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1357     }
1359     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1362 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1364     paintbox.x0 = pbox.min()[Geom::X];
1365     paintbox.y0 = pbox.min()[Geom::Y];
1366     paintbox.x1 = pbox.max()[Geom::X];
1367     paintbox.y1 = pbox.max()[Geom::Y];
1369     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1372 static void
1373 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1375     if ( st >= en ) return;
1376     if ( vst < 0 ) vst=0;
1377     if ( vst > 1 ) vst=1;
1378     if ( ven < 0 ) ven=0;
1379     if ( ven > 1 ) ven=1;
1380     float   sv=vst;
1381     float   dv=ven-vst;
1382     int     len=en-st;
1383     unsigned char*   d=(unsigned char*)dest.buffer;
1384     d+=(st-dest.startPix);
1385     if ( fabs(dv) < 0.001 ) {
1386         if ( vst > 0.999 ) {
1387             /* Simple copy */
1388             while (len > 0) {
1389                 d[0] = 255;
1390                 d += 1;
1391                 len -= 1;
1392             }
1393         } else {
1394             sv*=256;
1395             unsigned int c0_24=(int)sv;
1396             c0_24&=0xFF;
1397             while (len > 0) {
1398                 /* Draw */
1399                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1400                 d += 1;
1401                 len -= 1;
1402             }
1403         }
1404     } else {
1405         if ( en <= st+1 ) {
1406             sv=0.5*(vst+ven);
1407             sv*=256;
1408             unsigned int c0_24=(int)sv;
1409             c0_24&=0xFF;
1410             /* Draw */
1411             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1412         } else {
1413             dv/=len;
1414             sv+=0.5*dv; // correction trapezoidale
1415             sv*=16777216;
1416             dv*=16777216;
1417             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1418             int s0_24 = static_cast<int>(dv);
1419             while (len > 0) {
1420                 unsigned int ca;
1421                 /* Draw */
1422                 ca = c0_24 >> 16;
1423                 if ( ca > 255 ) ca=255;
1424                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1425                 d += 1;
1426                 c0_24 += s0_24;
1427                 c0_24 = CLAMP(c0_24, 0, 16777216);
1428                 len -= 1;
1429             }
1430         }
1431     }
1434 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1436     theS->CalcBBox();
1437     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1438     int    il,ir,it,ib;
1439     il=(int)floor(l);
1440     ir=(int)ceil(r);
1441     it=(int)floor(t);
1442     ib=(int)ceil(b);
1444     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1445     if ( il < m.area.x0 ) il=m.area.x0;
1446     if ( it < m.area.y0 ) it=m.area.y0;
1447     if ( ir > m.area.x1 ) ir=m.area.x1;
1448     if ( ib > m.area.y1 ) ib=m.area.y1;
1450     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1451     int    curPt;
1452     float  curY;
1453     theS->BeginQuickRaster(curY, curPt);
1455     FloatLigne *theI = new FloatLigne();
1456     IntLigne *theIL = new IntLigne();
1458     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1460     char *mdata = (char*)m.data.px;
1461     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1462     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1463     for (int y = it; y < ib; y++) {
1464         theI->Reset();
1465         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1466         theI->Flatten();
1467         theIL->Copy(theI);
1469         raster_info  dest;
1470         dest.startPix=il;
1471         dest.endPix=ir;
1472         dest.sth=il;
1473         dest.stv=y;
1474         dest.buffer=ligStart;
1475         theIL->Raster(dest, NULL, shape_run_A8_OR);
1476         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1477     }
1478     theS->EndQuickRaster();
1479     delete theI;
1480     delete theIL;
1484 /*
1485   Local Variables:
1486   mode:c++
1487   c-file-style:"stroustrup"
1488   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1489   indent-tabs-mode:nil
1490   fill-column:99
1491   End:
1492 */
1493 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :