diff options
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/math/rex.rs | 18 | ||||
| -rw-r--r-- | src/library/text/deco.rs | 18 | ||||
| -rw-r--r-- | src/library/text/mod.rs | 10 | ||||
| -rw-r--r-- | src/library/text/shaping.rs | 87 | ||||
| -rw-r--r-- | src/library/text/shift.rs | 4 | ||||
| -rw-r--r-- | src/library/utility/mod.rs | 4 |
6 files changed, 70 insertions, 71 deletions
diff --git a/src/library/math/rex.rs b/src/library/math/rex.rs index 9319d8ca..165642d3 100644 --- a/src/library/math/rex.rs +++ b/src/library/math/rex.rs @@ -4,7 +4,7 @@ use rex::layout::{LayoutSettings, Style}; use rex::parser::color::RGBA; use rex::render::{Backend, Cursor, Renderer}; -use crate::font::FaceId; +use crate::font::FontId; use crate::library::prelude::*; use crate::library::text::{variant, FontFamily, Lang, TextNode}; @@ -28,17 +28,17 @@ impl Layout for RexNode { ) -> TypResult<Vec<Frame>> { // Load the font. let span = self.tex.span; - let face_id = ctx + let font_id = ctx .fonts .select(self.family.as_str(), variant(styles)) .ok_or("failed to find math font") .at(span)?; // Prepare the font context. - let face = ctx.fonts.get(face_id); - let ctx = face + let font = ctx.fonts.get(font_id); + let ctx = font .math() - .map(|math| FontContext::new(face.ttf(), math)) + .map(|math| FontContext::new(font.ttf(), math)) .ok_or("font is not suitable for math") .at(span)?; @@ -61,7 +61,7 @@ impl Layout for RexNode { let mut top = Length::pt(y1); let mut bottom = Length::pt(-y0); if !self.display { - let metrics = face.metrics(); + let metrics = font.metrics(); top = styles.get(TextNode::TOP_EDGE).resolve(styles, metrics); bottom = -styles.get(TextNode::BOTTOM_EDGE).resolve(styles, metrics); }; @@ -76,7 +76,7 @@ impl Layout for RexNode { frame }, baseline: top, - face_id, + font_id, fill: styles.get(TextNode::FILL), lang: styles.get(TextNode::LANG), colors: vec![], @@ -93,7 +93,7 @@ impl Layout for RexNode { struct FrameBackend { frame: Frame, baseline: Length, - face_id: FaceId, + font_id: FontId, fill: Paint, lang: Lang, colors: Vec<RGBA>, @@ -119,7 +119,7 @@ impl Backend for FrameBackend { self.frame.push( self.transform(pos), Element::Text(Text { - face_id: self.face_id, + font_id: self.font_id, size: Length::pt(scale), fill: self.fill(), lang: self.lang, diff --git a/src/library/text/deco.rs b/src/library/text/deco.rs index e6a65eba..6d8b2854 100644 --- a/src/library/text/deco.rs +++ b/src/library/text/deco.rs @@ -94,12 +94,12 @@ pub fn decorate( pos: Point, width: Length, ) { - let face = fonts.get(text.face_id); - let face_metrics = face.metrics(); + let font = fonts.get(text.font_id); + let font_metrics = font.metrics(); let metrics = match deco.line { - STRIKETHROUGH => face_metrics.strikethrough, - OVERLINE => face_metrics.overline, - UNDERLINE | _ => face_metrics.underline, + STRIKETHROUGH => font_metrics.strikethrough, + OVERLINE => font_metrics.overline, + UNDERLINE | _ => font_metrics.underline, }; let evade = deco.evade && deco.line != STRIKETHROUGH; @@ -141,9 +141,9 @@ pub fn decorate( for glyph in text.glyphs.iter() { let dx = glyph.x_offset.at(text.size) + x; let mut builder = - BezPathBuilder::new(face_metrics.units_per_em, text.size, dx.to_raw()); + BezPathBuilder::new(font_metrics.units_per_em, text.size, dx.to_raw()); - let bbox = face.ttf().outline_glyph(GlyphId(glyph.id), &mut builder); + let bbox = font.ttf().outline_glyph(GlyphId(glyph.id), &mut builder); let path = builder.finish(); x += glyph.x_advance.at(text.size); @@ -151,8 +151,8 @@ pub fn decorate( // Only do the costly segments intersection test if the line // intersects the bounding box. if bbox.map_or(false, |bbox| { - let y_min = -face.to_em(bbox.y_max).at(text.size); - let y_max = -face.to_em(bbox.y_min).at(text.size); + let y_min = -font.to_em(bbox.y_max).at(text.size); + let y_max = -font.to_em(bbox.y_min).at(text.size); offset >= y_min && offset <= y_max }) { diff --git a/src/library/text/mod.rs b/src/library/text/mod.rs index 91eab2fd..9c4f33f1 100644 --- a/src/library/text/mod.rs +++ b/src/library/text/mod.rs @@ -25,7 +25,7 @@ use std::borrow::Cow; use ttf_parser::Tag; use crate::font::{ - Face, FaceMetrics, FontStretch, FontStyle, FontWeight, VerticalFontMetric, + Font, FontMetrics, FontStretch, FontStyle, FontWeight, VerticalFontMetric, }; use crate::library::prelude::*; use crate::util::EcoString; @@ -269,8 +269,8 @@ pub enum TextEdge { } impl TextEdge { - /// Resolve the value of the text edge given a font face. - pub fn resolve(self, styles: StyleChain, metrics: &FaceMetrics) -> Length { + /// Resolve the value of the text edge given a font's metrics. + pub fn resolve(self, styles: StyleChain, metrics: &FontMetrics) -> Length { match self { Self::Metric(metric) => metrics.vertical(metric).resolve(styles), Self::Length(length) => length.resolve(styles), @@ -333,7 +333,7 @@ impl Resolve for Smart<Hyphenate> { } } -/// A stylistic set in a font face. +/// A stylistic set in a font. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct StylisticSet(u8); @@ -514,7 +514,7 @@ impl Show for StrongNode { } } -/// Emphasized text, rendered with an italic face by default. +/// Emphasized text, rendered with an italic font by default. #[derive(Debug, Hash)] pub struct EmphNode(pub Content); diff --git a/src/library/text/shaping.rs b/src/library/text/shaping.rs index 1bac626d..4f7647d4 100644 --- a/src/library/text/shaping.rs +++ b/src/library/text/shaping.rs @@ -4,7 +4,7 @@ use std::str::FromStr; use rustybuzz::{Feature, UnicodeBuffer}; use super::*; -use crate::font::{FaceId, FontStore, FontVariant}; +use crate::font::{FontId, FontStore, FontVariant}; use crate::library::prelude::*; use crate::util::SliceExt; @@ -33,9 +33,9 @@ pub struct ShapedText<'a> { /// A single glyph resulting from shaping. #[derive(Debug, Copy, Clone)] pub struct ShapedGlyph { - /// The font face the glyph is contained in. - pub face_id: FaceId, - /// The glyph's index in the face. + /// The font the glyph is contained in. + pub font_id: FontId, + /// The glyph's index in the font. pub glyph_id: u16, /// The advance width of the glyph. pub x_advance: Em, @@ -94,8 +94,8 @@ impl<'a> ShapedText<'a> { let fill = self.styles.get(TextNode::FILL); let link = self.styles.get(TextNode::LINK); - for ((face_id, y_offset), group) in - self.glyphs.as_ref().group_by_key(|g| (g.face_id, g.y_offset)) + for ((font_id, y_offset), group) in + self.glyphs.as_ref().group_by_key(|g| (g.font_id, g.y_offset)) { let pos = Point::new(offset, top + shift + y_offset.at(self.size)); @@ -116,7 +116,7 @@ impl<'a> ShapedText<'a> { .collect(); let text = Text { - face_id, + font_id, size: self.size, lang, fill, @@ -151,9 +151,9 @@ impl<'a> ShapedText<'a> { let top_edge = self.styles.get(TextNode::TOP_EDGE); let bottom_edge = self.styles.get(TextNode::BOTTOM_EDGE); - // Expand top and bottom by reading the face's vertical metrics. - let mut expand = |face: &Face| { - let metrics = face.metrics(); + // Expand top and bottom by reading the font's vertical metrics. + let mut expand = |font: &Font| { + let metrics = font.metrics(); top.set_max(top_edge.resolve(self.styles, metrics)); bottom.set_max(-bottom_edge.resolve(self.styles, metrics)); }; @@ -162,15 +162,14 @@ impl<'a> ShapedText<'a> { // When there are no glyphs, we just use the vertical metrics of the // first available font. for family in families(self.styles) { - if let Some(face_id) = fonts.select(family, self.variant) { - expand(fonts.get(face_id)); + if let Some(font_id) = fonts.select(family, self.variant) { + expand(fonts.get(font_id)); break; } } } else { - for (face_id, _) in self.glyphs.group_by_key(|g| g.face_id) { - let face = fonts.get(face_id); - expand(face); + for (font_id, _) in self.glyphs.group_by_key(|g| g.font_id) { + expand(fonts.get(font_id)); } } @@ -217,15 +216,15 @@ impl<'a> ShapedText<'a> { /// Push a hyphen to end of the text. pub fn push_hyphen(&mut self, fonts: &mut FontStore) { families(self.styles).find_map(|family| { - let face_id = fonts.select(family, self.variant)?; - let face = fonts.get(face_id); - let ttf = face.ttf(); + let font_id = fonts.select(family, self.variant)?; + let font = fonts.get(font_id); + let ttf = font.ttf(); let glyph_id = ttf.glyph_index('-')?; - let x_advance = face.to_em(ttf.glyph_hor_advance(glyph_id)?); + let x_advance = font.to_em(ttf.glyph_hor_advance(glyph_id)?); let cluster = self.glyphs.last().map(|g| g.cluster).unwrap_or_default(); self.width += x_advance.at(self.size); self.glyphs.to_mut().push(ShapedGlyph { - face_id, + font_id, glyph_id: glyph_id.0, x_advance, x_offset: Em::zero(), @@ -303,7 +302,7 @@ impl Debug for ShapedText<'_> { struct ShapingContext<'a> { fonts: &'a mut FontStore, glyphs: Vec<ShapedGlyph>, - used: Vec<FaceId>, + used: Vec<FontId>, styles: StyleChain<'a>, size: Length, variant: FontVariant, @@ -378,17 +377,17 @@ fn shape_segment<'a>( .filter(|id| !ctx.used.contains(id)); } - // Extract the face id or shape notdef glyphs if we couldn't find any face. - let face_id = if let Some(id) = selection { + // Extract the font id or shape notdef glyphs if we couldn't find any font. + let font_id = if let Some(id) = selection { id } else { - if let Some(&face_id) = ctx.used.first() { - shape_tofus(ctx, base, text, face_id); + if let Some(&font_id) = ctx.used.first() { + shape_tofus(ctx, base, text, font_id); } return; }; - ctx.used.push(face_id); + ctx.used.push(font_id); // Fill the buffer with our text. let mut buffer = UnicodeBuffer::new(); @@ -401,8 +400,8 @@ fn shape_segment<'a>( }); // Shape! - let mut face = ctx.fonts.get(face_id); - let buffer = rustybuzz::shape(face.ttf(), &ctx.tags, buffer); + let mut font = ctx.fonts.get(font_id); + let buffer = rustybuzz::shape(font.ttf(), &ctx.tags, buffer); let infos = buffer.glyph_infos(); let pos = buffer.glyph_positions(); @@ -417,11 +416,11 @@ fn shape_segment<'a>( // Add the glyph to the shaped output. // TODO: Don't ignore y_advance. ctx.glyphs.push(ShapedGlyph { - face_id, + font_id, glyph_id: info.glyph_id as u16, - x_advance: face.to_em(pos[i].x_advance), - x_offset: face.to_em(pos[i].x_offset), - y_offset: face.to_em(pos[i].y_offset), + x_advance: font.to_em(pos[i].x_advance), + x_offset: font.to_em(pos[i].x_offset), + y_offset: font.to_em(pos[i].y_offset), cluster: base + cluster, safe_to_break: !info.unsafe_to_break(), c: text[cluster ..].chars().next().unwrap(), @@ -473,7 +472,7 @@ fn shape_segment<'a>( // Recursively shape the tofu sequence with the next family. shape_segment(ctx, base + range.start, &text[range], families.clone()); - face = ctx.fonts.get(face_id); + font = ctx.fonts.get(font_id); } i += 1; @@ -482,13 +481,13 @@ fn shape_segment<'a>( ctx.used.pop(); } -/// Shape the text with tofus from the given face. -fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, face_id: FaceId) { - let face = ctx.fonts.get(face_id); - let x_advance = face.advance(0).unwrap_or_default(); +/// Shape the text with tofus from the given font. +fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, font_id: FontId) { + let font = ctx.fonts.get(font_id); + let x_advance = font.advance(0).unwrap_or_default(); for (cluster, c) in text.char_indices() { ctx.glyphs.push(ShapedGlyph { - face_id, + font_id, glyph_id: 0, x_advance, x_offset: Em::zero(), @@ -512,8 +511,8 @@ fn track_and_space(ctx: &mut ShapingContext) { while let Some(glyph) = glyphs.next() { // Make non-breaking space same width as normal space. if glyph.c == '\u{00A0}' { - let face = ctx.fonts.get(glyph.face_id); - glyph.x_advance -= nbsp_delta(face).unwrap_or_default(); + let font = ctx.fonts.get(glyph.font_id); + glyph.x_advance -= nbsp_delta(font).unwrap_or_default(); } if glyph.is_space() { @@ -527,10 +526,10 @@ fn track_and_space(ctx: &mut ShapingContext) { } /// Difference between non-breaking and normal space. -fn nbsp_delta(face: &Face) -> Option<Em> { - let space = face.ttf().glyph_index(' ')?.0; - let nbsp = face.ttf().glyph_index('\u{00A0}')?.0; - Some(face.advance(nbsp)? - face.advance(space)?) +fn nbsp_delta(font: &Font) -> Option<Em> { + let space = font.ttf().glyph_index(' ')?.0; + let nbsp = font.ttf().glyph_index('\u{00A0}')?.0; + Some(font.advance(nbsp)? - font.advance(space)?) } /// Resolve the font variant with `BOLD` and `ITALIC` factored in. diff --git a/src/library/text/shift.rs b/src/library/text/shift.rs index 744479f2..fde969d3 100644 --- a/src/library/text/shift.rs +++ b/src/library/text/shift.rs @@ -94,8 +94,8 @@ fn search_text(content: &Content, mode: ScriptKind) -> Option<EcoString> { /// given string. fn is_shapable(fonts: &mut FontStore, text: &str, styles: StyleChain) -> bool { for family in styles.get(TextNode::FAMILY).iter() { - if let Some(face_id) = fonts.select(family.as_str(), variant(styles)) { - let ttf = fonts.get(face_id).ttf(); + if let Some(font_id) = fonts.select(family.as_str(), variant(styles)) { + let ttf = fonts.get(font_id).ttf(); return text.chars().all(|c| ttf.glyph_index(c).is_some()); } } diff --git a/src/library/utility/mod.rs b/src/library/utility/mod.rs index 9c95e60c..bc7c6f25 100644 --- a/src/library/utility/mod.rs +++ b/src/library/utility/mod.rs @@ -12,7 +12,7 @@ pub use string::*; use crate::eval::{Eval, Machine, Scopes}; use crate::library::prelude::*; -use crate::source::SourceFile; +use crate::source::Source; /// The name of a value's type. pub fn type_(_: &mut Machine, args: &mut Args) -> TypResult<Value> { @@ -33,7 +33,7 @@ pub fn eval(vm: &mut Machine, args: &mut Args) -> TypResult<Value> { let Spanned { v: src, span } = args.expect::<Spanned<String>>("source")?; // Parse the source and set a synthetic span for all nodes. - let source = SourceFile::synthesized(src, span); + let source = Source::synthesized(src, span); let ast = source.ast()?; // Evaluate the source. |
