]> git.donarmstrong.com Git - lilypond.git/blob - scm/paper.scm
[scm] Improve formatting of `define-public' functions.
[lilypond.git] / scm / paper.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18 (define-public (set-paper-dimension-variables mod)
19   (module-define! mod 'dimension-variables
20                   '(blot-diameter
21                     bottom-margin
22                     cm
23                     horizontal-shift
24                     in
25                     indent
26                     inner-margin
27                     inner-margin-default-scaled
28                     ledger-line-thickness
29                     left-margin
30                     left-margin-default-scaled
31                     line-thickness
32                     line-width
33                     mm
34                     outer-margin
35                     outer-margin-default-scaled
36                     paper-height
37                     paper-width
38                     pt
39                     right-margin
40                     right-margin-default-scaled
41                     short-indent
42                     staff-height
43                     staff-space
44                     top-margin)))
45
46 (define (calc-line-thickness staff-space pt)
47   ;; linear interpolation.
48
49   ;; !! synchronize with feta-params.mf
50   (let*
51       ((x1 (* 4.125 pt))
52        (x0 (* 5 pt))
53        (f1 (* 0.47 pt))
54        (f0 (* 0.50 pt)))
55
56     (/
57      (+
58       (* f1 (- staff-space x0))
59       (* f0 (- x1 staff-space)))
60      (- x1 x0))))
61
62 (define-public (layout-set-absolute-staff-size-in-module module staff-height)
63   (let*
64       ((pt (eval 'pt module))
65        (ss (/ staff-height 4))
66        (factor (/ staff-height (* 20 pt)))
67        (setm! (lambda (sym val)
68                 (module-define! module sym val))))
69
70     ;; Synchronized with the `text-font-size'
71     ;; binding in add-pango-fonts (see font.scm).
72     (setm! 'text-font-size (* 11 factor))
73
74     (setm! 'output-scale ss)
75     (setm! 'fonts (make-century-schoolbook-tree factor))
76     (setm! 'staff-height staff-height)
77     (setm! 'staff-space ss)
78
79     (setm! 'line-thickness (calc-line-thickness ss pt))
80
81     ;;  sync with feta
82     (setm! 'ledger-line-thickness (+ (* 0.5 pt) (/ ss 10)))
83
84     ;;  sync with feta
85     (setm! 'blot-diameter (* 0.4 pt))
86     ))
87
88 (define-public (layout-set-absolute-staff-size sz)
89   "Set the absolute staff size inside of a @code{\\layout@{@}} block.
90 @var{sz} is in points."
91   (layout-set-absolute-staff-size-in-module (current-module) sz))
92
93 (define-public (layout-set-staff-size sz)
94   "Set the staff size inside of a @code{\\layout@{@}} block.
95 @var{sz} is in points."
96
97   (layout-set-absolute-staff-size (* (eval 'pt (current-module)) sz)))
98
99 (define-safe-public (set-global-staff-size sz)
100   "Set the default staff size, where SZ is thought to be in PT."
101   (let* ((current-mod (current-module))
102          (parser (eval 'parser current-mod))
103          (pap (ly:parser-lookup parser '$defaultpaper))
104          (in-layout? (or (module-defined? current-mod 'is-paper)
105                          (module-defined? current-mod 'is-layout)))
106
107          ;; maybe not necessary.
108          ;; but let's be paranoid. Maybe someone still refers to the
109          ;; old one.
110          (new-paper (ly:output-def-clone pap))
111
112          (new-scope (ly:output-def-scope new-paper)))
113
114     (if in-layout?
115         (ly:warning (_ "set-global-staff-size: not in toplevel scope")))
116
117     (layout-set-absolute-staff-size-in-module new-scope
118                                               (* sz (eval 'pt new-scope)))
119     (module-define! current-mod '$defaultpaper new-paper)))
120
121 (define-public paper-alist
122
123   ;; don't use decimals.
124   ;; ISO 216 has a tolerance of +- 2mm
125
126   '(("a10" . (cons (* 26 mm) (* 37 mm)))
127     ("a9" . (cons (* 37 mm) (* 52 mm)))
128     ("a8" . (cons (* 52 mm) (* 74 mm)))
129     ("a7" . (cons (* 74 mm) (* 105 mm)))
130     ("a6" . (cons (* 105 mm) (* 148 mm)))
131     ("a5" . (cons (* 148 mm) (* 210 mm)))
132     ("a4" . (cons (* 210 mm) (* 297 mm)))
133     ("a3" . (cons (* 297 mm) (* 420 mm)))
134     ("a2" . (cons (* 420 mm) (* 594 mm)))
135     ("a1" . (cons (* 594 mm) (* 841 mm)))
136     ("a0" . (cons (* 841 mm) (* 1189 mm)))
137     ("b10" . (cons (* 31 mm) (* 44 mm)))
138     ("b9" . (cons (* 44 mm) (* 62 mm)))
139     ("b8" . (cons (* 62 mm) (* 88 mm)))
140     ("b7" . (cons (* 88 mm) (* 125 mm)))
141     ("b6" . (cons (* 125 mm) (* 176 mm)))
142     ("b5" . (cons (* 176 mm) (* 250 mm)))
143     ("b4" . (cons (* 250 mm) (* 353 mm)))
144     ("b3" . (cons (* 353 mm) (* 500 mm)))
145     ("b2" . (cons (* 500 mm) (* 707 mm)))
146     ("b1" . (cons (* 707 mm) (* 1000 mm)))
147     ("b0" . (cons (* 1000 mm) (* 1414 mm)))
148     ;; Below are two extended sizes defined in DIn 476
149     ("4a0" . (cons (* 1682 mm) (* 2378 mm)))
150     ("2a0" . (cons (* 1189 mm) (* 1682 mm)))
151     ;; Below are ISO 269 standard C series
152     ("c10" . (cons (* 28 mm) (* 40 mm)))
153     ("c9" . (cons (* 40 mm) (* 57 mm)))
154     ("c8" . (cons (* 57 mm) (* 81 mm)))
155     ("c7" . (cons (* 81 mm) (* 114 mm)))
156     ("c6" . (cons (* 114 mm) (* 162 mm)))
157     ("c5" . (cons (* 162 mm) (* 229 mm)))
158     ("c4" . (cons (* 229 mm) (* 324 mm)))
159     ("c3" . (cons (* 324 mm) (* 458 mm)))
160     ("c2" . (cons (* 458 mm) (* 648 mm)))
161     ("c1" . (cons (* 648 mm) (* 917 mm)))
162     ("c0" . (cons (* 917 mm) (* 1297 mm)))
163     ;; Below are North American paper sizes
164     ("legal" . (cons (* 8.5 in) (* 14.0 in)))
165     ("letter" . (cons (* 8.5 in) (* 11.0 in)))
166     ;; Ledger (17x11) is a 90 degree rotation of Tabloid
167     ("11x17" . (cons (* 11.0 in) (* 17.0 in)))
168     ;; government-letter by IEEE Printer Working Group, for children's writing
169     ("government-letter" . (cons (* 8 in) (* 10.5 in)))
170     ("government-legal" . (cons (* 8.5 in) (* 13.0 in)))
171     ("philippine-legal" . (cons (* 8.5 in) (* 13.0 in)))
172     ;; ANSI sizes
173     ("ansi a" . (cons (* 8.5 in) (* 11.0 in)))
174     ("ansi b" . (cons (* 17.0 in) (* 11.0 in)))
175     ("ansi c" . (cons (* 17.0 in) (* 22.0 in)))
176     ("ansi d" . (cons (* 22.0 in) (* 34.0 in)))
177     ("ansi e" . (cons (* 34.0 in) (* 44.0 in)))
178     ("engineering f" . (cons (* 28.0 in) (* 40.0 in)))
179     ;; G and H are very rare, and the lengths are variable up to 90 inches
180     ;; North American Architectural sizes
181     ("arch a" . (cons (* 9.0 in) (* 12.0 in)))
182     ("arch b" . (cons (* 12.0 in) (* 18.0 in)))
183     ("arch c" . (cons (* 18.0 in) (* 24.0 in)))
184     ("arch d" . (cons (* 24.0 in) (* 36.0 in)))
185     ("arch e" . (cons (* 36.0 in) (* 48.0 in)))
186     ("arch e1" . (cons (* 30.0 in) (* 42.0 in)))
187     ;; Other sizes
188     ;; Some are antique sizes which are still using in UK
189     ("statement" . (cons (* 5.5 in) (* 8.5 in)))
190     ("half letter" . (cons (* 5.5 in) (* 8.5 in)))
191     ("quarto" . (cons (* 8.0 in) (* 10.0 in)))
192     ("octavo" . (cons (* 6.75 in) (* 10.5 in)))
193     ("executive" . (cons (* 7.25 in) (* 10.5 in)))
194     ("monarch" . (cons (* 7.25 in) (* 10.5 in)))
195     ("foolscap" . (cons (* 8.27 in) (* 13.0 in)))
196     ("folio" . (cons (* 8.27 in) (* 13.0 in)))
197     ("super-b" . (cons (* 13.0 in) (* 19.0 in)))
198     ("post" . (cons (* 15.5 in) (* 19.5 in)))
199     ("crown" . (cons (* 15.0 in) (* 20.0 in)))
200     ("large post" . (cons (* 16.5 in) (* 21.0 in)))
201     ("demy" . (cons (* 17.5 in) (* 22.5 in)))
202     ("medium" . (cons (* 18.0 in) (* 23.0 in)))
203     ("broadsheet" . (cons (* 18.0 in) (* 24.0 in)))
204     ("royal" . (cons (* 20.0 in) (* 25.0 in)))
205     ("elephant" . (cons (* 23.0 in) (* 28.0 in)))
206     ("double demy" . (cons (* 22.5 in) (* 35.0 in)))
207     ("quad demy" . (cons (* 35.0 in) (* 45.0 in)))
208     ("atlas" . (cons (* 26.0 in) (* 34.0 in)))
209     ("imperial" . (cons (* 22.0 in) (* 30.0 in)))
210     ("antiquarian" . (cons (* 31.0 in) (* 53.0 in)))
211     ;; PA4 based sizes
212     ("pa0" . (cons (* 840 mm) (* 1120 mm)))
213     ("pa1" . (cons (* 560 mm) (* 840 mm)))
214     ("pa2" . (cons (* 420 mm) (* 560 mm)))
215     ("pa3" . (cons (* 280 mm) (* 420 mm)))
216     ("pa4" . (cons (* 210 mm) (* 280 mm)))
217     ("pa5" . (cons (* 140 mm) (* 210 mm)))
218     ("pa6" . (cons (* 105 mm) (* 140 mm)))
219     ("pa7" . (cons (* 70 mm) (* 105 mm)))
220     ("pa8" . (cons (* 52 mm) (* 70 mm)))
221     ("pa9" . (cons (* 35 mm) (* 52 mm)))
222     ("pa10" . (cons (* 26 mm) (* 35 mm)))
223     ;; F4 used in southeast Asia and Australia
224     ("f4" . (cons (* 210 mm) (* 330 mm)))
225     ))
226
227 ;; todo: take dimension arguments.
228
229 (define (set-paper-dimensions m w h)
230   "M is a module (i.e. layout->scope_ )"
231   (let*
232       ;; page layout - what to do with (printer specific!) margin settings?
233       ((paper-default (eval-carefully
234                        (assoc-get
235                         (ly:get-option 'paper-size)
236                         paper-alist
237                         #f
238                         #t)
239                        m
240                        (cons w h)))
241        ;; Horizontal margins, marked with 'preserve, are stored
242        ;; in renamed variables because they must not be overwritten.
243        ;; Output_def::normalize () needs to know
244        ;; whether the user set the value or not.
245        (scaleable-values `((("left-margin" . ,w) . preserve)
246                            (("right-margin" . ,w) . preserve)
247                            (("inner-margin" . ,w) . preserve)
248                            (("outer-margin" . ,w) . preserve)
249                            (("binding-offset" . ,w) . '())
250                            (("top-margin" . ,h) . '())
251                            (("bottom-margin" . ,h) . '())
252                            (("indent" . ,w) . '())
253                            (("short-indent" . ,w) . '())))
254        (scaled-values
255         (map
256          (lambda (entry)
257            (let ((entry-symbol
258                   (string->symbol
259                    (string-append (caar entry) "-default")))
260                  (orientation (cdar entry)))
261              (if paper-default
262                  (cons (if (eq? (cdr entry) 'preserve)
263                            (string-append (caar entry) "-default-scaled")
264                            (caar entry))
265                        (round (* orientation
266                                  (/ (eval-carefully entry-symbol m 0)
267                                     (if (= orientation w)
268                                         (car paper-default)
269                                         (cdr paper-default))))))
270                  entry)))
271          scaleable-values)))
272
273     (module-define! m 'paper-width w)
274     (module-define! m 'paper-height h)
275     ;; Sometimes, lilypond-book doesn't estimate a correct line-width.
276     ;; Therefore, we need to unset line-width.
277     (module-remove! m 'line-width)
278
279     (for-each
280      (lambda (value)
281        (let ((value-symbol (string->symbol (car value)))
282              (number (cdr value)))
283          (module-define! m value-symbol number)))
284      scaled-values)))
285
286 (define (internal-set-paper-size module name landscape?)
287   (define (swap x)
288     (cons (cdr x) (car x)))
289
290   (let* ((entry (assoc-get name paper-alist))
291          (is-paper? (module-defined? module 'is-paper))
292          (mm (eval 'mm module)))
293
294     (cond
295      ((not is-paper?)
296       (ly:warning (_ "This is not a \\layout {} object, ~S") module))
297      (entry
298
299       (set! entry (eval entry module))
300       (if landscape?
301           (set! entry (swap entry)))
302       (set-paper-dimensions module (car entry) (cdr entry))
303
304       (module-define! module 'papersizename name)
305       (module-define! module 'landscape
306                       (if landscape? #t #f)))
307      (else
308       (ly:warning (_ "Unknown paper size: ~a") name)))))
309
310 (define-safe-public (set-default-paper-size name . rest)
311   (internal-set-paper-size
312    (ly:output-def-scope (eval '$defaultpaper (current-module)))
313    name
314    (memq 'landscape rest)))
315
316 (define-public (set-paper-size name . rest)
317   (if (module-defined? (current-module) 'is-paper)
318       (internal-set-paper-size (current-module) name
319                                (memq 'landscape rest))
320
321       ;;; TODO: should raise (generic) exception with throw, and catch
322       ;;; that in parse-scm.cc
323       (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
324
325 (define-public (scale-layout paper scale)
326   "Return a clone of the paper, scaled by the given scale factor."
327   (let* ((new-paper (ly:output-def-clone paper))
328          (dim-vars (ly:output-def-lookup paper 'dimension-variables))
329          (old-scope (ly:output-def-scope paper))
330          (scope (ly:output-def-scope new-paper)))
331
332     (for-each
333      (lambda (v)
334        (let* ((var (module-variable old-scope v))
335               (val (if (variable? var) (variable-ref var) #f)))
336
337          (if (number? val)
338              (module-define! scope v (/ val scale))
339              ;; Cannot warn for non-numbers, eg. for paper-width, paper-height.
340              )))
341      dim-vars)
342     ;; Mark the clone.
343     (ly:output-def-set-variable! new-paper 'cloned #t)
344     new-paper))