Code

Refactoring SPColor to C++ and removing legacy CMYK implementation
[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 "inkscape-cairo.h"
33 #include "sp-filter.h"
34 #include "sp-filter-reference.h"
35 #include "display/nr-filter.h"
37 #include <cairo.h>
39 //int  showRuns=0;
40 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
42 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
43 static void nr_arena_shape_init(NRArenaShape *shape);
44 static void nr_arena_shape_finalize(NRObject *object);
46 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
47 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
48 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
49 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
51 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
52 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
53 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
54 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
56 static NRArenaItemClass *shape_parent_class;
58 NRType
59 nr_arena_shape_get_type(void)
60 {
61     static NRType type = 0;
62     if (!type) {
63         type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
64                                        "NRArenaShape",
65                                        sizeof(NRArenaShapeClass),
66                                        sizeof(NRArenaShape),
67                                        (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
68                                        (void (*)(NRObject *)) nr_arena_shape_init);
69     }
70     return type;
71 }
73 static void
74 nr_arena_shape_class_init(NRArenaShapeClass *klass)
75 {
76     NRObjectClass *object_class;
77     NRArenaItemClass *item_class;
79     object_class = (NRObjectClass *) klass;
80     item_class = (NRArenaItemClass *) klass;
82     shape_parent_class = (NRArenaItemClass *)  ((NRObjectClass *) klass)->parent;
84     object_class->finalize = nr_arena_shape_finalize;
85     object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
87     item_class->children = nr_arena_shape_children;
88     item_class->add_child = nr_arena_shape_add_child;
89     item_class->set_child_position = nr_arena_shape_set_child_position;
90     item_class->remove_child = nr_arena_shape_remove_child;
91     item_class->update = nr_arena_shape_update;
92     item_class->render = nr_arena_shape_render;
93     item_class->clip = nr_arena_shape_clip;
94     item_class->pick = nr_arena_shape_pick;
95 }
97 /**
98  * Initializes the arena shape, setting all parameters to null, 0, false,
99  * or other defaults
100  */
101 static void
102 nr_arena_shape_init(NRArenaShape *shape)
104     shape->curve = NULL;
105     shape->style = NULL;
106     shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
107     shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
109     nr_matrix_set_identity(&shape->ctm);
110     shape->fill_painter = NULL;
111     shape->stroke_painter = NULL;
112     shape->cached_fill = NULL;
113     shape->cached_stroke = NULL;
114     shape->cached_fpartialy = false;
115     shape->cached_spartialy = false;
116     shape->fill_shp = NULL;
117     shape->stroke_shp = NULL;
119     shape->delayed_shp = false;
121     shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
122     shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
123     nr_matrix_set_identity(&shape->cached_fctm);
124     nr_matrix_set_identity(&shape->cached_sctm);
126     shape->markers = NULL;
128     shape->last_pick = NULL;
129     shape->repick_after = 0;
132 static void
133 nr_arena_shape_finalize(NRObject *object)
135     NRArenaShape *shape = (NRArenaShape *) object;
137     if (shape->fill_shp) delete shape->fill_shp;
138     if (shape->stroke_shp) delete shape->stroke_shp;
139     if (shape->cached_fill) delete shape->cached_fill;
140     if (shape->cached_stroke) delete shape->cached_stroke;
141     if (shape->fill_painter) sp_painter_free(shape->fill_painter);
142     if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
144     if (shape->style) sp_style_unref(shape->style);
145     if (shape->curve) sp_curve_unref(shape->curve);
147     ((NRObjectClass *) shape_parent_class)->finalize(object);
150 /**
151  * Retrieves the markers from the item
152  */
153 static NRArenaItem *
154 nr_arena_shape_children(NRArenaItem *item)
156     NRArenaShape *shape = (NRArenaShape *) item;
158     return shape->markers;
161 /**
162  * Attaches child to item, and if ref is not NULL, sets it and ref->next as
163  * the prev and next items.  If ref is NULL, then it sets the item's markers
164  * as the next items.
165  */
166 static void
167 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
169     NRArenaShape *shape = (NRArenaShape *) item;
171     if (!ref) {
172         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
173     } else {
174         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
175     }
177     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
180 /**
181  * Removes child from the shape.  If there are no prev items in 
182  * the child, it sets items' markers to the next item in the child.
183  */
184 static void
185 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
187     NRArenaShape *shape = (NRArenaShape *) item;
189     if (child->prev) {
190         nr_arena_item_detach(item, child);
191     } else {
192         shape->markers = nr_arena_item_detach(item, child);
193     }
195     nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
198 /**
199  * Detaches child from item, and if there are no previous items in child, it 
200  * sets item's markers to the child.  It then attaches the child back onto the item.
201  * If ref is null, it sets the markers to be the next item, otherwise it uses
202  * the next/prev items in ref.
203  */
204 static void
205 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
207     NRArenaShape *shape = (NRArenaShape *) item;
209     if (child->prev) {
210         nr_arena_item_detach(item, child);
211     } else {
212         shape->markers = nr_arena_item_detach(item, child);
213     }
215     if (!ref) {
216         shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
217     } else {
218         ref->next = nr_arena_item_attach(item, child, ref, ref->next);
219     }
221     nr_arena_item_request_render(child);
224 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
225 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
226 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
228 /**
229  * Updates the arena shape 'item' and all of its children, including the markers.
230  */
231 static guint
232 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
234     NRRect bbox;
236     NRArenaShape *shape = NR_ARENA_SHAPE(item);
238     unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
240     unsigned int newstate;
241     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
242         newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
243         beststate = beststate & newstate;
244     }
246     if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
247         /* We do not have to create rendering structures */
248         shape->ctm = gc->transform;
249         if (state & NR_ARENA_ITEM_STATE_BBOX) {
250             if (shape->curve) {
251                 NRBPath bp;
252                 /* fixme: */
253                 bbox.x0 = bbox.y0 = NR_HUGE;
254                 bbox.x1 = bbox.y1 = -NR_HUGE;
255                 bp.path = SP_CURVE_BPATH(shape->curve);
256                 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
257                 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
258                 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
259                 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
260                 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
261             }
262             if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
263                 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
264                     nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
265                 }
266             }
267         }
268         return (state | item->state);
269     }
271     shape->delayed_shp=true;
272     shape->ctm = gc->transform;
273     bbox.x0 = bbox.y0 = NR_HUGE;
274     bbox.x1 = bbox.y1 = -NR_HUGE;
276     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
278     if (shape->curve) {
279         NRBPath bp;
280         /* fixme: */
281         bbox.x0 = bbox.y0 = NR_HUGE;
282         bbox.x1 = bbox.y1 = -NR_HUGE;
283         bp.path = SP_CURVE_BPATH(shape->curve);
284         nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
285         if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
286             float width, scale;
287             scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
288             width = MAX(0.125, shape->_stroke.width * scale);
289             if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
290                 bbox.x0-=width;
291                 bbox.x1+=width;
292                 bbox.y0-=width;
293                 bbox.y1+=width;
294             }
295             // those pesky miters, now
296             float miterMax=width*shape->_stroke.mitre_limit;
297             if ( miterMax > 0.01 ) {
298                 // grunt mode. we should compute the various miters instead (one for each point on the curve)
299                 bbox.x0-=miterMax;
300                 bbox.x1+=miterMax;
301                 bbox.y0-=miterMax;
302                 bbox.y1+=miterMax;
303             }
304         }
305     } else {
306     }
307     shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
308     shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
309     shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
310     shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
311     if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
313     /* Release state data */
314     if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
315         /* Concept test */
316         if (shape->fill_shp) {
317             delete shape->fill_shp;
318             shape->fill_shp = NULL;
319         }
320     }
321     if (shape->stroke_shp) {
322         delete shape->stroke_shp;
323         shape->stroke_shp = NULL;
324     }
325     if (shape->fill_painter) {
326         sp_painter_free(shape->fill_painter);
327         shape->fill_painter = NULL;
328     }
329     if (shape->stroke_painter) {
330         sp_painter_free(shape->stroke_painter);
331         shape->stroke_painter = NULL;
332     }
334     if (!shape->curve || 
335         !shape->style ||
336         sp_curve_is_empty(shape->curve) ||
337         (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
338          ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
339     {
340         item->bbox = shape->approx_bbox;
341         return NR_ARENA_ITEM_STATE_ALL;
342     }
344     /* Build state data */
345     if ( shape->delayed_shp ) {
346         item->bbox=shape->approx_bbox;
347     } else {
348         nr_arena_shape_update_stroke(shape, gc, area);
349         nr_arena_shape_update_fill(shape, gc, area);
351         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
352         nr_arena_shape_add_bboxes(shape,bbox);
354         shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
355         shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
356         shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
357         shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
358     }
360     if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
362     item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
363     item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
364     item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
365     item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
366     nr_arena_request_render_rect(item->arena, &item->bbox);
368     item->render_opacity = TRUE;
369     if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
370         if (gc && gc->parent) {
371             shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
372                                                               NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
373                                                               &shape->paintbox);
374         }
375         item->render_opacity = FALSE;
376     }
377     if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
378         if (gc && gc->parent) {
379             shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
380                                                                 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
381                                                                 &shape->paintbox);
382         }
383         item->render_opacity = FALSE;
384     }
385     if (  (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && 
386            shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
387           || (shape->markers)
388         )
389     {
390         // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
391         item->render_opacity = FALSE;
392     }
394     if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
395         for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
396             nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
397         }
398     }
400     return NR_ARENA_ITEM_STATE_ALL;
403 int matrix_is_isometry(NR::Matrix p) {
404     NR::Matrix   tp;
405     // transposition
406     tp[0]=p[0];
407     tp[1]=p[2];
408     tp[2]=p[1];
409     tp[3]=p[3];
410     for (int i = 4; i < 6; i++) // shut valgrind up :)
411         tp[i] = p[i] = 0;
412     NR::Matrix   isom = tp*p; // A^T * A = adjunct?
413     // Is the adjunct nearly an identity function?
414     if (isom.is_translation(0.01)) {
415         // the transformation is an isometry -> no need to recompute
416         // the uncrossed polygon
417         if ( p.det() < 0 )
418             return -1;
419         else
420             return 1;
421     }
422     return 0;
425 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
426     return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
429 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
430 void
431 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
433     if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
434         ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
435         if (TRUE || !shape->fill_shp) {
436             NR::Matrix  cached_to_new = NR::identity();
437             int isometry = 0;
438             if ( shape->cached_fill ) {
439                 if (shape->cached_fctm == gc->transform) {
440                     isometry = 2; // identity
441                 } else {
442                     cached_to_new = shape->cached_fctm.inverse() * gc->transform;
443                     isometry = matrix_is_isometry(cached_to_new);
444                 }
445                 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
446                     isometry = 0;
447             }
448             if ( isometry == 0 ) {
449                 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
450                 shape->cached_fill->Reset();
452                 Path*  thePath=new Path;
453                 Shape* theShape=new Shape;
454                 {
455                     NR::Matrix   tempMat(gc->transform);
456                     thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
457                 }
459                 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
460                     thePath->Convert(1.0);
461                     shape->cached_fpartialy = false;
462                 } else {
463                     thePath->Convert(area, 1.0);
464                     shape->cached_fpartialy = true;
465                 }
467                 thePath->Fill(theShape, 0);
469                 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
470                     shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
471                     // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
472                     //shape->cached_fill->Copy(theShape);
473                 } else {
474                     shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
475                 }
476                 shape->cached_fctm=gc->transform;
477                 shape->cached_farea = *area;
478                 delete theShape;
479                 delete thePath;
480                 if ( shape->fill_shp == NULL )
481                     shape->fill_shp = new Shape;
483                 shape->fill_shp->Copy(shape->cached_fill);
485             } else if ( 2 == isometry ) {
486                 if ( shape->fill_shp == NULL ) {
487                     shape->fill_shp = new Shape;
488                     shape->fill_shp->Copy(shape->cached_fill);
489                 }
490             } else {
492                 if ( shape->fill_shp == NULL )
493                     shape->fill_shp = new Shape;
495                 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
496                                        shape->cached_fill->numberOfEdges());
497                 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
498                     shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
499                 if ( isometry == 1 ) {
500                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
501                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
502                                                  shape->cached_fill->getEdge(i).en);
503                 } else if ( isometry == -1 ) { // need to flip poly.
504                     for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
505                         shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
506                                                  shape->cached_fill->getEdge(i).st);
507                 }
508                 shape->fill_shp->ForceToPolygon();
509                 shape->fill_shp->needPointsSorting();
510                 shape->fill_shp->needEdgesSorting();
511             }
512             shape->delayed_shp |= shape->cached_fpartialy;
513         }
514     }
517 void
518 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
520     SPStyle* style = shape->style;
522     float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
524     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
526     if (outline) {
527         // cairo does not need the livarot path for rendering
528         return; 
529     }
531     // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes 
532     // (since it will only be used for picking, not for rendering)
534     if (outline ||
535         ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
536          ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
538         float style_width = MAX(0.125, shape->_stroke.width * scale);
539         float width;
540         if (outline) {
541             width = 0.5; // 1 pixel wide, independent of zoom
542         } else {
543             width = style_width;
544         }
546         NR::Matrix  cached_to_new = NR::identity();
548         int isometry = 0;
549         if ( shape->cached_stroke ) {
550             if (shape->cached_sctm == gc->transform) {
551                 isometry = 2; // identity
552             } else {
553                 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
554                 isometry = matrix_is_isometry(cached_to_new);
555             }
556             if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
557                 isometry = 0;
558             if (0 != isometry && width != shape->cached_width) { 
559                 // if this happens without setting style, we have just switched to outline or back
560                 isometry = 0; 
561             } 
562         }
564         if ( isometry == 0 ) {
565             if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
566             shape->cached_stroke->Reset();
567             Path*  thePath = new Path;
568             Shape* theShape = new Shape;
569             {
570                 NR::Matrix   tempMat(gc->transform);
571                 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
572             }
574             // add some padding to the rendering area, so clipped path does not go into a render area
575             NRRectL padded_area = *area;
576             padded_area.x0 -= (NR::ICoord)width;
577             padded_area.x1 += (NR::ICoord)width;
578             padded_area.y0 -= (NR::ICoord)width;
579             padded_area.y1 += (NR::ICoord)width;
580             if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
581                 thePath->Convert((outline) ? 4.0 : 1.0);
582                 shape->cached_spartialy = false;
583             }
584             else {
585                 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
586                 shape->cached_spartialy = true;
587             }
589             if (style->stroke_dash.n_dash && !outline) {
590                 thePath->DashPolylineFromStyle(style, scale, 1.0);
591             }
593             ButtType butt=butt_straight;
594             switch (shape->_stroke.cap) {
595                 case NRArenaShape::BUTT_CAP:
596                     butt = butt_straight;
597                     break;
598                 case NRArenaShape::ROUND_CAP:
599                     butt = butt_round;
600                     break;
601                 case NRArenaShape::SQUARE_CAP:
602                     butt = butt_square;
603                     break;
604             }
605             JoinType join=join_straight;
606             switch (shape->_stroke.join) {
607                 case NRArenaShape::MITRE_JOIN:
608                     join = join_pointy;
609                     break;
610                 case NRArenaShape::ROUND_JOIN:
611                     join = join_round;
612                     break;
613                 case NRArenaShape::BEVEL_JOIN:
614                     join = join_straight;
615                     break;
616             }
618             if (outline) {
619                 butt = butt_straight;
620                 join = join_straight;
621             }
623             thePath->Stroke(theShape, false, 0.5*width, join, butt,
624                             0.5*width*shape->_stroke.mitre_limit);
627             if (outline) {
628                 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
629                 shape->cached_stroke->Copy(theShape);
630             } else {
631                 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
632             }
634             shape->cached_width = width;
636             shape->cached_sctm=gc->transform;
637             shape->cached_sarea = *area;
638             delete thePath;
639             delete theShape;
640             if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
642             shape->stroke_shp->Copy(shape->cached_stroke);
644         } else if ( 2 == isometry ) {
645             if ( shape->stroke_shp == NULL ) {
646                 shape->stroke_shp=new Shape;
647                 shape->stroke_shp->Copy(shape->cached_stroke);
648             }
649         } else {
650             if ( shape->stroke_shp == NULL )
651                 shape->stroke_shp=new Shape;
652             shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
653             for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
654                 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
655             if ( isometry == 1 ) {
656                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
657                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
658                                                shape->cached_stroke->getEdge(i).en);
659             } else if ( isometry == -1 ) {
660                 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
661                     shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
662                                                shape->cached_stroke->getEdge(i).st);
663             }
664             shape->stroke_shp->ForceToPolygon();
665             shape->stroke_shp->needPointsSorting();
666             shape->stroke_shp->needEdgesSorting();
667         }
668         shape->delayed_shp |= shape->cached_spartialy;
669     }
673 void
674 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
676     if ( shape->stroke_shp ) {
677         shape->stroke_shp->CalcBBox();
678         shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
679         shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
680         shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
681         shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
682         if ( bbox.x0 >= bbox.x1 ) {
683             if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
684                 bbox.x0=shape->stroke_shp->leftX;
685                 bbox.x1=shape->stroke_shp->rightX;
686             }
687         } else {
688             if ( shape->stroke_shp->leftX < bbox.x0 )
689                 bbox.x0=shape->stroke_shp->leftX;
690             if ( shape->stroke_shp->rightX > bbox.x1 )
691                 bbox.x1=shape->stroke_shp->rightX;
692         }
693         if ( bbox.y0 >= bbox.y1 ) {
694             if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
695                 bbox.y0=shape->stroke_shp->topY;
696                 bbox.y1=shape->stroke_shp->bottomY;
697             }
698         } else {
699             if ( shape->stroke_shp->topY < bbox.y0 )
700                 bbox.y0=shape->stroke_shp->topY;
701             if ( shape->stroke_shp->bottomY > bbox.y1 )
702                 bbox.y1=shape->stroke_shp->bottomY;
703         }
704     }
705     if ( shape->fill_shp ) {
706         shape->fill_shp->CalcBBox();
707         shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
708         shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
709         shape->fill_shp->topY=floor(shape->fill_shp->topY);
710         shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
711         if ( bbox.x0 >= bbox.x1 ) {
712             if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
713                 bbox.x0=shape->fill_shp->leftX;
714                 bbox.x1=shape->fill_shp->rightX;
715             }
716         } else {
717             if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
718             if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
719         }
720         if ( bbox.y0 >= bbox.y1 ) {
721             if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
722                 bbox.y0=shape->fill_shp->topY;
723                 bbox.y1=shape->fill_shp->bottomY;
724             }
725         } else {
726             if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
727             if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
728         }
729     }
732 // cairo outline rendering:
733 static unsigned int
734 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
736     NRArenaShape *shape = NR_ARENA_SHAPE(item);
738     if (!ct) 
739         return item->state;
741     guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
742     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
743     // by setting color channels in the "wrong" order
744     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));
746     cairo_set_line_width(ct, 0.5);
747     cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
748     cairo_new_path(ct);
750     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
752     cairo_stroke(ct);
754     return item->state;
757 // cairo stroke rendering (flat color only so far!):
758 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
759 // (need to switch them to premul before this can be enabled)
760 void
761 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
763     NRArenaShape *shape = NR_ARENA_SHAPE(item);
764     SPStyle const *style = shape->style;
766     float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
768     if (fabs(shape->_stroke.width * scale) < 0.01)
769         return;
771     cairo_t *ct = nr_create_cairo_context (area, pb);
773     if (!ct)
774         return;
776     guint32 rgba;
777     if ( item->render_opacity ) {
778         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
779                                                       SP_SCALE24_TO_FLOAT(style->opacity.value) );
780     } else {
781         rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
782     }
784     // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat 
785     // by setting color channels in the "wrong" order
786     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));
788     float style_width = MAX(0.125, shape->_stroke.width * scale);
789     cairo_set_line_width(ct, style_width);
791     switch (shape->_stroke.cap) {
792         case NRArenaShape::BUTT_CAP:
793             cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
794             break;
795         case NRArenaShape::ROUND_CAP:
796             cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
797             break;
798         case NRArenaShape::SQUARE_CAP:
799             cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
800             break;
801     }
802     switch (shape->_stroke.join) {
803         case NRArenaShape::MITRE_JOIN:
804             cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
805             break;
806         case NRArenaShape::ROUND_JOIN:
807             cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
808             break;
809         case NRArenaShape::BEVEL_JOIN:
810             cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
811             break;
812     }
814     cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
816     if (style->stroke_dash.n_dash) {
817         NRVpathDash dash;
818         dash.offset = style->stroke_dash.offset * scale;
819         dash.n_dash = style->stroke_dash.n_dash;
820         dash.dash = g_new(double, dash.n_dash);
821         for (int i = 0; i < dash.n_dash; i++) {
822             dash.dash[i] = style->stroke_dash.dash[i] * scale;
823         }
824         cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
825         g_free(dash.dash);
826     }
828     cairo_set_tolerance(ct, 0.1);
829     cairo_new_path(ct);
831     feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
833     cairo_stroke(ct);
835     cairo_surface_t *cst = cairo_get_target(ct);
836     cairo_destroy (ct);
837     cairo_surface_finish (cst);
838     cairo_surface_destroy (cst);
840     pb->empty = FALSE;
844 /**
845  * Renders the item.  Markers are just composed into the parent buffer.
846  */
847 static unsigned int
848 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
850     NRArenaShape *shape = NR_ARENA_SHAPE(item);
852     if (!shape->curve) return item->state;
853     if (!shape->style) return item->state;
855     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
857     if (outline) { // cairo outline rendering
859         pb->empty = FALSE;
860         unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
861         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
863     } else {
865     if ( shape->delayed_shp ) {
866         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
867             NRGC   tempGC(NULL);
868             tempGC.transform=shape->ctm;
869             shape->delayed_shp = false;
870             nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
871             nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
872 /*      NRRect bbox;
873         bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
874         nr_arena_shape_add_bboxes(shape,bbox);
875         item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
876         item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
877         item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
878         item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
879         shape->approx_bbox=item->bbox;*/
880         } else {
881             return item->state;
882         }
883     }
885     SPStyle const *style = shape->style;
886     if (shape->fill_shp) {
887         NRPixBlock m;
888         guint32 rgba;
890         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
892         // if memory allocation failed, abort render
893         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
894             nr_pixblock_release (&m);
895             return (item->state);
896         }
898         m.visible_area = pb->visible_area;
899         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
900         m.empty = FALSE;
902         if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
903             // do not render fill in any way
904         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
905             if ( item->render_opacity ) {
906                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
907                                                             SP_SCALE24_TO_FLOAT(style->opacity.value) );
908             } else {
909                 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
910             }
911             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
912             pb->empty = FALSE;
913         } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
914             if (shape->fill_painter) {
915                 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
916             }
917         }
919         nr_pixblock_release(&m);
920     }
922     if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
924         // cairo_arena_shape_render_stroke(item, area, pb);
926         guint32 rgba;
927         NRPixBlock m;
929         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
931         // if memory allocation failed, abort render
932         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
933             nr_pixblock_release (&m);
934             return (item->state);
935         }
937         m.visible_area = pb->visible_area;
938         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
939         m.empty = FALSE;
941             if ( item->render_opacity ) {
942                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
943                                                               SP_SCALE24_TO_FLOAT(style->opacity.value) );
944             } else {
945                 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
946             }
947             nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
948             pb->empty = FALSE;
950         nr_pixblock_release(&m);
952     } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
954         NRPixBlock m;
956         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
958         // if memory allocation failed, abort render
959         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
960             nr_pixblock_release (&m);
961             return (item->state);
962         }
964         m.visible_area = pb->visible_area; 
965         nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
966         m.empty = FALSE;
968         if (shape->stroke_painter) {
969             nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
970         }
972         nr_pixblock_release(&m);
973     }
975     } // non-cairo non-outline branch
977     /* Render markers into parent buffer */
978     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
979         unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
980         if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
981     }
983     return item->state;
987 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
988 // reenable this when the bug is fixed and remove the rest of this function
989 // TODO
990 #if defined(DEADCODE) && !defined(DEADCODE)
991 static guint
992 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
994     NRArenaShape *shape = NR_ARENA_SHAPE(item);
995     if (!shape->curve) return item->state;
997         cairo_t *ct = nr_create_cairo_context (area, pb);
999         if (!ct)
1000             return item->state;
1002         cairo_set_source_rgba(ct, 0, 0, 0, 1);
1004         cairo_new_path(ct);
1006         feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1008         cairo_fill(ct);
1010         cairo_surface_t *cst = cairo_get_target(ct);
1011         cairo_destroy (ct);
1012         cairo_surface_finish (cst);
1013         cairo_surface_destroy (cst);
1015         pb->empty = FALSE;
1017         return item->state;
1019 #endif //defined(DEADCODE) && !defined(DEADCODE)
1022 static guint
1023 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1025     //return cairo_arena_shape_clip(item, area, pb);
1027     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1028     if (!shape->curve) return item->state;
1030     if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1031         if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1032             NRGC   tempGC(NULL);
1033             tempGC.transform=shape->ctm;
1034             shape->delayed_shp = false;
1035             nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1036         } else {
1037             return item->state;
1038         }
1039     }
1041     if ( shape->fill_shp ) {
1042         NRPixBlock m;
1044         /* fixme: We can OR in one step (Lauris) */
1045         nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1047         // if memory allocation failed, abort 
1048         if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1049             nr_pixblock_release (&m);
1050             return (item->state);
1051         }
1053         m.visible_area = pb->visible_area; 
1054         nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1056         for (int y = area->y0; y < area->y1; y++) {
1057             unsigned char *s, *d;
1058             s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1059             d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1060             for (int x = area->x0; x < area->x1; x++) {
1061                 *d = NR_COMPOSEA_111(*s, *d);
1062                 d ++;
1063                 s ++;
1064             }
1065         }
1066         nr_pixblock_release(&m);
1067         pb->empty = FALSE;
1068     }
1070     return item->state;
1073 static NRArenaItem *
1074 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1076     NRArenaShape *shape = NR_ARENA_SHAPE(item);
1078     if (shape->repick_after > 0)
1079         shape->repick_after--;
1081     if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1082         return shape->last_pick;
1084     if (!shape->curve) return NULL;
1085     if (!shape->style) return NULL;
1086     if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1087         return NULL;
1089     GTimeVal tstart, tfinish;
1090     g_get_current_time (&tstart);
1092     bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1094     double width;
1095     if (outline) {
1096         width = 0.5;
1097     } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1098         float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1099         width = MAX(0.125, shape->_stroke.width * scale) / 2;
1100     } else {
1101         width = 0;
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 
1109              && shape->_fill.opacity > 1e-3 && !outline);
1111     if (item->arena->canvasarena) {
1112         NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1113         viewbox.growBy (width);
1114         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1115     } else {
1116         nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1117     }
1119     g_get_current_time (&tfinish);
1120     glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1121     //g_print ("pick time %lu\n", this_pick);
1123     if (this_pick > 10000) { // slow picking, remember to skip several new picks
1124         shape->repick_after = this_pick / 5000;
1125     }
1127     // covered by fill?
1128     if (needfill) {
1129         if (!shape->style->fill_rule.computed) {
1130             if (wind != 0) {
1131                 shape->last_pick = item;
1132                 return item;
1133             }
1134         } else {
1135             if (wind & 0x1) {
1136                 shape->last_pick = item;
1137                 return item;
1138             }
1139         }
1140     }
1142     // close to the edge, as defined by strokewidth and delta?
1143     // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1144     if (needfill || width > 0) { // if either fill or stroke visible,
1145         if ((dist - width) < delta) {
1146             shape->last_pick = item;
1147             return item;
1148         }
1149     }
1151     // if not picked on the shape itself, try its markers
1152     for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1153         NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1154         if (ret) {
1155             shape->last_pick = item;
1156             return item;
1157         }
1158     }
1160     shape->last_pick = NULL;
1161     return NULL;
1164 /**
1165  *
1166  *  Requests a render of the shape, then if the shape is already a curve it
1167  *  unrefs the old curve; if the new curve is valid it creates a copy of the
1168  *  curve and adds it to the shape.  Finally, it requests an update of the
1169  *  arena for the shape.
1170  */
1171 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1173     g_return_if_fail(shape != NULL);
1174     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1176     if ( justTrans == false ) {
1177         // dirty cached versions
1178         if ( shape->cached_fill ) {
1179             delete shape->cached_fill;
1180             shape->cached_fill=NULL;
1181         }
1182         if ( shape->cached_stroke ) {
1183             delete shape->cached_stroke;
1184             shape->cached_stroke=NULL;
1185         }
1186     }
1188     nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1190     if (shape->curve) {
1191         sp_curve_unref(shape->curve);
1192         shape->curve = NULL;
1193     }
1195     if (curve) {
1196         shape->curve = curve;
1197         sp_curve_ref(curve);
1198     }
1200     nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1203 void NRArenaShape::setFill(SPPaintServer *server) {
1204     _fill.paint.set(server);
1205     _invalidateCachedFill();
1208 void NRArenaShape::setFill(SPColor const &color) {
1209     _fill.paint.set(color);
1210     _invalidateCachedFill();
1213 void NRArenaShape::setFillOpacity(double opacity) {
1214     _fill.opacity = opacity;
1215     _invalidateCachedFill();
1218 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1219     _fill.rule = rule;
1220     _invalidateCachedFill();
1223 void NRArenaShape::setStroke(SPPaintServer *server) {
1224     _stroke.paint.set(server);
1225     _invalidateCachedStroke();
1228 void NRArenaShape::setStroke(SPColor const &color) {
1229     _stroke.paint.set(color);
1230     _invalidateCachedStroke();
1233 void NRArenaShape::setStrokeOpacity(double opacity) {
1234     _stroke.opacity = opacity;
1235     _invalidateCachedStroke();
1238 void NRArenaShape::setStrokeWidth(double width) {
1239     _stroke.width = width;
1240     _invalidateCachedStroke();
1243 void NRArenaShape::setMitreLimit(double limit) {
1244     _stroke.mitre_limit = limit;
1245     _invalidateCachedStroke();
1248 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1249     _stroke.cap = cap;
1250     _invalidateCachedStroke();
1253 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1254     _stroke.join = join;
1255     _invalidateCachedStroke();
1258 /** nr_arena_shape_set_style
1259  *
1260  * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1261  */
1262 void
1263 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1265     g_return_if_fail(shape != NULL);
1266     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1268     if (style) sp_style_ref(style);
1269     if (shape->style) sp_style_unref(shape->style);
1270     shape->style = style;
1272     if ( style->fill.isPaintserver() ) {
1273         shape->setFill(style->getFillPaintServer());
1274     } else if ( style->fill.isColor() ) {
1275         shape->setFill(style->fill.value.color);
1276     } else if ( style->fill.isNone() ) {
1277         shape->setFill(NULL);
1278     } else {
1279         g_assert_not_reached();
1280     }
1281     shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1282     switch (style->fill_rule.computed) {
1283         case SP_WIND_RULE_EVENODD: {
1284             shape->setFillRule(NRArenaShape::EVEN_ODD);
1285             break;
1286         }
1287         case SP_WIND_RULE_NONZERO: {
1288             shape->setFillRule(NRArenaShape::NONZERO);
1289             break;
1290         }
1291         default: {
1292             g_assert_not_reached();
1293         }
1294     }
1296     if ( style->stroke.isPaintserver() ) {
1297         shape->setStroke(style->getStrokePaintServer());
1298     } else if ( style->stroke.isColor() ) {
1299         shape->setStroke(style->stroke.value.color);
1300     } else if ( style->stroke.isNone() ) {
1301         shape->setStroke(NULL);
1302     } else {
1303         g_assert_not_reached();
1304     }
1305     shape->setStrokeWidth(style->stroke_width.computed);
1306     shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1307     switch (style->stroke_linecap.computed) {
1308         case SP_STROKE_LINECAP_ROUND: {
1309             shape->setLineCap(NRArenaShape::ROUND_CAP);
1310             break;
1311         }
1312         case SP_STROKE_LINECAP_SQUARE: {
1313             shape->setLineCap(NRArenaShape::SQUARE_CAP);
1314             break;
1315         }
1316         case SP_STROKE_LINECAP_BUTT: {
1317             shape->setLineCap(NRArenaShape::BUTT_CAP);
1318             break;
1319         }
1320         default: {
1321             g_assert_not_reached();
1322         }
1323     }
1324     switch (style->stroke_linejoin.computed) {
1325         case SP_STROKE_LINEJOIN_ROUND: {
1326             shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1327             break;
1328         }
1329         case SP_STROKE_LINEJOIN_BEVEL: {
1330             shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1331             break;
1332         }
1333         case SP_STROKE_LINEJOIN_MITER: {
1334             shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1335             break;
1336         }
1337         default: {
1338             g_assert_not_reached();
1339         }
1340     }
1341     shape->setMitreLimit(style->stroke_miterlimit.value);
1343     //if shape has a filter
1344     if (style->filter.set && style->getFilter()) {
1345         if (!shape->filter) {
1346             int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1347             shape->filter = new NR::Filter(primitives);
1348         }
1349         sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1350     } else {
1351         //no filter set for this shape
1352         delete shape->filter;
1353         shape->filter = NULL;
1354     }
1356     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1359 void
1360 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1362     g_return_if_fail(shape != NULL);
1363     g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1364     g_return_if_fail(pbox != NULL);
1366     if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1367         shape->paintbox = *pbox;
1368     } else {
1369         /* fixme: We kill warning, although not sure what to do here (Lauris) */
1370         shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1371         shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1372     }
1374     nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1377 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1379     paintbox.x0 = pbox.min()[NR::X];
1380     paintbox.y0 = pbox.min()[NR::Y];
1381     paintbox.x1 = pbox.max()[NR::X];
1382     paintbox.y1 = pbox.max()[NR::Y];
1384     nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1387 static void
1388 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1390     if ( st >= en ) return;
1391     if ( vst < 0 ) vst=0;
1392     if ( vst > 1 ) vst=1;
1393     if ( ven < 0 ) ven=0;
1394     if ( ven > 1 ) ven=1;
1395     float   sv=vst;
1396     float   dv=ven-vst;
1397     int     len=en-st;
1398     unsigned char*   d=(unsigned char*)dest.buffer;
1399     d+=(st-dest.startPix);
1400     if ( fabs(dv) < 0.001 ) {
1401         if ( vst > 0.999 ) {
1402             /* Simple copy */
1403             while (len > 0) {
1404                 d[0] = 255;
1405                 d += 1;
1406                 len -= 1;
1407             }
1408         } else {
1409             sv*=256;
1410             unsigned int c0_24=(int)sv;
1411             c0_24&=0xFF;
1412             while (len > 0) {
1413                 /* Draw */
1414                 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1415                 d += 1;
1416                 len -= 1;
1417             }
1418         }
1419     } else {
1420         if ( en <= st+1 ) {
1421             sv=0.5*(vst+ven);
1422             sv*=256;
1423             unsigned int c0_24=(int)sv;
1424             c0_24&=0xFF;
1425             /* Draw */
1426             d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1427         } else {
1428             dv/=len;
1429             sv+=0.5*dv; // correction trapezoidale
1430             sv*=16777216;
1431             dv*=16777216;
1432             int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1433             int s0_24 = static_cast<int>(dv);
1434             while (len > 0) {
1435                 unsigned int ca;
1436                 /* Draw */
1437                 ca = c0_24 >> 16;
1438                 if ( ca > 255 ) ca=255;
1439                 d[0] = NR_COMPOSEA_111(ca,d[0]);
1440                 d += 1;
1441                 c0_24 += s0_24;
1442                 c0_24 = CLAMP(c0_24, 0, 16777216);
1443                 len -= 1;
1444             }
1445         }
1446     }
1449 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1451     theS->CalcBBox();
1452     float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1453     int    il,ir,it,ib;
1454     il=(int)floor(l);
1455     ir=(int)ceil(r);
1456     it=(int)floor(t);
1457     ib=(int)ceil(b);
1459     if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1460     if ( il < m.area.x0 ) il=m.area.x0;
1461     if ( it < m.area.y0 ) it=m.area.y0;
1462     if ( ir > m.area.x1 ) ir=m.area.x1;
1463     if ( ib > m.area.y1 ) ib=m.area.y1;
1465     /* This is the FloatLigne version.  See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1466     int    curPt;
1467     float  curY;
1468     theS->BeginQuickRaster(curY, curPt);
1470     FloatLigne *theI = new FloatLigne();
1471     IntLigne *theIL = new IntLigne();
1473     theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1475     char *mdata = (char*)m.data.px;
1476     if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1477     uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1478     for (int y = it; y < ib; y++) {
1479         theI->Reset();
1480         theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1481         theI->Flatten();
1482         theIL->Copy(theI);
1484         raster_info  dest;
1485         dest.startPix=il;
1486         dest.endPix=ir;
1487         dest.sth=il;
1488         dest.stv=y;
1489         dest.buffer=ligStart;
1490         theIL->Raster(dest, NULL, shape_run_A8_OR);
1491         ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1492     }
1493     theS->EndQuickRaster();
1494     delete theI;
1495     delete theIL;
1499 /*
1500   Local Variables:
1501   mode:c++
1502   c-file-style:"stroustrup"
1503   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1504   indent-tabs-mode:nil
1505   fill-column:99
1506   End:
1507 */
1508 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :