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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <cmath>
#include <cerrno>
#include <glib.h>
#include <glibmm/fileutils.h>
#include <glibmm/markup.h>
#include "util/units.h"
#include "path-prefix.h"
#include "streq.h"
using Inkscape::Util::UNIT_TYPE_DIMENSIONLESS;
using Inkscape::Util::UNIT_TYPE_LINEAR;
using Inkscape::Util::UNIT_TYPE_RADIAL;
using Inkscape::Util::UNIT_TYPE_FONT_HEIGHT;
namespace
{
/**
* A std::map that gives the data type value for the string version.
*
* Note that we'd normally not return a reference to an internal version, but
* for this constant case it allows us to check against getTypeMappings().end().
*/
/** @todo consider hiding map behind hasFoo() and getFoo() type functions.*/
std::map<Glib::ustring, Inkscape::Util::UnitType> &getTypeMappings()
{
static bool init = false;
static std::map<Glib::ustring, Inkscape::Util::UnitType> typeMap;
if (!init)
{
init = true;
typeMap["DIMENSIONLESS"] = UNIT_TYPE_DIMENSIONLESS;
typeMap["LINEAR"] = UNIT_TYPE_LINEAR;
typeMap["RADIAL"] = UNIT_TYPE_RADIAL;
typeMap["FONT_HEIGHT"] = UNIT_TYPE_FONT_HEIGHT;
// Note that code was not yet handling LINEAR_SCALED, TIME, QTY and NONE
}
return typeMap;
}
} // namespace
namespace Inkscape {
namespace Util {
class UnitParser : public Glib::Markup::Parser
{
public:
typedef Glib::Markup::Parser::AttributeMap AttrMap;
typedef Glib::Markup::ParseContext Ctx;
UnitParser(UnitTable *table);
virtual ~UnitParser() {}
protected:
virtual void on_start_element(Ctx &ctx, Glib::ustring const &name, AttrMap const &attrs);
virtual void on_end_element(Ctx &ctx, Glib::ustring const &name);
virtual void on_text(Ctx &ctx, Glib::ustring const &text);
public:
UnitTable *tbl;
bool primary;
bool skip;
Unit unit;
};
UnitParser::UnitParser(UnitTable *table) :
tbl(table),
primary(false),
skip(false)
{
}
#define BUFSIZE (255)
Unit::Unit() :
type(UNIT_TYPE_DIMENSIONLESS), // should this or NONE be the default?
factor(1.0),
name(),
name_plural(),
abbr(),
description()
{
}
Unit::Unit(UnitType type,
double factor,
Glib::ustring const &name,
Glib::ustring const &name_plural,
Glib::ustring const &abbr,
Glib::ustring const &description) :
type(type),
factor(factor),
name(name),
name_plural(name_plural),
abbr(abbr),
description(description)
{
}
void Unit::clear()
{
*this = Unit();
}
int Unit::defaultDigits() const {
int factor_digits = int(log10(factor));
if (factor_digits < 0) {
g_warning("factor = %f, factor_digits = %d", factor, factor_digits);
g_warning("factor_digits < 0 - returning 0");
factor_digits = 0;
}
return factor_digits;
}
UnitTable::UnitTable()
{
gchar *filename = g_build_filename(INKSCAPE_UIDIR, "units.xml", NULL);
load(filename);
g_free(filename);
}
UnitTable::~UnitTable() {
for (UnitMap::iterator iter = _unit_map.begin(); iter != _unit_map.end(); ++iter)
{
delete (*iter).second;
}
}
void UnitTable::addUnit(Unit const &u, bool primary) {
_unit_map[u.abbr] = new Unit(u);
if (primary) {
_primary_unit[u.type] = u.abbr;
}
}
Unit UnitTable::getUnit(Glib::ustring const &unit_abbr) const {
UnitMap::const_iterator iter = _unit_map.find(unit_abbr);
if (iter != _unit_map.end()) {
return *((*iter).second);
} else {
return Unit();
}
}
bool UnitTable::deleteUnit(Unit const &u) {
bool deleted = false;
// Cannot delete the primary unit type since it's
// used for conversions
if (u.abbr != _primary_unit[u.type]) {
UnitMap::iterator iter = _unit_map.find(u.abbr);
if (iter != _unit_map.end()) {
delete (*iter).second;
_unit_map.erase(iter);
deleted = true;
}
}
return deleted;
}
bool UnitTable::hasUnit(Glib::ustring const &unit) const
{
UnitMap::const_iterator iter = _unit_map.find(unit);
return (iter != _unit_map.end());
}
UnitTable::UnitMap UnitTable::units(UnitType type) const
{
UnitMap submap;
for (UnitMap::const_iterator iter = _unit_map.begin(); iter != _unit_map.end(); ++iter) {
if (((*iter).second)->type == type) {
submap.insert(UnitMap::value_type((*iter).first, new Unit(*((*iter).second))));
}
}
return submap;
}
Glib::ustring UnitTable::primary(UnitType type) const
{
return _primary_unit[type];
}
bool UnitTable::load(std::string const &filename) {
UnitParser uparser(this);
Glib::Markup::ParseContext ctx(uparser);
try {
Glib::ustring unitfile = Glib::file_get_contents(filename);
ctx.parse(unitfile);
ctx.end_parse();
} catch (Glib::MarkupError const &e) {
g_warning("Problem loading units file '%s': %s\n", filename.c_str(), e.what().c_str());
return false;
}
return true;
}
bool UnitTable::save(std::string const &filename) {
g_warning("UnitTable::save(): not implemented");
return true;
}
void UnitParser::on_start_element(Ctx &ctx, Glib::ustring const &name, AttrMap const &attrs)
{
if (name == "unit") {
// reset for next use
unit.clear();
primary = false;
skip = false;
AttrMap::const_iterator f;
if ((f = attrs.find("type")) != attrs.end()) {
Glib::ustring type = f->second;
if (getTypeMappings().find(type) != getTypeMappings().end()) {
unit.type = getTypeMappings()[type];
} else {
g_warning("Skipping unknown unit type '%s'.\n", type.c_str());
skip = true;
}
}
if ((f = attrs.find("pri")) != attrs.end()) {
primary = (f->second[0] == 'y' || f->second[0] == 'Y');
}
}
}
void UnitParser::on_text(Ctx &ctx, Glib::ustring const &text)
{
Glib::ustring element = ctx.get_element();
if (element == "name") {
unit.name = text;
} else if (element == "plural") {
unit.name_plural = text;
} else if (element == "abbr") {
unit.abbr = text;
} else if (element == "factor") {
// TODO make sure we use the right conversion
unit.factor = g_ascii_strtod(text.c_str(), NULL);
} else if (element == "description") {
unit.description = text;
}
}
void UnitParser::on_end_element(Ctx &ctx, Glib::ustring const &name)
{
if (name == "unit" && !skip) {
tbl->addUnit(unit, primary);
}
}
} // namespace Util
} // namespace Inkscape
/*
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 :
|