1 #define __NR_ARENA_SHAPE_C__
3 /*
4 * RGBA display list system for inkscape
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
17 #include <display/nr-arena.h>
18 #include <display/nr-arena-shape.h>
19 #include "display/nr-filter.h"
20 #include "display/nr-filter-gaussian.h"
21 #include "display/nr-filter-types.h"
22 #include <libnr/n-art-bpath.h>
23 #include <libnr/nr-path.h>
24 #include <libnr/nr-pixops.h>
25 #include <libnr/nr-matrix-ops.h>
26 #include <libnr/nr-matrix-fns.h>
27 #include <libnr/nr-blit.h>
28 #include <livarot/Path.h>
29 #include <livarot/float-line.h>
30 #include <livarot/int-line.h>
31 #include <style.h>
32 #include "prefs-utils.h"
33 #include "sp-filter.h"
34 #include "sp-gaussian-blur.h"
35 #include "inkscape-cairo.h"
37 #include <cairo.h>
39 //int showRuns=0;
40 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
42 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
43 static void nr_arena_shape_init(NRArenaShape *shape);
44 static void nr_arena_shape_finalize(NRObject *object);
46 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
47 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
48 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
49 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
51 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
52 static unsigned int nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
53 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
54 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
56 static NRArenaItemClass *shape_parent_class;
58 NRType
59 nr_arena_shape_get_type(void)
60 {
61 static NRType type = 0;
62 if (!type) {
63 type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
64 "NRArenaShape",
65 sizeof(NRArenaShapeClass),
66 sizeof(NRArenaShape),
67 (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
68 (void (*)(NRObject *)) nr_arena_shape_init);
69 }
70 return type;
71 }
73 static void
74 nr_arena_shape_class_init(NRArenaShapeClass *klass)
75 {
76 NRObjectClass *object_class;
77 NRArenaItemClass *item_class;
79 object_class = (NRObjectClass *) klass;
80 item_class = (NRArenaItemClass *) klass;
82 shape_parent_class = (NRArenaItemClass *) ((NRObjectClass *) klass)->parent;
84 object_class->finalize = nr_arena_shape_finalize;
85 object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
87 item_class->children = nr_arena_shape_children;
88 item_class->add_child = nr_arena_shape_add_child;
89 item_class->set_child_position = nr_arena_shape_set_child_position;
90 item_class->remove_child = nr_arena_shape_remove_child;
91 item_class->update = nr_arena_shape_update;
92 item_class->render = nr_arena_shape_render;
93 item_class->clip = nr_arena_shape_clip;
94 item_class->pick = nr_arena_shape_pick;
95 }
97 /**
98 * Initializes the arena shape, setting all parameters to null, 0, false,
99 * or other defaults
100 */
101 static void
102 nr_arena_shape_init(NRArenaShape *shape)
103 {
104 shape->curve = NULL;
105 shape->style = NULL;
106 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
107 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
109 nr_matrix_set_identity(&shape->ctm);
110 shape->fill_painter = NULL;
111 shape->stroke_painter = NULL;
112 shape->cached_fill = NULL;
113 shape->cached_stroke = NULL;
114 shape->cached_fpartialy = false;
115 shape->cached_spartialy = false;
116 shape->fill_shp = NULL;
117 shape->stroke_shp = NULL;
119 shape->delayed_shp = false;
121 shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
122 shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
123 nr_matrix_set_identity(&shape->cached_fctm);
124 nr_matrix_set_identity(&shape->cached_sctm);
126 shape->markers = NULL;
127 }
129 static void
130 nr_arena_shape_finalize(NRObject *object)
131 {
132 NRArenaShape *shape = (NRArenaShape *) object;
134 if (shape->fill_shp) delete shape->fill_shp;
135 if (shape->stroke_shp) delete shape->stroke_shp;
136 if (shape->cached_fill) delete shape->cached_fill;
137 if (shape->cached_stroke) delete shape->cached_stroke;
138 if (shape->fill_painter) sp_painter_free(shape->fill_painter);
139 if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
141 if (shape->style) sp_style_unref(shape->style);
142 if (shape->curve) sp_curve_unref(shape->curve);
144 ((NRObjectClass *) shape_parent_class)->finalize(object);
145 }
147 /**
148 * Retrieves the markers from the item
149 */
150 static NRArenaItem *
151 nr_arena_shape_children(NRArenaItem *item)
152 {
153 NRArenaShape *shape = (NRArenaShape *) item;
155 return shape->markers;
156 }
158 /**
159 * Attaches child to item, and if ref is not NULL, sets it and ref->next as
160 * the prev and next items. If ref is NULL, then it sets the item's markers
161 * as the next items.
162 */
163 static void
164 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
165 {
166 NRArenaShape *shape = (NRArenaShape *) item;
168 if (!ref) {
169 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
170 } else {
171 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
172 }
174 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
175 }
177 /**
178 * Removes child from the shape. If there are no prev items in
179 * the child, it sets items' markers to the next item in the child.
180 */
181 static void
182 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
183 {
184 NRArenaShape *shape = (NRArenaShape *) item;
186 if (child->prev) {
187 nr_arena_item_detach(item, child);
188 } else {
189 shape->markers = nr_arena_item_detach(item, child);
190 }
192 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
193 }
195 /**
196 * Detaches child from item, and if there are no previous items in child, it
197 * sets item's markers to the child. It then attaches the child back onto the item.
198 * If ref is null, it sets the markers to be the next item, otherwise it uses
199 * the next/prev items in ref.
200 */
201 static void
202 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
203 {
204 NRArenaShape *shape = (NRArenaShape *) item;
206 if (child->prev) {
207 nr_arena_item_detach(item, child);
208 } else {
209 shape->markers = nr_arena_item_detach(item, child);
210 }
212 if (!ref) {
213 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
214 } else {
215 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
216 }
218 nr_arena_item_request_render(child);
219 }
221 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
222 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
223 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
225 /**
226 * Updates the arena shape 'item' and all of its children, including the markers.
227 */
228 static guint
229 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
230 {
231 NRRect bbox;
233 NRArenaShape *shape = NR_ARENA_SHAPE(item);
235 unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
237 unsigned int newstate;
238 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
239 newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
240 beststate = beststate & newstate;
241 }
243 if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
244 /* We do not have to create rendering structures */
245 shape->ctm = gc->transform;
246 if (state & NR_ARENA_ITEM_STATE_BBOX) {
247 if (shape->curve) {
248 NRBPath bp;
249 /* fixme: */
250 bbox.x0 = bbox.y0 = NR_HUGE;
251 bbox.x1 = bbox.y1 = -NR_HUGE;
252 bp.path = SP_CURVE_BPATH(shape->curve);
253 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
254 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
255 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
256 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
257 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
258 }
259 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
260 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
261 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
262 }
263 }
264 }
265 return (state | item->state);
266 }
268 shape->delayed_shp=true;
269 shape->ctm = gc->transform;
270 bbox.x0 = bbox.y0 = NR_HUGE;
271 bbox.x1 = bbox.y1 = -NR_HUGE;
273 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
275 if (shape->curve) {
276 NRBPath bp;
277 /* fixme: */
278 bbox.x0 = bbox.y0 = NR_HUGE;
279 bbox.x1 = bbox.y1 = -NR_HUGE;
280 bp.path = SP_CURVE_BPATH(shape->curve);
281 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
282 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
283 float width, scale;
284 scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
285 width = MAX(0.125, shape->_stroke.width * scale);
286 if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
287 bbox.x0-=width;
288 bbox.x1+=width;
289 bbox.y0-=width;
290 bbox.y1+=width;
291 }
292 // those pesky miters, now
293 float miterMax=width*shape->_stroke.mitre_limit;
294 if ( miterMax > 0.01 ) {
295 // grunt mode. we should compute the various miters instead (one for each point on the curve)
296 bbox.x0-=miterMax;
297 bbox.x1+=miterMax;
298 bbox.y0-=miterMax;
299 bbox.y1+=miterMax;
300 }
301 }
302 } else {
303 }
304 shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
305 shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
306 shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
307 shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
308 if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
310 /* Release state data */
311 if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
312 /* Concept test */
313 if (shape->fill_shp) {
314 delete shape->fill_shp;
315 shape->fill_shp = NULL;
316 }
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 sp_curve_is_empty(shape->curve) ||
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 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
349 nr_arena_shape_add_bboxes(shape,bbox);
351 shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
352 shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
353 shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
354 shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
355 }
357 if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
359 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
360 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
361 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
362 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
363 nr_arena_request_render_rect(item->arena, &item->bbox);
365 item->render_opacity = TRUE;
366 if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
367 if (gc && gc->parent) {
368 shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
369 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
370 &shape->paintbox);
371 }
372 item->render_opacity = FALSE;
373 }
374 if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
375 if (gc && gc->parent) {
376 shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
377 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
378 &shape->paintbox);
379 }
380 item->render_opacity = FALSE;
381 }
382 if ( item->render_opacity == TRUE
383 && shape->_fill.paint.type() != NRArenaShape::Paint::NONE
384 && shape->_stroke.paint.type() != NRArenaShape::Paint::NONE )
385 {
386 // don't merge item opacity with paint opacity if there is a stroke on the fill
387 item->render_opacity = FALSE;
388 }
390 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
391 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
392 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
393 }
394 }
396 return NR_ARENA_ITEM_STATE_ALL;
397 }
399 int matrix_is_isometry(NR::Matrix p) {
400 NR::Matrix tp;
401 // transposition
402 tp[0]=p[0];
403 tp[1]=p[2];
404 tp[2]=p[1];
405 tp[3]=p[3];
406 for (int i = 4; i < 6; i++) // shut valgrind up :)
407 tp[i] = p[i] = 0;
408 NR::Matrix isom = tp*p; // A^T * A = adjunct?
409 // Is the adjunct nearly an identity function?
410 if (isom.is_translation(0.01)) {
411 // the transformation is an isometry -> no need to recompute
412 // the uncrossed polygon
413 if ( p.det() < 0 )
414 return -1;
415 else
416 return 1;
417 }
418 return 0;
419 }
421 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
422 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
423 }
425 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
426 void
427 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
428 {
429 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
430 ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
431 if (TRUE || !shape->fill_shp) {
432 NR::Matrix cached_to_new = NR::identity();
433 int isometry = 0;
434 if ( shape->cached_fill ) {
435 if (shape->cached_fctm == gc->transform) {
436 isometry = 2; // identity
437 } else {
438 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
439 isometry = matrix_is_isometry(cached_to_new);
440 }
441 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
442 isometry = 0;
443 }
444 if ( isometry == 0 ) {
445 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
446 shape->cached_fill->Reset();
448 Path* thePath=new Path;
449 Shape* theShape=new Shape;
450 {
451 NR::Matrix tempMat(gc->transform);
452 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
453 }
455 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
456 thePath->Convert(1.0);
457 shape->cached_fpartialy = false;
458 } else {
459 thePath->Convert(area, 1.0);
460 shape->cached_fpartialy = true;
461 }
463 thePath->Fill(theShape, 0);
465 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
466 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
467 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
468 //shape->cached_fill->Copy(theShape);
469 } else {
470 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
471 }
472 shape->cached_fctm=gc->transform;
473 shape->cached_farea = *area;
474 delete theShape;
475 delete thePath;
476 if ( shape->fill_shp == NULL )
477 shape->fill_shp = new Shape;
479 shape->fill_shp->Copy(shape->cached_fill);
481 } else if ( 2 == isometry ) {
482 if ( shape->fill_shp == NULL ) {
483 shape->fill_shp = new Shape;
484 shape->fill_shp->Copy(shape->cached_fill);
485 }
486 } else {
488 if ( shape->fill_shp == NULL )
489 shape->fill_shp = new Shape;
491 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
492 shape->cached_fill->numberOfEdges());
493 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
494 shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
495 if ( isometry == 1 ) {
496 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
497 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
498 shape->cached_fill->getEdge(i).en);
499 } else if ( isometry == -1 ) { // need to flip poly.
500 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
501 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
502 shape->cached_fill->getEdge(i).st);
503 }
504 shape->fill_shp->ForceToPolygon();
505 shape->fill_shp->needPointsSorting();
506 shape->fill_shp->needEdgesSorting();
507 }
508 shape->delayed_shp |= shape->cached_fpartialy;
509 }
510 }
511 }
513 void
514 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
515 {
516 SPStyle* style = shape->style;
518 float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
520 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
522 if (outline) {
523 // cairo does not need the livarot path for rendering... but unfortunately it's still used for picking
524 // FIXME: switch picking to using cairo_in_stroke?
525 //return;
526 }
528 // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes
529 // (since it will only be used for picking, not for rendering)
531 if (outline ||
532 ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
533 ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
535 float style_width = MAX(0.125, shape->_stroke.width * scale);
536 float width;
537 if (outline) {
538 width = 0.5; // 1 pixel wide, independent of zoom
539 } else {
540 width = style_width;
541 }
543 NR::Matrix cached_to_new = NR::identity();
545 int isometry = 0;
546 if ( shape->cached_stroke ) {
547 if (shape->cached_sctm == gc->transform) {
548 isometry = 2; // identity
549 } else {
550 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
551 isometry = matrix_is_isometry(cached_to_new);
552 }
553 if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
554 isometry = 0;
555 if (0 != isometry && width != shape->cached_width) {
556 // if this happens without setting style, we have just switched to outline or back
557 isometry = 0;
558 }
559 }
561 if ( isometry == 0 ) {
562 if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
563 shape->cached_stroke->Reset();
564 Path* thePath = new Path;
565 Shape* theShape = new Shape;
566 {
567 NR::Matrix tempMat(gc->transform);
568 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
569 }
571 // add some padding to the rendering area, so clipped path does not go into a render area
572 NRRectL padded_area = *area;
573 padded_area.x0 -= (NR::ICoord)width;
574 padded_area.x1 += (NR::ICoord)width;
575 padded_area.y0 -= (NR::ICoord)width;
576 padded_area.y1 += (NR::ICoord)width;
577 if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
578 thePath->Convert((outline) ? 4.0 : 1.0);
579 shape->cached_spartialy = false;
580 }
581 else {
582 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
583 shape->cached_spartialy = true;
584 }
586 if (style->stroke_dash.n_dash && !outline) {
587 thePath->DashPolylineFromStyle(style, scale, 1.0);
588 }
590 ButtType butt=butt_straight;
591 switch (shape->_stroke.cap) {
592 case NRArenaShape::BUTT_CAP:
593 butt = butt_straight;
594 break;
595 case NRArenaShape::ROUND_CAP:
596 butt = butt_round;
597 break;
598 case NRArenaShape::SQUARE_CAP:
599 butt = butt_square;
600 break;
601 }
602 JoinType join=join_straight;
603 switch (shape->_stroke.join) {
604 case NRArenaShape::MITRE_JOIN:
605 join = join_pointy;
606 break;
607 case NRArenaShape::ROUND_JOIN:
608 join = join_round;
609 break;
610 case NRArenaShape::BEVEL_JOIN:
611 join = join_straight;
612 break;
613 }
615 if (outline) {
616 butt = butt_straight;
617 join = join_straight;
618 }
620 thePath->Stroke(theShape, false, 0.5*width, join, butt,
621 0.5*width*shape->_stroke.mitre_limit);
624 if (outline) {
625 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
626 shape->cached_stroke->Copy(theShape);
627 } else {
628 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
629 }
631 shape->cached_width = width;
633 shape->cached_sctm=gc->transform;
634 shape->cached_sarea = *area;
635 delete thePath;
636 delete theShape;
637 if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
639 shape->stroke_shp->Copy(shape->cached_stroke);
641 } else if ( 2 == isometry ) {
642 if ( shape->stroke_shp == NULL ) {
643 shape->stroke_shp=new Shape;
644 shape->stroke_shp->Copy(shape->cached_stroke);
645 }
646 } else {
647 if ( shape->stroke_shp == NULL )
648 shape->stroke_shp=new Shape;
649 shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
650 for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
651 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
652 if ( isometry == 1 ) {
653 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
654 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
655 shape->cached_stroke->getEdge(i).en);
656 } else if ( isometry == -1 ) {
657 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
658 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
659 shape->cached_stroke->getEdge(i).st);
660 }
661 shape->stroke_shp->ForceToPolygon();
662 shape->stroke_shp->needPointsSorting();
663 shape->stroke_shp->needEdgesSorting();
664 }
665 shape->delayed_shp |= shape->cached_spartialy;
666 }
667 }
670 void
671 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
672 {
673 if ( shape->stroke_shp ) {
674 shape->stroke_shp->CalcBBox();
675 shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
676 shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
677 shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
678 shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
679 if ( bbox.x0 >= bbox.x1 ) {
680 if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
681 bbox.x0=shape->stroke_shp->leftX;
682 bbox.x1=shape->stroke_shp->rightX;
683 }
684 } else {
685 if ( shape->stroke_shp->leftX < bbox.x0 )
686 bbox.x0=shape->stroke_shp->leftX;
687 if ( shape->stroke_shp->rightX > bbox.x1 )
688 bbox.x1=shape->stroke_shp->rightX;
689 }
690 if ( bbox.y0 >= bbox.y1 ) {
691 if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
692 bbox.y0=shape->stroke_shp->topY;
693 bbox.y1=shape->stroke_shp->bottomY;
694 }
695 } else {
696 if ( shape->stroke_shp->topY < bbox.y0 )
697 bbox.y0=shape->stroke_shp->topY;
698 if ( shape->stroke_shp->bottomY > bbox.y1 )
699 bbox.y1=shape->stroke_shp->bottomY;
700 }
701 }
702 if ( shape->fill_shp ) {
703 shape->fill_shp->CalcBBox();
704 shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
705 shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
706 shape->fill_shp->topY=floor(shape->fill_shp->topY);
707 shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
708 if ( bbox.x0 >= bbox.x1 ) {
709 if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
710 bbox.x0=shape->fill_shp->leftX;
711 bbox.x1=shape->fill_shp->rightX;
712 }
713 } else {
714 if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
715 if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
716 }
717 if ( bbox.y0 >= bbox.y1 ) {
718 if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
719 bbox.y0=shape->fill_shp->topY;
720 bbox.y1=shape->fill_shp->bottomY;
721 }
722 } else {
723 if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
724 if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
725 }
726 }
727 }
729 // cairo outline rendering:
730 static unsigned int
731 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Point shift)
732 {
733 NRArenaShape *shape = NR_ARENA_SHAPE(item);
735 if (!ct)
736 return item->state;
738 guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
739 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
740 // by setting color channels in the "wrong" order
741 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));
743 cairo_set_line_width(ct, 0.5);
744 cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
745 cairo_new_path(ct);
747 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), shift);
749 cairo_stroke(ct);
751 return item->state;
752 }
754 // cairo stroke rendering (flat color only so far!):
755 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
756 // (need to switch them to premul before this can be enabled)
757 void
758 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
759 {
760 NRArenaShape *shape = NR_ARENA_SHAPE(item);
761 SPStyle const *style = shape->style;
763 float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
765 if (fabs(shape->_stroke.width * scale) < 0.01)
766 return;
768 cairo_t *ct = nr_create_cairo_context (area, pb);
770 if (!ct)
771 return;
773 guint32 rgba;
774 if ( item->render_opacity ) {
775 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
776 shape->_stroke.opacity *
777 SP_SCALE24_TO_FLOAT(style->opacity.value));
778 } else {
779 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
780 shape->_stroke.opacity);
781 }
783 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
784 // by setting color channels in the "wrong" order
785 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));
787 float style_width = MAX(0.125, shape->_stroke.width * scale);
788 cairo_set_line_width(ct, style_width);
790 switch (shape->_stroke.cap) {
791 case NRArenaShape::BUTT_CAP:
792 cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
793 break;
794 case NRArenaShape::ROUND_CAP:
795 cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
796 break;
797 case NRArenaShape::SQUARE_CAP:
798 cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
799 break;
800 }
801 switch (shape->_stroke.join) {
802 case NRArenaShape::MITRE_JOIN:
803 cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
804 break;
805 case NRArenaShape::ROUND_JOIN:
806 cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
807 break;
808 case NRArenaShape::BEVEL_JOIN:
809 cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
810 break;
811 }
813 cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
815 if (style->stroke_dash.n_dash) {
816 NRVpathDash dash;
817 dash.offset = style->stroke_dash.offset * scale;
818 dash.n_dash = style->stroke_dash.n_dash;
819 dash.dash = g_new(double, dash.n_dash);
820 for (int i = 0; i < dash.n_dash; i++) {
821 dash.dash[i] = style->stroke_dash.dash[i] * scale;
822 }
823 cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
824 g_free(dash.dash);
825 }
827 cairo_set_tolerance(ct, 0.1);
828 cairo_new_path(ct);
830 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
832 cairo_stroke(ct);
834 cairo_surface_t *cst = cairo_get_target(ct);
835 cairo_destroy (ct);
836 cairo_surface_finish (cst);
837 cairo_surface_destroy (cst);
839 pb->empty = FALSE;
840 }
843 /**
844 * Renders the item. Markers are just composed into the parent buffer.
845 */
846 static unsigned int
847 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
848 {
849 NRArenaShape *shape = NR_ARENA_SHAPE(item);
851 if (!shape->curve) return item->state;
852 if (!shape->style) return item->state;
854 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
856 if (outline) {
857 pb->empty = FALSE;
858 return cairo_arena_shape_render_outline (ct, item, NR::Point(pb->area.x0, pb->area.y0));
859 }
861 if ( shape->delayed_shp ) {
862 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
863 NRGC tempGC(NULL);
864 tempGC.transform=shape->ctm;
865 shape->delayed_shp = false;
866 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
867 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
868 /* NRRect bbox;
869 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
870 nr_arena_shape_add_bboxes(shape,bbox);
871 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
872 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
873 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
874 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
875 shape->approx_bbox=item->bbox;*/
876 } else {
877 return item->state;
878 }
879 }
881 SPStyle const *style = shape->style;
882 if (shape->fill_shp) {
883 NRPixBlock m;
884 guint32 rgba;
886 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
888 // if memory allocation failed, abort render
889 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
890 nr_pixblock_release (&m);
891 return (item->state);
892 }
894 m.visible_area = pb->visible_area;
895 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
896 m.empty = FALSE;
898 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
899 // do not render fill in any way
900 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
901 if ( item->render_opacity ) {
902 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
903 shape->_fill.opacity *
904 SP_SCALE24_TO_FLOAT(style->opacity.value));
905 } else {
906 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
907 shape->_fill.opacity);
908 }
909 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
910 pb->empty = FALSE;
911 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
912 if (shape->fill_painter) {
913 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
914 }
915 }
917 nr_pixblock_release(&m);
918 }
920 if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
922 // cairo_arena_shape_render_stroke(item, area, pb);
924 guint32 rgba;
925 NRPixBlock m;
927 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
929 // if memory allocation failed, abort render
930 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
931 nr_pixblock_release (&m);
932 return (item->state);
933 }
935 m.visible_area = pb->visible_area;
936 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
937 m.empty = FALSE;
939 if ( item->render_opacity ) {
940 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
941 shape->_stroke.opacity *
942 SP_SCALE24_TO_FLOAT(style->opacity.value));
943 } else {
944 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
945 shape->_stroke.opacity);
946 }
947 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
948 pb->empty = FALSE;
950 nr_pixblock_release(&m);
952 } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
954 NRPixBlock m;
956 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
958 // if memory allocation failed, abort render
959 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
960 nr_pixblock_release (&m);
961 return (item->state);
962 }
964 m.visible_area = pb->visible_area;
965 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
966 m.empty = FALSE;
968 if (shape->stroke_painter) {
969 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
970 }
972 nr_pixblock_release(&m);
973 }
975 /* Just compose children into parent buffer */
976 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
977 unsigned int ret;
978 ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
979 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
980 }
982 return item->state;
983 }
986 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
987 // reenable this when the bug is fixed and remove the rest of this function
988 static guint
989 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
990 {
991 NRArenaShape *shape = NR_ARENA_SHAPE(item);
992 if (!shape->curve) return item->state;
994 cairo_t *ct = nr_create_cairo_context (area, pb);
996 if (!ct)
997 return item->state;
999 cairo_set_source_rgba(ct, 0, 0, 0, 1);
1001 cairo_new_path(ct);
1003 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Point(area->x0, area->y0));
1005 cairo_fill(ct);
1007 cairo_surface_t *cst = cairo_get_target(ct);
1008 cairo_destroy (ct);
1009 cairo_surface_finish (cst);
1010 cairo_surface_destroy (cst);
1012 pb->empty = FALSE;
1014 return item->state;
1015 }
1018 static guint
1019 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1020 {
1021 //return cairo_arena_shape_clip(item, area, pb);
1023 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1024 if (!shape->curve) return item->state;
1026 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1027 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1028 NRGC tempGC(NULL);
1029 tempGC.transform=shape->ctm;
1030 shape->delayed_shp = false;
1031 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1032 } else {
1033 return item->state;
1034 }
1035 }
1037 if ( shape->fill_shp ) {
1038 NRPixBlock m;
1040 /* fixme: We can OR in one step (Lauris) */
1041 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1043 // if memory allocation failed, abort
1044 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1045 nr_pixblock_release (&m);
1046 return (item->state);
1047 }
1049 m.visible_area = pb->visible_area;
1050 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1052 for (int y = area->y0; y < area->y1; y++) {
1053 unsigned char *s, *d;
1054 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1055 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1056 for (int x = area->x0; x < area->x1; x++) {
1057 *d = NR_COMPOSEA_111(*s, *d);
1058 d ++;
1059 s ++;
1060 }
1061 }
1062 nr_pixblock_release(&m);
1063 pb->empty = FALSE;
1064 }
1066 return item->state;
1067 }
1069 static NRArenaItem *
1070 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1071 {
1072 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1074 if (!shape->curve) return NULL;
1075 if (!shape->style) return NULL;
1076 if ( shape->delayed_shp ) {
1077 NRRectL area, updateArea;
1078 area.x0=(int)floor(p[NR::X]);
1079 area.x1=(int)ceil(p[NR::X]);
1080 area.y0=(int)floor(p[NR::Y]);
1081 area.y1=(int)ceil(p[NR::Y]);
1082 int idelta = (int)ceil(delta) + 1;
1083 // njh: inset rect
1084 area.x0-=idelta;
1085 area.x1+=idelta;
1086 area.y0-=idelta;
1087 area.y1+=idelta;
1088 if ( nr_rect_l_test_intersect(&area, &item->bbox) ) {
1089 NRGC tempGC(NULL);
1090 tempGC.transform=shape->ctm;
1091 updateArea = item->bbox;
1092 if (shape->cached_stroke)
1093 nr_rect_l_intersect (&updateArea, &updateArea, &shape->cached_sarea);
1095 shape->delayed_shp = false;
1096 nr_arena_shape_update_stroke(shape, &tempGC, &updateArea);
1097 nr_arena_shape_update_fill(shape, &tempGC, &updateArea);
1098 /* NRRect bbox;
1099 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
1100 nr_arena_shape_add_bboxes(shape,bbox);
1101 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
1102 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
1103 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
1104 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
1105 shape->approx_bbox=item->bbox;*/
1106 }
1107 }
1109 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1111 if (item->state & NR_ARENA_ITEM_STATE_RENDER) {
1112 if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1113 if (shape->fill_shp->PtWinding(p) > 0 ) return item;
1114 }
1115 if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1116 if (shape->stroke_shp->PtWinding(p) > 0 ) return item;
1117 }
1118 if (delta > 1e-3) {
1119 if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1120 if (distanceLessThanOrEqual(shape->fill_shp, p, delta)) return item;
1121 }
1122 if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1123 if (distanceLessThanOrEqual(shape->stroke_shp, p, delta)) return item;
1124 }
1125 }
1126 } else {
1127 NRBPath bp;
1128 bp.path = SP_CURVE_BPATH(shape->curve);
1129 double dist = NR_HUGE;
1130 int wind = 0;
1131 nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, &wind, &dist, NR_EPSILON);
1132 if (shape->_fill.paint.type() != NRArenaShape::Paint::NONE) {
1133 if (!shape->style->fill_rule.computed) {
1134 if (wind != 0) return item;
1135 } else {
1136 if (wind & 0x1) return item;
1137 }
1138 }
1139 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1140 /* fixme: We do not take stroke width into account here (Lauris) */
1141 if (dist < delta) return item;
1142 }
1143 }
1145 return NULL;
1146 }
1148 /**
1149 *
1150 * Requests a render of the shape, then if the shape is already a curve it
1151 * unrefs the old curve; if the new curve is valid it creates a copy of the
1152 * curve and adds it to the shape. Finally, it requests an update of the
1153 * arena for the shape.
1154 */
1155 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1156 {
1157 g_return_if_fail(shape != NULL);
1158 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1160 if ( justTrans == false ) {
1161 // dirty cached versions
1162 if ( shape->cached_fill ) {
1163 delete shape->cached_fill;
1164 shape->cached_fill=NULL;
1165 }
1166 if ( shape->cached_stroke ) {
1167 delete shape->cached_stroke;
1168 shape->cached_stroke=NULL;
1169 }
1170 }
1172 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1174 if (shape->curve) {
1175 sp_curve_unref(shape->curve);
1176 shape->curve = NULL;
1177 }
1179 if (curve) {
1180 shape->curve = curve;
1181 sp_curve_ref(curve);
1182 }
1184 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1185 }
1187 void NRArenaShape::setFill(SPPaintServer *server) {
1188 _fill.paint.set(server);
1189 _invalidateCachedFill();
1190 }
1192 void NRArenaShape::setFill(SPColor const &color) {
1193 _fill.paint.set(color);
1194 _invalidateCachedFill();
1195 }
1197 void NRArenaShape::setFillOpacity(double opacity) {
1198 _fill.opacity = opacity;
1199 _invalidateCachedFill();
1200 }
1202 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1203 _fill.rule = rule;
1204 _invalidateCachedFill();
1205 }
1207 void NRArenaShape::setStroke(SPPaintServer *server) {
1208 _stroke.paint.set(server);
1209 _invalidateCachedStroke();
1210 }
1212 void NRArenaShape::setStroke(SPColor const &color) {
1213 _stroke.paint.set(color);
1214 _invalidateCachedStroke();
1215 }
1217 void NRArenaShape::setStrokeOpacity(double opacity) {
1218 _stroke.opacity = opacity;
1219 _invalidateCachedStroke();
1220 }
1222 void NRArenaShape::setStrokeWidth(double width) {
1223 _stroke.width = width;
1224 _invalidateCachedStroke();
1225 }
1227 void NRArenaShape::setMitreLimit(double limit) {
1228 _stroke.mitre_limit = limit;
1229 _invalidateCachedStroke();
1230 }
1232 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1233 _stroke.cap = cap;
1234 _invalidateCachedStroke();
1235 }
1237 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1238 _stroke.join = join;
1239 _invalidateCachedStroke();
1240 }
1242 /** nr_arena_shape_set_style
1243 *
1244 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1245 */
1246 void
1247 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1248 {
1249 g_return_if_fail(shape != NULL);
1250 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1252 if (style) sp_style_ref(style);
1253 if (shape->style) sp_style_unref(shape->style);
1254 shape->style = style;
1256 switch (style->fill.type) {
1257 case SP_PAINT_TYPE_NONE: {
1258 shape->setFill(NULL);
1259 break;
1260 }
1261 case SP_PAINT_TYPE_COLOR: {
1262 shape->setFill(style->fill.value.color);
1263 break;
1264 }
1265 case SP_PAINT_TYPE_PAINTSERVER: {
1266 shape->setFill(style->fill.value.paint.server);
1267 break;
1268 }
1269 default: {
1270 g_assert_not_reached();
1271 }
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 switch (style->stroke.type) {
1289 case SP_PAINT_TYPE_NONE: {
1290 shape->setStroke(NULL);
1291 break;
1292 }
1293 case SP_PAINT_TYPE_COLOR: {
1294 shape->setStroke(style->stroke.value.color);
1295 break;
1296 }
1297 case SP_PAINT_TYPE_PAINTSERVER: {
1298 shape->setStroke(style->stroke.value.paint.server);
1299 break;
1300 }
1301 default: {
1302 g_assert_not_reached();
1303 }
1304 }
1305 shape->setStrokeWidth(style->stroke_width.computed);
1306 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1307 switch (style->stroke_linecap.computed) {
1308 case SP_STROKE_LINECAP_ROUND: {
1309 shape->setLineCap(NRArenaShape::ROUND_CAP);
1310 break;
1311 }
1312 case SP_STROKE_LINECAP_SQUARE: {
1313 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1314 break;
1315 }
1316 case SP_STROKE_LINECAP_BUTT: {
1317 shape->setLineCap(NRArenaShape::BUTT_CAP);
1318 break;
1319 }
1320 default: {
1321 g_assert_not_reached();
1322 }
1323 }
1324 switch (style->stroke_linejoin.computed) {
1325 case SP_STROKE_LINEJOIN_ROUND: {
1326 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1327 break;
1328 }
1329 case SP_STROKE_LINEJOIN_BEVEL: {
1330 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1331 break;
1332 }
1333 case SP_STROKE_LINEJOIN_MITER: {
1334 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1335 break;
1336 }
1337 default: {
1338 g_assert_not_reached();
1339 }
1340 }
1341 shape->setMitreLimit(style->stroke_miterlimit.value);
1343 //if shape has a filter
1344 if (style->filter.set && style->filter.filter)
1345 {
1346 shape->filter = new NR::Filter();
1347 shape->filter->set_x(style->filter.filter->x);
1348 shape->filter->set_y(style->filter.filter->y);
1349 shape->filter->set_width(style->filter.filter->width);
1350 shape->filter->set_height(style->filter.filter->height);
1352 //go through all SP filter primitives
1353 for(int i=0; i<style->filter.filter->_primitive_count; i++)
1354 {
1355 SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1356 //if primitive is gaussianblur
1357 if(SP_IS_GAUSSIANBLUR(primitive)) {
1358 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1359 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1360 float num = spblur->stdDeviation.getNumber();
1361 if( num>=0.0 )
1362 {
1363 float optnum = spblur->stdDeviation.getOptNumber();
1364 if( optnum>=0.0 )
1365 gaussian->set_deviation((double) num, (double) optnum);
1366 else
1367 gaussian->set_deviation((double) num);
1368 }
1369 }
1370 }
1371 }
1372 else
1373 {
1374 //no filter set for this shape
1375 shape->filter = NULL;
1376 }
1378 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1379 }
1381 void
1382 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1383 {
1384 g_return_if_fail(shape != NULL);
1385 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1386 g_return_if_fail(pbox != NULL);
1388 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1389 shape->paintbox = *pbox;
1390 } else {
1391 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1392 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1393 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1394 }
1396 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1397 }
1399 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1400 {
1401 paintbox.x0 = pbox.min()[NR::X];
1402 paintbox.y0 = pbox.min()[NR::Y];
1403 paintbox.x1 = pbox.max()[NR::X];
1404 paintbox.y1 = pbox.max()[NR::Y];
1406 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1407 }
1409 static void
1410 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1411 {
1412 if ( st >= en ) return;
1413 if ( vst < 0 ) vst=0;
1414 if ( vst > 1 ) vst=1;
1415 if ( ven < 0 ) ven=0;
1416 if ( ven > 1 ) ven=1;
1417 float sv=vst;
1418 float dv=ven-vst;
1419 int len=en-st;
1420 unsigned char* d=(unsigned char*)dest.buffer;
1421 d+=(st-dest.startPix);
1422 if ( fabs(dv) < 0.001 ) {
1423 if ( vst > 0.999 ) {
1424 /* Simple copy */
1425 while (len > 0) {
1426 d[0] = 255;
1427 d += 1;
1428 len -= 1;
1429 }
1430 } else {
1431 sv*=256;
1432 unsigned int c0_24=(int)sv;
1433 c0_24&=0xFF;
1434 while (len > 0) {
1435 /* Draw */
1436 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1437 d += 1;
1438 len -= 1;
1439 }
1440 }
1441 } else {
1442 if ( en <= st+1 ) {
1443 sv=0.5*(vst+ven);
1444 sv*=256;
1445 unsigned int c0_24=(int)sv;
1446 c0_24&=0xFF;
1447 /* Draw */
1448 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1449 } else {
1450 dv/=len;
1451 sv+=0.5*dv; // correction trapezoidale
1452 sv*=16777216;
1453 dv*=16777216;
1454 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1455 int s0_24 = static_cast<int>(dv);
1456 while (len > 0) {
1457 unsigned int ca;
1458 /* Draw */
1459 ca = c0_24 >> 16;
1460 if ( ca > 255 ) ca=255;
1461 d[0] = NR_COMPOSEA_111(ca,d[0]);
1462 d += 1;
1463 c0_24 += s0_24;
1464 c0_24 = CLAMP(c0_24, 0, 16777216);
1465 len -= 1;
1466 }
1467 }
1468 }
1469 }
1471 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1472 {
1473 theS->CalcBBox();
1474 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1475 int il,ir,it,ib;
1476 il=(int)floor(l);
1477 ir=(int)ceil(r);
1478 it=(int)floor(t);
1479 ib=(int)ceil(b);
1481 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1482 if ( il < m.area.x0 ) il=m.area.x0;
1483 if ( it < m.area.y0 ) it=m.area.y0;
1484 if ( ir > m.area.x1 ) ir=m.area.x1;
1485 if ( ib > m.area.y1 ) ib=m.area.y1;
1487 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1488 int curPt;
1489 float curY;
1490 theS->BeginQuickRaster(curY, curPt);
1492 FloatLigne *theI = new FloatLigne();
1493 IntLigne *theIL = new IntLigne();
1495 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1497 char *mdata = (char*)m.data.px;
1498 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1499 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1500 for (int y = it; y < ib; y++) {
1501 theI->Reset();
1502 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1503 theI->Flatten();
1504 theIL->Copy(theI);
1506 raster_info dest;
1507 dest.startPix=il;
1508 dest.endPix=ir;
1509 dest.sth=il;
1510 dest.stv=y;
1511 dest.buffer=ligStart;
1512 theIL->Raster(dest, NULL, shape_run_A8_OR);
1513 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1514 }
1515 theS->EndQuickRaster();
1516 delete theI;
1517 delete theIL;
1518 }
1521 /*
1522 Local Variables:
1523 mode:c++
1524 c-file-style:"stroustrup"
1525 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1526 indent-tabs-mode:nil
1527 fill-column:99
1528 End:
1529 */
1530 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :