Next / Previous / Contents / Shipman's homepage

4.3. class FoDim: General dimension

Use an instance of this class to represent any width or height or other dimension. The class is aware of the various different units that are allowed in XSL-FO dimension attributes.

The class supports a few of the common arithmetic operations that one typically needs in computing various document dimensions.

Dimensions are stored internally using values of Python's Decimal (fixed-point) type. This type's fixed-point arithmetic avoids many of the common pathologies of the float type. For documentation on this type, see the The Python Standard Library.

The constructor has this calling sequence:

FoDim(number, units)

The number may be any type that is acceptable to the decimal.Decimal() constructor.

The units argument may take any of these values:

UNITS_IN"in" Inches.
UNITS_MM"mm" Millimeters.
UNITS_CM"cm" Centimeters.
UNITS_PC"pc" Picas (12 points).
UNITS_PT"pt" Points (1/72.27″).

Here are the methods defined on an instance of class FoDim.


Return self as a string suitable for use as an XSL-FO dimension attribute.

.__add__(self, other)

Assuming other is another FoDim instance, returns their sum. If the units are different, the result will take into account standard unit conversions. For example:

>>> import fohelpers as h
>>> print h.FoDim(3, h.UNITS_MM) + h.FoDim(6, h.UNITS_CM)
.__sub__(self, other)

For other another FoDim instance, returns their difference as a FoDim instance.

.__mul__(self, other)

Multiplies by a constant: other is any type acceptable to the decimal.Decimal() constructor. The result is a FoDim.

>>> import decimal as d
>>> print h.FoDim('2.5', h.UNITS_IN) * "1.5"
.__div__(self, other)

Divides by a constant; other is any type acceptable to the decimal.Decimal() constructor.


Returns self with the opposite sign.


Returns a FoDim instance converted to newUnits, which takes the same values as the units argument of the FoDim constructor.