Module

Hylograph.Flow.Render.Text

Package
purescript-hylograph-flow-render
Repository
afcondon/purescript-hylograph-flow-render

Words in a node, set without a DOM.

The layout is pure and runs in node, so it cannot ask a browser how wide a string is. It estimates. estimateWidth adds up an advance for each character from a table of five classes, in ems, taken from the metrics of Helvetica and Arial (the same numbers to within a hundredth of an em): narrow letters and marks a quarter of an em, f t r and brackets a third, m w M W near nine tenths, capitals seven tenths, and everything else (lower case, figures) 0.56. Semibold is taken as six per cent wider.

HOW WRONG IT CAN BE. For Helvetica, Arial and Liberation Sans the classes are within about 0.06 em of each glyph's true advance, and over a label of ordinary words and figures the errors mostly cancel. Measured once, in Chrome, on 276 lines of cell ranges and short phrases set in Helvetica Neue: what the browser drew was between 0.87 and 1.01 of the estimate, 0.97 at the median. So the estimate ran as much as a seventh too wide (a line wrapped that would have fitted) and a hundredth too narrow. A face of other proportions is another matter: a condensed face runs a fifth narrower, a wide one (Verdana) a fifth wider, and the estimate knows nothing of kerning, letter-spacing or a text-transform set by a stylesheet (upper-casing a label makes it a quarter wider than estimated). caution (1.08 by default) is the factor the estimate is multiplied by before a line is judged to fit, so that an estimate a little low still wraps in time; raise it for a wider face. A page can measure what was drawn (getComputedTextLength) against the estimate.

wrap breaks at spaces, and inside a word only where the word alone is wider than the line. Text that would need more than the lines allowed ends in an ellipsis on the last line.

#Words Source

type Words = { detail :: Maybe String, title :: String }

What is written in a node: a title, and under it some detail.

#SetWords Source

type SetWords = { detail :: Array String, title :: Array String }

Words as set: the title's lines and the detail's, top to bottom.

#Lettering Source

type Lettering = { caution :: Number, detailLines :: Int, detailSize :: Number, inset :: Number, leading :: Number, margin :: Number, outset :: Number, titleLines :: Int, titleSize :: Number }

titleSize, detailSize: font sizes, in pixels. leading: a line's height over its font size. margin: above the first line and below the last. inset, outset: from the box's left side to the text, and from the text to its right side. titleLines, detailLines: the most lines each may take. caution: see the module's header.

#advance Source

advance :: Char -> Number

A character's advance, in ems, at regular weight.

#estimateWidth Source

estimateWidth :: Number -> String -> Number

The width of a string at a font size, in pixels, at regular weight: an estimate (see the module's header).

#heavier Source

heavier :: Number

How much wider semibold is taken to be than regular.

#wrap Source

wrap :: { factor :: Number, most :: Int, size :: Number, width :: Number } -> String -> Array String

A string as lines no wider than width (as estimated, factor times over), at most most of them (and at least one).

#setWords Source

setWords :: Lettering -> Number -> Words -> SetWords

Words set in a box of a breadth: the title semibold, the detail regular, each wrapped to the room there is.

#heightOf Source

heightOf :: Lettering -> SetWords -> Number

How tall a box must be to hold words as set.

#breadthOf Source

breadthOf :: Lettering -> SetWords -> Number

How broad a box must be to hold words as set (0.5): the widest line, as estimated and judged (caution, and heavier for the title), with the inset and the outset. available of it is that widest line, so the words set in a box this broad are within their room.

#lineHeight Source

lineHeight :: Lettering -> Number -> Number

A line's height at a font size.

#available Source

available :: Lettering -> Number -> Number

The room for text in a box of a breadth.