summaryrefslogtreecommitdiff
path: root/src/library/text/shaping.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-07 12:51:03 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-07 12:51:03 +0200
commiteb22eed31b08874fbbbee68d2ae59f0d02f9e95d (patch)
treec8f4926beee8fc5756eac9b8f6f63cafeb154cf4 /src/library/text/shaping.rs
parent3d52387eea321e94c13b61666f7a758052b20c5d (diff)
Make chinese justification less bad
Diffstat (limited to 'src/library/text/shaping.rs')
-rw-r--r--src/library/text/shaping.rs19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/library/text/shaping.rs b/src/library/text/shaping.rs
index 66936792..d398e56d 100644
--- a/src/library/text/shaping.rs
+++ b/src/library/text/shaping.rs
@@ -50,10 +50,15 @@ pub struct ShapedGlyph {
}
impl ShapedGlyph {
- /// Whether the glyph is a justifiable space.
+ /// Whether the glyph is a space.
pub fn is_space(&self) -> bool {
self.c == ' '
}
+
+ /// Whether the glyph is justifiable.
+ pub fn is_justifiable(&self) -> bool {
+ matches!(self.c, ' ' | ',' | ' ' | '。' | '、')
+ }
}
/// A side you can go toward.
@@ -68,7 +73,7 @@ impl<'a> ShapedText<'a> {
/// Build the shaped text's frame.
///
/// The `justification` defines how much extra advance width each
- /// [space glyph](ShapedGlyph::is_space) will get.
+ /// [justifiable glyph](ShapedGlyph::is_justifiable) will get.
pub fn build(&self, fonts: &FontStore, justification: Length) -> Frame {
let mut offset = Length::zero();
let mut frame = Frame::new(self.size);
@@ -84,7 +89,7 @@ impl<'a> ShapedText<'a> {
.map(|glyph| Glyph {
id: glyph.glyph_id,
x_advance: glyph.x_advance
- + if glyph.is_space() {
+ + if glyph.is_justifiable() {
frame.size.x += justification;
Em::from_length(justification, size)
} else {
@@ -115,16 +120,16 @@ impl<'a> ShapedText<'a> {
frame
}
- /// How many spaces the text contains.
- pub fn spaces(&self) -> usize {
- self.glyphs.iter().filter(|g| g.is_space()).count()
+ /// How many justifiable glyphs the text contains.
+ pub fn justifiables(&self) -> usize {
+ self.glyphs.iter().filter(|g| g.is_justifiable()).count()
}
/// The width of the spaces in the text.
pub fn stretch(&self) -> Length {
self.glyphs
.iter()
- .filter(|g| g.is_space())
+ .filter(|g| g.is_justifiable())
.map(|g| g.x_advance)
.sum::<Em>()
.resolve(self.styles.get(TextNode::SIZE))