diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/util/units.cpp | 39 | ||||
| -rw-r--r-- | src/util/units.h | 12 |
2 files changed, 50 insertions, 1 deletions
diff --git a/src/util/units.cpp b/src/util/units.cpp index f822d01de..3bcbabf89 100644 --- a/src/util/units.cpp +++ b/src/util/units.cpp @@ -334,10 +334,47 @@ void UnitsSAXHandler::_endElement(xmlChar const *xname) } } +/** Initialize a length. */ +Length::Length(Unit *u, double l) { + unit = u; + length = l; +} + +/** Checks if a length is compatible with the specified unit. */ +bool Length::compatibleWith(Unit *u) { + // Percentages + if (unit->type == UNIT_TYPE_DIMENSIONLESS || u->type == UNIT_TYPE_DIMENSIONLESS) { + return true; + } + + // Other units with same type + if (unit->type == u->type) { + return true; + } + + // Different, incompatible types + return false; +} + +/** Return the length's value in the specified unit. */ +double Length::value(Unit *u) { + return convert(length, unit, u); +} + +/** Convert distances. */ +double Length::convert(double from_dist, Unit *from, Unit *to) const { + // Incompatible units + if (from->type != to->type) { + return -1; + } + + // Compatible units + return from_dist / from->factor * to->factor; +} + } // namespace Util } // namespace Inkscape - /* Local Variables: mode:c++ diff --git a/src/util/units.h b/src/util/units.h index b22bdb1f2..46680bf35 100644 --- a/src/util/units.h +++ b/src/util/units.h @@ -84,6 +84,18 @@ class UnitTable { }; +class Length { +public: + Unit *unit; + double length; + + Length(Unit *u, double l); // constructor + bool compatibleWith(Unit *u); + double value(Unit *u); + + double convert(double from_dist, Unit *from, Unit *to) const; +}; + } // namespace Util } // namespace Inkscape |
