linebender / piet

An abstraction for 2D graphics.
Apache License 2.0
1.24k stars 93 forks source link

Add `Clone` impl to `TextAttribute` #483

Closed Fenex closed 2 years ago

Fenex commented 2 years ago

I have a 'static const array with some types of text styles:

const SEPARATOR_STYLE: &'static [TextAttribute; 1] = &[
    TextAttribute::TextColor(Color::BLUE)
];
const ERRORS_STYLE: &'static [TextAttribute; 2] = &[
    TextAttribute::TextColor(Color::YELLOW),
    TextAttribute::Strikethrough(true)
];

I would be happy to clone values from its, however TextAttribute doesn't implement a Clone trait. (PR #484)

I research all variants of TextAttribute and all its variats implements PartialEq and Debug, so TextAttribute could to derive this additional traits too. (PR #485)

more detail code example isssue ```Rust #[derive(Default)] struct StyledRanges { separator: Vec>, errors: Vec>, } impl StyledRanges { const SEPARATOR_STYLE: &'static [TextAttribute; 1] = &[ TextAttribute::TextColor(Color::BLUE) ]; const ERRORS_STYLE: &'static [TextAttribute; 2] = &[ TextAttribute::TextColor(Color::YELLOW), TextAttribute::Strikethrough(true) ]; } let (ranges, text): (StyledRanges, String) = logger.get_text_with_errors(); let mut attrs: Vec<(Range, &[TextAttribute])> = std::iter::empty() .chain(&mut ranges.separator.into_iter().zip(std::iter::repeat(&StyledRanges::SEPARATOR_STYLE[..]))) .chain(&mut ranges.errors.into_iter().zip(std::iter::repeat(&StyledRanges::ERRORS_STYLE[..]))) .collect(); // sort all types by `start` key due avoid assert // https://github.com/linebender/piet/blob/8f7bc70a097d585205573442b970ec087d4e8f3b/piet-cairo/src/text.rs#L265-L268 attrs.sort_by_key(|(r, _)| r.start); let mut text_layout: PietTextLayoutBuilder = ctx // &mut PaintCtx .text() // &mut PietText .new_text_layout(text) //PietTextLayoutBuilder .max_width(rect.width()); for (range /* Range */, attrs /* &[TextAttribute] */) in attrs { for attr /* &TextAttribute */ in attrs { text_layout = text_layout.range_attribute(range.clone(), attr.clone()); // <--- I cannot clone `attr` here! } } let text_layout = text_layout.build().unwrap(); ```
cmyr commented 2 years ago

Yes I agree this makes sense!

avitex commented 2 years ago

This can be closed as https://github.com/linebender/piet/pull/485 is merged :)