feat(input): bold and italic decorations wrap in their own font
- font family wrap feed becomes a font override: family, weight, style - weight and style come from the decoration's HighlightStyle, no new API Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -7,10 +7,10 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
7
7
| File | Change | Why |
8
8
|---|---|---|
9
9
| `Cargo.toml` | empty `[workspace]` table at the end | `cargo test --manifest-path vendor/gpui-base/Cargo.toml` works inside the app workspace (and nested worktrees) |
10
| `src/input/editor/decorations.rs` | `TextDecoration.font_family`, `with_font_family()`, `font_family_spans()`, `adjust_range_for_edit` made `pub(crate)`, doc on layering | font family per byte range; first collection / item wins |
11
| `src/input/editor/display_map/text_wrapper.rs` | `font_families` on `TextWrapper`, `split_run_by_font_families()`, `set_font_families()` / `adjust_font_families()`, `measured_wrap_boundaries` measures by range, shared `changed_ranges()` / `rewrap_rows_of()` | soft wrap measures font-family ranges in their own font |
12
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs`, `mod.rs` | pass-through for `set_font_families`, export `split_run_by_font_families` | plumbing |
13
| `src/input/base/element.rs` | prepaint pushes font-family spans into the display map; render runs split by font family | glyphs, caret, selection and hit testing use the same shaped runs |
10
| `src/input/editor/decorations.rs` | `TextDecoration.font_family`, `with_font_family()`, `FontOverride` (family, weight, style), `font_override_spans()` reading `font_family` + `style.font_weight` / `style.font_style`, `adjust_range_for_edit` made `pub(crate)`, doc on layering | font per byte range; first collection / item wins per property |
11
| `src/input/editor/display_map/text_wrapper.rs` | `font_overrides` on `TextWrapper`, `split_run_by_font_overrides()`, `set_font_overrides()` / `adjust_font_overrides()`, `measured_wrap_boundaries` measures by range, shared `changed_ranges()` / `rewrap_rows_of()` | soft wrap measures mono / bold / italic ranges in their own font |
12
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs`, `mod.rs` | pass-through for `set_font_overrides`, export `split_run_by_font_overrides` | plumbing |
13
| `src/input/base/element.rs` | prepaint pushes font-override spans into the display map; render runs split by font override | glyphs, caret, selection and hit testing use the same shaped runs |
14
14
| `src/input/base/state.rs` | `set_hanging_indents(markers)` on multi-line states (textarea and editor) | list lines: continuation rows start under the text after `•`, `◦`, `12.` |
15
15
| `src/input/editor/display_map/text_wrapper.rs` | `hanging_indents` on `TextWrapper`, `hanging_indent_at()`, `measured_wrap_boundaries(.., hanging_indent, ..)`, `shift_span()` shared with inline metrics; a line with a marker always wraps as `WrappingIndent::Same` | wrap continuation rows at `wrap_width - marker width`; the existing `LineItem.indent` / `LineLayout.wrap_indent` path then shifts rendering, caret, selection, hit testing and up/down moves |
16
16
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs` | pass-through for `set_hanging_indents`, adjust on edit | plumbing |
@@ -20,10 +20,10 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in
20
20
21
21
## Limits
22
22
23
- Only the font family changes wrapping. Bold/italic `HighlightStyle` decorations still wrap with the regular-weight font.
23
- Syntax-highlight (tree-sitter / LSP) bold or italic still wraps with the base font; only decorations feed wrapping.
24
24
- Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.
25
25
- Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.
26
- The unwrapped longest-line width (soft wrap off) ignores font families.
26
- The unwrapped longest-line width (soft wrap off) ignores font overrides.
27
27
28
28
## Re-apply on a gpui-kit upgrade
29
29
@@ -7,10 +7,10 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in7
| File | Change | Why |7
| File | Change | Why |8
|---|---|---|8
|---|---|---|9
| `Cargo.toml` | empty `[workspace]` table at the end | `cargo test --manifest-path vendor/gpui-base/Cargo.toml` works inside the app workspace (and nested worktrees) |9
| `Cargo.toml` | empty `[workspace]` table at the end | `cargo test --manifest-path vendor/gpui-base/Cargo.toml` works inside the app workspace (and nested worktrees) |10
| `src/input/editor/decorations.rs` | `TextDecoration.font_family`, `with_font_family()`, `font_family_spans()`, `adjust_range_for_edit` made `pub(crate)`, doc on layering | font family per byte range; first collection / item wins |10
| `src/input/editor/decorations.rs` | `TextDecoration.font_family`, `with_font_family()`, `FontOverride` (family, weight, style), `font_override_spans()` reading `font_family` + `style.font_weight` / `style.font_style`, `adjust_range_for_edit` made `pub(crate)`, doc on layering | font per byte range; first collection / item wins per property |11
| `src/input/editor/display_map/text_wrapper.rs` | `font_families` on `TextWrapper`, `split_run_by_font_families()`, `set_font_families()` / `adjust_font_families()`, `measured_wrap_boundaries` measures by range, shared `changed_ranges()` / `rewrap_rows_of()` | soft wrap measures font-family ranges in their own font |11
| `src/input/editor/display_map/text_wrapper.rs` | `font_overrides` on `TextWrapper`, `split_run_by_font_overrides()`, `set_font_overrides()` / `adjust_font_overrides()`, `measured_wrap_boundaries` measures by range, shared `changed_ranges()` / `rewrap_rows_of()` | soft wrap measures mono / bold / italic ranges in their own font |12
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs`, `mod.rs` | pass-through for `set_font_families`, export `split_run_by_font_families` | plumbing |12
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs`, `mod.rs` | pass-through for `set_font_overrides`, export `split_run_by_font_overrides` | plumbing |13
| `src/input/base/element.rs` | prepaint pushes font-family spans into the display map; render runs split by font family | glyphs, caret, selection and hit testing use the same shaped runs |13
| `src/input/base/element.rs` | prepaint pushes font-override spans into the display map; render runs split by font override | glyphs, caret, selection and hit testing use the same shaped runs |14
| `src/input/base/state.rs` | `set_hanging_indents(markers)` on multi-line states (textarea and editor) | list lines: continuation rows start under the text after `•`, `◦`, `12.` |14
| `src/input/base/state.rs` | `set_hanging_indents(markers)` on multi-line states (textarea and editor) | list lines: continuation rows start under the text after `•`, `◦`, `12.` |15
| `src/input/editor/display_map/text_wrapper.rs` | `hanging_indents` on `TextWrapper`, `hanging_indent_at()`, `measured_wrap_boundaries(.., hanging_indent, ..)`, `shift_span()` shared with inline metrics; a line with a marker always wraps as `WrappingIndent::Same` | wrap continuation rows at `wrap_width - marker width`; the existing `LineItem.indent` / `LineLayout.wrap_indent` path then shifts rendering, caret, selection, hit testing and up/down moves |15
| `src/input/editor/display_map/text_wrapper.rs` | `hanging_indents` on `TextWrapper`, `hanging_indent_at()`, `measured_wrap_boundaries(.., hanging_indent, ..)`, `shift_span()` shared with inline metrics; a line with a marker always wraps as `WrappingIndent::Same` | wrap continuation rows at `wrap_width - marker width`; the existing `LineItem.indent` / `LineLayout.wrap_indent` path then shifts rendering, caret, selection, hit testing and up/down moves |16
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs` | pass-through for `set_hanging_indents`, adjust on edit | plumbing |16
| `src/input/editor/display_map/wrap_map.rs`, `display_map.rs` | pass-through for `set_hanging_indents`, adjust on edit | plumbing |@@ -20,10 +20,10 @@ Upstream: crates.io `gpui-base 0.7.1` (git `87d10ae5`, `crates/base`). Wired in20
20
21
## Limits21
## Limits22
22
23
- Only the font family changes wrapping. Bold/italic `HighlightStyle` decorations still wrap with the regular-weight font.23
- Syntax-highlight (tree-sitter / LSP) bold or italic still wraps with the base font; only decorations feed wrapping.24
- Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.24
- Lines containing inline tokens wrap through gpui's `LineWrapper` with the base font only and ignore their hanging indent.25
- Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.25
- Hanging-indent markers are dropped by an edit inside them; the app sets them again on change.26
- The unwrapped longest-line width (soft wrap off) ignores font families.26
- The unwrapped longest-line width (soft wrap off) ignores font overrides.27
27
28
## Re-apply on a gpui-kit upgrade28
## Re-apply on a gpui-kit upgrade29
29
@@ -20,8 +20,8 @@ use crate::{
20
20
input::{
21
21
RopeExt as _,
22
22
blink_cursor::CURSOR_WIDTH,
23
decorations::font_family_spans,
24
display_map::{LineLayout, split_run_by_font_families},
23
decorations::font_override_spans,
24
display_map::{LineLayout, split_run_by_font_overrides},
25
25
},
26
26
};
27
27
@@ -2530,12 +2530,12 @@ impl<M: InputModeKind> Element for TextElement<M> {
2530
2530
window.text_system().layout_width(font_id, text_size, ' ')
2531
2531
};
2532
2532
2533
let font_families: Rc<[(Range<usize>, SharedString)]> = {
2533
let font_overrides: Rc<[(Range<usize>, crate::input::decorations::FontOverride)]> = {
2534
2534
let state = self.state.read(cx);
2535
2535
if state.masked {
2536
2536
Rc::from([])
2537
2537
} else {
2538
font_family_spans(&state.extras.decoration_layers()).into()
2538
font_override_spans(&state.extras.decoration_layers()).into()
2539
2539
}
2540
2540
};
2541
2541
self.state.update(cx, |state, cx| {
@@ -2543,7 +2543,7 @@ impl<M: InputModeKind> Element for TextElement<M> {
2543
2543
state.display_map.ensure_text_prepared(&state.text, cx);
2544
2544
state
2545
2545
.display_map
2546
.set_font_families(font_families.clone(), cx);
2546
.set_font_overrides(font_overrides.clone(), cx);
2547
2547
});
2548
2548
2549
2549
let state = self.state.read(cx);
@@ -2703,7 +2703,7 @@ impl<M: InputModeKind> Element for TextElement<M> {
2703
2703
}
2704
2704
2705
2705
let mut start = range.start;
2706
for run in split_run_by_font_families(run, range.clone(), &font_families) {
2706
for run in split_run_by_font_overrides(run, range.clone(), &font_overrides) {
2707
2707
let run_range = start..start + run.len;
2708
2708
start = run_range.end;
2709
2709
runs.extend(split_run_for_ime_underline(
@@ -20,8 +20,8 @@ use crate::{20
input::{20
input::{21
RopeExt as _,21
RopeExt as _,22
blink_cursor::CURSOR_WIDTH,22
blink_cursor::CURSOR_WIDTH,23
decorations::font_family_spans,23
decorations::font_override_spans,24
display_map::{LineLayout, split_run_by_font_families},24
display_map::{LineLayout, split_run_by_font_overrides},25
},25
},26
};26
};27
27
@@ -2530,12 +2530,12 @@ impl<M: InputModeKind> Element for TextElement<M> {2530
window.text_system().layout_width(font_id, text_size, ' ')2530
window.text_system().layout_width(font_id, text_size, ' ')2531
};2531
};2532
2532
2533
let font_families: Rc<[(Range<usize>, SharedString)]> = {2533
let font_overrides: Rc<[(Range<usize>, crate::input::decorations::FontOverride)]> = {2534
let state = self.state.read(cx);2534
let state = self.state.read(cx);2535
if state.masked {2535
if state.masked {2536
Rc::from([])2536
Rc::from([])2537
} else {2537
} else {2538
font_family_spans(&state.extras.decoration_layers()).into()2538
font_override_spans(&state.extras.decoration_layers()).into()2539
}2539
}2540
};2540
};2541
self.state.update(cx, |state, cx| {2541
self.state.update(cx, |state, cx| {@@ -2543,7 +2543,7 @@ impl<M: InputModeKind> Element for TextElement<M> {2543
state.display_map.ensure_text_prepared(&state.text, cx);2543
state.display_map.ensure_text_prepared(&state.text, cx);2544
state2544
state2545
.display_map2545
.display_map2546
.set_font_families(font_families.clone(), cx);2546
.set_font_overrides(font_overrides.clone(), cx);2547
});2547
});2548
2548
2549
let state = self.state.read(cx);2549
let state = self.state.read(cx);@@ -2703,7 +2703,7 @@ impl<M: InputModeKind> Element for TextElement<M> {2703
}2703
}2704
2704
2705
let mut start = range.start;2705
let mut start = range.start;2706
for run in split_run_by_font_families(run, range.clone(), &font_families) {2706
for run in split_run_by_font_overrides(run, range.clone(), &font_overrides) {2707
let run_range = start..start + run.len;2707
let run_range = start..start + run.len;2708
start = run_range.end;2708
start = run_range.end;2709
runs.extend(split_run_for_ime_underline(2709
runs.extend(split_run_for_ime_underline(@@ -1,7 +1,9 @@
1
1
use crate::input::{DecoratedMode, EditorMode};
2
2
use std::{collections::BTreeMap, ops::Range};
3
3
4
use gpui::{App, Context, HighlightStyle, Hsla, SharedString, WeakEntity};
4
use gpui::{
5
App, Context, Font, FontStyle, FontWeight, HighlightStyle, Hsla, SharedString, WeakEntity,
6
};
5
7
use ropey::Rope;
6
8
use sum_tree::Bias;
7
9
@@ -72,7 +74,7 @@ impl RangeDecoration {
72
74
pub struct TextDecoration {
73
75
pub range: Range<usize>,
74
76
pub style: HighlightStyle,
75
/// Font family override; unlike `style`, it changes glyph widths and soft wrapping.
77
/// Font family override. It, `style.font_weight` and `style.font_style` change soft wrapping.
76
78
pub font_family: Option<SharedString>,
77
79
}
78
80
@@ -530,14 +532,36 @@ pub(crate) fn adjust_range_for_edit(
530
532
start..end
531
533
}
532
534
533
/// Non-overlapping font family spans sorted by start; earlier layers and items win.
534
pub(crate) fn font_family_spans(layers: &[&[TextDecoration]]) -> Vec<(Range<usize>, SharedString)> {
535
let mut spans: Vec<(Range<usize>, SharedString)> = Vec::new();
536
for decoration in layers.iter().flat_map(|layer| layer.iter()) {
537
let Some(font_family) = &decoration.font_family else {
538
continue;
539
};
540
let mut uncovered = vec![decoration.range.clone()];
535
/// The font properties a text decoration changes, which also change glyph widths.
536
#[derive(Clone, Debug, Default, PartialEq)]
537
pub(crate) struct FontOverride {
538
pub(crate) family: Option<SharedString>,
539
pub(crate) weight: Option<FontWeight>,
540
pub(crate) style: Option<FontStyle>,
541
}
542
543
impl FontOverride {
544
pub(crate) fn apply(&self, font: &mut Font) {
545
if let Some(family) = &self.family {
546
font.family = family.clone();
547
}
548
if let Some(weight) = self.weight {
549
font.weight = weight;
550
}
551
if let Some(style) = self.style {
552
font.style = style;
553
}
554
}
555
}
556
557
/// Non-overlapping spans sorted by start; the first item covering a byte wins.
558
fn first_wins_spans<V>(items: impl Iterator<Item = (Range<usize>, V)>) -> Vec<(Range<usize>, V)>
559
where
560
V: Clone,
561
{
562
let mut spans: Vec<(Range<usize>, V)> = Vec::new();
563
for (range, value) in items {
564
let mut uncovered = vec![range];
541
565
for (covered, _) in &spans {
542
566
uncovered = uncovered
543
567
.into_iter()
@@ -550,16 +574,64 @@ pub(crate) fn font_family_spans(layers: &[&[TextDecoration]]) -> Vec<(Range<usiz
550
574
.filter(|range| !range.is_empty())
551
575
.collect();
552
576
}
553
spans.extend(
554
uncovered
555
.into_iter()
556
.map(|range| (range, font_family.clone())),
557
);
577
spans.extend(uncovered.into_iter().map(|range| (range, value.clone())));
558
578
}
559
579
spans.sort_by_key(|(range, _)| range.start);
560
580
spans
561
581
}
562
582
583
/// Font overrides per byte span; earlier layers and items win per property.
584
pub(crate) fn font_override_spans(
585
layers: &[&[TextDecoration]],
586
) -> Vec<(Range<usize>, FontOverride)> {
587
let decorations = || layers.iter().flat_map(|layer| layer.iter());
588
let families = first_wins_spans(decorations().filter_map(|decoration| {
589
Some((decoration.range.clone(), decoration.font_family.clone()?))
590
}));
591
let weights =
592
first_wins_spans(decorations().filter_map(|decoration| {
593
Some((decoration.range.clone(), decoration.style.font_weight?))
594
}));
595
let styles =
596
first_wins_spans(decorations().filter_map(|decoration| {
597
Some((decoration.range.clone(), decoration.style.font_style?))
598
}));
599
let mut edges: Vec<usize> = families
600
.iter()
601
.map(|(range, _)| range)
602
.chain(weights.iter().map(|(range, _)| range))
603
.chain(styles.iter().map(|(range, _)| range))
604
.flat_map(|range| [range.start, range.end])
605
.collect();
606
edges.sort_unstable();
607
edges.dedup();
608
fn value_at<V: Clone>(spans: &[(Range<usize>, V)], offset: usize) -> Option<V> {
609
let ix = spans.partition_point(|(range, _)| range.end <= offset);
610
spans
611
.get(ix)
612
.filter(|(range, _)| range.start <= offset)
613
.map(|(_, value)| value.clone())
614
}
615
let mut result: Vec<(Range<usize>, FontOverride)> = Vec::new();
616
for segment in edges.windows(2) {
617
let font_override = FontOverride {
618
family: value_at(&families, segment[0]),
619
weight: value_at(&weights, segment[0]),
620
style: value_at(&styles, segment[0]),
621
};
622
if font_override == FontOverride::default() {
623
continue;
624
}
625
match result.last_mut() {
626
Some((range, last)) if range.end == segment[0] && *last == font_override => {
627
range.end = segment[1];
628
}
629
_ => result.push((segment[0]..segment[1], font_override)),
630
}
631
}
632
result
633
}
634
563
635
fn normalize<T: TrackedDecoration>(text: &Rope, decorations: Vec<T>) -> Vec<T> {
564
636
decorations
565
637
.into_iter()
@@ -634,7 +706,7 @@ impl<M: DecoratedMode> InputBaseState<M> {
634
706
///
635
707
/// Collections are layered in insertion order; the first collection wins
636
708
/// when overlapping decorations set the same [`HighlightStyle`] property
637
/// or font family. Only the font family affects layout and soft wrapping.
709
/// or font family. Font family, weight and style also affect soft wrapping.
638
710
/// Callers should avoid conflicting overlaps within one collection.
639
711
pub fn create_decorations_collection(
640
712
&mut self,
@@ -824,7 +896,7 @@ mod tests {
824
896
}
825
897
826
898
#[test]
827
fn font_family_spans_let_earlier_layers_and_items_win() {
899
fn font_override_spans_let_earlier_layers_and_items_win_per_property() {
828
900
let style = HighlightStyle::default();
829
901
let first = [
830
902
TextDecoration::new(4..8, style).with_font_family("Mono"),
@@ -833,13 +905,60 @@ mod tests {
833
905
];
834
906
let second = [TextDecoration::new(2..12, style).with_font_family("Other")];
835
907
assert_eq!(TextDecoration::new(0..1, style).font_family, None);
908
let family = |name: &str| FontOverride {
909
family: Some(name.into()),
910
..Default::default()
911
};
912
assert_eq!(
913
font_override_spans(&[&first[..], &second[..]]),
914
vec![
915
(2..4, family("Other")),
916
(4..8, family("Mono")),
917
(8..10, family("Serif")),
918
(10..12, family("Other")),
919
]
920
);
921
922
let bold = HighlightStyle {
923
font_weight: Some(FontWeight::BOLD),
924
..Default::default()
925
};
926
let italic = HighlightStyle {
927
font_style: Some(FontStyle::Italic),
928
font_weight: Some(FontWeight::LIGHT),
929
..Default::default()
930
};
931
let layer = [
932
TextDecoration::new(0..6, bold).with_font_family("Mono"),
933
TextDecoration::new(4..8, italic),
934
];
836
935
assert_eq!(
837
font_family_spans(&[&first[..], &second[..]]),
936
font_override_spans(&[&layer[..]]),
838
937
vec![
839
(2..4, "Other".into()),
840
(4..8, "Mono".into()),
841
(8..10, "Serif".into()),
842
(10..12, "Other".into()),
938
(
939
0..4,
940
FontOverride {
941
family: Some("Mono".into()),
942
weight: Some(FontWeight::BOLD),
943
style: None,
944
}
945
),
946
(
947
4..6,
948
FontOverride {
949
family: Some("Mono".into()),
950
weight: Some(FontWeight::BOLD),
951
style: Some(FontStyle::Italic),
952
}
953
),
954
(
955
6..8,
956
FontOverride {
957
family: None,
958
weight: Some(FontWeight::LIGHT),
959
style: Some(FontStyle::Italic),
960
}
961
),
843
962
]
844
963
);
845
964
}
@@ -1,7 +1,9 @@1
use crate::input::{DecoratedMode, EditorMode};1
use crate::input::{DecoratedMode, EditorMode};2
use std::{collections::BTreeMap, ops::Range};2
use std::{collections::BTreeMap, ops::Range};3
3
4
use gpui::{App, Context, HighlightStyle, Hsla, SharedString, WeakEntity};4
use gpui::{5
App, Context, Font, FontStyle, FontWeight, HighlightStyle, Hsla, SharedString, WeakEntity,6
};5
use ropey::Rope;7
use ropey::Rope;6
use sum_tree::Bias;8
use sum_tree::Bias;7
9
@@ -72,7 +74,7 @@ impl RangeDecoration {72
pub struct TextDecoration {74
pub struct TextDecoration {73
pub range: Range<usize>,75
pub range: Range<usize>,74
pub style: HighlightStyle,76
pub style: HighlightStyle,75
/// Font family override; unlike `style`, it changes glyph widths and soft wrapping.77
/// Font family override. It, `style.font_weight` and `style.font_style` change soft wrapping.76
pub font_family: Option<SharedString>,78
pub font_family: Option<SharedString>,77
}79
}78
80
@@ -530,14 +532,36 @@ pub(crate) fn adjust_range_for_edit(530
start..end532
start..end531
}533
}532
534
533
/// Non-overlapping font family spans sorted by start; earlier layers and items win.535
/// The font properties a text decoration changes, which also change glyph widths.534
pub(crate) fn font_family_spans(layers: &[&[TextDecoration]]) -> Vec<(Range<usize>, SharedString)> {536
#[derive(Clone, Debug, Default, PartialEq)]535
let mut spans: Vec<(Range<usize>, SharedString)> = Vec::new();537
pub(crate) struct FontOverride {536
for decoration in layers.iter().flat_map(|layer| layer.iter()) {538
pub(crate) family: Option<SharedString>,537
let Some(font_family) = &decoration.font_family else {539
pub(crate) weight: Option<FontWeight>,538
continue;540
pub(crate) style: Option<FontStyle>,539
};541
}540
let mut uncovered = vec![decoration.range.clone()];542
543
impl FontOverride {544
pub(crate) fn apply(&self, font: &mut Font) {545
if let Some(family) = &self.family {546
font.family = family.clone();547
}548
if let Some(weight) = self.weight {549
font.weight = weight;550
}551
if let Some(style) = self.style {552
font.style = style;553
}554
}555
}556
557
/// Non-overlapping spans sorted by start; the first item covering a byte wins.558
fn first_wins_spans<V>(items: impl Iterator<Item = (Range<usize>, V)>) -> Vec<(Range<usize>, V)>559
where560
V: Clone,561
{562
let mut spans: Vec<(Range<usize>, V)> = Vec::new();563
for (range, value) in items {564
let mut uncovered = vec![range];541
for (covered, _) in &spans {565
for (covered, _) in &spans {542
uncovered = uncovered566
uncovered = uncovered543
.into_iter()567
.into_iter()@@ -550,16 +574,64 @@ pub(crate) fn font_family_spans(layers: &[&[TextDecoration]]) -> Vec<(Range<usiz550
.filter(|range| !range.is_empty())574
.filter(|range| !range.is_empty())551
.collect();575
.collect();552
}576
}553
spans.extend(577
spans.extend(uncovered.into_iter().map(|range| (range, value.clone())));554
uncovered555
.into_iter()556
.map(|range| (range, font_family.clone())),557
);558
}578
}559
spans.sort_by_key(|(range, _)| range.start);579
spans.sort_by_key(|(range, _)| range.start);560
spans580
spans561
}581
}562
582
583
/// Font overrides per byte span; earlier layers and items win per property.584
pub(crate) fn font_override_spans(585
layers: &[&[TextDecoration]],586
) -> Vec<(Range<usize>, FontOverride)> {587
let decorations = || layers.iter().flat_map(|layer| layer.iter());588
let families = first_wins_spans(decorations().filter_map(|decoration| {589
Some((decoration.range.clone(), decoration.font_family.clone()?))590
}));591
let weights =592
first_wins_spans(decorations().filter_map(|decoration| {593
Some((decoration.range.clone(), decoration.style.font_weight?))594
}));595
let styles =596
first_wins_spans(decorations().filter_map(|decoration| {597
Some((decoration.range.clone(), decoration.style.font_style?))598
}));599
let mut edges: Vec<usize> = families600
.iter()601
.map(|(range, _)| range)602
.chain(weights.iter().map(|(range, _)| range))603
.chain(styles.iter().map(|(range, _)| range))604
.flat_map(|range| [range.start, range.end])605
.collect();606
edges.sort_unstable();607
edges.dedup();608
fn value_at<V: Clone>(spans: &[(Range<usize>, V)], offset: usize) -> Option<V> {609
let ix = spans.partition_point(|(range, _)| range.end <= offset);610
spans611
.get(ix)612
.filter(|(range, _)| range.start <= offset)613
.map(|(_, value)| value.clone())614
}615
let mut result: Vec<(Range<usize>, FontOverride)> = Vec::new();616
for segment in edges.windows(2) {617
let font_override = FontOverride {618
family: value_at(&families, segment[0]),619
weight: value_at(&weights, segment[0]),620
style: value_at(&styles, segment[0]),621
};622
if font_override == FontOverride::default() {623
continue;624
}625
match result.last_mut() {626
Some((range, last)) if range.end == segment[0] && *last == font_override => {627
range.end = segment[1];628
}629
_ => result.push((segment[0]..segment[1], font_override)),630
}631
}632
result633
}634
563
fn normalize<T: TrackedDecoration>(text: &Rope, decorations: Vec<T>) -> Vec<T> {635
fn normalize<T: TrackedDecoration>(text: &Rope, decorations: Vec<T>) -> Vec<T> {564
decorations636
decorations565
.into_iter()637
.into_iter()@@ -634,7 +706,7 @@ impl<M: DecoratedMode> InputBaseState<M> {634
///706
///635
/// Collections are layered in insertion order; the first collection wins707
/// Collections are layered in insertion order; the first collection wins636
/// when overlapping decorations set the same [`HighlightStyle`] property708
/// when overlapping decorations set the same [`HighlightStyle`] property637
/// or font family. Only the font family affects layout and soft wrapping.709
/// or font family. Font family, weight and style also affect soft wrapping.638
/// Callers should avoid conflicting overlaps within one collection.710
/// Callers should avoid conflicting overlaps within one collection.639
pub fn create_decorations_collection(711
pub fn create_decorations_collection(640
&mut self,712
&mut self,@@ -824,7 +896,7 @@ mod tests {824
}896
}825
897
826
#[test]898
#[test]827
fn font_family_spans_let_earlier_layers_and_items_win() {899
fn font_override_spans_let_earlier_layers_and_items_win_per_property() {828
let style = HighlightStyle::default();900
let style = HighlightStyle::default();829
let first = [901
let first = [830
TextDecoration::new(4..8, style).with_font_family("Mono"),902
TextDecoration::new(4..8, style).with_font_family("Mono"),@@ -833,13 +905,60 @@ mod tests {833
];905
];834
let second = [TextDecoration::new(2..12, style).with_font_family("Other")];906
let second = [TextDecoration::new(2..12, style).with_font_family("Other")];835
assert_eq!(TextDecoration::new(0..1, style).font_family, None);907
assert_eq!(TextDecoration::new(0..1, style).font_family, None);908
let family = |name: &str| FontOverride {909
family: Some(name.into()),910
..Default::default()911
};912
assert_eq!(913
font_override_spans(&[&first[..], &second[..]]),914
vec![915
(2..4, family("Other")),916
(4..8, family("Mono")),917
(8..10, family("Serif")),918
(10..12, family("Other")),919
]920
);921
922
let bold = HighlightStyle {923
font_weight: Some(FontWeight::BOLD),924
..Default::default()925
};926
let italic = HighlightStyle {927
font_style: Some(FontStyle::Italic),928
font_weight: Some(FontWeight::LIGHT),929
..Default::default()930
};931
let layer = [932
TextDecoration::new(0..6, bold).with_font_family("Mono"),933
TextDecoration::new(4..8, italic),934
];836
assert_eq!(935
assert_eq!(837
font_family_spans(&[&first[..], &second[..]]),936
font_override_spans(&[&layer[..]]),838
vec![937
vec![839
(2..4, "Other".into()),938
(840
(4..8, "Mono".into()),939
0..4,841
(8..10, "Serif".into()),940
FontOverride {842
(10..12, "Other".into()),941
family: Some("Mono".into()),942
weight: Some(FontWeight::BOLD),943
style: None,944
}945
),946
(947
4..6,948
FontOverride {949
family: Some("Mono".into()),950
weight: Some(FontWeight::BOLD),951
style: Some(FontStyle::Italic),952
}953
),954
(955
6..8,956
FontOverride {957
family: None,958
weight: Some(FontWeight::LIGHT),959
style: Some(FontStyle::Italic),960
}961
),843
]962
]844
);963
);845
}964
}@@ -235,13 +235,13 @@ impl DisplayMap {
235
235
self.rebuild_fold_projection();
236
236
}
237
237
238
/// Measure these document byte ranges in another font family when wrapping.
239
pub(crate) fn set_font_families(
238
/// Measure these document byte ranges in another font when wrapping.
239
pub(crate) fn set_font_overrides(
240
240
&mut self,
241
font_families: std::rc::Rc<[(Range<usize>, gpui::SharedString)]>,
241
font_overrides: std::rc::Rc<[(Range<usize>, crate::input::decorations::FontOverride)]>,
242
242
cx: &mut App,
243
243
) {
244
self.wrap_map.set_font_families(font_families, cx);
244
self.wrap_map.set_font_overrides(font_overrides, cx);
245
245
self.rebuild_fold_projection();
246
246
}
247
247
@@ -235,13 +235,13 @@ impl DisplayMap {235
self.rebuild_fold_projection();235
self.rebuild_fold_projection();236
}236
}237
237
238
/// Measure these document byte ranges in another font family when wrapping.238
/// Measure these document byte ranges in another font when wrapping.239
pub(crate) fn set_font_families(239
pub(crate) fn set_font_overrides(240
&mut self,240
&mut self,241
font_families: std::rc::Rc<[(Range<usize>, gpui::SharedString)]>,241
font_overrides: std::rc::Rc<[(Range<usize>, crate::input::decorations::FontOverride)]>,242
cx: &mut App,242
cx: &mut App,243
) {243
) {244
self.wrap_map.set_font_families(font_families, cx);244
self.wrap_map.set_font_overrides(font_overrides, cx);245
self.rebuild_fold_projection();245
self.rebuild_fold_projection();246
}246
}247
247
@@ -17,7 +17,7 @@ mod wrap_map;
17
17
18
18
// Re-export public API
19
19
pub use self::display_map::{DisplayMap, WrappingIndent};
20
pub(crate) use self::text_wrapper::{LineLayout, split_run_by_font_families};
20
pub(crate) use self::text_wrapper::{LineLayout, split_run_by_font_overrides};
21
21
22
22
// Re-export FoldRange and extract_fold_ranges
23
23
pub use folding::FoldRange;
@@ -17,7 +17,7 @@ mod wrap_map;17
17
18
// Re-export public API18
// Re-export public API19
pub use self::display_map::{DisplayMap, WrappingIndent};19
pub use self::display_map::{DisplayMap, WrappingIndent};20
pub(crate) use self::text_wrapper::{LineLayout, split_run_by_font_families};20
pub(crate) use self::text_wrapper::{LineLayout, split_run_by_font_overrides};21
21
22
// Re-export FoldRange and extract_fold_ranges22
// Re-export FoldRange and extract_fold_ranges23
pub use folding::FoldRange;23
pub use folding::FoldRange;@@ -5,8 +5,8 @@ use std::ops::Range;
5
5
use std::rc::Rc;
6
6
7
7
use gpui::{
8
App, Font, LineFragment, Pixels, Point, ShapedLine, SharedString, Size, TextAlign, TextRun,
9
Window, point, px, size,
8
App, Font, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, TextRun, Window, point,
9
px, size,
10
10
};
11
11
use ropey::Rope;
12
12
use smallvec::SmallVec;
@@ -15,6 +15,7 @@ use unicode_segmentation::UnicodeSegmentation as _;
15
15
16
16
use crate::input::{
17
17
Point as TreeSitterPoint, RopeExt,
18
decorations::FontOverride,
18
19
layout::{LastLayout, WhitespaceIndicators},
19
20
};
20
21
@@ -110,16 +111,16 @@ fn measured_wrap_boundaries(
110
111
result
111
112
}
112
113
113
/// Split `run`, covering the document bytes `range`, where `font_families` override its family.
114
pub(crate) fn split_run_by_font_families(
114
/// Split `run`, covering the document bytes `range`, where `font_overrides` change its font.
115
pub(crate) fn split_run_by_font_overrides(
115
116
run: TextRun,
116
117
range: Range<usize>,
117
font_families: &[(Range<usize>, SharedString)],
118
font_overrides: &[(Range<usize>, FontOverride)],
118
119
) -> SmallVec<[TextRun; 1]> {
119
let first = font_families.partition_point(|(span, _)| span.end <= range.start);
120
let first = font_overrides.partition_point(|(span, _)| span.end <= range.start);
120
121
let mut runs = SmallVec::new();
121
122
let mut offset = range.start;
122
for (span, font_family) in &font_families[first..] {
123
for (span, font_override) in &font_overrides[first..] {
123
124
if span.start >= range.end {
124
125
break;
125
126
}
@@ -132,7 +133,7 @@ pub(crate) fn split_run_by_font_families(
132
133
});
133
134
}
134
135
let mut font = run.font.clone();
135
font.family = font_family.clone();
136
font_override.apply(&mut font);
136
137
runs.push(TextRun {
137
138
len: end - start,
138
139
font,
@@ -330,8 +331,8 @@ pub(crate) struct TextWrapper {
330
331
pub(crate) lines: SumTree<LineItem>,
331
332
332
333
inline_metrics: Rc<[(Range<usize>, Pixels)]>,
333
/// Sorted, non-overlapping document byte ranges measured in another font family.
334
font_families: Rc<[(Range<usize>, SharedString)]>,
334
/// Sorted, non-overlapping document byte ranges measured in another font.
335
font_overrides: Rc<[(Range<usize>, FontOverride)]>,
335
336
/// Sorted list marker ranges, at most one per line; continuation rows start under their end.
336
337
hanging_indents: Rc<[Range<usize>]>,
337
338
_initialized: bool,
@@ -348,7 +349,7 @@ impl TextWrapper {
348
349
wrapping_indent: WrappingIndent::default(),
349
350
lines: SumTree::new(&()),
350
351
inline_metrics: Rc::from([]),
351
font_families: Rc::from([]),
352
font_overrides: Rc::from([]),
352
353
hanging_indents: Rc::from([]),
353
354
_initialized: false,
354
355
}
@@ -481,7 +482,7 @@ impl TextWrapper {
481
482
.text_system()
482
483
.line_wrapper(self.font.clone(), self.font_size);
483
484
let metrics = self.inline_metrics.clone();
484
let font_families = self.font_families.clone();
485
let font_overrides = self.font_overrides.clone();
485
486
let hanging_indents = self.hanging_indents.clone();
486
487
let text_system = gpui::WindowTextSystem::new(cx.text_system().clone());
487
488
let font = self.font.clone();
@@ -521,7 +522,7 @@ impl TextWrapper {
521
522
wrapping_indent,
522
523
hanging_indent_at(&hanging_indents, line_start, line_str.len()),
523
524
|range| {
524
let runs = split_run_by_font_families(
525
let runs = split_run_by_font_overrides(
525
526
TextRun {
526
527
len: range.len(),
527
528
font: font.clone(),
@@ -531,7 +532,7 @@ impl TextWrapper {
531
532
strikethrough: None,
532
533
},
533
534
line_start + range.start..line_start + range.end,
534
&font_families,
535
&font_overrides,
535
536
);
536
537
text_system
537
538
.layout_line(&line_str[range], font_size, &runs, None)
@@ -549,30 +550,30 @@ impl TextWrapper {
549
550
);
550
551
}
551
552
552
pub(crate) fn adjust_font_families(&mut self, range: &Range<usize>, new_len: usize) {
553
if self.font_families.is_empty() {
553
pub(crate) fn adjust_font_overrides(&mut self, range: &Range<usize>, new_len: usize) {
554
if self.font_overrides.is_empty() {
554
555
return;
555
556
}
556
self.font_families = self
557
.font_families
557
self.font_overrides = self
558
.font_overrides
558
559
.iter()
559
.filter_map(|(span, font_family)| {
560
.filter_map(|(span, font_override)| {
560
561
let span = crate::input::decorations::adjust_range_for_edit(span, range, new_len);
561
(!span.is_empty()).then(|| (span, font_family.clone()))
562
(!span.is_empty()).then(|| (span, font_override.clone()))
562
563
})
563
564
.collect();
564
565
}
565
566
566
pub(crate) fn set_font_families(
567
pub(crate) fn set_font_overrides(
567
568
&mut self,
568
font_families: Rc<[(Range<usize>, SharedString)]>,
569
font_overrides: Rc<[(Range<usize>, FontOverride)]>,
569
570
cx: &mut App,
570
571
) {
571
if self.font_families == font_families {
572
if self.font_overrides == font_overrides {
572
573
return;
573
574
}
574
let affected = changed_ranges(&self.font_families, &font_families);
575
self.font_families = font_families;
575
let affected = changed_ranges(&self.font_overrides, &font_overrides);
576
self.font_overrides = font_overrides;
576
577
self.rewrap_rows_of(&affected, cx);
577
578
}
578
579
@@ -1344,35 +1345,49 @@ mod tests {
1344
1345
1345
1346
#[cfg(target_os = "linux")]
1346
1347
#[test]
1347
fn production_wrap_measures_font_family_ranges_in_their_own_font() {
1348
fn production_wrap_measures_font_override_ranges_in_their_own_font() {
1348
1349
let cx = shaping_test_context();
1349
1350
cx.update(|cx| {
1350
1351
let font = gpui::font("DejaVu Sans");
1351
1352
let mono = gpui::font("DejaVu Sans Mono");
1353
let bold = gpui::Font {
1354
weight: FontWeight::BOLD,
1355
..font.clone()
1356
};
1352
1357
let font_size = px(14.);
1353
1358
let value = "iiiiiiii iiiiiiii iiiiiiii";
1354
1359
let width = shaped_width("iiiiiiii iiiiiiii ", &font, font_size, cx) + px(1.);
1355
assert!(
1356
shaped_width("iiiiiiii", &mono, font_size, cx)
1357
> shaped_width("iiiiiiii ", &font, font_size, cx) + px(1.)
1358
);
1360
for wider in [&mono, &bold] {
1361
assert!(
1362
shaped_width("iiiiiiii", wider, font_size, cx)
1363
> shaped_width("iiiiiiii", &font, font_size, cx) + px(1.)
1364
);
1365
}
1359
1366
let text = Rope::from(value);
1360
let wrap = |font_families: Vec<(Range<usize>, SharedString)>, cx: &mut App| {
1367
let wrap = |font_override: Option<FontOverride>, cx: &mut App| {
1361
1368
let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));
1362
1369
wrapper.update(&text, &(0..0), &text, cx);
1363
wrapper.set_font_families(font_families.into(), cx);
1370
let font_overrides: Vec<_> =
1371
font_override.into_iter().map(|o| (9..17, o)).collect();
1372
wrapper.set_font_overrides(font_overrides.into(), cx);
1364
1373
wrapper.line(0).unwrap().wrapped_lines.to_vec()
1365
1374
};
1366
assert_eq!(wrap(vec![], cx), [0..18, 18..26]);
1367
assert_eq!(
1368
wrap(vec![(9..17, mono.family.clone())], cx),
1369
[0..9, 9..18, 18..26]
1370
);
1375
assert_eq!(wrap(None, cx), [0..18, 18..26]);
1376
let mono_override = FontOverride {
1377
family: Some(mono.family.clone()),
1378
..Default::default()
1379
};
1380
assert_eq!(wrap(Some(mono_override), cx), [0..9, 9..18, 18..26]);
1381
let bold_override = FontOverride {
1382
weight: Some(FontWeight::BOLD),
1383
..Default::default()
1384
};
1385
assert_eq!(wrap(Some(bold_override), cx), [0..9, 9..18, 18..26]);
1371
1386
});
1372
1387
}
1373
1388
1374
1389
#[test]
1375
fn font_family_runs_split_at_span_edges_and_follow_edits() {
1390
fn font_override_runs_split_at_span_edges_and_follow_edits() {
1376
1391
let run = TextRun {
1377
1392
len: 10,
1378
1393
font: gpui::font("Sans"),
@@ -1381,29 +1396,37 @@ mod tests {
1381
1396
underline: None,
1382
1397
strikethrough: None,
1383
1398
};
1384
let font_families = [(2..4, SharedString::from("Mono")), (8..20, "Mono".into())];
1385
let runs = split_run_by_font_families(run.clone(), 0..10, &font_families);
1399
let mono = FontOverride {
1400
family: Some("Mono".into()),
1401
weight: Some(FontWeight::BOLD),
1402
style: None,
1403
};
1404
let font_overrides = [(2..4, mono.clone()), (8..20, mono.clone())];
1405
let runs = split_run_by_font_overrides(run.clone(), 0..10, &font_overrides);
1386
1406
let runs: Vec<_> = runs
1387
1407
.iter()
1388
.map(|run| (run.len, run.font.family.to_string()))
1408
.map(|run| (run.len, run.font.family.to_string(), run.font.weight))
1389
1409
.collect();
1410
let (regular, bold) = (FontWeight::NORMAL, FontWeight::BOLD);
1390
1411
assert_eq!(
1391
1412
runs,
1392
[(2, "Sans"), (2, "Mono"), (4, "Sans"), (2, "Mono")]
1393
.map(|(len, family)| (len, family.to_string()))
1413
[
1414
(2, "Sans", regular),
1415
(2, "Mono", bold),
1416
(4, "Sans", regular),
1417
(2, "Mono", bold)
1418
]
1419
.map(|(len, family, weight)| (len, family.to_string(), weight))
1394
1420
);
1395
let runs = split_run_by_font_families(TextRun { len: 3, ..run }, 4..7, &font_families);
1421
let runs = split_run_by_font_overrides(TextRun { len: 3, ..run }, 4..7, &font_overrides);
1396
1422
assert_eq!(runs.len(), 1);
1397
1423
assert_eq!(runs[0].font.family, "Sans");
1398
1424
1399
1425
let mut wrapper = TextWrapper::new(test_font(), px(14.), None);
1400
wrapper.font_families = Rc::from(font_families);
1401
wrapper.adjust_font_families(&(0..0), 1);
1402
wrapper.adjust_font_families(&(9..21), 0);
1403
assert_eq!(
1404
&*wrapper.font_families,
1405
[(3..5, SharedString::from("Mono"))]
1406
);
1426
wrapper.font_overrides = Rc::from(font_overrides);
1427
wrapper.adjust_font_overrides(&(0..0), 1);
1428
wrapper.adjust_font_overrides(&(9..21), 0);
1429
assert_eq!(&*wrapper.font_overrides, [(3..5, mono)]);
1407
1430
}
1408
1431
1409
1432
#[test]
@@ -5,8 +5,8 @@ use std::ops::Range;5
use std::rc::Rc;5
use std::rc::Rc;6
6
7
use gpui::{7
use gpui::{8
App, Font, LineFragment, Pixels, Point, ShapedLine, SharedString, Size, TextAlign, TextRun,8
App, Font, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, TextRun, Window, point,9
Window, point, px, size,9
px, size,10
};10
};11
use ropey::Rope;11
use ropey::Rope;12
use smallvec::SmallVec;12
use smallvec::SmallVec;@@ -15,6 +15,7 @@ use unicode_segmentation::UnicodeSegmentation as _;15
15
16
use crate::input::{16
use crate::input::{17
Point as TreeSitterPoint, RopeExt,17
Point as TreeSitterPoint, RopeExt,18
decorations::FontOverride,18
layout::{LastLayout, WhitespaceIndicators},19
layout::{LastLayout, WhitespaceIndicators},19
};20
};20
21
@@ -110,16 +111,16 @@ fn measured_wrap_boundaries(110
result111
result111
}112
}112
113
113
/// Split `run`, covering the document bytes `range`, where `font_families` override its family.114
/// Split `run`, covering the document bytes `range`, where `font_overrides` change its font.114
pub(crate) fn split_run_by_font_families(115
pub(crate) fn split_run_by_font_overrides(115
run: TextRun,116
run: TextRun,116
range: Range<usize>,117
range: Range<usize>,117
font_families: &[(Range<usize>, SharedString)],118
font_overrides: &[(Range<usize>, FontOverride)],118
) -> SmallVec<[TextRun; 1]> {119
) -> SmallVec<[TextRun; 1]> {119
let first = font_families.partition_point(|(span, _)| span.end <= range.start);120
let first = font_overrides.partition_point(|(span, _)| span.end <= range.start);120
let mut runs = SmallVec::new();121
let mut runs = SmallVec::new();121
let mut offset = range.start;122
let mut offset = range.start;122
for (span, font_family) in &font_families[first..] {123
for (span, font_override) in &font_overrides[first..] {123
if span.start >= range.end {124
if span.start >= range.end {124
break;125
break;125
}126
}@@ -132,7 +133,7 @@ pub(crate) fn split_run_by_font_families(132
});133
});133
}134
}134
let mut font = run.font.clone();135
let mut font = run.font.clone();135
font.family = font_family.clone();136
font_override.apply(&mut font);136
runs.push(TextRun {137
runs.push(TextRun {137
len: end - start,138
len: end - start,138
font,139
font,@@ -330,8 +331,8 @@ pub(crate) struct TextWrapper {330
pub(crate) lines: SumTree<LineItem>,331
pub(crate) lines: SumTree<LineItem>,331
332
332
inline_metrics: Rc<[(Range<usize>, Pixels)]>,333
inline_metrics: Rc<[(Range<usize>, Pixels)]>,333
/// Sorted, non-overlapping document byte ranges measured in another font family.334
/// Sorted, non-overlapping document byte ranges measured in another font.334
font_families: Rc<[(Range<usize>, SharedString)]>,335
font_overrides: Rc<[(Range<usize>, FontOverride)]>,335
/// Sorted list marker ranges, at most one per line; continuation rows start under their end.336
/// Sorted list marker ranges, at most one per line; continuation rows start under their end.336
hanging_indents: Rc<[Range<usize>]>,337
hanging_indents: Rc<[Range<usize>]>,337
_initialized: bool,338
_initialized: bool,@@ -348,7 +349,7 @@ impl TextWrapper {348
wrapping_indent: WrappingIndent::default(),349
wrapping_indent: WrappingIndent::default(),349
lines: SumTree::new(&()),350
lines: SumTree::new(&()),350
inline_metrics: Rc::from([]),351
inline_metrics: Rc::from([]),351
font_families: Rc::from([]),352
font_overrides: Rc::from([]),352
hanging_indents: Rc::from([]),353
hanging_indents: Rc::from([]),353
_initialized: false,354
_initialized: false,354
}355
}@@ -481,7 +482,7 @@ impl TextWrapper {481
.text_system()482
.text_system()482
.line_wrapper(self.font.clone(), self.font_size);483
.line_wrapper(self.font.clone(), self.font_size);483
let metrics = self.inline_metrics.clone();484
let metrics = self.inline_metrics.clone();484
let font_families = self.font_families.clone();485
let font_overrides = self.font_overrides.clone();485
let hanging_indents = self.hanging_indents.clone();486
let hanging_indents = self.hanging_indents.clone();486
let text_system = gpui::WindowTextSystem::new(cx.text_system().clone());487
let text_system = gpui::WindowTextSystem::new(cx.text_system().clone());487
let font = self.font.clone();488
let font = self.font.clone();@@ -521,7 +522,7 @@ impl TextWrapper {521
wrapping_indent,522
wrapping_indent,522
hanging_indent_at(&hanging_indents, line_start, line_str.len()),523
hanging_indent_at(&hanging_indents, line_start, line_str.len()),523
|range| {524
|range| {524
let runs = split_run_by_font_families(525
let runs = split_run_by_font_overrides(525
TextRun {526
TextRun {526
len: range.len(),527
len: range.len(),527
font: font.clone(),528
font: font.clone(),@@ -531,7 +532,7 @@ impl TextWrapper {531
strikethrough: None,532
strikethrough: None,532
},533
},533
line_start + range.start..line_start + range.end,534
line_start + range.start..line_start + range.end,534
&font_families,535
&font_overrides,535
);536
);536
text_system537
text_system537
.layout_line(&line_str[range], font_size, &runs, None)538
.layout_line(&line_str[range], font_size, &runs, None)@@ -549,30 +550,30 @@ impl TextWrapper {549
);550
);550
}551
}551
552
552
pub(crate) fn adjust_font_families(&mut self, range: &Range<usize>, new_len: usize) {553
pub(crate) fn adjust_font_overrides(&mut self, range: &Range<usize>, new_len: usize) {553
if self.font_families.is_empty() {554
if self.font_overrides.is_empty() {554
return;555
return;555
}556
}556
self.font_families = self557
self.font_overrides = self557
.font_families558
.font_overrides558
.iter()559
.iter()559
.filter_map(|(span, font_family)| {560
.filter_map(|(span, font_override)| {560
let span = crate::input::decorations::adjust_range_for_edit(span, range, new_len);561
let span = crate::input::decorations::adjust_range_for_edit(span, range, new_len);561
(!span.is_empty()).then(|| (span, font_family.clone()))562
(!span.is_empty()).then(|| (span, font_override.clone()))562
})563
})563
.collect();564
.collect();564
}565
}565
566
566
pub(crate) fn set_font_families(567
pub(crate) fn set_font_overrides(567
&mut self,568
&mut self,568
font_families: Rc<[(Range<usize>, SharedString)]>,569
font_overrides: Rc<[(Range<usize>, FontOverride)]>,569
cx: &mut App,570
cx: &mut App,570
) {571
) {571
if self.font_families == font_families {572
if self.font_overrides == font_overrides {572
return;573
return;573
}574
}574
let affected = changed_ranges(&self.font_families, &font_families);575
let affected = changed_ranges(&self.font_overrides, &font_overrides);575
self.font_families = font_families;576
self.font_overrides = font_overrides;576
self.rewrap_rows_of(&affected, cx);577
self.rewrap_rows_of(&affected, cx);577
}578
}578
579
@@ -1344,35 +1345,49 @@ mod tests {1344
1345
1345
#[cfg(target_os = "linux")]1346
#[cfg(target_os = "linux")]1346
#[test]1347
#[test]1347
fn production_wrap_measures_font_family_ranges_in_their_own_font() {1348
fn production_wrap_measures_font_override_ranges_in_their_own_font() {1348
let cx = shaping_test_context();1349
let cx = shaping_test_context();1349
cx.update(|cx| {1350
cx.update(|cx| {1350
let font = gpui::font("DejaVu Sans");1351
let font = gpui::font("DejaVu Sans");1351
let mono = gpui::font("DejaVu Sans Mono");1352
let mono = gpui::font("DejaVu Sans Mono");1353
let bold = gpui::Font {1354
weight: FontWeight::BOLD,1355
..font.clone()1356
};1352
let font_size = px(14.);1357
let font_size = px(14.);1353
let value = "iiiiiiii iiiiiiii iiiiiiii";1358
let value = "iiiiiiii iiiiiiii iiiiiiii";1354
let width = shaped_width("iiiiiiii iiiiiiii ", &font, font_size, cx) + px(1.);1359
let width = shaped_width("iiiiiiii iiiiiiii ", &font, font_size, cx) + px(1.);1355
assert!(1360
for wider in [&mono, &bold] {1356
shaped_width("iiiiiiii", &mono, font_size, cx)1361
assert!(1357
> shaped_width("iiiiiiii ", &font, font_size, cx) + px(1.)1362
shaped_width("iiiiiiii", wider, font_size, cx)1358
);1363
> shaped_width("iiiiiiii", &font, font_size, cx) + px(1.)1364
);1365
}1359
let text = Rope::from(value);1366
let text = Rope::from(value);1360
let wrap = |font_families: Vec<(Range<usize>, SharedString)>, cx: &mut App| {1367
let wrap = |font_override: Option<FontOverride>, cx: &mut App| {1361
let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));1368
let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));1362
wrapper.update(&text, &(0..0), &text, cx);1369
wrapper.update(&text, &(0..0), &text, cx);1363
wrapper.set_font_families(font_families.into(), cx);1370
let font_overrides: Vec<_> =1371
font_override.into_iter().map(|o| (9..17, o)).collect();1372
wrapper.set_font_overrides(font_overrides.into(), cx);1364
wrapper.line(0).unwrap().wrapped_lines.to_vec()1373
wrapper.line(0).unwrap().wrapped_lines.to_vec()1365
};1374
};1366
assert_eq!(wrap(vec![], cx), [0..18, 18..26]);1375
assert_eq!(wrap(None, cx), [0..18, 18..26]);1367
assert_eq!(1376
let mono_override = FontOverride {1368
wrap(vec![(9..17, mono.family.clone())], cx),1377
family: Some(mono.family.clone()),1369
[0..9, 9..18, 18..26]1378
..Default::default()1370
);1379
};1380
assert_eq!(wrap(Some(mono_override), cx), [0..9, 9..18, 18..26]);1381
let bold_override = FontOverride {1382
weight: Some(FontWeight::BOLD),1383
..Default::default()1384
};1385
assert_eq!(wrap(Some(bold_override), cx), [0..9, 9..18, 18..26]);1371
});1386
});1372
}1387
}1373
1388
1374
#[test]1389
#[test]1375
fn font_family_runs_split_at_span_edges_and_follow_edits() {1390
fn font_override_runs_split_at_span_edges_and_follow_edits() {1376
let run = TextRun {1391
let run = TextRun {1377
len: 10,1392
len: 10,1378
font: gpui::font("Sans"),1393
font: gpui::font("Sans"),@@ -1381,29 +1396,37 @@ mod tests {1381
underline: None,1396
underline: None,1382
strikethrough: None,1397
strikethrough: None,1383
};1398
};1384
let font_families = [(2..4, SharedString::from("Mono")), (8..20, "Mono".into())];1399
let mono = FontOverride {1385
let runs = split_run_by_font_families(run.clone(), 0..10, &font_families);1400
family: Some("Mono".into()),1401
weight: Some(FontWeight::BOLD),1402
style: None,1403
};1404
let font_overrides = [(2..4, mono.clone()), (8..20, mono.clone())];1405
let runs = split_run_by_font_overrides(run.clone(), 0..10, &font_overrides);1386
let runs: Vec<_> = runs1406
let runs: Vec<_> = runs1387
.iter()1407
.iter()1388
.map(|run| (run.len, run.font.family.to_string()))1408
.map(|run| (run.len, run.font.family.to_string(), run.font.weight))1389
.collect();1409
.collect();1410
let (regular, bold) = (FontWeight::NORMAL, FontWeight::BOLD);1390
assert_eq!(1411
assert_eq!(1391
runs,1412
runs,1392
[(2, "Sans"), (2, "Mono"), (4, "Sans"), (2, "Mono")]1413
[1393
.map(|(len, family)| (len, family.to_string()))1414
(2, "Sans", regular),1415
(2, "Mono", bold),1416
(4, "Sans", regular),1417
(2, "Mono", bold)1418
]1419
.map(|(len, family, weight)| (len, family.to_string(), weight))1394
);1420
);1395
let runs = split_run_by_font_families(TextRun { len: 3, ..run }, 4..7, &font_families);1421
let runs = split_run_by_font_overrides(TextRun { len: 3, ..run }, 4..7, &font_overrides);1396
assert_eq!(runs.len(), 1);1422
assert_eq!(runs.len(), 1);1397
assert_eq!(runs[0].font.family, "Sans");1423
assert_eq!(runs[0].font.family, "Sans");1398
1424
1399
let mut wrapper = TextWrapper::new(test_font(), px(14.), None);1425
let mut wrapper = TextWrapper::new(test_font(), px(14.), None);1400
wrapper.font_families = Rc::from(font_families);1426
wrapper.font_overrides = Rc::from(font_overrides);1401
wrapper.adjust_font_families(&(0..0), 1);1427
wrapper.adjust_font_overrides(&(0..0), 1);1402
wrapper.adjust_font_families(&(9..21), 0);1428
wrapper.adjust_font_overrides(&(9..21), 0);1403
assert_eq!(1429
assert_eq!(&*wrapper.font_overrides, [(3..5, mono)]);1404
&*wrapper.font_families,1405
[(3..5, SharedString::from("Mono"))]1406
);1407
}1430
}1408
1431
1409
#[test]1432
#[test]@@ -108,7 +108,7 @@ impl WrapMap {
108
108
cx: &mut App,
109
109
) {
110
110
self.wrapper.adjust_inline_metrics(range, new_text.len());
111
self.wrapper.adjust_font_families(range, new_text.len());
111
self.wrapper.adjust_font_overrides(range, new_text.len());
112
112
self.wrapper.adjust_hanging_indents(range, new_text.len());
113
113
self.wrapper.update(changed_text, range, new_text, cx);
114
114
}
@@ -122,12 +122,12 @@ impl WrapMap {
122
122
self.wrapper.set_inline_metrics(metrics, cx);
123
123
}
124
124
125
pub(super) fn set_font_families(
125
pub(super) fn set_font_overrides(
126
126
&mut self,
127
font_families: std::rc::Rc<[(Range<usize>, gpui::SharedString)]>,
127
font_overrides: std::rc::Rc<[(Range<usize>, crate::input::decorations::FontOverride)]>,
128
128
cx: &mut App,
129
129
) {
130
self.wrapper.set_font_families(font_families, cx);
130
self.wrapper.set_font_overrides(font_overrides, cx);
131
131
}
132
132
133
133
pub(super) fn set_hanging_indents(
@@ -108,7 +108,7 @@ impl WrapMap {108
cx: &mut App,108
cx: &mut App,109
) {109
) {110
self.wrapper.adjust_inline_metrics(range, new_text.len());110
self.wrapper.adjust_inline_metrics(range, new_text.len());111
self.wrapper.adjust_font_families(range, new_text.len());111
self.wrapper.adjust_font_overrides(range, new_text.len());112
self.wrapper.adjust_hanging_indents(range, new_text.len());112
self.wrapper.adjust_hanging_indents(range, new_text.len());113
self.wrapper.update(changed_text, range, new_text, cx);113
self.wrapper.update(changed_text, range, new_text, cx);114
}114
}@@ -122,12 +122,12 @@ impl WrapMap {122
self.wrapper.set_inline_metrics(metrics, cx);122
self.wrapper.set_inline_metrics(metrics, cx);123
}123
}124
124
125
pub(super) fn set_font_families(125
pub(super) fn set_font_overrides(126
&mut self,126
&mut self,127
font_families: std::rc::Rc<[(Range<usize>, gpui::SharedString)]>,127
font_overrides: std::rc::Rc<[(Range<usize>, crate::input::decorations::FontOverride)]>,128
cx: &mut App,128
cx: &mut App,129
) {129
) {130
self.wrapper.set_font_families(font_families, cx);130
self.wrapper.set_font_overrides(font_overrides, cx);131
}131
}132
132
133
pub(super) fn set_hanging_indents(133
pub(super) fn set_hanging_indents(