

Sctx.font = (style + " 20px " + fontname).trim() įor (i = 0 i \n" + widths.toString() ĭocument.getElementById("output").innerHTML = outputhtml ĭocument.getElementById('usage').innerHTML = "Usage\nThe array above shows character width as a multiple of font size. Var canvas = document.getElementById("canvas") You then multiply this width by the font size to get the character width. Then you use OpenSCAD's ord() function to convert any character to a numeric code, which then serves as an index of the width array.
#OPENSCAD $FN CODE#
It then outputs a line of OpenSCAD code that you can paste into your OpenSCAD script. Then it generates an array of widths for every character from 0 to 255, dividing each by 20 to get a unitless width proportion compared to font size. So what this code does is use a dummy canvas on the page to get a context to which a font is assigned, arbitrarily 20 pixels in size. The secret sauce lies in the fact that JavaScript's 'canvas' support has a 'measureText()' method that measures the pixel length of any text for a given font, used like this: asureText(string).width Just input the font properties, click the button, and scroll down to see usage instructions. and the code is reproduced below, which you can run from this reply, or paste into your own HTML file and load into your browser locally. The tool to generate the OpenSCAD width array is here: From there it's trivial to get the width of a string, or the angular widths of characters wrapped around a cylinder. Then for any given character, you multiply this proportion by the font size to get the width of an individual character. I made a JavaScript thing that lets you input a font name and style, and it outputs an array of width proportions for ascii and extended ascii characters (codes 0-255). I have found a way to determine the widths of text characters in OpenSCAD.
