New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(lines-wordwrap): support wordbreak and hyphens option on lines method #3
Conversation
Thanks @arnaud-zg for working on this :) |
src/utils.js
Outdated
@@ -417,7 +417,30 @@ export function computeLinesDefault({ctx, text, max, wordSpacing, letterSpacing} | |||
} | |||
|
|||
if ([...line].length !== 0) { | |||
lines.push(line); | |||
if (options.wordwrap) { | |||
line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wordwrap option should be considered above before the breakpoint switch...case. You also should only check the last part and not the entire line. (Think about a line with 1000 characters and only the last 10char word is to long)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I moved it just before the process loop over text parts and compute the lines, and add a breakpoint of type SHY
for each new line. About the width of a line, we can't really know how much space it'll be on screen, so I think it's more interesting to measure it with measureText
and compare it with max
It's been a while that my pull request is on pending status, actually I need this feature on my project. It would be great if it can be merged this week. |
@arnaud-zg: sorry for taking so long to come back. This implementation may compute wrong values as the browser would not break the words like this (won't add textMetrics(el).lines(text, {}, {'word-break': 'break-all'})
/*
[ 'Craspharetraph',
'aretragravida.Vi',
'vamusconsequ',
'atlacusvelposu',
'erecongue.Duis',
'aloremvitaeexa',
'uctorscelerisqu',
'enoneuturpis.Ut',
'imperdietmagn',
'asitametjustobi',
'bendumvehicul',
'a.' ]
*/ Sorry to not mention this before. |
According to
Case 2
|
Up |
let breakPointType = null; | ||
|
||
if (options.wordBreak === 'break-all') { | ||
breakPointType = 'BAI'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As i said, the break-all case is already handled here:
https://github.com/bezoerb/text-metrics/blob/master/src/index.js#L143
https://github.com/bezoerb/text-metrics/blob/master/src/utils.js#L426
'agnasitametjustob-', | ||
'bibendumvehicul-', | ||
'la.' | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hyphens 'auto' behaves different. If you want to add a hyphen at the end of each line you can do
const value = textMetrics(el).lines(text.replace(/(.{1})/g,"$1­"))
Sorry @arnaud-zg, i don't think it's a good idea to add this feature as this won't match the expected behavior. When doing auto hyphens this should be done properly with grammatical correct results. |
👍 |
Details on issue #2