discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Is multiline text in bounding box possible?

JB
Jordan Brown
Fri, Mar 27, 2020 3:40 AM

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.

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.
M
MichaelAtOz
Fri, Mar 27, 2020 3:48 AM

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/

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/
CA
Carsten Arnholm
Fri, Mar 27, 2020 11:38 AM

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

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
CA
Carsten Arnholm
Fri, Mar 27, 2020 11:45 AM

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

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
NH
nop head
Fri, Mar 27, 2020 12:27 PM

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 arnholm@arnholm.org 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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

> 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 <arnholm@arnholm.org> 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 > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
JB
Jordan Brown
Fri, Mar 27, 2020 4:33 PM

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");.

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");.
JB
Jordan Brown
Fri, Mar 27, 2020 4:38 PM

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.

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.
RP
Ronaldo Persiano
Fri, Mar 27, 2020 5:08 PM

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.

[image: bbox_2D_example.PNG]

> > 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. [image: bbox_2D_example.PNG]
TP
Torsten Paul
Fri, Mar 27, 2020 5:21 PM

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.

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.
P
Parkinbot
Fri, Mar 27, 2020 5:49 PM

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/

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/