meta data for this page
This is an old revision of the document!
Text
Text can be used in designs to create the outline of charaters, which can then be used to create either 2D or 3D shapes.
NOTE: Only the ASCII characters can be used today.
Single Characters
A single character can be converted into an outline, i.e. an array of line segments. The character height
as well as the character position can be requested by specifying options. Note: The height
is the height of UPPERCASE characters.
Defaults:
- xOffset : 0
- yOffset : 0
- height : 21
- extrudeOffset : 0
- font : 'hershey simplex' (built in)
let outlines = vectorChar('H'); let outlines = vectorChar({ height: 50 }, 'i');
The above examples only create the outlines of the character. In order to convert to something useful, the following can be used to convert the outlines (segments) into paths.
const segmentToPath = (segment) => { return geometries.path2.fromPoints({close: false}, segment) } let paths = outlines.segments.map((segment) => segmentToPath(segment))
And of course, the paths can be used to create additional shapes, including 3D shapes of the characters.
Text Strings
In addition to single characters, complete text strings can be converted to a series of outlines, i.e. small line segments. Text can be split into multiple lines by including '\n'. And of course, text can be aligned and spaced by specifying options.
Defaults:
- xOffset : 0
- yOffset : 0
- height : 21
- lineSpacing = 1.4
- letterSpacing = 1
- align = 'left'
- extrudeOffset : 0
- font : 'hershey simplex' (built in)
let outlines = vectorText('JSCAD'); let outlines = vectorText({ yOffset: -90, height: 10, extrudeOffset: 2, input: 'JSCAD is awesome!' }); let outlines = vectorText({ height: 25, align: 'right', lineSpacing: 2.2, extrudeOffset: 2 }, 'JSCAD\nRocks!!');
Using Other Fonts
Other fonts can be used when creating the text outlines. Here are the steps.
- Download one or more font files from the fonts/vector directory
- Include the font file in the design
- Reference the font object
- Specify the 'font' in the options to vectorChar() or vectorText()
Note: Font objects append 'Font' to the font name, e.g. camBamStick1Font.
Default:
- font: hershey simplex
include('https://www.openjscad.org/fonts/camBamStick1.js'); function main (params) { let myfont = camBamStick1Font; // font object (! NOTE the "Font" suffix) let text = vectorText({font: myfont, height: 5},'OpenJSCAD'); return csgFromSegments(text); } function csgFromSegments (segments) { let output = []; segments.forEach(segment => output.push( rectangular_extrude(segment, { w:2, h:1 }) )); return union(output); }
Special Note: These fonts are NOT TrueType fonts. There are libraries that create outlines from TrueType (and other) fonts.