1 #define __NR_ARENA_SHAPE_C__
3 /*
4 * RGBA display list system for inkscape
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
17 #include <display/canvas-arena.h>
18 #include <display/nr-arena.h>
19 #include <display/nr-arena-shape.h>
20 #include <libnr/n-art-bpath.h>
21 #include <libnr/nr-path.h>
22 #include <libnr/nr-pixops.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-fns.h>
25 #include <libnr/nr-blit.h>
26 #include <livarot/Path.h>
27 #include <livarot/float-line.h>
28 #include <livarot/int-line.h>
29 #include <style.h>
30 #include "prefs-utils.h"
31 #include "inkscape-cairo.h"
33 #include "sp-filter.h"
34 #include "sp-filter-reference.h"
35 #include "display/nr-filter.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;
128 shape->last_pick = NULL;
129 shape->repick_after = 0;
130 }
132 static void
133 nr_arena_shape_finalize(NRObject *object)
134 {
135 NRArenaShape *shape = (NRArenaShape *) object;
137 if (shape->fill_shp) delete shape->fill_shp;
138 if (shape->stroke_shp) delete shape->stroke_shp;
139 if (shape->cached_fill) delete shape->cached_fill;
140 if (shape->cached_stroke) delete shape->cached_stroke;
141 if (shape->fill_painter) sp_painter_free(shape->fill_painter);
142 if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
144 if (shape->style) sp_style_unref(shape->style);
145 if (shape->curve) sp_curve_unref(shape->curve);
147 ((NRObjectClass *) shape_parent_class)->finalize(object);
148 }
150 /**
151 * Retrieves the markers from the item
152 */
153 static NRArenaItem *
154 nr_arena_shape_children(NRArenaItem *item)
155 {
156 NRArenaShape *shape = (NRArenaShape *) item;
158 return shape->markers;
159 }
161 /**
162 * Attaches child to item, and if ref is not NULL, sets it and ref->next as
163 * the prev and next items. If ref is NULL, then it sets the item's markers
164 * as the next items.
165 */
166 static void
167 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
168 {
169 NRArenaShape *shape = (NRArenaShape *) item;
171 if (!ref) {
172 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
173 } else {
174 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
175 }
177 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
178 }
180 /**
181 * Removes child from the shape. If there are no prev items in
182 * the child, it sets items' markers to the next item in the child.
183 */
184 static void
185 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
186 {
187 NRArenaShape *shape = (NRArenaShape *) item;
189 if (child->prev) {
190 nr_arena_item_detach(item, child);
191 } else {
192 shape->markers = nr_arena_item_detach(item, child);
193 }
195 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
196 }
198 /**
199 * Detaches child from item, and if there are no previous items in child, it
200 * sets item's markers to the child. It then attaches the child back onto the item.
201 * If ref is null, it sets the markers to be the next item, otherwise it uses
202 * the next/prev items in ref.
203 */
204 static void
205 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
206 {
207 NRArenaShape *shape = (NRArenaShape *) item;
209 if (child->prev) {
210 nr_arena_item_detach(item, child);
211 } else {
212 shape->markers = nr_arena_item_detach(item, child);
213 }
215 if (!ref) {
216 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
217 } else {
218 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
219 }
221 nr_arena_item_request_render(child);
222 }
224 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
225 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
226 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
228 /**
229 * Updates the arena shape 'item' and all of its children, including the markers.
230 */
231 static guint
232 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
233 {
234 NRRect bbox;
236 NRArenaShape *shape = NR_ARENA_SHAPE(item);
238 unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
240 unsigned int newstate;
241 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
242 newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
243 beststate = beststate & newstate;
244 }
246 if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
247 /* We do not have to create rendering structures */
248 shape->ctm = gc->transform;
249 if (state & NR_ARENA_ITEM_STATE_BBOX) {
250 if (shape->curve) {
251 NRBPath bp;
252 /* fixme: */
253 bbox.x0 = bbox.y0 = NR_HUGE;
254 bbox.x1 = bbox.y1 = -NR_HUGE;
255 bp.path = SP_CURVE_BPATH(shape->curve);
256 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
257 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
258 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
259 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
260 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
261 }
262 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
263 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
264 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
265 }
266 }
267 }
268 return (state | item->state);
269 }
271 shape->delayed_shp=true;
272 shape->ctm = gc->transform;
273 bbox.x0 = bbox.y0 = NR_HUGE;
274 bbox.x1 = bbox.y1 = -NR_HUGE;
276 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
278 if (shape->curve) {
279 NRBPath bp;
280 /* fixme: */
281 bbox.x0 = bbox.y0 = NR_HUGE;
282 bbox.x1 = bbox.y1 = -NR_HUGE;
283 bp.path = SP_CURVE_BPATH(shape->curve);
284 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
285 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
286 float width, scale;
287 scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
288 width = MAX(0.125, shape->_stroke.width * scale);
289 if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
290 bbox.x0-=width;
291 bbox.x1+=width;
292 bbox.y0-=width;
293 bbox.y1+=width;
294 }
295 // those pesky miters, now
296 float miterMax=width*shape->_stroke.mitre_limit;
297 if ( miterMax > 0.01 ) {
298 // grunt mode. we should compute the various miters instead (one for each point on the curve)
299 bbox.x0-=miterMax;
300 bbox.x1+=miterMax;
301 bbox.y0-=miterMax;
302 bbox.y1+=miterMax;
303 }
304 }
305 } else {
306 }
307 shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
308 shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
309 shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
310 shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
311 if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
313 /* Release state data */
314 if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
315 /* Concept test */
316 if (shape->fill_shp) {
317 delete shape->fill_shp;
318 shape->fill_shp = NULL;
319 }
320 }
321 if (shape->stroke_shp) {
322 delete shape->stroke_shp;
323 shape->stroke_shp = NULL;
324 }
325 if (shape->fill_painter) {
326 sp_painter_free(shape->fill_painter);
327 shape->fill_painter = NULL;
328 }
329 if (shape->stroke_painter) {
330 sp_painter_free(shape->stroke_painter);
331 shape->stroke_painter = NULL;
332 }
334 if (!shape->curve ||
335 !shape->style ||
336 sp_curve_is_empty(shape->curve) ||
337 (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
338 ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
339 {
340 item->bbox = shape->approx_bbox;
341 return NR_ARENA_ITEM_STATE_ALL;
342 }
344 /* Build state data */
345 if ( shape->delayed_shp ) {
346 item->bbox=shape->approx_bbox;
347 } else {
348 nr_arena_shape_update_stroke(shape, gc, area);
349 nr_arena_shape_update_fill(shape, gc, area);
351 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
352 nr_arena_shape_add_bboxes(shape,bbox);
354 shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
355 shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
356 shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
357 shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
358 }
360 if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
362 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
363 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
364 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
365 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
366 nr_arena_request_render_rect(item->arena, &item->bbox);
368 item->render_opacity = TRUE;
369 if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
370 if (gc && gc->parent) {
371 shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
372 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
373 &shape->paintbox);
374 }
375 item->render_opacity = FALSE;
376 }
377 if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
378 if (gc && gc->parent) {
379 shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
380 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
381 &shape->paintbox);
382 }
383 item->render_opacity = FALSE;
384 }
385 if ( (shape->_fill.paint.type() != NRArenaShape::Paint::NONE &&
386 shape->_stroke.paint.type() != NRArenaShape::Paint::NONE)
387 || (shape->markers)
388 )
389 {
390 // don't merge item opacity with paint opacity if there is a stroke on the fill, or markers on stroke
391 item->render_opacity = FALSE;
392 }
394 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
395 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
396 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
397 }
398 }
400 return NR_ARENA_ITEM_STATE_ALL;
401 }
403 int matrix_is_isometry(NR::Matrix p) {
404 NR::Matrix tp;
405 // transposition
406 tp[0]=p[0];
407 tp[1]=p[2];
408 tp[2]=p[1];
409 tp[3]=p[3];
410 for (int i = 4; i < 6; i++) // shut valgrind up :)
411 tp[i] = p[i] = 0;
412 NR::Matrix isom = tp*p; // A^T * A = adjunct?
413 // Is the adjunct nearly an identity function?
414 if (isom.is_translation(0.01)) {
415 // the transformation is an isometry -> no need to recompute
416 // the uncrossed polygon
417 if ( p.det() < 0 )
418 return -1;
419 else
420 return 1;
421 }
422 return 0;
423 }
425 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
426 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
427 }
429 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
430 void
431 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
432 {
433 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
434 ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
435 if (TRUE || !shape->fill_shp) {
436 NR::Matrix cached_to_new = NR::identity();
437 int isometry = 0;
438 if ( shape->cached_fill ) {
439 if (shape->cached_fctm == gc->transform) {
440 isometry = 2; // identity
441 } else {
442 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
443 isometry = matrix_is_isometry(cached_to_new);
444 }
445 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
446 isometry = 0;
447 }
448 if ( isometry == 0 ) {
449 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
450 shape->cached_fill->Reset();
452 Path* thePath=new Path;
453 Shape* theShape=new Shape;
454 {
455 NR::Matrix tempMat(gc->transform);
456 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
457 }
459 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
460 thePath->Convert(1.0);
461 shape->cached_fpartialy = false;
462 } else {
463 thePath->Convert(area, 1.0);
464 shape->cached_fpartialy = true;
465 }
467 thePath->Fill(theShape, 0);
469 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
470 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
471 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
472 //shape->cached_fill->Copy(theShape);
473 } else {
474 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
475 }
476 shape->cached_fctm=gc->transform;
477 shape->cached_farea = *area;
478 delete theShape;
479 delete thePath;
480 if ( shape->fill_shp == NULL )
481 shape->fill_shp = new Shape;
483 shape->fill_shp->Copy(shape->cached_fill);
485 } else if ( 2 == isometry ) {
486 if ( shape->fill_shp == NULL ) {
487 shape->fill_shp = new Shape;
488 shape->fill_shp->Copy(shape->cached_fill);
489 }
490 } else {
492 if ( shape->fill_shp == NULL )
493 shape->fill_shp = new Shape;
495 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
496 shape->cached_fill->numberOfEdges());
497 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
498 shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
499 if ( isometry == 1 ) {
500 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
501 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
502 shape->cached_fill->getEdge(i).en);
503 } else if ( isometry == -1 ) { // need to flip poly.
504 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
505 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
506 shape->cached_fill->getEdge(i).st);
507 }
508 shape->fill_shp->ForceToPolygon();
509 shape->fill_shp->needPointsSorting();
510 shape->fill_shp->needEdgesSorting();
511 }
512 shape->delayed_shp |= shape->cached_fpartialy;
513 }
514 }
515 }
517 void
518 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
519 {
520 SPStyle* style = shape->style;
522 float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
524 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
526 if (outline) {
527 // cairo does not need the livarot path for rendering
528 return;
529 }
531 // after switching normal stroke rendering to cairo too, optimize this: lower tolerance, disregard dashes
532 // (since it will only be used for picking, not for rendering)
534 if (outline ||
535 ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
536 ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
538 float style_width = MAX(0.125, shape->_stroke.width * scale);
539 float width;
540 if (outline) {
541 width = 0.5; // 1 pixel wide, independent of zoom
542 } else {
543 width = style_width;
544 }
546 NR::Matrix cached_to_new = NR::identity();
548 int isometry = 0;
549 if ( shape->cached_stroke ) {
550 if (shape->cached_sctm == gc->transform) {
551 isometry = 2; // identity
552 } else {
553 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
554 isometry = matrix_is_isometry(cached_to_new);
555 }
556 if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
557 isometry = 0;
558 if (0 != isometry && width != shape->cached_width) {
559 // if this happens without setting style, we have just switched to outline or back
560 isometry = 0;
561 }
562 }
564 if ( isometry == 0 ) {
565 if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
566 shape->cached_stroke->Reset();
567 Path* thePath = new Path;
568 Shape* theShape = new Shape;
569 {
570 NR::Matrix tempMat(gc->transform);
571 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
572 }
574 // add some padding to the rendering area, so clipped path does not go into a render area
575 NRRectL padded_area = *area;
576 padded_area.x0 -= (NR::ICoord)width;
577 padded_area.x1 += (NR::ICoord)width;
578 padded_area.y0 -= (NR::ICoord)width;
579 padded_area.y1 += (NR::ICoord)width;
580 if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
581 thePath->Convert((outline) ? 4.0 : 1.0);
582 shape->cached_spartialy = false;
583 }
584 else {
585 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
586 shape->cached_spartialy = true;
587 }
589 if (style->stroke_dash.n_dash && !outline) {
590 thePath->DashPolylineFromStyle(style, scale, 1.0);
591 }
593 ButtType butt=butt_straight;
594 switch (shape->_stroke.cap) {
595 case NRArenaShape::BUTT_CAP:
596 butt = butt_straight;
597 break;
598 case NRArenaShape::ROUND_CAP:
599 butt = butt_round;
600 break;
601 case NRArenaShape::SQUARE_CAP:
602 butt = butt_square;
603 break;
604 }
605 JoinType join=join_straight;
606 switch (shape->_stroke.join) {
607 case NRArenaShape::MITRE_JOIN:
608 join = join_pointy;
609 break;
610 case NRArenaShape::ROUND_JOIN:
611 join = join_round;
612 break;
613 case NRArenaShape::BEVEL_JOIN:
614 join = join_straight;
615 break;
616 }
618 if (outline) {
619 butt = butt_straight;
620 join = join_straight;
621 }
623 thePath->Stroke(theShape, false, 0.5*width, join, butt,
624 0.5*width*shape->_stroke.mitre_limit);
627 if (outline) {
628 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
629 shape->cached_stroke->Copy(theShape);
630 } else {
631 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
632 }
634 shape->cached_width = width;
636 shape->cached_sctm=gc->transform;
637 shape->cached_sarea = *area;
638 delete thePath;
639 delete theShape;
640 if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
642 shape->stroke_shp->Copy(shape->cached_stroke);
644 } else if ( 2 == isometry ) {
645 if ( shape->stroke_shp == NULL ) {
646 shape->stroke_shp=new Shape;
647 shape->stroke_shp->Copy(shape->cached_stroke);
648 }
649 } else {
650 if ( shape->stroke_shp == NULL )
651 shape->stroke_shp=new Shape;
652 shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
653 for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
654 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
655 if ( isometry == 1 ) {
656 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
657 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
658 shape->cached_stroke->getEdge(i).en);
659 } else if ( isometry == -1 ) {
660 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
661 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
662 shape->cached_stroke->getEdge(i).st);
663 }
664 shape->stroke_shp->ForceToPolygon();
665 shape->stroke_shp->needPointsSorting();
666 shape->stroke_shp->needEdgesSorting();
667 }
668 shape->delayed_shp |= shape->cached_spartialy;
669 }
670 }
673 void
674 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
675 {
676 if ( shape->stroke_shp ) {
677 shape->stroke_shp->CalcBBox();
678 shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
679 shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
680 shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
681 shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
682 if ( bbox.x0 >= bbox.x1 ) {
683 if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
684 bbox.x0=shape->stroke_shp->leftX;
685 bbox.x1=shape->stroke_shp->rightX;
686 }
687 } else {
688 if ( shape->stroke_shp->leftX < bbox.x0 )
689 bbox.x0=shape->stroke_shp->leftX;
690 if ( shape->stroke_shp->rightX > bbox.x1 )
691 bbox.x1=shape->stroke_shp->rightX;
692 }
693 if ( bbox.y0 >= bbox.y1 ) {
694 if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
695 bbox.y0=shape->stroke_shp->topY;
696 bbox.y1=shape->stroke_shp->bottomY;
697 }
698 } else {
699 if ( shape->stroke_shp->topY < bbox.y0 )
700 bbox.y0=shape->stroke_shp->topY;
701 if ( shape->stroke_shp->bottomY > bbox.y1 )
702 bbox.y1=shape->stroke_shp->bottomY;
703 }
704 }
705 if ( shape->fill_shp ) {
706 shape->fill_shp->CalcBBox();
707 shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
708 shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
709 shape->fill_shp->topY=floor(shape->fill_shp->topY);
710 shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
711 if ( bbox.x0 >= bbox.x1 ) {
712 if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
713 bbox.x0=shape->fill_shp->leftX;
714 bbox.x1=shape->fill_shp->rightX;
715 }
716 } else {
717 if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
718 if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
719 }
720 if ( bbox.y0 >= bbox.y1 ) {
721 if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
722 bbox.y0=shape->fill_shp->topY;
723 bbox.y1=shape->fill_shp->bottomY;
724 }
725 } else {
726 if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
727 if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
728 }
729 }
730 }
732 // cairo outline rendering:
733 static unsigned int
734 cairo_arena_shape_render_outline(cairo_t *ct, NRArenaItem *item, NR::Maybe<NR::Rect> area)
735 {
736 NRArenaShape *shape = NR_ARENA_SHAPE(item);
738 if (!ct)
739 return item->state;
741 guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
742 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
743 // by setting color channels in the "wrong" order
744 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));
746 cairo_set_line_width(ct, 0.5);
747 cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
748 cairo_new_path(ct);
750 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area, true, 0);
752 cairo_stroke(ct);
754 return item->state;
755 }
757 // cairo stroke rendering (flat color only so far!):
758 // works on canvas, but wrongs the colors in nonpremul buffers: icons and png export
759 // (need to switch them to premul before this can be enabled)
760 void
761 cairo_arena_shape_render_stroke(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
762 {
763 NRArenaShape *shape = NR_ARENA_SHAPE(item);
764 SPStyle const *style = shape->style;
766 float const scale = NR_MATRIX_DF_EXPANSION(shape->ctm);
768 if (fabs(shape->_stroke.width * scale) < 0.01)
769 return;
771 cairo_t *ct = nr_create_cairo_context (area, pb);
773 if (!ct)
774 return;
776 guint32 rgba;
777 if ( item->render_opacity ) {
778 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
779 shape->_stroke.opacity *
780 SP_SCALE24_TO_FLOAT(style->opacity.value));
781 } else {
782 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
783 shape->_stroke.opacity);
784 }
786 // FIXME: we use RGBA buffers but cairo writes BGRA (on i386), so we must cheat
787 // by setting color channels in the "wrong" order
788 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));
790 float style_width = MAX(0.125, shape->_stroke.width * scale);
791 cairo_set_line_width(ct, style_width);
793 switch (shape->_stroke.cap) {
794 case NRArenaShape::BUTT_CAP:
795 cairo_set_line_cap(ct, CAIRO_LINE_CAP_BUTT);
796 break;
797 case NRArenaShape::ROUND_CAP:
798 cairo_set_line_cap(ct, CAIRO_LINE_CAP_ROUND);
799 break;
800 case NRArenaShape::SQUARE_CAP:
801 cairo_set_line_cap(ct, CAIRO_LINE_CAP_SQUARE);
802 break;
803 }
804 switch (shape->_stroke.join) {
805 case NRArenaShape::MITRE_JOIN:
806 cairo_set_line_join(ct, CAIRO_LINE_JOIN_MITER);
807 break;
808 case NRArenaShape::ROUND_JOIN:
809 cairo_set_line_join(ct, CAIRO_LINE_JOIN_ROUND);
810 break;
811 case NRArenaShape::BEVEL_JOIN:
812 cairo_set_line_join(ct, CAIRO_LINE_JOIN_BEVEL);
813 break;
814 }
816 cairo_set_miter_limit (ct, style->stroke_miterlimit.value);
818 if (style->stroke_dash.n_dash) {
819 NRVpathDash dash;
820 dash.offset = style->stroke_dash.offset * scale;
821 dash.n_dash = style->stroke_dash.n_dash;
822 dash.dash = g_new(double, dash.n_dash);
823 for (int i = 0; i < dash.n_dash; i++) {
824 dash.dash[i] = style->stroke_dash.dash[i] * scale;
825 }
826 cairo_set_dash (ct, dash.dash, dash.n_dash, dash.offset);
827 g_free(dash.dash);
828 }
830 cairo_set_tolerance(ct, 0.1);
831 cairo_new_path(ct);
833 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), area->upgrade(), true, style_width);
835 cairo_stroke(ct);
837 cairo_surface_t *cst = cairo_get_target(ct);
838 cairo_destroy (ct);
839 cairo_surface_finish (cst);
840 cairo_surface_destroy (cst);
842 pb->empty = FALSE;
843 }
846 /**
847 * Renders the item. Markers are just composed into the parent buffer.
848 */
849 static unsigned int
850 nr_arena_shape_render(cairo_t *ct, NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
851 {
852 NRArenaShape *shape = NR_ARENA_SHAPE(item);
854 if (!shape->curve) return item->state;
855 if (!shape->style) return item->state;
857 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
859 if (outline) { // cairo outline rendering
861 pb->empty = FALSE;
862 unsigned int ret = cairo_arena_shape_render_outline (ct, item, (&pb->area)->upgrade());
863 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
865 } else {
867 if ( shape->delayed_shp ) {
868 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
869 NRGC tempGC(NULL);
870 tempGC.transform=shape->ctm;
871 shape->delayed_shp = false;
872 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
873 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
874 /* NRRect bbox;
875 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
876 nr_arena_shape_add_bboxes(shape,bbox);
877 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
878 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
879 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
880 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
881 shape->approx_bbox=item->bbox;*/
882 } else {
883 return item->state;
884 }
885 }
887 SPStyle const *style = shape->style;
888 if (shape->fill_shp) {
889 NRPixBlock m;
890 guint32 rgba;
892 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
894 // if memory allocation failed, abort render
895 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
896 nr_pixblock_release (&m);
897 return (item->state);
898 }
900 m.visible_area = pb->visible_area;
901 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
902 m.empty = FALSE;
904 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
905 // do not render fill in any way
906 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
907 if ( item->render_opacity ) {
908 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
909 shape->_fill.opacity *
910 SP_SCALE24_TO_FLOAT(style->opacity.value));
911 } else {
912 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
913 shape->_fill.opacity);
914 }
915 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
916 pb->empty = FALSE;
917 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
918 if (shape->fill_painter) {
919 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
920 }
921 }
923 nr_pixblock_release(&m);
924 }
926 if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR) {
928 // cairo_arena_shape_render_stroke(item, area, pb);
930 guint32 rgba;
931 NRPixBlock m;
933 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
935 // if memory allocation failed, abort render
936 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
937 nr_pixblock_release (&m);
938 return (item->state);
939 }
941 m.visible_area = pb->visible_area;
942 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
943 m.empty = FALSE;
945 if ( item->render_opacity ) {
946 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
947 shape->_stroke.opacity *
948 SP_SCALE24_TO_FLOAT(style->opacity.value));
949 } else {
950 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
951 shape->_stroke.opacity);
952 }
953 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
954 pb->empty = FALSE;
956 nr_pixblock_release(&m);
958 } else if (shape->stroke_shp && shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
960 NRPixBlock m;
962 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
964 // if memory allocation failed, abort render
965 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
966 nr_pixblock_release (&m);
967 return (item->state);
968 }
970 m.visible_area = pb->visible_area;
971 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
972 m.empty = FALSE;
974 if (shape->stroke_painter) {
975 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
976 }
978 nr_pixblock_release(&m);
979 }
981 } // non-cairo non-outline branch
983 /* Render markers into parent buffer */
984 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
985 unsigned int ret = nr_arena_item_invoke_render(ct, child, area, pb, flags);
986 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
987 }
989 return item->state;
990 }
993 // cairo clipping: this basically works except for the stride-must-be-divisible-by-4 cairo bug;
994 // reenable this when the bug is fixed and remove the rest of this function
995 static guint
996 cairo_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
997 {
998 NRArenaShape *shape = NR_ARENA_SHAPE(item);
999 if (!shape->curve) return item->state;
1001 cairo_t *ct = nr_create_cairo_context (area, pb);
1003 if (!ct)
1004 return item->state;
1006 cairo_set_source_rgba(ct, 0, 0, 0, 1);
1008 cairo_new_path(ct);
1010 feed_curve_to_cairo (ct, SP_CURVE_BPATH(shape->curve), NR::Matrix(shape->ctm), (area)->upgrade(), false, 0);
1012 cairo_fill(ct);
1014 cairo_surface_t *cst = cairo_get_target(ct);
1015 cairo_destroy (ct);
1016 cairo_surface_finish (cst);
1017 cairo_surface_destroy (cst);
1019 pb->empty = FALSE;
1021 return item->state;
1022 }
1025 static guint
1026 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
1027 {
1028 //return cairo_arena_shape_clip(item, area, pb);
1030 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1031 if (!shape->curve) return item->state;
1033 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
1034 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
1035 NRGC tempGC(NULL);
1036 tempGC.transform=shape->ctm;
1037 shape->delayed_shp = false;
1038 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
1039 } else {
1040 return item->state;
1041 }
1042 }
1044 if ( shape->fill_shp ) {
1045 NRPixBlock m;
1047 /* fixme: We can OR in one step (Lauris) */
1048 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
1050 // if memory allocation failed, abort
1051 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
1052 nr_pixblock_release (&m);
1053 return (item->state);
1054 }
1056 m.visible_area = pb->visible_area;
1057 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
1059 for (int y = area->y0; y < area->y1; y++) {
1060 unsigned char *s, *d;
1061 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
1062 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
1063 for (int x = area->x0; x < area->x1; x++) {
1064 *d = NR_COMPOSEA_111(*s, *d);
1065 d ++;
1066 s ++;
1067 }
1068 }
1069 nr_pixblock_release(&m);
1070 pb->empty = FALSE;
1071 }
1073 return item->state;
1074 }
1076 static NRArenaItem *
1077 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
1078 {
1079 NRArenaShape *shape = NR_ARENA_SHAPE(item);
1081 if (shape->repick_after > 0)
1082 shape->repick_after--;
1084 if (shape->repick_after > 0) // we are a slow, huge path. skip this pick, returning what was returned last time
1085 return shape->last_pick;
1087 if (!shape->curve) return NULL;
1088 if (!shape->style) return NULL;
1089 if (SP_SCALE24_TO_FLOAT(shape->style->opacity.value) == 0) // fully transparent, no pick
1090 return NULL;
1092 GTimeVal tstart, tfinish;
1093 g_get_current_time (&tstart);
1095 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1097 double width;
1098 if (outline) {
1099 width = 0.5;
1100 } else if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE && shape->_stroke.opacity > 1e-3) {
1101 float const scale = NR_MATRIX_DF_EXPANSION(&shape->ctm);
1102 width = MAX(0.125, shape->_stroke.width * scale) / 2;
1103 } else {
1104 width = 0;
1105 }
1107 NRBPath bp;
1108 bp.path = SP_CURVE_BPATH(shape->curve);
1109 double dist = NR_HUGE;
1110 int wind = 0;
1111 bool needfill = (shape->_fill.paint.type() != NRArenaShape::Paint::NONE
1112 && shape->_fill.opacity > 1e-3 && !outline);
1114 if (item->arena->canvasarena) {
1115 NR::Rect viewbox = item->arena->canvasarena->item.canvas->getViewbox();
1116 viewbox.growBy (width);
1117 nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
1118 } else {
1119 nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
1120 }
1122 g_get_current_time (&tfinish);
1123 glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
1124 //g_print ("pick time %lu\n", this_pick);
1126 if (this_pick > 10000) { // slow picking, remember to skip several new picks
1127 shape->repick_after = this_pick / 5000;
1128 }
1130 // covered by fill?
1131 if (needfill) {
1132 if (!shape->style->fill_rule.computed) {
1133 if (wind != 0) {
1134 shape->last_pick = item;
1135 return item;
1136 }
1137 } else {
1138 if (wind & 0x1) {
1139 shape->last_pick = item;
1140 return item;
1141 }
1142 }
1143 }
1145 // close to the edge, as defined by strokewidth and delta?
1146 // this ignores dashing (as if the stroke is solid) and always works as if caps are round
1147 if (needfill || width > 0) { // if either fill or stroke visible,
1148 if ((dist - width) < delta) {
1149 shape->last_pick = item;
1150 return item;
1151 }
1152 }
1154 // if not picked on the shape itself, try its markers
1155 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
1156 NRArenaItem *ret = nr_arena_item_invoke_pick(child, p, delta, 0);
1157 if (ret) {
1158 shape->last_pick = item;
1159 return item;
1160 }
1161 }
1163 shape->last_pick = NULL;
1164 return NULL;
1165 }
1167 /**
1168 *
1169 * Requests a render of the shape, then if the shape is already a curve it
1170 * unrefs the old curve; if the new curve is valid it creates a copy of the
1171 * curve and adds it to the shape. Finally, it requests an update of the
1172 * arena for the shape.
1173 */
1174 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1175 {
1176 g_return_if_fail(shape != NULL);
1177 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1179 if ( justTrans == false ) {
1180 // dirty cached versions
1181 if ( shape->cached_fill ) {
1182 delete shape->cached_fill;
1183 shape->cached_fill=NULL;
1184 }
1185 if ( shape->cached_stroke ) {
1186 delete shape->cached_stroke;
1187 shape->cached_stroke=NULL;
1188 }
1189 }
1191 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1193 if (shape->curve) {
1194 sp_curve_unref(shape->curve);
1195 shape->curve = NULL;
1196 }
1198 if (curve) {
1199 shape->curve = curve;
1200 sp_curve_ref(curve);
1201 }
1203 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1204 }
1206 void NRArenaShape::setFill(SPPaintServer *server) {
1207 _fill.paint.set(server);
1208 _invalidateCachedFill();
1209 }
1211 void NRArenaShape::setFill(SPColor const &color) {
1212 _fill.paint.set(color);
1213 _invalidateCachedFill();
1214 }
1216 void NRArenaShape::setFillOpacity(double opacity) {
1217 _fill.opacity = opacity;
1218 _invalidateCachedFill();
1219 }
1221 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1222 _fill.rule = rule;
1223 _invalidateCachedFill();
1224 }
1226 void NRArenaShape::setStroke(SPPaintServer *server) {
1227 _stroke.paint.set(server);
1228 _invalidateCachedStroke();
1229 }
1231 void NRArenaShape::setStroke(SPColor const &color) {
1232 _stroke.paint.set(color);
1233 _invalidateCachedStroke();
1234 }
1236 void NRArenaShape::setStrokeOpacity(double opacity) {
1237 _stroke.opacity = opacity;
1238 _invalidateCachedStroke();
1239 }
1241 void NRArenaShape::setStrokeWidth(double width) {
1242 _stroke.width = width;
1243 _invalidateCachedStroke();
1244 }
1246 void NRArenaShape::setMitreLimit(double limit) {
1247 _stroke.mitre_limit = limit;
1248 _invalidateCachedStroke();
1249 }
1251 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1252 _stroke.cap = cap;
1253 _invalidateCachedStroke();
1254 }
1256 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1257 _stroke.join = join;
1258 _invalidateCachedStroke();
1259 }
1261 /** nr_arena_shape_set_style
1262 *
1263 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1264 */
1265 void
1266 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1267 {
1268 g_return_if_fail(shape != NULL);
1269 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1271 if (style) sp_style_ref(style);
1272 if (shape->style) sp_style_unref(shape->style);
1273 shape->style = style;
1275 switch (style->fill.type) {
1276 case SP_PAINT_TYPE_NONE: {
1277 shape->setFill(NULL);
1278 break;
1279 }
1280 case SP_PAINT_TYPE_COLOR: {
1281 shape->setFill(style->fill.value.color);
1282 break;
1283 }
1284 case SP_PAINT_TYPE_PAINTSERVER: {
1285 shape->setFill(style->getFillPaintServer());
1286 break;
1287 }
1288 default: {
1289 g_assert_not_reached();
1290 }
1291 }
1292 shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1293 switch (style->fill_rule.computed) {
1294 case SP_WIND_RULE_EVENODD: {
1295 shape->setFillRule(NRArenaShape::EVEN_ODD);
1296 break;
1297 }
1298 case SP_WIND_RULE_NONZERO: {
1299 shape->setFillRule(NRArenaShape::NONZERO);
1300 break;
1301 }
1302 default: {
1303 g_assert_not_reached();
1304 }
1305 }
1307 switch (style->stroke.type) {
1308 case SP_PAINT_TYPE_NONE: {
1309 shape->setStroke(NULL);
1310 break;
1311 }
1312 case SP_PAINT_TYPE_COLOR: {
1313 shape->setStroke(style->stroke.value.color);
1314 break;
1315 }
1316 case SP_PAINT_TYPE_PAINTSERVER: {
1317 shape->setStroke(style->getStrokePaintServer());
1318 break;
1319 }
1320 default: {
1321 g_assert_not_reached();
1322 }
1323 }
1324 shape->setStrokeWidth(style->stroke_width.computed);
1325 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1326 switch (style->stroke_linecap.computed) {
1327 case SP_STROKE_LINECAP_ROUND: {
1328 shape->setLineCap(NRArenaShape::ROUND_CAP);
1329 break;
1330 }
1331 case SP_STROKE_LINECAP_SQUARE: {
1332 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1333 break;
1334 }
1335 case SP_STROKE_LINECAP_BUTT: {
1336 shape->setLineCap(NRArenaShape::BUTT_CAP);
1337 break;
1338 }
1339 default: {
1340 g_assert_not_reached();
1341 }
1342 }
1343 switch (style->stroke_linejoin.computed) {
1344 case SP_STROKE_LINEJOIN_ROUND: {
1345 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1346 break;
1347 }
1348 case SP_STROKE_LINEJOIN_BEVEL: {
1349 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1350 break;
1351 }
1352 case SP_STROKE_LINEJOIN_MITER: {
1353 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1354 break;
1355 }
1356 default: {
1357 g_assert_not_reached();
1358 }
1359 }
1360 shape->setMitreLimit(style->stroke_miterlimit.value);
1362 //if shape has a filter
1363 if (style->filter.set && style->getFilter()) {
1364 if (!shape->filter) {
1365 int primitives = sp_filter_primitive_count(SP_FILTER(style->getFilter()));
1366 shape->filter = new NR::Filter(primitives);
1367 }
1368 sp_filter_build_renderer(SP_FILTER(style->getFilter()), shape->filter);
1369 } else {
1370 //no filter set for this shape
1371 delete shape->filter;
1372 shape->filter = NULL;
1373 }
1375 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1376 }
1378 void
1379 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1380 {
1381 g_return_if_fail(shape != NULL);
1382 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1383 g_return_if_fail(pbox != NULL);
1385 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1386 shape->paintbox = *pbox;
1387 } else {
1388 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1389 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1390 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1391 }
1393 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1394 }
1396 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1397 {
1398 paintbox.x0 = pbox.min()[NR::X];
1399 paintbox.y0 = pbox.min()[NR::Y];
1400 paintbox.x1 = pbox.max()[NR::X];
1401 paintbox.y1 = pbox.max()[NR::Y];
1403 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1404 }
1406 static void
1407 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1408 {
1409 if ( st >= en ) return;
1410 if ( vst < 0 ) vst=0;
1411 if ( vst > 1 ) vst=1;
1412 if ( ven < 0 ) ven=0;
1413 if ( ven > 1 ) ven=1;
1414 float sv=vst;
1415 float dv=ven-vst;
1416 int len=en-st;
1417 unsigned char* d=(unsigned char*)dest.buffer;
1418 d+=(st-dest.startPix);
1419 if ( fabs(dv) < 0.001 ) {
1420 if ( vst > 0.999 ) {
1421 /* Simple copy */
1422 while (len > 0) {
1423 d[0] = 255;
1424 d += 1;
1425 len -= 1;
1426 }
1427 } else {
1428 sv*=256;
1429 unsigned int c0_24=(int)sv;
1430 c0_24&=0xFF;
1431 while (len > 0) {
1432 /* Draw */
1433 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1434 d += 1;
1435 len -= 1;
1436 }
1437 }
1438 } else {
1439 if ( en <= st+1 ) {
1440 sv=0.5*(vst+ven);
1441 sv*=256;
1442 unsigned int c0_24=(int)sv;
1443 c0_24&=0xFF;
1444 /* Draw */
1445 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1446 } else {
1447 dv/=len;
1448 sv+=0.5*dv; // correction trapezoidale
1449 sv*=16777216;
1450 dv*=16777216;
1451 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1452 int s0_24 = static_cast<int>(dv);
1453 while (len > 0) {
1454 unsigned int ca;
1455 /* Draw */
1456 ca = c0_24 >> 16;
1457 if ( ca > 255 ) ca=255;
1458 d[0] = NR_COMPOSEA_111(ca,d[0]);
1459 d += 1;
1460 c0_24 += s0_24;
1461 c0_24 = CLAMP(c0_24, 0, 16777216);
1462 len -= 1;
1463 }
1464 }
1465 }
1466 }
1468 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1469 {
1470 theS->CalcBBox();
1471 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1472 int il,ir,it,ib;
1473 il=(int)floor(l);
1474 ir=(int)ceil(r);
1475 it=(int)floor(t);
1476 ib=(int)ceil(b);
1478 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1479 if ( il < m.area.x0 ) il=m.area.x0;
1480 if ( it < m.area.y0 ) it=m.area.y0;
1481 if ( ir > m.area.x1 ) ir=m.area.x1;
1482 if ( ib > m.area.y1 ) ib=m.area.y1;
1484 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1485 int curPt;
1486 float curY;
1487 theS->BeginQuickRaster(curY, curPt);
1489 FloatLigne *theI = new FloatLigne();
1490 IntLigne *theIL = new IntLigne();
1492 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1494 char *mdata = (char*)m.data.px;
1495 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1496 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1497 for (int y = it; y < ib; y++) {
1498 theI->Reset();
1499 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1500 theI->Flatten();
1501 theIL->Copy(theI);
1503 raster_info dest;
1504 dest.startPix=il;
1505 dest.endPix=ir;
1506 dest.sth=il;
1507 dest.stv=y;
1508 dest.buffer=ligStart;
1509 theIL->Raster(dest, NULL, shape_run_A8_OR);
1510 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1511 }
1512 theS->EndQuickRaster();
1513 delete theI;
1514 delete theIL;
1515 }
1518 /*
1519 Local Variables:
1520 mode:c++
1521 c-file-style:"stroustrup"
1522 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1523 indent-tabs-mode:nil
1524 fill-column:99
1525 End:
1526 */
1527 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :