]> git.donarmstrong.com Git - lilypond.git/blob - scm/paper.scm
Merge branch 'master' of ssh+git://hanwen@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          (pap (eval '$defaultpaper current-mod))
74          (in-layout? (or (module-defined? current-mod 'is-paper)
75                          (module-defined? current-mod 'is-layout)))
76
77          ; maybe not necessary.
78          ; but let's be paranoid. Maybe someone still refers to the
79          ; old one. 
80          (new-paper (ly:output-def-clone pap))
81          
82          (new-scope (ly:output-def-scope new-paper)))
83     
84     (if in-layout?
85         (ly:warning (_ "set-global-staff-size: not in toplevel scope")))
86
87     (layout-set-absolute-staff-size-in-module new-scope
88                                               (* sz (eval 'pt new-scope)))
89     (module-define! current-mod '$defaultpaper new-paper)))
90
91 (define-public paper-alist
92
93   ;; don't use decimals.
94   ;; ISO 216 has a tolerance of +- 2mm
95   
96   '(("a7" . (cons (* 74 mm) (* 105 mm)))
97     ("a6" . (cons (* 105 mm) (* 148 mm)))
98     ("a5" . (cons (* 148 mm) (* 210 mm)))
99     ("a4" . (cons (* 210 mm) (* 297 mm)))
100     ("a3" . (cons (* 297 mm) (* 420 mm)))
101     ("legal" . (cons (* 8.5 in) (* 14.0 in)))
102     ("letter" . (cons (* 8.5 in) (* 11.0 in)))
103     ("11x17" . (cons (* 11.0 in) (* 17.0 in)))
104     ))
105
106 ;; todo: take dimension arguments.
107
108 (define (set-paper-dimensions m w h)
109   "M is a module (i.e. layout->scope_ )"
110   (let* ((mm (eval 'mm m)))
111     (module-define! m 'paper-width w)
112     (module-define! m 'paper-height h)
113     (module-define! m 'line-width (- w
114                                      (ly:modules-lookup (list m) 'left-margin (* 10 mm))
115                                      (ly:modules-lookup (list m) 'right-margin (* 10 mm))))
116
117     (module-define! m 'indent (/ w 14))
118     (module-define! m 'short-indent (* 5 mm))
119
120     ;; page layout - what to do with (printer specific!) margin settings?
121
122     ))
123
124 (define (internal-set-paper-size module name landscape?)
125   (define (swap x)
126     (cons (cdr x) (car x)))
127   
128   (let* ((entry (assoc name paper-alist))
129          (is-paper? (module-defined? module 'is-paper))
130          (mm (eval 'mm module)))
131     
132     (cond
133      ((not is-paper?)
134       (ly:warning (_ "This is not a \\layout {} object, ~S" module)))
135      ((pair? entry)
136
137       (set! entry (eval (cdr entry) module))
138       (if landscape?
139           (set! entry (swap entry)))
140       (set-paper-dimensions module (car entry) (cdr entry))
141
142       (module-define! module 'papersizename name)
143       (module-define! module 'landscape 
144                       (if landscape? #t #f)))
145      (else
146       (ly:warning (_ "Unknown papersize: ~a" name))))))
147
148 (define-safe-public (set-default-paper-size name . rest)
149   (internal-set-paper-size
150    (ly:output-def-scope (eval '$defaultpaper (current-module)))
151    name
152    (memq 'landscape rest)))
153
154 (define-public (set-paper-size name . rest)
155   (if (module-defined? (current-module) 'is-paper)
156       (internal-set-paper-size (current-module) name
157                                (memq 'landscape rest))
158
159       ;;; TODO: should raise (generic) exception with throw, and catch
160       ;;; that in parse-scm.cc
161       (ly:warning (_ "Must use #(set-paper-size .. ) within \\paper { ... }"))))
162
163 (define-public (scale-layout pap scale)
164   (let* ((new-pap (ly:output-def-clone pap))
165          (dim-vars (ly:output-def-lookup pap 'dimension-variables))
166          (old-scope (ly:output-def-scope pap))
167          (scope (ly:output-def-scope new-pap)))
168
169     (for-each
170      (lambda (v)
171        (let* ((var (module-variable old-scope v))
172               (val (if (variable? var) (variable-ref var) #f)))
173
174          (if (number? val)
175              (module-define! scope v
176                              (/ val scale))
177
178              ;; spurious warnings, eg. for paper-width, paper-height. 
179              ;; (ly:warning (_ "not a number, ~S = ~S " v  val))
180              )))
181      
182      dim-vars)
183     
184     new-pap))