branch: externals/scanner
commit 9eb4666522a4563ef10f93702c4d1ecaaa34af8e
Author: Raffael Stocker <r.stoc...@mnet-mail.de>
Commit: Raffael Stocker <r.stoc...@mnet-mail.de>

    improve error handling in size calculations
---
 scanner-test.el | 22 ++++++++++++++++++----
 scanner.el      | 31 +++++++++++++++++--------------
 2 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/scanner-test.el b/scanner-test.el
index 18f4deef40..efb62d2004 100644
--- a/scanner-test.el
+++ b/scanner-test.el
@@ -139,24 +139,38 @@
   "Test the cm to pixel conversion."
   (should (= 100 (scanner--cm-to-pixels 2.54 100)))
   (should (= 600 (scanner--cm-to-pixels 2.54 600)))
+  (should (= -600 (scanner--cm-to-pixels -2.54 600)))
+  (should (= -600 (scanner--cm-to-pixels 2.54 -600)))
   (should (= 0 (scanner--cm-to-pixels 2.54 0)))
+  (should (= 0 (scanner--cm-to-pixels 0 30)))
   (should (= 354 (scanner--cm-to-pixels 3 300)))
-  (should (= 300 (scanner--cm-to-pixels 2.54508 300))))
+  (should (= 300 (scanner--cm-to-pixels 2.54508 300)))
+  (should-error (= 300 (scanner--cm-to-pixels 'a 300))))
 
 (ert-deftest scanner--corner-pixels ()
   "Test the paper size to corner coordinates conversion."
-  (should (equal '(0 0 35078 25393) (scanner--corner-pixels '(297 215) 300))))
+  (should (equal '(0 0 35078 25393) (scanner--corner-pixels '(297 215) 300)))
+  (should-error (equal '(0 0 35078 25393) (scanner--corner-pixels 'a 300)))
+  (should-error (equal '(0 0 35078 25393)
+                       (scanner--corner-pixels '(a b) 300)))
+  (should-error (equal '(0 0 35078 25393)
+                       (scanner--corner-pixels '(-297 210) 300))))
 
 (ert-deftest scanner--keyword-string ()
   "Test the keyword to string conversion."
   (should (string= "keyword" (scanner--keyword-string :keyword)))
-  (should (string= "" (scanner--keyword-string :))))
+  (should (string= "" (scanner--keyword-string :)))
+  (should (string= "keyword" (scanner--keyword-string "keyword"))))
 
 (ert-deftest scanner--process-unpaper-size ()
   "Test paper size parsing."
   (should (eq nil (scanner--process-unpaper-size "none")))
   (should (eq :the-size (scanner--process-unpaper-size ":the-size")))
-  (should (string= "297cm,210cm" (scanner--process-unpaper-size 
"297cm,210cm"))))
+  (should (string= "297cm,210cm"
+                   (scanner--process-unpaper-size "297cm,210cm")))
+  (should-error (scanner--process-unpaper-size "297cm"))
+  (should-error (scanner--process-unpaper-size "297cm,"))
+  (should-error (scanner--process-unpaper-size '(297 210))))
 
 (provide 'scanner-test)
 
diff --git a/scanner.el b/scanner.el
index 52f010eed6..8664112326 100644
--- a/scanner.el
+++ b/scanner.el
@@ -620,7 +620,7 @@ y-dimension.  If no size is configured, return nil."
                'user-switches 'scanner-scanimage-switches)
   "The arguments list used for preview scans.")
 
-(defun scanner--size-cm (size)
+(defun scanner--size-in-cm (size)
   (cond ((and (keywordp size)
                          (plist-member scanner-paper-sizes size))
                 (mapcar (lambda (num) (/ num 10.0))
@@ -629,8 +629,8 @@ y-dimension.  If no size is configured, return nil."
                                                                  (let ((idx 
(string-match
                                                                                
          "\\([[:digit:]]+\\(\\.[[:digit:]]+\\)?\\)cm"
                                                                                
          size-str)))
-                                                                       (if idx 
(string-to-number
-                                                                               
         (match-string 1 size-str))
+                                                                       (if idx
+                                                                               
(string-to-number (match-string 1 size-str))
                                                                          
(user-error "Unknown size format: %s" size-str))))
                                                                (split-string 
size ",")))
                (t (error "Unknown paper size: %s" size))))
@@ -639,10 +639,13 @@ y-dimension.  If no size is configured, return nil."
   (floor (* (/ cm 2.54) resolution)))
 
 (defun scanner--corner-pixels (size resolution)
-  (list 0
-               0
-               (scanner--cm-to-pixels (car size) resolution)
-               (scanner--cm-to-pixels (cadr size) resolution)))
+  (let ((coords (list 0
+                                         0
+                                         (scanner--cm-to-pixels (car size) 
resolution)
+                                         (scanner--cm-to-pixels (cadr size) 
resolution))))
+       (unless (cl-notany #'cl-minusp coords)
+         (user-error "Size must be non-negative: %s" size))
+       coords))
 
 (defun scanner--program-args (argspec &rest args)
   "Return an arguments list as specified in ARGSPEC, assuming ARGS.
@@ -765,7 +768,7 @@ construct a shell command."
                                                                (if 
scanner-unpaper-mask-size
                                                                        
scanner-unpaper-mask-size
                                                                  
(scanner--corner-pixels
-                                                                  
(scanner--size-cm
+                                                                  
(scanner--size-in-cm
                                                                        
scanner-unpaper-pre-size)
                                                                   (plist-get 
scanner-resolution :doc)))
                                                                ",")))
@@ -1020,13 +1023,13 @@ selection is made."
   (interactive (scanner--select-rotation "Select post-rotation: "))
   (setq scanner-unpaper-post-rotation rotation))
 
-
 (defun scanner--process-unpaper-size (size)
-  (if (string= "none" size)
-         nil
-       (if (string= ":" (substring size 0 1))
-               (intern size)
-         size)))
+  (cond ((string= "none" size) nil)
+               ((string= ":" (substring size 0 1)) (intern size))
+               (t (if (or (not (string-match-p "[[:alnum:]]+,[[:alnum:]]+" 
size))
+                                  (cl-some #'string-empty-p (split-string size 
",")))
+                          (user-error "Size must have two values, separated by 
comma: %s" size)
+                        size))))
 
 ;;;###autoload
 (defun scanner-select-pre-size (size)

Reply via email to