Code

325681650e17a36b868230b70a7958b4cddc804c
[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 "sp-filter.h"
36 #include "sp-filter-reference.h"
37 #include "display/nr-filter.h"
38 #include <typeinfo>
39 #include <cairo.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, NR::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.set_identity();
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.set_identity();
130     shape->cached_sctm.set_identity();
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::Rect &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::Rect 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(), to_2geom(gc->transform));
258                 item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
259                 item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
260                 item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
261                 item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
262             }
263             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
264                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
265                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
266                 }
267             }
268         }
269         return (state | item->state);
270     }
272     shape->delayed_shp=true;
273     shape->ctm = gc->transform;
274     boundingbox[0][0] = boundingbox[1][0] = NR_HUGE;
275     boundingbox[0][1] = boundingbox[1][1] = -NR_HUGE;
277     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
279     if (shape->curve) {
280         boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), to_2geom(gc->transform));
282         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
283             float width, scale;
284             scale = NR::expansion(gc->transform);
285             width = MAX(0.125, shape->_stroke.width * scale);
286             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
287                 boundingbox.expandBy(width);
288             }
289             // those pesky miters, now
290             float miterMax=width*shape->_stroke.mitre_limit;
291             if ( miterMax > 0.01 ) {
292                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
293                 boundingbox.expandBy(miterMax);
294             }
295         }
296     } else {
297     }
298     shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
299     shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
300     shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
301     shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
302     if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
304     /* Release state data */
305     if (TRUE || !NR::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
306         /* Concept test */
307         if (shape->fill_shp) {
308             delete shape->fill_shp;
309             shape->fill_shp = NULL;
310         }
311     }
312     if (shape->stroke_shp) {
313         delete shape->stroke_shp;
314         shape->stroke_shp = NULL;
315     }
316     if (shape->fill_painter) {
317         sp_painter_free(shape->fill_painter);
318         shape->fill_painter = NULL;
319     }
320     if (shape->stroke_painter) {
321         sp_painter_free(shape->stroke_painter);
322         shape->stroke_painter = NULL;
323     }
325     if (!shape->curve || 
326         !shape->style ||
327         shape->curve->is_empty() ||
328         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
329          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
330     {
331         item->bbox = shape->approx_bbox;
332         return NR_ARENA_ITEM_STATE_ALL;
333     }
335     /* Build state data */
336     if ( shape->delayed_shp ) {
337         item->bbox=shape->approx_bbox;
338     } else {
339         nr_arena_shape_update_stroke(shape, gc, area);
340         nr_arena_shape_update_fill(shape, gc, area);
342         boundingbox[0][0] = boundingbox[0][1] = boundingbox[1][0] = boundingbox[1][1] = 0.0;
343         nr_arena_shape_add_bboxes(shape, boundingbox);
345         shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
346         shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
347         shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
348         shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
349     }
351     if (boundingbox.isEmpty())
352         return NR_ARENA_ITEM_STATE_ALL;
354     item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
355     item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
356     item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.0F);
357     item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.0F);
358     nr_arena_request_render_rect(item->arena, &item->bbox);
360     item->render_opacity = TRUE;
361     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
362         if (gc && gc->parent) {
363             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
364                                                               gc->transform, gc->parent->transform,
365                                                               &shape->paintbox);
366         }
367         item->render_opacity = FALSE;
368     }
369     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
370         if (gc && gc->parent) {
371             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
372                                                                 gc->transform, gc->parent->transform,
373                                                                 &shape->paintbox);
374         }
375         item->render_opacity = FALSE;
376     }
377     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
378            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
379           || (shape->markers)
380         )
381     {
382         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
383         item->render_opacity = FALSE;
384     }
386     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
387         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
388             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
389         }
390     }
392     return NR_ARENA_ITEM_STATE_ALL;
395 int matrix_is_isometry(NR::Matrix p) {
396     NR::Matrix   tp;
397     // transposition
398     tp[0]=p[0];
399     tp[1]=p[2];
400     tp[2]=p[1];
401     tp[3]=p[3];
402     for (int i = 4; i < 6; i++) // shut valgrind up :)
403         tp[i] = p[i] = 0;
404     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
405     // Is the adjunct nearly an identity function?
406     if (isom.is_translation(0.01)) {
407         // the transformation is an isometry -> no need to recompute
408         // the uncrossed polygon
409         if ( p.det() < 0 )
410             return -1;
411         else
412             return 1;
413     }
414     return 0;
417 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
418     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
421 /* returns true if the pathvector has a region that needs fill.
422  * is for optimizing purposes, so should be fast and can falsely return true. 
423  * CANNOT falsely return false. */
424 static bool has_inner_area(Geom::PathVector const & pv) {
425     // return false for the cases where there is surely no region to be filled
426     if (pv.empty())
427         return false;
429     if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
430         // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
431         Geom::Curve const * c = & pv.front().front();
432         if ( dynamic_cast<Geom::LineSegment const*>(c) ||
433              dynamic_cast<Geom::HLineSegment const*>(c) ||
434              dynamic_cast<Geom::VLineSegment const*>(c) )
435         {
436             return false;
437         }
438     }
440     return true; //too costly to see if it has region to be filled, so return true.
443 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
444 void
445 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
447     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
448 //        ((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
449           has_inner_area(shape->curve->get_pathvector()) ) {
450         if (TRUE || !shape->fill_shp) {
451             NR::Matrix  cached_to_new = NR::identity();
452             int isometry = 0;
453             if ( shape->cached_fill ) {
454                 if (shape->cached_fctm == gc->transform) {
455                     isometry = 2; // identity
456                 } else {
457                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
458                     isometry = matrix_is_isometry(cached_to_new);
459                 }
460                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
461                     isometry = 0;
462             }
463             if ( isometry == 0 ) {
464                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
465                 shape->cached_fill->Reset();
467                 Path*  thePath=new Path;
468                 Shape* theShape=new Shape;
469                 {
470                     Geom::Matrix tempMat(to_2geom(gc->transform));
471                     thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
472                 }
474                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
475                     thePath->Convert(1.0);
476                     shape->cached_fpartialy = false;
477                 } else {
478                     thePath->Convert(area, 1.0);
479                     shape->cached_fpartialy = true;
480                 }
482                 thePath->Fill(theShape, 0);
484                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
485                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
486                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
487                     //shape->cached_fill->Copy(theShape);
488                 } else {
489                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
490                 }
491                 shape->cached_fctm=gc->transform;
492                 shape->cached_farea = *area;
493                 delete theShape;
494                 delete thePath;
495                 if ( shape->fill_shp == NULL )
496                     shape->fill_shp = new Shape;
498                 shape->fill_shp->Copy(shape->cached_fill);
500             } else if ( 2 == isometry ) {
501                 if ( shape->fill_shp == NULL ) {
502                     shape->fill_shp = new Shape;
503                     shape->fill_shp->Copy(shape->cached_fill);
504                 }
505             } else {
507                 if ( shape->fill_shp == NULL )
508                     shape->fill_shp = new Shape;
510                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
511                                        shape->cached_fill->numberOfEdges());
512                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
513                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
514                 if ( isometry == 1 ) {
515                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
516                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
517                                                  shape->cached_fill->getEdge(i).en);
518                 } else if ( isometry == -1 ) { // need to flip poly.
519                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
520                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
521                                                  shape->cached_fill->getEdge(i).st);
522                 }
523                 shape->fill_shp->ForceToPolygon();
524                 shape->fill_shp->needPointsSorting();
525                 shape->fill_shp->needEdgesSorting();
526             }
527             shape->delayed_shp |= shape->cached_fpartialy;
528         }
529     }
532 void
533 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
535     SPStyle* style = shape->style;
537     float const scale = NR::expansion(gc->transform);
539     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
541     if (outline) {
542         // cairo does not need the livarot path for rendering
543         return; 
544     }
546     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
547     // (since it will only be used for picking, not for rendering)
549     if (outline ||
550         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
551          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
553         float style_width = MAX(0.125, shape->_stroke.width * scale);
554         float width;
555         if (outline) {
556             width = 0.5; // 1 pixel wide, independent of zoom
557         } else {
558             width = style_width;
559         }
561         NR::Matrix  cached_to_new = NR::identity();
563         int isometry = 0;
564         if ( shape->cached_stroke ) {
565             if (shape->cached_sctm == gc->transform) {
566                 isometry = 2; // identity
567             } else {
568                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
569                 isometry = matrix_is_isometry(cached_to_new);
570             }
571             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
572                 isometry = 0;
573             if (0 != isometry && width != shape->cached_width) { 
574                 // if this happens without setting style, we have just switched to outline or back
575                 isometry = 0; 
576             } 
577         }
579         if ( isometry == 0 ) {
580             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
581             shape->cached_stroke->Reset();
582             Path*  thePath = new Path;
583             Shape* theShape = new Shape;
584             {
585                 Geom::Matrix   tempMat( to_2geom(gc->transform) );
586                 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
587             }
589             // add some padding to the rendering area, so clipped path does not go into a render area
590             NRRectL padded_area = *area;
591             padded_area.x0 -= (NR::ICoord)width;
592             padded_area.x1 += (NR::ICoord)width;
593             padded_area.y0 -= (NR::ICoord)width;
594             padded_area.y1 += (NR::ICoord)width;
595             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
596                 thePath->Convert((outline) ? 4.0 : 1.0);
597                 shape->cached_spartialy = false;
598             }
599             else {
600                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
601                 shape->cached_spartialy = true;
602             }
604             if (style->stroke_dash.n_dash && !outline) {
605                 thePath->DashPolylineFromStyle(style, scale, 1.0);
606             }
608             ButtType butt=butt_straight;
609             switch (shape->_stroke.cap) {
610                 case NRArenaShape::BUTT_CAP:
611                     butt = butt_straight;
612                     break;
613                 case NRArenaShape::ROUND_CAP:
614                     butt = butt_round;
615                     break;
616                 case NRArenaShape::SQUARE_CAP:
617                     butt = butt_square;
618                     break;
619             }
620             JoinType join=join_straight;
621             switch (shape->_stroke.join) {
622                 case NRArenaShape::MITRE_JOIN:
623                     join = join_pointy;
624                     break;
625                 case NRArenaShape::ROUND_JOIN:
626                     join = join_round;
627                     break;
628                 case NRArenaShape::BEVEL_JOIN:
629                     join = join_straight;
630                     break;
631             }
633             if (outline) {
634                 butt = butt_straight;
635                 join = join_straight;
636             }
638             thePath->Stroke(theShape, false, 0.5*width, join, butt,
639                             0.5*width*shape->_stroke.mitre_limit);
642             if (outline) {
643                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
644                 shape->cached_stroke->Copy(theShape);
645             } else {
646                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
647             }
649             shape->cached_width = width;
651             shape->cached_sctm=gc->transform;
652             shape->cached_sarea = *area;
653             delete thePath;
654             delete theShape;
655             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
657             shape->stroke_shp->Copy(shape->cached_stroke);
659         } else if ( 2 == isometry ) {
660             if ( shape->stroke_shp == NULL ) {
661                 shape->stroke_shp=new Shape;
662                 shape->stroke_shp->Copy(shape->cached_stroke);
663             }
664         } else {
665             if ( shape->stroke_shp == NULL )
666                 shape->stroke_shp=new Shape;
667             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
668             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
669                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
670             if ( isometry == 1 ) {
671                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
672                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
673                                                shape->cached_stroke->getEdge(i).en);
674             } else if ( isometry == -1 ) {
675                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
676                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
677                                                shape->cached_stroke->getEdge(i).st);
678             }
679             shape->stroke_shp->ForceToPolygon();
680             shape->stroke_shp->needPointsSorting();
681             shape->stroke_shp->needEdgesSorting();
682         }
683         shape->delayed_shp |= shape->cached_spartialy;
684     }
688 void
689 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox)
691     /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
692      * if so, then this can be written much more compact ! */
694     if ( shape->stroke_shp ) {
695         Shape *larger = shape->stroke_shp;
696         larger->CalcBBox();
697         larger->leftX   = floor(larger->leftX);
698         larger->rightX  = ceil(larger->rightX);
699         larger->topY    = floor(larger->topY);
700         larger->bottomY = ceil(larger->bottomY);
701         Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
702                                 Geom::Interval(larger->topY,  larger->bottomY) );
703         bbox.unionWith(stroke_bbox);
704     }
706     if ( shape->fill_shp ) {
707         Shape *larger = shape->fill_shp;
708         larger->CalcBBox();
709         larger->leftX   = floor(larger->leftX);
710         larger->rightX  = ceil(larger->rightX);
711         larger->topY    = floor(larger->topY);
712         larger->bottomY = ceil(larger->bottomY);
713         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
714                               Geom::Interval(larger->topY,  larger->bottomY) );
715         bbox.unionWith(fill_bbox);
716     }
719 // cairo outline rendering:
720 static unsigned int
721 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
723     NRArenaShape *shape = NR_ARENA_SHAPE(item);
725     if (!ct) 
726         return item->state;
728     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
729     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
730     // by setting color channels in the "wrong" order
731     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));
733     cairo_set_line_width(ct, 0.5);
734     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
735     cairo_new_path(ct);
737     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area, true, 0);
739     cairo_stroke(ct);
741     return item->state;
744 // cairo stroke rendering (flat color only so far!):
745 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
746 // (need to switch them to premul before this can be enabled)
747 void
748 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
750     NRArenaShape *shape = NR_ARENA_SHAPE(item);
751     SPStyle const *style = shape->style;
753     float const scale = NR::expansion(shape->ctm);
755     if (fabs(shape->_stroke.width * scale) < 0.01)
756         return;
758     cairo_t *ct = nr_create_cairo_context (area, pb);
760     if (!ct)
761         return;
763     guint32 rgba;
764     if ( item->render_opacity ) {
765         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
766                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
767     } else {
768         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
769     }
771     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
772     // by setting color channels in the "wrong" order
773     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));
775     float style_width = MAX(0.125, shape->_stroke.width * scale);
776     cairo_set_line_width(ct, style_width);
778     switch (shape->_stroke.cap) {
779         case NRArenaShape::BUTT_CAP:
780             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
781             break;
782         case NRArenaShape::ROUND_CAP:
783             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
784             break;
785         case NRArenaShape::SQUARE_CAP:
786             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
787             break;
788     }
789     switch (shape->_stroke.join) {
790         case NRArenaShape::MITRE_JOIN:
791             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
792             break;
793         case NRArenaShape::ROUND_JOIN:
794             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
795             break;
796         case NRArenaShape::BEVEL_JOIN:
797             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
798             break;
799     }
801     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
803     if (style->stroke_dash.n_dash) {
804         NRVpathDash dash;
805         dash.offset = style->stroke_dash.offset * scale;
806         dash.n_dash = style->stroke_dash.n_dash;
807         dash.dash = g_new(double, dash.n_dash);
808         for (int i = 0; i < dash.n_dash; i++) {
809             dash.dash[i] = style->stroke_dash.dash[i] * scale;
810         }
811         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
812         g_free(dash.dash);
813     }
815     cairo_set_tolerance(ct, 0.1);
816     cairo_new_path(ct);
818     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area->upgrade(), true, style_width);
820     cairo_stroke(ct);
822     cairo_surface_t *cst = cairo_get_target(ct);
823     cairo_destroy (ct);
824     cairo_surface_finish (cst);
825     cairo_surface_destroy (cst);
827     pb->empty = FALSE;
831 /**
832  * Renders the item.  Markers are just composed into the parent buffer.
833  */
834 static unsigned int
835 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
837     NRArenaShape *shape = NR_ARENA_SHAPE(item);
839     if (!shape->curve) return item->state;
840     if (!shape->style) return item->state;
842     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
844     if (outline) { // cairo outline rendering
846         pb->empty = FALSE;
847         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
848         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
850     } else {
852     if ( shape->delayed_shp ) {
853         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
854             NRGC   tempGC(NULL);
855             tempGC.transform=shape->ctm;
856             shape->delayed_shp = false;
857             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
858             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
859 /*      NRRect bbox;
860         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
861         nr_arena_shape_add_bboxes(shape,bbox);
862         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
863         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
864         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
865         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
866         shape->approx_bbox=item->bbox;*/
867         } else {
868             return item->state;
869         }
870     }
872     SPStyle const *style = shape->style;
873     if (shape->fill_shp) {
874         NRPixBlock m;
875         guint32 rgba;
877         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
879         // if memory allocation failed, abort render
880         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
881             nr_pixblock_release (&m);
882             return (item->state);
883         }
885         m.visible_area = pb->visible_area;
886         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
887         m.empty = FALSE;
889         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
890             // do not render fill in any way
891         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
892             if ( item->render_opacity ) {
893                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
894                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
895             } else {
896                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
897             }
898             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
899             pb->empty = FALSE;
900         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
901             if (shape->fill_painter) {
902                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
903             }
904         }
906         nr_pixblock_release(&m);
907     }
909     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
911         // cairo_arena_shape_render_stroke(item, area, pb);
913         guint32 rgba;
914         NRPixBlock m;
916         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
918         // if memory allocation failed, abort render
919         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
920             nr_pixblock_release (&m);
921             return (item->state);
922         }
924         m.visible_area = pb->visible_area;
925         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
926         m.empty = FALSE;
928             if ( item->render_opacity ) {
929                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
930                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
931             } else {
932                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
933             }
934             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
935             pb->empty = FALSE;
937         nr_pixblock_release(&m);
939     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
941         NRPixBlock m;
943         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
945         // if memory allocation failed, abort render
946         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
947             nr_pixblock_release (&m);
948             return (item->state);
949         }
951         m.visible_area = pb->visible_area; 
952         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
953         m.empty = FALSE;
955         if (shape->stroke_painter) {
956             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
957         }
959         nr_pixblock_release(&m);
960     }
962     } // non-cairo non-outline branch
964     /* Render markers into parent buffer */
965     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
966         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
967         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
968     }
970     return item->state;
974 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
975 // reenable this when the bug is fixed and remove the rest of this function
976 // TODO
977 #if defined(DEADCODE) && !defined(DEADCODE)
978 static guint
979 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
981     NRArenaShape *shape = NR_ARENA_SHAPE(item);
982     if (!shape->curve) return item->state;
984         cairo_t *ct = nr_create_cairo_context (area, pb);
986         if (!ct)
987             return item->state;
989         cairo_set_source_rgba(ct, 0, 0, 0, 1);
991         cairo_new_path(ct);
993         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), (area)->upgrade(), false, 0);
995         cairo_fill(ct);
997         cairo_surface_t *cst = cairo_get_target(ct);
998         cairo_destroy (ct);
999         cairo_surface_finish (cst);
1000         cairo_surface_destroy (cst);
1002         pb->empty = FALSE;
1004         return item->state;
1006 #endif //defined(DEADCODE) && !defined(DEADCODE)
1009 static guint
1010 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1012     //return cairo_arena_shape_clip(item, area, pb);
1014     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1015     if (!shape->curve) return item->state;
1017     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1018         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1019             NRGC   tempGC(NULL);
1020             tempGC.transform=shape->ctm;
1021             shape->delayed_shp = false;
1022             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1023         } else {
1024             return item->state;
1025         }
1026     }
1028     if ( shape->fill_shp ) {
1029         NRPixBlock m;
1031         /* fixme: We can OR in one step (Lauris) */
1032         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1034         // if memory allocation failed, abort 
1035         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1036             nr_pixblock_release (&m);
1037             return (item->state);
1038         }
1040         m.visible_area = pb->visible_area; 
1041         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1043         for (int y = area->y0; y < area->y1; y++) {
1044             unsigned char *s, *d;
1045             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1046             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1047             for (int x = area->x0; x < area->x1; x++) {
1048                 *d = NR_COMPOSEA_111(*s, *d);
1049                 d ++;
1050                 s ++;
1051             }
1052         }
1053         nr_pixblock_release(&m);
1054         pb->empty = FALSE;
1055     }
1057     return item->state;
1060 static NRArenaItem *
1061 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1063     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1065     if (shape->repick_after > 0)
1066         shape->repick_after--;
1068     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1069         return shape->last_pick;
1071     if (!shape->curve) return NULL;
1072     if (!shape->style) return NULL;
1074     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1076     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
1077         // fully transparent, no pick unless outline mode
1078         return NULL;
1080     GTimeVal tstart, tfinish;
1081     g_get_current_time (&tstart);
1083     double width;
1084     if (outline) {
1085         width = 0.5;
1086     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1087         float const scale = NR::expansion(shape->ctm);
1088         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1089     } else {
1090         width = 0;
1091     }
1093     double dist = NR_HUGE;
1094     int wind = 0;
1095     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1096              && shape->_fill.opacity > 1e-3 && !outline);
1098     if (item->arena->canvasarena) {
1099         Geom::Rect viewbox = to_2geom(item->arena->canvasarena->item.canvas->getViewbox());
1100         viewbox.expandBy (width);
1101         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), to_2geom(shape->ctm), to_2geom(p), NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1102     } else {
1103         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), to_2geom(shape->ctm), to_2geom(p), NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1104     }
1106     g_get_current_time (&tfinish);
1107     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1108     //g_print ("pick time %lu\n", this_pick);
1110     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1111         shape->repick_after = this_pick / 5000;
1112     }
1114     // covered by fill?
1115     if (needfill) {
1116         if (!shape->style->fill_rule.computed) {
1117             if (wind != 0) {
1118                 shape->last_pick = item;
1119                 return item;
1120             }
1121         } else {
1122             if (wind & 0x1) {
1123                 shape->last_pick = item;
1124                 return item;
1125             }
1126         }
1127     }
1129     // close to the edge, as defined by strokewidth and delta?
1130     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1131     if (needfill || width > 0) { // if either fill or stroke visible,
1132         if ((dist - width) < delta) {
1133             shape->last_pick = item;
1134             return item;
1135         }
1136     }
1138     // if not picked on the shape itself, try its markers
1139     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1140         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1141         if (ret) {
1142             shape->last_pick = item;
1143             return item;
1144         }
1145     }
1147     shape->last_pick = NULL;
1148     return NULL;
1151 /**
1152  *
1153  *  Requests a render of the shape, then if the shape is already a curve it
1154  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1155  *  curve and adds it to the shape.  Finally, it requests an update of the
1156  *  arena for the shape.
1157  */
1158 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1160     g_return_if_fail(shape != NULL);
1161     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1163     if ( justTrans == false ) {
1164         // dirty cached versions
1165         if ( shape->cached_fill ) {
1166             delete shape->cached_fill;
1167             shape->cached_fill=NULL;
1168         }
1169         if ( shape->cached_stroke ) {
1170             delete shape->cached_stroke;
1171             shape->cached_stroke=NULL;
1172         }
1173     }
1175     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1177     if (shape->curve) {
1178         shape->curve->unref();
1179         shape->curve = NULL;
1180     }
1182     if (curve) {
1183         shape->curve = curve;
1184         curve->ref();
1185     }
1187     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1190 void NRArenaShape::setFill(SPPaintServer *server) {
1191     _fill.paint.set(server);
1192     _invalidateCachedFill();
1195 void NRArenaShape::setFill(SPColor const &color) {
1196     _fill.paint.set(color);
1197     _invalidateCachedFill();
1200 void NRArenaShape::setFillOpacity(double opacity) {
1201     _fill.opacity = opacity;
1202     _invalidateCachedFill();
1205 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1206     _fill.rule = rule;
1207     _invalidateCachedFill();
1210 void NRArenaShape::setStroke(SPPaintServer *server) {
1211     _stroke.paint.set(server);
1212     _invalidateCachedStroke();
1215 void NRArenaShape::setStroke(SPColor const &color) {
1216     _stroke.paint.set(color);
1217     _invalidateCachedStroke();
1220 void NRArenaShape::setStrokeOpacity(double opacity) {
1221     _stroke.opacity = opacity;
1222     _invalidateCachedStroke();
1225 void NRArenaShape::setStrokeWidth(double width) {
1226     _stroke.width = width;
1227     _invalidateCachedStroke();
1230 void NRArenaShape::setMitreLimit(double limit) {
1231     _stroke.mitre_limit = limit;
1232     _invalidateCachedStroke();
1235 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1236     _stroke.cap = cap;
1237     _invalidateCachedStroke();
1240 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1241     _stroke.join = join;
1242     _invalidateCachedStroke();
1245 /** nr_arena_shape_set_style
1246  *
1247  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1248  */
1249 void
1250 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1252     g_return_if_fail(shape != NULL);
1253     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1255     if (style) sp_style_ref(style);
1256     if (shape->style) sp_style_unref(shape->style);
1257     shape->style = style;
1259     if ( style->fill.isPaintserver() ) {
1260         shape->setFill(style->getFillPaintServer());
1261     } else if ( style->fill.isColor() ) {
1262         shape->setFill(style->fill.value.color);
1263     } else if ( style->fill.isNone() ) {
1264         shape->setFill(NULL);
1265     } else {
1266         g_assert_not_reached();
1267     }
1268     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1269     switch (style->fill_rule.computed) {
1270         case SP_WIND_RULE_EVENODD: {
1271             shape->setFillRule(NRArenaShape::EVEN_ODD);
1272             break;
1273         }
1274         case SP_WIND_RULE_NONZERO: {
1275             shape->setFillRule(NRArenaShape::NONZERO);
1276             break;
1277         }
1278         default: {
1279             g_assert_not_reached();
1280         }
1281     }
1283     if ( style->stroke.isPaintserver() ) {
1284         shape->setStroke(style->getStrokePaintServer());
1285     } else if ( style->stroke.isColor() ) {
1286         shape->setStroke(style->stroke.value.color);
1287     } else if ( style->stroke.isNone() ) {
1288         shape->setStroke(NULL);
1289     } else {
1290         g_assert_not_reached();
1291     }
1292     shape->setStrokeWidth(style->stroke_width.computed);
1293     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1294     switch (style->stroke_linecap.computed) {
1295         case SP_STROKE_LINECAP_ROUND: {
1296             shape->setLineCap(NRArenaShape::ROUND_CAP);
1297             break;
1298         }
1299         case SP_STROKE_LINECAP_SQUARE: {
1300             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1301             break;
1302         }
1303         case SP_STROKE_LINECAP_BUTT: {
1304             shape->setLineCap(NRArenaShape::BUTT_CAP);
1305             break;
1306         }
1307         default: {
1308             g_assert_not_reached();
1309         }
1310     }
1311     switch (style->stroke_linejoin.computed) {
1312         case SP_STROKE_LINEJOIN_ROUND: {
1313             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1314             break;
1315         }
1316         case SP_STROKE_LINEJOIN_BEVEL: {
1317             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1318             break;
1319         }
1320         case SP_STROKE_LINEJOIN_MITER: {
1321             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1322             break;
1323         }
1324         default: {
1325             g_assert_not_reached();
1326         }
1327     }
1328     shape->setMitreLimit(style->stroke_miterlimit.value);
1330     //if shape has a filter
1331     if (style->filter.set && style->getFilter()) {
1332         if (!shape->filter) {
1333             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1334             shape->filter = new NR::Filter(primitives);
1335         }
1336         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1337     } else {
1338         //no filter set for this shape
1339         delete shape->filter;
1340         shape->filter = NULL;
1341     }
1343     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1346 void
1347 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1349     g_return_if_fail(shape != NULL);
1350     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1351     g_return_if_fail(pbox != NULL);
1353     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1354         shape->paintbox = *pbox;
1355     } else {
1356         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1357         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1358         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1359     }
1361     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1364 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1366     paintbox.x0 = pbox.min()[NR::X];
1367     paintbox.y0 = pbox.min()[NR::Y];
1368     paintbox.x1 = pbox.max()[NR::X];
1369     paintbox.y1 = pbox.max()[NR::Y];
1371     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1374 static void
1375 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1377     if ( st >= en ) return;
1378     if ( vst < 0 ) vst=0;
1379     if ( vst > 1 ) vst=1;
1380     if ( ven < 0 ) ven=0;
1381     if ( ven > 1 ) ven=1;
1382     float   sv=vst;
1383     float   dv=ven-vst;
1384     int     len=en-st;
1385     unsigned char*   d=(unsigned char*)dest.buffer;
1386     d+=(st-dest.startPix);
1387     if ( fabs(dv) < 0.001 ) {
1388         if ( vst > 0.999 ) {
1389             /* Simple copy */
1390             while (len > 0) {
1391                 d[0] = 255;
1392                 d += 1;
1393                 len -= 1;
1394             }
1395         } else {
1396             sv*=256;
1397             unsigned int c0_24=(int)sv;
1398             c0_24&=0xFF;
1399             while (len > 0) {
1400                 /* Draw */
1401                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1402                 d += 1;
1403                 len -= 1;
1404             }
1405         }
1406     } else {
1407         if ( en <= st+1 ) {
1408             sv=0.5*(vst+ven);
1409             sv*=256;
1410             unsigned int c0_24=(int)sv;
1411             c0_24&=0xFF;
1412             /* Draw */
1413             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1414         } else {
1415             dv/=len;
1416             sv+=0.5*dv; // correction trapezoidale
1417             sv*=16777216;
1418             dv*=16777216;
1419             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1420             int s0_24 = static_cast<int>(dv);
1421             while (len > 0) {
1422                 unsigned int ca;
1423                 /* Draw */
1424                 ca = c0_24 >> 16;
1425                 if ( ca > 255 ) ca=255;
1426                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1427                 d += 1;
1428                 c0_24 += s0_24;
1429                 c0_24 = CLAMP(c0_24, 0, 16777216);
1430                 len -= 1;
1431             }
1432         }
1433     }
1436 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1438     theS->CalcBBox();
1439     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1440     int    il,ir,it,ib;
1441     il=(int)floor(l);
1442     ir=(int)ceil(r);
1443     it=(int)floor(t);
1444     ib=(int)ceil(b);
1446     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1447     if ( il < m.area.x0 ) il=m.area.x0;
1448     if ( it < m.area.y0 ) it=m.area.y0;
1449     if ( ir > m.area.x1 ) ir=m.area.x1;
1450     if ( ib > m.area.y1 ) ib=m.area.y1;
1452     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1453     int    curPt;
1454     float  curY;
1455     theS->BeginQuickRaster(curY, curPt);
1457     FloatLigne *theI = new FloatLigne();
1458     IntLigne *theIL = new IntLigne();
1460     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1462     char *mdata = (char*)m.data.px;
1463     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1464     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1465     for (int y = it; y < ib; y++) {
1466         theI->Reset();
1467         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1468         theI->Flatten();
1469         theIL->Copy(theI);
1471         raster_info  dest;
1472         dest.startPix=il;
1473         dest.endPix=ir;
1474         dest.sth=il;
1475         dest.stv=y;
1476         dest.buffer=ligStart;
1477         theIL->Raster(dest, NULL, shape_run_A8_OR);
1478         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1479     }
1480     theS->EndQuickRaster();
1481     delete theI;
1482     delete theIL;
1486 /*
1487   Local Variables:
1488   mode:c++
1489   c-file-style:"stroustrup"
1490   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1491   indent-tabs-mode:nil
1492   fill-column:99
1493   End:
1494 */
1495 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :