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