diff options
| author | Jabier Arraiza Cenoz <jabier.arraiza@marker.es> | 2013-09-19 22:33:11 +0000 |
|---|---|---|
| committer | Jabiertxof <jtx@jtx.marker.es> | 2013-09-19 22:33:11 +0000 |
| commit | 4bda89e32e33c7bdff5d3ea3c1ceee1f806de9f7 (patch) | |
| tree | caeb924426bcc861badc6fa81318b67460b26d47 /src/sp-ellipse.cpp | |
| parent | Update to trunk (diff) | |
| parent | updates for cmake (diff) | |
| download | inkscape-4bda89e32e33c7bdff5d3ea3c1ceee1f806de9f7.tar.gz inkscape-4bda89e32e33c7bdff5d3ea3c1ceee1f806de9f7.zip | |
Update to trunk
(bzr r11950.1.141)
Diffstat (limited to 'src/sp-ellipse.cpp')
| -rw-r--r-- | src/sp-ellipse.cpp | 608 |
1 files changed, 263 insertions, 345 deletions
diff --git a/src/sp-ellipse.cpp b/src/sp-ellipse.cpp index bf019fb13..7c6066054 100644 --- a/src/sp-ellipse.cpp +++ b/src/sp-ellipse.cpp @@ -35,6 +35,28 @@ #define noELLIPSE_VERBOSE + +#include "sp-factory.h" + +namespace { + SPObject* createEllipse() { + return new SPEllipse(); + } + + SPObject* createCircle() { + return new SPCircle(); + } + + SPObject* createArc() { + return new SPArc(); + } + + bool ellipseRegistered = SPFactory::instance().registerObject("svg:ellipse", createEllipse); + bool circleRegistered = SPFactory::instance().registerObject("svg:circle", createCircle); + bool arcRegistered = SPFactory::instance().registerObject("arc", createArc); +} + + #ifndef M_PI #define M_PI 3.14159265358979323846 #endif @@ -64,83 +86,51 @@ static double sp_round(double x, double y) } #endif -static void sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags); - -static void sp_genericellipse_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs); - -static void sp_genericellipse_set_shape(SPShape *shape); -static void sp_genericellipse_update_patheffect (SPLPEItem *lpeitem, bool write); - -static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, - guint flags); - static gboolean sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr); -G_DEFINE_TYPE(SPGenericEllipse, sp_genericellipse, SP_TYPE_SHAPE); - -static void sp_genericellipse_class_init(SPGenericEllipseClass *klass) -{ - SPObjectClass *sp_object_class = (SPObjectClass *) klass; - SPItemClass *item_class = (SPItemClass *) klass; - SPLPEItemClass *lpe_item_class = (SPLPEItemClass *) klass; - SPShapeClass *shape_class = (SPShapeClass *) klass; - - sp_object_class->update = sp_genericellipse_update; - sp_object_class->write = sp_genericellipse_write; +SPGenericEllipse::SPGenericEllipse() : SPShape() { + this->cx.unset(); + this->cy.unset(); + this->rx.unset(); + this->ry.unset(); - item_class->snappoints = sp_genericellipse_snappoints; - - shape_class->set_shape = sp_genericellipse_set_shape; - lpe_item_class->update_patheffect = sp_genericellipse_update_patheffect; + this->start = 0.0; + this->end = SP_2PI; + this->closed = TRUE; } -static void -sp_genericellipse_init(SPGenericEllipse *ellipse) -{ - ellipse->cx.unset(); - ellipse->cy.unset(); - ellipse->rx.unset(); - ellipse->ry.unset(); - - ellipse->start = 0.0; - ellipse->end = SP_2PI; - ellipse->closed = TRUE; +SPGenericEllipse::~SPGenericEllipse() { } -static void -sp_genericellipse_update(SPObject *object, SPCtx *ctx, guint flags) -{ +void SPGenericEllipse::update(SPCtx *ctx, guint flags) { if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) { - SPGenericEllipse *ellipse = (SPGenericEllipse *) object; - SPStyle const *style = object->style; Geom::Rect const &viewbox = ((SPItemCtx const *) ctx)->viewport; double const dx = viewbox.width(); double const dy = viewbox.height(); double const dr = sqrt(dx*dx + dy*dy)/sqrt(2); - double const em = style->font_size.computed; + double const em = this->style->font_size.computed; double const ex = em * 0.5; // fixme: get from pango or libnrtype - ellipse->cx.update(em, ex, dx); - ellipse->cy.update(em, ex, dy); - ellipse->rx.update(em, ex, dr); - ellipse->ry.update(em, ex, dr); - static_cast<SPShape *>(object)->setShape(); + + this->cx.update(em, ex, dx); + this->cy.update(em, ex, dy); + this->rx.update(em, ex, dr); + this->ry.update(em, ex, dr); + + this->set_shape(); } - if (((SPObjectClass *) sp_genericellipse_parent_class)->update) - ((SPObjectClass *) sp_genericellipse_parent_class)->update(object, ctx, flags); + SPShape::update(ctx, flags); } -static void -sp_genericellipse_update_patheffect(SPLPEItem *lpeitem, bool write) -{ - SPShape *shape = (SPShape *) lpeitem; - sp_genericellipse_set_shape(shape); +void SPGenericEllipse::update_patheffect(bool write) { + this->set_shape(); if (write) { - Inkscape::XML::Node *repr = shape->getRepr(); - if ( shape->_curve != NULL ) { - gchar *str = sp_svg_write_path(shape->_curve->get_pathvector()); + Inkscape::XML::Node *repr = this->getRepr(); + + if ( this->_curve != NULL ) { + gchar *str = sp_svg_write_path(this->_curve->get_pathvector()); repr->setAttribute("d", str); g_free(str); } else { @@ -148,22 +138,23 @@ sp_genericellipse_update_patheffect(SPLPEItem *lpeitem, bool write) } } - ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); } /* fixme: Think (Lauris) */ /* Can't we use arcto in this method? */ -static void sp_genericellipse_set_shape(SPShape *shape) -{ - if (sp_lpe_item_has_broken_path_effect(SP_LPE_ITEM(shape))) { +void SPGenericEllipse::set_shape() { + if (sp_lpe_item_has_broken_path_effect(this)) { g_warning ("The ellipse shape has unknown LPE on it! Convert to path to make it editable preserving the appearance; editing it as ellipse will remove the bad LPE"); - if (shape->getRepr()->attribute("d")) { + + if (this->getRepr()->attribute("d")) { // unconditionally read the curve from d, if any, to preserve appearance - Geom::PathVector pv = sp_svg_read_pathv(shape->getRepr()->attribute("d")); + Geom::PathVector pv = sp_svg_read_pathv(this->getRepr()->attribute("d")); SPCurve *cold = new SPCurve(pv); - shape->setCurveInsync( cold, TRUE); + this->setCurveInsync( cold, TRUE); cold->unref(); } + return; } @@ -173,38 +164,43 @@ static void sp_genericellipse_set_shape(SPShape *shape) double len; gint slice = FALSE; - SPGenericEllipse *ellipse = (SPGenericEllipse *) shape; - - if ((ellipse->rx.computed < 1e-18) || (ellipse->ry.computed < 1e-18)) { - return; + if ((this->rx.computed < 1e-18) || (this->ry.computed < 1e-18)) { + return; } - if (fabs(ellipse->end - ellipse->start) < 1e-9){ - return; + + if (fabs(this->end - this->start) < 1e-9) { + return; } - sp_genericellipse_normalize(ellipse); + sp_genericellipse_normalize(this); - rx = ellipse->rx.computed; - ry = ellipse->ry.computed; + rx = this->rx.computed; + ry = this->ry.computed; // figure out if we have a slice, guarding against rounding errors - len = fmod(ellipse->end - ellipse->start, SP_2PI); - if (len < 0.0) len += SP_2PI; + len = fmod(this->end - this->start, SP_2PI); + + if (len < 0.0) { + len += SP_2PI; + } + if (fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8) { slice = FALSE; - ellipse->end = ellipse->start + SP_2PI; + this->end = this->start + SP_2PI; } else { slice = TRUE; } SPCurve * curve = new SPCurve(); - curve->moveto(cos(ellipse->start), sin(ellipse->start)); + curve->moveto(cos(this->start), sin(this->start)); - for (s = ellipse->start; s < ellipse->end; s += M_PI_2) { + for (s = this->start; s < this->end; s += M_PI_2) { double e = s + M_PI_2; - if (e > ellipse->end){ - e = ellipse->end; + + if (e > this->end) { + e = this->end; } + len = 4*tan((e - s)/4)/3; double x0 = cos(s); double y0 = sin(s); @@ -221,65 +217,69 @@ static void sp_genericellipse_set_shape(SPShape *shape) curve->curveto(x1,y1, x2,y2, x3,y3); } - if (slice && ellipse->closed) { // TODO: is this check for "ellipse->closed" necessary? + if (slice && this->closed) { // TODO: is this check for "ellipse->closed" necessary? curve->lineto(0., 0.); } - if (ellipse->closed) { + + if (this->closed) { curve->closepath(); } - Geom::Affine aff = Geom::Scale(rx, ry) * Geom::Translate(ellipse->cx.computed, ellipse->cy.computed); + Geom::Affine aff = Geom::Scale(rx, ry) * Geom::Translate(this->cx.computed, this->cy.computed); curve->transform(aff); /* Reset the shape's curve to the "original_curve" * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/ - shape->setCurveInsync( curve, TRUE); - shape->setCurveBeforeLPE(curve); + this->setCurveInsync( curve, TRUE); + this->setCurveBeforeLPE(curve); - if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) { + if (sp_lpe_item_has_path_effect(this) && sp_lpe_item_path_effects_enabled(this)) { SPCurve *c_lpe = curve->copy(); - bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe); + bool success = sp_lpe_item_perform_path_effect(this, c_lpe); + if (success) { - shape->setCurveInsync( c_lpe, TRUE); + this->setCurveInsync( c_lpe, TRUE); } + c_lpe->unref(); } + curve->unref(); } -static void sp_genericellipse_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs) -{ - g_assert(item != NULL); - g_assert(SP_IS_GENERICELLIPSE(item)); - - SPGenericEllipse *ellipse = SP_GENERICELLIPSE(item); - sp_genericellipse_normalize(ellipse); - Geom::Affine const i2dt = item->i2dt_affine(); +void SPGenericEllipse::snappoints(std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs) { + sp_genericellipse_normalize(this); + Geom::Affine const i2dt = this->i2dt_affine(); // figure out if we have a slice, while guarding against rounding errors bool slice = false; - double len = fmod(ellipse->end - ellipse->start, SP_2PI); - if (len < 0.0) len += SP_2PI; + double len = fmod(this->end - this->start, SP_2PI); + + if (len < 0.0) { + len += SP_2PI; + } + if (fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8) { slice = false; - ellipse->end = ellipse->start + SP_2PI; + this->end = this->start + SP_2PI; } else { slice = true; } - double rx = ellipse->rx.computed; - double ry = ellipse->ry.computed; - double cx = ellipse->cx.computed; - double cy = ellipse->cy.computed; + double rx = this->rx.computed; + double ry = this->ry.computed; + double cx = this->cx.computed; + double cy = this->cy.computed; Geom::Point pt; - // Snap to the 4 quadrant points of the ellipse, but only if the arc + // Snap to the 4 quadrant points of the this, but only if the arc // spans far enough to include them if (snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_ELLIPSE_QUADRANT_POINT)) { double angle = 0; + for (angle = 0; angle < SP_2PI; angle += M_PI_2) { - if (angle >= ellipse->start && angle <= ellipse->end) { + if (angle >= this->start && angle <= this->end) { pt = Geom::Point(cx + cos(angle)*rx, cy + sin(angle)*ry) * i2dt; p.push_back(Inkscape::SnapCandidatePoint(pt, Inkscape::SNAPSOURCE_ELLIPSE_QUADRANT_POINT, Inkscape::SNAPTARGET_ELLIPSE_QUADRANT_POINT)); } @@ -287,13 +287,16 @@ static void sp_genericellipse_snappoints(SPItem const *item, std::vector<Inkscap } // Add the centre, if we have a closed slice or when explicitly asked for - bool c1 = snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_NODE_CUSP) && slice && ellipse->closed; + bool c1 = snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_NODE_CUSP) && slice && this->closed; bool c2 = snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_OBJECT_MIDPOINT); + if (c1 || c2) { pt = Geom::Point(cx, cy) * i2dt; + if (c1) { p.push_back(Inkscape::SnapCandidatePoint(pt, Inkscape::SNAPSOURCE_NODE_CUSP, Inkscape::SNAPTARGET_NODE_CUSP)); } + if (c2) { p.push_back(Inkscape::SnapCandidatePoint(pt, Inkscape::SNAPSOURCE_OBJECT_MIDPOINT, Inkscape::SNAPTARGET_OBJECT_MIDPOINT)); } @@ -302,13 +305,14 @@ static void sp_genericellipse_snappoints(SPItem const *item, std::vector<Inkscap // And if we have a slice, also snap to the endpoints if (snapprefs->isTargetSnappable(Inkscape::SNAPTARGET_NODE_CUSP) && slice) { // Add the start point, if it's not coincident with a quadrant point - if (fmod(ellipse->start, M_PI_2) != 0.0 ) { - pt = Geom::Point(cx + cos(ellipse->start)*rx, cy + sin(ellipse->start)*ry) * i2dt; + if (fmod(this->start, M_PI_2) != 0.0 ) { + pt = Geom::Point(cx + cos(this->start)*rx, cy + sin(this->start)*ry) * i2dt; p.push_back(Inkscape::SnapCandidatePoint(pt, Inkscape::SNAPSOURCE_NODE_CUSP, Inkscape::SNAPTARGET_NODE_CUSP)); } + // Add the end point, if it's not coincident with a quadrant point - if (fmod(ellipse->end, M_PI_2) != 0.0 ) { - pt = Geom::Point(cx + cos(ellipse->end)*rx, cy + sin(ellipse->end)*ry) * i2dt; + if (fmod(this->end, M_PI_2) != 0.0 ) { + pt = Geom::Point(cx + cos(this->end)*rx, cy + sin(this->end)*ry) * i2dt; p.push_back(Inkscape::SnapCandidatePoint(pt, Inkscape::SNAPSOURCE_NODE_CUSP, Inkscape::SNAPTARGET_NODE_CUSP)); } } @@ -320,141 +324,111 @@ sp_genericellipse_normalize(SPGenericEllipse *ellipse) ellipse->start = fmod(ellipse->start, SP_2PI); ellipse->end = fmod(ellipse->end, SP_2PI); - if (ellipse->start < 0.0) + if (ellipse->start < 0.0) { ellipse->start += SP_2PI; + } + double diff = ellipse->start - ellipse->end; - if (diff >= 0.0) + + if (diff >= 0.0) { ellipse->end += diff - fmod(diff, SP_2PI) + SP_2PI; + } /* Now we keep: 0 <= start < end <= 2*PI */ } -static Inkscape::XML::Node *sp_genericellipse_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) -{ - SPGenericEllipse *ellipse = SP_GENERICELLIPSE(object); - +Inkscape::XML::Node* SPGenericEllipse::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) { if (flags & SP_OBJECT_WRITE_EXT) { if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { repr = xml_doc->createElement("svg:path"); } - sp_repr_set_svg_double(repr, "sodipodi:cx", ellipse->cx.computed); - sp_repr_set_svg_double(repr, "sodipodi:cy", ellipse->cy.computed); - sp_repr_set_svg_double(repr, "sodipodi:rx", ellipse->rx.computed); - sp_repr_set_svg_double(repr, "sodipodi:ry", ellipse->ry.computed); + sp_repr_set_svg_double(repr, "sodipodi:cx", this->cx.computed); + sp_repr_set_svg_double(repr, "sodipodi:cy", this->cy.computed); + sp_repr_set_svg_double(repr, "sodipodi:rx", this->rx.computed); + sp_repr_set_svg_double(repr, "sodipodi:ry", this->ry.computed); - if (SP_IS_ARC(ellipse)) { - sp_arc_set_elliptical_path_attribute(SP_ARC(object), object->getRepr()); + if (SP_IS_ARC(this)) { + sp_arc_set_elliptical_path_attribute(SP_ARC(this), this->getRepr()); } } - sp_genericellipse_set_shape ((SPShape *) ellipse); // evaluate SPCurve - if (((SPObjectClass *) sp_genericellipse_parent_class)->write) { - ((SPObjectClass *) sp_genericellipse_parent_class)->write(object, xml_doc, repr, flags); - } + this->set_shape(); // evaluate SPCurve + + SPShape::write(xml_doc, repr, flags); return repr; } /* SVG <ellipse> element */ - -static void sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr); -static Inkscape::XML::Node *sp_ellipse_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags); -static void sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value); -static gchar *sp_ellipse_description(SPItem *item); - -G_DEFINE_TYPE(SPEllipse, sp_ellipse, SP_TYPE_GENERICELLIPSE); - -static void sp_ellipse_class_init(SPEllipseClass *klass) -{ - SPObjectClass *sp_object_class = (SPObjectClass *) klass; - SPItemClass *item_class = (SPItemClass *) klass; - - sp_object_class->build = sp_ellipse_build; - sp_object_class->write = sp_ellipse_write; - sp_object_class->set = sp_ellipse_set; - - item_class->description = sp_ellipse_description; +SPEllipse::SPEllipse() : SPGenericEllipse() { } -static void -sp_ellipse_init(SPEllipse */*ellipse*/) -{ - /* Nothing special */ +SPEllipse::~SPEllipse() { } -static void -sp_ellipse_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr) -{ - if (((SPObjectClass *) sp_ellipse_parent_class)->build) - (* ((SPObjectClass *) sp_ellipse_parent_class)->build) (object, document, repr); +void SPEllipse::build(SPDocument *document, Inkscape::XML::Node *repr) { + SPGenericEllipse::build(document, repr); - object->readAttr( "cx" ); - object->readAttr( "cy" ); - object->readAttr( "rx" ); - object->readAttr( "ry" ); + this->readAttr( "cx" ); + this->readAttr( "cy" ); + this->readAttr( "rx" ); + this->readAttr( "ry" ); } -static Inkscape::XML::Node * -sp_ellipse_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) -{ - SPGenericEllipse *ellipse; - - ellipse = SP_GENERICELLIPSE(object); +Inkscape::XML::Node* SPEllipse::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) { if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { repr = xml_doc->createElement("svg:ellipse"); } - sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed); - sp_repr_set_svg_double(repr, "cy", ellipse->cy.computed); - sp_repr_set_svg_double(repr, "rx", ellipse->rx.computed); - sp_repr_set_svg_double(repr, "ry", ellipse->ry.computed); + sp_repr_set_svg_double(repr, "cx", this->cx.computed); + sp_repr_set_svg_double(repr, "cy", this->cy.computed); + sp_repr_set_svg_double(repr, "rx", this->rx.computed); + sp_repr_set_svg_double(repr, "ry", this->ry.computed); - if (((SPObjectClass *) sp_ellipse_parent_class)->write) - (* ((SPObjectClass *) sp_ellipse_parent_class)->write) (object, xml_doc, repr, flags); + SPGenericEllipse::write(xml_doc, repr, flags); return repr; } -static void -sp_ellipse_set(SPObject *object, unsigned int key, gchar const *value) -{ - SPGenericEllipse *ellipse; - - ellipse = SP_GENERICELLIPSE(object); +void SPEllipse::set(unsigned int key, gchar const* value) { switch (key) { case SP_ATTR_CX: - ellipse->cx.readOrUnset(value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->cx.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_CY: - ellipse->cy.readOrUnset(value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->cy.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_RX: - if (!ellipse->rx.read(value) || (ellipse->rx.value <= 0.0)) { - ellipse->rx.unset(); + if (!this->rx.read(value) || (this->rx.value <= 0.0)) { + this->rx.unset(); } - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_RY: - if (!ellipse->ry.read(value) || (ellipse->ry.value <= 0.0)) { - ellipse->ry.unset(); + if (!this->ry.read(value) || (this->ry.value <= 0.0)) { + this->ry.unset(); } - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + default: - if (((SPObjectClass *) sp_ellipse_parent_class)->set) - ((SPObjectClass *) sp_ellipse_parent_class)->set(object, key, value); + SPGenericEllipse::set(key, value); break; } } -static gchar *sp_ellipse_description(SPItem */*item*/) -{ - return g_strdup(_("<b>Ellipse</b>")); +gchar* SPEllipse::description() { + return g_strdup(_("<b>Ellipse</b>")); } @@ -477,145 +451,84 @@ sp_ellipse_position_set(SPEllipse *ellipse, gdouble x, gdouble y, gdouble rx, gd } /* SVG <circle> element */ - -static void sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr); -static Inkscape::XML::Node *sp_circle_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags); -static void sp_circle_set(SPObject *object, unsigned int key, gchar const *value); -static gchar *sp_circle_description(SPItem *item); - -G_DEFINE_TYPE(SPCircle, sp_circle, SP_TYPE_GENERICELLIPSE); - -static void -sp_circle_class_init(SPCircleClass *klass) -{ - SPObjectClass *sp_object_class = (SPObjectClass *) klass; - SPItemClass *item_class = (SPItemClass *) klass; - - sp_object_class->build = sp_circle_build; - sp_object_class->write = sp_circle_write; - sp_object_class->set = sp_circle_set; - - item_class->description = sp_circle_description; +SPCircle::SPCircle() : SPGenericEllipse() { } -static void -sp_circle_init(SPCircle */*circle*/) -{ - /* Nothing special */ +SPCircle::~SPCircle() { } -static void -sp_circle_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr) -{ - if (((SPObjectClass *) sp_circle_parent_class)->build) - (* ((SPObjectClass *) sp_circle_parent_class)->build)(object, document, repr); +void SPCircle::build(SPDocument *document, Inkscape::XML::Node *repr) { + SPGenericEllipse::build(document, repr); - object->readAttr( "cx" ); - object->readAttr( "cy" ); - object->readAttr( "r" ); + this->readAttr( "cx" ); + this->readAttr( "cy" ); + this->readAttr( "r" ); } -static Inkscape::XML::Node * -sp_circle_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) -{ - SPGenericEllipse *ellipse; - - ellipse = SP_GENERICELLIPSE(object); +Inkscape::XML::Node* SPCircle::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) { if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { repr = xml_doc->createElement("svg:circle"); } - sp_repr_set_svg_double(repr, "cx", ellipse->cx.computed); - sp_repr_set_svg_double(repr, "cy", ellipse->cy.computed); - sp_repr_set_svg_double(repr, "r", ellipse->rx.computed); + sp_repr_set_svg_double(repr, "cx", this->cx.computed); + sp_repr_set_svg_double(repr, "cy", this->cy.computed); + sp_repr_set_svg_double(repr, "r", this->rx.computed); - if (((SPObjectClass *) sp_circle_parent_class)->write) - ((SPObjectClass *) sp_circle_parent_class)->write(object, xml_doc, repr, flags); + SPGenericEllipse::write(xml_doc, repr, flags); return repr; } -static void -sp_circle_set(SPObject *object, unsigned int key, gchar const *value) -{ - SPGenericEllipse *ge; - - ge = SP_GENERICELLIPSE(object); - +void SPCircle::set(unsigned int key, gchar const* value) { switch (key) { case SP_ATTR_CX: - ge->cx.readOrUnset(value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->cx.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_CY: - ge->cy.readOrUnset(value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->cy.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_R: - if (!ge->rx.read(value) || ge->rx.value <= 0.0) { - ge->rx.unset(); + if (!this->rx.read(value) || this->rx.value <= 0.0) { + this->rx.unset(); } - ge->ry = ge->rx; - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->ry = this->rx; + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + default: - if (((SPObjectClass *) sp_circle_parent_class)->set) - ((SPObjectClass *) sp_circle_parent_class)->set(object, key, value); + SPGenericEllipse::set(key, value); break; } } -static gchar *sp_circle_description(SPItem */*item*/) -{ - return g_strdup(_("<b>Circle</b>")); +gchar* SPCircle::description() { + return g_strdup(_("<b>Circle</b>")); } /* <path sodipodi:type="arc"> element */ - -static void sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr); -static Inkscape::XML::Node *sp_arc_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags); -static void sp_arc_set(SPObject *object, unsigned int key, gchar const *value); -static void sp_arc_modified(SPObject *object, guint flags); - -static gchar *sp_arc_description(SPItem *item); - -G_DEFINE_TYPE(SPArc, sp_arc, SP_TYPE_GENERICELLIPSE); - -static void -sp_arc_class_init(SPArcClass *klass) -{ - SPObjectClass *sp_object_class = (SPObjectClass *) klass; - SPItemClass *item_class = (SPItemClass *) klass; - - sp_object_class->build = sp_arc_build; - sp_object_class->write = sp_arc_write; - sp_object_class->set = sp_arc_set; - sp_object_class->modified = sp_arc_modified; - - item_class->description = sp_arc_description; +SPArc::SPArc() : SPGenericEllipse() { } -static void -sp_arc_init(SPArc */*arc*/) -{ - /* Nothing special */ +SPArc::~SPArc() { } -static void -sp_arc_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr) -{ - if (((SPObjectClass *) sp_arc_parent_class)->build) - (* ((SPObjectClass *) sp_arc_parent_class)->build) (object, document, repr); +void SPArc::build(SPDocument *document, Inkscape::XML::Node *repr) { + SPGenericEllipse::build(document, repr); - object->readAttr( "sodipodi:cx" ); - object->readAttr( "sodipodi:cy" ); - object->readAttr( "sodipodi:rx" ); - object->readAttr( "sodipodi:ry" ); + this->readAttr( "sodipodi:cx" ); + this->readAttr( "sodipodi:cy" ); + this->readAttr( "sodipodi:rx" ); + this->readAttr( "sodipodi:ry" ); - object->readAttr( "sodipodi:start" ); - object->readAttr( "sodipodi:end" ); - object->readAttr( "sodipodi:open" ); + this->readAttr( "sodipodi:start" ); + this->readAttr( "sodipodi:end" ); + this->readAttr( "sodipodi:open" ); } /* @@ -660,30 +573,31 @@ sp_arc_set_elliptical_path_attribute(SPArc *arc, Inkscape::XML::Node *repr) return true; } -static Inkscape::XML::Node * -sp_arc_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) -{ - SPGenericEllipse *ge = SP_GENERICELLIPSE(object); - SPArc *arc = SP_ARC(object); - +Inkscape::XML::Node* SPArc::write(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) { if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) { repr = xml_doc->createElement("svg:path"); } if (flags & SP_OBJECT_WRITE_EXT) { repr->setAttribute("sodipodi:type", "arc"); - sp_repr_set_svg_double(repr, "sodipodi:cx", ge->cx.computed); - sp_repr_set_svg_double(repr, "sodipodi:cy", ge->cy.computed); - sp_repr_set_svg_double(repr, "sodipodi:rx", ge->rx.computed); - sp_repr_set_svg_double(repr, "sodipodi:ry", ge->ry.computed); + + sp_repr_set_svg_double(repr, "sodipodi:cx", this->cx.computed); + sp_repr_set_svg_double(repr, "sodipodi:cy", this->cy.computed); + sp_repr_set_svg_double(repr, "sodipodi:rx", this->rx.computed); + sp_repr_set_svg_double(repr, "sodipodi:ry", this->ry.computed); // write start and end only if they are non-trivial; otherwise remove - gdouble len = fmod(ge->end - ge->start, SP_2PI); - if (len < 0.0) len += SP_2PI; + gdouble len = fmod(this->end - this->start, SP_2PI); + + if (len < 0.0) { + len += SP_2PI; + } + if (!(fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8)) { - sp_repr_set_svg_double(repr, "sodipodi:start", ge->start); - sp_repr_set_svg_double(repr, "sodipodi:end", ge->end); - repr->setAttribute("sodipodi:open", (!ge->closed) ? "true" : NULL); + sp_repr_set_svg_double(repr, "sodipodi:start", this->start); + sp_repr_set_svg_double(repr, "sodipodi:end", this->end); + + repr->setAttribute("sodipodi:open", (!this->closed) ? "true" : NULL); } else { repr->setAttribute("sodipodi:end", NULL); repr->setAttribute("sodipodi:start", NULL); @@ -692,86 +606,90 @@ sp_arc_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML:: } // write d= - sp_arc_set_elliptical_path_attribute(arc, repr); + sp_arc_set_elliptical_path_attribute(this, repr); - if (((SPObjectClass *) sp_arc_parent_class)->write) - ((SPObjectClass *) sp_arc_parent_class)->write(object, xml_doc, repr, flags); + SPGenericEllipse::write(xml_doc, repr, flags); return repr; } -static void -sp_arc_set(SPObject *object, unsigned int key, gchar const *value) -{ - SPGenericEllipse *ge = SP_GENERICELLIPSE(object); - +void SPArc::set(unsigned int key, gchar const* value) { switch (key) { case SP_ATTR_SODIPODI_CX: - ge->cx.readOrUnset(value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->cx.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_SODIPODI_CY: - ge->cy.readOrUnset(value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->cy.readOrUnset(value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_SODIPODI_RX: - if (!ge->rx.read(value) || ge->rx.computed <= 0.0) { - ge->rx.unset(); + if (!this->rx.read(value) || this->rx.computed <= 0.0) { + this->rx.unset(); } - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_SODIPODI_RY: - if (!ge->ry.read(value) || ge->ry.computed <= 0.0) { - ge->ry.unset(); + if (!this->ry.read(value) || this->ry.computed <= 0.0) { + this->ry.unset(); } - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_SODIPODI_START: if (value) { - sp_svg_number_read_d(value, &ge->start); + sp_svg_number_read_d(value, &this->start); } else { - ge->start = 0; + this->start = 0; } - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_SODIPODI_END: if (value) { - sp_svg_number_read_d(value, &ge->end); + sp_svg_number_read_d(value, &this->end); } else { - ge->end = 2 * M_PI; + this->end = 2 * M_PI; } - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + case SP_ATTR_SODIPODI_OPEN: - ge->closed = (!value); - object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + this->closed = (!value); + this->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); break; + default: - if (((SPObjectClass *) sp_arc_parent_class)->set) - ((SPObjectClass *) sp_arc_parent_class)->set(object, key, value); + SPGenericEllipse::set(key, value); break; } } -static void -sp_arc_modified(SPObject *object, guint flags) -{ +void SPArc::modified(guint flags) { if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) { - ((SPShape *) object)->setShape(); + this->set_shape(); } - if (((SPObjectClass *) sp_arc_parent_class)->modified) - ((SPObjectClass *) sp_arc_parent_class)->modified(object, flags); + SPGenericEllipse::modified(flags); } -static gchar *sp_arc_description(SPItem *item) -{ - SPGenericEllipse *ge = SP_GENERICELLIPSE(item); - gdouble len = fmod(ge->end - ge->start, SP_2PI); - if (len < 0.0) len += SP_2PI; +gchar* SPArc::description() { + gdouble len = fmod(this->end - this->start, SP_2PI); + + if (len < 0.0) { + len += SP_2PI; + } + if (!(fabs(len) < 1e-8 || fabs(len - SP_2PI) < 1e-8)) { - if (ge->closed) { + if (this->closed) { return g_strdup(_("<b>Segment</b>")); } else { return g_strdup(_("<b>Arc</b>")); |
