DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into table-hidden-chars
moppeeerc0af54eabd3f
diff --cc crates/ui-egui/src/canvas.rsindex a2fcf82a8,de4331c7d..3a1f8c733--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@@ -279,159 -165,50 +165,82 @@@ fn draw_guide_drag(app: &DesignApp, pai}}- pub fn zoom_at(app: &mut DesignApp, screen: Pos2, factor: f64) {- let Some(rect) = app.canvas_rect else { return };- let Some(v) = app.view_mut() else { return };- let xf = Xf::new(rect, v);- let c = xf.to_canvas(screen);- let screen = xf.unrotate(screen);- let nz = (v.zoom * factor).clamp(0.05, 40.0);- v.zoom = nz;- v.origin = Point::new(c.x - (screen.x - rect.min.x) as f64 / nz, c.y - (screen.y - rect.min.y) as f64 / nz);- }-- pub fn set_zoom(app: &mut DesignApp, z: f64) {- let Some(rect) = app.canvas_rect else { return };- let cur = app.view().map(|v| v.zoom).unwrap_or(1.0);- zoom_at(app, rect.center(), z / cur);- }-- /// Whether the wheel motion now arriving was made with Alt down. As egui does for Ctrl/Cmd, a- /// motion keeps its key to its end: the wheel is smoothed over a few frames, and a trackpad swipe- /// coasts on after the key is let go.- fn wheel_alt(ui: &egui::Ui) -> bool {- let id = egui::Id::new("canvas_wheel_alt");- // Alt, and whether a trackpad swipe is under way.- let (mut alt, mut swipe): (bool, bool) = ui.data(|d| d.get_temp(id)).unwrap_or_default();- ui.input(|i| {- for e in &i.events {- if let egui::Event::MouseWheel { phase, modifiers, .. } = e {- (alt, swipe) = match phase {- egui::TouchPhase::Start => (modifiers.alt, true),- egui::TouchPhase::Move => (modifiers.alt || (swipe && alt), swipe),- egui::TouchPhase::End | egui::TouchPhase::Cancel => (false, false),- };- }+ /// The rendered texture, placed where its region is in the current view.+ fn paint_render(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout, screen: Rect, preview: bool) {+ if let (Some(tex), Some(sh)) = (&app.canvas.texture, app.canvas.shown) {+ // Place the texture where its region is in the current view (shifted while panning,+ // scaled while a zoom re-render is pending).+ // The texture's corners in canvas space, placed through the view (rotation included).+ let (w, h) = (sh.size.0 as f64 / sh.zoom, sh.size.1 as f64 / sh.zoom);+ let o = sh.origin;+ let corners = [+ (o, pos2(0.0, 0.0)),+ (Point::new(o.x + w, o.y), pos2(1.0, 0.0)),+ (Point::new(o.x + w, o.y + h), pos2(1.0, 1.0)),+ (Point::new(o.x, o.y + h), pos2(0.0, 1.0)),+ ];+ let mut mesh = egui::Mesh::with_texture(tex.id());+ for (c, uv) in corners {+ mesh.vertices.push(egui::epaint::Vertex { pos: xf.to_screen(c), uv, color: Color32::WHITE });}- });- ui.data_mut(|d| d.insert_temp(id, (alt, swipe)));- alt- }-- pub fn apply_view_request(app: &mut DesignApp, p: &Value) {- if let Some(r) = p.get("showSpread").and_then(|v| serde_json::from_value::<SpreadRef>(v.clone()).ok()) {- show_spread(app, r);- }- // Power Zoom (Hand tool, Alt-press): zoom out to the spread, aim a view-sized rectangle, zoom- // back in where it was released.- if let Some(phase) = p.get("powerZoom").and_then(Value::as_str) {- let at = p.get("at").and_then(Value::as_array).map(|a| Point::new(a[0].as_f64().unwrap_or(0.0), a[1].as_f64().unwrap_or(0.0)));- match phase {- "start" => {- if let (Some(v), Some(rect)) = (app.view().copied(), app.canvas_rect) {- let centre = Xf::new(rect, &v).to_canvas(rect.center());- app.power_zoom = Some((v.zoom, at.unwrap_or(centre)));- fit(app, rect, "spread");- }- }- "move" => {- if let (Some(pz), Some(a)) = (&mut app.power_zoom, at) {- pz.1 = a;- }- }- _ => {- if let (Some((z, c)), Some(rect)) = (app.power_zoom.take(), app.canvas_rect)- && let Some(v) = app.view_mut()- {- let c = at.unwrap_or(c);- v.zoom = z;- v.origin = Point::new(c.x - rect.width() as f64 / 2.0 / z, c.y - rect.height() as f64 / 2.0 / z);- }+ mesh.indices.extend([0, 1, 2, 0, 2, 3]);+ if preview {+ // Preview: only the shown area of each spread keeps its content.+ for slot in &layout.slots {+ let r = shown_area(app, doc, xf, slot);+ painter.with_clip_rect(r.intersect(screen)).add(egui::Shape::mesh(mesh.clone()));}+ } else {+ painter.add(egui::Shape::mesh(mesh));}}- if let Some(d) = p.get("pan").and_then(Value::as_array) {- let (dx, dy) = (d.first().and_then(Value::as_f64).unwrap_or(0.0), d.get(1).and_then(Value::as_f64).unwrap_or(0.0));- if let Some(v) = app.view_mut() {- v.origin = Point::new(v.origin.x - dx / v.zoom, v.origin.y - dy / v.zoom);++++ /// As InDesign: an end-of-story # in every cell (after its text), a ¶ where a cell paragraph++ /// ends, and the table paragraph's ¶ just after the table's bottom right corner.++ #[test]++ fn table_cells_show_their_hidden_characters() {++ let mut d = Document::new(&designcraft_doc::build::NewDocument::default());++ let lid = d.default_layer();++ let (_, sid) = d.add_text_frame(SpreadRef::Doc(0), DRect::new(0.0, 0.0, 300.0, 300.0), lid, "", Default::default()).unwrap();++ let data = vec![vec!["Name:".to_string(), "".into()], vec!["a\nb".into(), "".into()]];++ let t = designcraft_doc::Table::from_strings(d.alloc(), &data, 200.0, &Default::default(), &Default::default());++ d.story_mut(sid).unwrap().insert_table(0, t);++ let cs = compose::compose_story(&d, sid, &compose::ComposeOptions::default());++ let frag = &cs.frames[0].tables[0];++ // The whole frame: the table's own line adds no second ¶; the story's last paragraph++ // (after the table) has its #.++ let story = d.story(sid).unwrap().text.clone();++ let all = frame_hidden_marks(&story, &cs.frames[0]);++ let marks: Vec<_> = all.iter().filter(|m| frag.rect.inflate(2.0, 2.0).contains(m.0)).cloned().collect();++ let ends = |m: &str| marks.iter().filter(|x| x.1 == m).count();++ assert_eq!(ends("#"), 4, "one per cell: {marks:?}");++ assert_eq!(ends("¶"), 2, "the cell's first paragraph and the table's, once: {marks:?}");++ assert_eq!(all.iter().filter(|m| m.1 == "¶" && m.3 > 20.0).count(), 0, "no ¶ the size of the table: {all:?}");++ // Each # is inside its cell, after the cell's text.++ for c in &frag.cells {++ let inside = marks.iter().filter(|m| m.1 == "#" && c.rect.contains(m.0)).count();++ assert_eq!(inside, 1, "cell {},{}: {marks:?}", c.row, c.col);+ }++ let name = frag.cell(0, 0).unwrap();++ assert!(marks.iter().any(|m| m.1 == "#" && name.rect.contains(m.0) && m.0.x > name.origin.x + 20.0), "after Name:");++ let last = marks.last().unwrap();++ assert_eq!((last.0, last.1), (designcraft_geom::Point::new(frag.rect.x1 + 1.0, frag.rect.y1), "¶"));+ }- if let (Some(a), Some(rect)) = (p.get("zoomAt").and_then(Value::as_array), app.canvas_rect)- && let Some(v) = app.view()- {- let c = Point::new(a[0].as_f64().unwrap_or(0.0), a[1].as_f64().unwrap_or(0.0));- let xf = Xf::new(rect, v);- let s = xf.to_screen(c);- zoom_at(app, s, p.get("factor").and_then(Value::as_f64).unwrap_or(2.0));- }- }-- fn mods(i: &egui::InputState, space: bool) -> Mods {- Mods { shift: i.modifiers.shift, alt: i.modifiers.alt, cmd: i.modifiers.command, ctrl: i.modifiers.ctrl, space }- }-- /// Held Space belongs to navigation only when neither the document nor a text field is typing.- fn navigation_space(app: &mut DesignApp, ctx: &egui::Context) -> bool {- ctx.input(|i| i.focused && i.key_down(egui::Key::Space)) && !app.session.wants_text() && !ctx.text_edit_focused()}- fn spring_zoom_mode(app: &DesignApp, ctx: &egui::Context, space: bool) -> bool {- let down = ctx.data(|d| d.get_temp::<bool>(egui::Id::new(("canvas_pointer_down", app.pane)))).unwrap_or(false);- space && ctx.input(|i| i.modifiers.command) && !down && !app.session.tool_busy()- }-- #[derive(Clone, Default)]- struct SpringZoom {- tool: designcraft_tools::ZoomTool,- down: bool,- }-- /// Lend the Zoom tool the gesture; the selected tool and document selection stay intact.- fn spring_zoom_input(app: &mut DesignApp, ui: &egui::Ui, resp: &egui::Response, xf: &Xf, active: bool) -> bool {- let id = egui::Id::new(("canvas_spring_zoom", app.pane));- let mut zoom: SpringZoom = ui.data(|d| d.get_temp(id)).unwrap_or_default();- if !ui.input(|i| i.focused) {- ui.data_mut(|d| d.remove::<SpringZoom>(id));- return false;- }- if !zoom.down && (!active || ui.input(|i| i.pointer.middle_down())) {- return false;- }- let (pointer, m) = ui.input(|i| (i.pointer.clone(), mods(i, true)));- let vi = app.view_info();- let mut events = Vec::new();- let canvas = if xf.rot == 0 { app.canvas_rect } else { Some(resp.rect) };- if pointer.primary_pressed()- && resp.hovered()- && let Some(p) = pointer.press_origin().filter(|p| canvas.is_some_and(|r| r.contains(*p)))- {- events.push(PointerEvent { kind: PointerKind::Down, pos: xf.to_canvas(p), mods: m, clicks: 1 });- zoom.down = true;- resp.request_focus();- }- if zoom.down- && let Some(p) = pointer.interact_pos()- {- if resp.dragged() && resp.drag_delta() != egui::Vec2::ZERO {- events.push(PointerEvent { kind: PointerKind::Drag, pos: xf.to_canvas(p), mods: m, clicks: 1 });- }- if pointer.primary_released() {- events.push(PointerEvent { kind: PointerKind::Up, pos: xf.to_canvas(p), mods: m, clicks: 1 });- zoom.down = false;- }- }- for e in events {- if let Err(err) = app.session.pointer_with_tool(&mut zoom.tool, &e, vi) {- app.status(err.to_string());- }+ /// Presentation: a click shows the next spread, Shift-click the previous one.+ fn present_click(app: &mut DesignApp, ui: &egui::Ui, resp: &egui::Response, rect: Rect) {+ if resp.clicked() {+ // The release that made the click says whether Shift was down.+ let shift = ui.input(|i| {+ i.modifiers.shift || i.events.iter().any(|e| matches!(e, egui::Event::PointerButton { pressed: false, modifiers, .. } if modifiers.shift))+ });+ step_spread(app, if shift { -1 } else { 1 });+ present_fit(app, rect);}- app.after_engine();- // Keep ownership until pointer release even if the shortcut was released first, so an Up- // cannot leak into the previous tool and edit the document.- ui.data_mut(|d| d.insert_temp(id, zoom));- true}+ /// Lay out the canvas, scrollbars and rulers, presentation mode, fit/view state, input handling and painting.pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {let t = Tokens::get(ui.ctx());let outer = ui.available_rect_before_wrap();diff --cc crates/ui-egui/src/canvas/text_overlay.rsindex 000000000,dc48ae720..6af92d842mode 000000,100644..100644--- a/crates/ui-egui/src/canvas/text_overlay.rs+++ b/crates/ui-egui/src/canvas/text_overlay.rs@@@ -1,0 -1,370 +1,394 @@@+ use super::frames::{item_canvas_xf, layer_color, path_screen};+ use super::{Affine, CanvasLayout, Color32, DesignApp, Document, Point, Pos2, Rect, Stroke, Xf, compose, pos2};++ pub(super) fn draw_text_selection(+ app: &DesignApp,+ painter: &egui::Painter,+ xf: &Xf,+ doc: &Document,+ layout: &CanvasLayout,+ ts: designcraft_doc::TextSel,+ cells: Option<designcraft_doc::TableSel>,+ ) {+ let cs = app.session.cache.get(doc, ts.story, None);+ let range = ts.range();+ let tint = Color32::from_rgba_unmultiplied(80, 140, 255, 110);+ // Selected table cells: tint every selected cell.+ if let Some(sel) = cells.filter(|c| c.story == ts.story) {+ let selected = doc.story(sel.story).and_then(|s| s.tables.get(&sel.table)).map(|t| t.selected_owners(sel.range)).unwrap_or_default();+ for ft in &cs.frames {+ let (Some((a, _)), Some(it)) = (item_canvas_xf(doc, layout, ft.frame), doc.item(ft.frame)) else { continue };+ let m = a * doc.text_xf(it);+ for t in ft.tables.iter().filter(|t| t.table == sel.table) {+ for c in t.cells.iter().filter(|c| selected.binary_search(&(c.row, c.col)).is_ok()) {+ let q = [+ xf.to_screen(m * Point::new(c.rect.x0, c.rect.y0)),+ xf.to_screen(m * Point::new(c.rect.x1, c.rect.y0)),+ xf.to_screen(m * Point::new(c.rect.x1, c.rect.y1)),+ xf.to_screen(m * Point::new(c.rect.x0, c.rect.y1)),+ ];+ painter.add(egui::Shape::convex_polygon(q.to_vec(), Color32::from_rgba_unmultiplied(80, 140, 255, 90), Stroke::NONE));+ }+ }+ }+ return;+ }+ if let Some(cell) = ts.cell {+ draw_cell_selection(app, painter, xf, doc, layout, &cs, ts, cell);+ return;+ }+ let mut runs: Vec<(usize, Affine, Vec<[Point; 4]>, Vec<(usize, usize)>)> = Vec::new();+ for (fi, ft) in cs.frames.iter().enumerate() {+ let Some((a, _)) = item_canvas_xf(doc, layout, ft.frame) else { continue };+ let Some(it) = doc.item(ft.frame) else { continue };+ let m = a * doc.text_xf(it);+ // Show the frame edge while typing.+ for poly in path_screen(it, xf, a) {+ painter.add(egui::Shape::line(poly, Stroke::new(1.0, layer_color(doc, it).gamma_multiply(0.6))));+ }+ if range.is_empty() {+ continue;+ }+ // Type on a path: the highlight bends with the text.+ if let Some(map) = compose::PathTextMap::new(it, ft) {+ for l in &ft.lines {+ let (s, e) = (range.start.max(l.range.start), range.end.min(l.range.end));+ if s >= e {+ continue;+ }+ for q in compose::highlight_quads(l, s, e, false) {+ let (x0, x1) = (q[0].x.min(q[1].x), q[0].x.max(q[1].x));+ let (y0, y1) = (q[0].y.min(q[2].y), q[0].y.max(q[2].y));+ // Short slices, each turned with the path.+ let n = ((x1 - x0) / 2.0).ceil().clamp(1.0, 2000.0) as usize;+ for k in 0..n {+ let (a, b) = (x0 + (x1 - x0) * k as f64 / n as f64, x0 + (x1 - x0) * (k + 1) as f64 / n as f64);+ let poly =+ [Point::new(a, y0), Point::new(b, y0), Point::new(b, y1), Point::new(a, y1)].map(|p| xf.to_screen(m * map.to_path(p)));+ painter.add(egui::Shape::convex_polygon(poly.to_vec(), tint, Stroke::NONE));+ }+ }+ }+ continue;+ }+ let mut quads = Vec::new();+ let mut glyphs = Vec::new();+ for (li, l) in ft.lines.iter().enumerate() {+ let s = range.start.max(l.range.start);+ let e = range.end.min(l.range.end);+ if s > e || (s == e && !(l.range.end < range.end && e == l.range.end)) {+ continue;+ }+ let past = e == l.range.end && range.end > l.range.end;+ quads.extend(compose::highlight_quads(l, s, e, past));+ // A selected drop cap is highlighted down to its baseline.+ if let Some(dc) = l.drop_cap {+ let sel = l.glyphs.iter().filter(|g| g.len > 0 && g.byte < dc.end && g.byte >= s && g.byte < e);+ let (a, b) = sel.fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), g| (a.min(g.x), b.max(g.x + g.adv)));+ if a < b {+ let r = dc.rect;+ quads.push([Point::new(a, r.y0), Point::new(b, r.y0), Point::new(b, r.y1), Point::new(a, r.y1)]);+ }+ }+ for (gi, g) in l.glyphs.iter().enumerate() {+ if g.visible && g.len > 0 && g.byte >= s && g.byte < e {+ glyphs.push((li, gi));+ }+ }+ }+ if !quads.is_empty() {+ runs.push((fi, m, quads, glyphs));+ }+ }+ if !runs.is_empty() {+ draw_inverse_highlight(app, painter, xf, doc, &cs, ts.story, &runs);+ }+ if ts.is_caret()+ && let Some((fi, x, bl, asc, desc)) = compose::caret(&cs, ts.focus)+ && let Some(ft) = cs.frames.get(fi)+ && let (Some((a, _)), Some(it)) = (item_canvas_xf(doc, layout, ft.frame), doc.item(ft.frame))+ {+ let m = a * doc.text_xf(it);+ // Type on a path: the caret stands across the path where it is along it.+ let on = |p: Point| compose::PathTextMap::new(it, ft).map_or(p, |map| map.to_path(p));+ let p0 = xf.to_screen(m * on(Point::new(x, bl - asc)));+ let p1 = xf.to_screen(m * on(Point::new(x, bl + desc)));+ let blink = (painter.ctx().input(|i| i.time) * 1.6) as i64 % 2 == 0;+ if blink {+ painter.line_segment([p0, p1], Stroke::new(1.0, Color32::BLACK));+ }+ painter.ctx().request_repaint_after(std::time::Duration::from_millis(330));+ }+ }++ /// Caret / text highlight inside a table cell (the cell's lines are offset by the cell origin).+ #[allow(clippy::too_many_arguments)]+ fn draw_cell_selection(+ _app: &DesignApp,+ painter: &egui::Painter,+ xf: &Xf,+ doc: &Document,+ layout: &CanvasLayout,+ cs: &compose::ComposedStory,+ ts: designcraft_doc::TextSel,+ cell: designcraft_doc::CellAddr,+ ) {+ // Footnote text or a cell: its composed story and where it sits in the frame.+ let found = match cell.footnote_id() {+ Some(id) => compose::find_note(cs, id).map(|(fi, n)| (fi, n.origin, n.text.clone())),+ None => compose::find_cell(cs, cell.table, cell.row, cell.col).map(|(fi, _, pc)| (fi, pc.origin, pc.text.clone())),+ };+ let Some((fi, origin, text)) = found else { return };+ let Some(ft) = cs.frames.get(fi) else { return };+ let (Some((a, _)), Some(it)) = (item_canvas_xf(doc, layout, ft.frame), doc.item(ft.frame)) else { return };+ let m = a * doc.text_xf(it) * designcraft_geom::Affine::translate(origin.to_vec2());+ let range = ts.range();+ if !range.is_empty()+ && let Some(cft) = text.frames.first()+ {+ for l in &cft.lines {+ let s = range.start.max(l.range.start);+ let e = range.end.min(l.range.end);+ if s > e || (s == e && !(l.range.end < range.end && e == l.range.end)) {+ continue;+ }+ let past = e == l.range.end && range.end > l.range.end;+ for q in compose::highlight_quads(l, s, e, past) {+ let poly = q.map(|p| xf.to_screen(m * p));+ painter.add(egui::Shape::convex_polygon(poly.to_vec(), Color32::from_rgba_unmultiplied(80, 140, 255, 110), Stroke::NONE));+ }+ }+ }+ if ts.is_caret()+ && let Some((_, x, bl, asc, desc)) = match cell.footnote_id() {+ Some(id) => compose::note_caret(cs, id, ts.focus),+ None => compose::cell_caret(cs, cell.table, cell.row, cell.col, ts.focus),+ }+ {+ let m = a * doc.text_xf(it);+ let (p0, p1) = (xf.to_screen(m * Point::new(x, bl - asc)), xf.to_screen(m * Point::new(x, bl + desc)));+ let blink = (painter.ctx().input(|i| i.time) * 1.6) as i64 % 2 == 0;+ if blink {+ painter.line_segment([p0, p1], Stroke::new(1.0, Color32::BLACK));+ }+ painter.ctx().request_repaint_after(std::time::Duration::from_millis(330));+ }+ }++ /// InDesign draws selected text inverted: a black highlight with the selected glyphs in the+ /// inverse of their colour (white for black text). The highlight and glyph outlines are+ /// rasterised with `vello_cpu` into a small screen-space texture over the selection.+ fn draw_inverse_highlight(+ app: &DesignApp,+ painter: &egui::Painter,+ xf: &Xf,+ doc: &Document,+ cs: &compose::ComposedStory,+ story: designcraft_doc::StoryId,+ runs: &[(usize, Affine, Vec<[Point; 4]>, Vec<(usize, usize)>)],+ ) {+ use designcraft_render::vello_cpu::{self, kurbo, peniko};+ let ctx = painter.ctx();+ let ppp = ctx.pixels_per_point();+ let to_screen = |m: Affine, p: Point| xf.to_screen(m * p);+ let mut bbox = Rect::NOTHING;+ for (_, m, quads, _) in runs {+ for q in quads {+ for p in q {+ bbox.extend_with(to_screen(*m, *p));+ }+ }+ }+ let bbox = bbox.intersect(painter.clip_rect());+ if !bbox.is_positive() {+ return;+ }+ // Snap to device pixels so the texture maps 1:1.+ let bbox = Rect::from_min_max(+ pos2((bbox.min.x * ppp).floor() / ppp, (bbox.min.y * ppp).floor() / ppp),+ pos2((bbox.max.x * ppp).ceil() / ppp, (bbox.max.y * ppp).ceil() / ppp),+ );+ let (w, h) = (((bbox.width() * ppp).round() as u32).clamp(1, 8192), ((bbox.height() * ppp).round() as u32).clamp(1, 8192));+ let Some(st) = app.session.active() else { return };+ let key = egui::Id::unique(("inverse_sel", st.uid, st.revision, std::sync::Arc::as_ptr(&st.doc) as usize, story.0))+ .with((runs.iter().map(|r| r.2.len() + r.3.len() * 7919).sum::<usize>(), w, h))+ .with((bbox.min.x.to_bits(), bbox.min.y.to_bits(), xf.zoom.to_bits(), st.selection.text.map(|t| (t.anchor, t.focus))));+ let cache_id = egui::Id::unique("inverse_sel_tex");+ let cached: Option<(egui::Id, egui::TextureHandle)> = ctx.data(|d| d.get_temp(cache_id));+ let tex = match cached {+ Some((k, t)) if k == key => t,+ _ => {+ let view = xf.affine(ppp as f64, bbox.min);+ let mut rc = vello_cpu::RenderContext::new_with(w as u16, h as u16, vello_cpu::RenderSettings { num_threads: 0, ..Default::default() });+ rc.set_paint(peniko::Color::from_rgba8(0, 0, 0, 255));+ for (_, m, quads, _) in runs {+ rc.set_transform(view * *m);+ for q in quads {+ let mut bp = kurbo::BezPath::new();+ bp.move_to(q[0]);+ for p in &q[1..] {+ bp.line_to(*p);+ }+ bp.close_path();+ rc.fill_path(&bp);+ }+ }+ let db = designcraft_fonts::FontDb::global();+ for (fi, m, _, glyphs) in runs {+ let Some(ft) = cs.frames.get(*fi) else { continue };+ for &(li, gi) in glyphs {+ let Some(l) = ft.lines.get(li) else { continue };+ let Some(g) = l.glyphs.get(gi) else { continue };+ let Some(style) = cs.styles.get(g.style as usize) else { continue };+ let Some(c) = doc.resolve_color(&style.fill, style.fill_tint) else { continue };+ let [r, gg, b, _] = c.to_rgba8(1.0);+ rc.set_paint(peniko::Color::from_rgba8(255 - r, 255 - gg, 255 - b, 255));+ let outline = db.outline(&g.face, g.gid);+ let skew =+ if style.skew != 0.0 { Affine::new([1.0, 0.0, -style.skew.to_radians().tan(), 1.0, 0.0, 0.0]) } else { Affine::IDENTITY };+ let shatai = style.shatai_xf(g, l.baseline).unwrap_or(Affine::IDENTITY);+ let ga = shatai * Affine::translate((g.x, l.baseline + g.y)) * skew * Affine::scale_non_uniform(g.sx, g.sy);+ rc.set_transform(view * *m * ga);+ rc.fill_path(&outline);+ }+ }+ rc.flush();+ let mut pm = vello_cpu::Pixmap::new(w as u16, h as u16);+ let mut res = vello_cpu::Resources::new();+ rc.render(&mut pm, &mut res);+ let ci = egui::ColorImage::from_rgba_premultiplied([w as usize, h as usize], pm.data_as_u8_slice());+ let t = crate::widgets::load_texture(ctx, "inverse_selection", ci, egui::TextureOptions::NEAREST);+ ctx.data_mut(|d| d.insert_temp(cache_id, (key, t.clone())));+ t+ }+ };+ painter.image(tex.id(), bbox, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);+ }++ /// View → Show Hidden Characters: ¶ paragraph ends, » tabs, · spaces, ¬ forced line breaks, # end of story.++/// As InDesign, every table cell shows its own marks (an empty cell its #), and a table's++/// paragraph return sits after the table's bottom right corner.+ pub(super) fn draw_hidden_characters(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout) {+ for story in doc.stories.values() {+ let cs = app.session.cache.get(doc, story.id, None);+ for ft in &cs.frames {+ let (Some((a, _)), Some(it)) = (item_canvas_xf(doc, layout, ft.frame), doc.item(ft.frame)) else { continue };+ let m = a * doc.text_xf(it);+ let col = layer_color(doc, it);- for l in &ft.lines {- let size = (l.ascent * 0.75 * xf.zoom).clamp(6.0, 40.0) as f32;- let font = egui::FontId::proportional(size);- for (x, mark, centered) in hidden_marks(&story.text, l) {- let p = xf.to_screen(m * Point::new(x, l.baseline));- let align = if centered { egui::Align2::CENTER_BOTTOM } else { egui::Align2::LEFT_BOTTOM };- painter.text(p, align, mark, font.clone(), col);- }++ for (p, mark, centered, ascent) in frame_hidden_marks(&story.text, ft) {++ let size = (ascent * 0.75 * xf.zoom).clamp(6.0, 40.0) as f32;++ let align = if centered { egui::Align2::CENTER_BOTTOM } else { egui::Align2::LEFT_BOTTOM };++ painter.text(xf.to_screen(m * p), align, mark, egui::FontId::proportional(size), col);++ }++ }++ }++}++++/// A frame's hidden-character marks in frame space (position at the baseline, mark, centered,++/// the line's ascent): its lines' marks, then its tables' — each text cell's own marks (nested++/// tables included) with its # where its text ends, and a table paragraph's ¶ after the last++/// row's bottom right corner. A table's own glyph-less line has no marks: its ¶ is that one.++fn frame_hidden_marks(text: &str, ft: &compose::FrameText) -> Vec<(Point, &'static str, bool, f64)> {++ let table_lines: Vec<usize> = ft.tables.iter().map(|t| t.line).collect();++ let mut out: Vec<_> = ft++ .lines++ .iter()++ .enumerate()++ .filter(|(i, _)| !table_lines.contains(i))++ .flat_map(|(_, l)| hidden_marks(text, l).into_iter().map(move |(x, mark, c)| (Point::new(x, l.baseline), mark, c, l.ascent)))++ .collect();++ for t in &ft.tables {++ for c in t.cells.iter().filter(|c| c.graphic.is_none()) {++ if let Some(cf) = c.text.frames.first() {++ out.extend(frame_hidden_marks(&c.source, cf).into_iter().map(|(p, mark, centered, a)| (p + c.origin.to_vec2(), mark, centered, a)));+ }+ }++ if t.last {++ out.push((Point::new(t.rect.x1 + 1.0, t.rect.y1), "¶", false, 12.0));++ }+ }++ out+ }++ /// The hidden-character marks on line `l` of a story with text `text`: frame-space x, mark, and+ /// whether the mark is centered on x (spaces) or starts at it.+ pub(super) fn hidden_marks(text: &str, l: &compose::Line) -> Vec<(f64, &'static str, bool)> {+ let mut out = Vec::new();+ for g in &l.glyphs {+ if g.len == 0 {+ continue;+ }+ let Some(ch) = text.get(g.byte..).and_then(|t| t.chars().next()) else { continue };+ let mark = match ch {+ ' ' => "·",+ '\t' => "»",+ '\u{2028}' => "¬",+ '\u{A0}' => "°",+ _ => continue,+ };+ if ch == ' ' {+ out.push((g.x + g.adv / 2.0, mark, true));+ } else {+ out.push((g.x, mark, false));+ }+ }+ if l.last_in_para {+ // The spaces trimmed off the line's end sit past `end_x`; the paragraph mark follows them.+ let end = l.glyphs.iter().filter(|g| g.len > 0 && !g.visible && g.x >= l.end_x - 1e-9).map(|g| g.x + g.adv).fold(l.end_x, f64::max);+ let mark = if l.range.end >= text.len() { "#" } else { "¶" };+ out.push((end + 1.0, mark, false));+ }+ out+ }++ /// Edit › Spelling › Dynamic Spelling: red squiggles under misspelled words (checked once per+ /// story version).+ pub(super) fn draw_dynamic_spelling(app: &DesignApp, ctx: &egui::Context, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout) {+ for story in doc.stories.values() {+ let key = egui::Id::unique(("dyn_spell", std::sync::Arc::as_ptr(story) as usize, doc.user_words.len()));+ let bad: std::sync::Arc<Vec<std::ops::Range<usize>>> = match ctx.data(|d| d.get_temp(key)) {+ Some(v) => v,+ None => {+ let v = std::sync::Arc::new(designcraft_engine::cmd::spelling::misspellings(doc, story, &doc.user_words));+ ctx.data_mut(|d| d.insert_temp(key, v.clone()));+ v+ }+ };+ if bad.is_empty() {+ continue;+ }+ let cs = app.session.cache.get(doc, story.id, None);+ let red = Stroke::new(1.0, Color32::from_rgb(230, 30, 30));+ for ft in &cs.frames {+ let (Some((a, _)), Some(it)) = (item_canvas_xf(doc, layout, ft.frame), doc.item(ft.frame)) else { continue };+ let m = a * doc.text_xf(it);+ for l in &ft.lines {+ for r in bad.iter().filter(|r| r.start < l.range.end && r.end > l.range.start) {+ let gs: Vec<_> = l.glyphs.iter().filter(|g| g.len > 0 && g.byte >= r.start && g.byte < r.end).collect();+ let (Some(x0), Some(x1)) = (gs.iter().map(|g| g.x).reduce(f64::min), gs.iter().map(|g| g.x + g.adv).reduce(f64::max)) else {+ continue;+ };+ let y = l.baseline + l.descent * 0.5;+ let (p0, p1) = (xf.to_screen(m * Point::new(x0, y)), xf.to_screen(m * Point::new(x1, y)));+ // A zigzag 2 px high, 4 px per wave.+ let n = ((p1.x - p0.x) / 2.0).max(1.0) as usize;+ let pts: Vec<Pos2> =+ (0..=n).map(|k| pos2(p0.x + (p1.x - p0.x) * k as f32 / n as f32, p0.y + if k % 2 == 0 { 0.0 } else { 2.0 })).collect();+ painter.add(egui::Shape::line(pts, red));+ }+ }+ }+ }+ }++ /// Plain text of the current text selection (story markers resolved to readable characters).+ pub fn selected_text(app: &DesignApp) -> Option<String> {+ let st = app.session.active()?;+ let t = st.selection.text?;+ let story = st.doc.text_story(t.story, t.cell)?;+ let s = story.slice(t.range());+ if s.is_empty() {+ return None;+ }+ Some(s.replace(designcraft_doc::story::FORCED_LINE_BREAK, "\n"))+ }