borgar / textbox

Library to layout text for display on SVG or Canvas.
Other
26 stars 6 forks source link

Support whitespace options when measuring text #24

Closed flother closed 2 years ago

flother commented 2 years ago

It can be useful to have control over whitespace. At the moment the measure function in lib/measure/index.js assumes you want "normal" HTML/SVG whitespace handling (like CSS's white-space: normal). This PR adds two arguments to the function that allow the caller to control whether the whitespace is trimmed or collapsed. It also makes the three measuring handlers consistent: before now, only the canvas handler trimmed and collapsed whitespace; the SVG and "dumb" handler left the text as-is.

When there are line breaks in the text but we're either not trimming whitespace or not collapsing whitespace, the function does what HTML's input element does and converts all "\n" to " ".

Fixes #23.