Skip to content

Commit

Permalink
Tree builder example
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 20, 2024
1 parent b460bf7 commit afa76cd
Showing 1 changed file with 84 additions and 31 deletions.
115 changes: 84 additions & 31 deletions examples/swash_render/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use image::codecs::png::PngEncoder;
use image::{self, Pixel, Rgba, RgbaImage};
use parley::layout::{Alignment, Glyph, GlyphRun, Layout, PositionedLayoutItem};
use parley::style::{FontStack, FontWeight, StyleProperty};
use parley::style::{FontStack, FontWeight, StyleProperty, TextStyle};
use parley::{FontContext, InlineBox, LayoutContext};
use peniko::Color;
use std::fs::File;
Expand Down Expand Up @@ -44,40 +44,93 @@ fn main() {
let mut layout_cx = LayoutContext::new();
let mut scale_cx = ScaleContext::new();

// Create a RangedBuilder
let mut builder = layout_cx.ranged_builder(&mut font_cx, &text, display_scale);

// Set default text colour styles (set foreground text color)
// Setup some Parley text styles
let brush_style = StyleProperty::Brush(text_color);
builder.push_default(&brush_style);

// Set default font family
let font_stack = FontStack::Source("system-ui");
let font_stack_style = StyleProperty::FontStack(font_stack);
builder.push_default(&font_stack_style);
builder.push_default(&StyleProperty::LineHeight(1.3));
builder.push_default(&StyleProperty::FontSize(16.0));

// Set the first 4 characters to bold
let font_stack_style: StyleProperty<Color> = StyleProperty::FontStack(font_stack);
let bold = FontWeight::new(600.0);
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();

let mut layout = if std::env::args().any(|arg| arg == "--tree") {
// TREE BUILDER
// ============

// TODO: cleanup API

let root_style = TextStyle {
brush: text_color,
font_stack,
line_height: 1.3,
font_size: 16.0,
..Default::default()
};

let mut builder = layout_cx.tree_builder(&mut font_cx, display_scale, &root_style);

builder.push_style_modification_span(&[bold_style]);
builder.push_text(&text[0..5]);
builder.pop_style_span();

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

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

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

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

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

// Build the builder into a Layout
// let mut layout: Layout<Color> = builder.build(&text);
let (layout, _text): (Layout<Color>, String) = builder.build();
layout
} else {
// RANGE BUILDER
// ============

// Creatse a RangedBuilder
let mut builder = layout_cx.ranged_builder(&mut font_cx, &text, display_scale);

// Set default text colour styles (set foreground text color)
builder.push_default(&brush_style);

// Set default font family
builder.push_default(&font_stack_style);
builder.push_default(&StyleProperty::LineHeight(1.3));
builder.push_default(&StyleProperty::FontSize(16.0));

// Set the first 4 characters to 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(&text);
let layout: Layout<Color> = builder.build(&text);
layout
};

// Perform layout (including bidi resolution and shaping) with start alignment
layout.break_all_lines(max_advance);
Expand Down

0 comments on commit afa76cd

Please sign in to comment.