summaryrefslogtreecommitdiff
path: root/pict.scm
diff options
context:
space:
mode:
Diffstat (limited to 'pict.scm')
-rw-r--r--pict.scm41
1 files changed, 35 insertions, 6 deletions
diff --git a/pict.scm b/pict.scm
index a9ec612..c755aed 100644
--- a/pict.scm
+++ b/pict.scm
@@ -1,6 +1,6 @@
;;; pict.scm --- A simple picture language for Guile
-;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
+;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public License
@@ -18,7 +18,8 @@
(define-module (pict)
- #:use-module (sxml simple)
+ #:use-module (pict sxml)
+ #:use-module ((sxml simple) #:hide (xml->sxml))
#:use-module (sxml transform)
#:use-module (sxml fold)
#:use-module ((sxml xpath) #:hide (filter))
@@ -34,6 +35,8 @@
pict-height
pict-rotation
+ pict-from-file
+
;; modifiers
fill
colorize
@@ -145,6 +148,22 @@ list VALS."
'(0 . ())
(cons 0 vals))))
+(define (pict-from-file file-name)
+ "Attempt to read FILE-NAME, convert its contents to SXML and wrap it
+in a <PICT> record. If this fails return #F."
+ (catch 'parser-error
+ (lambda ()
+ (make-pict
+ (match (call-with-input-file file-name xml->sxml)
+ (('*TOP* ('*PI* . rest) svg) svg)
+ (('*TOP* svg) svg))))
+ (lambda _
+ (format (current-error-port)
+ "Failed to parse picture from file `~a'.~%\
+Is this really an SVG file?~%"
+ file-name)
+ #f)))
+
;;; SXML utilities
@@ -689,12 +708,22 @@ of the SVG of PICT or #F."
(car m)))))
(define (pict-height pict)
- "Return the height of PICT."
- (pict-attr 'height pict))
+ "Return the numeric height of PICT."
+ (match (pict-attr 'height pict)
+ ((? string? s)
+ ;; Take value up to unit and convert to number
+ (let ((index (string-skip s char-set:digit)))
+ (string->number (substring s 0 index))))
+ ((? number? n) n)))
(define (pict-width pict)
- "Return the width of PICT."
- (pict-attr 'width pict))
+ "Return the numeric width of PICT."
+ (match (pict-attr 'width pict)
+ ((? string? s)
+ ;; Take value up to unit and convert to number
+ (let ((index (string-skip s char-set:digit)))
+ (string->number (substring s 0 index))))
+ ((? number? n) n)))
(define (pict-rotation pict)
"Return the rotation of PICT."