]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
Extract `connected-shape-min-max' into a new routine.
[lilypond.git] / scm / stencil.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2003--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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/>.
17
18 (define-public (stack-stencils axis dir padding stils)
19   "Stack stencils STILS in direction AXIS, DIR, using PADDING."
20   (cond
21    ((null? stils) empty-stencil)
22    ((null? (cdr stils)) (car stils))
23    (else (ly:stencil-combine-at-edge
24           (car stils) axis dir (stack-stencils axis dir padding (cdr stils))
25           padding))))
26
27 (define-public (stack-stencils-padding-list axis dir padding stils)
28   "Stack stencils STILS in direction AXIS, DIR, using a list of PADDING."
29   (cond
30    ((null? stils) empty-stencil)
31    ((null? (cdr stils)) (car stils))
32    (else (ly:stencil-combine-at-edge
33           (car stils)
34           axis dir
35           (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
36           (car padding)))))
37
38 (define-public (centered-stencil stencil)
39   "Center stencil @var{stencil} in both the X and Y directions"
40   (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
41
42 (define-public (stack-lines dir padding baseline stils)
43   "Stack vertically with a baseline-skip."
44   (define result empty-stencil)
45   (define last-y #f)
46   (do
47       ((last-stencil #f (car p))
48        (p stils (cdr p)))
49
50       ((null? p))
51
52     (if (number? last-y)
53         (begin
54           (let* ((dy (max (+ (* dir (interval-bound (ly:stencil-extent last-stencil Y) dir))
55                              padding
56                              (* (- dir) (interval-bound (ly:stencil-extent (car p) Y) (- dir))))
57                           baseline))
58                  (y (+ last-y  (* dir dy))))
59
60
61
62             (set! result
63                   (ly:stencil-add result (ly:stencil-translate-axis (car p) y Y)))
64             (set! last-y y)))
65         (begin
66           (set! last-y 0)
67           (set! result (car p)))))
68
69   result)
70
71
72 (define-public (bracketify-stencil stil axis thick protrusion padding)
73   "Add brackets around STIL, producing a new stencil."
74
75   (let* ((ext (ly:stencil-extent stil axis))
76          (lb (ly:bracket axis ext thick protrusion))
77          (rb (ly:bracket axis ext thick (- protrusion))))
78     (set! stil
79           (ly:stencil-combine-at-edge stil (other-axis axis) 1 rb padding))
80     (set! stil
81           (ly:stencil-combine-at-edge lb (other-axis axis) 1 stil padding))
82     stil))
83
84 (define (make-parenthesis-stencil
85          y-extent half-thickness width angularity)
86   "Create a parenthesis stencil.
87 @var{y-extent} is the Y extent of the markup inside the parenthesis.
88 @var{half-thickness} is the half thickness of the parenthesis.
89 @var{width} is the width of a parenthesis.
90 The higher the value of number @var{angularity},
91 the more angular the shape of the parenthesis."
92   (let* ((line-width 0.1)
93          ;; Horizontal position of baseline that end points run through.
94          (base-x
95           (if (< width 0)
96               (- width)
97               0))
98          ;; X value farthest from baseline on outside  of curve
99          (outer-x (+ base-x width))
100          ;; X extent of bezier sandwich centerline curves
101          (x-extent (ordered-cons base-x outer-x))
102          (bottom-y (interval-start y-extent))
103          (top-y (interval-end y-extent))
104
105          (lower-end-point (cons base-x bottom-y))
106          (upper-end-point (cons base-x top-y))
107
108          (outer-control-x (+ base-x (* 4/3 width)))
109          (inner-control-x (+ outer-control-x
110                              (if (< width 0)
111                                  half-thickness
112                                  (- half-thickness))))
113
114          ;; Vertical distance between a control point
115          ;; and the end point it connects to.
116          (offset-index (- (* 0.6 angularity) 0.8))
117          (lower-control-y (interval-index y-extent offset-index))
118          (upper-control-y (interval-index y-extent (- offset-index)))
119
120          (lower-outer-control-point
121           (cons outer-control-x lower-control-y))
122          (upper-outer-control-point
123           (cons outer-control-x upper-control-y))
124          (upper-inner-control-point
125           (cons inner-control-x upper-control-y))
126          (lower-inner-control-point
127           (cons inner-control-x lower-control-y)))
128
129     (ly:make-stencil
130      (list 'bezier-sandwich
131            `(quote ,(list
132                      ;; Step 4: curve through inner control points
133                      ;; to lower end point.
134                      upper-inner-control-point
135                      lower-inner-control-point
136                      lower-end-point
137                      ;; Step 3: move to upper end point.
138                      upper-end-point
139                      ;; Step 2: curve through outer control points
140                      ;; to upper end point.
141                      lower-outer-control-point
142                      upper-outer-control-point
143                      upper-end-point
144                      ;; Step 1: move to lower end point.
145                      lower-end-point))
146            line-width)
147      (interval-widen x-extent (/ line-width 2))
148      (interval-widen y-extent (/ line-width 2)))))
149
150 (define-public (parenthesize-stencil
151                 stencil half-thickness width angularity padding)
152   "Add parentheses around @var{stencil}, returning a new stencil."
153   (let* ((y-extent (ly:stencil-extent stencil Y))
154          (lp (make-parenthesis-stencil
155               y-extent half-thickness (- width) angularity))
156          (rp (make-parenthesis-stencil
157               y-extent half-thickness width angularity)))
158     (set! stencil (ly:stencil-combine-at-edge lp X RIGHT stencil padding))
159     (set! stencil (ly:stencil-combine-at-edge stencil X RIGHT rp padding))
160     stencil))
161
162 (define-public (make-line-stencil width startx starty endx endy)
163   "Make a line stencil of given linewidth and set its extents accordingly"
164   (let ((xext (cons (min startx endx) (max startx endx)))
165         (yext (cons (min starty endy) (max starty endy))))
166     (ly:make-stencil
167       (list 'draw-line width startx starty endx endy)
168       ; Since the line has rounded edges, we have to / can safely add half the
169       ; width to all coordinates!
170       (interval-widen xext (/ width 2))
171       (interval-widen yext (/ width 2)))))
172
173
174 (define-public (make-filled-box-stencil xext yext)
175   "Make a filled box."
176
177   (ly:make-stencil
178       (list 'round-filled-box (- (car xext)) (cdr xext)
179                        (- (car yext)) (cdr yext) 0.0)
180       xext yext))
181
182 (define-public (make-circle-stencil radius thickness fill)
183   "Make a circle of radius @var{radius} and thickness @var{thickness}"
184   (let*
185       ((out-radius (+ radius (/ thickness 2.0))))
186
187   (ly:make-stencil
188    (list 'circle radius thickness fill)
189    (cons (- out-radius) out-radius)
190    (cons (- out-radius) out-radius))))
191
192 (define-public (make-oval-stencil x-radius y-radius thickness fill)
193   "Make an oval from two Bezier curves, of x radius @var{x-radius},
194     y radius @code{y-radius},
195     and thickness @var{thickness} with fill defined by @code{fill}."
196   (let*
197       ((x-out-radius (+ x-radius (/ thickness 2.0)))
198        (y-out-radius (+ y-radius (/ thickness 2.0))) )
199
200   (ly:make-stencil
201    (list 'oval x-radius y-radius thickness fill)
202    (cons (- x-out-radius) x-out-radius)
203    (cons (- y-out-radius) y-out-radius))))
204
205 (define-public
206   (make-partial-ellipse-stencil
207     x-radius y-radius start-angle end-angle thick connect fill)
208
209   (define (make-radius-list x-radius y-radius)
210     (apply append
211            (map (lambda (adder)
212                   (map (lambda (quadrant)
213                          (cons (+ adder (car quadrant))
214                                (cdr quadrant)))
215                        `((0.0 . (,x-radius . 0.0))
216                          (,PI-OVER-TWO . (0.0 . ,y-radius))
217                          (,PI . (,(- x-radius) . 0.0))
218                          (,THREE-PI-OVER-TWO . (0.0 . ,(- y-radius))))))
219                 `(0.0 ,TWO-PI))))
220
221   (define
222     (insert-in-ordered-list ordering-function value inlist cutl? cutr?)
223     (define
224       (helper ordering-function value left-list right-list cutl? cutr?)
225       (if (null? right-list)
226           (append
227             (if cutl? '() left-list)
228             (list value)
229             (if cutr? '() right-list))
230           (if (ordering-function value (car right-list))
231               (append
232                 (if cutl? '() left-list)
233                 (list value)
234                 (if cutr? '() right-list))
235               (helper
236                 ordering-function
237                 value
238                 (append left-list (list (car right-list)))
239                 (cdr right-list)
240                 cutl?
241                 cutr?))))
242     (helper ordering-function value '() inlist cutl? cutr?))
243
244   (define (ordering-function-1 a b) (car< a b))
245
246   (define (ordering-function-2 a b) (car<= a b))
247
248   (define (min-max-crawler min-max side l)
249     (reduce min-max
250             (if (eq? min-max min) 100000 -100000)
251             (map (lambda (x) (side x)) l)))
252
253   (let*
254       ((x-out-radius (+ x-radius (/ thick 2.0)))
255        (y-out-radius (+ y-radius (/ thick 2.0)))
256        (new-end-angle (angle-0-2pi (degrees->radians end-angle)))
257        (end-radius (ellipse-radius x-out-radius y-out-radius new-end-angle))
258        (new-start-angle (angle-0-2pi (degrees->radians start-angle)))
259        (start-radius (ellipse-radius x-out-radius y-out-radius new-start-angle))
260        (radius-list (make-radius-list x-out-radius y-out-radius))
261        (rectangular-end-radius (polar->rectangular end-radius end-angle))
262        (rectangular-start-radius (polar->rectangular start-radius start-angle))
263        (new-end-angle
264          (if (<= new-end-angle new-start-angle)
265              (+ TWO-PI new-end-angle)
266              new-end-angle))
267        (possible-extrema
268          (insert-in-ordered-list
269            ordering-function-2
270            (cons new-end-angle rectangular-end-radius)
271            (insert-in-ordered-list
272              ordering-function-1
273              (cons new-start-angle rectangular-start-radius)
274              radius-list
275              #t
276              #f)
277            #f
278            #t)))
279     (ly:make-stencil
280       (list
281         'partial-ellipse
282         x-radius
283         y-radius
284         start-angle
285         end-angle
286         thick
287         connect
288         fill)
289       (cons (min-max-crawler min cadr possible-extrema)
290             (min-max-crawler max cadr possible-extrema))
291       (cons (min-max-crawler min cddr possible-extrema)
292             (min-max-crawler max cddr possible-extrema)))))
293
294 (define (connected-shape-min-max pointlist)
295
296   (define (line-part-min-max x1 x2)
297     (list (min x1 x2) (max x1 x2)))
298
299   (define (bezier-part-min-max x1 x2 x3 x4)
300     ((lambda (x) (list (reduce min 10000 x) (reduce max -10000 x)))
301       (map
302         (lambda (x)
303           (+ (* x1 (expt (- 1 x) 3))
304              (+ (* 3 (* x2 (* (expt (- 1 x) 2) x)))
305                 (+ (* 3 (* x3 (* (- 1 x) (expt x 2))))
306                    (* x4 (expt x 3))))))
307         (if (< (+ (expt x2 2) (+ (expt x3 2) (* x1 x4)))
308                (+ (* x1 x3) (+ (* x2 x4) (* x2 x3))))
309             (list 0.0 1.0)
310             (filter
311               (lambda (x) (and (>= x 0) (<= x 1)))
312               (append
313                 (list 0.0 1.0)
314                 (map (lambda (op)
315                        (if (not (eqv? 0.0
316                                       (- (+ x1 (* 3 x3)) (+ x4 (* 3 x2)))))
317                            ;; Zeros of the bezier curve
318                            (/ (+ (- x1 (* 2 x2))
319                                  (op x3
320                                      (sqrt (- (+ (expt x2 2)
321                                                  (+ (expt x3 2) (* x1 x4)))
322                                               (+ (* x1 x3)
323                                                  (+ (* x2 x4) (* x2 x3)))))))
324                               (- (+ x1 (* 3 x3)) (+ x4 (* 3 x2))))
325                            ;; Apply L'hopital's rule to get the zeros if 0/0
326                            (* (op 0 1)
327                               (/ (/ (- x4 x3) 2)
328                                  (sqrt (- (+ (* x2 x2)
329                                              (+ (* x3 x3) (* x1 x4)))
330                                           (+ (* x1 x3)
331                                              (+ (* x2 x4) (* x2 x3)))))))))
332                      (list + -))))))))
333
334   (define (bezier-min-max x1 y1 x2 y2 x3 y3 x4 y4)
335     (map (lambda (x)
336            (apply bezier-part-min-max x))
337          `((,x1 ,x2 ,x3 ,x4) (,y1 ,y2 ,y3 ,y4))))
338
339   (define (line-min-max x1 y1 x2 y2)
340     (map (lambda (x)
341            (apply line-part-min-max x))
342          `((,x1 ,x2) (,y1 ,y2))))
343
344   ((lambda (x)
345      (list
346        (reduce min +inf.0 (map caar x))
347        (reduce max -inf.0 (map cadar x))
348        (reduce min +inf.0 (map caadr x))
349        (reduce max -inf.0 (map cadadr x))))
350     (map (lambda (x)
351            (if (eq? (length x) 8)
352                (apply bezier-min-max x)
353                (apply line-min-max x)))
354          (map (lambda (x y)
355                 (append (list (cadr (reverse x)) (car (reverse x))) y))
356               (append (list (list 0 0))
357                       (reverse (cdr (reverse pointlist)))) pointlist))))
358
359 (define-public (make-connected-shape-stencil pointlist thickness
360                                               x-scale y-scale connect fill)
361   "Make a connected shape described by the list @var{pointlist}, with
362 thickness @var{thickness}, and scaled by @var{x-scale} in the X direction
363 and @var{y-scale} in the Y direction.  @var{connect} and @var{fill} are
364 boolean arguments that specify if the shape should be connected or filled,
365 respectively."
366
367   (let* ((boundlist (connected-shape-min-max pointlist)))
368   (ly:make-stencil
369     `(connected-shape
370       ',pointlist
371       ',thickness
372       ',x-scale
373       ',y-scale
374       ',connect
375       ',fill)
376     (coord-translate
377       ((if (< x-scale 0) reverse-interval identity)
378         (cons (* x-scale (list-ref boundlist 0))
379               (* x-scale (list-ref boundlist 1))))
380         `(,(/ thickness -2) . ,(/ thickness 2)))
381     (coord-translate
382       ((if (< y-scale 0) reverse-interval identity)
383         (cons (* y-scale (list-ref boundlist 2))
384               (* y-scale (list-ref boundlist 3))))
385         `(,(/ thickness -2) . ,(/ thickness 2))))))
386
387 (define-public (make-ellipse-stencil x-radius y-radius thickness fill)
388   "Make an ellipse of x radius @var{x-radius}, y radius @code{y-radius},
389     and thickness @var{thickness} with fill defined by @code{fill}."
390   (let*
391       ((x-out-radius (+ x-radius (/ thickness 2.0)))
392        (y-out-radius (+ y-radius (/ thickness 2.0))) )
393
394   (ly:make-stencil
395    (list 'ellipse x-radius y-radius thickness fill)
396    (cons (- x-out-radius) x-out-radius)
397    (cons (- y-out-radius) y-out-radius))))
398
399 (define-public (box-grob-stencil grob)
400   "Make a box of exactly the extents of the grob.  The box precisely
401 encloses the contents.
402 "
403   (let* ((xext (ly:grob-extent grob grob 0))
404          (yext (ly:grob-extent grob grob 1))
405          (thick 0.01))
406
407     (ly:stencil-add
408      (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
409      (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
410      (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
411      (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
412
413 ;; TODO merge this and prev function.
414 (define-public (box-stencil stencil thickness padding)
415   "Add a box around STENCIL, producing a new stencil."
416   (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
417          (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
418          (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
419          (x-rule (make-filled-box-stencil
420                   (interval-widen x-ext thickness) (cons 0 thickness))))
421     (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
422     (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
423     (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))
424     (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
425     stencil))
426
427 (define-public (circle-stencil stencil thickness padding)
428   "Add a circle around STENCIL, producing a new stencil."
429   (let* ((x-ext (ly:stencil-extent stencil X))
430          (y-ext (ly:stencil-extent stencil Y))
431          (diameter (max (interval-length x-ext)
432                         (interval-length y-ext)))
433          (radius (+ (/ diameter 2) padding thickness))
434          (circle (make-circle-stencil radius thickness #f)))
435
436     (ly:stencil-add
437      stencil
438      (ly:stencil-translate circle
439                            (cons
440                             (interval-center x-ext)
441                             (interval-center y-ext))))))
442
443 (define-public (oval-stencil stencil thickness x-padding y-padding)
444   "Add an oval around @code{stencil}, padded by the padding pair,
445    producing a new stencil."
446   (let* ((x-ext (ly:stencil-extent stencil X))
447          (y-ext (ly:stencil-extent stencil Y))
448          (x-length (+ (interval-length x-ext) x-padding thickness))
449          (y-length (+ (interval-length y-ext) y-padding thickness))
450          (x-radius (* 0.707 x-length) )
451          (y-radius (* 0.707 y-length) )
452          (oval (make-oval-stencil x-radius y-radius thickness #f)))
453
454     (ly:stencil-add
455      stencil
456      (ly:stencil-translate oval
457                            (cons
458                             (interval-center x-ext)
459                             (interval-center y-ext))))))
460
461 (define-public (ellipse-stencil stencil thickness x-padding y-padding)
462   "Add an ellipse around STENCIL, padded by the padding pair,
463    producing a new stencil."
464   (let* ((x-ext (ly:stencil-extent stencil X))
465          (y-ext (ly:stencil-extent stencil Y))
466          (x-length (+ (interval-length x-ext) x-padding thickness))
467          (y-length (+ (interval-length y-ext) y-padding thickness))
468          ;(aspect-ratio (/ x-length y-length))
469          (x-radius (* 0.707 x-length) )
470          (y-radius (* 0.707 y-length) )
471          ;(diameter (max (- (cdr x-ext) (car x-ext))
472          ;              (- (cdr y-ext) (car y-ext))))
473          ;(radius (+ (/ diameter 2) padding thickness))
474          (ellipse (make-ellipse-stencil x-radius y-radius thickness #f)))
475
476     (ly:stencil-add
477      stencil
478      (ly:stencil-translate ellipse
479                            (cons
480                             (interval-center x-ext)
481                             (interval-center y-ext))))))
482
483 (define-public (rounded-box-stencil stencil thickness padding blot)
484    "Add a rounded box around STENCIL, producing a new stencil."
485
486   (let* ((xext (interval-widen (ly:stencil-extent stencil 0) padding))
487          (yext (interval-widen (ly:stencil-extent stencil 1) padding))
488    (min-ext (min (-(cdr xext) (car xext)) (-(cdr yext) (car yext))))
489    (ideal-blot (min blot (/ min-ext 2)))
490    (ideal-thickness (min thickness (/ min-ext 2)))
491          (outer (ly:round-filled-box
492             (interval-widen xext ideal-thickness)
493             (interval-widen yext ideal-thickness)
494                ideal-blot))
495          (inner (ly:make-stencil (list 'color (x11-color 'white)
496             (ly:stencil-expr (ly:round-filled-box
497                xext yext (- ideal-blot ideal-thickness)))))))
498     (set! stencil (ly:stencil-add outer inner))
499     stencil))
500
501 (define-public (stencil-with-color stencil color)
502   (ly:make-stencil
503    (list 'color color (ly:stencil-expr stencil))
504    (ly:stencil-extent stencil X)
505    (ly:stencil-extent stencil Y)))
506
507 (define-public (stencil-whiteout stencil)
508   (let*
509       ((x-ext (ly:stencil-extent stencil X))
510        (y-ext (ly:stencil-extent stencil Y))
511
512        )
513
514     (ly:stencil-add
515      (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
516                          white)
517      stencil)
518     ))
519
520 (define-public (arrow-stencil-maker start? end?)
521   "Returns a function drawing a line from current point to @var{destination},
522    with optional arrows of @var{max-size} on start and end controlled by
523    @var{start?} and @var{end?}."
524   (lambda (destination max-size)
525   (let*
526       ((e_x 1+0i)
527        (e_y 0+1i)
528        (distance (sqrt (+ (* (car destination) (car destination))
529                           (* (cdr destination) (cdr destination)))))
530        (size (min max-size (/ distance 3)))
531        (rotate (lambda (z ang)
532                  (* (make-polar 1 ang)
533                     z)))
534        (complex-to-offset (lambda (z)
535                             (list (real-part z) (imag-part z))))
536
537        (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
538        (e_z (/ z-dest (magnitude z-dest)))
539        (triangle-points (list
540                          (* size -1+0.25i)
541                          0
542                          (* size -1-0.25i)))
543        (p1s (map (lambda (z)
544                    (+ z-dest (rotate z (angle z-dest))))
545                  triangle-points))
546        (p2s (map (lambda (z)
547                    (rotate z (angle (- z-dest))))
548                    triangle-points))
549        (null (cons 0 0))
550        (arrow-1
551         (ly:make-stencil
552          `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
553                    0.0
554                    #t) null null))
555        (arrow-2
556         (ly:make-stencil
557          `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
558                    0.0
559                    #t) null null ) )
560        (thickness (min (/ distance 12) 0.1))
561        (shorten-line (min (/ distance 3) 0.5))
562        (start (complex-to-offset (/ (* e_z shorten-line) 2)))
563        (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
564
565        (line (ly:make-stencil
566               `(draw-line ,thickness
567                           ,(car start) ,(cadr start)
568                           ,(car end) ,(cadr end)
569                           )
570               (cons (min 0 (car destination))
571                     (min 0 (cdr destination)))
572               (cons (max 0 (car destination))
573                     (max 0 (cdr destination)))))
574
575        (result
576          (ly:stencil-add
577            (if start? arrow-2 empty-stencil)
578            (if end? arrow-1 empty-stencil)
579            line)))
580
581     result)))
582
583 (define-public dimension-arrows (arrow-stencil-maker #t #t))
584
585 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
586 ;; ANNOTATIONS
587 ;;
588 ;; annotations are arrows indicating the numerical value of
589 ;; spacing variables
590 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
591
592 (define*-public (annotate-y-interval layout name extent is-length
593                                      #:key (color darkblue))
594   (let ((text-props (cons '((font-size . -3)
595                             (font-family . typewriter))
596                           (layout-extract-page-properties layout)))
597         (annotation #f))
598     (define (center-stencil-on-extent stil)
599       (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
600                             (cons 0 (interval-center extent))))
601     ;; do something sensible for 0,0 intervals.
602     (set! extent (interval-widen extent 0.001))
603     (if (not (interval-sane? extent))
604         (set! annotation (interpret-markup
605                           layout text-props
606                           (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
607         (let ((text-stencil (interpret-markup
608                              layout text-props
609                              (markup #:whiteout #:simple name)))
610               (dim-stencil (interpret-markup
611                             layout text-props
612                             (markup #:whiteout
613                                     #:simple (cond
614                                               ((interval-empty? extent)
615                                                (format "empty"))
616                                               (is-length
617                                                (ly:format "~$" (interval-length extent)))
618                                               (else
619                                                (ly:format "(~$,~$)"
620                                                        (car extent) (cdr extent)))))))
621               (arrows (ly:stencil-translate-axis
622                        (dimension-arrows (cons 0 (interval-length extent)) 1.0)
623                        (interval-start extent) Y)))
624           (set! annotation
625                 (center-stencil-on-extent text-stencil))
626           (set! annotation
627                 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
628           (set! annotation
629                 (ly:stencil-combine-at-edge annotation X LEFT
630                                             (center-stencil-on-extent dim-stencil)
631                                             0.5))
632           (set! annotation
633                 (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
634                                  (ly:stencil-extent annotation X)
635                                  (cons 10000 -10000)))))
636     annotation))
637
638
639 (define*-public (annotate-spacing-spec layout spacing-spec start-Y-offset prev-system-end
640                                       #:key (base-color blue))
641   (let* ((get-spacing-var (lambda (sym) (assoc-get sym spacing-spec 0.0)))
642          (space (get-spacing-var 'space))
643          (padding (get-spacing-var 'padding))
644          (min-dist (get-spacing-var 'minimum-distance))
645          (contrast-color (append (cdr base-color) (list (car base-color)))))
646     (stack-stencils X RIGHT 0.0
647                     (list
648                      (annotate-y-interval layout
649                                           "space"
650                                           (cons (- start-Y-offset space) start-Y-offset)
651                                           #t
652                                           #:color (map (lambda (x) (* x 0.25)) base-color))
653                      (annotate-y-interval layout
654                                           "min-dist"
655                                           (cons (- start-Y-offset min-dist) start-Y-offset)
656                                           #t
657                                           #:color (map (lambda (x) (* x 0.5)) base-color))
658                      (ly:stencil-add
659                       (annotate-y-interval layout
660                                            "bottom-of-extent"
661                                            (cons prev-system-end start-Y-offset)
662                                            #t
663                                            #:color base-color)
664                       (annotate-y-interval layout
665                                            "padding"
666                                            (cons (- prev-system-end padding) prev-system-end)
667                                            #t
668                                            #:color contrast-color))))))
669
670
671 (define-public (eps-file->stencil axis size file-name)
672   (let*
673       ((contents (ly:gulp-file file-name))
674        (bbox (get-postscript-bbox (car (string-split contents #\nul))))
675        (bbox-size (if (= axis X)
676                       (- (list-ref bbox 2) (list-ref bbox 0))
677                       (- (list-ref bbox 3) (list-ref bbox 1))
678                       ))
679        (factor (if (< 0 bbox-size)
680                    (exact->inexact (/ size bbox-size))
681                    0))
682        (scaled-bbox
683         (map (lambda (x) (* factor x)) bbox))
684        ; We need to shift the whole eps to (0,0), otherwise it will appear
685        ; displaced in lilypond (displacement will depend on the scaling!)
686        (translate-string (ly:format "~a ~a translate" (- (list-ref bbox 0)) (- (list-ref bbox 1))))
687        (clip-rect-string (ly:format
688                           "~a ~a ~a ~a rectclip"
689                           (list-ref bbox 0)
690                           (list-ref bbox 1)
691                           (- (list-ref bbox 2) (list-ref bbox 0))
692                           (- (list-ref bbox 3) (list-ref bbox 1)))))
693
694
695     (if bbox
696         (ly:make-stencil
697          (list
698           'embedded-ps
699           (string-append
700            (ly:format
701            "
702 gsave
703 currentpoint translate
704 BeginEPSF
705 ~a dup scale
706 ~a
707 ~a
708 %%BeginDocument: ~a
709 "         factor translate-string  clip-rect-string
710
711            file-name
712            )
713            contents
714            "%%EndDocument
715 EndEPSF
716 grestore
717 "))
718          ; Stencil starts at (0,0), since we have shifted the eps, and its
719          ; size is exactly the size of the scaled bounding box
720          (cons 0 (- (list-ref scaled-bbox 2) (list-ref scaled-bbox 0)))
721          (cons 0 (- (list-ref scaled-bbox 3) (list-ref scaled-bbox 1))))
722
723         (ly:make-stencil "" '(0 . 0) '(0 . 0)))
724     ))
725
726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727 ;; output signatures.
728
729 (define-public (write-system-signatures basename paper-systems count)
730   (if (pair? paper-systems)
731       (begin
732         (let*
733             ((outname (simple-format #f "~a-~a.signature" basename count)) )
734
735           (ly:message "Writing ~a" outname)
736           (write-system-signature outname (car paper-systems))
737           (write-system-signatures basename (cdr paper-systems) (1+ count))))))
738
739 (use-modules (scm paper-system))
740 (define-public (write-system-signature filename paper-system)
741   (define (float? x)
742     (and (number? x) (inexact? x)))
743
744   (define system-grob
745     (paper-system-system-grob paper-system))
746
747   (define output (open-output-file filename))
748
749   ;; todo: optionally use a command line flag? Or just junk this?
750   (define compare-expressions #f)
751   (define (strip-floats expr)
752     "Replace floats by #f"
753     (cond
754      ((float? expr) #f)
755      ((ly:font-metric? expr) (ly:font-name expr))
756      ((pair? expr) (cons (strip-floats (car expr))
757                          (strip-floats (cdr expr))))
758      (else expr)))
759
760   (define (fold-false-pairs expr)
761     "Try to remove lists of #f as much as possible."
762     (if (pair? expr)
763         (let*
764             ((first (car expr))
765              (rest (fold-false-pairs (cdr expr))))
766
767           (if first
768               (cons (fold-false-pairs first) rest)
769               rest))
770         expr))
771
772   (define (raw-string expr)
773     "escape quotes and slashes for python consumption"
774     (regexp-substitute/global #f "[@\n]" (simple-format #f "~a" expr) 'pre " " 'post))
775
776   (define (raw-pair expr)
777     (simple-format #f "~a ~a"
778             (car expr) (cdr expr)))
779
780   (define (found-grob expr)
781     (let*
782         ((grob (car expr))
783          (rest (cdr expr))
784          (collected '())
785          (cause (event-cause grob))
786          (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
787          (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
788
789          ;; todo: use stencil extent if available.
790          (x-ext (ly:grob-extent grob system-grob X))
791          (y-ext (ly:grob-extent grob system-grob Y))
792          (expression-skeleton
793           (if compare-expressions
794               (interpret-for-signature
795                #f (lambda (e)
796                     (set! collected (cons e collected)))
797                rest)
798              "")))
799
800       (simple-format output
801               "~a@~a@~a@~a@~a\n"
802               (cdr (assq 'name (ly:grob-property grob 'meta) ))
803               (raw-string location)
804               (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
805               (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
806               (raw-string collected))
807       ))
808
809   (define (interpret-for-signature escape collect expr)
810     (define (interpret expr)
811       (let*
812           ((head (if (pair? expr)
813                      (car expr)
814                      #f)))
815
816         (cond
817          ((eq? head 'grob-cause) (escape (cdr expr)))
818          ((eq? head 'color) (interpret (caddr expr)))
819          ((eq? head 'rotate-stencil) (interpret (caddr expr)))
820          ((eq? head 'translate-stencil) (interpret (caddr expr)))
821          ((eq? head 'combine-stencil)
822           (for-each (lambda (e) (interpret e))  (cdr expr)))
823          (else
824           (collect (fold-false-pairs (strip-floats expr))))
825
826          )))
827
828     (interpret expr))
829
830   (if (ly:grob? system-grob)
831       (begin
832         (display (simple-format #f "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
833                  output)
834         (interpret-for-signature found-grob (lambda (x) #f)
835                                  (ly:stencil-expr
836                                   (paper-system-stencil paper-system)))))
837
838   ;; should be superfluous, but leaking "too many open files"?
839   (close-port output))
840