Code

No more NRMatrix or NRPoint.
[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
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"
29 #include "nr-filter.h"
30 #include "libnr/nr-rect.h"
31 #include "nr-arena-group.h"
32 #include "prefs-utils.h"
34 namespace GC = Inkscape::GC;
36 static void nr_arena_item_class_init (NRArenaItemClass *klass);
37 static void nr_arena_item_init (NRArenaItem *item);
38 static void nr_arena_item_private_finalize (NRObject *object);
40 static NRObjectClass *parent_class;
42 NRType 
43 nr_arena_item_get_type (void)
44 {
45     static NRType type = 0;
46     if (!type) {
47         type = nr_object_register_type (NR_TYPE_OBJECT,
48                                         "NRArenaItem",
49                                         sizeof (NRArenaItemClass),
50                                         sizeof (NRArenaItem),
51                                         (void (*)(NRObjectClass *))
52                                         nr_arena_item_class_init,
53                                         (void (*)(NRObject *))
54                                         nr_arena_item_init);
55     }
56     return type;
57 }
59 static void
60 nr_arena_item_class_init (NRArenaItemClass *klass)
61 {
62     NRObjectClass *object_class;
64     object_class = (NRObjectClass *) klass;
66     parent_class = ((NRObjectClass *) klass)->parent;
68     object_class->finalize = nr_arena_item_private_finalize;
69     object_class->cpp_ctor = NRObject::invoke_ctor < NRArenaItem >;
70 }
72 static void
73 nr_arena_item_init (NRArenaItem *item)
74 {
75     item->arena = NULL;
76     item->parent = NULL;
77     item->next = item->prev = NULL;
79     item->key = 0;
81     item->state = 0;
82     item->sensitive = TRUE;
83     item->visible = TRUE;
85     memset (&item->bbox, 0, sizeof (item->bbox));
86     item->transform = NULL;
87     item->opacity = 255;
88     item->render_opacity = FALSE;
90     item->transform = NULL;
91     item->clip = NULL;
92     item->mask = NULL;
93     item->px = NULL;
94     item->data = NULL;
95     item->filter = NULL;
96     item->background_pb = NULL;
97     item->background_new = false;
98 }
100 static void
101 nr_arena_item_private_finalize (NRObject *object)
103     NRArenaItem *item = static_cast < NRArenaItem * >(object);
105     item->px = NULL;
106     item->transform = NULL;
108     ((NRObjectClass *) (parent_class))->finalize (object);
111 NRArenaItem *
112 nr_arena_item_children (NRArenaItem *item)
114     nr_return_val_if_fail (item != NULL, NULL);
115     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
117     if (NR_ARENA_ITEM_VIRTUAL (item, children))
118         return NR_ARENA_ITEM_VIRTUAL (item, children) (item);
120     return NULL;
123 NRArenaItem *
124 nr_arena_item_last_child (NRArenaItem *item)
126     nr_return_val_if_fail (item != NULL, NULL);
127     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
129     if (NR_ARENA_ITEM_VIRTUAL (item, last_child)) {
130         return NR_ARENA_ITEM_VIRTUAL (item, last_child) (item);
131     } else {
132         NRArenaItem *ref = nr_arena_item_children (item);
133         if (ref)
134             while (ref->next)
135                 ref = ref->next;
136         return ref;
137     }
140 void
141 nr_arena_item_add_child (NRArenaItem *item, NRArenaItem *child,
142                          NRArenaItem *ref)
144     nr_return_if_fail (item != NULL);
145     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
146     nr_return_if_fail (child != NULL);
147     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
148     nr_return_if_fail (child->parent == NULL);
149     nr_return_if_fail (child->prev == NULL);
150     nr_return_if_fail (child->next == NULL);
151     nr_return_if_fail (child->arena == item->arena);
152     nr_return_if_fail (child != ref);
153     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
154     nr_return_if_fail (!ref || (ref->parent == item));
156     if (NR_ARENA_ITEM_VIRTUAL (item, add_child))
157         NR_ARENA_ITEM_VIRTUAL (item, add_child) (item, child, ref);
160 void
161 nr_arena_item_remove_child (NRArenaItem *item, NRArenaItem *child)
163     nr_return_if_fail (item != NULL);
164     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
165     nr_return_if_fail (child != NULL);
166     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
167     nr_return_if_fail (child->parent == item);
169     if (NR_ARENA_ITEM_VIRTUAL (item, remove_child))
170         NR_ARENA_ITEM_VIRTUAL (item, remove_child) (item, child);
173 void
174 nr_arena_item_set_child_position (NRArenaItem *item, NRArenaItem *child,
175                                   NRArenaItem *ref)
177     nr_return_if_fail (item != NULL);
178     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
179     nr_return_if_fail (child != NULL);
180     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
181     nr_return_if_fail (child->parent == item);
182     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
183     nr_return_if_fail (!ref || (ref->parent == item));
185     if (NR_ARENA_ITEM_VIRTUAL (item, set_child_position))
186         NR_ARENA_ITEM_VIRTUAL (item, set_child_position) (item, child, ref);
189 NRArenaItem *
190 nr_arena_item_ref (NRArenaItem *item)
192     nr_object_ref ((NRObject *) item);
194     return item;
197 NRArenaItem *
198 nr_arena_item_unref (NRArenaItem *item)
200     nr_object_unref ((NRObject *) item);
202     return NULL;
205 unsigned int
206 nr_arena_item_invoke_update (NRArenaItem *item, NRRectL *area, NRGC *gc,
207                              unsigned int state, unsigned int reset)
209     NRGC childgc (gc);
211     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
212     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
213                            NR_ARENA_ITEM_STATE_INVALID);
214     nr_return_val_if_fail (!(state & NR_ARENA_ITEM_STATE_INVALID),
215                            NR_ARENA_ITEM_STATE_INVALID);
217 #ifdef NR_ARENA_ITEM_DEBUG_CASCADE
218     printf ("Update %s:%p %x %x %x\n",
219             nr_type_name_from_instance ((GTypeInstance *) item), item, state,
220             item->state, reset);
221 #endif
223     /* return if in error */
224     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
225         return item->state;
226     /* Set reset flags according to propagation status */
227     if (item->propagate) {
228         reset |= ~item->state;
229         item->propagate = FALSE;
230     }
231     /* Reset our state */
232     item->state &= ~reset;
233     /* Return if NOP */
234     if (!(~item->state & state))
235         return item->state;
236     /* Test whether to return immediately */
237     if (area && (item->state & NR_ARENA_ITEM_STATE_BBOX)) {
238         if (!nr_rect_l_test_intersect (area, &item->bbox))
239             return item->state;
240     }
242     /* Reset image cache, if not to be kept */
243     if (!(item->state & NR_ARENA_ITEM_STATE_IMAGE) && (item->px)) {
244         item->px = NULL;
245     }
247     /* Set up local gc */
248     childgc = *gc;
249     if (item->transform) {
250         childgc.transform = (*item->transform) * childgc.transform;
251     }
252     /* Remember the transformation matrix */
253     item->ctm = childgc.transform;
255     /* Invoke the real method */
256     item->state =
257         NR_ARENA_ITEM_VIRTUAL (item, update) (item, area, &childgc, state,
258                                               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) {
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;
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)
303    bool outline = (item->arena->rendermode == RENDERMODE_OUTLINE);
305     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
306     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
307                            NR_ARENA_ITEM_STATE_INVALID);
308     nr_return_val_if_fail (item->state & NR_ARENA_ITEM_STATE_BBOX,
309                            item->state);
311 #ifdef NR_ARENA_ITEM_VERBOSE
312     printf ("Invoke render %p: %d %d - %d %d\n", item, area->x0, area->y0,
313             area->x1, area->y1);
314 #endif
316     /* If we are outside bbox just return successfully */
317     if (!item->visible)
318         return item->state | NR_ARENA_ITEM_STATE_RENDER;
320     NRRectL carea;
321     nr_rect_l_intersect (&carea, area, &item->bbox);
322     if (nr_rect_l_test_empty (&carea))
323         return item->state | NR_ARENA_ITEM_STATE_RENDER;
324     if (item->filter && !outline) {
325         item->filter->area_enlarge (carea, item->ctm);
326         nr_rect_l_intersect (&carea, &carea, &item->bbox);
327     }
329     if (outline) {
330     // No caching in outline mode for now; investigate if it really gives any advantage with cairo.
331     // Also no attempts to clip anything; just render everything: item, clip, mask   
332             // First, render the object itself 
333             unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, pb, flags);
334             if (state & NR_ARENA_ITEM_STATE_INVALID) {
335                 /* Clean up and return error */
336                 item->state |= NR_ARENA_ITEM_STATE_INVALID;
337                 return item->state;
338             }
340             // render clip and mask, if any
341             guint32 saved_rgba = item->arena->outlinecolor; // save current outline color
342             // render clippath as an object, using a different color
343             if (item->clip) {
344                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "clips", 0x00ff00ff); // green clips
345                 NR_ARENA_ITEM_VIRTUAL (item->clip, render) (ct, item->clip, &carea, pb, flags);
346             } 
347             // render mask as an object, using a different color
348             if (item->mask) {
349                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "masks", 0x0000ffff); // blue masks
350                 NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, pb, flags);
351             }
352             item->arena->outlinecolor = saved_rgba; // restore outline color
354             return item->state | NR_ARENA_ITEM_STATE_RENDER;
355     }
357     NRPixBlock cpb;
358     if (item->px) {
359         /* Has cache pixblock, render this and return */
360         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P,
361                                   /* fixme: This probably cannot overflow, because we render only if visible */
362                                   /* fixme: and pixel cache is there only for small items */
363                                   /* fixme: But this still needs extra check (Lauris) */
364                                   item->bbox.x0, item->bbox.y0,
365                                   item->bbox.x1, item->bbox.y1,
366                                   item->px,
367                                   4 * (item->bbox.x1 - item->bbox.x0), FALSE,
368                                   FALSE);
369         nr_blit_pixblock_pixblock (pb, &cpb);
370         nr_pixblock_release (&cpb);
371         pb->empty = FALSE;
372         return item->state | NR_ARENA_ITEM_STATE_RENDER;
373     }
375     NRPixBlock *dpb = pb;
377     /* Setup cache if we can */
378     if ((!(flags & NR_ARENA_ITEM_RENDER_NO_CACHE)) &&
379         (carea.x0 <= item->bbox.x0) && (carea.y0 <= item->bbox.y0) &&
380         (carea.x1 >= item->bbox.x1) && (carea.y1 >= item->bbox.y1) &&
381         (((item->bbox.x1 - item->bbox.x0) * (item->bbox.y1 -
382                                              item->bbox.y0)) <= 4096)) {
383         // Item bbox is fully in renderable area and size is acceptable
384         carea.x0 = item->bbox.x0;
385         carea.y0 = item->bbox.y0;
386         carea.x1 = item->bbox.x1;
387         carea.y1 = item->bbox.y1;
388         item->px =
389             new (GC::ATOMIC) unsigned char[4 * (carea.x1 - carea.x0) *
390                                            (carea.y1 - carea.y0)];
391         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P, carea.x0,
392                                   carea.y0, carea.x1, carea.y1, item->px,
393                                   4 * (carea.x1 - carea.x0), TRUE, TRUE);
394         cpb.visible_area = pb->visible_area;
395         dpb = &cpb;
396         // Set nocache flag for downstream rendering
397         flags |= NR_ARENA_ITEM_RENDER_NO_CACHE;
398     }
400     /* Determine, whether we need temporary buffer */
401     if (item->clip || item->mask
402         || ((item->opacity != 255) && !item->render_opacity)
403         || (item->filter) || item->background_new
404         || (item->parent && item->parent->background_pb)) {
406         /* Setup and render item buffer */
407         NRPixBlock ipb;
408         nr_pixblock_setup_fast (&ipb, NR_PIXBLOCK_MODE_R8G8B8A8P,
409                                 carea.x0, carea.y0, carea.x1, carea.y1,
410                                 TRUE);
412         //  if memory allocation failed, abort render
413         if (ipb.size != NR_PIXBLOCK_SIZE_TINY && ipb.data.px == NULL) {
414             nr_pixblock_release (&ipb);
415             return (item->state);
416         }
418         /* If background access is used, save the pixblock address.
419          * This address is set to NULL at the end of this block */
420         if (item->background_new ||
421             (item->parent && item->parent->background_pb)) {
422             item->background_pb = &ipb;
423         }
425         ipb.visible_area = pb->visible_area;
426         if (item->filter) {
427               item->filter->area_enlarge (ipb.visible_area, item->ctm);
428         }
430         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, &ipb, flags);
431         if (state & NR_ARENA_ITEM_STATE_INVALID) {
432             /* Clean up and return error */
433             nr_pixblock_release (&ipb);
434             if (dpb != pb)
435                 nr_pixblock_release (dpb);
436             item->state |= NR_ARENA_ITEM_STATE_INVALID;
437             return item->state;
438         }
439         ipb.empty = FALSE;
441         /* Run filtering, if a filter is set for this object */
442         if (item->filter) {
443             item->filter->render (item, &ipb);
444         }
446         if (item->clip || item->mask) {
447             /* Setup mask pixblock */
448             NRPixBlock mpb;
449             nr_pixblock_setup_fast (&mpb, NR_PIXBLOCK_MODE_A8, carea.x0,
450                                     carea.y0, carea.x1, carea.y1, TRUE);
452             if (mpb.data.px != NULL) { // if memory allocation was successful
454                 mpb.visible_area = pb->visible_area;
455                 /* Do clip if needed */
456                 if (item->clip) {
457                     state = nr_arena_item_invoke_clip (item->clip, &carea, &mpb);
458                     if (state & NR_ARENA_ITEM_STATE_INVALID) {
459                         /* Clean up and return error */
460                         nr_pixblock_release (&mpb);
461                         nr_pixblock_release (&ipb);
462                         if (dpb != pb)
463                             nr_pixblock_release (dpb);
464                         item->state |= NR_ARENA_ITEM_STATE_INVALID;
465                         return item->state;
466                     }
467                     mpb.empty = FALSE;
468                 }
469                 /* Do mask if needed */
470                 if (item->mask) {
471                     NRPixBlock tpb;
472                     /* Set up yet another temporary pixblock */
473                     nr_pixblock_setup_fast (&tpb, NR_PIXBLOCK_MODE_R8G8B8A8N,
474                                             carea.x0, carea.y0, carea.x1,
475                                             carea.y1, TRUE);
477                     if (tpb.data.px != NULL) { // if memory allocation was successful
479                         tpb.visible_area = pb->visible_area;
480                         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, &tpb, flags);
481                         if (state & NR_ARENA_ITEM_STATE_INVALID) {
482                             /* Clean up and return error */
483                             nr_pixblock_release (&tpb);
484                             nr_pixblock_release (&mpb);
485                             nr_pixblock_release (&ipb);
486                             if (dpb != pb)
487                                 nr_pixblock_release (dpb);
488                             item->state |= NR_ARENA_ITEM_STATE_INVALID;
489                             return item->state;
490                         }
491                         /* Composite with clip */
492                         if (item->clip) {
493                             int x, y;
494                             for (y = carea.y0; y < carea.y1; y++) {
495                                 unsigned char *s, *d;
496                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
497                                                              carea.y0) * tpb.rs;
498                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
499                                                              carea.y0) * mpb.rs;
500                                 for (x = carea.x0; x < carea.x1; x++) {
501                                     unsigned int m;
502                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
503                                     d[0] =
504                                         FAST_DIV_ROUND < 3 * 255 * 255 >
505                                         (NR_PREMUL_123 (d[0], m));
506                                     s += 4;
507                                     d += 1;
508                                 }
509                             }
510                         } else {
511                             int x, y;
512                             for (y = carea.y0; y < carea.y1; y++) {
513                                 unsigned char *s, *d;
514                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
515                                                              carea.y0) * tpb.rs;
516                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
517                                                              carea.y0) * mpb.rs;
518                                 for (x = carea.x0; x < carea.x1; x++) {
519                                     unsigned int m;
520                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
521                                     d[0] = FAST_DIV_ROUND < 3 * 255 > (m);
522                                     s += 4;
523                                     d += 1;
524                                 }
525                             }
526                             mpb.empty = FALSE;
527                         }
528                     }
529                     nr_pixblock_release (&tpb);
530                 }
531                 /* Multiply with opacity if needed */
532                 if ((item->opacity != 255) && !item->render_opacity
533                     ) {
534                     int x, y;
535                     unsigned int a;
536                     a = item->opacity;
537                     for (y = carea.y0; y < carea.y1; y++) {
538                         unsigned char *d;
539                         d = NR_PIXBLOCK_PX (&mpb) + (y - carea.y0) * mpb.rs;
540                         for (x = carea.x0; x < carea.x1; x++) {
541                             d[0] = NR_PREMUL_111 (d[0], a);
542                             d += 1;
543                         }
544                     }
545                 }
546                 /* Compose rendering pixblock int destination */
547                 nr_blit_pixblock_pixblock_mask (dpb, &ipb, &mpb);
548             }
549             nr_pixblock_release (&mpb);
550         } else {
551             if (item->render_opacity) { // opacity was already rendered in, just copy to dpb here
552                 nr_blit_pixblock_pixblock(dpb, &ipb);
553             } else { // copy while multiplying by opacity
554                 nr_blit_pixblock_pixblock_alpha (dpb, &ipb, item->opacity);
555             }
556         }
557         nr_pixblock_release (&ipb);
558         dpb->empty = FALSE;
559         /* This pointer wouldn't be valid outside this block, so clear it */
560         item->background_pb = NULL;
561     } else {
562         /* Just render */
563         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, dpb, flags);
564         if (state & NR_ARENA_ITEM_STATE_INVALID) {
565             /* Clean up and return error */
566             if (dpb != pb)
567                 nr_pixblock_release (dpb);
568             item->state |= NR_ARENA_ITEM_STATE_INVALID;
569             return item->state;
570         }
571         dpb->empty = FALSE;
572     }
574     if (dpb != pb) {
575         /* Have to blit from cache */
576         nr_blit_pixblock_pixblock (pb, dpb);
577         nr_pixblock_release (dpb);
578         pb->empty = FALSE;
579         item->state |= NR_ARENA_ITEM_STATE_IMAGE;
580     }
582     return item->state | NR_ARENA_ITEM_STATE_RENDER;
585 unsigned int
586 nr_arena_item_invoke_clip (NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
588     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
589     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
590                            NR_ARENA_ITEM_STATE_INVALID);
591     /* we originally short-circuited if the object state included
592      * NR_ARENA_ITEM_STATE_CLIP (and showed a warning on the console);
593      * anyone know why we stopped doing so?
594      */
595     nr_return_val_if_fail ((pb->area.x1 - pb->area.x0) >=
596                            (area->x1 - area->x0),
597                            NR_ARENA_ITEM_STATE_INVALID);
598     nr_return_val_if_fail ((pb->area.y1 - pb->area.y0) >=
599                            (area->y1 - area->y0),
600                            NR_ARENA_ITEM_STATE_INVALID);
602 #ifdef NR_ARENA_ITEM_VERBOSE
603     printf ("Invoke clip by %p: %d %d - %d %d, item bbox %d %d - %d %d\n",
604             item, area->x0, area->y0, area->x1, area->y1, (&item->bbox)->x0,
605             (&item->bbox)->y0, (&item->bbox)->x1, (&item->bbox)->y1);
606 #endif
608     if (item->visible && nr_rect_l_test_intersect (area, &item->bbox)) {
609         /* Need render that item */
610         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->clip) {
611             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
612                 clip (item, area, pb);
613         }
614     }
616     return item->state;
619 NRArenaItem *
620 nr_arena_item_invoke_pick (NRArenaItem *item, NR::Point p, double delta,
621                            unsigned int sticky)
623     nr_return_val_if_fail (item != NULL, NULL);
624     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
626     // Sometimes there's no BBOX in item->state, reason unknown (bug 992817); I made this not an assert to remove the warning
627     if (!(item->state & NR_ARENA_ITEM_STATE_BBOX)
628         || !(item->state & NR_ARENA_ITEM_STATE_PICK))
629         return NULL;
631     if (!sticky && !(item->visible && item->sensitive))
632         return NULL;
634     // TODO: rewrite using NR::Rect
635     const double x = p[NR::X];
636     const double y = p[NR::Y];
638     if (((x + delta) >= item->bbox.x0) &&
639         ((x - delta) < item->bbox.x1) &&
640         ((y + delta) >= item->bbox.y0) && ((y - delta) < item->bbox.y1)) {
641         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->pick)
642             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
643                 pick (item, p, delta, sticky);
644     }
646     return NULL;
649 void
650 nr_arena_item_request_update (NRArenaItem *item, unsigned int reset,
651                               unsigned int propagate)
653     nr_return_if_fail (item != NULL);
654     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
655     nr_return_if_fail (!(reset & NR_ARENA_ITEM_STATE_INVALID));
657     if (propagate && !item->propagate)
658         item->propagate = TRUE;
660     if (item->state & reset) {
661         item->state &= ~reset;
662         if (item->parent) {
663             nr_arena_item_request_update (item->parent, reset, FALSE);
664         } else {
665             nr_arena_request_update (item->arena, item);
666         }
667     }
670 void
671 nr_arena_item_request_render (NRArenaItem *item)
673     nr_return_if_fail (item != NULL);
674     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
676     nr_arena_request_render_rect (item->arena, &item->bbox);
679 /* Public */
681 NRArenaItem *
682 nr_arena_item_unparent (NRArenaItem *item)
684     nr_return_val_if_fail (item != NULL, NULL);
685     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
687     nr_arena_item_request_render (item);
689     if (item->parent) {
690         nr_arena_item_remove_child (item->parent, item);
691     }
693     return NULL;
696 void
697 nr_arena_item_append_child (NRArenaItem *parent, NRArenaItem *child)
699     nr_return_if_fail (parent != NULL);
700     nr_return_if_fail (NR_IS_ARENA_ITEM (parent));
701     nr_return_if_fail (child != NULL);
702     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
703     nr_return_if_fail (parent->arena == child->arena);
704     nr_return_if_fail (child->parent == NULL);
705     nr_return_if_fail (child->prev == NULL);
706     nr_return_if_fail (child->next == NULL);
708     nr_arena_item_add_child (parent, child, nr_arena_item_last_child (parent));
711 void
712 nr_arena_item_set_transform (NRArenaItem *item, NR::Matrix const &transform)
714     NR::Matrix const t (transform);
715     nr_arena_item_set_transform (item, &t);
718 void
719 nr_arena_item_set_transform (NRArenaItem *item, NR::Matrix const *transform)
721     nr_return_if_fail (item != NULL);
722     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
724     if (!transform && !item->transform)
725         return;
727     const NR::Matrix *md = (item->transform) ? item->transform : &NR_MATRIX_IDENTITY;
728     const NR::Matrix *ms = (transform) ? transform : &NR_MATRIX_IDENTITY;
730     if (!NR::matrix_equalp(*md, *ms, NR_EPSILON)) {
731         nr_arena_item_request_render (item);
732         if (!transform || transform->test_identity()) {
733             /* Set to identity affine */
734             item->transform = NULL;
735         } else {
736             if (!item->transform)
737                 item->transform = new (GC::ATOMIC) NR::Matrix ();
738             *item->transform = *transform;
739         }
740         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
741     }
744 void
745 nr_arena_item_set_opacity (NRArenaItem *item, double opacity)
747     nr_return_if_fail (item != NULL);
748     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
750     nr_arena_item_request_render (item);
752     item->opacity = (unsigned int) (opacity * 255.9999);
755 void
756 nr_arena_item_set_sensitive (NRArenaItem *item, unsigned int sensitive)
758     nr_return_if_fail (item != NULL);
759     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
761     /* fixme: mess with pick/repick... */
763     item->sensitive = sensitive;
766 void
767 nr_arena_item_set_visible (NRArenaItem *item, unsigned int visible)
769     nr_return_if_fail (item != NULL);
770     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
772     item->visible = visible;
774     nr_arena_item_request_render (item);
777 void
778 nr_arena_item_set_clip (NRArenaItem *item, NRArenaItem *clip)
780     nr_return_if_fail (item != NULL);
781     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
782     nr_return_if_fail (!clip || NR_IS_ARENA_ITEM (clip));
784     if (clip != item->clip) {
785         nr_arena_item_request_render (item);
786         if (item->clip)
787             item->clip = nr_arena_item_detach (item, item->clip);
788         if (clip)
789             item->clip = nr_arena_item_attach (item, clip, NULL, NULL);
790         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
791     }
794 void
795 nr_arena_item_set_mask (NRArenaItem *item, NRArenaItem *mask)
797     nr_return_if_fail (item != NULL);
798     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
799     nr_return_if_fail (!mask || NR_IS_ARENA_ITEM (mask));
801     if (mask != item->mask) {
802         nr_arena_item_request_render (item);
803         if (item->mask)
804             item->mask = nr_arena_item_detach (item, item->mask);
805         if (mask)
806             item->mask = nr_arena_item_attach (item, mask, NULL, NULL);
807         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
808     }
811 void
812 nr_arena_item_set_order (NRArenaItem *item, int order)
814     nr_return_if_fail (item != NULL);
815     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
817     if (!item->parent)
818         return;
820     NRArenaItem *children = nr_arena_item_children (item->parent);
822     NRArenaItem *ref = NULL;
823     int pos = 0;
824     for (NRArenaItem *child = children; child != NULL; child = child->next) {
825         if (pos >= order)
826             break;
827         if (child != item) {
828             ref = child;
829             pos += 1;
830         }
831     }
833     nr_arena_item_set_child_position (item->parent, item, ref);
836 void
837 nr_arena_item_set_item_bbox (NRArenaItem *item, NR::Maybe<NR::Rect> &bbox)
839     nr_return_if_fail(item != NULL);
840     nr_return_if_fail(NR_IS_ARENA_ITEM(item));
842     item->item_bbox = bbox;
845 /** Returns a background image for use with filter effects. */
846 NRPixBlock *
847 nr_arena_item_get_background (NRArenaItem const *item, int depth)
849     NRPixBlock *pb;
850     if (!item->background_pb)
851         return NULL;
852     if (item->background_new) {
853         pb = new NRPixBlock ();
854         nr_pixblock_setup_fast (pb, item->background_pb->mode,
855                                 item->background_pb->area.x0,
856                                 item->background_pb->area.y0,
857                                 item->background_pb->area.x1,
858                                 item->background_pb->area.y1, true);
859         if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) // allocation failed
860             return NULL;
861     } else if (item->parent) {
862         pb = nr_arena_item_get_background (item->parent, depth + 1);
863     } else
864         return NULL;
866     if (depth > 0)
867         nr_blit_pixblock_pixblock (pb, item->background_pb);
869     return pb;
872 /* Helpers */
874 NRArenaItem *
875 nr_arena_item_attach (NRArenaItem *parent, NRArenaItem *child,
876                       NRArenaItem *prev, NRArenaItem *next)
878     nr_return_val_if_fail (parent != NULL, NULL);
879     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
880     nr_return_val_if_fail (child != NULL, NULL);
881     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
882     nr_return_val_if_fail (child->parent == NULL, NULL);
883     nr_return_val_if_fail (child->prev == NULL, NULL);
884     nr_return_val_if_fail (child->next == NULL, NULL);
885     nr_return_val_if_fail (!prev || NR_IS_ARENA_ITEM (prev), NULL);
886     nr_return_val_if_fail (!prev || (prev->parent == parent), NULL);
887     nr_return_val_if_fail (!prev || (prev->next == next), NULL);
888     nr_return_val_if_fail (!next || NR_IS_ARENA_ITEM (next), NULL);
889     nr_return_val_if_fail (!next || (next->parent == parent), NULL);
890     nr_return_val_if_fail (!next || (next->prev == prev), NULL);
892     child->parent = parent;
893     child->prev = prev;
894     child->next = next;
896     if (prev)
897         prev->next = child;
898     if (next)
899         next->prev = child;
901     return child;
904 NRArenaItem *
905 nr_arena_item_detach (NRArenaItem *parent, NRArenaItem *child)
907     nr_return_val_if_fail (parent != NULL, NULL);
908     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
909     nr_return_val_if_fail (child != NULL, NULL);
910     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
911     nr_return_val_if_fail (child->parent == parent, NULL);
913     NRArenaItem *prev = child->prev;
914     NRArenaItem *next = child->next;
916     child->parent = NULL;
917     child->prev = NULL;
918     child->next = NULL;
920     if (prev)
921         prev->next = next;
922     if (next)
923         next->prev = prev;
925     return next;
928 /*
929   Local Variables:
930   mode:c++
931   c-file-style:"stroustrup"
932   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
933   indent-tabs-mode:nil
934   fill-column:99
935   End:
936 */
937 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :