Re: Prevent sub-word wrapping with Paragraph?

15 views
Skip to first unread message

Julia Lavrova

unread,
Jun 8, 2021, 9:47:18 AM (4 days ago) Jun 8
to skia-d...@googlegroups.com
If I understand you correctly you want to setMaxLines(1) on your paragraph. 
That would stop wrapping. You can also set ellipsis to indicate that the text was cut.

On Tue, Jun 8, 2021 at 8:17 AM Joe <nick...@gmail.com> wrote:
I'm trying to use Paragraph to render wrapped text as is done in this CanvasKit fiddle. But I'd like to avoid wrapping within words and have a word clip instead of wrapping a subset of its characters. But I have not found any setting that will let me change Paragraph's behavior to accomplish this. Am I missing something? Would I need to use a different approach to accomplish this?

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/e8282d8d-606a-4867-9f35-61ae2848bf50n%40googlegroups.com.

Joe

unread,
Jun 8, 2021, 4:03:27 PM (4 days ago) Jun 8
to skia-discuss
Not quite. I'm trying to have words truncate if they are beyond the layout width instead of having their characters wrap (essentially, words should never be spread across multiple lines).


Desired behavior: 

Example.png

Current behavior:

Example2.png

Julia Lavrova

unread,
Jun 8, 2021, 4:15:43 PM (4 days ago) Jun 8
to skia-d...@googlegroups.com
1. Layout you text with width = infinity.
    It will get you min intrinsic width (the width of the longest word).
2. Layout your paragraph again with that min intrinsic width. Your paragraph will be as wide as the longest word.
3. Paint the paragraph.
4. Paint the red rectangle (or whatever you need) to override a part of a paragraph.

It's not exactly what you want but the closest I could think of.

Nicholas Eddy

unread,
Jun 9, 2021, 10:32:58 AM (3 days ago) Jun 9
to skia-d...@googlegroups.com
Thanks a lot for pointing this out! It got me much closer to what I wanted. However, I can still get the longest word to wrap as you can see below.

Example3.png

I'm doing the following:
paragraph.layout(POSITIVE_INFINITY)
paragraph.layout(max(paragraph.minIntrinsicWidth, width))

You received this message because you are subscribed to a topic in the Google Groups "skia-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/skia-discuss/BHr6tYW4KYg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/CAK%2BowuToFpWDzEKEnFib_zCpeKwgH1_C4JErz3iT0gKaGvC-Ew%40mail.gmail.com.

Julia Lavrova

unread,
Jun 9, 2021, 11:36:29 AM (3 days ago) Jun 9
to skia-d...@googlegroups.com
It does look wrong. 
Could you please send me the entire example that you are running?

Julia Lavrova

unread,
Jun 9, 2021, 12:12:31 PM (3 days ago) Jun 9
to skia-d...@googlegroups.com
Also, could you please try the following:
paragraph.layout(POSITIVE_INFINITY)
paragraph.layout(max(paragraph.minIntrinsicWidth + epsilon, width))
Where epsilon is a small number(I don't know how small, so you should try, something like 0.001, 0.01, 0.1, 1, no more).

Brian Osman

unread,
Jun 9, 2021, 12:19:51 PM (3 days ago) Jun 9
to skia-d...@googlegroups.com
I think the problem is that the proposed workaround doesn't do what's desired. It finds the maximum length of any *single* word, but once the text is laid out in a box, that word (or any word, really) might not start at the left edge of the box, if there's another preceding word. So it will still wrap.

Julia Lavrova

unread,
Jun 9, 2021, 12:24:21 PM (3 days ago) Jun 9
to skia-d...@googlegroups.com
Currently we are using the algorithm that does not start too long words from the new line.
However, if a word fits we do not break it even if we have to start a new line for that.



Joe

unread,
Jun 10, 2021, 11:00:41 AM (2 days ago) Jun 10
to skia-discuss
Here's a fiddle showing the behavior: https://fiddle.skia.org/c/33f6fa1d5dd00518fd431d2d64f91188.

Is there a work-around for what Brian described about words not starting at the left edge of the box?

Julia Lavrova

unread,
Jun 10, 2021, 11:06:11 AM (2 days ago) Jun 10
to skia-d...@googlegroups.com
I added +1 to the second layout width:
   paragraph->layout(paragraph->getMinIntrinsicWidth() + 1);
And now it does not break the word.
As I suspected, it's a rounding error.


Reply all
Reply to author
Forward
0 new messages