3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2003--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 (define-public (stack-stencils axis dir padding stils)
8 "Stack stencils STILS in direction AXIS, DIR, using PADDING."
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))
16 (define-public (stack-stencils-padding-list axis dir padding stils)
17 "Stack stencils STILS in direction AXIS, DIR, using a list of PADDING."
19 ((null? stils) empty-stencil)
20 ((null? (cdr stils)) (car stils))
21 (else (ly:stencil-combine-at-edge
22 (car stils) axis dir (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
25 (define-public (centered-stencil stencil)
26 "Center stencil @var{stencil} in both the X and Y directions"
27 (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
29 (define-public (stack-lines dir padding baseline stils)
30 "Stack vertically with a baseline-skip."
33 (if (null? (cdr stils))
35 (ly:stencil-combine-at-edge
37 (stack-lines dir padding baseline (cdr stils))
40 (define-public (bracketify-stencil stil axis thick protusion padding)
41 "Add brackets around STIL, producing a new stencil."
43 (let* ((ext (ly:stencil-extent stil axis))
44 (lb (ly:bracket axis ext thick (- protusion)))
45 (rb (ly:bracket axis ext thick protusion)))
47 (ly:stencil-combine-at-edge stil (other-axis axis) 1 lb padding))
49 (ly:stencil-combine-at-edge stil (other-axis axis) -1 rb padding))
52 (define-public (make-filled-box-stencil xext yext)
56 (list 'round-filled-box (- (car xext)) (cdr xext)
57 (- (car yext)) (cdr yext) 0.0)
60 (define-public (make-circle-stencil radius thickness fill)
61 "Make a circle of radius @var{radius} and thickness @var{thickness}"
63 (list 'circle radius thickness fill)
64 (cons (- radius) radius)
65 (cons (- radius) radius)))
67 (define-public (box-grob-stencil grob)
68 "Make a box of exactly the extents of the grob. The box precisely
69 encloses the contents.
71 (let* ((xext (ly:grob-extent grob grob 0))
72 (yext (ly:grob-extent grob grob 1))
76 (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
77 (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
78 (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
79 (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
81 ;; TODO merge this and prev function.
82 (define-public (box-stencil stencil thickness padding)
83 "Add a box around STENCIL, producing a new stencil."
84 (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
85 (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
86 (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
87 (x-rule (make-filled-box-stencil
88 (interval-widen x-ext thickness) (cons 0 thickness))))
89 (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
90 (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
91 (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))
92 (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
95 (define-public (circle-stencil stencil thickness padding)
96 "Add a circle around STENCIL, producing a new stencil."
97 (let* ((x-ext (ly:stencil-extent stencil 0))
98 (y-ext (ly:stencil-extent stencil 1))
99 (diameter (max (- (cdr x-ext) (car x-ext))
100 (- (cdr y-ext) (car y-ext))))
101 (radius (+ (/ diameter 2) padding thickness)))
103 (centered-stencil stencil) (make-circle-stencil radius thickness #f))))
105 (define-public (fontify-text font-metric text)
106 "Set TEXT with font FONT-METRIC, returning a stencil."
107 (let* ((b (ly:text-dimension font-metric text)))
109 `(text ,font-metric ,text) (car b) (cdr b))))
111 (define-public (fontify-text-white scale font-metric text)
112 "Set TEXT with scale factor s"
113 (let* ((b (ly:text-dimension font-metric text))
114 ;;urg -- workaround for using ps font
115 (c `(white-text ,(* 2 scale) ,text)))
116 ;;urg -- extent is not from ps font, but we hope it's close
117 (ly:make-stencil c (car b) (cdr b))))
119 (define-public (dimension-arrows destination)
120 "Draw twosided arrow from here to @var{destination}"
125 (rotate (lambda (z ang)
126 (* (make-polar 1 ang)
128 (complex-to-offset (lambda (z)
129 (list (real-part z) (imag-part z))))
131 (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
132 (e_z (/ z-dest (magnitude z-dest)))
133 (triangle-points '(-1+0.25i
136 (p1s (map (lambda (z)
137 (+ z-dest (rotate z (angle z-dest))))
139 (p2s (map (lambda (z)
140 (rotate z (angle (- z-dest))))
145 `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
150 `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
155 (start (complex-to-offset (/ (* e_z shorten-line) 2)))
156 (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
158 (line (ly:make-stencil
159 `(draw-line ,thickness
160 ,(car start) ,(cadr start)
161 ,(car end) ,(cadr end)
163 (cons (min 0 (car destination))
164 (min 0 (cdr destination)))
165 (cons (max 0 (car destination))
166 (max 0 (cdr destination)))))
168 (result (ly:stencil-add arrow-2 arrow-1 line)))
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;; annotations are arrows indicating the numerical value of
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 (define-public (annotate-y-interval layout name extent is-length)
184 (font-family . typewriter))
185 (layout-extract-page-properties layout)))
189 ;; do something sensible for 0,0 intervals.
190 (set! extent (interval-widen extent 0.001))
191 (if (not (interval-sane? extent))
192 (set! annotation (interpret-markup layout text-props
193 (make-simple-markup (format "~a: NaN/inf" name))))
195 ((text-stencil (interpret-markup
199 (make-whiteout-markup (make-simple-markup name))
200 (make-whiteout-markup
203 ((interval-empty? extent) "empty")
204 (is-length (format "~$" (interval-length extent)))
206 (format "(~$,~$)" (car extent)
207 (cdr extent))))))))))
209 (ly:stencil-translate-axis
210 (dimension-arrows (cons 0 (interval-length extent)))
211 (interval-start extent) Y)))
214 (ly:stencil-aligned-to text-stencil Y CENTER))
216 (set! annotation (ly:stencil-translate
218 (cons 0 (interval-center extent))))
222 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5 0))
225 (ly:make-stencil (ly:stencil-expr annotation)
226 (ly:stencil-extent annotation X)
227 (cons 10000 -10000)))))
231 (define-public (eps-file->stencil axis size file-name)
233 ((contents (ly:gulp-file file-name))
234 (bbox (get-postscript-bbox contents))
235 (bbox-size (if (= axis X)
236 (- (list-ref bbox 2) (list-ref bbox 0))
237 (- (list-ref bbox 3) (list-ref bbox 1))
239 (factor (exact->inexact (/ size bbox-size)))
241 (map (lambda (x) (* factor x)) bbox)))
259 (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
260 (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
262 (ly:make-stencil "" '(0 . 0) '(0 . 0)))
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;; output signatures.
268 (define-public (write-system-signatures basename paper-systems count)
269 (if (pair? paper-systems)
272 ((outname (format "~a-~a.signature" basename count)) )
274 (ly:message "Writing ~a" outname)
275 (write-system-signature outname (car paper-systems))
276 (write-system-signatures basename (cdr paper-systems) (1+ count))))))
279 (define-public (write-system-signature filename paper-system)
281 (and (number? x) (inexact? x)))
284 (paper-system-system-grob paper-system))
286 (define output (open-output-file filename))
288 (define (strip-floats expr)
289 "Replace floats by #f"
292 ((ly:font-metric? expr) (ly:font-name expr))
293 ((pair? expr) (cons (strip-floats (car expr))
294 (strip-floats (cdr expr))))
297 (define (fold-false-pairs expr)
298 "Try to remove lists of #f as much as possible."
302 (rest (fold-false-pairs (cdr expr))))
305 (cons (fold-false-pairs first) rest)
309 (define (music-cause grob)
311 ((cause (ly:grob-property grob 'cause)))
314 ((ly:music? cause) cause)
315 ((ly:grob? cause) (music-cause cause))
318 (define (pythonic-string expr)
319 "escape quotes and slashes for python consumption"
320 (regexp-substitute/global #f "([\n\\\\'\"])" (format "~a" expr) 'pre "\\" 1 'post))
322 (define (pythonic-pair expr)
324 (car expr) (cdr expr)))
326 (define (found-grob expr)
331 (cause (music-cause grob))
332 (input (if (ly:music? cause) (ly:music-property cause 'origin) #f))
333 (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
335 (x-ext (ly:grob-extent grob system-grob X))
336 (y-ext (ly:grob-extent grob system-grob Y))
339 (interpret-for-signature #f (lambda (e)
340 (set! collected (cons e collected)))
344 "['~a', '~a', ~a, ~a, '~a'],\n"
345 (cdr (assq 'name (ly:grob-property grob 'meta) ))
346 (pythonic-string location)
347 (pythonic-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
348 (pythonic-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
349 (pythonic-string collected))
352 (define (interpret-for-signature escape collect expr)
353 (define (interpret expr)
355 ((head (if (pair? expr)
360 ((eq? head 'grob-cause) (escape (cdr expr)))
361 ((eq? head 'color) (interpret (caddr expr)))
362 ((eq? head 'rotate-stencil) (interpret (caddr expr)))
363 ((eq? head 'translate-stencil) (interpret (caddr expr)))
364 ((eq? head 'combine-stencil)
365 (for-each (lambda (e) (interpret e)) (cdr expr)))
367 (collect (fold-false-pairs (strip-floats expr))))
373 (if (ly:grob? system-grob)
376 "# Output signature\n# Generated by LilyPond ~a on ~a\n"
378 (strftime "%c" (localtime (current-time)))
380 (interpret-for-signature found-grob (lambda (x) #f)
382 (paper-system-stencil paper-system))))))