Code

96ea76cbef83f14dc6d97c96d437e43dacab1fac
[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>
39 #include <glib.h>
40 #include "svg/svg.h"
41 #include <fenv.h>
43 //int  showRuns=0;
44 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
46 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
47 static void nr_arena_shape_init(NRArenaShape *shape);
48 static void nr_arena_shape_finalize(NRObject *object);
50 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
51 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
52 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
53 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
55 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
56 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
57 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
58 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int sticky);
60 static NRArenaItemClass *shape_parent_class;
62 NRType
63 nr_arena_shape_get_type(void)
64 {
65     static NRType type = 0;
66     if (!type) {
67         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
68                                        "NRArenaShape",
69                                        sizeof(NRArenaShapeClass),
70                                        sizeof(NRArenaShape),
71                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
72                                        (void (*)(NRObject *)) nr_arena_shape_init);
73     }
74     return type;
75 }
77 static void
78 nr_arena_shape_class_init(NRArenaShapeClass *klass)
79 {
80     NRObjectClass *object_class;
81     NRArenaItemClass *item_class;
83     object_class = (NRObjectClass *) klass;
84     item_class = (NRArenaItemClass *) klass;
86     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
88     object_class->finalize = nr_arena_shape_finalize;
89     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
91     item_class->children = nr_arena_shape_children;
92     item_class->add_child = nr_arena_shape_add_child;
93     item_class->set_child_position = nr_arena_shape_set_child_position;
94     item_class->remove_child = nr_arena_shape_remove_child;
95     item_class->update = nr_arena_shape_update;
96     item_class->render = nr_arena_shape_render;
97     item_class->clip = nr_arena_shape_clip;
98     item_class->pick = nr_arena_shape_pick;
99 }
101 /**
102  * Initializes the arena shape, setting all parameters to null, 0, false,
103  * or other defaults
104  */
105 static void
106 nr_arena_shape_init(NRArenaShape *shape)
108     shape->curve = NULL;
109     shape->style = NULL;
110     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
111     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
113     shape->ctm.setIdentity();
114     shape->fill_painter = NULL;
115     shape->stroke_painter = NULL;
116     shape->cached_fill = NULL;
117     shape->cached_stroke = NULL;
118     shape->cached_fpartialy = false;
119     shape->cached_spartialy = false;
120     shape->fill_shp = NULL;
121     shape->stroke_shp = NULL;
123     shape->delayed_shp = false;
125     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
126     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
127     shape->cached_fctm.setIdentity();
128     shape->cached_sctm.setIdentity();
130     shape->markers = NULL;
132     shape->last_pick = NULL;
133     shape->repick_after = 0;
136 static void
137 nr_arena_shape_finalize(NRObject *object)
139     NRArenaShape *shape = (NRArenaShape *) object;
141     if (shape->fill_shp) delete shape->fill_shp;
142     if (shape->stroke_shp) delete shape->stroke_shp;
143     if (shape->cached_fill) delete shape->cached_fill;
144     if (shape->cached_stroke) delete shape->cached_stroke;
145     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
146     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
148     if (shape->style) sp_style_unref(shape->style);
149     if (shape->curve) shape->curve->unref();
151     ((NRObjectClass *) shape_parent_class)->finalize(object);
154 /**
155  * Retrieves the markers from the item
156  */
157 static NRArenaItem *
158 nr_arena_shape_children(NRArenaItem *item)
160     NRArenaShape *shape = (NRArenaShape *) item;
162     return shape->markers;
165 /**
166  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
167  * the prev and next items.  If ref is NULL, then it sets the item's markers
168  * as the next items.
169  */
170 static void
171 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
173     NRArenaShape *shape = (NRArenaShape *) item;
175     if (!ref) {
176         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
177     } else {
178         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
179     }
181     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
184 /**
185  * Removes child from the shape.  If there are no prev items in 
186  * the child, it sets items' markers to the next item in the child.
187  */
188 static void
189 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
191     NRArenaShape *shape = (NRArenaShape *) item;
193     if (child->prev) {
194         nr_arena_item_detach(item, child);
195     } else {
196         shape->markers = nr_arena_item_detach(item, child);
197     }
199     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
202 /**
203  * Detaches child from item, and if there are no previous items in child, it 
204  * sets item's markers to the child.  It then attaches the child back onto the item.
205  * If ref is null, it sets the markers to be the next item, otherwise it uses
206  * the next/prev items in ref.
207  */
208 static void
209 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
211     NRArenaShape *shape = (NRArenaShape *) item;
213     if (child->prev) {
214         nr_arena_item_detach(item, child);
215     } else {
216         shape->markers = nr_arena_item_detach(item, child);
217     }
219     if (!ref) {
220         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
221     } else {
222         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
223     }
225     nr_arena_item_request_render(child);
228 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
229 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
230 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox);
232 /**
233  * Updates the arena shape 'item' and all of its children, including the markers.
234  */
235 static guint
236 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
238     Geom::OptRect boundingbox;
240     NRArenaShape *shape = NR_ARENA_SHAPE(item);
242     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
244     unsigned int newstate;
245     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
246         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
247         beststate = beststate & newstate;
248     }
250     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
251         /* We do not have to create rendering structures */
252         shape->ctm = gc->transform;
253         if (state & NR_ARENA_ITEM_STATE_BBOX) {
254             if (shape->curve) {
255                 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
256                 /// \todo  just write item->bbox = boundingbox
257                 if (boundingbox) {
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                 } else {
263                     item->bbox = NR_RECT_L_EMPTY;
264                 }
265             }
266             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
267                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
268                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
269                 }
270             }
271         }
272         return (state | item->state);
273     }
275     shape->delayed_shp=true;
276     shape->ctm = gc->transform;
277     boundingbox = Geom::OptRect();
279     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
281     if (shape->curve) {
282         boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
284         if (boundingbox && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
285             float width, scale;
286             scale = gc->transform.descrim();
287             width = MAX(0.125, shape->_stroke.width * scale);
288             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
289                 boundingbox->expandBy(width);
290             }
291             // those pesky miters, now
292             float miterMax=width*shape->_stroke.mitre_limit;
293             if ( miterMax > 0.01 ) {
294                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
295                 boundingbox->expandBy(miterMax);
296             }
297         }
298     } 
300     /// \todo  just write item->bbox = boundingbox
301     if (boundingbox) {
302         shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
303         shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
304         shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
305         shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
306     } else {
307         shape->approx_bbox = NR_RECT_L_EMPTY;
308     }
309     if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
311     /* Release state data */
312     if (shape->fill_shp) {
313         delete shape->fill_shp;
314         shape->fill_shp = NULL;
315     }
316     if (shape->stroke_shp) {
317         delete shape->stroke_shp;
318         shape->stroke_shp = NULL;
319     }
320     if (shape->fill_painter) {
321         sp_painter_free(shape->fill_painter);
322         shape->fill_painter = NULL;
323     }
324     if (shape->stroke_painter) {
325         sp_painter_free(shape->stroke_painter);
326         shape->stroke_painter = NULL;
327     }
329     if (!shape->curve || 
330         !shape->style ||
331         shape->curve->is_empty() ||
332         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
333          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
334     {
335         item->bbox = shape->approx_bbox;
336         return NR_ARENA_ITEM_STATE_ALL;
337     }
339     /* Build state data */
340     if ( shape->delayed_shp ) {
341         item->bbox=shape->approx_bbox;
342     } else {
343         nr_arena_shape_update_stroke(shape, gc, area);
344         nr_arena_shape_update_fill(shape, gc, area);
346         boundingbox = Geom::OptRect();
347         nr_arena_shape_add_bboxes(shape, boundingbox);
349         /// \todo  just write shape->approx_bbox = boundingbox
350         if (boundingbox) {
351             shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
352             shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
353             shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
354             shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
355         } else {
356             shape->approx_bbox = NR_RECT_L_EMPTY;
357         }
358     }
360     if (!boundingbox)
361         return NR_ARENA_ITEM_STATE_ALL;
363     /// \todo  just write item->bbox = boundingbox
364     item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
365     item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
366     item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.0F);
367     item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.0F);
369     item->render_opacity = TRUE;
370     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
371         if (gc && gc->parent) {
372             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
373                                                               gc->transform, gc->parent->transform,
374                                                               &shape->paintbox);
375         }
376         item->render_opacity = FALSE;
377     }
378     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
379         if (gc && gc->parent) {
380             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
381                                                                 gc->transform, gc->parent->transform,
382                                                                 &shape->paintbox);
383         }
384         item->render_opacity = FALSE;
385     }
386     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
387            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
388           || (shape->markers)
389         )
390     {
391         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
392         item->render_opacity = FALSE;
393     }
395     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
396         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
397             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
398         }
399     }
401     return NR_ARENA_ITEM_STATE_ALL;
404 int matrix_is_isometry(Geom::Matrix p) {
405     Geom::Matrix   tp;
406     // transposition
407     tp[0]=p[0];
408     tp[1]=p[2];
409     tp[2]=p[1];
410     tp[3]=p[3];
411     for (int i = 4; i < 6; i++) // shut valgrind up :)
412         tp[i] = p[i] = 0;
413     Geom::Matrix   isom = tp*p; // A^T * A = adjunct?
414     // Is the adjunct nearly an identity function?
415     if (isom.isTranslation(0.01)) {
416         // the transformation is an isometry -> no need to recompute
417         // the uncrossed polygon
418         if ( p.det() < 0 )
419             return -1;
420         else
421             return 1;
422     }
423     return 0;
426 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
427     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
430 /* returns true if the pathvector has a region that needs fill.
431  * is for optimizing purposes, so should be fast and can falsely return true. 
432  * CANNOT falsely return false. */
433 static bool has_inner_area(Geom::PathVector const & pv) {
434     // return false for the cases where there is surely no region to be filled
435     if (pv.empty())
436         return false;
438     if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
439         // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
440         if ( is_straight_curve(pv.front().front()) )
441         {
442             return false;
443         }
444     }
446     return true; //too costly to see if it has region to be filled, so return true.
449 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
450 void
451 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
453     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
454           has_inner_area(shape->curve->get_pathvector()) ) {
456             Geom::Matrix  cached_to_new = Geom::identity();
457             int isometry = 0;
458             if ( shape->cached_fill ) {
459                 if (shape->cached_fctm == gc->transform) {
460                     isometry = 2; // identity
461                 } else {
462                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
463                     isometry = matrix_is_isometry(cached_to_new);
464                 }
465                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
466                     isometry = 0;
467             }
468             if ( isometry == 0 ) {
469                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
470                 shape->cached_fill->Reset();
472                 Path*  thePath=new Path;
473                 Shape* theShape=new Shape;
474                 {
475                     Geom::Matrix tempMat(gc->transform);
476                     thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
477                 }
479                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
480                     thePath->Convert(1.0);
481                     shape->cached_fpartialy = false;
482                 } else {
483                     thePath->Convert(area, 1.0);
484                     shape->cached_fpartialy = true;
485                 }
487                 thePath->Fill(theShape, 0);
489                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
490                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
491                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
492                     //shape->cached_fill->Copy(theShape);
493                 } else {
494                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
495                 }
496                 shape->cached_fctm=gc->transform;
497                 shape->cached_farea = *area;
498                 delete theShape;
499                 delete thePath;
500                 if ( shape->fill_shp == NULL )
501                     shape->fill_shp = new Shape;
503                 shape->fill_shp->Copy(shape->cached_fill);
505             } else if ( 2 == isometry ) {
506                 if ( shape->fill_shp == NULL ) {
507                     shape->fill_shp = new Shape;
508                     shape->fill_shp->Copy(shape->cached_fill);
509                 }
510             } else {
512                 if ( shape->fill_shp == NULL )
513                     shape->fill_shp = new Shape;
515                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
516                                        shape->cached_fill->numberOfEdges());
517                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
518                     shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
519                 if ( isometry == 1 ) {
520                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
521                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
522                                                  shape->cached_fill->getEdge(i).en);
523                 } else if ( isometry == -1 ) { // need to flip poly.
524                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
525                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
526                                                  shape->cached_fill->getEdge(i).st);
527                 }
528                 shape->fill_shp->ForceToPolygon();
529                 shape->fill_shp->needPointsSorting();
530                 shape->fill_shp->needEdgesSorting();
531             }
532             shape->delayed_shp |= shape->cached_fpartialy;
533     }
536 void
537 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
539     SPStyle* style = shape->style;
541     float const scale = gc->transform.descrim();
543     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
545     if (outline) {
546         // cairo does not need the livarot path for rendering
547         return; 
548     }
550     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
551     // (since it will only be used for picking, not for rendering)
553     if (outline ||
554         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
555          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
557         float style_width = MAX(0.125, shape->_stroke.width * scale);
558         float width;
559         if (outline) {
560             width = 0.5; // 1 pixel wide, independent of zoom
561         } else {
562             width = style_width;
563         }
565         Geom::Matrix  cached_to_new = Geom::identity();
567         int isometry = 0;
568         if ( shape->cached_stroke ) {
569             if (shape->cached_sctm == gc->transform) {
570                 isometry = 2; // identity
571             } else {
572                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
573                 isometry = matrix_is_isometry(cached_to_new);
574             }
575             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
576                 isometry = 0;
577             if (0 != isometry && width != shape->cached_width) { 
578                 // if this happens without setting style, we have just switched to outline or back
579                 isometry = 0; 
580             } 
581         }
583         if ( isometry == 0 ) {
584             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
585             shape->cached_stroke->Reset();
586             Path*  thePath = new Path;
587             Shape* theShape = new Shape;
588             {
589                 Geom::Matrix   tempMat( gc->transform );
590                 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
591             }
593             // add some padding to the rendering area, so clipped path does not go into a render area
594             NRRectL padded_area = *area;
595             padded_area.x0 -= (NR::ICoord)width;
596             padded_area.x1 += (NR::ICoord)width;
597             padded_area.y0 -= (NR::ICoord)width;
598             padded_area.y1 += (NR::ICoord)width;
599             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
600                 thePath->Convert((outline) ? 4.0 : 1.0);
601                 shape->cached_spartialy = false;
602             }
603             else {
604                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
605                 shape->cached_spartialy = true;
606             }
608             if (style->stroke_dash.n_dash && !outline) {
609                 thePath->DashPolylineFromStyle(style, scale, 1.0);
610             }
612             ButtType butt=butt_straight;
613             switch (shape->_stroke.cap) {
614                 case NRArenaShape::BUTT_CAP:
615                     butt = butt_straight;
616                     break;
617                 case NRArenaShape::ROUND_CAP:
618                     butt = butt_round;
619                     break;
620                 case NRArenaShape::SQUARE_CAP:
621                     butt = butt_square;
622                     break;
623             }
624             JoinType join=join_straight;
625             switch (shape->_stroke.join) {
626                 case NRArenaShape::MITRE_JOIN:
627                     join = join_pointy;
628                     break;
629                 case NRArenaShape::ROUND_JOIN:
630                     join = join_round;
631                     break;
632                 case NRArenaShape::BEVEL_JOIN:
633                     join = join_straight;
634                     break;
635             }
637             if (outline) {
638                 butt = butt_straight;
639                 join = join_straight;
640             }
642             thePath->Stroke(theShape, false, 0.5*width, join, butt,
643                             0.5*width*shape->_stroke.mitre_limit);
646             if (outline) {
647                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
648                 shape->cached_stroke->Copy(theShape);
649             } else {
650                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
651             }
653             shape->cached_width = width;
655             shape->cached_sctm=gc->transform;
656             shape->cached_sarea = *area;
657             delete thePath;
658             delete theShape;
659             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
661             shape->stroke_shp->Copy(shape->cached_stroke);
663         } else if ( 2 == isometry ) {
664             if ( shape->stroke_shp == NULL ) {
665                 shape->stroke_shp=new Shape;
666                 shape->stroke_shp->Copy(shape->cached_stroke);
667             }
668         } else {
669             if ( shape->stroke_shp == NULL )
670                 shape->stroke_shp=new Shape;
671             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
672             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
673                 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
674             if ( isometry == 1 ) {
675                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
676                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
677                                                shape->cached_stroke->getEdge(i).en);
678             } else if ( isometry == -1 ) {
679                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
680                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
681                                                shape->cached_stroke->getEdge(i).st);
682             }
683             shape->stroke_shp->ForceToPolygon();
684             shape->stroke_shp->needPointsSorting();
685             shape->stroke_shp->needEdgesSorting();
686         }
687         shape->delayed_shp |= shape->cached_spartialy;
688     }
692 void
693 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox)
695     /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
696      * if so, then this can be written much more compact ! */
698     if ( shape->stroke_shp ) {
699         Shape *larger = shape->stroke_shp;
700         larger->CalcBBox();
701         larger->leftX   = floor(larger->leftX);
702         larger->rightX  = ceil(larger->rightX);
703         larger->topY    = floor(larger->topY);
704         larger->bottomY = ceil(larger->bottomY);
705         Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
706                                 Geom::Interval(larger->topY,  larger->bottomY) );
707         bbox.unionWith(stroke_bbox);
708     }
710     if ( shape->fill_shp ) {
711         Shape *larger = shape->fill_shp;
712         larger->CalcBBox();
713         larger->leftX   = floor(larger->leftX);
714         larger->rightX  = ceil(larger->rightX);
715         larger->topY    = floor(larger->topY);
716         larger->bottomY = ceil(larger->bottomY);
717         Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
718                               Geom::Interval(larger->topY,  larger->bottomY) );
719         bbox.unionWith(fill_bbox);
720     }
723 // cairo outline rendering:
724 static unsigned int
725 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, Geom::OptRect area)
727     NRArenaShape *shape = NR_ARENA_SHAPE(item);
729     if (!ct) 
730         return item->state;
732     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
733     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
734     // by setting color channels in the "wrong" order
735     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));
737     cairo_set_line_width(ct, 0.5);
738     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
739     cairo_new_path(ct);
741     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
743     cairo_stroke(ct);
745     return item->state;
748 // cairo stroke rendering (flat color only so far!):
749 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
750 // (need to switch them to premul before this can be enabled)
751 void
752 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
754     NRArenaShape *shape = NR_ARENA_SHAPE(item);
755     SPStyle const *style = shape->style;
757     float const scale = shape->ctm.descrim();
759     if (fabs(shape->_stroke.width * scale) < 0.01)
760         return;
762     cairo_t *ct = nr_create_cairo_context (area, pb);
764     if (!ct)
765         return;
767     guint32 rgba;
768     if ( item->render_opacity ) {
769         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
770                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
771     } else {
772         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
773     }
775     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
776     // by setting color channels in the "wrong" order
777     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));
779     float style_width = MAX(0.125, shape->_stroke.width * scale);
780     cairo_set_line_width(ct, style_width);
782     switch (shape->_stroke.cap) {
783         case NRArenaShape::BUTT_CAP:
784             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
785             break;
786         case NRArenaShape::ROUND_CAP:
787             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
788             break;
789         case NRArenaShape::SQUARE_CAP:
790             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
791             break;
792     }
793     switch (shape->_stroke.join) {
794         case NRArenaShape::MITRE_JOIN:
795             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
796             break;
797         case NRArenaShape::ROUND_JOIN:
798             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
799             break;
800         case NRArenaShape::BEVEL_JOIN:
801             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
802             break;
803     }
805     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
807     if (style->stroke_dash.n_dash) {
808         NRVpathDash dash;
809         dash.offset = style->stroke_dash.offset * scale;
810         dash.n_dash = style->stroke_dash.n_dash;
811         dash.dash = g_new(double, dash.n_dash);
812         for (int i = 0; i < dash.n_dash; i++) {
813             dash.dash[i] = style->stroke_dash.dash[i] * scale;
814         }
815         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
816         g_free(dash.dash);
817     }
819     cairo_set_tolerance(ct, 0.1);
820     cairo_new_path(ct);
822     feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
824     cairo_stroke(ct);
826     cairo_surface_t *cst = cairo_get_target(ct);
827     cairo_destroy (ct);
828     cairo_surface_finish (cst);
829     cairo_surface_destroy (cst);
831     pb->empty = FALSE;
835 /**
836  * Renders the item.  Markers are just composed into the parent buffer.
837  */
838 static unsigned int
839 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
841     NRArenaShape *shape = NR_ARENA_SHAPE(item);
843     if (!shape->curve) return item->state;
844     if (!shape->style) return item->state;
846     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
848     if (outline) { // cairo outline rendering
850         pb->empty = FALSE;
851         unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
852         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
854     } else {
856     if ( shape->delayed_shp ) {
857         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
858             NRGC   tempGC(NULL);
859             tempGC.transform=shape->ctm;
860             shape->delayed_shp = false;
861             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
862             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
863 /*      NRRect bbox;
864         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
865         nr_arena_shape_add_bboxes(shape,bbox);
866         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
867         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
868         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
869         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
870         shape->approx_bbox=item->bbox;*/
871         } else {
872             return item->state;
873         }
874     }
876     SPStyle const *style = shape->style;
877     if (shape->fill_shp) {
878         NRPixBlock m;
879         guint32 rgba;
881         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
883         // if memory allocation failed, abort render
884         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
885             nr_pixblock_release (&m);
886             return (item->state);
887         }
889         m.visible_area = pb->visible_area;
890         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
891         m.empty = FALSE;
893         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
894             // do not render fill in any way
895         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
896             if ( item->render_opacity ) {
897                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
898                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
899             } else {
900                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
901             }
902             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
903             pb->empty = FALSE;
904         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
905             if (shape->fill_painter) {
906                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
907             }
908         }
910         nr_pixblock_release(&m);
911     }
913     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
915         // cairo_arena_shape_render_stroke(item, area, pb);
917         guint32 rgba;
918         NRPixBlock m;
920         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
922         // if memory allocation failed, abort render
923         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
924             nr_pixblock_release (&m);
925             return (item->state);
926         }
928         m.visible_area = pb->visible_area;
929         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
930         m.empty = FALSE;
932             if ( item->render_opacity ) {
933                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
934                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
935             } else {
936                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
937             }
938             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
939             pb->empty = FALSE;
941         nr_pixblock_release(&m);
943     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
945         NRPixBlock m;
947         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
949         // if memory allocation failed, abort render
950         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
951             nr_pixblock_release (&m);
952             return (item->state);
953         }
955         m.visible_area = pb->visible_area; 
956         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
957         m.empty = FALSE;
959         if (shape->stroke_painter) {
960             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
961         }
963         nr_pixblock_release(&m);
964     }
966     } // non-cairo non-outline branch
968     /* Render markers into parent buffer */
969     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
970         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
971         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
972     }
974     return item->state;
978 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
979 // reenable this when the bug is fixed and remove the rest of this function
980 // TODO
981 #if defined(DEADCODE) && !defined(DEADCODE)
982 static guint
983 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
985     NRArenaShape *shape = NR_ARENA_SHAPE(item);
986     if (!shape->curve) return item->state;
988         cairo_t *ct = nr_create_cairo_context (area, pb);
990         if (!ct)
991             return item->state;
993         cairo_set_source_rgba(ct, 0, 0, 0, 1);
995         cairo_new_path(ct);
997         feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
999         cairo_fill(ct);
1001         cairo_surface_t *cst = cairo_get_target(ct);
1002         cairo_destroy (ct);
1003         cairo_surface_finish (cst);
1004         cairo_surface_destroy (cst);
1006         pb->empty = FALSE;
1008         return item->state;
1010 #endif //defined(DEADCODE) && !defined(DEADCODE)
1013 static guint
1014 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1016     //return cairo_arena_shape_clip(item, area, pb);
1018     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1019     if (!shape->curve) return item->state;
1021     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1022         if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1023             NRGC   tempGC(NULL);
1024             tempGC.transform=shape->ctm;
1025             shape->delayed_shp = false;
1026             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1027         } else {
1028             return item->state;
1029         }
1030     }
1032     if ( shape->fill_shp ) {
1033         NRPixBlock m;
1035         /* fixme: We can OR in one step (Lauris) */
1036         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1038         // if memory allocation failed, abort 
1039         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1040             nr_pixblock_release (&m);
1041             return (item->state);
1042         }
1044         m.visible_area = pb->visible_area; 
1045         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1047         for (int y = area->y0; y < area->y1; y++) {
1048             unsigned char *s, *d;
1049             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1050             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1051             for (int x = area->x0; x < area->x1; x++) {
1052                 *d = NR_COMPOSEA_111(*s, *d);
1053                 d ++;
1054                 s ++;
1055             }
1056         }
1057         nr_pixblock_release(&m);
1058         pb->empty = FALSE;
1059     }
1061     return item->state;
1064 static NRArenaItem *
1065 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1067     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1069     if (shape->repick_after > 0)
1070         shape->repick_after--;
1072     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1073         return shape->last_pick;
1075     if (!shape->curve) return NULL;
1076     if (!shape->style) return NULL;
1078     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1080     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline) 
1081         // fully transparent, no pick unless outline mode
1082         return NULL;
1084     GTimeVal tstart, tfinish;
1085     g_get_current_time (&tstart);
1087     double width;
1088     if (outline) {
1089         width = 0.5;
1090     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1091         float const scale = shape->ctm.descrim();
1092         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1093     } else {
1094         width = 0;
1095     }
1097     double dist = NR_HUGE;
1098     int wind = 0;
1099     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1100              && shape->_fill.opacity > 1e-3 && !outline);
1102     if (item->arena->canvasarena) {
1103         Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1104         viewbox.expandBy (width);
1105         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1106     } else {
1107         pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1108     }
1110     g_get_current_time (&tfinish);
1111     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1112     //g_print ("pick time %lu\n", this_pick);
1114     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1115         shape->repick_after = this_pick / 5000;
1116     }
1118     // covered by fill?
1119     if (needfill) {
1120         if (!shape->style->fill_rule.computed) {
1121             if (wind != 0) {
1122                 shape->last_pick = item;
1123                 return item;
1124             }
1125         } else {
1126             if (wind & 0x1) {
1127                 shape->last_pick = item;
1128                 return item;
1129             }
1130         }
1131     }
1133     // close to the edge, as defined by strokewidth and delta?
1134     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1135     if (needfill || width > 0) { // if either fill or stroke visible,
1136         if ((dist - width) < delta) {
1137             shape->last_pick = item;
1138             return item;
1139         }
1140     }
1142     // if not picked on the shape itself, try its markers
1143     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1144         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1145         if (ret) {
1146             shape->last_pick = item;
1147             return item;
1148         }
1149     }
1151     shape->last_pick = NULL;
1152     return NULL;
1155 /**
1156  *
1157  *  Requests a render of the shape, then if the shape is already a curve it
1158  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1159  *  curve and adds it to the shape.  Finally, it requests an update of the
1160  *  arena for the shape.
1161  */
1162 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1164     g_return_if_fail(shape != NULL);
1165     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1167     if ( justTrans == false ) {
1168         // dirty cached versions
1169         if ( shape->cached_fill ) {
1170             delete shape->cached_fill;
1171             shape->cached_fill=NULL;
1172         }
1173         if ( shape->cached_stroke ) {
1174             delete shape->cached_stroke;
1175             shape->cached_stroke=NULL;
1176         }
1177     }
1179     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1181     if (shape->curve) {
1182         shape->curve->unref();
1183         shape->curve = NULL;
1184     }
1186     if (curve) {
1187         shape->curve = curve;
1188         curve->ref();
1189     }
1191     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1194 void NRArenaShape::setFill(SPPaintServer *server) {
1195     _fill.paint.set(server);
1196     _invalidateCachedFill();
1199 void NRArenaShape::setFill(SPColor const &color) {
1200     _fill.paint.set(color);
1201     _invalidateCachedFill();
1204 void NRArenaShape::setFillOpacity(double opacity) {
1205     _fill.opacity = opacity;
1206     _invalidateCachedFill();
1209 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1210     _fill.rule = rule;
1211     _invalidateCachedFill();
1214 void NRArenaShape::setStroke(SPPaintServer *server) {
1215     _stroke.paint.set(server);
1216     _invalidateCachedStroke();
1219 void NRArenaShape::setStroke(SPColor const &color) {
1220     _stroke.paint.set(color);
1221     _invalidateCachedStroke();
1224 void NRArenaShape::setStrokeOpacity(double opacity) {
1225     _stroke.opacity = opacity;
1226     _invalidateCachedStroke();
1229 void NRArenaShape::setStrokeWidth(double width) {
1230     _stroke.width = width;
1231     _invalidateCachedStroke();
1234 void NRArenaShape::setMitreLimit(double limit) {
1235     _stroke.mitre_limit = limit;
1236     _invalidateCachedStroke();
1239 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1240     _stroke.cap = cap;
1241     _invalidateCachedStroke();
1244 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1245     _stroke.join = join;
1246     _invalidateCachedStroke();
1249 /** nr_arena_shape_set_style
1250  *
1251  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1252  */
1253 void
1254 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1256     g_return_if_fail(shape != NULL);
1257     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1259     if (style) sp_style_ref(style);
1260     if (shape->style) sp_style_unref(shape->style);
1261     shape->style = style;
1263     if ( style->fill.isPaintserver() ) {
1264         shape->setFill(style->getFillPaintServer());
1265     } else if ( style->fill.isColor() ) {
1266         shape->setFill(style->fill.value.color);
1267     } else if ( style->fill.isNone() ) {
1268         shape->setFill(NULL);
1269     } else {
1270         g_assert_not_reached();
1271     }
1272     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1273     switch (style->fill_rule.computed) {
1274         case SP_WIND_RULE_EVENODD: {
1275             shape->setFillRule(NRArenaShape::EVEN_ODD);
1276             break;
1277         }
1278         case SP_WIND_RULE_NONZERO: {
1279             shape->setFillRule(NRArenaShape::NONZERO);
1280             break;
1281         }
1282         default: {
1283             g_assert_not_reached();
1284         }
1285     }
1287     if ( style->stroke.isPaintserver() ) {
1288         shape->setStroke(style->getStrokePaintServer());
1289     } else if ( style->stroke.isColor() ) {
1290         shape->setStroke(style->stroke.value.color);
1291     } else if ( style->stroke.isNone() ) {
1292         shape->setStroke(NULL);
1293     } else {
1294         g_assert_not_reached();
1295     }
1296     shape->setStrokeWidth(style->stroke_width.computed);
1297     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1298     switch (style->stroke_linecap.computed) {
1299         case SP_STROKE_LINECAP_ROUND: {
1300             shape->setLineCap(NRArenaShape::ROUND_CAP);
1301             break;
1302         }
1303         case SP_STROKE_LINECAP_SQUARE: {
1304             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1305             break;
1306         }
1307         case SP_STROKE_LINECAP_BUTT: {
1308             shape->setLineCap(NRArenaShape::BUTT_CAP);
1309             break;
1310         }
1311         default: {
1312             g_assert_not_reached();
1313         }
1314     }
1315     switch (style->stroke_linejoin.computed) {
1316         case SP_STROKE_LINEJOIN_ROUND: {
1317             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1318             break;
1319         }
1320         case SP_STROKE_LINEJOIN_BEVEL: {
1321             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1322             break;
1323         }
1324         case SP_STROKE_LINEJOIN_MITER: {
1325             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1326             break;
1327         }
1328         default: {
1329             g_assert_not_reached();
1330         }
1331     }
1332     shape->setMitreLimit(style->stroke_miterlimit.value);
1334     //if shape has a filter
1335     if (style->filter.set && style->getFilter()) {
1336         if (!shape->filter) {
1337             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1338             shape->filter = new Inkscape::Filters::Filter(primitives);
1339         }
1340         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1341     } else {
1342         //no filter set for this shape
1343         delete shape->filter;
1344         shape->filter = NULL;
1345     }
1347     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1350 void
1351 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1353     g_return_if_fail(shape != NULL);
1354     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1355     g_return_if_fail(pbox != NULL);
1357     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1358         shape->paintbox = *pbox;
1359     } else {
1360         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1361         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1362         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1363     }
1365     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1368 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1370     paintbox.x0 = pbox.min()[Geom::X];
1371     paintbox.y0 = pbox.min()[Geom::Y];
1372     paintbox.x1 = pbox.max()[Geom::X];
1373     paintbox.y1 = pbox.max()[Geom::Y];
1375     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1378 static void
1379 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1381     if ( st >= en ) return;
1382     if ( vst < 0 ) vst=0;
1383     if ( vst > 1 ) vst=1;
1384     if ( ven < 0 ) ven=0;
1385     if ( ven > 1 ) ven=1;
1386     float   sv=vst;
1387     float   dv=ven-vst;
1388     int     len=en-st;
1389     unsigned char*   d=(unsigned char*)dest.buffer;
1390     d+=(st-dest.startPix);
1391     if ( fabs(dv) < 0.001 ) {
1392         if ( vst > 0.999 ) {
1393             /* Simple copy */
1394             while (len > 0) {
1395                 d[0] = 255;
1396                 d += 1;
1397                 len -= 1;
1398             }
1399         } else {
1400             sv*=256;
1401             unsigned int c0_24=(int)sv;
1402             c0_24&=0xFF;
1403             while (len > 0) {
1404                 /* Draw */
1405                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1406                 d += 1;
1407                 len -= 1;
1408             }
1409         }
1410     } else {
1411         if ( en <= st+1 ) {
1412             sv=0.5*(vst+ven);
1413             sv*=256;
1414             unsigned int c0_24=(int)sv;
1415             c0_24&=0xFF;
1416             /* Draw */
1417             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1418         } else {
1419             dv/=len;
1420             sv+=0.5*dv; // correction trapezoidale
1421             sv*=16777216;
1422             dv*=16777216;
1423             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1424             int s0_24 = static_cast<int>(dv);
1425             while (len > 0) {
1426                 unsigned int ca;
1427                 /* Draw */
1428                 ca = c0_24 >> 16;
1429                 if ( ca > 255 ) ca=255;
1430                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1431                 d += 1;
1432                 c0_24 += s0_24;
1433                 c0_24 = CLAMP(c0_24, 0, 16777216);
1434                 len -= 1;
1435             }
1436         }
1437     }
1440 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1442     theS->CalcBBox();
1443     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1444     int    il,ir,it,ib;
1445     il=(int)floor(l);
1446     ir=(int)ceil(r);
1447     it=(int)floor(t);
1448     ib=(int)ceil(b);
1450     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1451     if ( il < m.area.x0 ) il=m.area.x0;
1452     if ( it < m.area.y0 ) it=m.area.y0;
1453     if ( ir > m.area.x1 ) ir=m.area.x1;
1454     if ( ib > m.area.y1 ) ib=m.area.y1;
1456     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1457     int    curPt;
1458     float  curY;
1459     theS->BeginQuickRaster(curY, curPt);
1461     FloatLigne *theI = new FloatLigne();
1462     IntLigne *theIL = new IntLigne();
1464     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1466     char *mdata = (char*)m.data.px;
1467     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1468     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1469     for (int y = it; y < ib; y++) {
1470         theI->Reset();
1471         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1472         theI->Flatten();
1473         theIL->Copy(theI);
1475         raster_info  dest;
1476         dest.startPix=il;
1477         dest.endPix=ir;
1478         dest.sth=il;
1479         dest.stv=y;
1480         dest.buffer=ligStart;
1481         theIL->Raster(dest, NULL, shape_run_A8_OR);
1482         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1483     }
1484     theS->EndQuickRaster();
1485     delete theI;
1486     delete theIL;
1490 /*
1491   Local Variables:
1492   mode:c++
1493   c-file-style:"stroustrup"
1494   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1495   indent-tabs-mode:nil
1496   fill-column:99
1497   End:
1498 */
1499 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :