Discussion:
[OpenSCAD] length of a text object
k***@volksswitch.org
2018-11-28 04:00:57 UTC
Permalink
Does anyone know how to determine the length of a text object at run time
(i.e., report its value with an echo command)?



Thanks,

Ken
nop head
2018-11-28 07:58:42 UTC
Permalink
I don't think that is possible but you can make it a specific size using
resize().
Post by k***@volksswitch.org
Does anyone know how to determine the length of a text object at run time
(i.e., report its value with an echo command)?
Thanks,
Ken
_______________________________________________
OpenSCAD mailing list
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
runsun
2018-11-28 18:16:01 UTC
Permalink
i recall seeing a post in thingiverse doing just that. Too bad I forgot to
make a ref to it at that time, now I can't find it.



-----

$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer ); $ Tips ; $ Snippets

--
Sent from: http://forum.openscad.org/
runsun
2018-11-28 18:29:36 UTC
Permalink
Before you find a better solution, here is an ugly code that I came up with
years ago. I don't like it but it's the only thing I can find now. Check out
this <https://github.com/runsun/OpenSCAD_Tips/blob/master/snippets.md> for
the *hash()* and you also need to make a *sum(...)*. The *isnum()* is:
<code>floor(x)==x? true:( abs(x)+1>abs(x))</code>.
function textWidth( txt, size=10, scale=1 )=
( // A wild guess of what the total width of txt is.
// symbol width ratios in reference to average char
// width that is decided by "size"
let(symbol_w_ratios =
[
"P", 0.906, "Q", 1.055 , "R", 0.98, ".", 0.3768, "1", 0.654
, "2", 0.7542, "3", 0.7542, "4", 0.7542, "5", 0.7542, "6", 0.7542
, "7", 0.7542, "8", 0.7542, "9", 0.7542, "S", 0.905, "T", 0.828
, "U", 0.98 , "V", 0.906, "W", 1.282, "X", 0.906, "Y", 0.906
, "Z", 0.828, "0", 0.754, "A", 0.903, "B", 0.905, "C", 0.98
, "D", 0.98, "E", 0.909, "F", 0.83, "G", 1.055, "H", 0.98
, "I", 0.38, "J", 0.676, "K", 0.905, "L", 0.755, "M", 1.13
, "N", 0.98, "O", 1.055 , "/", 0.3768, " ", 0.3768, "+", 0.792
, "-", 0.452, "*", 0.5277, "(", 0.452, ")", 0.452, "a", 0.755
, "b", 0.755, "c", 0.678, "d", 0.754, "e", 0.754, "f", 0.35
, "g", 0.754, "h", 0.754, "i", 0.301, "j", 0.301, "k", 0.679
, "l", 0.301, "m", 1.13, "n", 0.754, "o", 0.754 , "p", 0.754
, "q", 0.754 , "r", 0.452, "s", 0.678, "t", 0.377, "u", 0.754
, "v", 0.678, "w", 0.9795, "x", 0.6785, "y", 0.678, "z", 0.678
, "!", 0.3767, "\"", 0.4815, "#", 0.7543, "$", 0.7543, "%", 1.206
, "&", 0.904 , "'", 0.259, ",", 0.3768, ":", 0.3768, ";", 0.3768
, "[", 0.3768, "]", 0.3768, "\\", 0.3768, "^", 0.6365, "`", 0.4518
, "_", 0.75, "|", 0.3522, "~", 0.7921, "{", 0.3625, "}", 0.3625
, chDEG, 0.5423
]
, sz= isnum(size)?size:size[0]//-1
, sc= isnum(scale)?scale:scale[0]
, expwidth = sum( [ for(i=range(txt))
hash( symbol_w_ratios, txt[i],0.6
)*sz*sc //, sz*sc)
] )
)
expwidth
);
-----

$ Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets

--
Sent from: http://forum.openscad.org/

Loading...