diff options
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/frame.rs | 18 | ||||
| -rw-r--r-- | src/layout/shaping.rs | 22 |
2 files changed, 15 insertions, 25 deletions
diff --git a/src/layout/frame.rs b/src/layout/frame.rs index 0c307dd4..e52e2751 100644 --- a/src/layout/frame.rs +++ b/src/layout/frame.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use super::{Constrained, Constraints}; use crate::color::Color; use crate::font::FaceId; -use crate::geom::{Length, Path, Point, Size}; +use crate::geom::{Em, Length, Path, Point, Size}; use crate::image::ImageId; /// A finished layout with elements at fixed positions. @@ -130,27 +130,15 @@ pub struct Text { pub glyphs: Vec<Glyph>, } -impl Text { - /// Encode the glyph ids into a big-endian byte buffer. - pub fn encode_glyphs_be(&self) -> Vec<u8> { - let mut bytes = Vec::with_capacity(2 * self.glyphs.len()); - for glyph in &self.glyphs { - bytes.push((glyph.id >> 8) as u8); - bytes.push((glyph.id & 0xff) as u8); - } - bytes - } -} - /// A glyph in a run of shaped text. #[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct Glyph { /// The glyph's index in the face. pub id: u16, /// The advance width of the glyph. - pub x_advance: Length, + pub x_advance: Em, /// The horizontal offset of the glyph. - pub x_offset: Length, + pub x_offset: Em, } /// A geometric shape. diff --git a/src/layout/shaping.rs b/src/layout/shaping.rs index fad7f234..efee1591 100644 --- a/src/layout/shaping.rs +++ b/src/layout/shaping.rs @@ -6,7 +6,7 @@ use rustybuzz::UnicodeBuffer; use super::{Element, Frame, Glyph, LayoutContext, Text}; use crate::eval::{FontState, LineState}; use crate::font::{Face, FaceId, FontVariant, LineMetrics}; -use crate::geom::{Dir, Length, Point, Size}; +use crate::geom::{Dir, Em, Length, Point, Size}; use crate::layout::Geometry; use crate::util::SliceExt; @@ -72,9 +72,9 @@ pub struct ShapedGlyph { /// The glyph's index in the face. pub glyph_id: u16, /// The advance width of the glyph. - pub x_advance: Length, + pub x_advance: Em, /// The horizontal offset of the glyph. - pub x_offset: Length, + pub x_offset: Em, /// The start index of the glyph in the source text. pub text_index: usize, /// Whether splitting the shaping result before this glyph would yield the @@ -106,7 +106,7 @@ impl<'a> ShapedText<'a> { x_advance: glyph.x_advance, x_offset: glyph.x_offset, }); - width += glyph.x_advance; + width += glyph.x_advance.to_length(text.size); } frame.push(pos, Element::Text(text)); @@ -267,8 +267,8 @@ fn shape_segment<'a>( glyphs.push(ShapedGlyph { face_id, glyph_id: info.glyph_id as u16, - x_advance: face.to_em(pos[i].x_advance).to_length(size), - x_offset: face.to_em(pos[i].x_offset).to_length(size), + x_advance: face.to_em(pos[i].x_advance), + x_offset: face.to_em(pos[i].x_offset), text_index: base + cluster, safe_to_break: !info.unsafe_to_break(), }); @@ -342,7 +342,9 @@ fn measure( let mut width = Length::zero(); let mut top = Length::zero(); let mut bottom = Length::zero(); - let mut expand_vertical = |face: &Face| { + + // Expand top and bottom by reading the face's vertical metrics. + let mut expand = |face: &Face| { top.set_max(face.vertical_metric(state.top_edge).to_length(state.size)); bottom.set_max(-face.vertical_metric(state.bottom_edge).to_length(state.size)); }; @@ -352,17 +354,17 @@ fn measure( // first available font. for family in state.families() { if let Some(face_id) = ctx.fonts.select(family, state.variant) { - expand_vertical(ctx.fonts.get(face_id)); + expand(ctx.fonts.get(face_id)); break; } } } else { for (face_id, group) in glyphs.group_by_key(|g| g.face_id) { let face = ctx.fonts.get(face_id); - expand_vertical(face); + expand(face); for glyph in group { - width += glyph.x_advance; + width += glyph.x_advance.to_length(state.size); } } } |
