sp-star.cpp revision a8ccce3fe7248cc5294a4f1716593c2c9c358c3c
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/*
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * <sodipodi:star> implementation
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Authors:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Mitsuru Oka <oka326@parkcity.ne.jp>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Lauris Kaplinski <lauris@kaplinski.com>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * bulia byak <buliabyak@users.sf.net>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Abhishek Sharma
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Copyright (C) 1999-2002 Lauris Kaplinski
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Copyright (C) 2000-2001 Ximian, Inc.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Released under GNU GPL, read the file 'COPYING' for more information
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#ifdef HAVE_CONFIG_H
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh# include "config.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#endif
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include <cstring>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include <string>
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include <glib.h>
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include <glibmm/i18n.h>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "svg/svg.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "attributes.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "display/curve.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "xml/repr.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "document.h"
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include <2geom/pathvector.h>
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#include "sp-star.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh#include "sp-factory.h"
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
fba63a357654d8b3e84c60007e40aa698cd45d19mikloshnamespace {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh SPObject* createStar() {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return new SPStar();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh bool starRegistered = SPFactory::instance().registerObject("star", createStar);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshSPStar::SPStar() : SPPolygon() {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->sides = 5;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->center = Geom::Point(0, 0);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->r[0] = 1.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->r[1] = 0.001;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->arg[0] = this->arg[1] = 0.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->flatsided = 0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->rounded = 0.0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->randomized = 0.0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshSPStar::~SPStar() {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emikloshvoid SPStar::build(SPDocument * document, Inkscape::XML::Node * repr) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh // CPPIFY: see header file
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh SPShape::build(document, repr);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:cx" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:cy" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:sides" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:r1" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:r2" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:arg1" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "sodipodi:arg2" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "inkscape:flatsided" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "inkscape:rounded" );
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->readAttr( "inkscape:randomized" );
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshInkscape::XML::Node* SPStar::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh repr = xml_doc->createElement("svg:path");
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
1cda9431ef400135f5e1bd899a94b921bdad0eafmiklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (flags & SP_OBJECT_WRITE_EXT) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh repr->setAttribute("sodipodi:type", "star");
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh sp_repr_set_int (repr, "sodipodi:sides", this->sides);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_set_svg_double(repr, "sodipodi:cx", this->center[Geom::X]);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_set_svg_double(repr, "sodipodi:cy", this->center[Geom::Y]);
a4d12a5147f3d1d6b568a326e39ef5dca384248dmiklosh sp_repr_set_svg_double(repr, "sodipodi:r1", this->r[0]);
1667116521643e2475184b048e0abb77a2aa9735miklosh sp_repr_set_svg_double(repr, "sodipodi:r2", this->r[1]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(repr, "sodipodi:arg1", this->arg[0]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_svg_double(repr, "sodipodi:arg2", this->arg[1]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_repr_set_boolean (repr, "inkscape:flatsided", this->flatsided);
1cda9431ef400135f5e1bd899a94b921bdad0eafmiklosh sp_repr_set_svg_double(repr, "inkscape:rounded", this->rounded);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_repr_set_svg_double(repr, "inkscape:randomized", this->randomized);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->set_shape();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh char *d = sp_svg_write_path (this->_curve->get_pathvector());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh repr->setAttribute("d", d);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(d);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // CPPIFY: see header file
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPShape::write(xml_doc, repr, flags);
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh return repr;
1667116521643e2475184b048e0abb77a2aa9735miklosh}
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735mikloshvoid SPStar::set(unsigned int key, const gchar* value) {
1667116521643e2475184b048e0abb77a2aa9735miklosh SVGLength::Unit unit;
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh /* fixme: we should really collect updates */
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh switch (key) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh case SP_ATTR_SODIPODI_SIDES:
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh if (value) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->sides = atoi (value);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->sides = CLAMP(this->sides, 3, 1024);
1667116521643e2475184b048e0abb77a2aa9735miklosh } else {
1667116521643e2475184b048e0abb77a2aa9735miklosh this->sides = 5;
1667116521643e2475184b048e0abb77a2aa9735miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh case SP_ATTR_SODIPODI_CX:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (!sp_svg_length_read_ldd (value, &unit, NULL, &this->center[Geom::X]) ||
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh (unit == SVGLength::EM) ||
68664e00e2372534b4df2fdc5f54f836bafece18miklosh (unit == SVGLength::EX) ||
68664e00e2372534b4df2fdc5f54f836bafece18miklosh (unit == SVGLength::PERCENT)) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh this->center[Geom::X] = 0.0;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
68664e00e2372534b4df2fdc5f54f836bafece18miklosh
68664e00e2372534b4df2fdc5f54f836bafece18miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh break;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh
68664e00e2372534b4df2fdc5f54f836bafece18miklosh case SP_ATTR_SODIPODI_CY:
68664e00e2372534b4df2fdc5f54f836bafece18miklosh if (!sp_svg_length_read_ldd (value, &unit, NULL, &this->center[Geom::Y]) ||
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh (unit == SVGLength::EM) ||
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh (unit == SVGLength::EX) ||
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh (unit == SVGLength::PERCENT)) {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->center[Geom::Y] = 0.0;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh }
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh case SP_ATTR_SODIPODI_R1:
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh if (!sp_svg_length_read_ldd (value, &unit, NULL, &this->r[0]) ||
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh (unit == SVGLength::EM) ||
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh (unit == SVGLength::EX) ||
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh (unit == SVGLength::PERCENT)) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->r[0] = 1.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* fixme: Need CLAMP (Lauris) */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case SP_ATTR_SODIPODI_R2:
1667116521643e2475184b048e0abb77a2aa9735miklosh if (!sp_svg_length_read_ldd (value, &unit, NULL, &this->r[1]) ||
1667116521643e2475184b048e0abb77a2aa9735miklosh (unit == SVGLength::EM) ||
1667116521643e2475184b048e0abb77a2aa9735miklosh (unit == SVGLength::EX) ||
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh (unit == SVGLength::PERCENT)) {
1667116521643e2475184b048e0abb77a2aa9735miklosh this->r[1] = 0.0;
1667116521643e2475184b048e0abb77a2aa9735miklosh }
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1667116521643e2475184b048e0abb77a2aa9735miklosh return;
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh case SP_ATTR_SODIPODI_ARG1:
1667116521643e2475184b048e0abb77a2aa9735miklosh if (value) {
1667116521643e2475184b048e0abb77a2aa9735miklosh this->arg[0] = g_ascii_strtod (value, NULL);
1667116521643e2475184b048e0abb77a2aa9735miklosh } else {
1667116521643e2475184b048e0abb77a2aa9735miklosh this->arg[0] = 0.0;
1667116521643e2475184b048e0abb77a2aa9735miklosh }
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1667116521643e2475184b048e0abb77a2aa9735miklosh break;
1667116521643e2475184b048e0abb77a2aa9735miklosh
1667116521643e2475184b048e0abb77a2aa9735miklosh case SP_ATTR_SODIPODI_ARG2:
1667116521643e2475184b048e0abb77a2aa9735miklosh if (value) {
1667116521643e2475184b048e0abb77a2aa9735miklosh this->arg[1] = g_ascii_strtod (value, NULL);
1667116521643e2475184b048e0abb77a2aa9735miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->arg[1] = 0.0;
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh }
68664e00e2372534b4df2fdc5f54f836bafece18miklosh
1cda9431ef400135f5e1bd899a94b921bdad0eafmiklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
68664e00e2372534b4df2fdc5f54f836bafece18miklosh break;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh
68664e00e2372534b4df2fdc5f54f836bafece18miklosh case SP_ATTR_INKSCAPE_FLATSIDED:
68664e00e2372534b4df2fdc5f54f836bafece18miklosh if (value && !strcmp(value, "true")) {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh this->flatsided = true;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh } else {
68664e00e2372534b4df2fdc5f54f836bafece18miklosh this->flatsided = false;
68664e00e2372534b4df2fdc5f54f836bafece18miklosh }
68664e00e2372534b4df2fdc5f54f836bafece18miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case SP_ATTR_INKSCAPE_ROUNDED:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (value) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->rounded = g_ascii_strtod (value, NULL);
1667116521643e2475184b048e0abb77a2aa9735miklosh } else {
c53f16f52840e8c0f2be9c1cc3af633c0ba1552emiklosh this->rounded = 0.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh case SP_ATTR_INKSCAPE_RANDOMIZED:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (value) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->randomized = g_ascii_strtod (value, NULL);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->randomized = 0.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh break;
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh
fba63a357654d8b3e84c60007e40aa698cd45d19miklosh default:
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // CPPIFY: see header file
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPShape::set(key, value);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh break;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh}
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh
d27f5758e12c3107ee69e66702043931e0756f6bmikloshvoid SPStar::update(SPCtx *ctx, guint flags) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (flags & (SP_OBJECT_MODIFIED_FLAG |
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SP_OBJECT_STYLE_MODIFIED_FLAG |
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->set_shape();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // CPPIFY: see header file
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh SPShape::update(ctx, flags);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshvoid SPStar::update_patheffect(bool write) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->set_shape();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (write) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Inkscape::XML::Node *repr = this->getRepr();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if ( this->_curve != NULL ) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh gchar *str = sp_svg_write_path(this->_curve->get_pathvector());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh repr->setAttribute("d", str);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_free(str);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh repr->setAttribute("d", NULL);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshconst char* SPStar::displayName() const {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (this->flatsided == false)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return _("Star");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return _("Polygon");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshgchar* SPStar::description() const {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // while there will never be less than 3 vertices, we still need to
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // make calls to ngettext because the pluralization may be different
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // for various numbers >=3. The singular form is used as the index.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return g_strdup_printf (ngettext(_("with %d vertex"), _("with %d vertices"),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->sides), this->sides);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshReturns a unit-length vector at 90 degrees to the direction from o to n
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic Geom::Point
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshrot90_rel (Geom::Point o, Geom::Point n)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return ((1/Geom::L2(n - o)) * Geom::Point ((n - o)[Geom::Y], (o - n)[Geom::X]));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshReturns a unique 32 bit int for a given point.
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshObvious (but acceptable for my purposes) limits to uniqueness:
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh- returned value for x,y repeats for x+n*1024,y+n*1024
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh- returned value is unchanged when the point is moved by less than 1/1024 of px
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh*/
3711b3e25395437ee0a09dbbb2a76d999c4ef322mikloshstatic guint32
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshpoint_unique_int (Geom::Point o)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return ((guint32)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh 65536 *
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh (((int) floor (o[Geom::X] * 64)) % 1024 + ((int) floor (o[Geom::X] * 1024)) % 64)
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh +
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh (((int) floor (o[Geom::Y] * 64)) % 1024 + ((int) floor (o[Geom::Y] * 1024)) % 64)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh );
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshReturns the next pseudorandom value using the Linear Congruential Generator algorithm (LCG)
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshwith the parameters (m = 2^32, a = 69069, b = 1). These parameters give a full-period generator,
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshi.e. it is guaranteed to go through all integers < 2^32 (see http://random.mat.sbg.ac.at/~charly/server/server.html)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh*/
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic inline guint32
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshlcg_next(guint32 const prev)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return (guint32) ( 69069 * prev + 1 );
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh/**
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshReturns a random number in the range [-0.5, 0.5) from the given seed, stepping the given number of steps from the seed.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh*/
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic double
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshrnd (guint32 const seed, unsigned steps) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh guint32 lcg = seed;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh for (; steps > 0; steps --)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh lcg = lcg_next (lcg);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return ( lcg / 4294967296. ) - 0.5;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshstatic Geom::Point
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshsp_star_get_curvepoint (SPStar *star, SPStarPoint point, gint index, bool previ)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // the point whose neighboring curve handle we're calculating
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point o = sp_star_get_xy (star, point, index);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // indices of previous and next points
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gint pi = (index > 0)? (index - 1) : (star->sides - 1);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gint ni = (index < star->sides - 1)? (index + 1) : 0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // the other point type
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPStarPoint other = (point == SP_STAR_POINT_KNOT2? SP_STAR_POINT_KNOT1 : SP_STAR_POINT_KNOT2);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // the neighbors of o; depending on flatsided, they're either the same type (polygon) or the other type (star)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point prev = (star->flatsided? sp_star_get_xy (star, point, pi) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT2? index : pi));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point next = (star->flatsided? sp_star_get_xy (star, point, ni) : sp_star_get_xy (star, other, point == SP_STAR_POINT_KNOT1? index : ni));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // prev-next midpoint
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point mid = 0.5 * (prev + next);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // point to which we direct the bissector of the curve handles;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // it's far enough outside the star on the perpendicular to prev-next through mid
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point biss = mid + 100000 * rot90_rel (mid, next);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // lengths of vectors to prev and next
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gdouble prev_len = Geom::L2 (prev - o);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gdouble next_len = Geom::L2 (next - o);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // unit-length vector perpendicular to o-biss
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Geom::Point rot = rot90_rel (o, biss);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // multiply rot by star->rounded coefficient and the distance to the star point; flip for next
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point ret;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (previ) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret = (star->rounded * prev_len) * rot;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret = (star->rounded * next_len * -1) * rot;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (star->randomized == 0) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // add the vector to o to get the final curvepoint
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return o + ret;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // the seed corresponding to the exact point
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh guint32 seed = point_unique_int (o);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // randomly rotate (by step 3 from the seed) and scale (by step 4) the vector
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret = ret * Geom::Affine (Geom::Rotate (star->randomized * M_PI * rnd (seed, 3)));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret *= ( 1 + star->randomized * rnd (seed, 4));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // the randomized corner point
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Point o_randomized = sp_star_get_xy (star, point, index, true);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return o_randomized + ret;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#define NEXT false
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh#define PREV true
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SPStar::set_shape() {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // perhaps we should convert all our shapes into LPEs without source path
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // and with knotholders for parameters, then this situation will be handled automatically
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // by disabling the entire stack (including the shape LPE)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (hasBrokenPathEffect()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_warning ("The star shape has unknown LPE on it! Convert to path to make it editable preserving the appearance; editing it as star will remove the bad LPE");
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (this->getRepr()->attribute("d")) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // unconditionally read the curve from d, if any, to preserve appearance
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::PathVector pv = sp_svg_read_pathv(this->getRepr()->attribute("d"));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPCurve *cold = new SPCurve(pv);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->setCurveInsync( cold, TRUE);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->setCurveBeforeLPE(cold);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh cold->unref();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPCurve *c = new SPCurve ();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh bool not_rounded = (fabs (this->rounded) < 1e-4);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // note that we pass randomized=true to sp_star_get_xy, because the curve must be randomized;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // other places that call that function (e.g. the knotholder) need the exact point
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // draw 1st segment
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->moveto(sp_star_get_xy (this, SP_STAR_POINT_KNOT1, 0, true));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (this->flatsided == false) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (not_rounded) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->lineto(sp_star_get_xy (this, SP_STAR_POINT_KNOT2, 0, true));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, 0, NEXT),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, 0, PREV),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_star_get_xy (this, SP_STAR_POINT_KNOT2, 0, true));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // draw all middle segments
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh for (gint i = 1; i < sides; i++) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (not_rounded) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->lineto(sp_star_get_xy (this, SP_STAR_POINT_KNOT1, i, true));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (this->flatsided == false) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, i - 1, NEXT),
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i, PREV),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_star_get_xy (this, SP_STAR_POINT_KNOT1, i, true));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i - 1, NEXT),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i, PREV),
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh sp_star_get_xy (this, SP_STAR_POINT_KNOT1, i, true));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (this->flatsided == false) {
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (not_rounded) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->lineto(sp_star_get_xy (this, SP_STAR_POINT_KNOT2, i, true));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, i, NEXT),
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, i, PREV),
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_star_get_xy (this, SP_STAR_POINT_KNOT2, i, true));
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh // draw last segment
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (!not_rounded) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (this->flatsided == false) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT2, sides - 1, NEXT),
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, 0, PREV),
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_star_get_xy (this, SP_STAR_POINT_KNOT1, 0, true));
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh } else {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh c->curveto(sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, sides - 1, NEXT),
d27f5758e12c3107ee69e66702043931e0756f6bmiklosh sp_star_get_curvepoint (this, SP_STAR_POINT_KNOT1, 0, PREV),
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_star_get_xy (this, SP_STAR_POINT_KNOT1, 0, true));
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh c->closepath();
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh /* Reset the shape'scurve to the "original_curve"
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh this->setCurveInsync( c, TRUE);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh this->setCurveBeforeLPE( c );
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (hasPathEffect() && pathEffectsEnabled()) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh SPCurve *c_lpe = c->copy();
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh bool success = this->performPathEffect(c_lpe);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh if (success) {
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_lpe_item_apply_to_mask(this);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh sp_lpe_item_apply_to_clippath(this);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh this->setCurveInsync( c_lpe, TRUE);
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh }
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh c_lpe->unref();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh c->unref();
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshsp_star_position_set (SPStar *star, gint sides, Geom::Point center, gdouble r1, gdouble r2, gdouble arg1, gdouble arg2, bool isflat, double rounded, double randomized)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_return_if_fail (star != NULL);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh g_return_if_fail (SP_IS_STAR (star));
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh star->sides = CLAMP(sides, 3, 1024);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->center = center;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->r[0] = MAX (r1, 0.001);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (isflat == false) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->r[1] = CLAMP(r2, 0.0, star->r[0]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
920fbbf5386a5d3b1d0a1304cb71eb48112abe0dmiklosh star->r[1] = CLAMP( r1*cos(M_PI/sides) ,0.0, star->r[0] );
920fbbf5386a5d3b1d0a1304cb71eb48112abe0dmiklosh }
920fbbf5386a5d3b1d0a1304cb71eb48112abe0dmiklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->arg[0] = arg1;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->arg[1] = arg2;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->flatsided = isflat;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh star->rounded = rounded;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->randomized = randomized;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh star->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshvoid SPStar::snappoints(std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs) const {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // We will determine the star's midpoint ourselves, instead of trusting on the base class
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Therefore snapping to object midpoints is temporarily disabled
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Inkscape::SnapPreferences local_snapprefs = *snapprefs;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh local_snapprefs.setTargetSnappable(Inkscape::SNAPTARGET_OBJECT_MIDPOINT, false);
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // CPPIFY: see header file
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh SPShape::snappoints(p, &local_snapprefs);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_OBJECT_MIDPOINT)) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Affine const i2dt (this->i2dt_affine ());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh p.push_back(Inkscape::SnapCandidatePoint(this->center * i2dt,Inkscape::SNAPSOURCE_OBJECT_MIDPOINT, Inkscape::SNAPTARGET_OBJECT_MIDPOINT));
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh}
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshGeom::Affine SPStar::set_transform(Geom::Affine const &xform)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Only set transform with proportional scaling
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (!xform.withoutTranslation().isUniformScale()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return xform;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh // Allow live effects
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh if (hasPathEffect() && pathEffectsEnabled()) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh return xform;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh /* Calculate star start in parent coords. */
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh Geom::Point pos( this->center * xform );
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* This function takes care of translation and scaling, we return whatever parts we can't
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh handle. */
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh Geom::Affine ret(Geom::Affine(xform).withoutTranslation());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh gdouble const s = hypot(ret[0], ret[1]);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh if (s > 1e-9) {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret[0] /= s;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret[1] /= s;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret[2] /= s;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret[3] /= s;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh } else {
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret[0] = 1.0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh ret[1] = 0.0;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh ret[2] = 0.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh ret[3] = 1.0;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh }
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh this->r[0] *= s;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->r[1] *= s;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh /* Find start in item coords */
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh pos = pos * ret.inverse();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->center = pos;
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->set_shape();
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Adjust stroke width
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->adjust_stroke(s);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Adjust pattern fill
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->adjust_pattern(xform * ret.inverse());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh // Adjust gradient fill
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh this->adjust_gradient(xform * ret.inverse());
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh return ret;
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh}
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh/**
3686c32a570c3df738a09b34e85fc5d6bd50d020miklosh * sp_star_get_xy: Get X-Y value as item coordinate system
3711b3e25395437ee0a09dbbb2a76d999c4ef322miklosh * @star: star item
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh * @point: point type to obtain X-Y value
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * @index: index of vertex
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * @p: pointer to store X-Y value
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * @randomized: false (default) if you want to get exact, not randomized point
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh *
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh * Initial item coordinate system is same as document coordinate system.
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh */
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshGeom::Point
7a7fa095a483e8b652af9f00e5169f62c84f09b9mikloshsp_star_get_xy (SPStar const *star, SPStarPoint point, gint index, bool randomized)
7a7fa095a483e8b652af9f00e5169f62c84f09b9miklosh{
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh gdouble darg = 2.0 * M_PI / (double) star->sides;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh double arg = star->arg[point];
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh arg += index * darg;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh Geom::Point xy = star->r[point] * Geom::Point(cos(arg), sin(arg)) + star->center;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh if (!randomized || star->randomized == 0) {
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // return the exact point
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh return xy;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh } else { // randomize the point
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // find out the seed, unique for this point so that randomization is the same so long as the original point is stationary
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh guint32 seed = point_unique_int (xy);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // the full range (corresponding to star->randomized == 1.0) is equal to the star's diameter
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh double range = 2 * MAX (star->r[0], star->r[1]);
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // find out the random displacement; x is controlled by step 1 from the seed, y by the step 2
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh Geom::Point shift (star->randomized * range * rnd (seed, 1), star->randomized * range * rnd (seed, 2));
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh // add the shift to the exact point
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh return xy + shift;
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh }
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh}
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh/*
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh Local Variables:
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh mode:c++
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh c-file-style:"stroustrup"
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh indent-tabs-mode:nil
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh fill-column:99
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh End:
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh*/
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
dc4f69a188c203f2fdc65f22d0d57904a8c52dd7miklosh