From c828a2eb6f1b69ea62780ea100003b81c137585f Mon Sep 17 00:00:00 2001 From: Aristotelis <5459332+glutanimate@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:36:46 +0200 Subject: [PATCH] Add APIs for IO card rendering (#2739) * Refactor: Add index to shapes package * Add shape draw callback API to setupImageCloze * Expose IO drawing API, switch away from image cloze naming We currently use "image occlusion" in most places, but some references to "image cloze" still remain. For consistency's sake and to make it easier to quickly find IO-related code, this commit replaces all remaining references to "image cloze", only maintaining those required for backwards compatibility with existing note types. * Add cloze ordinal to shapes * Do not mutate original shapes during (de)normalization Mutating shapes would be a recipe for trouble when combined with IO API use by external consumers. (makeNormal(makeAbsolute(makeNormal())) is not idempotent, and keeping track of the original state would introduce additional complexity with no discernible performance benefit or otherwise.) * Tweak IO API, allowing modifications to ShapeProperties * Tweak drawShape parameters * Switch method order For consistency with previous implementation * Run Rust formatters * Simplify position (de)normalization --------- Co-authored-by: Glutanimate --- proto/anki/image_occlusion.proto | 4 +- rslib/src/image_occlusion/imagedata.rs | 9 +- rslib/src/image_occlusion/notetype.rs | 2 +- ts/image-occlusion/review.ts | 139 ++++++++++++++++-------- ts/image-occlusion/shapes/base.ts | 40 +++++-- ts/image-occlusion/shapes/ellipse.ts | 26 +++-- ts/image-occlusion/shapes/from-cloze.ts | 1 + ts/image-occlusion/shapes/index.ts | 9 ++ ts/image-occlusion/shapes/polygon.ts | 34 ++++-- ts/image-occlusion/shapes/rectangle.ts | 26 +++-- ts/image-occlusion/shapes/text.ts | 28 +++-- ts/image-occlusion/shapes/to-cloze.ts | 3 +- ts/reviewer/index.ts | 5 +- ts/reviewer/reviewer_extras.ts | 7 +- 14 files changed, 228 insertions(+), 105 deletions(-) create mode 100644 ts/image-occlusion/shapes/index.ts diff --git a/proto/anki/image_occlusion.proto b/proto/anki/image_occlusion.proto index 82f7d38ce..79ea2fe9f 100644 --- a/proto/anki/image_occlusion.proto +++ b/proto/anki/image_occlusion.proto @@ -64,7 +64,7 @@ message GetImageOcclusionNoteResponse { repeated ImageOcclusionProperty properties = 2; } - message ImageClozeNote { + message ImageOcclusionNote { bytes image_data = 1; repeated ImageOcclusion occlusions = 2; string header = 3; @@ -73,7 +73,7 @@ message GetImageOcclusionNoteResponse { } oneof value { - ImageClozeNote note = 1; + ImageOcclusionNote note = 1; string error = 2; } } diff --git a/rslib/src/image_occlusion/imagedata.rs b/rslib/src/image_occlusion/imagedata.rs index 3ec3a49ee..17613344c 100644 --- a/rslib/src/image_occlusion/imagedata.rs +++ b/rslib/src/image_occlusion/imagedata.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use anki_io::metadata; use anki_io::read_file; -use anki_proto::image_occlusion::get_image_occlusion_note_response::ImageClozeNote; +use anki_proto::image_occlusion::get_image_occlusion_note_response::ImageOcclusionNote; use anki_proto::image_occlusion::get_image_occlusion_note_response::Value; use anki_proto::image_occlusion::AddImageOcclusionNoteRequest; use anki_proto::image_occlusion::GetImageForOcclusionResponse; @@ -82,9 +82,12 @@ impl Collection { Ok(GetImageOcclusionNoteResponse { value: Some(value) }) } - pub fn get_image_occlusion_note_inner(&mut self, note_id: NoteId) -> Result { + pub fn get_image_occlusion_note_inner( + &mut self, + note_id: NoteId, + ) -> Result { let note = self.storage.get_note(note_id)?.or_not_found(note_id)?; - let mut cloze_note = ImageClozeNote::default(); + let mut cloze_note = ImageOcclusionNote::default(); let fields = note.fields(); diff --git a/rslib/src/image_occlusion/notetype.rs b/rslib/src/image_occlusion/notetype.rs index 0f94c5e6d..488a29cfd 100644 --- a/rslib/src/image_occlusion/notetype.rs +++ b/rslib/src/image_occlusion/notetype.rs @@ -99,7 +99,7 @@ pub(crate) fn image_occlusion_notetype(tr: &I18n) -> Notetype {