Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Type: files with Auto alignment open in older builds (review of #300)

bflatastic3121bba275a7
diff --git a/crates/doc/src/text.rs b/crates/doc/src/text.rs
index cc2f468b4..d1a6a0c14 100644
--- a/crates/doc/src/text.rs
+++ b/crates/doc/src/text.rs
@@ -268,40 +268,122 @@ fn default_align_on() -> char {
pub const DEFAULT_TAB_INTERVAL: f64 = 36.0;
/// Paragraph attributes (the Paragraph panel).
+///
+/// Saved through [`ParaStyleFile`], which keeps files with text openable by builds from before
+/// [`Justify::Auto`].
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
+#[serde(from = "ParaStyleFile", into = "ParaStyleFile")]
pub struct ParaStyle {
- #[serde(default)]
pub justify: Justify,
- #[serde(default)]
pub left_indent: f64,
- #[serde(default)]
pub right_indent: f64,
- #[serde(default)]
pub first_line_indent: f64,
- #[serde(default)]
pub space_before: f64,
- #[serde(default)]
pub space_after: f64,
- #[serde(default)]
pub hyphenate: bool,
/// Tab stops (Tabs panel), sorted by position.
- #[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tabs: Vec<TabStop>,
/// Paragraph style (Paragraph Styles panel) these attributes come from; None = Normal.
- #[serde(default, skip_serializing_if = "Option::is_none")]
pub style_name: Option<String>,
/// Japanese composition: the spacing of punctuation (Paragraph panel › Mojikumi). Type made
/// with the Type tools and `text.create` takes [`Mojikumi::LineEndHalf`]; documents from before
/// it and imported text (already set) keep [`Mojikumi::None`].
- #[serde(default, skip_serializing_if = "Mojikumi::is_none")]
pub mojikumi: Mojikumi,
/// Paragraph direction (Paragraph panel): the base direction of each paragraph for
/// bidirectional text (UAX #9). None: from each paragraph's first strong character (Hebrew or
/// Arabic: right to left).
- #[serde(default, skip_serializing_if = "Option::is_none")]
pub direction: Option<ParaDirection>,
}
+/// [`ParaStyle`] as saved. [`Justify::Auto`] is written as the alignment it has in the paragraph
+/// direction (`Right` for right to left, else `Left`) with `justify_auto` set: builds without Auto
+/// read the alignment and ignore the flag, and builds with it read Auto back.
+#[derive(Serialize, Deserialize)]
+struct ParaStyleFile {
+ #[serde(default)]
+ justify: Justify,
+ #[serde(default, skip_serializing_if = "crate::skip::is_default")]
+ justify_auto: bool,
+ #[serde(default)]
+ left_indent: f64,
+ #[serde(default)]
+ right_indent: f64,
+ #[serde(default)]
+ first_line_indent: f64,
+ #[serde(default)]
+ space_before: f64,
+ #[serde(default)]
+ space_after: f64,
+ #[serde(default)]
+ hyphenate: bool,
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ tabs: Vec<TabStop>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ style_name: Option<String>,
+ #[serde(default, skip_serializing_if = "Mojikumi::is_none")]
+ mojikumi: Mojikumi,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ direction: Option<ParaDirection>,
+}
+
+impl From<ParaStyle> for ParaStyleFile {
+ fn from(p: ParaStyle) -> Self {
+ let justify_auto = p.justify == Justify::Auto;
+ let justify = match p.justify {
+ Justify::Auto if p.direction == Some(ParaDirection::RightToLeft) => Justify::Right,
+ Justify::Auto => Justify::Left,
+ j => j,
+ };
+ let ParaStyle {
+ left_indent,
+ right_indent,
+ first_line_indent,
+ space_before,
+ space_after,
+ hyphenate,
+ tabs,
+ style_name,
+ mojikumi,
+ direction,
+ ..
+ } = p;
+ Self {
+ justify,
+ justify_auto,
+ left_indent,
+ right_indent,
+ first_line_indent,
+ space_before,
+ space_after,
+ hyphenate,
+ tabs,
+ style_name,
+ mojikumi,
+ direction,
+ }
+ }
+}
+
+impl From<ParaStyleFile> for ParaStyle {
+ fn from(f: ParaStyleFile) -> Self {
+ let justify = if f.justify_auto { Justify::Auto } else { f.justify };
+ let ParaStyleFile {
+ left_indent,
+ right_indent,
+ first_line_indent,
+ space_before,
+ space_after,
+ hyphenate,
+ tabs,
+ style_name,
+ mojikumi,
+ direction,
+ ..
+ } = f;
+ Self { justify, left_indent, right_indent, first_line_indent, space_before, space_after, hyphenate, tabs, style_name, mojikumi, direction }
+ }
+}
+
/// A paragraph's base direction ([`ParaStyle::direction`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -634,4 +716,45 @@ mod tests {
assert!(!t.move_area_anchors(&[(3, 0)], Vec2::new(5.0, 5.0)));
assert_eq!(t, before);
}
+
+ /// Builds from before [`Justify::Auto`] read new type's alignment as Left or Right (and ignore
+ /// `justify_auto`); this build reads Auto back.
+ #[test]
+ fn auto_alignment_saves_readable_by_older_builds() {
+ #[derive(Deserialize, Debug, PartialEq)]
+ enum OldJustify {
+ Left,
+ Center,
+ Right,
+ JustifyLeft,
+ JustifyCenter,
+ JustifyRight,
+ JustifyAll,
+ }
+ #[derive(Deserialize)]
+ struct OldPara {
+ justify: OldJustify,
+ }
+ #[derive(Deserialize)]
+ struct OldText {
+ para: OldPara,
+ }
+ for (direction, physical) in
+ [(Some(ParaDirection::RightToLeft), OldJustify::Right), (Some(ParaDirection::LeftToRight), OldJustify::Left), (None, OldJustify::Left)]
+ {
+ let mut t = TextObject::point(Point::ZERO, "שלום", CharStyle::default());
+ t.para = ParaStyle { justify: Justify::Auto, direction, ..Default::default() };
+ let json = serde_json::to_string(&t).unwrap();
+ let old: OldText = serde_json::from_str(&json).unwrap();
+ assert_eq!(old.para.justify, physical, "{json}");
+ assert_eq!(serde_json::from_str::<TextObject>(&json).unwrap(), t);
+ }
+ // Other alignments save as before, without the flag.
+ let p = ParaStyle { justify: Justify::Right, ..Default::default() };
+ let json = serde_json::to_string(&p).unwrap();
+ assert!(!json.contains("justify_auto"), "{json}");
+ assert_eq!(serde_json::from_str::<ParaStyle>(&json).unwrap(), p);
+ // Files from before the flag keep their alignment.
+ assert_eq!(serde_json::from_str::<ParaStyle>(r#"{"justify":"Center"}"#).unwrap().justify, Justify::Center);
+ }
}
diff --git a/crates/engine/src/cmd/textstyles.rs b/crates/engine/src/cmd/textstyles.rs
index eef862d95..f11204a82 100644
--- a/crates/engine/src/cmd/textstyles.rs
+++ b/crates/engine/src/cmd/textstyles.rs
@@ -115,12 +115,20 @@ pub fn specs() -> Vec<CommandSpec> {
// ---------- attribute plumbing ----------
+/// `v` as a JSON map with one key per attribute: Auto alignment, which files save as its
+/// direction's alignment plus `justify_auto` (see `vectorcraft_doc::ParaStyle`), is `"justify":
+/// "Auto"`, so a style's `justify` replaces it.
+fn to_map<T: Serialize>(v: &T) -> Option<Map<String, Value>> {
+ let Ok(Value::Object(mut m)) = serde_json::to_value(v) else { return None };
+ if m.remove("justify_auto") == Some(Value::Bool(true)) {
+ m.insert("justify".into(), Value::from("Auto"));
+ }
+ Some(m)
+}
+
/// A style's attributes as a JSON map, without the style name.
fn to_attrs<T: Serialize>(v: &T) -> Map<String, Value> {
- let mut m = match serde_json::to_value(v) {
- Ok(Value::Object(m)) => m,
- _ => Map::new(),
- };
+ let mut m = to_map(v).unwrap_or_default();
m.remove("style_name");
m
}
@@ -128,7 +136,7 @@ fn to_attrs<T: Serialize>(v: &T) -> Map<String, Value> {
/// `base` with `attrs` written over it (unknown or ill-typed attributes are an error).
fn with_attrs<T: Serialize + DeserializeOwned>(base: &T, attrs: &Map<String, Value>, cmd: &str) -> Result<T> {
let mut m = to_attrs(base);
- if let Ok(Value::Object(full)) = serde_json::to_value(base) {
+ if let Some(full) = to_map(base) {
m.insert("style_name".into(), full.get("style_name").cloned().unwrap_or(Value::Null));
}
for (k, v) in attrs {
@@ -144,7 +152,7 @@ fn with_attrs<T: Serialize + DeserializeOwned>(base: &T, attrs: &Map<String, Val
/// value (or not set by it) take the new value; overrides stay. Unchanged if the style doesn't
/// round-trip through JSON.
fn restyle<T: Serialize + DeserializeOwned + Clone>(cur: &T, old: &Map<String, Value>, new: &Map<String, Value>) -> T {
- let Ok(Value::Object(mut m)) = serde_json::to_value(cur) else { return cur.clone() };
+ let Some(mut m) = to_map(cur) else { return cur.clone() };
for (k, v) in new {
if old.get(k).is_none_or(|o| m.get(k) == Some(o)) {
m.insert(k.clone(), v.clone());
diff --git a/crates/engine/src/tests_textedit.rs b/crates/engine/src/tests_textedit.rs
index 18602689c..2b1a2f4a5 100644
--- a/crates/engine/src/tests_textedit.rs
+++ b/crates/engine/src/tests_textedit.rs
@@ -550,6 +550,7 @@ fn automatic_alignment_updates_during_edits_and_can_be_overridden() {
assert_eq!(restored.para.justify, vectorcraft_doc::Justify::Auto);
// Legacy documents with explicit alignment retain that choice.
let mut legacy = serde_json::to_value(t).unwrap();
+ legacy["para"].as_object_mut().unwrap().remove("justify_auto");
legacy["para"]["justify"] = json!("Left");
assert_eq!(serde_json::from_value::<TextObject>(legacy).unwrap().para.justify, vectorcraft_doc::Justify::Left);
}