BANNER
PrevChapter 2Next

Length

parse-measurement

(parse-measurement measure)

Returns a two-item list of the magnitude and units components of measure. If measure is not correctly formed, one or both of the magnitude and units will be returned as #f.

(define (parse-measurement measure)
  ;; Parses measurement and returns '(magnitude units).  Either may be #f
  ;; if measure is not a correctly formatted measurement.  Leading and
  ;; trailing spaces are skipped.
  (let* ((magstart  (find-first-char measure " " "0123456789."))
	 (unitstart (find-first-char measure " 0123456789." ""))
	 (unitend   (find-first-char measure "" " " unitstart))
	 (magnitude (if (< magstart 0)
			#f
			(if (< unitstart 0)
			    (substring measure 
				       magstart 
				       (string-length measure))
			    (substring measure magstart unitstart))))
	 (unit      (if (< unitstart 0)
			#f
			(if (< unitend 0)
			    (substring measure 
				       unitstart 
				       (string-length measure))
			    (substring measure unitstart unitend)))))
  (list magnitude unit))

measurement-to-length

(measurement-to-length measure)

Returns a length object converted from the value of the measure string.

(define unit-conversion-alist
  (list
   '("default" 1pi)
   '("mm" 1mm)
   '("cm" 1cm)
   '("in" 1in)
   '("pi" 1pi)
   '("pc" 1pi)
   '("pt" 1pt)
   '("px" 1px)
   '("barleycorn" 2pi)))

(define (measurement-to-length measure)
  ;; Converts the string measure into a DSSSL length
  (let* ((pm (car (parse-measurement measure)))
	 (pu (car (cdr (parse-measurement measure))))
	 (magnitude (if pm pm "1"))
	 (units     (if pu pu (if pm "pt" "default"))
	 (unitconv  (assoc units unit-conversion-alist))
	 (factor    (if unitconv (car (cdr unitconv)) 1pt)))
    (* (string->number magnitude) factor)))

PrevHomeNext
StringUpNode