Code

95b0c0eae69328aea84ec3d15d79cf7258142005
[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 "nr-arena.h"
24 #include "nr-arena-item.h"
25 #include "gc-core.h"
27 #include "nr-filter.h"
28 #include "libnr/nr-rect.h"
29 #include "nr-arena-group.h"
30 #include "prefs-utils.h"
32 namespace GC = Inkscape::GC;
34 static void nr_arena_item_class_init (NRArenaItemClass *klass);
35 static void nr_arena_item_init (NRArenaItem *item);
36 static void nr_arena_item_private_finalize (NRObject *object);
38 static NRObjectClass *parent_class;
40 NRType 
41 nr_arena_item_get_type (void)
42 {
43     static NRType type = 0;
44     if (!type) {
45         type = nr_object_register_type (NR_TYPE_OBJECT,
46                                         "NRArenaItem",
47                                         sizeof (NRArenaItemClass),
48                                         sizeof (NRArenaItem),
49                                         (void (*)(NRObjectClass *))
50                                         nr_arena_item_class_init,
51                                         (void (*)(NRObject *))
52                                         nr_arena_item_init);
53     }
54     return type;
55 }
57 static void
58 nr_arena_item_class_init (NRArenaItemClass *klass)
59 {
60     NRObjectClass *object_class;
62     object_class = (NRObjectClass *) klass;
64     parent_class = ((NRObjectClass *) klass)->parent;
66     object_class->finalize = nr_arena_item_private_finalize;
67     object_class->cpp_ctor = NRObject::invoke_ctor < NRArenaItem >;
68 }
70 static void
71 nr_arena_item_init (NRArenaItem *item)
72 {
73     item->arena = NULL;
74     item->parent = NULL;
75     item->next = item->prev = NULL;
77     item->key = 0;
79     item->state = 0;
80     item->sensitive = TRUE;
81     item->visible = TRUE;
83     memset (&item->bbox, 0, sizeof (item->bbox));
84     item->transform = NULL;
85     item->opacity = 255;
86     item->render_opacity = FALSE;
88     item->transform = NULL;
89     item->clip = NULL;
90     item->mask = NULL;
91     item->px = NULL;
92     item->data = NULL;
93     item->filter = NULL;
94     item->background_pb = NULL;
95     item->background_new = false;
96 }
98 static void
99 nr_arena_item_private_finalize (NRObject *object)
101     NRArenaItem *item = static_cast < NRArenaItem * >(object);
103     item->px = NULL;
104     item->transform = NULL;
106     ((NRObjectClass *) (parent_class))->finalize (object);
109 NRArenaItem *
110 nr_arena_item_children (NRArenaItem *item)
112     nr_return_val_if_fail (item != NULL, NULL);
113     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
115     if (NR_ARENA_ITEM_VIRTUAL (item, children))
116         return NR_ARENA_ITEM_VIRTUAL (item, children) (item);
118     return NULL;
121 NRArenaItem *
122 nr_arena_item_last_child (NRArenaItem *item)
124     nr_return_val_if_fail (item != NULL, NULL);
125     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
127     if (NR_ARENA_ITEM_VIRTUAL (item, last_child)) {
128         return NR_ARENA_ITEM_VIRTUAL (item, last_child) (item);
129     } else {
130         NRArenaItem *ref = nr_arena_item_children (item);
131         if (ref)
132             while (ref->next)
133                 ref = ref->next;
134         return ref;
135     }
138 void
139 nr_arena_item_add_child (NRArenaItem *item, NRArenaItem *child,
140                          NRArenaItem *ref)
142     nr_return_if_fail (item != NULL);
143     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
144     nr_return_if_fail (child != NULL);
145     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
146     nr_return_if_fail (child->parent == NULL);
147     nr_return_if_fail (child->prev == NULL);
148     nr_return_if_fail (child->next == NULL);
149     nr_return_if_fail (child->arena == item->arena);
150     nr_return_if_fail (child != ref);
151     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
152     nr_return_if_fail (!ref || (ref->parent == item));
154     if (NR_ARENA_ITEM_VIRTUAL (item, add_child))
155         NR_ARENA_ITEM_VIRTUAL (item, add_child) (item, child, ref);
158 void
159 nr_arena_item_remove_child (NRArenaItem *item, NRArenaItem *child)
161     nr_return_if_fail (item != NULL);
162     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
163     nr_return_if_fail (child != NULL);
164     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
165     nr_return_if_fail (child->parent == item);
167     if (NR_ARENA_ITEM_VIRTUAL (item, remove_child))
168         NR_ARENA_ITEM_VIRTUAL (item, remove_child) (item, child);
171 void
172 nr_arena_item_set_child_position (NRArenaItem *item, NRArenaItem *child,
173                                   NRArenaItem *ref)
175     nr_return_if_fail (item != NULL);
176     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
177     nr_return_if_fail (child != NULL);
178     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
179     nr_return_if_fail (child->parent == item);
180     nr_return_if_fail (!ref || NR_IS_ARENA_ITEM (ref));
181     nr_return_if_fail (!ref || (ref->parent == item));
183     if (NR_ARENA_ITEM_VIRTUAL (item, set_child_position))
184         NR_ARENA_ITEM_VIRTUAL (item, set_child_position) (item, child, ref);
187 NRArenaItem *
188 nr_arena_item_ref (NRArenaItem *item)
190     nr_object_ref ((NRObject *) item);
192     return item;
195 NRArenaItem *
196 nr_arena_item_unref (NRArenaItem *item)
198     nr_object_unref ((NRObject *) item);
200     return NULL;
203 unsigned int
204 nr_arena_item_invoke_update (NRArenaItem *item, NRRectL *area, NRGC *gc,
205                              unsigned int state, unsigned int reset)
207     NRGC childgc (gc);
209     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
210     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
211                            NR_ARENA_ITEM_STATE_INVALID);
212     nr_return_val_if_fail (!(state & NR_ARENA_ITEM_STATE_INVALID),
213                            NR_ARENA_ITEM_STATE_INVALID);
215 #ifdef NR_ARENA_ITEM_DEBUG_CASCADE
216     printf ("Update %s:%p %x %x %x\n",
217             nr_type_name_from_instance ((GTypeInstance *) item), item, state,
218             item->state, reset);
219 #endif
221     /* return if in error */
222     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
223         return item->state;
224     /* Set reset flags according to propagation status */
225     if (item->propagate) {
226         reset |= ~item->state;
227         item->propagate = FALSE;
228     }
229     /* Reset our state */
230     item->state &= ~reset;
231     /* Return if NOP */
232     if (!(~item->state & state))
233         return item->state;
234     /* Test whether to return immediately */
235     if (area && (item->state & NR_ARENA_ITEM_STATE_BBOX)) {
236         if (!nr_rect_l_test_intersect (area, &item->bbox))
237             return item->state;
238     }
240     /* Reset image cache, if not to be kept */
241     if (!(item->state & NR_ARENA_ITEM_STATE_IMAGE) && (item->px)) {
242         item->px = NULL;
243     }
245     /* Set up local gc */
246     childgc = *gc;
247     if (item->transform) {
248         nr_matrix_multiply (&childgc.transform, item->transform,
249                             &childgc.transform);
250     }
251     /* Remember the transformation matrix */
252     item->ctm = childgc.transform;
254     /* Invoke the real method */
255     item->state =
256         NR_ARENA_ITEM_VIRTUAL (item, update) (item, area, &childgc, state,
257                                               reset);
258     if (item->state & NR_ARENA_ITEM_STATE_INVALID)
259         return item->state;
260     /* Enlarge the bounding box to contain filter effects */
261     if (item->filter) {
262         item->filter->bbox_enlarge (item->bbox);
263     }
264     // fixme: to fix the display glitches, in outline mode bbox must be a combination of 
265     // full item bbox and its clip and mask (after we have the API to get these)
267     /* Clipping */
268     if (item->clip) {
269         // FIXME: since here we only need bbox, consider passing 
270         // ((state & !(NR_ARENA_ITEM_STATE_RENDER)) | NR_ARENA_ITEM_STATE_BBOX)
271         // instead of state, so it does not have to create rendering structures in nr_arena_shape_update
272         unsigned int newstate = nr_arena_item_invoke_update (item->clip, area, &childgc, state, reset); 
273         if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
274             item->state |= NR_ARENA_ITEM_STATE_INVALID;
275             return item->state;
276         }
277         nr_rect_l_intersect (&item->bbox, &item->bbox, &item->clip->bbox);
278     }
279     /* Masking */
280     if (item->mask) {
281         unsigned int newstate = nr_arena_item_invoke_update (item->mask, area, &childgc, state, reset);
282         if (newstate & NR_ARENA_ITEM_STATE_INVALID) {
283             item->state |= NR_ARENA_ITEM_STATE_INVALID;
284             return item->state;
285         }
286         nr_rect_l_intersect (&item->bbox, &item->bbox, &item->mask->bbox);
287     }
289     return item->state;
292 /**
293  *    Render item to pixblock.
294  *
295  *    \return Has NR_ARENA_ITEM_STATE_RENDER set on success.
296  */
298 unsigned int
299 nr_arena_item_invoke_render (cairo_t *ct, NRArenaItem *item, NRRectL const *area,
300                              NRPixBlock *pb, unsigned int flags)
302    bool outline = (item->arena->rendermode == RENDERMODE_OUTLINE);
304     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
305     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
306                            NR_ARENA_ITEM_STATE_INVALID);
307     nr_return_val_if_fail (item->state & NR_ARENA_ITEM_STATE_BBOX,
308                            item->state);
310 #ifdef NR_ARENA_ITEM_VERBOSE
311     printf ("Invoke render %p: %d %d - %d %d\n", item, area->x0, area->y0,
312             area->x1, area->y1);
313 #endif
315     /* If we are outside bbox just return successfully */
316     if (!item->visible)
317         return item->state | NR_ARENA_ITEM_STATE_RENDER;
319     NRRectL carea;
320     nr_rect_l_intersect (&carea, area, &item->bbox);
321     if (nr_rect_l_test_empty (&carea))
322         return item->state | NR_ARENA_ITEM_STATE_RENDER;
323     if (item->filter && !outline) {
324         item->filter->area_enlarge (carea, item->ctm);
325         nr_rect_l_intersect (&carea, &carea, &item->bbox);
326     }
328     if (outline) {
329     // No caching in outline mode for now; investigate if it really gives any advantage with cairo.
330     // Also no attempts to clip anything; just render everything: item, clip, mask   
331             // First, render the object itself 
332             unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, pb, flags);
333             if (state & NR_ARENA_ITEM_STATE_INVALID) {
334                 /* Clean up and return error */
335                 item->state |= NR_ARENA_ITEM_STATE_INVALID;
336                 return item->state;
337             }
339             // render clip and mask, if any
340             guint32 saved_rgba = item->arena->outlinecolor; // save current outline color
341             // render clippath as an object, using a different color
342             if (item->clip) {
343                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "clips", 0x00ff00ff); // green clips
344                 NR_ARENA_ITEM_VIRTUAL (item->clip, render) (ct, item->clip, &carea, pb, flags);
345             } 
346             // render mask as an object, using a different color
347             if (item->mask) {
348                 item->arena->outlinecolor = prefs_get_int_attribute("options.wireframecolors", "masks", 0x0000ffff); // blue masks
349                 NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, pb, flags);
350             }
351             item->arena->outlinecolor = saved_rgba; // restore outline color
353             return item->state | NR_ARENA_ITEM_STATE_RENDER;
354     }
356     NRPixBlock cpb;
357     if (item->px) {
358         /* Has cache pixblock, render this and return */
359         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P,
360                                   /* fixme: This probably cannot overflow, because we render only if visible */
361                                   /* fixme: and pixel cache is there only for small items */
362                                   /* fixme: But this still needs extra check (Lauris) */
363                                   item->bbox.x0, item->bbox.y0,
364                                   item->bbox.x1, item->bbox.y1,
365                                   item->px,
366                                   4 * (item->bbox.x1 - item->bbox.x0), FALSE,
367                                   FALSE);
368         nr_blit_pixblock_pixblock (pb, &cpb);
369         nr_pixblock_release (&cpb);
370         pb->empty = FALSE;
371         return item->state | NR_ARENA_ITEM_STATE_RENDER;
372     }
374     NRPixBlock *dpb = pb;
376     /* Setup cache if we can */
377     if ((!(flags & NR_ARENA_ITEM_RENDER_NO_CACHE)) &&
378         (carea.x0 <= item->bbox.x0) && (carea.y0 <= item->bbox.y0) &&
379         (carea.x1 >= item->bbox.x1) && (carea.y1 >= item->bbox.y1) &&
380         (((item->bbox.x1 - item->bbox.x0) * (item->bbox.y1 -
381                                              item->bbox.y0)) <= 4096)) {
382         // Item bbox is fully in renderable area and size is acceptable
383         carea.x0 = item->bbox.x0;
384         carea.y0 = item->bbox.y0;
385         carea.x1 = item->bbox.x1;
386         carea.y1 = item->bbox.y1;
387         item->px =
388             new (GC::ATOMIC) unsigned char[4 * (carea.x1 - carea.x0) *
389                                            (carea.y1 - carea.y0)];
390         nr_pixblock_setup_extern (&cpb, NR_PIXBLOCK_MODE_R8G8B8A8P, carea.x0,
391                                   carea.y0, carea.x1, carea.y1, item->px,
392                                   4 * (carea.x1 - carea.x0), TRUE, TRUE);
393         cpb.visible_area = pb->visible_area;
394         dpb = &cpb;
395         // Set nocache flag for downstream rendering
396         flags |= NR_ARENA_ITEM_RENDER_NO_CACHE;
397     }
399     /* Determine, whether we need temporary buffer */
400     if (item->clip || item->mask
401         || ((item->opacity != 255) && !item->render_opacity)
402         || (item->filter) || item->background_new
403         || (item->parent && item->parent->background_pb)) {
405         /* Setup and render item buffer */
406         NRPixBlock ipb;
407         nr_pixblock_setup_fast (&ipb, NR_PIXBLOCK_MODE_R8G8B8A8P,
408                                 carea.x0, carea.y0, carea.x1, carea.y1,
409                                 TRUE);
411         //  if memory allocation failed, abort render
412         if (ipb.size != NR_PIXBLOCK_SIZE_TINY && ipb.data.px == NULL) {
413             nr_pixblock_release (&ipb);
414             return (item->state);
415         }
417         /* If background access is used, save the pixblock address.
418          * This address is set to NULL at the end of this block */
419         if (item->background_new ||
420             (item->parent && item->parent->background_pb)) {
421             item->background_pb = &ipb;
422         }
424         ipb.visible_area = pb->visible_area;
425         if (item->filter) {
426               item->filter->area_enlarge (ipb.visible_area, item->ctm);
427         }
429         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, &ipb, flags);
430         if (state & NR_ARENA_ITEM_STATE_INVALID) {
431             /* Clean up and return error */
432             nr_pixblock_release (&ipb);
433             if (dpb != pb)
434                 nr_pixblock_release (dpb);
435             item->state |= NR_ARENA_ITEM_STATE_INVALID;
436             return item->state;
437         }
438         ipb.empty = FALSE;
440         /* Run filtering, if a filter is set for this object */
441         if (item->filter) {
442             item->filter->render (item, &ipb);
443         }
445         if (item->clip || item->mask) {
446             /* Setup mask pixblock */
447             NRPixBlock mpb;
448             nr_pixblock_setup_fast (&mpb, NR_PIXBLOCK_MODE_A8, carea.x0,
449                                     carea.y0, carea.x1, carea.y1, TRUE);
451             if (mpb.data.px != NULL) { // if memory allocation was successful
453                 mpb.visible_area = pb->visible_area;
454                 /* Do clip if needed */
455                 if (item->clip) {
456                     state = nr_arena_item_invoke_clip (item->clip, &carea, &mpb);
457                     if (state & NR_ARENA_ITEM_STATE_INVALID) {
458                         /* Clean up and return error */
459                         nr_pixblock_release (&mpb);
460                         nr_pixblock_release (&ipb);
461                         if (dpb != pb)
462                             nr_pixblock_release (dpb);
463                         item->state |= NR_ARENA_ITEM_STATE_INVALID;
464                         return item->state;
465                     }
466                     mpb.empty = FALSE;
467                 }
468                 /* Do mask if needed */
469                 if (item->mask) {
470                     NRPixBlock tpb;
471                     /* Set up yet another temporary pixblock */
472                     nr_pixblock_setup_fast (&tpb, NR_PIXBLOCK_MODE_R8G8B8A8N,
473                                             carea.x0, carea.y0, carea.x1,
474                                             carea.y1, TRUE);
476                     if (tpb.data.px != NULL) { // if memory allocation was successful
478                         tpb.visible_area = pb->visible_area;
479                         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item->mask, render) (ct, item->mask, &carea, &tpb, flags);
480                         if (state & NR_ARENA_ITEM_STATE_INVALID) {
481                             /* Clean up and return error */
482                             nr_pixblock_release (&tpb);
483                             nr_pixblock_release (&mpb);
484                             nr_pixblock_release (&ipb);
485                             if (dpb != pb)
486                                 nr_pixblock_release (dpb);
487                             item->state |= NR_ARENA_ITEM_STATE_INVALID;
488                             return item->state;
489                         }
490                         /* Composite with clip */
491                         if (item->clip) {
492                             int x, y;
493                             for (y = carea.y0; y < carea.y1; y++) {
494                                 unsigned char *s, *d;
495                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
496                                                              carea.y0) * tpb.rs;
497                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
498                                                              carea.y0) * mpb.rs;
499                                 for (x = carea.x0; x < carea.x1; x++) {
500                                     unsigned int m;
501                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
502                                     d[0] =
503                                         FAST_DIV_ROUND < 3 * 255 * 255 >
504                                         (NR_PREMUL_123 (d[0], m));
505                                     s += 4;
506                                     d += 1;
507                                 }
508                             }
509                         } else {
510                             int x, y;
511                             for (y = carea.y0; y < carea.y1; y++) {
512                                 unsigned char *s, *d;
513                                 s = NR_PIXBLOCK_PX (&tpb) + (y -
514                                                              carea.y0) * tpb.rs;
515                                 d = NR_PIXBLOCK_PX (&mpb) + (y -
516                                                              carea.y0) * mpb.rs;
517                                 for (x = carea.x0; x < carea.x1; x++) {
518                                     unsigned int m;
519                                     m = NR_PREMUL_112 (s[0] + s[1] + s[2], s[3]);
520                                     d[0] = FAST_DIV_ROUND < 3 * 255 > (m);
521                                     s += 4;
522                                     d += 1;
523                                 }
524                             }
525                             mpb.empty = FALSE;
526                         }
527                     }
528                     nr_pixblock_release (&tpb);
529                 }
530                 /* Multiply with opacity if needed */
531                 if ((item->opacity != 255) && !item->render_opacity
532                     ) {
533                     int x, y;
534                     unsigned int a;
535                     a = item->opacity;
536                     for (y = carea.y0; y < carea.y1; y++) {
537                         unsigned char *d;
538                         d = NR_PIXBLOCK_PX (&mpb) + (y - carea.y0) * mpb.rs;
539                         for (x = carea.x0; x < carea.x1; x++) {
540                             d[0] = NR_PREMUL_111 (d[0], a);
541                             d += 1;
542                         }
543                     }
544                 }
545                 /* Compose rendering pixblock int destination */
546                 nr_blit_pixblock_pixblock_mask (dpb, &ipb, &mpb);
547             }
548             nr_pixblock_release (&mpb);
549         } else {
550             if (item->render_opacity) { // opacity was already rendered in, just copy to dpb here
551                 nr_blit_pixblock_pixblock(dpb, &ipb);
552             } else { // copy while multiplying by opacity
553                 nr_blit_pixblock_pixblock_alpha (dpb, &ipb, item->opacity);
554             }
555         }
556         nr_pixblock_release (&ipb);
557         dpb->empty = FALSE;
558         /* This pointer wouldn't be valid outside this block, so clear it */
559         item->background_pb = NULL;
560     } else {
561         /* Just render */
562         unsigned int state = NR_ARENA_ITEM_VIRTUAL (item, render) (ct, item, &carea, dpb, flags);
563         if (state & NR_ARENA_ITEM_STATE_INVALID) {
564             /* Clean up and return error */
565             if (dpb != pb)
566                 nr_pixblock_release (dpb);
567             item->state |= NR_ARENA_ITEM_STATE_INVALID;
568             return item->state;
569         }
570         dpb->empty = FALSE;
571     }
573     if (dpb != pb) {
574         /* Have to blit from cache */
575         nr_blit_pixblock_pixblock (pb, dpb);
576         nr_pixblock_release (dpb);
577         pb->empty = FALSE;
578         item->state |= NR_ARENA_ITEM_STATE_IMAGE;
579     }
581     return item->state | NR_ARENA_ITEM_STATE_RENDER;
584 unsigned int
585 nr_arena_item_invoke_clip (NRArenaItem *item, NRRectL *area, NRPixBlock *pb)
587     nr_return_val_if_fail (item != NULL, NR_ARENA_ITEM_STATE_INVALID);
588     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item),
589                            NR_ARENA_ITEM_STATE_INVALID);
590     /* we originally short-circuited if the object state included
591      * NR_ARENA_ITEM_STATE_CLIP (and showed a warning on the console);
592      * anyone know why we stopped doing so?
593      */
594     nr_return_val_if_fail ((pb->area.x1 - pb->area.x0) >=
595                            (area->x1 - area->x0),
596                            NR_ARENA_ITEM_STATE_INVALID);
597     nr_return_val_if_fail ((pb->area.y1 - pb->area.y0) >=
598                            (area->y1 - area->y0),
599                            NR_ARENA_ITEM_STATE_INVALID);
601 #ifdef NR_ARENA_ITEM_VERBOSE
602     printf ("Invoke clip by %p: %d %d - %d %d, item bbox %d %d - %d %d\n",
603             item, area->x0, area->y0, area->x1, area->y1, (&item->bbox)->x0,
604             (&item->bbox)->y0, (&item->bbox)->x1, (&item->bbox)->y1);
605 #endif
607     if (item->visible && nr_rect_l_test_intersect (area, &item->bbox)) {
608         /* Need render that item */
609         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->clip) {
610             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
611                 clip (item, area, pb);
612         }
613     }
615     return item->state;
618 NRArenaItem *
619 nr_arena_item_invoke_pick (NRArenaItem *item, NR::Point p, double delta,
620                            unsigned int sticky)
622     nr_return_val_if_fail (item != NULL, NULL);
623     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
625     // Sometimes there's no BBOX in item->state, reason unknown (bug 992817); I made this not an assert to remove the warning
626     if (!(item->state & NR_ARENA_ITEM_STATE_BBOX)
627         || !(item->state & NR_ARENA_ITEM_STATE_PICK))
628         return NULL;
630     if (!sticky && !(item->visible && item->sensitive))
631         return NULL;
633     // TODO: rewrite using NR::Rect
634     const double x = p[NR::X];
635     const double y = p[NR::Y];
637     if (((x + delta) >= item->bbox.x0) &&
638         ((x - delta) < item->bbox.x1) &&
639         ((y + delta) >= item->bbox.y0) && ((y - delta) < item->bbox.y1)) {
640         if (((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->pick)
641             return ((NRArenaItemClass *) NR_OBJECT_GET_CLASS (item))->
642                 pick (item, p, delta, sticky);
643     }
645     return NULL;
648 void
649 nr_arena_item_request_update (NRArenaItem *item, unsigned int reset,
650                               unsigned int propagate)
652     nr_return_if_fail (item != NULL);
653     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
654     nr_return_if_fail (!(reset & NR_ARENA_ITEM_STATE_INVALID));
656     if (propagate && !item->propagate)
657         item->propagate = TRUE;
659     if (item->state & reset) {
660         item->state &= ~reset;
661         if (item->parent) {
662             nr_arena_item_request_update (item->parent, reset, FALSE);
663         } else {
664             nr_arena_request_update (item->arena, item);
665         }
666     }
669 void
670 nr_arena_item_request_render (NRArenaItem *item)
672     nr_return_if_fail (item != NULL);
673     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
675     nr_arena_request_render_rect (item->arena, &item->bbox);
678 /* Public */
680 NRArenaItem *
681 nr_arena_item_unparent (NRArenaItem *item)
683     nr_return_val_if_fail (item != NULL, NULL);
684     nr_return_val_if_fail (NR_IS_ARENA_ITEM (item), NULL);
686     nr_arena_item_request_render (item);
688     if (item->parent) {
689         nr_arena_item_remove_child (item->parent, item);
690     }
692     return NULL;
695 void
696 nr_arena_item_append_child (NRArenaItem *parent, NRArenaItem *child)
698     nr_return_if_fail (parent != NULL);
699     nr_return_if_fail (NR_IS_ARENA_ITEM (parent));
700     nr_return_if_fail (child != NULL);
701     nr_return_if_fail (NR_IS_ARENA_ITEM (child));
702     nr_return_if_fail (parent->arena == child->arena);
703     nr_return_if_fail (child->parent == NULL);
704     nr_return_if_fail (child->prev == NULL);
705     nr_return_if_fail (child->next == NULL);
707     nr_arena_item_add_child (parent, child, nr_arena_item_last_child (parent));
710 void
711 nr_arena_item_set_transform (NRArenaItem *item, NR::Matrix const &transform)
713     NRMatrix const t (transform);
714     nr_arena_item_set_transform (item, &t);
717 void
718 nr_arena_item_set_transform (NRArenaItem *item, NRMatrix const *transform)
720     nr_return_if_fail (item != NULL);
721     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
723     if (!transform && !item->transform)
724         return;
726     const NRMatrix *md = (item->transform) ? item->transform : &NR_MATRIX_IDENTITY;
727     const NRMatrix *ms = (transform) ? transform : &NR_MATRIX_IDENTITY;
729     if (!NR_MATRIX_DF_TEST_CLOSE (md, ms, NR_EPSILON)) {
730         nr_arena_item_request_render (item);
731         if (!transform || nr_matrix_test_identity (transform, NR_EPSILON)) {
732             /* Set to identity affine */
733             item->transform = NULL;
734         } else {
735             if (!item->transform)
736                 item->transform = new (GC::ATOMIC) NRMatrix ();
737             *item->transform = *transform;
738         }
739         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
740     }
743 void
744 nr_arena_item_set_opacity (NRArenaItem *item, double opacity)
746     nr_return_if_fail (item != NULL);
747     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
749     nr_arena_item_request_render (item);
751     item->opacity = (unsigned int) (opacity * 255.9999);
754 void
755 nr_arena_item_set_sensitive (NRArenaItem *item, unsigned int sensitive)
757     nr_return_if_fail (item != NULL);
758     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
760     /* fixme: mess with pick/repick... */
762     item->sensitive = sensitive;
765 void
766 nr_arena_item_set_visible (NRArenaItem *item, unsigned int visible)
768     nr_return_if_fail (item != NULL);
769     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
771     item->visible = visible;
773     nr_arena_item_request_render (item);
776 void
777 nr_arena_item_set_clip (NRArenaItem *item, NRArenaItem *clip)
779     nr_return_if_fail (item != NULL);
780     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
781     nr_return_if_fail (!clip || NR_IS_ARENA_ITEM (clip));
783     if (clip != item->clip) {
784         nr_arena_item_request_render (item);
785         if (item->clip)
786             item->clip = nr_arena_item_detach (item, item->clip);
787         if (clip)
788             item->clip = nr_arena_item_attach (item, clip, NULL, NULL);
789         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
790     }
793 void
794 nr_arena_item_set_mask (NRArenaItem *item, NRArenaItem *mask)
796     nr_return_if_fail (item != NULL);
797     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
798     nr_return_if_fail (!mask || NR_IS_ARENA_ITEM (mask));
800     if (mask != item->mask) {
801         nr_arena_item_request_render (item);
802         if (item->mask)
803             item->mask = nr_arena_item_detach (item, item->mask);
804         if (mask)
805             item->mask = nr_arena_item_attach (item, mask, NULL, NULL);
806         nr_arena_item_request_update (item, NR_ARENA_ITEM_STATE_ALL, TRUE);
807     }
810 void
811 nr_arena_item_set_order (NRArenaItem *item, int order)
813     nr_return_if_fail (item != NULL);
814     nr_return_if_fail (NR_IS_ARENA_ITEM (item));
816     if (!item->parent)
817         return;
819     NRArenaItem *children = nr_arena_item_children (item->parent);
821     NRArenaItem *ref = NULL;
822     int pos = 0;
823     for (NRArenaItem *child = children; child != NULL; child = child->next) {
824         if (pos >= order)
825             break;
826         if (child != item) {
827             ref = child;
828             pos += 1;
829         }
830     }
832     nr_arena_item_set_child_position (item->parent, item, ref);
835 void
836 nr_arena_item_set_item_bbox (NRArenaItem *item, NR::Maybe<NR::Rect> &bbox)
838     nr_return_if_fail(item != NULL);
839     nr_return_if_fail(NR_IS_ARENA_ITEM(item));
841     item->item_bbox = bbox;
844 /** Returns a background image for use with filter effects. */
845 NRPixBlock *
846 nr_arena_item_get_background (NRArenaItem const *item, int depth)
848     NRPixBlock *pb;
849     if (!item->background_pb)
850         return NULL;
851     if (item->background_new) {
852         pb = new NRPixBlock ();
853         nr_pixblock_setup_fast (pb, item->background_pb->mode,
854                                 item->background_pb->area.x0,
855                                 item->background_pb->area.y0,
856                                 item->background_pb->area.x1,
857                                 item->background_pb->area.y1, true);
858         if (pb->size != NR_PIXBLOCK_SIZE_TINY && pb->data.px == NULL) // allocation failed
859             return NULL;
860     } else if (item->parent) {
861         pb = nr_arena_item_get_background (item->parent, depth + 1);
862     } else
863         return NULL;
865     if (depth > 0)
866         nr_blit_pixblock_pixblock (pb, item->background_pb);
868     return pb;
871 /* Helpers */
873 NRArenaItem *
874 nr_arena_item_attach (NRArenaItem *parent, NRArenaItem *child,
875                       NRArenaItem *prev, NRArenaItem *next)
877     nr_return_val_if_fail (parent != NULL, NULL);
878     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
879     nr_return_val_if_fail (child != NULL, NULL);
880     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
881     nr_return_val_if_fail (child->parent == NULL, NULL);
882     nr_return_val_if_fail (child->prev == NULL, NULL);
883     nr_return_val_if_fail (child->next == NULL, NULL);
884     nr_return_val_if_fail (!prev || NR_IS_ARENA_ITEM (prev), NULL);
885     nr_return_val_if_fail (!prev || (prev->parent == parent), NULL);
886     nr_return_val_if_fail (!prev || (prev->next == next), NULL);
887     nr_return_val_if_fail (!next || NR_IS_ARENA_ITEM (next), NULL);
888     nr_return_val_if_fail (!next || (next->parent == parent), NULL);
889     nr_return_val_if_fail (!next || (next->prev == prev), NULL);
891     child->parent = parent;
892     child->prev = prev;
893     child->next = next;
895     if (prev)
896         prev->next = child;
897     if (next)
898         next->prev = child;
900     return child;
903 NRArenaItem *
904 nr_arena_item_detach (NRArenaItem *parent, NRArenaItem *child)
906     nr_return_val_if_fail (parent != NULL, NULL);
907     nr_return_val_if_fail (NR_IS_ARENA_ITEM (parent), NULL);
908     nr_return_val_if_fail (child != NULL, NULL);
909     nr_return_val_if_fail (NR_IS_ARENA_ITEM (child), NULL);
910     nr_return_val_if_fail (child->parent == parent, NULL);
912     NRArenaItem *prev = child->prev;
913     NRArenaItem *next = child->next;
915     child->parent = NULL;
916     child->prev = NULL;
917     child->next = NULL;
919     if (prev)
920         prev->next = next;
921     if (next)
922         next->prev = prev;
924     return next;
927 /*
928   Local Variables:
929   mode:c++
930   c-file-style:"stroustrup"
931   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
932   indent-tabs-mode:nil
933   fill-column:99
934   End:
935 */
936 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :