de.infinityloop.util
Class UnitConverter

java.lang.Object
  extended by de.infinityloop.util.Units
      extended by de.infinityloop.util.UnitConverter

public class UnitConverter
extends de.infinityloop.util.Units

Unit converter class Converts between several units like cm, mm, twips, pt, inches etc.


Field Summary
static int kDefaultPrecision
           
 
Fields inherited from class de.infinityloop.util.Units
CM, DEG, EM, EMU, EX, GRAD, HP, IN, kDefault, kSame, M, MM, PC, PECENT, PT, PX, RAD, TW
 
Constructor Summary
UnitConverter()
          Default constructor
UnitConverter(double dpi)
          Construct with at the same time setting the dpi value for conversions
UnitConverter(double rV, int rU)
          Constructor with at the same time setting the reference value and unit for relative conversions like em, ex, or percentages.
UnitConverter(double rV, int rU, double fsV, int fsU)
          Constructor with at the same time setting the reference values for percentages and font size
UnitConverter(java.lang.String reference, java.lang.String fontsize)
          Constructor with at the same time setting the reference values for percentages and font size
 
Method Summary
 double convert(double src, int srcUnit, int destUnit)
          Converts a value from one unit to another.
 double convert(java.lang.String srcString, int destUnit)
          Converts a value from one unit to another.
 java.lang.String convertString(double src, int srcUnit, int destUnit, int prec)
          Converts a value from one unit to another.
 double getDPI()
          Returns the currently set global DPI value used for calculating conversions
static double getNumber(java.lang.String srcString)
          Returns only the numerical part of the value, i.e. this trims the extension and converts to a double.
static java.lang.String precisionString(double d, int prec)
          Convert the passed floating point value to a string representation with the specified number of decimals.
 void setDPI(double dpi)
           
 void setPrecision(int decs)
          Sets the precision to be used for all following conversions.
 
Methods inherited from class de.infinityloop.util.Units
getUnit, getUnitId, getUnitString, isRelativeUnit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

kDefaultPrecision

public static final int kDefaultPrecision
See Also:
Constant Field Values
Constructor Detail

UnitConverter

public UnitConverter()
Default constructor


UnitConverter

public UnitConverter(double rV,
                     int rU)
Constructor with at the same time setting the reference value and unit for relative conversions like em, ex, or percentages.

Parameters:
rV - the value
rU - its unit

UnitConverter

public UnitConverter(double rV,
                     int rU,
                     double fsV,
                     int fsU)
Constructor with at the same time setting the reference values for percentages and font size

Parameters:
rV - the percentage reference value
rU - the percentage reference value unit
fsV - the current font size reference value
fsU - the current font size reference value unit

UnitConverter

public UnitConverter(java.lang.String reference,
                     java.lang.String fontsize)
Constructor with at the same time setting the reference values for percentages and font size

Parameters:
reference - the percentage reference value
fontsize - the current font size reference value

UnitConverter

public UnitConverter(double dpi)
Construct with at the same time setting the dpi value for conversions

Parameters:
dpi - the dpi value to use in px conversions
Method Detail

setDPI

public void setDPI(double dpi)
Parameters:
dpi - sets the DPI value to use in subsequent calculations

getDPI

public double getDPI()
Returns the currently set global DPI value used for calculating conversions


setPrecision

public void setPrecision(int decs)
Sets the precision to be used for all following conversions.

Parameters:
decs - the number of decimals

convert

public double convert(double src,
                      int srcUnit,
                      int destUnit)
Converts a value from one unit to another.

Parameters:
src - source value
srcUnit - source unit
destUnit - destination unit
Returns:
the converted value in the new unit

convert

public double convert(java.lang.String srcString,
                      int destUnit)
Converts a value from one unit to another. The source unit is analyzed from the source string.

Parameters:
srcString - the source value string
destUnit - the unit to convert to
Returns:
the converted value in the new unit

getNumber

public static double getNumber(java.lang.String srcString)
Returns only the numerical part of the value, i.e. this trims the extension and converts to a double.

Parameters:
srcString - the value specification, e.g. "1em" or "2.54mm"
Returns:
the number value, e.g. 1.0 or 2.54

convertString

public java.lang.String convertString(double src,
                                      int srcUnit,
                                      int destUnit,
                                      int prec)
Converts a value from one unit to another.

Parameters:
src - the source value
srcUnit - the unit the source value is in
destUnit - the unit to convert to
prec - the precision the resulting string should have
Returns:
the converted value in the new unit as String

precisionString

public static java.lang.String precisionString(double d,
                                               int prec)
Convert the passed floating point value to a string representation with the specified number of decimals.

Parameters:
d - the value to convert to a string
prec - the decimal precision (number of decimals)
Returns:
string representation of input value