Skip to content

Commit

Permalink
Update examples for inline boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 20, 2024
1 parent ac59681 commit 9976cf1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
37 changes: 32 additions & 5 deletions examples/swash_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use image::codecs::png::PngEncoder;
use image::{self, Pixel, Rgba, RgbaImage};
use parley::layout::{Alignment, Glyph, GlyphRun, Layout};
use parley::layout::{Alignment, Glyph, GlyphRun, Layout, PositionedLayoutItem};
use parley::style::{FontStack, FontWeight, StyleProperty};
use parley::{FontContext, LayoutContext};
use parley::{FontContext, InlineBox, LayoutContext};
use peniko::Color;
use std::fs::File;
use swash::scale::image::Content;
Expand Down Expand Up @@ -63,11 +63,25 @@ fn main() {
let bold_style = StyleProperty::FontWeight(bold);
builder.push(&bold_style, 0..4);

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

// Build the builder into a Layout
let mut layout: Layout<Color> = builder.build();

// Perform layout (including bidi resolution and shaping) with start alignment
layout.break_all_lines(max_advance, Alignment::Start);
layout.break_all_lines(max_advance);
layout.align(max_advance, Alignment::Start);

// Create image to render into
let width = layout.width().ceil() as u32 + (padding * 2);
Expand All @@ -77,8 +91,21 @@ fn main() {
// Iterate over laid out lines
for line in layout.lines() {
// Iterate over GlyphRun's within each line
for glyph_run in line.glyph_runs() {
render_glyph_run(&mut scale_cx, &glyph_run, &mut img, padding);
for item in line.items() {
match item {
PositionedLayoutItem::GlyphRun(glyph_run) => {
render_glyph_run(&mut scale_cx, &glyph_run, &mut img, padding);
}
PositionedLayoutItem::InlineBox(inline_box) => {
for x_off in 0..(inline_box.width.floor() as u32) {
for y_off in 0..(inline_box.height.floor() as u32) {
let x = inline_box.x as u32 + x_off + padding;
let y = inline_box.y as u32 + y_off + padding;
img.put_pixel(x, y, Rgba([0, 0, 0, 255]));
}
}
}
};
}
}

Expand Down
35 changes: 29 additions & 6 deletions examples/tiny_skia_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
//! Note: Emoji rendering is not currently implemented in this example. See the swash example
//! if you need emoji rendering.

use parley::layout::{Alignment, GlyphRun, Layout};
use parley::layout::{Alignment, GlyphRun, Layout, PositionedLayoutItem};
use parley::style::{FontStack, FontWeight, StyleProperty};
use parley::{FontContext, LayoutContext};
use parley::{FontContext, InlineBox, LayoutContext};
use peniko::Color as PenikoColor;
use skrifa::instance::{LocationRef, NormalizedCoord, Size};
use skrifa::outline::{DrawSettings, OutlinePen};
use skrifa::raw::FontRef as ReadFontsRef;
use skrifa::{GlyphId, MetadataProvider, OutlineGlyph};
use tiny_skia::{
Color as TinySkiaColor, FillRule, Paint, PathBuilder, Pixmap, PixmapMut, Transform,
Color as TinySkiaColor, FillRule, Paint, PathBuilder, Pixmap, PixmapMut, Rect, Transform,
};

fn main() {
Expand Down Expand Up @@ -64,11 +64,19 @@ fn main() {
let bold_style = StyleProperty::FontWeight(bold);
builder.push(&bold_style, 0..4);

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

// Build the builder into a Layout
let mut layout: Layout<PenikoColor> = builder.build();

// Perform layout (including bidi resolution and shaping) with start alignment
layout.break_all_lines(max_advance, Alignment::Start);
layout.break_all_lines(max_advance);
layout.align(max_advance, Alignment::Start);
let width = layout.width().ceil() as u32;
let height = layout.height().ceil() as u32;
let padded_width = width + padding * 2;
Expand All @@ -85,8 +93,17 @@ fn main() {

// Render each glyph run
for line in layout.lines() {
for glyph_run in line.glyph_runs() {
render_glyph_run(&glyph_run, &mut pen, padding);
for item in line.items() {
match item {
PositionedLayoutItem::GlyphRun(glyph_run) => {
render_glyph_run(&glyph_run, &mut pen, padding);
}
PositionedLayoutItem::InlineBox(inline_box) => {
pen.set_origin(inline_box.x + padding as f32, inline_box.y + padding as f32);
pen.set_color(to_tiny_skia(foreground_color));
pen.fill_rect(inline_box.width, inline_box.height);
}
};
}
}

Expand Down Expand Up @@ -177,6 +194,12 @@ impl TinySkiaPen<'_> {
self.paint.set_color(color);
}

fn fill_rect(&mut self, width: f32, height: f32) {
let rect = Rect::from_xywh(self.x, self.y, width, height).unwrap();
self.pixmap
.fill_rect(rect, &self.paint, Transform::identity(), None);
}

fn draw_glyph(
&mut self,
glyph: &OutlineGlyph<'_>,
Expand Down

0 comments on commit 9976cf1

Please sign in to comment.