]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
Resolve.
[lilypond.git] / scm / stencil.scm
1 ;;;; stencil.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2007 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 s"
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 (dimension-arrows destination) 
129   "Draw twosided arrow from here to @var{destination}"
130   
131   (let*
132       ((e_x 1+0i)
133        (e_y 0+1i)
134        (rotate (lambda (z ang)
135                  (* (make-polar 1 ang)
136                     z)))
137        (complex-to-offset (lambda (z)
138                             (list (real-part z) (imag-part z))))
139        
140        (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
141        (e_z (/ z-dest (magnitude z-dest)))
142        (triangle-points '(-1+0.25i
143                           0
144                           -1-0.25i))
145        (p1s (map (lambda (z)
146                    (+ z-dest (rotate z (angle z-dest))))
147                  triangle-points))
148        (p2s (map (lambda (z)
149                    (rotate z (angle (- z-dest))))
150                    triangle-points))
151        (null (cons 0 0)) 
152        (arrow-1  
153         (ly:make-stencil
154          `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
155                    0.0
156                    #t) null null))
157        (arrow-2
158         (ly:make-stencil
159          `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
160                    0.0
161                    #t) null null ) )
162        (thickness 0.1)
163        (shorten-line 0.5)
164        (start (complex-to-offset (/ (* e_z shorten-line) 2)))
165        (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
166        
167        (line (ly:make-stencil
168               `(draw-line ,thickness
169                           ,(car start) ,(cadr start)
170                           ,(car end) ,(cadr end)
171                           )
172               (cons (min 0 (car destination))
173                     (min 0 (cdr destination)))
174               (cons (max 0 (car destination))
175                     (max 0 (cdr destination)))))
176                     
177        (result (ly:stencil-add arrow-2 arrow-1 line)))
178
179
180     result))
181
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;; ANNOTATIONS
184 ;;
185 ;; annotations are arrows indicating the numerical value of
186 ;; spacing variables 
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188
189 (define*-public (annotate-y-interval layout name extent is-length
190                                      #:key (color darkblue))
191   (let ((text-props (cons '((font-size . -3)
192                             (font-family . typewriter))
193                           (layout-extract-page-properties layout)))
194         (annotation #f))
195     (define (center-stencil-on-extent stil)
196       (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
197                             (cons 0 (interval-center extent))))
198     ;; do something sensible for 0,0 intervals. 
199     (set! extent (interval-widen extent 0.001))
200     (if (not (interval-sane? extent))
201         (set! annotation (interpret-markup
202                           layout text-props
203                           (make-simple-markup (format "~a: NaN/inf" name))))
204         (let ((text-stencil (interpret-markup
205                              layout text-props
206                              (markup #:whiteout #:simple name)))
207               (dim-stencil (interpret-markup
208                             layout text-props
209                             (markup #:whiteout
210                                     #:simple (cond
211                                               ((interval-empty? extent)
212                                                (format "empty"))
213                                               (is-length
214                                                (fancy-format "~$" (interval-length extent)))
215                                               (else
216                                                (fancy-format "(~$,~$)"
217                                                        (car extent) (cdr extent)))))))
218               (arrows (ly:stencil-translate-axis 
219                        (dimension-arrows (cons 0 (interval-length extent)))
220                        (interval-start extent) Y)))
221           (set! annotation
222                 (center-stencil-on-extent text-stencil))
223           (set! annotation
224                 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5 0))
225           (set! annotation
226                 (ly:stencil-combine-at-edge annotation X LEFT
227                                             (center-stencil-on-extent dim-stencil)
228                                             0.5 0))
229           (set! annotation
230                 (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
231                                  (ly:stencil-extent annotation X)
232                                  (cons 10000 -10000)))))
233     annotation))
234
235
236 (define-public (eps-file->stencil axis size file-name)
237   (let*
238       ((contents (ly:gulp-file file-name))
239        (bbox (get-postscript-bbox (car (string-split contents #\nul))))
240        (bbox-size (if (= axis X)
241                       (- (list-ref bbox 2) (list-ref bbox 0))
242                       (- (list-ref bbox 3) (list-ref bbox 1))
243                       ))
244        (factor (exact->inexact (/ size bbox-size)))
245        (scaled-bbox
246         (map (lambda (x) (* factor x)) bbox))
247        (clip-rect-string (format
248                           "~a ~a ~a ~a rectclip"
249                           (list-ref bbox 0) 
250                           (list-ref bbox 1) 
251                           (- (list-ref bbox 2) (list-ref bbox 0))
252                           (- (list-ref bbox 3) (list-ref bbox 1)))))
253     
254
255     (if bbox
256         (ly:make-stencil
257          (list
258           'embedded-ps
259           (string-append
260            (format
261            "
262 gsave
263 currentpoint translate
264 BeginEPSF
265 ~a dup scale
266 ~a 
267 %%BeginDocument: ~a
268 "          factor clip-rect-string
269
270            file-name
271            )
272            contents
273            "%%EndDocument
274 EndEPSF
275 grestore
276 "))
277         
278          (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
279          (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
280         
281         (ly:make-stencil "" '(0 . 0) '(0 . 0)))
282     ))
283
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285 ;; output signatures.
286
287 (define-public (write-system-signatures basename paper-systems count)
288   (if (pair? paper-systems)
289       (begin
290         (let*
291             ((outname (format "~a-~a.signature" basename count)) )
292              
293           (ly:message "Writing ~a" outname)
294           (write-system-signature outname (car paper-systems))
295           (write-system-signatures basename (cdr paper-systems) (1+ count))))))
296
297
298 (use-modules (scm paper-system))
299 (define-public (write-system-signature filename paper-system)
300   (define (float? x)
301     (and (number? x) (inexact? x)))
302
303   (define system-grob
304     (paper-system-system-grob paper-system))
305   
306   (define output (open-output-file filename))
307   
308   (define (strip-floats expr)
309     "Replace floats by #f"
310     (cond
311      ((float? expr) #f)
312      ((ly:font-metric? expr) (ly:font-name expr))
313      ((pair? expr) (cons (strip-floats (car expr))
314                          (strip-floats (cdr expr))))
315      (else expr)))
316
317   (define (fold-false-pairs expr)
318     "Try to remove lists of #f as much as possible."
319     (if (pair? expr)
320         (let*
321             ((first (car expr))
322              (rest (fold-false-pairs (cdr expr))))
323
324           (if first
325               (cons (fold-false-pairs first) rest)
326               rest))
327         expr))
328   
329
330   (define (pythonic-string expr)
331     "escape quotes and slashes for python consumption"
332     (regexp-substitute/global #f "([\n\\\\'\"])" (format "~a" expr) 'pre "\\" 1 'post))
333
334   (define (pythonic-pair expr)
335     (format "(~a,~a)"
336             (car expr) (cdr expr)))
337
338
339   (define (raw-string expr)
340     "escape quotes and slashes for python consumption"
341     (regexp-substitute/global #f "[@\n]" (format "~a" expr) 'pre " " 'post))
342
343   (define (raw-pair expr)
344     (format "~a ~a"
345             (car expr) (cdr expr)))
346   
347   (define (found-grob expr)
348     (let*
349         ((grob (car expr))
350          (rest (cdr expr))
351          (collected '())
352          (cause (event-cause grob))
353          (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
354          (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
355
356          (x-ext (ly:grob-extent grob system-grob X))
357          (y-ext (ly:grob-extent grob system-grob Y))
358          )
359
360       (interpret-for-signature #f (lambda (e)
361                                     (set! collected (cons e collected)))
362                                rest)
363
364       (format output
365               "~a@~a@~a@~a@~a\n"
366               (cdr (assq 'name (ly:grob-property grob 'meta) ))
367               (raw-string location)
368               (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
369               (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
370               (raw-string collected))
371       ))
372
373   (define (interpret-for-signature escape collect expr)
374     (define (interpret expr)
375       (let*
376           ((head (if (pair? expr)
377                      (car expr)
378                      #f)))
379
380         (cond
381          ((eq? head 'grob-cause) (escape (cdr expr)))
382          ((eq? head 'color) (interpret (caddr expr)))
383          ((eq? head 'rotate-stencil) (interpret (caddr expr)))
384          ((eq? head 'translate-stencil) (interpret (caddr expr)))
385          ((eq? head 'combine-stencil)
386           (for-each (lambda (e) (interpret e))  (cdr expr)))
387          (else
388           (collect (fold-false-pairs (strip-floats expr))))
389          
390          )))
391
392     (interpret expr))
393
394   (if (ly:grob? system-grob)
395       (begin
396         (display (format "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
397                  output)
398         (interpret-for-signature found-grob (lambda (x) #f)
399                                  (ly:stencil-expr
400                                   (paper-system-stencil paper-system)))))
401
402   ;; should be superfluous, but leaking "too many open files"?
403   (close-port output))
404