pub(crate) fn allows_break_between(prev: char, c: char) -> bool {
!never_starts_line(c)
&& !never_ends_line(prev)
&& !(prev == '—' && c == '—')
&& !(is_cjk(prev) && closes_cjk_text(c))
}
fn closes_cjk_text(c: char) -> bool {
matches!(c, ',' | '.' | ';' | ':' | '!' | '?' | ')' | ']' | '}' | '%')
}
fn is_cjk(c: char) -> bool {
matches!(
c,
'\u{3000}'..='\u{30FF}'
| '\u{3400}'..='\u{4DBF}'
| '\u{4E00}'..='\u{9FFF}'
| '\u{AC00}'..='\u{D7AF}'
| '\u{F900}'..='\u{FAFF}'
| '\u{FF00}'..='\u{FFEF}'
| '\u{20000}'..='\u{3FFFF}'
)
}
fn never_starts_line(c: char) -> bool {
matches!(
c,
'、' | '。' | ',' | '.' | ':' | ';' | '?' | '!' | '。' | '、'
| ')' | ']' | '}' | '」' | '』' | '】' | '〉' | '》' | '〕' | '〗' | '〙'
| '〛' | '〞' | '〟' | '⦆' | '」' | '’' | '”'
| '…' | '‥' | '・' | '・' | '〜' | '‼' | '⁇' | '⁈' | '⁉' | '々' | '〻' | 'ゝ'
| 'ゞ' | 'ヽ' | 'ヾ'
| '%'
)
}
fn never_ends_line(c: char) -> bool {
matches!(
c,
'(' | '[' | '{'
| '('
| '['
| '{'
| '「'
| '『'
| '【'
| '〈'
| '《'
| '〔'
| '〖'
| '〘'
| '〚'
| '〝'
| '⦅'
| '「'
| '‘'
| '“'
)
}
#[cfg(test)]
mod tests {
use super::{allows_break_between, never_ends_line, never_starts_line};
use crate::{
Boundary, FontId, GlyphId, IndentAdjustment, LineFragment, LineLayout, LineWrapper, Pixels,
ShapedGlyph, ShapedRun, TestAppContext, TestDispatcher, TextRun, TextSystem,
WindowTextSystem, font, point, px,
};
use std::sync::Arc;
const SENTENCE: &str = "今天的天气非常好,我们一起去公园散步吧。";
const MIXED: &str = "我用(Rust)重写了程序:速度提升了8%,超出预期……「渲染」部分表现亮眼——尤其是《文档》提到的部分。";
fn wrapper() -> (LineWrapper, Arc<TextSystem>) {
let cx = TestAppContext::build(TestDispatcher::new(0), None);
let text_system = cx.text_system().clone();
let id = text_system.resolve_font(&font(".ZedMono"));
(
LineWrapper::new(id, px(16.), text_system.clone()),
text_system,
)
}
fn width_of(text_system: &TextSystem, wrapper: &LineWrapper, text: &str) -> Pixels {
text.chars()
.map(|c| {
text_system
.advance(wrapper.font_id, wrapper.font_size, c)
.unwrap()
.width
})
.fold(px(0.), |sum, width| sum + width)
}
fn wrap(wrapper: &mut LineWrapper, text: &str, width: Pixels) -> Vec<usize> {
wrapper
.wrap_line(
&[LineFragment::text(text)],
width,
IndentAdjustment::NoIndent,
)
.map(|Boundary { ix, .. }| ix)
.collect()
}
#[test]
fn a_comma_moves_down_with_the_character_before_it() {
let (mut wrapper, text_system) = wrapper();
let fits = "今天的天气非常好";
let comma = width_of(&text_system, &wrapper, ",");
assert!(comma > px(0.));
let width = width_of(&text_system, &wrapper, fits) + comma / 2.;
let boundaries = wrap(&mut wrapper, SENTENCE, width);
assert_eq!(boundaries[0], fits.len() - "好".len());
assert_eq!(&SENTENCE[boundaries[0]..], "好,我们一起去公园散步吧。");
}
#[test]
fn an_ascii_comma_after_cjk_text_moves_down_with_it() {
let (mut wrapper, text_system) = wrapper();
let sentence = "今天的天气非常好,我们一起去公园散步吧.";
let fits = "今天的天气非常好";
let comma = width_of(&text_system, &wrapper, ",");
assert!(comma > px(0.));
let width = width_of(&text_system, &wrapper, fits) + comma / 2.;
let boundaries = wrap(&mut wrapper, sentence, width);
assert_eq!(&sentence[boundaries[0]..], "好,我们一起去公园散步吧.");
}
#[test]
fn no_width_starts_a_line_with_closing_or_ends_one_with_opening_punctuation() {
let text = MIXED;
let (mut wrapper, text_system) = wrapper();
let one = width_of(&text_system, &wrapper, "中");
for columns in 6..text.chars().count() {
let width = one * columns as f32 + one / 2.;
for ix in wrap(&mut wrapper, text, width) {
let next = text[ix..].chars().next().unwrap();
let prev = text[..ix].chars().next_back().unwrap();
assert!(
!never_starts_line(next) && !never_ends_line(prev),
"{columns} columns: `{}` / `{}`",
&text[..ix],
&text[ix..],
);
}
}
}
#[test]
fn ideographs_and_latin_text_still_break_as_before() {
assert!(allows_break_between('好', '我'));
assert!(allows_break_between(',', '我'));
assert!(allows_break_between('用', '('));
assert!(allows_break_between(')', '重'));
assert!(!allows_break_between('—', '—'));
assert!(!allows_break_between('8', '%'));
assert!(allows_break_between('去', '('));
assert!(!allows_break_between('(', '公'));
assert!(!allows_break_between('园', ')'));
assert!(!allows_break_between('好', ','));
assert!(!allows_break_between('な', '.'));
assert!(!allows_break_between('8', '%'));
assert!(allows_break_between('m', '.'));
assert!(allows_break_between('b', ')'));
assert!(allows_break_between(',', '你'));
let (mut wrapper, text_system) = wrapper();
let one = width_of(&text_system, &wrapper, "中");
assert_eq!(wrap(&mut wrapper, "中文中文中文", one * 2.5), [6, 12]);
let width = width_of(&text_system, &wrapper, "world, 你好") + one / 2.;
assert_eq!(wrap(&mut wrapper, "Hello world, 你好世界", width), [6, 19]);
}
#[test]
fn text_after_an_element_breaks_as_if_it_started_the_line() {
let (mut wrapper, text_system) = wrapper();
let one = width_of(&text_system, &wrapper, "中");
let fragments = [
LineFragment::text("看("),
LineFragment::element(one, 1),
LineFragment::text("我们一起散步"),
];
let boundaries: Vec<_> = wrapper
.wrap_line(&fragments, one * 3.5, IndentAdjustment::NoIndent)
.map(|Boundary { ix, .. }| ix)
.collect();
assert_eq!(boundaries[0], "看(".len() + 1);
}
#[test]
fn shaped_text_keeps_a_comma_with_the_character_before_it() {
let cx = TestAppContext::build(TestDispatcher::new(0), None);
let text_system = WindowTextSystem::new(cx.text_system().clone());
let runs = [TextRun {
len: SENTENCE.len(),
font: font(".ZedMono"),
..Default::default()
}];
let shape = |wrap_width| {
text_system
.shape_text(SENTENCE.into(), px(16.), &runs, wrap_width, None)
.unwrap()
.remove(0)
};
let glyph_x = |line: &crate::WrappedLine, index: usize| {
line.layout
.unwrapped_layout
.runs
.iter()
.flat_map(|run| &run.glyphs)
.find(|glyph| glyph.index == index)
.unwrap()
.position
.x
};
let comma = SENTENCE.find(',').unwrap();
let line = shape(None);
let width = (glyph_x(&line, comma) + glyph_x(&line, comma + ','.len_utf8())) / 2.;
let line = shape(Some(width));
let boundary = line.layout.wrap_boundaries()[0];
let layout = &line.layout.unwrapped_layout;
let ix = layout.runs[boundary.run_ix].glyphs[boundary.glyph_ix].index;
assert_eq!(&SENTENCE[ix..], "好,我们一起去公园散步吧。");
}
fn layout_of(text: &str, advance: f32) -> LineLayout {
let glyphs: Vec<_> = text
.char_indices()
.enumerate()
.map(|(column, (index, _))| ShapedGlyph {
id: GlyphId(0),
position: point(px(advance * column as f32), px(0.)),
index,
is_emoji: false,
})
.collect();
LineLayout {
font_size: px(16.),
width: px(advance * glyphs.len() as f32),
ascent: px(12.),
descent: px(4.),
runs: vec![ShapedRun {
font_id: FontId(0),
glyphs,
}],
len: text.len(),
}
}
fn wrapped_lines(text: &str, wrap_width: f32) -> Vec<&str> {
let layout = layout_of(text, 10.);
let mut start = 0;
let mut lines = Vec::new();
for boundary in layout.compute_wrap_boundaries(text, px(wrap_width), None) {
let end = layout.runs[boundary.run_ix].glyphs[boundary.glyph_ix].index;
lines.push(&text[start..end]);
start = end;
}
lines.push(&text[start..]);
lines
}
#[test]
fn shaped_lines_keep_cjk_punctuation_with_the_character_before_it() {
assert_eq!(
wrapped_lines(SENTENCE, 85.),
["今天的天气非常", "好,我们一起去公", "园散步吧。"]
);
assert_eq!(
wrapped_lines("我们去(公园)散步", 45.),
["我们去", "(公园)", "散步"]
);
assert_eq!(
wrapped_lines("Hello world, 你好世界", 135.),
["Hello world, ", "你好世界"]
);
}
#[test]
fn no_width_breaks_a_shaped_line_inside_cjk_punctuation() {
for columns in 6..MIXED.chars().count() {
let lines = wrapped_lines(MIXED, 10. * columns as f32 + 5.);
for pair in lines.windows(2) {
let prev = pair[0].chars().next_back().unwrap();
let next = pair[1].chars().next().unwrap();
assert!(
allows_break_between(prev, next),
"{columns} columns: {lines:?}"
);
}
}
}
#[test]
fn a_shaped_line_breaks_where_it_overflows_when_no_break_is_allowed() {
let text = ",。、!?:;,。";
assert_eq!(
wrapped_lines(text, 25.),
[",。", "、!", "?:", ";,", "。"]
);
assert_eq!(
wrapped_lines(text, 5.),
[",", "。", "、", "!", "?", ":", ";", ",", "。"]
);
assert_eq!(
wrapped_lines("好,,,,,,", 35.),
["好,,", ",,,", ","]
);
}
}