mirror of
https://github.com/typst/typst
synced 2025-08-22 10:44:14 +08:00
Store size in ShapedGlyph
This commit is contained in:
parent
8228d16a1e
commit
098f8386ab
@ -330,7 +330,7 @@ fn adjust_cj_at_line_start(p: &Preparation, items: &mut Items) {
|
||||
let glyph = shaped.glyphs.to_mut().first_mut().unwrap();
|
||||
let shrink = glyph.shrinkability().0;
|
||||
glyph.shrink_left(shrink);
|
||||
shaped.width -= shrink.at(shaped.size);
|
||||
shaped.width -= shrink.at(glyph.size);
|
||||
} else if p.config.cjk_latin_spacing
|
||||
&& glyph.is_cj_script()
|
||||
&& glyph.x_offset > Em::zero()
|
||||
@ -342,7 +342,7 @@ fn adjust_cj_at_line_start(p: &Preparation, items: &mut Items) {
|
||||
glyph.x_advance -= shrink;
|
||||
glyph.x_offset = Em::zero();
|
||||
glyph.adjustability.shrinkability.0 = Em::zero();
|
||||
shaped.width -= shrink.at(shaped.size);
|
||||
shaped.width -= shrink.at(glyph.size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -360,7 +360,7 @@ fn adjust_cj_at_line_end(p: &Preparation, items: &mut Items) {
|
||||
let shrink = glyph.shrinkability().1;
|
||||
let punct = shaped.glyphs.to_mut().last_mut().unwrap();
|
||||
punct.shrink_right(shrink);
|
||||
shaped.width -= shrink.at(shaped.size);
|
||||
shaped.width -= shrink.at(punct.size);
|
||||
} else if p.config.cjk_latin_spacing
|
||||
&& glyph.is_cj_script()
|
||||
&& (glyph.x_advance - glyph.x_offset) > Em::one()
|
||||
@ -371,7 +371,7 @@ fn adjust_cj_at_line_end(p: &Preparation, items: &mut Items) {
|
||||
let glyph = shaped.glyphs.to_mut().last_mut().unwrap();
|
||||
glyph.x_advance -= shrink;
|
||||
glyph.adjustability.shrinkability.1 = Em::zero();
|
||||
shaped.width -= shrink.at(shaped.size);
|
||||
shaped.width -= shrink.at(glyph.size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -465,7 +465,7 @@ pub fn commit(
|
||||
&& TextElem::overhang_in(text.styles)
|
||||
&& (line.items.len() > 1 || text.glyphs.len() > 1)
|
||||
{
|
||||
let amount = overhang(glyph.c) * glyph.x_advance.at(text.size);
|
||||
let amount = overhang(glyph.c) * glyph.x_advance.at(glyph.size);
|
||||
offset -= amount;
|
||||
remaining += amount;
|
||||
}
|
||||
@ -479,7 +479,7 @@ pub fn commit(
|
||||
&& TextElem::overhang_in(text.styles)
|
||||
&& (line.items.len() > 1 || text.glyphs.len() > 1)
|
||||
{
|
||||
let amount = overhang(glyph.c) * glyph.x_advance.at(text.size);
|
||||
let amount = overhang(glyph.c) * glyph.x_advance.at(glyph.size);
|
||||
remaining += amount;
|
||||
}
|
||||
}
|
||||
|
@ -927,9 +927,9 @@ impl Estimates {
|
||||
let byte_len = g.range.len();
|
||||
let stretch = g.stretchability().0 + g.stretchability().1;
|
||||
let shrink = g.shrinkability().0 + g.shrinkability().1;
|
||||
widths.push(byte_len, g.x_advance.at(shaped.size));
|
||||
stretchability.push(byte_len, stretch.at(shaped.size));
|
||||
shrinkability.push(byte_len, shrink.at(shaped.size));
|
||||
widths.push(byte_len, g.x_advance.at(g.size));
|
||||
stretchability.push(byte_len, stretch.at(g.size));
|
||||
shrinkability.push(byte_len, shrink.at(g.size));
|
||||
justifiables.push(byte_len, g.is_justifiable() as usize);
|
||||
}
|
||||
} else {
|
||||
|
@ -144,7 +144,7 @@ fn add_cjk_latin_spacing(items: &mut [(Range, Item)]) {
|
||||
// The spacing is default to 1/4 em, and can be shrunk to 1/8 em.
|
||||
glyph.x_advance += Em::new(0.25);
|
||||
glyph.adjustability.shrinkability.1 += Em::new(0.125);
|
||||
text.width += Em::new(0.25).at(text.size);
|
||||
text.width += Em::new(0.25).at(glyph.size);
|
||||
}
|
||||
|
||||
// Case 2: Latin followed by a CJ character
|
||||
@ -152,7 +152,7 @@ fn add_cjk_latin_spacing(items: &mut [(Range, Item)]) {
|
||||
glyph.x_advance += Em::new(0.25);
|
||||
glyph.x_offset += Em::new(0.25);
|
||||
glyph.adjustability.shrinkability.0 += Em::new(0.125);
|
||||
text.width += Em::new(0.25).at(text.size);
|
||||
text.width += Em::new(0.25).at(glyph.size);
|
||||
}
|
||||
|
||||
prev = Some(glyph);
|
||||
|
@ -42,8 +42,6 @@ pub struct ShapedText<'a> {
|
||||
pub styles: StyleChain<'a>,
|
||||
/// The font variant.
|
||||
pub variant: FontVariant,
|
||||
/// The font size.
|
||||
pub size: Abs,
|
||||
/// The width of the text's bounding box.
|
||||
pub width: Abs,
|
||||
/// The shaped glyphs.
|
||||
@ -63,8 +61,8 @@ pub struct ShapedGlyph {
|
||||
pub x_offset: Em,
|
||||
/// The vertical offset of the glyph.
|
||||
pub y_offset: Em,
|
||||
/// How much to scale the glyph compared to its normal size.
|
||||
pub scale: Em,
|
||||
/// The font size for the glyph.
|
||||
pub size: Abs,
|
||||
/// The adjustability of the glyph.
|
||||
pub adjustability: Adjustability,
|
||||
/// The byte range of this glyph's cluster in the full inline layout. A
|
||||
@ -225,16 +223,17 @@ impl<'a> ShapedText<'a> {
|
||||
let mut frame = Frame::soft(size);
|
||||
frame.set_baseline(top);
|
||||
|
||||
let size = TextElem::size_in(self.styles);
|
||||
let shift = TextElem::baseline_in(self.styles);
|
||||
let decos = TextElem::deco_in(self.styles);
|
||||
let fill = TextElem::fill_in(self.styles);
|
||||
let stroke = TextElem::stroke_in(self.styles);
|
||||
let span_offset = TextElem::span_offset_in(self.styles);
|
||||
|
||||
for ((font, y_offset, scale), group) in self
|
||||
for ((font, y_offset, glyph_size), group) in self
|
||||
.glyphs
|
||||
.as_ref()
|
||||
.group_by_key(|g| (g.font.clone(), g.y_offset, g.scale))
|
||||
.group_by_key(|g| (g.font.clone(), g.y_offset, g.size))
|
||||
{
|
||||
let mut range = group[0].range.clone();
|
||||
for glyph in group {
|
||||
@ -242,7 +241,7 @@ impl<'a> ShapedText<'a> {
|
||||
range.end = range.end.max(glyph.range.end);
|
||||
}
|
||||
|
||||
let pos = Point::new(offset, top + shift - y_offset.at(self.size));
|
||||
let pos = Point::new(offset, top + shift - y_offset.at(size));
|
||||
let glyphs: Vec<Glyph> = group
|
||||
.iter()
|
||||
.map(|shaped: &ShapedGlyph| {
|
||||
@ -262,11 +261,11 @@ impl<'a> ShapedText<'a> {
|
||||
adjustability_right * justification_ratio;
|
||||
if shaped.is_justifiable() {
|
||||
justification_right +=
|
||||
Em::from_length(extra_justification, self.size)
|
||||
Em::from_length(extra_justification, glyph_size)
|
||||
}
|
||||
|
||||
frame.size_mut().x += justification_left.at(self.size)
|
||||
+ justification_right.at(self.size);
|
||||
frame.size_mut().x += justification_left.at(glyph_size)
|
||||
+ justification_right.at(glyph_size);
|
||||
|
||||
// We may not be able to reach the offset completely if
|
||||
// it exceeds u16, but better to have a roughly correct
|
||||
@ -309,7 +308,7 @@ impl<'a> ShapedText<'a> {
|
||||
|
||||
let item = TextItem {
|
||||
font,
|
||||
size: scale.at(self.size),
|
||||
size: glyph_size,
|
||||
lang: self.lang,
|
||||
region: self.region,
|
||||
fill: fill.clone(),
|
||||
@ -341,12 +340,13 @@ impl<'a> ShapedText<'a> {
|
||||
let mut top = Abs::zero();
|
||||
let mut bottom = Abs::zero();
|
||||
|
||||
let size = TextElem::size_in(self.styles);
|
||||
let top_edge = TextElem::top_edge_in(self.styles);
|
||||
let bottom_edge = TextElem::bottom_edge_in(self.styles);
|
||||
|
||||
// Expand top and bottom by reading the font's vertical metrics.
|
||||
let mut expand = |font: &Font, bounds: TextEdgeBounds| {
|
||||
let (t, b) = font.edges(top_edge, bottom_edge, self.size, bounds);
|
||||
let (t, b) = font.edges(top_edge, bottom_edge, size, bounds);
|
||||
top.set_max(t);
|
||||
bottom.set_max(b);
|
||||
};
|
||||
@ -393,18 +393,16 @@ impl<'a> ShapedText<'a> {
|
||||
pub fn stretchability(&self) -> Abs {
|
||||
self.glyphs
|
||||
.iter()
|
||||
.map(|g| g.stretchability().0 + g.stretchability().1)
|
||||
.sum::<Em>()
|
||||
.at(self.size)
|
||||
.map(|g|( g.stretchability().0 + g.stretchability().1).at(g.size))
|
||||
.sum()
|
||||
}
|
||||
|
||||
/// The shrinkability of the text
|
||||
pub fn shrinkability(&self) -> Abs {
|
||||
self.glyphs
|
||||
.iter()
|
||||
.map(|g| g.shrinkability().0 + g.shrinkability().1)
|
||||
.sum::<Em>()
|
||||
.at(self.size)
|
||||
.map(|g| (g.shrinkability().0 + g.shrinkability().1).at(g.size))
|
||||
.sum()
|
||||
}
|
||||
|
||||
/// Reshape a range of the shaped text, reusing information from this
|
||||
@ -423,9 +421,8 @@ impl<'a> ShapedText<'a> {
|
||||
lang: self.lang,
|
||||
region: self.region,
|
||||
styles: self.styles,
|
||||
size: self.size,
|
||||
variant: self.variant,
|
||||
width: glyphs_width(glyphs).at(self.size),
|
||||
width: glyphs_width(glyphs),
|
||||
glyphs: Cow::Borrowed(glyphs),
|
||||
}
|
||||
} else {
|
||||
@ -489,14 +486,15 @@ impl<'a> ShapedText<'a> {
|
||||
// that subtracting either of the endpoints by self.base doesn't
|
||||
// underflow. See <https://github.com/typst/typst/issues/2283>.
|
||||
.unwrap_or_else(|| self.base..self.base);
|
||||
self.width += x_advance.at(self.size);
|
||||
let size = TextElem::size_in(self.styles);
|
||||
self.width += x_advance.at(size);
|
||||
let glyph = ShapedGlyph {
|
||||
font,
|
||||
glyph_id: glyph_id.0,
|
||||
x_advance,
|
||||
x_offset: Em::zero(),
|
||||
y_offset: Em::zero(),
|
||||
scale: Em::one(),
|
||||
size,
|
||||
adjustability: Adjustability::default(),
|
||||
range,
|
||||
safe_to_break: true,
|
||||
@ -706,16 +704,15 @@ fn shape<'a>(
|
||||
region,
|
||||
styles,
|
||||
variant: ctx.variant,
|
||||
size,
|
||||
width: glyphs_width(&ctx.glyphs).at(size),
|
||||
width: glyphs_width(&ctx.glyphs),
|
||||
glyphs: Cow::Owned(ctx.glyphs),
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the width of a run of glyphs relative to the font size, accounting
|
||||
/// for their individual scaling factors and other font metrics.
|
||||
fn glyphs_width(glyphs: &[ShapedGlyph]) -> Em {
|
||||
glyphs.iter().map(|g| g.x_advance * g.scale.get()).sum()
|
||||
fn glyphs_width(glyphs: &[ShapedGlyph]) -> Abs {
|
||||
glyphs.iter().map(|g| g.x_advance.at(g.size)).sum()
|
||||
}
|
||||
|
||||
/// Holds shaping results and metadata common to all shaped segments.
|
||||
@ -902,7 +899,7 @@ fn shape_segment<'a>(
|
||||
x_advance,
|
||||
x_offset: font.to_em(pos[i].x_offset) + script_compensation,
|
||||
y_offset: font.to_em(pos[i].y_offset) + script_shift,
|
||||
scale,
|
||||
size: scale.at(ctx.size),
|
||||
adjustability: Adjustability::default(),
|
||||
range: start..end,
|
||||
safe_to_break: !info.unsafe_to_break(),
|
||||
@ -1054,7 +1051,7 @@ fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, font: Font) {
|
||||
x_advance,
|
||||
x_offset: Em::zero(),
|
||||
y_offset: Em::zero(),
|
||||
scale: Em::one(),
|
||||
size: ctx.size,
|
||||
adjustability: Adjustability::default(),
|
||||
range: start..end,
|
||||
safe_to_break: true,
|
||||
|
Loading…
x
Reference in New Issue
Block a user