Skip to content

Commit

Permalink
Merge pull request #82 from Wybxc/master
Browse files Browse the repository at this point in the history
fix: CJK characters display width
  • Loading branch information
Marwes committed Sep 21, 2023
2 parents 18fa7e0 + d1cdc4e commit 81bf465
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ features = ["termcolor"]
arrayvec = "0.5"
typed-arena = "2.0.0"
termcolor = { version = "1.1.0", optional = true }
unicode-segmentation = "1"
unicode-width = "0.1"

[dev-dependencies]
criterion = "0.4"
Expand Down
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,16 +1248,16 @@ where
Doc::SmallText(s) => s,
_ => return self,
};
use unicode_segmentation::UnicodeSegmentation;

if s.is_ascii() {
self
} else {
let grapheme_len = s.graphemes(true).count();
let display_width = unicode_width::UnicodeWidthStr::width(s);

let DocBuilder(allocator, _) = self;
DocBuilder(
allocator,
Doc::RenderLen(grapheme_len, self.into_doc()).into(),
Doc::RenderLen(display_width, self.into_doc()).into(),
)
}
}
Expand Down Expand Up @@ -2126,4 +2126,15 @@ mod tests {

test!(8, doc, "ÅÄÖ test");
}

#[test]
fn cjk_display_width() {
let arena = Arena::<()>::new();
let doc = arena
.text("你好")
.append(arena.line().append(arena.text("abc")).align())
.into_doc();

test!(doc, "你好\n abc");
}
}

0 comments on commit 81bf465

Please sign in to comment.