length of a text object

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

length of a text object

khackbarth

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
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: length of a text object

nophead
I don't think that is possible but you can make it a specific size using resize().

On Wed, 28 Nov 2018, 04:01 <[hidden email] wrote:

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
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: length of a text object

JordanBrown
In reply to this post by khackbarth
On 11/27/2018 8:00 PM, [hidden email] wrote:

Does anyone know how to determine the length of a text object at run time (i.e., report its value with an echo command)?


Do you mean length as in the number of characters, or length as in the width of the resulting object?

If you mean the number of characters, the len( ) function does the trick.

If you mean the width... no, though there is a feature that might let you avoid needing to know.� If you need to center the text object against something else, or you need to align its right end rather than its left end, you can use the "halign" parameter of the text( ) function to specify where the 0,0 point of the object is.� Similarly, though you don't ask about it, the valign parameter will let you specify vertical alignment.

For example:
text("ABC", halign="center", valign="center");
will yield "ABC" centered around 0,0.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: length of a text object

runsun
In reply to this post by khackbarth
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 );&nbsp;$ Tips ;&nbsp;$ Snippets

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: length of a text object

runsun
In reply to this post by khackbarth
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.7921, "=", 0.7921, ">", 0.7921, "?", 0.7542, "@", 1.3766
>        , "[", 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/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets