]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
Make the origin of a connected-shape configurable.
[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 origin 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 origin)
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   ;; a connected shape path must begin at point '(0 . 0)
368   (let* ((origin (list 0 0))
369          (boundlist (connected-shape-min-max origin pointlist)))
370   (ly:make-stencil
371     `(connected-shape
372       ',pointlist
373       ',thickness
374       ',x-scale
375       ',y-scale
376       ',connect
377       ',fill)
378     (coord-translate
379       ((if (< x-scale 0) reverse-interval identity)
380         (cons (* x-scale (list-ref boundlist 0))
381               (* x-scale (list-ref boundlist 1))))
382         `(,(/ thickness -2) . ,(/ thickness 2)))
383     (coord-translate
384       ((if (< y-scale 0) reverse-interval identity)
385         (cons (* y-scale (list-ref boundlist 2))
386               (* y-scale (list-ref boundlist 3))))
387         `(,(/ thickness -2) . ,(/ thickness 2))))))
388
389 (define-public (make-ellipse-stencil x-radius y-radius thickness fill)
390   "Make an ellipse of x radius @var{x-radius}, y radius @code{y-radius},
391     and thickness @var{thickness} with fill defined by @code{fill}."
392   (let*
393       ((x-out-radius (+ x-radius (/ thickness 2.0)))
394        (y-out-radius (+ y-radius (/ thickness 2.0))) )
395
396   (ly:make-stencil
397    (list 'ellipse x-radius y-radius thickness fill)
398    (cons (- x-out-radius) x-out-radius)
399    (cons (- y-out-radius) y-out-radius))))
400
401 (define-public (box-grob-stencil grob)
402   "Make a box of exactly the extents of the grob.  The box precisely
403 encloses the contents.
404 "
405   (let* ((xext (ly:grob-extent grob grob 0))
406          (yext (ly:grob-extent grob grob 1))
407          (thick 0.01))
408
409     (ly:stencil-add
410      (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
411      (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
412      (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
413      (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
414
415 ;; TODO merge this and prev function.
416 (define-public (box-stencil stencil thickness padding)
417   "Add a box around STENCIL, producing a new stencil."
418   (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
419          (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
420          (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
421          (x-rule (make-filled-box-stencil
422                   (interval-widen x-ext thickness) (cons 0 thickness))))
423     (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
424     (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
425     (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))
426     (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
427     stencil))
428
429 (define-public (circle-stencil stencil thickness padding)
430   "Add a circle around STENCIL, producing a new stencil."
431   (let* ((x-ext (ly:stencil-extent stencil X))
432          (y-ext (ly:stencil-extent stencil Y))
433          (diameter (max (interval-length x-ext)
434                         (interval-length y-ext)))
435          (radius (+ (/ diameter 2) padding thickness))
436          (circle (make-circle-stencil radius thickness #f)))
437
438     (ly:stencil-add
439      stencil
440      (ly:stencil-translate circle
441                            (cons
442                             (interval-center x-ext)
443                             (interval-center y-ext))))))
444
445 (define-public (oval-stencil stencil thickness x-padding y-padding)
446   "Add an oval around @code{stencil}, padded by the padding pair,
447    producing a new stencil."
448   (let* ((x-ext (ly:stencil-extent stencil X))
449          (y-ext (ly:stencil-extent stencil Y))
450          (x-length (+ (interval-length x-ext) x-padding thickness))
451          (y-length (+ (interval-length y-ext) y-padding thickness))
452          (x-radius (* 0.707 x-length) )
453          (y-radius (* 0.707 y-length) )
454          (oval (make-oval-stencil x-radius y-radius thickness #f)))
455
456     (ly:stencil-add
457      stencil
458      (ly:stencil-translate oval
459                            (cons
460                             (interval-center x-ext)
461                             (interval-center y-ext))))))
462
463 (define-public (ellipse-stencil stencil thickness x-padding y-padding)
464   "Add an ellipse around STENCIL, padded by the padding pair,
465    producing a new stencil."
466   (let* ((x-ext (ly:stencil-extent stencil X))
467          (y-ext (ly:stencil-extent stencil Y))
468          (x-length (+ (interval-length x-ext) x-padding thickness))
469          (y-length (+ (interval-length y-ext) y-padding thickness))
470          ;(aspect-ratio (/ x-length y-length))
471          (x-radius (* 0.707 x-length) )
472          (y-radius (* 0.707 y-length) )
473          ;(diameter (max (- (cdr x-ext) (car x-ext))
474          ;              (- (cdr y-ext) (car y-ext))))
475          ;(radius (+ (/ diameter 2) padding thickness))
476          (ellipse (make-ellipse-stencil x-radius y-radius thickness #f)))
477
478     (ly:stencil-add
479      stencil
480      (ly:stencil-translate ellipse
481                            (cons
482                             (interval-center x-ext)
483                             (interval-center y-ext))))))
484
485 (define-public (rounded-box-stencil stencil thickness padding blot)
486    "Add a rounded box around STENCIL, producing a new stencil."
487
488   (let* ((xext (interval-widen (ly:stencil-extent stencil 0) padding))
489          (yext (interval-widen (ly:stencil-extent stencil 1) padding))
490    (min-ext (min (-(cdr xext) (car xext)) (-(cdr yext) (car yext))))
491    (ideal-blot (min blot (/ min-ext 2)))
492    (ideal-thickness (min thickness (/ min-ext 2)))
493          (outer (ly:round-filled-box
494             (interval-widen xext ideal-thickness)
495             (interval-widen yext ideal-thickness)
496                ideal-blot))
497          (inner (ly:make-stencil (list 'color (x11-color 'white)
498             (ly:stencil-expr (ly:round-filled-box
499                xext yext (- ideal-blot ideal-thickness)))))))
500     (set! stencil (ly:stencil-add outer inner))
501     stencil))
502
503 (define-public (stencil-with-color stencil color)
504   (ly:make-stencil
505    (list 'color color (ly:stencil-expr stencil))
506    (ly:stencil-extent stencil X)
507    (ly:stencil-extent stencil Y)))
508
509 (define-public (stencil-whiteout stencil)
510   (let*
511       ((x-ext (ly:stencil-extent stencil X))
512        (y-ext (ly:stencil-extent stencil Y))
513
514        )
515
516     (ly:stencil-add
517      (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
518                          white)
519      stencil)
520     ))
521
522 (define-public (arrow-stencil-maker start? end?)
523   "Returns a function drawing a line from current point to @var{destination},
524    with optional arrows of @var{max-size} on start and end controlled by
525    @var{start?} and @var{end?}."
526   (lambda (destination max-size)
527   (let*
528       ((e_x 1+0i)
529        (e_y 0+1i)
530        (distance (sqrt (+ (* (car destination) (car destination))
531                           (* (cdr destination) (cdr destination)))))
532        (size (min max-size (/ distance 3)))
533        (rotate (lambda (z ang)
534                  (* (make-polar 1 ang)
535                     z)))
536        (complex-to-offset (lambda (z)
537                             (list (real-part z) (imag-part z))))
538
539        (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
540        (e_z (/ z-dest (magnitude z-dest)))
541        (triangle-points (list
542                          (* size -1+0.25i)
543                          0
544                          (* size -1-0.25i)))
545        (p1s (map (lambda (z)
546                    (+ z-dest (rotate z (angle z-dest))))
547                  triangle-points))
548        (p2s (map (lambda (z)
549                    (rotate z (angle (- z-dest))))
550                    triangle-points))
551        (null (cons 0 0))
552        (arrow-1
553         (ly:make-stencil
554          `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
555                    0.0
556                    #t) null null))
557        (arrow-2
558         (ly:make-stencil
559          `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
560                    0.0
561                    #t) null null ) )
562        (thickness (min (/ distance 12) 0.1))
563        (shorten-line (min (/ distance 3) 0.5))
564        (start (complex-to-offset (/ (* e_z shorten-line) 2)))
565        (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
566
567        (line (ly:make-stencil
568               `(draw-line ,thickness
569                           ,(car start) ,(cadr start)
570                           ,(car end) ,(cadr end)
571                           )
572               (cons (min 0 (car destination))
573                     (min 0 (cdr destination)))
574               (cons (max 0 (car destination))
575                     (max 0 (cdr destination)))))
576
577        (result
578          (ly:stencil-add
579            (if start? arrow-2 empty-stencil)
580            (if end? arrow-1 empty-stencil)
581            line)))
582
583     result)))
584
585 (define-public dimension-arrows (arrow-stencil-maker #t #t))
586
587 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
588 ;; ANNOTATIONS
589 ;;
590 ;; annotations are arrows indicating the numerical value of
591 ;; spacing variables
592 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
593
594 (define*-public (annotate-y-interval layout name extent is-length
595                                      #:key (color darkblue))
596   (let ((text-props (cons '((font-size . -3)
597                             (font-family . typewriter))
598                           (layout-extract-page-properties layout)))
599         (annotation #f))
600     (define (center-stencil-on-extent stil)
601       (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
602                             (cons 0 (interval-center extent))))
603     ;; do something sensible for 0,0 intervals.
604     (set! extent (interval-widen extent 0.001))
605     (if (not (interval-sane? extent))
606         (set! annotation (interpret-markup
607                           layout text-props
608                           (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
609         (let ((text-stencil (interpret-markup
610                              layout text-props
611                              (markup #:whiteout #:simple name)))
612               (dim-stencil (interpret-markup
613                             layout text-props
614                             (markup #:whiteout
615                                     #:simple (cond
616                                               ((interval-empty? extent)
617                                                (format "empty"))
618                                               (is-length
619                                                (ly:format "~$" (interval-length extent)))
620                                               (else
621                                                (ly:format "(~$,~$)"
622                                                        (car extent) (cdr extent)))))))
623               (arrows (ly:stencil-translate-axis
624                        (dimension-arrows (cons 0 (interval-length extent)) 1.0)
625                        (interval-start extent) Y)))
626           (set! annotation
627                 (center-stencil-on-extent text-stencil))
628           (set! annotation
629                 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
630           (set! annotation
631                 (ly:stencil-combine-at-edge annotation X LEFT
632                                             (center-stencil-on-extent dim-stencil)
633                                             0.5))
634           (set! annotation
635                 (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
636                                  (ly:stencil-extent annotation X)
637                                  (cons 10000 -10000)))))
638     annotation))
639
640
641 (define*-public (annotate-spacing-spec layout spacing-spec start-Y-offset prev-system-end
642                                       #:key (base-color blue))
643   (let* ((get-spacing-var (lambda (sym) (assoc-get sym spacing-spec 0.0)))
644          (space (get-spacing-var 'space))
645          (padding (get-spacing-var 'padding))
646          (min-dist (get-spacing-var 'minimum-distance))
647          (contrast-color (append (cdr base-color) (list (car base-color)))))
648     (stack-stencils X RIGHT 0.0
649                     (list
650                      (annotate-y-interval layout
651                                           "space"
652                                           (cons (- start-Y-offset space) start-Y-offset)
653                                           #t
654                                           #:color (map (lambda (x) (* x 0.25)) base-color))
655                      (annotate-y-interval layout
656                                           "min-dist"
657                                           (cons (- start-Y-offset min-dist) start-Y-offset)
658                                           #t
659                                           #:color (map (lambda (x) (* x 0.5)) base-color))
660                      (ly:stencil-add
661                       (annotate-y-interval layout
662                                            "bottom-of-extent"
663                                            (cons prev-system-end start-Y-offset)
664                                            #t
665                                            #:color base-color)
666                       (annotate-y-interval layout
667                                            "padding"
668                                            (cons (- prev-system-end padding) prev-system-end)
669                                            #t
670                                            #:color contrast-color))))))
671
672
673 (define-public (eps-file->stencil axis size file-name)
674   (let*
675       ((contents (ly:gulp-file file-name))
676        (bbox (get-postscript-bbox (car (string-split contents #\nul))))
677        (bbox-size (if (= axis X)
678                       (- (list-ref bbox 2) (list-ref bbox 0))
679                       (- (list-ref bbox 3) (list-ref bbox 1))
680                       ))
681        (factor (if (< 0 bbox-size)
682                    (exact->inexact (/ size bbox-size))
683                    0))
684        (scaled-bbox
685         (map (lambda (x) (* factor x)) bbox))
686        ; We need to shift the whole eps to (0,0), otherwise it will appear
687        ; displaced in lilypond (displacement will depend on the scaling!)
688        (translate-string (ly:format "~a ~a translate" (- (list-ref bbox 0)) (- (list-ref bbox 1))))
689        (clip-rect-string (ly:format
690                           "~a ~a ~a ~a rectclip"
691                           (list-ref bbox 0)
692                           (list-ref bbox 1)
693                           (- (list-ref bbox 2) (list-ref bbox 0))
694                           (- (list-ref bbox 3) (list-ref bbox 1)))))
695
696
697     (if bbox
698         (ly:make-stencil
699          (list
700           'embedded-ps
701           (string-append
702            (ly:format
703            "
704 gsave
705 currentpoint translate
706 BeginEPSF
707 ~a dup scale
708 ~a
709 ~a
710 %%BeginDocument: ~a
711 "         factor translate-string  clip-rect-string
712
713            file-name
714            )
715            contents
716            "%%EndDocument
717 EndEPSF
718 grestore
719 "))
720          ; Stencil starts at (0,0), since we have shifted the eps, and its
721          ; size is exactly the size of the scaled bounding box
722          (cons 0 (- (list-ref scaled-bbox 2) (list-ref scaled-bbox 0)))
723          (cons 0 (- (list-ref scaled-bbox 3) (list-ref scaled-bbox 1))))
724
725         (ly:make-stencil "" '(0 . 0) '(0 . 0)))
726     ))
727
728 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
729 ;; output signatures.
730
731 (define-public (write-system-signatures basename paper-systems count)
732   (if (pair? paper-systems)
733       (begin
734         (let*
735             ((outname (simple-format #f "~a-~a.signature" basename count)) )
736
737           (ly:message "Writing ~a" outname)
738           (write-system-signature outname (car paper-systems))
739           (write-system-signatures basename (cdr paper-systems) (1+ count))))))
740
741 (use-modules (scm paper-system))
742 (define-public (write-system-signature filename paper-system)
743   (define (float? x)
744     (and (number? x) (inexact? x)))
745
746   (define system-grob
747     (paper-system-system-grob paper-system))
748
749   (define output (open-output-file filename))
750
751   ;; todo: optionally use a command line flag? Or just junk this?
752   (define compare-expressions #f)
753   (define (strip-floats expr)
754     "Replace floats by #f"
755     (cond
756      ((float? expr) #f)
757      ((ly:font-metric? expr) (ly:font-name expr))
758      ((pair? expr) (cons (strip-floats (car expr))
759                          (strip-floats (cdr expr))))
760      (else expr)))
761
762   (define (fold-false-pairs expr)
763     "Try to remove lists of #f as much as possible."
764     (if (pair? expr)
765         (let*
766             ((first (car expr))
767              (rest (fold-false-pairs (cdr expr))))
768
769           (if first
770               (cons (fold-false-pairs first) rest)
771               rest))
772         expr))
773
774   (define (raw-string expr)
775     "escape quotes and slashes for python consumption"
776     (regexp-substitute/global #f "[@\n]" (simple-format #f "~a" expr) 'pre " " 'post))
777
778   (define (raw-pair expr)
779     (simple-format #f "~a ~a"
780             (car expr) (cdr expr)))
781
782   (define (found-grob expr)
783     (let*
784         ((grob (car expr))
785          (rest (cdr expr))
786          (collected '())
787          (cause (event-cause grob))
788          (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
789          (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
790
791          ;; todo: use stencil extent if available.
792          (x-ext (ly:grob-extent grob system-grob X))
793          (y-ext (ly:grob-extent grob system-grob Y))
794          (expression-skeleton
795           (if compare-expressions
796               (interpret-for-signature
797                #f (lambda (e)
798                     (set! collected (cons e collected)))
799                rest)
800              "")))
801
802       (simple-format output
803               "~a@~a@~a@~a@~a\n"
804               (cdr (assq 'name (ly:grob-property grob 'meta) ))
805               (raw-string location)
806               (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
807               (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
808               (raw-string collected))
809       ))
810
811   (define (interpret-for-signature escape collect expr)
812     (define (interpret expr)
813       (let*
814           ((head (if (pair? expr)
815                      (car expr)
816                      #f)))
817
818         (cond
819          ((eq? head 'grob-cause) (escape (cdr expr)))
820          ((eq? head 'color) (interpret (caddr expr)))
821          ((eq? head 'rotate-stencil) (interpret (caddr expr)))
822          ((eq? head 'translate-stencil) (interpret (caddr expr)))
823          ((eq? head 'combine-stencil)
824           (for-each (lambda (e) (interpret e))  (cdr expr)))
825          (else
826           (collect (fold-false-pairs (strip-floats expr))))
827
828          )))
829
830     (interpret expr))
831
832   (if (ly:grob? system-grob)
833       (begin
834         (display (simple-format #f "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
835                  output)
836         (interpret-for-signature found-grob (lambda (x) #f)
837                                  (ly:stencil-expr
838                                   (paper-system-stencil paper-system)))))
839
840   ;; should be superfluous, but leaking "too many open files"?
841   (close-port output))
842