1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 ;; for define-safe-public when byte-compiling using Guile V2
19 (use-modules (scm safe-utility-defs))
21 (define-public (set-paper-dimension-variables mod)
22 (module-define! mod 'dimension-variables
27 footnote-footer-padding
34 inner-margin-default-scaled
37 left-margin-default-scaled
42 outer-margin-default-scaled
47 right-margin-default-scaled
53 (define (calc-line-thickness staff-space pt)
54 ;; linear interpolation.
56 ;; !! synchronize with feta-params.mf
65 (* f1 (- staff-space x0))
66 (* f0 (- x1 staff-space)))
69 (define-public (layout-set-absolute-staff-size-in-module module staff-height)
71 ((pt (eval 'pt module))
72 (ss (/ staff-height 4))
73 (factor (/ staff-height (* 20 pt)))
74 (setm! (lambda (sym val)
75 (module-define! module sym val))))
77 ;; Synchronized with the `text-font-size'
78 ;; binding in add-pango-fonts (see font.scm).
79 (setm! 'text-font-size (* 11 factor))
81 (setm! 'output-scale ss)
82 (setm! 'fonts (make-century-schoolbook-tree factor))
83 (setm! 'staff-height staff-height)
84 (setm! 'staff-space ss)
86 (setm! 'line-thickness (calc-line-thickness ss pt))
89 (setm! 'ledger-line-thickness (+ (* 0.5 pt) (/ ss 10)))
92 (setm! 'blot-diameter (* 0.4 pt))
95 (define-public (layout-set-absolute-staff-size sz)
96 "Set the absolute staff size inside of a @code{\\layout@{@}} block.
97 @var{sz} is in points."
98 (layout-set-absolute-staff-size-in-module (current-module) sz))
100 (define-public (layout-set-staff-size sz)
101 "Set the staff size inside of a @code{\\layout@{@}} block.
102 @var{sz} is in points."
104 (layout-set-absolute-staff-size (* (eval 'pt (current-module)) sz)))
106 (define-safe-public (set-global-staff-size sz)
107 "Set the default staff size, where SZ is thought to be in PT."
108 (let* ((current-mod (current-module))
109 (parser (eval 'parser current-mod))
110 (pap (ly:parser-lookup parser '$defaultpaper))
111 (in-layout? (or (module-defined? current-mod 'is-paper)
112 (module-defined? current-mod 'is-layout)))
114 ;; maybe not necessary.
115 ;; but let's be paranoid. Maybe someone still refers to the
117 (new-paper (ly:output-def-clone pap))
119 (new-scope (ly:output-def-scope new-paper)))
122 (ly:warning (_ "set-global-staff-size: not in toplevel scope")))
124 (layout-set-absolute-staff-size-in-module new-scope
125 (* sz (eval 'pt new-scope)))
126 (module-define! current-mod '$defaultpaper new-paper)))
128 (define-public paper-alist
130 ;; don't use decimals.
131 ;; ISO 216 has a tolerance of +- 2mm
133 ;; TODO Autogenerate the following list so it appears under the
134 ;; 'Predefined paper sizes' node in notation-appendices.itely
135 ;; currently the list below has been copied and formatted manually.
136 ;; Therefore, please add any new entries to the *itely file as well.
138 '(("a10" . (cons (* 26 mm) (* 37 mm)))
139 ("a9" . (cons (* 37 mm) (* 52 mm)))
140 ("a8" . (cons (* 52 mm) (* 74 mm)))
141 ("a7" . (cons (* 74 mm) (* 105 mm)))
142 ("a6" . (cons (* 105 mm) (* 148 mm)))
143 ("a5" . (cons (* 148 mm) (* 210 mm)))
144 ("a4" . (cons (* 210 mm) (* 297 mm)))
145 ("a3" . (cons (* 297 mm) (* 420 mm)))
146 ("a2" . (cons (* 420 mm) (* 594 mm)))
147 ("a1" . (cons (* 594 mm) (* 841 mm)))
148 ("a0" . (cons (* 841 mm) (* 1189 mm)))
149 ("b10" . (cons (* 31 mm) (* 44 mm)))
150 ("b9" . (cons (* 44 mm) (* 62 mm)))
151 ("b8" . (cons (* 62 mm) (* 88 mm)))
152 ("b7" . (cons (* 88 mm) (* 125 mm)))
153 ("b6" . (cons (* 125 mm) (* 176 mm)))
154 ("b5" . (cons (* 176 mm) (* 250 mm)))
155 ("b4" . (cons (* 250 mm) (* 353 mm)))
156 ("b3" . (cons (* 353 mm) (* 500 mm)))
157 ("b2" . (cons (* 500 mm) (* 707 mm)))
158 ("b1" . (cons (* 707 mm) (* 1000 mm)))
159 ("b0" . (cons (* 1000 mm) (* 1414 mm)))
160 ;; Below are two extended sizes defined in DIn 476
161 ("4a0" . (cons (* 1682 mm) (* 2378 mm)))
162 ("2a0" . (cons (* 1189 mm) (* 1682 mm)))
163 ;; Below are ISO 269 standard C series
164 ("c10" . (cons (* 28 mm) (* 40 mm)))
165 ("c9" . (cons (* 40 mm) (* 57 mm)))
166 ("c8" . (cons (* 57 mm) (* 81 mm)))
167 ("c7" . (cons (* 81 mm) (* 114 mm)))
168 ("c6" . (cons (* 114 mm) (* 162 mm)))
169 ("c5" . (cons (* 162 mm) (* 229 mm)))
170 ("c4" . (cons (* 229 mm) (* 324 mm)))
171 ("c3" . (cons (* 324 mm) (* 458 mm)))
172 ("c2" . (cons (* 458 mm) (* 648 mm)))
173 ("c1" . (cons (* 648 mm) (* 917 mm)))
174 ("c0" . (cons (* 917 mm) (* 1297 mm)))
175 ;; Below are North American paper sizes
176 ("junior-legal" . (cons (* 8.0 in) (* 5.0 in)))
177 ("legal" . (cons (* 8.5 in) (* 14.0 in)))
178 ("letter" . (cons (* 8.5 in) (* 11.0 in)))
179 ;; Ledger (17x11) is a 90 degree rotation of Tabloid
180 ("17x11" . (cons (* 17.0 in) (* 11.0 in)))
181 ("ledger" . (cons (* 17.0 in) (* 11.0 in)))
183 ("11x17" . (cons (* 11.0 in) (* 17.0 in)))
184 ("tabloid" . (cons (* 11.0 in) (* 17.0 in)))
185 ;; government-letter by IEEE Printer Working Group, for children's writing
186 ("government-letter" . (cons (* 8 in) (* 10.5 in)))
187 ("government-legal" . (cons (* 8.5 in) (* 13.0 in)))
188 ("philippine-legal" . (cons (* 8.5 in) (* 13.0 in)))
190 ("ansi a" . (cons (* 8.5 in) (* 11.0 in)))
191 ("ansi b" . (cons (* 17.0 in) (* 11.0 in)))
192 ("ansi c" . (cons (* 17.0 in) (* 22.0 in)))
193 ("ansi d" . (cons (* 22.0 in) (* 34.0 in)))
194 ("ansi e" . (cons (* 34.0 in) (* 44.0 in)))
195 ("engineering f" . (cons (* 28.0 in) (* 40.0 in)))
196 ;; G and H are very rare, and the lengths are variable up to 90 inches
197 ;; North American Architectural sizes
198 ("arch a" . (cons (* 9.0 in) (* 12.0 in)))
199 ("arch b" . (cons (* 12.0 in) (* 18.0 in)))
200 ("arch c" . (cons (* 18.0 in) (* 24.0 in)))
201 ("arch d" . (cons (* 24.0 in) (* 36.0 in)))
202 ("arch e" . (cons (* 36.0 in) (* 48.0 in)))
203 ("arch e1" . (cons (* 30.0 in) (* 42.0 in)))
205 ;; Some are antique sizes which are still using in UK
206 ("statement" . (cons (* 5.5 in) (* 8.5 in)))
207 ("half letter" . (cons (* 5.5 in) (* 8.5 in)))
208 ("quarto" . (cons (* 8.0 in) (* 10.0 in)))
209 ("octavo" . (cons (* 6.75 in) (* 10.5 in)))
210 ("executive" . (cons (* 7.25 in) (* 10.5 in)))
211 ("monarch" . (cons (* 7.25 in) (* 10.5 in)))
212 ("foolscap" . (cons (* 8.27 in) (* 13.0 in)))
213 ("folio" . (cons (* 8.27 in) (* 13.0 in)))
214 ("super-b" . (cons (* 13.0 in) (* 19.0 in)))
215 ("post" . (cons (* 15.5 in) (* 19.5 in)))
216 ("crown" . (cons (* 15.0 in) (* 20.0 in)))
217 ("large post" . (cons (* 16.5 in) (* 21.0 in)))
218 ("demy" . (cons (* 17.5 in) (* 22.5 in)))
219 ("medium" . (cons (* 18.0 in) (* 23.0 in)))
220 ("broadsheet" . (cons (* 18.0 in) (* 24.0 in)))
221 ("royal" . (cons (* 20.0 in) (* 25.0 in)))
222 ("elephant" . (cons (* 23.0 in) (* 28.0 in)))
223 ("double demy" . (cons (* 22.5 in) (* 35.0 in)))
224 ("quad demy" . (cons (* 35.0 in) (* 45.0 in)))
225 ("atlas" . (cons (* 26.0 in) (* 34.0 in)))
226 ("imperial" . (cons (* 22.0 in) (* 30.0 in)))
227 ("antiquarian" . (cons (* 31.0 in) (* 53.0 in)))
229 ("pa0" . (cons (* 840 mm) (* 1120 mm)))
230 ("pa1" . (cons (* 560 mm) (* 840 mm)))
231 ("pa2" . (cons (* 420 mm) (* 560 mm)))
232 ("pa3" . (cons (* 280 mm) (* 420 mm)))
233 ("pa4" . (cons (* 210 mm) (* 280 mm)))
234 ("pa5" . (cons (* 140 mm) (* 210 mm)))
235 ("pa6" . (cons (* 105 mm) (* 140 mm)))
236 ("pa7" . (cons (* 70 mm) (* 105 mm)))
237 ("pa8" . (cons (* 52 mm) (* 70 mm)))
238 ("pa9" . (cons (* 35 mm) (* 52 mm)))
239 ("pa10" . (cons (* 26 mm) (* 35 mm)))
240 ;; F4 used in southeast Asia and Australia
241 ("f4" . (cons (* 210 mm) (* 330 mm)))
244 ;; todo: take dimension arguments.
245 (define (lookup-paper-name module name landscape?)
246 "Look up @var{name} and return a number pair of width and height,
247 where @var{landscape?} specifies whether the dimensions should be swapped
248 unless explicitly overriden in the name."
250 (cond ((string-suffix? "landscape" name)
252 (string-trim-right (string-drop-right name 9)))
254 ((string-suffix? "portrait" name)
256 (string-trim-right (string-drop-right name 8)))
259 (is-paper? (module-defined? module 'is-paper))
260 (entry (and is-paper?
261 (eval-carefully (assoc-get name paper-alist)
265 (if swapped? (cons (cdr entry) (car entry)) entry))))
267 (define (set-paper-dimensions m w h landscape?)
268 "M is a module (i.e. layout->scope_ )"
270 ;; page layout - what to do with (printer specific!) margin settings?
271 ((paper-default (or (lookup-paper-name
272 m (ly:get-option 'paper-size) landscape?)
274 ;; Horizontal margins, marked with #t in the cddr, are stored
275 ;; in renamed variables because they must not be overwritten.
276 ;; The cadr indicates whether a value is a vertical dimension.
277 ;; Output_def::normalize () needs to know
278 ;; whether the user set the value or not.
279 (scaleable-values '(("left-margin" #f . #t)
280 ("right-margin" #f . #t)
281 ("inner-margin" #f . #t)
282 ("outer-margin" #f . #t)
283 ("binding-offset" #f . #f)
284 ("top-margin" #t . #f)
285 ("bottom-margin" #t . #f)
287 ("short-indent" #f . #f)))
293 (string-append (car entry) "-default")))
294 (vertical? (cadr entry)))
295 (cons (if (cddr entry)
296 (string-append (car entry) "-default-scaled")
298 (round (* (if vertical? h w)
299 (/ (eval-carefully entry-symbol m 0)
300 ((if vertical? cdr car)
304 (module-define! m 'paper-width w)
305 (module-define! m 'paper-height h)
306 ;; Sometimes, lilypond-book doesn't estimate a correct line-width.
307 ;; Therefore, we need to unset line-width.
308 (module-remove! m 'line-width)
312 (let ((value-symbol (string->symbol (car value)))
313 (number (cdr value)))
314 (module-define! m value-symbol number)))
317 (define (internal-set-paper-size module name landscape?)
318 (let* ((entry (lookup-paper-name module name landscape?))
319 (is-paper? (module-defined? module 'is-paper)))
322 (ly:warning (_ "This is not a \\layout {} object, ~S") module))
324 (set-paper-dimensions module (car entry) (cdr entry) landscape?)
326 (module-define! module 'papersizename name)
327 (module-define! module 'landscape
328 (if landscape? #t #f)))
330 (ly:warning (_ "Unknown paper size: ~a") name)))))
332 (define-safe-public (set-default-paper-size name . rest)
333 (let* ((pap (module-ref (current-module) '$defaultpaper))
334 (new-paper (ly:output-def-clone pap))
335 (new-scope (ly:output-def-scope new-paper)))
336 (internal-set-paper-size
339 (memq 'landscape rest))
340 (module-set! (current-module) '$defaultpaper new-paper)))
342 (define-public (set-paper-size name . rest)
343 (if (module-defined? (current-module) 'is-paper)
344 (internal-set-paper-size (current-module) name
345 (memq 'landscape rest))
347 ;;; TODO: should raise (generic) exception with throw, and catch
348 ;;; that in parse-scm.cc
349 (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
351 (define-public (scale-layout paper scale)
352 "Return a clone of the paper, scaled by the given scale factor."
353 (let* ((new-paper (ly:output-def-clone paper))
354 (dim-vars (ly:output-def-lookup paper 'dimension-variables))
355 (old-scope (ly:output-def-scope paper))
356 (scope (ly:output-def-scope new-paper)))
360 (let* ((var (module-variable old-scope v))
361 (val (if (variable? var) (variable-ref var) #f)))
364 (module-define! scope v (/ val scale))
365 ;; Cannot warn for non-numbers, eg. for paper-width, paper-height.
369 (ly:output-def-set-variable! new-paper 'cloned #t)