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