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