]> git.donarmstrong.com Git - lilypond.git/blob - scm/stencil.scm
Merge branch 'lilypond/translation' of ssh://trettig@git.sv.gnu.org/srv/git/lilypond...
[lilypond.git] / scm / stencil.scm
1 ;;;; stencil.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2003--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7 (define-public (stack-stencils axis dir padding stils)
8   "Stack stencils STILS in direction AXIS, DIR, using PADDING."
9   (cond
10    ((null? stils) empty-stencil)
11    ((null? (cdr stils)) (car stils))
12    (else (ly:stencil-combine-at-edge
13           (car stils) axis dir (stack-stencils axis dir padding (cdr stils))
14           padding))))
15
16 (define-public (stack-stencils-padding-list axis dir padding stils)
17   "Stack stencils STILS in direction AXIS, DIR, using a list of PADDING."
18   (cond
19    ((null? stils) empty-stencil)
20    ((null? (cdr stils)) (car stils))
21    (else (ly:stencil-combine-at-edge
22           (car stils)
23           axis dir
24           (stack-stencils-padding-list axis dir (cdr padding) (cdr stils))
25           (car padding)))))
26
27 (define-public (centered-stencil stencil)
28   "Center stencil @var{stencil} in both the X and Y directions"
29   (ly:stencil-aligned-to (ly:stencil-aligned-to stencil X CENTER) Y CENTER))
30
31 (define-public (stack-lines dir padding baseline stils)
32   "Stack vertically with a baseline-skip."
33   (define result empty-stencil)
34   (define last-y #f)
35   (do
36       ((last-stencil #f (car p))
37        (p stils (cdr p)))
38       
39       ((null? p))
40
41     (if (number? last-y)
42         (begin
43           (let* ((dy (max (+ (* dir (interval-bound (ly:stencil-extent last-stencil Y) dir))
44                              padding
45                              (* (- dir) (interval-bound (ly:stencil-extent (car p) Y) (- dir))))
46                           baseline))
47                  (y (+ last-y  (* dir dy))))
48             
49                           
50             
51             (set! result
52                   (ly:stencil-add result (ly:stencil-translate-axis (car p) y Y)))
53             (set! last-y y)))
54         (begin
55           (set! last-y 0)
56           (set! result (car p)))))
57
58   result)
59     
60
61 (define-public (bracketify-stencil stil axis thick protusion padding)
62   "Add brackets around STIL, producing a new stencil."
63
64   (let* ((ext (ly:stencil-extent stil axis))
65          (lb (ly:bracket axis ext thick (- protusion)))
66          (rb (ly:bracket axis ext thick protusion)))
67     (set! stil
68           (ly:stencil-combine-at-edge stil (other-axis axis) 1 lb padding))
69     (set! stil
70           (ly:stencil-combine-at-edge stil (other-axis axis) -1 rb padding))
71     stil))
72
73 (define-public (make-filled-box-stencil xext yext)
74   "Make a filled box."
75   
76   (ly:make-stencil
77       (list 'round-filled-box (- (car xext)) (cdr xext)
78                        (- (car yext)) (cdr yext) 0.0)
79       xext yext))
80
81 (define-public (make-circle-stencil radius thickness fill)
82   "Make a circle of radius @var{radius} and thickness @var{thickness}"
83   (let*
84       ((out-radius (+ radius (/ thickness 2.0))))
85     
86   (ly:make-stencil
87    (list 'circle radius thickness fill) 
88    (cons (- out-radius) out-radius)
89    (cons (- out-radius) out-radius))))
90
91 (define-public (box-grob-stencil grob)
92   "Make a box of exactly the extents of the grob.  The box precisely
93 encloses the contents.
94 "
95   (let* ((xext (ly:grob-extent grob grob 0))
96          (yext (ly:grob-extent grob grob 1))
97          (thick 0.1))
98     
99     (ly:stencil-add
100      (make-filled-box-stencil xext (cons (- (car yext) thick) (car yext)))
101      (make-filled-box-stencil xext (cons (cdr yext) (+ (cdr yext) thick)))
102      (make-filled-box-stencil (cons (cdr xext) (+ (cdr xext) thick)) yext)
103      (make-filled-box-stencil (cons (- (car xext) thick) (car xext)) yext))))
104
105 ;; TODO merge this and prev function. 
106 (define-public (box-stencil stencil thickness padding)
107   "Add a box around STENCIL, producing a new stencil."
108   (let* ((x-ext (interval-widen (ly:stencil-extent stencil 0) padding))
109          (y-ext (interval-widen (ly:stencil-extent stencil 1) padding))
110          (y-rule (make-filled-box-stencil (cons 0 thickness) y-ext))
111          (x-rule (make-filled-box-stencil
112                   (interval-widen x-ext thickness) (cons 0 thickness))))
113     (set! stencil (ly:stencil-combine-at-edge stencil X 1 y-rule padding))
114     (set! stencil (ly:stencil-combine-at-edge stencil X -1 y-rule padding))
115     (set! stencil (ly:stencil-combine-at-edge stencil Y 1 x-rule 0.0))  
116     (set! stencil (ly:stencil-combine-at-edge stencil Y -1 x-rule 0.0))
117     stencil))
118
119 (define-public (circle-stencil stencil thickness padding)
120   "Add a circle around STENCIL, producing a new stencil."
121   (let* ((x-ext (ly:stencil-extent stencil 0))
122          (y-ext (ly:stencil-extent stencil 1))
123          (diameter (max (- (cdr x-ext) (car x-ext))
124                         (- (cdr y-ext) (car y-ext))))
125          (radius (+ (/ diameter 2) padding thickness))
126          (circle (make-circle-stencil radius thickness #f)))
127
128     (ly:stencil-add
129      stencil
130      (ly:stencil-translate circle
131                            (cons
132                             (interval-center x-ext)
133                             (interval-center y-ext))))))
134
135 (define-public (rounded-box-stencil stencil thickness padding blot)
136    "Add a rounded box around STENCIL, producing a new stencil."  
137
138   (let* ((xext (interval-widen (ly:stencil-extent stencil 0) padding))
139          (yext (interval-widen (ly:stencil-extent stencil 1) padding))
140    (min-ext (min (-(cdr xext) (car xext)) (-(cdr yext) (car yext))))
141    (ideal-blot (min blot (/ min-ext 2)))
142    (ideal-thickness (min thickness (/ min-ext 2)))
143          (outer (ly:round-filled-box
144             (interval-widen xext ideal-thickness) 
145             (interval-widen yext ideal-thickness) 
146                ideal-blot))
147          (inner (ly:make-stencil (list 'color (x11-color 'white) 
148             (ly:stencil-expr (ly:round-filled-box 
149                xext yext (- ideal-blot ideal-thickness)))))))
150     (set! stencil (ly:stencil-add outer inner))
151     stencil))
152
153
154 (define-public (fontify-text font-metric text)
155   "Set TEXT with font FONT-METRIC, returning a stencil."
156   (let* ((b (ly:text-dimension font-metric text)))
157     (ly:make-stencil
158      `(text ,font-metric ,text) (car b) (cdr b))))
159      
160 (define-public (fontify-text-white scale font-metric text)
161   "Set TEXT with scale factor SCALE"
162   (let* ((b (ly:text-dimension font-metric text))
163          ;;urg -- workaround for using ps font
164          (c `(white-text ,(* 2 scale) ,text)))
165     ;;urg -- extent is not from ps font, but we hope it's close
166     (ly:make-stencil c (car b) (cdr b))))
167
168 (define-public (stencil-with-color stencil color)
169   (ly:make-stencil
170    (list 'color color (ly:stencil-expr stencil))
171    (ly:stencil-extent stencil X)
172    (ly:stencil-extent stencil Y)))
173   
174 (define-public (stencil-whiteout stencil)
175   (let*
176       ((x-ext (ly:stencil-extent stencil X))
177        (y-ext (ly:stencil-extent stencil Y))
178
179        )
180     
181     (ly:stencil-add
182      (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
183                          white)
184      stencil)
185     ))
186
187 (define-public (dimension-arrows destination) 
188   "Draw twosided arrow from here to @var{destination}"
189   
190   (let*
191       ((e_x 1+0i)
192        (e_y 0+1i)
193        (rotate (lambda (z ang)
194                  (* (make-polar 1 ang)
195                     z)))
196        (complex-to-offset (lambda (z)
197                             (list (real-part z) (imag-part z))))
198        
199        (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
200        (e_z (/ z-dest (magnitude z-dest)))
201        (triangle-points '(-1+0.25i
202                           0
203                           -1-0.25i))
204        (p1s (map (lambda (z)
205                    (+ z-dest (rotate z (angle z-dest))))
206                  triangle-points))
207        (p2s (map (lambda (z)
208                    (rotate z (angle (- z-dest))))
209                    triangle-points))
210        (null (cons 0 0)) 
211        (arrow-1  
212         (ly:make-stencil
213          `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
214                    0.0
215                    #t) null null))
216        (arrow-2
217         (ly:make-stencil
218          `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
219                    0.0
220                    #t) null null ) )
221        (thickness 0.1)
222        (shorten-line 0.5)
223        (start (complex-to-offset (/ (* e_z shorten-line) 2)))
224        (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
225        
226        (line (ly:make-stencil
227               `(draw-line ,thickness
228                           ,(car start) ,(cadr start)
229                           ,(car end) ,(cadr end)
230                           )
231               (cons (min 0 (car destination))
232                     (min 0 (cdr destination)))
233               (cons (max 0 (car destination))
234                     (max 0 (cdr destination)))))
235                     
236        (result (ly:stencil-add arrow-2 arrow-1 line)))
237
238
239     result))
240
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 ;; ANNOTATIONS
243 ;;
244 ;; annotations are arrows indicating the numerical value of
245 ;; spacing variables 
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
247
248 (define*-public (annotate-y-interval layout name extent is-length
249                                      #:key (color darkblue))
250   (let ((text-props (cons '((font-size . -3)
251                             (font-family . typewriter))
252                           (layout-extract-page-properties layout)))
253         (annotation #f))
254     (define (center-stencil-on-extent stil)
255       (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
256                             (cons 0 (interval-center extent))))
257     ;; do something sensible for 0,0 intervals. 
258     (set! extent (interval-widen extent 0.001))
259     (if (not (interval-sane? extent))
260         (set! annotation (interpret-markup
261                           layout text-props
262                           (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
263         (let ((text-stencil (interpret-markup
264                              layout text-props
265                              (markup #:whiteout #:simple name)))
266               (dim-stencil (interpret-markup
267                             layout text-props
268                             (markup #:whiteout
269                                     #:simple (cond
270                                               ((interval-empty? extent)
271                                                (format "empty"))
272                                               (is-length
273                                                (ly:format "~$" (interval-length extent)))
274                                               (else
275                                                (ly:format "(~$,~$)"
276                                                        (car extent) (cdr extent)))))))
277               (arrows (ly:stencil-translate-axis 
278                        (dimension-arrows (cons 0 (interval-length extent)))
279                        (interval-start extent) Y)))
280           (set! annotation
281                 (center-stencil-on-extent text-stencil))
282           (set! annotation
283                 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
284           (set! annotation
285                 (ly:stencil-combine-at-edge annotation X LEFT
286                                             (center-stencil-on-extent dim-stencil)
287                                             0.5))
288           (set! annotation
289                 (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
290                                  (ly:stencil-extent annotation X)
291                                  (cons 10000 -10000)))))
292     annotation))
293
294
295 (define-public (eps-file->stencil axis size file-name)
296   (let*
297       ((contents (ly:gulp-file file-name))
298        (bbox (get-postscript-bbox (car (string-split contents #\nul))))
299        (bbox-size (if (= axis X)
300                       (- (list-ref bbox 2) (list-ref bbox 0))
301                       (- (list-ref bbox 3) (list-ref bbox 1))
302                       ))
303        (factor (if (< 0 bbox-size)
304                    (exact->inexact (/ size bbox-size))
305                    0))
306        (scaled-bbox
307         (map (lambda (x) (* factor x)) bbox))
308        (clip-rect-string (ly:format
309                           "~a ~a ~a ~a rectclip"
310                           (list-ref bbox 0) 
311                           (list-ref bbox 1) 
312                           (- (list-ref bbox 2) (list-ref bbox 0))
313                           (- (list-ref bbox 3) (list-ref bbox 1)))))
314     
315
316     (if bbox
317         (ly:make-stencil
318          (list
319           'embedded-ps
320           (string-append
321            (ly:format
322            "
323 gsave
324 currentpoint translate
325 BeginEPSF
326 ~a dup scale
327 ~a 
328 %%BeginDocument: ~a
329 "          factor clip-rect-string
330
331            file-name
332            )
333            contents
334            "%%EndDocument
335 EndEPSF
336 grestore
337 "))
338         
339          (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
340          (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
341         
342         (ly:make-stencil "" '(0 . 0) '(0 . 0)))
343     ))
344
345 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
346 ;; output signatures.
347
348 (define-public (write-system-signatures basename paper-systems count)
349   (if (pair? paper-systems)
350       (begin
351         (let*
352             ((outname (simple-format #f "~a-~a.signature" basename count)) )
353              
354           (ly:message "Writing ~a" outname)
355           (write-system-signature outname (car paper-systems))
356           (write-system-signatures basename (cdr paper-systems) (1+ count))))))
357
358 (use-modules (scm paper-system))
359 (define-public (write-system-signature filename paper-system)
360   (define (float? x)
361     (and (number? x) (inexact? x)))
362
363   (define system-grob
364     (paper-system-system-grob paper-system))
365   
366   (define output (open-output-file filename))
367
368   ;; todo: optionally use a command line flag? Or just junk this?
369   (define compare-expressions #f)
370   (define (strip-floats expr)
371     "Replace floats by #f"
372     (cond
373      ((float? expr) #f)
374      ((ly:font-metric? expr) (ly:font-name expr))
375      ((pair? expr) (cons (strip-floats (car expr))
376                          (strip-floats (cdr expr))))
377      (else expr)))
378
379   (define (fold-false-pairs expr)
380     "Try to remove lists of #f as much as possible."
381     (if (pair? expr)
382         (let*
383             ((first (car expr))
384              (rest (fold-false-pairs (cdr expr))))
385
386           (if first
387               (cons (fold-false-pairs first) rest)
388               rest))
389         expr))
390   
391   (define (raw-string expr)
392     "escape quotes and slashes for python consumption"
393     (regexp-substitute/global #f "[@\n]" (simple-format #f "~a" expr) 'pre " " 'post))
394
395   (define (raw-pair expr)
396     (simple-format #f "~a ~a"
397             (car expr) (cdr expr)))
398   
399   (define (found-grob expr)
400     (let*
401         ((grob (car expr))
402          (rest (cdr expr))
403          (collected '())
404          (cause (event-cause grob))
405          (input (if (ly:stream-event? cause) (ly:event-property cause 'origin) #f))
406          (location (if (ly:input-location? input) (ly:input-file-line-char-column input) '()))
407
408          ;; todo: use stencil extent if available.
409          (x-ext (ly:grob-extent grob system-grob X))
410          (y-ext (ly:grob-extent grob system-grob Y))
411          (expression-skeleton
412           (if compare-expressions
413               (interpret-for-signature
414                #f (lambda (e)
415                     (set! collected (cons e collected)))
416                rest)
417              "")))
418
419       (simple-format output
420               "~a@~a@~a@~a@~a\n"
421               (cdr (assq 'name (ly:grob-property grob 'meta) ))
422               (raw-string location)
423               (raw-pair (if (interval-empty? x-ext) '(1 . -1) x-ext))
424               (raw-pair (if (interval-empty? y-ext) '(1 . -1) y-ext))
425               (raw-string collected))
426       ))
427
428   (define (interpret-for-signature escape collect expr)
429     (define (interpret expr)
430       (let*
431           ((head (if (pair? expr)
432                      (car expr)
433                      #f)))
434
435         (cond
436          ((eq? head 'grob-cause) (escape (cdr expr)))
437          ((eq? head 'color) (interpret (caddr expr)))
438          ((eq? head 'rotate-stencil) (interpret (caddr expr)))
439          ((eq? head 'translate-stencil) (interpret (caddr expr)))
440          ((eq? head 'combine-stencil)
441           (for-each (lambda (e) (interpret e))  (cdr expr)))
442          (else
443           (collect (fold-false-pairs (strip-floats expr))))
444          
445          )))
446
447     (interpret expr))
448
449   (if (ly:grob? system-grob)
450       (begin
451         (display (simple-format #f "# Output signature\n# Generated by LilyPond ~a\n" (lilypond-version))
452                  output)
453         (interpret-for-signature found-grob (lambda (x) #f)
454                                  (ly:stencil-expr
455                                   (paper-system-stencil paper-system)))))
456
457   ;; should be superfluous, but leaking "too many open files"?
458   (close-port output))
459