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