Portability | GHC |
---|---|

Stability | unstable |

Maintainer | stephen.tetley@gmail.com |

Safe Haskell | None |

Approximate glyph size calculations for Label's and their bounding boxes.

Calculations are based on metrics derived from the Courier font. As Courier is a monospaced font, applying these metrics to other font families will usually produce over-estimates (bounding boxes will be longer than the true visual length of the text). Furthermore, even italic or bold Courier will have different metrics.

This is a deficiency of Wumpus, and limits its text handling capabilities - for example, text cannot be reliably centered or right aligned as its true length is not known. However, more powerful alternatives would need access to the metrics embedded within font files. This would require a font loader and add significant implementation complexity.

- type FontSize = Int
- type CharCount = Int
- data AfmUnit
- afmUnit :: FontSize -> Double -> AfmUnit
- afmValue :: FontSize -> AfmUnit -> Double
- mono_width :: AfmUnit
- mono_cap_height :: AfmUnit
- mono_x_height :: AfmUnit
- mono_descender :: AfmUnit
- mono_ascender :: AfmUnit
- mono_left_margin :: AfmUnit
- mono_right_margin :: AfmUnit
- charWidth :: FontSize -> Double
- textWidth :: FontSize -> CharCount -> Double
- capHeight :: FontSize -> Double
- xcharHeight :: FontSize -> Double
- totalCharHeight :: FontSize -> Double
- ascenderHeight :: FontSize -> Double
- descenderDepth :: FontSize -> Double
- textBounds :: FontSize -> DPoint2 -> String -> BoundingBox Double
- textBoundsEsc :: FontSize -> DPoint2 -> EscapedText -> BoundingBox Double
- charCount :: String -> CharCount

# Type synonyms

Wrapped Double representing 1/1000 of the scale factor (Point size) of a font. AFM files encode all measurements as these units.

afmUnit :: FontSize -> Double -> AfmUnitSource

Compute the size of a measurement in Afm units scaled by the point size of the font.

afmValue :: FontSize -> AfmUnit -> DoubleSource

Compute the size of a measurement in PostScript points scaling the Afm unit size by the point size of the font.

# Scaling values derived from Courier

The ratio of width to point size of a letter in Courier.

mono_width = 600

mono_cap_height :: AfmUnitSource

The ratio of cap height to point size of a letter in Courier.

mono_cap_height = 562

mono_x_height :: AfmUnitSource

The ratio of x height to point size of a letter in Courier.

This is also known as the "body height".

mono_x_height = 426

mono_descender :: AfmUnitSource

The ratio of descender depth to point size of a letter in Courier.

mono_descender = -157

mono_ascender :: AfmUnitSource

The ratio of ascender to point size of a letter in Courier.

mono_ascender = 629

mono_left_margin :: AfmUnitSource

The left margin for the bounding box of printed text as a ratio to point size for Courier.

mono_left_margin = -46

mono_right_margin :: AfmUnitSource

The right margin for the bounding box of printed text as a ratio to point size for Courier.

mono_right_margin = 50

# Courier metrics

charWidth :: FontSize -> DoubleSource

Approximate the width of a monospace character using metrics derived from the Courier font.

textWidth :: FontSize -> CharCount -> DoubleSource

`textWidth`

: ` font_size * char_count -> PtSize `

Text width at the supplied font_size. It is expected that the
`char_ount`

has been calculated with the `charCount`

function.

NOTE - this does not account for any left and right margins around the printed text.

capHeight :: FontSize -> DoubleSource

Height of capitals e.g. 'A' using metrics derived the Courier monospaced font.

xcharHeight :: FontSize -> DoubleSource

Height of the lower-case char 'x' using metrics derived the Courier monospaced font.

totalCharHeight :: FontSize -> DoubleSource

The total height span of the glyph bounding box for the Courier monospaced font.

ascenderHeight :: FontSize -> DoubleSource

Ascender height for font size `sz`

using metrics from the
Courier monospaced font.

descenderDepth :: FontSize -> DoubleSource

Descender depth for font size `sz`

using metrics from the
Courier monospaced font.

# Size calculation

textBounds :: FontSize -> DPoint2 -> String -> BoundingBox DoubleSource

`textBounds`

: ` font_size * baseline_left * text -> BBox `

Find the bounding box for the character count at the supplied font-size.

The supplied point represents the baseline left corner of the a regular upper-case letter (that is without descenders). The bounding box adds a margin around all sides of the text.

The metrics used are derived from Courier - a monospaced font. For proportional fonts the calculated bounding box will usually be too long.

textBoundsEsc :: FontSize -> DPoint2 -> EscapedText -> BoundingBox DoubleSource

`textBoundsEsc`

: ` font_size * baseline_left * escaped_text -> BBox `

Version of textBounds for already escaped text.