Portability | GHC |
---|---|
Stability | unstable |
Maintainer | stephen.tetley@gmail.com |
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 PtScale
- ptSizeScale :: PtScale -> PtSize -> PtSize
- mono_width :: PtScale
- mono_cap_height :: PtScale
- mono_x_height :: PtScale
- mono_descender :: PtScale
- mono_ascender :: PtScale
- mono_left_margin :: PtScale
- mono_right_margin :: PtScale
- charWidth :: FontSize -> PtSize
- textWidth :: FontSize -> CharCount -> PtSize
- capHeight :: FontSize -> PtSize
- xcharHeight :: FontSize -> PtSize
- totalCharHeight :: FontSize -> PtSize
- ascenderHeight :: FontSize -> PtSize
- descenderDepth :: FontSize -> PtSize
- textBounds :: (Num u, Ord u, FromPtSize u) => FontSize -> Point2 u -> String -> BoundingBox u
- textBoundsEsc :: (Num u, Ord u, FromPtSize u) => FontSize -> Point2 u -> EscapedText -> BoundingBox u
- 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.
ptSizeScale :: PtScale -> PtSize -> PtSizeSource
ptSizeScale
: scale_factor -> pt_size -> PTSize
Scale the point size by the scale factor.
Scaling values derived from Courier
The ratio of width to point size of a letter in Courier.
mono_width = 0.6
mono_cap_height :: PtScaleSource
The ratio of cap height to point size of a letter in Courier.
mono_cap_height = 0.562
mono_x_height :: PtScaleSource
The ratio of x height to point size of a letter in Courier.
This is also known as the "body height".
mono_x_height = 0.426
mono_descender :: PtScaleSource
The ratio of descender depth to point size of a letter in Courier.
mono_descender = -0.157
mono_ascender :: PtScaleSource
The ratio of ascender to point size of a letter in Courier.
mono_ascender = 0.629
mono_left_margin :: PtScaleSource
The left margin for the bounding box of printed text as a ratio to point size for Courier.
mono_left_margin = -0.046
mono_right_margin :: PtScaleSource
The right margin for the bounding box of printed text as a ratio to point size for Courier.
mono_right_margin = 0.050
Courier metrics
charWidth :: FontSize -> PtSizeSource
Approximate the width of a monospace character using metrics derived from the Courier font.
textWidth :: FontSize -> CharCount -> PtSizeSource
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 -> PtSizeSource
Height of capitals e.g. 'A' using metrics derived the Courier monospaced font.
xcharHeight :: FontSize -> PtSizeSource
Height of the lower-case char 'x' using metrics derived the Courier monospaced font.
totalCharHeight :: FontSize -> PtSizeSource
The total height span of the glyph bounding box for the Courier monospaced font.
ascenderHeight :: FontSize -> PtSizeSource
Ascender height for font size sz
using metrics from the
Courier monospaced font.
descenderDepth :: FontSize -> PtSizeSource
Descender depth for font size sz
using metrics from the
Courier monospaced font.
Size calculation
textBounds :: (Num u, Ord u, FromPtSize u) => FontSize -> Point2 u -> String -> BoundingBox uSource
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 :: (Num u, Ord u, FromPtSize u) => FontSize -> Point2 u -> EscapedText -> BoundingBox uSource
textBoundsEsc
: font_size * baseline_left * escaped_text -> BBox
Version of textBounds for already escaped text.