Code

- new: Print Colors Preview Dialog and rendermode
[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-blit.h>
23 #include <libnr/nr-convert2geom.h>
24 #include <2geom/pathvector.h>
25 #include <2geom/curves.h>
26 #include <livarot/Path.h>
27 #include <livarot/float-line.h>
28 #include <livarot/int-line.h>
29 #include <style.h>
30 #include "inkscape-cairo.h"
31 #include "helper/geom.h"
32 #include "helper/geom-curves.h"
33 #include "sp-filter.h"
34 #include "sp-filter-reference.h"
35 #include "display/nr-filter.h"
36 #include <typeinfo>
37 #include <cairo.h>
38 #include "preferences.h"
39 #include "svg/svg-device-color.h"
41 #include <glib.h>
42 #include "svg/svg.h"
43 #include <fenv.h>
45 //int  showRuns=0;
46 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
48 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
49 static void nr_arena_shape_init(NRArenaShape *shape);
50 static void nr_arena_shape_finalize(NRObject *object);
52 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
53 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
54 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
55 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
57 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
58 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
59 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
60 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int sticky);
62 static NRArenaItemClass *shape_parent_class;
64 NRType
65 nr_arena_shape_get_type(void)
66 {
67     static NRType type = 0;
68     if (!type) {
69         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
70                                        "NRArenaShape",
71                                        sizeof(NRArenaShapeClass),
72                                        sizeof(NRArenaShape),
73                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
74                                        (void (*)(NRObject *)) nr_arena_shape_init);
75     }
76     return type;
77 }
79 static void
80 nr_arena_shape_class_init(NRArenaShapeClass *klass)
81 {
82     NRObjectClass *object_class;
83     NRArenaItemClass *item_class;
85     object_class = (NRObjectClass *) klass;
86     item_class = (NRArenaItemClass *) klass;
88     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
90     object_class->finalize = nr_arena_shape_finalize;
91     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
93     item_class->children = nr_arena_shape_children;
94     item_class->add_child = nr_arena_shape_add_child;
95     item_class->set_child_position = nr_arena_shape_set_child_position;
96     item_class->remove_child = nr_arena_shape_remove_child;
97     item_class->update = nr_arena_shape_update;
98     item_class->render = nr_arena_shape_render;
99     item_class->clip = nr_arena_shape_clip;
100     item_class->pick = nr_arena_shape_pick;
103 /**
104  * Initializes the arena shape, setting all parameters to null, 0, false,
105  * or other defaults
106  */
107 static void
108 nr_arena_shape_init(NRArenaShape *shape)
110     shape->curve = NULL;
111     shape->style = NULL;
112     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
113     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
115     shape->ctm.setIdentity();
116     shape->fill_painter = NULL;
117     shape->stroke_painter = NULL;
118     shape->cached_fill = NULL;
119     shape->cached_stroke = NULL;
120     shape->cached_fpartialy = false;
121     shape->cached_spartialy = false;
122     shape->fill_shp = NULL;
123     shape->stroke_shp = NULL;
125     shape->delayed_shp = false;
127     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
128     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
129     shape->cached_fctm.setIdentity();
130     shape->cached_sctm.setIdentity();
132     shape->markers = NULL;
134     shape->last_pick = NULL;
135     shape->repick_after = 0;
138 static void
139 nr_arena_shape_finalize(NRObject *object)
141     NRArenaShape *shape = (NRArenaShape *) object;
143     if (shape->fill_shp) delete shape->fill_shp;
144     if (shape->stroke_shp) delete shape->stroke_shp;
145     if (shape->cached_fill) delete shape->cached_fill;
146     if (shape->cached_stroke) delete shape->cached_stroke;
147     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
148     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
150     if (shape->style) sp_style_unref(shape->style);
151     if (shape->curve) shape->curve->unref();
153     ((NRObjectClass *) shape_parent_class)->finalize(object);
156 /**
157  * Retrieves the markers from the item
158  */
159 static NRArenaItem *
160 nr_arena_shape_children(NRArenaItem *item)
162     NRArenaShape *shape = (NRArenaShape *) item;
164     return shape->markers;
167 /**
168  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
169  * the prev and next items.  If ref is NULL, then it sets the item's markers
170  * as the next items.
171  */
172 static void
173 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
175     NRArenaShape *shape = (NRArenaShape *) item;
177     if (!ref) {
178         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
179     } else {
180         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
181     }
183     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
186 /**
187  * Removes child from the shape.  If there are no prev items in 
188  * the child, it sets items' markers to the next item in the child.
189  */
190 static void
191 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
193     NRArenaShape *shape = (NRArenaShape *) item;
195     if (child->prev) {
196         nr_arena_item_detach(item, child);
197     } else {
198         shape->markers = nr_arena_item_detach(item, child);
199     }
201     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
204 /**
205  * Detaches child from item, and if there are no previous items in child, it 
206  * sets item's markers to the child.  It then attaches the child back onto the item.
207  * If ref is null, it sets the markers to be the next item, otherwise it uses
208  * the next/prev items in ref.
209  */
210 static void
211 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
213     NRArenaShape *shape = (NRArenaShape *) item;
215     if (child->prev) {
216         nr_arena_item_detach(item, child);
217     } else {
218         shape->markers = nr_arena_item_detach(item, child);
219     }
221     if (!ref) {
222         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
223     } else {
224         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
225     }
227     nr_arena_item_request_render(child);
230 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
231 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
232 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox);
234 /**
235  * Updates the arena shape 'item' and all of its children, including the markers.
236  */
237 static guint
238 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
240     Geom::OptRect boundingbox;
242     NRArenaShape *shape = NR_ARENA_SHAPE(item);
244     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
246     unsigned int newstate;
247     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
248         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
249         beststate = beststate & newstate;
250     }
252     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
253         /* We do not have to create rendering structures */
254         shape->ctm = gc->transform;
255         if (state & NR_ARENA_ITEM_STATE_BBOX) {
256             if (shape->curve) {
257                 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
258                 /// \todo  just write item->bbox = boundingbox
259                 if (boundingbox) {
260                     item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
261                     item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
262                     item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
263                     item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
264                 } else {
265                     item->bbox = NR_RECT_L_EMPTY;
266                 }
267             }
268             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
269                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
270                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
271                 }
272             }
273         }
274         return (state | item->state);
275     }
277     shape->delayed_shp=true;
278     shape->ctm = gc->transform;
279     boundingbox = Geom::OptRect();
281     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
283     if (shape->curve) {
284         boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
286         if (boundingbox && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
287             float width, scale;
288             scale = gc->transform.descrim();
289             width = MAX(0.125, shape->_stroke.width * scale);
290             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
291                 boundingbox->expandBy(width);
292             }
293             // those pesky miters, now
294             float miterMax=width*shape->_stroke.mitre_limit;
295             if ( miterMax > 0.01 ) {
296                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
297                 boundingbox->expandBy(miterMax);
298             }
299         }
300     } 
302     /// \todo  just write item->bbox = boundingbox
303     if (boundingbox) {
304         shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
305         shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
306         shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
307         shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
308     } else {
309         shape->approx_bbox = NR_RECT_L_EMPTY;
310     }
311     if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
313     /* Release state data */
314     if (shape->fill_shp) {
315         delete shape->fill_shp;
316         shape->fill_shp = NULL;
317     }
318     if (shape->stroke_shp) {
319         delete shape->stroke_shp;
320         shape->stroke_shp = NULL;
321     }
322     if (shape->fill_painter) {
323         sp_painter_free(shape->fill_painter);
324         shape->fill_painter = NULL;
325     }
326     if (shape->stroke_painter) {
327         sp_painter_free(shape->stroke_painter);
328         shape->stroke_painter = NULL;
329     }
331     if (!shape->curve || 
332         !shape->style ||
333         shape->curve->is_empty() ||
334         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
335          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
336     {
337         item->bbox = shape->approx_bbox;
338         return NR_ARENA_ITEM_STATE_ALL;
339     }
341     /* Build state data */
342     if ( shape->delayed_shp ) {
343         item->bbox=shape->approx_bbox;
344     } else {
345         nr_arena_shape_update_stroke(shape, gc, area);
346         nr_arena_shape_update_fill(shape, gc, area);
348         boundingbox = Geom::OptRect();
349         nr_arena_shape_add_bboxes(shape, boundingbox);
351         /// \todo  just write shape->approx_bbox = boundingbox
352         if (boundingbox) {
353             shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
354             shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
355             shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
356             shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
357         } else {
358             shape->approx_bbox = NR_RECT_L_EMPTY;
359         }
360     }
362     if (!boundingbox)
363         return NR_ARENA_ITEM_STATE_ALL;
365     /// \todo  just write item->bbox = boundingbox
366     item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
367     item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
368     item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.0F);
369     item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.0F);
371     item->render_opacity = TRUE;
372     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
373         if (gc && gc->parent) {
374             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
375                                                               gc->transform, gc->parent->transform,
376                                                               &shape->paintbox);
377         }
378         item->render_opacity = FALSE;
379     }
380     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
381         if (gc && gc->parent) {
382             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
383                                                                 gc->transform, gc->parent->transform,
384                                                                 &shape->paintbox);
385         }
386         item->render_opacity = FALSE;
387     }
388     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
389            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
390           || (shape->markers)
391         )
392     {
393         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
394         item->render_opacity = FALSE;
395     }
397     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
398         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
399             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
400         }
401     }
403     return NR_ARENA_ITEM_STATE_ALL;
406 int matrix_is_isometry(Geom::Matrix p) {
407     Geom::Matrix   tp;
408     // transposition
409     tp[0]=p[0];
410     tp[1]=p[2];
411     tp[2]=p[1];
412     tp[3]=p[3];
413     for (int i = 4; i < 6; i++) // shut valgrind up :)
414         tp[i] = p[i] = 0;
415     Geom::Matrix   isom = tp*p; // A^T * A = adjunct?
416     // Is the adjunct nearly an identity function?
417     if (isom.isTranslation(0.01)) {
418         // the transformation is an isometry -> no need to recompute
419         // the uncrossed polygon
420         if ( p.det() < 0 )
421             return -1;
422         else
423             return 1;
424     }
425     return 0;
428 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
429     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
432 /* returns true if the pathvector has a region that needs fill.
433  * is for optimizing purposes, so should be fast and can falsely return true. 
434  * CANNOT falsely return false. */
435 static bool has_inner_area(Geom::PathVector const & pv) {
436     // return false for the cases where there is surely no region to be filled
437     if (pv.empty())
438         return false;
440     if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
441         // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
442         if ( is_straight_curve(pv.front().front()) )
443         {
444             return false;
445         }
446     }
448     return true; //too costly to see if it has region to be filled, so return true.
451 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
452 void
453 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
455     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
456           has_inner_area(shape->curve->get_pathvector()) ) {
458             Geom::Matrix  cached_to_new = Geom::identity();
459             int isometry = 0;
460             if ( shape->cached_fill ) {
461                 if (shape->cached_fctm == gc->transform) {
462                     isometry = 2; // identity
463                 } else {
464                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
465                     isometry = matrix_is_isometry(cached_to_new);
466                 }
467                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
468                     isometry = 0;
469             }
470             if ( isometry == 0 ) {
471                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
472                 shape->cached_fill->Reset();
474                 Path*  thePath=new Path;
475                 Shape* theShape=new Shape;
476                 {
477                     Geom::Matrix tempMat(gc->transform);
478                     thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
479                 }
481                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
482                     thePath->Convert(1.0);
483                     shape->cached_fpartialy = false;
484                 } else {
485                     thePath->Convert(area, 1.0);
486                     shape->cached_fpartialy = true;
487                 }
489                 thePath->Fill(theShape, 0);
491                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
492                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
493                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
494                     //shape->cached_fill->Copy(theShape);
495                 } else {
496                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
497                 }
498                 shape->cached_fctm=gc->transform;
499                 shape->cached_farea = *area;
500                 delete theShape;
501                 delete thePath;
502                 if ( shape->fill_shp == NULL )
503                     shape->fill_shp = new Shape;
505                 shape->fill_shp->Copy(shape->cached_fill);
507             } else if ( 2 == isometry ) {
508                 if ( shape->fill_shp == NULL ) {
509                     shape->fill_shp = new Shape;
510                     shape->fill_shp->Copy(shape->cached_fill);
511                 }
512             } else {
514                 if ( shape->fill_shp == NULL )
515                     shape->fill_shp = new Shape;
517                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
518                                        shape->cached_fill->numberOfEdges());
519                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
520                     shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
521                 if ( isometry == 1 ) {
522                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
523                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
524                                                  shape->cached_fill->getEdge(i).en);
525                 } else if ( isometry == -1 ) { // need to flip poly.
526                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
527                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
528                                                  shape->cached_fill->getEdge(i).st);
529                 }
530                 shape->fill_shp->ForceToPolygon();
531                 shape->fill_shp->needPointsSorting();
532                 shape->fill_shp->needEdgesSorting();
533             }
534             shape->delayed_shp |= shape->cached_fpartialy;
535     }
538 void
539 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
541     SPStyle* style = shape->style;
543     float const scale = gc->transform.descrim();
545     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
547     if (outline) {
548         // cairo does not need the livarot path for rendering
549         return; 
550     }
552     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
553     // (since it will only be used for picking, not for rendering)
555     if (outline ||
556         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
557          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
559         float style_width = MAX(0.125, shape->_stroke.width * scale);
560         float width;
561         if (outline) {
562             width = 0.5; // 1 pixel wide, independent of zoom
563         } else {
564             width = style_width;
565         }
567         Geom::Matrix  cached_to_new = Geom::identity();
569         int isometry = 0;
570         if ( shape->cached_stroke ) {
571             if (shape->cached_sctm == gc->transform) {
572                 isometry = 2; // identity
573             } else {
574                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
575                 isometry = matrix_is_isometry(cached_to_new);
576             }
577             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
578                 isometry = 0;
579             if (0 != isometry && width != shape->cached_width) { 
580                 // if this happens without setting style, we have just switched to outline or back
581                 isometry = 0; 
582             } 
583         }
585         if ( isometry == 0 ) {
586             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
587             shape->cached_stroke->Reset();
588             Path*  thePath = new Path;
589             Shape* theShape = new Shape;
590             {
591                 Geom::Matrix   tempMat( gc->transform );
592                 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
593             }
595             // add some padding to the rendering area, so clipped path does not go into a render area
596             NRRectL padded_area = *area;
597             padded_area.x0 -= (NR::ICoord)width;
598             padded_area.x1 += (NR::ICoord)width;
599             padded_area.y0 -= (NR::ICoord)width;
600             padded_area.y1 += (NR::ICoord)width;
601             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
602                 thePath->Convert((outline) ? 4.0 : 1.0);
603                 shape->cached_spartialy = false;
604             }
605             else {
606                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
607                 shape->cached_spartialy = true;
608             }
610             if (style->stroke_dash.n_dash && !outline) {
611                 thePath->DashPolylineFromStyle(style, scale, 1.0);
612             }
614             ButtType butt=butt_straight;
615             switch (shape->_stroke.cap) {
616                 case NRArenaShape::BUTT_CAP:
617                     butt = butt_straight;
618                     break;
619                 case NRArenaShape::ROUND_CAP:
620                     butt = butt_round;
621                     break;
622                 case NRArenaShape::SQUARE_CAP:
623                     butt = butt_square;
624                     break;
625             }
626             JoinType join=join_straight;
627             switch (shape->_stroke.join) {
628                 case NRArenaShape::MITRE_JOIN:
629                     join = join_pointy;
630                     break;
631                 case NRArenaShape::ROUND_JOIN:
632                     join = join_round;
633                     break;
634                 case NRArenaShape::BEVEL_JOIN:
635                     join = join_straight;
636                     break;
637             }
639             if (outline) {
640                 butt = butt_straight;
641                 join = join_straight;
642             }
644             thePath->Stroke(theShape, false, 0.5*width, join, butt,
645                             0.5*width*shape->_stroke.mitre_limit);
648             if (outline) {
649                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
650                 shape->cached_stroke->Copy(theShape);
651             } else {
652                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
653             }
655             shape->cached_width = width;
657             shape->cached_sctm=gc->transform;
658             shape->cached_sarea = *area;
659             delete thePath;
660             delete theShape;
661             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
663             shape->stroke_shp->Copy(shape->cached_stroke);
665         } else if ( 2 == isometry ) {
666             if ( shape->stroke_shp == NULL ) {
667                 shape->stroke_shp=new Shape;
668                 shape->stroke_shp->Copy(shape->cached_stroke);
669             }
670         } else {
671             if ( shape->stroke_shp == NULL )
672                 shape->stroke_shp=new Shape;
673             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
674             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
675                 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
676             if ( isometry == 1 ) {
677                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
678                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
679                                                shape->cached_stroke->getEdge(i).en);
680             } else if ( isometry == -1 ) {
681                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
682                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
683                                                shape->cached_stroke->getEdge(i).st);
684             }
685             shape->stroke_shp->ForceToPolygon();
686             shape->stroke_shp->needPointsSorting();
687             shape->stroke_shp->needEdgesSorting();
688         }
689         shape->delayed_shp |= shape->cached_spartialy;
690     }
694 void
695 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox)
697     /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
698      * if so, then this can be written much more compact ! */
700     if ( shape->stroke_shp ) {
701         Shape *larger = shape->stroke_shp;
702         larger->CalcBBox();
703         larger->leftX   = floor(larger->leftX);
704         larger->rightX  = ceil(larger->rightX);
705         larger->topY    = floor(larger->topY);
706         larger->bottomY = ceil(larger->bottomY);
707         Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
708                                 Geom::Interval(larger->topY,  larger->bottomY) );
709         bbox.unionWith(stroke_bbox);
710     }
712     if ( shape->fill_shp ) {
713         Shape *larger = shape->fill_shp;
714         larger->CalcBBox();
715         larger->leftX   = floor(larger->leftX);
716         larger->rightX  = ceil(larger->rightX);
717         larger->topY    = floor(larger->topY);
718         larger->bottomY = ceil(larger->bottomY);
719         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
720                               Geom::Interval(larger->topY,  larger->bottomY) );
721         bbox.unionWith(fill_bbox);
722     }
725 // cairo outline rendering:
726 static unsigned int
727 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, Geom::OptRect area)
729     NRArenaShape *shape = NR_ARENA_SHAPE(item);
731     if (!ct) 
732         return item->state;
734     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
735     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
736     // by setting color channels in the "wrong" order
737     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));
739     cairo_set_line_width(ct, 0.5);
740     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
741     cairo_new_path(ct);
743     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
745     cairo_stroke(ct);
747     return item->state;
750 // cairo stroke rendering (flat color only so far!):
751 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
752 // (need to switch them to premul before this can be enabled)
753 void
754 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
756     NRArenaShape *shape = NR_ARENA_SHAPE(item);
757     SPStyle const *style = shape->style;
759     float const scale = shape->ctm.descrim();
761     if (fabs(shape->_stroke.width * scale) < 0.01)
762         return;
764     cairo_t *ct = nr_create_cairo_context (area, pb);
766     if (!ct)
767         return;
769     guint32 rgba;
770     if ( item->render_opacity ) {
771         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
772                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
773     } else {
774         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
775     }
777     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
778     // by setting color channels in the "wrong" order
779     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));
781     float style_width = MAX(0.125, shape->_stroke.width * scale);
782     cairo_set_line_width(ct, style_width);
784     switch (shape->_stroke.cap) {
785         case NRArenaShape::BUTT_CAP:
786             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
787             break;
788         case NRArenaShape::ROUND_CAP:
789             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
790             break;
791         case NRArenaShape::SQUARE_CAP:
792             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
793             break;
794     }
795     switch (shape->_stroke.join) {
796         case NRArenaShape::MITRE_JOIN:
797             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
798             break;
799         case NRArenaShape::ROUND_JOIN:
800             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
801             break;
802         case NRArenaShape::BEVEL_JOIN:
803             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
804             break;
805     }
807     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
809     if (style->stroke_dash.n_dash) {
810         NRVpathDash dash;
811         dash.offset = style->stroke_dash.offset * scale;
812         dash.n_dash = style->stroke_dash.n_dash;
813         dash.dash = g_new(double, dash.n_dash);
814         for (int i = 0; i < dash.n_dash; i++) {
815             dash.dash[i] = style->stroke_dash.dash[i] * scale;
816         }
817         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
818         g_free(dash.dash);
819     }
821     cairo_set_tolerance(ct, 0.1);
822     cairo_new_path(ct);
824     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
826     cairo_stroke(ct);
828     cairo_surface_t *cst = cairo_get_target(ct);
829     cairo_destroy (ct);
830     cairo_surface_finish (cst);
831     cairo_surface_destroy (cst);
833     pb->empty = FALSE;
837 /**
838  * Renders the item.  Markers are just composed into the parent buffer.
839  */
840 static unsigned int
841 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
843     NRArenaShape *shape = NR_ARENA_SHAPE(item);
845     if (!shape->curve) return item->state;
846     if (!shape->style) return item->state;
848     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
849     bool print_colors = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_PRINT_COLORS_PREVIEW);
851     if (outline) { // cairo outline rendering
853         pb->empty = FALSE;
854         unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
855         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
857     } else {
859     if ( shape->delayed_shp ) {
860         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
861             NRGC   tempGC(NULL);
862             tempGC.transform=shape->ctm;
863             shape->delayed_shp = false;
864             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
865             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
866 /*      NRRect bbox;
867         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
868         nr_arena_shape_add_bboxes(shape,bbox);
869         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
870         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
871         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
872         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
873         shape->approx_bbox=item->bbox;*/
874         } else {
875             return item->state;
876         }
877     }
879     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
881     SPStyle const *style = shape->style;
882     bool render_cyan = prefs->getBool("/options/printcolorspreview/cyan", true);
883     bool render_magenta = prefs->getBool("/options/printcolorspreview/magenta", true);
884     bool render_yellow = prefs->getBool("/options/printcolorspreview/yellow", true);
885     bool render_black = prefs->getBool("/options/printcolorspreview/black", true);
887     float rgb_v[3];
888     float cmyk_v[4];
889 #define FLOAT_TO_UINT8(f) (int(f*255))
890 #define RGBA_R(v) ((v) >> 24)
891 #define RGBA_G(v) (((v) >> 16) & 0xff)
892 #define RGBA_B(v) (((v) >> 8) & 0xff)
893 #define RGBA_A(v) ((v) & 0xff)
895     if (shape->fill_shp) {
896         NRPixBlock m;
897         guint32 rgba;
899         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
901         // if memory allocation failed, abort render
902         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
903             nr_pixblock_release (&m);
904             return (item->state);
905         }
907         m.visible_area = pb->visible_area;
908         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
909         m.empty = FALSE;
911         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
912             // do not render fill in any way
913         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
915             const SPColor* fill_color = &shape->_fill.paint.color();
916             if ( item->render_opacity ) {
917                 rgba = fill_color->toRGBA32( shape->_fill.opacity *
918                                         SP_SCALE24_TO_FLOAT(style->opacity.value) );
919             } else {
920                 rgba = fill_color->toRGBA32( shape->_fill.opacity );
921             }
923             if (print_colors){
924                 sp_color_rgb_to_cmyk_floatv (cmyk_v, RGBA_R(rgba)/256.0, RGBA_G(rgba)/256.0, RGBA_B(rgba)/256.0); 
925                 sp_color_cmyk_to_rgb_floatv (rgb_v, render_cyan ? cmyk_v[0] : 0,
926                                                     render_magenta ? cmyk_v[1] : 0,
927                                                     render_yellow ? cmyk_v[2] : 0,
928                                                     render_black ? cmyk_v[3] : 0);
929                 rgba = (FLOAT_TO_UINT8(rgb_v[0])<<24) + (FLOAT_TO_UINT8(rgb_v[1])<<16) + (FLOAT_TO_UINT8(rgb_v[2])<<8) + 0xff;
930             }
932             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
933             pb->empty = FALSE;
934         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
935             if (shape->fill_painter) {
936                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
937             }
938         }
940         nr_pixblock_release(&m);
941     }
943     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
945         // cairo_arena_shape_render_stroke(item, area, pb);
947         guint32 rgba;
948         NRPixBlock m;
950         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
952         // if memory allocation failed, abort render
953         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
954             nr_pixblock_release (&m);
955             return (item->state);
956         }
958         m.visible_area = pb->visible_area;
959         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
960         m.empty = FALSE;
962         const SPColor* stroke_color = &shape->_stroke.paint.color();
963         if ( item->render_opacity ) {
964             rgba = stroke_color->toRGBA32( shape->_stroke.opacity *
965                                     SP_SCALE24_TO_FLOAT(style->opacity.value) );
966         } else {
967             rgba = stroke_color->toRGBA32( shape->_stroke.opacity );
968         }
970         if (print_colors){
971             sp_color_rgb_to_cmyk_floatv (cmyk_v, RGBA_R(rgba)/256.0, RGBA_G(rgba)/256.0, RGBA_B(rgba)/256.0); 
972             sp_color_cmyk_to_rgb_floatv (rgb_v, render_cyan ? cmyk_v[0] : 0,
973                                                 render_magenta ? cmyk_v[1] : 0,
974                                                 render_yellow ? cmyk_v[2] : 0,
975                                                 render_black ? cmyk_v[3] : 0);
976             rgba = (FLOAT_TO_UINT8(rgb_v[0])<<24) + (FLOAT_TO_UINT8(rgb_v[1])<<16) + (FLOAT_TO_UINT8(rgb_v[2])<<8) + 0xff;
977         }
979         nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
980         pb->empty = FALSE;
982         nr_pixblock_release(&m);
984     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
986         NRPixBlock m;
988         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
990         // if memory allocation failed, abort render
991         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
992             nr_pixblock_release (&m);
993             return (item->state);
994         }
996         m.visible_area = pb->visible_area; 
997         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
998         m.empty = FALSE;
1000         if (shape->stroke_painter) {
1001             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
1002         }
1004         nr_pixblock_release(&m);
1005     }
1007     } // non-cairo non-outline branch
1009     /* Render markers into parent buffer */
1010     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1011         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
1012         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
1013     }
1015     return item->state;
1019 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
1020 // reenable this when the bug is fixed and remove the rest of this function
1021 // TODO
1022 #if defined(DEADCODE) && !defined(DEADCODE)
1023 static guint
1024 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1026     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1027     if (!shape->curve) return item->state;
1029         cairo_t *ct = nr_create_cairo_context (area, pb);
1031         if (!ct)
1032             return item->state;
1034         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1036         cairo_new_path(ct);
1038         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
1040         cairo_fill(ct);
1042         cairo_surface_t *cst = cairo_get_target(ct);
1043         cairo_destroy (ct);
1044         cairo_surface_finish (cst);
1045         cairo_surface_destroy (cst);
1047         pb->empty = FALSE;
1049         return item->state;
1051 #endif //defined(DEADCODE) && !defined(DEADCODE)
1054 static guint
1055 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1057     //return cairo_arena_shape_clip(item, area, pb);
1059     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1060     if (!shape->curve) return item->state;
1062     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1063         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1064             NRGC   tempGC(NULL);
1065             tempGC.transform=shape->ctm;
1066             shape->delayed_shp = false;
1067             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1068         } else {
1069             return item->state;
1070         }
1071     }
1073     if ( shape->fill_shp ) {
1074         NRPixBlock m;
1076         /* fixme: We can OR in one step (Lauris) */
1077         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1079         // if memory allocation failed, abort 
1080         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1081             nr_pixblock_release (&m);
1082             return (item->state);
1083         }
1085         m.visible_area = pb->visible_area; 
1086         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1088         for (int y = area->y0; y < area->y1; y++) {
1089             unsigned char *s, *d;
1090             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1091             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1092             for (int x = area->x0; x < area->x1; x++) {
1093                 *d = NR_COMPOSEA_111(*s, *d);
1094                 d ++;
1095                 s ++;
1096             }
1097         }
1098         nr_pixblock_release(&m);
1099         pb->empty = FALSE;
1100     }
1102     return item->state;
1105 static NRArenaItem *
1106 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1108     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1110     if (shape->repick_after > 0)
1111         shape->repick_after--;
1113     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1114         return shape->last_pick;
1116     if (!shape->curve) return NULL;
1117     if (!shape->style) return NULL;
1119     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1121     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
1122         // fully transparent, no pick unless outline mode
1123         return NULL;
1125     GTimeVal tstart, tfinish;
1126     g_get_current_time (&tstart);
1128     double width;
1129     if (outline) {
1130         width = 0.5;
1131     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1132         float const scale = shape->ctm.descrim();
1133         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1134     } else {
1135         width = 0;
1136     }
1138     double dist = NR_HUGE;
1139     int wind = 0;
1140     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1141              && shape->_fill.opacity > 1e-3 && !outline);
1143     if (item->arena->canvasarena) {
1144         Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1145         viewbox.expandBy (width);
1146         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1147     } else {
1148         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1149     }
1151     g_get_current_time (&tfinish);
1152     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1153     //g_print ("pick time %lu\n", this_pick);
1155     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1156         shape->repick_after = this_pick / 5000;
1157     }
1159     // covered by fill?
1160     if (needfill) {
1161         if (!shape->style->fill_rule.computed) {
1162             if (wind != 0) {
1163                 shape->last_pick = item;
1164                 return item;
1165             }
1166         } else {
1167             if (wind & 0x1) {
1168                 shape->last_pick = item;
1169                 return item;
1170             }
1171         }
1172     }
1174     // close to the edge, as defined by strokewidth and delta?
1175     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1176     if (needfill || width > 0) { // if either fill or stroke visible,
1177         if ((dist - width) < delta) {
1178             shape->last_pick = item;
1179             return item;
1180         }
1181     }
1183     // if not picked on the shape itself, try its markers
1184     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1185         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1186         if (ret) {
1187             shape->last_pick = item;
1188             return item;
1189         }
1190     }
1192     shape->last_pick = NULL;
1193     return NULL;
1196 /**
1197  *
1198  *  Requests a render of the shape, then if the shape is already a curve it
1199  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1200  *  curve and adds it to the shape.  Finally, it requests an update of the
1201  *  arena for the shape.
1202  */
1203 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1205     g_return_if_fail(shape != NULL);
1206     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1208     if ( justTrans == false ) {
1209         // dirty cached versions
1210         if ( shape->cached_fill ) {
1211             delete shape->cached_fill;
1212             shape->cached_fill=NULL;
1213         }
1214         if ( shape->cached_stroke ) {
1215             delete shape->cached_stroke;
1216             shape->cached_stroke=NULL;
1217         }
1218     }
1220     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1222     if (shape->curve) {
1223         shape->curve->unref();
1224         shape->curve = NULL;
1225     }
1227     if (curve) {
1228         shape->curve = curve;
1229         curve->ref();
1230     }
1232     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1235 void NRArenaShape::setFill(SPPaintServer *server) {
1236     _fill.paint.set(server);
1237     _invalidateCachedFill();
1240 void NRArenaShape::setFill(SPColor const &color) {
1241     _fill.paint.set(color);
1242     _invalidateCachedFill();
1245 void NRArenaShape::setFillOpacity(double opacity) {
1246     _fill.opacity = opacity;
1247     _invalidateCachedFill();
1250 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1251     _fill.rule = rule;
1252     _invalidateCachedFill();
1255 void NRArenaShape::setStroke(SPPaintServer *server) {
1256     _stroke.paint.set(server);
1257     _invalidateCachedStroke();
1260 void NRArenaShape::setStroke(SPColor const &color) {
1261     _stroke.paint.set(color);
1262     _invalidateCachedStroke();
1265 void NRArenaShape::setStrokeOpacity(double opacity) {
1266     _stroke.opacity = opacity;
1267     _invalidateCachedStroke();
1270 void NRArenaShape::setStrokeWidth(double width) {
1271     _stroke.width = width;
1272     _invalidateCachedStroke();
1275 void NRArenaShape::setMitreLimit(double limit) {
1276     _stroke.mitre_limit = limit;
1277     _invalidateCachedStroke();
1280 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1281     _stroke.cap = cap;
1282     _invalidateCachedStroke();
1285 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1286     _stroke.join = join;
1287     _invalidateCachedStroke();
1290 /** nr_arena_shape_set_style
1291  *
1292  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1293  */
1294 void
1295 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1297     g_return_if_fail(shape != NULL);
1298     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1300     if (style) sp_style_ref(style);
1301     if (shape->style) sp_style_unref(shape->style);
1302     shape->style = style;
1304     if ( style->fill.isPaintserver() ) {
1305         shape->setFill(style->getFillPaintServer());
1306     } else if ( style->fill.isColor() ) {
1307         shape->setFill(style->fill.value.color);
1308     } else if ( style->fill.isNone() ) {
1309         shape->setFill(NULL);
1310     } else {
1311         g_assert_not_reached();
1312     }
1313     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1314     switch (style->fill_rule.computed) {
1315         case SP_WIND_RULE_EVENODD: {
1316             shape->setFillRule(NRArenaShape::EVEN_ODD);
1317             break;
1318         }
1319         case SP_WIND_RULE_NONZERO: {
1320             shape->setFillRule(NRArenaShape::NONZERO);
1321             break;
1322         }
1323         default: {
1324             g_assert_not_reached();
1325         }
1326     }
1328     if ( style->stroke.isPaintserver() ) {
1329         shape->setStroke(style->getStrokePaintServer());
1330     } else if ( style->stroke.isColor() ) {
1331         shape->setStroke(style->stroke.value.color);
1332     } else if ( style->stroke.isNone() ) {
1333         shape->setStroke(NULL);
1334     } else {
1335         g_assert_not_reached();
1336     }
1337     shape->setStrokeWidth(style->stroke_width.computed);
1338     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1339     switch (style->stroke_linecap.computed) {
1340         case SP_STROKE_LINECAP_ROUND: {
1341             shape->setLineCap(NRArenaShape::ROUND_CAP);
1342             break;
1343         }
1344         case SP_STROKE_LINECAP_SQUARE: {
1345             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1346             break;
1347         }
1348         case SP_STROKE_LINECAP_BUTT: {
1349             shape->setLineCap(NRArenaShape::BUTT_CAP);
1350             break;
1351         }
1352         default: {
1353             g_assert_not_reached();
1354         }
1355     }
1356     switch (style->stroke_linejoin.computed) {
1357         case SP_STROKE_LINEJOIN_ROUND: {
1358             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1359             break;
1360         }
1361         case SP_STROKE_LINEJOIN_BEVEL: {
1362             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1363             break;
1364         }
1365         case SP_STROKE_LINEJOIN_MITER: {
1366             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1367             break;
1368         }
1369         default: {
1370             g_assert_not_reached();
1371         }
1372     }
1373     shape->setMitreLimit(style->stroke_miterlimit.value);
1375     //if shape has a filter
1376     if (style->filter.set && style->getFilter()) {
1377         if (!shape->filter) {
1378             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1379             shape->filter = new Inkscape::Filters::Filter(primitives);
1380         }
1381         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1382     } else {
1383         //no filter set for this shape
1384         delete shape->filter;
1385         shape->filter = NULL;
1386     }
1388     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1391 void
1392 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1394     g_return_if_fail(shape != NULL);
1395     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1396     g_return_if_fail(pbox != NULL);
1398     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1399         shape->paintbox = *pbox;
1400     } else {
1401         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1402         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1403         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1404     }
1406     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1409 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1411     paintbox.x0 = pbox.min()[Geom::X];
1412     paintbox.y0 = pbox.min()[Geom::Y];
1413     paintbox.x1 = pbox.max()[Geom::X];
1414     paintbox.y1 = pbox.max()[Geom::Y];
1416     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1419 static void
1420 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1422     if ( st >= en ) return;
1423     if ( vst < 0 ) vst=0;
1424     if ( vst > 1 ) vst=1;
1425     if ( ven < 0 ) ven=0;
1426     if ( ven > 1 ) ven=1;
1427     float   sv=vst;
1428     float   dv=ven-vst;
1429     int     len=en-st;
1430     unsigned char*   d=(unsigned char*)dest.buffer;
1431     d+=(st-dest.startPix);
1432     if ( fabs(dv) < 0.001 ) {
1433         if ( vst > 0.999 ) {
1434             /* Simple copy */
1435             while (len > 0) {
1436                 d[0] = 255;
1437                 d += 1;
1438                 len -= 1;
1439             }
1440         } else {
1441             sv*=256;
1442             unsigned int c0_24=(int)sv;
1443             c0_24&=0xFF;
1444             while (len > 0) {
1445                 /* Draw */
1446                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1447                 d += 1;
1448                 len -= 1;
1449             }
1450         }
1451     } else {
1452         if ( en <= st+1 ) {
1453             sv=0.5*(vst+ven);
1454             sv*=256;
1455             unsigned int c0_24=(int)sv;
1456             c0_24&=0xFF;
1457             /* Draw */
1458             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1459         } else {
1460             dv/=len;
1461             sv+=0.5*dv; // correction trapezoidale
1462             sv*=16777216;
1463             dv*=16777216;
1464             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1465             int s0_24 = static_cast<int>(dv);
1466             while (len > 0) {
1467                 unsigned int ca;
1468                 /* Draw */
1469                 ca = c0_24 >> 16;
1470                 if ( ca > 255 ) ca=255;
1471                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1472                 d += 1;
1473                 c0_24 += s0_24;
1474                 c0_24 = CLAMP(c0_24, 0, 16777216);
1475                 len -= 1;
1476             }
1477         }
1478     }
1481 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1483     theS->CalcBBox();
1484     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1485     int    il,ir,it,ib;
1486     il=(int)floor(l);
1487     ir=(int)ceil(r);
1488     it=(int)floor(t);
1489     ib=(int)ceil(b);
1491     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1492     if ( il < m.area.x0 ) il=m.area.x0;
1493     if ( it < m.area.y0 ) it=m.area.y0;
1494     if ( ir > m.area.x1 ) ir=m.area.x1;
1495     if ( ib > m.area.y1 ) ib=m.area.y1;
1497     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1498     int    curPt;
1499     float  curY;
1500     theS->BeginQuickRaster(curY, curPt);
1502     FloatLigne *theI = new FloatLigne();
1503     IntLigne *theIL = new IntLigne();
1505     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1507     char *mdata = (char*)m.data.px;
1508     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1509     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1510     for (int y = it; y < ib; y++) {
1511         theI->Reset();
1512         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1513         theI->Flatten();
1514         theIL->Copy(theI);
1516         raster_info  dest;
1517         dest.startPix=il;
1518         dest.endPix=ir;
1519         dest.sth=il;
1520         dest.stv=y;
1521         dest.buffer=ligStart;
1522         theIL->Raster(dest, NULL, shape_run_A8_OR);
1523         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1524     }
1525     theS->EndQuickRaster();
1526     delete theI;
1527     delete theIL;
1531 /*
1532   Local Variables:
1533   mode:c++
1534   c-file-style:"stroustrup"
1535   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1536   indent-tabs-mode:nil
1537   fill-column:99
1538   End:
1539 */
1540 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :