1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2003--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
18 (define (make-bezier-sandwich-stencil coords thick xext yext)
19 (let* ((command-list `(moveto
20 ,(car (list-ref coords 3))
21 ,(cdr (list-ref coords 3))
23 ,(car (list-ref coords 0))
24 ,(cdr (list-ref coords 0))
25 ,(car (list-ref coords 1))
26 ,(cdr (list-ref coords 1))
27 ,(car (list-ref coords 2))
28 ,(cdr (list-ref coords 2))
30 ,(car (list-ref coords 4))
31 ,(cdr (list-ref coords 4))
32 ,(car (list-ref coords 5))
33 ,(cdr (list-ref coords 5))
34 ,(car (list-ref coords 6))
35 ,(cdr (list-ref coords 6))
38 `(path ,thick `(,@' ,command-list) 'round 'round #t)
42 (define-public (stack-stencils axis dir padding stils)
43 "Stack stencils @var{stils} in direction @var{axis}, @var{dir}, using
46 ((null? stils) empty-stencil)
47 ((null? (cdr stils)) (car stils))
48 (else (ly:stencil-combine-at-edge
49 (car stils) axis dir (stack-stencils axis dir padding (cdr stils))
52 (define-public (stack-stencils-padding-list axis dir padding stils)
53 "Stack stencils @var{stils} in direction @var{axis}, @var{dir}, using
54 a list of @var{padding}."
56 ((null? stils) empty-stencil)
57 ((null? (cdr stils)) (car stils))
58 (else (ly:stencil-combine-at-edge
61 (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
64 (define-public (centered-stencil stencil)
65 "Center stencil @var{stencil} in both the X and Y directions."
66 (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
68 (define-public (stack-lines dir padding baseline stils)
69 "Stack vertically with a baseline skip."
70 (define result empty-stencil)
73 ((last-stencil #f (car p))
80 (let* ((dy (max (+ (* dir (interval-bound (ly:stencil-extent last-stencil Y) dir))
82 (* (- dir) (interval-bound (ly:stencil-extent (car p) Y) (- dir))))
84 (y (+ last-y (* dir dy))))
89 (ly:stencil-add result (ly:stencil-translate-axis (car p) y Y)))
93 (set! result (car p)))))
98 (define-public (bracketify-stencil stil axis thick protrusion padding)
99 "Add brackets around @var{stil}, producing a new stencil."
101 (let* ((ext (ly:stencil-extent stil axis))
102 (lb (ly:bracket axis ext thick protrusion))
103 (rb (ly:bracket axis ext thick (- protrusion))))
105 (ly:stencil-combine-at-edge stil (other-axis axis) 1 rb padding))
107 (ly:stencil-combine-at-edge lb (other-axis axis) 1 stil padding))
110 (define (make-parenthesis-stencil
111 y-extent half-thickness width angularity)
112 "Create a parenthesis stencil.
113 @var{y-extent} is the Y extent of the markup inside the parenthesis.
114 @var{half-thickness} is the half thickness of the parenthesis.
115 @var{width} is the width of a parenthesis.
116 The higher the value of number @var{angularity},
117 the more angular the shape of the parenthesis."
118 (let* ((line-width 0.1)
119 ;; Horizontal position of baseline that end points run through.
124 ;; X value farthest from baseline on outside of curve
125 (outer-x (+ base-x width))
126 ;; X extent of bezier sandwich centerline curves
127 (x-extent (ordered-cons base-x outer-x))
128 (bottom-y (interval-start y-extent))
129 (top-y (interval-end y-extent))
131 (lower-end-point (cons base-x bottom-y))
132 (upper-end-point (cons base-x top-y))
134 (outer-control-x (+ base-x (* 4/3 width)))
135 (inner-control-x (+ outer-control-x
138 (- half-thickness))))
140 ;; Vertical distance between a control point
141 ;; and the end point it connects to.
142 (offset-index (- (* 0.6 angularity) 0.8))
143 (lower-control-y (interval-index y-extent offset-index))
144 (upper-control-y (interval-index y-extent (- offset-index)))
146 (lower-outer-control-point
147 (cons outer-control-x lower-control-y))
148 (upper-outer-control-point
149 (cons outer-control-x upper-control-y))
150 (upper-inner-control-point
151 (cons inner-control-x upper-control-y))
152 (lower-inner-control-point
153 (cons inner-control-x lower-control-y)))
155 (make-bezier-sandwich-stencil
157 ;; Step 4: curve through inner control points
158 ;; to lower end point.
159 upper-inner-control-point
160 lower-inner-control-point
162 ;; Step 3: move to upper end point.
164 ;; Step 2: curve through outer control points
165 ;; to upper end point.
166 lower-outer-control-point
167 upper-outer-control-point
169 ;; Step 1: move to lower end point.
172 (interval-widen x-extent (/ line-width 2))
173 (interval-widen y-extent (/ line-width 2)))))
175 (define-public (parenthesize-stencil
176 stencil half-thickness width angularity padding)
177 "Add parentheses around @var{stencil}, returning a new stencil."
178 (let* ((y-extent (ly:stencil-extent stencil Y))
179 (lp (make-parenthesis-stencil
180 y-extent half-thickness (- width) angularity))
181 (rp (make-parenthesis-stencil
182 y-extent half-thickness width angularity)))
183 (set! stencil (ly:stencil-combine-at-edge lp X RIGHT stencil padding))
184 (set! stencil (ly:stencil-combine-at-edge stencil X RIGHT rp padding))
187 (define-public (make-line-stencil width startx starty endx endy)
188 "Make a line stencil of given linewidth and set its extents accordingly."
189 (let ((xext (cons (min startx endx) (max startx endx)))
190 (yext (cons (min starty endy) (max starty endy))))
192 (list 'draw-line width startx starty endx endy)
193 ; Since the line has rounded edges, we have to / can safely add half the
194 ; width to all coordinates!
195 (interval-widen xext (/ width 2))
196 (interval-widen yext (/ width 2)))))
199 (define-public (make-filled-box-stencil xext yext)
203 (list 'round-filled-box (- (car xext)) (cdr xext)
204 (- (car yext)) (cdr yext) 0.0)
207 (define-public (make-circle-stencil radius thickness fill)
208 "Make a circle of radius @var{radius} and thickness @var{thickness}."
210 ((out-radius (+ radius (/ thickness 2.0))))
213 (list 'circle radius thickness fill)
214 (cons (- out-radius) out-radius)
215 (cons (- out-radius) out-radius))))
217 (define-public (make-oval-stencil x-radius y-radius thickness fill)
218 "Make an oval from two Bezier curves, of x@tie{}radius @var{x-radius},
219 y@tie{}radius @code{y-radius}, and thickness @var{thickness} with fill
220 defined by @code{fill}."
222 ((x-out-radius (+ x-radius (/ thickness 2.0)))
223 (y-out-radius (+ y-radius (/ thickness 2.0)))
228 (commands `(,(list 'moveto x-max 0)
229 ,(list 'curveto x-max y-max x-min y-max x-min 0)
230 ,(list 'curveto x-min y-min x-max y-min x-max 0)
232 (command-list (fold-right append '() commands)))
234 `(path ,thickness `(,@',command-list) 'round 'round ,fill)
235 (cons (- x-out-radius) x-out-radius)
236 (cons (- y-out-radius) y-out-radius))))
239 (make-partial-ellipse-stencil
240 x-radius y-radius start-angle end-angle thick connect fill)
242 (define (make-radius-list x-radius y-radius)
245 (map (lambda (quadrant)
246 (cons (+ adder (car quadrant))
248 `((0.0 . (,x-radius . 0.0))
249 (,PI-OVER-TWO . (0.0 . ,y-radius))
250 (,PI . (,(- x-radius) . 0.0))
251 (,THREE-PI-OVER-TWO . (0.0 . ,(- y-radius))))))
255 (insert-in-ordered-list ordering-function value inlist cutl? cutr?)
257 (helper ordering-function value left-list right-list cutl? cutr?)
258 (if (null? right-list)
260 (if cutl? '() left-list)
262 (if cutr? '() right-list))
263 (if (ordering-function value (car right-list))
265 (if cutl? '() left-list)
267 (if cutr? '() right-list))
271 (append left-list (list (car right-list)))
275 (helper ordering-function value '() inlist cutl? cutr?))
277 (define (ordering-function-1 a b) (car< a b))
279 (define (ordering-function-2 a b) (car<= a b))
281 (define (min-max-crawler min-max side l)
283 (if (eq? min-max min) 100000 -100000)
284 (map (lambda (x) (side x)) l)))
287 ((x-out-radius (+ x-radius (/ thick 2.0)))
288 (y-out-radius (+ y-radius (/ thick 2.0)))
289 (new-end-angle (angle-0-2pi (degrees->radians end-angle)))
290 (end-radius (ellipse-radius x-out-radius y-out-radius new-end-angle))
291 (new-start-angle (angle-0-2pi (degrees->radians start-angle)))
292 (start-radius (ellipse-radius x-out-radius y-out-radius new-start-angle))
293 (radius-list (make-radius-list x-out-radius y-out-radius))
294 (rectangular-end-radius (polar->rectangular end-radius end-angle))
295 (rectangular-start-radius (polar->rectangular start-radius start-angle))
297 (if (<= new-end-angle new-start-angle)
298 (+ TWO-PI new-end-angle)
301 (insert-in-ordered-list
303 (cons new-end-angle rectangular-end-radius)
304 (insert-in-ordered-list
306 (cons new-start-angle rectangular-start-radius)
322 (cons (min-max-crawler min cadr possible-extrema)
323 (min-max-crawler max cadr possible-extrema))
324 (cons (min-max-crawler min cddr possible-extrema)
325 (min-max-crawler max cddr possible-extrema)))))
327 (define (line-part-min-max x1 x2)
328 (list (min x1 x2) (max x1 x2)))
330 (define (bezier-part-min-max x1 x2 x3 x4)
331 ((lambda (x) (list (reduce min 10000 x) (reduce max -10000 x)))
334 (+ (* x1 (expt (- 1 x) 3))
335 (+ (* 3 (* x2 (* (expt (- 1 x) 2) x)))
336 (+ (* 3 (* x3 (* (- 1 x) (expt x 2))))
337 (* x4 (expt x 3))))))
338 (if (< (+ (expt x2 2) (+ (expt x3 2) (* x1 x4)))
339 (+ (* x1 x3) (+ (* x2 x4) (* x2 x3))))
342 (lambda (x) (and (>= x 0) (<= x 1)))
347 (exact->inexact (- (+ x1 (* 3 x3)) (+ x4 (* 3 x2))))))
348 ;; Zeros of the bezier curve
349 (/ (+ (- x1 (* 2 x2))
351 (sqrt (- (+ (expt x2 2)
352 (+ (expt x3 2) (* x1 x4)))
354 (+ (* x2 x4) (* x2 x3)))))))
355 (- (+ x1 (* 3 x3)) (+ x4 (* 3 x2))))
356 ;; Apply L'hopital's rule to get the zeros if 0/0
359 (sqrt (- (+ (* x2 x2)
360 (+ (* x3 x3) (* x1 x4)))
362 (+ (* x2 x4) (* x2 x3)))))))))
365 (define (bezier-min-max x1 y1 x2 y2 x3 y3 x4 y4)
367 (apply bezier-part-min-max x))
368 `((,x1 ,x2 ,x3 ,x4) (,y1 ,y2 ,y3 ,y4))))
370 (define (line-min-max x1 y1 x2 y2)
372 (apply line-part-min-max x))
373 `((,x1 ,x2) (,y1 ,y2))))
375 (define (path-min-max origin pointlist)
379 (reduce min +inf.0 (map caar x))
380 (reduce max -inf.0 (map cadar x))
381 (reduce min +inf.0 (map caadr x))
382 (reduce max -inf.0 (map cadadr x))))
385 (apply bezier-min-max x)
386 (apply line-min-max x)))
388 (append (list (cadr (reverse x)) (car (reverse x))) y))
389 (append (list origin)
390 (reverse (cdr (reverse pointlist)))) pointlist))))
392 (define-public (make-connected-path-stencil pointlist thickness
393 x-scale y-scale connect fill)
394 "Make a connected path described by the list @var{pointlist}, with
395 thickness @var{thickness}, and scaled by @var{x-scale} in the X direction
396 and @var{y-scale} in the Y direction. @var{connect} and @var{fill} are
397 boolean arguments that specify if the path should be connected or filled,
400 ;; paths using this routine are designed to begin at point '(0 . 0)
401 (let* ((origin (list 0 0))
402 (boundlist (path-min-max origin pointlist))
403 ;; modify pointlist to scale the coordinates
404 (path (map (lambda (x)
407 (lambda (x1 x2 x3 x4 x5 x6)
421 ;; a path must begin with a `moveto'
422 (prepend-origin (apply list (cons 'moveto origin) path))
423 ;; if this path is connected, add closepath to the end
424 (final-path (if connect
425 (append prepend-origin (list 'closepath))
427 (command-list (fold-right append '() final-path)))
435 ((if (< x-scale 0) reverse-interval identity)
436 (cons (* x-scale (list-ref boundlist 0))
437 (* x-scale (list-ref boundlist 1))))
438 `(,(/ thickness -2) . ,(/ thickness 2)))
440 ((if (< y-scale 0) reverse-interval identity)
441 (cons (* y-scale (list-ref boundlist 2))
442 (* y-scale (list-ref boundlist 3))))
443 `(,(/ thickness -2) . ,(/ thickness 2))))))
445 (define-public (make-ellipse-stencil x-radius y-radius thickness fill)
446 "Make an ellipse of x@tie{}radius @var{x-radius}, y@tie{}radius
447 @code{y-radius}, and thickness @var{thickness} with fill defined by
450 ((x-out-radius (+ x-radius (/ thickness 2.0)))
451 (y-out-radius (+ y-radius (/ thickness 2.0))) )
454 (list 'ellipse x-radius y-radius thickness fill)
455 (cons (- x-out-radius) x-out-radius)
456 (cons (- y-out-radius) y-out-radius))))
458 (define-public (box-grob-stencil grob)
459 "Make a box of exactly the extents of the grob. The box precisely
460 encloses the contents."
461 (let* ((xext (ly:grob-extent grob grob 0))
462 (yext (ly:grob-extent grob grob 1))
466 (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
467 (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
468 (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
469 (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
471 ;; TODO merge this and prev function.
472 (define-public (box-stencil stencil thickness padding)
473 "Add a box around @var{stencil}, producing a new stencil."
474 (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
475 (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
476 (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
477 (x-rule (make-filled-box-stencil
478 (interval-widen x-ext thickness) (cons 0 thickness))))
479 (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
480 (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
481 (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))
482 (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
485 (define-public (circle-stencil stencil thickness padding)
486 "Add a circle around @var{stencil}, producing a new stencil."
487 (let* ((x-ext (ly:stencil-extent stencil X))
488 (y-ext (ly:stencil-extent stencil Y))
489 (diameter (max (interval-length x-ext)
490 (interval-length y-ext)))
491 (radius (+ (/ diameter 2) padding thickness))
492 (circle (make-circle-stencil radius thickness #f)))
496 (ly:stencil-translate circle
498 (interval-center x-ext)
499 (interval-center y-ext))))))
501 (define-public (oval-stencil stencil thickness x-padding y-padding)
502 "Add an oval around @code{stencil}, padded by the padding pair,
503 producing a new stencil."
504 (let* ((x-ext (ly:stencil-extent stencil X))
505 (y-ext (ly:stencil-extent stencil Y))
506 (x-length (+ (interval-length x-ext) x-padding thickness))
507 (y-length (+ (interval-length y-ext) y-padding thickness))
508 (x-radius (* 0.707 x-length) )
509 (y-radius (* 0.707 y-length) )
510 (oval (make-oval-stencil x-radius y-radius thickness #f)))
514 (ly:stencil-translate oval
516 (interval-center x-ext)
517 (interval-center y-ext))))))
519 (define-public (ellipse-stencil stencil thickness x-padding y-padding)
520 "Add an ellipse around @var{stencil}, padded by the padding pair,
521 producing a new stencil."
522 (let* ((x-ext (ly:stencil-extent stencil X))
523 (y-ext (ly:stencil-extent stencil Y))
524 (x-length (+ (interval-length x-ext) x-padding thickness))
525 (y-length (+ (interval-length y-ext) y-padding thickness))
526 ;(aspect-ratio (/ x-length y-length))
527 (x-radius (* 0.707 x-length) )
528 (y-radius (* 0.707 y-length) )
529 ;(diameter (max (- (cdr x-ext) (car x-ext))
530 ; (- (cdr y-ext) (car y-ext))))
531 ;(radius (+ (/ diameter 2) padding thickness))
532 (ellipse (make-ellipse-stencil x-radius y-radius thickness #f)))
536 (ly:stencil-translate ellipse
538 (interval-center x-ext)
539 (interval-center y-ext))))))
541 (define-public (rounded-box-stencil stencil thickness padding blot)
542 "Add a rounded box around @var{stencil}, producing a new stencil."
544 (let* ((xext (interval-widen (ly:stencil-extent stencil 0) padding))
545 (yext (interval-widen (ly:stencil-extent stencil 1) padding))
546 (min-ext (min (-(cdr xext) (car xext)) (-(cdr yext) (car yext))))
547 (ideal-blot (min blot (/ min-ext 2)))
548 (ideal-thickness (min thickness (/ min-ext 2)))
549 (outer (ly:round-filled-box
550 (interval-widen xext ideal-thickness)
551 (interval-widen yext ideal-thickness)
553 (inner (ly:make-stencil (list 'color (x11-color 'white)
554 (ly:stencil-expr (ly:round-filled-box
555 xext yext (- ideal-blot ideal-thickness)))))))
556 (set! stencil (ly:stencil-add outer inner))
559 (define-public (stencil-with-color stencil color)
561 (list 'color color (ly:stencil-expr stencil))
562 (ly:stencil-extent stencil X)
563 (ly:stencil-extent stencil Y)))
565 (define-public (stencil-whiteout stencil)
567 ((x-ext (ly:stencil-extent stencil X))
568 (y-ext (ly:stencil-extent stencil Y))
573 (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
578 (define-public (arrow-stencil-maker start? end?)
579 "Return a function drawing a line from current point to @code{destination},
580 with optional arrows of @code{max-size} on start and end controlled by
581 @var{start?} and @var{end?}."
582 (lambda (destination max-size)
586 (distance (sqrt (+ (* (car destination) (car destination))
587 (* (cdr destination) (cdr destination)))))
588 (size (min max-size (/ distance 3)))
589 (rotate (lambda (z ang)
590 (* (make-polar 1 ang)
592 (complex-to-offset (lambda (z)
593 (list (real-part z) (imag-part z))))
595 (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
596 (e_z (/ z-dest (magnitude z-dest)))
597 (triangle-points (list
601 (p1s (map (lambda (z)
602 (+ z-dest (rotate z (angle z-dest))))
604 (p2s (map (lambda (z)
605 (rotate z (angle (- z-dest))))
610 `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
615 `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
618 (thickness (min (/ distance 12) 0.1))
619 (shorten-line (min (/ distance 3) 0.5))
620 (start (complex-to-offset (/ (* e_z shorten-line) 2)))
621 (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
623 (line (ly:make-stencil
624 `(draw-line ,thickness
625 ,(car start) ,(cadr start)
626 ,(car end) ,(cadr end)
628 (cons (min 0 (car destination))
629 (min 0 (cdr destination)))
630 (cons (max 0 (car destination))
631 (max 0 (cdr destination)))))
635 (if start? arrow-2 empty-stencil)
636 (if end? arrow-1 empty-stencil)
641 (define-public dimension-arrows (arrow-stencil-maker #t #t))
643 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
646 ;; annotations are arrows indicating the numerical value of
648 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
650 (define*-public (annotate-y-interval layout name extent is-length
651 #:key (color darkblue))
652 (let ((text-props (cons '((font-size . -3)
653 (font-family . typewriter))
654 (layout-extract-page-properties layout)))
656 (define (center-stencil-on-extent stil)
657 (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
658 (cons 0 (interval-center extent))))
659 ;; do something sensible for 0,0 intervals.
660 (set! extent (interval-widen extent 0.001))
661 (if (not (interval-sane? extent))
662 (set! annotation (interpret-markup
664 (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
665 (let ((text-stencil (interpret-markup
667 (markup #:whiteout #:simple name)))
668 (dim-stencil (interpret-markup
672 ((interval-empty? extent)
675 (ly:format "~$" (interval-length extent)))
678 (car extent) (cdr extent)))))))
679 (arrows (ly:stencil-translate-axis
680 (dimension-arrows (cons 0 (interval-length extent)) 1.0)
681 (interval-start extent) Y)))
683 (center-stencil-on-extent text-stencil))
685 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
687 (ly:stencil-combine-at-edge annotation X LEFT
688 (center-stencil-on-extent dim-stencil)
691 (stencil-with-color annotation color))))
695 ;; TODO: figure out how to annotate padding nicely
696 ;; TODO: emphasize either padding or min-dist depending on which constraint was active
697 (define*-public (annotate-spacing-spec layout spacing-spec start-Y-offset next-staff-Y
698 #:key (base-color blue))
699 (let* ((get-spacing-var (lambda (sym) (assoc-get sym spacing-spec 0.0)))
700 (space (get-spacing-var 'basic-distance))
701 (padding (get-spacing-var 'padding))
702 (min-dist (get-spacing-var 'minimum-distance))
703 (contrast-color (append (cdr base-color) (list (car base-color))))
704 (min-dist-blocks (<= (- start-Y-offset min-dist) next-staff-Y))
705 (min-dist-color (if min-dist-blocks contrast-color base-color))
706 (basic-annotation (annotate-y-interval layout
708 (cons (- start-Y-offset space) start-Y-offset)
710 #:color (map (lambda (x) (* x 0.25)) base-color)))
711 (min-annotation (annotate-y-interval layout
713 (cons (- start-Y-offset min-dist) start-Y-offset)
715 #:color min-dist-color))
716 (extra-annotation (annotate-y-interval layout
718 (cons next-staff-Y (- start-Y-offset min-dist))
720 #:color (map (lambda (x) (* x 0.5)) min-dist-color))))
722 (stack-stencils X RIGHT 0.0
727 (ly:stencil-add min-annotation extra-annotation))))))
729 (define-public (eps-file->stencil axis size file-name)
731 ((contents (ly:gulp-file file-name))
732 (bbox (get-postscript-bbox (car (string-split contents #\nul))))
733 (bbox-size (if (= axis X)
734 (- (list-ref bbox 2) (list-ref bbox 0))
735 (- (list-ref bbox 3) (list-ref bbox 1))
737 (factor (if (< 0 bbox-size)
738 (exact->inexact (/ size bbox-size))
741 (map (lambda (x) (* factor x)) bbox))
742 ; We need to shift the whole eps to (0,0), otherwise it will appear
743 ; displaced in lilypond (displacement will depend on the scaling!)
744 (translate-string (ly:format "~a ~a translate" (- (list-ref bbox 0)) (- (list-ref bbox 1))))
745 (clip-rect-string (ly:format
746 "~a ~a ~a ~a rectclip"
749 (- (list-ref bbox 2) (list-ref bbox 0))
750 (- (list-ref bbox 3) (list-ref bbox 1)))))
761 currentpoint translate
767 " factor translate-string clip-rect-string
776 ; Stencil starts at (0,0), since we have shifted the eps, and its
777 ; size is exactly the size of the scaled bounding box
778 (cons 0 (- (list-ref scaled-bbox 2) (list-ref scaled-bbox 0)))
779 (cons 0 (- (list-ref scaled-bbox 3) (list-ref scaled-bbox 1))))
781 (ly:make-stencil "" '(0 . 0) '(0 . 0)))
784 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
785 ;; output signatures.
787 (define-public (write-system-signatures basename paper-systems count)
788 (if (pair? paper-systems)
791 ((outname (simple-format #f "~a-~a.signature" basename count)) )
793 (ly:message "Writing ~a" outname)
794 (write-system-signature outname (car paper-systems))
795 (write-system-signatures basename (cdr paper-systems) (1+ count))))))
797 (use-modules (scm paper-system))
798 (define-public (write-system-signature filename paper-system)
800 (and (number? x) (inexact? x)))
803 (paper-system-system-grob paper-system))
805 (define output (open-output-file filename))
807 ;; todo: optionally use a command line flag? Or just junk this?
808 (define compare-expressions #f)
809 (define (strip-floats expr)
810 "Replace floats by #f"
813 ((ly:font-metric? expr) (ly:font-name expr))
814 ((pair? expr) (cons (strip-floats (car expr))
815 (strip-floats (cdr expr))))
818 (define (fold-false-pairs expr)
819 "Try to remove lists of #f as much as possible."
823 (rest (fold-false-pairs (cdr expr))))
826 (cons (fold-false-pairs first) rest)
830 (define (raw-string expr)
831 "escape quotes and slashes for python consumption"
832 (regexp-substitute/global #f "[@\n]" (simple-format #f "~a" expr) 'pre " " 'post))
834 (define (raw-pair expr)
835 (simple-format #f "~a ~a"
836 (car expr) (cdr expr)))
838 (define (found-grob expr)
843 (cause (event-cause grob))
844 (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
845 (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
847 ;; todo: use stencil extent if available.
848 (x-ext (ly:grob-extent grob system-grob X))
849 (y-ext (ly:grob-extent grob system-grob Y))
851 (if compare-expressions
852 (interpret-for-signature
854 (set! collected (cons e collected)))
858 (simple-format output
860 (cdr (assq 'name (ly:grob-property grob 'meta) ))
861 (raw-string location)
862 (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
863 (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
864 (raw-string collected))
867 (define (interpret-for-signature escape collect expr)
868 (define (interpret expr)
870 ((head (if (pair? expr)
875 ((eq? head 'grob-cause) (escape (cdr expr)))
876 ((eq? head 'color) (interpret (caddr expr)))
877 ((eq? head 'rotate-stencil) (interpret (caddr expr)))
878 ((eq? head 'translate-stencil) (interpret (caddr expr)))
879 ((eq? head 'combine-stencil)
880 (for-each (lambda (e) (interpret e)) (cdr expr)))
882 (collect (fold-false-pairs (strip-floats expr))))
888 (if (ly:grob? system-grob)
890 (display (simple-format #f "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
892 (interpret-for-signature found-grob (lambda (x) #f)
894 (paper-system-stencil paper-system)))))
896 ;; should be superfluous, but leaking "too many open files"?