Code

- try to use more forward declarations for less dependencies on display/curve.h
[inkscape.git] / src / display / nr-arena-shape.cpp
1 #define __NR_ARENA_SHAPE_C__
3 /*
4  * RGBA display list system for inkscape
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2001-2002 Lauris Kaplinski
10  * Copyright (C) 2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
17 #include <display/canvas-arena.h>
18 #include <display/nr-arena.h>
19 #include <display/nr-arena-shape.h>
20 #include "display/curve.h"
21 #include <libnr/n-art-bpath.h>
22 #include <libnr/nr-path.h>
23 #include <libnr/nr-pixops.h>
24 #include <libnr/nr-matrix-ops.h>
25 #include <libnr/nr-matrix-fns.h>
26 #include <libnr/nr-blit.h>
27 #include <livarot/Path.h>
28 #include <livarot/float-line.h>
29 #include <livarot/int-line.h>
30 #include <style.h>
31 #include "prefs-utils.h"
32 #include "inkscape-cairo.h"
34 #include "sp-filter.h"
35 #include "sp-filter-reference.h"
36 #include "display/nr-filter.h"
38 #include <cairo.h>
40 //int  showRuns=0;
41 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
43 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
44 static void nr_arena_shape_init(NRArenaShape *shape);
45 static void nr_arena_shape_finalize(NRObject *object);
47 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
48 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
49 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
50 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
52 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
53 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
54 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
55 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
57 static NRArenaItemClass *shape_parent_class;
59 NRType
60 nr_arena_shape_get_type(void)
61 {
62     static NRType type = 0;
63     if (!type) {
64         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
65                                        "NRArenaShape",
66                                        sizeof(NRArenaShapeClass),
67                                        sizeof(NRArenaShape),
68                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
69                                        (void (*)(NRObject *)) nr_arena_shape_init);
70     }
71     return type;
72 }
74 static void
75 nr_arena_shape_class_init(NRArenaShapeClass *klass)
76 {
77     NRObjectClass *object_class;
78     NRArenaItemClass *item_class;
80     object_class = (NRObjectClass *) klass;
81     item_class = (NRArenaItemClass *) klass;
83     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
85     object_class->finalize = nr_arena_shape_finalize;
86     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
88     item_class->children = nr_arena_shape_children;
89     item_class->add_child = nr_arena_shape_add_child;
90     item_class->set_child_position = nr_arena_shape_set_child_position;
91     item_class->remove_child = nr_arena_shape_remove_child;
92     item_class->update = nr_arena_shape_update;
93     item_class->render = nr_arena_shape_render;
94     item_class->clip = nr_arena_shape_clip;
95     item_class->pick = nr_arena_shape_pick;
96 }
98 /**
99  * Initializes the arena shape, setting all parameters to null, 0, false,
100  * or other defaults
101  */
102 static void
103 nr_arena_shape_init(NRArenaShape *shape)
105     shape->curve = NULL;
106     shape->style = NULL;
107     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
108     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
110     shape->ctm.set_identity();
111     shape->fill_painter = NULL;
112     shape->stroke_painter = NULL;
113     shape->cached_fill = NULL;
114     shape->cached_stroke = NULL;
115     shape->cached_fpartialy = false;
116     shape->cached_spartialy = false;
117     shape->fill_shp = NULL;
118     shape->stroke_shp = NULL;
120     shape->delayed_shp = false;
122     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
123     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
124     shape->cached_fctm.set_identity();
125     shape->cached_sctm.set_identity();
127     shape->markers = NULL;
129     shape->last_pick = NULL;
130     shape->repick_after = 0;
133 static void
134 nr_arena_shape_finalize(NRObject *object)
136     NRArenaShape *shape = (NRArenaShape *) object;
138     if (shape->fill_shp) delete shape->fill_shp;
139     if (shape->stroke_shp) delete shape->stroke_shp;
140     if (shape->cached_fill) delete shape->cached_fill;
141     if (shape->cached_stroke) delete shape->cached_stroke;
142     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
143     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
145     if (shape->style) sp_style_unref(shape->style);
146     if (shape->curve) shape->curve->unref();
148     ((NRObjectClass *) shape_parent_class)->finalize(object);
151 /**
152  * Retrieves the markers from the item
153  */
154 static NRArenaItem *
155 nr_arena_shape_children(NRArenaItem *item)
157     NRArenaShape *shape = (NRArenaShape *) item;
159     return shape->markers;
162 /**
163  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
164  * the prev and next items.  If ref is NULL, then it sets the item's markers
165  * as the next items.
166  */
167 static void
168 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
170     NRArenaShape *shape = (NRArenaShape *) item;
172     if (!ref) {
173         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
174     } else {
175         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
176     }
178     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
181 /**
182  * Removes child from the shape.  If there are no prev items in 
183  * the child, it sets items' markers to the next item in the child.
184  */
185 static void
186 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
188     NRArenaShape *shape = (NRArenaShape *) item;
190     if (child->prev) {
191         nr_arena_item_detach(item, child);
192     } else {
193         shape->markers = nr_arena_item_detach(item, child);
194     }
196     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
199 /**
200  * Detaches child from item, and if there are no previous items in child, it 
201  * sets item's markers to the child.  It then attaches the child back onto the item.
202  * If ref is null, it sets the markers to be the next item, otherwise it uses
203  * the next/prev items in ref.
204  */
205 static void
206 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
208     NRArenaShape *shape = (NRArenaShape *) item;
210     if (child->prev) {
211         nr_arena_item_detach(item, child);
212     } else {
213         shape->markers = nr_arena_item_detach(item, child);
214     }
216     if (!ref) {
217         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
218     } else {
219         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
220     }
222     nr_arena_item_request_render(child);
225 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
226 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
227 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
229 /**
230  * Updates the arena shape 'item' and all of its children, including the markers.
231  */
232 static guint
233 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
235     NRRect bbox;
237     NRArenaShape *shape = NR_ARENA_SHAPE(item);
239     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
241     unsigned int newstate;
242     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
243         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
244         beststate = beststate & newstate;
245     }
247     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
248         /* We do not have to create rendering structures */
249         shape->ctm = gc->transform;
250         if (state & NR_ARENA_ITEM_STATE_BBOX) {
251             if (shape->curve) {
252                 NRBPath bp;
253                 /* fixme: */
254                 bbox.x0 = bbox.y0 = NR_HUGE;
255                 bbox.x1 = bbox.y1 = -NR_HUGE;
256                 bp.path = SP_CURVE_BPATH(shape->curve);
257                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
258                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
259                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
260                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
261                 item->bbox.y1 = (gint32)(bbox.y1 + 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     bbox.x0 = bbox.y0 = NR_HUGE;
275     bbox.x1 = bbox.y1 = -NR_HUGE;
277     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
279     if (shape->curve) {
280         NRBPath bp;
281         /* fixme: */
282         bbox.x0 = bbox.y0 = NR_HUGE;
283         bbox.x1 = bbox.y1 = -NR_HUGE;
284         bp.path = SP_CURVE_BPATH(shape->curve);
285         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
286         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
287             float width, scale;
288             scale = NR::expansion(gc->transform);
289             width = MAX(0.125, shape->_stroke.width * scale);
290             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
291                 bbox.x0-=width;
292                 bbox.x1+=width;
293                 bbox.y0-=width;
294                 bbox.y1+=width;
295             }
296             // those pesky miters, now
297             float miterMax=width*shape->_stroke.mitre_limit;
298             if ( miterMax > 0.01 ) {
299                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
300                 bbox.x0-=miterMax;
301                 bbox.x1+=miterMax;
302                 bbox.y0-=miterMax;
303                 bbox.y1+=miterMax;
304             }
305         }
306     } else {
307     }
308     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
309     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
310     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
311     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
312     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
314     /* Release state data */
315     if (TRUE || !NR::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
316         /* Concept test */
317         if (shape->fill_shp) {
318             delete shape->fill_shp;
319             shape->fill_shp = NULL;
320         }
321     }
322     if (shape->stroke_shp) {
323         delete shape->stroke_shp;
324         shape->stroke_shp = NULL;
325     }
326     if (shape->fill_painter) {
327         sp_painter_free(shape->fill_painter);
328         shape->fill_painter = NULL;
329     }
330     if (shape->stroke_painter) {
331         sp_painter_free(shape->stroke_painter);
332         shape->stroke_painter = NULL;
333     }
335     if (!shape->curve || 
336         !shape->style ||
337         shape->curve->is_empty() ||
338         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
339          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
340     {
341         item->bbox = shape->approx_bbox;
342         return NR_ARENA_ITEM_STATE_ALL;
343     }
345     /* Build state data */
346     if ( shape->delayed_shp ) {
347         item->bbox=shape->approx_bbox;
348     } else {
349         nr_arena_shape_update_stroke(shape, gc, area);
350         nr_arena_shape_update_fill(shape, gc, area);
352         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
353         nr_arena_shape_add_bboxes(shape,bbox);
355         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
356         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
357         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
358         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
359     }
361     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
363     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
364     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
365     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
366     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
367     nr_arena_request_render_rect(item->arena, &item->bbox);
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(NR::Matrix p) {
405     NR::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     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
414     // Is the adjunct nearly an identity function?
415     if (isom.is_translation(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 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
431 void
432 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
434     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
435         ((shape->curve->_end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
436         if (TRUE || !shape->fill_shp) {
437             NR::Matrix  cached_to_new = NR::identity();
438             int isometry = 0;
439             if ( shape->cached_fill ) {
440                 if (shape->cached_fctm == gc->transform) {
441                     isometry = 2; // identity
442                 } else {
443                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
444                     isometry = matrix_is_isometry(cached_to_new);
445                 }
446                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
447                     isometry = 0;
448             }
449             if ( isometry == 0 ) {
450                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
451                 shape->cached_fill->Reset();
453                 Path*  thePath=new Path;
454                 Shape* theShape=new Shape;
455                 {
456                     NR::Matrix   tempMat(gc->transform);
457                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
458                 }
460                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
461                     thePath->Convert(1.0);
462                     shape->cached_fpartialy = false;
463                 } else {
464                     thePath->Convert(area, 1.0);
465                     shape->cached_fpartialy = true;
466                 }
468                 thePath->Fill(theShape, 0);
470                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
471                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
472                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
473                     //shape->cached_fill->Copy(theShape);
474                 } else {
475                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
476                 }
477                 shape->cached_fctm=gc->transform;
478                 shape->cached_farea = *area;
479                 delete theShape;
480                 delete thePath;
481                 if ( shape->fill_shp == NULL )
482                     shape->fill_shp = new Shape;
484                 shape->fill_shp->Copy(shape->cached_fill);
486             } else if ( 2 == isometry ) {
487                 if ( shape->fill_shp == NULL ) {
488                     shape->fill_shp = new Shape;
489                     shape->fill_shp->Copy(shape->cached_fill);
490                 }
491             } else {
493                 if ( shape->fill_shp == NULL )
494                     shape->fill_shp = new Shape;
496                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
497                                        shape->cached_fill->numberOfEdges());
498                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
499                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
500                 if ( isometry == 1 ) {
501                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
502                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
503                                                  shape->cached_fill->getEdge(i).en);
504                 } else if ( isometry == -1 ) { // need to flip poly.
505                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
506                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
507                                                  shape->cached_fill->getEdge(i).st);
508                 }
509                 shape->fill_shp->ForceToPolygon();
510                 shape->fill_shp->needPointsSorting();
511                 shape->fill_shp->needEdgesSorting();
512             }
513             shape->delayed_shp |= shape->cached_fpartialy;
514         }
515     }
518 void
519 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
521     SPStyle* style = shape->style;
523     float const scale = NR::expansion(gc->transform);
525     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
527     if (outline) {
528         // cairo does not need the livarot path for rendering
529         return; 
530     }
532     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
533     // (since it will only be used for picking, not for rendering)
535     if (outline ||
536         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
537          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
539         float style_width = MAX(0.125, shape->_stroke.width * scale);
540         float width;
541         if (outline) {
542             width = 0.5; // 1 pixel wide, independent of zoom
543         } else {
544             width = style_width;
545         }
547         NR::Matrix  cached_to_new = NR::identity();
549         int isometry = 0;
550         if ( shape->cached_stroke ) {
551             if (shape->cached_sctm == gc->transform) {
552                 isometry = 2; // identity
553             } else {
554                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
555                 isometry = matrix_is_isometry(cached_to_new);
556             }
557             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
558                 isometry = 0;
559             if (0 != isometry && width != shape->cached_width) { 
560                 // if this happens without setting style, we have just switched to outline or back
561                 isometry = 0; 
562             } 
563         }
565         if ( isometry == 0 ) {
566             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
567             shape->cached_stroke->Reset();
568             Path*  thePath = new Path;
569             Shape* theShape = new Shape;
570             {
571                 NR::Matrix   tempMat(gc->transform);
572                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
573             }
575             // add some padding to the rendering area, so clipped path does not go into a render area
576             NRRectL padded_area = *area;
577             padded_area.x0 -= (NR::ICoord)width;
578             padded_area.x1 += (NR::ICoord)width;
579             padded_area.y0 -= (NR::ICoord)width;
580             padded_area.y1 += (NR::ICoord)width;
581             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
582                 thePath->Convert((outline) ? 4.0 : 1.0);
583                 shape->cached_spartialy = false;
584             }
585             else {
586                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
587                 shape->cached_spartialy = true;
588             }
590             if (style->stroke_dash.n_dash && !outline) {
591                 thePath->DashPolylineFromStyle(style, scale, 1.0);
592             }
594             ButtType butt=butt_straight;
595             switch (shape->_stroke.cap) {
596                 case NRArenaShape::BUTT_CAP:
597                     butt = butt_straight;
598                     break;
599                 case NRArenaShape::ROUND_CAP:
600                     butt = butt_round;
601                     break;
602                 case NRArenaShape::SQUARE_CAP:
603                     butt = butt_square;
604                     break;
605             }
606             JoinType join=join_straight;
607             switch (shape->_stroke.join) {
608                 case NRArenaShape::MITRE_JOIN:
609                     join = join_pointy;
610                     break;
611                 case NRArenaShape::ROUND_JOIN:
612                     join = join_round;
613                     break;
614                 case NRArenaShape::BEVEL_JOIN:
615                     join = join_straight;
616                     break;
617             }
619             if (outline) {
620                 butt = butt_straight;
621                 join = join_straight;
622             }
624             thePath->Stroke(theShape, false, 0.5*width, join, butt,
625                             0.5*width*shape->_stroke.mitre_limit);
628             if (outline) {
629                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
630                 shape->cached_stroke->Copy(theShape);
631             } else {
632                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
633             }
635             shape->cached_width = width;
637             shape->cached_sctm=gc->transform;
638             shape->cached_sarea = *area;
639             delete thePath;
640             delete theShape;
641             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
643             shape->stroke_shp->Copy(shape->cached_stroke);
645         } else if ( 2 == isometry ) {
646             if ( shape->stroke_shp == NULL ) {
647                 shape->stroke_shp=new Shape;
648                 shape->stroke_shp->Copy(shape->cached_stroke);
649             }
650         } else {
651             if ( shape->stroke_shp == NULL )
652                 shape->stroke_shp=new Shape;
653             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
654             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
655                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
656             if ( isometry == 1 ) {
657                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
658                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
659                                                shape->cached_stroke->getEdge(i).en);
660             } else if ( isometry == -1 ) {
661                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
662                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
663                                                shape->cached_stroke->getEdge(i).st);
664             }
665             shape->stroke_shp->ForceToPolygon();
666             shape->stroke_shp->needPointsSorting();
667             shape->stroke_shp->needEdgesSorting();
668         }
669         shape->delayed_shp |= shape->cached_spartialy;
670     }
674 void
675 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
677     if ( shape->stroke_shp ) {
678         shape->stroke_shp->CalcBBox();
679         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
680         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
681         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
682         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
683         if ( bbox.x0 >= bbox.x1 ) {
684             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
685                 bbox.x0=shape->stroke_shp->leftX;
686                 bbox.x1=shape->stroke_shp->rightX;
687             }
688         } else {
689             if ( shape->stroke_shp->leftX < bbox.x0 )
690                 bbox.x0=shape->stroke_shp->leftX;
691             if ( shape->stroke_shp->rightX > bbox.x1 )
692                 bbox.x1=shape->stroke_shp->rightX;
693         }
694         if ( bbox.y0 >= bbox.y1 ) {
695             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
696                 bbox.y0=shape->stroke_shp->topY;
697                 bbox.y1=shape->stroke_shp->bottomY;
698             }
699         } else {
700             if ( shape->stroke_shp->topY < bbox.y0 )
701                 bbox.y0=shape->stroke_shp->topY;
702             if ( shape->stroke_shp->bottomY > bbox.y1 )
703                 bbox.y1=shape->stroke_shp->bottomY;
704         }
705     }
706     if ( shape->fill_shp ) {
707         shape->fill_shp->CalcBBox();
708         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
709         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
710         shape->fill_shp->topY=floor(shape->fill_shp->topY);
711         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
712         if ( bbox.x0 >= bbox.x1 ) {
713             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
714                 bbox.x0=shape->fill_shp->leftX;
715                 bbox.x1=shape->fill_shp->rightX;
716             }
717         } else {
718             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
719             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
720         }
721         if ( bbox.y0 >= bbox.y1 ) {
722             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
723                 bbox.y0=shape->fill_shp->topY;
724                 bbox.y1=shape->fill_shp->bottomY;
725             }
726         } else {
727             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
728             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
729         }
730     }
733 // cairo outline rendering:
734 static unsigned int
735 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
737     NRArenaShape *shape = NR_ARENA_SHAPE(item);
739     if (!ct) 
740         return item->state;
742     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
743     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
744     // by setting color channels in the "wrong" order
745     cairo_set_source_rgba(ct, SP_RGBA32_B_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_R_F(rgba), SP_RGBA32_A_F(rgba));
747     cairo_set_line_width(ct, 0.5);
748     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
749     cairo_new_path(ct);
751     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
753     cairo_stroke(ct);
755     return item->state;
758 // cairo stroke rendering (flat color only so far!):
759 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
760 // (need to switch them to premul before this can be enabled)
761 void
762 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
764     NRArenaShape *shape = NR_ARENA_SHAPE(item);
765     SPStyle const *style = shape->style;
767     float const scale = NR::expansion(shape->ctm);
769     if (fabs(shape->_stroke.width * scale) < 0.01)
770         return;
772     cairo_t *ct = nr_create_cairo_context (area, pb);
774     if (!ct)
775         return;
777     guint32 rgba;
778     if ( item->render_opacity ) {
779         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
780                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
781     } else {
782         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
783     }
785     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
786     // by setting color channels in the "wrong" order
787     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));
789     float style_width = MAX(0.125, shape->_stroke.width * scale);
790     cairo_set_line_width(ct, style_width);
792     switch (shape->_stroke.cap) {
793         case NRArenaShape::BUTT_CAP:
794             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
795             break;
796         case NRArenaShape::ROUND_CAP:
797             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
798             break;
799         case NRArenaShape::SQUARE_CAP:
800             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
801             break;
802     }
803     switch (shape->_stroke.join) {
804         case NRArenaShape::MITRE_JOIN:
805             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
806             break;
807         case NRArenaShape::ROUND_JOIN:
808             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
809             break;
810         case NRArenaShape::BEVEL_JOIN:
811             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
812             break;
813     }
815     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
817     if (style->stroke_dash.n_dash) {
818         NRVpathDash dash;
819         dash.offset = style->stroke_dash.offset * scale;
820         dash.n_dash = style->stroke_dash.n_dash;
821         dash.dash = g_new(double, dash.n_dash);
822         for (int i = 0; i < dash.n_dash; i++) {
823             dash.dash[i] = style->stroke_dash.dash[i] * scale;
824         }
825         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
826         g_free(dash.dash);
827     }
829     cairo_set_tolerance(ct, 0.1);
830     cairo_new_path(ct);
832     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
834     cairo_stroke(ct);
836     cairo_surface_t *cst = cairo_get_target(ct);
837     cairo_destroy (ct);
838     cairo_surface_finish (cst);
839     cairo_surface_destroy (cst);
841     pb->empty = FALSE;
845 /**
846  * Renders the item.  Markers are just composed into the parent buffer.
847  */
848 static unsigned int
849 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
851     NRArenaShape *shape = NR_ARENA_SHAPE(item);
853     if (!shape->curve) return item->state;
854     if (!shape->style) return item->state;
856     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
858     if (outline) { // cairo outline rendering
860         pb->empty = FALSE;
861         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
862         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
864     } else {
866     if ( shape->delayed_shp ) {
867         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
868             NRGC   tempGC(NULL);
869             tempGC.transform=shape->ctm;
870             shape->delayed_shp = false;
871             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
872             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
873 /*      NRRect bbox;
874         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
875         nr_arena_shape_add_bboxes(shape,bbox);
876         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
877         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
878         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
879         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
880         shape->approx_bbox=item->bbox;*/
881         } else {
882             return item->state;
883         }
884     }
886     SPStyle const *style = shape->style;
887     if (shape->fill_shp) {
888         NRPixBlock m;
889         guint32 rgba;
891         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
893         // if memory allocation failed, abort render
894         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
895             nr_pixblock_release (&m);
896             return (item->state);
897         }
899         m.visible_area = pb->visible_area;
900         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
901         m.empty = FALSE;
903         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
904             // do not render fill in any way
905         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
906             if ( item->render_opacity ) {
907                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
908                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
909             } else {
910                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
911             }
912             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
913             pb->empty = FALSE;
914         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
915             if (shape->fill_painter) {
916                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
917             }
918         }
920         nr_pixblock_release(&m);
921     }
923     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
925         // cairo_arena_shape_render_stroke(item, area, pb);
927         guint32 rgba;
928         NRPixBlock m;
930         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
932         // if memory allocation failed, abort render
933         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
934             nr_pixblock_release (&m);
935             return (item->state);
936         }
938         m.visible_area = pb->visible_area;
939         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
940         m.empty = FALSE;
942             if ( item->render_opacity ) {
943                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
944                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
945             } else {
946                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
947             }
948             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
949             pb->empty = FALSE;
951         nr_pixblock_release(&m);
953     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
955         NRPixBlock m;
957         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
959         // if memory allocation failed, abort render
960         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
961             nr_pixblock_release (&m);
962             return (item->state);
963         }
965         m.visible_area = pb->visible_area; 
966         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
967         m.empty = FALSE;
969         if (shape->stroke_painter) {
970             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
971         }
973         nr_pixblock_release(&m);
974     }
976     } // non-cairo non-outline branch
978     /* Render markers into parent buffer */
979     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
980         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
981         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
982     }
984     return item->state;
988 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
989 // reenable this when the bug is fixed and remove the rest of this function
990 // TODO
991 #if defined(DEADCODE) && !defined(DEADCODE)
992 static guint
993 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
995     NRArenaShape *shape = NR_ARENA_SHAPE(item);
996     if (!shape->curve) return item->state;
998         cairo_t *ct = nr_create_cairo_context (area, pb);
1000         if (!ct)
1001             return item->state;
1003         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1005         cairo_new_path(ct);
1007         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1009         cairo_fill(ct);
1011         cairo_surface_t *cst = cairo_get_target(ct);
1012         cairo_destroy (ct);
1013         cairo_surface_finish (cst);
1014         cairo_surface_destroy (cst);
1016         pb->empty = FALSE;
1018         return item->state;
1020 #endif //defined(DEADCODE) && !defined(DEADCODE)
1023 static guint
1024 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1026     //return cairo_arena_shape_clip(item, area, pb);
1028     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1029     if (!shape->curve) return item->state;
1031     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1032         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1033             NRGC   tempGC(NULL);
1034             tempGC.transform=shape->ctm;
1035             shape->delayed_shp = false;
1036             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1037         } else {
1038             return item->state;
1039         }
1040     }
1042     if ( shape->fill_shp ) {
1043         NRPixBlock m;
1045         /* fixme: We can OR in one step (Lauris) */
1046         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1048         // if memory allocation failed, abort 
1049         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1050             nr_pixblock_release (&m);
1051             return (item->state);
1052         }
1054         m.visible_area = pb->visible_area; 
1055         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1057         for (int y = area->y0; y < area->y1; y++) {
1058             unsigned char *s, *d;
1059             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1060             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1061             for (int x = area->x0; x < area->x1; x++) {
1062                 *d = NR_COMPOSEA_111(*s, *d);
1063                 d ++;
1064                 s ++;
1065             }
1066         }
1067         nr_pixblock_release(&m);
1068         pb->empty = FALSE;
1069     }
1071     return item->state;
1074 static NRArenaItem *
1075 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1077     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1079     if (shape->repick_after > 0)
1080         shape->repick_after--;
1082     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1083         return shape->last_pick;
1085     if (!shape->curve) return NULL;
1086     if (!shape->style) return NULL;
1087     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1088         return NULL;
1090     GTimeVal tstart, tfinish;
1091     g_get_current_time (&tstart);
1093     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1095     double width;
1096     if (outline) {
1097         width = 0.5;
1098     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1099         float const scale = NR::expansion(shape->ctm);
1100         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1101     } else {
1102         width = 0;
1103     }
1105     NRBPath bp;
1106     bp.path = SP_CURVE_BPATH(shape->curve);
1107     double dist = NR_HUGE;
1108     int wind = 0;
1109     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1110              && shape->_fill.opacity > 1e-3 && !outline);
1112     if (item->arena->canvasarena) {
1113         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1114         viewbox.growBy (width);
1115         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1116     } else {
1117         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1118     }
1120     g_get_current_time (&tfinish);
1121     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1122     //g_print ("pick time %lu\n", this_pick);
1124     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1125         shape->repick_after = this_pick / 5000;
1126     }
1128     // covered by fill?
1129     if (needfill) {
1130         if (!shape->style->fill_rule.computed) {
1131             if (wind != 0) {
1132                 shape->last_pick = item;
1133                 return item;
1134             }
1135         } else {
1136             if (wind & 0x1) {
1137                 shape->last_pick = item;
1138                 return item;
1139             }
1140         }
1141     }
1143     // close to the edge, as defined by strokewidth and delta?
1144     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1145     if (needfill || width > 0) { // if either fill or stroke visible,
1146         if ((dist - width) < delta) {
1147             shape->last_pick = item;
1148             return item;
1149         }
1150     }
1152     // if not picked on the shape itself, try its markers
1153     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1154         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1155         if (ret) {
1156             shape->last_pick = item;
1157             return item;
1158         }
1159     }
1161     shape->last_pick = NULL;
1162     return NULL;
1165 /**
1166  *
1167  *  Requests a render of the shape, then if the shape is already a curve it
1168  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1169  *  curve and adds it to the shape.  Finally, it requests an update of the
1170  *  arena for the shape.
1171  */
1172 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1174     g_return_if_fail(shape != NULL);
1175     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1177     if ( justTrans == false ) {
1178         // dirty cached versions
1179         if ( shape->cached_fill ) {
1180             delete shape->cached_fill;
1181             shape->cached_fill=NULL;
1182         }
1183         if ( shape->cached_stroke ) {
1184             delete shape->cached_stroke;
1185             shape->cached_stroke=NULL;
1186         }
1187     }
1189     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1191     if (shape->curve) {
1192         shape->curve->unref();
1193         shape->curve = NULL;
1194     }
1196     if (curve) {
1197         shape->curve = curve;
1198         curve->ref();
1199     }
1201     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1204 void NRArenaShape::setFill(SPPaintServer *server) {
1205     _fill.paint.set(server);
1206     _invalidateCachedFill();
1209 void NRArenaShape::setFill(SPColor const &color) {
1210     _fill.paint.set(color);
1211     _invalidateCachedFill();
1214 void NRArenaShape::setFillOpacity(double opacity) {
1215     _fill.opacity = opacity;
1216     _invalidateCachedFill();
1219 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1220     _fill.rule = rule;
1221     _invalidateCachedFill();
1224 void NRArenaShape::setStroke(SPPaintServer *server) {
1225     _stroke.paint.set(server);
1226     _invalidateCachedStroke();
1229 void NRArenaShape::setStroke(SPColor const &color) {
1230     _stroke.paint.set(color);
1231     _invalidateCachedStroke();
1234 void NRArenaShape::setStrokeOpacity(double opacity) {
1235     _stroke.opacity = opacity;
1236     _invalidateCachedStroke();
1239 void NRArenaShape::setStrokeWidth(double width) {
1240     _stroke.width = width;
1241     _invalidateCachedStroke();
1244 void NRArenaShape::setMitreLimit(double limit) {
1245     _stroke.mitre_limit = limit;
1246     _invalidateCachedStroke();
1249 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1250     _stroke.cap = cap;
1251     _invalidateCachedStroke();
1254 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1255     _stroke.join = join;
1256     _invalidateCachedStroke();
1259 /** nr_arena_shape_set_style
1260  *
1261  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1262  */
1263 void
1264 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1266     g_return_if_fail(shape != NULL);
1267     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1269     if (style) sp_style_ref(style);
1270     if (shape->style) sp_style_unref(shape->style);
1271     shape->style = style;
1273     if ( style->fill.isPaintserver() ) {
1274         shape->setFill(style->getFillPaintServer());
1275     } else if ( style->fill.isColor() ) {
1276         shape->setFill(style->fill.value.color);
1277     } else if ( style->fill.isNone() ) {
1278         shape->setFill(NULL);
1279     } else {
1280         g_assert_not_reached();
1281     }
1282     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1283     switch (style->fill_rule.computed) {
1284         case SP_WIND_RULE_EVENODD: {
1285             shape->setFillRule(NRArenaShape::EVEN_ODD);
1286             break;
1287         }
1288         case SP_WIND_RULE_NONZERO: {
1289             shape->setFillRule(NRArenaShape::NONZERO);
1290             break;
1291         }
1292         default: {
1293             g_assert_not_reached();
1294         }
1295     }
1297     if ( style->stroke.isPaintserver() ) {
1298         shape->setStroke(style->getStrokePaintServer());
1299     } else if ( style->stroke.isColor() ) {
1300         shape->setStroke(style->stroke.value.color);
1301     } else if ( style->stroke.isNone() ) {
1302         shape->setStroke(NULL);
1303     } else {
1304         g_assert_not_reached();
1305     }
1306     shape->setStrokeWidth(style->stroke_width.computed);
1307     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1308     switch (style->stroke_linecap.computed) {
1309         case SP_STROKE_LINECAP_ROUND: {
1310             shape->setLineCap(NRArenaShape::ROUND_CAP);
1311             break;
1312         }
1313         case SP_STROKE_LINECAP_SQUARE: {
1314             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1315             break;
1316         }
1317         case SP_STROKE_LINECAP_BUTT: {
1318             shape->setLineCap(NRArenaShape::BUTT_CAP);
1319             break;
1320         }
1321         default: {
1322             g_assert_not_reached();
1323         }
1324     }
1325     switch (style->stroke_linejoin.computed) {
1326         case SP_STROKE_LINEJOIN_ROUND: {
1327             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1328             break;
1329         }
1330         case SP_STROKE_LINEJOIN_BEVEL: {
1331             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1332             break;
1333         }
1334         case SP_STROKE_LINEJOIN_MITER: {
1335             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1336             break;
1337         }
1338         default: {
1339             g_assert_not_reached();
1340         }
1341     }
1342     shape->setMitreLimit(style->stroke_miterlimit.value);
1344     //if shape has a filter
1345     if (style->filter.set && style->getFilter()) {
1346         if (!shape->filter) {
1347             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1348             shape->filter = new NR::Filter(primitives);
1349         }
1350         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1351     } else {
1352         //no filter set for this shape
1353         delete shape->filter;
1354         shape->filter = NULL;
1355     }
1357     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1360 void
1361 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1363     g_return_if_fail(shape != NULL);
1364     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1365     g_return_if_fail(pbox != NULL);
1367     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1368         shape->paintbox = *pbox;
1369     } else {
1370         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1371         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1372         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1373     }
1375     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1378 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1380     paintbox.x0 = pbox.min()[NR::X];
1381     paintbox.y0 = pbox.min()[NR::Y];
1382     paintbox.x1 = pbox.max()[NR::X];
1383     paintbox.y1 = pbox.max()[NR::Y];
1385     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1388 static void
1389 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1391     if ( st >= en ) return;
1392     if ( vst < 0 ) vst=0;
1393     if ( vst > 1 ) vst=1;
1394     if ( ven < 0 ) ven=0;
1395     if ( ven > 1 ) ven=1;
1396     float   sv=vst;
1397     float   dv=ven-vst;
1398     int     len=en-st;
1399     unsigned char*   d=(unsigned char*)dest.buffer;
1400     d+=(st-dest.startPix);
1401     if ( fabs(dv) < 0.001 ) {
1402         if ( vst > 0.999 ) {
1403             /* Simple copy */
1404             while (len > 0) {
1405                 d[0] = 255;
1406                 d += 1;
1407                 len -= 1;
1408             }
1409         } else {
1410             sv*=256;
1411             unsigned int c0_24=(int)sv;
1412             c0_24&=0xFF;
1413             while (len > 0) {
1414                 /* Draw */
1415                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1416                 d += 1;
1417                 len -= 1;
1418             }
1419         }
1420     } else {
1421         if ( en <= st+1 ) {
1422             sv=0.5*(vst+ven);
1423             sv*=256;
1424             unsigned int c0_24=(int)sv;
1425             c0_24&=0xFF;
1426             /* Draw */
1427             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1428         } else {
1429             dv/=len;
1430             sv+=0.5*dv; // correction trapezoidale
1431             sv*=16777216;
1432             dv*=16777216;
1433             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1434             int s0_24 = static_cast<int>(dv);
1435             while (len > 0) {
1436                 unsigned int ca;
1437                 /* Draw */
1438                 ca = c0_24 >> 16;
1439                 if ( ca > 255 ) ca=255;
1440                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1441                 d += 1;
1442                 c0_24 += s0_24;
1443                 c0_24 = CLAMP(c0_24, 0, 16777216);
1444                 len -= 1;
1445             }
1446         }
1447     }
1450 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1452     theS->CalcBBox();
1453     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1454     int    il,ir,it,ib;
1455     il=(int)floor(l);
1456     ir=(int)ceil(r);
1457     it=(int)floor(t);
1458     ib=(int)ceil(b);
1460     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1461     if ( il < m.area.x0 ) il=m.area.x0;
1462     if ( it < m.area.y0 ) it=m.area.y0;
1463     if ( ir > m.area.x1 ) ir=m.area.x1;
1464     if ( ib > m.area.y1 ) ib=m.area.y1;
1466     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1467     int    curPt;
1468     float  curY;
1469     theS->BeginQuickRaster(curY, curPt);
1471     FloatLigne *theI = new FloatLigne();
1472     IntLigne *theIL = new IntLigne();
1474     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1476     char *mdata = (char*)m.data.px;
1477     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1478     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1479     for (int y = it; y < ib; y++) {
1480         theI->Reset();
1481         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1482         theI->Flatten();
1483         theIL->Copy(theI);
1485         raster_info  dest;
1486         dest.startPix=il;
1487         dest.endPix=ir;
1488         dest.sth=il;
1489         dest.stv=y;
1490         dest.buffer=ligStart;
1491         theIL->Raster(dest, NULL, shape_run_A8_OR);
1492         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1493     }
1494     theS->EndQuickRaster();
1495     delete theI;
1496     delete theIL;
1500 /*
1501   Local Variables:
1502   mode:c++
1503   c-file-style:"stroustrup"
1504   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1505   indent-tabs-mode:nil
1506   fill-column:99
1507   End:
1508 */
1509 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :