summaryrefslogtreecommitdiffstats
path: root/src/util/units.cpp
diff options
context:
space:
mode:
authorJabier Arraiza Cenoz <jabier.arraiza@marker.es>2016-03-14 16:37:50 +0000
committerJabiertxof <jtx@jtx.marker.es>2016-03-14 16:37:50 +0000
commitb8d22beef5345210ad27cdc2685083aeae6f8f3b (patch)
treed69b8bfd19d3627a8425a1b265c2abf229b05354 /src/util/units.cpp
parentfixes for update to trunk (diff)
parent"Relative to" option for node alignment. (diff)
downloadinkscape-b8d22beef5345210ad27cdc2685083aeae6f8f3b.tar.gz
inkscape-b8d22beef5345210ad27cdc2685083aeae6f8f3b.zip
update to trunk
(bzr r13708.1.39)
Diffstat (limited to 'src/util/units.cpp')
-rw-r--r--src/util/units.cpp70
1 files changed, 56 insertions, 14 deletions
diff --git a/src/util/units.cpp b/src/util/units.cpp
index 2c72ec3ae..2e7a3b1d2 100644
--- a/src/util/units.cpp
+++ b/src/util/units.cpp
@@ -81,7 +81,21 @@ unsigned const svg_length_lookup[] = {
UNIT_CODE_PERCENT
};
-
+/* From SP_CSS_UNIT_* to unit */
+unsigned const sp_css_unit_lookup[] = {
+ 0,
+ UNIT_CODE_PX,
+ UNIT_CODE_PT,
+ UNIT_CODE_PC,
+ UNIT_CODE_MM,
+ UNIT_CODE_CM,
+ UNIT_CODE_IN,
+ UNIT_CODE_EM,
+ UNIT_CODE_EX,
+ UNIT_CODE_PERCENT
+ // UNIT_CODE_FT Missing,
+ // UNIT_CODE_MT Missing,
+};
// maps unit codes obtained from their abbreviations to their SVGLength unit indexes
typedef INK_UNORDERED_MAP<unsigned, SVGLength::Unit> UnitCodeLookup;
@@ -213,6 +227,10 @@ bool Unit::compatibleWith(Glib::ustring const &u) const
{
return compatibleWith(unit_table.getUnit(u));
}
+bool Unit::compatibleWith(char const *u) const
+{
+ return compatibleWith(unit_table.getUnit(u));
+}
bool Unit::operator==(Unit const &other) const
{
@@ -231,7 +249,29 @@ int Unit::svgUnit() const
return 0;
}
-
+double Unit::convert(double from_dist, Unit const *to) const
+{
+ // Percentage
+ if (to->type == UNIT_TYPE_DIMENSIONLESS) {
+ return from_dist * to->factor;
+ }
+
+ // Incompatible units
+ if (type != to->type) {
+ return -1;
+ }
+
+ // Compatible units
+ return from_dist * factor / to->factor;
+}
+double Unit::convert(double from_dist, Glib::ustring const &to) const
+{
+ return convert(from_dist, unit_table.getUnit(to));
+}
+double Unit::convert(double from_dist, char const *to) const
+{
+ return convert(from_dist, unit_table.getUnit(to));
+}
Unit UnitTable::_empty_unit;
@@ -283,6 +323,19 @@ Unit const *UnitTable::getUnit(SVGLength::Unit u) const
}
return &_empty_unit;
}
+/* SP_CSS_UNIT lookup */
+Unit const *UnitTable::getUnit(unsigned int u) const
+{
+ if (u == 0 || u > 9) {
+ return &_empty_unit;
+ }
+
+ UnitCodeMap::const_iterator f = _unit_map.find(sp_css_unit_lookup[u]);
+ if (f != _unit_map.end()) {
+ return &(*f->second);
+ }
+ return &_empty_unit;
+}
Unit const *UnitTable::findUnit(double factor, UnitType type) const
{
@@ -505,18 +558,7 @@ Glib::ustring Quantity::string() const {
double Quantity::convert(double from_dist, Unit const *from, Unit const *to)
{
- // Percentage
- if (to->type == UNIT_TYPE_DIMENSIONLESS) {
- return from_dist * to->factor;
- }
-
- // Incompatible units
- if (from->type != to->type) {
- return -1;
- }
-
- // Compatible units
- return from_dist * from->factor / to->factor;
+ return from->convert(from_dist, to);
}
double Quantity::convert(double from_dist, Glib::ustring const &from, Unit const *to)
{