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 '(("a10" . (cons (* 26 mm) (* 37 mm)))
98 ("a9" . (cons (* 37 mm) (* 52 mm)))
99 ("a8" . (cons (* 52 mm) (* 74 mm)))
100 ("a7" . (cons (* 74 mm) (* 105 mm)))
101 ("a6" . (cons (* 105 mm) (* 148 mm)))
102 ("a5" . (cons (* 148 mm) (* 210 mm)))
103 ("a4" . (cons (* 210 mm) (* 297 mm)))
104 ("a3" . (cons (* 297 mm) (* 420 mm)))
105 ("a2" . (cons (* 420 mm) (* 594 mm)))
106 ("a1" . (cons (* 594 mm) (* 841 mm)))
107 ("a0" . (cons (* 841 mm) (* 1189 mm)))
108 ("b10" . (cons (* 31 mm) (* 44 mm)))
109 ("b9" . (cons (* 44 mm) (* 62 mm)))
110 ("b8" . (cons (* 62 mm) (* 88 mm)))
111 ("b7" . (cons (* 88 mm) (* 125 mm)))
112 ("b6" . (cons (* 125 mm) (* 176 mm)))
113 ("b5" . (cons (* 176 mm) (* 250 mm)))
114 ("b4" . (cons (* 250 mm) (* 353 mm)))
115 ("b3" . (cons (* 353 mm) (* 500 mm)))
116 ("b2" . (cons (* 500 mm) (* 707 mm)))
117 ("b1" . (cons (* 707 mm) (* 1000 mm)))
118 ("b0" . (cons (* 1000 mm) (* 1414 mm)))
119 ;; Below are two extended sizes defined in DIn 476
120 ("4a0" . (cons (* 1682 mm) (* 2378 mm)))
121 ("2a0" . (cons (* 1189 mm) (* 1682 mm)))
122 ;; Below are ISO 269 standard C series
123 ("c10" . (cons (* 28 mm) (* 40 mm)))
124 ("c9" . (cons (* 40 mm) (* 57 mm)))
125 ("c8" . (cons (* 57 mm) (* 81 mm)))
126 ("c7" . (cons (* 81 mm) (* 114 mm)))
127 ("c6" . (cons (* 114 mm) (* 162 mm)))
128 ("c5" . (cons (* 162 mm) (* 229 mm)))
129 ("c4" . (cons (* 229 mm) (* 324 mm)))
130 ("c3" . (cons (* 324 mm) (* 458 mm)))
131 ("c2" . (cons (* 458 mm) (* 648 mm)))
132 ("c1" . (cons (* 648 mm) (* 917 mm)))
133 ("c0" . (cons (* 917 mm) (* 1297 mm)))
134 ;; Below are North American paper sizes
135 ("legal" . (cons (* 8.5 in) (* 14.0 in)))
136 ("letter" . (cons (* 8.5 in) (* 11.0 in)))
137 ;; Ledger (17x11) is a 90 degree rotation of Tabloid
138 ("11x17" . (cons (* 11.0 in) (* 17.0 in)))
139 ;; government-letter by IEEE Printer Working Group, for children's writing
140 ("government-letter" . (cons (* 8 in) (* 10.5 in)))
141 ("government-legal" . (cons (* 8.5 in) (* 13.0 in)))
142 ("philippine-legal" . (cons (* 8.5 in) (* 13.0 in)))
144 ("ansi a" . (cons (* 8.5 in) (* 11.0 in)))
145 ("ansi b" . (cons (* 17.0 in) (* 11.0 in)))
146 ("ansi c" . (cons (* 17.0 in) (* 22.0 in)))
147 ("ansi d" . (cons (* 22.0 in) (* 34.0 in)))
148 ("ansi e" . (cons (* 34.0 in) (* 44.0 in)))
149 ("engineering f" . (cons (* 28.0 in) (* 40.0 in)))
150 ;; G and H are very rare, and the lengths are variable up to 90 inches
151 ;; North American Architectural sizes
152 ("arch a" . (cons (* 9.0 in) (* 12.0 in)))
153 ("arch b" . (cons (* 12.0 in) (* 18.0 in)))
154 ("arch c" . (cons (* 18.0 in) (* 24.0 in)))
155 ("arch d" . (cons (* 24.0 in) (* 36.0 in)))
156 ("arch e" . (cons (* 36.0 in) (* 48.0 in)))
157 ("arch e1" . (cons (* 30.0 in) (* 42.0 in)))
159 ;; Some are antique sizes which are still using in UK
160 ("statement" . (cons (* 5.5 in) (* 8.5 in)))
161 ("half letter" . (cons (* 5.5 in) (* 8.5 in)))
162 ("quarto" . (cons (* 8.0 in) (* 10.0 in)))
163 ("octavo" . (cons (* 6.75 in) (* 10.5 in)))
164 ("executive" . (cons (* 7.25 in) (* 10.5 in)))
165 ("monarch" . (cons (* 7.25 in) (* 10.5 in)))
166 ("foolscap" . (cons (* 8.27 in) (* 13.0 in)))
167 ("folio" . (cons (* 8.27 in) (* 13.0 in)))
168 ("super-b" . (cons (* 13.0 in) (* 19.0 in)))
169 ("post" . (cons (* 15.5 in) (* 19.5 in)))
170 ("crown" . (cons (* 15.0 in) (* 20.0 in)))
171 ("large post" . (cons (* 16.5 in) (* 21.0 in)))
172 ("demy" . (cons (* 17.5 in) (* 22.5 in)))
173 ("medium" . (cons (* 18.0 in) (* 23.0 in)))
174 ("broadsheet" . (cons (* 18.0 in) (* 24.0 in)))
175 ("royal" . (cons (* 20.0 in) (* 25.0 in)))
176 ("elephant" . (cons (* 23.0 in) (* 28.0 in)))
177 ("double demy" . (cons (* 22.5 in) (* 35.0 in)))
178 ("quad demy" . (cons (* 35.0 in) (* 45.0 in)))
179 ("atlas" . (cons (* 26.0 in) (* 34.0 in)))
180 ("imperial" . (cons (* 22.0 in) (* 30.0 in)))
181 ("antiquarian" . (cons (* 31.0 in) (* 53.0 in)))
183 ("pa0" . (cons (* 840 mm) (* 1120 mm)))
184 ("pa1" . (cons (* 560 mm) (* 840 mm)))
185 ("pa2" . (cons (* 420 mm) (* 560 mm)))
186 ("pa3" . (cons (* 280 mm) (* 420 mm)))
187 ("pa4" . (cons (* 210 mm) (* 280 mm)))
188 ("pa5" . (cons (* 140 mm) (* 210 mm)))
189 ("pa6" . (cons (* 105 mm) (* 140 mm)))
190 ("pa7" . (cons (* 70 mm) (* 105 mm)))
191 ("pa8" . (cons (* 52 mm) (* 70 mm)))
192 ("pa9" . (cons (* 35 mm) (* 52 mm)))
193 ("pa10" . (cons (* 26 mm) (* 35 mm)))
194 ;; F4 used in southeast Asia and Australia
195 ("f4" . (cons (* 210 mm) (* 330 mm)))
198 ;; todo: take dimension arguments.
200 (define (set-paper-dimensions m w h)
201 "M is a module (i.e. layout->scope_ )"
202 (let* ((mm (eval 'mm m)))
203 (module-define! m 'paper-width w)
204 (module-define! m 'paper-height h)
205 (module-define! m 'line-width (- w
206 (ly:modules-lookup (list m) 'left-margin (* 10 mm))
207 (ly:modules-lookup (list m) 'right-margin (* 10 mm))))
209 (module-define! m 'indent (/ w 14))
210 (module-define! m 'short-indent (* 5 mm))
212 ;; page layout - what to do with (printer specific!) margin settings?
216 (define (internal-set-paper-size module name landscape?)
218 (cons (cdr x) (car x)))
220 (let* ((entry (assoc name paper-alist))
221 (is-paper? (module-defined? module 'is-paper))
222 (mm (eval 'mm module)))
226 (ly:warning (_ "This is not a \\layout {} object, ~S" module)))
229 (set! entry (eval (cdr entry) module))
231 (set! entry (swap entry)))
232 (set-paper-dimensions module (car entry) (cdr entry))
234 (module-define! module 'papersizename name)
235 (module-define! module 'landscape
236 (if landscape? #t #f)))
238 (ly:warning (_ "Unknown papersize: ~a" name))))))
240 (define-safe-public (set-default-paper-size name . rest)
241 (internal-set-paper-size
242 (ly:output-def-scope (eval '$defaultpaper (current-module)))
244 (memq 'landscape rest)))
246 (define-public (set-paper-size name . rest)
247 (if (module-defined? (current-module) 'is-paper)
248 (internal-set-paper-size (current-module) name
249 (memq 'landscape rest))
251 ;;; TODO: should raise (generic) exception with throw, and catch
252 ;;; that in parse-scm.cc
253 (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
255 (define-public (scale-layout pap scale)
256 (let* ((new-pap (ly:output-def-clone pap))
257 (dim-vars (ly:output-def-lookup pap 'dimension-variables))
258 (old-scope (ly:output-def-scope pap))
259 (scope (ly:output-def-scope new-pap)))
263 (let* ((var (module-variable old-scope v))
264 (val (if (variable? var) (variable-ref var) #f)))
267 (module-define! scope v
270 ;; spurious warnings, eg. for paper-width, paper-height.
271 ;; (ly:warning (_ "not a number, ~S = ~S " v val))