Code

Added renderer support for feBlend filter effect
[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 <libnr/n-art-bpath.h>
21 #include <libnr/nr-path.h>
22 #include <libnr/nr-pixops.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-fns.h>
25 #include <libnr/nr-blit.h>
26 #include <livarot/Path.h>
27 #include <livarot/float-line.h>
28 #include <livarot/int-line.h>
29 #include <style.h>
30 #include "prefs-utils.h"
31 #include "sp-filter.h"
32 #include "inkscape-cairo.h"
34 #include "display/nr-filter.h"
35 #include "display/nr-filter-gaussian.h"
36 #include "display/nr-filter-types.h"
37 #include "sp-gaussian-blur.h"
38 #include "sp-feblend.h"
39 #include "display/nr-filter-blend.h"
41 #include <cairo.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, NR::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     nr_matrix_set_identity(&shape->ctm);
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     nr_matrix_set_identity(&shape->cached_fctm);
128     nr_matrix_set_identity(&shape->cached_sctm);
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) sp_curve_unref(shape->curve);
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,NRRect &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     NRRect bbox;
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                 NRBPath bp;
256                 /* fixme: */
257                 bbox.x0 = bbox.y0 = NR_HUGE;
258                 bbox.x1 = bbox.y1 = -NR_HUGE;
259                 bp.path = SP_CURVE_BPATH(shape->curve);
260                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
261                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
262                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
263                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
264                 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
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     bbox.x0 = bbox.y0 = NR_HUGE;
278     bbox.x1 = bbox.y1 = -NR_HUGE;
280     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
282     if (shape->curve) {
283         NRBPath bp;
284         /* fixme: */
285         bbox.x0 = bbox.y0 = NR_HUGE;
286         bbox.x1 = bbox.y1 = -NR_HUGE;
287         bp.path = SP_CURVE_BPATH(shape->curve);
288         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
289         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
290             float width, scale;
291             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
292             width = MAX(0.125, shape->_stroke.width * scale);
293             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
294                 bbox.x0-=width;
295                 bbox.x1+=width;
296                 bbox.y0-=width;
297                 bbox.y1+=width;
298             }
299             // those pesky miters, now
300             float miterMax=width*shape->_stroke.mitre_limit;
301             if ( miterMax > 0.01 ) {
302                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
303                 bbox.x0-=miterMax;
304                 bbox.x1+=miterMax;
305                 bbox.y0-=miterMax;
306                 bbox.y1+=miterMax;
307             }
308         }
309     } else {
310     }
311     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
312     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
313     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
314     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
315     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
317     /* Release state data */
318     if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
319         /* Concept test */
320         if (shape->fill_shp) {
321             delete shape->fill_shp;
322             shape->fill_shp = NULL;
323         }
324     }
325     if (shape->stroke_shp) {
326         delete shape->stroke_shp;
327         shape->stroke_shp = NULL;
328     }
329     if (shape->fill_painter) {
330         sp_painter_free(shape->fill_painter);
331         shape->fill_painter = NULL;
332     }
333     if (shape->stroke_painter) {
334         sp_painter_free(shape->stroke_painter);
335         shape->stroke_painter = NULL;
336     }
338     if (!shape->curve || 
339         !shape->style ||
340         sp_curve_is_empty(shape->curve) ||
341         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
342          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
343     {
344         item->bbox = shape->approx_bbox;
345         return NR_ARENA_ITEM_STATE_ALL;
346     }
348     /* Build state data */
349     if ( shape->delayed_shp ) {
350         item->bbox=shape->approx_bbox;
351     } else {
352         nr_arena_shape_update_stroke(shape, gc, area);
353         nr_arena_shape_update_fill(shape, gc, area);
355         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
356         nr_arena_shape_add_bboxes(shape,bbox);
358         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
359         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
360         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
361         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
362     }
364     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
366     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
367     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
368     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
369     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
370     nr_arena_request_render_rect(item->arena, &item->bbox);
372     item->render_opacity = TRUE;
373     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
374         if (gc && gc->parent) {
375             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
376                                                               NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
377                                                               &shape->paintbox);
378         }
379         item->render_opacity = FALSE;
380     }
381     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
382         if (gc && gc->parent) {
383             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
384                                                                 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
385                                                                 &shape->paintbox);
386         }
387         item->render_opacity = FALSE;
388     }
389     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
390            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
391           || (shape->markers)
392         )
393     {
394         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
395         item->render_opacity = FALSE;
396     }
398     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
399         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
400             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
401         }
402     }
404     return NR_ARENA_ITEM_STATE_ALL;
407 int matrix_is_isometry(NR::Matrix p) {
408     NR::Matrix   tp;
409     // transposition
410     tp[0]=p[0];
411     tp[1]=p[2];
412     tp[2]=p[1];
413     tp[3]=p[3];
414     for (int i = 4; i < 6; i++) // shut valgrind up :)
415         tp[i] = p[i] = 0;
416     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
417     // Is the adjunct nearly an identity function?
418     if (isom.is_translation(0.01)) {
419         // the transformation is an isometry -> no need to recompute
420         // the uncrossed polygon
421         if ( p.det() < 0 )
422             return -1;
423         else
424             return 1;
425     }
426     return 0;
429 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
430     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
433 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
434 void
435 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
437     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
438         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
439         if (TRUE || !shape->fill_shp) {
440             NR::Matrix  cached_to_new = NR::identity();
441             int isometry = 0;
442             if ( shape->cached_fill ) {
443                 if (shape->cached_fctm == gc->transform) {
444                     isometry = 2; // identity
445                 } else {
446                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
447                     isometry = matrix_is_isometry(cached_to_new);
448                 }
449                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
450                     isometry = 0;
451             }
452             if ( isometry == 0 ) {
453                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
454                 shape->cached_fill->Reset();
456                 Path*  thePath=new Path;
457                 Shape* theShape=new Shape;
458                 {
459                     NR::Matrix   tempMat(gc->transform);
460                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
461                 }
463                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
464                     thePath->Convert(1.0);
465                     shape->cached_fpartialy = false;
466                 } else {
467                     thePath->Convert(area, 1.0);
468                     shape->cached_fpartialy = true;
469                 }
471                 thePath->Fill(theShape, 0);
473                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
474                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
475                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
476                     //shape->cached_fill->Copy(theShape);
477                 } else {
478                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
479                 }
480                 shape->cached_fctm=gc->transform;
481                 shape->cached_farea = *area;
482                 delete theShape;
483                 delete thePath;
484                 if ( shape->fill_shp == NULL )
485                     shape->fill_shp = new Shape;
487                 shape->fill_shp->Copy(shape->cached_fill);
489             } else if ( 2 == isometry ) {
490                 if ( shape->fill_shp == NULL ) {
491                     shape->fill_shp = new Shape;
492                     shape->fill_shp->Copy(shape->cached_fill);
493                 }
494             } else {
496                 if ( shape->fill_shp == NULL )
497                     shape->fill_shp = new Shape;
499                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
500                                        shape->cached_fill->numberOfEdges());
501                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
502                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
503                 if ( isometry == 1 ) {
504                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
505                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
506                                                  shape->cached_fill->getEdge(i).en);
507                 } else if ( isometry == -1 ) { // need to flip poly.
508                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
509                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
510                                                  shape->cached_fill->getEdge(i).st);
511                 }
512                 shape->fill_shp->ForceToPolygon();
513                 shape->fill_shp->needPointsSorting();
514                 shape->fill_shp->needEdgesSorting();
515             }
516             shape->delayed_shp |= shape->cached_fpartialy;
517         }
518     }
521 void
522 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
524     SPStyle* style = shape->style;
526     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
528     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
530     if (outline) {
531         // cairo does not need the livarot path for rendering
532         return; 
533     }
535     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
536     // (since it will only be used for picking, not for rendering)
538     if (outline ||
539         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
540          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
542         float style_width = MAX(0.125, shape->_stroke.width * scale);
543         float width;
544         if (outline) {
545             width = 0.5; // 1 pixel wide, independent of zoom
546         } else {
547             width = style_width;
548         }
550         NR::Matrix  cached_to_new = NR::identity();
552         int isometry = 0;
553         if ( shape->cached_stroke ) {
554             if (shape->cached_sctm == gc->transform) {
555                 isometry = 2; // identity
556             } else {
557                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
558                 isometry = matrix_is_isometry(cached_to_new);
559             }
560             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
561                 isometry = 0;
562             if (0 != isometry && width != shape->cached_width) { 
563                 // if this happens without setting style, we have just switched to outline or back
564                 isometry = 0; 
565             } 
566         }
568         if ( isometry == 0 ) {
569             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
570             shape->cached_stroke->Reset();
571             Path*  thePath = new Path;
572             Shape* theShape = new Shape;
573             {
574                 NR::Matrix   tempMat(gc->transform);
575                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
576             }
578             // add some padding to the rendering area, so clipped path does not go into a render area
579             NRRectL padded_area = *area;
580             padded_area.x0 -= (NR::ICoord)width;
581             padded_area.x1 += (NR::ICoord)width;
582             padded_area.y0 -= (NR::ICoord)width;
583             padded_area.y1 += (NR::ICoord)width;
584             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
585                 thePath->Convert((outline) ? 4.0 : 1.0);
586                 shape->cached_spartialy = false;
587             }
588             else {
589                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
590                 shape->cached_spartialy = true;
591             }
593             if (style->stroke_dash.n_dash && !outline) {
594                 thePath->DashPolylineFromStyle(style, scale, 1.0);
595             }
597             ButtType butt=butt_straight;
598             switch (shape->_stroke.cap) {
599                 case NRArenaShape::BUTT_CAP:
600                     butt = butt_straight;
601                     break;
602                 case NRArenaShape::ROUND_CAP:
603                     butt = butt_round;
604                     break;
605                 case NRArenaShape::SQUARE_CAP:
606                     butt = butt_square;
607                     break;
608             }
609             JoinType join=join_straight;
610             switch (shape->_stroke.join) {
611                 case NRArenaShape::MITRE_JOIN:
612                     join = join_pointy;
613                     break;
614                 case NRArenaShape::ROUND_JOIN:
615                     join = join_round;
616                     break;
617                 case NRArenaShape::BEVEL_JOIN:
618                     join = join_straight;
619                     break;
620             }
622             if (outline) {
623                 butt = butt_straight;
624                 join = join_straight;
625             }
627             thePath->Stroke(theShape, false, 0.5*width, join, butt,
628                             0.5*width*shape->_stroke.mitre_limit);
631             if (outline) {
632                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
633                 shape->cached_stroke->Copy(theShape);
634             } else {
635                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
636             }
638             shape->cached_width = width;
640             shape->cached_sctm=gc->transform;
641             shape->cached_sarea = *area;
642             delete thePath;
643             delete theShape;
644             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
646             shape->stroke_shp->Copy(shape->cached_stroke);
648         } else if ( 2 == isometry ) {
649             if ( shape->stroke_shp == NULL ) {
650                 shape->stroke_shp=new Shape;
651                 shape->stroke_shp->Copy(shape->cached_stroke);
652             }
653         } else {
654             if ( shape->stroke_shp == NULL )
655                 shape->stroke_shp=new Shape;
656             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
657             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
658                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
659             if ( isometry == 1 ) {
660                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
661                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
662                                                shape->cached_stroke->getEdge(i).en);
663             } else if ( isometry == -1 ) {
664                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
665                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
666                                                shape->cached_stroke->getEdge(i).st);
667             }
668             shape->stroke_shp->ForceToPolygon();
669             shape->stroke_shp->needPointsSorting();
670             shape->stroke_shp->needEdgesSorting();
671         }
672         shape->delayed_shp |= shape->cached_spartialy;
673     }
677 void
678 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
680     if ( shape->stroke_shp ) {
681         shape->stroke_shp->CalcBBox();
682         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
683         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
684         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
685         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
686         if ( bbox.x0 >= bbox.x1 ) {
687             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
688                 bbox.x0=shape->stroke_shp->leftX;
689                 bbox.x1=shape->stroke_shp->rightX;
690             }
691         } else {
692             if ( shape->stroke_shp->leftX < bbox.x0 )
693                 bbox.x0=shape->stroke_shp->leftX;
694             if ( shape->stroke_shp->rightX > bbox.x1 )
695                 bbox.x1=shape->stroke_shp->rightX;
696         }
697         if ( bbox.y0 >= bbox.y1 ) {
698             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
699                 bbox.y0=shape->stroke_shp->topY;
700                 bbox.y1=shape->stroke_shp->bottomY;
701             }
702         } else {
703             if ( shape->stroke_shp->topY < bbox.y0 )
704                 bbox.y0=shape->stroke_shp->topY;
705             if ( shape->stroke_shp->bottomY > bbox.y1 )
706                 bbox.y1=shape->stroke_shp->bottomY;
707         }
708     }
709     if ( shape->fill_shp ) {
710         shape->fill_shp->CalcBBox();
711         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
712         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
713         shape->fill_shp->topY=floor(shape->fill_shp->topY);
714         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
715         if ( bbox.x0 >= bbox.x1 ) {
716             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
717                 bbox.x0=shape->fill_shp->leftX;
718                 bbox.x1=shape->fill_shp->rightX;
719             }
720         } else {
721             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
722             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
723         }
724         if ( bbox.y0 >= bbox.y1 ) {
725             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
726                 bbox.y0=shape->fill_shp->topY;
727                 bbox.y1=shape->fill_shp->bottomY;
728             }
729         } else {
730             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
731             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
732         }
733     }
736 // cairo outline rendering:
737 static unsigned int
738 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
740     NRArenaShape *shape = NR_ARENA_SHAPE(item);
742     if (!ct) 
743         return item->state;
745     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
746     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
747     // by setting color channels in the "wrong" order
748     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));
750     cairo_set_line_width(ct, 0.5);
751     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
752     cairo_new_path(ct);
754     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
756     cairo_stroke(ct);
758     return item->state;
761 // cairo stroke rendering (flat color only so far!):
762 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
763 // (need to switch them to premul before this can be enabled)
764 void
765 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
767     NRArenaShape *shape = NR_ARENA_SHAPE(item);
768     SPStyle const *style = shape->style;
770     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
772     if (fabs(shape->_stroke.width * scale) < 0.01)
773         return;
775     cairo_t *ct = nr_create_cairo_context (area, pb);
777     if (!ct) 
778         return;
780     guint32 rgba;
781     if ( item->render_opacity ) {
782         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
783                                           shape->_stroke.opacity *
784                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
785     } else {
786         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
787                                           shape->_stroke.opacity);
788     }
790     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
791     // by setting color channels in the "wrong" order
792     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));
794     float style_width = MAX(0.125, shape->_stroke.width * scale);
795     cairo_set_line_width(ct, style_width);
797     switch (shape->_stroke.cap) {
798         case NRArenaShape::BUTT_CAP:
799             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
800             break;
801         case NRArenaShape::ROUND_CAP:
802             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
803             break;
804         case NRArenaShape::SQUARE_CAP:
805             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
806             break;
807     }
808     switch (shape->_stroke.join) {
809         case NRArenaShape::MITRE_JOIN:
810             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
811             break;
812         case NRArenaShape::ROUND_JOIN:
813             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
814             break;
815         case NRArenaShape::BEVEL_JOIN:
816             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
817             break;
818     }
820     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
822     if (style->stroke_dash.n_dash) {
823         NRVpathDash dash;
824         dash.offset = style->stroke_dash.offset * scale;
825         dash.n_dash = style->stroke_dash.n_dash;
826         dash.dash = g_new(double, dash.n_dash);
827         for (int i = 0; i < dash.n_dash; i++) {
828             dash.dash[i] = style->stroke_dash.dash[i] * scale;
829         }
830         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
831         g_free(dash.dash);
832     }
834     cairo_set_tolerance(ct, 0.1);
835     cairo_new_path(ct);
837     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
839     cairo_stroke(ct);
841     cairo_surface_t *cst = cairo_get_target(ct);
842     cairo_destroy (ct);
843     cairo_surface_finish (cst);
844     cairo_surface_destroy (cst);
846     pb->empty = FALSE;
850 /**
851  * Renders the item.  Markers are just composed into the parent buffer.
852  */
853 static unsigned int
854 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
856     NRArenaShape *shape = NR_ARENA_SHAPE(item);
858     if (!shape->curve) return item->state;
859     if (!shape->style) return item->state;
861     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
863     if (outline) { // cairo outline rendering
865         pb->empty = FALSE;
866         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
867         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
869     } else {
871     if ( shape->delayed_shp ) {
872         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
873             NRGC   tempGC(NULL);
874             tempGC.transform=shape->ctm;
875             shape->delayed_shp = false;
876             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
877             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
878 /*      NRRect bbox;
879         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
880         nr_arena_shape_add_bboxes(shape,bbox);
881         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
882         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
883         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
884         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
885         shape->approx_bbox=item->bbox;*/
886         } else {
887             return item->state;
888         }
889     }
891     SPStyle const *style = shape->style;
892     if (shape->fill_shp) {
893         NRPixBlock m;
894         guint32 rgba;
896         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
898         // if memory allocation failed, abort render
899         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
900             nr_pixblock_release (&m);
901             return (item->state);
902         }
904         m.visible_area = pb->visible_area; 
905         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
906         m.empty = FALSE;
908         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
909             // do not render fill in any way
910         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
911             if ( item->render_opacity ) {
912                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
913                                                   shape->_fill.opacity *
914                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
915             } else {
916                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
917                                                   shape->_fill.opacity);
918             }
919             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
920             pb->empty = FALSE;
921         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
922             if (shape->fill_painter) {
923                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
924             }
925         }
927         nr_pixblock_release(&m);
928     }
930     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
932         // cairo_arena_shape_render_stroke(item, area, pb);
934         guint32 rgba;
935         NRPixBlock m;
937         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
939         // if memory allocation failed, abort render
940         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
941             nr_pixblock_release (&m);
942             return (item->state);
943         }
945         m.visible_area = pb->visible_area; 
946         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
947         m.empty = FALSE;
949             if ( item->render_opacity ) {
950                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
951                                                   shape->_stroke.opacity *
952                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
953             } else {
954                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
955                                                   shape->_stroke.opacity);
956             }
957             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
958             pb->empty = FALSE;
960         nr_pixblock_release(&m);
962     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
964         NRPixBlock m;
966         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
968         // if memory allocation failed, abort render
969         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
970             nr_pixblock_release (&m);
971             return (item->state);
972         }
974         m.visible_area = pb->visible_area; 
975         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
976         m.empty = FALSE;
978         if (shape->stroke_painter) {
979             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
980         }
982         nr_pixblock_release(&m);
983     }
985     } // non-cairo non-outline branch
987     /* Render markers into parent buffer */
988     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
989         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
990         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
991     }
993     return item->state;
997 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
998 // reenable this when the bug is fixed and remove the rest of this function
999 static guint
1000 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1002     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1003     if (!shape->curve) return item->state;
1005         cairo_t *ct = nr_create_cairo_context (area, pb);
1007         if (!ct) 
1008             return item->state;
1010         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1012         cairo_new_path(ct);
1014         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1016         cairo_fill(ct);
1018         cairo_surface_t *cst = cairo_get_target(ct);
1019         cairo_destroy (ct);
1020         cairo_surface_finish (cst);
1021         cairo_surface_destroy (cst);
1023         pb->empty = FALSE;
1025         return item->state;
1029 static guint
1030 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1032     //return cairo_arena_shape_clip(item, area, pb);
1034     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1035     if (!shape->curve) return item->state;
1037     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1038         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1039             NRGC   tempGC(NULL);
1040             tempGC.transform=shape->ctm;
1041             shape->delayed_shp = false;
1042             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1043         } else {
1044             return item->state;
1045         }
1046     }
1048     if ( shape->fill_shp ) {
1049         NRPixBlock m;
1051         /* fixme: We can OR in one step (Lauris) */
1052         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1054         // if memory allocation failed, abort 
1055         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1056             nr_pixblock_release (&m);
1057             return (item->state);
1058         }
1060         m.visible_area = pb->visible_area; 
1061         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1063         for (int y = area->y0; y < area->y1; y++) {
1064             unsigned char *s, *d;
1065             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1066             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1067             for (int x = area->x0; x < area->x1; x++) {
1068                 *d = NR_COMPOSEA_111(*s, *d);
1069                 d ++;
1070                 s ++;
1071             }
1072         }
1073         nr_pixblock_release(&m);
1074         pb->empty = FALSE;
1075     }
1077     return item->state;
1080 static NRArenaItem *
1081 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1083     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1085     if (shape->repick_after > 0)
1086         shape->repick_after--;
1088     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1089         return shape->last_pick;
1091     if (!shape->curve) return NULL;
1092     if (!shape->style) return NULL;
1093     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1094         return NULL;
1096     GTimeVal tstart, tfinish;
1097     g_get_current_time (&tstart);
1099     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1101     double width;
1102     if (outline) {
1103         width = 0.5;
1104     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1105         float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1106         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1107     } else {
1108         width = 0;
1109     }
1111     NRBPath bp;
1112     bp.path = SP_CURVE_BPATH(shape->curve);
1113     double dist = NR_HUGE;
1114     int wind = 0;
1115     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE 
1116              && shape->_fill.opacity > 1e-3 && !outline);
1118     if (item->arena->canvasarena) {
1119         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1120         viewbox.growBy (width);
1121         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1122     } else {
1123         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1124     }
1126     g_get_current_time (&tfinish);
1127     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1128     //g_print ("pick time %lu\n", this_pick);
1130     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1131         shape->repick_after = this_pick / 5000;
1132     }
1134     // covered by fill?
1135     if (needfill) {
1136         if (!shape->style->fill_rule.computed) {
1137             if (wind != 0) {
1138                 shape->last_pick = item;
1139                 return item;
1140             }
1141         } else {
1142             if (wind & 0x1) {
1143                 shape->last_pick = item;
1144                 return item;
1145             }
1146         }
1147     }
1149     // close to the edge, as defined by strokewidth and delta?
1150     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1151     if (needfill || width > 0) { // if either fill or stroke visible,
1152         if ((dist - width) < delta) {
1153             shape->last_pick = item;
1154             return item;
1155         }
1156     }
1158     // if not picked on the shape itself, try its markers
1159     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1160         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1161         if (ret) {
1162             shape->last_pick = item;
1163             return item;
1164         }
1165     }
1167     shape->last_pick = NULL;
1168     return NULL;
1171 /**
1172  *
1173  *  Requests a render of the shape, then if the shape is already a curve it
1174  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1175  *  curve and adds it to the shape.  Finally, it requests an update of the
1176  *  arena for the shape.
1177  */
1178 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1180     g_return_if_fail(shape != NULL);
1181     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1183     if ( justTrans == false ) {
1184         // dirty cached versions
1185         if ( shape->cached_fill ) {
1186             delete shape->cached_fill;
1187             shape->cached_fill=NULL;
1188         }
1189         if ( shape->cached_stroke ) {
1190             delete shape->cached_stroke;
1191             shape->cached_stroke=NULL;
1192         }
1193     }
1195     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1197     if (shape->curve) {
1198         sp_curve_unref(shape->curve);
1199         shape->curve = NULL;
1200     }
1202     if (curve) {
1203         shape->curve = curve;
1204         sp_curve_ref(curve);
1205     }
1207     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1210 void NRArenaShape::setFill(SPPaintServer *server) {
1211     _fill.paint.set(server);
1212     _invalidateCachedFill();
1215 void NRArenaShape::setFill(SPColor const &color) {
1216     _fill.paint.set(color);
1217     _invalidateCachedFill();
1220 void NRArenaShape::setFillOpacity(double opacity) {
1221     _fill.opacity = opacity;
1222     _invalidateCachedFill();
1225 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1226     _fill.rule = rule;
1227     _invalidateCachedFill();
1230 void NRArenaShape::setStroke(SPPaintServer *server) {
1231     _stroke.paint.set(server);
1232     _invalidateCachedStroke();
1235 void NRArenaShape::setStroke(SPColor const &color) {
1236     _stroke.paint.set(color);
1237     _invalidateCachedStroke();
1240 void NRArenaShape::setStrokeOpacity(double opacity) {
1241     _stroke.opacity = opacity;
1242     _invalidateCachedStroke();
1245 void NRArenaShape::setStrokeWidth(double width) {
1246     _stroke.width = width;
1247     _invalidateCachedStroke();
1250 void NRArenaShape::setMitreLimit(double limit) {
1251     _stroke.mitre_limit = limit;
1252     _invalidateCachedStroke();
1255 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1256     _stroke.cap = cap;
1257     _invalidateCachedStroke();
1260 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1261     _stroke.join = join;
1262     _invalidateCachedStroke();
1265 /** nr_arena_shape_set_style
1266  *
1267  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1268  */
1269 void
1270 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1272     g_return_if_fail(shape != NULL);
1273     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1275     if (style) sp_style_ref(style);
1276     if (shape->style) sp_style_unref(shape->style);
1277     shape->style = style;
1279     switch (style->fill.type) {
1280         case SP_PAINT_TYPE_NONE: {
1281             shape->setFill(NULL);
1282             break;
1283         }
1284         case SP_PAINT_TYPE_COLOR: {
1285             shape->setFill(style->fill.value.color);
1286             break;
1287         }
1288         case SP_PAINT_TYPE_PAINTSERVER: {
1289             shape->setFill(style->fill.value.paint.server);
1290             break;
1291         }
1292         default: {
1293             g_assert_not_reached();
1294         }
1295     }
1296     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1297     switch (style->fill_rule.computed) {
1298         case SP_WIND_RULE_EVENODD: {
1299             shape->setFillRule(NRArenaShape::EVEN_ODD);
1300             break;
1301         }
1302         case SP_WIND_RULE_NONZERO: {
1303             shape->setFillRule(NRArenaShape::NONZERO);
1304             break;
1305         }
1306         default: {
1307             g_assert_not_reached();
1308         }
1309     }
1311     switch (style->stroke.type) {
1312         case SP_PAINT_TYPE_NONE: {
1313             shape->setStroke(NULL);
1314             break;
1315         }
1316         case SP_PAINT_TYPE_COLOR: {
1317             shape->setStroke(style->stroke.value.color);
1318             break;
1319         }
1320         case SP_PAINT_TYPE_PAINTSERVER: {
1321             shape->setStroke(style->stroke.value.paint.server);
1322             break;
1323         }
1324         default: {
1325             g_assert_not_reached();
1326         }
1327     }
1328     shape->setStrokeWidth(style->stroke_width.computed);
1329     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1330     switch (style->stroke_linecap.computed) {
1331         case SP_STROKE_LINECAP_ROUND: {
1332             shape->setLineCap(NRArenaShape::ROUND_CAP);
1333             break;
1334         }
1335         case SP_STROKE_LINECAP_SQUARE: {
1336             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1337             break;
1338         }
1339         case SP_STROKE_LINECAP_BUTT: {
1340             shape->setLineCap(NRArenaShape::BUTT_CAP);
1341             break;
1342         }
1343         default: {
1344             g_assert_not_reached();
1345         }
1346     }
1347     switch (style->stroke_linejoin.computed) {
1348         case SP_STROKE_LINEJOIN_ROUND: {
1349             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1350             break;
1351         }
1352         case SP_STROKE_LINEJOIN_BEVEL: {
1353             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1354             break;
1355         }
1356         case SP_STROKE_LINEJOIN_MITER: {
1357             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1358             break;
1359         }
1360         default: {
1361             g_assert_not_reached();
1362         }
1363     }
1364     shape->setMitreLimit(style->stroke_miterlimit.value);
1366     //if shape has a filter
1367     if (style->filter.set && style->filter.filter) 
1368     {
1369         shape->filter = new NR::Filter();
1370         shape->filter->set_x(style->filter.filter->x);
1371         shape->filter->set_y(style->filter.filter->y);
1372         shape->filter->set_width(style->filter.filter->width);
1373         shape->filter->set_height(style->filter.filter->height);
1374         
1375         //go through all SP filter primitives
1376         for(int i=0; i<style->filter.filter->_primitive_count; i++)
1377         {
1378             SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1379             //if primitive is gaussianblur
1380             if(SP_IS_GAUSSIANBLUR(primitive)) {
1381                 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1382                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1383                 float num = spblur->stdDeviation.getNumber();
1384                 if( num>=0.0 )
1385                 {
1386                     float optnum = spblur->stdDeviation.getOptNumber();
1387                     if( optnum>=0.0 )
1388                         gaussian->set_deviation((double) num, (double) optnum);
1389                     else
1390                         gaussian->set_deviation((double) num);
1391                 }
1392             } else if(SP_IS_FEBLEND(primitive)) {
1393                 // TODO: this is just a test. Besides this whole filter
1394                 // creation needs to be redone
1395                 NR::FilterBlend *nrblend = (NR::FilterBlend *) shape->filter->add_primitive(NR::NR_FILTER_BLEND);
1396                 SPFeBlend *spblend = SP_FEBLEND(primitive);
1397                 nrblend->set_mode(spblend->blend_mode);
1398             }
1399         }
1400     }
1401     else
1402     {
1403         //no filter set for this shape
1404         shape->filter = NULL;
1405     }
1407     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1410 void
1411 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1413     g_return_if_fail(shape != NULL);
1414     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1415     g_return_if_fail(pbox != NULL);
1417     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1418         shape->paintbox = *pbox;
1419     } else {
1420         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1421         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1422         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1423     }
1425     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1428 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1430     paintbox.x0 = pbox.min()[NR::X];
1431     paintbox.y0 = pbox.min()[NR::Y];
1432     paintbox.x1 = pbox.max()[NR::X];
1433     paintbox.y1 = pbox.max()[NR::Y];
1435     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1438 static void
1439 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1441     if ( st >= en ) return;
1442     if ( vst < 0 ) vst=0;
1443     if ( vst > 1 ) vst=1;
1444     if ( ven < 0 ) ven=0;
1445     if ( ven > 1 ) ven=1;
1446     float   sv=vst;
1447     float   dv=ven-vst;
1448     int     len=en-st;
1449     unsigned char*   d=(unsigned char*)dest.buffer;
1450     d+=(st-dest.startPix);
1451     if ( fabs(dv) < 0.001 ) {
1452         if ( vst > 0.999 ) {
1453             /* Simple copy */
1454             while (len > 0) {
1455                 d[0] = 255;
1456                 d += 1;
1457                 len -= 1;
1458             }
1459         } else {
1460             sv*=256;
1461             unsigned int c0_24=(int)sv;
1462             c0_24&=0xFF;
1463             while (len > 0) {
1464                 /* Draw */
1465                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1466                 d += 1;
1467                 len -= 1;
1468             }
1469         }
1470     } else {
1471         if ( en <= st+1 ) {
1472             sv=0.5*(vst+ven);
1473             sv*=256;
1474             unsigned int c0_24=(int)sv;
1475             c0_24&=0xFF;
1476             /* Draw */
1477             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1478         } else {
1479             dv/=len;
1480             sv+=0.5*dv; // correction trapezoidale
1481             sv*=16777216;
1482             dv*=16777216;
1483             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1484             int s0_24 = static_cast<int>(dv);
1485             while (len > 0) {
1486                 unsigned int ca;
1487                 /* Draw */
1488                 ca = c0_24 >> 16;
1489                 if ( ca > 255 ) ca=255;
1490                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1491                 d += 1;
1492                 c0_24 += s0_24;
1493                 c0_24 = CLAMP(c0_24, 0, 16777216);
1494                 len -= 1;
1495             }
1496         }
1497     }
1500 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1502     theS->CalcBBox();
1503     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1504     int    il,ir,it,ib;
1505     il=(int)floor(l);
1506     ir=(int)ceil(r);
1507     it=(int)floor(t);
1508     ib=(int)ceil(b);
1510     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1511     if ( il < m.area.x0 ) il=m.area.x0;
1512     if ( it < m.area.y0 ) it=m.area.y0;
1513     if ( ir > m.area.x1 ) ir=m.area.x1;
1514     if ( ib > m.area.y1 ) ib=m.area.y1;
1516     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1517     int    curPt;
1518     float  curY;
1519     theS->BeginQuickRaster(curY, curPt);
1521     FloatLigne *theI = new FloatLigne();
1522     IntLigne *theIL = new IntLigne();
1524     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1526     char *mdata = (char*)m.data.px;
1527     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1528     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1529     for (int y = it; y < ib; y++) {
1530         theI->Reset();
1531         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1532         theI->Flatten();
1533         theIL->Copy(theI);
1535         raster_info  dest;
1536         dest.startPix=il;
1537         dest.endPix=ir;
1538         dest.sth=il;
1539         dest.stv=y;
1540         dest.buffer=ligStart;
1541         theIL->Raster(dest, NULL, shape_run_A8_OR);
1542         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1543     }
1544     theS->EndQuickRaster();
1545     delete theI;
1546     delete theIL;
1550 /*
1551   Local Variables:
1552   mode:c++
1553   c-file-style:"stroustrup"
1554   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1555   indent-tabs-mode:nil
1556   fill-column:99
1557   End:
1558 */
1559 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :