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

Size in TextLayoutInfo does not correspond to actual Text2d dimensions with non-one scale factor #7787

Closed
rparrett opened this issue Feb 22, 2023 · 2 comments · Fixed by #7794
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior

Comments

@rparrett
Copy link
Contributor

rparrett commented Feb 22, 2023

Bevy version

main

Relevant system information

AdapterInfo { name: "Apple M1 Max", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", backend: Metal }
SystemInfo { os: "MacOS 13.1 ", kernel: "22.2.0", cpu: "Apple M1 Max", core_count: "10", memory: "64.0 GiB" }

(Scale factor 2.0)

What you did

Spawn a Text2dBundle with a SpriteBundle in the background, sized according to TextLayoutInfo.size.

use bevy::{prelude::*, text::TextLayoutInfo};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .add_system(resize)
        .run();
}

fn resize(
    mut sprite_query: Query<&mut Sprite>,
    info_query: Query<&TextLayoutInfo, Changed<TextLayoutInfo>>,
) {
    let mut sprite = sprite_query.single_mut();

    for info in &info_query {
        sprite.custom_size = Some(Vec2::splat(4.) + info.size);
    }
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());

    commands.spawn(Text2dBundle {
        text: Text::from_section(
            "text2dtext2dtext2d",
            TextStyle {
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                font_size: 60.0,
                color: Color::WHITE,
            },
        ),
        transform: Transform::from_xyz(0., 0., 1.),
        ..default()
    });

    commands.spawn(SpriteBundle {
        sprite: Sprite {
            color: Color::BLACK,
            ..default()
        },
        ..default()
    });
}

What went wrong

I expected a black box underneath my text, with 4px of padding.

Instead, the black box is 2x the size of the text with an additional 4px of padding.

text2dsize

Additional information

I had previously used Text2dSize for this purpose which worked fine, but it was removed in #6807.

I can/will work around this by querying for the scale factor and dividing, but it seems like a hack.

@rparrett rparrett added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Feb 22, 2023
@ickshonpe
Copy link
Contributor

ickshonpe commented Feb 23, 2023

This is my fault, I should have considered this when I made this change.

I think this points to a place the code can be simplified and there isn't any need for TextInfo to be a scaled value. It's just inverted again in the extraction function anyway.

I'll make a short PR that should fix it now.

@ickshonpe
Copy link
Contributor

#7794

@rparrett rparrett added A-UI Graphical user interfaces, styles, layouts, and widgets and removed S-Needs-Triage This issue needs to be labelled labels Feb 23, 2023
github-merge-queue bot pushed a commit that referenced this issue Sep 11, 2023
…t a scaled value. (#7794)

# Objective

`TextLayoutInfo::size` isn't the drawn size of the text, but a scaled
value. This is fragile, counter-intuitive and makes it awkward to
retrieve the correct value.

## Solution

Multiply `TextLayoutInfo::size` by the reciprocal of the window's scale
factor after generating the text layout in `update_text2d_layout` and
`bevy_ui::widget::text_system`.

---

fixes: #7787

## Changelog

* Multiply `TextLayoutInfo::size` by the reciprocal of the scale factor
after text computation to reflect the actual size of the text as drawn.
* Reorder the operations in `extract_text2d_sprite` to apply the
alignment offset before the scale factor scaling.

## Migration Guide

The `size` value of `TextLayoutInfo` is stored in logical pixels and has
been renamed to `logical_size`. There is no longer any need to divide by
the window's scale factor to get the logical size.
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this issue Jan 9, 2024
…t a scaled value. (bevyengine#7794)

# Objective

`TextLayoutInfo::size` isn't the drawn size of the text, but a scaled
value. This is fragile, counter-intuitive and makes it awkward to
retrieve the correct value.

## Solution

Multiply `TextLayoutInfo::size` by the reciprocal of the window's scale
factor after generating the text layout in `update_text2d_layout` and
`bevy_ui::widget::text_system`.

---

fixes: bevyengine#7787

## Changelog

* Multiply `TextLayoutInfo::size` by the reciprocal of the scale factor
after text computation to reflect the actual size of the text as drawn.
* Reorder the operations in `extract_text2d_sprite` to apply the
alignment offset before the scale factor scaling.

## Migration Guide

The `size` value of `TextLayoutInfo` is stored in logical pixels and has
been renamed to `logical_size`. There is no longer any need to divide by
the window's scale factor to get the logical size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants