From 7f6c410ca56285c6343a692bec7866a2e5f60b34 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 31 May 2023 13:45:12 +1000 Subject: [PATCH] Various changes to I/O handling (#2513) * Store coordinates as ratios of full size * Use single definition for cappedCanvasSize() * Move I/O review code into ts/image-occlusion A bit simpler when it's all in one place. * Reduce number precision, and round to whole pixels >>> n=10000 >>> for i in range(1, int(n)): assert i == round(float("%0.4f" % (i/n))*n) * Minor typing tweak So, it turns out that typing is mostly broken in ts/image-occlusion. We're importing from fabric which is a js file without types, so types like fabric.Canvas are resolving to any. I first tried switching to `@types/fabric`, which introduced a slew of typing errors. Wasted a few hours trying to address them, before deciding to give up on it, since the types were not complete. Then found fabric has a 6.0 beta that introduces typing, and spent some time with that, but ran into some new issues as it still seems to be a work in progress. I think we're probably best off waiting until it's out and stabilized before sinking more effort into this. * Refactor (de)serialization of occlusions To make the code easier to follow/maintain, cloze deletions are now decoded/ encoded into simple data classes, which can then be converted to Fabric objects and back. The data objects handle converting from absolute/normal positions, and producing values suitable for writing to text (eg truncated floats). Various other changes: - Polygon points are now stored as 'x,y x2,y2 ...' instead of JSON in cloze divs, as that makes the handling consistent with reading from cloze deletion text. - Fixed the reviewer not showing updated placement when a polygon was moved. - Disabled rotation controls in the editor, since we don't support rotation during review. - Renamed hideInactive to occludeInactive, as it wasn't clear whether the former meant to hide the occlusions, or keep them (hiding the content). It's stored as 'oi=1' in the cloze text. * Increase canvas size limit, and double pixels when required. * Size canvas based on container size This results in sharper masks when the intrinsic image size is smaller than the container, and more legible ones when the container is smaller than the intrinsic image size. By using the container instead of the viewport, we account for margins, and when the pixel ratio is 1x, the canvas size and container size should match. * Disable zoom animation on editor load * Default to rectangle when adding new occlusions * Allow users to add/update notes directly from mask editing page * The mask editor needs to work with css pixels, not actual pixels The canvas and image were being scaled too large, which impacted performance. --- build/configure/src/web.rs | 2 +- rslib/src/image_occlusion/imageocclusion.rs | 21 +-- ts/image-occlusion/ImageOcclusionPage.svelte | 9 +- ts/image-occlusion/MaskEditor.svelte | 7 +- ts/image-occlusion/SideToolbar.svelte | 10 +- ts/image-occlusion/add-or-update-note.ts | 70 ++++++++ ts/image-occlusion/canvas-scale.ts | 48 +++++ ts/image-occlusion/generate.ts | 167 ------------------ ts/image-occlusion/mask-editor.ts | 35 ++-- .../review.ts} | 161 ++++++++--------- ts/image-occlusion/shapes/base.ts | 69 ++++++++ ts/image-occlusion/shapes/ellipse.ts | 51 ++++++ ts/image-occlusion/shapes/floats.ts | 10 ++ ts/image-occlusion/shapes/from-cloze.ts | 136 ++++++++++++++ ts/image-occlusion/shapes/polygon.ts | 62 +++++++ ts/image-occlusion/shapes/position.ts | 24 +++ ts/image-occlusion/shapes/rectangle.ts | 51 ++++++ ts/image-occlusion/shapes/to-cloze.ts | 106 +++++++++++ ts/image-occlusion/tools/add-from-cloze.ts | 30 ++++ ts/image-occlusion/tools/lib.ts | 14 +- ts/image-occlusion/tools/shape-generate.ts | 131 -------------- ts/image-occlusion/tools/tool-ellipse.ts | 7 +- ts/image-occlusion/tools/tool-polygon.ts | 11 +- ts/image-occlusion/tools/tool-rect.ts | 7 +- ts/image-occlusion/tsconfig.json | 1 + ts/image-occlusion/types.ts | 11 ++ ts/reviewer/index.ts | 2 +- ts/reviewer/reviewer_extras.ts | 7 +- 28 files changed, 809 insertions(+), 451 deletions(-) create mode 100644 ts/image-occlusion/add-or-update-note.ts create mode 100644 ts/image-occlusion/canvas-scale.ts delete mode 100644 ts/image-occlusion/generate.ts rename ts/{reviewer/image_occlusion.ts => image-occlusion/review.ts} (55%) create mode 100644 ts/image-occlusion/shapes/base.ts create mode 100644 ts/image-occlusion/shapes/ellipse.ts create mode 100644 ts/image-occlusion/shapes/floats.ts create mode 100644 ts/image-occlusion/shapes/from-cloze.ts create mode 100644 ts/image-occlusion/shapes/polygon.ts create mode 100644 ts/image-occlusion/shapes/position.ts create mode 100644 ts/image-occlusion/shapes/rectangle.ts create mode 100644 ts/image-occlusion/shapes/to-cloze.ts create mode 100644 ts/image-occlusion/tools/add-from-cloze.ts delete mode 100644 ts/image-occlusion/tools/shape-generate.ts create mode 100644 ts/image-occlusion/types.ts diff --git a/build/configure/src/web.rs b/build/configure/src/web.rs index 52e096177..755dc405c 100644 --- a/build/configure/src/web.rs +++ b/build/configure/src/web.rs @@ -394,7 +394,7 @@ fn build_and_check_editor(build: &mut Build) -> Result<()> { } fn build_and_check_reviewer(build: &mut Build) -> Result<()> { - let reviewer_deps = inputs![":ts:lib", glob!("ts/reviewer/**"),]; + let reviewer_deps = inputs![":ts:lib", glob!("ts/{reviewer,image-occlusion}/**"),]; build.add( "ts:reviewer:reviewer.js", EsbuildScript { diff --git a/rslib/src/image_occlusion/imageocclusion.rs b/rslib/src/image_occlusion/imageocclusion.rs index f2c3b41ee..c1e7da551 100644 --- a/rslib/src/image_occlusion/imageocclusion.rs +++ b/rslib/src/image_occlusion/imageocclusion.rs @@ -3,10 +3,11 @@ use std::fmt::Write; -// split following -// text = "rect:399.01,99.52,167.09,33.78:fill=#0a2cee:stroke=1" -// with -// result = "data-shape="rect" data-left="399.01" data-top="99.52" data-width="167.09" data-height="33.78" data-fill="\#0a2cee" data-stroke="1"" +// convert text like +// rect:left=.2325:top=.3261:width=.202:height=.0975 +// to something like +// result = "data-shape="rect" data-left="399.01" data-top="99.52" +// data-width="167.09" data-height="33.78" pub fn get_image_cloze_data(text: &str) -> String { let mut result = String::new(); let parts: Vec<&str> = text.split(':').collect(); @@ -57,18 +58,18 @@ pub fn get_image_cloze_data(text: &str) -> String { let mut point_str = String::new(); for point_pair in values[1].split(' ') { let Some((x, y)) = point_pair.split_once(',') else { continue }; - write!(&mut point_str, "[{},{}],", x, y).unwrap(); + write!(&mut point_str, "{},{} ", x, y).unwrap(); } - // remove the trailing comma + // remove the trailing space point_str.pop(); if !point_str.is_empty() { - result.push_str(&format!("data-points=\"[{}]\" ", point_str)); + result.push_str(&format!("data-points=\"{point_str}\" ")); } } } - "hideinactive" => { + "oi" => { if !values[1].is_empty() { - result.push_str(&format!("data-hideinactive=\"{}\" ", values[1])); + result.push_str(&format!("data-occludeInactive=\"{}\" ", values[1])); } } _ => {} @@ -102,6 +103,6 @@ fn test_get_image_cloze_data() { ); assert_eq!( get_image_cloze_data("polygon:points=0,0 10,10 20,0"), - r#"data-shape="polygon" data-points="[[0,0],[10,10],[20,0]]" "#, + r#"data-shape="polygon" data-points="0,0 10,10 20,0" "#, ); } diff --git a/ts/image-occlusion/ImageOcclusionPage.svelte b/ts/image-occlusion/ImageOcclusionPage.svelte index 93ed9c20a..21a26a938 100644 --- a/ts/image-occlusion/ImageOcclusionPage.svelte +++ b/ts/image-occlusion/ImageOcclusionPage.svelte @@ -6,7 +6,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import * as tr from "@tslib/ftl"; import Container from "../components/Container.svelte"; - import { addOrUpdateNote } from "./generate"; + import { addOrUpdateNote } from "./add-or-update-note"; import type { IOMode } from "./lib"; import MasksEditor from "./MaskEditor.svelte"; import Notes from "./Notes.svelte"; @@ -15,11 +15,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html export let mode: IOMode; async function hideAllGuessOne(): Promise { - addOrUpdateNote(mode, false); + addOrUpdateNote(mode, true); } async function hideOneGuessOne(): Promise { - addOrUpdateNote(mode, true); + addOrUpdateNote(mode, false); } const items = [ @@ -50,8 +50,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +