sp-line.cpp revision 4bd9cc13b0264b6577fd94bbbd737c4608b3466d
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * SVG <line> implementation
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Authors:
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Lauris Kaplinski <lauris@kaplinski.com>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Abhishek Sharma
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Jon A. Cruz <jon@joncruz.org>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Copyright (C) 1999-2002 Lauris Kaplinski
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Released under GNU GPL, read the file 'COPYING' for more information
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#ifdef HAVE_CONFIG_H
9512fe850e98fdd448c638ca63fdd92a8a510255ahl# include "config.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#endif
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "attributes.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "style.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "sp-line.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "sp-guide.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "display/curve.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include <glibmm/i18n.h>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "xml/repr.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "document.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "inkscape.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlSPShapeClass * SPLineClass::static_parent_class = 0;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlGType SPLine::sp_line_get_type(void)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl static GType line_type = 0;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (!line_type) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl GTypeInfo line_info = {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sizeof(SPLineClass),
9512fe850e98fdd448c638ca63fdd92a8a510255ahl NULL, /* base_init */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl NULL, /* base_finalize */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl (GClassInitFunc) SPLineClass::sp_line_class_init,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl NULL, /* klass_finalize */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl NULL, /* klass_data */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sizeof(SPLine),
9512fe850e98fdd448c638ca63fdd92a8a510255ahl 16, /* n_preallocs */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl (GInstanceInitFunc) init,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl NULL, /* value_table */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl };
9512fe850e98fdd448c638ca63fdd92a8a510255ahl line_type = g_type_register_static(SP_TYPE_SHAPE, "SPLine", &line_info,(GTypeFlags)0);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl return line_type;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlvoid SPLineClass::sp_line_class_init(SPLineClass *klass)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl SPLineClass::static_parent_class = (SPShapeClass *) g_type_class_ref(SP_TYPE_SHAPE);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl SPObjectClass *sp_object_class = (SPObjectClass *) klass;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sp_object_class->build = SPLine::build;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sp_object_class->set = SPLine::set;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sp_object_class->write = SPLine::write;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl SPItemClass *item_class = (SPItemClass *) klass;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl item_class->description = SPLine::getDescription;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl item_class->set_transform = SPLine::setTransform;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl item_class->convert_to_guides = SPLine::convertToGuides;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sp_object_class->update = SPLine::update;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl SPShapeClass *shape_class = (SPShapeClass *) klass;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl //shape_class->set_shape = SPLine::setShape;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}
CLine::CLine(SPLine* line) : CShape(line) {
this->spline = line;
}
CLine::~CLine() {
}
void SPLine::init(SPLine * line)
{
line->cline = new CLine(line);
line->cshape = line->cline;
line->clpeitem = line->cline;
line->citem = line->cline;
line->cobject = line->cline;
line->x1.unset();
line->y1.unset();
line->x2.unset();
line->y2.unset();
}
void CLine::onBuild(SPDocument * document, Inkscape::XML::Node * repr) {
SPLine* object = this->spline;
CShape::onBuild(document, repr);
object->readAttr( "x1" );
object->readAttr( "y1" );
object->readAttr( "x2" );
object->readAttr( "y2" );
}
// CPPIFY: remove
void SPLine::build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
{
((SPLine*)object)->cline->onBuild(document, repr);
}
void CLine::onSet(unsigned int key, const gchar* value) {
SPLine* object = this->spline;
SPLine * line = object;
/* fixme: we should really collect updates */
switch (key) {
case SP_ATTR_X1:
line->x1.readOrUnset(value);
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_Y1:
line->y1.readOrUnset(value);
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_X2:
line->x2.readOrUnset(value);
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
break;
case SP_ATTR_Y2:
line->y2.readOrUnset(value);
object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
break;
default:
CShape::onSet(key, value);
break;
}
}
// CPPIFY: remove
void SPLine::set(SPObject *object, unsigned int key, const gchar *value)
{
((SPLine*)object)->cline->onSet(key, value);
}
void CLine::onUpdate(SPCtx *ctx, guint flags) {
SPLine* object = this->spline;
if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
SPLine *line = SP_LINE(object);
SPStyle const *style = object->style;
SPItemCtx const *ictx = (SPItemCtx const *) ctx;
double const w = ictx->viewport.width();
double const h = ictx->viewport.height();
double const em = style->font_size.computed;
double const ex = em * 0.5; // fixme: get from pango or libnrtype.
line->x1.update(em, ex, w);
line->x2.update(em, ex, w);
line->y1.update(em, ex, h);
line->y2.update(em, ex, h);
((SPShape *) object)->setShape();
}
CShape::onUpdate(ctx, flags);
}
// CPPIFY: remove
void SPLine::update(SPObject *object, SPCtx *ctx, guint flags)
{
((SPLine*)object)->cline->onUpdate(ctx, flags);
}
Inkscape::XML::Node* CLine::onWrite(Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags) {
SPLine* object = this->spline;
SPLine *line = object;
if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
repr = xml_doc->createElement("svg:line");
}
if (repr != object->getRepr()) {
repr->mergeFrom(object->getRepr(), "id");
}
sp_repr_set_svg_double(repr, "x1", line->x1.computed);
sp_repr_set_svg_double(repr, "y1", line->y1.computed);
sp_repr_set_svg_double(repr, "x2", line->x2.computed);
sp_repr_set_svg_double(repr, "y2", line->y2.computed);
CShape::onWrite(xml_doc, repr, flags);
return repr;
}
// CPPIFY: remove
Inkscape::XML::Node * SPLine::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
{
return ((SPLine*)object)->cline->onWrite(xml_doc, repr, flags);
}
gchar* CLine::onDescription() {
return g_strdup(_("<b>Line</b>"));
}
// CPPIFY: remove
gchar * SPLine::getDescription(SPItem *item)
{
return ((SPLine*)item)->cline->onDescription();
}
void CLine::onConvertToGuides() {
SPLine* item = this->spline;
SPLine *line = item;
Geom::Point points[2];
Geom::Affine const i2dt(item->i2dt_affine());
points[0] = Geom::Point(line->x1.computed, line->y1.computed)*i2dt;
points[1] = Geom::Point(line->x2.computed, line->y2.computed)*i2dt;
SPGuide::createSPGuide(item->document, points[0], points[1]);
}
// CPPIFY: remove
void SPLine::convertToGuides(SPItem *item)
{
((SPLine*)item)->cline->onConvertToGuides();
}
Geom::Affine CLine::onSetTransform(Geom::Affine const &transform) {
SPLine* item = this->spline;
SPLine *line = item;
Geom::Point points[2];
points[0] = Geom::Point(line->x1.computed, line->y1.computed);
points[1] = Geom::Point(line->x2.computed, line->y2.computed);
points[0] *= transform;
points[1] *= transform;
line->x1.computed = points[0][Geom::X];
line->y1.computed = points[0][Geom::Y];
line->x2.computed = points[1][Geom::X];
line->y2.computed = points[1][Geom::Y];
item->adjust_stroke(transform.descrim());
SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
return Geom::identity();
}
// CPPIFY: remove
Geom::Affine SPLine::setTransform(SPItem *item, Geom::Affine const &xform)
{
return ((SPLine*)item)->cline->onSetTransform(xform);
}
void CLine::onSetShape() {
SPLine* shape = this->spline;
SPLine *line = shape;
SPCurve *c = new SPCurve();
c->moveto(line->x1.computed, line->y1.computed);
c->lineto(line->x2.computed, line->y2.computed);
shape->setCurveInsync(c, TRUE); // *_insync does not call update, avoiding infinite recursion when set_shape is called by update
shape->setCurveBeforeLPE(c);
// LPE's cannot be applied to lines. (the result can (generally) not be represented as SPLine)
c->unref();
}
// CPPIFY: remove
void SPLine::setShape(SPShape *shape)
{
((SPLine*)shape)->cline->onSetShape();
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :