-;;; paper.scm -- scm paper variables and functions
-;;;
-;;; source file of the GNU LilyPond music typesetter
-;;;
-;;; (c) 1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
-
-;;; All dimensions are measured in staff-spaces
-
-
-; This is a mess : global namespace pollution. We should wait
-; till guile has proper toplevel environment support.
-
-
-;; Beams should be prevented to conflict with the stafflines,
-;; especially at small slopes
-;; ----------------------------------------------------------
-;; ########
-;; ########
-;; ########
-;; --------------########------------------------------------
-;; ########
-;;
-;; hang straddle sit inter hang
-
-;; inter seems to be a modern quirk, we don't use that
-
-(define staff-line 0.10)
-(define beam-thickness (* 0.52 (- 1 staff-line)))
-(define beam-straddle 0)
-(define beam-sit (/ (+ beam-thickness staff-line) 2))
-(define beam-hang (- 1 (/ (- beam-thickness staff-line) 2)))
-
-;; Note: quanting period is take as quants.top () - quants[0],
-;; which should be 1 (== 1 interline)
-
-(define beam-normal-dy-quants
- (list 0 (/ (+ beam-thickness staff-line) 2) (+ beam-thickness staff-line) 1))
-
-;; two popular veritcal beam quantings
-;; see params.ly: #'beam-vertical-quants
-(define (beam-normal-y-quants multiplicity dy)
- (let ((quants (list beam-hang)))
- (if (or (<= multiplicity 1) (>= (abs dy) (/ staff-line 2)))
- (set! quants (cons beam-sit quants)))
- (if (or (<= multiplicity 2) (>= (abs dy) (/ staff-line 2)))
- (set! quants (cons beam-straddle quants)))
- ;; period: 1 (interline)
- (append quants (list (+ 1 (car quants))))))
-
-(define (beam-traditional-y-quants multiplicity dy)
- (let ((quants '()))
- (if (>= dy (/ staff-line -2))
- (set! quants (cons beam-hang quants)))
- (if (and (<= multiplicity 1) (<= dy (/ staff-line 2)))
- (set! quants (cons beam-sit quants)))
- (if (or (<= multiplicity 2) (>= (abs dy) (/ staff-line 2)))
- (set! quants (cons beam-straddle quants)))
- ;; period: 1 (interline)
- (append quants (list (+ 1 (car quants))))))
-
-
-;; There are several ways to calculate the direction of a beam
-;;
-;; * majority: number count of up or down notes
-;; * mean : mean centre distance of all notes
-;; * median : mean centre distance weighted per note
-
-(define (dir-compare up down)
- (if (= up down)
- 0
- (if (> up down)
- 1
- -1)))
-
-;; (up . down)
-(define (beam-dir-majority count total)
- (dir-compare (car count) (cdr count)))
-
-(define (beam-dir-mean count total)
- (dir-compare (car total) (cdr total)))
-
-(define (beam-dir-median count total)
- (if (and (> (car count) 0)
- (> (cdr count) 0))
- (dir-compare (/ (car total) (car count)) (/ (cdr total) (cdr count)))
- (dir-compare (car count) (cdr count))))
-
-
-;;; Default variables and settings
-
-(define beam-height-quants beam-normal-dy-quants)
-(define beam-vertical-position-quants beam-normal-y-quants)
-
-
-;; [Ross] states that the majority of the notes dictates the
-;; direction (and not the mean of "center distance")
-;;
-;; But is that because it really looks better, or because he wants
-;; to provide some real simple hands-on rules?
-;;
-;; We have our doubts, so we simply provide all sensible alternatives.
-(define beam-dir-algorithm beam-dir-majority)
-
-
-;; array index flag-2 (what a name!!), last if index>size
-;; unbeamed stems
-(define stem-length '(3.5 3.5 3.5 4.5 5.0))
-(define grace-length-factor 0.8)
-(define grace-stem-length
- (map (lambda (x) (* grace-length-factor x)) stem-length))
-
-;; array index multiplicity, last if index>size
-;; beamed stems
-(define beamed-stem-shorten '(0.5))
-(define beamed-stem-length '(0.0 2.5 2.0 1.5))
-(define grace-beamed-stem-length '(0.0 2.5 2.0 1.5))
-(define beamed-stem-minimum-length '(0.0 1.5 1.25 1.0))
-(define grace-beamed-stem-minimum-length
- (map (lambda (x) (* grace-length-factor x)) beamed-stem-minimum-length))
-
-;; Stems in unnatural (forced) direction should be shortened,
-;; according to [Roush & Gourlay]. Their suggestion to knock off
-;; a whole staffspace seems a bit drastical: we'll do half.
-
-;; TODO
-;; - take #forced stems into account (now done in C++)?
-;; - take y-position of chord or beam into account
-
-(define stem-shorten '(0.5))
-(define grace-stem-shorten '(0.0))
-
-;; urg
-(define pi (* 2 (acos 0)))
-
-(define (slur-default-height h-inf r-0 b)
- (let ((alpha (/ (* 2.0 h-inf) pi))
- (beta (/ (* pi r-0) (* 2.0 h-inf))))
- (* alpha (atan (* beta b)))))
-
-;; see mf/feta-pendaal.mf
-(define pedal-kerning-alist '())
-(set! pedal-kerning-alist
- (append
- '(
- (("P" . "e") . -0.07)
- (("e" . "d") . -0.53)
- (("*" . "P") . 0.2)
- )
- pedal-kerning-alist))
-
-(define (pedal-kerning l r)
- (let ((entry (assoc (cons l r) pedal-kerning-alist)))
- (if entry
- (cdr entry))))
-
+;;;; paper.scm -- manipulate the paper and layout block.
+;;;;
+;;;; source file of the GNU LilyPond music typesetter
+;;;;
+;;;; (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+(define-public (set-paper-dimension-variables mod)
+ (module-define! mod 'dimension-variables
+ '(pt mm cm in staff-height staff-space
+ page-top-space
+ between-system-space between-system-padding
+ line-width indent paper-width paper-height horizontal-shift
+ staff-space line-thickness ledgerline-thickness
+ blot-diameter left-margin right-margin)))
+
+(define (calc-line-thickness staff-space pt)
+ ;; linear interpolation.
+
+ ;; !! synchronize with feta-params.mf
+ (let*
+ ((x1 (* 4.125 pt))
+ (x0 (* 5 pt))
+ (f1 (* 0.47 pt))
+ (f0 (* 0.50 pt)))
+
+ (/
+ (+
+ (* f1 (- staff-space x0))
+ (* f0 (- x1 staff-space)))
+ (- x1 x0))))
+
+(define-public (layout-set-absolute-staff-size-in-module m staff-height)
+ (let*
+ ((pt (eval 'pt m))
+ (ss (/ staff-height 4))
+ (factor (/ staff-height (* 20 pt))))
+
+ (module-define! m 'text-font-size (* 12 factor))
+
+ (module-define! m 'output-scale ss)
+ (module-define! m 'fonts
+ (if tex-backend?
+ (make-cmr-tree factor)
+ (make-century-schoolbook-tree factor)))
+ (module-define! m 'staff-height staff-height)
+ (module-define! m 'staff-space ss)
+
+ (module-define! m 'line-thickness (calc-line-thickness ss pt))
+
+ ;; sync with feta
+ (module-define! m 'ledgerline-thickness (+ (* 0.5 pt) (/ ss 10)))
+
+ ;; sync with feta
+ (module-define! m 'blot-diameter (* 0.4 pt))
+ ))
+
+ (define-public (layout-set-absolute-staff-size sz)
+ "Function to be called inside a \\layout{} block to set the staff size. SZ is in
+points"
+
+ (layout-set-absolute-staff-size-in-module (current-module) sz))
+
+(define-public (layout-set-staff-size sz)
+ "Function to be called inside a \\layout{} block to set the staff
+size. SZ is in points"
+
+ (layout-set-absolute-staff-size (* (eval 'pt (current-module)) sz)))
+
+(define-safe-public (set-global-staff-size sz)
+ "Set the default staff size, where SZ is thought to be in PT."
+ (let* ((current-mod (current-module))
+ (pap (eval '$defaultpaper current-mod))
+ (in-layout? (or (module-defined? current-mod 'is-paper)
+ (module-defined? current-mod 'is-layout)))
+
+ ; maybe not necessary.
+ ; but let's be paranoid. Maybe someone still refers to the
+ ; old one.
+ (new-paper (ly:output-def-clone pap))
+
+ (new-scope (ly:output-def-scope new-paper)))
+
+ (if in-layout?
+ (ly:warning (_ "set-global-staff-size: not in toplevel scope")))
+
+ (layout-set-absolute-staff-size-in-module new-scope
+ (* sz (eval 'pt new-scope)))
+ (module-define! current-mod '$defaultpaper new-paper)))
+
+(define-public paper-alist
+
+ ;; don't use decimals.
+ ;; ISO 216 has a tolerance of +- 2mm
+
+ '(("a7" . (cons (* 74 mm) (* 105 mm)))
+ ("a6" . (cons (* 105 mm) (* 148 mm)))
+ ("a5" . (cons (* 148 mm) (* 210 mm)))
+ ("a4" . (cons (* 210 mm) (* 297 mm)))
+ ("a3" . (cons (* 297 mm) (* 420 mm)))
+ ("legal" . (cons (* 8.5 in) (* 14.0 in)))
+ ("letter" . (cons (* 8.5 in) (* 11.0 in)))
+ ("11x17" . (cons (* 11.0 in) (* 17.0 in)))
+ ))
+
+;; todo: take dimension arguments.
+
+(define (set-paper-dimensions m w h)
+ "M is a module (i.e. layout->scope_ )"
+ (let* ((mm (eval 'mm m)))
+ (module-define! m 'paper-width w)
+ (module-define! m 'paper-height h)
+ (module-define! m 'line-width (- w
+ (ly:modules-lookup (list m) 'left-margin (* 10 mm))
+ (ly:modules-lookup (list m) 'right-margin (* 10 mm))))
+
+ (module-define! m 'indent (/ w 14))
+
+ ;; page layout - what to do with (printer specific!) margin settings?
+
+ ))
+
+(define (internal-set-paper-size module name landscape?)
+ (define (swap x)
+ (cons (cdr x) (car x)))
+
+ (let* ((entry (assoc name paper-alist))
+ (is-paper? (module-defined? module 'is-paper))
+ (mm (eval 'mm module)))
+
+ (cond
+ ((not is-paper?)
+ (ly:warning (_ "This is not a \\layout {} object, ~S" module)))
+ ((pair? entry)
+
+ (set! entry (eval (cdr entry) module))
+ (if landscape?
+ (set! entry (swap entry)))
+ (set-paper-dimensions module (car entry) (cdr entry))
+
+ (module-define! module 'papersizename name)
+ (module-define! module 'landscape
+ (if landscape? #t #f)))
+ (else
+ (ly:warning (_ "Unknown papersize: ~a" name))))))
+
+(define-safe-public (set-default-paper-size name . rest)
+ (internal-set-paper-size
+ (ly:output-def-scope (eval '$defaultpaper (current-module)))
+ name
+ (memq 'landscape rest)))
+
+(define-public (set-paper-size name . rest)
+ (if (module-defined? (current-module) 'is-paper)
+ (internal-set-paper-size (current-module) name
+ (memq 'landscape rest))
+
+ ;;; TODO: should raise (generic) exception with throw, and catch
+ ;;; that in parse-scm.cc
+ (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
+
+(define-public (scale-layout pap scale)
+ (let* ((new-pap (ly:output-def-clone pap))
+ (dim-vars (ly:output-def-lookup pap 'dimension-variables))
+ (old-scope (ly:output-def-scope pap))
+ (scope (ly:output-def-scope new-pap)))
+
+ (for-each
+ (lambda (v)
+ (let* ((var (module-variable old-scope v))
+ (val (if (variable? var) (variable-ref var) #f)))
+
+ (if (number? val)
+ (module-define! scope v
+ (/ val scale))
+
+ ;; spurious warnings, eg. for paper-width, paper-height.
+ ;; (ly:warning (_ "not a number, ~S = ~S " v val))
+ )))
+
+ dim-vars)
+
+ new-pap))