Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
attribute-sort-util.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright (C) 2018 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10/*
11 * attribute-sort-util.cpp
12 *
13 * Created on: Jun 10, 2016
14 * Author: Tavmjong Bah
15 */
16
21#include <fstream>
22#include <sstream>
23#include <iostream>
24#include <vector>
25#include <utility> // std::pair
26#include <algorithm> // std::sort
27
28#include <glibmm/ustring.h>
29
30#include "attribute-sort-util.h"
31
32#include "xml/repr.h"
34#include "xml/sp-css-attr.h"
35
36#include "attributes.h"
37
40
41static void sp_attribute_sort_recursive(Node& repr);
42static void sp_attribute_sort_element(Node& repr);
43static void sp_attribute_sort_style(Node& repr);
44static void sp_attribute_sort_style(Node& repr, SPCSSAttr& css);
45
53
58
60 Glib::ustring element = repr.name();
61
62 // Only sort elements in svg namespace
63 if( element.substr(0,4) == "svg:" ) {
65 }
66 }
67
68 for(Node *child=repr.firstChild() ; child ; child = child->next()) {
70 }
71}
72
76static bool cmp(std::pair< Glib::ustring, Glib::ustring > const &a,
77 std::pair< Glib::ustring, Glib::ustring > const &b) {
78 auto val_a = sp_attribute_lookup(a.first.c_str());
79 auto val_b = sp_attribute_lookup(b.first.c_str());
80 if (val_a == SPAttr::INVALID) return false; // Unknown attributes at end.
81 if (val_b == SPAttr::INVALID) return true; // Unknown attributes at end.
82 return val_a < val_b;
83}
84
88static void sp_attribute_sort_element(Node& repr) {
89
90 g_return_if_fail (repr.type() == Inkscape::XML::NodeType::ELEMENT_NODE);
91
93
94 // Sort attributes:
95
96 // It doesn't seem possible to sort a List directly so we dump the list into
97 // a std::list and sort that. Not very efficient. Sad.
98
99 std::vector<std::pair< Glib::ustring, Glib::ustring > > my_list;
100 for ( const auto & iter : repr.attributeList()) {
101
102 Glib::ustring attribute = g_quark_to_string(iter.key);
103 Glib::ustring value = (const char*)iter.value;
104
105 // C++11 my_list.emlace_back(attribute, value);
106 my_list.emplace_back(attribute,value);
107 }
108 std::sort(my_list.begin(), my_list.end(), cmp);
109 // Delete all attributes.
110 for (const auto& it : my_list) {
111 // Removing "inkscape:label" results in crash when Layers dialog is open.
112 if (it.first != "inkscape:label") {
113 repr.removeAttribute(it.first);
114 }
115 }
116 // Insert all attributes in proper order
117 for (const auto& it : my_list) {
118 if (it.first != "inkscape:label") {
119 repr.setAttribute( it.first, it.second);
120 }
121 }
122}
123
124
128static void sp_attribute_sort_style(Node& repr) {
129
130 g_return_if_fail (repr.type() == Inkscape::XML::NodeType::ELEMENT_NODE);
131
132 // Find element's style
133 SPCSSAttr *css = sp_repr_css_attr( &repr, "style" );
135
136 // Convert css node's properties data to string and set repr node's attribute "style" to that string.
137 // sp_repr_css_set( repr, css, "style"); // Don't use as it will cause loop.
138 Glib::ustring value;
140 repr.setAttributeOrRemoveIfEmpty("style", value);
141
143}
144
145
150
151 // Loop over all properties in "style" node.
152 std::vector<std::pair< Glib::ustring, Glib::ustring > > my_list;
153 for ( const auto & iter : css.attributeList()) {
154
155 Glib::ustring property = g_quark_to_string(iter.key);
156 Glib::ustring value = (const char*)iter.value;
157
158 // C++11 my_list.emlace_back(property, value);
159 my_list.emplace_back(property,value);
160 }
161 std::sort(my_list.begin(), my_list.end(), cmp);
162 // Delete all attributes.
163 for (const auto& it : my_list) {
164 sp_repr_css_set_property( &css, it.first.c_str(), nullptr );
165 }
166 // Insert all attributes in proper order
167 for (const auto& it : my_list) {
168 sp_repr_css_set_property( &css, it.first.c_str(), it.second.c_str() );
169 }
170}
171
172/*
173 Local Variables:
174 mode:c++
175 c-file-style:"stroustrup"
176 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
177 indent-tabs-mode:nil
178 fill-column:99
179 End:
180*/
181// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
TODO: insert short description here.
static bool cmp(std::pair< Glib::ustring, Glib::ustring > const &a, std::pair< Glib::ustring, Glib::ustring > const &b)
Compare function.
static void sp_attribute_sort_style(Node &repr)
Sort CSS style on an element.
static void sp_attribute_sort_recursive(Node &repr)
Sort recursively over all elements.
void sp_attribute_sort_tree(Node &repr)
Sort attributes by name.
static void sp_attribute_sort_element(Node &repr)
Sort attributes on an element.
TODO: insert short description here.
SPAttr sp_attribute_lookup(gchar const *key)
Get attribute id by name.
Lookup dictionary for attributes/properties.
@ INVALID
Must have value 0.
Key-value pair representing an attribute.
Interface for refcounted XML nodes.
Definition node.h:80
virtual char const * name() const =0
Get the name of the element node.
virtual const AttributeVector & attributeList() const =0
Get a list of the node's attributes.
void setAttributeOrRemoveIfEmpty(Inkscape::Util::const_char_ptr key, Inkscape::Util::const_char_ptr value)
Change an attribute of this node.
Definition node.cpp:167
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.
void removeAttribute(Inkscape::Util::const_char_ptr key)
Remove an attribute of this node.
Definition node.h:280
virtual NodeType type() const =0
Get the type of the node.
std::shared_ptr< Css const > css
@ ELEMENT_NODE
Regular element node, e.g. <group />.
Ocnode * child[8]
Definition quantize.cpp:33
void sp_repr_css_write_string(SPCSSAttr *css, Glib::ustring &str)
Write a style attribute string from a list of properties stored in an SPCSAttr object.
Definition repr-css.cpp:242
void sp_repr_css_attr_unref(SPCSSAttr *css)
Unreferences an SPCSSAttr (will be garbage collected if no references remain).
Definition repr-css.cpp:76
SPCSSAttr * sp_repr_css_attr(Node const *repr, gchar const *attr)
Creates a new SPCSSAttr with one attribute (i.e.
Definition repr-css.cpp:88
void sp_repr_css_set_property(SPCSSAttr *css, gchar const *name, gchar const *value)
Set a style property to a new value (e.g.
Definition repr-css.cpp:190
C facade to Inkscape::XML::Node.
SPCSSAttr - interface for CSS Attributes.