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 ( (shape->_fill.paint.type() != NRArenaShape::Paint::NONE &&
383 shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
384 || (shape->markers)
385 )
386 {
387 // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
388 item->render_opacity = FALSE;
389 }
391 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
392 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
393 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
394 }
395 }
397 return NR_ARENA_ITEM_STATE_ALL;
398 }
400 int matrix_is_isometry(NR::Matrix p) {
401 NR::Matrix tp;
402 // transposition
403 tp[0]=p[0];
404 tp[1]=p[2];
405 tp[2]=p[1];
406 tp[3]=p[3];
407 for (int i = 4; i < 6; i++) // shut valgrind up :)
408 tp[i] = p[i] = 0;
409 NR::Matrix isom = tp*p; // A^T * A = adjunct?
410 // Is the adjunct nearly an identity function?
411 if (isom.is_translation(0.01)) {
412 // the transformation is an isometry -> no need to recompute
413 // the uncrossed polygon
414 if ( p.det() < 0 )
415 return -1;
416 else
417 return 1;
418 }
419 return 0;
420 }
422 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
423 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
424 }
426 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
427 void
428 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
429 {
430 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
431 ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
432 if (TRUE || !shape->fill_shp) {
433 NR::Matrix cached_to_new = NR::identity();
434 int isometry = 0;
435 if ( shape->cached_fill ) {
436 if (shape->cached_fctm == gc->transform) {
437 isometry = 2; // identity
438 } else {
439 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
440 isometry = matrix_is_isometry(cached_to_new);
441 }
442 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
443 isometry = 0;
444 }
445 if ( isometry == 0 ) {
446 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
447 shape->cached_fill->Reset();
449 Path* thePath=new Path;
450 Shape* theShape=new Shape;
451 {
452 NR::Matrix tempMat(gc->transform);
453 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
454 }
456 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
457 thePath->Convert(1.0);
458 shape->cached_fpartialy = false;
459 } else {
460 thePath->Convert(area, 1.0);
461 shape->cached_fpartialy = true;
462 }
464 thePath->Fill(theShape, 0);
466 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
467 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
468 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
469 //shape->cached_fill->Copy(theShape);
470 } else {
471 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
472 }
473 shape->cached_fctm=gc->transform;
474 shape->cached_farea = *area;
475 delete theShape;
476 delete thePath;
477 if ( shape->fill_shp == NULL )
478 shape->fill_shp = new Shape;
480 shape->fill_shp->Copy(shape->cached_fill);
482 } else if ( 2 == isometry ) {
483 if ( shape->fill_shp == NULL ) {
484 shape->fill_shp = new Shape;
485 shape->fill_shp->Copy(shape->cached_fill);
486 }
487 } else {
489 if ( shape->fill_shp == NULL )
490 shape->fill_shp = new Shape;
492 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
493 shape->cached_fill->numberOfEdges());
494 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
495 shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
496 if ( isometry == 1 ) {
497 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
498 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
499 shape->cached_fill->getEdge(i).en);
500 } else if ( isometry == -1 ) { // need to flip poly.
501 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
502 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
503 shape->cached_fill->getEdge(i).st);
504 }
505 shape->fill_shp->ForceToPolygon();
506 shape->fill_shp->needPointsSorting();
507 shape->fill_shp->needEdgesSorting();
508 }
509 shape->delayed_shp |= shape->cached_fpartialy;
510 }
511 }
512 }
514 void
515 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
516 {
517 SPStyle* style = shape->style;
519 float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
521 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
523 if (outline) {
524 // cairo does not need the livarot path for rendering
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::Rect area)
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), area, true, 0);
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::Rect(area), true, style_width);
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) { // cairo outline rendering
858 pb->empty = FALSE;
859 unsigned int ret = cairo_arena_shape_render_outline (ct, item, NR::Rect(&pb->area));
860 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
862 } else {
864 if ( shape->delayed_shp ) {
865 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
866 NRGC tempGC(NULL);
867 tempGC.transform=shape->ctm;
868 shape->delayed_shp = false;
869 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
870 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
871 /* NRRect bbox;
872 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
873 nr_arena_shape_add_bboxes(shape,bbox);
874 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
875 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
876 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
877 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
878 shape->approx_bbox=item->bbox;*/
879 } else {
880 return item->state;
881 }
882 }
884 SPStyle const *style = shape->style;
885 if (shape->fill_shp) {
886 NRPixBlock m;
887 guint32 rgba;
889 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
891 // if memory allocation failed, abort render
892 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
893 nr_pixblock_release (&m);
894 return (item->state);
895 }
897 m.visible_area = pb->visible_area;
898 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
899 m.empty = FALSE;
901 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
902 // do not render fill in any way
903 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
904 if ( item->render_opacity ) {
905 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
906 shape->_fill.opacity *
907 SP_SCALE24_TO_FLOAT(style->opacity.value));
908 } else {
909 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
910 shape->_fill.opacity);
911 }
912 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
913 pb->empty = FALSE;
914 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
915 if (shape->fill_painter) {
916 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
917 }
918 }
920 nr_pixblock_release(&m);
921 }
923 if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
925 // cairo_arena_shape_render_stroke(item, area, pb);
927 guint32 rgba;
928 NRPixBlock m;
930 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
932 // if memory allocation failed, abort render
933 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
934 nr_pixblock_release (&m);
935 return (item->state);
936 }
938 m.visible_area = pb->visible_area;
939 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
940 m.empty = FALSE;
942 if ( item->render_opacity ) {
943 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
944 shape->_stroke.opacity *
945 SP_SCALE24_TO_FLOAT(style->opacity.value));
946 } else {
947 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
948 shape->_stroke.opacity);
949 }
950 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
951 pb->empty = FALSE;
953 nr_pixblock_release(&m);
955 } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
957 NRPixBlock m;
959 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
961 // if memory allocation failed, abort render
962 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
963 nr_pixblock_release (&m);
964 return (item->state);
965 }
967 m.visible_area = pb->visible_area;
968 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
969 m.empty = FALSE;
971 if (shape->stroke_painter) {
972 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
973 }
975 nr_pixblock_release(&m);
976 }
978 } // non-cairo non-outline branch
980 /* Render markers into parent buffer */
981 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
982 unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
983 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
984 }
986 return item->state;
987 }
990 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
991 // reenable this when the bug is fixed and remove the rest of this function
992 static guint
993 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
994 {
995 NRArenaShape *shape = NR_ARENA_SHAPE(item);
996 if (!shape->curve) return item->state;
998 cairo_t *ct = nr_create_cairo_context (area, pb);
1000 if (!ct)
1001 return item->state;
1003 cairo_set_source_rgba(ct, 0, 0, 0, 1);
1005 cairo_new_path(ct);
1007 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), NR::Rect(area), false, 0);
1009 cairo_fill(ct);
1011 cairo_surface_t *cst = cairo_get_target(ct);
1012 cairo_destroy (ct);
1013 cairo_surface_finish (cst);
1014 cairo_surface_destroy (cst);
1016 pb->empty = FALSE;
1018 return item->state;
1019 }
1022 static guint
1023 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1024 {
1025 //return cairo_arena_shape_clip(item, area, pb);
1027 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1028 if (!shape->curve) return item->state;
1030 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1031 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1032 NRGC tempGC(NULL);
1033 tempGC.transform=shape->ctm;
1034 shape->delayed_shp = false;
1035 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1036 } else {
1037 return item->state;
1038 }
1039 }
1041 if ( shape->fill_shp ) {
1042 NRPixBlock m;
1044 /* fixme: We can OR in one step (Lauris) */
1045 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1047 // if memory allocation failed, abort
1048 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1049 nr_pixblock_release (&m);
1050 return (item->state);
1051 }
1053 m.visible_area = pb->visible_area;
1054 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1056 for (int y = area->y0; y < area->y1; y++) {
1057 unsigned char *s, *d;
1058 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1059 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1060 for (int x = area->x0; x < area->x1; x++) {
1061 *d = NR_COMPOSEA_111(*s, *d);
1062 d ++;
1063 s ++;
1064 }
1065 }
1066 nr_pixblock_release(&m);
1067 pb->empty = FALSE;
1068 }
1070 return item->state;
1071 }
1073 static NRArenaItem *
1074 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1075 {
1076 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1077 if (!shape->curve) return NULL;
1078 if (!shape->style) return NULL;
1080 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1082 float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1083 double width;
1084 if (outline) {
1085 width = 0.5;
1086 } else {
1087 width = MAX(0.125, shape->_stroke.width * scale) / 2;
1088 }
1090 NRBPath bp;
1091 bp.path = SP_CURVE_BPATH(shape->curve);
1092 double dist = NR_HUGE;
1093 int wind = 0;
1094 bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE && !outline);
1095 nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5);
1097 // pick fill
1098 if (needfill) {
1099 if (!shape->style->fill_rule.computed) {
1100 if (wind != 0) return item;
1101 } else {
1102 if (wind & 0x1) return item;
1103 }
1104 }
1106 // pick stroke
1107 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1108 // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1109 if ((dist - width) < delta) return item;
1110 }
1112 // if not picked on the shape itself, try its markers
1113 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1114 NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1115 if (ret)
1116 return ret;
1117 }
1119 return NULL;
1120 }
1122 /**
1123 *
1124 * Requests a render of the shape, then if the shape is already a curve it
1125 * unrefs the old curve; if the new curve is valid it creates a copy of the
1126 * curve and adds it to the shape. Finally, it requests an update of the
1127 * arena for the shape.
1128 */
1129 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1130 {
1131 g_return_if_fail(shape != NULL);
1132 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1134 if ( justTrans == false ) {
1135 // dirty cached versions
1136 if ( shape->cached_fill ) {
1137 delete shape->cached_fill;
1138 shape->cached_fill=NULL;
1139 }
1140 if ( shape->cached_stroke ) {
1141 delete shape->cached_stroke;
1142 shape->cached_stroke=NULL;
1143 }
1144 }
1146 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1148 if (shape->curve) {
1149 sp_curve_unref(shape->curve);
1150 shape->curve = NULL;
1151 }
1153 if (curve) {
1154 shape->curve = curve;
1155 sp_curve_ref(curve);
1156 }
1158 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1159 }
1161 void NRArenaShape::setFill(SPPaintServer *server) {
1162 _fill.paint.set(server);
1163 _invalidateCachedFill();
1164 }
1166 void NRArenaShape::setFill(SPColor const &color) {
1167 _fill.paint.set(color);
1168 _invalidateCachedFill();
1169 }
1171 void NRArenaShape::setFillOpacity(double opacity) {
1172 _fill.opacity = opacity;
1173 _invalidateCachedFill();
1174 }
1176 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1177 _fill.rule = rule;
1178 _invalidateCachedFill();
1179 }
1181 void NRArenaShape::setStroke(SPPaintServer *server) {
1182 _stroke.paint.set(server);
1183 _invalidateCachedStroke();
1184 }
1186 void NRArenaShape::setStroke(SPColor const &color) {
1187 _stroke.paint.set(color);
1188 _invalidateCachedStroke();
1189 }
1191 void NRArenaShape::setStrokeOpacity(double opacity) {
1192 _stroke.opacity = opacity;
1193 _invalidateCachedStroke();
1194 }
1196 void NRArenaShape::setStrokeWidth(double width) {
1197 _stroke.width = width;
1198 _invalidateCachedStroke();
1199 }
1201 void NRArenaShape::setMitreLimit(double limit) {
1202 _stroke.mitre_limit = limit;
1203 _invalidateCachedStroke();
1204 }
1206 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1207 _stroke.cap = cap;
1208 _invalidateCachedStroke();
1209 }
1211 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1212 _stroke.join = join;
1213 _invalidateCachedStroke();
1214 }
1216 /** nr_arena_shape_set_style
1217 *
1218 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1219 */
1220 void
1221 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1222 {
1223 g_return_if_fail(shape != NULL);
1224 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1226 if (style) sp_style_ref(style);
1227 if (shape->style) sp_style_unref(shape->style);
1228 shape->style = style;
1230 switch (style->fill.type) {
1231 case SP_PAINT_TYPE_NONE: {
1232 shape->setFill(NULL);
1233 break;
1234 }
1235 case SP_PAINT_TYPE_COLOR: {
1236 shape->setFill(style->fill.value.color);
1237 break;
1238 }
1239 case SP_PAINT_TYPE_PAINTSERVER: {
1240 shape->setFill(style->fill.value.paint.server);
1241 break;
1242 }
1243 default: {
1244 g_assert_not_reached();
1245 }
1246 }
1247 shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1248 switch (style->fill_rule.computed) {
1249 case SP_WIND_RULE_EVENODD: {
1250 shape->setFillRule(NRArenaShape::EVEN_ODD);
1251 break;
1252 }
1253 case SP_WIND_RULE_NONZERO: {
1254 shape->setFillRule(NRArenaShape::NONZERO);
1255 break;
1256 }
1257 default: {
1258 g_assert_not_reached();
1259 }
1260 }
1262 switch (style->stroke.type) {
1263 case SP_PAINT_TYPE_NONE: {
1264 shape->setStroke(NULL);
1265 break;
1266 }
1267 case SP_PAINT_TYPE_COLOR: {
1268 shape->setStroke(style->stroke.value.color);
1269 break;
1270 }
1271 case SP_PAINT_TYPE_PAINTSERVER: {
1272 shape->setStroke(style->stroke.value.paint.server);
1273 break;
1274 }
1275 default: {
1276 g_assert_not_reached();
1277 }
1278 }
1279 shape->setStrokeWidth(style->stroke_width.computed);
1280 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1281 switch (style->stroke_linecap.computed) {
1282 case SP_STROKE_LINECAP_ROUND: {
1283 shape->setLineCap(NRArenaShape::ROUND_CAP);
1284 break;
1285 }
1286 case SP_STROKE_LINECAP_SQUARE: {
1287 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1288 break;
1289 }
1290 case SP_STROKE_LINECAP_BUTT: {
1291 shape->setLineCap(NRArenaShape::BUTT_CAP);
1292 break;
1293 }
1294 default: {
1295 g_assert_not_reached();
1296 }
1297 }
1298 switch (style->stroke_linejoin.computed) {
1299 case SP_STROKE_LINEJOIN_ROUND: {
1300 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1301 break;
1302 }
1303 case SP_STROKE_LINEJOIN_BEVEL: {
1304 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1305 break;
1306 }
1307 case SP_STROKE_LINEJOIN_MITER: {
1308 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1309 break;
1310 }
1311 default: {
1312 g_assert_not_reached();
1313 }
1314 }
1315 shape->setMitreLimit(style->stroke_miterlimit.value);
1317 //if shape has a filter
1318 if (style->filter.set && style->filter.filter)
1319 {
1320 shape->filter = new NR::Filter();
1321 shape->filter->set_x(style->filter.filter->x);
1322 shape->filter->set_y(style->filter.filter->y);
1323 shape->filter->set_width(style->filter.filter->width);
1324 shape->filter->set_height(style->filter.filter->height);
1326 //go through all SP filter primitives
1327 for(int i=0; i<style->filter.filter->_primitive_count; i++)
1328 {
1329 SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1330 //if primitive is gaussianblur
1331 if(SP_IS_GAUSSIANBLUR(primitive)) {
1332 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1333 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1334 float num = spblur->stdDeviation.getNumber();
1335 if( num>=0.0 )
1336 {
1337 float optnum = spblur->stdDeviation.getOptNumber();
1338 if( optnum>=0.0 )
1339 gaussian->set_deviation((double) num, (double) optnum);
1340 else
1341 gaussian->set_deviation((double) num);
1342 }
1343 }
1344 }
1345 }
1346 else
1347 {
1348 //no filter set for this shape
1349 shape->filter = NULL;
1350 }
1352 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1353 }
1355 void
1356 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1357 {
1358 g_return_if_fail(shape != NULL);
1359 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1360 g_return_if_fail(pbox != NULL);
1362 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1363 shape->paintbox = *pbox;
1364 } else {
1365 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1366 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1367 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1368 }
1370 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1371 }
1373 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1374 {
1375 paintbox.x0 = pbox.min()[NR::X];
1376 paintbox.y0 = pbox.min()[NR::Y];
1377 paintbox.x1 = pbox.max()[NR::X];
1378 paintbox.y1 = pbox.max()[NR::Y];
1380 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1381 }
1383 static void
1384 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1385 {
1386 if ( st >= en ) return;
1387 if ( vst < 0 ) vst=0;
1388 if ( vst > 1 ) vst=1;
1389 if ( ven < 0 ) ven=0;
1390 if ( ven > 1 ) ven=1;
1391 float sv=vst;
1392 float dv=ven-vst;
1393 int len=en-st;
1394 unsigned char* d=(unsigned char*)dest.buffer;
1395 d+=(st-dest.startPix);
1396 if ( fabs(dv) < 0.001 ) {
1397 if ( vst > 0.999 ) {
1398 /* Simple copy */
1399 while (len > 0) {
1400 d[0] = 255;
1401 d += 1;
1402 len -= 1;
1403 }
1404 } else {
1405 sv*=256;
1406 unsigned int c0_24=(int)sv;
1407 c0_24&=0xFF;
1408 while (len > 0) {
1409 /* Draw */
1410 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1411 d += 1;
1412 len -= 1;
1413 }
1414 }
1415 } else {
1416 if ( en <= st+1 ) {
1417 sv=0.5*(vst+ven);
1418 sv*=256;
1419 unsigned int c0_24=(int)sv;
1420 c0_24&=0xFF;
1421 /* Draw */
1422 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1423 } else {
1424 dv/=len;
1425 sv+=0.5*dv; // correction trapezoidale
1426 sv*=16777216;
1427 dv*=16777216;
1428 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1429 int s0_24 = static_cast<int>(dv);
1430 while (len > 0) {
1431 unsigned int ca;
1432 /* Draw */
1433 ca = c0_24 >> 16;
1434 if ( ca > 255 ) ca=255;
1435 d[0] = NR_COMPOSEA_111(ca,d[0]);
1436 d += 1;
1437 c0_24 += s0_24;
1438 c0_24 = CLAMP(c0_24, 0, 16777216);
1439 len -= 1;
1440 }
1441 }
1442 }
1443 }
1445 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1446 {
1447 theS->CalcBBox();
1448 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1449 int il,ir,it,ib;
1450 il=(int)floor(l);
1451 ir=(int)ceil(r);
1452 it=(int)floor(t);
1453 ib=(int)ceil(b);
1455 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1456 if ( il < m.area.x0 ) il=m.area.x0;
1457 if ( it < m.area.y0 ) it=m.area.y0;
1458 if ( ir > m.area.x1 ) ir=m.area.x1;
1459 if ( ib > m.area.y1 ) ib=m.area.y1;
1461 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1462 int curPt;
1463 float curY;
1464 theS->BeginQuickRaster(curY, curPt);
1466 FloatLigne *theI = new FloatLigne();
1467 IntLigne *theIL = new IntLigne();
1469 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1471 char *mdata = (char*)m.data.px;
1472 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1473 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1474 for (int y = it; y < ib; y++) {
1475 theI->Reset();
1476 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1477 theI->Flatten();
1478 theIL->Copy(theI);
1480 raster_info dest;
1481 dest.startPix=il;
1482 dest.endPix=ir;
1483 dest.sth=il;
1484 dest.stv=y;
1485 dest.buffer=ligStart;
1486 theIL->Raster(dest, NULL, shape_run_A8_OR);
1487 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1488 }
1489 theS->EndQuickRaster();
1490 delete theI;
1491 delete theIL;
1492 }
1495 /*
1496 Local Variables:
1497 mode:c++
1498 c-file-style:"stroustrup"
1499 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1500 indent-tabs-mode:nil
1501 fill-column:99
1502 End:
1503 */
1504 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :