Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Vertical alignment #293

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
50 changes: 25 additions & 25 deletions examples/swash_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn main() {
let bg_color = Rgba([255, 255, 255, 255]);

// Padding around the output image
let padding = 20;
let padding = (20f32 * display_scale).round() as u32;

// Create a FontContext, LayoutContext and ScaleContext
//
Expand Down Expand Up @@ -92,21 +92,21 @@ fn main() {

builder.push_text(&text[5..40]);

builder.push_inline_box(InlineBox {
id: 0,
index: 0,
width: 50.0,
height: 50.0,
});
builder.push_inline_box(InlineBox::new(
0,
0,
50.0 * display_scale,
50.0 * display_scale,
));

builder.push_text(&text[40..50]);

builder.push_inline_box(InlineBox {
id: 1,
index: 50,
width: 50.0,
height: 30.0,
});
builder.push_inline_box(InlineBox::new(
1,
50,
50.0 * display_scale,
30.0 * display_scale,
));

builder.push_text(&text[50..141]);

Expand Down Expand Up @@ -147,18 +147,18 @@ fn main() {
builder.push(underline_style, 141..150);
builder.push(strikethrough_style, 155..168);

builder.push_inline_box(InlineBox {
id: 0,
index: 40,
width: 50.0,
height: 50.0,
});
builder.push_inline_box(InlineBox {
id: 1,
index: 50,
width: 50.0,
height: 30.0,
});
builder.push_inline_box(InlineBox::new(
0,
40,
50.0 * display_scale,
50.0 * display_scale,
));
builder.push_inline_box(InlineBox::new(
1,
50,
50.0 * display_scale,
30.0 * display_scale,
));

// Build the builder into a Layout
// let mut layout: Layout<ColorBrush> = builder.build(&text);
Expand Down
7 changes: 1 addition & 6 deletions examples/tiny_skia_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ fn main() {
builder.push(StyleProperty::Underline(true), 141..150);
builder.push(StyleProperty::Strikethrough(true), 155..168);

builder.push_inline_box(InlineBox {
id: 0,
index: 40,
width: 50.0,
height: 50.0,
});
builder.push_inline_box(InlineBox::new(0, 40, 50.0, 50.0));

// Build the builder into a Layout
let mut layout: Layout<ColorBrush> = builder.build(&text);
Expand Down
19 changes: 19 additions & 0 deletions parley/src/inline_box.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::{ResolvedBaselineShift, VerticalAlign};

/// A box to be laid out inline with text
#[derive(Debug, Clone)]
pub struct InlineBox {
Expand All @@ -14,4 +16,21 @@ pub struct InlineBox {
pub width: f32,
/// The height of the box in pixels
pub height: f32,
/// The baseline along which this item is aligned.
pub vertical_align: VerticalAlign,
/// Additional baseline alignment applied afterwards.
pub baseline_shift: ResolvedBaselineShift,
}

impl InlineBox {
pub fn new(id: u64, index: usize, width: f32, height: f32) -> Self {
Self {
id,
index,
width,
height,
vertical_align: Default::default(),
baseline_shift: Default::default(),
}
}
}
36 changes: 36 additions & 0 deletions parley/src/layout/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ pub(crate) struct RunData {
pub(crate) advance: f32,
}

impl RunData {
pub(crate) fn unique_styles<'a, 'b, B: Brush>(
&'a self,
layout: &'b LayoutData<B>,
mut cb: impl FnMut(&'b Style<B>),
) {
let glyph_start = self.glyph_start;
for cluster in &layout.clusters[self.cluster_range.clone()] {
if cluster.glyph_len != 0xFF && cluster.has_divergent_styles() {
let mut start = glyph_start + cluster.glyph_offset as usize;
let end = start + cluster.glyph_len as usize;

'runs: loop {
let start_glyph = &layout.glyphs[start];
cb(&layout.styles[start_glyph.style_index()]);

loop {
start += 1;

if start >= end {
break 'runs;
}
if layout.glyphs[start].style_index != start_glyph.style_index {
break;
}
}
}
} else {
cb(&layout.styles[cluster.style_index as usize]);
}
}
}
}

#[derive(Copy, Clone, Default, PartialEq, Debug)]
pub enum BreakReason {
#[default]
Expand Down Expand Up @@ -335,6 +369,8 @@ impl<B: Brush> LayoutData<B> {
ascent: metrics.ascent,
descent: metrics.descent,
leading: metrics.leading,
x_height: metrics.x_height,
font_size,
underline_offset: metrics.underline_offset,
underline_size: metrics.stroke_size,
strikethrough_offset: metrics.strikeout_offset,
Expand Down
Loading
Loading