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

renderer does not render paragraphs crated during draw calls because it drops them first #2670

Open
4 tasks done
janWilejan opened this issue Nov 17, 2024 · 0 comments
Open
4 tasks done
Labels
bug Something isn't working

Comments

@janWilejan
Copy link

Is your issue REALLY a bug?

  • My issue is indeed a bug!
  • I am not crazy! I will not fill out this form just to ask a question or request a feature. Pinky promise.

Is there an existing issue for this?

  • I have searched the existing issues.

Is this issue related to iced?

  • My hardware is compatible and my graphics drivers are up-to-date.

What happened?

creating a custom text widget to render a paragraph of text using renderer.fill_paragraph. the text does not appear.

it seems that the problem is that you're immediately dropping the paragraph of text before queueuing it to be rendered (at line 44 of tiny_skia/src/layer.rs). it seems this is a mistake? surely you would rather drop the text after it has been rendered?

a similar mistake was made on line 63 in Layer::draw_editor.

the relevant part of this SSCCE is the draw() method. the rest is boilerplate.

use iced::advanced::layout::Node;
use iced::advanced::renderer::{self};
use iced::advanced::text::Paragraph as _;
use iced::advanced::text::{self};
use iced::advanced::Widget;
use iced::widget::center;
use iced::{Color, Element, Font, Length, Size};

struct State {
    text: String,
}

impl Default for State {
    fn default() -> Self {
        State {
            text: "Hello, world!".into(),
        }
    }
}

#[derive(Debug)]
enum Message {}

pub fn main() -> iced::Result {
    iced::run("Custom text widget", update, view)
}
fn update(_state: &mut State, _message: Message) {}

fn view(state: &State) -> Element<Message> {
    center(CustomTextWidget::new(&state.text)).into()
}

#[derive(Default)]
pub struct CustomTextWidget<'a> {
    text: &'a str,
}

impl<'a> CustomTextWidget<'a> {
    pub fn new(text: &'a str) -> Self {
        Self { text }
    }
}

impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> for CustomTextWidget<'a>
where
    Renderer: iced::advanced::Renderer
        + text::Renderer<Paragraph = iced::advanced::graphics::text::Paragraph>,
{
    fn size(&self) -> iced::Size<iced::Length> {
        Size::new(Length::Fill, Length::Fill)
    }

    fn layout(
        &self,
        _tree: &mut iced::advanced::widget::Tree,
        _renderer: &Renderer,
        limits: &iced::advanced::layout::Limits,
    ) -> iced::advanced::layout::Node {
        Node::new(limits.max())
    }

    fn draw(
        &self,
        _tree: &iced::advanced::widget::Tree,
        renderer: &mut Renderer,
        _theme: &Theme,
        _style: &iced::advanced::renderer::Style,
        layout: iced::advanced::Layout<'_>,
        _cursor: iced::advanced::mouse::Cursor,
        _viewport: &iced::Rectangle,
    ) {
        let text = text::Text {
            content: &*self.text,
            bounds: layout.bounds().size(),
            size: iced::Pixels(20.0),
            line_height: text::LineHeight::Relative(1.0),
            font: Font::DEFAULT,
            horizontal_alignment: iced::alignment::Horizontal::Center,
            vertical_alignment: iced::alignment::Vertical::Center,
            shaping: text::Shaping::Advanced,
            wrapping: text::Wrapping::Word,
        };
        let paragraph = Renderer::Paragraph::with_text(text);
        // Uncomment me!
        //std::mem::forget(paragraph.clone());
        renderer.fill_paragraph(
            &paragraph,
            layout.bounds().center(),
            Color::BLACK,
            layout.bounds(),
        );
    }
}

impl<'a, Message, Theme, Renderer> From<CustomTextWidget<'a>>
    for Element<'a, Message, Theme, Renderer>
where
    Renderer: renderer::Renderer
        + iced::advanced::text::Renderer<Paragraph = iced::advanced::graphics::text::Paragraph>,
{
    fn from(value: CustomTextWidget<'a>) -> Self {
        Self::new(value)
    }
}

What is the expected behavior?

Text should appear.

Uncommenting the code that says "Uncomment me!" shows the text properly, by preventing the paragraph from being dropped, by causing a memory leak. This is not ideal.

Version

crates.io release

Operating System

Windows

Do you have any log output?

this error appears unrelated. (issue #2625)

Job 1, 'cargo run' terminated by signal SIGSEGV (Address boundary error)
@janWilejan janWilejan added the bug Something isn't working label Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant