Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
copy-resource.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include "copy-resource.h"
4#include "document.h"
5#include "object/sp-defs.h"
6#include "object/sp-object.h"
7#include "style.h"
8#include "util/uri.h"
9#include "xml/repr.h"
10
11// Make a copy of referenced: fill and stroke styles, clip paths
12void copy_style_links(const SPObject* source, SPDocument* src_document, SPDocument* dest_document) {
13
15
16 const char* fill = sp_repr_css_property(css, "fill", "none");
17 if (auto link = try_extract_uri(fill)) {
18 sp_copy_resource(src_document->getObjectByHref(*link), dest_document);
19 }
20
21 const char* stroke = sp_repr_css_property(css, "stroke", "none");
22 if (auto link = try_extract_uri(stroke)) {
23 sp_copy_resource(src_document->getObjectByHref(*link), dest_document);
24 }
25
27
28 if (auto clip = source->getAttribute("clip-path")) {
29 if (auto clip_path = try_extract_uri(clip)) {
30 sp_copy_resource(src_document->getObjectByHref(*clip_path), dest_document);
31 }
32 }
33
34 for (auto& child : source->children) {
35 copy_style_links(&child, src_document, dest_document);
36 }
37}
38
39
40SPObject* sp_copy_resource(const SPObject* source, SPDocument* dest_document) {
41 if (!source || !source->document || !dest_document) {
42 return nullptr;
43 }
44
45 // make a copy of the 'source' object
46 auto src_doc = source->document;
47 auto dest_defs = dest_document->getDefs();
48 Inkscape::XML::Document* xml_doc = dest_document->getReprDoc();
49 Inkscape::XML::Node* repr = source->getRepr()->duplicate(xml_doc);
50 dest_defs->getRepr()->addChild(repr, nullptr);
51 auto object = dest_document->getObjectByRepr(repr);
52 g_assert(object != nullptr);
54
55 // if 'source' references another object, copy it too
56 auto xhref = object->getAttribute("xlink:href");
57 auto href = object->getAttribute("href");
58
59 if (href || xhref) {
60 if (!href) {
61 href = xhref;
62 }
63 if (!dest_document->getObjectByHref(href)) {
64 sp_copy_resource(src_doc->getObjectByHref(href), dest_document);
65 }
66 }
67
68 // check fill and stroke for references to other objects, like gradients, and copy them too
69 copy_style_links(object, src_doc, dest_document);
70
71 return object;
72}
Interface for refcounted XML nodes.
Definition node.h:80
virtual void addChild(Node *child, Node *after)=0
Insert another node as a child of this node.
virtual Node * duplicate(Document *doc) const =0
Create a duplicate of this node.
Typed SVG document implementation.
Definition document.h:101
SPDefs * getDefs()
Return the main defs object for the document.
Definition document.cpp:247
Inkscape::XML::Document * getReprDoc()
Our Inkscape::XML::Document.
Definition document.h:211
SPObject * getObjectByHref(std::string const &href) const
SPObject * getObjectByRepr(Inkscape::XML::Node *repr) const
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
SPDocument * document
Definition sp-object.h:188
Inkscape::XML::Node * getRepr()
Returns the XML representation of tree.
char const * getAttribute(char const *name) const
ChildrenList children
Definition sp-object.h:907
SPObject * sp_copy_resource(const SPObject *source, SPDocument *dest_document)
Copy source resource form one document into another destination document.
void copy_style_links(const SPObject *source, SPDocument *src_document, SPDocument *dest_document)
std::shared_ptr< Css const > css
Colors::Color fill
Colors::Color stroke
static R & release(R &r)
Decrements the reference count of a anchored object.
static T clip(T const &v, T const &a, T const &b)
Ocnode * child[8]
Definition quantize.cpp:33
void sp_repr_css_attr_unref(SPCSSAttr *css)
Unreferences an SPCSSAttr (will be garbage collected if no references remain).
Definition repr-css.cpp:76
char const * sp_repr_css_property(SPCSSAttr *css, gchar const *name, gchar const *defval)
Returns a character string of the value of a given style property or a default value if the attribute...
Definition repr-css.cpp:147
C facade to Inkscape::XML::Node.
Interface for XML documents.
Definition document.h:43
static const unsigned SP_STYLE_FLAG_ALWAYS(1<< 2)
SPCSSAttr * sp_css_attr_from_object(SPObject *object, guint const flags)
Definition style.cpp:1427
SPStyle - a style object for SPItem objects.
std::optional< std::string > try_extract_uri(const char *url)
Try extracting URI from "url(xyz)" string using extract_uri.
Definition uri.cpp:102
URI functions as per 4.3.4 of CSS 2.1 http://www.w3.org/TR/CSS21/syndata.html#uri.