Skip to content
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

Vertically centered text appears too low #14

Open
paulkernfeld opened this issue Aug 10, 2019 · 5 comments
Open

Vertically centered text appears too low #14

paulkernfeld opened this issue Aug 10, 2019 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@paulkernfeld
Copy link
Contributor

I created an example here that illustrates. I would expect that the base of the text and the top of a capital letter are each the same distance from the vertical border of the window, but instead I observe that the base of the text is closer to the bottom of the window than the top of the capital is to the top of the window.

@hecrj
Copy link
Owner

hecrj commented Aug 10, 2019

glyph-brush's vertical align doesn't align a set of glyphs in particular. It think it only uses the font properties, so it accounts for accents and other kind of symbols on top of some glyphs.

image

This is desirable for most use cases when you have dynamic text because it avoids awkward jumps. I am guessing the small difference that is still left depends entirely on the font.

If you want to completely center the produced glyphs, you should be able to use GlyphBrush::pixel_bounds (from the GlyphCruncher implementation) and then set the screen_position appropriately when queueing.

@hecrj
Copy link
Owner

hecrj commented Aug 10, 2019

Something like this:

let mut section = Section {
    text: "gŽ",
    color: [0.0, 0.0, 0.0, 1.0],
    scale: Scale::uniform(scale as f32),
    bounds: (size.width as f32, size.height as f32),
    ..Section::default()
};

let pixel_bounds =
    glyph_brush.pixel_bounds(section).expect("Pixel bounds");

section.screen_position = (
    size.width as f32 * 0.5 - pixel_bounds.min.x as f32 * 0.5,
    size.height as f32 * 0.5 - pixel_bounds.min.y as f32 * 0.5,
);

section.layout = Layout::default_single_line()
    .h_align(HorizontalAlign::Center)
    .v_align(VerticalAlign::Center);

glyph_brush.queue(section);

Produces:

image

It doesn't seem to be entirely centered still (?).

@hecrj
Copy link
Owner

hecrj commented Aug 10, 2019

Also, I am sure we could implement our own GlyphPositioner to do this directly without using pixel_bounds.

@hecrj hecrj added enhancement New feature or request help wanted Extra attention is needed question Further information is requested labels Aug 10, 2019
@paulkernfeld
Copy link
Contributor Author

I was also imagining a centering strategy that did not depend dynamically on the particular glyphs.

Maybe it's wrong to approach this with the expectation that the baseline of the text and the top of a capital letter are perfectly equidistant from the edges; I'm not an expert in text layout. Trying out a bunch of different fonts in a text box in Google Docs, the vertical alignment seems to depend a bit on the selected typeface.

Overall I think it might make sense to close this issue, since it seems like wgpu_glyph is probably handling vertical centering in a good wya.

@hecrj
Copy link
Owner

hecrj commented Aug 21, 2019

The built-in glyph-brush glyph positioner uses rusttype::VMetrics, which seems to talk about highest/lowest glyph points, to align lines.

I am not sure if fonts provide enough information to perform the alignment you describe, but rusttype doesn't seem to expose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants