Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::time::Instant;
use parley::fontique::Blob;
use parley::{
Alignment, AlignmentOptions, FontContext, FontFamily, FontWeight, GenericFamily, InlineBox,
InlineBoxKind, Layout, LayoutContext, LineHeight, StyleProperty,
InlineBoxKind, InlineBoxVerticalAlign, Layout, LayoutContext, LineHeight, StyleProperty,
};
use peniko::Color;

Expand Down Expand Up @@ -207,6 +207,7 @@ pub fn build_rich_layout(
width: 50.0,
height: 50.0,
baseline: None,
vertical_align: InlineBoxVerticalAlign::default(),
});

let mut layout = builder.build(&config.text);
Expand Down
9 changes: 8 additions & 1 deletion examples/swash_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use image::codecs::png::PngEncoder;
use image::{self, Pixel, Rgba, RgbaImage};
use parley::layout::{Alignment, Glyph, GlyphRun, Layout, PositionedLayoutItem};
use parley::style::{FontFamily, FontWeight, StyleProperty, TextStyle};
use parley::{AlignmentOptions, FontContext, InlineBox, InlineBoxKind, LayoutContext, LineHeight};
use parley::{
AlignmentOptions, FontContext, InlineBox, InlineBoxKind, InlineBoxVerticalAlign, LayoutContext,
LineHeight,
};
use std::fs::File;
use swash::FontRef;
use swash::scale::image::Content;
Expand Down Expand Up @@ -99,6 +102,7 @@ fn main() {
width: 50.0,
height: 50.0,
baseline: None,
vertical_align: InlineBoxVerticalAlign::default(),
});

builder.push_text(&text[40..50]);
Expand All @@ -110,6 +114,7 @@ fn main() {
width: 50.0,
height: 30.0,
baseline: None,
vertical_align: InlineBoxVerticalAlign::default(),
});

builder.push_text(&text[50..141]);
Expand Down Expand Up @@ -161,6 +166,7 @@ fn main() {
width: 50.0,
height: 50.0,
baseline: None,
vertical_align: InlineBoxVerticalAlign::default(),
});
builder.push_inline_box(InlineBox {
id: 1,
Expand All @@ -169,6 +175,7 @@ fn main() {
width: 50.0,
height: 30.0,
baseline: None,
vertical_align: InlineBoxVerticalAlign::default(),
});

// Build the builder into a Layout
Expand Down
4 changes: 3 additions & 1 deletion examples/tiny_skia_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

use parley::{
Alignment, AlignmentOptions, FontContext, FontWeight, GenericFamily, GlyphRun, InlineBox,
InlineBoxKind, Layout, LayoutContext, LineHeight, PositionedLayoutItem, StyleProperty,
InlineBoxKind, InlineBoxVerticalAlign, Layout, LayoutContext, LineHeight, PositionedLayoutItem,
StyleProperty,
};
use skrifa::{
GlyphId, MetadataProvider, OutlineGlyph,
Expand Down Expand Up @@ -93,6 +94,7 @@ fn main() {
width: 50.0,
height: 50.0,
baseline: None,
vertical_align: InlineBoxVerticalAlign::default(),
});

// Build the builder into a Layout
Expand Down
20 changes: 20 additions & 0 deletions parley/src/inline_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ pub struct InlineBox {
///
/// If `None`, the baseline is the box's bottom edge.
pub baseline: Option<f32>,
/// How the box is aligned within its line box.
pub vertical_align: InlineBoxVerticalAlign,
}

/// How an [`InlineBox`] is aligned within its line box.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Default)]
pub enum InlineBoxVerticalAlign {
/// The box's baseline is aligned with the line's baseline.
///
/// This corresponds to `vertical-align: baseline` in CSS.
#[default]
Baseline,
/// The top of the box is aligned with the top of the line box.
///
/// This corresponds to `vertical-align: top` in CSS.
Top,
/// The bottom of the box is aligned with the bottom of the line box.
///
/// This corresponds to `vertical-align: bottom` in CSS.
Bottom,
}

/// Whether a box is in-flow (takes up space in the layout) or out-of-flow (e.g. absolutely positioned)
Expand Down
15 changes: 12 additions & 3 deletions parley/src/layout/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::layout::data::{LayoutItemKind, LineData};
use crate::layout::layout::Layout;
use crate::layout::run::Run;
use crate::style::Brush;
use crate::{InlineBox, InlineBoxKind};
use crate::{InlineBox, InlineBoxKind, InlineBoxVerticalAlign};

use core::ops::Range;
use parley_engine::Glyph;
Expand Down Expand Up @@ -321,10 +321,19 @@ impl<'a, B: Brush> Iterator for GlyphRunIter<'a, B> {
if inline_box.kind == InlineBoxKind::InFlow {
self.offset += inline_box.width;
}
let y = match inline_box.vertical_align {
InlineBoxVerticalAlign::Baseline => {
self.line.data.metrics.baseline
- inline_box.baseline.unwrap_or(inline_box.height)
}
InlineBoxVerticalAlign::Top => self.line.data.metrics.block_min_coord,
InlineBoxVerticalAlign::Bottom => {
self.line.data.metrics.block_max_coord - inline_box.height
}
};
return Some(PositionedLayoutItem::InlineBox(PositionedInlineBox {
x,
y: self.line.data.metrics.baseline
- inline_box.baseline.unwrap_or(inline_box.height),
y,
width: inline_box.width,
height: inline_box.height,
baseline: inline_box.baseline,
Expand Down
Loading