From 3fd986a007c972a9f8d0073f884e1f54311a8131 Mon Sep 17 00:00:00 2001 From: Steven Malis Date: Sat, 5 Sep 2020 13:48:32 -0700 Subject: [PATCH 1/7] Add support for strikethrough formatting on text. --- piet-coregraphics/src/ct_helpers.rs | 20 ++++++++++++++++++++ piet-coregraphics/src/text.rs | 6 +++++- piet-direct2d/src/dwrite.rs | 7 +++++++ piet-direct2d/src/text.rs | 1 + piet/src/samples/picture_5.rs | 1 + piet/src/samples/picture_8.rs | 1 + piet/src/text.rs | 2 ++ piet/src/util.rs | 3 +++ 8 files changed, 40 insertions(+), 1 deletion(-) diff --git a/piet-coregraphics/src/ct_helpers.rs b/piet-coregraphics/src/ct_helpers.rs index d89f51739..b0793542f 100644 --- a/piet-coregraphics/src/ct_helpers.rs +++ b/piet-coregraphics/src/ct_helpers.rs @@ -171,6 +171,26 @@ impl AttributedString { } } + #[allow(non_upper_case_globals)] + pub(crate) fn set_strikethrough(&mut self, range: CFRange, strikethrough: bool) { + // UnderlineStyle constants are also used for strikethrough styles + const kCTUnderlineStyleNone: i32 = 0x00; + const kCTUnderlineStyleSingle: i32 = 0x01; + + let value = if strikethrough { + kCTUnderlineStyleSingle + } else { + kCTUnderlineStyleNone + }; + unsafe { + self.inner.set_attribute( + range, + string_attributes::kCTStrikethroughStyleAttributeName, + &CFNumber::from(value).as_CFType(), + ) + } + } + pub(crate) fn set_fg_color(&mut self, range: CFRange, color: &Color) { let (r, g, b, a) = color.as_rgba(); let color = CGColor::rgb(r, g, b, a); diff --git a/piet-coregraphics/src/text.rs b/piet-coregraphics/src/text.rs index 347129a8a..d4e370c54 100644 --- a/piet-coregraphics/src/text.rs +++ b/piet-coregraphics/src/text.rs @@ -135,7 +135,8 @@ impl CoreGraphicsTextLayoutBuilder { } // Some attributes are 'standalone' and can just be added to the attributed string // immediately. - if matches!(&attr, TextAttribute::ForegroundColor(_) | TextAttribute::Underline(_)) { + if matches!(&attr, TextAttribute::ForegroundColor(_) | TextAttribute::Underline(_) | TextAttribute::Strikethrough(_)) + { return self.add_immediately(attr, range); } @@ -164,6 +165,8 @@ impl CoreGraphicsTextLayoutBuilder { .set_fg_color(whole_range, &self.attrs.defaults.fg_color); self.attr_string .set_underline(whole_range, self.attrs.defaults.underline); + self.attr_string + .set_strikethrough(whole_range, self.attrs.defaults.strikethrough); } fn add_immediately(&mut self, attr: TextAttribute, range: Range) { @@ -175,6 +178,7 @@ impl CoreGraphicsTextLayoutBuilder { self.attr_string.set_fg_color(range, &color); } TextAttribute::Underline(flag) => self.attr_string.set_underline(range, flag), + TextAttribute::Strikethrough(flag) => self.attr_string.set_strikethrough(range, flag), _ => unreachable!(), } } diff --git a/piet-direct2d/src/dwrite.rs b/piet-direct2d/src/dwrite.rs index 97f992fca..69c1bd8f0 100644 --- a/piet-direct2d/src/dwrite.rs +++ b/piet-direct2d/src/dwrite.rs @@ -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()); diff --git a/piet-direct2d/src/text.rs b/piet-direct2d/src/text.rs index d12c87ee4..f14b6c9bb 100644 --- a/piet-direct2d/src/text.rs +++ b/piet-direct2d/src/text.rs @@ -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)), } } diff --git a/piet/src/samples/picture_5.rs b/piet/src/samples/picture_5.rs index 7666f6e6b..1283cecea 100644 --- a/piet/src/samples/picture_5.rs +++ b/piet/src/samples/picture_5.rs @@ -29,6 +29,7 @@ pub fn draw(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) diff --git a/piet/src/samples/picture_8.rs b/piet/src/samples/picture_8.rs index 462b3b85b..e419d6d8d 100644 --- a/piet/src/samples/picture_8.rs +++ b/piet/src/samples/picture_8.rs @@ -40,6 +40,7 @@ pub fn draw(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.)); diff --git a/piet/src/text.rs b/piet/src/text.rs index 9ded98675..b159368b3 100644 --- a/piet/src/text.rs +++ b/piet/src/text.rs @@ -123,6 +123,8 @@ pub enum TextAttribute { Style(FontStyle), /// Underline. Underline(bool), + /// Strikethrough + Strikethrough(bool), } /// A trait for laying out text. diff --git a/piet/src/util.rs b/piet/src/util.rs index d14d8d64c..fd5a8a95d 100644 --- a/piet/src/util.rs +++ b/piet/src/util.rs @@ -136,6 +136,7 @@ pub struct LayoutDefaults { pub fg_color: Color, pub style: FontStyle, pub underline: bool, + pub strikethrough: bool, } impl LayoutDefaults { @@ -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, } } } @@ -161,6 +163,7 @@ impl Default for LayoutDefaults { fg_color: DEFAULT_TEXT_COLOR, style: FontStyle::default(), underline: false, + strikethrough: false, } } } From 7ffee44b32656a0eb67e5500323a6f98555c17ea Mon Sep 17 00:00:00 2001 From: Steven Malis Date: Sat, 5 Sep 2020 14:00:30 -0700 Subject: [PATCH 2/7] Deduplicate underline style constants to placate clippy. --- piet-coregraphics/src/ct_helpers.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/piet-coregraphics/src/ct_helpers.rs b/piet-coregraphics/src/ct_helpers.rs index b0793542f..9673cd309 100644 --- a/piet-coregraphics/src/ct_helpers.rs +++ b/piet-coregraphics/src/ct_helpers.rs @@ -97,6 +97,11 @@ struct CTParagraphStyleSetting { value: *const c_void, } +#[allow(non_upper_case_globals)] +// UnderlineStyle constants are also used for strikethrough styles +const kCTUnderlineStyleNone: i32 = 0x00; +const kCTUnderlineStyleSingle: i32 = 0x01; + impl CTParagraphStyleSetting { fn alignment(alignment: TextAlignment, is_rtl: bool) -> Self { static LEFT: CTTextAlignment = CTTextAlignment::Left; @@ -154,9 +159,6 @@ impl AttributedString { #[allow(non_upper_case_globals)] pub(crate) fn set_underline(&mut self, range: CFRange, underline: bool) { - const kCTUnderlineStyleNone: i32 = 0x00; - const kCTUnderlineStyleSingle: i32 = 0x01; - let value = if underline { kCTUnderlineStyleSingle } else { @@ -171,12 +173,7 @@ impl AttributedString { } } - #[allow(non_upper_case_globals)] pub(crate) fn set_strikethrough(&mut self, range: CFRange, strikethrough: bool) { - // UnderlineStyle constants are also used for strikethrough styles - const kCTUnderlineStyleNone: i32 = 0x00; - const kCTUnderlineStyleSingle: i32 = 0x01; - let value = if strikethrough { kCTUnderlineStyleSingle } else { From 0752f943c3a514d0baf562949f7185e59832d0d9 Mon Sep 17 00:00:00 2001 From: Steven Malis Date: Sat, 5 Sep 2020 14:17:17 -0700 Subject: [PATCH 3/7] I don't actually understand how coretext works lol. --- piet-coregraphics/src/ct_helpers.rs | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/piet-coregraphics/src/ct_helpers.rs b/piet-coregraphics/src/ct_helpers.rs index 9673cd309..f048de621 100644 --- a/piet-coregraphics/src/ct_helpers.rs +++ b/piet-coregraphics/src/ct_helpers.rs @@ -97,11 +97,6 @@ struct CTParagraphStyleSetting { value: *const c_void, } -#[allow(non_upper_case_globals)] -// UnderlineStyle constants are also used for strikethrough styles -const kCTUnderlineStyleNone: i32 = 0x00; -const kCTUnderlineStyleSingle: i32 = 0x01; - impl CTParagraphStyleSetting { fn alignment(alignment: TextAlignment, is_rtl: bool) -> Self { static LEFT: CTTextAlignment = CTTextAlignment::Left; @@ -159,6 +154,9 @@ impl AttributedString { #[allow(non_upper_case_globals)] pub(crate) fn set_underline(&mut self, range: CFRange, underline: bool) { + const kCTUnderlineStyleNone: i32 = 0x00; + const kCTUnderlineStyleSingle: i32 = 0x01; + let value = if underline { kCTUnderlineStyleSingle } else { @@ -174,18 +172,7 @@ impl AttributedString { } pub(crate) fn set_strikethrough(&mut self, range: CFRange, strikethrough: bool) { - let value = if strikethrough { - kCTUnderlineStyleSingle - } else { - kCTUnderlineStyleNone - }; - unsafe { - self.inner.set_attribute( - range, - string_attributes::kCTStrikethroughStyleAttributeName, - &CFNumber::from(value).as_CFType(), - ) - } + todo!() } pub(crate) fn set_fg_color(&mut self, range: CFRange, color: &Color) { From 09b8ce280bfff040a7334f6b1d58f1cbceaceb11 Mon Sep 17 00:00:00 2001 From: Steven Malis Date: Sat, 5 Sep 2020 14:23:33 -0700 Subject: [PATCH 4/7] Remove attempts to add coregraphics support. --- piet-coregraphics/src/ct_helpers.rs | 4 ---- piet-coregraphics/src/text.rs | 6 +----- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/piet-coregraphics/src/ct_helpers.rs b/piet-coregraphics/src/ct_helpers.rs index f048de621..d89f51739 100644 --- a/piet-coregraphics/src/ct_helpers.rs +++ b/piet-coregraphics/src/ct_helpers.rs @@ -171,10 +171,6 @@ impl AttributedString { } } - pub(crate) fn set_strikethrough(&mut self, range: CFRange, strikethrough: bool) { - todo!() - } - pub(crate) fn set_fg_color(&mut self, range: CFRange, color: &Color) { let (r, g, b, a) = color.as_rgba(); let color = CGColor::rgb(r, g, b, a); diff --git a/piet-coregraphics/src/text.rs b/piet-coregraphics/src/text.rs index d4e370c54..347129a8a 100644 --- a/piet-coregraphics/src/text.rs +++ b/piet-coregraphics/src/text.rs @@ -135,8 +135,7 @@ impl CoreGraphicsTextLayoutBuilder { } // Some attributes are 'standalone' and can just be added to the attributed string // immediately. - if matches!(&attr, TextAttribute::ForegroundColor(_) | TextAttribute::Underline(_) | TextAttribute::Strikethrough(_)) - { + if matches!(&attr, TextAttribute::ForegroundColor(_) | TextAttribute::Underline(_)) { return self.add_immediately(attr, range); } @@ -165,8 +164,6 @@ impl CoreGraphicsTextLayoutBuilder { .set_fg_color(whole_range, &self.attrs.defaults.fg_color); self.attr_string .set_underline(whole_range, self.attrs.defaults.underline); - self.attr_string - .set_strikethrough(whole_range, self.attrs.defaults.strikethrough); } fn add_immediately(&mut self, attr: TextAttribute, range: Range) { @@ -178,7 +175,6 @@ impl CoreGraphicsTextLayoutBuilder { self.attr_string.set_fg_color(range, &color); } TextAttribute::Underline(flag) => self.attr_string.set_underline(range, flag), - TextAttribute::Strikethrough(flag) => self.attr_string.set_strikethrough(range, flag), _ => unreachable!(), } } From 10a137159a3c563dd099525f2ea7a61d594a343c Mon Sep 17 00:00:00 2001 From: Steven Malis Date: Sat, 5 Sep 2020 14:31:06 -0700 Subject: [PATCH 5/7] Add a coregraphics stub. --- piet-coregraphics/src/text.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/piet-coregraphics/src/text.rs b/piet-coregraphics/src/text.rs index 347129a8a..0903dd163 100644 --- a/piet-coregraphics/src/text.rs +++ b/piet-coregraphics/src/text.rs @@ -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!(), } } From c7a3039d856f448904e68104ad121a79614f9c94 Mon Sep 17 00:00:00 2001 From: Steven Malis Date: Sat, 5 Sep 2020 14:40:06 -0700 Subject: [PATCH 6/7] Update d2d snapshots. --- snapshots | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snapshots b/snapshots index b333b32c5..0bc53ff10 160000 --- a/snapshots +++ b/snapshots @@ -1 +1 @@ -Subproject commit b333b32c5abb3757777ddaa69763c36d4d901ef4 +Subproject commit 0bc53ff10ee466e2e5fb51cf77ec1e7e33a470f7 From 2bc79ccb6933ee7bb275c19718edeac10aa5bd7f Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Sun, 6 Sep 2020 11:38:16 -0400 Subject: [PATCH 7/7] Update piet/src/text.rs --- piet/src/text.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/piet/src/text.rs b/piet/src/text.rs index b159368b3..53a5a22cd 100644 --- a/piet/src/text.rs +++ b/piet/src/text.rs @@ -123,7 +123,7 @@ pub enum TextAttribute { Style(FontStyle), /// Underline. Underline(bool), - /// Strikethrough + /// Strikethrough. Strikethrough(bool), }