fbba4cc48caaa56c21939104603deaffebd44288
1 /*
2 * SVG <pattern> implementation
3 *
4 * Author:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak <buliabyak@users.sf.net>
7 * Jon A. Cruz <jon@joncruz.org>
8 * Abhishek Sharma
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;
55 bool use_cached_tile;
56 Geom::Matrix ca2pa;
57 Geom::Matrix pa2ca;
58 NRRectL cached_bbox;
59 NRPixBlock cached_tile;
61 std::map<SPObject *, sigc::connection> *_release_connections;
62 };
64 static void sp_pattern_class_init (SPPatternClass *klass);
65 static void sp_pattern_init (SPPattern *gr);
67 static void sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
68 static void sp_pattern_release (SPObject *object);
69 static void sp_pattern_set (SPObject *object, unsigned int key, const gchar *value);
70 static void sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
71 static void sp_pattern_update (SPObject *object, SPCtx *ctx, unsigned int flags);
72 static void sp_pattern_modified (SPObject *object, unsigned int flags);
74 static void pattern_ref_changed(SPObject *old_ref, SPObject *ref, SPPattern *pat);
75 static void pattern_ref_modified (SPObject *ref, guint flags, SPPattern *pattern);
77 static SPPainter *sp_pattern_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, Geom::Matrix const &parent_transform, const NRRect *bbox);
78 static void sp_pattern_painter_free (SPPaintServer *ps, SPPainter *painter);
80 static SPPaintServerClass * pattern_parent_class;
82 GType
83 sp_pattern_get_type (void)
84 {
85 static GType pattern_type = 0;
86 if (!pattern_type) {
87 GTypeInfo pattern_info = {
88 sizeof (SPPatternClass),
89 NULL, /* base_init */
90 NULL, /* base_finalize */
91 (GClassInitFunc) sp_pattern_class_init,
92 NULL, /* class_finalize */
93 NULL, /* class_data */
94 sizeof (SPPattern),
95 16, /* n_preallocs */
96 (GInstanceInitFunc) sp_pattern_init,
97 NULL, /* value_table */
98 };
99 pattern_type = g_type_register_static (SP_TYPE_PAINT_SERVER, "SPPattern", &pattern_info, (GTypeFlags)0);
100 }
101 return pattern_type;
102 }
104 static void
105 sp_pattern_class_init (SPPatternClass *klass)
106 {
107 SPObjectClass *sp_object_class;
108 SPPaintServerClass *ps_class;
110 sp_object_class = (SPObjectClass *) klass;
111 ps_class = (SPPaintServerClass *) klass;
113 pattern_parent_class = (SPPaintServerClass*)g_type_class_ref (SP_TYPE_PAINT_SERVER);
115 sp_object_class->build = sp_pattern_build;
116 sp_object_class->release = sp_pattern_release;
117 sp_object_class->set = sp_pattern_set;
118 sp_object_class->child_added = sp_pattern_child_added;
119 sp_object_class->update = sp_pattern_update;
120 sp_object_class->modified = sp_pattern_modified;
122 // do we need _write? seems to work without it
124 ps_class->painter_new = sp_pattern_painter_new;
125 ps_class->painter_free = sp_pattern_painter_free;
126 }
128 static void
129 sp_pattern_init (SPPattern *pat)
130 {
131 pat->ref = new SPPatternReference(SP_OBJECT(pat));
132 pat->ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(pattern_ref_changed), pat));
134 pat->patternUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
135 pat->patternUnits_set = FALSE;
137 pat->patternContentUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
138 pat->patternContentUnits_set = FALSE;
140 pat->patternTransform = NR::identity();
141 pat->patternTransform_set = FALSE;
143 pat->x.unset();
144 pat->y.unset();
145 pat->width.unset();
146 pat->height.unset();
148 pat->viewBox_set = FALSE;
150 new (&pat->modified_connection) sigc::connection();
151 }
153 static void
154 sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
155 {
156 if (((SPObjectClass *) pattern_parent_class)->build)
157 (* ((SPObjectClass *) pattern_parent_class)->build) (object, document, repr);
159 object->readAttr( "patternUnits" );
160 object->readAttr( "patternContentUnits" );
161 object->readAttr( "patternTransform" );
162 object->readAttr( "x" );
163 object->readAttr( "y" );
164 object->readAttr( "width" );
165 object->readAttr( "height" );
166 object->readAttr( "viewBox" );
167 object->readAttr( "xlink:href" );
169 /* Register ourselves */
170 document->addResource("pattern", object);
171 }
173 static void
174 sp_pattern_release (SPObject *object)
175 {
176 SPPattern *pat;
178 pat = (SPPattern *) object;
180 if (SP_OBJECT_DOCUMENT (object)) {
181 /* Unregister ourselves */
182 SP_OBJECT_DOCUMENT (object)->removeResource("pattern", SP_OBJECT (object));
183 }
185 if (pat->ref) {
186 pat->modified_connection.disconnect();
187 pat->ref->detach();
188 delete pat->ref;
189 pat->ref = NULL;
190 }
192 pat->modified_connection.~connection();
194 if (((SPObjectClass *) pattern_parent_class)->release)
195 ((SPObjectClass *) pattern_parent_class)->release (object);
196 }
198 static void
199 sp_pattern_set (SPObject *object, unsigned int key, const gchar *value)
200 {
201 SPPattern *pat = SP_PATTERN (object);
203 switch (key) {
204 case SP_ATTR_PATTERNUNITS:
205 if (value) {
206 if (!strcmp (value, "userSpaceOnUse")) {
207 pat->patternUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
208 } else {
209 pat->patternUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
210 }
211 pat->patternUnits_set = TRUE;
212 } else {
213 pat->patternUnits_set = FALSE;
214 }
215 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
216 break;
217 case SP_ATTR_PATTERNCONTENTUNITS:
218 if (value) {
219 if (!strcmp (value, "userSpaceOnUse")) {
220 pat->patternContentUnits = SP_PATTERN_UNITS_USERSPACEONUSE;
221 } else {
222 pat->patternContentUnits = SP_PATTERN_UNITS_OBJECTBOUNDINGBOX;
223 }
224 pat->patternContentUnits_set = TRUE;
225 } else {
226 pat->patternContentUnits_set = FALSE;
227 }
228 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
229 break;
230 case SP_ATTR_PATTERNTRANSFORM: {
231 Geom::Matrix t;
232 if (value && sp_svg_transform_read (value, &t)) {
233 pat->patternTransform = t;
234 pat->patternTransform_set = TRUE;
235 } else {
236 pat->patternTransform = NR::identity();
237 pat->patternTransform_set = FALSE;
238 }
239 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
240 break;
241 }
242 case SP_ATTR_X:
243 pat->x.readOrUnset(value);
244 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
245 break;
246 case SP_ATTR_Y:
247 pat->y.readOrUnset(value);
248 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
249 break;
250 case SP_ATTR_WIDTH:
251 pat->width.readOrUnset(value);
252 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
253 break;
254 case SP_ATTR_HEIGHT:
255 pat->height.readOrUnset(value);
256 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
257 break;
258 case SP_ATTR_VIEWBOX: {
259 /* fixme: Think (Lauris) */
260 double x, y, width, height;
261 char *eptr;
263 if (value) {
264 eptr = (gchar *) value;
265 x = g_ascii_strtod (eptr, &eptr);
266 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
267 y = g_ascii_strtod (eptr, &eptr);
268 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
269 width = g_ascii_strtod (eptr, &eptr);
270 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
271 height = g_ascii_strtod (eptr, &eptr);
272 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
273 if ((width > 0) && (height > 0)) {
274 pat->viewBox.x0 = x;
275 pat->viewBox.y0 = y;
276 pat->viewBox.x1 = x + width;
277 pat->viewBox.y1 = y + height;
278 pat->viewBox_set = TRUE;
279 } else {
280 pat->viewBox_set = FALSE;
281 }
282 } else {
283 pat->viewBox_set = FALSE;
284 }
285 object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
286 break;
287 }
288 case SP_ATTR_XLINK_HREF:
289 if ( value && pat->href && ( strcmp(value, pat->href) == 0 ) ) {
290 /* Href unchanged, do nothing. */
291 } else {
292 g_free(pat->href);
293 pat->href = NULL;
294 if (value) {
295 // First, set the href field; it's only used in the "unchanged" check above.
296 pat->href = g_strdup(value);
297 // Now do the attaching, which emits the changed signal.
298 if (value) {
299 try {
300 pat->ref->attach(Inkscape::URI(value));
301 } catch (Inkscape::BadURIException &e) {
302 g_warning("%s", e.what());
303 pat->ref->detach();
304 }
305 } else {
306 pat->ref->detach();
307 }
308 }
309 }
310 break;
311 default:
312 if (((SPObjectClass *) pattern_parent_class)->set)
313 ((SPObjectClass *) pattern_parent_class)->set (object, key, value);
314 break;
315 }
316 }
318 static void
319 sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
320 {
321 SPPattern *pat = SP_PATTERN (object);
323 if (((SPObjectClass *) (pattern_parent_class))->child_added)
324 (* ((SPObjectClass *) (pattern_parent_class))->child_added) (object, child, ref);
326 SPObject *ochild = object->get_child_by_repr(child);
327 if (SP_IS_ITEM (ochild)) {
329 SPPaintServer *ps = SP_PAINT_SERVER (pat);
330 unsigned position = SP_ITEM(ochild)->pos_in_parent();
332 for (SPPainter *p = ps->painters; p != NULL; p = p->next) {
334 SPPatPainter *pp = (SPPatPainter *) p;
335 NRArenaItem *ai = SP_ITEM (ochild)->invoke_show (pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
337 if (ai) {
338 nr_arena_item_add_child (pp->root, ai, NULL);
339 nr_arena_item_set_order (ai, position);
340 nr_arena_item_unref (ai);
341 }
342 }
343 }
344 }
346 /* TODO: do we need a ::remove_child handler? */
348 /* fixme: We need ::order_changed handler too (Lauris) */
350 GSList *pattern_getchildren(SPPattern *pat)
351 {
352 GSList *l = NULL;
354 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
355 if (pat_i->firstChild()) { // find the first one with children
356 for (SPObject *child = pat->firstChild() ; child ; child = child->getNext() ) {
357 l = g_slist_prepend (l, child);
358 }
359 break; // do not go further up the chain if children are found
360 }
361 }
363 return l;
364 }
366 static void
367 sp_pattern_update (SPObject *object, SPCtx *ctx, unsigned int flags)
368 {
369 SPPattern *pat = SP_PATTERN (object);
371 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
372 flags &= SP_OBJECT_MODIFIED_CASCADE;
374 GSList *l = pattern_getchildren (pat);
375 l = g_slist_reverse (l);
377 while (l) {
378 SPObject *child = SP_OBJECT (l->data);
379 sp_object_ref (child, NULL);
380 l = g_slist_remove (l, child);
381 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
382 child->updateDisplay(ctx, flags);
383 }
384 sp_object_unref (child, NULL);
385 }
386 }
388 static void
389 sp_pattern_modified (SPObject *object, guint flags)
390 {
391 SPPattern *pat = SP_PATTERN (object);
393 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
394 flags &= SP_OBJECT_MODIFIED_CASCADE;
396 GSList *l = pattern_getchildren (pat);
397 l = g_slist_reverse (l);
399 while (l) {
400 SPObject *child = SP_OBJECT (l->data);
401 sp_object_ref (child, NULL);
402 l = g_slist_remove (l, child);
403 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
404 child->emitModified(flags);
405 }
406 sp_object_unref (child, NULL);
407 }
408 }
410 /**
411 Gets called when the pattern is reattached to another <pattern>
412 */
413 static void
414 pattern_ref_changed(SPObject *old_ref, SPObject *ref, SPPattern *pat)
415 {
416 if (old_ref) {
417 pat->modified_connection.disconnect();
418 }
419 if (SP_IS_PATTERN (ref)) {
420 pat->modified_connection = ref->connectModified(sigc::bind<2>(sigc::ptr_fun(&pattern_ref_modified), pat));
421 }
423 pattern_ref_modified (ref, 0, pat);
424 }
426 /**
427 Gets called when the referenced <pattern> is changed
428 */
429 static void
430 pattern_ref_modified (SPObject */*ref*/, guint /*flags*/, SPPattern *pattern)
431 {
432 if (SP_IS_OBJECT (pattern))
433 SP_OBJECT (pattern)->requestModified(SP_OBJECT_MODIFIED_FLAG);
434 /* Conditional to avoid causing infinite loop if there's a cycle in the href chain. */
435 }
438 /**
439 Count how many times pat is used by the styles of o and its descendants
440 */
441 guint
442 count_pattern_hrefs(SPObject *o, SPPattern *pat)
443 {
444 if (!o)
445 return 1;
447 guint i = 0;
449 SPStyle *style = SP_OBJECT_STYLE(o);
450 if (style
451 && style->fill.isPaintserver()
452 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(style))
453 && SP_PATTERN(SP_STYLE_FILL_SERVER(style)) == pat)
454 {
455 i ++;
456 }
457 if (style
458 && style->stroke.isPaintserver()
459 && SP_IS_PATTERN(SP_STYLE_STROKE_SERVER(style))
460 && SP_PATTERN(SP_STYLE_STROKE_SERVER(style)) == pat)
461 {
462 i ++;
463 }
465 for ( SPObject *child = o->firstChild(); child != NULL; child = child->next ) {
466 i += count_pattern_hrefs(child, pat);
467 }
469 return i;
470 }
472 SPPattern *pattern_chain(SPPattern *pattern)
473 {
474 SPDocument *document = SP_OBJECT_DOCUMENT (pattern);
475 Inkscape::XML::Document *xml_doc = document->getReprDoc();
476 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
478 Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
479 repr->setAttribute("inkscape:collect", "always");
480 gchar *parent_ref = g_strconcat ("#", SP_OBJECT_REPR(pattern)->attribute("id"), NULL);
481 repr->setAttribute("xlink:href", parent_ref);
482 g_free (parent_ref);
484 defsrepr->addChild(repr, NULL);
485 const gchar *child_id = repr->attribute("id");
486 SPObject *child = document->getObjectById(child_id);
487 g_assert (SP_IS_PATTERN (child));
489 return SP_PATTERN (child);
490 }
492 SPPattern *
493 sp_pattern_clone_if_necessary (SPItem *item, SPPattern *pattern, const gchar *property)
494 {
495 if (!pattern->href || pattern->hrefcount > count_pattern_hrefs(item, pattern)) {
496 pattern = pattern_chain (pattern);
497 gchar *href = g_strconcat ("url(#", SP_OBJECT_REPR (pattern)->attribute("id"), ")", NULL);
499 SPCSSAttr *css = sp_repr_css_attr_new ();
500 sp_repr_css_set_property (css, property, href);
501 sp_repr_css_change_recursive (SP_OBJECT_REPR (item), css, "style");
502 }
503 return pattern;
504 }
506 void
507 sp_pattern_transform_multiply (SPPattern *pattern, Geom::Matrix postmul, bool set)
508 {
509 // this formula is for a different interpretation of pattern transforms as described in (*) in sp-pattern.cpp
510 // for it to work, we also need sp_object_read_attr (SP_OBJECT (item), "transform");
511 //pattern->patternTransform = premul * item->transform * pattern->patternTransform * item->transform.inverse() * postmul;
513 // otherwise the formula is much simpler
514 if (set) {
515 pattern->patternTransform = postmul;
516 } else {
517 pattern->patternTransform = pattern_patternTransform(pattern) * postmul;
518 }
519 pattern->patternTransform_set = TRUE;
521 gchar *c=sp_svg_transform_write(pattern->patternTransform);
522 SP_OBJECT_REPR(pattern)->setAttribute("patternTransform", c);
523 g_free(c);
524 }
526 const gchar *pattern_tile(GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Matrix transform, Geom::Matrix move)
527 {
528 Inkscape::XML::Document *xml_doc = document->getReprDoc();
529 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
531 Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
532 repr->setAttribute("patternUnits", "userSpaceOnUse");
533 sp_repr_set_svg_double(repr, "width", bounds.dimensions()[Geom::X]);
534 sp_repr_set_svg_double(repr, "height", bounds.dimensions()[Geom::Y]);
536 gchar *t=sp_svg_transform_write(transform);
537 repr->setAttribute("patternTransform", t);
538 g_free(t);
540 defsrepr->appendChild(repr);
541 const gchar *pat_id = repr->attribute("id");
542 SPObject *pat_object = document->getObjectById(pat_id);
544 for (GSList *i = reprs; i != NULL; i = i->next) {
545 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data);
546 SPItem *copy = SP_ITEM(pat_object->appendChildRepr(node));
548 Geom::Matrix dup_transform;
549 if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform))
550 dup_transform = Geom::identity();
551 dup_transform *= move;
553 copy->doWriteTransform(SP_OBJECT_REPR(copy), dup_transform, NULL, false);
554 }
556 Inkscape::GC::release(repr);
557 return pat_id;
558 }
560 SPPattern *pattern_getroot(SPPattern *pat)
561 {
562 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
563 if ( pat_i->firstChild() ) { // find the first one with children
564 return pat_i;
565 }
566 }
567 return pat; // document is broken, we can't get to root; but at least we can return pat which is supposedly a valid pattern
568 }
572 // Access functions that look up fields up the chain of referenced patterns and return the first one which is set
573 // FIXME: all of them must use chase_hrefs the same as in SPGradient, to avoid lockup on circular refs
575 guint pattern_patternUnits (SPPattern *pat)
576 {
577 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
578 if (pat_i->patternUnits_set)
579 return pat_i->patternUnits;
580 }
581 return pat->patternUnits;
582 }
584 guint pattern_patternContentUnits (SPPattern *pat)
585 {
586 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
587 if (pat_i->patternContentUnits_set)
588 return pat_i->patternContentUnits;
589 }
590 return pat->patternContentUnits;
591 }
593 Geom::Matrix const &pattern_patternTransform(SPPattern const *pat)
594 {
595 for (SPPattern const *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
596 if (pat_i->patternTransform_set)
597 return pat_i->patternTransform;
598 }
599 return pat->patternTransform;
600 }
602 gdouble pattern_x (SPPattern *pat)
603 {
604 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
605 if (pat_i->x._set)
606 return pat_i->x.computed;
607 }
608 return 0;
609 }
611 gdouble pattern_y (SPPattern *pat)
612 {
613 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
614 if (pat_i->y._set)
615 return pat_i->y.computed;
616 }
617 return 0;
618 }
620 gdouble pattern_width (SPPattern *pat)
621 {
622 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
623 if (pat_i->width._set)
624 return pat_i->width.computed;
625 }
626 return 0;
627 }
629 gdouble pattern_height (SPPattern *pat)
630 {
631 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
632 if (pat_i->height._set)
633 return pat_i->height.computed;
634 }
635 return 0;
636 }
638 NRRect *pattern_viewBox (SPPattern *pat)
639 {
640 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
641 if (pat_i->viewBox_set)
642 return &(pat_i->viewBox);
643 }
644 return &(pat->viewBox);
645 }
647 bool pattern_hasItemChildren (SPPattern *pat)
648 {
649 bool hasChildren = false;
650 for (SPObject *child = pat->firstChild() ; child && !hasChildren ; child = child->getNext() ) {
651 if (SP_IS_ITEM(child)) {
652 hasChildren = true;
653 }
654 }
655 return hasChildren;
656 }
660 /* Painter */
662 static void sp_pat_fill (SPPainter *painter, NRPixBlock *pb);
664 // item in this pattern is about to be deleted, hide it on our arena and disconnect
665 void
666 sp_pattern_painter_release (SPObject *obj, SPPatPainter *painter)
667 {
668 std::map<SPObject *, sigc::connection>::iterator iter = painter->_release_connections->find(obj);
669 if (iter != painter->_release_connections->end()) {
670 iter->second.disconnect();
671 painter->_release_connections->erase(obj);
672 }
674 SP_ITEM(obj)->invoke_hide(painter->dkey);
675 }
677 /**
678 Creates a painter (i.e. the thing that does actual filling at the given zoom).
679 See (*) below for why the parent_transform may be necessary.
680 */
681 static SPPainter *
682 sp_pattern_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, Geom::Matrix const &/*parent_transform*/, const NRRect *bbox)
683 {
684 SPPattern *pat = SP_PATTERN (ps);
685 SPPatPainter *pp = g_new (SPPatPainter, 1);
687 pp->painter.type = SP_PAINTER_IND;
688 pp->painter.fill = sp_pat_fill;
690 pp->pat = pat;
692 if (pattern_patternUnits (pat) == SP_PATTERN_UNITS_OBJECTBOUNDINGBOX) {
693 /* BBox to user coordinate system */
694 Geom::Matrix bbox2user (bbox->x1 - bbox->x0, 0.0, 0.0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
696 // the final patternTransform, taking into account bbox
697 Geom::Matrix const ps2user(pattern_patternTransform(pat) * bbox2user);
699 // see (*) comment below
700 pp->ps2px = ps2user * full_transform;
701 } else {
702 /* Problem: What to do, if we have mixed lengths and percentages? */
703 /* Currently we do ignore percentages at all, but that is not good (lauris) */
705 /* fixme: We may try to normalize here too, look at linearGradient (Lauris) */
707 // (*) The spec says, "This additional transformation matrix [patternTransform] is
708 // post-multiplied to (i.e., inserted to the right of) any previously defined
709 // transformations, including the implicit transformation necessary to convert from
710 // object bounding box units to user space." To me, this means that the order should be:
711 // item_transform * patternTransform * parent_transform
712 // However both Batik and Adobe plugin use:
713 // patternTransform * item_transform * parent_transform
714 // So here I comply with the majority opinion, but leave my interpretation commented out below.
715 // (To get item_transform, I subtract parent from full.)
717 //pp->ps2px = (full_transform / parent_transform) * pattern_patternTransform(pat) * parent_transform;
718 pp->ps2px = pattern_patternTransform(pat) * full_transform;
719 }
721 pp->px2ps = pp->ps2px.inverse();
723 if (pat->viewBox_set) {
724 gdouble tmp_x = pattern_width (pat) / (pattern_viewBox(pat)->x1 - pattern_viewBox(pat)->x0);
725 gdouble tmp_y = pattern_height (pat) / (pattern_viewBox(pat)->y1 - pattern_viewBox(pat)->y0);
727 // FIXME: preserveAspectRatio must be taken into account here too!
728 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);
730 Geom::Matrix vb2us = vb2ps * pattern_patternTransform(pat);
732 // see (*)
733 pp->pcs2px = vb2us * full_transform;
734 } else {
735 /* No viewbox, have to parse units */
736 if (pattern_patternContentUnits (pat) == SP_PATTERN_UNITS_OBJECTBOUNDINGBOX) {
737 /* BBox to user coordinate system */
738 Geom::Matrix bbox2user (bbox->x1 - bbox->x0, 0.0, 0.0, bbox->y1 - bbox->y0, bbox->x0, bbox->y0);
740 Geom::Matrix pcs2user = pattern_patternTransform(pat) * bbox2user;
742 // see (*)
743 pp->pcs2px = pcs2user * full_transform;
744 } else {
745 // see (*)
746 //pcs2px = (full_transform / parent_transform) * pattern_patternTransform(pat) * parent_transform;
747 pp->pcs2px = pattern_patternTransform(pat) * full_transform;
748 }
750 pp->pcs2px = Geom::Translate (pattern_x (pat), pattern_y (pat)) * pp->pcs2px;
751 }
753 /* Create arena */
754 pp->arena = NRArena::create();
756 pp->dkey = SPItem::display_key_new (1);
758 /* Create group */
759 pp->root = NRArenaGroup::create(pp->arena);
761 /* Show items */
762 pp->_release_connections = new std::map<SPObject *, sigc::connection>;
763 for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
764 if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
765 for (SPObject *child = pat_i->firstChild() ; child; child = child->getNext() ) {
766 if (SP_IS_ITEM (child)) {
767 // for each item in pattern,
768 // show it on our arena,
769 NRArenaItem *cai = SP_ITEM(child)->invoke_show(pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
770 // add to the group,
771 nr_arena_item_append_child (pp->root, cai);
772 // and connect to the release signal in case the item gets deleted
773 pp->_release_connections->insert(std::make_pair(child, child->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_pattern_painter_release), pp))));
774 }
775 }
776 break; // do not go further up the chain if children are found
777 }
778 }
780 {
781 NRRect one_tile,tr_tile;
782 one_tile.x0=pattern_x(pp->pat);
783 one_tile.y0=pattern_y(pp->pat);
784 one_tile.x1=one_tile.x0+pattern_width (pp->pat);
785 one_tile.y1=one_tile.y0+pattern_height (pp->pat);
786 // TODO: remove ps2px_nr after converting to 2geom
787 NR::Matrix ps2px_nr = from_2geom(pp->ps2px);
788 nr_rect_d_matrix_transform (&tr_tile, &one_tile, &ps2px_nr);
789 int tr_width=(int)ceil(1.3*(tr_tile.x1-tr_tile.x0));
790 int tr_height=(int)ceil(1.3*(tr_tile.y1-tr_tile.y0));
791 // if ( tr_width < 10000 && tr_height < 10000 && tr_width*tr_height < 1000000 ) {
792 pp->use_cached_tile=false;//true;
793 if ( tr_width > 1000 ) tr_width=1000;
794 if ( tr_height > 1000 ) tr_height=1000;
795 pp->cached_bbox.x0=0;
796 pp->cached_bbox.y0=0;
797 pp->cached_bbox.x1=tr_width;
798 pp->cached_bbox.y1=tr_height;
800 if (pp->use_cached_tile) {
801 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);
802 }
804 pp->pa2ca[0]=((double)tr_width)/(one_tile.x1-one_tile.x0);
805 pp->pa2ca[1]=0;
806 pp->pa2ca[2]=0;
807 pp->pa2ca[3]=((double)tr_height)/(one_tile.y1-one_tile.y0);
808 pp->pa2ca[4]=-one_tile.x0*pp->pa2ca[0];
809 pp->pa2ca[5]=-one_tile.y0*pp->pa2ca[1];
810 pp->ca2pa[0]=(one_tile.x1-one_tile.x0)/((double)tr_width);
811 pp->ca2pa[1]=0;
812 pp->ca2pa[2]=0;
813 pp->ca2pa[3]=(one_tile.y1-one_tile.y0)/((double)tr_height);
814 pp->ca2pa[4]=one_tile.x0;
815 pp->ca2pa[5]=one_tile.y0;
816 // } else {
817 // pp->use_cached_tile=false;
818 // }
819 }
821 NRGC gc(NULL);
822 if ( pp->use_cached_tile ) {
823 gc.transform=pp->pa2ca;
824 } else {
825 gc.transform = pp->pcs2px;
826 }
827 nr_arena_item_invoke_update (pp->root, NULL, &gc, NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_ALL);
828 if ( pp->use_cached_tile ) {
829 nr_arena_item_invoke_render (NULL, pp->root, &pp->cached_bbox, &pp->cached_tile, 0);
830 } else {
831 // nothing to do now
832 }
834 return (SPPainter *) pp;
835 }
838 static void
839 sp_pattern_painter_free (SPPaintServer */*ps*/, SPPainter *painter)
840 {
841 SPPatPainter *pp = (SPPatPainter *) painter;
842 // free our arena
843 if (pp->arena) {
844 ((NRObject *) pp->arena)->unreference();
845 pp->arena = NULL;
846 }
848 // disconnect all connections
849 std::map<SPObject *, sigc::connection>::iterator iter;
850 for (iter = pp->_release_connections->begin() ; iter!=pp->_release_connections->end() ; iter++) {
851 iter->second.disconnect();
852 }
853 pp->_release_connections->clear();
854 delete pp->_release_connections;
856 if ( pp->use_cached_tile ) nr_pixblock_release(&pp->cached_tile);
857 g_free (pp);
858 }
860 void
861 get_cached_tile_pixel(SPPatPainter* pp,double x,double y,unsigned char &r,unsigned char &g,unsigned char &b,unsigned char &a)
862 {
863 int ca_h=(int)floor(x);
864 int ca_v=(int)floor(y);
865 int r_x=(int)floor(16*(x-floor(x)));
866 int r_y=(int)floor(16*(y-floor(y)));
867 unsigned int tl_m=(16-r_x)*(16-r_y);
868 unsigned int bl_m=(16-r_x)*r_y;
869 unsigned int tr_m=r_x*(16-r_y);
870 unsigned int br_m=r_x*r_y;
871 int cb_h=ca_h+1;
872 int cb_v=ca_v+1;
873 if ( cb_h >= pp->cached_bbox.x1 ) cb_h=0;
874 if ( cb_v >= pp->cached_bbox.y1 ) cb_v=0;
876 unsigned char* tlx=NR_PIXBLOCK_PX(&pp->cached_tile)+(ca_v*pp->cached_tile.rs)+4*ca_h;
877 unsigned char* trx=NR_PIXBLOCK_PX(&pp->cached_tile)+(ca_v*pp->cached_tile.rs)+4*cb_h;
878 unsigned char* blx=NR_PIXBLOCK_PX(&pp->cached_tile)+(cb_v*pp->cached_tile.rs)+4*ca_h;
879 unsigned char* brx=NR_PIXBLOCK_PX(&pp->cached_tile)+(cb_v*pp->cached_tile.rs)+4*cb_h;
881 unsigned int tl_c=tlx[0];
882 unsigned int tr_c=trx[0];
883 unsigned int bl_c=blx[0];
884 unsigned int br_c=brx[0];
885 unsigned int f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
886 r=f_c;
887 tl_c=tlx[1];
888 tr_c=trx[1];
889 bl_c=blx[1];
890 br_c=brx[1];
891 f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
892 g=f_c;
893 tl_c=tlx[2];
894 tr_c=trx[2];
895 bl_c=blx[2];
896 br_c=brx[2];
897 f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
898 b=f_c;
899 tl_c=tlx[3];
900 tr_c=trx[3];
901 bl_c=blx[3];
902 br_c=brx[3];
903 f_c=(tl_m*tl_c+tr_m*tr_c+bl_m*bl_c+br_m*br_c)>>8;
904 a=f_c;
905 }
907 static void
908 sp_pat_fill (SPPainter *painter, NRPixBlock *pb)
909 {
910 SPPatPainter *pp;
911 NRRect ba, psa;
912 NRRectL area;
913 double x, y;
915 pp = (SPPatPainter *) painter;
917 if (pattern_width (pp->pat) < NR_EPSILON) return;
918 if (pattern_height (pp->pat) < NR_EPSILON) return;
920 /* Find buffer area in gradient space */
921 /* fixme: This is suboptimal (Lauris) */
923 if ( pp->use_cached_tile ) {
924 double pat_w=pattern_width (pp->pat);
925 double pat_h=pattern_height (pp->pat);
926 if ( pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N || pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P ) { // same thing because it's filling an empty pixblock
927 unsigned char* lpx=NR_PIXBLOCK_PX(pb);
928 double px_y=pb->area.y0;
929 for (int j=pb->area.y0;j<pb->area.y1;j++) {
930 unsigned char* cpx=lpx;
931 double px_x = pb->area.x0;
933 double ps_x=pp->px2ps[0]*px_x+pp->px2ps[2]*px_y+pp->px2ps[4];
934 double ps_y=pp->px2ps[1]*px_x+pp->px2ps[3]*px_y+pp->px2ps[5];
935 for (int i=pb->area.x0;i<pb->area.x1;i++) {
936 while ( ps_x > pat_w ) ps_x-=pat_w;
937 while ( ps_x < 0 ) ps_x+=pat_w;
938 while ( ps_y > pat_h ) ps_y-=pat_h;
939 while ( ps_y < 0 ) ps_y+=pat_h;
940 double ca_x=pp->pa2ca[0]*ps_x+pp->pa2ca[2]*ps_y+pp->pa2ca[4];
941 double ca_y=pp->pa2ca[1]*ps_x+pp->pa2ca[3]*ps_y+pp->pa2ca[5];
942 unsigned char n_a,n_r,n_g,n_b;
943 get_cached_tile_pixel(pp,ca_x,ca_y,n_r,n_g,n_b,n_a);
944 cpx[0]=n_r;
945 cpx[1]=n_g;
946 cpx[2]=n_b;
947 cpx[3]=n_a;
949 px_x+=1.0;
950 ps_x+=pp->px2ps[0];
951 ps_y+=pp->px2ps[1];
952 cpx+=4;
953 }
954 px_y+=1.0;
955 lpx+=pb->rs;
956 }
957 } else if ( pb->mode == NR_PIXBLOCK_MODE_R8G8B8 ) {
958 unsigned char* lpx=NR_PIXBLOCK_PX(pb);
959 double px_y=pb->area.y0;
960 for (int j=pb->area.y0;j<pb->area.y1;j++) {
961 unsigned char* cpx=lpx;
962 double px_x = pb->area.x0;
964 double ps_x=pp->px2ps[0]*px_x+pp->px2ps[2]*px_y+pp->px2ps[4];
965 double ps_y=pp->px2ps[1]*px_x+pp->px2ps[3]*px_y+pp->px2ps[5];
966 for (int i=pb->area.x0;i<pb->area.x1;i++) {
967 while ( ps_x > pat_w ) ps_x-=pat_w;
968 while ( ps_x < 0 ) ps_x+=pat_w;
969 while ( ps_y > pat_h ) ps_y-=pat_h;
970 while ( ps_y < 0 ) ps_y+=pat_h;
971 double ca_x=pp->pa2ca[0]*ps_x+pp->pa2ca[2]*ps_y+pp->pa2ca[4];
972 double ca_y=pp->pa2ca[1]*ps_x+pp->pa2ca[3]*ps_y+pp->pa2ca[5];
973 unsigned char n_a,n_r,n_g,n_b;
974 get_cached_tile_pixel(pp,ca_x,ca_y,n_r,n_g,n_b,n_a);
975 cpx[0]=n_r;
976 cpx[1]=n_g;
977 cpx[2]=n_b;
979 px_x+=1.0;
980 ps_x+=pp->px2ps[0];
981 ps_y+=pp->px2ps[1];
982 cpx+=4;
983 }
984 px_y+=1.0;
985 lpx+=pb->rs;
986 }
987 }
988 } else {
989 ba.x0 = pb->area.x0;
990 ba.y0 = pb->area.y0;
991 ba.x1 = pb->area.x1;
992 ba.y1 = pb->area.y1;
994 // Trying to solve this bug: https://bugs.launchpad.net/inkscape/+bug/167416
995 // Bail out if the transformation matrix has extreme values. If we bail out
996 // however, then something (which was meaningless anyway) won't be rendered,
997 // which is better than getting stuck in a virtually infinite loop
998 if (fabs(pp->px2ps[0]) < 1e6 &&
999 fabs(pp->px2ps[3]) < 1e6 &&
1000 fabs(pp->px2ps[4]) < 1e6 &&
1001 fabs(pp->px2ps[5]) < 1e6)
1002 {
1003 // TODO: remove px2ps_nr after converting to 2geom
1004 NR::Matrix px2ps_nr = from_2geom(pp->px2ps);
1005 nr_rect_d_matrix_transform (&psa, &ba, &px2ps_nr);
1007 psa.x0 = floor ((psa.x0 - pattern_x (pp->pat)) / pattern_width (pp->pat)) -1;
1008 psa.y0 = floor ((psa.y0 - pattern_y (pp->pat)) / pattern_height (pp->pat)) -1;
1009 psa.x1 = ceil ((psa.x1 - pattern_x (pp->pat)) / pattern_width (pp->pat)) +1;
1010 psa.y1 = ceil ((psa.y1 - pattern_y (pp->pat)) / pattern_height (pp->pat)) +1;
1012 // If psa is too wide or tall, then something must be wrong! This is due to
1013 // nr_rect_d_matrix_transform (&psa, &ba, &pp->px2ps) using a weird transformation matrix pp->px2ps.
1014 g_assert(std::abs(psa.x1 - psa.x0) < 1e6);
1015 g_assert(std::abs(psa.y1 - psa.y0) < 1e6);
1017 for (y = psa.y0; y < psa.y1; y++) {
1018 for (x = psa.x0; x < psa.x1; x++) {
1019 NRPixBlock ppb;
1020 double psx, psy;
1022 psx = x * pattern_width (pp->pat);
1023 psy = y * pattern_height (pp->pat);
1025 area.x0 = (gint32)(pb->area.x0 - (pp->ps2px[0] * psx + pp->ps2px[2] * psy));
1026 area.y0 = (gint32)(pb->area.y0 - (pp->ps2px[1] * psx + pp->ps2px[3] * psy));
1027 area.x1 = area.x0 + pb->area.x1 - pb->area.x0;
1028 area.y1 = area.y0 + pb->area.y1 - pb->area.y0;
1030 // We do not update here anymore
1032 // Set up buffer
1033 // fixme: (Lauris)
1034 nr_pixblock_setup_extern (&ppb, pb->mode, area.x0, area.y0, area.x1, area.y1, NR_PIXBLOCK_PX (pb), pb->rs, FALSE, FALSE);
1036 nr_arena_item_invoke_render (NULL, pp->root, &area, &ppb, 0);
1038 nr_pixblock_release (&ppb);
1039 }
1040 }
1041 }
1042 }
1043 }
1046 /*
1047 Local Variables:
1048 mode:c++
1049 c-file-style:"stroustrup"
1050 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1051 indent-tabs-mode:nil
1052 fill-column:99
1053 End:
1054 */
1055 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :