Closed Chen1Plus closed 6 months ago
Thanks for picking this up!
I've tested this locally and it seems to work - my only concern is that this approach (aligning as part of drawing, rather than as part of layout) gives you some surprising behaviour with multi-line text:
The Text
as a whole is centered, but each line is still left-aligned.
If possible, I think it would be better to do the centering as part of the layout (which lives in src/graphics/text/cache.rs
). The way I've seen this done in other libraries is:
measure_word
function will most of this for you, I think - you'd just need to also include spaces in the total.cursor.x
to -line_width / 2.0
; if the text is right-aligned, set cursor.x
to -line_width
.This should make multi-line text work as you'd expect, and it also has the nice side effect of making it so Text::get_bounds
gives you bounds that take into account alignment.
The only slightly annoying thing about this approach is that you can't re-use the same Text
for multiple alignments (at least not without repeatng the layout, like we do when you change the content/font).
Let me know what you think - maybe I'm overthinking this 😄
I agree with you. It would be a better solution. However, I've been a bit busy lately, so I might need some more time to open next PR for this.
I agree with you. It would be a better solution. However, I've been a bit busy lately, so I might need some more time to open next PR for this.
That's no problem, appreciate the work you've done up to this point!
resolve #343