Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
filter-file.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2008 Authors:
4 * Ted Gould <ted@gould.cx>
5 *
6 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
7 */
8
9#include "filter.h"
10
11/* System includes */
12#include <glibmm/i18n.h>
13
14/* Inkscape */
15#include "io/resource.h"
17#include "xml/repr.h" // sp_repr_read_file
18
19/* Extension */
20#include "extension/extension.h"
21#include "extension/system.h"
22
23
24using namespace Inkscape::IO::Resource;
25
26namespace Inkscape {
27namespace Extension {
28namespace Internal {
29namespace Filter {
30
31void
32filters_load_file (Glib::ustring filename, gchar * menuname)
33{
34 Inkscape::XML::Document *doc = sp_repr_read_file(filename.c_str(), INKSCAPE_EXTENSION_URI);
35 if (doc == nullptr) {
36 g_warning("File (%s) is not parseable as XML. Ignored.", filename.c_str());
37 return;
38 }
39
40 Inkscape::XML::Node * root = doc->root();
41 if (strcmp(root->name(), "svg:svg")) {
43 g_warning("File (%s) is not SVG. Ignored.", filename.c_str());
44 return;
45 }
46
48 child != nullptr; child = child->next()) {
49 if (!strcmp(child->name(), "svg:defs")) {
50 for (Inkscape::XML::Node * defs = child->firstChild();
51 defs != nullptr; defs = defs->next()) {
52 if (!strcmp(defs->name(), "svg:filter")) {
53 Filter::filters_load_node(defs, menuname);
54 } // oh! a filter
55 } //defs
56 } // is defs
57 } // children of root
58
60 return;
61}
62
64{
65 for(auto &filename: get_filenames(USER, FILTERS, {".svg"})) {
66 filters_load_file(filename, _("Personal"));
67 }
68 for(auto &filename: get_filenames(SHARED, FILTERS, {".svg"})) {
69 filters_load_file(filename, _("Personal"));
70 }
71 for(auto &filename: get_filenames(SYSTEM, FILTERS, {".svg"})) {
72 filters_load_file(filename, _("Bundled"));
73 }
74}
75
76
78
79class mywriter : public Inkscape::IO::BasicWriter {
80 Glib::ustring _str;
81public:
82 void close() override;
83 void flush() override;
84 void put (char ch) override;
85 gchar const * c_str () { return _str.c_str(); }
86};
87
88void mywriter::close () { return; }
89void mywriter::flush () { return; }
90void mywriter::put (char ch) { _str += ch; }
91
92
93void
95{
96 gchar const * label = node->attribute("inkscape:label");
97 gchar const * menu = node->attribute("inkscape:menu");
98 gchar const * menu_tooltip = node->attribute("inkscape:menu-tooltip");
99 gchar const * id = node->attribute("id");
100
101 if (label == nullptr) {
102 label = id;
103 }
104
105 // clang-format off
106 gchar * xml_str = g_strdup_printf(
107 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
108 "<name>%s</name>\n"
109 "<id>org.inkscape.effect.filter.%s</id>\n"
110 "<effect>\n"
111 "<object-type>all</object-type>\n"
112 "<effects-menu>\n"
113 "<submenu name=\"" N_("Filters") "\">\n"
114 "<submenu name=\"%s\"/>\n"
115 "</submenu>\n"
116 "</effects-menu>\n"
117 "<menu-tip>%s</menu-tip>\n"
118 "</effect>\n"
119 "</inkscape-extension>\n", label, id, menu? menu : menuname, menu_tooltip? menu_tooltip : label);
120 // clang-format on
121
122 // FIXME: Bad hack: since we pull out a single filter node out of SVG file and
123 // serialize it, it loses the namespace declarations from the root, so we must provide
124 // one right here for our inkscape attributes
125 node->setAttribute("xmlns:inkscape", SP_INKSCAPE_NS_URI);
126
127 mywriter writer;
128 sp_repr_write_stream(node, writer, 0, FALSE, g_quark_from_static_string("svg"), 0, 0);
129
130 Inkscape::Extension::build_from_mem(xml_str, std::make_unique<Filter>(g_strdup(writer.c_str())));
131 g_free(xml_str);
132 return;
133}
134
135}; /* namespace Filter */
136}; /* namespace Internal */
137}; /* namespace Extension */
138}; /* namespace Inkscape */
139
static void filters_load_node(Inkscape::XML::Node *node, gchar *menuname)
This class and its descendants are for unicode character-oriented output.
Interface for refcounted XML nodes.
Definition node.h:80
virtual Node * next()=0
Get the next sibling of this node.
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:25
virtual Node * firstChild()=0
Get the first child of this node.
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
virtual Node * root()=0
Get the root node of this node's document.
A way to clear the N_ macro, which is defined as an inline function.
RootCluster root
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
Inkscape::XML::Node * node
Glib::ustring label
void filters_load_file(Glib::ustring filename, gchar *menuname)
void build_from_mem(gchar const *buffer, std::unique_ptr< Implementation::Implementation > in_imp)
Create a module from a buffer holding an XML description.
Definition system.cpp:459
static R & release(R &r)
Decrements the reference count of a anchored object.
std::vector< std::string > get_filenames(Type type, std::vector< const char * > const &extensions, std::vector< const char * > const &exclusions)
Definition resource.cpp:267
Helper class to stream background task notifications as a series of messages.
Ocnode * child[8]
Definition quantize.cpp:33
Document * sp_repr_read_file(const gchar *filename, const gchar *default_ns, bool xinclude)
Reads XML from a file, and returns the Document.
Definition repr-io.cpp:274
void sp_repr_write_stream(Node *repr, Writer &out, gint indent_level, bool add_whitespace, Glib::QueryQuark elide_prefix, int inlineattrs, int indent, gchar const *const old_href_base, gchar const *const new_href_base)
Definition repr-io.cpp:890
C facade to Inkscape::XML::Node.
Inkscape::IO::Resource - simple resource API.
Interface for XML documents.
Definition document.h:43