1 ;;;; paper.scm -- manipulate the paper and layout block.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 (define-public (set-paper-dimension-variables mod)
8 (module-define! mod 'dimension-variables
9 '(pt mm cm in staff-height staff-space
11 between-system-space between-system-padding
12 line-width indent short-indent paper-width paper-height horizontal-shift
13 staff-space line-thickness ledgerline-thickness
14 blot-diameter left-margin right-margin)))
16 (define (calc-line-thickness staff-space pt)
17 ;; linear interpolation.
19 ;; !! synchronize with feta-params.mf
28 (* f1 (- staff-space x0))
29 (* f0 (- x1 staff-space)))
32 (define-public (layout-set-absolute-staff-size-in-module module staff-height)
34 ((pt (eval 'pt module))
35 (ss (/ staff-height 4))
36 (factor (/ staff-height (* 20 pt)))
37 (setm! (lambda (sym val)
38 (module-define! module sym val))))
40 (setm! 'text-font-size (* 12 factor))
42 (setm! 'output-scale ss)
45 (make-cmr-tree factor)
46 (make-century-schoolbook-tree factor)))
47 (setm! 'staff-height staff-height)
48 (setm! 'staff-space ss)
50 (setm! 'line-thickness (calc-line-thickness ss pt))
53 (setm! 'ledgerline-thickness (+ (* 0.5 pt) (/ ss 10)))
56 (setm! 'blot-diameter (* 0.4 pt))
59 (define-public (layout-set-absolute-staff-size sz)
60 "Function to be called inside a \\layout{} block to set the staff
61 size. SZ is in points"
62 (layout-set-absolute-staff-size-in-module (current-module) sz))
64 (define-public (layout-set-staff-size sz)
65 "Function to be called inside a \\layout{} block to set the staff
66 size. SZ is in points"
68 (layout-set-absolute-staff-size (* (eval 'pt (current-module)) sz)))
70 (define-safe-public (set-global-staff-size sz)
71 "Set the default staff size, where SZ is thought to be in PT."
72 (let* ((current-mod (current-module))
73 (parser (eval 'parser current-mod))
74 (pap (ly:parser-lookup parser '$defaultpaper))
75 (in-layout? (or (module-defined? current-mod 'is-paper)
76 (module-defined? current-mod 'is-layout)))
78 ; maybe not necessary.
79 ; but let's be paranoid. Maybe someone still refers to the
81 (new-paper (ly:output-def-clone pap))
83 (new-scope (ly:output-def-scope new-paper)))
86 (ly:warning (_ "set-global-staff-size: not in toplevel scope")))
88 (layout-set-absolute-staff-size-in-module new-scope
89 (* sz (eval 'pt new-scope)))
90 (module-define! current-mod '$defaultpaper new-paper)))
92 (define-public paper-alist
94 ;; don't use decimals.
95 ;; ISO 216 has a tolerance of +- 2mm
97 '(("a7" . (cons (* 74 mm) (* 105 mm)))
98 ("a6" . (cons (* 105 mm) (* 148 mm)))
99 ("a5" . (cons (* 148 mm) (* 210 mm)))
100 ("a4" . (cons (* 210 mm) (* 297 mm)))
101 ("a3" . (cons (* 297 mm) (* 420 mm)))
102 ("legal" . (cons (* 8.5 in) (* 14.0 in)))
103 ("letter" . (cons (* 8.5 in) (* 11.0 in)))
104 ("11x17" . (cons (* 11.0 in) (* 17.0 in)))
107 ;; todo: take dimension arguments.
109 (define (set-paper-dimensions m w h)
110 "M is a module (i.e. layout->scope_ )"
111 (let* ((mm (eval 'mm m)))
112 (module-define! m 'paper-width w)
113 (module-define! m 'paper-height h)
114 (module-define! m 'line-width (- w
115 (ly:modules-lookup (list m) 'left-margin (* 10 mm))
116 (ly:modules-lookup (list m) 'right-margin (* 10 mm))))
118 (module-define! m 'indent (/ w 14))
119 (module-define! m 'short-indent (* 5 mm))
121 ;; page layout - what to do with (printer specific!) margin settings?
125 (define (internal-set-paper-size module name landscape?)
127 (cons (cdr x) (car x)))
129 (let* ((entry (assoc name paper-alist))
130 (is-paper? (module-defined? module 'is-paper))
131 (mm (eval 'mm module)))
135 (ly:warning (_ "This is not a \\layout {} object, ~S" module)))
138 (set! entry (eval (cdr entry) module))
140 (set! entry (swap entry)))
141 (set-paper-dimensions module (car entry) (cdr entry))
143 (module-define! module 'papersizename name)
144 (module-define! module 'landscape
145 (if landscape? #t #f)))
147 (ly:warning (_ "Unknown papersize: ~a" name))))))
149 (define-safe-public (set-default-paper-size name . rest)
150 (internal-set-paper-size
151 (ly:output-def-scope (eval '$defaultpaper (current-module)))
153 (memq 'landscape rest)))
155 (define-public (set-paper-size name . rest)
156 (if (module-defined? (current-module) 'is-paper)
157 (internal-set-paper-size (current-module) name
158 (memq 'landscape rest))
160 ;;; TODO: should raise (generic) exception with throw, and catch
161 ;;; that in parse-scm.cc
162 (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
164 (define-public (scale-layout pap scale)
165 (let* ((new-pap (ly:output-def-clone pap))
166 (dim-vars (ly:output-def-lookup pap 'dimension-variables))
167 (old-scope (ly:output-def-scope pap))
168 (scope (ly:output-def-scope new-pap)))
172 (let* ((var (module-variable old-scope v))
173 (val (if (variable? var) (variable-ref var) #f)))
176 (module-define! scope v
179 ;; spurious warnings, eg. for paper-width, paper-height.
180 ;; (ly:warning (_ "not a number, ~S = ~S " v val))