Measuring and wrapping text in OpenSCAD by arpruss 3d model
3dmdb logo
Thingiverse
Measuring and wrapping text in OpenSCAD by arpruss

Measuring and wrapping text in OpenSCAD by arpruss

by Thingiverse
Last crawled date: 4 years, 2 months ago
OpenSCAD does not have built-in functions for measuring text size and word wrapping text. This library provides these.
Make sure you have fontmetricsdata.scad and fontmetrics.scad in your current directory and that you start your code with
use ;
Public modules:
drawWrappedText(text,font="Liberation Sans",size=10,spacing=1,linespacing=1,indent=0,width=800,halign="left"): wrap text to the specified width; you can have multiple paragraphs separated by \n, and you can choose "left", "right", and "justify" as the alignment
drawText(): a partial clone (minus the language, script and only good for left-to-right) of the built-in text(), but with greater rendering fidelity at small font sizes
Public functions:
measureText(text,font="Liberation Sans",size=10.,spacing=1.): measure the width of text; this is, technically, the x-advance of the text, so the text--especially in italics--may stick out the right hand side
measureTextBounds(text,font="Liberation Sans",size=10.,spacing=1.,valign="baseline",halign="left"): returns [[leftX,bottomY],[width,height]] of the rectangle taken up by the actual text; curves near edges may slightly spill out
measureWrappedText(text,font="Liberation Sans",size=10,spacing=1,linespacing=1,indent=0,width=800,halign="left"): same as measureTextBounds() except that it measures the text as rendered by drawWrappedText() instead of drawText()/text().
measureTextAscender(text,font="Liberation Sans",size=10.,valign="baseline"): measures the ascenders of the single-line text (height above baseline)
measureTextDescender(text,font="Liberation Sans",size=10.,valign="baseline"): measures the bottom of the single-line text relative to the baseline (typically negative)
measureLeftBearing(text,font="Liberation Sans",size=10.): measures the distance from the left of the beginning of the shape of the first glyph in text (single line)
measureRightBearing(text,font="Liberation Sans",size=10.): measures the distance from the right-advance of the text to the right-hand-side of the shape of the last glyph in text (single line)
ascender(font="Liberation Sans",size=10.): the ascender size reported by the font
descender(font="Liberation Sans",size=10.): the descender size reported by the font
The library includes metrics and kerning information for the OpenSCAD built-in Liberation fonts plus all the text fonts in the Microsoft Core Fonts package. It won't work with other fonts, unless you use my Python scripts to generate more font information.
Updates:
08/22/2019: Bug fixes
07/18/2018: Webdings support
07/17/2018: tweak side-bearing and ascender and descender measurement; change return format for bounds; add text wrapping

Tags