Code

two picking optimizations: 1 use our canvas' viewbox so that invisible segments can...
[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/nr-filter.h"
21 #include "display/nr-filter-gaussian.h"
22 #include "display/nr-filter-types.h"
23 #include <libnr/n-art-bpath.h>
24 #include <libnr/nr-path.h>
25 #include <libnr/nr-pixops.h>
26 #include <libnr/nr-matrix-ops.h>
27 #include <libnr/nr-matrix-fns.h>
28 #include <libnr/nr-blit.h>
29 #include <livarot/Path.h>
30 #include <livarot/float-line.h>
31 #include <livarot/int-line.h>
32 #include <style.h>
33 #include "prefs-utils.h"
34 #include "sp-filter.h"
35 #include "sp-gaussian-blur.h"
36 #include "inkscape-cairo.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     nr_matrix_set_identity(&shape->ctm);
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     nr_matrix_set_identity(&shape->cached_fctm);
125     nr_matrix_set_identity(&shape->cached_sctm);
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) sp_curve_unref(shape->curve);
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 == 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_MATRIX_DF_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_matrix_test_transform_equal(&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         sp_curve_is_empty(shape->curve) ||
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                                                               NR::Matrix(&gc->transform), NR::Matrix(&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                                                                 NR::Matrix(&gc->transform), NR::Matrix(&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_MATRIX_DF_EXPANSION(&gc->transform);
525     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == 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_MATRIX_DF_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 = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
780                                           shape->_stroke.opacity *
781                                           SP_SCALE24_TO_FLOAT(style->opacity.value));
782     } else {
783         rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
784                                           shape->_stroke.opacity);
785     }
787     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
788     // by setting color channels in the "wrong" order
789     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));
791     float style_width = MAX(0.125, shape->_stroke.width * scale);
792     cairo_set_line_width(ct, style_width);
794     switch (shape->_stroke.cap) {
795         case NRArenaShape::BUTT_CAP:
796             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
797             break;
798         case NRArenaShape::ROUND_CAP:
799             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
800             break;
801         case NRArenaShape::SQUARE_CAP:
802             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
803             break;
804     }
805     switch (shape->_stroke.join) {
806         case NRArenaShape::MITRE_JOIN:
807             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
808             break;
809         case NRArenaShape::ROUND_JOIN:
810             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
811             break;
812         case NRArenaShape::BEVEL_JOIN:
813             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
814             break;
815     }
817     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
819     if (style->stroke_dash.n_dash) {
820         NRVpathDash dash;
821         dash.offset = style->stroke_dash.offset * scale;
822         dash.n_dash = style->stroke_dash.n_dash;
823         dash.dash = g_new(double, dash.n_dash);
824         for (int i = 0; i < dash.n_dash; i++) {
825             dash.dash[i] = style->stroke_dash.dash[i] * scale;
826         }
827         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
828         g_free(dash.dash);
829     }
831     cairo_set_tolerance(ct, 0.1);
832     cairo_new_path(ct);
834     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
836     cairo_stroke(ct);
838     cairo_surface_t *cst = cairo_get_target(ct);
839     cairo_destroy (ct);
840     cairo_surface_finish (cst);
841     cairo_surface_destroy (cst);
843     pb->empty = FALSE;
847 /**
848  * Renders the item.  Markers are just composed into the parent buffer.
849  */
850 static unsigned int
851 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
853     NRArenaShape *shape = NR_ARENA_SHAPE(item);
855     if (!shape->curve) return item->state;
856     if (!shape->style) return item->state;
858     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
860     if (outline) { // cairo outline rendering
862         pb->empty = FALSE;
863         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
864         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
866     } else {
868     if ( shape->delayed_shp ) {
869         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
870             NRGC   tempGC(NULL);
871             tempGC.transform=shape->ctm;
872             shape->delayed_shp = false;
873             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
874             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
875 /*      NRRect bbox;
876         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
877         nr_arena_shape_add_bboxes(shape,bbox);
878         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
879         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
880         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
881         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
882         shape->approx_bbox=item->bbox;*/
883         } else {
884             return item->state;
885         }
886     }
888     SPStyle const *style = shape->style;
889     if (shape->fill_shp) {
890         NRPixBlock m;
891         guint32 rgba;
893         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
895         // if memory allocation failed, abort render
896         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
897             nr_pixblock_release (&m);
898             return (item->state);
899         }
901         m.visible_area = pb->visible_area; 
902         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
903         m.empty = FALSE;
905         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
906             // do not render fill in any way
907         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
908             if ( item->render_opacity ) {
909                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
910                                                   shape->_fill.opacity *
911                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
912             } else {
913                 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
914                                                   shape->_fill.opacity);
915             }
916             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
917             pb->empty = FALSE;
918         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
919             if (shape->fill_painter) {
920                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
921             }
922         }
924         nr_pixblock_release(&m);
925     }
927     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
929         // cairo_arena_shape_render_stroke(item, area, pb);
931         guint32 rgba;
932         NRPixBlock m;
934         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
936         // if memory allocation failed, abort render
937         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
938             nr_pixblock_release (&m);
939             return (item->state);
940         }
942         m.visible_area = pb->visible_area; 
943         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
944         m.empty = FALSE;
946             if ( item->render_opacity ) {
947                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
948                                                   shape->_stroke.opacity *
949                                                   SP_SCALE24_TO_FLOAT(style->opacity.value));
950             } else {
951                 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
952                                                   shape->_stroke.opacity);
953             }
954             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
955             pb->empty = FALSE;
957         nr_pixblock_release(&m);
959     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
961         NRPixBlock m;
963         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
965         // if memory allocation failed, abort render
966         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
967             nr_pixblock_release (&m);
968             return (item->state);
969         }
971         m.visible_area = pb->visible_area; 
972         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
973         m.empty = FALSE;
975         if (shape->stroke_painter) {
976             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
977         }
979         nr_pixblock_release(&m);
980     }
982     } // non-cairo non-outline branch
984     /* Render markers into parent buffer */
985     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
986         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
987         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
988     }
990     return item->state;
994 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
995 // reenable this when the bug is fixed and remove the rest of this function
996 static guint
997 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
999     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1000     if (!shape->curve) return item->state;
1002         cairo_t *ct = nr_create_cairo_context (area, pb);
1004         if (!ct) 
1005             return item->state;
1007         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1009         cairo_new_path(ct);
1011         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1013         cairo_fill(ct);
1015         cairo_surface_t *cst = cairo_get_target(ct);
1016         cairo_destroy (ct);
1017         cairo_surface_finish (cst);
1018         cairo_surface_destroy (cst);
1020         pb->empty = FALSE;
1022         return item->state;
1026 static guint
1027 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1029     //return cairo_arena_shape_clip(item, area, pb);
1031     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1032     if (!shape->curve) return item->state;
1034     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1035         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1036             NRGC   tempGC(NULL);
1037             tempGC.transform=shape->ctm;
1038             shape->delayed_shp = false;
1039             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1040         } else {
1041             return item->state;
1042         }
1043     }
1045     if ( shape->fill_shp ) {
1046         NRPixBlock m;
1048         /* fixme: We can OR in one step (Lauris) */
1049         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1051         // if memory allocation failed, abort 
1052         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1053             nr_pixblock_release (&m);
1054             return (item->state);
1055         }
1057         m.visible_area = pb->visible_area; 
1058         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1060         for (int y = area->y0; y < area->y1; y++) {
1061             unsigned char *s, *d;
1062             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1063             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1064             for (int x = area->x0; x < area->x1; x++) {
1065                 *d = NR_COMPOSEA_111(*s, *d);
1066                 d ++;
1067                 s ++;
1068             }
1069         }
1070         nr_pixblock_release(&m);
1071         pb->empty = FALSE;
1072     }
1074     return item->state;
1077 static NRArenaItem *
1078 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1080     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1082     if (shape->repick_after > 0)
1083         shape->repick_after--;
1085     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1086         return shape->last_pick;
1088     if (!shape->curve) return NULL;
1089     if (!shape->style) return NULL;
1091     GTimeVal tstart, tfinish;
1092     g_get_current_time (&tstart);
1094     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1096     float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1097     double width;
1098     if (outline) {
1099         width = 0.5;
1100     } else {
1101         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1102     }
1104     NRBPath bp;
1105     bp.path = SP_CURVE_BPATH(shape->curve);
1106     double dist = NR_HUGE;
1107     int wind = 0;
1108     bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && !outline);
1110     if (item->arena->canvasarena) {
1111         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1112         viewbox.growBy (width);
1113         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1114     } else {
1115         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1116     }
1118     g_get_current_time (&tfinish);
1119     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1120     //g_print ("pick time %lu\n", this_pick);
1122     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1123         shape->repick_after = this_pick / 5000;
1124     }
1126     // pick fill
1127     if (needfill) {
1128         if (!shape->style->fill_rule.computed) {
1129             if (wind != 0) {
1130                 shape->last_pick = item;
1131                 return item;
1132             }
1133         } else {
1134             if (wind & 0x1) {
1135                 shape->last_pick = item;
1136                 return item;
1137             }
1138         }
1139     }
1141     // pick stroke
1142     if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1143         // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1144         if ((dist - width) < delta) {
1145                 shape->last_pick = item;
1146                 return item;
1147         }
1148     }
1150     // if not picked on the shape itself, try its markers
1151     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1152         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1153         if (ret) {
1154             shape->last_pick = item;
1155             return item;
1156         }
1157     }
1159     shape->last_pick = NULL;
1160     return NULL;
1163 /**
1164  *
1165  *  Requests a render of the shape, then if the shape is already a curve it
1166  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1167  *  curve and adds it to the shape.  Finally, it requests an update of the
1168  *  arena for the shape.
1169  */
1170 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1172     g_return_if_fail(shape != NULL);
1173     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1175     if ( justTrans == false ) {
1176         // dirty cached versions
1177         if ( shape->cached_fill ) {
1178             delete shape->cached_fill;
1179             shape->cached_fill=NULL;
1180         }
1181         if ( shape->cached_stroke ) {
1182             delete shape->cached_stroke;
1183             shape->cached_stroke=NULL;
1184         }
1185     }
1187     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1189     if (shape->curve) {
1190         sp_curve_unref(shape->curve);
1191         shape->curve = NULL;
1192     }
1194     if (curve) {
1195         shape->curve = curve;
1196         sp_curve_ref(curve);
1197     }
1199     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1202 void NRArenaShape::setFill(SPPaintServer *server) {
1203     _fill.paint.set(server);
1204     _invalidateCachedFill();
1207 void NRArenaShape::setFill(SPColor const &color) {
1208     _fill.paint.set(color);
1209     _invalidateCachedFill();
1212 void NRArenaShape::setFillOpacity(double opacity) {
1213     _fill.opacity = opacity;
1214     _invalidateCachedFill();
1217 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1218     _fill.rule = rule;
1219     _invalidateCachedFill();
1222 void NRArenaShape::setStroke(SPPaintServer *server) {
1223     _stroke.paint.set(server);
1224     _invalidateCachedStroke();
1227 void NRArenaShape::setStroke(SPColor const &color) {
1228     _stroke.paint.set(color);
1229     _invalidateCachedStroke();
1232 void NRArenaShape::setStrokeOpacity(double opacity) {
1233     _stroke.opacity = opacity;
1234     _invalidateCachedStroke();
1237 void NRArenaShape::setStrokeWidth(double width) {
1238     _stroke.width = width;
1239     _invalidateCachedStroke();
1242 void NRArenaShape::setMitreLimit(double limit) {
1243     _stroke.mitre_limit = limit;
1244     _invalidateCachedStroke();
1247 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1248     _stroke.cap = cap;
1249     _invalidateCachedStroke();
1252 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1253     _stroke.join = join;
1254     _invalidateCachedStroke();
1257 /** nr_arena_shape_set_style
1258  *
1259  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1260  */
1261 void
1262 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1264     g_return_if_fail(shape != NULL);
1265     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1267     if (style) sp_style_ref(style);
1268     if (shape->style) sp_style_unref(shape->style);
1269     shape->style = style;
1271     switch (style->fill.type) {
1272         case SP_PAINT_TYPE_NONE: {
1273             shape->setFill(NULL);
1274             break;
1275         }
1276         case SP_PAINT_TYPE_COLOR: {
1277             shape->setFill(style->fill.value.color);
1278             break;
1279         }
1280         case SP_PAINT_TYPE_PAINTSERVER: {
1281             shape->setFill(style->fill.value.paint.server);
1282             break;
1283         }
1284         default: {
1285             g_assert_not_reached();
1286         }
1287     }
1288     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1289     switch (style->fill_rule.computed) {
1290         case SP_WIND_RULE_EVENODD: {
1291             shape->setFillRule(NRArenaShape::EVEN_ODD);
1292             break;
1293         }
1294         case SP_WIND_RULE_NONZERO: {
1295             shape->setFillRule(NRArenaShape::NONZERO);
1296             break;
1297         }
1298         default: {
1299             g_assert_not_reached();
1300         }
1301     }
1303     switch (style->stroke.type) {
1304         case SP_PAINT_TYPE_NONE: {
1305             shape->setStroke(NULL);
1306             break;
1307         }
1308         case SP_PAINT_TYPE_COLOR: {
1309             shape->setStroke(style->stroke.value.color);
1310             break;
1311         }
1312         case SP_PAINT_TYPE_PAINTSERVER: {
1313             shape->setStroke(style->stroke.value.paint.server);
1314             break;
1315         }
1316         default: {
1317             g_assert_not_reached();
1318         }
1319     }
1320     shape->setStrokeWidth(style->stroke_width.computed);
1321     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1322     switch (style->stroke_linecap.computed) {
1323         case SP_STROKE_LINECAP_ROUND: {
1324             shape->setLineCap(NRArenaShape::ROUND_CAP);
1325             break;
1326         }
1327         case SP_STROKE_LINECAP_SQUARE: {
1328             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1329             break;
1330         }
1331         case SP_STROKE_LINECAP_BUTT: {
1332             shape->setLineCap(NRArenaShape::BUTT_CAP);
1333             break;
1334         }
1335         default: {
1336             g_assert_not_reached();
1337         }
1338     }
1339     switch (style->stroke_linejoin.computed) {
1340         case SP_STROKE_LINEJOIN_ROUND: {
1341             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1342             break;
1343         }
1344         case SP_STROKE_LINEJOIN_BEVEL: {
1345             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1346             break;
1347         }
1348         case SP_STROKE_LINEJOIN_MITER: {
1349             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1350             break;
1351         }
1352         default: {
1353             g_assert_not_reached();
1354         }
1355     }
1356     shape->setMitreLimit(style->stroke_miterlimit.value);
1358     //if shape has a filter
1359     if (style->filter.set && style->filter.filter) 
1360     {
1361         shape->filter = new NR::Filter();
1362         shape->filter->set_x(style->filter.filter->x);
1363         shape->filter->set_y(style->filter.filter->y);
1364         shape->filter->set_width(style->filter.filter->width);
1365         shape->filter->set_height(style->filter.filter->height);
1366         
1367         //go through all SP filter primitives
1368         for(int i=0; i<style->filter.filter->_primitive_count; i++)
1369         {
1370             SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1371             //if primitive is gaussianblur
1372             if(SP_IS_GAUSSIANBLUR(primitive)) {
1373                 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1374                 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1375                 float num = spblur->stdDeviation.getNumber();
1376                 if( num>=0.0 )
1377                 {
1378                     float optnum = spblur->stdDeviation.getOptNumber();
1379                     if( optnum>=0.0 )
1380                         gaussian->set_deviation((double) num, (double) optnum);
1381                     else
1382                         gaussian->set_deviation((double) num);
1383                 }
1384             }
1385         }
1386     }
1387     else
1388     {
1389         //no filter set for this shape
1390         shape->filter = NULL;
1391     }
1393     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1396 void
1397 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1399     g_return_if_fail(shape != NULL);
1400     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1401     g_return_if_fail(pbox != NULL);
1403     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1404         shape->paintbox = *pbox;
1405     } else {
1406         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1407         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1408         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1409     }
1411     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1414 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1416     paintbox.x0 = pbox.min()[NR::X];
1417     paintbox.y0 = pbox.min()[NR::Y];
1418     paintbox.x1 = pbox.max()[NR::X];
1419     paintbox.y1 = pbox.max()[NR::Y];
1421     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1424 static void
1425 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1427     if ( st >= en ) return;
1428     if ( vst < 0 ) vst=0;
1429     if ( vst > 1 ) vst=1;
1430     if ( ven < 0 ) ven=0;
1431     if ( ven > 1 ) ven=1;
1432     float   sv=vst;
1433     float   dv=ven-vst;
1434     int     len=en-st;
1435     unsigned char*   d=(unsigned char*)dest.buffer;
1436     d+=(st-dest.startPix);
1437     if ( fabs(dv) < 0.001 ) {
1438         if ( vst > 0.999 ) {
1439             /* Simple copy */
1440             while (len > 0) {
1441                 d[0] = 255;
1442                 d += 1;
1443                 len -= 1;
1444             }
1445         } else {
1446             sv*=256;
1447             unsigned int c0_24=(int)sv;
1448             c0_24&=0xFF;
1449             while (len > 0) {
1450                 /* Draw */
1451                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1452                 d += 1;
1453                 len -= 1;
1454             }
1455         }
1456     } else {
1457         if ( en <= st+1 ) {
1458             sv=0.5*(vst+ven);
1459             sv*=256;
1460             unsigned int c0_24=(int)sv;
1461             c0_24&=0xFF;
1462             /* Draw */
1463             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1464         } else {
1465             dv/=len;
1466             sv+=0.5*dv; // correction trapezoidale
1467             sv*=16777216;
1468             dv*=16777216;
1469             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1470             int s0_24 = static_cast<int>(dv);
1471             while (len > 0) {
1472                 unsigned int ca;
1473                 /* Draw */
1474                 ca = c0_24 >> 16;
1475                 if ( ca > 255 ) ca=255;
1476                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1477                 d += 1;
1478                 c0_24 += s0_24;
1479                 c0_24 = CLAMP(c0_24, 0, 16777216);
1480                 len -= 1;
1481             }
1482         }
1483     }
1486 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1488     theS->CalcBBox();
1489     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1490     int    il,ir,it,ib;
1491     il=(int)floor(l);
1492     ir=(int)ceil(r);
1493     it=(int)floor(t);
1494     ib=(int)ceil(b);
1496     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1497     if ( il < m.area.x0 ) il=m.area.x0;
1498     if ( it < m.area.y0 ) it=m.area.y0;
1499     if ( ir > m.area.x1 ) ir=m.area.x1;
1500     if ( ib > m.area.y1 ) ib=m.area.y1;
1502     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1503     int    curPt;
1504     float  curY;
1505     theS->BeginQuickRaster(curY, curPt);
1507     FloatLigne *theI = new FloatLigne();
1508     IntLigne *theIL = new IntLigne();
1510     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1512     char *mdata = (char*)m.data.px;
1513     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1514     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1515     for (int y = it; y < ib; y++) {
1516         theI->Reset();
1517         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1518         theI->Flatten();
1519         theIL->Copy(theI);
1521         raster_info  dest;
1522         dest.startPix=il;
1523         dest.endPix=ir;
1524         dest.sth=il;
1525         dest.stv=y;
1526         dest.buffer=ligStart;
1527         theIL->Raster(dest, NULL, shape_run_A8_OR);
1528         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1529     }
1530     theS->EndQuickRaster();
1531     delete theI;
1532     delete theIL;
1536 /*
1537   Local Variables:
1538   mode:c++
1539   c-file-style:"stroustrup"
1540   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1541   indent-tabs-mode:nil
1542   fill-column:99
1543   End:
1544 */
1545 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :