mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-05 20:27:02 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f086af023f |
@@ -12,7 +12,7 @@ use crate::{DocLayout, Page, Placed};
|
||||
|
||||
/// Caret geometry on a page: a bar from (`x`, `top`) to (`x + width`, `top + height`). It is
|
||||
/// upright (`width` 0) except in turned text (a table cell's text direction), where it lies
|
||||
/// across the page (`height` 0).
|
||||
/// across the page (`height` 0), and `turn` is the direction its line reads in.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Caret {
|
||||
pub page: usize,
|
||||
@@ -20,6 +20,7 @@ pub struct Caret {
|
||||
pub top: f32,
|
||||
pub height: f32,
|
||||
pub width: f32,
|
||||
pub turn: TextDirection,
|
||||
}
|
||||
|
||||
/// An equation as [`DocLayout::equation_box`] finds it, in page coordinates: its left edge
|
||||
@@ -757,7 +758,7 @@ impl DocLayout {
|
||||
let base = top + (l.baseline - l.top);
|
||||
let ctop = (base - asc).max(top);
|
||||
let r = crate::turn_rect(*turn, *x, *y, Rect::new(cx, ctop, 0.0, (base + desc - ctop).max(4.0)));
|
||||
return Some(Caret { page: pi, x: r.x, top: r.y, height: r.h, width: r.w });
|
||||
return Some(Caret { page: pi, x: r.x, top: r.y, height: r.h, width: r.w, turn: *turn });
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -765,18 +766,15 @@ impl DocLayout {
|
||||
/// The style run text typed at `pos` would take (the character's before it, or at the start
|
||||
/// of a paragraph the first character's) and the baseline it sits on, on the page: `pos`'s
|
||||
/// line's, raised or lowered by the run's shift (superscript, `w:position`). An input method's
|
||||
/// composition is drawn with them, as Word draws it in the text's own font and size. `None` in
|
||||
/// turned text, whose lines aren't horizontal.
|
||||
/// composition is drawn with them, as Word draws it in the text's own font and size. In turned
|
||||
/// text the baseline is meaningless (the lines aren't horizontal); use the run only.
|
||||
pub fn typing_style(&self, pos: &Pos, page_hint: usize) -> Option<(StyleRun, f32)> {
|
||||
for (_, it) in self.pieces(pos.story, &pos.path, page_hint) {
|
||||
let Placed::Lines { para, l0, l1, y, turn, .. } = it else { continue };
|
||||
let Placed::Lines { para, l0, l1, y, .. } = it else { continue };
|
||||
let li = para.line_of(pos.off);
|
||||
if li < *l0 || li >= *l1 {
|
||||
continue;
|
||||
}
|
||||
if *turn != TextDirection::Horizontal {
|
||||
return None;
|
||||
}
|
||||
let first = para.lines.get(*l0)?;
|
||||
let line = para.lines.get(li)?;
|
||||
let at = |off: usize| para.clusters.iter().find(|c| c.start <= off && off < c.end).and_then(|c| para.styles.get(c.style as usize));
|
||||
|
||||
@@ -13,6 +13,7 @@ use craft::ui::primitives::{FaceRun, PaintShape, StrokeWidth, TextRun};
|
||||
use craft::ui::runtime::env::Label;
|
||||
use craft::ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx};
|
||||
use egui::{Color32, Pos2, Rect, Response, Ui, pos2, vec2};
|
||||
use wordcraft_doc::props::TextDirection;
|
||||
use wordcraft_layout::DocLayout;
|
||||
|
||||
use super::cursor::{caret_color_at, preedit_cursor_chars};
|
||||
@@ -39,8 +40,13 @@ struct Placement {
|
||||
/// The selection is collapsed, so a caret is drawn.
|
||||
collapsed: bool,
|
||||
/// The face, size and baseline on screen text typed at the caret takes, which a composition is drawn in. `None`
|
||||
/// in an equation, in turned text and without a style run.
|
||||
/// in an equation and without a style run. In turned text the baseline is unused.
|
||||
typing: Option<TypingFace>,
|
||||
/// The direction the caret's line reads in; turned text has a composition running down or up the page.
|
||||
turn: TextDirection,
|
||||
/// Turned text: the caret's top on screen, where the composition starts, and the line's width across the page.
|
||||
y0: f32,
|
||||
across: f32,
|
||||
}
|
||||
|
||||
/// The face text typed at the caret asks for, its size and its baseline on screen (`DocLayout::typing_style`).
|
||||
@@ -76,6 +82,9 @@ fn place(app: &WordApp, doc: &DocLayout, pages: &[PlacedPage]) -> Option<Placeme
|
||||
in_equation: true,
|
||||
collapsed: true,
|
||||
typing: None,
|
||||
turn: TextDirection::Horizontal,
|
||||
y0,
|
||||
across: 0.0,
|
||||
});
|
||||
}
|
||||
let sel = preview_sel(app).unwrap_or(&app.session.sel);
|
||||
@@ -105,15 +114,17 @@ fn place(app: &WordApp, doc: &DocLayout, pages: &[PlacedPage]) -> Option<Placeme
|
||||
} else {
|
||||
[pos2(x.round() + 0.5, y0), pos2(x.round() + 0.5, y1)]
|
||||
};
|
||||
// Word draws a composition in the font and size of the text it goes into; not in turned text, whose lines aren't
|
||||
// horizontal.
|
||||
let typing = if c.width > 0.0 {
|
||||
None
|
||||
} else {
|
||||
doc.typing_style(&sel.focus, app.session.page_hint)
|
||||
.filter(|(run, base)| run.size.is_finite() && run.size > 0.0 && base.is_finite())
|
||||
.map(|(run, base)| TypingFace { face: run.requested_face, size: run.size * scale, baseline: pr.rect.min.y + base * scale })
|
||||
};
|
||||
// Word draws a composition in the font and size of the text it goes into, along the line (turned text: down or up
|
||||
// the page).
|
||||
let turned = c.width > 0.0;
|
||||
let typing = doc
|
||||
.typing_style(&sel.focus, app.session.page_hint)
|
||||
.filter(|(run, base)| run.size.is_finite() && run.size > 0.0 && (turned || base.is_finite()))
|
||||
.map(|(run, base)| TypingFace {
|
||||
face: run.requested_face,
|
||||
size: run.size * scale,
|
||||
baseline: if turned { 0.0 } else { pr.rect.min.y + base * scale },
|
||||
});
|
||||
Some(Placement {
|
||||
bar,
|
||||
page: c.page,
|
||||
@@ -124,6 +135,9 @@ fn place(app: &WordApp, doc: &DocLayout, pages: &[PlacedPage]) -> Option<Placeme
|
||||
in_equation: false,
|
||||
collapsed: sel.is_collapsed(),
|
||||
typing,
|
||||
turn: c.turn,
|
||||
y0,
|
||||
across: c.width * scale,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -204,6 +218,9 @@ pub(crate) fn paint_caret(app: &mut WordApp, ui: &mut Ui, pass: &ToolPass, body:
|
||||
cursor,
|
||||
color: ink(app),
|
||||
typing: pl.typing,
|
||||
turn: pl.turn,
|
||||
y0: pl.y0,
|
||||
across: pl.across,
|
||||
});
|
||||
} else {
|
||||
let look = step(app, request(pl.bar, shown));
|
||||
@@ -224,8 +241,8 @@ pub(crate) fn paint_caret(app: &mut WordApp, ui: &mut Ui, pass: &ToolPass, body:
|
||||
// them an empty rect that clips their strokes away.
|
||||
let out = CaretMarks::new(tl!("Text cursor").to_string(), bars, composition).show_at(ui, pass.rect);
|
||||
// The candidate window sits at the input method's cursor inside the composition, as production placed it.
|
||||
if let Some(x) = out.composition_caret {
|
||||
report = Rect::from_min_max(pos2(x, report.min.y), pos2(x + 1.0, report.max.y));
|
||||
if let Some(r) = out.composition_caret {
|
||||
report = if pl.turn.is_turned() { r } else { Rect::from_min_max(pos2(r.min.x, report.min.y), pos2(r.min.x + 1.0, report.max.y)) };
|
||||
}
|
||||
}
|
||||
// Reported after the marks, which lay the composition out: this pass's final caret for the input method.
|
||||
@@ -245,6 +262,11 @@ struct Composition {
|
||||
color: Color32,
|
||||
/// The face, size and baseline it is drawn in; without them, the interface font from the caret's bottom.
|
||||
typing: Option<TypingFace>,
|
||||
/// The line's direction; turned, the composition's characters stack upright down or up from `y0`, in a column
|
||||
/// `across` wide starting at `left`.
|
||||
turn: TextDirection,
|
||||
y0: f32,
|
||||
across: f32,
|
||||
}
|
||||
|
||||
/// The style of the caret marks: the composition's ink.
|
||||
@@ -265,8 +287,9 @@ pub(crate) struct CaretMarks {
|
||||
common: Common,
|
||||
bars: Vec<(Bar, Color32)>,
|
||||
composition: Option<Composition>,
|
||||
/// Where the paint put the caret inside the composition (screen x), measured from the laid-out text.
|
||||
composition_caret: Option<f32>,
|
||||
/// Where the paint put the caret inside the composition (a bar in screen points; turned text: the rect the
|
||||
/// composition fills), measured from the laid-out text.
|
||||
composition_caret: Option<Rect>,
|
||||
}
|
||||
|
||||
impl CaretMarks {
|
||||
@@ -279,7 +302,7 @@ impl CaretMarks {
|
||||
/// What the marks report: the caret's place inside the composition they drew, for the candidate window.
|
||||
pub(crate) struct CaretMarksOutput {
|
||||
response: Response,
|
||||
composition_caret: Option<f32>,
|
||||
composition_caret: Option<Rect>,
|
||||
}
|
||||
|
||||
impl Outcome for CaretMarksOutput {
|
||||
@@ -330,6 +353,12 @@ impl Component for CaretMarks {
|
||||
}
|
||||
let Some(c) = &self.composition else { return };
|
||||
let color = DataColor::new(c.color);
|
||||
if c.turn.is_turned()
|
||||
&& let Some(t) = &c.typing
|
||||
{
|
||||
self.composition_caret = Some(paint_turned(&draw, c, t, color));
|
||||
return;
|
||||
}
|
||||
let (layout, top, bottom) = match &c.typing {
|
||||
// In the text's own face and size, its first baseline on the line's, as typed text would sit. Characters the
|
||||
// face lacks (CJK in a Latin font) come from the interface fonts; a size past the font atlas is capped.
|
||||
@@ -350,7 +379,7 @@ impl Component for CaretMarks {
|
||||
draw.stroke(&PaintShape::Path(underline), StrokeWidth::Thin, color);
|
||||
let x = (c.left + layout.char_x(c.cursor)).round() + 0.5;
|
||||
draw.stroke(&PaintShape::Path(vec![pos2(x, top), pos2(x, bottom)]), StrokeWidth::Medium, color);
|
||||
self.composition_caret = Some(x);
|
||||
self.composition_caret = Some(Rect::from_min_max(pos2(x, top), pos2(x, bottom)));
|
||||
}
|
||||
|
||||
fn access(&self, access: &mut Access, _: &Cx<'_, Self>) {
|
||||
@@ -361,3 +390,27 @@ impl Component for CaretMarks {
|
||||
CaretMarksOutput { response: cx.response.clone(), composition_caret: self.composition_caret }
|
||||
}
|
||||
}
|
||||
|
||||
/// A composition in turned text: its characters upright, each in a cell of the run's size, stacked from the caret down
|
||||
/// the page (`Down`) or up it (`Up`) in a column across the line, with the underline along the column's left edge and
|
||||
/// the input method's cursor across it. Returns the rect the composition fills, which the candidate window avoids.
|
||||
fn paint_turned(draw: &craft::ui::primitives::Draw<'_>, c: &Composition, t: &TypingFace, color: DataColor) -> Rect {
|
||||
let key = face_key(t.face);
|
||||
let sign = if c.turn == TextDirection::Up { -1.0 } else { 1.0 };
|
||||
// The cell's edge `i` characters from the start.
|
||||
let edge = |i: usize| c.y0 + sign * t.size * i as f32;
|
||||
let n = c.text.chars().count();
|
||||
for (i, ch) in c.text.chars().enumerate() {
|
||||
let mut buf = [0; 4];
|
||||
let s: &str = ch.encode_utf8(&mut buf);
|
||||
// Centred in the column as an em-wide ideograph; the baseline sits at 0.86 em in the cell, where its body lies.
|
||||
let top = edge(i).min(edge(i + 1));
|
||||
let at = pos2(c.left + (c.across - t.size) / 2.0, top + 0.86 * t.size);
|
||||
draw.face_run(at, &FaceRun::new(s, &key, t.size, color), || face_bytes(t.face));
|
||||
}
|
||||
let end = edge(n);
|
||||
draw.stroke(&PaintShape::Path(vec![pos2(c.left, c.y0), pos2(c.left, end)]), StrokeWidth::Thin, color);
|
||||
let at = edge(c.cursor.min(n)).round() + 0.5;
|
||||
draw.stroke(&PaintShape::Path(vec![pos2(c.left, at), pos2(c.left + c.across, at)]), StrokeWidth::Medium, color);
|
||||
Rect::from_two_pos(pos2(c.left, c.y0), pos2(c.left + c.across, end))
|
||||
}
|
||||
|
||||
@@ -2899,3 +2899,26 @@ fn shift_clicking_a_picture_does_not_scroll_the_view() {
|
||||
assert!(picked.contains(&first) && picked.contains(&second), "both pictures are selected: {picked:?} {:?} at {at:?}", h.state().session.sel);
|
||||
assert_eq!(h.state().canvas.view.offset, before, "the view stayed where it was");
|
||||
}
|
||||
|
||||
/// In vertical text the composition runs down the line, upright and at the text's size, and the candidate window
|
||||
/// reports the column it fills, not a horizontal strip (#8980).
|
||||
#[test]
|
||||
fn a_composition_in_vertical_text_runs_down_the_line() {
|
||||
let mut app = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default());
|
||||
app.run("document.setText", json!({"text": "Hello"})).unwrap();
|
||||
app.run("layout.textDirection", json!({"value": "tbRl"})).unwrap();
|
||||
app.run("caret.docEnd", json!({})).unwrap();
|
||||
let mut h = composing_harness(app);
|
||||
kana_test_glyphs(&h.ctx);
|
||||
h.event(egui::Event::Ime(egui::ImeEvent::Preedit { text: "にほん".into(), active_range_chars: None }));
|
||||
for _ in 0..4 {
|
||||
h.app_frame();
|
||||
}
|
||||
let kana: Vec<_> = ["に", "ほ", "ん"].iter().filter_map(|k| drawn_composition(&h, k)).collect();
|
||||
assert_eq!(kana.len(), 3, "each kana is drawn on its own");
|
||||
let at = |i: usize| kana[i].pos;
|
||||
assert!(at(0).y < at(1).y && at(1).y < at(2).y, "the kana stack down the page");
|
||||
assert!((at(0).x - at(2).x).abs() < 1.0, "in one column");
|
||||
let ime = h.output().platform_output.ime.clone().expect("the canvas places the candidate window").cursor_rect;
|
||||
assert!(ime.height() > ime.width(), "the candidate window sits by the column: {ime:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user