From 1c06fa5e2552b291534f0b96542070f00fd0778b Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Mon, 24 Jun 2013 20:47:57 -0400 Subject: Added length class. (bzr r12380.1.1) --- src/util/units.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/util/units.cpp') 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++ -- cgit v1.2.3