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

Added strikethrough #1953

Merged
merged 7 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Maximilian Köstler
Bruno Dupuis
Christopher Noel Hesse
Marcin Zając
Laura Gallo
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can find its changes [documented below](#070---2021-01-01).

### Added

- Strikethrough rich text attribute ([#1953] by [@jenra-uwu])
- System fonts loaded so that SVG images render text ([#1850] by [@DrGabble])
- Add `scroll()` method in WidgetExt ([#1600] by [@totsteps])
- `write!` for `RichTextBuilder` ([#1596] by [@Maan2003])
Expand Down
7 changes: 5 additions & 2 deletions druid/examples/markdown_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// On Windows platform, don't show a console when opening the app.
#![windows_subsystem = "windows"]

use pulldown_cmark::{Event as ParseEvent, Parser, Tag};
use pulldown_cmark::{Event as ParseEvent, Options, Parser, Tag};

use druid::text::{AttributesAdder, RichText, RichTextBuilder};
use druid::widget::prelude::*;
Expand Down Expand Up @@ -141,7 +141,7 @@ fn rebuild_rendered_text(text: &str) -> RichText {
let mut builder = RichTextBuilder::new();
let mut tag_stack = Vec::new();

let parser = Parser::new(text);
let parser = Parser::new_ext(text, Options::ENABLE_STRIKETHROUGH);
for event in parser {
match event {
ParseEvent::Start(tag) => {
Expand Down Expand Up @@ -218,6 +218,9 @@ fn add_attribute_for_tag(tag: &Tag, mut attrs: AttributesAdder) {
Tag::Strong => {
attrs.weight(FontWeight::BOLD);
}
Tag::Strikethrough => {
attrs.strikethrough(true);
}
Tag::Link(_link_ty, target, _title) => {
attrs
.underline(true)
Expand Down
18 changes: 16 additions & 2 deletions druid/src/text/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct AttributeSpans {
fg_color: SpanSet<KeyOrValue<Color>>,
style: SpanSet<FontStyle>,
underline: SpanSet<bool>,
strikethrough: SpanSet<bool>,
font_descriptor: SpanSet<KeyOrValue<FontDescriptor>>,
}

Expand Down Expand Up @@ -100,6 +101,8 @@ pub enum Attribute {
Style(FontStyle),
/// Underline.
Underline(bool),
/// Strikethrough
Strikethrough(bool),
/// A [`FontDescriptor`](struct.FontDescriptor.html).
Descriptor(KeyOrValue<FontDescriptor>),
}
Expand Down Expand Up @@ -131,6 +134,7 @@ impl AttributeSpans {
Attribute::TextColor(attr) => self.fg_color.add(Span::new(range, attr)),
Attribute::Style(attr) => self.style.add(Span::new(range, attr)),
Attribute::Underline(attr) => self.underline.add(Span::new(range, attr)),
Attribute::Strikethrough(attr) => self.strikethrough.add(Span::new(range, attr)),
Attribute::Descriptor(attr) => self.font_descriptor.add(Span::new(range, attr)),
}
}
Expand Down Expand Up @@ -175,6 +179,11 @@ impl AttributeSpans {
.iter()
.map(|s| (s.range.clone(), PietAttr::Underline(s.attr))),
);
items.extend(
self.strikethrough
.iter()
.map(|s| (s.range.clone(), PietAttr::Strikethrough(s.attr)))
);

// sort by ascending start order; this is a stable sort
// so items that come from FontDescriptor will stay at the front
Expand Down Expand Up @@ -254,7 +263,7 @@ impl<T: Clone> SpanSet<T> {
///
/// `new_len` is the length of the inserted text.
//TODO: we could be smarter here about just extending the existing spans
//as requred for insertions in the interior of a span.
//as required for insertions in the interior of a span.
//TODO: this isn't currently used; it should be used if we use spans with
//some editable type.
// the branches are much more readable without sharing code
Expand Down Expand Up @@ -322,7 +331,7 @@ impl Attribute {
Attribute::FontSize(size.into())
}

/// Create a new forground color attribute.
/// Create a new foreground color attribute.
pub fn text_color(color: impl Into<KeyOrValue<Color>>) -> Self {
Attribute::TextColor(color.into())
}
Expand All @@ -347,6 +356,11 @@ impl Attribute {
Attribute::Underline(underline)
}

/// Create a new strikethrough attribute.
pub fn strikethrough(strikethrough: bool) -> Self {
Attribute::Strikethrough(strikethrough)
}

/// Create a new `FontDescriptor` attribute.
pub fn font_descriptor(font: impl Into<KeyOrValue<FontDescriptor>>) -> Self {
Attribute::Descriptor(font.into())
Expand Down
8 changes: 7 additions & 1 deletion druid/src/text/rich_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl AttributesAdder<'_> {
self
}

/// Add a forground color attribute.
/// Add a foreground color attribute.
pub fn text_color(&mut self, color: impl Into<KeyOrValue<Color>>) -> &mut Self {
self.add_attr(Attribute::text_color(color));
self
Expand Down Expand Up @@ -232,6 +232,12 @@ impl AttributesAdder<'_> {
self
}

/// Add a strikethrough attribute.
pub fn strikethrough(&mut self, strikethrough: bool) -> &mut Self {
self.add_attr(Attribute::Strikethrough(strikethrough));
self
}

/// Add a `FontDescriptor` attribute.
pub fn font_descriptor(&mut self, font: impl Into<KeyOrValue<FontDescriptor>>) -> &mut Self {
self.add_attr(Attribute::font_descriptor(font));
Expand Down