1 #define __NR_ARENA_ITEM_C__
3 /*
4 * RGBA display list system for inkscape
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #define noNR_ARENA_ITEM_VERBOSE
16 #define noNR_ARENA_ITEM_DEBUG_CASCADE
18 #include <cstring>
19 #include <string>
21 #include <libnr/nr-blit.h>
22 #include <libnr/nr-pixops.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-fns.h>
25 #include "nr-arena.h"
26 #include "nr-arena-item.h"
27 #include "gc-core.h"
28 #include "helper/geom.h"
30 #include "nr-filter.h"
31 #include "libnr/nr-rect.h"
32 #include "nr-arena-group.h"
33 #include "preferences.h"
35 namespace GC = Inkscape::GC;
37 static void nr_arena_item_class_init (NRArenaItemClass *klass);
38 static void nr_arena_item_init (NRArenaItem *item);
39 static void nr_arena_item_private_finalize (NRObject *object);
41 static NRObjectClass *parent_class;
43 NRType
44 nr_arena_item_get_type (void)
45 {
46 static NRType type = 0;
47 if (!type) {
48 type = nr_object_register_type (NR_TYPE_OBJECT,
49 "NRArenaItem",
50 sizeof (NRArenaItemClass),
51 sizeof (NRArenaItem),
52 (void (*)(NRObjectClass *))
53 nr_arena_item_class_init,
54 (void (*)(NRObject *))
55 nr_arena_item_init);
56 }
57 return type;
58 }
60 static void
61 nr_arena_item_class_init (NRArenaItemClass *klass)
62 {
63 NRObjectClass *object_class;
65 object_class = (NRObjectClass *) klass;
67 parent_class = ((NRObjectClass *) klass)->parent;
69 object_class->finalize = nr_arena_item_private_finalize;
70 object_class->cpp_ctor = NRObject::invoke_ctor < NRArenaItem >;
71 }
73 static void
74 nr_arena_item_init (NRArenaItem *item)
75 {
76 item->arena = NULL;
77 item->parent = NULL;
78 item->next = item->prev = NULL;
80 item->key = 0;
82 item->state = 0;
83 item->sensitive = TRUE;
84 item->visible = TRUE;
86 memset (&item->bbox, 0, sizeof (item->bbox));
87 item->transform = NULL;
88 item->opacity = 255;
89 item->render_opacity = FALSE;
91 item->transform = NULL;
92 item->clip = NULL;
93 item->mask = NULL;
94 item->px = NULL;
95 item->data = NULL;
96 item->filter = NULL;
97 item->background_pb = NULL;
98 item->background_new = false;
99 }
101 static void
102 nr_arena_item_private_finalize (NRObject *object)
103 {
104 NRArenaItem *item = static_cast < NRArenaItem * >(object);
106 item->px = NULL;
107 item->transform = NULL;
109 ((NRObjectClass *) (parent_class))->finalize (object);
110 }
112 NRArenaItem *
113 nr_arena_item_children (NRArenaItem *item)
114 {
115 nr_return_val_if_fail (item != NULL, NULL);
116 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
118 if (NR_ARENA_ITEM_VIRTUAL (item, children))
119 return NR_ARENA_ITEM_VIRTUAL (item, children) (item);
121 return NULL;
122 }
124 NRArenaItem *
125 nr_arena_item_last_child (NRArenaItem *item)
126 {
127 nr_return_val_if_fail (item != NULL, NULL);
128 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
130 if (NR_ARENA_ITEM_VIRTUAL (item, last_child)) {
131 return NR_ARENA_ITEM_VIRTUAL (item, last_child) (item);
132 } else {
133 NRArenaItem *ref = nr_arena_item_children (item);
134 if (ref)
135 while (ref->next)
136 ref = ref->next;
137 return ref;
138 }
139 }
141 void
142 nr_arena_item_add_child (NRArenaItem *item, NRArenaItem *child,
143 NRArenaItem *ref)
144 {
145 nr_return_if_fail (item != NULL);
146 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
147 nr_return_if_fail (child != NULL);
148 nr_return_if_fail (NR_IS_ARENA_ITEM (child));
149 nr_return_if_fail (child->parent == NULL);
150 nr_return_if_fail (child->prev == NULL);
151 nr_return_if_fail (child->next == NULL);
152 nr_return_if_fail (child->arena == item->arena);
153 nr_return_if_fail (child != ref);
154 nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
155 nr_return_if_fail (!ref || (ref->parent == item));
157 if (NR_ARENA_ITEM_VIRTUAL (item, add_child))
158 NR_ARENA_ITEM_VIRTUAL (item, add_child) (item, child, ref);
159 }
161 void
162 nr_arena_item_remove_child (NRArenaItem *item, NRArenaItem *child)
163 {
164 nr_return_if_fail (item != NULL);
165 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
166 nr_return_if_fail (child != NULL);
167 nr_return_if_fail (NR_IS_ARENA_ITEM (child));
168 nr_return_if_fail (child->parent == item);
170 if (NR_ARENA_ITEM_VIRTUAL (item, remove_child))
171 NR_ARENA_ITEM_VIRTUAL (item, remove_child) (item, child);
172 }
174 void
175 nr_arena_item_set_child_position (NRArenaItem *item, NRArenaItem *child,
176 NRArenaItem *ref)
177 {
178 nr_return_if_fail (item != NULL);
179 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
180 nr_return_if_fail (child != NULL);
181 nr_return_if_fail (NR_IS_ARENA_ITEM (child));
182 nr_return_if_fail (child->parent == item);
183 nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
184 nr_return_if_fail (!ref || (ref->parent == item));
186 if (NR_ARENA_ITEM_VIRTUAL (item, set_child_position))
187 NR_ARENA_ITEM_VIRTUAL (item, set_child_position) (item, child, ref);
188 }
190 NRArenaItem *
191 nr_arena_item_ref (NRArenaItem *item)
192 {
193 nr_object_ref ((NRObject *) item);
195 return item;
196 }
198 NRArenaItem *
199 nr_arena_item_unref (NRArenaItem *item)
200 {
201 nr_object_unref ((NRObject *) item);
203 return NULL;
204 }
206 unsigned int
207 nr_arena_item_invoke_update (NRArenaItem *item, NRRectL *area, NRGC *gc,
208 unsigned int state, unsigned int reset)
209 {
210 NRGC childgc (gc);
211 bool filter = (item->arena->rendermode == Inkscape::RENDERMODE_NORMAL);
213 nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
214 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
215 NR_ARENA_ITEM_STATE_INVALID);
216 nr_return_val_if_fail (!(state & NR_ARENA_ITEM_STATE_INVALID),
217 NR_ARENA_ITEM_STATE_INVALID);
219 #ifdef NR_ARENA_ITEM_DEBUG_CASCADE
220 printf ("Update %s:%p %x %x %x\n",
221 nr_type_name_from_instance ((GTypeInstance *) item), item, state,
222 item->state, reset);
223 #endif
225 /* return if in error */
226 if (item->state & NR_ARENA_ITEM_STATE_INVALID)
227 return item->state;
228 /* Set reset flags according to propagation status */
229 if (item->propagate) {
230 reset |= ~item->state;
231 item->propagate = FALSE;
232 }
233 /* Reset our state */
234 item->state &= ~reset;
235 /* Return if NOP */
236 if (!(~item->state & state))
237 return item->state;
238 /* Test whether to return immediately */
239 if (area && (item->state & NR_ARENA_ITEM_STATE_BBOX)) {
240 if (!nr_rect_l_test_intersect_ptr(area, &item->bbox))
241 return item->state;
242 }
244 /* Reset image cache, if not to be kept */
245 if (!(item->state & NR_ARENA_ITEM_STATE_IMAGE) && (item->px)) {
246 item->px = NULL;
247 }
249 /* Set up local gc */
250 childgc = *gc;
251 if (item->transform) {
252 childgc.transform = (*item->transform) * childgc.transform;
253 }
254 /* Remember the transformation matrix */
255 item->ctm = childgc.transform;
257 /* Invoke the real method */
258 item->state = NR_ARENA_ITEM_VIRTUAL (item, update) (item, area, &childgc, state, reset);
259 if (item->state & NR_ARENA_ITEM_STATE_INVALID)
260 return item->state;
261 /* Enlarge the bounding box to contain filter effects */
262 if (item->filter && filter) {
263 item->filter->bbox_enlarge (item->bbox);
264 }
265 // fixme: to fix the display glitches, in outline mode bbox must be a combination of
266 // full item bbox and its clip and mask (after we have the API to get these)
268 /* Clipping */
269 if (item->clip) {
270 // FIXME: since here we only need bbox, consider passing
271 // ((state & !(NR_ARENA_ITEM_STATE_RENDER)) | NR_ARENA_ITEM_STATE_BBOX)
272 // instead of state, so it does not have to create rendering structures in nr_arena_shape_update
273 unsigned int newstate = nr_arena_item_invoke_update (item->clip, area, &childgc, state, reset);
274 if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
275 item->state |= NR_ARENA_ITEM_STATE_INVALID;
276 return item->state;
277 }
278 nr_rect_l_intersect (&item->bbox, &item->bbox, &item->clip->bbox);
279 }
280 /* Masking */
281 if (item->mask) {
282 unsigned int newstate = nr_arena_item_invoke_update (item->mask, area, &childgc, state, reset);
283 if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
284 item->state |= NR_ARENA_ITEM_STATE_INVALID;
285 return item->state;
286 }
287 nr_rect_l_intersect (&item->bbox, &item->bbox, &item->mask->bbox);
288 }
290 return item->state;
291 }
293 /**
294 * Render item to pixblock.
295 *
296 * \return Has NR_ARENA_ITEM_STATE_RENDER set on success.
297 */
299 unsigned int
300 nr_arena_item_invoke_render (cairo_t *ct, NRArenaItem *item, NRRectL const *area,
301 NRPixBlock *pb, unsigned int flags)
302 {
303 bool outline = (item->arena->rendermode == Inkscape::RENDERMODE_OUTLINE);
304 bool filter = (item->arena->rendermode == Inkscape::RENDERMODE_NORMAL);
306 nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
307 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
308 NR_ARENA_ITEM_STATE_INVALID);
309 nr_return_val_if_fail (item->state & NR_ARENA_ITEM_STATE_BBOX,
310 item->state);
312 #ifdef NR_ARENA_ITEM_VERBOSE
313 printf ("Invoke render %p: %d %d - %d %d\n", item, area->x0, area->y0,
314 area->x1, area->y1);
315 #endif
317 /* If we are outside bbox just return successfully */
318 if (!item->visible)
319 return item->state | NR_ARENA_ITEM_STATE_RENDER;
321 NRRectL carea;
322 nr_rect_l_intersect (&carea, area, &item->bbox);
323 if (nr_rect_l_test_empty(carea))
324 return item->state | NR_ARENA_ITEM_STATE_RENDER;
325 if (item->filter && filter) {
326 item->filter->area_enlarge (carea, item);
327 nr_rect_l_intersect (&carea, &carea, &item->bbox);
328 }
330 if (outline) {
331 // No caching in outline mode for now; investigate if it really gives any advantage with cairo.
332 // Also no attempts to clip anything; just render everything: item, clip, mask
333 // First, render the object itself
334 unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, pb, flags);
335 if (state & NR_ARENA_ITEM_STATE_INVALID) {
336 /* Clean up and return error */
337 item->state |= NR_ARENA_ITEM_STATE_INVALID;
338 return item->state;
339 }
341 // render clip and mask, if any
342 guint32 saved_rgba = item->arena->outlinecolor; // save current outline color
343 // render clippath as an object, using a different color
344 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
345 if (item->clip) {
346 item->arena->outlinecolor = prefs->getInt("/options/wireframecolors/clips", 0x00ff00ff); // green clips
347 NR_ARENA_ITEM_VIRTUAL (item->clip, render) (ct, item->clip, &carea, pb, flags);
348 }
349 // render mask as an object, using a different color
350 if (item->mask) {
351 item->arena->outlinecolor = prefs->getInt("/options/wireframecolors/masks", 0x0000ffff); // blue masks
352 NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, pb, flags);
353 }
354 item->arena->outlinecolor = saved_rgba; // restore outline color
356 return item->state | NR_ARENA_ITEM_STATE_RENDER;
357 }
359 NRPixBlock cpb;
360 if (item->px) {
361 /* Has cache pixblock, render this and return */
362 nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P,
363 /* fixme: This probably cannot overflow, because we render only if visible */
364 /* fixme: and pixel cache is there only for small items */
365 /* fixme: But this still needs extra check (Lauris) */
366 item->bbox.x0, item->bbox.y0,
367 item->bbox.x1, item->bbox.y1,
368 item->px,
369 4 * (item->bbox.x1 - item->bbox.x0), FALSE,
370 FALSE);
371 nr_blit_pixblock_pixblock (pb, &cpb);
372 nr_pixblock_release (&cpb);
373 pb->empty = FALSE;
374 return item->state | NR_ARENA_ITEM_STATE_RENDER;
375 }
377 NRPixBlock *dpb = pb;
379 /* Setup cache if we can */
380 if ((!(flags & NR_ARENA_ITEM_RENDER_NO_CACHE)) &&
381 (carea.x0 <= item->bbox.x0) && (carea.y0 <= item->bbox.y0) &&
382 (carea.x1 >= item->bbox.x1) && (carea.y1 >= item->bbox.y1) &&
383 (((item->bbox.x1 - item->bbox.x0) * (item->bbox.y1 -
384 item->bbox.y0)) <= 4096)) {
385 // Item bbox is fully in renderable area and size is acceptable
386 carea.x0 = item->bbox.x0;
387 carea.y0 = item->bbox.y0;
388 carea.x1 = item->bbox.x1;
389 carea.y1 = item->bbox.y1;
390 item->px =
391 new (GC::ATOMIC) unsigned char[4 * (carea.x1 - carea.x0) *
392 (carea.y1 - carea.y0)];
393 nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P, carea.x0,
394 carea.y0, carea.x1, carea.y1, item->px,
395 4 * (carea.x1 - carea.x0), TRUE, TRUE);
396 cpb.visible_area = pb->visible_area;
397 dpb = &cpb;
398 // Set nocache flag for downstream rendering
399 flags |= NR_ARENA_ITEM_RENDER_NO_CACHE;
400 }
402 /* Determine, whether we need temporary buffer */
403 if (item->clip || item->mask
404 || ((item->opacity != 255) && !item->render_opacity)
405 || (item->filter && filter) || item->background_new
406 || (item->parent && item->parent->background_pb)) {
408 /* Setup and render item buffer */
409 NRPixBlock ipb;
410 nr_pixblock_setup_fast (&ipb, NR_PIXBLOCK_MODE_R8G8B8A8P,
411 carea.x0, carea.y0, carea.x1, carea.y1,
412 TRUE);
414 // if memory allocation failed, abort render
415 if (ipb.size != NR_PIXBLOCK_SIZE_TINY && ipb.data.px == NULL) {
416 nr_pixblock_release (&ipb);
417 return (item->state);
418 }
420 /* If background access is used, save the pixblock address.
421 * This address is set to NULL at the end of this block */
422 if (item->background_new ||
423 (item->parent && item->parent->background_pb)) {
424 item->background_pb = &ipb;
425 }
427 ipb.visible_area = pb->visible_area;
428 if (item->filter && filter) {
429 item->filter->area_enlarge (ipb.visible_area, item);
430 }
432 unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, &ipb, flags);
433 if (state & NR_ARENA_ITEM_STATE_INVALID) {
434 /* Clean up and return error */
435 nr_pixblock_release (&ipb);
436 if (dpb != pb)
437 nr_pixblock_release (dpb);
438 item->state |= NR_ARENA_ITEM_STATE_INVALID;
439 return item->state;
440 }
441 ipb.empty = FALSE;
443 /* Run filtering, if a filter is set for this object */
444 if (item->filter && filter) {
445 item->filter->render (item, &ipb);
446 }
448 if (item->clip || item->mask) {
449 /* Setup mask pixblock */
450 NRPixBlock mpb;
451 nr_pixblock_setup_fast (&mpb, NR_PIXBLOCK_MODE_A8, carea.x0,
452 carea.y0, carea.x1, carea.y1, TRUE);
454 if (mpb.data.px != NULL) { // if memory allocation was successful
456 mpb.visible_area = pb->visible_area;
457 /* Do clip if needed */
458 if (item->clip) {
459 state = nr_arena_item_invoke_clip (item->clip, &carea, &mpb);
460 if (state & NR_ARENA_ITEM_STATE_INVALID) {
461 /* Clean up and return error */
462 nr_pixblock_release (&mpb);
463 nr_pixblock_release (&ipb);
464 if (dpb != pb)
465 nr_pixblock_release (dpb);
466 item->state |= NR_ARENA_ITEM_STATE_INVALID;
467 return item->state;
468 }
469 mpb.empty = FALSE;
470 }
471 /* Do mask if needed */
472 if (item->mask) {
473 NRPixBlock tpb;
474 /* Set up yet another temporary pixblock */
475 nr_pixblock_setup_fast (&tpb, NR_PIXBLOCK_MODE_R8G8B8A8N,
476 carea.x0, carea.y0, carea.x1,
477 carea.y1, TRUE);
479 if (tpb.data.px != NULL) { // if memory allocation was successful
481 tpb.visible_area = pb->visible_area;
482 unsigned int state = NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, &tpb, flags);
483 if (state & NR_ARENA_ITEM_STATE_INVALID) {
484 /* Clean up and return error */
485 nr_pixblock_release (&tpb);
486 nr_pixblock_release (&mpb);
487 nr_pixblock_release (&ipb);
488 if (dpb != pb)
489 nr_pixblock_release (dpb);
490 item->state |= NR_ARENA_ITEM_STATE_INVALID;
491 return item->state;
492 }
493 /* Composite with clip */
494 if (item->clip) {
495 int x, y;
496 for (y = carea.y0; y < carea.y1; y++) {
497 unsigned char *s, *d;
498 s = NR_PIXBLOCK_PX (&tpb) + (y -
499 carea.y0) * tpb.rs;
500 d = NR_PIXBLOCK_PX (&mpb) + (y -
501 carea.y0) * mpb.rs;
502 for (x = carea.x0; x < carea.x1; x++) {
503 unsigned int m;
504 m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
505 d[0] =
506 FAST_DIV_ROUND < 3 * 255 * 255 >
507 (NR_PREMUL_123 (d[0], m));
508 s += 4;
509 d += 1;
510 }
511 }
512 } else {
513 int x, y;
514 for (y = carea.y0; y < carea.y1; y++) {
515 unsigned char *s, *d;
516 s = NR_PIXBLOCK_PX (&tpb) + (y -
517 carea.y0) * tpb.rs;
518 d = NR_PIXBLOCK_PX (&mpb) + (y -
519 carea.y0) * mpb.rs;
520 for (x = carea.x0; x < carea.x1; x++) {
521 unsigned int m;
522 m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
523 d[0] = FAST_DIV_ROUND < 3 * 255 > (m);
524 s += 4;
525 d += 1;
526 }
527 }
528 mpb.empty = FALSE;
529 }
530 }
531 nr_pixblock_release (&tpb);
532 }
533 /* Multiply with opacity if needed */
534 if ((item->opacity != 255) && !item->render_opacity
535 ) {
536 int x, y;
537 unsigned int a;
538 a = item->opacity;
539 for (y = carea.y0; y < carea.y1; y++) {
540 unsigned char *d;
541 d = NR_PIXBLOCK_PX (&mpb) + (y - carea.y0) * mpb.rs;
542 for (x = carea.x0; x < carea.x1; x++) {
543 d[0] = NR_PREMUL_111 (d[0], a);
544 d += 1;
545 }
546 }
547 }
548 /* Compose rendering pixblock int destination */
549 nr_blit_pixblock_pixblock_mask (dpb, &ipb, &mpb);
550 }
551 nr_pixblock_release (&mpb);
552 } else {
553 if (item->render_opacity) { // opacity was already rendered in, just copy to dpb here
554 nr_blit_pixblock_pixblock(dpb, &ipb);
555 } else { // copy while multiplying by opacity
556 nr_blit_pixblock_pixblock_alpha (dpb, &ipb, item->opacity);
557 }
558 }
559 nr_pixblock_release (&ipb);
560 dpb->empty = FALSE;
561 /* This pointer wouldn't be valid outside this block, so clear it */
562 item->background_pb = NULL;
563 } else {
564 /* Just render */
565 unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, dpb, flags);
566 if (state & NR_ARENA_ITEM_STATE_INVALID) {
567 /* Clean up and return error */
568 if (dpb != pb)
569 nr_pixblock_release (dpb);
570 item->state |= NR_ARENA_ITEM_STATE_INVALID;
571 return item->state;
572 }
573 dpb->empty = FALSE;
574 }
576 if (dpb != pb) {
577 /* Have to blit from cache */
578 nr_blit_pixblock_pixblock (pb, dpb);
579 nr_pixblock_release (dpb);
580 pb->empty = FALSE;
581 item->state |= NR_ARENA_ITEM_STATE_IMAGE;
582 }
584 return item->state | NR_ARENA_ITEM_STATE_RENDER;
585 }
587 unsigned int
588 nr_arena_item_invoke_clip (NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
589 {
590 nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
591 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
592 NR_ARENA_ITEM_STATE_INVALID);
593 /* we originally short-circuited if the object state included
594 * NR_ARENA_ITEM_STATE_CLIP (and showed a warning on the console);
595 * anyone know why we stopped doing so?
596 */
597 nr_return_val_if_fail ((pb->area.x1 - pb->area.x0) >=
598 (area->x1 - area->x0),
599 NR_ARENA_ITEM_STATE_INVALID);
600 nr_return_val_if_fail ((pb->area.y1 - pb->area.y0) >=
601 (area->y1 - area->y0),
602 NR_ARENA_ITEM_STATE_INVALID);
604 #ifdef NR_ARENA_ITEM_VERBOSE
605 printf ("Invoke clip by %p: %d %d - %d %d, item bbox %d %d - %d %d\n",
606 item, area->x0, area->y0, area->x1, area->y1, (&item->bbox)->x0,
607 (&item->bbox)->y0, (&item->bbox)->x1, (&item->bbox)->y1);
608 #endif
610 if (item->visible && nr_rect_l_test_intersect_ptr(area, &item->bbox)) {
611 /* Need render that item */
612 if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->clip) {
613 return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
614 clip (item, area, pb);
615 }
616 }
618 return item->state;
619 }
621 NRArenaItem *
622 nr_arena_item_invoke_pick (NRArenaItem *item, Geom::Point p, double delta,
623 unsigned int sticky)
624 {
625 nr_return_val_if_fail (item != NULL, NULL);
626 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
628 // Sometimes there's no BBOX in item->state, reason unknown (bug 992817); I made this not an assert to remove the warning
629 if (!(item->state & NR_ARENA_ITEM_STATE_BBOX)
630 || !(item->state & NR_ARENA_ITEM_STATE_PICK))
631 return NULL;
633 if (!sticky && !(item->visible && item->sensitive))
634 return NULL;
636 // TODO: rewrite using Geom::Rect
637 const double x = p[Geom::X];
638 const double y = p[Geom::Y];
640 if (((x + delta) >= item->bbox.x0) &&
641 ((x - delta) < item->bbox.x1) &&
642 ((y + delta) >= item->bbox.y0) && ((y - delta) < item->bbox.y1)) {
643 if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->pick)
644 return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
645 pick (item, p, delta, sticky);
646 }
648 return NULL;
649 }
651 void
652 nr_arena_item_request_update (NRArenaItem *item, unsigned int reset,
653 unsigned int propagate)
654 {
655 nr_return_if_fail (item != NULL);
656 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
657 nr_return_if_fail (!(reset & NR_ARENA_ITEM_STATE_INVALID));
659 if (propagate && !item->propagate)
660 item->propagate = TRUE;
662 if (item->state & reset) {
663 item->state &= ~reset;
664 if (item->parent) {
665 nr_arena_item_request_update (item->parent, reset, FALSE);
666 } else {
667 nr_arena_request_update (item->arena, item);
668 }
669 }
670 }
672 void
673 nr_arena_item_request_render (NRArenaItem *item)
674 {
675 nr_return_if_fail (item != NULL);
676 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
678 nr_arena_request_render_rect (item->arena, &item->bbox);
679 }
681 /* Public */
683 NRArenaItem *
684 nr_arena_item_unparent (NRArenaItem *item)
685 {
686 nr_return_val_if_fail (item != NULL, NULL);
687 nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
689 nr_arena_item_request_render (item);
691 if (item->parent) {
692 nr_arena_item_remove_child (item->parent, item);
693 }
695 return NULL;
696 }
698 void
699 nr_arena_item_append_child (NRArenaItem *parent, NRArenaItem *child)
700 {
701 nr_return_if_fail (parent != NULL);
702 nr_return_if_fail (NR_IS_ARENA_ITEM (parent));
703 nr_return_if_fail (child != NULL);
704 nr_return_if_fail (NR_IS_ARENA_ITEM (child));
705 nr_return_if_fail (parent->arena == child->arena);
706 nr_return_if_fail (child->parent == NULL);
707 nr_return_if_fail (child->prev == NULL);
708 nr_return_if_fail (child->next == NULL);
710 nr_arena_item_add_child (parent, child, nr_arena_item_last_child (parent));
711 }
713 void
714 nr_arena_item_set_transform (NRArenaItem *item, Geom::Matrix const &transform)
715 {
716 Geom::Matrix const t (transform);
717 nr_arena_item_set_transform (item, &t);
718 }
720 void
721 nr_arena_item_set_transform (NRArenaItem *item, Geom::Matrix const *transform)
722 {
723 nr_return_if_fail (item != NULL);
724 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
726 if (!transform && !item->transform)
727 return;
729 const Geom::Matrix *md = (item->transform) ? item->transform : &GEOM_MATRIX_IDENTITY;
730 const Geom::Matrix *ms = (transform) ? transform : &GEOM_MATRIX_IDENTITY;
732 if (!Geom::matrix_equalp(*md, *ms, NR_EPSILON)) {
733 nr_arena_item_request_render (item);
734 if (!transform || transform->isIdentity()) {
735 /* Set to identity affine */
736 item->transform = NULL;
737 } else {
738 if (!item->transform)
739 item->transform = new (GC::ATOMIC) Geom::Matrix ();
740 *item->transform = *transform;
741 }
742 nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
743 }
744 }
746 void
747 nr_arena_item_set_opacity (NRArenaItem *item, double opacity)
748 {
749 nr_return_if_fail (item != NULL);
750 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
752 nr_arena_item_request_render (item);
754 item->opacity = (unsigned int) (opacity * 255.9999);
755 }
757 void
758 nr_arena_item_set_sensitive (NRArenaItem *item, unsigned int sensitive)
759 {
760 nr_return_if_fail (item != NULL);
761 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
763 /* fixme: mess with pick/repick... */
765 item->sensitive = sensitive;
766 }
768 void
769 nr_arena_item_set_visible (NRArenaItem *item, unsigned int visible)
770 {
771 nr_return_if_fail (item != NULL);
772 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
774 item->visible = visible;
776 nr_arena_item_request_render (item);
777 }
779 void
780 nr_arena_item_set_clip (NRArenaItem *item, NRArenaItem *clip)
781 {
782 nr_return_if_fail (item != NULL);
783 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
784 nr_return_if_fail (!clip || NR_IS_ARENA_ITEM (clip));
786 if (clip != item->clip) {
787 nr_arena_item_request_render (item);
788 if (item->clip)
789 item->clip = nr_arena_item_detach (item, item->clip);
790 if (clip)
791 item->clip = nr_arena_item_attach (item, clip, NULL, NULL);
792 nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
793 }
794 }
796 void
797 nr_arena_item_set_mask (NRArenaItem *item, NRArenaItem *mask)
798 {
799 nr_return_if_fail (item != NULL);
800 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
801 nr_return_if_fail (!mask || NR_IS_ARENA_ITEM (mask));
803 if (mask != item->mask) {
804 nr_arena_item_request_render (item);
805 if (item->mask)
806 item->mask = nr_arena_item_detach (item, item->mask);
807 if (mask)
808 item->mask = nr_arena_item_attach (item, mask, NULL, NULL);
809 nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
810 }
811 }
813 void
814 nr_arena_item_set_order (NRArenaItem *item, int order)
815 {
816 nr_return_if_fail (item != NULL);
817 nr_return_if_fail (NR_IS_ARENA_ITEM (item));
819 if (!item->parent)
820 return;
822 NRArenaItem *children = nr_arena_item_children (item->parent);
824 NRArenaItem *ref = NULL;
825 int pos = 0;
826 for (NRArenaItem *child = children; child != NULL; child = child->next) {
827 if (pos >= order)
828 break;
829 if (child != item) {
830 ref = child;
831 pos += 1;
832 }
833 }
835 nr_arena_item_set_child_position (item->parent, item, ref);
836 }
838 void
839 nr_arena_item_set_item_bbox (NRArenaItem *item, Geom::OptRect &bbox)
840 {
841 nr_return_if_fail(item != NULL);
842 nr_return_if_fail(NR_IS_ARENA_ITEM(item));
844 item->item_bbox = bbox;
845 }
847 /** Returns a background image for use with filter effects. */
848 NRPixBlock *
849 nr_arena_item_get_background (NRArenaItem const *item, int depth)
850 {
851 NRPixBlock *pb;
852 if (!item->background_pb)
853 return NULL;
854 if (item->background_new) {
855 pb = new NRPixBlock ();
856 nr_pixblock_setup_fast (pb, item->background_pb->mode,
857 item->background_pb->area.x0,
858 item->background_pb->area.y0,
859 item->background_pb->area.x1,
860 item->background_pb->area.y1, true);
861 if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) // allocation failed
862 return NULL;
863 } else if (item->parent) {
864 pb = nr_arena_item_get_background (item->parent, depth + 1);
865 } else
866 return NULL;
868 if (depth > 0)
869 nr_blit_pixblock_pixblock (pb, item->background_pb);
871 return pb;
872 }
874 /* Helpers */
876 NRArenaItem *
877 nr_arena_item_attach (NRArenaItem *parent, NRArenaItem *child,
878 NRArenaItem *prev, NRArenaItem *next)
879 {
880 nr_return_val_if_fail (parent != NULL, NULL);
881 nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
882 nr_return_val_if_fail (child != NULL, NULL);
883 nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
884 nr_return_val_if_fail (child->parent == NULL, NULL);
885 nr_return_val_if_fail (child->prev == NULL, NULL);
886 nr_return_val_if_fail (child->next == NULL, NULL);
887 nr_return_val_if_fail (!prev || NR_IS_ARENA_ITEM (prev), NULL);
888 nr_return_val_if_fail (!prev || (prev->parent == parent), NULL);
889 nr_return_val_if_fail (!prev || (prev->next == next), NULL);
890 nr_return_val_if_fail (!next || NR_IS_ARENA_ITEM (next), NULL);
891 nr_return_val_if_fail (!next || (next->parent == parent), NULL);
892 nr_return_val_if_fail (!next || (next->prev == prev), NULL);
894 child->parent = parent;
895 child->prev = prev;
896 child->next = next;
898 if (prev)
899 prev->next = child;
900 if (next)
901 next->prev = child;
903 return child;
904 }
906 NRArenaItem *
907 nr_arena_item_detach (NRArenaItem *parent, NRArenaItem *child)
908 {
909 nr_return_val_if_fail (parent != NULL, NULL);
910 nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
911 nr_return_val_if_fail (child != NULL, NULL);
912 nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
913 nr_return_val_if_fail (child->parent == parent, NULL);
915 NRArenaItem *prev = child->prev;
916 NRArenaItem *next = child->next;
918 child->parent = NULL;
919 child->prev = NULL;
920 child->next = NULL;
922 if (prev)
923 prev->next = next;
924 if (next)
925 next->prev = prev;
927 return next;
928 }
930 /*
931 Local Variables:
932 mode:c++
933 c-file-style:"stroustrup"
934 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
935 indent-tabs-mode:nil
936 fill-column:99
937 End:
938 */
939 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :