Skip to content

Commit 45db9e8

Browse files
committed
Extract color overrides from attrs_list into new color_spans field
1 parent 829258a commit 45db9e8

File tree

16 files changed

+307
-172
lines changed

16 files changed

+307
-172
lines changed

examples/editor-libcosmic/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct Window {
8888
theme: Theme,
8989
path_opt: Option<PathBuf>,
9090
attrs: Attrs,
91+
color: Option<cosmic_text::Color>,
9192
font_size: FontSize,
9293
#[cfg(not(feature = "vi"))]
9394
editor: Mutex<SyntaxEditor<'static>>,
@@ -114,7 +115,7 @@ impl Window {
114115
let mut editor = self.editor.lock().unwrap();
115116
let mut font_system = FONT_SYSTEM.lock().unwrap();
116117
let mut editor = editor.borrow_with(&mut font_system);
117-
match editor.load_text(&path, &self.attrs) {
118+
match editor.load_text(&path, &self.attrs, self.color) {
118119
Ok(()) => {
119120
log::info!("opened '{}'", path.display());
120121
self.path_opt = Some(path);
@@ -160,6 +161,7 @@ impl Application for Window {
160161
font_size: FontSize::Body,
161162
path_opt: None,
162163
attrs,
164+
color: None,
163165
editor: Mutex::new(editor),
164166
};
165167
if let Some(arg) = env::args().nth(1) {
@@ -269,15 +271,12 @@ impl Application for Window {
269271

270272
let Color { r, g, b, a } = self.theme.palette().text;
271273
let as_u8 = |component: f32| (component * 255.0) as u8;
272-
self.attrs.color_opt = Some(cosmic_text::Color::rgba(
274+
self.color = Some(cosmic_text::Color::rgba(
273275
as_u8(r),
274276
as_u8(g),
275277
as_u8(b),
276278
as_u8(a),
277279
));
278-
279-
let mut editor = self.editor.lock().unwrap();
280-
update_attrs(&mut *editor, &self.attrs);
281280
}
282281
}
283282

@@ -360,7 +359,7 @@ impl Application for Window {
360359
]
361360
.align_items(Alignment::Center)
362361
.spacing(8),
363-
text_box(&self.editor)
362+
text_box(&self.editor, self.color)
364363
]
365364
.spacing(8)
366365
.padding(16)

examples/editor-libcosmic/src/text.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cosmic::{
1111
iced_winit::renderer::BorderRadius,
1212
theme::Theme,
1313
};
14-
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache};
14+
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, Spans, SwashCache};
1515
use std::{cmp, sync::Mutex, time::Instant};
1616

1717
use crate::FONT_SYSTEM;
@@ -50,7 +50,7 @@ impl Text {
5050
let instant = Instant::now();
5151

5252
//TODO: make it possible to set attrs
53-
let mut line = BufferLine::new(string, AttrsList::new(Attrs::new()));
53+
let mut line = BufferLine::new(string, AttrsList::new(Attrs::new()), Spans::default());
5454

5555
//TODO: do we have to immediately shape?
5656
line.shape(&mut FONT_SYSTEM.lock().unwrap());
@@ -180,8 +180,8 @@ where
180180
for glyph in layout_line.glyphs.iter() {
181181
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
182182

183-
let glyph_color = match glyph.color_opt {
184-
Some(some) => some,
183+
let glyph_color = match self.line.color_spans().get(glyph.start) {
184+
Some(some) => *some,
185185
None => text_color,
186186
};
187187

examples/editor-libcosmic/src/text_box.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ impl StyleSheet for Theme {
4747

4848
pub struct TextBox<'a, Editor> {
4949
editor: &'a Mutex<Editor>,
50+
color: Option<cosmic_text::Color>,
5051
padding: Padding,
5152
}
5253

5354
impl<'a, Editor> TextBox<'a, Editor> {
54-
pub fn new(editor: &'a Mutex<Editor>) -> Self {
55+
pub fn new(editor: &'a Mutex<Editor>, color: Option<cosmic_text::Color>) -> Self {
5556
Self {
5657
editor,
58+
color,
5759
padding: Padding::new(0),
5860
}
5961
}
@@ -64,8 +66,11 @@ impl<'a, Editor> TextBox<'a, Editor> {
6466
}
6567
}
6668

67-
pub fn text_box<Editor>(editor: &Mutex<Editor>) -> TextBox<Editor> {
68-
TextBox::new(editor)
69+
pub fn text_box<Editor>(
70+
editor: &Mutex<Editor>,
71+
color: Option<cosmic_text::Color>,
72+
) -> TextBox<Editor> {
73+
TextBox::new(editor, color)
6974
}
7075

7176
impl<'a, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
@@ -156,12 +161,14 @@ where
156161
);
157162
}
158163

159-
let text_color = cosmic_text::Color::rgba(
160-
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
161-
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
162-
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
163-
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
164-
);
164+
let text_color = self.color.unwrap_or_else(|| {
165+
cosmic_text::Color::rgba(
166+
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
167+
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
168+
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
169+
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
170+
)
171+
});
165172

166173
let mut editor = self.editor.lock().unwrap();
167174

examples/editor-orbclient/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn main() {
7878
.monospaced(true)
7979
.family(Family::Monospace)
8080
.build();
81-
match editor.load_text(&path, attrs) {
81+
match editor.load_text(&path, attrs, None) {
8282
Ok(()) => (),
8383
Err(err) => {
8484
log::error!("failed to load {:?}: {}", path, err);

examples/rich-text/src/main.rs

Lines changed: 81 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use cosmic_text::{
44
Action, Attrs, AttrsBuilder, AttrsList, Buffer, BufferLine, Color, Edit, Editor, Family,
5-
FontSystem, Metrics, Style, SwashCache, Weight,
5+
FontSystem, Metrics, Spans, Style, SwashCache, Weight,
66
};
77
use orbclient::{EventOption, Renderer, Window, WindowFlag};
88
use std::{
@@ -54,96 +54,129 @@ fn main() {
5454

5555
editor.buffer_mut().lines.clear();
5656

57-
let lines: &[&[(&str, AttrsBuilder)]] = &[
57+
let lines: &[&[(&str, AttrsBuilder, Option<Color>)]] = &[
5858
&[
59-
("B", attrs.clone().weight(Weight::BOLD)),
60-
("old ", attrs.clone()),
61-
("I", attrs.clone().style(Style::Italic)),
62-
("talic ", attrs.clone()),
63-
("f", attrs.clone()),
64-
("i ", attrs.clone()),
65-
("f", attrs.clone().weight(Weight::BOLD)),
66-
("i ", attrs.clone()),
67-
("f", attrs.clone().style(Style::Italic)),
68-
("i ", attrs.clone()),
59+
("B", attrs.clone().weight(Weight::BOLD), None),
60+
("old ", attrs.clone(), None),
61+
("I", attrs.clone().style(Style::Italic), None),
62+
("talic ", attrs.clone(), None),
63+
("f", attrs.clone(), None),
64+
("i ", attrs.clone(), None),
65+
("f", attrs.clone().weight(Weight::BOLD), None),
66+
("i ", attrs.clone(), None),
67+
("f", attrs.clone().style(Style::Italic), None),
68+
("i ", attrs.clone(), None),
6969
],
7070
&[
71-
("Sans-Serif Normal ", attrs.clone()),
72-
("Sans-Serif Bold ", attrs.clone().weight(Weight::BOLD)),
73-
("Sans-Serif Italic ", attrs.clone().style(Style::Italic)),
71+
("Sans-Serif Normal ", attrs.clone(), None),
72+
("Sans-Serif Bold ", attrs.clone().weight(Weight::BOLD), None),
73+
(
74+
"Sans-Serif Italic ",
75+
attrs.clone().style(Style::Italic),
76+
None,
77+
),
7478
(
7579
"Sans-Serif Bold Italic",
7680
attrs.clone().weight(Weight::BOLD).style(Style::Italic),
81+
None,
7782
),
7883
],
7984
&[
80-
("Serif Normal ", serif_attrs.clone()),
81-
("Serif Bold ", serif_attrs.clone().weight(Weight::BOLD)),
82-
("Serif Italic ", serif_attrs.clone().style(Style::Italic)),
85+
("Serif Normal ", serif_attrs.clone(), None),
86+
(
87+
"Serif Bold ",
88+
serif_attrs.clone().weight(Weight::BOLD),
89+
None,
90+
),
91+
(
92+
"Serif Italic ",
93+
serif_attrs.clone().style(Style::Italic),
94+
None,
95+
),
8396
(
8497
"Serif Bold Italic",
8598
serif_attrs.weight(Weight::BOLD).style(Style::Italic),
99+
None,
86100
),
87101
],
88102
&[
89-
("Mono Normal ", mono_attrs.clone()),
90-
("Mono Bold ", mono_attrs.clone().weight(Weight::BOLD)),
91-
("Mono Italic ", mono_attrs.clone().style(Style::Italic)),
103+
("Mono Normal ", mono_attrs.clone(), None),
104+
("Mono Bold ", mono_attrs.clone().weight(Weight::BOLD), None),
105+
(
106+
"Mono Italic ",
107+
mono_attrs.clone().style(Style::Italic),
108+
None,
109+
),
92110
(
93111
"Mono Bold Italic",
94112
mono_attrs.weight(Weight::BOLD).style(Style::Italic),
113+
None,
95114
),
96115
],
97116
&[
98-
("Comic Normal ", comic_attrs.clone()),
99-
("Comic Bold ", comic_attrs.clone().weight(Weight::BOLD)),
100-
("Comic Italic ", comic_attrs.clone().style(Style::Italic)),
117+
("Comic Normal ", comic_attrs.clone(), None),
118+
(
119+
"Comic Bold ",
120+
comic_attrs.clone().weight(Weight::BOLD),
121+
None,
122+
),
123+
(
124+
"Comic Italic ",
125+
comic_attrs.clone().style(Style::Italic),
126+
None,
127+
),
101128
(
102129
"Comic Bold Italic",
103130
comic_attrs.weight(Weight::BOLD).style(Style::Italic),
131+
None,
104132
),
105133
],
106134
&[
107-
("R", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
108-
("A", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
109-
("I", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
110-
("N", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
111-
("B", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
112-
("O", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
113-
("W ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
114-
("Red ", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
115-
("Orange ", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
116-
("Yellow ", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
117-
("Green ", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
118-
("Blue ", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
119-
("Indigo ", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
120-
("Violet ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
121-
("U", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
122-
("N", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
123-
("I", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
124-
("C", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
125-
("O", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
126-
("R", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
127-
("N", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
135+
("R", attrs.clone(), Some(Color::rgb(0xFF, 0x00, 0x00))),
136+
("A", attrs.clone(), Some(Color::rgb(0xFF, 0x7F, 0x00))),
137+
("I", attrs.clone(), Some(Color::rgb(0xFF, 0xFF, 0x00))),
138+
("N", attrs.clone(), Some(Color::rgb(0x00, 0xFF, 0x00))),
139+
("B", attrs.clone(), Some(Color::rgb(0x00, 0x00, 0xFF))),
140+
("O", attrs.clone(), Some(Color::rgb(0x4B, 0x00, 0x82))),
141+
("W ", attrs.clone(), Some(Color::rgb(0x94, 0x00, 0xD3))),
142+
("Red ", attrs.clone(), Some(Color::rgb(0xFF, 0x00, 0x00))),
143+
("Orange ", attrs.clone(), Some(Color::rgb(0xFF, 0x7F, 0x00))),
144+
("Yellow ", attrs.clone(), Some(Color::rgb(0xFF, 0xFF, 0x00))),
145+
("Green ", attrs.clone(), Some(Color::rgb(0x00, 0xFF, 0x00))),
146+
("Blue ", attrs.clone(), Some(Color::rgb(0x00, 0x00, 0xFF))),
147+
("Indigo ", attrs.clone(), Some(Color::rgb(0x4B, 0x00, 0x82))),
148+
("Violet ", attrs.clone(), Some(Color::rgb(0x94, 0x00, 0xD3))),
149+
("U", attrs.clone(), Some(Color::rgb(0x94, 0x00, 0xD3))),
150+
("N", attrs.clone(), Some(Color::rgb(0x4B, 0x00, 0x82))),
151+
("I", attrs.clone(), Some(Color::rgb(0x00, 0x00, 0xFF))),
152+
("C", attrs.clone(), Some(Color::rgb(0x00, 0xFF, 0x00))),
153+
("O", attrs.clone(), Some(Color::rgb(0xFF, 0xFF, 0x00))),
154+
("R", attrs.clone(), Some(Color::rgb(0xFF, 0x7F, 0x00))),
155+
("N", attrs.clone(), Some(Color::rgb(0xFF, 0x00, 0x00))),
128156
],
129157
&[(
130158
"生活,삶,जिंदगी 😀 FPS",
131-
attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00)),
159+
attrs.clone(),
160+
Some(Color::rgb(0xFF, 0x00, 0x00)),
132161
)],
133162
];
134163
for line in lines {
135164
let mut line_text = String::new();
136165
let mut attrs_list = AttrsList::new(attrs.clone().build());
137-
for (text, attrs) in line.iter() {
166+
let mut color_spans = Spans::<Color>::default();
167+
for (text, attrs, color) in line.iter() {
138168
let start = line_text.len();
139169
line_text.push_str(text);
140170
let end = line_text.len();
141171
attrs_list.add_span(start..end, attrs.clone().build());
172+
if let Some(color) = color {
173+
color_spans.add(start..end, *color);
174+
}
142175
}
143176
editor
144177
.buffer_mut()
145178
.lines
146-
.push(BufferLine::new(line_text, attrs_list));
179+
.push(BufferLine::new(line_text, attrs_list, color_spans));
147180
}
148181

149182
let mut swash_cache = SwashCache::new();

examples/terminal/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
let attrs = Attrs::new();
2929

3030
// Add some text!
31-
buffer.set_text(" Hi, Rust! 🦀", attrs);
31+
buffer.set_text(" Hi, Rust! 🦀", attrs, None);
3232

3333
// Perform shaping as desired
3434
buffer.shape_until_scroll();

0 commit comments

Comments
 (0)