Add text layers

Introduce editable text layers: a resizable text box with editable text,
font size, color, font family, and alignment.

Core:
- New TextLayer/TextContent (text_layer.rs), wired into AnyLayer/LayerType
  and all the exhaustive match sites; structured so content can be keyframed
  later via content_at().
- fonts.rs: thread-local parley FontContext with three bundled fonts
  (Liberation Sans/Serif/Mono, SIL OFL), system-font enumeration consolidated
  to base families, document-embedded fonts, glyph/caret/selection geometry,
  and a background preloader for the picker fonts.
- Rendering via parley layout + Scene::draw_glyphs (renderer.rs); text
  composites through the vector path.
- Actions: CreateTextClipAction (vector-layer branch, undoable),
  SetTextContentAction, ResizeTextBoxAction.
- .beam font embedding: MediaKind::Font rows (content-hash dedupe) written on
  save and registered on load, with bundled-default fallback.
- VectorClip content bounds include text boxes so text-only clips are
  selectable/draggable.

Editor:
- Text tool: click empty/raster/video to create a top-level text layer, or a
  vector layer to create+enter a clip containing the text; click an existing
  box to edit it.
- Hybrid in-place editing: a hidden egui TextEdit drives input/IME/caret while
  the text and caret/selection render in Vello; empty just-created layers are
  removed on commit.
- Selection outline + 8 resize handles (re-wrap text) with hover cursors;
  factored the corner/edge resize-cursor mapping shared with the Transform tool.
- Info panel: edit text, size, color, alignment, box size, and a font-family
  picker that previews each entry in its own font (fonts preloaded in the
  background to avoid hitches).

Deps: add parley (git, pinned to match vello's peniko); bundle Liberation
fonts under lightningbeam-core/assets/fonts. Gitignore the local
.cargo/config.toml used to select a machine's ffmpeg.
This commit is contained in:
Skyler Lehmkuhl
2026-06-27 18:14:47 -04:00
parent 58a3c829d7
commit fd582828c2
37 changed files with 2070 additions and 184 deletions
@@ -46,10 +46,16 @@ pub struct InfopanelPane {
fps_drag_start: Option<f64>,
/// Resize mode for the active raster layer's "to document size" action (scale vs canvas).
raster_resize_mode: lightningbeam_core::raster_layer::RasterResizeMode,
/// Font families already registered into egui for the family-picker previews
/// (rendered each entry in its own font), so we only load each once.
registered_preview_fonts: std::collections::HashSet<String>,
}
impl InfopanelPane {
pub fn new() -> Self {
// Kick off background loading of the font-picker fonts at startup so the dropdown
// is ready without a hitch by the time the user opens it.
lightningbeam_core::fonts::start_preload();
let presets = bundled_brushes();
let default_eraser_idx = presets.iter().position(|p| p.name == "Brush");
Self {
@@ -64,8 +70,26 @@ impl InfopanelPane {
selected_tool_gradient_stop: None,
fps_drag_start: None,
raster_resize_mode: lightningbeam_core::raster_layer::RasterResizeMode::Scale,
registered_preview_fonts: std::collections::HashSet::new(),
}
}
/// Register pre-loaded `bytes` for `family` into egui under `FontFamily::Name(family)`
/// so a label can be rendered in that font. Cheap (no font IO); the add is idempotent
/// and batched into one atlas rebuild at frame end, so the preview appears next frame.
fn register_font_bytes(&mut self, ctx: &egui::Context, family: &str, bytes: Vec<u8>) {
if family.is_empty() || !self.registered_preview_fonts.insert(family.to_string()) {
return;
}
ctx.add_font(egui::epaint::text::FontInsert::new(
family,
egui::FontData::from_owned(bytes),
vec![egui::epaint::text::InsertFontFamily {
family: egui::FontFamily::Name(family.into()),
priority: egui::epaint::text::FontPriority::Highest,
}],
));
}
}
/// Aggregated info about the current DCEL selection
@@ -1167,6 +1191,7 @@ impl InfopanelPane {
AnyLayer::Effect(_) => "Effect",
AnyLayer::Group(_) => "Group",
AnyLayer::Raster(_) => "Raster",
AnyLayer::Text(_) => "Text",
};
ui.horizontal(|ui| {
ui.label("Type:");
@@ -1247,6 +1272,186 @@ impl InfopanelPane {
});
}
/// Render a text-layer section: edit the text content, font, size, color,
/// alignment, and box size of the active text layer. Driven by the *active*
/// layer so it appears whenever a text layer is selected.
fn render_text_layer_section(&mut self, ui: &mut Ui, path: &NodePath, shared: &mut SharedPaneState, layer_id: Uuid) {
use lightningbeam_core::text_layer::TextAlign;
use lightningbeam_core::actions::{ResizeTextBoxAction, SetTextContentAction};
// Snapshot current values, then drop the document borrow before mutating shared.
let snapshot = {
let document = shared.action_executor.document();
match document.get_layer(&layer_id) {
Some(AnyLayer::Text(tl)) => {
Some((tl.content.clone(), tl.box_origin, tl.box_width, tl.box_height))
}
_ => None,
}
};
let Some((content0, origin, w0, h0)) = snapshot else { return };
// Families egui has actually bound (added fonts take effect next frame). Using an
// unbound `FontFamily::Name` panics, so only preview-render families in this set.
let bound_families: std::collections::HashSet<egui::FontFamily> =
ui.ctx().fonts(|f| f.families().into_iter().collect());
// Set when the font dropdown popup is open this frame (its closure only runs then),
// so we can synchronously finish loading any not-yet-preloaded fonts the user sees.
let mut dropdown_open = false;
egui::CollapsingHeader::new("Text")
.id_salt(("text_layer", path))
.default_open(true)
.show(ui, |ui| {
ui.add_space(4.0);
let mut content = content0.clone();
let mut content_changed = false;
ui.label("Text:");
if ui
.add(egui::TextEdit::multiline(&mut content.text).desired_rows(2).desired_width(f32::INFINITY))
.changed()
{
content_changed = true;
}
ui.horizontal(|ui| {
ui.label("Size:");
if ui
.add(egui::DragValue::new(&mut content.font_size).range(1.0..=2000.0).speed(0.5))
.changed()
{
content_changed = true;
}
});
ui.horizontal(|ui| {
ui.label("Color:");
// content.color is sRGB-encoded RGBA in 0..1 (matches peniko Color::new).
let mut col = egui::Color32::from_rgba_unmultiplied(
(content.color[0] * 255.0).round() as u8,
(content.color[1] * 255.0).round() as u8,
(content.color[2] * 255.0).round() as u8,
(content.color[3] * 255.0).round() as u8,
);
if ui.color_edit_button_srgba(&mut col).changed() {
content.color = [
col.r() as f32 / 255.0,
col.g() as f32 / 255.0,
col.b() as f32 / 255.0,
col.a() as f32 / 255.0,
];
content_changed = true;
}
});
ui.horizontal(|ui| {
ui.label("Align:");
for (label, a) in [("L", TextAlign::Left), ("C", TextAlign::Center), ("R", TextAlign::Right), ("J", TextAlign::Justify)] {
if ui.selectable_label(content.align == a, label).clicked() {
content.align = a;
content_changed = true;
}
}
});
// Font family picker (bundled families first, then system; embedded fonts
// register under their own names on load).
ui.horizontal(|ui| {
ui.label("Font:");
let current_label = if content.font_family.is_empty() {
"(Default)".to_string()
} else {
content.font_family.clone()
};
egui::ComboBox::from_id_salt(("text_font", path))
.selected_text(current_label)
.show_ui(ui, |ui| {
dropdown_open = true;
if ui.selectable_label(content.font_family.is_empty(), "(Default)").clicked() {
content.font_family.clear();
content_changed = true;
}
// The popup closure only runs while the dropdown is open, so each
// entry is rendered in its own font and queued for registration.
for fam in lightningbeam_core::fonts::families() {
let named = egui::FontFamily::Name(fam.as_str().into());
// Render in its own font only once egui has bound it; otherwise
// plain (and queue it for registration so it previews next frame).
let label = if bound_families.contains(&named) {
egui::RichText::new(&fam).family(named)
} else {
egui::RichText::new(&fam)
};
if ui.selectable_label(content.font_family == fam, label).clicked() {
content.font_family = fam.clone();
content_changed = true;
}
}
});
// Missing-font indicator.
if !content.font_family.is_empty()
&& !lightningbeam_core::fonts::family_available(&content.font_family)
{
ui.colored_label(egui::Color32::from_rgb(230, 160, 60), "⚠ missing");
}
});
// Box size.
let mut bw = w0;
let mut bh = h0;
let mut box_changed = false;
ui.horizontal(|ui| {
ui.label("Box:");
if ui.add(egui::DragValue::new(&mut bw).range(1.0..=100000.0).speed(1.0)).changed() {
box_changed = true;
}
ui.label("×");
if ui.add(egui::DragValue::new(&mut bh).range(1.0..=100000.0).speed(1.0)).changed() {
box_changed = true;
}
});
ui.add_space(4.0);
if content_changed {
shared.pending_actions.push(Box::new(SetTextContentAction::new(layer_id, content)));
}
if box_changed {
shared.pending_actions.push(Box::new(ResizeTextBoxAction::new(layer_id, origin, bw, bh)));
}
});
// Register fonts the background preloader has finished (cheap: bytes already loaded),
// a few per frame so each atlas rebuild stays small. Only fall back to synchronous
// loading for families the user is actively viewing in the open dropdown that the
// background hasn't reached yet. Repaint while there's more to do or the preloader
// is still running, so registration keeps flowing without input.
let ctx = ui.ctx().clone();
const PER_FRAME: usize = 4;
let mut budget = PER_FRAME;
let mut more = false;
for fam in lightningbeam_core::fonts::families() {
if self.registered_preview_fonts.contains(&fam) {
continue;
}
let bytes = lightningbeam_core::fonts::take_preloaded(&fam).or_else(|| {
if dropdown_open { lightningbeam_core::fonts::family_font_bytes(&fam) } else { None }
});
if let Some(bytes) = bytes {
if budget == 0 {
more = true;
break;
}
self.register_font_bytes(&ctx, &fam, bytes);
budget -= 1;
}
}
if more || !lightningbeam_core::fonts::preload_done() {
ctx.request_repaint();
}
}
/// Render clip instance info section
fn render_clip_instance_section(&self, ui: &mut Ui, path: &NodePath, shared: &SharedPaneState, clip_ids: &[Uuid]) {
let document = shared.action_executor.document();
@@ -1270,6 +1475,7 @@ impl InfopanelPane {
AnyLayer::Effect(l) => &l.clip_instances,
AnyLayer::Group(_) => &[],
AnyLayer::Raster(_) => &[],
AnyLayer::Text(_) => &[],
};
if let Some(ci) = instances.iter().find(|c| c.id == ci_id) {
found = true;
@@ -1601,6 +1807,7 @@ impl PaneRenderer for InfopanelPane {
// active (independent of selection focus, since painting doesn't focus the layer).
if let Some(active_id) = *shared.active_layer_id {
self.render_raster_layer_section(ui, path, shared, active_id);
self.render_text_layer_section(ui, path, shared, active_id);
}
// Onion-skinning view settings — always available, regardless of selection.