sp-offset.cpp revision a4ba6cf08033d624f247cc38c2b8b75f01ba0531
#define __SP_OFFSET_C__
/** \file
* Implementation of <path sodipodi:type="inkscape:offset">.
*/
/*
* Authors: (of the sp-spiral.c upon which this file was constructed):
* Mitsuru Oka <oka326@parkcity.ne.jp>
* Lauris Kaplinski <lauris@kaplinski.com>
*
* Copyright (C) 1999-2002 Lauris Kaplinski
* Copyright (C) 2000-2001 Ximian, Inc.
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <cstring>
#include <string>
#include "attributes.h"
#include "enums.h"
#include "prefs-utils.h"
#include "sp-text.h"
#include "sp-offset.h"
#include "sp-use-reference.h"
#include "uri.h"
#include "libnr/n-art-bpath.h"
#include <libnr/nr-matrix-fns.h>
class SPDocument;
#define noOFFSET_VERBOSE
/** \note
* SPOffset is a derivative of SPShape, much like the SPSpiral or SPRect.
* The goal is to have a source shape (= originalPath), an offset (= radius)
* and compute the offset of the source by the radius. To get it to work,
* one needs to know what the source is and what the radius is, and how it's
* stored in the xml representation. The object itself is a "path" element,
* to get lots of shape functionality for free. The source is the easy part:
* it's stored in a "inkscape:original" attribute in the path. In case of
* "linked" offset, as they've been dubbed, there is an additional
* "inkscape:href" that contains the id of an element of the svg.
* When built, the object will attach a listener vector to that object and
* rebuild the "inkscape:original" whenever the href'd object changes. This
* is of course grossly inefficient, and also does not react to changes
* to the href'd during context stuff (like changing the shape of a star by
* dragging control points) unless the path of that object is changed during
* the context (seems to be the case for SPEllipse). The computation of the
* offset is done in sp_offset_set_shape(), a function that is called whenever
* a change occurs to the offset (change of source or change of radius).
* just like the sp-star and other, this path derivative can make control
* points, or more precisely one control point, that's enough to define the
* radius (look in object-edit).
*/
static Inkscape::XML::Node *sp_offset_write (SPObject * object, Inkscape::XML::Document *doc, Inkscape::XML::Node * repr,
// slow= source path->polygon->offset of polygon->polygon->path
// fast= source path->offset of source path->polygon->path
// fast is not mathematically correct, because computing the offset of a single
// cubic bezier patch is not trivial; in particular, there are problems with holes
// reappearing in offset when the radius becomes too large
static bool use_slow_but_correct_offset_method=false;
static SPShapeClass *parent_class;
/**
* Register SPOffset class and return its type number.
*/
sp_offset_get_type (void)
{
static GType offset_type = 0;
if (!offset_type)
{
GTypeInfo offset_info = {
sizeof (SPOffsetClass),
NULL, /* base_init */
NULL, /* base_finalize */
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (SPOffset),
16, /* n_preallocs */
NULL, /* value_table */
};
(GTypeFlags) 0);
}
return offset_type;
}
/**
* SPOffset vtable initialization.
*/
static void
{
}
/**
* Callback for SPOffset object initialization.
*/
static void
{
offset->sourceDirty=false;
offset->isUpdating=false;
// init various connections
// set up the uri reference
offset->_changed_connection = offset->sourceRef->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_offset_href_changed), offset));
}
/**
* Callback for SPOffset finalization.
*/
static void
{
}
/**
* Virtual build: set offset attributes from corresponding repr.
*/
static void
{
} else {
}
} else {
}
} else {
if (oldA) {
nA[0]='#';
}
}
}
/**
* Virtual write: write offset attributes to corresponding repr.
*/
sp_offset_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
{
}
if (flags & SP_OBJECT_WRITE_EXT) {
/** \todo
* Fixme: we may replace these attributes by
* inkscape:offset="cx cy exp revo rad arg t0"
*/
}
// Make sure the object has curve
}
// write that curve to "d"
g_free (d);
return repr;
}
/**
* Virtual release callback.
*/
static void
{
}
}
/**
* Set callback: the function that is called whenever a change is made to
* the description of the object.
*/
static void
{
/* fixme: we should really collect updates */
switch (key)
{
} else {
}
}
break;
case SP_ATTR_INKSCAPE_RADIUS:
case SP_ATTR_SODIPODI_RADIUS:
}
break;
case SP_ATTR_INKSCAPE_HREF:
case SP_ATTR_XLINK_HREF:
} else {
} else {
try {
} catch (Inkscape::BadURIException &e) {
}
}
}
break;
default:
break;
}
}
/**
* Update callback: the object has changed, recompute its shape.
*/
static void
{
if (flags &
}
offset->isUpdating=false;
}
/**
* Returns a textual description of object.
*/
static gchar *
{
if ( offset->sourceHref ) {
// TRANSLATORS COMMENT: %s is either "outset" or "inset" depending on sign
return g_strdup_printf(_("<b>Linked offset</b>, %s by %f pt"),
} else {
// TRANSLATORS COMMENT: %s is either "outset" or "inset" depending on sign
return g_strdup_printf(_("<b>Dynamic offset</b>, %s by %f pt"),
}
}
/**
* Converts an NArtBpath (like the one stored in a SPCurve) into a
* livarot Path. Duplicate of splivarot.
*/
Path *
{
return NULL;
dest->SetBackData (false);
{
int i;
bool closed = false;
float lastX = 0.0;
float lastY = 0.0;
{
{
case NR_LINETO:
{
}
break;
case NR_CURVETO:
{
}
break;
case NR_MOVETO_OPEN:
case NR_MOVETO:
if (closed)
{
}
break;
default:
break;
}
}
if (closed)
}
return dest;
}
/**
* Compute and set shape's offset.
*/
static void
{
// oops : no path?! (the offset object should do harakiri)
return;
}
#ifdef OFFSET_VERBOSE
#endif
// au boulot
// grosso modo: 0
// just put the source shape as the offseted one, no one will notice
// it's also useless to compute the offset with a 0 radius
if ( res_d ) {
c->unref();
}
return;
}
// extra paraniac careful check. the preceding if () should take care of this case
if ( use_slow_but_correct_offset_method == false ) {
// version par outline
res->SetBackData (false);
// and now: offset
float o_width;
{
}
else
{
}
if (o_width >= 1.0)
{
// res->ConvertForOffset (1.0, orig, offset->rad);
}
else
{
// res->ConvertForOffset (o_width, orig, offset->rad);
}
if (exp != 0)
//g_print ("coa %g exp %g item %p\n", size * 0.001, exp, item);
}
// if (o_width >= 1.0)
// {
// orig->Coalesce (0.1); // small treshhold, since we only want to get rid of small segments
// the curve should already be computed by the Outline() function
// orig->ConvertEvenLines (1.0);
// orig->Simplify (0.5);
// }
// else
// {
// orig->Coalesce (0.1*o_width);
// orig->ConvertEvenLines (o_width);
// orig->Simplify (0.5 * o_width);
// }
delete theShape;
delete theRes;
delete res;
} else {
// version par makeoffset
// and now: offset
float o_width;
{
}
else
{
}
// one has to have a measure of the details
if (o_width >= 1.0)
{
}
else
{
}
int nbPart=0;
// we offset contours separately, because we can.
// this way, we avoid doing a unique big ConvertToShape when dealing with big shapes with lots of holes
{
for (int i=0;i<nbPart;i++) {
{
// raffiner si besoin
if ( mesure < 10.0 ) {
}
}
if ( partSurf < 0 ) { // inverse par rapport a la realite
// plein
holes[i]=0;
onePart->ConvertToShape(oneCleanPart,fill_positive); // there aren't intersections in that one, but maybe duplicate points and null edges
typicalSize*=0.01;
if ( nPartSurf >= 0 ) {
// inversion de la surface -> disparait
delete parts[i];
} else {
}
/* int firstP=theShape->nbPt;
for (int j=0;j<onePart->nbPt;j++) theShape->AddPoint(onePart->pts[j].x);
for (int j=0;j<onePart->nbAr;j++) theShape->AddEdge(firstP+onePart->aretes[j].st,firstP+onePart->aretes[j].en);*/
} else {
// trou
holes[i]=1;
// for (int j=0;j<onePart->nbAr;j++) onePart->Inverse(j); // pas oublier de reinverser
typicalSize*=0.01;
if ( nPartSurf >= 0 ) {
// inversion de la surface -> disparait
delete parts[i];
} else {
}
/* int firstP=theShape->nbPt;
for (int j=0;j<onePart->nbPt;j++) theShape->AddPoint(onePart->pts[j].x);
for (int j=0;j<onePart->nbAr;j++) theShape->AddEdge(firstP+onePart->aretes[j].en,firstP+onePart->aretes[j].st);*/
}
// delete parts[i];
}
// theShape->MakeOffset(theRes,offset->rad,join_round,20.0);
delete onePart;
delete oneCleanPart;
}
if ( nbPart > 1 ) {
for (int i=0;i<nbPart;i++) {
if ( parts[i] ) {
if ( holes[i] ) {
} else {
}
}
}
} else if ( nbPart == 1 ) {
} else {
}
// theRes->ConvertToShape (theShape, fill_positive);
// theRes->ConvertToForme (orig);
/* if (o_width >= 1.0) {
orig->ConvertEvenLines (1.0);
orig->Simplify (1.0);
} else {
orig->ConvertEvenLines (1.0*o_width);
orig->Simplify (1.0 * o_width);
}*/
delete res;
delete theShape;
delete theRes;
}
{
{
// Aie.... nothing left.
//printf("%s\n",res_d);
}
else
{
}
delete orig;
c->unref();
}
}
/**
* Virtual snappoints function.
*/
{
}
}
// utilitaires pour les poignees
// used to get the distance to the shape: distance to polygon give the fabs(radius), we still need
// the sign. for edges, it's easy to determine which side the point is on, for points of the polygon
// it's trickier: we need to identify which angle the point is in; to that effect, we take each
// successive clockwise angle (A,C) and check if the vector B given by the point is in the angle or
// outside.
// another method would be to use the Winding() function to test whether the point is inside or outside
// the polygon (it would be wiser to do so, in fact, but i like being stupid)
/**
*
* \todo
* FIXME: This can be done using linear operations, more stably and
* faster. method: transform A and C into B's space, A should be
* negative and B should be positive in the orthogonal component. I
* think this is equivalent to
* dot(A, rot90(B))*dot(C, rot90(B)) == -1.
* -- njh
*/
bool
{
if (ab_c <= -1.0)
if (ab_c >= 1.0)
ab_a = 0;
if (ab_s < 0)
if (bc_c <= -1.0)
if (bc_c >= 1.0)
bc_a = 0;
if (bc_s < 0)
if (ca_c <= -1.0)
if (ca_c >= 1.0)
ca_a = 0;
if (ca_s < 0)
return true;
return false;
}
/**
* Distance to the original path; that function is called from object-edit
* to set the radius when the control knot moves.
*
* The sign of the result is the radius we're going to offset the shape with,
* so result > 0 ==outset and result < 0 ==inset. thus result<0 means
* 'px inside source'.
*/
double
{
return 1.0;
double dist = 1.0;
/** \todo
* Awfully damn stupid method: uncross the source path EACH TIME you
* need to compute the distance. The good way to do this would be to
* store the uncrossed source path somewhere, and delete it when the
* context is finished. Hopefully this part is much faster than actually
* computing the offset (which happen just after), so the time spent in
* this function should end up being negligible with respect to the
* delay of one context.
*/
// move
{
}
else
{
double ptDist = -1.0;
bool ptSet = false;
double arDist = -1.0;
bool arSet = false;
// first get the minimum distance to the points
for (int i = 0; i < theRes->numberOfPoints(); i++)
{
{
{
// we have a new minimum distance
// now we need to wheck if px is inside or outside (for the sign)
do
{
// one angle
{
}
{
}
{
// we're in that angle. set the sign, and exit that loop
{
ptSet = true;
}
else
{
ptSet = true;
}
break;
}
}
}
}
}
// loop over the edges to try to improve the distance
for (int i = 0; i < theRes->numberOfEdges(); i++)
{
if (len > 0.0001)
{
{
// we're in the zone of influence of the segment
{
arSet = true;
}
}
}
}
{
if (arSet == false)
if (ptSet == false)
else
}
}
delete theShape;
delete theRes;
return dist;
}
/**
* Computes a point on the offset; used to set a "seed" position for
* the control knot.
*
* \return the topmost point on the offset.
*/
void
{
return;
{
return;
}
{
return;
}
{
return;
}
{
theShape->SortPoints ();
}
delete theShape;
delete finalPath;
}
// the listening functions
{
return;
offset->_delete_connection = SP_OBJECT(to)->connectDelete(sigc::bind(sigc::ptr_fun(&sp_offset_delete_self), offset));
offset->_transformed_connection = SP_ITEM(to)->connectTransformed(sigc::bind(sigc::ptr_fun(&sp_offset_move_compensate), offset));
offset->_modified_connection = SP_OBJECT(to)->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_offset_source_modified), offset));
}
{
return;
}
static void
{
offset->sourceDirty=true;
}
}
static void
{
guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
if (mode == SP_CLONE_COMPENSATION_NONE) return;
if (!(m.is_translation())) return;
// calculate the compensation matrix and the advertized movement matrix
if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
} else if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
compensate = m;
advertized_move = m;
} else {
}
// commit the compensation
}
static void
{
guint const mode = prefs_get_int_attribute("options.cloneorphans", "value", SP_CLONE_ORPHANS_UNLINK);
if (mode == SP_CLONE_ORPHANS_UNLINK) {
// leave it be. just forget about the source
} else if (mode == SP_CLONE_ORPHANS_DELETE) {
}
}
static void
{
offset->sourceDirty=true;
}
static void
{
offset->sourceDirty=false;
// le mauvais cas: pas d'attribut d => il faut verifier que c'est une SPShape puis prendre le contour
// The bad case: no d attribute. Must check that it's an SPShape and then take the outline.
if (SP_IS_SHAPE (item)) {
return;
}
if (SP_IS_TEXT (item)) {
return;
}
// Finish up.
{
{
}
{
}
else
{
}
delete theShape;
delete theRes;
delete res;
delete orig;
}
}
SPItem *
{
if (SP_IS_ITEM (refobj))
}
return NULL;
}
/*
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:encoding=utf-8:textwidth=99 :