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