]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
Merge branch 'jneeman' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman
[lilypond.git] / scm / stencil.scm
1 ;;;; stencil.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7 (define-public (stack-stencils axis dir padding stils)
8   "Stack stencils STILS in direction AXIS, DIR, using PADDING."
9   (cond
10    ((null? stils) empty-stencil)
11    ((null? (cdr stils)) (car stils))
12    (else (ly:stencil-combine-at-edge
13           (car stils) axis dir (stack-stencils axis dir padding (cdr stils))
14           padding))))
15
16 (define-public (stack-stencils-padding-list axis dir padding stils)
17   "Stack stencils STILS in direction AXIS, DIR, using a list of PADDING."
18   (cond
19    ((null? stils) empty-stencil)
20    ((null? (cdr stils)) (car stils))
21    (else (ly:stencil-combine-at-edge
22           (car stils)
23           axis dir
24           (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
25           (car padding)))))
26
27 (define-public (centered-stencil stencil)
28   "Center stencil @var{stencil} in both the X and Y directions"
29   (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
30
31 (define-public (stack-lines dir padding baseline stils)
32   "Stack vertically with a baseline-skip."
33   (if (null? stils)
34       empty-stencil
35       (if (null? (cdr stils))
36           (car stils)
37           (ly:stencil-combine-at-edge
38            (car stils) Y dir 
39            (stack-lines dir padding baseline (cdr stils))
40            padding baseline))))
41
42 (define-public (bracketify-stencil stil axis thick protusion padding)
43   "Add brackets around STIL, producing a new stencil."
44
45   (let* ((ext (ly:stencil-extent stil axis))
46          (lb (ly:bracket axis ext thick (- protusion)))
47          (rb (ly:bracket axis ext thick protusion)))
48     (set! stil
49           (ly:stencil-combine-at-edge stil (other-axis axis) 1 lb padding))
50     (set! stil
51           (ly:stencil-combine-at-edge stil (other-axis axis) -1 rb padding))
52     stil))
53
54 (define-public (make-filled-box-stencil xext yext)
55   "Make a filled box."
56   
57   (ly:make-stencil
58       (list 'round-filled-box (- (car xext)) (cdr xext)
59                        (- (car yext)) (cdr yext) 0.0)
60       xext yext))
61
62 (define-public (make-circle-stencil radius thickness fill)
63   "Make a circle of radius @var{radius} and thickness @var{thickness}"
64   (ly:make-stencil
65    (list 'circle radius thickness fill) 
66    (cons (- radius) radius)
67    (cons (- radius) radius)))
68
69 (define-public (box-grob-stencil grob)
70   "Make a box of exactly the extents of the grob.  The box precisely
71 encloses the contents.
72 "
73   (let* ((xext (ly:grob-extent grob grob 0))
74          (yext (ly:grob-extent grob grob 1))
75          (thick 0.1))
76     
77     (ly:stencil-add
78      (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
79      (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
80      (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
81      (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
82
83 ;; TODO merge this and prev function. 
84 (define-public (box-stencil stencil thickness padding)
85   "Add a box around STENCIL, producing a new stencil."
86   (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
87          (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
88          (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
89          (x-rule (make-filled-box-stencil
90                   (interval-widen x-ext thickness) (cons 0 thickness))))
91     (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
92     (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
93     (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))  
94     (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
95     stencil))
96
97 (define-public (circle-stencil stencil thickness padding)
98   "Add a circle around STENCIL, producing a new stencil."
99   (let* ((x-ext (ly:stencil-extent stencil 0))
100          (y-ext (ly:stencil-extent stencil 1))
101          (diameter (max (- (cdr x-ext) (car x-ext))
102                         (- (cdr y-ext) (car y-ext))))
103          (radius (+ (/ diameter 2) padding thickness))
104          (circle (make-circle-stencil radius thickness #f)))
105
106     (ly:stencil-add
107      stencil
108      (ly:stencil-translate circle
109                            (cons
110                             (interval-center x-ext)
111                             (interval-center y-ext))))))
112
113
114 (define-public (fontify-text font-metric text)
115   "Set TEXT with font FONT-METRIC, returning a stencil."
116   (let* ((b (ly:text-dimension font-metric text)))
117     (ly:make-stencil
118      `(text ,font-metric ,text) (car b) (cdr b))))
119      
120 (define-public (fontify-text-white scale font-metric text)
121   "Set TEXT with scale factor SCALE"
122   (let* ((b (ly:text-dimension font-metric text))
123          ;;urg -- workaround for using ps font
124          (c `(white-text ,(* 2 scale) ,text)))
125     ;;urg -- extent is not from ps font, but we hope it's close
126     (ly:make-stencil c (car b) (cdr b))))
127
128 (define-public (stencil-with-color stencil color)
129   (ly:make-stencil
130    (list 'color color (ly:stencil-expr stencil))
131    (ly:stencil-extent stencil X)
132    (ly:stencil-extent stencil Y)))
133   
134 (define-public (stencil-whiteout stencil)
135   (let*
136       ((x-ext (ly:stencil-extent stencil X))
137        (y-ext (ly:stencil-extent stencil Y))
138
139        )
140     
141     (ly:stencil-add
142      (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
143                          white)
144      stencil)
145     ))
146
147 (define-public (dimension-arrows destination) 
148   "Draw twosided arrow from here to @var{destination}"
149   
150   (let*
151       ((e_x 1+0i)
152        (e_y 0+1i)
153        (rotate (lambda (z ang)
154                  (* (make-polar 1 ang)
155                     z)))
156        (complex-to-offset (lambda (z)
157                             (list (real-part z) (imag-part z))))
158        
159        (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
160        (e_z (/ z-dest (magnitude z-dest)))
161        (triangle-points '(-1+0.25i
162                           0
163                           -1-0.25i))
164        (p1s (map (lambda (z)
165                    (+ z-dest (rotate z (angle z-dest))))
166                  triangle-points))
167        (p2s (map (lambda (z)
168                    (rotate z (angle (- z-dest))))
169                    triangle-points))
170        (null (cons 0 0)) 
171        (arrow-1  
172         (ly:make-stencil
173          `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
174                    0.0
175                    #t) null null))
176        (arrow-2
177         (ly:make-stencil
178          `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
179                    0.0
180                    #t) null null ) )
181        (thickness 0.1)
182        (shorten-line 0.5)
183        (start (complex-to-offset (/ (* e_z shorten-line) 2)))
184        (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
185        
186        (line (ly:make-stencil
187               `(draw-line ,thickness
188                           ,(car start) ,(cadr start)
189                           ,(car end) ,(cadr end)
190                           )
191               (cons (min 0 (car destination))
192                     (min 0 (cdr destination)))
193               (cons (max 0 (car destination))
194                     (max 0 (cdr destination)))))
195                     
196        (result (ly:stencil-add arrow-2 arrow-1 line)))
197
198
199     result))
200
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;; ANNOTATIONS
203 ;;
204 ;; annotations are arrows indicating the numerical value of
205 ;; spacing variables 
206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207
208 (define*-public (annotate-y-interval layout name extent is-length
209                                      #:key (color darkblue))
210   (let ((text-props (cons '((font-size . -3)
211                             (font-family . typewriter))
212                           (layout-extract-page-properties layout)))
213         (annotation #f))
214     (define (center-stencil-on-extent stil)
215       (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
216                             (cons 0 (interval-center extent))))
217     ;; do something sensible for 0,0 intervals. 
218     (set! extent (interval-widen extent 0.001))
219     (if (not (interval-sane? extent))
220         (set! annotation (interpret-markup
221                           layout text-props
222                           (make-simple-markup (format "~a: NaN/inf" name))))
223         (let ((text-stencil (interpret-markup
224                              layout text-props
225                              (markup #:whiteout #:simple name)))
226               (dim-stencil (interpret-markup
227                             layout text-props
228                             (markup #:whiteout
229                                     #:simple (cond
230                                               ((interval-empty? extent)
231                                                (format "empty"))
232                                               (is-length
233                                                (format "~$" (interval-length extent)))
234                                               (else
235                                                (format "(~$,~$)"
236                                                        (car extent) (cdr extent)))))))
237               (arrows (ly:stencil-translate-axis 
238                        (dimension-arrows (cons 0 (interval-length extent)))
239                        (interval-start extent) Y)))
240           (set! annotation
241                 (center-stencil-on-extent text-stencil))
242           (set! annotation
243                 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5 0))
244           (set! annotation
245                 (ly:stencil-combine-at-edge annotation X LEFT
246                                             (center-stencil-on-extent dim-stencil)
247                                             0.5 0))
248           (set! annotation
249                 (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
250                                  (ly:stencil-extent annotation X)
251                                  (cons 10000 -10000)))))
252     annotation))
253
254
255 (define-public (eps-file->stencil axis size file-name)
256   (let*
257       ((contents (ly:gulp-file file-name))
258        (bbox (get-postscript-bbox contents))
259        (bbox-size (if (= axis X)
260                       (- (list-ref bbox 2) (list-ref bbox 0))
261                       (- (list-ref bbox 3) (list-ref bbox 1))
262                       ))
263        (factor (exact->inexact (/ size bbox-size)))
264        (scaled-bbox
265         (map (lambda (x) (* factor x)) bbox)))
266
267     (if bbox
268         (ly:make-stencil
269          (list
270           'embedded-ps
271           (string-append
272            (format
273            "
274 gsave
275 currentpoint translate
276 BeginEPSF
277 ~a ~a scale
278 %%BeginDocument: ~a
279 "          factor factor
280            file-name
281            )
282            contents
283            "%%EndDocument
284 EndEPSF
285 grestore
286 "))
287         
288          (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
289          (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
290         
291         (ly:make-stencil "" '(0 . 0) '(0 . 0)))
292     ))
293
294 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
295 ;; output signatures.
296
297 (define-public (write-system-signatures basename paper-systems count)
298   (if (pair? paper-systems)
299       (begin
300         (let*
301             ((outname (format "~a-~a.signature" basename count)) )
302              
303           (ly:message "Writing ~a" outname)
304           (write-system-signature outname (car paper-systems))
305           (write-system-signatures basename (cdr paper-systems) (1+ count))))))
306
307
308 (use-modules (scm paper-system))
309 (define-public (write-system-signature filename paper-system)
310   (define (float? x)
311     (and (number? x) (inexact? x)))
312
313   (define system-grob
314     (paper-system-system-grob paper-system))
315   
316   (define output (open-output-file filename))
317   
318   (define (strip-floats expr)
319     "Replace floats by #f"
320     (cond
321      ((float? expr) #f)
322      ((ly:font-metric? expr) (ly:font-name expr))
323      ((pair? expr) (cons (strip-floats (car expr))
324                          (strip-floats (cdr expr))))
325      (else expr)))
326
327   (define (fold-false-pairs expr)
328     "Try to remove lists of #f as much as possible."
329     (if (pair? expr)
330         (let*
331             ((first (car expr))
332              (rest (fold-false-pairs (cdr expr))))
333
334           (if first
335               (cons (fold-false-pairs first) rest)
336               rest))
337         expr))
338   
339
340   (define (pythonic-string expr)
341     "escape quotes and slashes for python consumption"
342     (regexp-substitute/global #f "([\n\\\\'\"])" (format "~a" expr) 'pre "\\" 1 'post))
343
344   (define (pythonic-pair expr)
345     (format "(~a,~a)"
346             (car expr) (cdr expr)))
347
348
349   (define (raw-string expr)
350     "escape quotes and slashes for python consumption"
351     (regexp-substitute/global #f "[@\n]" (format "~a" expr) 'pre " " 'post))
352
353   (define (raw-pair expr)
354     (format "~a ~a"
355             (car expr) (cdr expr)))
356   
357   (define (found-grob expr)
358     (let*
359         ((grob (car expr))
360          (rest (cdr expr))
361          (collected '())
362          (cause (event-cause grob))
363          (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
364          (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
365
366          (x-ext (ly:grob-extent grob system-grob X))
367          (y-ext (ly:grob-extent grob system-grob Y))
368          )
369
370       (interpret-for-signature #f (lambda (e)
371                                     (set! collected (cons e collected)))
372                                rest)
373
374       (format output
375               "~a@~a@~a@~a@~a\n"
376               (cdr (assq 'name (ly:grob-property grob 'meta) ))
377               (raw-string location)
378               (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
379               (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
380               (raw-string collected))
381       ))
382
383   (define (interpret-for-signature escape collect expr)
384     (define (interpret expr)
385       (let*
386           ((head (if (pair? expr)
387                      (car expr)
388                      #f)))
389
390         (cond
391          ((eq? head 'grob-cause) (escape (cdr expr)))
392          ((eq? head 'color) (interpret (caddr expr)))
393          ((eq? head 'rotate-stencil) (interpret (caddr expr)))
394          ((eq? head 'translate-stencil) (interpret (caddr expr)))
395          ((eq? head 'combine-stencil)
396           (for-each (lambda (e) (interpret e))  (cdr expr)))
397          (else
398           (collect (fold-false-pairs (strip-floats expr))))
399          
400          )))
401
402     (interpret expr))
403
404   (if (ly:grob? system-grob)
405       (begin
406         (display (format "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
407                  output)
408         (interpret-for-signature found-grob (lambda (x) #f)
409                                  (ly:stencil-expr
410                                   (paper-system-stencil paper-system)))))
411
412   ;; should be superfluous, but leaking "too many open files"?
413   (close-port output))
414