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