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
47 (ly:stencil-stack front axis dir next padding))
51 (define-public (stack-stencils-padding-list axis dir paddings stils)
52 "Stack stencils @var{stils} in direction @var{axis}, @var{dir}, using
53 a list of @var{paddings}."
57 (lambda (next padding front)
58 (let ((offset (+ (- (interval-end (ly:stencil-extent front axis))
59 (interval-start (ly:stencil-extent next axis)))
63 (ly:stencil-translate-axis next offset axis))))
68 (define-public (centered-stencil stencil)
69 "Center stencil @var{stencil} in both the X and Y directions."
70 (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
72 (define-public (stack-lines dir padding baseline stils)
73 "Stack vertically with a baseline skip."
75 (lambda (next back) (ly:stencil-stack next Y dir back padding baseline))
79 ;; X-empty stencils may add vertical space. A stencil that is
80 ;; merely Y-empty counts as horizontal spacing. Since we want
81 ;; those to register as lines of their own (is this a good
82 ;; idea?), we make them a separately visible line.
83 (if (and (ly:stencil-empty? s Y)
84 (not (ly:stencil-empty? s X)))
85 (ly:make-stencil (ly:stencil-expr s) (ly:stencil-extent s X) '(0 . 0))
89 (define-public (bracketify-stencil stil axis thick protrusion padding)
90 "Add brackets around @var{stil}, producing a new stencil."
92 (let* ((ext (ly:stencil-extent stil axis))
93 (lb (ly:bracket axis ext thick protrusion))
94 (rb (ly:bracket axis ext thick (- protrusion))))
96 (ly:stencil-combine-at-edge stil (other-axis axis) 1 rb padding))
98 (ly:stencil-combine-at-edge stil (other-axis axis) -1 lb padding))
101 (define (make-parenthesis-stencil
102 y-extent half-thickness width angularity)
103 "Create a parenthesis stencil.
104 @var{y-extent} is the Y extent of the markup inside the parenthesis.
105 @var{half-thickness} is the half thickness of the parenthesis.
106 @var{width} is the width of a parenthesis.
107 The higher the value of number @var{angularity},
108 the more angular the shape of the parenthesis."
109 (let* ((line-width 0.1)
110 ;; Horizontal position of baseline that end points run through.
115 ;; X value farthest from baseline on outside of curve
116 (outer-x (+ base-x width))
117 ;; X extent of bezier sandwich centerline curves
118 (x-extent (ordered-cons base-x outer-x))
119 (bottom-y (interval-start y-extent))
120 (top-y (interval-end y-extent))
122 (lower-end-point (cons base-x bottom-y))
123 (upper-end-point (cons base-x top-y))
125 (outer-control-x (+ base-x (* 4/3 width)))
126 (inner-control-x (+ outer-control-x
129 (- half-thickness))))
131 ;; Vertical distance between a control point
132 ;; and the end point it connects to.
133 (offset-index (- (* 0.6 angularity) 0.8))
134 (lower-control-y (interval-index y-extent offset-index))
135 (upper-control-y (interval-index y-extent (- offset-index)))
137 (lower-outer-control-point
138 (cons outer-control-x lower-control-y))
139 (upper-outer-control-point
140 (cons outer-control-x upper-control-y))
141 (upper-inner-control-point
142 (cons inner-control-x upper-control-y))
143 (lower-inner-control-point
144 (cons inner-control-x lower-control-y)))
146 (make-bezier-sandwich-stencil
148 ;; Step 4: curve through inner control points
149 ;; to lower end point.
150 upper-inner-control-point
151 lower-inner-control-point
153 ;; Step 3: move to upper end point.
155 ;; Step 2: curve through outer control points
156 ;; to upper end point.
157 lower-outer-control-point
158 upper-outer-control-point
160 ;; Step 1: move to lower end point.
163 (interval-widen x-extent (/ line-width 2))
164 (interval-widen y-extent (/ line-width 2)))))
166 (define-public (parenthesize-stencil
167 stencil half-thickness width angularity padding)
168 "Add parentheses around @var{stencil}, returning a new stencil."
169 (let* ((y-extent (ly:stencil-extent stencil Y))
170 (lp (make-parenthesis-stencil
171 y-extent half-thickness (- width) angularity))
172 (rp (make-parenthesis-stencil
173 y-extent half-thickness width angularity)))
174 (set! stencil (ly:stencil-combine-at-edge stencil X LEFT lp padding))
175 (set! stencil (ly:stencil-combine-at-edge stencil X RIGHT rp padding))
178 (define-public (make-line-stencil width startx starty endx endy)
179 "Make a line stencil of given linewidth and set its extents accordingly."
180 (let ((xext (cons (min startx endx) (max startx endx)))
181 (yext (cons (min starty endy) (max starty endy))))
183 (list 'draw-line width startx starty endx endy)
184 ;; Since the line has rounded edges, we have to / can safely add half the
185 ;; width to all coordinates!
186 (interval-widen xext (/ width 2))
187 (interval-widen yext (/ width 2)))))
189 (define-public (make-transparent-box-stencil xext yext)
190 "Make a transparent box."
192 (list 'transparent-stencil
193 (ly:stencil-expr (make-filled-box-stencil xext yext)))
196 (define-public (make-filled-box-stencil xext yext)
200 (list 'round-filled-box (- (car xext)) (cdr xext)
201 (- (car yext)) (cdr yext) 0.0)
204 (define-public (make-circle-stencil radius thickness fill)
205 "Make a circle of radius @var{radius} and thickness @var{thickness}."
207 ((out-radius (+ radius (/ thickness 2.0))))
210 (list 'circle radius thickness fill)
211 (cons (- out-radius) out-radius)
212 (cons (- out-radius) out-radius))))
214 (define-public (make-oval-stencil x-radius y-radius thickness fill)
215 "Make an oval from two Bezier curves, of x@tie{}radius @var{x-radius},
216 y@tie{}radius @code{y-radius}, and thickness @var{thickness} with fill
217 defined by @code{fill}."
219 ((x-out-radius (+ x-radius (/ thickness 2.0)))
220 (y-out-radius (+ y-radius (/ thickness 2.0)))
225 (commands `(,(list 'moveto x-max 0)
226 ,(list 'curveto x-max y-max x-min y-max x-min 0)
227 ,(list 'curveto x-min y-min x-max y-min x-max 0)
229 (command-list (fold-right append '() commands)))
231 `(path ,thickness `(,@',command-list) 'round 'round ,fill)
232 (cons (- x-out-radius) x-out-radius)
233 (cons (- y-out-radius) y-out-radius))))
236 (make-partial-ellipse-stencil
237 x-radius y-radius start-angle end-angle thick connect fill)
238 "Create an elliptical arc
239 @var{x-radius} is the X radius of the arc.
240 @var{y-radius} is the Y radius of the arc.
241 @var{start-angle} is the starting angle of the arc in degrees.
242 @var{end-angle} is the ending angle of the arc in degrees.
243 @var{thick} is the thickness of the line.
244 @var{connect} is a boolean flag indicating if the end should
245 be connected to the start by a line.
246 @var{fill} is a boolean flag indicating if the shape should be filled."
247 (define (make-radius-list x-radius y-radius)
248 "Makes a list of angle/radius pairs at intervals of PI/2 for
249 the partial ellipse until 7*PI/2. For example, in pseudo-code:
250 > (make-radius-list 2 3)\
251 \n((0.0 . 2) (PI/2 . 3) (PI . -2) (3*PI/2 . -3)\
252 \n(2*PI . 2) (5*PI/2 . 3) (3*PI . -2) (7*PI/2 . -3))
256 (map (lambda (quadrant)
257 (cons (+ adder (car quadrant))
259 `((0.0 . (,x-radius . 0.0))
260 (,PI-OVER-TWO . (0.0 . ,y-radius))
261 (,PI . (,(- x-radius) . 0.0))
262 (,THREE-PI-OVER-TWO . (0.0 . ,(- y-radius))))))
266 (insert-in-ordered-list ordering-function value inlist cutl? cutr?)
267 "Insert @var{value} in ordered list @var{inlist}. If @var{cutl?}, we
268 cut away any parts of @var{inlist} before @var{value}. @var{cutr?} works
269 the same way but for the right side. For example:
270 > (insert-in-ordered-list < 4 '(1 2 3 6 7) #f #f)
272 > (insert-in-ordered-list < 4 '(1 2 3 6 7) #t #f)
274 > (insert-in-ordered-list < 4 '(1 2 3 6 7) #f #t)
278 (helper ordering-function value left-list right-list cutl? cutr?)
279 (if (null? right-list)
281 (if cutl? '() left-list)
283 (if cutr? '() right-list))
284 (if (ordering-function value (car right-list))
286 (if cutl? '() left-list)
288 (if cutr? '() right-list))
292 (append left-list (list (car right-list)))
296 (helper ordering-function value '() inlist cutl? cutr?))
298 (define (ordering-function-1 a b) (car< a b))
300 (define (ordering-function-2 a b) (car<= a b))
302 (define (min-max-crawler min-max side l)
303 "Apply function @var{side} to each member of list and
304 then reduce using @var{min-max}:
305 > (min-max-crawler min car '((0 . 3) (-1 . 4) (1 . 2)))
307 > (min-max-crawler min cdr '((0 . 3) (-1 . 4) (1 . 2)))
311 (if (eq? min-max min) 100000 -100000)
315 (;; the outside limit of the x-radius
316 (x-out-radius (+ x-radius (/ thick 2.0)))
317 ;; the outside limit of the y-radius
318 (y-out-radius (+ y-radius (/ thick 2.0)))
319 ;; end angle to radians
320 (new-end-angle (angle-0-2pi (degrees->radians end-angle)))
321 ;; length of the radius at the end angle
322 (end-radius (ellipse-radius x-out-radius y-out-radius new-end-angle))
323 ;; start angle to radians
324 (new-start-angle (angle-0-2pi (degrees->radians start-angle)))
325 ;; length of the radius at the start angle
326 (start-radius (ellipse-radius x-out-radius y-out-radius new-start-angle))
327 ;; points that the arc passes through at 90 degree intervals
328 (radius-list (make-radius-list x-out-radius y-out-radius))
329 ;; rectangular coordinates of arc endpoint
330 (rectangular-end-radius (polar->rectangular end-radius end-angle))
331 ;; rectangular coordinates of arc begin point
332 (rectangular-start-radius (polar->rectangular start-radius start-angle))
333 ;; we want the end angle to always be bigger than the start angle
334 ;; so we redefine it here just in case it is less
336 (if (<= new-end-angle new-start-angle)
337 (+ TWO-PI new-end-angle)
339 ;; all the points that may be extrema of the arc
340 ;; this is the 90 degree points plus the beginning and end points
341 ;; we use this to calculate extents
343 (insert-in-ordered-list
345 (cons new-end-angle rectangular-end-radius)
346 (insert-in-ordered-list
348 (cons new-start-angle rectangular-start-radius)
364 ;; we know the extrema points by crawling through the
365 ;; list of possible extrema and finding the min and max
367 (cons (min-max-crawler min cadr possible-extrema)
368 (min-max-crawler max cadr possible-extrema))
369 (cons (min-max-crawler min cddr possible-extrema)
370 (min-max-crawler max cddr possible-extrema)))))
372 (define (line-part-min-max x1 x2)
373 (list (min x1 x2) (max x1 x2)))
375 (define (bezier-part-min-max x1 x2 x3 x4)
376 ((lambda (x) (list (reduce min 10000 x) (reduce max -10000 x)))
379 (+ (* x1 (expt (- 1 x) 3))
380 (+ (* 3 (* x2 (* (expt (- 1 x) 2) x)))
381 (+ (* 3 (* x3 (* (- 1 x) (expt x 2))))
382 (* x4 (expt x 3))))))
383 (if (< (+ (expt x2 2) (+ (expt x3 2) (* x1 x4)))
384 (+ (* x1 x3) (+ (* x2 x4) (* x2 x3))))
387 (lambda (x) (and (>= x 0) (<= x 1)))
392 (exact->inexact (- (+ x1 (* 3 x3)) (+ x4 (* 3 x2))))))
393 ;; Zeros of the bezier curve
394 (/ (+ (- x1 (* 2 x2))
396 (sqrt (- (+ (expt x2 2)
397 (+ (expt x3 2) (* x1 x4)))
399 (+ (* x2 x4) (* x2 x3)))))))
400 (- (+ x1 (* 3 x3)) (+ x4 (* 3 x2))))
401 ;; Apply L'hopital's rule to get the zeros if 0/0
404 (sqrt (- (+ (* x2 x2)
405 (+ (* x3 x3) (* x1 x4)))
407 (+ (* x2 x4) (* x2 x3)))))))))
410 (define (bezier-min-max x1 y1 x2 y2 x3 y3 x4 y4)
412 (apply bezier-part-min-max x))
413 `((,x1 ,x2 ,x3 ,x4) (,y1 ,y2 ,y3 ,y4))))
415 (define (line-min-max x1 y1 x2 y2)
417 (apply line-part-min-max x))
418 `((,x1 ,x2) (,y1 ,y2))))
420 (define (path-min-max origin pointlist)
424 (reduce min +inf.0 (map caar x))
425 (reduce max -inf.0 (map cadar x))
426 (reduce min +inf.0 (map caadr x))
427 (reduce max -inf.0 (map cadadr x))))
430 (apply bezier-min-max x)
431 (apply line-min-max x)))
433 (append (list (cadr (reverse x)) (car (reverse x))) y))
434 (append (list origin)
435 (reverse (cdr (reverse pointlist)))) pointlist))))
437 (define-public (make-connected-path-stencil pointlist thickness
438 x-scale y-scale connect fill)
439 "Make a connected path described by the list @var{pointlist}, with
440 thickness @var{thickness}, and scaled by @var{x-scale} in the X direction
441 and @var{y-scale} in the Y direction. @var{connect} and @var{fill} are
442 boolean arguments that specify if the path should be connected or filled,
445 ;; paths using this routine are designed to begin at point '(0 . 0)
446 (let* ((origin (list 0 0))
447 (boundlist (path-min-max origin pointlist))
448 ;; modify pointlist to scale the coordinates
449 (path (map (lambda (x)
452 (lambda (x1 x2 x3 x4 x5 x6)
466 ;; a path must begin with a `moveto'
467 (prepend-origin (apply list (cons 'moveto origin) path))
468 ;; if this path is connected, add closepath to the end
469 (final-path (if connect
470 (append prepend-origin (list 'closepath))
472 (command-list (fold-right append '() final-path)))
480 ((if (< x-scale 0) reverse-interval identity)
481 (cons (* x-scale (list-ref boundlist 0))
482 (* x-scale (list-ref boundlist 1))))
483 `(,(/ thickness -2) . ,(/ thickness 2)))
485 ((if (< y-scale 0) reverse-interval identity)
486 (cons (* y-scale (list-ref boundlist 2))
487 (* y-scale (list-ref boundlist 3))))
488 `(,(/ thickness -2) . ,(/ thickness 2))))))
490 (define-public (make-ellipse-stencil x-radius y-radius thickness fill)
491 "Make an ellipse of x@tie{}radius @var{x-radius}, y@tie{}radius
492 @code{y-radius}, and thickness @var{thickness} with fill defined by
495 ((x-out-radius (+ x-radius (/ thickness 2.0)))
496 (y-out-radius (+ y-radius (/ thickness 2.0))) )
499 (list 'ellipse x-radius y-radius thickness fill)
500 (cons (- x-out-radius) x-out-radius)
501 (cons (- y-out-radius) y-out-radius))))
503 (define-public (box-grob-stencil grob)
504 "Make a box of exactly the extents of the grob. The box precisely
505 encloses the contents."
506 (let* ((xext (ly:grob-extent grob grob 0))
507 (yext (ly:grob-extent grob grob 1))
511 (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
512 (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
513 (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
514 (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
516 ;; TODO merge this and prev function.
517 (define-public (box-stencil stencil thickness padding)
518 "Add a box around @var{stencil}, producing a new stencil."
519 (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
520 (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
521 (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
522 (x-rule (make-filled-box-stencil
523 (interval-widen x-ext thickness) (cons 0 thickness))))
524 (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
525 (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
526 (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))
527 (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
530 (define-public (circle-stencil stencil thickness padding)
531 "Add a circle around @var{stencil}, producing a new stencil."
532 (let* ((x-ext (ly:stencil-extent stencil X))
533 (y-ext (ly:stencil-extent stencil Y))
534 (diameter (max (interval-length x-ext)
535 (interval-length y-ext)))
536 (radius (+ (/ diameter 2) padding thickness))
537 (circle (make-circle-stencil radius thickness #f)))
541 (ly:stencil-translate circle
543 (interval-center x-ext)
544 (interval-center y-ext))))))
546 (define-public (oval-stencil stencil thickness x-padding y-padding)
547 "Add an oval around @code{stencil}, padded by the padding pair,
548 producing a new stencil."
549 (let* ((x-ext (ly:stencil-extent stencil X))
550 (y-ext (ly:stencil-extent stencil Y))
551 (x-length (+ (interval-length x-ext) x-padding thickness))
552 (y-length (+ (interval-length y-ext) y-padding thickness))
553 (x-radius (* 0.707 x-length) )
554 (y-radius (* 0.707 y-length) )
555 (oval (make-oval-stencil x-radius y-radius thickness #f)))
559 (ly:stencil-translate oval
561 (interval-center x-ext)
562 (interval-center y-ext))))))
564 (define-public (ellipse-stencil stencil thickness x-padding y-padding)
565 "Add an ellipse around @var{stencil}, padded by the padding pair,
566 producing a new stencil."
567 (let* ((x-ext (ly:stencil-extent stencil X))
568 (y-ext (ly:stencil-extent stencil Y))
569 (x-length (+ (interval-length x-ext) x-padding thickness))
570 (y-length (+ (interval-length y-ext) y-padding thickness))
571 ;; (aspect-ratio (/ x-length y-length))
572 (x-radius (* 0.707 x-length) )
573 (y-radius (* 0.707 y-length) )
574 ;; (diameter (max (- (cdr x-ext) (car x-ext))
575 ;; (- (cdr y-ext) (car y-ext))))
576 ;; radius (+ (/ diameter 2) padding thickness))
577 (ellipse (make-ellipse-stencil x-radius y-radius thickness #f)))
581 (ly:stencil-translate ellipse
583 (interval-center x-ext)
584 (interval-center y-ext))))))
586 (define-public (rounded-box-stencil stencil thickness padding blot)
587 "Add a rounded box around @var{stencil}, producing a new stencil."
589 (let* ((xext (interval-widen (ly:stencil-extent stencil 0) padding))
590 (yext (interval-widen (ly:stencil-extent stencil 1) padding))
591 (min-ext (min (-(cdr xext) (car xext)) (-(cdr yext) (car yext))))
592 (ideal-blot (min blot (/ min-ext 2)))
593 (ideal-thickness (min thickness (/ min-ext 2)))
594 (outer (ly:round-filled-box
595 (interval-widen xext ideal-thickness)
596 (interval-widen yext ideal-thickness)
598 (inner (ly:make-stencil (list 'color (x11-color 'white)
599 (ly:stencil-expr (ly:round-filled-box
600 xext yext (- ideal-blot ideal-thickness)))))))
601 (set! stencil (ly:stencil-add outer inner))
604 (define-public (stencil-with-color stencil color)
606 (list 'color color (ly:stencil-expr stencil))
607 (ly:stencil-extent stencil X)
608 (ly:stencil-extent stencil Y)))
610 (define-public (stencil-whiteout stencil)
612 ((x-ext (ly:stencil-extent stencil X))
613 (y-ext (ly:stencil-extent stencil Y))
618 (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
623 (define-public (arrow-stencil-maker start? end?)
624 "Return a function drawing a line from current point to @code{destination},
625 with optional arrows of @code{max-size} on start and end controlled by
626 @var{start?} and @var{end?}."
627 (lambda (destination max-size)
631 (distance (sqrt (+ (* (car destination) (car destination))
632 (* (cdr destination) (cdr destination)))))
633 (size (min max-size (/ distance 3)))
634 (rotate (lambda (z ang)
635 (* (make-polar 1 ang)
637 (complex-to-offset (lambda (z)
638 (list (real-part z) (imag-part z))))
640 (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
641 (e_z (/ z-dest (magnitude z-dest)))
642 (triangle-points (list
646 (p1s (map (lambda (z)
647 (+ z-dest (rotate z (angle z-dest))))
649 (p2s (map (lambda (z)
650 (rotate z (angle (- z-dest))))
655 `(polygon (quote ,(append-map complex-to-offset p1s))
660 `(polygon (quote ,(append-map complex-to-offset p2s))
663 (thickness (min (/ distance 12) 0.1))
664 (shorten-line (min (/ distance 3) 0.5))
665 (start (complex-to-offset (/ (* e_z shorten-line) 2)))
666 (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
668 (line (ly:make-stencil
669 `(draw-line ,thickness
670 ,(car start) ,(cadr start)
671 ,(car end) ,(cadr end)
673 (cons (min 0 (car destination))
674 (min 0 (cdr destination)))
675 (cons (max 0 (car destination))
676 (max 0 (cdr destination)))))
680 (if start? arrow-2 empty-stencil)
681 (if end? arrow-1 empty-stencil)
686 (define-public dimension-arrows (arrow-stencil-maker #t #t))
688 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
691 ;; annotations are arrows indicating the numerical value of
693 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
695 (define*-public (annotate-y-interval layout name extent is-length
696 #:key (color darkblue))
697 (let ((text-props (cons '((font-size . -3)
698 (font-family . typewriter))
699 (layout-extract-page-properties layout)))
701 (define (center-stencil-on-extent stil)
702 (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
703 (cons 0 (interval-center extent))))
704 ;; do something sensible for 0,0 intervals.
705 (set! extent (interval-widen extent 0.001))
706 (if (not (interval-sane? extent))
707 (set! annotation (interpret-markup
709 (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
710 (let ((text-stencil (interpret-markup
712 (markup #:whiteout #:simple name)))
713 (dim-stencil (interpret-markup
717 ((interval-empty? extent)
720 (ly:format "~$" (interval-length extent)))
723 (car extent) (cdr extent)))))))
724 (arrows (ly:stencil-translate-axis
725 (dimension-arrows (cons 0 (interval-length extent)) 1.0)
726 (interval-start extent) Y)))
728 (center-stencil-on-extent text-stencil))
730 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
732 (ly:stencil-combine-at-edge annotation X LEFT
733 (center-stencil-on-extent dim-stencil)
736 (stencil-with-color annotation color))))
740 ;; TODO: figure out how to annotate padding nicely
741 ;; TODO: emphasize either padding or min-dist depending on which constraint was active
742 (define*-public (annotate-spacing-spec layout spacing-spec start-Y-offset next-staff-Y
743 #:key (base-color blue))
744 (let* ((get-spacing-var (lambda (sym) (assoc-get sym spacing-spec 0.0)))
745 (space (get-spacing-var 'basic-distance))
746 (padding (get-spacing-var 'padding))
747 (min-dist (get-spacing-var 'minimum-distance))
748 (contrast-color (append (cdr base-color) (list (car base-color))))
749 (min-dist-blocks (<= (- start-Y-offset min-dist) next-staff-Y))
750 (min-dist-color (if min-dist-blocks contrast-color base-color))
751 (basic-annotation (annotate-y-interval layout
753 (cons (- start-Y-offset space) start-Y-offset)
755 #:color (map (lambda (x) (* x 0.25)) base-color)))
756 (min-annotation (annotate-y-interval layout
758 (cons (- start-Y-offset min-dist) start-Y-offset)
760 #:color min-dist-color))
761 (extra-annotation (annotate-y-interval layout
763 (cons next-staff-Y (- start-Y-offset min-dist))
765 #:color (map (lambda (x) (* x 0.5)) min-dist-color))))
767 (stack-stencils X RIGHT 0.0
772 (ly:stencil-add min-annotation extra-annotation))))))
774 (define-public (eps-file->stencil axis size file-name)
776 ((contents (ly:gulp-file file-name))
777 (bbox (get-postscript-bbox (car (string-split contents #\nul))))
778 (bbox-size (if (= axis X)
779 (- (list-ref bbox 2) (list-ref bbox 0))
780 (- (list-ref bbox 3) (list-ref bbox 1))
782 (factor (if (< 0 bbox-size)
783 (exact->inexact (/ size bbox-size))
786 (map (lambda (x) (* factor x)) bbox))
787 ;; We need to shift the whole eps to (0,0), otherwise it will appear
788 ;; displaced in lilypond (displacement will depend on the scaling!)
789 (translate-string (ly:format "~a ~a translate" (- (list-ref bbox 0)) (- (list-ref bbox 1))))
790 (clip-rect-string (ly:format
791 "~a ~a ~a ~a rectclip"
794 (- (list-ref bbox 2) (list-ref bbox 0))
795 (- (list-ref bbox 3) (list-ref bbox 1)))))
806 currentpoint translate
812 " factor translate-string clip-rect-string
821 ;; Stencil starts at (0,0), since we have shifted the eps, and its
822 ;; size is exactly the size of the scaled bounding box
823 (cons 0 (- (list-ref scaled-bbox 2) (list-ref scaled-bbox 0)))
824 (cons 0 (- (list-ref scaled-bbox 3) (list-ref scaled-bbox 1))))
826 (ly:make-stencil "" '(0 . 0) '(0 . 0)))
829 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
830 ;; output signatures.
832 (define-public (write-system-signatures basename paper-systems count)
833 (if (pair? paper-systems)
836 ((outname (simple-format #f "~a-~a.signature" basename count)) )
838 (ly:message "Writing ~a" outname)
839 (write-system-signature outname (car paper-systems))
840 (write-system-signatures basename (cdr paper-systems) (1+ count))))))
842 (use-modules (scm paper-system))
843 (define-public (write-system-signature filename paper-system)
845 (and (number? x) (inexact? x)))
848 (paper-system-system-grob paper-system))
850 (define output (open-output-file filename))
852 ;; todo: optionally use a command line flag? Or just junk this?
853 (define compare-expressions #f)
854 (define (strip-floats expr)
855 "Replace floats by #f"
858 ((ly:font-metric? expr) (ly:font-name expr))
859 ((pair? expr) (cons (strip-floats (car expr))
860 (strip-floats (cdr expr))))
863 (define (fold-false-pairs expr)
864 "Try to remove lists of #f as much as possible."
868 (rest (fold-false-pairs (cdr expr))))
871 (cons (fold-false-pairs first) rest)
875 (define (raw-string expr)
876 "escape quotes and slashes for python consumption"
877 (regexp-substitute/global #f "[@\n]" (simple-format #f "~a" expr) 'pre " " 'post))
879 (define (raw-pair expr)
880 (simple-format #f "~a ~a"
881 (car expr) (cdr expr)))
883 (define (found-grob expr)
888 (cause (event-cause grob))
889 (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
890 (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
892 ;; todo: use stencil extent if available.
893 (x-ext (ly:grob-extent grob system-grob X))
894 (y-ext (ly:grob-extent grob system-grob Y))
896 (if compare-expressions
897 (interpret-for-signature
899 (set! collected (cons e collected)))
903 (simple-format output
905 (cdr (assq 'name (ly:grob-property grob 'meta) ))
906 (raw-string location)
907 (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
908 (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
909 (raw-string collected))
912 (define (interpret-for-signature escape collect expr)
913 (define (interpret expr)
915 ((head (if (pair? expr)
920 ((eq? head 'grob-cause) (escape (cdr expr)))
921 ((eq? head 'color) (interpret (caddr expr)))
922 ((eq? head 'rotate-stencil) (interpret (caddr expr)))
923 ((eq? head 'translate-stencil) (interpret (caddr expr)))
924 ((eq? head 'combine-stencil)
925 (for-each (lambda (e) (interpret e)) (cdr expr)))
927 (collect (fold-false-pairs (strip-floats expr))))
933 (if (ly:grob? system-grob)
935 (display (simple-format #f "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
937 (interpret-for-signature found-grob (lambda (x) #f)
939 (paper-system-stencil paper-system)))))
941 ;; should be superfluous, but leaking "too many open files"?