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