Units

Lengths, angles and quantities, and the unit factories svg.mm, svg.inch, svg.deg, … A unit on its own is one of itself (height=svg.inch). Units SVG has no suffix for (metres, feet, miles, …) are written in the nearest SVG unit.

Lengths, angles and their units: svg.mm(5), 5 * svg.inch, svg.Length, svg.Angle, svg.Quantity.

Units SVG has no suffix for (metres, feet, miles, …) are written in the nearest SVG unit, so every value converts back to SVG.

class Angle(value: float, unit: str = 'deg')[source]

Bases: object

An angle with a unit; float(angle) is degrees, as every svgwriter rotation expects.

Any two angles add, subtract and compare whatever their units; plain numbers count as degrees, as in SVG. Positive angles turn from +x towards +y, which is clockwise on screen. Multiplying or dividing by lengths and angles gives a ~svgwriter.model.units.quantities.Quantity.

Parameters:
  • value (float) – The magnitude.

  • unit (str, optional) – "deg", "rad", "grad" or "turn", or a spelled-out name ("degrees", "radians", "gon", "turns", …). Defaults to “deg”.

Examples

>>> import svgwriter as svg
>>> float(svg.turn(0.25)), str(svg.rad(1) + 30)
(90.0, '1.5236rad')
>>> svg.rect(0, 0, 4, 2).rotate(svg.rad(math.pi / 2)).matrix.decompose()[2]
90.0
>>> str(Angle(2, "radians"))
'2rad'
value: float
unit: str
classmethod parse(text: str | float | Angle) → Angle[source]

An angle from "45deg", "1.2rad", "0.5turn", a number (degrees) or an angle.

Parameters:

text (str, float or Angle) – The value.

Returns:

Angle – The parsed angle.

Raises:
  • ValueError – If the text is not an angle.

  • TypeError – If the value is a length or another non-angle quantity.

Examples

>>> Angle.parse("0.5turn").degrees
180.0
property degrees: float

The angle in degrees.

property radians: float

The angle in radians.

to(unit: str) → Angle[source]

The same angle in another unit ("deg", "rad", "grad", "turn" or a spelled-out name).

wrap() → Angle[source]

The same direction within one turn, from 0 up to (not including) a full turn.

Returns:

Angle – The wrapped angle, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(-90).wrap()), str(svg.turn(1.25).wrap())
('270deg', '0.25turn')
diff(other: Angle | float, ccw: bool | None = None) → Angle[source]

The turn from this angle to other, the short way round unless a direction is given.

Parameters:
  • other (Angle or float) – The target angle; numbers are degrees.

  • ccw (bool, optional) – None takes the shorter arc (at most half a turn either way); True turns counter-clockwise on screen (negative angles) and False clockwise (positive angles). Defaults to None.

Returns:

Angle – The signed turn, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(350).diff(10)), str(svg.deg(350).diff(10, ccw=True))
('20deg', '-340deg')
lerp(other: Angle | float, t: float, ccw: bool | None = None) → Angle[source]

The angle a fraction t of the way to other, turning as diff does.

Parameters:
  • other (Angle or float) – The end angle; numbers are degrees.

  • t (float) – 0 gives this angle, 1 gives other (as the same direction).

  • ccw (bool, optional) – Turning direction, as in diff. Defaults to None (the shorter arc).

Returns:

Angle – The blended angle, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(350).lerp(10, 0.5))
'360deg'
clamp(low: Angle | float, high: Angle | float, circular: bool = False) → Angle[source]

This angle limited to the range from low to high.

Parameters:
  • low (Angle or float) – The limits; numbers are degrees.

  • high (Angle or float) – The limits; numbers are degrees.

  • circular (bool, optional) – Treat the range as the arc swept clockwise from low to high, so directions are compared modulo a turn and an angle outside snaps to the nearer end. Defaults to False (plain numeric clamping).

Returns:

Angle – The limited angle, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(100).clamp(0, 90)), str(svg.deg(5).clamp(350, 20, circular=True))
('90deg', '5deg')
>>> str(svg.deg(100).clamp(350, 20, circular=True))
'20deg'
sin() → float[source]

The sine of this angle.

cos() → float[source]

The cosine of this angle.

tan() → float[source]

The tangent of this angle.

arclength(radius: Length | float) → Length[source]

Length of the arc this angle sweeps on a circle of radius.

Parameters:

radius (Length or float) – The radius; numbers are user units.

Returns:

Length – The arc length, in the radius’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.turn(0.5).arclength(svg.mm(10)))
'31.4159mm'
svg_value(precision: int = 4) → str[source]

The angle as SVG writes it: degrees, without a unit.

class AngleUnit(unit: str)[source]

Bases: Angle

One of a unit, usable as an angle or as a factory: deg(30), 30 * deg and rotate=turn.

Parameters:

unit (str) – The unit, by suffix or spelled out ("deg", "radians").

Examples

>>> import svgwriter as svg
>>> str(30 * svg.deg), str(svg.turn(0.5)), str(svg.turn)
('30deg', '0.5turn', '1turn')
class Length(value: float, unit: Unit = Unit.NONE)[source]

Bases: object

A number with an optional unit.

The unit may be spelled out ("millimetres", "inches"); metres and feet are accepted too and stored as millimetres and inches, since SVG has no such units. Multiplying or dividing by other lengths and angles gives a ~svgwriter.model.units.quantities.Quantity (an area, for instance) that turns back into a length when the dimensions cancel.

Parameters:
  • value (float) – The magnitude.

  • unit (Unit or str, optional) – The unit; Unit.NONE means user units. Defaults to Unit.NONE.

See also

Unit

The available units.

Examples

>>> from svgwriter.model.units import Length, Unit
>>> str(Length(2.5, Unit.MM))
'2.5mm'
>>> Length.parse("1in").to_user_units()
96.0
>>> str(Length(1.5, "metres")), str(Length(2, "ft"))
('1500mm', '24in')
>>> Length(96) == 96, Length(1, "in") == 96, Length(1, "em") == 16
(True, True, False)
value: float
unit: Unit
classmethod parse(text: str) → Length[source]
classmethod parse(text: float) → Length
classmethod parse(text: Length) → Length

Parse length text such as "12.5mm".

Numbers become unitless lengths and Length values pass through unchanged.

Parameters:

text (str or float or Length) – The value to convert.

Returns:

Length – The parsed length.

Raises:
  • ValueError – If the text is not a valid length.

  • TypeError – If the value is an angle or another non-length quantity.

Examples

>>> from svgwriter.model.units import Length
>>> Length.parse("12.5mm")
Length(value=12.5, unit=<Unit.MM: 'mm'>)
>>> Length.parse(3).value
3.0
to_user_units(*, units_per_inch: float = 96.0, font_size: float = 16.0, reference: float | None = None) → float[source]

Convert to user units (pixels).

Absolute units use units_per_inch; em and ex use the font size.

Parameters:
  • units_per_inch (float, optional) – User units per inch for absolute units. Defaults to 96, as SVG defines.

  • font_size (float, optional) – Font size in pixels for em and ex. Defaults to the library default font size.

  • reference (float, optional) – The length that 100% resolves to. Defaults to None.

Returns:

float – The length in user units.

Raises:

ValueError – If the length is a percentage and no reference is given.

Examples

>>> from svgwriter.model.units import Length, Unit
>>> Length(1, Unit.IN).to_user_units(units_per_inch=72)
72.0
>>> Length(50, Unit.PERCENT).to_user_units(reference=200)
100.0
svg_value(precision: int = 4) → str[source]

Serialize the length as attribute text.

Parameters:

precision (int, optional) – Maximum number of decimal places. Defaults to the library default precision.

Returns:

str – The number followed by its unit.

Examples

>>> from svgwriter.model.units import Length, Unit
>>> Length(1.23456, Unit.PX).svg_value(2)
'1.23px'
property absolute: bool

Whether the unit converts to user units without context (not em, ex or %).

to(unit: Unit | str) → Length[source]

The same length in another absolute unit.

Parameters:

unit (Unit or str) – Target unit ("mm", "in", "px", "" for user units, …).

Returns:

Length – The converted length, at 96 user units per inch as SVG defines.

Raises:

ValueError – If either unit is relative (em, ex, %).

Examples

>>> import svgwriter as svg
>>> svg.inch(1).to("mm")
Length(value=25.4, unit=<Unit.MM: 'mm'>)
class LengthUnit(unit: Unit | str)[source]

Bases: Length

One of a unit, usable as a length or as a factory: mm(5), 5 * mm and height=inch.

A unit is a Length of 1 in that unit, so it goes anywhere a length does. Units SVG has no name for (metres, feet, miles…) are written in the nearest SVG unit.

Parameters:

unit (Unit or str) – The unit, by suffix or spelled out ("mm", "miles").

Examples

>>> import svgwriter as svg
>>> str(5 * svg.mm), str(svg.inch(2) - 10 * svg.mm), str(svg.units.ft(1))
('5mm', '1.6063in', '12in')
>>> svg.inch == 96, str(svg.units.mi(1))
(True, '63360in')
class Quantity(value: float, length: int = 0, angle: int = 0, length_unit: Unit | None = None, angle_unit: str = 'rad')[source]

Bases: object

A value of dimension length^n · angle^m, the result of multiplying or dividing lengths and angles.

Arithmetic works like a maths problem: products add exponents, quotients subtract them, and sums need equal dimensions. Results that are plain lengths, angles or numbers come back as Length, Angle or float, in the unit of the first length or angle involved.

Parameters:
  • value (float) – Magnitude in user units^length · radians^angle.

  • length (int, optional) – Exponents of length and angle. Default to 0.

  • angle (int, optional) – Exponents of length and angle. Default to 0.

  • length_unit (Unit, optional) – Unit used to show lengths and to give back a Length. Defaults to user units.

  • angle_unit (str, optional) – Unit used to show angles and to give back an Angle. Defaults to “rad”.

See also

dimension_name

How a dimension reads.

Examples

>>> import svgwriter as svg
>>> area = svg.mm(3) * svg.mm(2)
>>> str(area), area.dimension
('6mm²', 'length²')
>>> area / svg.mm(4)
Length(value=1.5, unit=<Unit.MM: 'mm'>)
>>> svg.mm(6) / svg.mm(3)
2.0
value: float
length: int
angle: int
length_unit: Unit | None
angle_unit: str
classmethod of(value: object) → Quantity[source]

The quantity behind a length, angle, number or quantity.

Parameters:

value (Length, Angle, Quantity or float) – The value; lengths must be absolute (not em, ex or %).

Returns:

Quantity – The value in base units with its exponents.

Raises:
  • TypeError – If the value is not a number, length, angle or quantity.

  • ValueError – If a length is relative.

Examples

>>> import svgwriter as svg
>>> Quantity.of(svg.inch(1)).value
96.0
property dimension: str

The dimension as text, such as "length²".

simplify() → Quantity | Length | Angle | float[source]

This value as a Length, Angle or float when its dimension allows.

Returns:

Quantity, Length, Angle or float – The simplest type for the dimension.

Examples

>>> Quantity(2.0, 0, 0).simplify()
2.0
class Unit(*values)[source]

Bases: Keyword

Length units: SVG 1.1’s plus CSS’s Q (quarter millimetres).

NONE (also USER) means user units. PIXEL and INCH are aliases of PX and IN. str(unit) gives the suffix as written in SVG.

See also

Length

A value with a unit.

Examples

>>> from svgwriter.model.units import Unit
>>> Unit("mm") is Unit.MM
True
>>> str(Unit.PERCENT)
'%'
NONE = ''
PX = 'px'
PT = 'pt'
PC = 'pc'
MM = 'mm'
CM = 'cm'
IN = 'in'
EM = 'em'
EX = 'ex'
PERCENT = '%'
Q = 'Q'
USER = ''
PIXEL = 'px'
INCH = 'in'
angle(value: float, unit: str = 'deg') → Angle[source]

An angle from a number and a unit, which may be spelled out.

Parameters:
  • value (float) – The magnitude.

  • unit (str, optional) – "deg", "rad", "grad", "turn" or a name such as "degrees", "radians", "gon", "turns". Defaults to “deg”.

Returns:

Angle – The angle.

Raises:

ValueError – If the unit is unknown.

Examples

>>> import svgwriter as svg
>>> str(svg.angle(0.25, "turns")), svg.angle(1, "radians").unit
('0.25turn', 'rad')
length(value: float, unit: Unit | str = Unit.NONE) → Length[source]

A length from a number and a unit, which may be spelled out.

Parameters:
  • value (float) – The magnitude.

  • unit (Unit or str, optional) – "mm", "in", "millimetres", "inches", "m", "ft", …; metres and feet are stored as millimetres and inches. Defaults to user units.

Returns:

Length – The length.

Raises:

ValueError – If the unit is unknown.

Examples

>>> import svgwriter as svg
>>> str(svg.length(2, "centimetres")), str(svg.length(0.5, "m"))
('2cm', '500mm')

Lengths with units (10, 2.5mm, 50%).

class Length(value: float, unit: Unit = Unit.NONE)[source]

Bases: object

A number with an optional unit.

The unit may be spelled out ("millimetres", "inches"); metres and feet are accepted too and stored as millimetres and inches, since SVG has no such units. Multiplying or dividing by other lengths and angles gives a ~svgwriter.model.units.quantities.Quantity (an area, for instance) that turns back into a length when the dimensions cancel.

Parameters:
  • value (float) – The magnitude.

  • unit (Unit or str, optional) – The unit; Unit.NONE means user units. Defaults to Unit.NONE.

See also

Unit

The available units.

Examples

>>> from svgwriter.model.units import Length, Unit
>>> str(Length(2.5, Unit.MM))
'2.5mm'
>>> Length.parse("1in").to_user_units()
96.0
>>> str(Length(1.5, "metres")), str(Length(2, "ft"))
('1500mm', '24in')
>>> Length(96) == 96, Length(1, "in") == 96, Length(1, "em") == 16
(True, True, False)
value: float
unit: Unit
classmethod parse(text: str) → Length[source]
classmethod parse(text: float) → Length
classmethod parse(text: Length) → Length

Parse length text such as "12.5mm".

Numbers become unitless lengths and Length values pass through unchanged.

Parameters:

text (str or float or Length) – The value to convert.

Returns:

Length – The parsed length.

Raises:
  • ValueError – If the text is not a valid length.

  • TypeError – If the value is an angle or another non-length quantity.

Examples

>>> from svgwriter.model.units import Length
>>> Length.parse("12.5mm")
Length(value=12.5, unit=<Unit.MM: 'mm'>)
>>> Length.parse(3).value
3.0
to_user_units(*, units_per_inch: float = 96.0, font_size: float = 16.0, reference: float | None = None) → float[source]

Convert to user units (pixels).

Absolute units use units_per_inch; em and ex use the font size.

Parameters:
  • units_per_inch (float, optional) – User units per inch for absolute units. Defaults to 96, as SVG defines.

  • font_size (float, optional) – Font size in pixels for em and ex. Defaults to the library default font size.

  • reference (float, optional) – The length that 100% resolves to. Defaults to None.

Returns:

float – The length in user units.

Raises:

ValueError – If the length is a percentage and no reference is given.

Examples

>>> from svgwriter.model.units import Length, Unit
>>> Length(1, Unit.IN).to_user_units(units_per_inch=72)
72.0
>>> Length(50, Unit.PERCENT).to_user_units(reference=200)
100.0
svg_value(precision: int = 4) → str[source]

Serialize the length as attribute text.

Parameters:

precision (int, optional) – Maximum number of decimal places. Defaults to the library default precision.

Returns:

str – The number followed by its unit.

Examples

>>> from svgwriter.model.units import Length, Unit
>>> Length(1.23456, Unit.PX).svg_value(2)
'1.23px'
property absolute: bool

Whether the unit converts to user units without context (not em, ex or %).

to(unit: Unit | str) → Length[source]

The same length in another absolute unit.

Parameters:

unit (Unit or str) – Target unit ("mm", "in", "px", "" for user units, …).

Returns:

Length – The converted length, at 96 user units per inch as SVG defines.

Raises:

ValueError – If either unit is relative (em, ex, %).

Examples

>>> import svgwriter as svg
>>> svg.inch(1).to("mm")
Length(value=25.4, unit=<Unit.MM: 'mm'>)
class Unit(*values)[source]

Bases: Keyword

Length units: SVG 1.1’s plus CSS’s Q (quarter millimetres).

NONE (also USER) means user units. PIXEL and INCH are aliases of PX and IN. str(unit) gives the suffix as written in SVG.

See also

Length

A value with a unit.

Examples

>>> from svgwriter.model.units import Unit
>>> Unit("mm") is Unit.MM
True
>>> str(Unit.PERCENT)
'%'
NONE = ''
PX = 'px'
PT = 'pt'
PC = 'pc'
MM = 'mm'
CM = 'cm'
IN = 'in'
EM = 'em'
EX = 'ex'
PERCENT = '%'
Q = 'Q'
USER = ''
PIXEL = 'px'
INCH = 'in'

Angles with units: degrees, radians, gradians and turns, converted and combined freely.

class Angle(value: float, unit: str = 'deg')[source]

Bases: object

An angle with a unit; float(angle) is degrees, as every svgwriter rotation expects.

Any two angles add, subtract and compare whatever their units; plain numbers count as degrees, as in SVG. Positive angles turn from +x towards +y, which is clockwise on screen. Multiplying or dividing by lengths and angles gives a ~svgwriter.model.units.quantities.Quantity.

Parameters:
  • value (float) – The magnitude.

  • unit (str, optional) – "deg", "rad", "grad" or "turn", or a spelled-out name ("degrees", "radians", "gon", "turns", …). Defaults to “deg”.

Examples

>>> import svgwriter as svg
>>> float(svg.turn(0.25)), str(svg.rad(1) + 30)
(90.0, '1.5236rad')
>>> svg.rect(0, 0, 4, 2).rotate(svg.rad(math.pi / 2)).matrix.decompose()[2]
90.0
>>> str(Angle(2, "radians"))
'2rad'
value: float
unit: str
classmethod parse(text: str | float | Angle) → Angle[source]

An angle from "45deg", "1.2rad", "0.5turn", a number (degrees) or an angle.

Parameters:

text (str, float or Angle) – The value.

Returns:

Angle – The parsed angle.

Raises:
  • ValueError – If the text is not an angle.

  • TypeError – If the value is a length or another non-angle quantity.

Examples

>>> Angle.parse("0.5turn").degrees
180.0
property degrees: float

The angle in degrees.

property radians: float

The angle in radians.

to(unit: str) → Angle[source]

The same angle in another unit ("deg", "rad", "grad", "turn" or a spelled-out name).

wrap() → Angle[source]

The same direction within one turn, from 0 up to (not including) a full turn.

Returns:

Angle – The wrapped angle, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(-90).wrap()), str(svg.turn(1.25).wrap())
('270deg', '0.25turn')
diff(other: Angle | float, ccw: bool | None = None) → Angle[source]

The turn from this angle to other, the short way round unless a direction is given.

Parameters:
  • other (Angle or float) – The target angle; numbers are degrees.

  • ccw (bool, optional) – None takes the shorter arc (at most half a turn either way); True turns counter-clockwise on screen (negative angles) and False clockwise (positive angles). Defaults to None.

Returns:

Angle – The signed turn, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(350).diff(10)), str(svg.deg(350).diff(10, ccw=True))
('20deg', '-340deg')
lerp(other: Angle | float, t: float, ccw: bool | None = None) → Angle[source]

The angle a fraction t of the way to other, turning as diff does.

Parameters:
  • other (Angle or float) – The end angle; numbers are degrees.

  • t (float) – 0 gives this angle, 1 gives other (as the same direction).

  • ccw (bool, optional) – Turning direction, as in diff. Defaults to None (the shorter arc).

Returns:

Angle – The blended angle, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(350).lerp(10, 0.5))
'360deg'
clamp(low: Angle | float, high: Angle | float, circular: bool = False) → Angle[source]

This angle limited to the range from low to high.

Parameters:
  • low (Angle or float) – The limits; numbers are degrees.

  • high (Angle or float) – The limits; numbers are degrees.

  • circular (bool, optional) – Treat the range as the arc swept clockwise from low to high, so directions are compared modulo a turn and an angle outside snaps to the nearer end. Defaults to False (plain numeric clamping).

Returns:

Angle – The limited angle, in this angle’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.deg(100).clamp(0, 90)), str(svg.deg(5).clamp(350, 20, circular=True))
('90deg', '5deg')
>>> str(svg.deg(100).clamp(350, 20, circular=True))
'20deg'
sin() → float[source]

The sine of this angle.

cos() → float[source]

The cosine of this angle.

tan() → float[source]

The tangent of this angle.

arclength(radius: Length | float) → Length[source]

Length of the arc this angle sweeps on a circle of radius.

Parameters:

radius (Length or float) – The radius; numbers are user units.

Returns:

Length – The arc length, in the radius’s unit.

Examples

>>> import svgwriter as svg
>>> str(svg.turn(0.5).arclength(svg.mm(10)))
'31.4159mm'
svg_value(precision: int = 4) → str[source]

The angle as SVG writes it: degrees, without a unit.

Products and quotients of lengths and angles: values carrying dimension exponents.

mm(3) * mm(2) is an area, deg(90) / mm(5) an angle per length. Each Quantity stores its magnitude in base units (user units for length, radians for angle) with the exponents of both; once a calculation comes back to a plain length, angle or number it is a Length, an Angle or a float again.

class Quantity(value: float, length: int = 0, angle: int = 0, length_unit: Unit | None = None, angle_unit: str = 'rad')[source]

Bases: object

A value of dimension length^n · angle^m, the result of multiplying or dividing lengths and angles.

Arithmetic works like a maths problem: products add exponents, quotients subtract them, and sums need equal dimensions. Results that are plain lengths, angles or numbers come back as Length, Angle or float, in the unit of the first length or angle involved.

Parameters:
  • value (float) – Magnitude in user units^length · radians^angle.

  • length (int, optional) – Exponents of length and angle. Default to 0.

  • angle (int, optional) – Exponents of length and angle. Default to 0.

  • length_unit (Unit, optional) – Unit used to show lengths and to give back a Length. Defaults to user units.

  • angle_unit (str, optional) – Unit used to show angles and to give back an Angle. Defaults to “rad”.

See also

dimension_name

How a dimension reads.

Examples

>>> import svgwriter as svg
>>> area = svg.mm(3) * svg.mm(2)
>>> str(area), area.dimension
('6mm²', 'length²')
>>> area / svg.mm(4)
Length(value=1.5, unit=<Unit.MM: 'mm'>)
>>> svg.mm(6) / svg.mm(3)
2.0
value: float
length: int
angle: int
length_unit: Unit | None
angle_unit: str
classmethod of(value: object) → Quantity[source]

The quantity behind a length, angle, number or quantity.

Parameters:

value (Length, Angle, Quantity or float) – The value; lengths must be absolute (not em, ex or %).

Returns:

Quantity – The value in base units with its exponents.

Raises:
  • TypeError – If the value is not a number, length, angle or quantity.

  • ValueError – If a length is relative.

Examples

>>> import svgwriter as svg
>>> Quantity.of(svg.inch(1)).value
96.0
property dimension: str

The dimension as text, such as "length²".

simplify() → Quantity | Length | Angle | float[source]

This value as a Length, Angle or float when its dimension allows.

Returns:

Quantity, Length, Angle or float – The simplest type for the dimension.

Examples

>>> Quantity(2.0, 0, 0).simplify()
2.0
dimension_name(length: int, angle: int) → str[source]

How a dimension reads in messages: "length²", "length·angle⁻¹", "number".

Parameters:
  • length (int) – The exponents of length and angle.

  • angle (int) – The exponents of length and angle.

Returns:

str – The dimension written with superscript exponents.

Examples

>>> from svgwriter.model.units.quantities import dimension_name
>>> dimension_name(2, 0), dimension_name(1, -1), dimension_name(0, 0)
('length²', 'length·angle⁻¹', 'number')