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