Expand description
Parley is a library for rich text layout.
Some key types are:
-
FontContextandLayoutContextare resources which should be shared globally (or at coarse-grained boundaries).FontContextis database of fonts.LayoutContextis scratch space that allows for reuse of allocations between layouts.
-
RangedBuilderandTreeBuilderwhich are builders for creating aLayout.RangedBuilderallows styles to be specified as a flatVecof spansTreeBuilderallows styles to be specified as a tree of spans
They are constructed using the
ranged_builderandtree_buildermethods onLayoutContext. -
Layoutwhich represents styled paragraph(s) of text and can perform shaping, line-breaking, bidi-reordering, and alignment of that text.Layoutsupports re-linebreaking and re-aligning many times (in case the width at which wrapping should occur changes). But if the text content or the styles applied to that content change then a newLayoutmust be created using a newRangedBuilderorTreeBuilder.
§Usage Example
See the examples directory for more complete usage examples that include rendering.
use parley::{
Alignment, AlignmentOptions, FontContext, FontWeight, InlineBox, Layout, LayoutContext,
LineHeight, PositionedLayoutItem, StyleProperty,
};
// Create a FontContext (font database) and LayoutContext (scratch space).
// These are both intended to be constructed rarely (perhaps even once per app):
let mut font_cx = FontContext::new();
let mut layout_cx = LayoutContext::new();
// Create a `RangedBuilder` or a `TreeBuilder`, which are used to construct a `Layout`.
const DISPLAY_SCALE : f32 = 1.0;
const TEXT : &str = "Lorem Ipsum...";
let mut builder = layout_cx.ranged_builder(&mut font_cx, &TEXT, DISPLAY_SCALE, true);
// Set default styles that apply to the entire layout
builder.push_default(StyleProperty::FontSize(16.0));
// Set a style that applies to the first 4 characters
builder.push(StyleProperty::FontWeight(FontWeight::new(600.0)), 0..4);
// Add a box to be laid out inline with the text
builder.push_inline_box(InlineBox { id: 0, index: 5, width: 50.0, height: 50.0 });
// Build the builder into a Layout
let mut layout: Layout<()> = builder.build(&TEXT);
// Run line-breaking and alignment on the Layout
const MAX_WIDTH : Option<f32> = Some(100.0);
layout.break_all_lines(MAX_WIDTH);
layout.align(MAX_WIDTH, Alignment::Start, AlignmentOptions::default());
// Inspect computed layout (see examples for more details)
let width = layout.width();
let height = layout.height();
for line in layout.lines() {
for item in line.items() {
match item {
PositionedLayoutItem::GlyphRun(glyph_run) => {
// Render the glyph run
}
PositionedLayoutItem::InlineBox(inline_box) => {
// Render the inline box
}
};
}
}Re-exports§
Modules§
Structs§
- Bounding
Box - A bounding box.
- Font
Context - A font database/cache (wrapper around a Fontique
CollectionandSourceCache). - Font
Data - Owned shareable font resource.
- Inline
Box - A box to be laid out inline with text
- Layout
- Text layout.
- Layout
Context - Shared scratch space used when constructing text layouts.
- Ranged
Builder - Builder for constructing a text layout with ranged attributes.
- Tree
Builder - Builder for constructing a text layout with a tree of attributes.
Type Aliases§
- Font
Deprecated