From 0349ea191330b8e2fa10df7393acfd2095009301 Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 09:39:01 +0100 Subject: [PATCH 1/7] Make postscript op owned --- src/functions.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index a70f0a4..6db5e6b 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -237,8 +237,8 @@ impl<'a> PostScriptFunction<'a> { deref!('a, PostScriptFunction<'a> => Stream<'a>, stream); /// PostScript operators for use in Type 4 functions. -#[derive(Debug, Copy, Clone, PartialEq)] -pub enum PostScriptOp<'a> { +#[derive(Debug, Clone, PartialEq)] +pub enum PostScriptOp { /// Push a real number. Real(f32), /// Push an integer number. @@ -315,9 +315,9 @@ pub enum PostScriptOp<'a> { Xor, /// Conditional. Runs if boolean argument is true. - If(&'a [Self]), + If(Vec), /// Conditional. Decides which branch to run depending on boolean argument. - IfElse(&'a [Self], &'a [Self]), + IfElse(Vec, Vec), /// Copy the top elements. One integer argument. Copy, @@ -333,7 +333,7 @@ pub enum PostScriptOp<'a> { Roll, } -impl<'a> PostScriptOp<'a> { +impl PostScriptOp { /// Encode a slice of operations into a byte stream. pub fn encode(ops: &[Self]) -> Vec { let mut buf = Vec::new(); @@ -357,18 +357,18 @@ impl<'a> PostScriptOp<'a> { } fn write(&self, buf: &mut Vec) { - match *self { - Self::Real(r) => buf.push_decimal(r), - Self::Integer(i) => buf.push_val(i), + match self { + Self::Real(r) => buf.push_decimal(*r), + Self::Integer(i) => buf.push_val(*i), Self::If(ops) => { - Self::write_slice(ops, buf); + Self::write_slice(&ops, buf); buf.push(b'\n'); buf.extend(self.operator()); } Self::IfElse(ops1, ops2) => { - Self::write_slice(ops1, buf); + Self::write_slice(&ops1, buf); buf.push(b'\n'); - Self::write_slice(ops2, buf); + Self::write_slice(&ops2, buf); buf.push(b'\n'); buf.extend(self.operator()); } @@ -441,7 +441,7 @@ mod tests { Dup, Real(0.0), Ge, - IfElse(&[Real(1.0), Add], &[Neg]), + IfElse(vec![Real(1.0), Add], vec![Neg]), Add, ]; From aa14c6705da4e972cf83240d232a0ae999868541 Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 09:54:35 +0100 Subject: [PATCH 2/7] Change separators & fix float handling --- src/functions.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index 6db5e6b..55b6678 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -344,11 +344,11 @@ impl PostScriptOp { fn write_slice(ops: &[Self], buf: &mut Vec) { buf.push(b'{'); if ops.len() > 1 { - buf.push(b'\n'); + buf.push(b' '); } for op in ops { op.write(buf); - buf.push(b'\n'); + buf.push(b' '); } if ops.len() == 1 { buf.pop(); @@ -358,18 +358,18 @@ impl PostScriptOp { fn write(&self, buf: &mut Vec) { match self { - Self::Real(r) => buf.push_decimal(*r), + Self::Real(r) => buf.push_float(*r), Self::Integer(i) => buf.push_val(*i), Self::If(ops) => { Self::write_slice(&ops, buf); - buf.push(b'\n'); + buf.push(b' '); buf.extend(self.operator()); } Self::IfElse(ops1, ops2) => { Self::write_slice(&ops1, buf); - buf.push(b'\n'); + buf.push(b' '); Self::write_slice(&ops2, buf); - buf.push(b'\n'); + buf.push(b' '); buf.extend(self.operator()); } _ => buf.extend(self.operator()), From fd360b158caff3304a246c477c2ab53c358752e1 Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 09:58:12 +0100 Subject: [PATCH 3/7] Revert to push decimal --- src/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.rs b/src/functions.rs index 55b6678..dc0e810 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -358,7 +358,7 @@ impl PostScriptOp { fn write(&self, buf: &mut Vec) { match self { - Self::Real(r) => buf.push_float(*r), + Self::Real(r) => buf.push_decimal(*r), Self::Integer(i) => buf.push_val(*i), Self::If(ops) => { Self::write_slice(&ops, buf); From 9e686a28e03905d5e06b4901ebc311bdd057712d Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 11:28:39 +0100 Subject: [PATCH 4/7] Make PostScriptOp copyable again --- src/functions.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index dc0e810..4a23a4e 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -238,7 +238,7 @@ deref!('a, PostScriptFunction<'a> => Stream<'a>, stream); /// PostScript operators for use in Type 4 functions. #[derive(Debug, Clone, PartialEq)] -pub enum PostScriptOp { +pub enum PostScriptOp<'a> { /// Push a real number. Real(f32), /// Push an integer number. @@ -315,9 +315,9 @@ pub enum PostScriptOp { Xor, /// Conditional. Runs if boolean argument is true. - If(Vec), + If(&'a [Self]), /// Conditional. Decides which branch to run depending on boolean argument. - IfElse(Vec, Vec), + IfElse(&'a [Self], &'a [Self]), /// Copy the top elements. One integer argument. Copy, @@ -333,7 +333,7 @@ pub enum PostScriptOp { Roll, } -impl PostScriptOp { +impl PostScriptOp<'_> { /// Encode a slice of operations into a byte stream. pub fn encode(ops: &[Self]) -> Vec { let mut buf = Vec::new(); @@ -441,13 +441,13 @@ mod tests { Dup, Real(0.0), Ge, - IfElse(vec![Real(1.0), Add], vec![Neg]), + IfElse(&[Real(1.0), Add], &[Neg]), Add, ]; assert_eq!( PostScriptOp::encode(&ops), - b"{\n3.0\n2.0\nmul\nexch\ndup\n0.0\nge\n{\n1.0\nadd\n}\n{neg}\nifelse\nadd\n}" + b"{ 3.0 2.0 mul exch dup 0.0 ge { 1.0 add } {neg} ifelse add }" ); } } From 577bdbb53c2b1ad93477b0b48c4bef742e28324b Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 11:29:06 +0100 Subject: [PATCH 5/7] make copyable --- src/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.rs b/src/functions.rs index 4a23a4e..d56cd46 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -237,7 +237,7 @@ impl<'a> PostScriptFunction<'a> { deref!('a, PostScriptFunction<'a> => Stream<'a>, stream); /// PostScript operators for use in Type 4 functions. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq)] pub enum PostScriptOp<'a> { /// Push a real number. Real(f32), From 81c9a9b503f7a8f4cf02fac6e48ee0555a24498c Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 11:30:13 +0100 Subject: [PATCH 6/7] Align with previosu code --- src/functions.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index d56cd46..c6c5a7b 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -333,7 +333,7 @@ pub enum PostScriptOp<'a> { Roll, } -impl PostScriptOp<'_> { +impl<'a> PostScriptOp<'a> { /// Encode a slice of operations into a byte stream. pub fn encode(ops: &[Self]) -> Vec { let mut buf = Vec::new(); @@ -357,18 +357,18 @@ impl PostScriptOp<'_> { } fn write(&self, buf: &mut Vec) { - match self { - Self::Real(r) => buf.push_decimal(*r), - Self::Integer(i) => buf.push_val(*i), + match *self { + Self::Real(r) => buf.push_decimal(r), + Self::Integer(i) => buf.push_val(i), Self::If(ops) => { Self::write_slice(&ops, buf); buf.push(b' '); buf.extend(self.operator()); } Self::IfElse(ops1, ops2) => { - Self::write_slice(&ops1, buf); + Self::write_slice(ops1, buf); buf.push(b' '); - Self::write_slice(&ops2, buf); + Self::write_slice(ops2, buf); buf.push(b' '); buf.extend(self.operator()); } From 6fe04a742af9a0129eb7c94f39eabcca09cdb1fc Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl <47084093+LaurenzV@users.noreply.github.com> Date: Sun, 27 Oct 2024 11:30:38 +0100 Subject: [PATCH 7/7] remove reference --- src/functions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.rs b/src/functions.rs index c6c5a7b..50300b1 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -361,7 +361,7 @@ impl<'a> PostScriptOp<'a> { Self::Real(r) => buf.push_decimal(r), Self::Integer(i) => buf.push_val(i), Self::If(ops) => { - Self::write_slice(&ops, buf); + Self::write_slice(ops, buf); buf.push(b' '); buf.extend(self.operator()); }