sp-symbol.cpp revision 36bb2154f1627a17c3591eb4d7f89335e8b5dadd
0N/A/*
0N/A * SVG <symbol> implementation
1879N/A *
0N/A * Authors:
0N/A * Lauris Kaplinski <lauris@kaplinski.com>
0N/A * Abhishek Sharma
0N/A * Jon A. Cruz <jon@joncruz.org>
0N/A *
0N/A * Copyright (C) 1999-2003 Lauris Kaplinski
0N/A *
0N/A * Released under GNU GPL, read the file 'COPYING' for more information
0N/A */
0N/A
0N/A#ifdef HAVE_CONFIG_H
0N/A# include "config.h"
0N/A#endif
0N/A
0N/A#include <cstring>
0N/A#include <string>
1472N/A
1472N/A#include <2geom/transforms.h>
1472N/A#include "display/drawing-group.h"
0N/A#include "xml/repr.h"
0N/A#include "attributes.h"
0N/A#include "print.h"
0N/A#include "sp-symbol.h"
0N/A#include "document.h"
0N/A
0N/Astatic void sp_symbol_class_init (SPSymbolClass *klass);
0N/Astatic void sp_symbol_init (SPSymbol *symbol);
0N/A
0N/Astatic void sp_symbol_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
0N/Astatic void sp_symbol_release (SPObject *object);
0N/Astatic void sp_symbol_set (SPObject *object, unsigned int key, const gchar *value);
0N/Astatic void sp_symbol_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
0N/Astatic void sp_symbol_update (SPObject *object, SPCtx *ctx, guint flags);
0N/Astatic void sp_symbol_modified (SPObject *object, guint flags);
0N/Astatic Inkscape::XML::Node *sp_symbol_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
1879N/A
1879N/Astatic Inkscape::DrawingItem *sp_symbol_show (SPItem *item, Inkscape::Drawing &drawing, unsigned int key, unsigned int flags);
1879N/Astatic void sp_symbol_hide (SPItem *item, unsigned int key);
1879N/Astatic Geom::OptRect sp_symbol_bbox(SPItem const *item, Geom::Affine const &transform, SPItem::BBoxType type);
0N/Astatic void sp_symbol_print (SPItem *item, SPPrintContext *ctx);
0N/A
0N/Astatic SPGroupClass *parent_class;
0N/A
0N/AGType
0N/Asp_symbol_get_type (void)
0N/A{
0N/A static GType type = 0;
0N/A if (!type) {
0N/A GTypeInfo info = {
0N/A sizeof (SPSymbolClass),
0N/A NULL, NULL,
0N/A (GClassInitFunc) sp_symbol_class_init,
0N/A NULL, NULL,
0N/A sizeof (SPSymbol),
0N/A 16,
0N/A (GInstanceInitFunc) sp_symbol_init,
0N/A NULL, /* value_table */
0N/A };
0N/A type = g_type_register_static (SP_TYPE_GROUP, "SPSymbol", &info, (GTypeFlags)0);
0N/A }
0N/A return type;
0N/A}
0N/A
0N/Astatic void sp_symbol_class_init(SPSymbolClass *klass)
0N/A{
0N/A SPObjectClass *sp_object_class = (SPObjectClass *) klass;
0N/A SPItemClass *sp_item_class = (SPItemClass *) klass;
0N/A
0N/A parent_class = (SPGroupClass *)g_type_class_ref (SP_TYPE_GROUP);
0N/A
0N/A sp_object_class->build = sp_symbol_build;
0N/A sp_object_class->release = sp_symbol_release;
0N/A sp_object_class->set = sp_symbol_set;
0N/A sp_object_class->child_added = sp_symbol_child_added;
0N/A sp_object_class->update = sp_symbol_update;
0N/A sp_object_class->modified = sp_symbol_modified;
0N/A sp_object_class->write = sp_symbol_write;
0N/A
0N/A sp_item_class->show = sp_symbol_show;
0N/A sp_item_class->hide = sp_symbol_hide;
0N/A sp_item_class->bbox = sp_symbol_bbox;
0N/A sp_item_class->print = sp_symbol_print;
0N/A}
0N/A
0N/Astatic void sp_symbol_init(SPSymbol *symbol)
0N/A{
0N/A symbol->viewBox_set = FALSE;
0N/A
0N/A symbol->c2p = Geom::identity();
0N/A}
0N/A
0N/Astatic void sp_symbol_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
0N/A{
0N/A object->readAttr( "viewBox" );
0N/A object->readAttr( "preserveAspectRatio" );
0N/A
0N/A if (((SPObjectClass *) parent_class)->build) {
0N/A ((SPObjectClass *) parent_class)->build (object, document, repr);
0N/A }
0N/A}
0N/A
0N/Astatic void sp_symbol_release(SPObject *object)
0N/A{
0N/A if (((SPObjectClass *) parent_class)->release) {
0N/A ((SPObjectClass *) parent_class)->release (object);
0N/A }
0N/A}
0N/A
0N/Astatic void sp_symbol_set(SPObject *object, unsigned int key, const gchar *value)
0N/A{
0N/A SPSymbol *symbol = SP_SYMBOL(object);
0N/A
0N/A switch (key) {
0N/A case SP_ATTR_VIEWBOX:
0N/A if (value) {
0N/A double x, y, width, height;
0N/A char *eptr;
0N/A /* fixme: We have to take original item affine into account */
0N/A /* fixme: Think (Lauris) */
0N/A eptr = (gchar *) value;
0N/A x = g_ascii_strtod (eptr, &eptr);
0N/A while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
0N/A y = g_ascii_strtod (eptr, &eptr);
0N/A while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
0N/A width = g_ascii_strtod (eptr, &eptr);
0N/A while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
0N/A height = g_ascii_strtod (eptr, &eptr);
0N/A while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
0N/A if ((width > 0) && (height > 0)) {
0N/A /* Set viewbox */
0N/A symbol->viewBox.x0 = x;
0N/A symbol->viewBox.y0 = y;
0N/A symbol->viewBox.x1 = x + width;
0N/A symbol->viewBox.y1 = y + height;
0N/A symbol->viewBox_set = TRUE;
0N/A } else {
0N/A symbol->viewBox_set = FALSE;
0N/A }
0N/A } else {
0N/A symbol->viewBox_set = FALSE;
0N/A }
0N/A object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
0N/A break;
0N/A case SP_ATTR_PRESERVEASPECTRATIO:
0N/A /* Do setup before, so we can use break to escape */
0N/A symbol->aspect_set = FALSE;
0N/A symbol->aspect_align = SP_ASPECT_NONE;
0N/A symbol->aspect_clip = SP_ASPECT_MEET;
0N/A object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
0N/A if (value) {
0N/A int len;
0N/A gchar c[256];
0N/A const gchar *p, *e;
0N/A unsigned int align, clip;
0N/A p = value;
0N/A while (*p && *p == 32) p += 1;
0N/A if (!*p) break;
0N/A e = p;
0N/A while (*e && *e != 32) e += 1;
0N/A len = e - p;
0N/A if (len > 8) break;
0N/A memcpy (c, value, len);
0N/A c[len] = 0;
0N/A /* Now the actual part */
0N/A if (!strcmp (c, "none")) {
0N/A align = SP_ASPECT_NONE;
0N/A } else if (!strcmp (c, "xMinYMin")) {
0N/A align = SP_ASPECT_XMIN_YMIN;
0N/A } else if (!strcmp (c, "xMidYMin")) {
0N/A align = SP_ASPECT_XMID_YMIN;
0N/A } else if (!strcmp (c, "xMaxYMin")) {
0N/A align = SP_ASPECT_XMAX_YMIN;
0N/A } else if (!strcmp (c, "xMinYMid")) {
0N/A align = SP_ASPECT_XMIN_YMID;
0N/A } else if (!strcmp (c, "xMidYMid")) {
0N/A align = SP_ASPECT_XMID_YMID;
0N/A } else if (!strcmp (c, "xMaxYMid")) {
0N/A align = SP_ASPECT_XMAX_YMID;
0N/A } else if (!strcmp (c, "xMinYMax")) {
0N/A align = SP_ASPECT_XMIN_YMAX;
0N/A } else if (!strcmp (c, "xMidYMax")) {
0N/A align = SP_ASPECT_XMID_YMAX;
0N/A } else if (!strcmp (c, "xMaxYMax")) {
0N/A align = SP_ASPECT_XMAX_YMAX;
0N/A } else {
0N/A break;
0N/A }
0N/A clip = SP_ASPECT_MEET;
0N/A while (*e && *e == 32) e += 1;
0N/A if (*e) {
0N/A if (!strcmp (e, "meet")) {
0N/A clip = SP_ASPECT_MEET;
0N/A } else if (!strcmp (e, "slice")) {
0N/A clip = SP_ASPECT_SLICE;
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A symbol->aspect_set = TRUE;
0N/A symbol->aspect_align = align;
0N/A symbol->aspect_clip = clip;
0N/A }
0N/A break;
0N/A default:
0N/A if (((SPObjectClass *) parent_class)->set)
0N/A ((SPObjectClass *) parent_class)->set (object, key, value);
0N/A break;
0N/A }
0N/A}
0N/A
0N/Astatic void sp_symbol_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
0N/A{
0N/A if (((SPObjectClass *) (parent_class))->child_added) {
0N/A ((SPObjectClass *) (parent_class))->child_added (object, child, ref);
0N/A }
0N/A}
0N/A
0N/Astatic void sp_symbol_update(SPObject *object, SPCtx *ctx, guint flags)
0N/A{
0N/A SPSymbol *symbol = SP_SYMBOL(object);
0N/A SPItemCtx *ictx = (SPItemCtx *) ctx;
0N/A SPItemCtx rctx;
0N/A
0N/A if (object->cloned) {
0N/A /* Cloned <symbol> is actually renderable */
0N/A
0N/A /* fixme: We have to set up clip here too */
0N/A
0N/A /* Create copy of item context */
0N/A rctx = *ictx;
0N/A
0N/A /* Calculate child to parent transformation */
0N/A /* Apply parent <use> translation (set up as vewport) */
0N/A symbol->c2p = Geom::Affine(Geom::Translate(rctx.vp.x0, rctx.vp.y0));
0N/A
0N/A if (symbol->viewBox_set) {
0N/A double x, y, width, height;
0N/A /* Determine actual viewbox in viewport coordinates */
0N/A if (symbol->aspect_align == SP_ASPECT_NONE) {
0N/A x = 0.0;
0N/A y = 0.0;
0N/A width = rctx.vp.x1 - rctx.vp.x0;
0N/A height = rctx.vp.y1 - rctx.vp.y0;
0N/A } else {
0N/A double scalex, scaley, scale;
0N/A /* Things are getting interesting */
0N/A scalex = (rctx.vp.x1 - rctx.vp.x0) / (symbol->viewBox.x1 - symbol->viewBox.x0);
0N/A scaley = (rctx.vp.y1 - rctx.vp.y0) / (symbol->viewBox.y1 - symbol->viewBox.y0);
0N/A scale = (symbol->aspect_clip == SP_ASPECT_MEET) ? MIN (scalex, scaley) : MAX (scalex, scaley);
0N/A width = (symbol->viewBox.x1 - symbol->viewBox.x0) * scale;
0N/A height = (symbol->viewBox.y1 - symbol->viewBox.y0) * scale;
0N/A /* Now place viewbox to requested position */
0N/A switch (symbol->aspect_align) {
0N/A case SP_ASPECT_XMIN_YMIN:
0N/A x = 0.0;
0N/A y = 0.0;
0N/A break;
0N/A case SP_ASPECT_XMID_YMIN:
0N/A x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
0N/A y = 0.0;
0N/A break;
0N/A case SP_ASPECT_XMAX_YMIN:
0N/A x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
0N/A y = 0.0;
0N/A break;
0N/A case SP_ASPECT_XMIN_YMID:
0N/A x = 0.0;
0N/A y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
0N/A break;
0N/A case SP_ASPECT_XMID_YMID:
0N/A x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
0N/A y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
0N/A break;
0N/A case SP_ASPECT_XMAX_YMID:
0N/A x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
0N/A y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
0N/A break;
0N/A case SP_ASPECT_XMIN_YMAX:
0N/A x = 0.0;
0N/A y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
0N/A break;
0N/A case SP_ASPECT_XMID_YMAX:
0N/A x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
0N/A y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
0N/A break;
0N/A case SP_ASPECT_XMAX_YMAX:
0N/A x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
0N/A y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
0N/A break;
0N/A default:
0N/A x = 0.0;
0N/A y = 0.0;
0N/A break;
0N/A }
0N/A }
0N/A /* Compose additional transformation from scale and position */
0N/A Geom::Affine q;
0N/A q[0] = width / (symbol->viewBox.x1 - symbol->viewBox.x0);
0N/A q[1] = 0.0;
0N/A q[2] = 0.0;
0N/A q[3] = height / (symbol->viewBox.y1 - symbol->viewBox.y0);
0N/A q[4] = -symbol->viewBox.x0 * q[0] + x;
0N/A q[5] = -symbol->viewBox.y0 * q[3] + y;
0N/A /* Append viewbox transformation */
0N/A symbol->c2p = q * symbol->c2p;
0N/A }
0N/A
0N/A rctx.i2doc = symbol->c2p * (Geom::Affine)rctx.i2doc;
0N/A
0N/A /* If viewBox is set initialize child viewport */
0N/A /* Otherwise <use> has set it up already */
0N/A if (symbol->viewBox_set) {
0N/A rctx.vp.x0 = symbol->viewBox.x0;
0N/A rctx.vp.y0 = symbol->viewBox.y0;
0N/A rctx.vp.x1 = symbol->viewBox.x1;
0N/A rctx.vp.y1 = symbol->viewBox.y1;
0N/A rctx.i2vp = Geom::identity();
0N/A }
0N/A
0N/A // And invoke parent method
0N/A if (((SPObjectClass *) (parent_class))->update) {
0N/A ((SPObjectClass *) (parent_class))->update (object, (SPCtx *) &rctx, flags);
0N/A }
0N/A
0N/A // As last step set additional transform of drawing group
0N/A for (SPItemView *v = symbol->display; v != NULL; v = v->next) {
0N/A Inkscape::DrawingGroup *g = dynamic_cast<Inkscape::DrawingGroup *>(v->arenaitem);
0N/A g->setChildTransform(symbol->c2p);
0N/A }
0N/A } else {
0N/A // No-op
0N/A if (((SPObjectClass *) (parent_class))->update) {
0N/A ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void sp_symbol_modified(SPObject *object, guint flags)
0N/A{
0N/A SP_SYMBOL(object);
0N/A
0N/A if (((SPObjectClass *) (parent_class))->modified) {
0N/A (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
0N/A }
0N/A}
0N/A
0N/Astatic Inkscape::XML::Node *sp_symbol_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
0N/A{
0N/A SP_SYMBOL(object);
0N/A
0N/A if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
0N/A repr = xml_doc->createElement("svg:symbol");
0N/A }
0N/A
0N/A //XML Tree being used directly here while it shouldn't be.
0N/A repr->setAttribute("viewBox", object->getRepr()->attribute("viewBox"));
0N/A
0N/A //XML Tree being used directly here while it shouldn't be.
0N/A repr->setAttribute("preserveAspectRatio", object->getRepr()->attribute("preserveAspectRatio"));
0N/A
0N/A if (((SPObjectClass *) (parent_class))->write) {
0N/A ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
0N/A }
0N/A
0N/A return repr;
0N/A}
0N/A
0N/Astatic Inkscape::DrawingItem *sp_symbol_show(SPItem *item, Inkscape::Drawing &drawing, unsigned int key, unsigned int flags)
0N/A{
0N/A SPSymbol *symbol = SP_SYMBOL(item);
0N/A Inkscape::DrawingItem *ai = 0;
0N/A
0N/A if (symbol->cloned) {
0N/A // Cloned <symbol> is actually renderable
0N/A if (((SPItemClass *) (parent_class))->show) {
0N/A ai = ((SPItemClass *) (parent_class))->show (item, drawing, key, flags);
0N/A Inkscape::DrawingGroup *g = dynamic_cast<Inkscape::DrawingGroup *>(ai);
0N/A if (g) {
0N/A g->setChildTransform(symbol->c2p);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return ai;
0N/A}
0N/A
0N/Astatic void sp_symbol_hide(SPItem *item, unsigned int key)
0N/A{
0N/A SPSymbol *symbol = SP_SYMBOL(item);
0N/A
0N/A if (symbol->cloned) {
0N/A /* Cloned <symbol> is actually renderable */
0N/A if (((SPItemClass *) (parent_class))->hide) {
0N/A ((SPItemClass *) (parent_class))->hide (item, key);
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic Geom::OptRect sp_symbol_bbox(SPItem const *item, Geom::Affine const &transform, SPItem::BBoxType type)
0N/A{
0N/A SPSymbol const *symbol = SP_SYMBOL(item);
0N/A Geom::OptRect bbox;
0N/A
0N/A if (symbol->cloned) {
0N/A // Cloned <symbol> is actually renderable
0N/A
0N/A if (((SPItemClass *) (parent_class))->bbox) {
0N/A Geom::Affine const a( symbol->c2p * transform );
0N/A bbox = ((SPItemClass *) (parent_class))->bbox(item, a, type);
0N/A }
0N/A }
0N/A return bbox;
0N/A}
0N/A
0N/Astatic void sp_symbol_print(SPItem *item, SPPrintContext *ctx)
0N/A{
0N/A SPSymbol *symbol = SP_SYMBOL(item);
0N/A if (symbol->cloned) {
0N/A // Cloned <symbol> is actually renderable
0N/A
0N/A sp_print_bind(ctx, &symbol->c2p, 1.0);
0N/A
0N/A if (((SPItemClass *) (parent_class))->print) {
0N/A ((SPItemClass *) (parent_class))->print (item, ctx);
0N/A }
2883N/A
0N/A sp_print_release (ctx);
0N/A }
0N/A}
0N/A