1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
* TODO: insert short description here
*//*
* Authors: see git history
*
* Copyright (C) 2018 Authors
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
/*
* attribute-sort-util.cpp
*
* Created on: Jun 10, 2016
* Author: Tavmjong Bah
*/
/**
* Utility functions for sorting attributes by name.
*/
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <utility> // std::pair
#include <algorithm> // std::sort
#include "attribute-sort-util.h"
#include "xml/repr.h"
#include "xml/attribute-record.h"
#include "attributes.h"
using Inkscape::XML::Node;
using Inkscape::XML::AttributeRecord;
using Inkscape::Util::List;
/**
* Sort attributes by name.
*/
void sp_attribute_sort_tree(Node *repr) {
g_return_if_fail (repr != nullptr);
sp_attribute_sort_recursive( repr );
}
/**
* Sort recursively over all elements.
*/
void sp_attribute_sort_recursive(Node *repr) {
g_return_if_fail (repr != nullptr);
if( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
Glib::ustring element = repr->name();
// Only sort elements in svg namespace
if( element.substr(0,4) == "svg:" ) {
sp_attribute_sort_element( repr );
}
}
for(Node *child=repr->firstChild() ; child ; child = child->next()) {
sp_attribute_sort_recursive( child );
}
}
/**
* Compare function
*/
bool cmp(std::pair< Glib::ustring, Glib::ustring > const &a,
std::pair< Glib::ustring, Glib::ustring > const &b) {
unsigned val_a = sp_attribute_lookup(a.first.c_str());
unsigned val_b = sp_attribute_lookup(b.first.c_str());
if (val_a == 0) return false; // Unknown attributes at end.
if (val_b == 0) return true; // Unknown attributes at end.
return val_a < val_b;
}
/**
* Sort attributes on an element
*/
void sp_attribute_sort_element(Node *repr) {
g_return_if_fail (repr != nullptr);
g_return_if_fail (repr->type() == Inkscape::XML::ELEMENT_NODE);
// Glib::ustring element = repr->name();
// Glib::ustring id = (repr->attribute( "id" )==NULL ? "" : repr->attribute( "id" ));
sp_attribute_sort_style(repr);
// Sort attributes:
// It doesn't seem possible to sort a List directly so we dump the list into
// a std::list and sort that. Not very efficient. Sad.
List<AttributeRecord const> attributes = repr->attributeList();
std::vector<std::pair< Glib::ustring, Glib::ustring > > my_list;
for ( List<AttributeRecord const> iter = attributes ; iter ; ++iter ) {
Glib::ustring attribute = g_quark_to_string(iter->key);
Glib::ustring value = (const char*)iter->value;
// C++11 my_list.emlace_back(attribute, value);
my_list.emplace_back(attribute,value);
}
std::sort(my_list.begin(), my_list.end(), cmp);
// Delete all attributes.
//for (auto it: my_list) {
for (auto & it : my_list) {
// Removing "inkscape:label" results in crash when Layers dialog is open.
if (it.first != "inkscape:label") {
repr->removeAttribute(it.first);
}
}
// Insert all attributes in proper order
for (auto & it : my_list) {
if (it.first != "inkscape:label") {
repr->setAttribute( it.first, it.second);
}
}
}
/**
* Sort CSS style on an element.
*/
void sp_attribute_sort_style(Node *repr) {
g_return_if_fail (repr != nullptr);
g_return_if_fail (repr->type() == Inkscape::XML::ELEMENT_NODE);
// Find element's style
SPCSSAttr *css = sp_repr_css_attr( repr, "style" );
sp_attribute_sort_style(repr, css);
// Convert css node's properties data to string and set repr node's attribute "style" to that string.
// sp_repr_css_set( repr, css, "style"); // Don't use as it will cause loop.
Glib::ustring value;
sp_repr_css_write_string(css, value);
repr->setAttributeOrRemoveIfEmpty("style", value);
sp_repr_css_attr_unref( css );
}
/**
* Sort CSS style on an element.
*/
Glib::ustring sp_attribute_sort_style(Node *repr, gchar const *string) {
g_return_val_if_fail (repr != nullptr, NULL);
g_return_val_if_fail (repr->type() == Inkscape::XML::ELEMENT_NODE, NULL);
SPCSSAttr *css = sp_repr_css_attr_new();
sp_repr_css_attr_add_from_string( css, string );
sp_attribute_sort_style(repr, css);
Glib::ustring string_cleaned;
sp_repr_css_write_string (css, string_cleaned);
sp_repr_css_attr_unref( css );
return string_cleaned;
}
/**
* Sort CSS style on an element.
*/
void sp_attribute_sort_style(Node* repr, SPCSSAttr *css) {
g_return_if_fail (repr != nullptr);
g_return_if_fail (css != nullptr);
Glib::ustring element = repr->name();
Glib::ustring id = (repr->attribute( "id" )==nullptr ? "" : repr->attribute( "id" ));
// Loop over all properties in "style" node.
std::vector<std::pair< Glib::ustring, Glib::ustring > > my_list;
for ( List<AttributeRecord const> iter = css->attributeList() ; iter ; ++iter ) {
Glib::ustring property = g_quark_to_string(iter->key);
Glib::ustring value = (const char*)iter->value;
// C++11 my_list.emlace_back(property, value);
my_list.emplace_back(property,value);
}
std::sort(my_list.begin(), my_list.end(), cmp);
// Delete all attributes.
//for (auto it: my_list) {
for (auto & it : my_list) {
sp_repr_css_set_property( css, it.first.c_str(), nullptr );
}
// Insert all attributes in proper order
for (auto & it : my_list) {
sp_repr_css_set_property( css, it.first.c_str(), it.second.c_str() );
}
}
/*
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 :
|