Bump to 0.2.0: SW/GLES paint parity, shared font resolution, FrameState refactor, docs and packaging fixes
Rendering parity (software ↔ GLES). The software backend now rounds glyph pen positions and image destinations to the nearest integer pixel, matching what the GLES backend already did; previously it truncated, so text and 1:1 images could land up to half a pixel off between the two backends and the bilinear sample read ~1 px softer than the source. Gradients and shadows are deliberately left unimplemented on the software backend, and the GLES multi-rect `glScissor` clip is left coarse on purpose: making it exact would need stencil bits the EGL config does not carry, or routing the partial-redraw path through the offscreen clip layer, which would break its `fill` / `clear_rects_transparent` scissor semantics. Adds software-backend pixel tests covering the snapping. Font resolution unification. The system-font candidate chain, `find_font_opt` and `load_default_font_bytes` lived in two copies (`render/helpers` and `gles_render/helpers`) that had already diverged — one resolved through `find_font_opt`, the other inlined the candidate loop — and now live once in `system_fonts`. The two per-backend `OnceLock` default-font caches and `primary_handle` collapse into a single `system_fonts::default_handle`. Module docs and the `font_registry` caller are updated accordingly. Shared image validation and rect inflation. `draw_image_data`'s dimension check and its one-line warning were byte-duplicated across both backends and are now `render::helpers::validate_rgba_dims`. The six manual symmetric `Rect`-inflate literals in the GLES primitives reuse the existing `Rect::expand`. FrameState and DrawCtx de-duplication. The eleven `SurfaceState` fields the draw pass owns and threads through `DrawCtx` — `widget_rects`, the cursor / selection maps, the scroll state, `accessible_extras`, `prev_focused` / `prev_hovered` / `prev_pressed` — move into a `FrameState` sub-struct. The per-frame `build_draw_ctx` / `commit_draw_ctx` helpers can then borrow `&mut ss.frame`, disjoint from `ss.canvas` and `ss.pool`, so the four frame paths (software / GLES × full / partial) replace their duplicated `DrawCtx` construction and write-back with a single helper call each. A whole-`SurfaceState` borrow could not express this (partial borrows do not cross function boundaries), which is why the helpers take the sub-struct. `content_dirty` stays on `SurfaceState` — it is an invalidation flag, not frame state — and is reset at the call site. rich_text tests. Adds the previously-missing `tests.rs` for the `RichText` widget: one hit rect per visual line a link spans (the widget's core invariant), the single-line and no-link cases, preferred-size growth with hard line breaks, and `map_msg` range preservation — all headless against a software `Canvas`, with line counts forced by `\n` so they do not depend on any system font's measured width. Documentation. Fills the rustdoc gaps on the embedder-facing surface: `core::UiSurface` accessors, the `egl_context` public API, the `GlesCanvas` methods, `theme::typography` and `theme::error`, and the `RichText` / `Text` builders; adds a crate-level "Rendering backends" overview. `CHANGELOG.md` is added (0.2.0 / 0.1.0), and `docs/widgets.md` / `docs/cookbook.md` gain `rich_text`, `external`, and the CPU-draw / path-clip / externally-laid-out-tree recipes. `debian/changelog` gets the 0.2.0-1 entry. Private intra-doc links to `system_fonts` are demoted to code spans so `cargo doc` is warning-free. Packaging. The `libltk-dev` registry crate shipped a `Cargo.toml` declaring the `lookup` bench while the `Makefile` install copied only `src/`, so Cargo refused to parse the manifest over a missing `benches/lookup.rs`; the install now ships `benches/` as well (the file alone satisfies the parse — criterion is a dev-dependency and is not resolved when the crate is consumed as a library). `Cargo.toml` is bumped to 0.2.0 to match the package version and the `ltk-0.2.0` registry directory.
This commit is contained in:
@@ -23,6 +23,8 @@ reference, see [`docs/widgets.md`](./widgets.md). For theme JSON, see
|
||||
- [Tab navigation between widgets](#tab-navigation-between-widgets)
|
||||
- [Multi-screen app via sub-state pattern](#multi-screen-app-via-sub-state-pattern)
|
||||
- [Embedding ltk without `ltk::run`](#embedding-ltk-without-ltkrun)
|
||||
- [Custom CPU drawing and path clipping](#custom-cpu-drawing-and-path-clipping)
|
||||
- [Projecting an externally-laid-out view tree](#projecting-an-externally-laid-out-view-tree)
|
||||
|
||||
---
|
||||
|
||||
@@ -823,3 +825,97 @@ interaction state changed.
|
||||
**See also**: [`tests/core_surface.rs`](../tests/core_surface.rs) for
|
||||
the full set of supported operations,
|
||||
[`docs/onboarding.md`](./onboarding.md#when-to-use-coreuisurface).
|
||||
|
||||
---
|
||||
|
||||
## Custom CPU drawing and path clipping
|
||||
|
||||
Painting something the widget set does not cover — a gauge, a
|
||||
`VectorDrawable`, a Lottie frame, a shaped avatar — straight onto the
|
||||
canvas, identically on the GLES and software backends. `External::cpu`
|
||||
gives you a closure invoked once per frame with the `Canvas` and the
|
||||
widget's laid-out rect; `set_clip_path` then clips arbitrary draws to a
|
||||
vector path with an anti-aliased edge.
|
||||
|
||||
```rust,no_run
|
||||
# use ltk::{ External, Element, Color, PathCmd };
|
||||
# #[ derive( Clone ) ] enum Msg {}
|
||||
# fn _ex() -> Element<Msg> {
|
||||
External::cpu( 96.0, 96.0, |canvas, rect|
|
||||
{
|
||||
// Background, unclipped.
|
||||
canvas.fill_rect( rect, Color::rgb( 0.10, 0.10, 0.12 ), 0.0 );
|
||||
|
||||
// Snapshot the outer clip so the path clip does not leak into the
|
||||
// rest of the frame, then clip the foreground to a circle.
|
||||
let saved = canvas.clip_bounds();
|
||||
let r = rect.width.min( rect.height ) / 2.0;
|
||||
let ( cx, cy ) = ( rect.x + rect.width / 2.0, rect.y + rect.height / 2.0 );
|
||||
let k = r * 0.5523;
|
||||
canvas.set_clip_path( &[
|
||||
PathCmd::MoveTo( cx, cy - r ),
|
||||
PathCmd::CubicTo( cx + k, cy - r, cx + r, cy - k, cx + r, cy ),
|
||||
PathCmd::CubicTo( cx + r, cy + k, cx + k, cy + r, cx, cy + r ),
|
||||
PathCmd::CubicTo( cx - k, cy + r, cx - r, cy + k, cx - r, cy ),
|
||||
PathCmd::CubicTo( cx - r, cy - k, cx - k, cy - r, cx, cy - r ),
|
||||
PathCmd::Close,
|
||||
] );
|
||||
canvas.fill_rect( rect, Color::rgb( 0.20, 0.50, 0.95 ), 0.0 );
|
||||
canvas.set_clip_rects( &saved );
|
||||
} ).into()
|
||||
# }
|
||||
```
|
||||
|
||||
The path clip is bracketed: `set_clip_path` installs it and
|
||||
`set_clip_rects( &saved )` flushes it (on GLES this is when the offscreen
|
||||
layer is composited). `clip_bounds` snapshots the prior clip first so an
|
||||
outer clip is restored rather than dropped. `fill_path` / `stroke_path`
|
||||
take the same `PathCmd` list to paint a path instead of clipping to it.
|
||||
|
||||
**See also**: [`examples/clip_path.rs`](../examples/clip_path.rs) (rounded
|
||||
rect, circle, triangle — same smooth result on both backends).
|
||||
|
||||
---
|
||||
|
||||
## Projecting an externally-laid-out view tree
|
||||
|
||||
Hosting a widget tree whose geometry is computed elsewhere — an Android
|
||||
measure/layout pass, say, which yields one absolute rect per view —
|
||||
projected onto an ltk `stack` in paint order. `measure_text` gives the
|
||||
external layout the same metrics the renderer will use, without a live
|
||||
`Canvas`; `push_placed` drops each child at its exact rect; and
|
||||
`push_placed_clipped` clips a child's overflow like Android's
|
||||
`clipChildren`.
|
||||
|
||||
```rust,no_run
|
||||
# use ltk::{ measure_text, stack, text, Element, Rect };
|
||||
# #[ derive( Clone ) ] enum Msg {}
|
||||
# struct View { label: String, rect: Rect, clip: Option<Rect> }
|
||||
# fn external_layout_pass() -> Vec<View> { Vec::new() }
|
||||
# fn _ex() -> Element<Msg> {
|
||||
// The external layout pass measures text with the renderer's own metrics.
|
||||
let ( w, line_h ) = measure_text( "Inbox", 16.0 );
|
||||
let _ = ( w, line_h );
|
||||
|
||||
let views = external_layout_pass();
|
||||
let mut s = stack();
|
||||
for v in views
|
||||
{
|
||||
let child = text( v.label );
|
||||
s = match v.clip
|
||||
{
|
||||
Some( clip ) => s.push_placed_clipped( child, v.rect, clip ),
|
||||
None => s.push_placed( child, v.rect ),
|
||||
};
|
||||
}
|
||||
s.into()
|
||||
# }
|
||||
```
|
||||
|
||||
To rasterise the result into a caller-owned buffer instead of presenting
|
||||
it, render through a [`core::UiSurface`](#embedding-ltk-without-ltkrun)
|
||||
and call `Canvas::read_rgba_pixels( &mut buf )` — it returns tightly
|
||||
packed straight-alpha RGBA8 (top-left row first) from either backend
|
||||
(the software path un-premultiplies for you). Branch on
|
||||
`Canvas::is_software()` when a draw must honour a real path clip on
|
||||
software but only a bounding rect on GLES.
|
||||
|
||||
@@ -18,9 +18,9 @@ patterns built from these widgets, see [`docs/cookbook.md`](./cookbook.md).
|
||||
- [Continuous controls](#continuous-controls)
|
||||
- [`slider`](#slider) · [`vslider`](#vslider) · [`progress_bar`](#progress_bar)
|
||||
- [Text input and display](#text-input-and-display)
|
||||
- [`text`](#text) · [`text_edit`](#text_edit)
|
||||
- [`text`](#text) · [`text_edit`](#text_edit) · [`rich_text`](#rich_text)
|
||||
- [Decoration and chrome](#decoration-and-chrome)
|
||||
- [`container`](#container) · [`separator`](#separator) · [`img_widget`](#img_widget)
|
||||
- [`container`](#container) · [`separator`](#separator) · [`img_widget`](#img_widget) · [`external`](#external)
|
||||
- [Clipping wrappers](#clipping-wrappers)
|
||||
- [`scroll`](#scroll) · [`viewport`](#viewport) · [`flex`](#flex) · [`carousel`](#carousel)
|
||||
- [Overlays and feedback](#overlays-and-feedback)
|
||||
@@ -355,6 +355,35 @@ runtime — only what the user sees on screen changes.
|
||||
**See also**: the password recipe in
|
||||
[`docs/cookbook.md`](./cookbook.md#password-field-with-pam-submit).
|
||||
|
||||
### `rich_text`
|
||||
|
||||
A wrapped paragraph that carries a `Msg` per clickable link range — the
|
||||
ltk counterpart of an Android `Spanned` with `URLSpan` / `ClickableSpan`.
|
||||
Unlike `text`, the layout pass emits one hit rect per link *line*, so a
|
||||
link that wraps across lines is hit-tested on each of its lines and taps
|
||||
land on the link rather than on the whole paragraph.
|
||||
|
||||
**When**: body copy with inline links — a terms-and-conditions blurb, a
|
||||
chat message with a URL, an "about" screen crediting a project.
|
||||
|
||||
```rust,no_run
|
||||
# use ltk::{ rich_text, Element };
|
||||
# #[ derive( Clone ) ] enum Msg { OpenTerms, OpenPrivacy }
|
||||
# fn _ex() -> Element<Msg> {
|
||||
let body = "By continuing you accept the Terms and the Privacy Policy.";
|
||||
rich_text( body )
|
||||
.size( 14.0 )
|
||||
.link( 30, 35, Msg::OpenTerms ) // byte range of "Terms"
|
||||
.link( 44, 58, Msg::OpenPrivacy ) // byte range of "Privacy Policy"
|
||||
.into()
|
||||
# }
|
||||
```
|
||||
|
||||
Link ranges are **byte** offsets into the content `[start, end)`, drawn
|
||||
underlined in `link_color`. `color` sets the non-link text colour, `size`
|
||||
the font size (any `Length`), and `font( family, weight, style )` the
|
||||
typeface resolved through the active theme on draw.
|
||||
|
||||
---
|
||||
|
||||
## Decoration and chrome
|
||||
@@ -436,6 +465,39 @@ explicit display dimensions.
|
||||
**See also**: [`Image::from_path`](../src/widget/image.rs) helper for
|
||||
disk-loaded files (PNG, JPEG via the `image` crate).
|
||||
|
||||
### `external`
|
||||
|
||||
An escape hatch that reserves layout space and defers its pixels to a
|
||||
caller-provided producer, composited in-line with the rest of the tree.
|
||||
Two sources:
|
||||
|
||||
- `External::cpu( w, h, |canvas, rect| … )` — an immediate-mode CPU
|
||||
drawing closure invoked once per frame with the `Canvas` and the
|
||||
widget's laid-out `rect` (physical pixels). Works on **both** backends
|
||||
and is the way to host a custom `onDraw`-style routine — paths, clips,
|
||||
text — straight onto the canvas with no GL round-trip.
|
||||
- `External::new( w, h, ExternalSource::Texture( … ) )` — samples a
|
||||
caller-owned GL texture each frame (a web engine, a video decoder).
|
||||
**GLES only**; the producer keeps the texture and ltk only composites.
|
||||
|
||||
**When**: a `VectorDrawable` / Lottie frame, a custom-painted gauge, or
|
||||
embedding another renderer's output.
|
||||
|
||||
```rust,no_run
|
||||
# use ltk::{ External, Element };
|
||||
# #[ derive( Clone ) ] enum Msg {}
|
||||
# fn _ex() -> Element<Msg> {
|
||||
External::cpu( 120.0, 120.0, |canvas, rect|
|
||||
{
|
||||
canvas.fill_rect( rect, ltk::Color::rgb( 0.1, 0.1, 0.12 ), 8.0 );
|
||||
// any Canvas primitive: fill_path, set_clip_path, draw_text, …
|
||||
} ).into()
|
||||
# }
|
||||
```
|
||||
|
||||
**See also**: the CPU-drawing and path-clip recipe in
|
||||
[`docs/cookbook.md`](./cookbook.md#custom-cpu-drawing-and-path-clipping).
|
||||
|
||||
---
|
||||
|
||||
## Clipping wrappers
|
||||
|
||||
Reference in New Issue
Block a user