mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-02 10:47:02 -06:00
fix(equation): draw symbol and structure pictures without the checkerboard, symbols at a readable size (#7975)
Pictures are alpha ink masks drawn without the transparency grid. The symbol strip requests its picture from the tile's short side (the tiles are 24 x 52 pt and the picture is fitted), and sets symbols at 22 pt, so they read at about 16 pt. Partial: the Tools labels (Unicode, LaTeX, Normal Text) are still icon-only at 1440, as the ribbon fit keeps Tools at the compact level. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
98e71c1478
commit
3b77539db2
@@ -38,6 +38,7 @@ pub struct RecentSymbol {
|
||||
}
|
||||
|
||||
/// Characters per row of the Symbols map (Word's grid is 16 wide).
|
||||
// lean: the cap assumes this many columns; read the laid-out count if the map becomes resizable.
|
||||
const GRID_COLUMNS: usize = 16;
|
||||
/// Rows of the Symbols map shown before it scrolls.
|
||||
const GRID_ROWS: usize = 8;
|
||||
|
||||
@@ -21,7 +21,7 @@ use wordcraft_doc::math::{MNode, MRun, parse_linear};
|
||||
use wordcraft_engine::math_gallery::{BUILT_INS, STRUCTURES, SYMBOL_SETS};
|
||||
|
||||
use crate::WordApp;
|
||||
use crate::previews::{MathSource, math_key, math_picture, math_thumbnails};
|
||||
use crate::previews::{MathSource, math_key, math_painter, math_picture, math_thumbnails};
|
||||
use crate::ribbon::common::menu_button;
|
||||
|
||||
/// What each structure button shows.
|
||||
@@ -154,10 +154,14 @@ fn math_menu_button(ui: &mut Ui, picture: &str, label: &str, body: impl FnMut(&m
|
||||
let _ = menu_button(ui, face, tl!(label).to_string(), None, body);
|
||||
}
|
||||
|
||||
/// The size a symbol is set at. The strip's tiles are 24 pt wide and the picture is fitted to them, so it reads at
|
||||
/// about 16 pt (11 pt drew a speck).
|
||||
const SYMBOL_PT: f32 = 22.0;
|
||||
|
||||
/// The picture source of one symbol, as a literal run.
|
||||
fn symbol_source(c: char, ink: Color32) -> MathSource {
|
||||
pub(crate) fn symbol_source(c: char, ink: Color32) -> MathSource {
|
||||
let nodes = vec![MNode::Run(MRun { text: c.to_string(), lit: true, ..Default::default() })];
|
||||
MathSource::new(nodes, false, 11.0, [1, 1], ink)
|
||||
MathSource::new(nodes, false, SYMBOL_PT, [1, 1], ink).fitted()
|
||||
}
|
||||
|
||||
/// A grid of rendered math tiles, `(name, picture key)` each over its own source, shaped as the sources are;
|
||||
@@ -221,13 +225,14 @@ fn symbols(ui: &mut Ui, app: &mut WordApp) {
|
||||
let ink = ink(ui);
|
||||
let thumbs = math_thumbnails(ui.ctx());
|
||||
let sources: Vec<MathSource> = all.iter().map(|c| symbol_source(*c, ink)).collect();
|
||||
let painters: Vec<_> = all.iter().zip(&sources).map(|(c, source)| math_painter(math_key(&format!("sym:{c}")), &thumbs, source)).collect();
|
||||
let items: Vec<GalleryItem<'_, char>> = all
|
||||
.iter()
|
||||
.zip(&sources)
|
||||
.map(|(c, source)| GalleryItem {
|
||||
.zip(&painters)
|
||||
.map(|(c, paint)| GalleryItem {
|
||||
key: *c,
|
||||
label: Label::from(symbol_tooltip(*c, &math_placeholder(ui, &c.to_string()))),
|
||||
face: GalleryFace::rendered(math_key(&format!("sym:{c}")), &thumbs, source),
|
||||
face: GalleryFace::Paint(paint),
|
||||
})
|
||||
.collect();
|
||||
let mut sets = |ui: &mut Ui| {
|
||||
|
||||
@@ -10,7 +10,10 @@ use std::sync::Arc;
|
||||
use craft::theme::Mode;
|
||||
use craft::ui::display::Picture;
|
||||
use craft::ui::list::ThumbAspect;
|
||||
use craft::ui::primitives::textures::{LoadedPicture, Revision, ThumbRender, ThumbRequest, ThumbSource, ThumbnailOptions, Thumbnails};
|
||||
use craft::ui::primitives::ContentPainterCx;
|
||||
use craft::ui::primitives::textures::{
|
||||
LoadedPicture, Priority, Revision, ThumbPaint, ThumbRender, ThumbRequest, ThumbSource, ThumbnailOptions, Thumbnails,
|
||||
};
|
||||
use craft::ui::runtime::env::Label;
|
||||
use egui::{Color32, ColorImage, Vec2};
|
||||
use wordcraft_doc::props::CharProps;
|
||||
@@ -163,19 +166,37 @@ pub(crate) fn dim(img: &mut ColorImage) {
|
||||
|
||||
/// Renders an equation off the frame for [`math_picture`]: the inputs of one tile, owned so the render job
|
||||
/// can run on a queue lane.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct MathSource {
|
||||
nodes: Arc<wordcraft_doc::math::Arg>,
|
||||
display: bool,
|
||||
pt: f32,
|
||||
aspect: [u16; 2],
|
||||
ink: Color32,
|
||||
/// Shrink `pt` until the ink clears the picture's edges (symbols of any width, in any font).
|
||||
fit: bool,
|
||||
}
|
||||
|
||||
impl MathSource {
|
||||
/// An equation of `nodes` (`display` style, font size `pt`) for a picture `aspect[0]` wide to `aspect[1]` high,
|
||||
/// inked `ink`. The picture is an ink mask baked in `ink`, so the theme's text colour is part of its revision.
|
||||
pub(crate) fn new(nodes: wordcraft_doc::math::Arg, display: bool, pt: f32, aspect: [u16; 2], ink: Color32) -> Self {
|
||||
MathSource { nodes: Arc::new(nodes), display, pt, aspect, ink }
|
||||
MathSource { nodes: Arc::new(nodes), display, pt, aspect, ink, fit: false }
|
||||
}
|
||||
|
||||
/// This source, its size cut down where its ink would reach the picture's edge.
|
||||
pub(crate) fn fitted(mut self) -> Self {
|
||||
self.fit = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// The picture for a `w`×`h` pt box at `ppp`, fitted when asked.
|
||||
pub(crate) fn image(&self, w: f32, h: f32, ppp: f32) -> Option<ColorImage> {
|
||||
if self.fit {
|
||||
math_image_fitted(&self.nodes, self.display, self.pt, w, h, ppp)
|
||||
} else {
|
||||
math_image(&self.nodes, self.display, self.pt, w, h, ppp)
|
||||
}
|
||||
}
|
||||
|
||||
/// The shape the tile or button shows the picture in.
|
||||
@@ -199,10 +220,9 @@ impl ThumbSource<u64> for MathSource {
|
||||
}
|
||||
|
||||
fn render(&self, _key: &u64, request: &ThumbRequest) -> ThumbRender {
|
||||
let (nodes, display, pt, size, ink, ppp) =
|
||||
(Arc::clone(&self.nodes), self.display, self.pt, self.size_for(request), self.ink, request.pixels_per_point);
|
||||
let (source, size, ink, ppp) = (self.clone(), self.size_for(request), self.ink, request.pixels_per_point);
|
||||
ThumbRender::Later(Box::new(move |_| {
|
||||
Ok(match math_image(&nodes, display, pt, size.x, size.y, ppp) {
|
||||
Ok(match source.image(size.x, size.y, ppp) {
|
||||
Some(mut img) => {
|
||||
for p in &mut img.pixels {
|
||||
*p = Color32::from_rgba_unmultiplied(ink.r(), ink.g(), ink.b(), p.a());
|
||||
@@ -225,9 +245,47 @@ pub(crate) fn math_thumbnails(ctx: &egui::Context) -> Thumbnails<u64> {
|
||||
Thumbnails::in_context(ctx, "wordcraft.math", ThumbnailOptions::new())
|
||||
}
|
||||
|
||||
/// The picture request for a tile `rect`: sized by its short side. The picture is fitted into the rect, so a
|
||||
/// request from the long side of a tall tile (24 x 52 pt in the ribbon) shrinks the symbol to a speck.
|
||||
pub(crate) fn cell_request(rect: egui::Rect, pixels_per_point: f32) -> ThumbRequest {
|
||||
ThumbRequest::for_cell(rect.width().min(rect.height()), pixels_per_point)
|
||||
}
|
||||
|
||||
/// Paints the equation picture `key` in a rect, asked for at the size it is painted, for a `GalleryFace::Paint`. The
|
||||
/// picture is an ink mask, so it is drawn without the transparency grid; the store's placeholder shows until the render
|
||||
/// lands. lean: only `GalleryFace::rendered` lacks `Picture`'s `checker(false)`; drop this once it has one.
|
||||
pub(crate) fn math_painter<'a>(key: u64, thumbnails: &'a Thumbnails<u64>, source: &'a MathSource) -> impl Fn(&ContentPainterCx, egui::Rect) + 'a {
|
||||
move |cx, rect| {
|
||||
let request = cell_request(rect, cx.pixels_per_point());
|
||||
let state = thumbnails.state(cx.painter.ctx(), source, &key, &request, Priority::VISIBLE);
|
||||
thumbnails.paint(cx, rect, &state, &ThumbPaint::default().checker(false));
|
||||
}
|
||||
}
|
||||
|
||||
/// An equation picture that shows the framework's placeholder until the render lands.
|
||||
pub(crate) fn math_picture<'a>(label: impl Into<Label>, key: u64, thumbnails: &'a Thumbnails<u64>, source: &'a MathSource) -> Picture<'a> {
|
||||
Picture::rendered(label, key, thumbnails, source)
|
||||
Picture::rendered(label, key, thumbnails, source).checker(false)
|
||||
}
|
||||
|
||||
/// [`math_image`], redrawn smaller while the ink reaches within a margin of the picture's edge, so a glyph
|
||||
/// wider than the tile in this platform's font is shrunk to fit and narrow ones keep `pt`. Measured, not assumed.
|
||||
fn math_image_fitted(nodes: &wordcraft_doc::math::Arg, display: bool, pt: f32, w: f32, h: f32, ppp: f32) -> Option<ColorImage> {
|
||||
let mut pt = pt;
|
||||
let mut img = math_image(nodes, display, pt, w, h, ppp)?;
|
||||
for _ in 0..4 {
|
||||
let [iw, ih] = img.size;
|
||||
let margin = (iw / 16).max(2);
|
||||
let cols: Vec<usize> = (0..iw).filter(|x| (0..ih).any(|y| img.pixels[y * iw + x].a() > 64)).collect();
|
||||
let (Some(lo), Some(hi)) = (cols.first().copied(), cols.last().copied()) else { break };
|
||||
if lo >= margin && hi + margin < iw {
|
||||
break;
|
||||
}
|
||||
// Clipped ink under-measures, so always step down by at least a tenth.
|
||||
let room = iw.saturating_sub(2 * margin) as f32 / (hi - lo + 1) as f32;
|
||||
pt *= room.min(0.9);
|
||||
img = math_image(nodes, display, pt, w, h, ppp)?;
|
||||
}
|
||||
Some(img)
|
||||
}
|
||||
|
||||
/// An equation rasterised for a tile of `w`×`h` points, as an ink mask.
|
||||
|
||||
@@ -53,3 +53,23 @@ fn font_sample_shows_small_caps_and_the_script_mark() {
|
||||
assert_ne!(draw(&plain, None, false), draw(&plain, sup, false), "the mark is drawn");
|
||||
assert_ne!(draw(&plain, None, false), draw(&plain, None, true), "Switch Modes transforms the preview");
|
||||
}
|
||||
|
||||
/// #7975: a symbol in the Equation tab's strip reads in its tile, not as a speck. The real tile is 24 x 52 pt and the
|
||||
/// picture is fitted into it, so the ink as shown spans a third of the tile's width; wide arrows and operators keep
|
||||
/// their whole ink inside the picture (no clipping).
|
||||
#[test]
|
||||
fn symbol_thumbnails_read_in_the_strips_real_tiles() {
|
||||
let tile = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(24.0, 52.0));
|
||||
for c in ['=', '+', '\u{221E}', '\u{00B1}', '\u{27F9}', '\u{27FA}', '\u{27F7}', '\u{2230}'] {
|
||||
let source = crate::equation_tab::symbol_source(c, Color32::WHITE);
|
||||
let size = source.size_for(&cell_request(tile, 1.0));
|
||||
let img = source.image(size.x, size.y, 1.0).expect("renders");
|
||||
let [w, h] = img.size;
|
||||
let inked: Vec<(usize, usize)> = (0..w * h).filter(|i| img.pixels[*i].a() > 64).map(|i| (i % w, i / w)).collect();
|
||||
let (lo, hi) = (inked.iter().map(|p| p.0).min().unwrap_or(0), inked.iter().map(|p| p.0).max().unwrap_or(0));
|
||||
let fit = (tile.width() / w as f32).min(tile.height() / h as f32);
|
||||
let shown = (hi - lo + 1) as f32 * fit;
|
||||
assert!(shown * 3.0 >= tile.width(), "{c} is a speck: {shown} pt wide in a {} pt tile", tile.width());
|
||||
assert!(lo > 0 && hi + 1 < w, "{c} touches the picture's edge ({lo}..{hi} of {w})");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user