1 #define __NR_ARENA_SHAPE_C__
3 /*
4 * RGBA display list system for inkscape
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
17 #include <display/canvas-arena.h>
18 #include <display/nr-arena.h>
19 #include <display/nr-arena-shape.h>
20 #include "display/curve.h"
21 #include <libnr/n-art-bpath.h>
22 #include <libnr/nr-path.h>
23 #include <libnr/nr-pixops.h>
24 #include <libnr/nr-matrix-ops.h>
25 #include <libnr/nr-matrix-fns.h>
26 #include <libnr/nr-blit.h>
27 #include <libnr/nr-convert2geom.h>
28 #include <2geom/pathvector.h>
29 #include <2geom/curves.h>
30 #include <livarot/Path.h>
31 #include <livarot/float-line.h>
32 #include <livarot/int-line.h>
33 #include <style.h>
34 #include "prefs-utils.h"
35 #include "inkscape-cairo.h"
36 #include "helper/geom.h"
37 #include "sp-filter.h"
38 #include "sp-filter-reference.h"
39 #include "display/nr-filter.h"
40 #include <typeinfo>
41 #include <cairo.h>
43 //int showRuns=0;
44 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
46 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
47 static void nr_arena_shape_init(NRArenaShape *shape);
48 static void nr_arena_shape_finalize(NRObject *object);
50 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
51 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
52 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
53 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
55 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
56 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
57 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
58 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
60 static NRArenaItemClass *shape_parent_class;
62 NRType
63 nr_arena_shape_get_type(void)
64 {
65 static NRType type = 0;
66 if (!type) {
67 type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
68 "NRArenaShape",
69 sizeof(NRArenaShapeClass),
70 sizeof(NRArenaShape),
71 (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
72 (void (*)(NRObject *)) nr_arena_shape_init);
73 }
74 return type;
75 }
77 static void
78 nr_arena_shape_class_init(NRArenaShapeClass *klass)
79 {
80 NRObjectClass *object_class;
81 NRArenaItemClass *item_class;
83 object_class = (NRObjectClass *) klass;
84 item_class = (NRArenaItemClass *) klass;
86 shape_parent_class = (NRArenaItemClass *) ((NRObjectClass *) klass)->parent;
88 object_class->finalize = nr_arena_shape_finalize;
89 object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
91 item_class->children = nr_arena_shape_children;
92 item_class->add_child = nr_arena_shape_add_child;
93 item_class->set_child_position = nr_arena_shape_set_child_position;
94 item_class->remove_child = nr_arena_shape_remove_child;
95 item_class->update = nr_arena_shape_update;
96 item_class->render = nr_arena_shape_render;
97 item_class->clip = nr_arena_shape_clip;
98 item_class->pick = nr_arena_shape_pick;
99 }
101 /**
102 * Initializes the arena shape, setting all parameters to null, 0, false,
103 * or other defaults
104 */
105 static void
106 nr_arena_shape_init(NRArenaShape *shape)
107 {
108 shape->curve = NULL;
109 shape->style = NULL;
110 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
111 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
113 shape->ctm.set_identity();
114 shape->fill_painter = NULL;
115 shape->stroke_painter = NULL;
116 shape->cached_fill = NULL;
117 shape->cached_stroke = NULL;
118 shape->cached_fpartialy = false;
119 shape->cached_spartialy = false;
120 shape->fill_shp = NULL;
121 shape->stroke_shp = NULL;
123 shape->delayed_shp = false;
125 shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
126 shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
127 shape->cached_fctm.set_identity();
128 shape->cached_sctm.set_identity();
130 shape->markers = NULL;
132 shape->last_pick = NULL;
133 shape->repick_after = 0;
134 }
136 static void
137 nr_arena_shape_finalize(NRObject *object)
138 {
139 NRArenaShape *shape = (NRArenaShape *) object;
141 if (shape->fill_shp) delete shape->fill_shp;
142 if (shape->stroke_shp) delete shape->stroke_shp;
143 if (shape->cached_fill) delete shape->cached_fill;
144 if (shape->cached_stroke) delete shape->cached_stroke;
145 if (shape->fill_painter) sp_painter_free(shape->fill_painter);
146 if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
148 if (shape->style) sp_style_unref(shape->style);
149 if (shape->curve) shape->curve->unref();
151 ((NRObjectClass *) shape_parent_class)->finalize(object);
152 }
154 /**
155 * Retrieves the markers from the item
156 */
157 static NRArenaItem *
158 nr_arena_shape_children(NRArenaItem *item)
159 {
160 NRArenaShape *shape = (NRArenaShape *) item;
162 return shape->markers;
163 }
165 /**
166 * Attaches child to item, and if ref is not NULL, sets it and ref->next as
167 * the prev and next items. If ref is NULL, then it sets the item's markers
168 * as the next items.
169 */
170 static void
171 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
172 {
173 NRArenaShape *shape = (NRArenaShape *) item;
175 if (!ref) {
176 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
177 } else {
178 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
179 }
181 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
182 }
184 /**
185 * Removes child from the shape. If there are no prev items in
186 * the child, it sets items' markers to the next item in the child.
187 */
188 static void
189 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
190 {
191 NRArenaShape *shape = (NRArenaShape *) item;
193 if (child->prev) {
194 nr_arena_item_detach(item, child);
195 } else {
196 shape->markers = nr_arena_item_detach(item, child);
197 }
199 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
200 }
202 /**
203 * Detaches child from item, and if there are no previous items in child, it
204 * sets item's markers to the child. It then attaches the child back onto the item.
205 * If ref is null, it sets the markers to be the next item, otherwise it uses
206 * the next/prev items in ref.
207 */
208 static void
209 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
210 {
211 NRArenaShape *shape = (NRArenaShape *) item;
213 if (child->prev) {
214 nr_arena_item_detach(item, child);
215 } else {
216 shape->markers = nr_arena_item_detach(item, child);
217 }
219 if (!ref) {
220 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
221 } else {
222 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
223 }
225 nr_arena_item_request_render(child);
226 }
228 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
229 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
230 void nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox);
232 /**
233 * Updates the arena shape 'item' and all of its children, including the markers.
234 */
235 static guint
236 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
237 {
238 Geom::Rect boundingbox;
240 NRArenaShape *shape = NR_ARENA_SHAPE(item);
242 unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
244 unsigned int newstate;
245 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
246 newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
247 beststate = beststate & newstate;
248 }
250 if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
251 /* We do not have to create rendering structures */
252 shape->ctm = gc->transform;
253 if (state & NR_ARENA_ITEM_STATE_BBOX) {
254 if (shape->curve) {
255 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), to_2geom(gc->transform));
256 item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
257 item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
258 item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
259 item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
260 }
261 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
262 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
263 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
264 }
265 }
266 }
267 return (state | item->state);
268 }
270 shape->delayed_shp=true;
271 shape->ctm = gc->transform;
272 boundingbox[0][0] = boundingbox[1][0] = NR_HUGE;
273 boundingbox[0][1] = boundingbox[1][1] = -NR_HUGE;
275 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
277 if (shape->curve) {
278 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), to_2geom(gc->transform));
280 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
281 float width, scale;
282 scale = NR::expansion(gc->transform);
283 width = MAX(0.125, shape->_stroke.width * scale);
284 if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
285 boundingbox.expandBy(width);
286 }
287 // those pesky miters, now
288 float miterMax=width*shape->_stroke.mitre_limit;
289 if ( miterMax > 0.01 ) {
290 // grunt mode. we should compute the various miters instead (one for each point on the curve)
291 boundingbox.expandBy(miterMax);
292 }
293 }
294 } else {
295 }
296 shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
297 shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
298 shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
299 shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
300 if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
302 /* Release state data */
303 if (TRUE || !NR::transform_equalp(gc->transform, shape->ctm, NR_EPSILON)) {
304 /* Concept test */
305 if (shape->fill_shp) {
306 delete shape->fill_shp;
307 shape->fill_shp = NULL;
308 }
309 }
310 if (shape->stroke_shp) {
311 delete shape->stroke_shp;
312 shape->stroke_shp = NULL;
313 }
314 if (shape->fill_painter) {
315 sp_painter_free(shape->fill_painter);
316 shape->fill_painter = NULL;
317 }
318 if (shape->stroke_painter) {
319 sp_painter_free(shape->stroke_painter);
320 shape->stroke_painter = NULL;
321 }
323 if (!shape->curve ||
324 !shape->style ||
325 shape->curve->is_empty() ||
326 (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
327 ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
328 {
329 item->bbox = shape->approx_bbox;
330 return NR_ARENA_ITEM_STATE_ALL;
331 }
333 /* Build state data */
334 if ( shape->delayed_shp ) {
335 item->bbox=shape->approx_bbox;
336 } else {
337 nr_arena_shape_update_stroke(shape, gc, area);
338 nr_arena_shape_update_fill(shape, gc, area);
340 boundingbox[0][0] = boundingbox[0][1] = boundingbox[1][0] = boundingbox[1][1] = 0.0;
341 nr_arena_shape_add_bboxes(shape, boundingbox);
343 shape->approx_bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
344 shape->approx_bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
345 shape->approx_bbox.x1 = (gint32)(boundingbox[0][1] + 1.9999F);
346 shape->approx_bbox.y1 = (gint32)(boundingbox[1][1] + 1.9999F);
347 }
349 if (boundingbox.isEmpty())
350 return NR_ARENA_ITEM_STATE_ALL;
352 item->bbox.x0 = (gint32)(boundingbox[0][0] - 1.0F);
353 item->bbox.y0 = (gint32)(boundingbox[1][0] - 1.0F);
354 item->bbox.x1 = (gint32)(boundingbox[0][1] + 1.0F);
355 item->bbox.y1 = (gint32)(boundingbox[1][1] + 1.0F);
356 nr_arena_request_render_rect(item->arena, &item->bbox);
358 item->render_opacity = TRUE;
359 if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
360 if (gc && gc->parent) {
361 shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
362 gc->transform, gc->parent->transform,
363 &shape->paintbox);
364 }
365 item->render_opacity = FALSE;
366 }
367 if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
368 if (gc && gc->parent) {
369 shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
370 gc->transform, gc->parent->transform,
371 &shape->paintbox);
372 }
373 item->render_opacity = FALSE;
374 }
375 if ( (shape->_fill.paint.type() != NRArenaShape::Paint::NONE &&
376 shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
377 || (shape->markers)
378 )
379 {
380 // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
381 item->render_opacity = FALSE;
382 }
384 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
385 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
386 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
387 }
388 }
390 return NR_ARENA_ITEM_STATE_ALL;
391 }
393 int matrix_is_isometry(NR::Matrix p) {
394 NR::Matrix tp;
395 // transposition
396 tp[0]=p[0];
397 tp[1]=p[2];
398 tp[2]=p[1];
399 tp[3]=p[3];
400 for (int i = 4; i < 6; i++) // shut valgrind up :)
401 tp[i] = p[i] = 0;
402 NR::Matrix isom = tp*p; // A^T * A = adjunct?
403 // Is the adjunct nearly an identity function?
404 if (isom.is_translation(0.01)) {
405 // the transformation is an isometry -> no need to recompute
406 // the uncrossed polygon
407 if ( p.det() < 0 )
408 return -1;
409 else
410 return 1;
411 }
412 return 0;
413 }
415 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
416 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
417 }
419 /* returns true if the pathvector has a region that needs fill.
420 * is for optimizing purposes, so should be fast and can falsely return true.
421 * CANNOT falsely return false. */
422 static bool has_inner_area(Geom::PathVector const & pv) {
423 // return false for the cases where there is surely no region to be filled
424 if (pv.empty())
425 return false;
427 if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
428 // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
429 Geom::Curve const * c = & pv.front().front();
430 if ( dynamic_cast<Geom::LineSegment const*>(c) ||
431 dynamic_cast<Geom::HLineSegment const*>(c) ||
432 dynamic_cast<Geom::VLineSegment const*>(c) )
433 {
434 return false;
435 }
436 }
438 return true; //too costly to see if it has region to be filled, so return true.
439 }
441 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
442 void
443 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
444 {
445 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
446 // ((shape->curve->get_length() > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) { // <-- this used to be the old code, i think it has to determine that the path has a sort of 'internal region' where fill would occur
447 has_inner_area(shape->curve->get_pathvector()) ) {
448 if (TRUE || !shape->fill_shp) {
449 NR::Matrix cached_to_new = NR::identity();
450 int isometry = 0;
451 if ( shape->cached_fill ) {
452 if (shape->cached_fctm == gc->transform) {
453 isometry = 2; // identity
454 } else {
455 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
456 isometry = matrix_is_isometry(cached_to_new);
457 }
458 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
459 isometry = 0;
460 }
461 if ( isometry == 0 ) {
462 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
463 shape->cached_fill->Reset();
465 Path* thePath=new Path;
466 Shape* theShape=new Shape;
467 {
468 Geom::Matrix tempMat(to_2geom(gc->transform));
469 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
470 }
472 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
473 thePath->Convert(1.0);
474 shape->cached_fpartialy = false;
475 } else {
476 thePath->Convert(area, 1.0);
477 shape->cached_fpartialy = true;
478 }
480 thePath->Fill(theShape, 0);
482 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
483 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
484 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
485 //shape->cached_fill->Copy(theShape);
486 } else {
487 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
488 }
489 shape->cached_fctm=gc->transform;
490 shape->cached_farea = *area;
491 delete theShape;
492 delete thePath;
493 if ( shape->fill_shp == NULL )
494 shape->fill_shp = new Shape;
496 shape->fill_shp->Copy(shape->cached_fill);
498 } else if ( 2 == isometry ) {
499 if ( shape->fill_shp == NULL ) {
500 shape->fill_shp = new Shape;
501 shape->fill_shp->Copy(shape->cached_fill);
502 }
503 } else {
505 if ( shape->fill_shp == NULL )
506 shape->fill_shp = new Shape;
508 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
509 shape->cached_fill->numberOfEdges());
510 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
511 shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
512 if ( isometry == 1 ) {
513 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
514 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
515 shape->cached_fill->getEdge(i).en);
516 } else if ( isometry == -1 ) { // need to flip poly.
517 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
518 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
519 shape->cached_fill->getEdge(i).st);
520 }
521 shape->fill_shp->ForceToPolygon();
522 shape->fill_shp->needPointsSorting();
523 shape->fill_shp->needEdgesSorting();
524 }
525 shape->delayed_shp |= shape->cached_fpartialy;
526 }
527 }
528 }
530 void
531 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
532 {
533 SPStyle* style = shape->style;
535 float const scale = NR::expansion(gc->transform);
537 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
539 if (outline) {
540 // cairo does not need the livarot path for rendering
541 return;
542 }
544 // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes
545 // (since it will only be used for picking, not for rendering)
547 if (outline ||
548 ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
549 ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
551 float style_width = MAX(0.125, shape->_stroke.width * scale);
552 float width;
553 if (outline) {
554 width = 0.5; // 1 pixel wide, independent of zoom
555 } else {
556 width = style_width;
557 }
559 NR::Matrix cached_to_new = NR::identity();
561 int isometry = 0;
562 if ( shape->cached_stroke ) {
563 if (shape->cached_sctm == gc->transform) {
564 isometry = 2; // identity
565 } else {
566 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
567 isometry = matrix_is_isometry(cached_to_new);
568 }
569 if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
570 isometry = 0;
571 if (0 != isometry && width != shape->cached_width) {
572 // if this happens without setting style, we have just switched to outline or back
573 isometry = 0;
574 }
575 }
577 if ( isometry == 0 ) {
578 if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
579 shape->cached_stroke->Reset();
580 Path* thePath = new Path;
581 Shape* theShape = new Shape;
582 {
583 Geom::Matrix tempMat( to_2geom(gc->transform) );
584 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
585 }
587 // add some padding to the rendering area, so clipped path does not go into a render area
588 NRRectL padded_area = *area;
589 padded_area.x0 -= (NR::ICoord)width;
590 padded_area.x1 += (NR::ICoord)width;
591 padded_area.y0 -= (NR::ICoord)width;
592 padded_area.y1 += (NR::ICoord)width;
593 if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
594 thePath->Convert((outline) ? 4.0 : 1.0);
595 shape->cached_spartialy = false;
596 }
597 else {
598 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
599 shape->cached_spartialy = true;
600 }
602 if (style->stroke_dash.n_dash && !outline) {
603 thePath->DashPolylineFromStyle(style, scale, 1.0);
604 }
606 ButtType butt=butt_straight;
607 switch (shape->_stroke.cap) {
608 case NRArenaShape::BUTT_CAP:
609 butt = butt_straight;
610 break;
611 case NRArenaShape::ROUND_CAP:
612 butt = butt_round;
613 break;
614 case NRArenaShape::SQUARE_CAP:
615 butt = butt_square;
616 break;
617 }
618 JoinType join=join_straight;
619 switch (shape->_stroke.join) {
620 case NRArenaShape::MITRE_JOIN:
621 join = join_pointy;
622 break;
623 case NRArenaShape::ROUND_JOIN:
624 join = join_round;
625 break;
626 case NRArenaShape::BEVEL_JOIN:
627 join = join_straight;
628 break;
629 }
631 if (outline) {
632 butt = butt_straight;
633 join = join_straight;
634 }
636 thePath->Stroke(theShape, false, 0.5*width, join, butt,
637 0.5*width*shape->_stroke.mitre_limit);
640 if (outline) {
641 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
642 shape->cached_stroke->Copy(theShape);
643 } else {
644 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
645 }
647 shape->cached_width = width;
649 shape->cached_sctm=gc->transform;
650 shape->cached_sarea = *area;
651 delete thePath;
652 delete theShape;
653 if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
655 shape->stroke_shp->Copy(shape->cached_stroke);
657 } else if ( 2 == isometry ) {
658 if ( shape->stroke_shp == NULL ) {
659 shape->stroke_shp=new Shape;
660 shape->stroke_shp->Copy(shape->cached_stroke);
661 }
662 } else {
663 if ( shape->stroke_shp == NULL )
664 shape->stroke_shp=new Shape;
665 shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
666 for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
667 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
668 if ( isometry == 1 ) {
669 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
670 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
671 shape->cached_stroke->getEdge(i).en);
672 } else if ( isometry == -1 ) {
673 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
674 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
675 shape->cached_stroke->getEdge(i).st);
676 }
677 shape->stroke_shp->ForceToPolygon();
678 shape->stroke_shp->needPointsSorting();
679 shape->stroke_shp->needEdgesSorting();
680 }
681 shape->delayed_shp |= shape->cached_spartialy;
682 }
683 }
686 void
687 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::Rect &bbox)
688 {
689 /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
690 * if so, then this can be written much more compact ! */
692 if ( shape->stroke_shp ) {
693 Shape *larger = shape->stroke_shp;
694 larger->CalcBBox();
695 larger->leftX = floor(larger->leftX);
696 larger->rightX = ceil(larger->rightX);
697 larger->topY = floor(larger->topY);
698 larger->bottomY = ceil(larger->bottomY);
699 Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
700 Geom::Interval(larger->topY, larger->bottomY) );
701 bbox.unionWith(stroke_bbox);
702 }
704 if ( shape->fill_shp ) {
705 Shape *larger = shape->fill_shp;
706 larger->CalcBBox();
707 larger->leftX = floor(larger->leftX);
708 larger->rightX = ceil(larger->rightX);
709 larger->topY = floor(larger->topY);
710 larger->bottomY = ceil(larger->bottomY);
711 Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
712 Geom::Interval(larger->topY, larger->bottomY) );
713 bbox.unionWith(fill_bbox);
714 }
715 }
717 // cairo outline rendering:
718 static unsigned int
719 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
720 {
721 NRArenaShape *shape = NR_ARENA_SHAPE(item);
723 if (!ct)
724 return item->state;
726 guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
727 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
728 // by setting color channels in the "wrong" order
729 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));
731 cairo_set_line_width(ct, 0.5);
732 cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
733 cairo_new_path(ct);
735 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area, true, 0);
737 cairo_stroke(ct);
739 return item->state;
740 }
742 // cairo stroke rendering (flat color only so far!):
743 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
744 // (need to switch them to premul before this can be enabled)
745 void
746 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
747 {
748 NRArenaShape *shape = NR_ARENA_SHAPE(item);
749 SPStyle const *style = shape->style;
751 float const scale = NR::expansion(shape->ctm);
753 if (fabs(shape->_stroke.width * scale) < 0.01)
754 return;
756 cairo_t *ct = nr_create_cairo_context (area, pb);
758 if (!ct)
759 return;
761 guint32 rgba;
762 if ( item->render_opacity ) {
763 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
764 SP_SCALE24_TO_FLOAT(style->opacity.value) );
765 } else {
766 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
767 }
769 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
770 // by setting color channels in the "wrong" order
771 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));
773 float style_width = MAX(0.125, shape->_stroke.width * scale);
774 cairo_set_line_width(ct, style_width);
776 switch (shape->_stroke.cap) {
777 case NRArenaShape::BUTT_CAP:
778 cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
779 break;
780 case NRArenaShape::ROUND_CAP:
781 cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
782 break;
783 case NRArenaShape::SQUARE_CAP:
784 cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
785 break;
786 }
787 switch (shape->_stroke.join) {
788 case NRArenaShape::MITRE_JOIN:
789 cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
790 break;
791 case NRArenaShape::ROUND_JOIN:
792 cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
793 break;
794 case NRArenaShape::BEVEL_JOIN:
795 cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
796 break;
797 }
799 cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
801 if (style->stroke_dash.n_dash) {
802 NRVpathDash dash;
803 dash.offset = style->stroke_dash.offset * scale;
804 dash.n_dash = style->stroke_dash.n_dash;
805 dash.dash = g_new(double, dash.n_dash);
806 for (int i = 0; i < dash.n_dash; i++) {
807 dash.dash[i] = style->stroke_dash.dash[i] * scale;
808 }
809 cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
810 g_free(dash.dash);
811 }
813 cairo_set_tolerance(ct, 0.1);
814 cairo_new_path(ct);
816 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), area->upgrade(), true, style_width);
818 cairo_stroke(ct);
820 cairo_surface_t *cst = cairo_get_target(ct);
821 cairo_destroy (ct);
822 cairo_surface_finish (cst);
823 cairo_surface_destroy (cst);
825 pb->empty = FALSE;
826 }
829 /**
830 * Renders the item. Markers are just composed into the parent buffer.
831 */
832 static unsigned int
833 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
834 {
835 NRArenaShape *shape = NR_ARENA_SHAPE(item);
837 if (!shape->curve) return item->state;
838 if (!shape->style) return item->state;
840 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
842 if (outline) { // cairo outline rendering
844 pb->empty = FALSE;
845 unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
846 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
848 } else {
850 if ( shape->delayed_shp ) {
851 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
852 NRGC tempGC(NULL);
853 tempGC.transform=shape->ctm;
854 shape->delayed_shp = false;
855 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
856 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
857 /* NRRect bbox;
858 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
859 nr_arena_shape_add_bboxes(shape,bbox);
860 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
861 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
862 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
863 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
864 shape->approx_bbox=item->bbox;*/
865 } else {
866 return item->state;
867 }
868 }
870 SPStyle const *style = shape->style;
871 if (shape->fill_shp) {
872 NRPixBlock m;
873 guint32 rgba;
875 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
877 // if memory allocation failed, abort render
878 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
879 nr_pixblock_release (&m);
880 return (item->state);
881 }
883 m.visible_area = pb->visible_area;
884 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
885 m.empty = FALSE;
887 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
888 // do not render fill in any way
889 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
890 if ( item->render_opacity ) {
891 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
892 SP_SCALE24_TO_FLOAT(style->opacity.value) );
893 } else {
894 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
895 }
896 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
897 pb->empty = FALSE;
898 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
899 if (shape->fill_painter) {
900 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
901 }
902 }
904 nr_pixblock_release(&m);
905 }
907 if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
909 // cairo_arena_shape_render_stroke(item, area, pb);
911 guint32 rgba;
912 NRPixBlock m;
914 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
916 // if memory allocation failed, abort render
917 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
918 nr_pixblock_release (&m);
919 return (item->state);
920 }
922 m.visible_area = pb->visible_area;
923 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
924 m.empty = FALSE;
926 if ( item->render_opacity ) {
927 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
928 SP_SCALE24_TO_FLOAT(style->opacity.value) );
929 } else {
930 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
931 }
932 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
933 pb->empty = FALSE;
935 nr_pixblock_release(&m);
937 } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
939 NRPixBlock m;
941 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
943 // if memory allocation failed, abort render
944 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
945 nr_pixblock_release (&m);
946 return (item->state);
947 }
949 m.visible_area = pb->visible_area;
950 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
951 m.empty = FALSE;
953 if (shape->stroke_painter) {
954 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
955 }
957 nr_pixblock_release(&m);
958 }
960 } // non-cairo non-outline branch
962 /* Render markers into parent buffer */
963 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
964 unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
965 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
966 }
968 return item->state;
969 }
972 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
973 // reenable this when the bug is fixed and remove the rest of this function
974 // TODO
975 #if defined(DEADCODE) && !defined(DEADCODE)
976 static guint
977 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
978 {
979 NRArenaShape *shape = NR_ARENA_SHAPE(item);
980 if (!shape->curve) return item->state;
982 cairo_t *ct = nr_create_cairo_context (area, pb);
984 if (!ct)
985 return item->state;
987 cairo_set_source_rgba(ct, 0, 0, 0, 1);
989 cairo_new_path(ct);
991 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), to_2geom(shape->ctm), (area)->upgrade(), false, 0);
993 cairo_fill(ct);
995 cairo_surface_t *cst = cairo_get_target(ct);
996 cairo_destroy (ct);
997 cairo_surface_finish (cst);
998 cairo_surface_destroy (cst);
1000 pb->empty = FALSE;
1002 return item->state;
1003 }
1004 #endif //defined(DEADCODE) && !defined(DEADCODE)
1007 static guint
1008 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1009 {
1010 //return cairo_arena_shape_clip(item, area, pb);
1012 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1013 if (!shape->curve) return item->state;
1015 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1016 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1017 NRGC tempGC(NULL);
1018 tempGC.transform=shape->ctm;
1019 shape->delayed_shp = false;
1020 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1021 } else {
1022 return item->state;
1023 }
1024 }
1026 if ( shape->fill_shp ) {
1027 NRPixBlock m;
1029 /* fixme: We can OR in one step (Lauris) */
1030 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1032 // if memory allocation failed, abort
1033 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1034 nr_pixblock_release (&m);
1035 return (item->state);
1036 }
1038 m.visible_area = pb->visible_area;
1039 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1041 for (int y = area->y0; y < area->y1; y++) {
1042 unsigned char *s, *d;
1043 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1044 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1045 for (int x = area->x0; x < area->x1; x++) {
1046 *d = NR_COMPOSEA_111(*s, *d);
1047 d ++;
1048 s ++;
1049 }
1050 }
1051 nr_pixblock_release(&m);
1052 pb->empty = FALSE;
1053 }
1055 return item->state;
1056 }
1058 static NRArenaItem *
1059 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1060 {
1061 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1063 if (shape->repick_after > 0)
1064 shape->repick_after--;
1066 if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1067 return shape->last_pick;
1069 if (!shape->curve) return NULL;
1070 if (!shape->style) return NULL;
1072 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1074 if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline)
1075 // fully transparent, no pick unless outline mode
1076 return NULL;
1078 GTimeVal tstart, tfinish;
1079 g_get_current_time (&tstart);
1081 double width;
1082 if (outline) {
1083 width = 0.5;
1084 } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1085 float const scale = NR::expansion(shape->ctm);
1086 width = MAX(0.125, shape->_stroke.width * scale) / 2;
1087 } else {
1088 width = 0;
1089 }
1091 double dist = NR_HUGE;
1092 int wind = 0;
1093 bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE
1094 && shape->_fill.opacity > 1e-3 && !outline);
1096 if (item->arena->canvasarena) {
1097 Geom::Rect viewbox = to_2geom(item->arena->canvasarena->item.canvas->getViewbox());
1098 viewbox.expandBy (width);
1099 pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), to_2geom(shape->ctm), to_2geom(p), NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1100 } else {
1101 pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), to_2geom(shape->ctm), to_2geom(p), NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1102 }
1104 g_get_current_time (&tfinish);
1105 glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1106 //g_print ("pick time %lu\n", this_pick);
1108 if (this_pick > 10000) { // slow picking, remember to skip several new picks
1109 shape->repick_after = this_pick / 5000;
1110 }
1112 // covered by fill?
1113 if (needfill) {
1114 if (!shape->style->fill_rule.computed) {
1115 if (wind != 0) {
1116 shape->last_pick = item;
1117 return item;
1118 }
1119 } else {
1120 if (wind & 0x1) {
1121 shape->last_pick = item;
1122 return item;
1123 }
1124 }
1125 }
1127 // close to the edge, as defined by strokewidth and delta?
1128 // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1129 if (needfill || width > 0) { // if either fill or stroke visible,
1130 if ((dist - width) < delta) {
1131 shape->last_pick = item;
1132 return item;
1133 }
1134 }
1136 // if not picked on the shape itself, try its markers
1137 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1138 NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1139 if (ret) {
1140 shape->last_pick = item;
1141 return item;
1142 }
1143 }
1145 shape->last_pick = NULL;
1146 return NULL;
1147 }
1149 /**
1150 *
1151 * Requests a render of the shape, then if the shape is already a curve it
1152 * unrefs the old curve; if the new curve is valid it creates a copy of the
1153 * curve and adds it to the shape. Finally, it requests an update of the
1154 * arena for the shape.
1155 */
1156 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1157 {
1158 g_return_if_fail(shape != NULL);
1159 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1161 if ( justTrans == false ) {
1162 // dirty cached versions
1163 if ( shape->cached_fill ) {
1164 delete shape->cached_fill;
1165 shape->cached_fill=NULL;
1166 }
1167 if ( shape->cached_stroke ) {
1168 delete shape->cached_stroke;
1169 shape->cached_stroke=NULL;
1170 }
1171 }
1173 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1175 if (shape->curve) {
1176 shape->curve->unref();
1177 shape->curve = NULL;
1178 }
1180 if (curve) {
1181 shape->curve = curve;
1182 curve->ref();
1183 }
1185 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1186 }
1188 void NRArenaShape::setFill(SPPaintServer *server) {
1189 _fill.paint.set(server);
1190 _invalidateCachedFill();
1191 }
1193 void NRArenaShape::setFill(SPColor const &color) {
1194 _fill.paint.set(color);
1195 _invalidateCachedFill();
1196 }
1198 void NRArenaShape::setFillOpacity(double opacity) {
1199 _fill.opacity = opacity;
1200 _invalidateCachedFill();
1201 }
1203 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1204 _fill.rule = rule;
1205 _invalidateCachedFill();
1206 }
1208 void NRArenaShape::setStroke(SPPaintServer *server) {
1209 _stroke.paint.set(server);
1210 _invalidateCachedStroke();
1211 }
1213 void NRArenaShape::setStroke(SPColor const &color) {
1214 _stroke.paint.set(color);
1215 _invalidateCachedStroke();
1216 }
1218 void NRArenaShape::setStrokeOpacity(double opacity) {
1219 _stroke.opacity = opacity;
1220 _invalidateCachedStroke();
1221 }
1223 void NRArenaShape::setStrokeWidth(double width) {
1224 _stroke.width = width;
1225 _invalidateCachedStroke();
1226 }
1228 void NRArenaShape::setMitreLimit(double limit) {
1229 _stroke.mitre_limit = limit;
1230 _invalidateCachedStroke();
1231 }
1233 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1234 _stroke.cap = cap;
1235 _invalidateCachedStroke();
1236 }
1238 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1239 _stroke.join = join;
1240 _invalidateCachedStroke();
1241 }
1243 /** nr_arena_shape_set_style
1244 *
1245 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1246 */
1247 void
1248 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1249 {
1250 g_return_if_fail(shape != NULL);
1251 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1253 if (style) sp_style_ref(style);
1254 if (shape->style) sp_style_unref(shape->style);
1255 shape->style = style;
1257 if ( style->fill.isPaintserver() ) {
1258 shape->setFill(style->getFillPaintServer());
1259 } else if ( style->fill.isColor() ) {
1260 shape->setFill(style->fill.value.color);
1261 } else if ( style->fill.isNone() ) {
1262 shape->setFill(NULL);
1263 } else {
1264 g_assert_not_reached();
1265 }
1266 shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1267 switch (style->fill_rule.computed) {
1268 case SP_WIND_RULE_EVENODD: {
1269 shape->setFillRule(NRArenaShape::EVEN_ODD);
1270 break;
1271 }
1272 case SP_WIND_RULE_NONZERO: {
1273 shape->setFillRule(NRArenaShape::NONZERO);
1274 break;
1275 }
1276 default: {
1277 g_assert_not_reached();
1278 }
1279 }
1281 if ( style->stroke.isPaintserver() ) {
1282 shape->setStroke(style->getStrokePaintServer());
1283 } else if ( style->stroke.isColor() ) {
1284 shape->setStroke(style->stroke.value.color);
1285 } else if ( style->stroke.isNone() ) {
1286 shape->setStroke(NULL);
1287 } else {
1288 g_assert_not_reached();
1289 }
1290 shape->setStrokeWidth(style->stroke_width.computed);
1291 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1292 switch (style->stroke_linecap.computed) {
1293 case SP_STROKE_LINECAP_ROUND: {
1294 shape->setLineCap(NRArenaShape::ROUND_CAP);
1295 break;
1296 }
1297 case SP_STROKE_LINECAP_SQUARE: {
1298 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1299 break;
1300 }
1301 case SP_STROKE_LINECAP_BUTT: {
1302 shape->setLineCap(NRArenaShape::BUTT_CAP);
1303 break;
1304 }
1305 default: {
1306 g_assert_not_reached();
1307 }
1308 }
1309 switch (style->stroke_linejoin.computed) {
1310 case SP_STROKE_LINEJOIN_ROUND: {
1311 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1312 break;
1313 }
1314 case SP_STROKE_LINEJOIN_BEVEL: {
1315 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1316 break;
1317 }
1318 case SP_STROKE_LINEJOIN_MITER: {
1319 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1320 break;
1321 }
1322 default: {
1323 g_assert_not_reached();
1324 }
1325 }
1326 shape->setMitreLimit(style->stroke_miterlimit.value);
1328 //if shape has a filter
1329 if (style->filter.set && style->getFilter()) {
1330 if (!shape->filter) {
1331 int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1332 shape->filter = new NR::Filter(primitives);
1333 }
1334 sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1335 } else {
1336 //no filter set for this shape
1337 delete shape->filter;
1338 shape->filter = NULL;
1339 }
1341 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1342 }
1344 void
1345 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1346 {
1347 g_return_if_fail(shape != NULL);
1348 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1349 g_return_if_fail(pbox != NULL);
1351 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1352 shape->paintbox = *pbox;
1353 } else {
1354 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1355 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1356 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1357 }
1359 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1360 }
1362 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1363 {
1364 paintbox.x0 = pbox.min()[NR::X];
1365 paintbox.y0 = pbox.min()[NR::Y];
1366 paintbox.x1 = pbox.max()[NR::X];
1367 paintbox.y1 = pbox.max()[NR::Y];
1369 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1370 }
1372 static void
1373 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1374 {
1375 if ( st >= en ) return;
1376 if ( vst < 0 ) vst=0;
1377 if ( vst > 1 ) vst=1;
1378 if ( ven < 0 ) ven=0;
1379 if ( ven > 1 ) ven=1;
1380 float sv=vst;
1381 float dv=ven-vst;
1382 int len=en-st;
1383 unsigned char* d=(unsigned char*)dest.buffer;
1384 d+=(st-dest.startPix);
1385 if ( fabs(dv) < 0.001 ) {
1386 if ( vst > 0.999 ) {
1387 /* Simple copy */
1388 while (len > 0) {
1389 d[0] = 255;
1390 d += 1;
1391 len -= 1;
1392 }
1393 } else {
1394 sv*=256;
1395 unsigned int c0_24=(int)sv;
1396 c0_24&=0xFF;
1397 while (len > 0) {
1398 /* Draw */
1399 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1400 d += 1;
1401 len -= 1;
1402 }
1403 }
1404 } else {
1405 if ( en <= st+1 ) {
1406 sv=0.5*(vst+ven);
1407 sv*=256;
1408 unsigned int c0_24=(int)sv;
1409 c0_24&=0xFF;
1410 /* Draw */
1411 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1412 } else {
1413 dv/=len;
1414 sv+=0.5*dv; // correction trapezoidale
1415 sv*=16777216;
1416 dv*=16777216;
1417 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1418 int s0_24 = static_cast<int>(dv);
1419 while (len > 0) {
1420 unsigned int ca;
1421 /* Draw */
1422 ca = c0_24 >> 16;
1423 if ( ca > 255 ) ca=255;
1424 d[0] = NR_COMPOSEA_111(ca,d[0]);
1425 d += 1;
1426 c0_24 += s0_24;
1427 c0_24 = CLAMP(c0_24, 0, 16777216);
1428 len -= 1;
1429 }
1430 }
1431 }
1432 }
1434 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1435 {
1436 theS->CalcBBox();
1437 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1438 int il,ir,it,ib;
1439 il=(int)floor(l);
1440 ir=(int)ceil(r);
1441 it=(int)floor(t);
1442 ib=(int)ceil(b);
1444 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1445 if ( il < m.area.x0 ) il=m.area.x0;
1446 if ( it < m.area.y0 ) it=m.area.y0;
1447 if ( ir > m.area.x1 ) ir=m.area.x1;
1448 if ( ib > m.area.y1 ) ib=m.area.y1;
1450 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1451 int curPt;
1452 float curY;
1453 theS->BeginQuickRaster(curY, curPt);
1455 FloatLigne *theI = new FloatLigne();
1456 IntLigne *theIL = new IntLigne();
1458 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1460 char *mdata = (char*)m.data.px;
1461 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1462 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1463 for (int y = it; y < ib; y++) {
1464 theI->Reset();
1465 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1466 theI->Flatten();
1467 theIL->Copy(theI);
1469 raster_info dest;
1470 dest.startPix=il;
1471 dest.endPix=ir;
1472 dest.sth=il;
1473 dest.stv=y;
1474 dest.buffer=ligStart;
1475 theIL->Raster(dest, NULL, shape_run_A8_OR);
1476 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1477 }
1478 theS->EndQuickRaster();
1479 delete theI;
1480 delete theIL;
1481 }
1484 /*
1485 Local Variables:
1486 mode:c++
1487 c-file-style:"stroustrup"
1488 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1489 indent-tabs-mode:nil
1490 fill-column:99
1491 End:
1492 */
1493 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :