Is multiline text in bounding box possible?

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

Is multiline text in bounding box possible?

Troberg
I want to put some text, automatically wrapped within a bounding box.
Something like a module with a signature somewhat like this:

module textbox(text, font, textsize, xsize,ysize)

What I'm missing is any way to know the dimensions of a string.

I can't see a way to do this, but sometimes I'm stupid and missing something
obvious, so I'll throw the question out here.



--
Sent from: http://forum.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: Is multiline text in bounding box possible?

MichaelAtOz
Administrator
Short answer, No.
Long answer has lots of font tech reasons.

Possibly for 2035 climate compliant version...



-----
Admin - email* me if you need anything,  or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything, or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: Is multiline text in bounding box possible?

cacb
In reply to this post by Troberg
On 26.03.2020 13:32, Troberg wrote:
> I want to put some text, automatically wrapped within a bounding box.
> Something like a module with a signature somewhat like this:
>
> module textbox(text, font, textsize, xsize,ysize)
>
> What I'm missing is any way to know the dimensions of a string.
>
> I can't see a way to do this, but sometimes I'm stupid and missing something
> obvious, so I'll throw the question out here.

You can't do that in OpenSCAD. In general you can't query the model for
information such as dimensions.

It is possible in other systems
https://gist.github.com/arnholm/23c482530fd1882c615f5e6e3c9eedca

Carsten Arnholm

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

Re: Is multiline text in bounding box possible?

tp3
On 26.03.20 22:50, Carsten Arnholm wrote:
> It is possible in other systems
> https://gist.github.com/arnholm/23c482530fd1882c615f5e6e3c9eedca

I don't think this is doing what was asked for ("automatically
wrapped within a bounding box").

ciao,
  Torsten.

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

Re: Is multiline text in bounding box possible?

cacb
On 26.03.2020 23:02, Torsten Paul wrote:
> On 26.03.20 22:50, Carsten Arnholm wrote:
>> It is possible in other systems
>> https://gist.github.com/arnholm/23c482530fd1882c615f5e6e3c9eedca
>
> I don't think this is doing what was asked for ("automatically
> wrapped within a bounding box").

Everyone is free to think what they want, but that's what

    boundingbox@ b = txt2d.box();

does.

Carsten Arnholm

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

Re: Is multiline text in bounding box possible?

tp3
On 26.03.20 23:15, Carsten Arnholm wrote:

> On 26.03.2020 23:02, Torsten Paul wrote:
>> On 26.03.20 22:50, Carsten Arnholm wrote:
>>> It is possible in other systems
>>> https://gist.github.com/arnholm/23c482530fd1882c615f5e6e3c9eedca
>>
>> I don't think this is doing what was asked for ("automatically
>> wrapped within a bounding box").
>
> Everyone is free to think what they want, but that's what
>
>    boundingbox@ b = txt2d.box();
>
> does.

Automatically wrapped means, the size of the box is given
and the text will automatically break the text to fit that
box from a single line string.
Basically doing a layout with a flexible number of lines
depending of the width of the defined box.

You say that's what that code line is doing?

ciao,
  Torsten.



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

Re: Is multiline text in bounding box possible?

lar3ry
In reply to this post by Troberg
Troberg wrote
> I want to put some text, automatically wrapped within a bounding box.
> Something like a module with a signature somewhat like this:
>
> module textbox(text, font, textsize, xsize,ysize)
>
> What I'm missing is any way to know the dimensions of a string.

I knew I saw it somewhere, and I finally found it. Unfortunately, I have not
been able to get it to work from either the Thingiverse link or the github
link, each for a different reason.
Perhaps soemone more knowledgeable than I can figure out the errors.

https://www.thingiverse.com/thing:3004457
https://github.com/arpruss/miscellaneous-scad

There is no demo in the github repository.




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

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

Re: Is multiline text in bounding box possible?

tp3
On 26.03.20 23:57, lar3ry wrote:
> https://www.thingiverse.com/thing:3004457
> https://github.com/arpruss/miscellaneous-scad
>
> There is no demo in the github repository.

Nice. The example from Thingiverse works fine. You
need both the fontmetrics.scad and fontmetricsdata.scad
files.

With that:

use <fontmetrics.scad>

text="Lorem ipsum dolor sit amet, quibusdam integer ut condimentum id interdum, in convallis varius nibh consectetuer ut, sed mauris in ut
vel, phasellus non eu tortor quis ante. Nisl lobortis donec, blandit enim pulvinar a wisi suscipit ut, volutpat magna ad ornare metus orci,
id tempor dapibus sit sed ultricies. Magna nibh libero, viverra suspendisse auctor. Egestas magna nullam pulvinar consequat, a enim eu
ornare quis, lacus quis dui id quisque eros. Vitae dignissim dolor sociis, fusce elit a sit, eget posuere sit neque, vivamus enim luctus
etiam et, vitae pellentesque. Sed proin mauris consectetuer tempor auctor, et malesuada magna a torquent non et, mi arcu vitae ligula.";

drawWrappedText(text, font = "Liberation Sans", size = 10, spacing = 1, linespacing = 1, indent = 0, width = 250, halign = "left");

does exactly what it says, it wraps by word at the given
width.

ciao,
  Torsten.

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

Re: Is multiline text in bounding box possible?

lar3ry

> Nice. The example from Thingiverse works fine. You
> need both the fontmetrics.scad and fontmetricsdata.scad
> files.

Ahh... I just checked, and I had downloaded it in July 1018, so it was
obviously out of date.
It failed with a 'variable size not specified as parameter'

There is a lot of interesting looking stuff on the github repository.



--
Sent from: http://forum.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: Is multiline text in bounding box possible?

JordanBrown
In reply to this post by cacb
On 3/26/2020 2:50 PM, Carsten Arnholm wrote:
You can't do that in OpenSCAD. In general you can't query the model for information such as dimensions.

This is kind of a special case.  Although you can't ask anything about the results of the text() operator, you could have a function that did the same underlying *font* processing and gave you text metric information without ever getting to the geometry processing.

Just like you can't get the bounding box of a polygon, but you can get the bounding box of the array of points that you feed to polygon().

Not that you can do it today, but it's not the same as the general problem of querying geometry.

Michael says it's hard to do internally.  I don't know anything about Qt so I don't know.

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

Re: Is multiline text in bounding box possible?

JordanBrown
On 3/26/2020 8:30 PM, Jordan Brown wrote:
Michael says it's hard to do internally.  I don't know anything about Qt so I don't know.

On looking a little deeper, it's FreeType that I don't know anything about.


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

Re: Is multiline text in bounding box possible?

MichaelAtOz
Administrator
I did look into it a while ago, there are bounding box functions which could
get the data without invoking CGAL, but it takes a particular mix of skills
to write that, which I don't have. [I can read code pretty well but I think
my technical coding days are over]



-----
Admin - email* me if you need anything,  or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything, or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: Is multiline text in bounding box possible?

cacb
In reply to this post by tp3
On 26.03.2020 23:23, Torsten Paul wrote:
> Automatically wrapped means, the size of the box is given
> and the text will automatically break the text to fit that
> box from a single line string.
> Basically doing a layout with a flexible number of lines
> depending of the width of the defined box.
>
> You say that's what that code line is doing?

If you go back to my first reply, you will see what I was saying. I was
pointing out about what you can't do in OpenSCAD: "In general you can't
query the model for information such as dimensions."

Then I gave an example from another system where the model was queried
for information such as dimensions.

As it happens the few lines of code did indeed wrap the text.

The code takes a number of lines, computes the bounding box of the text
and fits a box around it.

You're welcome.

Carsten Arnholm

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

Re: Is multiline text in bounding box possible?

cacb
In reply to this post by JordanBrown
On 27.03.2020 04:30, Jordan Brown wrote:

> On 3/26/2020 2:50 PM, Carsten Arnholm wrote:
>> You can't do that in OpenSCAD. In general you can't query the model
>> for information such as dimensions.
>
> This is kind of a special case.  Although you can't ask anything about
> the results of the text() operator, you could have a function that did
> the same underlying *font* processing and gave you text metric
> information without ever getting to the geometry processing.
>
> Just like you can't get the bounding box of a polygon, but you can get
> the bounding box of the array of points that you feed to polygon().
>
> Not that you can do it today, but it's not the same as the general
> problem of querying geometry.

The OpenSCAD language does not allow you query the model in any way,
because it is declarative. Technically there is no problem computing
bounding boxes, but when the language does not allow processing to
return values you are stuck.

Carsten Arnholm


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

Re: Is multiline text in bounding box possible?

nophead
> The code takes a number of lines, computes the bounding box of the text and fits a box around it.

I think there is a difference in the meaning of "wrap". The OP wants the text wrapped inside a fixed size box. I.e. line breaks inserted where it meets the edges. Carsten is meaning wrap a box around some text. i.e. making it big enough to enclose the text.

On Fri, 27 Mar 2020 at 11:45, Carsten Arnholm <[hidden email]> wrote:
On 27.03.2020 04:30, Jordan Brown wrote:
> On 3/26/2020 2:50 PM, Carsten Arnholm wrote:
>> You can't do that in OpenSCAD. In general you can't query the model
>> for information such as dimensions.
>
> This is kind of a special case.  Although you can't ask anything about
> the results of the text() operator, you could have a function that did
> the same underlying *font* processing and gave you text metric
> information without ever getting to the geometry processing.
>
> Just like you can't get the bounding box of a polygon, but you can get
> the bounding box of the array of points that you feed to polygon().
>
> Not that you can do it today, but it's not the same as the general
> problem of querying geometry.

The OpenSCAD language does not allow you query the model in any way,
because it is declarative. Technically there is no problem computing
bounding boxes, but when the language does not allow processing to
return values you are stuck.

Carsten Arnholm


_______________________________________________
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: Is multiline text in bounding box possible?

JordanBrown
In reply to this post by cacb
On 3/27/2020 4:45 AM, Carsten Arnholm wrote:
The OpenSCAD language does not allow you query the model in any way, because it is declarative. Technically there is no problem computing bounding boxes, but when the language does not allow processing to return values you are stuck.

Measuring text doesn't require querying the model.  It's a function of the inputs to text() and the processing that text() does to generate the resulting shape, not of that resulting shape.

Consider this analogy:  You can trivially calculate the bounding box for cube(10), because you know the inputs and you know the shape of the object that results.  It would be easy to have a function bbcube(10) that returned the bounding box.  However, you cannot easily calculate the bounding box for rotate(10) cube(10);.  That would require querying the model.

Similarly, text() is an operation that takes particular inputs and produces corresponding geometric outputs.  OpenSCAD internally knows the metrics of the glyphs generated, and how the glyphs are concatenated to form the final shape that is the input to the CSG and CGAL stages.  There could be a function that does the same processing that is used to generate the shape sent to those geometry stages, but stops at the point where it has the bounding box and returns it.  (Looking at the sources, I don't think this would even be all that hard to do, for somebody who is familiar with working on OpenSCAD.)  As with the cube(10) example above, the fact that you could calculate the bounding box for text("hello") doesn't mean that you could calculate the bounding box for rotate(10) text("hello");.

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

Re: Is multiline text in bounding box possible?

JordanBrown
In reply to this post by nophead
On 3/27/2020 5:27 AM, nop head wrote:
I think there is a difference in the meaning of "wrap". The OP wants the text wrapped inside a fixed size box. I.e. line breaks inserted where it meets the edges. Carsten is meaning wrap a box around some text. i.e. making it big enough to enclose the text.

The key operation (that OpenSCAD doesn't have) is the same:  determining the bounding box of the text.

If you want to enclose the text, you calculate its bounding box and generate an enclosure that size.

If you want to break the text into multiple chunks that fit inside a fixed-size box, you break the text up into words and successively calculate the bounding boxes of the words, moving to a new line when the next word won't fix into the destination box.


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

Re: Is multiline text in bounding box possible?

Ronaldo
In reply to this post by nophead
I think there is a difference in the meaning of "wrap". The OP wants the text wrapped inside a fixed size box. I.e. line breaks inserted where it meets the edges. Carsten is meaning wrap a box around some text. i.e. making it big enough to enclose the text.

Thie computation of the bounding box for the former case can be done by user's space code. See attached file.

There is an alternative bbox_3D in tips&tricks (https://en.m.wikibooks.org/wiki/OpenSCAD_User_Manual/Tips_and_Tricks). If a margin is needed we can use offset() operator.

 
bbox_2D_example.PNG

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

bbox.scad (2K) Download Attachment
tp3
Reply | Threaded
Open this post in threaded view
|

Re: Is multiline text in bounding box possible?

tp3
In reply to this post by JordanBrown
On 27.03.20 17:33, Jordan Brown wrote:
> Measuring text doesn't require querying the model. It's a
> function of the inputs to text() and the processing that
> text() does to generate the resulting shape, not of that
> resulting shape.

Correct. It's not needed for that use case. It will be
trivial to extend text() such it returns the data via

  data = text("Hallo", ...);
  echo(data.glyph[0].advance.x);

The challenge is the definition and behavior of those
data structures with regards to also being able to store
geometry.
(discussion in https://github.com/openscad/openscad/pull/3087)
Maybe, if we can't get this moving we have to limit
the scope. But it would be great to welcome modules also
in the club of first class citizen.

ciao,
  Torsten.


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

Re: Is multiline text in bounding box possible?

Parkinbot
In reply to this post by JordanBrown
JordanBrown wrote
> Measuring text doesn't require querying the model.  It's a function of
> the inputs to text().

That is correct. A built-in textmetrics() function would have an
implementation like len(), str() or ord().

A hint: The only control we currently have to stuff text into a given box is
the resize() operator:

resize([12, 0, 0], auto=true)  text("hi there");
translate([0, -2])
resize([12, 0, 0], auto=true)  text("this is a longer text");

A specific workaround would be to
- compose your text in Inkscape (origin is bottom left of the paper),
- call the object to path function,
- save it as SVG
- import the SVG with import() to OpenSCAD
- resize it with resize()

A more serious generic workaround would be: Write a program in any fullblown
language like Java, Python, C++ ... that inquires the fontmetrics of all
fonts installed on a given system and outputs this information into a scad
file that implements textsize() - more or less like the thingverse post does
it.




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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
12