Code

* [INTL: es] Spanish translation update by Lucas Vieites (closes: #180399)
[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 #include "config.h"
17 #include <libnr/nr-matrix-ops.h>
18 #include "libnr/nr-matrix-fns.h"
19 #include <libnr/nr-translate-matrix-ops.h>
20 #include "macros.h"
21 #include "svg/svg.h"
22 #include "display/nr-arena.h"
23 #include "display/nr-arena-group.h"
24 #include "attributes.h"
25 #include "document-private.h"
26 #include "uri.h"
27 #include "sp-pattern.h"
28 #include "xml/repr.h"
30 #include <sigc++/functors/ptr_fun.h>
31 #include <sigc++/adaptors/bind.h>
33 /*
34  * Pattern
35  */
37 class SPPatPainter;
39 struct SPPatPainter {
40         SPPainter painter;
41         SPPattern *pat;
43         NRMatrix ps2px;
44         NRMatrix px2ps;
45         NRMatrix pcs2px;
47         NRArena *arena;
48         unsigned int dkey;
49         NRArenaItem *root;
50         
51         bool         use_cached_tile;
52         NRMatrix     ca2pa;
53         NRMatrix     pa2ca;
54         NRRectL      cached_bbox;
55         NRPixBlock   cached_tile;
56 };
58 static void sp_pattern_class_init (SPPatternClass *klass);
59 static void sp_pattern_init (SPPattern *gr);
61 static void sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
62 static void sp_pattern_release (SPObject *object);
63 static void sp_pattern_set (SPObject *object, unsigned int key, const gchar *value);
64 static void sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
65 static void sp_pattern_update (SPObject *object, SPCtx *ctx, unsigned int flags);
66 static void sp_pattern_modified (SPObject *object, unsigned int flags);
68 static void pattern_ref_changed(SPObject *old_ref, SPObject *ref, SPPattern *pat);
69 static void pattern_ref_modified (SPObject *ref, guint flags, SPPattern *pattern);
71 static SPPainter *sp_pattern_painter_new (SPPaintServer *ps, NR::Matrix const &full_transform, NR::Matrix const &parent_transform, const NRRect *bbox);
72 static void sp_pattern_painter_free (SPPaintServer *ps, SPPainter *painter);
74 static SPPaintServerClass * pattern_parent_class;
76 GType
77 sp_pattern_get_type (void)
78 {
79         static GType pattern_type = 0;
80         if (!pattern_type) {
81                 GTypeInfo pattern_info = {
82                         sizeof (SPPatternClass),
83                         NULL,   /* base_init */
84                         NULL,   /* base_finalize */
85                         (GClassInitFunc) sp_pattern_class_init,
86                         NULL,   /* class_finalize */
87                         NULL,   /* class_data */
88                         sizeof (SPPattern),
89                         16,     /* n_preallocs */
90                         (GInstanceInitFunc) sp_pattern_init,
91                         NULL,   /* value_table */
92                 };
93                 pattern_type = g_type_register_static (SP_TYPE_PAINT_SERVER, "SPPattern", &pattern_info, (GTypeFlags)0);
94         }
95         return pattern_type;
96 }
98 static void
99 sp_pattern_class_init (SPPatternClass *klass)
101         SPObjectClass *sp_object_class;
102         SPPaintServerClass *ps_class;
104         sp_object_class = (SPObjectClass *) klass;
105         ps_class = (SPPaintServerClass *) klass;
107         pattern_parent_class = (SPPaintServerClass*)g_type_class_ref (SP_TYPE_PAINT_SERVER);
109         sp_object_class->build = sp_pattern_build;
110         sp_object_class->release = sp_pattern_release;
111         sp_object_class->set = sp_pattern_set;
112         sp_object_class->child_added = sp_pattern_child_added;
113         sp_object_class->update = sp_pattern_update;
114         sp_object_class->modified = sp_pattern_modified;
116         // do we need _write? seems to work without it
118         ps_class->painter_new = sp_pattern_painter_new;
119         ps_class->painter_free = sp_pattern_painter_free;
122 static void
123 sp_pattern_init (SPPattern *pat)
125         pat->ref = new SPPatternReference(SP_OBJECT(pat));
126         pat->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(pattern_ref_changed), pat));
128         pat->patternUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
129         pat->patternUnits_set = FALSE;
131         pat->patternContentUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
132         pat->patternContentUnits_set = FALSE;
134         pat->patternTransform = NR::identity();
135         pat->patternTransform_set = FALSE;
137         pat->x.unset();
138         pat->y.unset();
139         pat->width.unset();
140         pat->height.unset();
142         pat->viewBox_set = FALSE;
144         new (&pat->modified_connection) sigc::connection();
147 static void
148 sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
150         if (((SPObjectClass *) pattern_parent_class)->build)
151                 (* ((SPObjectClass *) pattern_parent_class)->build) (object, document, repr);
153         sp_object_read_attr (object, "patternUnits");
154         sp_object_read_attr (object, "patternContentUnits");
155         sp_object_read_attr (object, "patternTransform");
156         sp_object_read_attr (object, "x");
157         sp_object_read_attr (object, "y");
158         sp_object_read_attr (object, "width");
159         sp_object_read_attr (object, "height");
160         sp_object_read_attr (object, "viewBox");
161         sp_object_read_attr (object, "xlink:href");
163         /* Register ourselves */
164         sp_document_add_resource (document, "pattern", object);
167 static void
168 sp_pattern_release (SPObject *object)
170         SPPattern *pat;
172         pat = (SPPattern *) object;
174         if (SP_OBJECT_DOCUMENT (object)) {
175                 /* Unregister ourselves */
176                 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "pattern", SP_OBJECT (object));
177         }
179         if (pat->ref) {
180                 pat->modified_connection.disconnect();
181                 pat->ref->detach();
182                 delete pat->ref;
183                 pat->ref = NULL;
184         }
186         pat->modified_connection.~connection();
188         if (((SPObjectClass *) pattern_parent_class)->release)
189                 ((SPObjectClass *) pattern_parent_class)->release (object);
192 static void
193 sp_pattern_set (SPObject *object, unsigned int key, const gchar *value)
195         SPPattern *pat = SP_PATTERN (object);
197         switch (key) {
198         case SP_ATTR_PATTERNUNITS:
199                 if (value) {
200                         if (!strcmp (value, "userSpaceOnUse")) {
201                                 pat->patternUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
202                         } else {
203                                 pat->patternUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
204                         }
205                         pat->patternUnits_set = TRUE;
206                 } else {
207                         pat->patternUnits_set = FALSE;
208                 }
209                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
210                 break;
211         case SP_ATTR_PATTERNCONTENTUNITS:
212                 if (value) {
213                         if (!strcmp (value, "userSpaceOnUse")) {
214                                 pat->patternContentUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
215                         } else {
216                                 pat->patternContentUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
217                         }
218                         pat->patternContentUnits_set = TRUE;
219                 } else {
220                         pat->patternContentUnits_set = FALSE;
221                 }
222                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
223                 break;
224         case SP_ATTR_PATTERNTRANSFORM: {
225                 NR::Matrix t;
226                 if (value && sp_svg_transform_read (value, &t)) {
227                         pat->patternTransform = t;
228                         pat->patternTransform_set = TRUE;
229                 } else {
230                         pat->patternTransform = NR::identity();
231                         pat->patternTransform_set = FALSE;
232                 }
233                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
234                 break;
235         }
236         case SP_ATTR_X:
237                 pat->x.readOrUnset(value);
238                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
239                 break;
240         case SP_ATTR_Y:
241                 pat->y.readOrUnset(value);
242                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
243                 break;
244         case SP_ATTR_WIDTH:
245                 pat->width.readOrUnset(value);
246                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
247                 break;
248         case SP_ATTR_HEIGHT:
249                 pat->height.readOrUnset(value);
250                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
251                 break;
252         case SP_ATTR_VIEWBOX: {
253                 /* fixme: Think (Lauris) */
254                 double x, y, width, height;
255                 char *eptr;
257                 if (value) {
258                         eptr = (gchar *) value;
259                         x = g_ascii_strtod (eptr, &eptr);
260                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
261                         y = g_ascii_strtod (eptr, &eptr);
262                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
263                         width = g_ascii_strtod (eptr, &eptr);
264                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
265                         height = g_ascii_strtod (eptr, &eptr);
266                         while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
267                         if ((width > 0) && (height > 0)) {
268                                 pat->viewBox.x0 = x;
269                                 pat->viewBox.y0 = y;
270                                 pat->viewBox.x1 = x + width;
271                                 pat->viewBox.y1 = y + height;
272                                 pat->viewBox_set = TRUE;
273                         } else {
274                                 pat->viewBox_set = FALSE;
275                         }
276                 } else {
277                         pat->viewBox_set = FALSE;
278                 }
279                 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
280                 break;
281         }
282         case SP_ATTR_XLINK_HREF:
283                 if ( value && pat->href && ( strcmp(value, pat->href) == 0 ) ) {
284                         /* Href unchanged, do nothing. */
285                 } else {
286                         g_free(pat->href);
287                         pat->href = NULL;
288                         if (value) {
289                                 // First, set the href field; it's only used in the "unchanged" check above.
290                                 pat->href = g_strdup(value);
291                                 // Now do the attaching, which emits the changed signal.
292                                 if (value) {
293                                         try {
294                                                 pat->ref->attach(Inkscape::URI(value));
295                                         } catch (Inkscape::BadURIException &e) {
296                                                 g_warning("%s", e.what());
297                                                 pat->ref->detach();
298                                         }
299                                 } else {
300                                         pat->ref->detach();
301                                 }
302                         }
303                 }
304                 break;
305         default:
306                 if (((SPObjectClass *) pattern_parent_class)->set)
307                         ((SPObjectClass *) pattern_parent_class)->set (object, key, value);
308                 break;
309         }
312 static void
313 sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
315         SPPattern *pat = SP_PATTERN (object);
317         if (((SPObjectClass *) (pattern_parent_class))->child_added)
318                 (* ((SPObjectClass *) (pattern_parent_class))->child_added) (object, child, ref);
320         SPObject *ochild = sp_object_get_child_by_repr(object, child);
321         if (SP_IS_ITEM (ochild)) {
323                 SPPaintServer *ps = SP_PAINT_SERVER (pat);
324                 unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
326                 for (SPPainter *p = ps->painters; p != NULL; p = p->next) {
328                         SPPatPainter *pp = (SPPatPainter *) p;
329                         NRArenaItem *ai = sp_item_invoke_show (SP_ITEM (ochild), pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
331                         if (ai) {
332                                 nr_arena_item_add_child (pp->root, ai, NULL);
333                                 nr_arena_item_set_order (ai, position);
334                                 nr_arena_item_unref (ai);
335                         }
336                 }
337         }
340 /* TODO: do we need a ::remove_child handler? */
342 /* fixme: We need ::order_changed handler too (Lauris) */
344 GSList *
345 pattern_getchildren (SPPattern *pat)
347         GSList *l = NULL;
349         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
350                 if (sp_object_first_child(SP_OBJECT(pat_i))) { // find the first one with children
351                         for (SPObject *child = sp_object_first_child(SP_OBJECT (pat)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
352                                 l = g_slist_prepend (l, child);
353                         }
354                         break; // do not go further up the chain if children are found
355                 }
356         }
358         return l;
361 static void
362 sp_pattern_update (SPObject *object, SPCtx *ctx, unsigned int flags)
364         SPPattern *pat = SP_PATTERN (object);
366         if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
367         flags &= SP_OBJECT_MODIFIED_CASCADE;
369         GSList *l = pattern_getchildren (pat);
370         l = g_slist_reverse (l);
372         while (l) {
373                 SPObject *child = SP_OBJECT (l->data);
374                 sp_object_ref (child, NULL);
375                 l = g_slist_remove (l, child);
376                 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
377                         child->updateDisplay(ctx, flags);
378                 }
379                 sp_object_unref (child, NULL);
380         }
383 static void
384 sp_pattern_modified (SPObject *object, guint flags)
386         SPPattern *pat = SP_PATTERN (object);
388         if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
389         flags &= SP_OBJECT_MODIFIED_CASCADE;
391         GSList *l = pattern_getchildren (pat);
392         l = g_slist_reverse (l);
394         while (l) {
395                 SPObject *child = SP_OBJECT (l->data);
396                 sp_object_ref (child, NULL);
397                 l = g_slist_remove (l, child);
398                 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
399                         child->emitModified(flags);
400                 }
401                 sp_object_unref (child, NULL);
402         }
405 /**
406 Gets called when the pattern is reattached to another <pattern>
407 */
408 static void
409 pattern_ref_changed(SPObject *old_ref, SPObject *ref, SPPattern *pat)
411         if (old_ref) {
412                 pat->modified_connection.disconnect();
413         }
414         if (SP_IS_PATTERN (ref)) {
415                 pat->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&pattern_ref_modified), pat));
416         }
418         pattern_ref_modified (ref, 0, pat);
421 /**
422 Gets called when the referenced <pattern> is changed
423 */
424 static void
425 pattern_ref_modified (SPObject */*ref*/, guint /*flags*/, SPPattern *pattern)
427         if (SP_IS_OBJECT (pattern))
428                 SP_OBJECT (pattern)->requestModified(SP_OBJECT_MODIFIED_FLAG);
431 guint
432 pattern_users (SPPattern *pattern)
434         return SP_OBJECT (pattern)->hrefcount;
437 SPPattern *
438 pattern_chain (SPPattern *pattern)
440         SPDocument *document = SP_OBJECT_DOCUMENT (pattern);
441         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
442         Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
444         Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
445         repr->setAttribute("inkscape:collect", "always");
446         gchar *parent_ref = g_strconcat ("#", SP_OBJECT_REPR(pattern)->attribute("id"), NULL);
447         repr->setAttribute("xlink:href",  parent_ref);
448         g_free (parent_ref);
450         defsrepr->addChild(repr, NULL);
451         const gchar *child_id = repr->attribute("id");
452         SPObject *child = document->getObjectById(child_id);
453         g_assert (SP_IS_PATTERN (child));
455         return SP_PATTERN (child);
458 SPPattern *
459 sp_pattern_clone_if_necessary (SPItem *item, SPPattern *pattern, const gchar *property)
461         if (pattern_users(pattern) > 1) {
462                 pattern = pattern_chain (pattern);
463                 gchar *href = g_strconcat ("url(#", SP_OBJECT_REPR (pattern)->attribute("id"), ")", NULL);
465                 SPCSSAttr *css = sp_repr_css_attr_new ();
466                 sp_repr_css_set_property (css, property, href);
467                 sp_repr_css_change_recursive (SP_OBJECT_REPR (item), css, "style");
468         }
469         return pattern;
472 void
473 sp_pattern_transform_multiply (SPPattern *pattern, NR::Matrix postmul, bool set)
475         // this formula is for a different interpretation of pattern transforms as described in (*) in sp-pattern.cpp
476         // for it to work, we also need    sp_object_read_attr (SP_OBJECT (item), "transform");
477         //pattern->patternTransform = premul * item->transform * pattern->patternTransform * item->transform.inverse() * postmul;
479         // otherwise the formula is much simpler
480         if (set) {
481                 pattern->patternTransform = postmul;
482         } else {
483                 pattern->patternTransform = pattern_patternTransform(pattern) * postmul;
484         }
485         pattern->patternTransform_set = TRUE;
487         gchar *c=sp_svg_transform_write(pattern->patternTransform);
488         SP_OBJECT_REPR(pattern)->setAttribute("patternTransform", c);
489         g_free(c);
492 const gchar *
493 pattern_tile (GSList *reprs, NR::Rect bounds, SPDocument *document, NR::Matrix transform, NR::Matrix move)
495         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
496         Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
498         Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
499         repr->setAttribute("patternUnits", "userSpaceOnUse");
500         sp_repr_set_svg_double(repr, "width", bounds.extent(NR::X));
501         sp_repr_set_svg_double(repr, "height", bounds.extent(NR::Y));
503         gchar *t=sp_svg_transform_write(transform);
504         repr->setAttribute("patternTransform", t);
505         g_free(t);
507         defsrepr->appendChild(repr);
508         const gchar *pat_id = repr->attribute("id");
509         SPObject *pat_object = document->getObjectById(pat_id);
511         for (GSList *i = reprs; i != NULL; i = i->next) {
512                 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data);
513                 SPItem *copy = SP_ITEM(pat_object->appendChildRepr(node));
515                 NR::Matrix dup_transform;
516                 if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform))
517                         dup_transform = NR::identity();
518                 dup_transform *= move;
520                 sp_item_write_transform(copy, SP_OBJECT_REPR(copy), dup_transform);
521         }
523         Inkscape::GC::release(repr);
524         return pat_id;
527 SPPattern *
528 pattern_getroot (SPPattern *pat)
530         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
531                 if (sp_object_first_child(SP_OBJECT(pat_i))) { // find the first one with children
532                         return pat_i;
533                 }
534         }
535         return pat; // document is broken, we can't get to root; but at least we can return pat which is supposedly a valid pattern
540 // Access functions that look up fields up the chain of referenced patterns and return the first one which is set
541 // FIXME: all of them must use chase_hrefs the same as in SPGradient, to avoid lockup on circular refs
543 guint pattern_patternUnits (SPPattern *pat)
545         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
546                 if (pat_i->patternUnits_set)
547                         return pat_i->patternUnits;
548         }
549         return pat->patternUnits;
552 guint pattern_patternContentUnits (SPPattern *pat)
554         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
555                 if (pat_i->patternContentUnits_set)
556                         return pat_i->patternContentUnits;
557         }
558         return pat->patternContentUnits;
561 NR::Matrix const &pattern_patternTransform(SPPattern const *pat)
563         for (SPPattern const *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
564                 if (pat_i->patternTransform_set)
565                         return pat_i->patternTransform;
566         }
567         return pat->patternTransform;
570 gdouble pattern_x (SPPattern *pat)
572         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
573                 if (pat_i->x._set)
574                         return pat_i->x.computed;
575         }
576         return 0;
579 gdouble pattern_y (SPPattern *pat)
581         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
582                 if (pat_i->y._set)
583                         return pat_i->y.computed;
584         }
585         return 0;
588 gdouble pattern_width (SPPattern *pat)
590         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
591                 if (pat_i->width._set)
592                         return pat_i->width.computed;
593         }
594         return 0;
597 gdouble pattern_height (SPPattern *pat)
599         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
600                 if (pat_i->height._set)
601                         return pat_i->height.computed;
602         }
603         return 0;
606 NRRect *pattern_viewBox (SPPattern *pat)
608         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
609                 if (pat_i->viewBox_set)
610                         return &(pat_i->viewBox);
611         }
612         return &(pat->viewBox);
615 bool pattern_hasItemChildren (SPPattern *pat)
617         for (SPObject *child = sp_object_first_child(SP_OBJECT(pat)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
618                 if (SP_IS_ITEM (child)) {
619                         return true;
620                 }
621         }
622         return false;
627 /* Painter */
629 static void sp_pat_fill (SPPainter *painter, NRPixBlock *pb);
631 /**
632 Creates a painter (i.e. the thing that does actual filling at the given zoom).
633 See (*) below for why the parent_transform may be necessary.
634 */
635 static SPPainter *
636 sp_pattern_painter_new (SPPaintServer *ps, NR::Matrix const &full_transform, NR::Matrix const &/*parent_transform*/, const NRRect *bbox)
638         SPPattern *pat = SP_PATTERN (ps);
639         SPPatPainter *pp = g_new (SPPatPainter, 1);
641         pp->painter.type = SP_PAINTER_IND;
642         pp->painter.fill = sp_pat_fill;
644         pp->pat = pat;
646         if (pattern_patternUnits (pat) == SP_PATTERN_UNITS_OBJECTBOUNDINGBOX) {
647                 /* BBox to user coordinate system */
648                 NR::Matrix bbox2user (bbox->x1 - bbox->x0, 0.0, 0.0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
650                 // the final patternTransform, taking into account bbox
651                 NR::Matrix const ps2user(pattern_patternTransform(pat) * bbox2user);
653                 // see (*) comment below
654                 NR::Matrix ps2px = ps2user * full_transform;
656                 ps2px.copyto (&pp->ps2px);
658         } else {
659                 /* Problem: What to do, if we have mixed lengths and percentages? */
660                 /* Currently we do ignore percentages at all, but that is not good (lauris) */
662                 /* fixme: We may try to normalize here too, look at linearGradient (Lauris) */
664                 // (*) The spec says, "This additional transformation matrix [patternTransform] is
665                 // post-multiplied to (i.e., inserted to the right of) any previously defined
666                 // transformations, including the implicit transformation necessary to convert from
667                 // object bounding box units to user space." To me, this means that the order should be:
668                 // item_transform * patternTransform * parent_transform
669                 // However both Batik and Adobe plugin use:
670                 // patternTransform * item_transform * parent_transform
671                 // So here I comply with the majority opinion, but leave my interpretation commented out below.
672                 // (To get item_transform, I subtract parent from full.)
674                 //NR::Matrix ps2px = (full_transform / parent_transform) * pattern_patternTransform(pat) * parent_transform;
675                 NR::Matrix ps2px = pattern_patternTransform(pat) * full_transform;
677                 ps2px.copyto (&pp->ps2px);
678         }
680         nr_matrix_invert (&pp->px2ps, &pp->ps2px);
682         if (pat->viewBox_set) {
683                 gdouble tmp_x = (pattern_viewBox(pat)->x1 - pattern_viewBox(pat)->x0) / pattern_width (pat);
684                 gdouble tmp_y = (pattern_viewBox(pat)->y1 - pattern_viewBox(pat)->y0) / pattern_height (pat);
686                 // FIXME: preserveAspectRatio must be taken into account here too!
687                 NR::Matrix vb2ps (tmp_x, 0.0, 0.0, tmp_y, pattern_x(pat) - pattern_viewBox(pat)->x0, pattern_y(pat) - pattern_viewBox(pat)->y0);
689                 NR::Matrix vb2us = vb2ps * pattern_patternTransform(pat);
691                 // see (*)
692                 NR::Matrix pcs2px = vb2us * full_transform;
694                 pcs2px.copyto (&pp->pcs2px);
695         } else {
696                 NR::Matrix pcs2px;
698                 /* No viewbox, have to parse units */
699                 if (pattern_patternContentUnits (pat) == SP_PATTERN_UNITS_OBJECTBOUNDINGBOX) {
700                         /* BBox to user coordinate system */
701                         NR::Matrix bbox2user (bbox->x1 - bbox->x0, 0.0, 0.0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
703                         NR::Matrix pcs2user = pattern_patternTransform(pat) * bbox2user;
705                         // see (*)
706                         pcs2px = pcs2user * full_transform;
707                 } else {
708                         // see (*)
709                         //pcs2px = (full_transform / parent_transform) * pattern_patternTransform(pat) * parent_transform;
710                         pcs2px = pattern_patternTransform(pat) * full_transform;
711                 }
713                 pcs2px = NR::translate (pattern_x (pat), pattern_y (pat)) * pcs2px;
715                 pcs2px.copyto (&pp->pcs2px);
716         }
718         /* Create arena */
719         pp->arena = NRArena::create();
721         pp->dkey = sp_item_display_key_new (1);
723         /* Create group */
724         pp->root = NRArenaGroup::create(pp->arena);
726         /* Show items */
727         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
728                 if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
729                         for (SPObject *child = sp_object_first_child(SP_OBJECT(pat_i)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
730                                 if (SP_IS_ITEM (child)) {
731                                         NRArenaItem *cai;
732                                         cai = sp_item_invoke_show (SP_ITEM (child), pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
733                                         nr_arena_item_append_child (pp->root, cai);
734                                         nr_arena_item_unref (cai);
735                                 }
736                         }
737                         break; // do not go further up the chain if children are found
738                 }
739         }
741         {
742                 NRRect    one_tile,tr_tile;
743                 one_tile.x0=pattern_x(pp->pat);
744                 one_tile.y0=pattern_y(pp->pat);
745                 one_tile.x1=one_tile.x0+pattern_width (pp->pat);
746                 one_tile.y1=one_tile.y0+pattern_height (pp->pat);
747                 nr_rect_d_matrix_transform (&tr_tile, &one_tile, &pp->ps2px);
748                 int       tr_width=(int)ceil(1.3*(tr_tile.x1-tr_tile.x0));
749                 int       tr_height=(int)ceil(1.3*(tr_tile.y1-tr_tile.y0));
750 //              if ( tr_width < 10000 && tr_height < 10000 && tr_width*tr_height < 1000000 ) {
751                 pp->use_cached_tile=false;//true;
752                         if ( tr_width > 1000 ) tr_width=1000;
753                         if ( tr_height > 1000 ) tr_height=1000;
754                         pp->cached_bbox.x0=0;
755                         pp->cached_bbox.y0=0;
756                         pp->cached_bbox.x1=tr_width;
757                         pp->cached_bbox.y1=tr_height;
759                         if (pp->use_cached_tile) {
760                                 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);
761                         }
763                         pp->pa2ca.c[0]=((double)tr_width)/(one_tile.x1-one_tile.x0);
764                         pp->pa2ca.c[1]=0;
765                         pp->pa2ca.c[2]=0;
766                         pp->pa2ca.c[3]=((double)tr_height)/(one_tile.y1-one_tile.y0);
767                         pp->pa2ca.c[4]=-one_tile.x0*pp->pa2ca.c[0];
768                         pp->pa2ca.c[5]=-one_tile.y0*pp->pa2ca.c[1];
769                         pp->ca2pa.c[0]=(one_tile.x1-one_tile.x0)/((double)tr_width);
770                         pp->ca2pa.c[1]=0;
771                         pp->ca2pa.c[2]=0;
772                         pp->ca2pa.c[3]=(one_tile.y1-one_tile.y0)/((double)tr_height);
773                         pp->ca2pa.c[4]=one_tile.x0;
774                         pp->ca2pa.c[5]=one_tile.y0;
775 //              } else {
776 //                      pp->use_cached_tile=false;
777 //              }
778         }
779         
780         NRGC gc(NULL);
781         if ( pp->use_cached_tile ) {
782                 gc.transform=pp->pa2ca;
783         } else {
784                 gc.transform = pp->pcs2px;
785         }
786         nr_arena_item_invoke_update (pp->root, NULL, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_ALL);
787         if ( pp->use_cached_tile ) {
788                 nr_arena_item_invoke_render (NULL, pp->root, &pp->cached_bbox, &pp->cached_tile, 0);
789         } else {
790                 // nothing to do now
791         }
792         
793         return (SPPainter *) pp;
796 static void
797 sp_pattern_painter_free (SPPaintServer */*ps*/, SPPainter *painter)
799         SPPatPainter *pp = (SPPatPainter *) painter;
800         SPPattern *pat = pp->pat;
802         for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
803                 if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
804                         for (SPObject *child = sp_object_first_child(SP_OBJECT(pat_i)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
805                                 if (SP_IS_ITEM (child)) {
806                                                 sp_item_invoke_hide (SP_ITEM (child), pp->dkey);
807                                 }
808                         }
809                         break; // do not go further up the chain if children are found
810                 }
811         }
812         if ( pp->use_cached_tile ) nr_pixblock_release(&pp->cached_tile);
813         g_free (pp);
816 void
817 get_cached_tile_pixel(SPPatPainter* pp,double x,double y,unsigned char &r,unsigned char &g,unsigned char &b,unsigned char &a)
819         int    ca_h=(int)floor(x);
820         int    ca_v=(int)floor(y);
821         int    r_x=(int)floor(16*(x-floor(x)));
822         int    r_y=(int)floor(16*(y-floor(y)));
823         unsigned int    tl_m=(16-r_x)*(16-r_y);
824         unsigned int    bl_m=(16-r_x)*r_y;
825         unsigned int    tr_m=r_x*(16-r_y);
826         unsigned int    br_m=r_x*r_y;
827         int    cb_h=ca_h+1;
828         int    cb_v=ca_v+1;
829         if ( cb_h >= pp->cached_bbox.x1 ) cb_h=0;
830         if ( cb_v >= pp->cached_bbox.y1 ) cb_v=0;
831         
832         unsigned char* tlx=NR_PIXBLOCK_PX(&pp->cached_tile)+(ca_v*pp->cached_tile.rs)+4*ca_h;
833         unsigned char* trx=NR_PIXBLOCK_PX(&pp->cached_tile)+(ca_v*pp->cached_tile.rs)+4*cb_h;
834         unsigned char* blx=NR_PIXBLOCK_PX(&pp->cached_tile)+(cb_v*pp->cached_tile.rs)+4*ca_h;
835         unsigned char* brx=NR_PIXBLOCK_PX(&pp->cached_tile)+(cb_v*pp->cached_tile.rs)+4*cb_h;
836         
837         unsigned int tl_c=tlx[0];
838         unsigned int tr_c=trx[0];
839         unsigned int bl_c=blx[0];
840         unsigned int br_c=brx[0];
841         unsigned int f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
842         r=f_c;
843         tl_c=tlx[1];
844         tr_c=trx[1];
845         bl_c=blx[1];
846         br_c=brx[1];
847         f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
848         g=f_c;
849         tl_c=tlx[2];
850         tr_c=trx[2];
851         bl_c=blx[2];
852         br_c=brx[2];
853         f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
854         b=f_c;
855         tl_c=tlx[3];
856         tr_c=trx[3];
857         bl_c=blx[3];
858         br_c=brx[3];
859         f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
860         a=f_c;
863 static void
864 sp_pat_fill (SPPainter *painter, NRPixBlock *pb)
866         SPPatPainter *pp;
867         NRRect ba, psa;
868         NRRectL area;
869         double x, y;
871         pp = (SPPatPainter *) painter;
873         if (pattern_width (pp->pat) < NR_EPSILON) return;
874         if (pattern_height (pp->pat) < NR_EPSILON) return;
876         /* Find buffer area in gradient space */
877         /* fixme: This is suboptimal (Lauris) */
879         if ( pp->use_cached_tile ) {
880                 double   pat_w=pattern_width (pp->pat);
881                 double   pat_h=pattern_height (pp->pat);
882                 if ( pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N || pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P ) { // same thing because it's filling an empty pixblock
883                         unsigned char*  lpx=NR_PIXBLOCK_PX(pb);
884                         double          px_y=pb->area.y0;
885                         for (int j=pb->area.y0;j<pb->area.y1;j++) {
886                                 unsigned char* cpx=lpx;
887                                 double         px_x = pb->area.x0;
888                                 
889                                 double ps_x=pp->px2ps.c[0]*px_x+pp->px2ps.c[2]*px_y+pp->px2ps.c[4];
890                                 double ps_y=pp->px2ps.c[1]*px_x+pp->px2ps.c[3]*px_y+pp->px2ps.c[5];
891                                 for (int i=pb->area.x0;i<pb->area.x1;i++) {
892                                         while ( ps_x > pat_w ) ps_x-=pat_w;
893                                         while ( ps_x < 0 ) ps_x+=pat_w;
894                                         while ( ps_y > pat_h ) ps_y-=pat_h;
895                                         while ( ps_y < 0 ) ps_y+=pat_h;
896                                         double ca_x=pp->pa2ca.c[0]*ps_x+pp->pa2ca.c[2]*ps_y+pp->pa2ca.c[4];
897                                         double ca_y=pp->pa2ca.c[1]*ps_x+pp->pa2ca.c[3]*ps_y+pp->pa2ca.c[5];
898                                         unsigned char n_a,n_r,n_g,n_b;
899                                         get_cached_tile_pixel(pp,ca_x,ca_y,n_r,n_g,n_b,n_a);
900                                         cpx[0]=n_r;
901                                         cpx[1]=n_g;
902                                         cpx[2]=n_b;
903                                         cpx[3]=n_a;
904                                         
905                                         px_x+=1.0;
906                                         ps_x+=pp->px2ps.c[0];
907                                         ps_y+=pp->px2ps.c[1];
908                                         cpx+=4;
909                                 }
910                                 px_y+=1.0;
911                                 lpx+=pb->rs;
912                         }
913                 } else if ( pb->mode == NR_PIXBLOCK_MODE_R8G8B8 ) {
914                         unsigned char*  lpx=NR_PIXBLOCK_PX(pb);
915                         double          px_y=pb->area.y0;
916                         for (int j=pb->area.y0;j<pb->area.y1;j++) {
917                                 unsigned char* cpx=lpx;
918                                 double         px_x = pb->area.x0;
919                                 
920                                 double ps_x=pp->px2ps.c[0]*px_x+pp->px2ps.c[2]*px_y+pp->px2ps.c[4];
921                                 double ps_y=pp->px2ps.c[1]*px_x+pp->px2ps.c[3]*px_y+pp->px2ps.c[5];
922                                 for (int i=pb->area.x0;i<pb->area.x1;i++) {
923                                         while ( ps_x > pat_w ) ps_x-=pat_w;
924                                         while ( ps_x < 0 ) ps_x+=pat_w;
925                                         while ( ps_y > pat_h ) ps_y-=pat_h;
926                                         while ( ps_y < 0 ) ps_y+=pat_h;
927                                         double ca_x=pp->pa2ca.c[0]*ps_x+pp->pa2ca.c[2]*ps_y+pp->pa2ca.c[4];
928                                         double ca_y=pp->pa2ca.c[1]*ps_x+pp->pa2ca.c[3]*ps_y+pp->pa2ca.c[5];
929                                         unsigned char n_a,n_r,n_g,n_b;
930                                         get_cached_tile_pixel(pp,ca_x,ca_y,n_r,n_g,n_b,n_a);
931                                         cpx[0]=n_r;
932                                         cpx[1]=n_g;
933                                         cpx[2]=n_b;
934                                         
935                                         px_x+=1.0;
936                                         ps_x+=pp->px2ps.c[0];
937                                         ps_y+=pp->px2ps.c[1];
938                                         cpx+=4;
939                                 }
940                                 px_y+=1.0;
941                                 lpx+=pb->rs;
942                         }
943                 }
944         } else {
945                 ba.x0 = pb->area.x0;
946                 ba.y0 = pb->area.y0;
947                 ba.x1 = pb->area.x1;
948                 ba.y1 = pb->area.y1;
949                 
950         // Trying to solve this bug: https://bugs.launchpad.net/inkscape/+bug/167416
951         // Bail out if the transformation matrix has extreme values. If we bail out
952         // however, then something (which was meaningless anyway) won't be rendered, 
953         // which is better than getting stuck in a virtually infinite loop
954         if (fabs(pp->px2ps.c[0]) < 1e6 && 
955             fabs(pp->px2ps.c[3]) < 1e6 &&
956             fabs(pp->px2ps.c[4]) < 1e6 &&
957             fabs(pp->px2ps.c[5]) < 1e6) 
958         {
959             nr_rect_d_matrix_transform (&psa, &ba, &pp->px2ps);
960                 
961                 psa.x0 = floor ((psa.x0 - pattern_x (pp->pat)) / pattern_width (pp->pat)) -1;
962                 psa.y0 = floor ((psa.y0 - pattern_y (pp->pat)) / pattern_height (pp->pat)) -1;
963                 psa.x1 = ceil ((psa.x1 - pattern_x (pp->pat)) / pattern_width (pp->pat)) +1;
964                 psa.y1 = ceil ((psa.y1 - pattern_y (pp->pat)) / pattern_height (pp->pat)) +1;
965                 
966             // If psa is too wide or tall, then something must be wrong! This is due to
967             // nr_rect_d_matrix_transform (&psa, &ba, &pp->px2ps) using a weird transformation matrix pp->px2ps.
968             g_assert(std::abs(psa.x1 - psa.x0) < 1e6);
969             g_assert(std::abs(psa.y1 - psa.y0) < 1e6);
970             
971             for (y = psa.y0; y < psa.y1; y++) {
972                         for (x = psa.x0; x < psa.x1; x++) {
973                                 NRPixBlock ppb;
974                                 double psx, psy;
975                                 
976                                 psx = x * pattern_width (pp->pat);
977                                 psy = y * pattern_height (pp->pat);
978                                 
979                                 area.x0 = (gint32)(pb->area.x0 - (pp->ps2px.c[0] * psx + pp->ps2px.c[2] * psy));
980                                 area.y0 = (gint32)(pb->area.y0 - (pp->ps2px.c[1] * psx + pp->ps2px.c[3] * psy));
981                                 area.x1 = area.x0 + pb->area.x1 - pb->area.x0;
982                                 area.y1 = area.y0 + pb->area.y1 - pb->area.y0;
983                                 
984                                 // We do not update here anymore
985     
986                                 // Set up buffer
987                                 // fixme: (Lauris)
988                                 nr_pixblock_setup_extern (&ppb, pb->mode, area.x0, area.y0, area.x1, area.y1, NR_PIXBLOCK_PX (pb), pb->rs, FALSE, FALSE);
989                                 
990                                 nr_arena_item_invoke_render (NULL, pp->root, &area, &ppb, 0);
991                                 
992                                 nr_pixblock_release (&ppb);
993                         }
994                 }
995         } 
996         }