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

Line width measurement different when aligned #169

Closed
Imberflur opened this issue Aug 15, 2023 · 0 comments · Fixed by #175
Closed

Line width measurement different when aligned #169

Imberflur opened this issue Aug 15, 2023 · 0 comments · Fixed by #175

Comments

@Imberflur
Copy link
Contributor

Imberflur commented Aug 15, 2023

Line width appears to be measured from the left side of the bounds rather than the start of the text (for LTR text). This is noticeable when text is aligned so that it doesn't start at the left side (e.g. Align::Center or Align::Right). (looking at the code, I believe the same applies to RTL text but in the opposite direction)

Is this intentional or a bug? For layout of UIs it would be useful to have this be the width of the actual text.

For example:

    use cosmic_text::{Align, Attrs, AttrsList, BufferLine, FontSystem, Shaping, Wrap};

    let mut font_system = FontSystem::new();
    let font_size = 14.0;
    let width = 8000.0;

    let mut line = BufferLine::new(
        "Hello, Rust!",
        AttrsList::new(Attrs::new()),
        Shaping::Advanced,
    );

    let layout = line.layout(&mut font_system, font_size, width, Wrap::None);
    let w = layout[0].w;

    line.set_align(Some(Align::Center));
    let layout = line.layout(&mut font_system, font_size, width, Wrap::None);
    let center_w = layout[0].w;

    line.set_align(Some(Align::Right));
    let layout = line.layout(&mut font_system, font_size, width, Wrap::None);
    let right_w = layout[0].w;

    dbg!(w);
    dbg!(center_w);
    dbg!(right_w);

prints:

w = 74.242
center_w = 4037.121
right_w = 8000.0

I have a tested a potential fix locally that could be made into a PR.

Imberflur added a commit to Imberflur/cosmic-text that referenced this issue Aug 26, 2023
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.

See pop-os#134

* Instead of computing the LayoutLine width from the positioned and
  aligned glyphs, we pass through width computed during line wrapping
  (unless justified alignment is used, in this case we use the old
  approach because the use case for measuring the width isn't really
  applicable to justified text since that will just expand to the
  provided width). For the produced width to later give the same
  wrapping results when passed in as the `line_width` it needs to use
  the same exact float arithmatic that was used to compute the width
  that is compared against `line_width` when making line wrapping
  choices. Passing this width through as the LayoutLine width is the
  most covenient option without making more major changse to the API.
  Nevertheless, I am imagining that if we get a dedicated measurement
  method (i.e. that doesn't do the final positioning and alignment of
  glyphs and which caches `Vec<VisualLine>`), then this width can just
  be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
  pop-os#169.
* Switch substraction from `fit_x` to checking whether potential
  addition to the current line width would exceed the `line_width`. This
  avoids the float error being dependent on the provided `line_width`
  value.
* When eliminating trailing space from the line width, we avoid
  backtracking with subtraction (which would not give the same exact
  value due to float error) and instead save the previous width and use
  that.
* If the previous word did not exceed the line_width, we now include a
  single blank word even if it would cross the width limit since its
  width won't be counted. This is necessary to get the same wrapping
  behavior when re-using the measured width (which doesn't count a
  single trailing blank word). Note, this whitespace logic may be
  reworked anyway if <pop-os#155>
  is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
  start of a section of spaces which included characters that weren't
  even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
  justification, and line breaks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant