Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #343 update: tab lists checked through paragraph attributes and styles

aiongg03697c53f2c2
diff --cc crates/doc/src/attrs.rs
index 61a9f19a8,3a9eae0a2..937d43452
--- a/crates/doc/src/attrs.rs
+++ b/crates/doc/src/attrs.rs
@@@ -572,9 -530,60 +572,63 @@@ pub struct TabStop
pub align_on: String,
}
+/// The colour of a paragraph rule that follows the paragraph's text colour.
+pub const TEXT_COLOR: &str = "Text Color";
+
+ /// Most stops a paragraph holds.
+ pub const MAX_TAB_STOPS: usize = 100;
+ /// Furthest stop position: 216 in, the largest page.
+ pub const MAX_TAB_POSITION: f64 = 15_552.0;
+ /// Longest leader, in characters.
+ pub const MAX_TAB_LEADER: usize = 8;
+ /// Stops closer than this share a position.
+ pub const SAME_TAB_POSITION: f64 = 0.01;
+
+ impl TabStop {
+ /// A finite position clamped to `0..=MAX_TAB_POSITION`.
+ pub fn clamp_position(v: f64) -> Result<f64, String> {
+ if v.is_finite() { Ok(v.clamp(0.0, MAX_TAB_POSITION)) } else { Err("position must be a finite number".into()) }
+ }
+
+ /// A leader: up to [`MAX_TAB_LEADER`] characters, no control characters.
+ pub fn check_leader(v: &str) -> Result<(), String> {
+ if v.chars().count() > MAX_TAB_LEADER || v.chars().any(char::is_control) {
+ return Err(format!("`leader`: at most {MAX_TAB_LEADER} printable characters"));
+ }
+ Ok(())
+ }
+
+ /// An align-on character: one printable character or none (a decimal point).
+ pub fn check_align_on(v: &str) -> Result<(), String> {
+ if v.chars().count() > 1 || v.chars().any(char::is_control) {
+ return Err("`alignOn`: one printable character".into());
+ }
+ Ok(())
+ }
+
+ /// A tab-stop list as the Tabs commands keep it: at most [`MAX_TAB_STOPS`] stops, positions
+ /// clamped, leaders and align-on characters checked, a char stop aligning on `.` unless it
+ /// names a character, sorted by position, and one stop per position (the later one wins).
+ pub fn checked_list(tabs: Vec<TabStop>) -> Result<Vec<TabStop>, String> {
+ if tabs.len() > MAX_TAB_STOPS {
+ return Err(format!("a paragraph holds at most {MAX_TAB_STOPS} tab stops"));
+ }
+ let mut out: Vec<TabStop> = Vec::with_capacity(tabs.len());
+ for mut t in tabs {
+ t.position = Self::clamp_position(t.position)?;
+ Self::check_leader(&t.leader)?;
+ Self::check_align_on(&t.align_on)?;
+ if t.align == TabAlign::Char && t.align_on.is_empty() {
+ t.align_on = ".".into();
+ }
+ out.retain(|o| (o.position - t.position).abs() >= SAME_TAB_POSITION);
+ out.push(t);
+ }
+ out.sort_by(|a, b| a.position.total_cmp(&b.position));
+ Ok(out)
+ }
+ }
+
/// A paragraph rule (Rule Above / Rule Below).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]