document.cpp revision 728227b29d44a286bf6843115860bd0bc0108771
/*
* SPDocument manipulation
*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* MenTaLguY <mental@rydia.net>
* bulia byak <buliabyak@users.sf.net>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
* Tavmjong Bah <tavmjong@free.fr>
*
* Copyright (C) 2004-2005 MenTaLguY
* Copyright (C) 1999-2002 Lauris Kaplinski
* Copyright (C) 2000-2001 Ximian, Inc.
* Copyright (C) 2012 Tavmjong Bah
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
/** \class SPDocument
* SPDocument serves as the container of both model trees (agnostic XML
* and typed object tree), and implements all of the document-level
* functionality used by the program. Many document level operations, like
* load, save, print, export and so on, use SPDocument as their basic datatype.
*
* SPDocument implements undo and redo stacks and an id-based object
* dictionary. Thanks to unique id attributes, the latter can be used to
* map from the XML tree back to the object tree.
*
* SPDocument performs the basic operations needed for asynchronous
* update notification (SPObject ::modified virtual method), and implements
* the 'modified' signal, as well.
*/
#define noSP_DOCUMENT_DEBUG_IDLE
#define noSP_DOCUMENT_DEBUG_UNDO
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string>
#include <cstring>
#include "widgets/desktop-widget.h"
#include "desktop.h"
#include "dir-util.h"
#include "display/drawing-item.h"
#include "document-private.h"
#include "document-undo.h"
#include "id-clash.h"
#include "inkscape.h"
#include "inkscape-version.h"
#include "persp3d.h"
#include "preferences.h"
#include "profile-manager.h"
#include "rdf.h"
#include "sp-factory.h"
#include "sp-item-group.h"
#include "sp-namedview.h"
#include "sp-symbol.h"
#include "transf_mat_3x4.h"
#include "xml/rebase-hrefs.h"
#include "libcroco/cr-cascade.h"
using Inkscape::DocumentUndo;
// Higher number means lower priority.
// Should have a lower priority than SP_DOCUMENT_UPDATE_PRIORITY,
// since we want it to happen when there are no more updates.
//gboolean sp_document_resource_list_free(gpointer key, gpointer value, gpointer data);
static gint doc_mem_count = 0;
static unsigned long next_serial = 0;
SPDocument::SPDocument() :
actionkey(),
modified_id(0),
oldSignalsConnected(false),
_node_cache_valid(false)
{
// Penalise libavoid for choosing paths with needless extra segments.
// This results in much better looking orthogonal connector paths.
SPDocumentPrivate *p = new SPDocumentPrivate();
p->serial = next_serial++;
p->sensitive = false;
p->history_size = 0;
p->seeking = false;
priv = p;
// Once things are set, hook in the manager
// XXX only for testing!
}
SPDocument::~SPDocument() {
if ( profileManager ) {
delete profileManager;
profileManager = 0;
}
if (router) {
delete router;
}
if (oldSignalsConnected) {
} else {
}
if (priv) {
}
DocumentUndo::clearRedo(this);
DocumentUndo::clearUndo(this);
if (root) {
}
/* Free resources */
}
if (name) {
}
if (base) {
}
if (uri) {
}
if (modified_id) {
modified_id = 0;
}
if (rerouting_handler_id) {
rerouting_handler_id = 0;
}
if (keepalive) {
}
if (this->current_persp3d_impl)
delete this->current_persp3d_impl;
this->current_persp3d_impl = NULL;
// This is at the end of the destructor, because preceding code adds new orphans to the queue
}
{
}
{
if (!root) {
return NULL;
}
}
// Check if current_persp3d is still valid
if (current_persp3d == plist[i])
return current_persp3d;
}
// If not, return the first perspective in defs (which may be NULL of none exists)
current_persp3d = persp3d_document_first_persp (this);
return current_persp3d;
}
return current_persp3d_impl;
}
//current_persp3d_impl = persp->perspective_impl;
}
{
if (SP_IS_PERSP3D(i)) {
}
}
}
/**
void SPDocument::initialize_current_persp3d()
{
this->current_persp3d = persp3d_document_first_persp(this);
if (!this->current_persp3d) {
this->current_persp3d = persp3d_create_xml_element(this);
}
}
**/
unsigned long SPDocument::serial() const {
}
}
void SPDocument::collectOrphans() {
while (!_collection_queue.empty()) {
for (std::vector<SPObject *>::const_iterator iter = objects.begin(); iter != objects.end(); ++iter) {
object->collectOrphan();
}
}
}
{
}
unsigned int keepalive,
{
if (parent) {
}
}
}
}
#ifndef WIN32
#else
// FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
#endif
// base is simply the part of the path before filename; e.g. when running "inkscape ../file.svg" the base is "../"
// which is why we use g_get_current_dir() in calculating the abs path above
//This is NULL for a new document
if (base) {
} else {
}
// Create SPRoot element
// Node is not a valid root element
delete rootObj;
// fixme: what to do here?
throw;
}
// Recursively build object tree
/* fixme: Not sure about this, but lets assume ::build updates */
/* fixme: Again, I moved these here to allow version determining in ::build (Lauris) */
/* Eliminate obsolete sodipodi:docbase, for privacy reasons */
/* Eliminate any claim to adhere to a profile, as we don't try to */
// creating namedview
// if there's none in the document already,
//rnew->setAttribute("id", "base");
// Add namedview data from the preferences
// we can't use getAllEntries because this could produce non-SVG doubles
}
if (!bordercolor.empty()) {
}
// insert into the document
// clean up
}
// Defs
}
/* Default RDF */
if (keepalive) {
}
// Check if the document already has a perspective (e.g., when opening an existing
// document). If not, create a new one and set it as the current perspective.
if (!document->getCurrentPersp3D()) {
//document->setCurrentPersp3D(persp3d_create_xml_element (document));
}
// reset undo key when selection changes, so that same-key actions on different objects are not coalesced
));
));
document->oldSignalsConnected = true;
return document;
}
/**
* Fetches a document and attaches it to the current document as a child href
*/
{
SPDocument *parent = this;
// Check myself and any parents int he chain
break;
}
// Then check children of those.
break;
}
}
}
// Load a fresh document from the svg source.
if(!document) {
}
return document;
}
/**
* Fetches document from URI, or creates new, if NULL; public document
* appears in document list.
*/
SPDocument *SPDocument::createNewDoc(gchar const *uri, unsigned int keepalive, bool make_new, SPDocument *parent)
{
if (uri) {
gchar *s, *p;
/* Try to fetch repr from file */
/* If file cannot be loaded, return NULL without warning */
/* If xml file is not svg, return NULL without warning */
/* fixme: destroy document */
p = strrchr(s, '/');
if (p) {
p[1] = '\0';
} else {
}
if (make_new) {
}
g_free(s);
} else {
if (make_new) {
}
}
//# These should be set by now
return doc;
}
SPDocument *SPDocument::createNewDocFromMem(gchar const *buffer, gint length, unsigned int keepalive)
{
if ( rdoc ) {
// Only continue to create a non-null doc if it could be loaded
// If xml file is not svg, return NULL without warning
// TODO fixme: destroy document
} else {
}
}
return doc;
}
{
return this;
}
{
return NULL;
}
/// guaranteed not to return nullptr
{
}
/// guaranteed not to return nullptr
// returns 'px' units as default, like legacy Inkscape
// THIS SHOULD NOT BE USED... INSTEAD USE DOCUMENT SCALE
{
}
/// Sets document scale (by changing viewBox)
root->viewBox_set = true;
root->updateRepr();
}
/// Sets document scale (by changing viewBox, x and y scaling equal)
}
/// user-units).
{
if( root->viewBox_set ) {
double scale_x = 1.0;
double scale_y = 1.0;
}
}
}
// std::cout << "SPDocument::getDocumentScale():\n" << scale << std::endl;
return scale;
}
// Avoid calling root->updateRepr() twice by combining setting width and height.
// (As done on every delete as clipboard calls this via fitToRect(). Also called in page-sizer.cpp)
void SPDocument::setWidthAndHeight(const Inkscape::Util::Quantity &width, const Inkscape::Util::Quantity &height, bool changeSize)
{
else
old_width_converted = Inkscape::Util::Quantity::convert(root->width.value, old_width_units, width.unit);
else
old_height_converted = Inkscape::Util::Quantity::convert(root->height.value, old_height_units, height.unit);
// viewBox scaled by relative change in page size (maintains document scale).
}
root->updateRepr();
}
{
}
}
}
{
else
old_width_converted = Inkscape::Util::Quantity::convert(root->width.value, old_width_units, width.unit);
root->viewBox.setMax(Geom::Point(root->viewBox.left() + (root->width.value / old_width_converted) * root->viewBox.width(), root->viewBox.bottom()));
root->updateRepr();
}
{
}
}
}
{
else
old_height_converted = Inkscape::Util::Quantity::convert(root->height.value, old_height_units, height.unit);
root->viewBox.setMax(Geom::Point(root->viewBox.right(), root->viewBox.top() + (root->height.value / old_height_converted) * root->viewBox.height()));
root->updateRepr();
}
{
if (root->viewBox_set) {
} else {
}
return viewBox;
}
{
root->viewBox_set = true;
root->updateRepr();
}
{
}
{
}
/**
* Given a Geom::Rect that may, for example, correspond to the bbox of an object,
* this function fits the canvas to that rect by resizing the canvas
* and translating the document root into position.
* \param rect fit document size to this
* \param with_margins add margins to rect, by taking margins from this
* document's namedview (<sodipodi:namedview> "fit-margin-..."
* attributes, and "units")
*/
{
/* in px */
double margin_top = 0.0;
double margin_left = 0.0;
double margin_right = 0.0;
double margin_bottom = 0.0;
if (with_margins && nv) {
margin_top = nv->getMarginLength("fit-margin-top", nv_units, unit_table.getUnit("px"), w, h, false);
margin_left = nv->getMarginLength("fit-margin-left", nv_units, unit_table.getUnit("px"), w, h, true);
margin_right = nv->getMarginLength("fit-margin-right", nv_units, unit_table.getUnit("px"), w, h, true);
margin_bottom = nv->getMarginLength("fit-margin-bottom", nv_units, unit_table.getUnit("px"), w, h, false);
}
}
Inkscape::Util::Quantity(Inkscape::Util::Quantity::convert(rect_with_margins.width(), "px", nv_units), nv_units),
Inkscape::Util::Quantity(Inkscape::Util::Quantity::convert(rect_with_margins.height(), "px", nv_units), nv_units)
);
- rect_with_margins.min());
if(nv) {
// update the viewport so the drawing appears to stay where it was
}
}
{
if (this->base) {
}
if (base) {
}
}
{
if (filename) {
#ifndef WIN32
#else
// FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
#endif
} else {
}
// Update saveable repr attributes.
// Changing uri in the document repr must not be not undoable.
DocumentUndo::setUndoSensitive(this, false);
if (rebase) {
}
}
/**
* Sets base, name and uri members of \a document. Doesn't update
* any relative hrefs in the document: thus, this is primarily for
* newly-created documents.
*
* \see sp_document_change_uri_and_hrefs
*/
{
do_change_uri(filename, false);
}
/**
* Changes the base, name and uri members of \a document, and updates any
* relative hrefs in the document to be relative to the new base.
*
* \see sp_document_set_uri
*/
{
do_change_uri(filename, true);
}
{
}
{
}
{
}
{
}
{
}
void
{
// printf("Starting Reconstruction\n");
return;
}
{
}
void
{
// printf("Finishing Reconstruction\n");
/**
// Reference to the old persp3d object is invalid after reconstruction.
initialize_current_persp3d();
return;
**/
}
{
}
void SPDocument::_emitModified() {
static guint const flags = SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG;
root->emitModified(0);
_node_cache_valid=false;
}
if (object) {
//g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) == NULL);
//g_hash_table_insert(priv->iddef, GINT_TO_POINTER(idq), object);
} else {
//g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) != NULL);
//g_hash_table_remove(priv->iddef, GINT_TO_POINTER(idq));
}
} else { // discard unused signal
}
}
}
void
{
}
void
{
}
{
}
{
return NULL;
}
// GQuark idq = g_quark_from_string(id);
//gpointer rv = g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq));
{
}
else
{
return NULL;
}
}
{
}
{
if (object) {
} else {
}
}
{
else
return NULL;
}
{
if (document_language) {
while (isspace(*document_language))
}
if ( !document_language || 0 == *document_language) {
// retrieve system language
}
}
}
if ( NULL != document_language ) {
}
}
}
if ( NULL == document_language )
return document_language;
}
/* Object modification root handler */
void SPDocument::requestModified()
{
if (!modified_id) {
sp_document_idle_handler, this, NULL);
}
if (!rerouting_handler_id) {
sp_document_rerouting_handler, this, NULL);
}
}
{
// Set up viewport in case svg has it defined as percentages
} else { // as a last resort, set size to A4
ctx->viewport = Geom::Rect::from_xywh(0, 0, Inkscape::Util::Quantity::convert(210, "mm", "px"), Inkscape::Util::Quantity::convert(297, "mm", "px"));
}
}
/**
* Tries to update the document state based on the modified and
* "update required" flags, and return true if the document has
* been brought fully up to date.
*/
bool
{
/* Process updates */
setupViewport(&ctx);
DocumentUndo::setUndoSensitive(this, false);
}
this->_emitModified();
}
}
/**
* Repeatedly works on getting the document updated, since sometimes
* it takes more than one pass to get the document updated. But it
* usually should not take more than a few loops, and certainly never
* more than 32 iterations. So we bail out if we hit 32 iterations,
* since this typically indicates we're stuck in an update loop.
*/
{
// Bring the document up-to-date, specifically via the following:
// 1a) Process all document updates.
// 1b) When completed, process connector routing changes.
// 2a) Process any updates resulting from connector reroutings.
int counter = 32;
// Process document updates.
while (!_updateDocument()) {
if (counter == 0) {
break;
}
counter--;
}
if (counter == 0)
{
break;
}
// After updates on the first pass we get libavoid to process all the
// changed objects and provide new routings. This may cause some objects
// to be modified, hence the second update pass.
if (pass == 1) {
}
}
if (modified_id) {
// Remove handler
modified_id = 0;
}
if (rerouting_handler_id) {
// Remove handler
rerouting_handler_id = 0;
}
return counter>0;
}
/**
* An idle handler to update the document. Returns true if
* the document needs further updates.
*/
static gint
{
bool status = !doc->_updateDocument(); // method TRUE if it does NOT need further modification, so invert
if (!status) {
doc->modified_id = 0;
}
return status;
}
/**
* An idle handler to reroute connectors in the document.
*/
static gint
{
// Process any queued movement actions and determine new routings for
// object-avoiding connectors. Callbacks will be used to update and
// redraw affected connectors.
// We don't need to handle rerouting again until there are further
// diagram updates.
doc->rerouting_handler_id = 0;
return false;
}
{
}
{
}
static std::vector<SPItem*> &find_items_in_area(std::vector<SPItem*> &s, SPGroup *group, unsigned int dkey, Geom::Rect const &area,
bool (*test)(Geom::Rect const &, Geom::Rect const &), bool take_insensitive = false, bool into_groups = false)
{
if ( SP_IS_ITEM(o) ) {
} else {
}
}
}
}
return s;
}
/**
Returns true if an item is among the descendants of group (recursively).
*/
{
bool inGroup = false;
if ( SP_IS_ITEM(o) ) {
inGroup = true;
} else if ( SP_IS_GROUP(o) ) {
}
}
}
return inGroup;
}
SPItem *SPDocument::getItemFromListAtPointBottom(unsigned int dkey, SPGroup *group, std::vector<SPItem*> const &list,Geom::Point const &p, bool take_insensitive)
{
SPItem *bottomMost = 0;
if ( SP_IS_ITEM(o) ) {
bottomMost = item;
}
}
if ( !bottomMost && SP_IS_GROUP(o) ) {
// return null if not found:
}
}
}
return bottomMost;
}
/**
Turn the SVG DOM into a flat list of nodes that can be searched from top-down.
The list can be persisted, which improves "find at multiple points" speed.
Returns true if upto is reached.
*/
bool SPDocument::build_flat_item_list(unsigned int dkey, SPGroup *group, gboolean into_groups, bool take_insensitive, SPItem *upto)
{
bool found_upto = false;
if (!SP_IS_ITEM(o)) {
continue;
}
found_upto = true;
break;
}
if (found_upto)
break;
} else {
}
}
}
return found_upto;
}
/**
Returns the topmost (in z-order) item from the descendants of group (recursively) which
is at the point p, or NULL if none. Honors into_groups on whether to recurse into
non-layer groups or not. Honors take_insensitive on whether to return insensitive
items. If upto != NULL, then if item upto is encountered (at any level), stops searching
upwards in z-order and returns what it has found so far (i.e. the found item is
guaranteed to be lower than upto). Requires a list of nodes built by
build_flat_item_list.
*/
static SPItem *find_item_at_point(std::deque<SPItem*> *nodes, unsigned int dkey, Geom::Point const &p)
{
child = *i;
break;
}
}
return seen;
}
/**
Returns the topmost non-layer group from the descendants of group which is at point
p, or NULL if none. Recurses into layers but not into groups.
*/
{
if (!SP_IS_ITEM(o)) {
continue;
}
if (newseen) {
}
}
// seen remembers the last (topmost) of groups pickable at this point
}
}
}
return seen;
}
/*
* Return list of items, contained in box
*
* Assumes box is normalized (and g_asserts it!)
*
*/
std::vector<SPItem*> SPDocument::getItemsInBox(unsigned int dkey, Geom::Rect const &box, bool into_groups) const
{
}
/*
* Return list of items, that the parts of the item contained in box
*
* Assumes box is normalized (and g_asserts it!)
*
*/
std::vector<SPItem*> SPDocument::getItemsPartiallyInBox(unsigned int dkey, Geom::Rect const &box, bool into_groups) const
{
}
std::vector<SPItem*> SPDocument::getItemsAtPoints(unsigned const key, std::vector<Geom::Point> points, bool all_layers, size_t limit)
{
// When picking along the path, we don't want small objects close together
// (such as hatching strokes) to obscure each other by their deltas,
// so we temporarily set delta to a small value
// Cache a flattened SVG DOM to speed up selection.
if(!_node_cache_valid){
_node_cache.clear();
_node_cache_valid=true;
}
if(desktop){
}
size_t item_counter = 0;
item_counter++;
//limit 0 = no limit
if(item_counter == limit){
return items;
}
}
}
// and now we restore it back
return items;
}
{
// Build a flattened SVG DOM for find_item_at_point.
if(!_node_cache_valid){
_node_cache.clear();
if(!upto)
_node_cache_valid = true;
}
}
{
}
// Resource management
{
bool result = false;
/*in general, do not send signal if the object has no id (yet),
it means the object is not completely built.
(happens when pasting swatches across documents, cf bug 1495106)
[this check should be more generally presend on emit() calls since
the backtrace is unusable with crashed from this cause]
*/
result = true;
}
return result;
}
{
bool result = false;
result = true;
}
return result;
}
{
}
{
}
/* Helpers */
{
count++; // obj itself
}
return count;
}
{
}
{
if (SP_IS_DEFS(obj)) {
// fixme: some inkscape-internal nodes in the future might not be collectable
}
} else {
}
}
}
unsigned int SPDocument::vacuumDocument()
{
unsigned int start = objects_in_document(this);
unsigned int end;
unsigned int iterations = 0;
do {
this->collectOrphans();
iterations++;
newend = objects_in_document(this);
}
bool SPDocument::isSeeking() const {
}
this->modified_since_save = modified;
if (SP_ACTIVE_DESKTOP) {
if (parent) { // during load, SP_ACTIVE_DESKTOP may be !nullptr, but parent might still be nullptr
}
}
}
/**
* Paste SVG defs from the document retrieved from the clipboard or imported document into the active document.
* @param clipdoc The document to paste.
* @pre @c clipdoc != NULL and pasting into the active document is possible.
*/
{
prevent_id_clashes(source, this);
for (std::vector<Inkscape::XML::Node const *>::iterator defs = defsNodes.begin(); defs != defsNodes.end(); ++defs) {
}
}
void SPDocument::importDefsNode(SPDocument *source, Inkscape::XML::Node *defs, Inkscape::XML::Node *target_defs)
{
int stagger=0;
/* Note, "clipboard" throughout the comments means "the document that is either the clipboard
or an imported document", as importDefs is called in both contexts.
The order of the records in the clipboard is unpredictable and there may be both
forward and backwards references to other records within it. There may be definitions in
the clipboard that duplicate definitions in the present document OR that duplicate other
definitions in the clipboard. (Inkscape will not have created these, but they may be read
in from other SVG sources.)
There are 3 passes to clean this up:
In the first find and mark definitions in the clipboard that are duplicates of those in the
present document. Change the ID to "RESERVED_FOR_INKSCAPE_DUPLICATE_DEF_XXXXXXXXX".
(Inkscape will not reuse an ID, and the XXXXXXXXX keeps it from automatically creating new ones.)
References in the clipboard to the old clipboard name are converted to the name used
in the current document.
In the second find and mark definitions in the clipboard that are duplicates of earlier
definitions in the clipbard. Unfortunately this is O(n^2) and could be very slow for a large
SVG with thousands of definitions. As before, references are adjusted to reflect the name
going forward.
In the final cycle copy over those records not marked with that ID.
If an SVG file uses the special ID it will cause problems!
If this function is called because of the paste of a true clipboard the caller will have passed in a
COPY of the clipboard items. That is good, because this routine modifies that document. If the calling
behavior ever changes, so that the same document is passed in on multiple pastes, this routine will break
as in the following example:
1. Paste clipboard containing B same as A into document containing A. Result, B is dropped
and all references to it will point to A.
2. Paste same clipboard into a new document. It will not contain A, so there will be unsatisfied
references in that window.
*/
/* First pass: remove duplicates in clipboard of definitions in document */
/* If this clipboard has been pasted into one document, and is now being pasted into another,
or pasted again into the same, it will already have been processed. If we detect that then
skip the rest of this pass. */
// Prevent duplicates of solid swatches by checking if equivalent swatch already exists
// Change object references to the existing equivalent gradient
}
// do NOT break here, there could be more than 1 duplicate!
}
}
}
}
}
/* Second pass: remove duplicates in clipboard of earlier definitions in clipboard */
// Change object references to the existing equivalent gradient
// two id's in the clipboard should never be the same, so always change references
// do NOT break here, there could be more than 1 duplicate!
}
}
}
}
}
/* Final pass: copy over those parts which are not duplicates */
/* Ignore duplicate defs marked in the first pass */
bool duplicate = false;
// Prevent duplication of symbols... could be more clever.
// The tag "_inkscape_duplicate" is added to "id" by ClipboardManagerImpl::copySymbol().
// We assume that symbols are in defs section (not required by SVG spec).
// This is our symbol, now get rid of tag
// Check that it really is a duplicate
duplicate = true;
break;
}
}
}
if ( !duplicate ) {
}
}
}
if (!duplicate) {
}
}
}
/*
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 :