3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2003--2007 Han-Wen Nienhuys <hanwen@xs4all.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
24 (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
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))
31 (define-public (stack-lines dir padding baseline stils)
32 "Stack vertically with a baseline-skip."
33 (define result empty-stencil)
36 ((last-stencil #f (car p))
43 (let* ((dy (max (+ (* dir (interval-bound (ly:stencil-extent last-stencil Y) dir))
45 (* (- dir) (interval-bound (ly:stencil-extent (car p) Y) (- dir))))
47 (y (+ last-y (* dir dy))))
52 (ly:stencil-add result (ly:stencil-translate-axis (car p) y Y)))
56 (set! result (car p)))))
61 (define-public (bracketify-stencil stil axis thick protusion padding)
62 "Add brackets around STIL, producing a new stencil."
64 (let* ((ext (ly:stencil-extent stil axis))
65 (lb (ly:bracket axis ext thick (- protusion)))
66 (rb (ly:bracket axis ext thick protusion)))
68 (ly:stencil-combine-at-edge stil (other-axis axis) 1 lb padding))
70 (ly:stencil-combine-at-edge stil (other-axis axis) -1 rb padding))
73 (define-public (make-filled-box-stencil xext yext)
77 (list 'round-filled-box (- (car xext)) (cdr xext)
78 (- (car yext)) (cdr yext) 0.0)
81 (define-public (make-circle-stencil radius thickness fill)
82 "Make a circle of radius @var{radius} and thickness @var{thickness}"
84 (list 'circle radius thickness fill)
85 (cons (- radius) radius)
86 (cons (- radius) radius)))
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.
92 (let* ((xext (ly:grob-extent grob grob 0))
93 (yext (ly:grob-extent grob grob 1))
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))))
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))
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)))
127 (ly:stencil-translate circle
129 (interval-center x-ext)
130 (interval-center y-ext))))))
133 (define-public (fontify-text font-metric text)
134 "Set TEXT with font FONT-METRIC, returning a stencil."
135 (let* ((b (ly:text-dimension font-metric text)))
137 `(text ,font-metric ,text) (car b) (cdr b))))
139 (define-public (fontify-text-white scale font-metric text)
140 "Set TEXT with scale factor SCALE"
141 (let* ((b (ly:text-dimension font-metric text))
142 ;;urg -- workaround for using ps font
143 (c `(white-text ,(* 2 scale) ,text)))
144 ;;urg -- extent is not from ps font, but we hope it's close
145 (ly:make-stencil c (car b) (cdr b))))
147 (define-public (stencil-with-color stencil color)
149 (list 'color color (ly:stencil-expr stencil))
150 (ly:stencil-extent stencil X)
151 (ly:stencil-extent stencil Y)))
153 (define-public (stencil-whiteout stencil)
155 ((x-ext (ly:stencil-extent stencil X))
156 (y-ext (ly:stencil-extent stencil Y))
161 (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
166 (define-public (dimension-arrows destination)
167 "Draw twosided arrow from here to @var{destination}"
172 (rotate (lambda (z ang)
173 (* (make-polar 1 ang)
175 (complex-to-offset (lambda (z)
176 (list (real-part z) (imag-part z))))
178 (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
179 (e_z (/ z-dest (magnitude z-dest)))
180 (triangle-points '(-1+0.25i
183 (p1s (map (lambda (z)
184 (+ z-dest (rotate z (angle z-dest))))
186 (p2s (map (lambda (z)
187 (rotate z (angle (- z-dest))))
192 `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
197 `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
202 (start (complex-to-offset (/ (* e_z shorten-line) 2)))
203 (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
205 (line (ly:make-stencil
206 `(draw-line ,thickness
207 ,(car start) ,(cadr start)
208 ,(car end) ,(cadr end)
210 (cons (min 0 (car destination))
211 (min 0 (cdr destination)))
212 (cons (max 0 (car destination))
213 (max 0 (cdr destination)))))
215 (result (ly:stencil-add arrow-2 arrow-1 line)))
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; annotations are arrows indicating the numerical value of
225 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 (define*-public (annotate-y-interval layout name extent is-length
228 #:key (color darkblue))
229 (let ((text-props (cons '((font-size . -3)
230 (font-family . typewriter))
231 (layout-extract-page-properties layout)))
233 (define (center-stencil-on-extent stil)
234 (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
235 (cons 0 (interval-center extent))))
236 ;; do something sensible for 0,0 intervals.
237 (set! extent (interval-widen extent 0.001))
238 (if (not (interval-sane? extent))
239 (set! annotation (interpret-markup
241 (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
242 (let ((text-stencil (interpret-markup
244 (markup #:whiteout #:simple name)))
245 (dim-stencil (interpret-markup
249 ((interval-empty? extent)
252 (ly:format "~$" (interval-length extent)))
255 (car extent) (cdr extent)))))))
256 (arrows (ly:stencil-translate-axis
257 (dimension-arrows (cons 0 (interval-length extent)))
258 (interval-start extent) Y)))
260 (center-stencil-on-extent text-stencil))
262 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
264 (ly:stencil-combine-at-edge annotation X LEFT
265 (center-stencil-on-extent dim-stencil)
268 (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
269 (ly:stencil-extent annotation X)
270 (cons 10000 -10000)))))
274 (define-public (eps-file->stencil axis size file-name)
276 ((contents (ly:gulp-file file-name))
277 (bbox (get-postscript-bbox (car (string-split contents #\nul))))
278 (bbox-size (if (= axis X)
279 (- (list-ref bbox 2) (list-ref bbox 0))
280 (- (list-ref bbox 3) (list-ref bbox 1))
282 (factor (if (< 0 bbox-size)
283 (exact->inexact (/ size bbox-size))
286 (map (lambda (x) (* factor x)) bbox))
287 (clip-rect-string (ly:format
288 "~a ~a ~a ~a rectclip"
291 (- (list-ref bbox 2) (list-ref bbox 0))
292 (- (list-ref bbox 3) (list-ref bbox 1)))))
303 currentpoint translate
308 " factor clip-rect-string
318 (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
319 (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
321 (ly:make-stencil "" '(0 . 0) '(0 . 0)))
324 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
325 ;; output signatures.
327 (define-public (write-system-signatures basename paper-systems count)
328 (if (pair? paper-systems)
331 ((outname (simple-format #f "~a-~a.signature" basename count)) )
333 (ly:message "Writing ~a" outname)
334 (write-system-signature outname (car paper-systems))
335 (write-system-signatures basename (cdr paper-systems) (1+ count))))))
337 (use-modules (scm paper-system))
338 (define-public (write-system-signature filename paper-system)
340 (and (number? x) (inexact? x)))
343 (paper-system-system-grob paper-system))
345 (define output (open-output-file filename))
347 ;; todo: optionally use a command line flag? Or just junk this?
348 (define compare-expressions #f)
349 (define (strip-floats expr)
350 "Replace floats by #f"
353 ((ly:font-metric? expr) (ly:font-name expr))
354 ((pair? expr) (cons (strip-floats (car expr))
355 (strip-floats (cdr expr))))
358 (define (fold-false-pairs expr)
359 "Try to remove lists of #f as much as possible."
363 (rest (fold-false-pairs (cdr expr))))
366 (cons (fold-false-pairs first) rest)
370 (define (raw-string expr)
371 "escape quotes and slashes for python consumption"
372 (regexp-substitute/global #f "[@\n]" (simple-format #f "~a" expr) 'pre " " 'post))
374 (define (raw-pair expr)
375 (simple-format #f "~a ~a"
376 (car expr) (cdr expr)))
378 (define (found-grob expr)
383 (cause (event-cause grob))
384 (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
385 (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
387 ;; todo: use stencil extent if available.
388 (x-ext (ly:grob-extent grob system-grob X))
389 (y-ext (ly:grob-extent grob system-grob Y))
391 (if compare-expressions
392 (interpret-for-signature
394 (set! collected (cons e collected)))
398 (simple-format output
400 (cdr (assq 'name (ly:grob-property grob 'meta) ))
401 (raw-string location)
402 (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
403 (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
404 (raw-string collected))
407 (define (interpret-for-signature escape collect expr)
408 (define (interpret expr)
410 ((head (if (pair? expr)
415 ((eq? head 'grob-cause) (escape (cdr expr)))
416 ((eq? head 'color) (interpret (caddr expr)))
417 ((eq? head 'rotate-stencil) (interpret (caddr expr)))
418 ((eq? head 'translate-stencil) (interpret (caddr expr)))
419 ((eq? head 'combine-stencil)
420 (for-each (lambda (e) (interpret e)) (cdr expr)))
422 (collect (fold-false-pairs (strip-floats expr))))
428 (if (ly:grob? system-grob)
430 (display (simple-format #f "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
432 (interpret-for-signature found-grob (lambda (x) #f)
434 (paper-system-stencil paper-system)))))
436 ;; should be superfluous, but leaking "too many open files"?