Code

revert jasper's way overzealous fix in png-write.cpp rev 13700; new fix in item_rende...
[inkscape.git] / src / display / nr-arena-item.cpp
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
19 #include <libnr/nr-blit.h>
20 #include <libnr/nr-pixops.h>
21 #include "nr-arena.h"
22 #include "nr-arena-item.h"
23 #include "gc-core.h"
25 #include "nr-filter.h"
26 #include "libnr/nr-rect.h"
27 #include "nr-arena-group.h"
28 #include "prefs-utils.h"
30 namespace GC = Inkscape::GC;
32 static void nr_arena_item_class_init (NRArenaItemClass *klass);
33 static void nr_arena_item_init (NRArenaItem *item);
34 static void nr_arena_item_private_finalize (NRObject *object);
36 static NRObjectClass *parent_class;
38 NRType 
39 nr_arena_item_get_type (void)
40 {
41     static NRType type = 0;
42     if (!type) {
43         type = nr_object_register_type (NR_TYPE_OBJECT,
44                                         "NRArenaItem",
45                                         sizeof (NRArenaItemClass),
46                                         sizeof (NRArenaItem),
47                                         (void (*)(NRObjectClass *))
48                                         nr_arena_item_class_init,
49                                         (void (*)(NRObject *))
50                                         nr_arena_item_init);
51     }
52     return type;
53 }
55 static void
56 nr_arena_item_class_init (NRArenaItemClass *klass)
57 {
58     NRObjectClass *object_class;
60     object_class = (NRObjectClass *) klass;
62     parent_class = ((NRObjectClass *) klass)->parent;
64     object_class->finalize = nr_arena_item_private_finalize;
65     object_class->cpp_ctor = NRObject::invoke_ctor < NRArenaItem >;
66 }
68 static void
69 nr_arena_item_init (NRArenaItem *item)
70 {
71     item->arena = NULL;
72     item->parent = NULL;
73     item->next = item->prev = NULL;
75     item->key = 0;
77     item->state = 0;
78     item->sensitive = TRUE;
79     item->visible = TRUE;
81     memset (&item->bbox, 0, sizeof (item->bbox));
82     item->transform = NULL;
83     item->opacity = 255;
84     item->render_opacity = FALSE;
86     item->transform = NULL;
87     item->clip = NULL;
88     item->mask = NULL;
89     item->px = NULL;
90     item->data = NULL;
91     item->filter = NULL;
92     item->background_pb = NULL;
93     item->background_new = false;
94 }
96 static void
97 nr_arena_item_private_finalize (NRObject *object)
98 {
99     NRArenaItem *item = static_cast < NRArenaItem * >(object);
101     item->px = NULL;
102     item->transform = NULL;
104     ((NRObjectClass *) (parent_class))->finalize (object);
107 NRArenaItem *
108 nr_arena_item_children (NRArenaItem *item)
110     nr_return_val_if_fail (item != NULL, NULL);
111     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
113     if (NR_ARENA_ITEM_VIRTUAL (item, children))
114         return NR_ARENA_ITEM_VIRTUAL (item, children) (item);
116     return NULL;
119 NRArenaItem *
120 nr_arena_item_last_child (NRArenaItem *item)
122     nr_return_val_if_fail (item != NULL, NULL);
123     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
125     if (NR_ARENA_ITEM_VIRTUAL (item, last_child)) {
126         return NR_ARENA_ITEM_VIRTUAL (item, last_child) (item);
127     } else {
128         NRArenaItem *ref = nr_arena_item_children (item);
129         if (ref)
130             while (ref->next)
131                 ref = ref->next;
132         return ref;
133     }
136 void
137 nr_arena_item_add_child (NRArenaItem *item, NRArenaItem *child,
138                          NRArenaItem *ref)
140     nr_return_if_fail (item != NULL);
141     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
142     nr_return_if_fail (child != NULL);
143     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
144     nr_return_if_fail (child->parent == NULL);
145     nr_return_if_fail (child->prev == NULL);
146     nr_return_if_fail (child->next == NULL);
147     nr_return_if_fail (child->arena == item->arena);
148     nr_return_if_fail (child != ref);
149     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
150     nr_return_if_fail (!ref || (ref->parent == item));
152     if (NR_ARENA_ITEM_VIRTUAL (item, add_child))
153         NR_ARENA_ITEM_VIRTUAL (item, add_child) (item, child, ref);
156 void
157 nr_arena_item_remove_child (NRArenaItem *item, NRArenaItem *child)
159     nr_return_if_fail (item != NULL);
160     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
161     nr_return_if_fail (child != NULL);
162     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
163     nr_return_if_fail (child->parent == item);
165     if (NR_ARENA_ITEM_VIRTUAL (item, remove_child))
166         NR_ARENA_ITEM_VIRTUAL (item, remove_child) (item, child);
169 void
170 nr_arena_item_set_child_position (NRArenaItem *item, NRArenaItem *child,
171                                   NRArenaItem *ref)
173     nr_return_if_fail (item != NULL);
174     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
175     nr_return_if_fail (child != NULL);
176     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
177     nr_return_if_fail (child->parent == item);
178     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
179     nr_return_if_fail (!ref || (ref->parent == item));
181     if (NR_ARENA_ITEM_VIRTUAL (item, set_child_position))
182         NR_ARENA_ITEM_VIRTUAL (item, set_child_position) (item, child, ref);
185 NRArenaItem *
186 nr_arena_item_ref (NRArenaItem *item)
188     nr_object_ref ((NRObject *) item);
190     return item;
193 NRArenaItem *
194 nr_arena_item_unref (NRArenaItem *item)
196     nr_object_unref ((NRObject *) item);
198     return NULL;
201 unsigned int
202 nr_arena_item_invoke_update (NRArenaItem *item, NRRectL *area, NRGC *gc,
203                              unsigned int state, unsigned int reset)
205     NRGC childgc (gc);
207     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
208     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
209                            NR_ARENA_ITEM_STATE_INVALID);
210     nr_return_val_if_fail (!(state & NR_ARENA_ITEM_STATE_INVALID),
211                            NR_ARENA_ITEM_STATE_INVALID);
213 #ifdef NR_ARENA_ITEM_DEBUG_CASCADE
214     printf ("Update %s:%p %x %x %x\n",
215             nr_type_name_from_instance ((GTypeInstance *) item), item, state,
216             item->state, reset);
217 #endif
219     /* return if in error */
220     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
221         return item->state;
222     /* Set reset flags according to propagation status */
223     if (item->propagate) {
224         reset |= ~item->state;
225         item->propagate = FALSE;
226     }
227     /* Reset our state */
228     item->state &= ~reset;
229     /* Return if NOP */
230     if (!(~item->state & state))
231         return item->state;
232     /* Test whether to return immediately */
233     if (area && (item->state & NR_ARENA_ITEM_STATE_BBOX)) {
234         if (!nr_rect_l_test_intersect (area, &item->bbox))
235             return item->state;
236     }
238     /* Reset image cache, if not to be kept */
239     if (!(item->state & NR_ARENA_ITEM_STATE_IMAGE) && (item->px)) {
240         item->px = NULL;
241     }
243     /* Set up local gc */
244     childgc = *gc;
245     if (item->transform) {
246         nr_matrix_multiply (&childgc.transform, item->transform,
247                             &childgc.transform);
248     }
249     /* Remember the transformation matrix */
250     item->ctm = childgc.transform;
252     /* Invoke the real method */
253     item->state =
254         NR_ARENA_ITEM_VIRTUAL (item, update) (item, area, &childgc, state,
255                                               reset);
256     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
257         return item->state;
258     /* Enlarge the bounding box to contain filter effects */
259     if (item->filter) {
260         item->filter->bbox_enlarge (item->bbox);
261     }
262     // fixme: to fix the display glitches, in outline mode bbox must be a combination of 
263     // full item bbox and its clip and mask (after we have the API to get these)
265     /* Clipping */
266     if (item->clip) {
267         // FIXME: since here we only need bbox, consider passing 
268         // ((state & !(NR_ARENA_ITEM_STATE_RENDER)) | NR_ARENA_ITEM_STATE_BBOX)
269         // instead of state, so it does not have to create rendering structures in nr_arena_shape_update
270         unsigned int newstate = nr_arena_item_invoke_update (item->clip, area, &childgc, state, reset); 
271         if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
272             item->state |= NR_ARENA_ITEM_STATE_INVALID;
273             return item->state;
274         }
275         nr_rect_l_intersect (&item->bbox, &item->bbox, &item->clip->bbox);
276     }
277     /* Masking */
278     if (item->mask) {
279         unsigned int newstate = nr_arena_item_invoke_update (item->mask, area, &childgc, state, reset);
280         if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
281             item->state |= NR_ARENA_ITEM_STATE_INVALID;
282             return item->state;
283         }
284         nr_rect_l_intersect (&item->bbox, &item->bbox, &item->mask->bbox);
285     }
287     return item->state;
290 /**
291  *    Render item to pixblock.
292  *
293  *    \return Has NR_ARENA_ITEM_STATE_RENDER set on success.
294  */
296 unsigned int
297 nr_arena_item_invoke_render (cairo_t *ct, NRArenaItem *item, NRRectL const *area,
298                              NRPixBlock *pb, unsigned int flags)
300    bool outline = (item->arena->rendermode == RENDERMODE_OUTLINE);
302     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
303     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
304                            NR_ARENA_ITEM_STATE_INVALID);
305     nr_return_val_if_fail (item->state & NR_ARENA_ITEM_STATE_BBOX,
306                            item->state);
308 #ifdef NR_ARENA_ITEM_VERBOSE
309     printf ("Invoke render %p: %d %d - %d %d\n", item, area->x0, area->y0,
310             area->x1, area->y1);
311 #endif
313     /* If we are outside bbox just return successfully */
314     if (!item->visible)
315         return item->state | NR_ARENA_ITEM_STATE_RENDER;
317     NRRectL carea;
318     nr_rect_l_intersect (&carea, area, &item->bbox);
319     if (nr_rect_l_test_empty (&carea))
320         return item->state | NR_ARENA_ITEM_STATE_RENDER;
321     if (item->filter && !outline) {
322         item->filter->area_enlarge (carea, item->ctm);
323         nr_rect_l_intersect (&carea, &carea, &item->bbox);
324     }
326     if (outline) {
327     // No caching in outline mode for now; investigate if it really gives any advantage with cairo.
328     // Also no attempts to clip anything; just render everything: item, clip, mask   
329             // First, render the object itself 
330             unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, pb, flags);
331             if (state & NR_ARENA_ITEM_STATE_INVALID) {
332                 /* Clean up and return error */
333                 item->state |= NR_ARENA_ITEM_STATE_INVALID;
334                 return item->state;
335             }
337             // render clip and mask, if any
338             guint32 saved_rgba = item->arena->outlinecolor; // save current outline color
339             // render clippath as an object, using a different color
340             if (item->clip) {
341                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "clips", 0x00ff00ff); // green clips
342                 NR_ARENA_ITEM_VIRTUAL (item->clip, render) (ct, item->clip, &carea, pb, flags);
343             } 
344             // render mask as an object, using a different color
345             if (item->mask) {
346                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "masks", 0x0000ffff); // blue masks
347                 NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, pb, flags);
348             }
349             item->arena->outlinecolor = saved_rgba; // restore outline color
351             return item->state | NR_ARENA_ITEM_STATE_RENDER;
352     }
354     NRPixBlock cpb;
355     if (item->px) {
356         /* Has cache pixblock, render this and return */
357         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P,
358                                   /* fixme: This probably cannot overflow, because we render only if visible */
359                                   /* fixme: and pixel cache is there only for small items */
360                                   /* fixme: But this still needs extra check (Lauris) */
361                                   item->bbox.x0, item->bbox.y0,
362                                   item->bbox.x1, item->bbox.y1,
363                                   item->px,
364                                   4 * (item->bbox.x1 - item->bbox.x0), FALSE,
365                                   FALSE);
366         nr_blit_pixblock_pixblock (pb, &cpb);
367         nr_pixblock_release (&cpb);
368         pb->empty = FALSE;
369         return item->state | NR_ARENA_ITEM_STATE_RENDER;
370     }
372     NRPixBlock *dpb = pb;
374     /* Setup cache if we can */
375     if ((!(flags & NR_ARENA_ITEM_RENDER_NO_CACHE)) &&
376         (carea.x0 <= item->bbox.x0) && (carea.y0 <= item->bbox.y0) &&
377         (carea.x1 >= item->bbox.x1) && (carea.y1 >= item->bbox.y1) &&
378         (((item->bbox.x1 - item->bbox.x0) * (item->bbox.y1 -
379                                              item->bbox.y0)) <= 4096)) {
380         // Item bbox is fully in renderable area and size is acceptable
381         carea.x0 = item->bbox.x0;
382         carea.y0 = item->bbox.y0;
383         carea.x1 = item->bbox.x1;
384         carea.y1 = item->bbox.y1;
385         item->px =
386             new (GC::ATOMIC) unsigned char[4 * (carea.x1 - carea.x0) *
387                                            (carea.y1 - carea.y0)];
388         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P, carea.x0,
389                                   carea.y0, carea.x1, carea.y1, item->px,
390                                   4 * (carea.x1 - carea.x0), TRUE, TRUE);
391         cpb.visible_area = pb->visible_area;
392         dpb = &cpb;
393         // Set nocache flag for downstream rendering
394         flags |= NR_ARENA_ITEM_RENDER_NO_CACHE;
395     } 
397     /* Determine, whether we need temporary buffer */
398     if (item->clip || item->mask
399         || ((item->opacity != 255) && !item->render_opacity)
400         || (item->filter) || item->background_new
401         || (item->parent && item->parent->background_pb)) {
403         /* Setup and render item buffer */
404         NRPixBlock ipb;
405         nr_pixblock_setup_fast (&ipb, NR_PIXBLOCK_MODE_R8G8B8A8P,
406                                 carea.x0, carea.y0, carea.x1, carea.y1,
407                                 TRUE);
409         //  if memory allocation failed, abort render
410         if (ipb.size != NR_PIXBLOCK_SIZE_TINY && ipb.data.px == NULL) {
411             nr_pixblock_release (&ipb);
412             return (item->state);
413         }
415         /* If background access is used, save the pixblock address.
416          * This address is set to NULL at the end of this block */
417         if (item->background_new ||
418             (item->parent && item->parent->background_pb)) {
419             item->background_pb = &ipb;
420         }
422         ipb.visible_area = pb->visible_area;
423         if (item->filter) {
424               item->filter->area_enlarge (ipb.visible_area, item->ctm);
425         }
427         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, &ipb, flags);
428         if (state & NR_ARENA_ITEM_STATE_INVALID) {
429             /* Clean up and return error */
430             nr_pixblock_release (&ipb);
431             if (dpb != pb)
432                 nr_pixblock_release (dpb);
433             item->state |= NR_ARENA_ITEM_STATE_INVALID;
434             return item->state;
435         }
436         ipb.empty = FALSE;
438         /* Run filtering, if a filter is set for this object */
439         if (item->filter) {
440             item->filter->render (item, &ipb);
441         }
443         if (item->clip || item->mask) {
444             /* Setup mask pixblock */
445             NRPixBlock mpb;
446             nr_pixblock_setup_fast (&mpb, NR_PIXBLOCK_MODE_A8, carea.x0,
447                                     carea.y0, carea.x1, carea.y1, TRUE);
449             if (mpb.data.px != NULL) { // if memory allocation was successful
451                 mpb.visible_area = pb->visible_area;
452                 /* Do clip if needed */
453                 if (item->clip) {
454                     state = nr_arena_item_invoke_clip (item->clip, &carea, &mpb);
455                     if (state & NR_ARENA_ITEM_STATE_INVALID) {
456                         /* Clean up and return error */
457                         nr_pixblock_release (&mpb);
458                         nr_pixblock_release (&ipb);
459                         if (dpb != pb)
460                             nr_pixblock_release (dpb);
461                         item->state |= NR_ARENA_ITEM_STATE_INVALID;
462                         return item->state;
463                     }
464                     mpb.empty = FALSE;
465                 }
466                 /* Do mask if needed */
467                 if (item->mask) {
468                     NRPixBlock tpb;
469                     /* Set up yet another temporary pixblock */
470                     nr_pixblock_setup_fast (&tpb, NR_PIXBLOCK_MODE_R8G8B8A8N,
471                                             carea.x0, carea.y0, carea.x1,
472                                             carea.y1, TRUE);
474                     if (tpb.data.px != NULL) { // if memory allocation was successful
476                         tpb.visible_area = pb->visible_area;
477                         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, &tpb, flags);
478                         if (state & NR_ARENA_ITEM_STATE_INVALID) {
479                             /* Clean up and return error */
480                             nr_pixblock_release (&tpb);
481                             nr_pixblock_release (&mpb);
482                             nr_pixblock_release (&ipb);
483                             if (dpb != pb)
484                                 nr_pixblock_release (dpb);
485                             item->state |= NR_ARENA_ITEM_STATE_INVALID;
486                             return item->state;
487                         }
488                         /* Composite with clip */
489                         if (item->clip) {
490                             int x, y;
491                             for (y = carea.y0; y < carea.y1; y++) {
492                                 unsigned char *s, *d;
493                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
494                                                              carea.y0) * tpb.rs;
495                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
496                                                              carea.y0) * mpb.rs;
497                                 for (x = carea.x0; x < carea.x1; x++) {
498                                     unsigned int m;
499                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
500                                     d[0] =
501                                         FAST_DIV_ROUND < 3 * 255 * 255 >
502                                         (NR_PREMUL_123 (d[0], m));
503                                     s += 4;
504                                     d += 1;
505                                 }
506                             }
507                         } else {
508                             int x, y;
509                             for (y = carea.y0; y < carea.y1; y++) {
510                                 unsigned char *s, *d;
511                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
512                                                              carea.y0) * tpb.rs;
513                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
514                                                              carea.y0) * mpb.rs;
515                                 for (x = carea.x0; x < carea.x1; x++) {
516                                     unsigned int m;
517                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
518                                     d[0] = FAST_DIV_ROUND < 3 * 255 > (m);
519                                     s += 4;
520                                     d += 1;
521                                 }
522                             }
523                             mpb.empty = FALSE;
524                         }
525                     }
526                     nr_pixblock_release (&tpb);
527                 }
528                 /* Multiply with opacity if needed */
529                 if ((item->opacity != 255) && !item->render_opacity
530                     ) {
531                     int x, y;
532                     unsigned int a;
533                     a = item->opacity;
534                     for (y = carea.y0; y < carea.y1; y++) {
535                         unsigned char *d;
536                         d = NR_PIXBLOCK_PX (&mpb) + (y - carea.y0) * mpb.rs;
537                         for (x = carea.x0; x < carea.x1; x++) {
538                             d[0] = NR_PREMUL_111 (d[0], a);
539                             d += 1;
540                         }
541                     }
542                 }
543                 /* Compose rendering pixblock int destination */
544                 nr_blit_pixblock_pixblock_mask (dpb, &ipb, &mpb);
545             }
546             nr_pixblock_release (&mpb);
547         } else {
548             if (item->render_opacity) { // opacity was already rendered in, just copy to dpb here
549                 nr_blit_pixblock_pixblock(dpb, &ipb);
550             } else { // copy while multiplying by opacity
551                 nr_blit_pixblock_pixblock_alpha (dpb, &ipb, item->opacity);
552             }
553         }
554         nr_pixblock_release (&ipb);
555         dpb->empty = FALSE;
556         /* This pointer wouldn't be valid outside this block, so clear it */
557         item->background_pb = NULL;
558     } else {
559         /* Just render */
560         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, dpb, flags);
561         if (state & NR_ARENA_ITEM_STATE_INVALID) {
562             /* Clean up and return error */
563             if (dpb != pb)
564                 nr_pixblock_release (dpb);
565             item->state |= NR_ARENA_ITEM_STATE_INVALID;
566             return item->state;
567         }
568         dpb->empty = FALSE;
569     }
571     if (dpb != pb) {
572         /* Have to blit from cache */
573         nr_blit_pixblock_pixblock (pb, dpb);
574         nr_pixblock_release (dpb);
575         pb->empty = FALSE;
576         item->state |= NR_ARENA_ITEM_STATE_IMAGE;
577     }
579     return item->state | NR_ARENA_ITEM_STATE_RENDER;
582 unsigned int
583 nr_arena_item_invoke_clip (NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
585     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
586     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
587                            NR_ARENA_ITEM_STATE_INVALID);
588     /* we originally short-circuited if the object state included
589      * NR_ARENA_ITEM_STATE_CLIP (and showed a warning on the console);
590      * anyone know why we stopped doing so?
591      */
592     nr_return_val_if_fail ((pb->area.x1 - pb->area.x0) >=
593                            (area->x1 - area->x0),
594                            NR_ARENA_ITEM_STATE_INVALID);
595     nr_return_val_if_fail ((pb->area.y1 - pb->area.y0) >=
596                            (area->y1 - area->y0),
597                            NR_ARENA_ITEM_STATE_INVALID);
599 #ifdef NR_ARENA_ITEM_VERBOSE
600     printf ("Invoke clip by %p: %d %d - %d %d, item bbox %d %d - %d %d\n",
601             item, area->x0, area->y0, area->x1, area->y1, (&item->bbox)->x0,
602             (&item->bbox)->y0, (&item->bbox)->x1, (&item->bbox)->y1);
603 #endif
605     if (item->visible && nr_rect_l_test_intersect (area, &item->bbox)) {
606         /* Need render that item */
607         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->clip) {
608             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
609                 clip (item, area, pb);
610         }
611     }
613     return item->state;
616 NRArenaItem *
617 nr_arena_item_invoke_pick (NRArenaItem *item, NR::Point p, double delta,
618                            unsigned int sticky)
620     nr_return_val_if_fail (item != NULL, NULL);
621     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
623     // Sometimes there's no BBOX in item->state, reason unknown (bug 992817); I made this not an assert to remove the warning
624     if (!(item->state & NR_ARENA_ITEM_STATE_BBOX)
625         || !(item->state & NR_ARENA_ITEM_STATE_PICK))
626         return NULL;
628     if (!sticky && !(item->visible && item->sensitive))
629         return NULL;
631     // TODO: rewrite using NR::Rect
632     const double x = p[NR::X];
633     const double y = p[NR::Y];
635     if (((x + delta) >= item->bbox.x0) &&
636         ((x - delta) < item->bbox.x1) &&
637         ((y + delta) >= item->bbox.y0) && ((y - delta) < item->bbox.y1)) {
638         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->pick)
639             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
640                 pick (item, p, delta, sticky);
641     }
643     return NULL;
646 void
647 nr_arena_item_request_update (NRArenaItem *item, unsigned int reset,
648                               unsigned int propagate)
650     nr_return_if_fail (item != NULL);
651     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
652     nr_return_if_fail (!(reset & NR_ARENA_ITEM_STATE_INVALID));
654     if (propagate && !item->propagate)
655         item->propagate = TRUE;
657     if (item->state & reset) {
658         item->state &= ~reset;
659         if (item->parent) {
660             nr_arena_item_request_update (item->parent, reset, FALSE);
661         } else {
662             nr_arena_request_update (item->arena, item);
663         }
664     }
667 void
668 nr_arena_item_request_render (NRArenaItem *item)
670     nr_return_if_fail (item != NULL);
671     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
673     nr_arena_request_render_rect (item->arena, &item->bbox);
676 /* Public */
678 NRArenaItem *
679 nr_arena_item_unparent (NRArenaItem *item)
681     nr_return_val_if_fail (item != NULL, NULL);
682     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
684     nr_arena_item_request_render (item);
686     if (item->parent) {
687         nr_arena_item_remove_child (item->parent, item);
688     }
690     return NULL;
693 void
694 nr_arena_item_append_child (NRArenaItem *parent, NRArenaItem *child)
696     nr_return_if_fail (parent != NULL);
697     nr_return_if_fail (NR_IS_ARENA_ITEM (parent));
698     nr_return_if_fail (child != NULL);
699     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
700     nr_return_if_fail (parent->arena == child->arena);
701     nr_return_if_fail (child->parent == NULL);
702     nr_return_if_fail (child->prev == NULL);
703     nr_return_if_fail (child->next == NULL);
705     nr_arena_item_add_child (parent, child, nr_arena_item_last_child (parent));
708 void
709 nr_arena_item_set_transform (NRArenaItem *item, NR::Matrix const &transform)
711     NRMatrix const t (transform);
712     nr_arena_item_set_transform (item, &t);
715 void
716 nr_arena_item_set_transform (NRArenaItem *item, NRMatrix const *transform)
718     nr_return_if_fail (item != NULL);
719     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
721     if (!transform && !item->transform)
722         return;
724     const NRMatrix *md = (item->transform) ? item->transform : &NR_MATRIX_IDENTITY;
725     const NRMatrix *ms = (transform) ? transform : &NR_MATRIX_IDENTITY;
727     if (!NR_MATRIX_DF_TEST_CLOSE (md, ms, NR_EPSILON)) {
728         nr_arena_item_request_render (item);
729         if (!transform || nr_matrix_test_identity (transform, NR_EPSILON)) {
730             /* Set to identity affine */
731             item->transform = NULL;
732         } else {
733             if (!item->transform)
734                 item->transform = new (GC::ATOMIC) NRMatrix ();
735             *item->transform = *transform;
736         }
737         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
738     }
741 void
742 nr_arena_item_set_opacity (NRArenaItem *item, double opacity)
744     nr_return_if_fail (item != NULL);
745     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
747     nr_arena_item_request_render (item);
749     item->opacity = (unsigned int) (opacity * 255.9999);
752 void
753 nr_arena_item_set_sensitive (NRArenaItem *item, unsigned int sensitive)
755     nr_return_if_fail (item != NULL);
756     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
758     /* fixme: mess with pick/repick... */
760     item->sensitive = sensitive;
763 void
764 nr_arena_item_set_visible (NRArenaItem *item, unsigned int visible)
766     nr_return_if_fail (item != NULL);
767     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
769     item->visible = visible;
771     nr_arena_item_request_render (item);
774 void
775 nr_arena_item_set_clip (NRArenaItem *item, NRArenaItem *clip)
777     nr_return_if_fail (item != NULL);
778     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
779     nr_return_if_fail (!clip || NR_IS_ARENA_ITEM (clip));
781     if (clip != item->clip) {
782         nr_arena_item_request_render (item);
783         if (item->clip)
784             item->clip = nr_arena_item_detach (item, item->clip);
785         if (clip)
786             item->clip = nr_arena_item_attach (item, clip, NULL, NULL);
787         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
788     }
791 void
792 nr_arena_item_set_mask (NRArenaItem *item, NRArenaItem *mask)
794     nr_return_if_fail (item != NULL);
795     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
796     nr_return_if_fail (!mask || NR_IS_ARENA_ITEM (mask));
798     if (mask != item->mask) {
799         nr_arena_item_request_render (item);
800         if (item->mask)
801             item->mask = nr_arena_item_detach (item, item->mask);
802         if (mask)
803             item->mask = nr_arena_item_attach (item, mask, NULL, NULL);
804         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
805     }
808 void
809 nr_arena_item_set_order (NRArenaItem *item, int order)
811     nr_return_if_fail (item != NULL);
812     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
814     if (!item->parent)
815         return;
817     NRArenaItem *children = nr_arena_item_children (item->parent);
819     NRArenaItem *ref = NULL;
820     int pos = 0;
821     for (NRArenaItem *child = children; child != NULL; child = child->next) {
822         if (pos >= order)
823             break;
824         if (child != item) {
825             ref = child;
826             pos += 1;
827         }
828     }
830     nr_arena_item_set_child_position (item->parent, item, ref);
833 /** Returns a background image for use with filter effects. */
834 NRPixBlock *
835 nr_arena_item_get_background (NRArenaItem const *item, int depth)
837     NRPixBlock *pb;
838     if (!item->background_pb)
839         return NULL;
840     if (item->background_new) {
841         pb = new NRPixBlock ();
842         nr_pixblock_setup_fast (pb, item->background_pb->mode,
843                                 item->background_pb->area.x0,
844                                 item->background_pb->area.y0,
845                                 item->background_pb->area.x1,
846                                 item->background_pb->area.y1, true);
847         if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) // allocation failed
848             return NULL;
849     } else if (item->parent) {
850         pb = nr_arena_item_get_background (item->parent, depth + 1);
851     } else
852         return NULL;
854     if (depth > 0)
855         nr_blit_pixblock_pixblock (pb, item->background_pb);
857     return pb;
860 /* Helpers */
862 NRArenaItem *
863 nr_arena_item_attach (NRArenaItem *parent, NRArenaItem *child,
864                       NRArenaItem *prev, NRArenaItem *next)
866     nr_return_val_if_fail (parent != NULL, NULL);
867     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
868     nr_return_val_if_fail (child != NULL, NULL);
869     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
870     nr_return_val_if_fail (child->parent == NULL, NULL);
871     nr_return_val_if_fail (child->prev == NULL, NULL);
872     nr_return_val_if_fail (child->next == NULL, NULL);
873     nr_return_val_if_fail (!prev || NR_IS_ARENA_ITEM (prev), NULL);
874     nr_return_val_if_fail (!prev || (prev->parent == parent), NULL);
875     nr_return_val_if_fail (!prev || (prev->next == next), NULL);
876     nr_return_val_if_fail (!next || NR_IS_ARENA_ITEM (next), NULL);
877     nr_return_val_if_fail (!next || (next->parent == parent), NULL);
878     nr_return_val_if_fail (!next || (next->prev == prev), NULL);
880     child->parent = parent;
881     child->prev = prev;
882     child->next = next;
884     if (prev)
885         prev->next = child;
886     if (next)
887         next->prev = child;
889     return child;
892 NRArenaItem *
893 nr_arena_item_detach (NRArenaItem *parent, NRArenaItem *child)
895     nr_return_val_if_fail (parent != NULL, NULL);
896     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
897     nr_return_val_if_fail (child != NULL, NULL);
898     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
899     nr_return_val_if_fail (child->parent == parent, NULL);
901     NRArenaItem *prev = child->prev;
902     NRArenaItem *next = child->next;
904     child->parent = NULL;
905     child->prev = NULL;
906     child->next = NULL;
908     if (prev)
909         prev->next = next;
910     if (next)
911         next->prev = prev;
913     return next;
916 /*
917   Local Variables:
918   mode:c++
919   c-file-style:"stroustrup"
920   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
921   indent-tabs-mode:nil
922   fill-column:99
923   End:
924 */
925 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :