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 */
15 #include <2geom/svg-path.h>
16 #include <2geom/svg-path-parser.h>
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/nr-pixops.h>
22 #include <libnr/nr-blit.h>
23 #include <libnr/nr-convert2geom.h>
24 #include <2geom/pathvector.h>
25 #include <2geom/curves.h>
26 #include <livarot/Path.h>
27 #include <livarot/float-line.h>
28 #include <livarot/int-line.h>
29 #include <style.h>
30 #include "inkscape-cairo.h"
31 #include "helper/geom.h"
32 #include "helper/geom-curves.h"
33 #include "sp-filter.h"
34 #include "sp-filter-reference.h"
35 #include "display/nr-filter.h"
36 #include <typeinfo>
37 #include <cairo.h>
39 #include <glib.h>
40 #include "svg/svg.h"
41 #include <fenv.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, Geom::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.setIdentity();
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.setIdentity();
128 shape->cached_sctm.setIdentity();
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::OptRect &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::OptRect 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(), gc->transform);
256 /// \todo just write item->bbox = boundingbox
257 if (boundingbox) {
258 item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
259 item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
260 item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
261 item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
262 } else {
263 item->bbox = NR_RECT_L_EMPTY;
264 }
265 }
266 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
267 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
268 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
269 }
270 }
271 }
272 return (state | item->state);
273 }
275 shape->delayed_shp=true;
276 shape->ctm = gc->transform;
277 boundingbox = Geom::OptRect();
279 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
281 if (shape->curve) {
282 boundingbox = bounds_exact_transformed(shape->curve->get_pathvector(), gc->transform);
284 if (boundingbox && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
285 float width, scale;
286 scale = gc->transform.descrim();
287 width = MAX(0.125, shape->_stroke.width * scale);
288 if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
289 boundingbox->expandBy(width);
290 }
291 // those pesky miters, now
292 float miterMax=width*shape->_stroke.mitre_limit;
293 if ( miterMax > 0.01 ) {
294 // grunt mode. we should compute the various miters instead (one for each point on the curve)
295 boundingbox->expandBy(miterMax);
296 }
297 }
298 }
300 /// \todo just write item->bbox = boundingbox
301 if (boundingbox) {
302 shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
303 shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
304 shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
305 shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
306 } else {
307 shape->approx_bbox = NR_RECT_L_EMPTY;
308 }
309 if ( area && nr_rect_l_test_intersect_ptr(area, &shape->approx_bbox) ) shape->delayed_shp=false;
311 /* Release state data */
312 if (shape->fill_shp) {
313 delete shape->fill_shp;
314 shape->fill_shp = NULL;
315 }
316 if (shape->stroke_shp) {
317 delete shape->stroke_shp;
318 shape->stroke_shp = NULL;
319 }
320 if (shape->fill_painter) {
321 sp_painter_free(shape->fill_painter);
322 shape->fill_painter = NULL;
323 }
324 if (shape->stroke_painter) {
325 sp_painter_free(shape->stroke_painter);
326 shape->stroke_painter = NULL;
327 }
329 if (!shape->curve ||
330 !shape->style ||
331 shape->curve->is_empty() ||
332 (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
333 ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
334 {
335 item->bbox = shape->approx_bbox;
336 return NR_ARENA_ITEM_STATE_ALL;
337 }
339 /* Build state data */
340 if ( shape->delayed_shp ) {
341 item->bbox=shape->approx_bbox;
342 } else {
343 nr_arena_shape_update_stroke(shape, gc, area);
344 nr_arena_shape_update_fill(shape, gc, area);
346 boundingbox = Geom::OptRect();
347 nr_arena_shape_add_bboxes(shape, boundingbox);
349 /// \todo just write shape->approx_bbox = boundingbox
350 if (boundingbox) {
351 shape->approx_bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
352 shape->approx_bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
353 shape->approx_bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.9999F);
354 shape->approx_bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.9999F);
355 } else {
356 shape->approx_bbox = NR_RECT_L_EMPTY;
357 }
358 }
360 if (!boundingbox)
361 return NR_ARENA_ITEM_STATE_ALL;
363 /// \todo just write item->bbox = boundingbox
364 item->bbox.x0 = (gint32)((*boundingbox)[0][0] - 1.0F);
365 item->bbox.y0 = (gint32)((*boundingbox)[1][0] - 1.0F);
366 item->bbox.x1 = (gint32)((*boundingbox)[0][1] + 1.0F);
367 item->bbox.y1 = (gint32)((*boundingbox)[1][1] + 1.0F);
368 nr_arena_request_render_rect(item->arena, &item->bbox);
370 item->render_opacity = TRUE;
371 if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
372 if (gc && gc->parent) {
373 shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
374 gc->transform, gc->parent->transform,
375 &shape->paintbox);
376 }
377 item->render_opacity = FALSE;
378 }
379 if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
380 if (gc && gc->parent) {
381 shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
382 gc->transform, gc->parent->transform,
383 &shape->paintbox);
384 }
385 item->render_opacity = FALSE;
386 }
387 if ( (shape->_fill.paint.type() != NRArenaShape::Paint::NONE &&
388 shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
389 || (shape->markers)
390 )
391 {
392 // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
393 item->render_opacity = FALSE;
394 }
396 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
397 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
398 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
399 }
400 }
402 return NR_ARENA_ITEM_STATE_ALL;
403 }
405 int matrix_is_isometry(Geom::Matrix p) {
406 Geom::Matrix tp;
407 // transposition
408 tp[0]=p[0];
409 tp[1]=p[2];
410 tp[2]=p[1];
411 tp[3]=p[3];
412 for (int i = 4; i < 6; i++) // shut valgrind up :)
413 tp[i] = p[i] = 0;
414 Geom::Matrix isom = tp*p; // A^T * A = adjunct?
415 // Is the adjunct nearly an identity function?
416 if (isom.isTranslation(0.01)) {
417 // the transformation is an isometry -> no need to recompute
418 // the uncrossed polygon
419 if ( p.det() < 0 )
420 return -1;
421 else
422 return 1;
423 }
424 return 0;
425 }
427 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
428 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
429 }
431 /* returns true if the pathvector has a region that needs fill.
432 * is for optimizing purposes, so should be fast and can falsely return true.
433 * CANNOT falsely return false. */
434 static bool has_inner_area(Geom::PathVector const & pv) {
435 // return false for the cases where there is surely no region to be filled
436 if (pv.empty())
437 return false;
439 if ( (pv.size() == 1) && (pv.front().size() <= 1) ) {
440 // vector has only one path with only one segment, see if that's a non-curve segment: that would mean no internal region
441 if ( is_straight_curve(pv.front().front()) )
442 {
443 return false;
444 }
445 }
447 return true; //too costly to see if it has region to be filled, so return true.
448 }
450 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
451 void
452 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
453 {
454 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
455 has_inner_area(shape->curve->get_pathvector()) ) {
457 Geom::Matrix cached_to_new = Geom::identity();
458 int isometry = 0;
459 if ( shape->cached_fill ) {
460 if (shape->cached_fctm == gc->transform) {
461 isometry = 2; // identity
462 } else {
463 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
464 isometry = matrix_is_isometry(cached_to_new);
465 }
466 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
467 isometry = 0;
468 }
469 if ( isometry == 0 ) {
470 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
471 shape->cached_fill->Reset();
473 Path* thePath=new Path;
474 Shape* theShape=new Shape;
475 {
476 Geom::Matrix tempMat(gc->transform);
477 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
478 }
480 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
481 thePath->Convert(1.0);
482 shape->cached_fpartialy = false;
483 } else {
484 thePath->Convert(area, 1.0);
485 shape->cached_fpartialy = true;
486 }
488 thePath->Fill(theShape, 0);
490 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
491 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
492 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
493 //shape->cached_fill->Copy(theShape);
494 } else {
495 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
496 }
497 shape->cached_fctm=gc->transform;
498 shape->cached_farea = *area;
499 delete theShape;
500 delete thePath;
501 if ( shape->fill_shp == NULL )
502 shape->fill_shp = new Shape;
504 shape->fill_shp->Copy(shape->cached_fill);
506 } else if ( 2 == isometry ) {
507 if ( shape->fill_shp == NULL ) {
508 shape->fill_shp = new Shape;
509 shape->fill_shp->Copy(shape->cached_fill);
510 }
511 } else {
513 if ( shape->fill_shp == NULL )
514 shape->fill_shp = new Shape;
516 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
517 shape->cached_fill->numberOfEdges());
518 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
519 shape->fill_shp->AddPoint(to_2geom(shape->cached_fill->getPoint(i).x) * cached_to_new);
520 if ( isometry == 1 ) {
521 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
522 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
523 shape->cached_fill->getEdge(i).en);
524 } else if ( isometry == -1 ) { // need to flip poly.
525 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
526 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
527 shape->cached_fill->getEdge(i).st);
528 }
529 shape->fill_shp->ForceToPolygon();
530 shape->fill_shp->needPointsSorting();
531 shape->fill_shp->needEdgesSorting();
532 }
533 shape->delayed_shp |= shape->cached_fpartialy;
534 }
535 }
537 void
538 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
539 {
540 SPStyle* style = shape->style;
542 float const scale = gc->transform.descrim();
544 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
546 if (outline) {
547 // cairo does not need the livarot path for rendering
548 return;
549 }
551 // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes
552 // (since it will only be used for picking, not for rendering)
554 if (outline ||
555 ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
556 ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
558 float style_width = MAX(0.125, shape->_stroke.width * scale);
559 float width;
560 if (outline) {
561 width = 0.5; // 1 pixel wide, independent of zoom
562 } else {
563 width = style_width;
564 }
566 Geom::Matrix cached_to_new = Geom::identity();
568 int isometry = 0;
569 if ( shape->cached_stroke ) {
570 if (shape->cached_sctm == gc->transform) {
571 isometry = 2; // identity
572 } else {
573 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
574 isometry = matrix_is_isometry(cached_to_new);
575 }
576 if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
577 isometry = 0;
578 if (0 != isometry && width != shape->cached_width) {
579 // if this happens without setting style, we have just switched to outline or back
580 isometry = 0;
581 }
582 }
584 if ( isometry == 0 ) {
585 if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
586 shape->cached_stroke->Reset();
587 Path* thePath = new Path;
588 Shape* theShape = new Shape;
589 {
590 Geom::Matrix tempMat( gc->transform );
591 thePath->LoadPathVector(shape->curve->get_pathvector(), tempMat, true);
592 }
594 // add some padding to the rendering area, so clipped path does not go into a render area
595 NRRectL padded_area = *area;
596 padded_area.x0 -= (NR::ICoord)width;
597 padded_area.x1 += (NR::ICoord)width;
598 padded_area.y0 -= (NR::ICoord)width;
599 padded_area.y1 += (NR::ICoord)width;
600 if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
601 thePath->Convert((outline) ? 4.0 : 1.0);
602 shape->cached_spartialy = false;
603 }
604 else {
605 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
606 shape->cached_spartialy = true;
607 }
609 if (style->stroke_dash.n_dash && !outline) {
610 thePath->DashPolylineFromStyle(style, scale, 1.0);
611 }
613 ButtType butt=butt_straight;
614 switch (shape->_stroke.cap) {
615 case NRArenaShape::BUTT_CAP:
616 butt = butt_straight;
617 break;
618 case NRArenaShape::ROUND_CAP:
619 butt = butt_round;
620 break;
621 case NRArenaShape::SQUARE_CAP:
622 butt = butt_square;
623 break;
624 }
625 JoinType join=join_straight;
626 switch (shape->_stroke.join) {
627 case NRArenaShape::MITRE_JOIN:
628 join = join_pointy;
629 break;
630 case NRArenaShape::ROUND_JOIN:
631 join = join_round;
632 break;
633 case NRArenaShape::BEVEL_JOIN:
634 join = join_straight;
635 break;
636 }
638 if (outline) {
639 butt = butt_straight;
640 join = join_straight;
641 }
643 thePath->Stroke(theShape, false, 0.5*width, join, butt,
644 0.5*width*shape->_stroke.mitre_limit);
647 if (outline) {
648 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
649 shape->cached_stroke->Copy(theShape);
650 } else {
651 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
652 }
654 shape->cached_width = width;
656 shape->cached_sctm=gc->transform;
657 shape->cached_sarea = *area;
658 delete thePath;
659 delete theShape;
660 if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
662 shape->stroke_shp->Copy(shape->cached_stroke);
664 } else if ( 2 == isometry ) {
665 if ( shape->stroke_shp == NULL ) {
666 shape->stroke_shp=new Shape;
667 shape->stroke_shp->Copy(shape->cached_stroke);
668 }
669 } else {
670 if ( shape->stroke_shp == NULL )
671 shape->stroke_shp=new Shape;
672 shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
673 for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
674 shape->stroke_shp->AddPoint(to_2geom(shape->cached_stroke->getPoint(i).x) * cached_to_new);
675 if ( isometry == 1 ) {
676 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
677 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
678 shape->cached_stroke->getEdge(i).en);
679 } else if ( isometry == -1 ) {
680 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
681 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
682 shape->cached_stroke->getEdge(i).st);
683 }
684 shape->stroke_shp->ForceToPolygon();
685 shape->stroke_shp->needPointsSorting();
686 shape->stroke_shp->needEdgesSorting();
687 }
688 shape->delayed_shp |= shape->cached_spartialy;
689 }
690 }
693 void
694 nr_arena_shape_add_bboxes(NRArenaShape* shape, Geom::OptRect &bbox)
695 {
696 /* TODO: are these two if's mutually exclusive? ( i.e. "shape->stroke_shp <=> !shape->fill_shp" )
697 * if so, then this can be written much more compact ! */
699 if ( shape->stroke_shp ) {
700 Shape *larger = shape->stroke_shp;
701 larger->CalcBBox();
702 larger->leftX = floor(larger->leftX);
703 larger->rightX = ceil(larger->rightX);
704 larger->topY = floor(larger->topY);
705 larger->bottomY = ceil(larger->bottomY);
706 Geom::Rect stroke_bbox( Geom::Interval(larger->leftX, larger->rightX),
707 Geom::Interval(larger->topY, larger->bottomY) );
708 bbox.unionWith(stroke_bbox);
709 }
711 if ( shape->fill_shp ) {
712 Shape *larger = shape->fill_shp;
713 larger->CalcBBox();
714 larger->leftX = floor(larger->leftX);
715 larger->rightX = ceil(larger->rightX);
716 larger->topY = floor(larger->topY);
717 larger->bottomY = ceil(larger->bottomY);
718 Geom::Rect fill_bbox( Geom::Interval(larger->leftX, larger->rightX),
719 Geom::Interval(larger->topY, larger->bottomY) );
720 bbox.unionWith(fill_bbox);
721 }
722 }
724 // cairo outline rendering:
725 static unsigned int
726 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, Geom::OptRect area)
727 {
728 NRArenaShape *shape = NR_ARENA_SHAPE(item);
730 if (!ct)
731 return item->state;
733 guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
734 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
735 // by setting color channels in the "wrong" order
736 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));
738 cairo_set_line_width(ct, 0.5);
739 cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
740 cairo_new_path(ct);
742 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, area, true, 0);
744 cairo_stroke(ct);
746 return item->state;
747 }
749 // cairo stroke rendering (flat color only so far!):
750 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
751 // (need to switch them to premul before this can be enabled)
752 void
753 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
754 {
755 NRArenaShape *shape = NR_ARENA_SHAPE(item);
756 SPStyle const *style = shape->style;
758 float const scale = shape->ctm.descrim();
760 if (fabs(shape->_stroke.width * scale) < 0.01)
761 return;
763 cairo_t *ct = nr_create_cairo_context (area, pb);
765 if (!ct)
766 return;
768 guint32 rgba;
769 if ( item->render_opacity ) {
770 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
771 SP_SCALE24_TO_FLOAT(style->opacity.value) );
772 } else {
773 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
774 }
776 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
777 // by setting color channels in the "wrong" order
778 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));
780 float style_width = MAX(0.125, shape->_stroke.width * scale);
781 cairo_set_line_width(ct, style_width);
783 switch (shape->_stroke.cap) {
784 case NRArenaShape::BUTT_CAP:
785 cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
786 break;
787 case NRArenaShape::ROUND_CAP:
788 cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
789 break;
790 case NRArenaShape::SQUARE_CAP:
791 cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
792 break;
793 }
794 switch (shape->_stroke.join) {
795 case NRArenaShape::MITRE_JOIN:
796 cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
797 break;
798 case NRArenaShape::ROUND_JOIN:
799 cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
800 break;
801 case NRArenaShape::BEVEL_JOIN:
802 cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
803 break;
804 }
806 cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
808 if (style->stroke_dash.n_dash) {
809 NRVpathDash dash;
810 dash.offset = style->stroke_dash.offset * scale;
811 dash.n_dash = style->stroke_dash.n_dash;
812 dash.dash = g_new(double, dash.n_dash);
813 for (int i = 0; i < dash.n_dash; i++) {
814 dash.dash[i] = style->stroke_dash.dash[i] * scale;
815 }
816 cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
817 g_free(dash.dash);
818 }
820 cairo_set_tolerance(ct, 0.1);
821 cairo_new_path(ct);
823 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, to_2geom(area->upgrade()), true, style_width);
825 cairo_stroke(ct);
827 cairo_surface_t *cst = cairo_get_target(ct);
828 cairo_destroy (ct);
829 cairo_surface_finish (cst);
830 cairo_surface_destroy (cst);
832 pb->empty = FALSE;
833 }
836 /**
837 * Renders the item. Markers are just composed into the parent buffer.
838 */
839 static unsigned int
840 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
841 {
842 NRArenaShape *shape = NR_ARENA_SHAPE(item);
844 if (!shape->curve) return item->state;
845 if (!shape->style) return item->state;
847 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
849 if (outline) { // cairo outline rendering
851 pb->empty = FALSE;
852 unsigned int ret = cairo_arena_shape_render_outline (ct, item, to_2geom((&pb->area)->upgrade()));
853 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
855 } else {
857 if ( shape->delayed_shp ) {
858 if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
859 NRGC tempGC(NULL);
860 tempGC.transform=shape->ctm;
861 shape->delayed_shp = false;
862 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
863 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
864 /* NRRect bbox;
865 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
866 nr_arena_shape_add_bboxes(shape,bbox);
867 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
868 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
869 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
870 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
871 shape->approx_bbox=item->bbox;*/
872 } else {
873 return item->state;
874 }
875 }
877 SPStyle const *style = shape->style;
878 if (shape->fill_shp) {
879 NRPixBlock m;
880 guint32 rgba;
882 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
884 // if memory allocation failed, abort render
885 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
886 nr_pixblock_release (&m);
887 return (item->state);
888 }
890 m.visible_area = pb->visible_area;
891 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
892 m.empty = FALSE;
894 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
895 // do not render fill in any way
896 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
897 if ( item->render_opacity ) {
898 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity *
899 SP_SCALE24_TO_FLOAT(style->opacity.value) );
900 } else {
901 rgba = shape->_fill.paint.color().toRGBA32( shape->_fill.opacity );
902 }
903 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
904 pb->empty = FALSE;
905 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
906 if (shape->fill_painter) {
907 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
908 }
909 }
911 nr_pixblock_release(&m);
912 }
914 if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
916 // cairo_arena_shape_render_stroke(item, area, pb);
918 guint32 rgba;
919 NRPixBlock m;
921 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
923 // if memory allocation failed, abort render
924 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
925 nr_pixblock_release (&m);
926 return (item->state);
927 }
929 m.visible_area = pb->visible_area;
930 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
931 m.empty = FALSE;
933 if ( item->render_opacity ) {
934 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity *
935 SP_SCALE24_TO_FLOAT(style->opacity.value) );
936 } else {
937 rgba = shape->_stroke.paint.color().toRGBA32( shape->_stroke.opacity );
938 }
939 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
940 pb->empty = FALSE;
942 nr_pixblock_release(&m);
944 } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
946 NRPixBlock m;
948 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
950 // if memory allocation failed, abort render
951 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
952 nr_pixblock_release (&m);
953 return (item->state);
954 }
956 m.visible_area = pb->visible_area;
957 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
958 m.empty = FALSE;
960 if (shape->stroke_painter) {
961 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
962 }
964 nr_pixblock_release(&m);
965 }
967 } // non-cairo non-outline branch
969 /* Render markers into parent buffer */
970 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
971 unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
972 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
973 }
975 return item->state;
976 }
979 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
980 // reenable this when the bug is fixed and remove the rest of this function
981 // TODO
982 #if defined(DEADCODE) && !defined(DEADCODE)
983 static guint
984 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
985 {
986 NRArenaShape *shape = NR_ARENA_SHAPE(item);
987 if (!shape->curve) return item->state;
989 cairo_t *ct = nr_create_cairo_context (area, pb);
991 if (!ct)
992 return item->state;
994 cairo_set_source_rgba(ct, 0, 0, 0, 1);
996 cairo_new_path(ct);
998 feed_pathvector_to_cairo (ct, shape->curve->get_pathvector(), shape->ctm, (area)->upgrade(), false, 0);
1000 cairo_fill(ct);
1002 cairo_surface_t *cst = cairo_get_target(ct);
1003 cairo_destroy (ct);
1004 cairo_surface_finish (cst);
1005 cairo_surface_destroy (cst);
1007 pb->empty = FALSE;
1009 return item->state;
1010 }
1011 #endif //defined(DEADCODE) && !defined(DEADCODE)
1014 static guint
1015 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1016 {
1017 //return cairo_arena_shape_clip(item, area, pb);
1019 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1020 if (!shape->curve) return item->state;
1022 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1023 if ( nr_rect_l_test_intersect_ptr(area, &item->bbox) ) {
1024 NRGC tempGC(NULL);
1025 tempGC.transform=shape->ctm;
1026 shape->delayed_shp = false;
1027 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1028 } else {
1029 return item->state;
1030 }
1031 }
1033 if ( shape->fill_shp ) {
1034 NRPixBlock m;
1036 /* fixme: We can OR in one step (Lauris) */
1037 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1039 // if memory allocation failed, abort
1040 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1041 nr_pixblock_release (&m);
1042 return (item->state);
1043 }
1045 m.visible_area = pb->visible_area;
1046 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1048 for (int y = area->y0; y < area->y1; y++) {
1049 unsigned char *s, *d;
1050 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1051 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1052 for (int x = area->x0; x < area->x1; x++) {
1053 *d = NR_COMPOSEA_111(*s, *d);
1054 d ++;
1055 s ++;
1056 }
1057 }
1058 nr_pixblock_release(&m);
1059 pb->empty = FALSE;
1060 }
1062 return item->state;
1063 }
1065 static NRArenaItem *
1066 nr_arena_shape_pick(NRArenaItem *item, Geom::Point p, double delta, unsigned int /*sticky*/)
1067 {
1068 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1070 if (shape->repick_after > 0)
1071 shape->repick_after--;
1073 if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1074 return shape->last_pick;
1076 if (!shape->curve) return NULL;
1077 if (!shape->style) return NULL;
1079 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
1081 if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0 && !outline)
1082 // fully transparent, no pick unless outline mode
1083 return NULL;
1085 GTimeVal tstart, tfinish;
1086 g_get_current_time (&tstart);
1088 double width;
1089 if (outline) {
1090 width = 0.5;
1091 } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1092 float const scale = shape->ctm.descrim();
1093 width = MAX(0.125, shape->_stroke.width * scale) / 2;
1094 } else {
1095 width = 0;
1096 }
1098 double dist = NR_HUGE;
1099 int wind = 0;
1100 bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE
1101 && shape->_fill.opacity > 1e-3 && !outline);
1103 if (item->arena->canvasarena) {
1104 Geom::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1105 viewbox.expandBy (width);
1106 pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1107 } else {
1108 pathv_matrix_point_bbox_wind_distance(shape->curve->get_pathvector(), shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1109 }
1111 g_get_current_time (&tfinish);
1112 glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1113 //g_print ("pick time %lu\n", this_pick);
1115 if (this_pick > 10000) { // slow picking, remember to skip several new picks
1116 shape->repick_after = this_pick / 5000;
1117 }
1119 // covered by fill?
1120 if (needfill) {
1121 if (!shape->style->fill_rule.computed) {
1122 if (wind != 0) {
1123 shape->last_pick = item;
1124 return item;
1125 }
1126 } else {
1127 if (wind & 0x1) {
1128 shape->last_pick = item;
1129 return item;
1130 }
1131 }
1132 }
1134 // close to the edge, as defined by strokewidth and delta?
1135 // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1136 if (needfill || width > 0) { // if either fill or stroke visible,
1137 if ((dist - width) < delta) {
1138 shape->last_pick = item;
1139 return item;
1140 }
1141 }
1143 // if not picked on the shape itself, try its markers
1144 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1145 NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1146 if (ret) {
1147 shape->last_pick = item;
1148 return item;
1149 }
1150 }
1152 shape->last_pick = NULL;
1153 return NULL;
1154 }
1156 /**
1157 *
1158 * Requests a render of the shape, then if the shape is already a curve it
1159 * unrefs the old curve; if the new curve is valid it creates a copy of the
1160 * curve and adds it to the shape. Finally, it requests an update of the
1161 * arena for the shape.
1162 */
1163 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1164 {
1165 g_return_if_fail(shape != NULL);
1166 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1168 if ( justTrans == false ) {
1169 // dirty cached versions
1170 if ( shape->cached_fill ) {
1171 delete shape->cached_fill;
1172 shape->cached_fill=NULL;
1173 }
1174 if ( shape->cached_stroke ) {
1175 delete shape->cached_stroke;
1176 shape->cached_stroke=NULL;
1177 }
1178 }
1180 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1182 if (shape->curve) {
1183 shape->curve->unref();
1184 shape->curve = NULL;
1185 }
1187 if (curve) {
1188 shape->curve = curve;
1189 curve->ref();
1190 }
1192 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1193 }
1195 void NRArenaShape::setFill(SPPaintServer *server) {
1196 _fill.paint.set(server);
1197 _invalidateCachedFill();
1198 }
1200 void NRArenaShape::setFill(SPColor const &color) {
1201 _fill.paint.set(color);
1202 _invalidateCachedFill();
1203 }
1205 void NRArenaShape::setFillOpacity(double opacity) {
1206 _fill.opacity = opacity;
1207 _invalidateCachedFill();
1208 }
1210 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1211 _fill.rule = rule;
1212 _invalidateCachedFill();
1213 }
1215 void NRArenaShape::setStroke(SPPaintServer *server) {
1216 _stroke.paint.set(server);
1217 _invalidateCachedStroke();
1218 }
1220 void NRArenaShape::setStroke(SPColor const &color) {
1221 _stroke.paint.set(color);
1222 _invalidateCachedStroke();
1223 }
1225 void NRArenaShape::setStrokeOpacity(double opacity) {
1226 _stroke.opacity = opacity;
1227 _invalidateCachedStroke();
1228 }
1230 void NRArenaShape::setStrokeWidth(double width) {
1231 _stroke.width = width;
1232 _invalidateCachedStroke();
1233 }
1235 void NRArenaShape::setMitreLimit(double limit) {
1236 _stroke.mitre_limit = limit;
1237 _invalidateCachedStroke();
1238 }
1240 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1241 _stroke.cap = cap;
1242 _invalidateCachedStroke();
1243 }
1245 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1246 _stroke.join = join;
1247 _invalidateCachedStroke();
1248 }
1250 /** nr_arena_shape_set_style
1251 *
1252 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1253 */
1254 void
1255 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1256 {
1257 g_return_if_fail(shape != NULL);
1258 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1260 if (style) sp_style_ref(style);
1261 if (shape->style) sp_style_unref(shape->style);
1262 shape->style = style;
1264 if ( style->fill.isPaintserver() ) {
1265 shape->setFill(style->getFillPaintServer());
1266 } else if ( style->fill.isColor() ) {
1267 shape->setFill(style->fill.value.color);
1268 } else if ( style->fill.isNone() ) {
1269 shape->setFill(NULL);
1270 } else {
1271 g_assert_not_reached();
1272 }
1273 shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1274 switch (style->fill_rule.computed) {
1275 case SP_WIND_RULE_EVENODD: {
1276 shape->setFillRule(NRArenaShape::EVEN_ODD);
1277 break;
1278 }
1279 case SP_WIND_RULE_NONZERO: {
1280 shape->setFillRule(NRArenaShape::NONZERO);
1281 break;
1282 }
1283 default: {
1284 g_assert_not_reached();
1285 }
1286 }
1288 if ( style->stroke.isPaintserver() ) {
1289 shape->setStroke(style->getStrokePaintServer());
1290 } else if ( style->stroke.isColor() ) {
1291 shape->setStroke(style->stroke.value.color);
1292 } else if ( style->stroke.isNone() ) {
1293 shape->setStroke(NULL);
1294 } else {
1295 g_assert_not_reached();
1296 }
1297 shape->setStrokeWidth(style->stroke_width.computed);
1298 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1299 switch (style->stroke_linecap.computed) {
1300 case SP_STROKE_LINECAP_ROUND: {
1301 shape->setLineCap(NRArenaShape::ROUND_CAP);
1302 break;
1303 }
1304 case SP_STROKE_LINECAP_SQUARE: {
1305 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1306 break;
1307 }
1308 case SP_STROKE_LINECAP_BUTT: {
1309 shape->setLineCap(NRArenaShape::BUTT_CAP);
1310 break;
1311 }
1312 default: {
1313 g_assert_not_reached();
1314 }
1315 }
1316 switch (style->stroke_linejoin.computed) {
1317 case SP_STROKE_LINEJOIN_ROUND: {
1318 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1319 break;
1320 }
1321 case SP_STROKE_LINEJOIN_BEVEL: {
1322 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1323 break;
1324 }
1325 case SP_STROKE_LINEJOIN_MITER: {
1326 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1327 break;
1328 }
1329 default: {
1330 g_assert_not_reached();
1331 }
1332 }
1333 shape->setMitreLimit(style->stroke_miterlimit.value);
1335 //if shape has a filter
1336 if (style->filter.set && style->getFilter()) {
1337 if (!shape->filter) {
1338 int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1339 shape->filter = new Inkscape::Filters::Filter(primitives);
1340 }
1341 sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1342 } else {
1343 //no filter set for this shape
1344 delete shape->filter;
1345 shape->filter = NULL;
1346 }
1348 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1349 }
1351 void
1352 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1353 {
1354 g_return_if_fail(shape != NULL);
1355 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1356 g_return_if_fail(pbox != NULL);
1358 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1359 shape->paintbox = *pbox;
1360 } else {
1361 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1362 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1363 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1364 }
1366 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1367 }
1369 void NRArenaShape::setPaintBox(Geom::Rect const &pbox)
1370 {
1371 paintbox.x0 = pbox.min()[Geom::X];
1372 paintbox.y0 = pbox.min()[Geom::Y];
1373 paintbox.x1 = pbox.max()[Geom::X];
1374 paintbox.y1 = pbox.max()[Geom::Y];
1376 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1377 }
1379 static void
1380 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1381 {
1382 if ( st >= en ) return;
1383 if ( vst < 0 ) vst=0;
1384 if ( vst > 1 ) vst=1;
1385 if ( ven < 0 ) ven=0;
1386 if ( ven > 1 ) ven=1;
1387 float sv=vst;
1388 float dv=ven-vst;
1389 int len=en-st;
1390 unsigned char* d=(unsigned char*)dest.buffer;
1391 d+=(st-dest.startPix);
1392 if ( fabs(dv) < 0.001 ) {
1393 if ( vst > 0.999 ) {
1394 /* Simple copy */
1395 while (len > 0) {
1396 d[0] = 255;
1397 d += 1;
1398 len -= 1;
1399 }
1400 } else {
1401 sv*=256;
1402 unsigned int c0_24=(int)sv;
1403 c0_24&=0xFF;
1404 while (len > 0) {
1405 /* Draw */
1406 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1407 d += 1;
1408 len -= 1;
1409 }
1410 }
1411 } else {
1412 if ( en <= st+1 ) {
1413 sv=0.5*(vst+ven);
1414 sv*=256;
1415 unsigned int c0_24=(int)sv;
1416 c0_24&=0xFF;
1417 /* Draw */
1418 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1419 } else {
1420 dv/=len;
1421 sv+=0.5*dv; // correction trapezoidale
1422 sv*=16777216;
1423 dv*=16777216;
1424 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1425 int s0_24 = static_cast<int>(dv);
1426 while (len > 0) {
1427 unsigned int ca;
1428 /* Draw */
1429 ca = c0_24 >> 16;
1430 if ( ca > 255 ) ca=255;
1431 d[0] = NR_COMPOSEA_111(ca,d[0]);
1432 d += 1;
1433 c0_24 += s0_24;
1434 c0_24 = CLAMP(c0_24, 0, 16777216);
1435 len -= 1;
1436 }
1437 }
1438 }
1439 }
1441 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1442 {
1443 theS->CalcBBox();
1444 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1445 int il,ir,it,ib;
1446 il=(int)floor(l);
1447 ir=(int)ceil(r);
1448 it=(int)floor(t);
1449 ib=(int)ceil(b);
1451 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1452 if ( il < m.area.x0 ) il=m.area.x0;
1453 if ( it < m.area.y0 ) it=m.area.y0;
1454 if ( ir > m.area.x1 ) ir=m.area.x1;
1455 if ( ib > m.area.y1 ) ib=m.area.y1;
1457 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1458 int curPt;
1459 float curY;
1460 theS->BeginQuickRaster(curY, curPt);
1462 FloatLigne *theI = new FloatLigne();
1463 IntLigne *theIL = new IntLigne();
1465 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1467 char *mdata = (char*)m.data.px;
1468 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1469 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1470 for (int y = it; y < ib; y++) {
1471 theI->Reset();
1472 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1473 theI->Flatten();
1474 theIL->Copy(theI);
1476 raster_info dest;
1477 dest.startPix=il;
1478 dest.endPix=ir;
1479 dest.sth=il;
1480 dest.stv=y;
1481 dest.buffer=ligStart;
1482 theIL->Raster(dest, NULL, shape_run_A8_OR);
1483 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1484 }
1485 theS->EndQuickRaster();
1486 delete theI;
1487 delete theIL;
1488 }
1491 /*
1492 Local Variables:
1493 mode:c++
1494 c-file-style:"stroustrup"
1495 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1496 indent-tabs-mode:nil
1497 fill-column:99
1498 End:
1499 */
1500 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :