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