-
Notifications
You must be signed in to change notification settings - Fork 106
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
Ability to obtain baseline Y offset #123
Comments
We have encountered this issue as well in |
2 tasks
15 tasks
Opened #141 that I believe should fix this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I try to utilize
cosmic-text
to draw text to a bitmap and to obtain a tight bounding box around the result. Right now, I am confused by the calculation of the vertical offset of the text. To elaborate further, let us assume that we draw a single glyph, i.e. the letter"g"
, using aBuffer
. For the sake of simplicity, we setline_height == font_size == 72.0
(i.e. we do not apply leading aka additional distance between the lines). When we callBuffer::draw()
, the code first creates aLayoutRunIter
whoseline_y
property is initialized as follows:cosmic-text/src/buffer.rs
Line 215 in bfb5eef
In our case,
y_offset()
evaluates to0.0
(no leading, see above) and therefore, our run starts at a vertical offset of0.0
. Now, we callnext()
on the iterator to draw the first run and observe thatline_y
is incremented by the line height which is equal to the font size (aka72.0
):cosmic-text/src/buffer.rs
Line 241 in bfb5eef
Finally, we obtain the glyph image from `swash' and draw it to the given offset:
cosmic-text/src/buffer.rs
Lines 718 to 720 in bfb5eef
run.line_y
still evaluates to72.0
in our example. However, if I understand correctly, theswash
image is positioned relatively to the baseline of the glyph. I did not find explicit confirmation for this assumption in theswash
documentation, but the swash_demo repo provides an extensive sample for this. When drawing its layout, it incorporates the baseline into the vertical offset as seen here:https://github.com/dfrg/swash_demo/blob/55aedbc8604201f1b4925e2b986db59b838dc062/src/main.rs#L493
In opposition,
cosmic-text
effectively places the baseline at the bottom of the line. When I try to draw my"g"
glyph to the top-left corner of a bitmap and set my bounding box height to72 pix
, I end up with the following result:How to fix this? I am not adept in typography, but for my understanding, the vertical offset must be corrected by the descent of the font as displayed here:
Indeed, to draw my glyphs to the bitmap, I tried to calculate the baseline of the font by myself to apply this correction. This approach works, but
cosmic-text
makes it pretty hard to implement it. Basically, I have to layout my line, obtain theFontID
from the glyphs, query theFont
from theFontSystem
and do something like this:In fact, this is about the same calculation that is done in the
swash
demo, see here:https://github.com/dfrg/swash_demo/blob/d08ced5f1e92b62cd637415c1f66c440d737b31b/src/layout/line_breaker.rs#L324-L326
By using
baseline
instead offont_size
(resp. the total line height) as vertical offset, I finally get the correct result. I can live with this approach, but it would be nice ifcosmic-text
made it easier for me to access the baseline offsets of the layout lines.Sorry for the long explanation. I hope the issue has been made clear - and thanks for your awesome work on this crate!
The text was updated successfully, but these errors were encountered: