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

Add support for strikethrough formatting on text. #299

Merged
merged 7 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions piet-coregraphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ impl Attributes {
TextAttribute::Weight(w) => self.weight = Some(Span::new(w, range)),
TextAttribute::FontSize(s) => self.size = Some(Span::new(s, range)),
TextAttribute::Style(s) => self.style = Some(Span::new(s, range)),
TextAttribute::Strikethrough(_) => { /* Unimplemented for now as coregraphics doesn't have native strikethrough support. */
}
_ => unreachable!(),
}
}
Expand Down
7 changes: 7 additions & 0 deletions piet-direct2d/src/dwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@ impl TextLayout {
}
}

pub(crate) fn set_strikethrough(&mut self, range: Utf16Range, flag: bool) {
let flag = if flag { TRUE } else { FALSE };
unsafe {
self.0.SetStrikethrough(flag, range.into());
}
}

pub(crate) fn set_size(&mut self, range: Utf16Range, size: f32) {
unsafe {
self.0.SetFontSize(size, range.into());
Expand Down
1 change: 1 addition & 0 deletions piet-direct2d/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl D2DTextLayoutBuilder {
TextAttribute::Weight(weight) => layout.set_weight(utf16_range, weight),
TextAttribute::Style(style) => layout.set_style(utf16_range, style),
TextAttribute::Underline(flag) => layout.set_underline(utf16_range, flag),
TextAttribute::Strikethrough(flag) => layout.set_strikethrough(utf16_range, flag),
TextAttribute::ForegroundColor(color) => self.colors.push((utf16_range, color)),
}
}
Expand Down
1 change: 1 addition & 0 deletions piet/src/samples/picture_5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn draw<R: RenderContext>(rc: &mut R) -> Result<(), Error> {
.default_attribute(FontWeight::BOLD)
.range_attribute(..200, TextAttribute::ForegroundColor(BLUE))
.range_attribute(10..100, FontWeight::NORMAL)
.range_attribute(20..50, TextAttribute::Strikethrough(true))
.range_attribute(40..300, TextAttribute::Underline(false))
.range_attribute(60..160, FontStyle::Regular)
.range_attribute(140..220, FontWeight::NORMAL)
Expand Down
1 change: 1 addition & 0 deletions piet/src/samples/picture_8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn draw<R: RenderContext>(rc: &mut R) -> Result<(), Error> {
.range_attribute(220.., TextAttribute::FontSize(18.0))
.range_attribute(240.., FontStyle::Italic)
.range_attribute(280.., TextAttribute::Underline(true))
.range_attribute(320.., TextAttribute::Strikethrough(true))
.build()?;

rc.draw_text(&en_leading, (0., 0.));
Expand Down
2 changes: 2 additions & 0 deletions piet/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ pub enum TextAttribute {
Style(FontStyle),
/// Underline.
Underline(bool),
/// Strikethrough.
Strikethrough(bool),
}

/// A trait for laying out text.
Expand Down
3 changes: 3 additions & 0 deletions piet/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub struct LayoutDefaults {
pub fg_color: Color,
pub style: FontStyle,
pub underline: bool,
pub strikethrough: bool,
}

impl LayoutDefaults {
Expand All @@ -148,6 +149,7 @@ impl LayoutDefaults {
TextAttribute::Style(style) => self.style = style,
TextAttribute::Underline(flag) => self.underline = flag,
TextAttribute::ForegroundColor(color) => self.fg_color = color,
TextAttribute::Strikethrough(flag) => self.strikethrough = flag,
}
}
}
Expand All @@ -161,6 +163,7 @@ impl Default for LayoutDefaults {
fg_color: DEFAULT_TEXT_COLOR,
style: FontStyle::default(),
underline: false,
strikethrough: false,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion snapshots