Code

show waiting cursor while ExecutionEnv is working
[inkscape.git] / src / sp-pattern.cpp
1 #define __SP_PATTERN_C__
3 /*
4  * SVG <pattern> implementation
5  *
6  * Author:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *
10  * Copyright (C) 2002 Lauris Kaplinski
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #include <cstring>
20 #include <string>
21 #include <libnr/nr-matrix-ops.h>
22 #include "libnr/nr-matrix-fns.h"
23 #include <2geom/transforms.h>
24 #include "macros.h"
25 #include "svg/svg.h"
26 #include "display/nr-arena.h"
27 #include "display/nr-arena-group.h"
28 #include "attributes.h"
29 #include "document-private.h"
30 #include "uri.h"
31 #include "sp-pattern.h"
32 #include "xml/repr.h"
34 #include <sigc++/functors/ptr_fun.h>
35 #include <sigc++/adaptors/bind.h>
37 /*
38  * Pattern
39  */
41 class SPPatPainter;
43 struct SPPatPainter {
44         SPPainter painter;
45         SPPattern *pat;
47         Geom::Matrix ps2px;
48         Geom::Matrix px2ps;
49         Geom::Matrix pcs2px;
51         NRArena *arena;
52         unsigned int dkey;
53         NRArenaItem *root;
54         
55         bool         use_cached_tile;
56         Geom::Matrix     ca2pa;
57         Geom::Matrix     pa2ca;
58         NRRectL      cached_bbox;
59         NRPixBlock   cached_tile;
60 };
62 static void sp_pattern_class_init (SPPatternClass *klass);
63 static void sp_pattern_init (SPPattern *gr);
65 static void sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
66 static void sp_pattern_release (SPObject *object);
67 static void sp_pattern_set (SPObject *object, unsigned int key, const gchar *value);
68 static void sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
69 static void sp_pattern_update (SPObject *object, SPCtx *ctx, unsigned int flags);
70 static void sp_pattern_modified (SPObject *object, unsigned int flags);
72 static void pattern_ref_changed(SPObject *old_ref, SPObject *ref, SPPattern *pat);
73 static void pattern_ref_modified (SPObject *ref, guint flags, SPPattern *pattern);
75 static SPPainter *sp_pattern_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, Geom::Matrix const &parent_transform, const NRRect *bbox);
76 static void sp_pattern_painter_free (SPPaintServer *ps, SPPainter *painter);
78 static SPPaintServerClass * pattern_parent_class;
80 GType
81 sp_pattern_get_type (void)
82 {
83         static GType pattern_type = 0;
84         if (!pattern_type) {
85                 GTypeInfo pattern_info = {
86                         sizeof (SPPatternClass),
87                         NULL,   /* base_init */
88                         NULL,   /* base_finalize */
89                         (GClassInitFunc) sp_pattern_class_init,
90                         NULL,   /* class_finalize */
91                         NULL,   /* class_data */
92                         sizeof (SPPattern),
93                         16,     /* n_preallocs */
94                         (GInstanceInitFunc) sp_pattern_init,
95                         NULL,   /* value_table */
96                 };
97                 pattern_type = g_type_register_static (SP_TYPE_PAINT_SERVER, "SPPattern", &pattern_info, (GTypeFlags)0);
98         }
99         return pattern_type;
102 static void
103 sp_pattern_class_init (SPPatternClass *klass)
105         SPObjectClass *sp_object_class;
106         SPPaintServerClass *ps_class;
108         sp_object_class = (SPObjectClass *) klass;
109         ps_class = (SPPaintServerClass *) klass;
111         pattern_parent_class = (SPPaintServerClass*)g_type_class_ref (SP_TYPE_PAINT_SERVER);
113         sp_object_class->build = sp_pattern_build;
114         sp_object_class->release = sp_pattern_release;
115         sp_object_class->set = sp_pattern_set;
116         sp_object_class->child_added = sp_pattern_child_added;
117         sp_object_class->update = sp_pattern_update;
118         sp_object_class->modified = sp_pattern_modified;
120         // do we need _write? seems to work without it
122         ps_class->painter_new = sp_pattern_painter_new;
123         ps_class->painter_free = sp_pattern_painter_free;
126 static void
127 sp_pattern_init (SPPattern *pat)
129         pat->ref = new SPPatternReference(SP_OBJECT(pat));
130         pat->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(pattern_ref_changed), pat));
132         pat->patternUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
133         pat->patternUnits_set = FALSE;
135         pat->patternContentUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
136         pat->patternContentUnits_set = FALSE;
138         pat->patternTransform = NR::identity();
139         pat->patternTransform_set = FALSE;
141         pat->x.unset();
142         pat->y.unset();
143         pat->width.unset();
144         pat->height.unset();
146         pat->viewBox_set = FALSE;
148         new (&pat->modified_connection) sigc::connection();
151 static void
152 sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
154         if (((SPObjectClass *) pattern_parent_class)->build)
155                 (* ((SPObjectClass *) pattern_parent_class)->build) (object, document, repr);
157         sp_object_read_attr (object, "patternUnits");
158         sp_object_read_attr (object, "patternContentUnits");
159         sp_object_read_attr (object, "patternTransform");
160         sp_object_read_attr (object, "x");
161         sp_object_read_attr (object, "y");
162         sp_object_read_attr (object, "width");
163         sp_object_read_attr (object, "height");
164         sp_object_read_attr (object, "viewBox");
165         sp_object_read_attr (object, "xlink:href");
167         /* Register ourselves */
168         sp_document_add_resource (document, "pattern", object);
171 static void
172 sp_pattern_release (SPObject *object)
174         SPPattern *pat;
176         pat = (SPPattern *) object;
178         if (SP_OBJECT_DOCUMENT (object)) {
179                 /* Unregister ourselves */
180                 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "pattern", SP_OBJECT (object));
181         }
183         if (pat->ref) {
184                 pat->modified_connection.disconnect();
185                 pat->ref->detach();
186                 delete pat->ref;
187                 pat->ref = NULL;
188         }
190         pat->modified_connection.~connection();
192         if (((SPObjectClass *) pattern_parent_class)->release)
193                 ((SPObjectClass *) pattern_parent_class)->release (object);
196 static void
197 sp_pattern_set (SPObject *object, unsigned int key, const gchar *value)
199         SPPattern *pat = SP_PATTERN (object);
201         switch (key) {
202         case SP_ATTR_PATTERNUNITS:
203                 if (value) {
204                         if (!strcmp (value, "userSpaceOnUse")) {
205                                 pat->patternUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
206                         } else {
207                                 pat->patternUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
208                         }
209                         pat->patternUnits_set = TRUE;
210                 } else {
211                         pat->patternUnits_set = FALSE;
212                 }
213                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
214                 break;
215         case SP_ATTR_PATTERNCONTENTUNITS:
216                 if (value) {
217                         if (!strcmp (value, "userSpaceOnUse")) {
218                                 pat->patternContentUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
219                         } else {
220                                 pat->patternContentUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
221                         }
222                         pat->patternContentUnits_set = TRUE;
223                 } else {
224                         pat->patternContentUnits_set = FALSE;
225                 }
226                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
227                 break;
228         case SP_ATTR_PATTERNTRANSFORM: {
229                 Geom::Matrix t;
230                 if (value && sp_svg_transform_read (value, &t)) {
231                         pat->patternTransform = t;
232                         pat->patternTransform_set = TRUE;
233                 } else {
234                         pat->patternTransform = NR::identity();
235                         pat->patternTransform_set = FALSE;
236                 }
237                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
238                 break;
239         }
240         case SP_ATTR_X:
241                 pat->x.readOrUnset(value);
242                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
243                 break;
244         case SP_ATTR_Y:
245                 pat->y.readOrUnset(value);
246                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
247                 break;
248         case SP_ATTR_WIDTH:
249                 pat->width.readOrUnset(value);
250                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
251                 break;
252         case SP_ATTR_HEIGHT:
253                 pat->height.readOrUnset(value);
254                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
255                 break;
256         case SP_ATTR_VIEWBOX: {
257                 /* fixme: Think (Lauris) */
258                 double x, y, width, height;
259                 char *eptr;
261                 if (value) {
262                         eptr = (gchar *) value;
263                         x = g_ascii_strtod (eptr, &eptr);
264                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
265                         y = g_ascii_strtod (eptr, &eptr);
266                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
267                         width = g_ascii_strtod (eptr, &eptr);
268                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
269                         height = g_ascii_strtod (eptr, &eptr);
270                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
271                         if ((width > 0) && (height > 0)) {
272                                 pat->viewBox.x0 = x;
273                                 pat->viewBox.y0 = y;
274                                 pat->viewBox.x1 = x + width;
275                                 pat->viewBox.y1 = y + height;
276                                 pat->viewBox_set = TRUE;
277                         } else {
278                                 pat->viewBox_set = FALSE;
279                         }
280                 } else {
281                         pat->viewBox_set = FALSE;
282                 }
283                 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
284                 break;
285         }
286         case SP_ATTR_XLINK_HREF:
287                 if ( value && pat->href && ( strcmp(value, pat->href) == 0 ) ) {
288                         /* Href unchanged, do nothing. */
289                 } else {
290                         g_free(pat->href);
291                         pat->href = NULL;
292                         if (value) {
293                                 // First, set the href field; it's only used in the "unchanged" check above.
294                                 pat->href = g_strdup(value);
295                                 // Now do the attaching, which emits the changed signal.
296                                 if (value) {
297                                         try {
298                                                 pat->ref->attach(Inkscape::URI(value));
299                                         } catch (Inkscape::BadURIException &e) {
300                                                 g_warning("%s", e.what());
301                                                 pat->ref->detach();
302                                         }
303                                 } else {
304                                         pat->ref->detach();
305                                 }
306                         }
307                 }
308                 break;
309         default:
310                 if (((SPObjectClass *) pattern_parent_class)->set)
311                         ((SPObjectClass *) pattern_parent_class)->set (object, key, value);
312                 break;
313         }
316 static void
317 sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
319         SPPattern *pat = SP_PATTERN (object);
321         if (((SPObjectClass *) (pattern_parent_class))->child_added)
322                 (* ((SPObjectClass *) (pattern_parent_class))->child_added) (object, child, ref);
324         SPObject *ochild = sp_object_get_child_by_repr(object, child);
325         if (SP_IS_ITEM (ochild)) {
327                 SPPaintServer *ps = SP_PAINT_SERVER (pat);
328                 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
330                 for (SPPainter *p = ps->painters; p != NULL; p = p->next) {
332                         SPPatPainter *pp = (SPPatPainter *) p;
333                         NRArenaItem *ai = sp_item_invoke_show (SP_ITEM (ochild), pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
335                         if (ai) {
336                                 nr_arena_item_add_child (pp->root, ai, NULL);
337                                 nr_arena_item_set_order (ai, position);
338                                 nr_arena_item_unref (ai);
339                         }
340                 }
341         }
344 /* TODO: do we need a ::remove_child handler? */
346 /* fixme: We need ::order_changed handler too (Lauris) */
348 GSList *
349 pattern_getchildren (SPPattern *pat)
351         GSList *l = NULL;
353         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
354                 if (sp_object_first_child(SP_OBJECT(pat_i))) { // find the first one with children
355                         for (SPObject *child = sp_object_first_child(SP_OBJECT (pat)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
356                                 l = g_slist_prepend (l, child);
357                         }
358                         break; // do not go further up the chain if children are found
359                 }
360         }
362         return l;
365 static void
366 sp_pattern_update (SPObject *object, SPCtx *ctx, unsigned int flags)
368         SPPattern *pat = SP_PATTERN (object);
370         if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
371         flags &= SP_OBJECT_MODIFIED_CASCADE;
373         GSList *l = pattern_getchildren (pat);
374         l = g_slist_reverse (l);
376         while (l) {
377                 SPObject *child = SP_OBJECT (l->data);
378                 sp_object_ref (child, NULL);
379                 l = g_slist_remove (l, child);
380                 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
381                         child->updateDisplay(ctx, flags);
382                 }
383                 sp_object_unref (child, NULL);
384         }
387 static void
388 sp_pattern_modified (SPObject *object, guint flags)
390         SPPattern *pat = SP_PATTERN (object);
392         if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
393         flags &= SP_OBJECT_MODIFIED_CASCADE;
395         GSList *l = pattern_getchildren (pat);
396         l = g_slist_reverse (l);
398         while (l) {
399                 SPObject *child = SP_OBJECT (l->data);
400                 sp_object_ref (child, NULL);
401                 l = g_slist_remove (l, child);
402                 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
403                         child->emitModified(flags);
404                 }
405                 sp_object_unref (child, NULL);
406         }
409 /**
410 Gets called when the pattern is reattached to another <pattern>
411 */
412 static void
413 pattern_ref_changed(SPObject *old_ref, SPObject *ref, SPPattern *pat)
415         if (old_ref) {
416                 pat->modified_connection.disconnect();
417         }
418         if (SP_IS_PATTERN (ref)) {
419                 pat->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&pattern_ref_modified), pat));
420         }
422         pattern_ref_modified (ref, 0, pat);
425 /**
426 Gets called when the referenced <pattern> is changed
427 */
428 static void
429 pattern_ref_modified (SPObject */*ref*/, guint /*flags*/, SPPattern *pattern)
431         if (SP_IS_OBJECT (pattern))
432                 SP_OBJECT (pattern)->requestModified(SP_OBJECT_MODIFIED_FLAG);
435 guint
436 pattern_users (SPPattern *pattern)
438         return SP_OBJECT (pattern)->hrefcount;
441 SPPattern *
442 pattern_chain (SPPattern *pattern)
444         SPDocument *document = SP_OBJECT_DOCUMENT (pattern);
445         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
446         Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
448         Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
449         repr->setAttribute("inkscape:collect", "always");
450         gchar *parent_ref = g_strconcat ("#", SP_OBJECT_REPR(pattern)->attribute("id"), NULL);
451         repr->setAttribute("xlink:href",  parent_ref);
452         g_free (parent_ref);
454         defsrepr->addChild(repr, NULL);
455         const gchar *child_id = repr->attribute("id");
456         SPObject *child = document->getObjectById(child_id);
457         g_assert (SP_IS_PATTERN (child));
459         return SP_PATTERN (child);
462 SPPattern *
463 sp_pattern_clone_if_necessary (SPItem *item, SPPattern *pattern, const gchar *property)
465         if (pattern_users(pattern) > 1) {
466                 pattern = pattern_chain (pattern);
467                 gchar *href = g_strconcat ("url(#", SP_OBJECT_REPR (pattern)->attribute("id"), ")", NULL);
469                 SPCSSAttr *css = sp_repr_css_attr_new ();
470                 sp_repr_css_set_property (css, property, href);
471                 sp_repr_css_change_recursive (SP_OBJECT_REPR (item), css, "style");
472         }
473         return pattern;
476 void
477 sp_pattern_transform_multiply (SPPattern *pattern, Geom::Matrix postmul, bool set)
479         // this formula is for a different interpretation of pattern transforms as described in (*) in sp-pattern.cpp
480         // for it to work, we also need    sp_object_read_attr (SP_OBJECT (item), "transform");
481         //pattern->patternTransform = premul * item->transform * pattern->patternTransform * item->transform.inverse() * postmul;
483         // otherwise the formula is much simpler
484         if (set) {
485                 pattern->patternTransform = postmul;
486         } else {
487                 pattern->patternTransform = pattern_patternTransform(pattern) * postmul;
488         }
489         pattern->patternTransform_set = TRUE;
491         gchar *c=sp_svg_transform_write(pattern->patternTransform);
492         SP_OBJECT_REPR(pattern)->setAttribute("patternTransform", c);
493         g_free(c);
496 const gchar *
497 pattern_tile (GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Matrix transform, Geom::Matrix move)
499         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
500         Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
502         Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
503         repr->setAttribute("patternUnits", "userSpaceOnUse");
504         sp_repr_set_svg_double(repr, "width", bounds.dimensions()[Geom::X]);
505         sp_repr_set_svg_double(repr, "height", bounds.dimensions()[Geom::Y]);
507         gchar *t=sp_svg_transform_write(transform);
508         repr->setAttribute("patternTransform", t);
509         g_free(t);
511         defsrepr->appendChild(repr);
512         const gchar *pat_id = repr->attribute("id");
513         SPObject *pat_object = document->getObjectById(pat_id);
515         for (GSList *i = reprs; i != NULL; i = i->next) {
516                 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data);
517                 SPItem *copy = SP_ITEM(pat_object->appendChildRepr(node));
519                 Geom::Matrix dup_transform;
520                 if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform))
521                         dup_transform = Geom::identity();
522                 dup_transform *= move;
524                 sp_item_write_transform(copy, SP_OBJECT_REPR(copy), dup_transform);
525         }
527         Inkscape::GC::release(repr);
528         return pat_id;
531 SPPattern *
532 pattern_getroot (SPPattern *pat)
534         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
535                 if (sp_object_first_child(SP_OBJECT(pat_i))) { // find the first one with children
536                         return pat_i;
537                 }
538         }
539         return pat; // document is broken, we can't get to root; but at least we can return pat which is supposedly a valid pattern
544 // Access functions that look up fields up the chain of referenced patterns and return the first one which is set
545 // FIXME: all of them must use chase_hrefs the same as in SPGradient, to avoid lockup on circular refs
547 guint pattern_patternUnits (SPPattern *pat)
549         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
550                 if (pat_i->patternUnits_set)
551                         return pat_i->patternUnits;
552         }
553         return pat->patternUnits;
556 guint pattern_patternContentUnits (SPPattern *pat)
558         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
559                 if (pat_i->patternContentUnits_set)
560                         return pat_i->patternContentUnits;
561         }
562         return pat->patternContentUnits;
565 Geom::Matrix const &pattern_patternTransform(SPPattern const *pat)
567         for (SPPattern const *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
568                 if (pat_i->patternTransform_set)
569                         return pat_i->patternTransform;
570         }
571         return pat->patternTransform;
574 gdouble pattern_x (SPPattern *pat)
576         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
577                 if (pat_i->x._set)
578                         return pat_i->x.computed;
579         }
580         return 0;
583 gdouble pattern_y (SPPattern *pat)
585         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
586                 if (pat_i->y._set)
587                         return pat_i->y.computed;
588         }
589         return 0;
592 gdouble pattern_width (SPPattern *pat)
594         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
595                 if (pat_i->width._set)
596                         return pat_i->width.computed;
597         }
598         return 0;
601 gdouble pattern_height (SPPattern *pat)
603         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
604                 if (pat_i->height._set)
605                         return pat_i->height.computed;
606         }
607         return 0;
610 NRRect *pattern_viewBox (SPPattern *pat)
612         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
613                 if (pat_i->viewBox_set)
614                         return &(pat_i->viewBox);
615         }
616         return &(pat->viewBox);
619 bool pattern_hasItemChildren (SPPattern *pat)
621         for (SPObject *child = sp_object_first_child(SP_OBJECT(pat)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
622                 if (SP_IS_ITEM (child)) {
623                         return true;
624                 }
625         }
626         return false;
631 /* Painter */
633 static void sp_pat_fill (SPPainter *painter, NRPixBlock *pb);
635 /**
636 Creates a painter (i.e. the thing that does actual filling at the given zoom).
637 See (*) below for why the parent_transform may be necessary.
638 */
639 static SPPainter *
640 sp_pattern_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, Geom::Matrix const &/*parent_transform*/, const NRRect *bbox)
642         SPPattern *pat = SP_PATTERN (ps);
643         SPPatPainter *pp = g_new (SPPatPainter, 1);
645         pp->painter.type = SP_PAINTER_IND;
646         pp->painter.fill = sp_pat_fill;
648         pp->pat = pat;
650         if (pattern_patternUnits (pat) == SP_PATTERN_UNITS_OBJECTBOUNDINGBOX) {
651                 /* BBox to user coordinate system */
652                 Geom::Matrix bbox2user (bbox->x1 - bbox->x0, 0.0, 0.0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
654                 // the final patternTransform, taking into account bbox
655                 Geom::Matrix const ps2user(pattern_patternTransform(pat) * bbox2user);
657                 // see (*) comment below
658                 pp->ps2px = ps2user * full_transform;
659         } else {
660                 /* Problem: What to do, if we have mixed lengths and percentages? */
661                 /* Currently we do ignore percentages at all, but that is not good (lauris) */
663                 /* fixme: We may try to normalize here too, look at linearGradient (Lauris) */
665                 // (*) The spec says, "This additional transformation matrix [patternTransform] is
666                 // post-multiplied to (i.e., inserted to the right of) any previously defined
667                 // transformations, including the implicit transformation necessary to convert from
668                 // object bounding box units to user space." To me, this means that the order should be:
669                 // item_transform * patternTransform * parent_transform
670                 // However both Batik and Adobe plugin use:
671                 // patternTransform * item_transform * parent_transform
672                 // So here I comply with the majority opinion, but leave my interpretation commented out below.
673                 // (To get item_transform, I subtract parent from full.)
675                 //pp->ps2px = (full_transform / parent_transform) * pattern_patternTransform(pat) * parent_transform;
676                 pp->ps2px = pattern_patternTransform(pat) * full_transform;
677         }
679         pp->px2ps = pp->ps2px.inverse();
681         if (pat->viewBox_set) {
682                 gdouble tmp_x = pattern_width (pat) / (pattern_viewBox(pat)->x1 - pattern_viewBox(pat)->x0);
683                 gdouble tmp_y = pattern_height (pat) / (pattern_viewBox(pat)->y1 - pattern_viewBox(pat)->y0);
685                 // FIXME: preserveAspectRatio must be taken into account here too!
686                 Geom::Matrix vb2ps (tmp_x, 0.0, 0.0, tmp_y, pattern_x(pat) - pattern_viewBox(pat)->x0 * tmp_x, pattern_y(pat) - pattern_viewBox(pat)->y0 * tmp_y);
688                 Geom::Matrix vb2us = vb2ps * pattern_patternTransform(pat);
690                 // see (*)
691                 pp->pcs2px = vb2us * full_transform;
692         } else {
693                 /* No viewbox, have to parse units */
694                 if (pattern_patternContentUnits (pat) == SP_PATTERN_UNITS_OBJECTBOUNDINGBOX) {
695                         /* BBox to user coordinate system */
696                         Geom::Matrix bbox2user (bbox->x1 - bbox->x0, 0.0, 0.0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
698                         Geom::Matrix pcs2user = pattern_patternTransform(pat) * bbox2user;
700                         // see (*)
701                         pp->pcs2px = pcs2user * full_transform;
702                 } else {
703                         // see (*)
704                         //pcs2px = (full_transform / parent_transform) * pattern_patternTransform(pat) * parent_transform;
705                         pp->pcs2px = pattern_patternTransform(pat) * full_transform;
706                 }
708                 pp->pcs2px = Geom::Translate (pattern_x (pat), pattern_y (pat)) * pp->pcs2px;
709         }
711         /* Create arena */
712         pp->arena = NRArena::create();
714         pp->dkey = sp_item_display_key_new (1);
716         /* Create group */
717         pp->root = NRArenaGroup::create(pp->arena);
719         /* Show items */
720         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
721                 if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
722                         for (SPObject *child = sp_object_first_child(SP_OBJECT(pat_i)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
723                                 if (SP_IS_ITEM (child)) {
724                                         NRArenaItem *cai;
725                                         cai = sp_item_invoke_show (SP_ITEM (child), pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
726                                         nr_arena_item_append_child (pp->root, cai);
727                                 }
728                         }
729                         break; // do not go further up the chain if children are found
730                 }
731         }
733         {
734                 NRRect    one_tile,tr_tile;
735                 one_tile.x0=pattern_x(pp->pat);
736                 one_tile.y0=pattern_y(pp->pat);
737                 one_tile.x1=one_tile.x0+pattern_width (pp->pat);
738                 one_tile.y1=one_tile.y0+pattern_height (pp->pat);
739                 // TODO: remove ps2px_nr after converting to 2geom
740                 NR::Matrix ps2px_nr = from_2geom(pp->ps2px);
741                 nr_rect_d_matrix_transform (&tr_tile, &one_tile, &ps2px_nr);
742                 int       tr_width=(int)ceil(1.3*(tr_tile.x1-tr_tile.x0));
743                 int       tr_height=(int)ceil(1.3*(tr_tile.y1-tr_tile.y0));
744 //              if ( tr_width < 10000 && tr_height < 10000 && tr_width*tr_height < 1000000 ) {
745                 pp->use_cached_tile=false;//true;
746                         if ( tr_width > 1000 ) tr_width=1000;
747                         if ( tr_height > 1000 ) tr_height=1000;
748                         pp->cached_bbox.x0=0;
749                         pp->cached_bbox.y0=0;
750                         pp->cached_bbox.x1=tr_width;
751                         pp->cached_bbox.y1=tr_height;
753                         if (pp->use_cached_tile) {
754                                 nr_pixblock_setup (&pp->cached_tile,NR_PIXBLOCK_MODE_R8G8B8A8N, pp->cached_bbox.x0, pp->cached_bbox.y0, pp->cached_bbox.x1, pp->cached_bbox.y1,TRUE);
755                         }
757                         pp->pa2ca[0]=((double)tr_width)/(one_tile.x1-one_tile.x0);
758                         pp->pa2ca[1]=0;
759                         pp->pa2ca[2]=0;
760                         pp->pa2ca[3]=((double)tr_height)/(one_tile.y1-one_tile.y0);
761                         pp->pa2ca[4]=-one_tile.x0*pp->pa2ca[0];
762                         pp->pa2ca[5]=-one_tile.y0*pp->pa2ca[1];
763                         pp->ca2pa[0]=(one_tile.x1-one_tile.x0)/((double)tr_width);
764                         pp->ca2pa[1]=0;
765                         pp->ca2pa[2]=0;
766                         pp->ca2pa[3]=(one_tile.y1-one_tile.y0)/((double)tr_height);
767                         pp->ca2pa[4]=one_tile.x0;
768                         pp->ca2pa[5]=one_tile.y0;
769 //              } else {
770 //                      pp->use_cached_tile=false;
771 //              }
772         }
773         
774         NRGC gc(NULL);
775         if ( pp->use_cached_tile ) {
776                 gc.transform=pp->pa2ca;
777         } else {
778                 gc.transform = pp->pcs2px;
779         }
780         nr_arena_item_invoke_update (pp->root, NULL, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_ALL);
781         if ( pp->use_cached_tile ) {
782                 nr_arena_item_invoke_render (NULL, pp->root, &pp->cached_bbox, &pp->cached_tile, 0);
783         } else {
784                 // nothing to do now
785         }
786         
787         return (SPPainter *) pp;
790 static void
791 sp_pattern_painter_free (SPPaintServer */*ps*/, SPPainter *painter)
793         SPPatPainter *pp = (SPPatPainter *) painter;
794         SPPattern *pat = pp->pat;
796         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
797                 if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
798                         for (SPObject *child = sp_object_first_child(SP_OBJECT(pat_i)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
799                                 if (SP_IS_ITEM (child)) {
800                                                 sp_item_invoke_hide (SP_ITEM (child), pp->dkey);
801                                 }
802                         }
803                         break; // do not go further up the chain if children are found
804                 }
805         }
806         if ( pp->use_cached_tile ) nr_pixblock_release(&pp->cached_tile);
807         g_free (pp);
810 void
811 get_cached_tile_pixel(SPPatPainter* pp,double x,double y,unsigned char &r,unsigned char &g,unsigned char &b,unsigned char &a)
813         int    ca_h=(int)floor(x);
814         int    ca_v=(int)floor(y);
815         int    r_x=(int)floor(16*(x-floor(x)));
816         int    r_y=(int)floor(16*(y-floor(y)));
817         unsigned int    tl_m=(16-r_x)*(16-r_y);
818         unsigned int    bl_m=(16-r_x)*r_y;
819         unsigned int    tr_m=r_x*(16-r_y);
820         unsigned int    br_m=r_x*r_y;
821         int    cb_h=ca_h+1;
822         int    cb_v=ca_v+1;
823         if ( cb_h >= pp->cached_bbox.x1 ) cb_h=0;
824         if ( cb_v >= pp->cached_bbox.y1 ) cb_v=0;
825         
826         unsigned char* tlx=NR_PIXBLOCK_PX(&pp->cached_tile)+(ca_v*pp->cached_tile.rs)+4*ca_h;
827         unsigned char* trx=NR_PIXBLOCK_PX(&pp->cached_tile)+(ca_v*pp->cached_tile.rs)+4*cb_h;
828         unsigned char* blx=NR_PIXBLOCK_PX(&pp->cached_tile)+(cb_v*pp->cached_tile.rs)+4*ca_h;
829         unsigned char* brx=NR_PIXBLOCK_PX(&pp->cached_tile)+(cb_v*pp->cached_tile.rs)+4*cb_h;
830         
831         unsigned int tl_c=tlx[0];
832         unsigned int tr_c=trx[0];
833         unsigned int bl_c=blx[0];
834         unsigned int br_c=brx[0];
835         unsigned int f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
836         r=f_c;
837         tl_c=tlx[1];
838         tr_c=trx[1];
839         bl_c=blx[1];
840         br_c=brx[1];
841         f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
842         g=f_c;
843         tl_c=tlx[2];
844         tr_c=trx[2];
845         bl_c=blx[2];
846         br_c=brx[2];
847         f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
848         b=f_c;
849         tl_c=tlx[3];
850         tr_c=trx[3];
851         bl_c=blx[3];
852         br_c=brx[3];
853         f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
854         a=f_c;
857 static void
858 sp_pat_fill (SPPainter *painter, NRPixBlock *pb)
860         SPPatPainter *pp;
861         NRRect ba, psa;
862         NRRectL area;
863         double x, y;
865         pp = (SPPatPainter *) painter;
867         if (pattern_width (pp->pat) < NR_EPSILON) return;
868         if (pattern_height (pp->pat) < NR_EPSILON) return;
870         /* Find buffer area in gradient space */
871         /* fixme: This is suboptimal (Lauris) */
873         if ( pp->use_cached_tile ) {
874                 double   pat_w=pattern_width (pp->pat);
875                 double   pat_h=pattern_height (pp->pat);
876                 if ( pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N || pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P ) { // same thing because it's filling an empty pixblock
877                         unsigned char*  lpx=NR_PIXBLOCK_PX(pb);
878                         double          px_y=pb->area.y0;
879                         for (int j=pb->area.y0;j<pb->area.y1;j++) {
880                                 unsigned char* cpx=lpx;
881                                 double         px_x = pb->area.x0;
882                                 
883                                 double ps_x=pp->px2ps[0]*px_x+pp->px2ps[2]*px_y+pp->px2ps[4];
884                                 double ps_y=pp->px2ps[1]*px_x+pp->px2ps[3]*px_y+pp->px2ps[5];
885                                 for (int i=pb->area.x0;i<pb->area.x1;i++) {
886                                         while ( ps_x > pat_w ) ps_x-=pat_w;
887                                         while ( ps_x < 0 ) ps_x+=pat_w;
888                                         while ( ps_y > pat_h ) ps_y-=pat_h;
889                                         while ( ps_y < 0 ) ps_y+=pat_h;
890                                         double ca_x=pp->pa2ca[0]*ps_x+pp->pa2ca[2]*ps_y+pp->pa2ca[4];
891                                         double ca_y=pp->pa2ca[1]*ps_x+pp->pa2ca[3]*ps_y+pp->pa2ca[5];
892                                         unsigned char n_a,n_r,n_g,n_b;
893                                         get_cached_tile_pixel(pp,ca_x,ca_y,n_r,n_g,n_b,n_a);
894                                         cpx[0]=n_r;
895                                         cpx[1]=n_g;
896                                         cpx[2]=n_b;
897                                         cpx[3]=n_a;
898                                         
899                                         px_x+=1.0;
900                                         ps_x+=pp->px2ps[0];
901                                         ps_y+=pp->px2ps[1];
902                                         cpx+=4;
903                                 }
904                                 px_y+=1.0;
905                                 lpx+=pb->rs;
906                         }
907                 } else if ( pb->mode == NR_PIXBLOCK_MODE_R8G8B8 ) {
908                         unsigned char*  lpx=NR_PIXBLOCK_PX(pb);
909                         double          px_y=pb->area.y0;
910                         for (int j=pb->area.y0;j<pb->area.y1;j++) {
911                                 unsigned char* cpx=lpx;
912                                 double         px_x = pb->area.x0;
913                                 
914                                 double ps_x=pp->px2ps[0]*px_x+pp->px2ps[2]*px_y+pp->px2ps[4];
915                                 double ps_y=pp->px2ps[1]*px_x+pp->px2ps[3]*px_y+pp->px2ps[5];
916                                 for (int i=pb->area.x0;i<pb->area.x1;i++) {
917                                         while ( ps_x > pat_w ) ps_x-=pat_w;
918                                         while ( ps_x < 0 ) ps_x+=pat_w;
919                                         while ( ps_y > pat_h ) ps_y-=pat_h;
920                                         while ( ps_y < 0 ) ps_y+=pat_h;
921                                         double ca_x=pp->pa2ca[0]*ps_x+pp->pa2ca[2]*ps_y+pp->pa2ca[4];
922                                         double ca_y=pp->pa2ca[1]*ps_x+pp->pa2ca[3]*ps_y+pp->pa2ca[5];
923                                         unsigned char n_a,n_r,n_g,n_b;
924                                         get_cached_tile_pixel(pp,ca_x,ca_y,n_r,n_g,n_b,n_a);
925                                         cpx[0]=n_r;
926                                         cpx[1]=n_g;
927                                         cpx[2]=n_b;
928                                         
929                                         px_x+=1.0;
930                                         ps_x+=pp->px2ps[0];
931                                         ps_y+=pp->px2ps[1];
932                                         cpx+=4;
933                                 }
934                                 px_y+=1.0;
935                                 lpx+=pb->rs;
936                         }
937                 }
938         } else {
939                 ba.x0 = pb->area.x0;
940                 ba.y0 = pb->area.y0;
941                 ba.x1 = pb->area.x1;
942                 ba.y1 = pb->area.y1;
943                 
944         // Trying to solve this bug: https://bugs.launchpad.net/inkscape/+bug/167416
945         // Bail out if the transformation matrix has extreme values. If we bail out
946         // however, then something (which was meaningless anyway) won't be rendered, 
947         // which is better than getting stuck in a virtually infinite loop
948         if (fabs(pp->px2ps[0]) < 1e6 && 
949             fabs(pp->px2ps[3]) < 1e6 &&
950             fabs(pp->px2ps[4]) < 1e6 &&
951             fabs(pp->px2ps[5]) < 1e6) 
952         {
953             // TODO: remove px2ps_nr after converting to 2geom
954             NR::Matrix px2ps_nr = from_2geom(pp->px2ps);
955             nr_rect_d_matrix_transform (&psa, &ba, &px2ps_nr);
956                 
957                 psa.x0 = floor ((psa.x0 - pattern_x (pp->pat)) / pattern_width (pp->pat)) -1;
958                 psa.y0 = floor ((psa.y0 - pattern_y (pp->pat)) / pattern_height (pp->pat)) -1;
959                 psa.x1 = ceil ((psa.x1 - pattern_x (pp->pat)) / pattern_width (pp->pat)) +1;
960                 psa.y1 = ceil ((psa.y1 - pattern_y (pp->pat)) / pattern_height (pp->pat)) +1;
961                 
962             // If psa is too wide or tall, then something must be wrong! This is due to
963             // nr_rect_d_matrix_transform (&psa, &ba, &pp->px2ps) using a weird transformation matrix pp->px2ps.
964             g_assert(std::abs(psa.x1 - psa.x0) < 1e6);
965             g_assert(std::abs(psa.y1 - psa.y0) < 1e6);
966             
967             for (y = psa.y0; y < psa.y1; y++) {
968                         for (x = psa.x0; x < psa.x1; x++) {
969                                 NRPixBlock ppb;
970                                 double psx, psy;
971                                 
972                                 psx = x * pattern_width (pp->pat);
973                                 psy = y * pattern_height (pp->pat);
974                                 
975                                 area.x0 = (gint32)(pb->area.x0 - (pp->ps2px[0] * psx + pp->ps2px[2] * psy));
976                                 area.y0 = (gint32)(pb->area.y0 - (pp->ps2px[1] * psx + pp->ps2px[3] * psy));
977                                 area.x1 = area.x0 + pb->area.x1 - pb->area.x0;
978                                 area.y1 = area.y0 + pb->area.y1 - pb->area.y0;
979                                 
980                                 // We do not update here anymore
981     
982                                 // Set up buffer
983                                 // fixme: (Lauris)
984                                 nr_pixblock_setup_extern (&ppb, pb->mode, area.x0, area.y0, area.x1, area.y1, NR_PIXBLOCK_PX (pb), pb->rs, FALSE, FALSE);
985                                 
986                                 nr_arena_item_invoke_render (NULL, pp->root, &area, &ppb, 0);
987                                 
988                                 nr_pixblock_release (&ppb);
989                         }
990                 }
991         } 
992         }