]> git.donarmstrong.com Git - lilypond.git/blob - scm/paper.scm
Merge branch 'master' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[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                   '(pt mm cm in staff-height staff-space
10                        page-top-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)))
15
16 (define (calc-line-thickness staff-space pt)
17   ;; linear interpolation.
18
19   ;; !! synchronize with feta-params.mf
20   (let*
21     ((x1 (* 4.125 pt))
22      (x0 (* 5 pt))
23      (f1 (* 0.47 pt))
24      (f0 (* 0.50 pt)))
25
26     (/
27      (+
28       (* f1 (- staff-space x0))
29            (* f0 (- x1 staff-space)))
30      (- x1 x0))))
31
32 (define-public (layout-set-absolute-staff-size-in-module module staff-height)
33   (let*
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))))
39
40     (setm! 'text-font-size (* 12 factor))
41     
42     (setm! 'output-scale ss)
43     (setm! 'fonts
44                     (if tex-backend?
45                         (make-cmr-tree factor)
46                         (make-century-schoolbook-tree factor)))
47     (setm! 'staff-height staff-height)
48     (setm! 'staff-space ss)
49
50     (setm! 'line-thickness (calc-line-thickness ss pt))
51
52     ;;  sync with feta  
53     (setm! 'ledgerline-thickness (+ (* 0.5 pt) (/ ss 10)))
54
55     ;;  sync with feta  
56     (setm! 'blot-diameter (* 0.4 pt))
57     ))
58
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))
63
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"
67
68   (layout-set-absolute-staff-size (* (eval 'pt (current-module)) sz)))
69
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)))
77
78          ; maybe not necessary.
79          ; but let's be paranoid. Maybe someone still refers to the
80          ; old one. 
81          (new-paper (ly:output-def-clone pap))
82          
83          (new-scope (ly:output-def-scope new-paper)))
84     
85     (if in-layout?
86         (ly:warning (_ "set-global-staff-size: not in toplevel scope")))
87
88     (layout-set-absolute-staff-size-in-module new-scope
89                                               (* sz (eval 'pt new-scope)))
90     (module-define! current-mod '$defaultpaper new-paper)))
91
92 (define-public paper-alist
93
94   ;; don't use decimals.
95   ;; ISO 216 has a tolerance of +- 2mm
96
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)))
143     ;; ANSI sizes
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)))
158     ;; Other sizes
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     ("executive" . (cons (* 7.25 in) (* 10.5 in)))
164     ("monarch" . (cons (* 7.25 in) (* 10.5 in)))
165     ("foolscap" . (cons (* 8.27 in) (* 13.0 in)))
166     ("folio" . (cons (* 8.27 in) (* 13.0 in)))
167     ("super-b" . (cons (* 13.0 in) (* 19.0 in)))
168     ("post" . (cons (* 15.5 in) (* 19.5 in)))
169     ("crown" . (cons (* 15.0 in) (* 20.0 in)))
170     ("large post" . (cons (* 16.5 in) (* 21.0 in)))
171     ("demy" . (cons (* 17.5 in) (* 22.5 in)))
172     ("medium" . (cons (* 18.0 in) (* 23.0 in)))
173     ("broadsheet" . (cons (* 18.0 in) (* 24.0 in)))
174     ("royal" . (cons (* 20.0 in) (* 25.0 in)))
175     ("elephant" . (cons (* 23.0 in) (* 28.0 in)))
176     ("double demy" . (cons (* 22.5 in) (* 35.0 in)))
177     ("quad demy" . (cons (* 35.0 in) (* 45.0 in)))
178     ("atlas" . (cons (* 26.0 in) (* 34.0 in)))
179     ("imperial" . (cons (* 22.0 in) (* 30.0 in)))
180     ("antiquarian" . (cons (* 31.0 in) (* 53.0 in)))
181     ;; PA4 based sizes
182     ("pa0" . (cons (* 840 mm) (* 1120 mm)))
183     ("pa1" . (cons (* 560 mm) (* 840 mm)))
184     ("pa2" . (cons (* 420 mm) (* 560 mm)))
185     ("pa3" . (cons (* 280 mm) (* 420 mm)))
186     ("pa4" . (cons (* 210 mm) (* 280 mm)))
187     ("pa5" . (cons (* 140 mm) (* 210 mm)))
188     ("pa6" . (cons (* 105 mm) (* 140 mm)))
189     ("pa7" . (cons (* 70 mm) (* 105 mm)))
190     ("pa8" . (cons (* 52 mm) (* 70 mm)))
191     ("pa9" . (cons (* 35 mm) (* 52 mm)))
192     ("pa10" . (cons (* 26 mm) (* 35 mm)))
193     ;; F4 used in southeast Asia and Australia
194     ("f4" . (cons (* 210 mm) (* 330 mm)))
195    ))
196
197 ;; todo: take dimension arguments.
198
199 (define (set-paper-dimensions m w h)
200   "M is a module (i.e. layout->scope_ )"
201   (let* ((mm (eval 'mm m)))
202     (module-define! m 'paper-width w)
203     (module-define! m 'paper-height h)
204     (module-define! m 'line-width (- w
205                                      (ly:modules-lookup (list m) 'left-margin (* 10 mm))
206                                      (ly:modules-lookup (list m) 'right-margin (* 10 mm))))
207
208     (module-define! m 'indent (/ w 14))
209     (module-define! m 'short-indent (* 5 mm))
210
211     ;; page layout - what to do with (printer specific!) margin settings?
212
213     ))
214
215 (define (internal-set-paper-size module name landscape?)
216   (define (swap x)
217     (cons (cdr x) (car x)))
218   
219   (let* ((entry (assoc name paper-alist))
220          (is-paper? (module-defined? module 'is-paper))
221          (mm (eval 'mm module)))
222     
223     (cond
224      ((not is-paper?)
225       (ly:warning (_ "This is not a \\layout {} object, ~S" module)))
226      ((pair? entry)
227
228       (set! entry (eval (cdr entry) module))
229       (if landscape?
230           (set! entry (swap entry)))
231       (set-paper-dimensions module (car entry) (cdr entry))
232
233       (module-define! module 'papersizename name)
234       (module-define! module 'landscape 
235                       (if landscape? #t #f)))
236      (else
237       (ly:warning (_ "Unknown papersize: ~a" name))))))
238
239 (define-safe-public (set-default-paper-size name . rest)
240   (internal-set-paper-size
241    (ly:output-def-scope (eval '$defaultpaper (current-module)))
242    name
243    (memq 'landscape rest)))
244
245 (define-public (set-paper-size name . rest)
246   (if (module-defined? (current-module) 'is-paper)
247       (internal-set-paper-size (current-module) name
248                                (memq 'landscape rest))
249
250       ;;; TODO: should raise (generic) exception with throw, and catch
251       ;;; that in parse-scm.cc
252       (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
253
254 (define-public (scale-layout pap scale)
255   (let* ((new-pap (ly:output-def-clone pap))
256          (dim-vars (ly:output-def-lookup pap 'dimension-variables))
257          (old-scope (ly:output-def-scope pap))
258          (scope (ly:output-def-scope new-pap)))
259
260     (for-each
261      (lambda (v)
262        (let* ((var (module-variable old-scope v))
263               (val (if (variable? var) (variable-ref var) #f)))
264
265          (if (number? val)
266              (module-define! scope v
267                              (/ val scale))
268
269              ;; spurious warnings, eg. for paper-width, paper-height. 
270              ;; (ly:warning (_ "not a number, ~S = ~S " v  val))
271              )))
272      
273      dim-vars)
274     
275     new-pap))