mirror of
https://github.com/typst/typst
synced 2025-05-15 01:25:28 +08:00
Explain ShapedGlyph
and Glyph
(#2901)
This commit is contained in:
parent
5338b02f9f
commit
5eedff8667
@ -240,24 +240,24 @@ impl<'a> ShapedText<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let pos = Point::new(offset, top + shift - y_offset.at(self.size));
|
let pos = Point::new(offset, top + shift - y_offset.at(self.size));
|
||||||
let glyphs = group
|
let glyphs: Vec<Glyph> = group
|
||||||
.iter()
|
.iter()
|
||||||
.map(|glyph| {
|
.map(|shaped: &ShapedGlyph| {
|
||||||
let adjustability_left = if justification_ratio < 0.0 {
|
let adjustability_left = if justification_ratio < 0.0 {
|
||||||
glyph.shrinkability().0
|
shaped.shrinkability().0
|
||||||
} else {
|
} else {
|
||||||
glyph.stretchability().0
|
shaped.stretchability().0
|
||||||
};
|
};
|
||||||
let adjustability_right = if justification_ratio < 0.0 {
|
let adjustability_right = if justification_ratio < 0.0 {
|
||||||
glyph.shrinkability().1
|
shaped.shrinkability().1
|
||||||
} else {
|
} else {
|
||||||
glyph.stretchability().1
|
shaped.stretchability().1
|
||||||
};
|
};
|
||||||
|
|
||||||
let justification_left = adjustability_left * justification_ratio;
|
let justification_left = adjustability_left * justification_ratio;
|
||||||
let mut justification_right =
|
let mut justification_right =
|
||||||
adjustability_right * justification_ratio;
|
adjustability_right * justification_ratio;
|
||||||
if glyph.is_justifiable() {
|
if shaped.is_justifiable() {
|
||||||
justification_right +=
|
justification_right +=
|
||||||
Em::from_length(extra_justification, self.size)
|
Em::from_length(extra_justification, self.size)
|
||||||
}
|
}
|
||||||
@ -265,15 +265,33 @@ impl<'a> ShapedText<'a> {
|
|||||||
frame.size_mut().x += justification_left.at(self.size)
|
frame.size_mut().x += justification_left.at(self.size)
|
||||||
+ justification_right.at(self.size);
|
+ justification_right.at(self.size);
|
||||||
|
|
||||||
|
// |<---- a Glyph ---->|
|
||||||
|
// -->|ShapedGlyph|<--
|
||||||
|
// +---+-----------+---+
|
||||||
|
// | | *********| |
|
||||||
|
// | | * | |
|
||||||
|
// | | * ****| |
|
||||||
|
// | | * *| |
|
||||||
|
// | | *********| |
|
||||||
|
// +---+--+--------+---+
|
||||||
|
// A B C D
|
||||||
|
// Note A, B, D could be positive, zero, or negative.
|
||||||
|
// A: justification_left
|
||||||
|
// B: ShapedGlyph's x_offset
|
||||||
|
// (though a small part of the glyph may go inside B)
|
||||||
|
// B+C: ShapedGlyph's x_advance
|
||||||
|
// D: justification_right
|
||||||
|
// A+B: Glyph's x_offset
|
||||||
|
// A+B+C+D: Glyph's x_advance
|
||||||
Glyph {
|
Glyph {
|
||||||
id: glyph.glyph_id,
|
id: shaped.glyph_id,
|
||||||
x_advance: glyph.x_advance
|
x_advance: shaped.x_advance
|
||||||
+ justification_left
|
+ justification_left
|
||||||
+ justification_right,
|
+ justification_right,
|
||||||
x_offset: glyph.x_offset + justification_left,
|
x_offset: shaped.x_offset + justification_left,
|
||||||
range: (glyph.range.start - range.start).saturating_as()
|
range: (shaped.range.start - range.start).saturating_as()
|
||||||
..(glyph.range.end - range.start).saturating_as(),
|
..(shaped.range.end - range.start).saturating_as(),
|
||||||
span: glyph.span,
|
span: shaped.span,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -21,7 +21,8 @@ pub struct TextItem {
|
|||||||
pub lang: Lang,
|
pub lang: Lang,
|
||||||
/// The item's plain text.
|
/// The item's plain text.
|
||||||
pub text: EcoString,
|
pub text: EcoString,
|
||||||
/// The glyphs.
|
/// The glyphs. The number of glyphs may be different from the number of
|
||||||
|
/// characters in the plain text due to e.g. ligatures.
|
||||||
pub glyphs: Vec<Glyph>,
|
pub glyphs: Vec<Glyph>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +50,8 @@ pub struct Glyph {
|
|||||||
pub x_advance: Em,
|
pub x_advance: Em,
|
||||||
/// The horizontal offset of the glyph.
|
/// The horizontal offset of the glyph.
|
||||||
pub x_offset: Em,
|
pub x_offset: Em,
|
||||||
/// The range of the glyph in its item's text.
|
/// The range of the glyph in its item's text. The range's length may
|
||||||
|
/// be more than one due to multi-byte UTF-8 encoding or ligatures.
|
||||||
pub range: Range<u16>,
|
pub range: Range<u16>,
|
||||||
/// The source code location of the text.
|
/// The source code location of the text.
|
||||||
pub span: (Span, u16),
|
pub span: (Span, u16),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user