917b2af806185b3bba7ee1d2b985822815875d06
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"
36 #include <cairo.h>
38 //int showRuns=0;
39 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS);
41 static void nr_arena_shape_class_init(NRArenaShapeClass *klass);
42 static void nr_arena_shape_init(NRArenaShape *shape);
43 static void nr_arena_shape_finalize(NRObject *object);
45 static NRArenaItem *nr_arena_shape_children(NRArenaItem *item);
46 static void nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
47 static void nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child);
48 static void nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref);
50 static guint nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset);
51 static unsigned int nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags);
52 static guint nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb);
53 static NRArenaItem *nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int sticky);
55 static NRArenaItemClass *shape_parent_class;
57 NRType
58 nr_arena_shape_get_type(void)
59 {
60 static NRType type = 0;
61 if (!type) {
62 type = nr_object_register_type(NR_TYPE_ARENA_ITEM,
63 "NRArenaShape",
64 sizeof(NRArenaShapeClass),
65 sizeof(NRArenaShape),
66 (void (*)(NRObjectClass *)) nr_arena_shape_class_init,
67 (void (*)(NRObject *)) nr_arena_shape_init);
68 }
69 return type;
70 }
72 static void
73 nr_arena_shape_class_init(NRArenaShapeClass *klass)
74 {
75 NRObjectClass *object_class;
76 NRArenaItemClass *item_class;
78 object_class = (NRObjectClass *) klass;
79 item_class = (NRArenaItemClass *) klass;
81 shape_parent_class = (NRArenaItemClass *) ((NRObjectClass *) klass)->parent;
83 object_class->finalize = nr_arena_shape_finalize;
84 object_class->cpp_ctor = NRObject::invoke_ctor<NRArenaShape>;
86 item_class->children = nr_arena_shape_children;
87 item_class->add_child = nr_arena_shape_add_child;
88 item_class->set_child_position = nr_arena_shape_set_child_position;
89 item_class->remove_child = nr_arena_shape_remove_child;
90 item_class->update = nr_arena_shape_update;
91 item_class->render = nr_arena_shape_render;
92 item_class->clip = nr_arena_shape_clip;
93 item_class->pick = nr_arena_shape_pick;
94 }
96 /**
97 * Initializes the arena shape, setting all parameters to null, 0, false,
98 * or other defaults
99 */
100 static void
101 nr_arena_shape_init(NRArenaShape *shape)
102 {
103 shape->curve = NULL;
104 shape->style = NULL;
105 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
106 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
108 nr_matrix_set_identity(&shape->ctm);
109 shape->fill_painter = NULL;
110 shape->stroke_painter = NULL;
111 shape->cached_fill = NULL;
112 shape->cached_stroke = NULL;
113 shape->cached_fpartialy = false;
114 shape->cached_spartialy = false;
115 shape->fill_shp = NULL;
116 shape->stroke_shp = NULL;
118 shape->delayed_shp = false;
120 shape->approx_bbox.x0 = shape->approx_bbox.y0 = 0;
121 shape->approx_bbox.x1 = shape->approx_bbox.y1 = 0;
122 nr_matrix_set_identity(&shape->cached_fctm);
123 nr_matrix_set_identity(&shape->cached_sctm);
125 shape->markers = NULL;
126 }
128 static void
129 nr_arena_shape_finalize(NRObject *object)
130 {
131 NRArenaShape *shape = (NRArenaShape *) object;
133 if (shape->fill_shp) delete shape->fill_shp;
134 if (shape->stroke_shp) delete shape->stroke_shp;
135 if (shape->cached_fill) delete shape->cached_fill;
136 if (shape->cached_stroke) delete shape->cached_stroke;
137 if (shape->fill_painter) sp_painter_free(shape->fill_painter);
138 if (shape->stroke_painter) sp_painter_free(shape->stroke_painter);
140 if (shape->style) sp_style_unref(shape->style);
141 if (shape->curve) sp_curve_unref(shape->curve);
143 ((NRObjectClass *) shape_parent_class)->finalize(object);
144 }
146 /**
147 * Retrieves the markers from the item
148 */
149 static NRArenaItem *
150 nr_arena_shape_children(NRArenaItem *item)
151 {
152 NRArenaShape *shape = (NRArenaShape *) item;
154 return shape->markers;
155 }
157 /**
158 * Attaches child to item, and if ref is not NULL, sets it and ref->next as
159 * the prev and next items. If ref is NULL, then it sets the item's markers
160 * as the next items.
161 */
162 static void
163 nr_arena_shape_add_child(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
164 {
165 NRArenaShape *shape = (NRArenaShape *) item;
167 if (!ref) {
168 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
169 } else {
170 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
171 }
173 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
174 }
176 /**
177 * Removes child from the shape. If there are no prev items in
178 * the child, it sets items' markers to the next item in the child.
179 */
180 static void
181 nr_arena_shape_remove_child(NRArenaItem *item, NRArenaItem *child)
182 {
183 NRArenaShape *shape = (NRArenaShape *) item;
185 if (child->prev) {
186 nr_arena_item_detach(item, child);
187 } else {
188 shape->markers = nr_arena_item_detach(item, child);
189 }
191 nr_arena_item_request_update(item, NR_ARENA_ITEM_STATE_ALL, FALSE);
192 }
194 /**
195 * Detaches child from item, and if there are no previous items in child, it
196 * sets item's markers to the child. It then attaches the child back onto the item.
197 * If ref is null, it sets the markers to be the next item, otherwise it uses
198 * the next/prev items in ref.
199 */
200 static void
201 nr_arena_shape_set_child_position(NRArenaItem *item, NRArenaItem *child, NRArenaItem *ref)
202 {
203 NRArenaShape *shape = (NRArenaShape *) item;
205 if (child->prev) {
206 nr_arena_item_detach(item, child);
207 } else {
208 shape->markers = nr_arena_item_detach(item, child);
209 }
211 if (!ref) {
212 shape->markers = nr_arena_item_attach(item, child, NULL, shape->markers);
213 } else {
214 ref->next = nr_arena_item_attach(item, child, ref, ref->next);
215 }
217 nr_arena_item_request_render(child);
218 }
220 void nr_arena_shape_update_stroke(NRArenaShape *shape, NRGC* gc, NRRectL *area);
221 void nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape = false);
222 void nr_arena_shape_add_bboxes(NRArenaShape* shape,NRRect &bbox);
224 /**
225 * Updates the arena shape 'item' and all of its children, including the markers.
226 */
227 static guint
228 nr_arena_shape_update(NRArenaItem *item, NRRectL *area, NRGC *gc, guint state, guint reset)
229 {
230 NRRect bbox;
232 NRArenaShape *shape = NR_ARENA_SHAPE(item);
234 unsigned int beststate = NR_ARENA_ITEM_STATE_ALL;
236 unsigned int newstate;
237 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
238 newstate = nr_arena_item_invoke_update(child, area, gc, state, reset);
239 beststate = beststate & newstate;
240 }
242 if (!(state & NR_ARENA_ITEM_STATE_RENDER)) {
243 /* We do not have to create rendering structures */
244 shape->ctm = gc->transform;
245 if (state & NR_ARENA_ITEM_STATE_BBOX) {
246 if (shape->curve) {
247 NRBPath bp;
248 /* fixme: */
249 bbox.x0 = bbox.y0 = NR_HUGE;
250 bbox.x1 = bbox.y1 = -NR_HUGE;
251 bp.path = SP_CURVE_BPATH(shape->curve);
252 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
253 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
254 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
255 item->bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
256 item->bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
257 }
258 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
259 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
260 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
261 }
262 }
263 }
264 return (state | item->state);
265 }
267 shape->delayed_shp=true;
268 shape->ctm = gc->transform;
269 bbox.x0 = bbox.y0 = NR_HUGE;
270 bbox.x1 = bbox.y1 = -NR_HUGE;
272 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
274 if (shape->curve) {
275 NRBPath bp;
276 /* fixme: */
277 bbox.x0 = bbox.y0 = NR_HUGE;
278 bbox.x1 = bbox.y1 = -NR_HUGE;
279 bp.path = SP_CURVE_BPATH(shape->curve);
280 nr_path_matrix_bbox_union(&bp, gc->transform, &bbox);
281 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
282 float width, scale;
283 scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
284 width = MAX(0.125, shape->_stroke.width * scale);
285 if ( fabs(shape->_stroke.width * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
286 bbox.x0-=width;
287 bbox.x1+=width;
288 bbox.y0-=width;
289 bbox.y1+=width;
290 }
291 // those pesky miters, now
292 float miterMax=width*shape->_stroke.mitre_limit;
293 if ( miterMax > 0.01 ) {
294 // grunt mode. we should compute the various miters instead (one for each point on the curve)
295 bbox.x0-=miterMax;
296 bbox.x1+=miterMax;
297 bbox.y0-=miterMax;
298 bbox.y1+=miterMax;
299 }
300 }
301 } else {
302 }
303 shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
304 shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
305 shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
306 shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
307 if ( area && nr_rect_l_test_intersect(area, &shape->approx_bbox) ) shape->delayed_shp=false;
309 /* Release state data */
310 if (TRUE || !nr_matrix_test_transform_equal(&gc->transform, &shape->ctm, NR_EPSILON)) {
311 /* Concept test */
312 if (shape->fill_shp) {
313 delete shape->fill_shp;
314 shape->fill_shp = NULL;
315 }
316 }
317 if (shape->stroke_shp) {
318 delete shape->stroke_shp;
319 shape->stroke_shp = NULL;
320 }
321 if (shape->fill_painter) {
322 sp_painter_free(shape->fill_painter);
323 shape->fill_painter = NULL;
324 }
325 if (shape->stroke_painter) {
326 sp_painter_free(shape->stroke_painter);
327 shape->stroke_painter = NULL;
328 }
330 if (!shape->curve ||
331 !shape->style ||
332 sp_curve_is_empty(shape->curve) ||
333 (( shape->_fill.paint.type() == NRArenaShape::Paint::NONE ) &&
334 ( shape->_stroke.paint.type() == NRArenaShape::Paint::NONE && !outline) ))
335 {
336 item->bbox = shape->approx_bbox;
337 return NR_ARENA_ITEM_STATE_ALL;
338 }
340 /* Build state data */
341 if ( shape->delayed_shp ) {
342 item->bbox=shape->approx_bbox;
343 } else {
344 nr_arena_shape_update_stroke(shape, gc, area);
345 nr_arena_shape_update_fill(shape, gc, area);
347 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
348 nr_arena_shape_add_bboxes(shape,bbox);
350 shape->approx_bbox.x0 = (gint32)(bbox.x0 - 1.0F);
351 shape->approx_bbox.y0 = (gint32)(bbox.y0 - 1.0F);
352 shape->approx_bbox.x1 = (gint32)(bbox.x1 + 1.9999F);
353 shape->approx_bbox.y1 = (gint32)(bbox.y1 + 1.9999F);
354 }
356 if (nr_rect_d_test_empty(&bbox)) return NR_ARENA_ITEM_STATE_ALL;
358 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
359 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
360 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
361 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
362 nr_arena_request_render_rect(item->arena, &item->bbox);
364 item->render_opacity = TRUE;
365 if ( shape->_fill.paint.type() == NRArenaShape::Paint::SERVER ) {
366 if (gc && gc->parent) {
367 shape->fill_painter = sp_paint_server_painter_new(shape->_fill.paint.server(),
368 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
369 &shape->paintbox);
370 }
371 item->render_opacity = FALSE;
372 }
373 if ( shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER ) {
374 if (gc && gc->parent) {
375 shape->stroke_painter = sp_paint_server_painter_new(shape->_stroke.paint.server(),
376 NR::Matrix(&gc->transform), NR::Matrix(&gc->parent->transform),
377 &shape->paintbox);
378 }
379 item->render_opacity = FALSE;
380 }
381 if ( item->render_opacity == TRUE
382 && shape->_fill.paint.type() != NRArenaShape::Paint::NONE
383 && shape->_stroke.paint.type() != NRArenaShape::Paint::NONE )
384 {
385 // don't merge item opacity with paint opacity if there is a stroke on the fill
386 item->render_opacity = FALSE;
387 }
389 if (beststate & NR_ARENA_ITEM_STATE_BBOX) {
390 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
391 nr_rect_l_union(&item->bbox, &item->bbox, &child->bbox);
392 }
393 }
395 return NR_ARENA_ITEM_STATE_ALL;
396 }
398 int matrix_is_isometry(NR::Matrix p) {
399 NR::Matrix tp;
400 // transposition
401 tp[0]=p[0];
402 tp[1]=p[2];
403 tp[2]=p[1];
404 tp[3]=p[3];
405 for (int i = 4; i < 6; i++) // shut valgrind up :)
406 tp[i] = p[i] = 0;
407 NR::Matrix isom = tp*p; // A^T * A = adjunct?
408 // Is the adjunct nearly an identity function?
409 if (isom.is_translation(0.01)) {
410 // the transformation is an isometry -> no need to recompute
411 // the uncrossed polygon
412 if ( p.det() < 0 )
413 return -1;
414 else
415 return 1;
416 }
417 return 0;
418 }
420 static bool is_inner_area(NRRectL const &outer, NRRectL const &inner) {
421 return (outer.x0 <= inner.x0 && outer.y0 <= inner.y0 && outer.x1 >= inner.x1 && outer.y1 >= inner.y1);
422 }
424 /** force_shape is used for clipping paths, when we need the shape for clipping even if it's not filled */
425 void
426 nr_arena_shape_update_fill(NRArenaShape *shape, NRGC *gc, NRRectL *area, bool force_shape)
427 {
428 if ((shape->_fill.paint.type() != NRArenaShape::Paint::NONE || force_shape) &&
429 ((shape->curve->end > 2) || (SP_CURVE_BPATH(shape->curve)[1].code == NR_CURVETO)) ) {
430 if (TRUE || !shape->fill_shp) {
431 NR::Matrix cached_to_new = NR::identity();
432 int isometry = 0;
433 if ( shape->cached_fill ) {
434 if (shape->cached_fctm == gc->transform) {
435 isometry = 2; // identity
436 } else {
437 cached_to_new = shape->cached_fctm.inverse() * gc->transform;
438 isometry = matrix_is_isometry(cached_to_new);
439 }
440 if (0 != isometry && !is_inner_area(shape->cached_farea, *area))
441 isometry = 0;
442 }
443 if ( isometry == 0 ) {
444 if ( shape->cached_fill == NULL ) shape->cached_fill=new Shape;
445 shape->cached_fill->Reset();
447 Path* thePath=new Path;
448 Shape* theShape=new Shape;
449 {
450 NR::Matrix tempMat(gc->transform);
451 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve),tempMat,true);
452 }
454 if (is_inner_area(*area, NR_ARENA_ITEM(shape)->bbox)) {
455 thePath->Convert(1.0);
456 shape->cached_fpartialy = false;
457 } else {
458 thePath->Convert(area, 1.0);
459 shape->cached_fpartialy = true;
460 }
462 thePath->Fill(theShape, 0);
464 if ( shape->_fill.rule == NRArenaShape::EVEN_ODD ) {
465 shape->cached_fill->ConvertToShape(theShape, fill_oddEven);
466 // alternatively, this speeds up rendering of oddeven shapes but disables AA :(
467 //shape->cached_fill->Copy(theShape);
468 } else {
469 shape->cached_fill->ConvertToShape(theShape, fill_nonZero);
470 }
471 shape->cached_fctm=gc->transform;
472 shape->cached_farea = *area;
473 delete theShape;
474 delete thePath;
475 if ( shape->fill_shp == NULL )
476 shape->fill_shp = new Shape;
478 shape->fill_shp->Copy(shape->cached_fill);
480 } else if ( 2 == isometry ) {
481 if ( shape->fill_shp == NULL ) {
482 shape->fill_shp = new Shape;
483 shape->fill_shp->Copy(shape->cached_fill);
484 }
485 } else {
487 if ( shape->fill_shp == NULL )
488 shape->fill_shp = new Shape;
490 shape->fill_shp->Reset(shape->cached_fill->numberOfPoints(),
491 shape->cached_fill->numberOfEdges());
492 for (int i = 0; i < shape->cached_fill->numberOfPoints(); i++)
493 shape->fill_shp->AddPoint(shape->cached_fill->getPoint(i).x * cached_to_new);
494 if ( isometry == 1 ) {
495 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
496 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).st,
497 shape->cached_fill->getEdge(i).en);
498 } else if ( isometry == -1 ) { // need to flip poly.
499 for (int i = 0; i < shape->cached_fill->numberOfEdges(); i++)
500 shape->fill_shp->AddEdge(shape->cached_fill->getEdge(i).en,
501 shape->cached_fill->getEdge(i).st);
502 }
503 shape->fill_shp->ForceToPolygon();
504 shape->fill_shp->needPointsSorting();
505 shape->fill_shp->needEdgesSorting();
506 }
507 shape->delayed_shp |= shape->cached_fpartialy;
508 }
509 }
510 }
512 void
513 nr_arena_shape_update_stroke(NRArenaShape *shape,NRGC* gc, NRRectL *area)
514 {
515 SPStyle* style = shape->style;
517 float const scale = NR_MATRIX_DF_EXPANSION(&gc->transform);
519 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
521 if (outline) {
522 return; // cairo does not need the livarot path, hehe
523 }
525 if (outline ||
526 ((shape->_stroke.paint.type() != NRArenaShape::Paint::NONE) &&
527 ( fabs(shape->_stroke.width * scale) > 0.01 ))) { // sinon c'est 0=oon veut pas de bord
529 float style_width = MAX(0.125, shape->_stroke.width * scale);
530 float width;
531 if (outline) {
532 width = 0.5; // 1 pixel wide, independent of zoom
533 } else {
534 width = style_width;
535 }
537 NR::Matrix cached_to_new = NR::identity();
539 int isometry = 0;
540 if ( shape->cached_stroke ) {
541 if (shape->cached_sctm == gc->transform) {
542 isometry = 2; // identity
543 } else {
544 cached_to_new = shape->cached_sctm.inverse() * gc->transform;
545 isometry = matrix_is_isometry(cached_to_new);
546 }
547 if (0 != isometry && !is_inner_area(shape->cached_sarea, *area))
548 isometry = 0;
549 if (0 != isometry && width != shape->cached_width) {
550 // if this happens without setting style, we have just switched to outline or back
551 isometry = 0;
552 }
553 }
555 if ( isometry == 0 ) {
556 if ( shape->cached_stroke == NULL ) shape->cached_stroke=new Shape;
557 shape->cached_stroke->Reset();
558 Path* thePath = new Path;
559 Shape* theShape = new Shape;
560 {
561 NR::Matrix tempMat(gc->transform);
562 thePath->LoadArtBPath(SP_CURVE_BPATH(shape->curve), tempMat, true);
563 }
565 // add some padding to the rendering area, so clipped path does not go into a render area
566 NRRectL padded_area = *area;
567 padded_area.x0 -= (NR::ICoord)width;
568 padded_area.x1 += (NR::ICoord)width;
569 padded_area.y0 -= (NR::ICoord)width;
570 padded_area.y1 += (NR::ICoord)width;
571 if ((style->stroke_dash.n_dash && !outline) || is_inner_area(padded_area, NR_ARENA_ITEM(shape)->bbox)) {
572 thePath->Convert((outline) ? 4.0 : 1.0);
573 shape->cached_spartialy = false;
574 }
575 else {
576 thePath->Convert(&padded_area, (outline) ? 4.0 : 1.0);
577 shape->cached_spartialy = true;
578 }
580 if (style->stroke_dash.n_dash && !outline) {
581 thePath->DashPolylineFromStyle(style, scale, 1.0);
582 }
584 ButtType butt=butt_straight;
585 switch (shape->_stroke.cap) {
586 case NRArenaShape::BUTT_CAP:
587 butt = butt_straight;
588 break;
589 case NRArenaShape::ROUND_CAP:
590 butt = butt_round;
591 break;
592 case NRArenaShape::SQUARE_CAP:
593 butt = butt_square;
594 break;
595 }
596 JoinType join=join_straight;
597 switch (shape->_stroke.join) {
598 case NRArenaShape::MITRE_JOIN:
599 join = join_pointy;
600 break;
601 case NRArenaShape::ROUND_JOIN:
602 join = join_round;
603 break;
604 case NRArenaShape::BEVEL_JOIN:
605 join = join_straight;
606 break;
607 }
609 if (outline) {
610 butt = butt_straight;
611 join = join_straight;
612 }
614 thePath->Stroke(theShape, false, 0.5*width, join, butt,
615 0.5*width*shape->_stroke.mitre_limit);
618 if (outline) {
619 // speeds it up, but uses evenodd for the stroke shape (which does not matter for 1-pixel wide outline)
620 shape->cached_stroke->Copy(theShape);
621 } else {
622 shape->cached_stroke->ConvertToShape(theShape, fill_nonZero);
623 }
625 shape->cached_width = width;
627 shape->cached_sctm=gc->transform;
628 shape->cached_sarea = *area;
629 delete thePath;
630 delete theShape;
631 if ( shape->stroke_shp == NULL ) shape->stroke_shp=new Shape;
633 shape->stroke_shp->Copy(shape->cached_stroke);
635 } else if ( 2 == isometry ) {
636 if ( shape->stroke_shp == NULL ) {
637 shape->stroke_shp=new Shape;
638 shape->stroke_shp->Copy(shape->cached_stroke);
639 }
640 } else {
641 if ( shape->stroke_shp == NULL )
642 shape->stroke_shp=new Shape;
643 shape->stroke_shp->Reset(shape->cached_stroke->numberOfPoints(), shape->cached_stroke->numberOfEdges());
644 for (int i = 0; i < shape->cached_stroke->numberOfPoints(); i++)
645 shape->stroke_shp->AddPoint(shape->cached_stroke->getPoint(i).x * cached_to_new);
646 if ( isometry == 1 ) {
647 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
648 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).st,
649 shape->cached_stroke->getEdge(i).en);
650 } else if ( isometry == -1 ) {
651 for (int i = 0; i < shape->cached_stroke->numberOfEdges(); i++)
652 shape->stroke_shp->AddEdge(shape->cached_stroke->getEdge(i).en,
653 shape->cached_stroke->getEdge(i).st);
654 }
655 shape->stroke_shp->ForceToPolygon();
656 shape->stroke_shp->needPointsSorting();
657 shape->stroke_shp->needEdgesSorting();
658 }
659 shape->delayed_shp |= shape->cached_spartialy;
660 }
661 }
664 void
665 nr_arena_shape_add_bboxes(NRArenaShape* shape, NRRect &bbox)
666 {
667 if ( shape->stroke_shp ) {
668 shape->stroke_shp->CalcBBox();
669 shape->stroke_shp->leftX=floor(shape->stroke_shp->leftX);
670 shape->stroke_shp->rightX=ceil(shape->stroke_shp->rightX);
671 shape->stroke_shp->topY=floor(shape->stroke_shp->topY);
672 shape->stroke_shp->bottomY=ceil(shape->stroke_shp->bottomY);
673 if ( bbox.x0 >= bbox.x1 ) {
674 if ( shape->stroke_shp->leftX < shape->stroke_shp->rightX ) {
675 bbox.x0=shape->stroke_shp->leftX;
676 bbox.x1=shape->stroke_shp->rightX;
677 }
678 } else {
679 if ( shape->stroke_shp->leftX < bbox.x0 )
680 bbox.x0=shape->stroke_shp->leftX;
681 if ( shape->stroke_shp->rightX > bbox.x1 )
682 bbox.x1=shape->stroke_shp->rightX;
683 }
684 if ( bbox.y0 >= bbox.y1 ) {
685 if ( shape->stroke_shp->topY < shape->stroke_shp->bottomY ) {
686 bbox.y0=shape->stroke_shp->topY;
687 bbox.y1=shape->stroke_shp->bottomY;
688 }
689 } else {
690 if ( shape->stroke_shp->topY < bbox.y0 )
691 bbox.y0=shape->stroke_shp->topY;
692 if ( shape->stroke_shp->bottomY > bbox.y1 )
693 bbox.y1=shape->stroke_shp->bottomY;
694 }
695 }
696 if ( shape->fill_shp ) {
697 shape->fill_shp->CalcBBox();
698 shape->fill_shp->leftX=floor(shape->fill_shp->leftX);
699 shape->fill_shp->rightX=ceil(shape->fill_shp->rightX);
700 shape->fill_shp->topY=floor(shape->fill_shp->topY);
701 shape->fill_shp->bottomY=ceil(shape->fill_shp->bottomY);
702 if ( bbox.x0 >= bbox.x1 ) {
703 if ( shape->fill_shp->leftX < shape->fill_shp->rightX ) {
704 bbox.x0=shape->fill_shp->leftX;
705 bbox.x1=shape->fill_shp->rightX;
706 }
707 } else {
708 if ( shape->fill_shp->leftX < bbox.x0 ) bbox.x0=shape->fill_shp->leftX;
709 if ( shape->fill_shp->rightX > bbox.x1 ) bbox.x1=shape->fill_shp->rightX;
710 }
711 if ( bbox.y0 >= bbox.y1 ) {
712 if ( shape->fill_shp->topY < shape->fill_shp->bottomY ) {
713 bbox.y0=shape->fill_shp->topY;
714 bbox.y1=shape->fill_shp->bottomY;
715 }
716 } else {
717 if ( shape->fill_shp->topY < bbox.y0 ) bbox.y0=shape->fill_shp->topY;
718 if ( shape->fill_shp->bottomY > bbox.y1 ) bbox.y1=shape->fill_shp->bottomY;
719 }
720 }
721 }
723 /**
724 * Renders the item. Markers are just composed into the parent buffer.
725 */
726 static unsigned int
727 nr_arena_shape_render(NRArenaItem *item, NRRectL *area, NRPixBlock *pb, unsigned int flags)
728 {
729 NRArenaShape *shape = NR_ARENA_SHAPE(item);
731 if (!shape->curve) return item->state;
732 if (!shape->style) return item->state;
734 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
736 if (outline) { // fixme: if no problems reported, remove old outline stuff
737 NRPixBlock m;
738 int width = area->x1 - area->x0;
739 int height = area->y1 - area->y0;
741 //known bug: buffer for cairo must have stride divisible by 4, even though this is a one-byte-per-pixel mode
742 int rem = width % 4;
743 int x1 = area->x1;
744 if (rem != 0) {
745 width += (4 - rem);
746 x1 = area->x0 + width;
747 }
749 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, x1, area->y1, TRUE);
750 m.visible_area = pb->visible_area;
751 m.empty = FALSE;
753 cairo_surface_t* cst = cairo_image_surface_create_for_data
754 (NR_PIXBLOCK_PX(&m),
755 CAIRO_FORMAT_A8,
756 width,
757 height,
758 m.rs);
759 cairo_t *ct = cairo_create (cst);
760 cairo_set_source_rgba(ct, 0, 0, 0, 1.0);
761 cairo_set_line_width(ct, 0.5);
762 cairo_set_miter_limit(ct, 0.0);
763 cairo_set_tolerance(ct, 1.25); // low quality, but good enough for outline mode
764 cairo_new_path(ct);
766 NR::Matrix trans(shape->ctm);
767 NR::Point lastX(0,0);
768 bool closed = false;
769 NArtBpath *bpath = SP_CURVE_BPATH(shape->curve);
770 for (int i = 0; bpath[i].code != NR_END; i++) {
771 switch (bpath[i].code) {
772 case NR_MOVETO_OPEN:
773 case NR_MOVETO:
774 if (closed) cairo_close_path(ct);
775 closed = (bpath[i].code == NR_MOVETO);
776 lastX[NR::X] = bpath[i].x3;
777 lastX[NR::Y] = bpath[i].y3;
778 lastX*=trans;
779 lastX -= NR::Point(area->x0, area->y0);
780 cairo_move_to(ct, lastX[NR::X], lastX[NR::Y]);
781 break;
783 case NR_LINETO:
784 lastX[NR::X] = bpath[i].x3;
785 lastX[NR::Y] = bpath[i].y3;
786 lastX*=trans;
787 lastX -= NR::Point(area->x0, area->y0);
788 cairo_line_to(ct, lastX[NR::X], lastX[NR::Y]);
789 break;
791 case NR_CURVETO: {
792 NR::Point tm1, tm2, tm3;
793 tm1[0]=bpath[i].x1;
794 tm1[1]=bpath[i].y1;
795 tm2[0]=bpath[i].x2;
796 tm2[1]=bpath[i].y2;
797 tm3[0]=bpath[i].x3;
798 tm3[1]=bpath[i].y3;
799 tm1*=trans;
800 tm2*=trans;
801 tm3*=trans;
802 tm1 -= NR::Point(area->x0, area->y0);
803 tm2 -= NR::Point(area->x0, area->y0);
804 tm3 -= NR::Point(area->x0, area->y0);
805 cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
806 break;
807 }
809 default:
810 break;
811 }
812 }
814 cairo_stroke(ct);
816 cairo_destroy (ct);
817 cairo_surface_finish (cst);
819 guint32 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
820 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
821 pb->empty = FALSE;
823 cairo_surface_destroy (cst);
825 nr_pixblock_release(&m);
827 return item->state;
828 }
830 if ( shape->delayed_shp ) {
831 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
832 NRGC tempGC(NULL);
833 tempGC.transform=shape->ctm;
834 shape->delayed_shp = false;
835 nr_arena_shape_update_stroke(shape,&tempGC,&pb->visible_area);
836 nr_arena_shape_update_fill(shape,&tempGC,&pb->visible_area);
837 /* NRRect bbox;
838 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
839 nr_arena_shape_add_bboxes(shape,bbox);
840 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
841 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
842 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
843 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
844 shape->approx_bbox=item->bbox;*/
845 } else {
846 return item->state;
847 }
848 }
850 SPStyle const *style = shape->style;
851 if ( shape->fill_shp && !outline) {
852 NRPixBlock m;
853 guint32 rgba;
855 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
857 // if memory allocation failed, abort render
858 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
859 nr_pixblock_release (&m);
860 return (item->state);
861 }
863 m.visible_area = pb->visible_area;
864 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
865 m.empty = FALSE;
867 if (shape->_fill.paint.type() == NRArenaShape::Paint::NONE) {
868 // do not render fill in any way
869 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::COLOR) {
870 if ( item->render_opacity ) {
871 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
872 shape->_fill.opacity *
873 SP_SCALE24_TO_FLOAT(style->opacity.value));
874 } else {
875 rgba = sp_color_get_rgba32_falpha(&shape->_fill.paint.color(),
876 shape->_fill.opacity);
877 }
878 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
879 pb->empty = FALSE;
880 } else if (shape->_fill.paint.type() == NRArenaShape::Paint::SERVER) {
881 if (shape->fill_painter) {
882 nr_arena_render_paintserver_fill(pb, area, shape->fill_painter, shape->_fill.opacity, &m);
883 }
884 }
886 nr_pixblock_release(&m);
887 }
889 if ( shape->stroke_shp ) {
890 NRPixBlock m;
891 guint32 rgba;
893 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
895 // if memory allocation failed, abort render
896 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
897 nr_pixblock_release (&m);
898 return (item->state);
899 }
901 m.visible_area = pb->visible_area;
902 nr_pixblock_render_shape_mask_or(m, shape->stroke_shp);
903 m.empty = FALSE;
905 if (shape->_stroke.paint.type() == NRArenaShape::Paint::COLOR || outline) {
906 if (outline) {
907 rgba = NR_ARENA_ITEM(shape)->arena->outlinecolor;
908 } else if ( item->render_opacity ) {
909 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
910 shape->_stroke.opacity *
911 SP_SCALE24_TO_FLOAT(style->opacity.value));
912 } else {
913 rgba = sp_color_get_rgba32_falpha(&shape->_stroke.paint.color(),
914 shape->_stroke.opacity);
915 }
916 nr_blit_pixblock_mask_rgba32(pb, &m, rgba);
917 pb->empty = FALSE;
918 } else if (shape->_stroke.paint.type() == NRArenaShape::Paint::SERVER) {
919 if (shape->stroke_painter) {
920 nr_arena_render_paintserver_fill(pb, area, shape->stroke_painter, shape->_stroke.opacity, &m);
921 }
922 }
924 nr_pixblock_release(&m);
925 }
927 /* Just compose children into parent buffer */
928 for (NRArenaItem *child = shape->markers; child != NULL; child = child->next) {
929 unsigned int ret;
930 ret = nr_arena_item_invoke_render(child, area, pb, flags);
931 if (ret & NR_ARENA_ITEM_STATE_INVALID) return ret;
932 }
934 return item->state;
935 }
937 static guint
938 nr_arena_shape_clip(NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
939 {
940 NRArenaShape *shape = NR_ARENA_SHAPE(item);
941 if (!shape->curve) return item->state;
943 if ( shape->delayed_shp || shape->fill_shp == NULL) { // we need a fill shape no matter what
944 if ( nr_rect_l_test_intersect(area, &item->bbox) ) {
945 NRGC tempGC(NULL);
946 tempGC.transform=shape->ctm;
947 shape->delayed_shp = false;
948 nr_arena_shape_update_fill(shape, &tempGC, &pb->visible_area, true);
949 } else {
950 return item->state;
951 }
952 }
954 if ( shape->fill_shp ) {
955 NRPixBlock m;
957 /* fixme: We can OR in one step (Lauris) */
958 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, area->x0, area->y0, area->x1, area->y1, TRUE);
960 // if memory allocation failed, abort
961 if (m.size != NR_PIXBLOCK_SIZE_TINY && m.data.px == NULL) {
962 nr_pixblock_release (&m);
963 return (item->state);
964 }
966 m.visible_area = pb->visible_area;
967 nr_pixblock_render_shape_mask_or(m,shape->fill_shp);
969 for (int y = area->y0; y < area->y1; y++) {
970 unsigned char *s, *d;
971 s = NR_PIXBLOCK_PX(&m) + (y - area->y0) * m.rs;
972 d = NR_PIXBLOCK_PX(pb) + (y - area->y0) * pb->rs;
973 for (int x = area->x0; x < area->x1; x++) {
974 *d = NR_COMPOSEA_111(*s, *d);
975 d ++;
976 s ++;
977 }
978 }
979 nr_pixblock_release(&m);
980 pb->empty = FALSE;
981 }
983 return item->state;
984 }
986 static NRArenaItem *
987 nr_arena_shape_pick(NRArenaItem *item, NR::Point p, double delta, unsigned int /*sticky*/)
988 {
989 NRArenaShape *shape = NR_ARENA_SHAPE(item);
991 if (!shape->curve) return NULL;
992 if (!shape->style) return NULL;
993 if ( shape->delayed_shp ) {
994 NRRectL area, updateArea;
995 area.x0=(int)floor(p[NR::X]);
996 area.x1=(int)ceil(p[NR::X]);
997 area.y0=(int)floor(p[NR::Y]);
998 area.y1=(int)ceil(p[NR::Y]);
999 int idelta = (int)ceil(delta) + 1;
1000 // njh: inset rect
1001 area.x0-=idelta;
1002 area.x1+=idelta;
1003 area.y0-=idelta;
1004 area.y1+=idelta;
1005 if ( nr_rect_l_test_intersect(&area, &item->bbox) ) {
1006 NRGC tempGC(NULL);
1007 tempGC.transform=shape->ctm;
1008 updateArea = item->bbox;
1009 if (shape->cached_stroke)
1010 nr_rect_l_intersect (&updateArea, &updateArea, &shape->cached_sarea);
1012 shape->delayed_shp = false;
1013 nr_arena_shape_update_stroke(shape, &tempGC, &updateArea);
1014 nr_arena_shape_update_fill(shape, &tempGC, &updateArea);
1015 /* NRRect bbox;
1016 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
1017 nr_arena_shape_add_bboxes(shape,bbox);
1018 item->bbox.x0 = (gint32)(bbox.x0 - 1.0F);
1019 item->bbox.y0 = (gint32)(bbox.y0 - 1.0F);
1020 item->bbox.x1 = (gint32)(bbox.x1 + 1.0F);
1021 item->bbox.y1 = (gint32)(bbox.y1 + 1.0F);
1022 shape->approx_bbox=item->bbox;*/
1023 }
1024 }
1026 bool outline = (NR_ARENA_ITEM(shape)->arena->rendermode == RENDERMODE_OUTLINE);
1028 if (item->state & NR_ARENA_ITEM_STATE_RENDER) {
1029 if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1030 if (shape->fill_shp->PtWinding(p) > 0 ) return item;
1031 }
1032 if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1033 if (shape->stroke_shp->PtWinding(p) > 0 ) return item;
1034 }
1035 if (delta > 1e-3) {
1036 if (shape->fill_shp && (shape->_fill.paint.type() != NRArenaShape::Paint::NONE)) {
1037 if (distanceLessThanOrEqual(shape->fill_shp, p, delta)) return item;
1038 }
1039 if (shape->stroke_shp && (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline)) {
1040 if (distanceLessThanOrEqual(shape->stroke_shp, p, delta)) return item;
1041 }
1042 }
1043 } else {
1044 NRBPath bp;
1045 bp.path = SP_CURVE_BPATH(shape->curve);
1046 double dist = NR_HUGE;
1047 int wind = 0;
1048 nr_path_matrix_point_bbox_wind_distance(&bp, shape->ctm, p, NULL, &wind, &dist, NR_EPSILON);
1049 if (shape->_fill.paint.type() != NRArenaShape::Paint::NONE) {
1050 if (!shape->style->fill_rule.computed) {
1051 if (wind != 0) return item;
1052 } else {
1053 if (wind & 0x1) return item;
1054 }
1055 }
1056 if (shape->_stroke.paint.type() != NRArenaShape::Paint::NONE || outline) {
1057 /* fixme: We do not take stroke width into account here (Lauris) */
1058 if (dist < delta) return item;
1059 }
1060 }
1062 return NULL;
1063 }
1065 /**
1066 *
1067 * Requests a render of the shape, then if the shape is already a curve it
1068 * unrefs the old curve; if the new curve is valid it creates a copy of the
1069 * curve and adds it to the shape. Finally, it requests an update of the
1070 * arena for the shape.
1071 */
1072 void nr_arena_shape_set_path(NRArenaShape *shape, SPCurve *curve,bool justTrans)
1073 {
1074 g_return_if_fail(shape != NULL);
1075 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1077 if ( justTrans == false ) {
1078 // dirty cached versions
1079 if ( shape->cached_fill ) {
1080 delete shape->cached_fill;
1081 shape->cached_fill=NULL;
1082 }
1083 if ( shape->cached_stroke ) {
1084 delete shape->cached_stroke;
1085 shape->cached_stroke=NULL;
1086 }
1087 }
1089 nr_arena_item_request_render(NR_ARENA_ITEM(shape));
1091 if (shape->curve) {
1092 sp_curve_unref(shape->curve);
1093 shape->curve = NULL;
1094 }
1096 if (curve) {
1097 shape->curve = curve;
1098 sp_curve_ref(curve);
1099 }
1101 nr_arena_item_request_update(NR_ARENA_ITEM(shape), NR_ARENA_ITEM_STATE_ALL, FALSE);
1102 }
1104 void NRArenaShape::setFill(SPPaintServer *server) {
1105 _fill.paint.set(server);
1106 _invalidateCachedFill();
1107 }
1109 void NRArenaShape::setFill(SPColor const &color) {
1110 _fill.paint.set(color);
1111 _invalidateCachedFill();
1112 }
1114 void NRArenaShape::setFillOpacity(double opacity) {
1115 _fill.opacity = opacity;
1116 _invalidateCachedFill();
1117 }
1119 void NRArenaShape::setFillRule(NRArenaShape::FillRule rule) {
1120 _fill.rule = rule;
1121 _invalidateCachedFill();
1122 }
1124 void NRArenaShape::setStroke(SPPaintServer *server) {
1125 _stroke.paint.set(server);
1126 _invalidateCachedStroke();
1127 }
1129 void NRArenaShape::setStroke(SPColor const &color) {
1130 _stroke.paint.set(color);
1131 _invalidateCachedStroke();
1132 }
1134 void NRArenaShape::setStrokeOpacity(double opacity) {
1135 _stroke.opacity = opacity;
1136 _invalidateCachedStroke();
1137 }
1139 void NRArenaShape::setStrokeWidth(double width) {
1140 _stroke.width = width;
1141 _invalidateCachedStroke();
1142 }
1144 void NRArenaShape::setMitreLimit(double limit) {
1145 _stroke.mitre_limit = limit;
1146 _invalidateCachedStroke();
1147 }
1149 void NRArenaShape::setLineCap(NRArenaShape::CapType cap) {
1150 _stroke.cap = cap;
1151 _invalidateCachedStroke();
1152 }
1154 void NRArenaShape::setLineJoin(NRArenaShape::JoinType join) {
1155 _stroke.join = join;
1156 _invalidateCachedStroke();
1157 }
1159 /** nr_arena_shape_set_style
1160 *
1161 * Unrefs any existing style and ref's to the given one, then requests an update of the arena
1162 */
1163 void
1164 nr_arena_shape_set_style(NRArenaShape *shape, SPStyle *style)
1165 {
1166 g_return_if_fail(shape != NULL);
1167 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1169 if (style) sp_style_ref(style);
1170 if (shape->style) sp_style_unref(shape->style);
1171 shape->style = style;
1173 switch (style->fill.type) {
1174 case SP_PAINT_TYPE_NONE: {
1175 shape->setFill(NULL);
1176 break;
1177 }
1178 case SP_PAINT_TYPE_COLOR: {
1179 shape->setFill(style->fill.value.color);
1180 break;
1181 }
1182 case SP_PAINT_TYPE_PAINTSERVER: {
1183 shape->setFill(style->fill.value.paint.server);
1184 break;
1185 }
1186 default: {
1187 g_assert_not_reached();
1188 }
1189 }
1190 shape->setFillOpacity(SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1191 switch (style->fill_rule.computed) {
1192 case SP_WIND_RULE_EVENODD: {
1193 shape->setFillRule(NRArenaShape::EVEN_ODD);
1194 break;
1195 }
1196 case SP_WIND_RULE_NONZERO: {
1197 shape->setFillRule(NRArenaShape::NONZERO);
1198 break;
1199 }
1200 default: {
1201 g_assert_not_reached();
1202 }
1203 }
1205 switch (style->stroke.type) {
1206 case SP_PAINT_TYPE_NONE: {
1207 shape->setStroke(NULL);
1208 break;
1209 }
1210 case SP_PAINT_TYPE_COLOR: {
1211 shape->setStroke(style->stroke.value.color);
1212 break;
1213 }
1214 case SP_PAINT_TYPE_PAINTSERVER: {
1215 shape->setStroke(style->stroke.value.paint.server);
1216 break;
1217 }
1218 default: {
1219 g_assert_not_reached();
1220 }
1221 }
1222 shape->setStrokeWidth(style->stroke_width.computed);
1223 shape->setStrokeOpacity(SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1224 switch (style->stroke_linecap.computed) {
1225 case SP_STROKE_LINECAP_ROUND: {
1226 shape->setLineCap(NRArenaShape::ROUND_CAP);
1227 break;
1228 }
1229 case SP_STROKE_LINECAP_SQUARE: {
1230 shape->setLineCap(NRArenaShape::SQUARE_CAP);
1231 break;
1232 }
1233 case SP_STROKE_LINECAP_BUTT: {
1234 shape->setLineCap(NRArenaShape::BUTT_CAP);
1235 break;
1236 }
1237 default: {
1238 g_assert_not_reached();
1239 }
1240 }
1241 switch (style->stroke_linejoin.computed) {
1242 case SP_STROKE_LINEJOIN_ROUND: {
1243 shape->setLineJoin(NRArenaShape::ROUND_JOIN);
1244 break;
1245 }
1246 case SP_STROKE_LINEJOIN_BEVEL: {
1247 shape->setLineJoin(NRArenaShape::BEVEL_JOIN);
1248 break;
1249 }
1250 case SP_STROKE_LINEJOIN_MITER: {
1251 shape->setLineJoin(NRArenaShape::MITRE_JOIN);
1252 break;
1253 }
1254 default: {
1255 g_assert_not_reached();
1256 }
1257 }
1258 shape->setMitreLimit(style->stroke_miterlimit.value);
1260 //if shape has a filter
1261 if (style->filter.set && style->filter.filter)
1262 {
1263 shape->filter = new NR::Filter();
1264 shape->filter->set_x(style->filter.filter->x);
1265 shape->filter->set_y(style->filter.filter->y);
1266 shape->filter->set_width(style->filter.filter->width);
1267 shape->filter->set_height(style->filter.filter->height);
1269 //go through all SP filter primitives
1270 for(int i=0; i<style->filter.filter->_primitive_count; i++)
1271 {
1272 SPFilterPrimitive *primitive = style->filter.filter->_primitives[i];
1273 //if primitive is gaussianblur
1274 if(SP_IS_GAUSSIANBLUR(primitive)) {
1275 NR::FilterGaussian * gaussian = (NR::FilterGaussian *) shape->filter->add_primitive(NR::NR_FILTER_GAUSSIANBLUR);
1276 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
1277 float num = spblur->stdDeviation.getNumber();
1278 if( num>=0.0 )
1279 {
1280 float optnum = spblur->stdDeviation.getOptNumber();
1281 if( optnum>=0.0 )
1282 gaussian->set_deviation((double) num, (double) optnum);
1283 else
1284 gaussian->set_deviation((double) num);
1285 }
1286 }
1287 }
1288 }
1289 else
1290 {
1291 //no filter set for this shape
1292 shape->filter = NULL;
1293 }
1295 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1296 }
1298 void
1299 nr_arena_shape_set_paintbox(NRArenaShape *shape, NRRect const *pbox)
1300 {
1301 g_return_if_fail(shape != NULL);
1302 g_return_if_fail(NR_IS_ARENA_SHAPE(shape));
1303 g_return_if_fail(pbox != NULL);
1305 if ((pbox->x0 < pbox->x1) && (pbox->y0 < pbox->y1)) {
1306 shape->paintbox = *pbox;
1307 } else {
1308 /* fixme: We kill warning, although not sure what to do here (Lauris) */
1309 shape->paintbox.x0 = shape->paintbox.y0 = 0.0F;
1310 shape->paintbox.x1 = shape->paintbox.y1 = 256.0F;
1311 }
1313 nr_arena_item_request_update(shape, NR_ARENA_ITEM_STATE_ALL, FALSE);
1314 }
1316 void NRArenaShape::setPaintBox(NR::Rect const &pbox)
1317 {
1318 paintbox.x0 = pbox.min()[NR::X];
1319 paintbox.y0 = pbox.min()[NR::Y];
1320 paintbox.x1 = pbox.max()[NR::X];
1321 paintbox.y1 = pbox.max()[NR::Y];
1323 nr_arena_item_request_update(this, NR_ARENA_ITEM_STATE_ALL, FALSE);
1324 }
1326 static void
1327 shape_run_A8_OR(raster_info &dest,void */*data*/,int st,float vst,int en,float ven)
1328 {
1329 if ( st >= en ) return;
1330 if ( vst < 0 ) vst=0;
1331 if ( vst > 1 ) vst=1;
1332 if ( ven < 0 ) ven=0;
1333 if ( ven > 1 ) ven=1;
1334 float sv=vst;
1335 float dv=ven-vst;
1336 int len=en-st;
1337 unsigned char* d=(unsigned char*)dest.buffer;
1338 d+=(st-dest.startPix);
1339 if ( fabs(dv) < 0.001 ) {
1340 if ( vst > 0.999 ) {
1341 /* Simple copy */
1342 while (len > 0) {
1343 d[0] = 255;
1344 d += 1;
1345 len -= 1;
1346 }
1347 } else {
1348 sv*=256;
1349 unsigned int c0_24=(int)sv;
1350 c0_24&=0xFF;
1351 while (len > 0) {
1352 /* Draw */
1353 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1354 d += 1;
1355 len -= 1;
1356 }
1357 }
1358 } else {
1359 if ( en <= st+1 ) {
1360 sv=0.5*(vst+ven);
1361 sv*=256;
1362 unsigned int c0_24=(int)sv;
1363 c0_24&=0xFF;
1364 /* Draw */
1365 d[0] = NR_COMPOSEA_111(c0_24,d[0]);
1366 } else {
1367 dv/=len;
1368 sv+=0.5*dv; // correction trapezoidale
1369 sv*=16777216;
1370 dv*=16777216;
1371 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
1372 int s0_24 = static_cast<int>(dv);
1373 while (len > 0) {
1374 unsigned int ca;
1375 /* Draw */
1376 ca = c0_24 >> 16;
1377 if ( ca > 255 ) ca=255;
1378 d[0] = NR_COMPOSEA_111(ca,d[0]);
1379 d += 1;
1380 c0_24 += s0_24;
1381 c0_24 = CLAMP(c0_24, 0, 16777216);
1382 len -= 1;
1383 }
1384 }
1385 }
1386 }
1388 void nr_pixblock_render_shape_mask_or(NRPixBlock &m,Shape* theS)
1389 {
1390 theS->CalcBBox();
1391 float l = theS->leftX, r = theS->rightX, t = theS->topY, b = theS->bottomY;
1392 int il,ir,it,ib;
1393 il=(int)floor(l);
1394 ir=(int)ceil(r);
1395 it=(int)floor(t);
1396 ib=(int)ceil(b);
1398 if ( il >= m.area.x1 || ir <= m.area.x0 || it >= m.area.y1 || ib <= m.area.y0 ) return;
1399 if ( il < m.area.x0 ) il=m.area.x0;
1400 if ( it < m.area.y0 ) it=m.area.y0;
1401 if ( ir > m.area.x1 ) ir=m.area.x1;
1402 if ( ib > m.area.y1 ) ib=m.area.y1;
1404 /* This is the FloatLigne version. See svn (prior to Apr 2006) for versions using BitLigne or direct BitLigne. */
1405 int curPt;
1406 float curY;
1407 theS->BeginQuickRaster(curY, curPt);
1409 FloatLigne *theI = new FloatLigne();
1410 IntLigne *theIL = new IntLigne();
1412 theS->DirectQuickScan(curY, curPt, (float) it, true, 1.0);
1414 char *mdata = (char*)m.data.px;
1415 if ( m.size == NR_PIXBLOCK_SIZE_TINY ) mdata=(char*)m.data.p;
1416 uint32_t *ligStart = ((uint32_t*)(mdata + ((il - m.area.x0) + m.rs * (it - m.area.y0))));
1417 for (int y = it; y < ib; y++) {
1418 theI->Reset();
1419 theS->QuickScan(curY, curPt, ((float)(y+1)), theI, 1.0);
1420 theI->Flatten();
1421 theIL->Copy(theI);
1423 raster_info dest;
1424 dest.startPix=il;
1425 dest.endPix=ir;
1426 dest.sth=il;
1427 dest.stv=y;
1428 dest.buffer=ligStart;
1429 theIL->Raster(dest, NULL, shape_run_A8_OR);
1430 ligStart=((uint32_t*)(((char*)ligStart)+m.rs));
1431 }
1432 theS->EndQuickRaster();
1433 delete theI;
1434 delete theIL;
1435 }
1438 /*
1439 Local Variables:
1440 mode:c++
1441 c-file-style:"stroustrup"
1442 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1443 indent-tabs-mode:nil
1444 fill-column:99
1445 End:
1446 */
1447 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :