Keep font directly in GlyphFragment

This commit is contained in:
Laurenz 2023-02-03 17:46:30 +01:00
parent 15bc932ec1
commit fd7b629f67
7 changed files with 27 additions and 25 deletions

View File

@ -84,7 +84,7 @@ impl LayoutMath for AccentNode {
let base = ctx.layout_fragment(&self.base)?; let base = ctx.layout_fragment(&self.base)?;
ctx.unstyle(); ctx.unstyle();
let base_attach = match base { let base_attach = match &base {
MathFragment::Glyph(base) => { MathFragment::Glyph(base) => {
attachment(ctx, base.id, base.italics_correction) attachment(ctx, base.id, base.italics_correction)
} }
@ -118,7 +118,7 @@ impl LayoutMath for AccentNode {
let mut frame = Frame::new(size); let mut frame = Frame::new(size);
frame.set_baseline(baseline); frame.set_baseline(baseline);
frame.push_frame(accent_pos, accent); frame.push_frame(accent_pos, accent);
frame.push_frame(base_pos, base.to_frame(ctx)); frame.push_frame(base_pos, base.to_frame());
ctx.push(FrameFragment::new(ctx, frame).with_base_ascent(base_ascent)); ctx.push(FrameFragment::new(ctx, frame).with_base_ascent(base_ascent));
Ok(()) Ok(())

View File

@ -225,18 +225,18 @@ fn scripts(
let mut frame = Frame::new(Size::new(width, ascent + descent)); let mut frame = Frame::new(Size::new(width, ascent + descent));
frame.set_baseline(ascent); frame.set_baseline(ascent);
frame.push_frame(base_pos, base.to_frame(ctx)); frame.push_frame(base_pos, base.to_frame());
if let Some(sup) = sup { if let Some(sup) = sup {
let sup_pos = let sup_pos =
Point::new(sup_delta + base_width, ascent - shift_up - sup.ascent()); Point::new(sup_delta + base_width, ascent - shift_up - sup.ascent());
frame.push_frame(sup_pos, sup.to_frame(ctx)); frame.push_frame(sup_pos, sup.to_frame());
} }
if let Some(sub) = sub { if let Some(sub) = sub {
let sub_pos = let sub_pos =
Point::new(sub_delta + base_width, ascent + shift_down - sub.ascent()); Point::new(sub_delta + base_width, ascent + shift_down - sub.ascent());
frame.push_frame(sub_pos, sub.to_frame(ctx)); frame.push_frame(sub_pos, sub.to_frame());
} }
ctx.push(FrameFragment::new(ctx, frame).with_class(class)); ctx.push(FrameFragment::new(ctx, frame).with_class(class));
@ -279,17 +279,17 @@ fn limits(
let mut frame = Frame::new(Size::new(width, height)); let mut frame = Frame::new(Size::new(width, height));
frame.set_baseline(base_pos.y + base.ascent()); frame.set_baseline(base_pos.y + base.ascent());
frame.push_frame(base_pos, base.to_frame(ctx)); frame.push_frame(base_pos, base.to_frame());
if let Some(top) = top { if let Some(top) = top {
let top_pos = Point::with_x((width - top.width()) / 2.0 + delta); let top_pos = Point::with_x((width - top.width()) / 2.0 + delta);
frame.push_frame(top_pos, top.to_frame(ctx)); frame.push_frame(top_pos, top.to_frame());
} }
if let Some(bottom) = bottom { if let Some(bottom) = bottom {
let bottom_pos = let bottom_pos =
Point::new((width - bottom.width()) / 2.0 - delta, height - bottom.height()); Point::new((width - bottom.width()) / 2.0 - delta, height - bottom.height());
frame.push_frame(bottom_pos, bottom.to_frame(ctx)); frame.push_frame(bottom_pos, bottom.to_frame());
} }
ctx.push(FrameFragment::new(ctx, frame).with_class(class)); ctx.push(FrameFragment::new(ctx, frame).with_class(class));

View File

@ -118,7 +118,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
} }
pub fn layout_frame(&mut self, node: &dyn LayoutMath) -> SourceResult<Frame> { pub fn layout_frame(&mut self, node: &dyn LayoutMath) -> SourceResult<Frame> {
Ok(self.layout_fragment(node)?.to_frame(self)) Ok(self.layout_fragment(node)?.to_frame())
} }
pub fn layout_content(&mut self, content: &Content) -> SourceResult<Frame> { pub fn layout_content(&mut self, content: &Content) -> SourceResult<Frame> {

View File

@ -101,7 +101,7 @@ fn scale(
Some(MathClass::Opening | MathClass::Closing | MathClass::Fence) Some(MathClass::Opening | MathClass::Closing | MathClass::Fence)
) { ) {
let glyph = match fragment { let glyph = match fragment {
MathFragment::Glyph(glyph) => *glyph, MathFragment::Glyph(glyph) => glyph.clone(),
MathFragment::Variant(variant) => GlyphFragment::new(ctx, variant.c), MathFragment::Variant(variant) => GlyphFragment::new(ctx, variant.c),
_ => return, _ => return,
}; };

View File

@ -105,9 +105,9 @@ impl MathFragment {
} }
} }
pub fn to_frame(self, ctx: &MathContext) -> Frame { pub fn to_frame(self) -> Frame {
match self { match self {
Self::Glyph(glyph) => glyph.to_frame(ctx), Self::Glyph(glyph) => glyph.to_frame(),
Self::Variant(variant) => variant.frame, Self::Variant(variant) => variant.frame,
Self::Frame(fragment) => fragment.frame, Self::Frame(fragment) => fragment.frame,
_ => Frame::new(self.size()), _ => Frame::new(self.size()),
@ -133,10 +133,11 @@ impl From<FrameFragment> for MathFragment {
} }
} }
#[derive(Clone, Copy)] #[derive(Clone)]
pub struct GlyphFragment { pub struct GlyphFragment {
pub id: GlyphId, pub id: GlyphId,
pub c: char, pub c: char,
pub font: Font,
pub lang: Lang, pub lang: Lang,
pub fill: Paint, pub fill: Paint,
pub width: Abs, pub width: Abs,
@ -178,6 +179,7 @@ impl GlyphFragment {
Self { Self {
id, id,
c, c,
font: ctx.font.clone(),
lang: ctx.styles().get(TextNode::LANG), lang: ctx.styles().get(TextNode::LANG),
fill: ctx.styles().get(TextNode::FILL), fill: ctx.styles().get(TextNode::FILL),
style: ctx.style, style: ctx.style,
@ -197,11 +199,11 @@ impl GlyphFragment {
self.ascent + self.descent self.ascent + self.descent
} }
pub fn to_variant(&self, ctx: &MathContext) -> VariantFragment { pub fn to_variant(&self) -> VariantFragment {
VariantFragment { VariantFragment {
c: self.c, c: self.c,
id: Some(self.id), id: Some(self.id),
frame: self.to_frame(ctx), frame: self.to_frame(),
style: self.style, style: self.style,
font_size: self.font_size, font_size: self.font_size,
italics_correction: self.italics_correction, italics_correction: self.italics_correction,
@ -209,9 +211,9 @@ impl GlyphFragment {
} }
} }
pub fn to_frame(&self, ctx: &MathContext) -> Frame { pub fn to_frame(&self) -> Frame {
let text = Text { let text = Text {
font: ctx.font.clone(), font: self.font.clone(),
size: self.font_size, size: self.font_size,
fill: self.fill, fill: self.fill,
lang: self.lang, lang: self.lang,

View File

@ -139,7 +139,7 @@ impl MathRow {
let mut frame = Frame::new(Size::zero()); let mut frame = Frame::new(Size::zero());
for (i, row) in rows.into_iter().enumerate() { for (i, row) in rows.into_iter().enumerate() {
let sub = row.to_line_frame(ctx, &points, align); let sub = row.to_line_frame(&points, align);
let size = frame.size_mut(); let size = frame.size_mut();
if i > 0 { if i > 0 {
size.y += leading; size.y += leading;
@ -155,11 +155,11 @@ impl MathRow {
} }
frame frame
} else { } else {
self.to_line_frame(ctx, points, align) self.to_line_frame(points, align)
} }
} }
fn to_line_frame(self, ctx: &MathContext, points: &[Abs], align: Align) -> Frame { fn to_line_frame(self, points: &[Abs], align: Align) -> Frame {
let ascent = self.ascent(); let ascent = self.ascent();
let descent = self.descent(); let descent = self.descent();
let size = Size::new(Abs::zero(), ascent + descent); let size = Size::new(Abs::zero(), ascent + descent);
@ -192,7 +192,7 @@ impl MathRow {
let y = ascent - fragment.ascent(); let y = ascent - fragment.ascent();
let pos = Point::new(x, y); let pos = Point::new(x, y);
x += fragment.width(); x += fragment.width();
frame.push_frame(pos, fragment.to_frame(ctx)); frame.push_frame(pos, fragment.to_frame());
} }
frame.size_mut().x = x; frame.size_mut().x = x;

View File

@ -57,7 +57,7 @@ fn stretch_glyph(
// If the base glyph is good enough, use it. // If the base glyph is good enough, use it.
let advance = if horizontal { base.width } else { base.height() }; let advance = if horizontal { base.width } else { base.height() };
if short_target <= advance { if short_target <= advance {
return base.to_variant(ctx); return base.to_variant();
} }
// Search for a pre-made variant with a good advance. // Search for a pre-made variant with a good advance.
@ -65,7 +65,7 @@ fn stretch_glyph(
let mut best_advance = base.width; let mut best_advance = base.width;
for variant in construction.variants { for variant in construction.variants {
best_id = variant.variant_glyph; best_id = variant.variant_glyph;
best_advance = variant.advance_measurement.scaled(ctx); best_advance = base.font.to_em(variant.advance_measurement).at(base.font_size);
if short_target <= best_advance { if short_target <= best_advance {
break; break;
} }
@ -73,7 +73,7 @@ fn stretch_glyph(
// This is either good or the best we've got. // This is either good or the best we've got.
if short_target <= best_advance || construction.assembly.is_none() { if short_target <= best_advance || construction.assembly.is_none() {
return GlyphFragment::with_id(ctx, base.c, best_id).to_variant(ctx); return GlyphFragment::with_id(ctx, base.c, best_id).to_variant();
} }
// Assemble from parts. // Assemble from parts.
@ -169,7 +169,7 @@ fn assemble(
} else { } else {
Point::with_y(full - offset - fragment.height()) Point::with_y(full - offset - fragment.height())
}; };
frame.push_frame(pos, fragment.to_frame(ctx)); frame.push_frame(pos, fragment.to_frame());
offset += advance; offset += advance;
} }