Skip to content

Commit e74f2c5

Browse files
authored
add From<&Sugar> for FragmentStyle (#502)
* add From<&Sugar> for FragmentStyle * `cargo fmt`
1 parent 5e7502e commit e74f2c5

File tree

3 files changed

+76
-62
lines changed

3 files changed

+76
-62
lines changed

sugarloaf/src/layout/span_style.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use crate::layout::builder_data::FontSettingKey;
1313
use crate::layout::builder_data::EMPTY_FONT_SETTINGS;
1414
use crate::sugarloaf::primitives::SugarCursor;
15+
use crate::Sugar;
16+
use crate::SugarDecoration;
17+
use crate::SugarStyle;
1518
pub use swash::text::Language;
1619
use swash::{Setting, Stretch, Style, Weight};
1720

@@ -111,6 +114,66 @@ impl FragmentStyle {
111114
}
112115
}
113116

117+
impl From<&Sugar> for FragmentStyle {
118+
fn from(sugar: &Sugar) -> Self {
119+
let mut style = FragmentStyle::default();
120+
121+
match sugar.style {
122+
SugarStyle::BoldItalic => {
123+
style.font_attrs.1 = Weight::BOLD;
124+
style.font_attrs.2 = Style::Italic;
125+
}
126+
SugarStyle::Bold => {
127+
style.font_attrs.1 = Weight::BOLD;
128+
}
129+
SugarStyle::Italic => {
130+
style.font_attrs.2 = Style::Italic;
131+
}
132+
_ => {}
133+
}
134+
135+
let mut has_underline_cursor = false;
136+
match sugar.cursor {
137+
SugarCursor::Underline(cursor_color) => {
138+
style.underline = true;
139+
style.underline_offset = Some(-1.);
140+
style.underline_color = Some(cursor_color);
141+
style.underline_size = Some(-1.);
142+
143+
has_underline_cursor = true;
144+
}
145+
SugarCursor::Block(cursor_color) => {
146+
style.cursor = SugarCursor::Block(cursor_color);
147+
}
148+
SugarCursor::Caret(cursor_color) => {
149+
style.cursor = SugarCursor::Caret(cursor_color);
150+
}
151+
_ => {}
152+
}
153+
154+
match &sugar.decoration {
155+
SugarDecoration::Underline => {
156+
if !has_underline_cursor {
157+
style.underline = true;
158+
style.underline_offset = Some(-2.);
159+
style.underline_size = Some(1.);
160+
}
161+
}
162+
SugarDecoration::Strikethrough => {
163+
style.underline = true;
164+
style.underline_offset = Some(style.font_size / 2.);
165+
style.underline_size = Some(2.);
166+
}
167+
_ => {}
168+
}
169+
170+
style.color = sugar.foreground_color;
171+
style.background_color = Some(sugar.background_color);
172+
173+
style
174+
}
175+
}
176+
114177
/// Style that can be applied to a range of text.
115178
#[derive(Debug, PartialEq, Clone)]
116179
pub enum SpanStyle {

sugarloaf/src/sugarloaf/compositors/advanced.rs

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
// https://github.com/dfrg/swash_demo/blob/master/LICENSE
88

99
use crate::font::FontLibrary;
10-
use crate::font::{Style, Weight};
10+
1111
use crate::layout::{
1212
Content, ContentBuilder, Direction, FragmentStyle, LayoutContext, RenderData,
1313
};
1414
use crate::sugarloaf::tree::SugarTree;
15-
use crate::{SugarCursor, SugarDecoration, SugarStyle};
1615

1716
pub struct Advanced {
1817
pub render_data: RenderData,
@@ -98,72 +97,19 @@ impl Advanced {
9897
}
9998

10099
let line = &tree.lines[line_number];
101-
102-
for i in 0..line.len() {
103-
let mut style = FragmentStyle {
100+
for sugar in line.inner() {
101+
let style = FragmentStyle {
104102
font_size: tree.layout.font_size,
105-
..Default::default()
103+
..FragmentStyle::from(sugar)
106104
};
107105

108-
match line[i].style {
109-
SugarStyle::BoldItalic => {
110-
style.font_attrs.1 = Weight::BOLD;
111-
style.font_attrs.2 = Style::Italic;
112-
}
113-
SugarStyle::Bold => {
114-
style.font_attrs.1 = Weight::BOLD;
115-
}
116-
SugarStyle::Italic => {
117-
style.font_attrs.2 = Style::Italic;
118-
}
119-
_ => {}
120-
}
121-
122-
let mut has_underline_cursor = false;
123-
match line[i].cursor {
124-
SugarCursor::Underline(cursor_color) => {
125-
style.underline = true;
126-
style.underline_offset = Some(-1.);
127-
style.underline_color = Some(cursor_color);
128-
style.underline_size = Some(-1.);
129-
130-
has_underline_cursor = true;
131-
}
132-
SugarCursor::Block(cursor_color) => {
133-
style.cursor = SugarCursor::Block(cursor_color);
134-
}
135-
SugarCursor::Caret(cursor_color) => {
136-
style.cursor = SugarCursor::Caret(cursor_color);
137-
}
138-
_ => {}
139-
}
140-
141-
match &line[i].decoration {
142-
SugarDecoration::Underline => {
143-
if !has_underline_cursor {
144-
style.underline = true;
145-
style.underline_offset = Some(-2.);
146-
style.underline_size = Some(1.);
147-
}
148-
}
149-
SugarDecoration::Strikethrough => {
150-
style.underline = true;
151-
style.underline_offset = Some(style.font_size / 2.);
152-
style.underline_size = Some(2.);
153-
}
154-
_ => {}
155-
}
156-
157-
style.color = line[i].foreground_color;
158-
style.background_color = Some(line[i].background_color);
159-
160-
if line[i].repeated > 0 {
161-
let text = std::iter::repeat(line[i].content)
162-
.take(line[i].repeated + 1)
106+
if sugar.repeated > 0 {
107+
let text = std::iter::repeat(sugar.content)
108+
.take(sugar.repeated + 1)
163109
.collect::<String>();
164110
self.content_builder.add_text(&text, style);
165111
} else {
166-
self.content_builder.add_char(line[i].content, style);
112+
self.content_builder.add_char(sugar.content, style);
167113
}
168114
}
169115

sugarloaf/src/sugarloaf/primitives.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ impl SugarLine {
340340
self.inner.len()
341341
}
342342

343+
#[inline]
344+
pub fn inner(&self) -> &Vec<Sugar> {
345+
&self.inner
346+
}
347+
343348
// #[inline]
344349
// fn compute_hash(&mut self) {
345350
// 00000000000000

0 commit comments

Comments
 (0)