Class Length

java.lang.Object
com.pdftools.geometry.units.Length
All Implemented Interfaces:
Comparable<Length>

public class Length extends Object implements Comparable<Length>

Class that represents a measurable length. Provides conversion between units, parsing of string representations of lengths, and basic mathematical operations.

The unit used in PDF documents is

invalid reference
Units.POINT
, which is also used internally by the Length object to store length values. For that reason, when converting units to and from the internally used unit, minor numerical differences may occur due to floating-point arithmetic.

  • Field Details

    • ZERO

      public static final Length ZERO
    • MAX_VALUE

      public static final Length MAX_VALUE
  • Constructor Details

    • Length

      public Length(double value, Length.Units unit)
      Constructs a newly allocated Length object using the specified value and unit.
      Parameters:
      value - the numerical value.
      unit - the unit.
  • Method Details

    • to

      public double to(Length.Units unit)
      Get the numerical length value of the specified unit.
      Parameters:
      unit - the unit.
      Returns:
    • parse

      public static Length parse(String value)
      Creates a Length object by parsing a String representation of a length with its unit.
      Parameters:
      value - Value-unit pair of the form "<value><unit>". Examples: "12.3cm" or "23.9mm". Allowed units are "um", "mm", "cm", "m", "km", "pt", and "in".
      Throws:
      IllegalArgumentException - if unit given in value is invalid.
    • toString

      public String toString()
      Creates a string representation with an associated suitable metric unit, "m", "cm" or "mm".
      Overrides:
      toString in class Object
    • toString

      public String toString(Length.Units unit)
      Creates a string representation with the specified unit.
    • parseArray

      public static Length[] parseArray(String value, int size)
      Creates an array of Length by parsing a String representation of lengths with units.
      Parameters:
      value - A group of value-unit pairs of the form "<value1><unit1> <value2><unit2>...". Example: "12.3cm 23.9mm 0.25in" etc. Allowed units are "um", "mm", "cm", "m", "km", "pt" and "in".
      size - Number of value-unit pairs represented by the value parameter.
      Throws:
      IllegalArgumentException - if parameter size doesn't fit the number of values in value.
      IllegalArgumentException - if a unit is invalid.
    • parseArray

      public static Length[] parseArray(String value)
      Creates an array of Length by parsing a String representation of lengths with units.
      Parameters:
      value - A group of value-unit pairs of the form "<value1><unit1> <value2><unit2>...". Example: "12.3cm 23.9mm 0.25in" etc. Allowed units are "um", "mm", "cm", "m", "km", "pt" and "in".
      Throws:
      IllegalArgumentException - if a unit is invalid.
    • add

      public Length add(Length addend)
    • subtract

      public Length subtract(Length subtrahend)
    • multiply

      public Length multiply(double multiplicand)
    • divide

      public Length divide(double divisor)
    • divide

      public double divide(Length divisor)
    • min

      public static Length min(Length lhs, Length rhs)
    • max

      public static Length max(Length lhs, Length rhs)
    • abs

      public static Length abs(Length length)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Length o)
      Specified by:
      compareTo in interface Comparable<Length>