Improve reshape condition (#2547)

This commit is contained in:
Peng Guanwen 2023-11-07 04:43:15 +08:00 committed by GitHub
parent e550f0a8f7
commit 4a7d3585d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 11 deletions

View File

@ -488,17 +488,35 @@ impl<'a> ShapedText<'a> {
}
// Find any glyph with the text index.
let mut idx = self
.glyphs
.binary_search_by(|g| {
let ordering = g.range.start.cmp(&text_index);
if ltr {
ordering
} else {
ordering.reverse()
}
})
.ok()?;
let found = self.glyphs.binary_search_by(|g: &ShapedGlyph| {
let ordering = g.range.start.cmp(&text_index);
if ltr {
ordering
} else {
ordering.reverse()
}
});
let mut idx = match found {
Ok(idx) => idx,
Err(idx) => {
// Handle the special case where we break before a '\n'
//
// For example: (assume `a` is a CJK character with three bytes)
// text: " a \n b "
// index: 0 1 2 3 4 5
// text_index: ^
// glyphs: 0 . 1
//
// We will get found = Err(1), because '\n' does not have a glyph.
// But it's safe to break here. Thus the following condition:
// - glyphs[0].end == text_index == 3
// - text[3] == '\n'
return (idx > 0
&& self.glyphs[idx - 1].range.end == text_index
&& self.text[text_index - self.base..].starts_with('\n'))
.then_some(idx);
}
};
let next = match towards {
Side::Left => usize::checked_sub,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

@ -14,3 +14,16 @@
中文中ab文a中文ab中文
---
// Issue #2538
#set text(cjk-latin-spacing: auto)
abc字
abc字#linebreak()
abc字#linebreak()
abc字\