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

Primitive::Text.bounds has semantic meaning depending on alignment #567

Closed
twitchyliquid64 opened this issue Oct 16, 2020 · 1 comment
Closed
Labels
question Further information is requested text

Comments

@twitchyliquid64
Copy link

Primitive::Text is defined as:

    Text {
        /// The contents of the text
        content: String,
        /// The bounds of the text
        bounds: Rectangle,
        /// The color of the text
        color: Color,
        /// The size of the text
        size: f32,
        /// The font of the text
        font: Font,
        /// The horizontal alignment of the text
        horizontal_alignment: HorizontalAlignment,
        /// The vertical alignment of the text
        vertical_alignment: VerticalAlignment,
    },

Based on the doc comments, I would assume that bounds.x & bounds.y refer to the bounding box top-left, and width+x + height+y refer to the bottom right.

This is only true if alignment is set to Left & Top:

        let x = match horizontal_alignment {
            iced_native::HorizontalAlignment::Left => bounds.x,
            iced_native::HorizontalAlignment::Center => bounds.center_x(),
            iced_native::HorizontalAlignment::Right => bounds.x + bounds.width,
        };

        let y = match vertical_alignment {
            iced_native::VerticalAlignment::Top => bounds.y,
            iced_native::VerticalAlignment::Center => bounds.center_y(),
            iced_native::VerticalAlignment::Bottom => bounds.y + bounds.height,
        };

Hence, the bounds X/Y change, mapping to the input X/Y position that the wgpu backend expects for text of that alignment.

This has proven very challenging to work around for the raqote backend, which always needs the top-left X/Y position.

Could we either:

  1. Change the semantics so bounds is always the bounding box, or
  2. Provide the information from TextBackend::measure() in the primitive, so its easier to recompute the correct position (rather than either caching text runs in the backend or re-adding all the glyph sizes)
@hecrj hecrj added the question Further information is requested label Oct 16, 2020
@hecrj
Copy link
Member

hecrj commented Oct 16, 2020

I talked a bit about this in #377 (review).

For a raqote backend, I believe it may be interesting to not use Primitive at all and instead implement the widget Renderer traits directly. We can find and discuss any similarities later.

@hecrj hecrj closed this as completed Jan 19, 2022
@hecrj hecrj added the text label Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested text
Projects
None yet
Development

No branches or pull requests

2 participants