]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
Merge http://git.sv.gnu.org/r/lilypond
[lilypond.git] / scm / define-markup-commands.scm
1 ;;;; define-markup-commands.scm -- markup commands
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 2000--2006  Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
7
8
9 ;;; markup commands
10 ;;;  * each markup function should have a doc string with
11 ;;     syntax, description and example. 
12
13 (use-modules (ice-9 regex))
14
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;; utility functions
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
19 (define-public empty-stencil (ly:make-stencil '() '(1 . -1) '(1 . -1)))
20 (define-public point-stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
21
22
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;; geometric shapes
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26
27 (define-builtin-markup-command (draw-line layout props dest)
28   (number-pair?)
29   "A simple line.  Uses the @code{thickness} property."
30   (let*
31       ((th (chain-assoc-get 'thickness props  0.1))
32        (x (car dest))
33        (y (cdr dest))
34        (s (ly:make-stencil
35            `(draw-line
36              ,th
37              0 0
38              ,x ,y)
39
40            (cons (min x 0) (max x 0))
41            (cons (min y 0) (max y 0)))))
42
43     s))
44
45 (define-builtin-markup-command (draw-circle layout props radius thickness fill)
46   (number? number? boolean?)
47   "A circle of radius @var{radius}, thickness @var{thickness} and
48 optionally filled."
49   (make-circle-stencil radius thickness fill))
50
51 (define-builtin-markup-command (triangle layout props filled) (boolean?)
52   "A triangle, either filled or empty."
53   (let*
54       ((th (chain-assoc-get 'thickness props  0.1))
55        (size (chain-assoc-get 'font-size props 0))
56        (ex (* (magstep size)
57               0.8
58               (chain-assoc-get 'baseline-skip props 2))))
59
60     (ly:make-stencil
61      `(polygon '(0.0 0.0
62                      ,ex 0.0
63                      ,(* 0.5 ex)
64                      ,(* 0.86 ex))
65            ,th
66            ,filled)
67
68      (cons 0 ex)
69      (cons 0 (* .86 ex))
70      )))
71
72 (define-builtin-markup-command (circle layout props arg) (markup?)
73   "Draw a circle around @var{arg}.  Use @code{thickness},
74 @code{circle-padding} and @code{font-size} properties to determine line
75 thickness and padding around the markup."
76   
77   (let* ((th (chain-assoc-get 'thickness props  0.1))
78          (size (chain-assoc-get 'font-size props 0))
79          (pad
80           (* (magstep size)
81              (chain-assoc-get 'circle-padding props 0.2)))
82          (m (interpret-markup layout props arg)))
83     (circle-stencil m th pad)))
84
85 (define-builtin-markup-command (with-url layout props url arg) (string? markup?)
86   "Add a link to URL @var{url} around @var{arg}.  This only works in
87 the PDF backend."
88   (let* ((stil (interpret-markup layout props arg))
89          (xextent (ly:stencil-extent stil X))
90          (yextent (ly:stencil-extent stil Y))
91          (old-expr (ly:stencil-expr stil))
92          (url-expr (list 'url-link url `(quote ,xextent) `(quote ,yextent))))
93
94     (ly:stencil-add (ly:make-stencil url-expr xextent yextent) stil)))
95
96
97 (define-builtin-markup-command (beam layout props width slope thickness)
98   (number? number? number?)
99   "Create a beam with the specified parameters."
100   (let* ((y (* slope width))
101          (yext (cons (min 0 y) (max 0 y)))
102          (half (/ thickness 2)))
103
104     (ly:make-stencil
105      `(polygon ',(list 
106                   0 (/ thickness -2)
107                     width (+ (* width slope)  (/ thickness -2))
108                     width (+ (* width slope)  (/ thickness 2))
109                     0 (/ thickness 2))
110                ,(ly:output-def-lookup layout 'blot-diameter)
111                #t)
112      (cons 0 width)
113      (cons (+ (- half) (car yext))
114            (+ half (cdr yext))))))
115
116 (define-builtin-markup-command (box layout props arg) (markup?)
117   "Draw a box round @var{arg}.  Looks at @code{thickness},
118 @code{box-padding} and @code{font-size} properties to determine line
119 thickness and padding around the markup."
120   
121   (let* ((th (chain-assoc-get 'thickness props  0.1))
122          (size (chain-assoc-get 'font-size props 0))
123          (pad (* (magstep size)
124                  (chain-assoc-get 'box-padding props 0.2)))
125          (m (interpret-markup layout props arg)))
126     (box-stencil m th pad)))
127
128 (define-builtin-markup-command (filled-box layout props xext yext blot)
129   (number-pair? number-pair? number?)
130   "Draw a box with rounded corners of dimensions @var{xext} and
131 @var{yext}.  For example,
132 @verbatim
133 \\filled-box #'(-.3 . 1.8) #'(-.3 . 1.8) #0
134 @end verbatim
135 creates a box extending horizontally from -0.3 to 1.8 and
136 vertically from -0.3 up to 1.8, with corners formed from a
137 circle of diameter@tie{}0 (i.e. sharp corners)."
138   (ly:round-filled-box
139    xext yext blot))
140
141 (define-builtin-markup-command (rotate layout props ang arg) (number? markup?)
142   "Rotate object with @var{ang} degrees around its center."
143   (let* ((stil (interpret-markup layout props arg)))
144     (ly:stencil-rotate stil ang 0 0)))
145
146
147 (define-builtin-markup-command (whiteout layout props arg) (markup?)
148   "Provide a white underground for @var{arg}."
149   (stencil-whiteout (interpret-markup layout props arg)))
150
151 (define-builtin-markup-command (pad-markup layout props padding arg) (number? markup?)
152   "Add space around a markup object."
153
154   (let*
155       ((stil (interpret-markup layout props arg))
156        (xext (ly:stencil-extent stil X))
157        (yext (ly:stencil-extent stil Y)))
158
159     (ly:make-stencil
160      (ly:stencil-expr stil)
161      (interval-widen xext padding)
162      (interval-widen yext padding))))
163
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 ;; space
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167
168 ;;FIXME: is this working? 
169 (define-builtin-markup-command (strut layout props) ()
170   "Create a box of the same height as the space in the current font."
171   (let ((m (ly:text-interface::interpret-markup layout props " ")))
172     (ly:make-stencil (ly:stencil-expr m)
173                      '(1000 . -1000)
174                      (ly:stencil-extent m X)
175                      )))
176
177
178 ;; todo: fix negative space
179 (define-builtin-markup-command (hspace layout props amount) (number?)
180   "This produces a invisible object taking horizontal space.  For example,
181
182 @example 
183 \\markup @{ A \\hspace #2.0 B @} 
184 @end example
185
186 @noindent
187 puts extra space between A and@tie{}B, on top of the space that is
188 normally inserted before elements on a line."
189   (if (> amount 0)
190       (ly:make-stencil "" (cons 0 amount) '(-1 . 1))
191       (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
192
193
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195 ;; importing graphics.
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197
198 (define-builtin-markup-command (stencil layout props stil) (ly:stencil?)
199   "Use a stencil as markup."
200   stil)
201
202 (define bbox-regexp
203   (make-regexp "%%BoundingBox:[ \t]+([0-9-]+)[ \t]+([0-9-]+)[ \t]+([0-9-]+)[ \t]+([0-9-]+)"))
204
205 (define (get-postscript-bbox string)
206   "Extract the bbox from STRING, or return #f if not present."
207   (let*
208       ((match (regexp-exec bbox-regexp string)))
209     
210     (if match
211         (map (lambda (x)
212                (string->number (match:substring match x)))
213              (cdr (iota 5)))
214              
215         #f)))
216
217 (define-builtin-markup-command (epsfile layout props axis size file-name) (number? number? string?)
218   "Inline an EPS image.  The image is scaled along @var{axis} to
219 @var{size}."
220
221   (if (ly:get-option 'safe)
222       (interpret-markup layout props "not allowed in safe")
223       (eps-file->stencil axis size file-name)
224       ))
225
226 (define-builtin-markup-command (postscript layout props str) (string?)
227   "This inserts @var{str} directly into the output as a PostScript
228 command string.  Due to technicalities of the output backends,
229 different scales should be used for the @TeX{} and PostScript backend,
230 selected with @code{-f}. 
231
232 For the @TeX{} backend, the following string prints a rotated text
233
234 @cindex rotated text
235
236 @example
237 0 0 moveto /ecrm10 findfont 
238 1.75 scalefont setfont 90 rotate (hello) show
239 @end example
240
241 @noindent
242 The magical constant 1.75 scales from LilyPond units (staff spaces) to
243 @TeX{} dimensions.
244
245 For the postscript backend, use the following
246
247 @example
248 gsave /ecrm10 findfont 
249  10.0 output-scale div 
250  scalefont setfont 90 rotate (hello) show grestore 
251 @end example"
252
253   ;; FIXME
254   (ly:make-stencil
255    (list 'embedded-ps
256          (format "
257 gsave currentpoint translate
258 0.1 setlinewidth
259  ~a
260 grestore
261 "
262                  str))
263    '(0 . 0) '(0 . 0)))
264
265
266 (define-builtin-markup-command (score layout props score) (ly:score?)
267   "Inline an image of music."
268   (let* ((output (ly:score-embedded-format score layout)))
269
270     (if (ly:music-output? output)
271         (paper-system-stencil
272          (vector-ref (ly:paper-score-paper-systems output) 0))
273         (begin
274           (ly:warning (_"no systems found in \\score markup, does it have a \\layout block?"))
275           empty-stencil))))
276
277 (define-builtin-markup-command (null layout props) ()
278   "An empty markup with extents of a single point."
279
280   point-stencil)
281
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 ;; basic formatting.
284 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285
286
287
288 (define-builtin-markup-command (simple layout props str) (string?)
289   "A simple text string; @code{\\markup @{ foo @}} is equivalent with
290 @code{\\markup @{ \\simple #\"foo\" @}}."
291   (interpret-markup layout props str))
292
293 (define-builtin-markup-command (tied-lyric layout props str) (string?)
294   "Like simple-markup, but use tie characters for @q{~} tilde symbols."
295   (if (string-contains str "~")
296       (let*
297           ((parts (string-split str #\~))
298            (tie-str (ly:wide-char->utf-8 #x203f))
299            (joined  (list-join parts tie-str))
300            (join-stencil (interpret-markup layout props tie-str))
301            )
302
303         (interpret-markup layout 
304                           (prepend-alist-chain
305                            'word-space
306                            (/ (interval-length (ly:stencil-extent join-stencil X)) -3.5)
307                            props)
308                           (make-line-markup joined)))
309                            ;(map (lambda (s) (interpret-markup layout props s)) parts))
310       (interpret-markup layout props str)))
311
312 (define-public empty-markup
313   (make-simple-markup ""))
314
315 ;; helper for justifying lines.
316 (define (get-fill-space word-count line-width text-widths)
317   "Calculate the necessary paddings between each two adjacent texts.
318         The lengths of all texts are stored in @var{text-widths}.
319         The normal formula for the padding between texts a and b is:
320         padding = line-width/(word-count - 1) - (length(a) + length(b))/2
321         The first and last padding have to be calculated specially using the
322         whole length of the first or last text.
323         Return a list of paddings.
324 "
325   (cond
326    ((null? text-widths) '())
327    
328    ;; special case first padding
329    ((= (length text-widths) word-count)
330     (cons 
331      (- (- (/ line-width (1- word-count)) (car text-widths))
332         (/ (car (cdr text-widths)) 2))
333      (get-fill-space word-count line-width (cdr text-widths))))
334    ;; special case last padding
335    ((= (length text-widths) 2)
336     (list (- (/ line-width (1- word-count))
337              (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
338    (else
339     (cons 
340      (- (/ line-width (1- word-count))
341         (/ (+ (car text-widths) (car (cdr text-widths))) 2))
342      (get-fill-space word-count line-width (cdr text-widths))))))
343
344 (define-builtin-markup-command (fill-line layout props markups)
345   (markup-list?)
346   "Put @var{markups} in a horizontal line of width @var{line-width}.
347 The markups are spaced or flushed to fill the entire line.
348 If there are no arguments, return an empty stencil."
349  
350   (let* ((orig-stencils (interpret-markup-list layout props markups))
351          (stencils
352           (map (lambda (stc)
353                  (if (ly:stencil-empty? stc)
354                      point-stencil
355                      stc)) orig-stencils))
356          (text-widths
357           (map (lambda (stc)
358                  (if (ly:stencil-empty? stc)
359                      0.0
360                      (interval-length (ly:stencil-extent stc X))))
361                stencils))
362          (text-width (apply + text-widths))
363          (text-dir (chain-assoc-get 'text-direction props RIGHT))
364          (word-count (length stencils))
365          (word-space (chain-assoc-get 'word-space props 1))
366          (prop-line-width (chain-assoc-get 'line-width props #f))
367          (line-width (if prop-line-width prop-line-width
368                          (ly:output-def-lookup layout 'line-width)))
369          (fill-space
370                 (cond
371                         ((= word-count 1) 
372                                 (list
373                                         (/ (- line-width text-width) 2)
374                                         (/ (- line-width text-width) 2)))
375                         ((= word-count 2)
376                                 (list
377                                         (- line-width text-width)))
378                         (else 
379                                 (get-fill-space word-count line-width text-widths))))
380          (fill-space-normal
381           (map (lambda (x)
382                  (if (< x word-space)
383                      word-space
384                      x))
385                fill-space))
386                                         
387          (line-stencils (if (= word-count 1)
388                             (list
389                              point-stencil
390                              (car stencils)
391                              point-stencil)
392                             stencils)))
393
394     (if (= text-dir LEFT)
395         (set! line-stencils (reverse line-stencils)))
396
397     (if (null? (remove ly:stencil-empty? orig-stencils))
398         empty-stencil
399         (stack-stencils-padding-list X
400                                      RIGHT fill-space-normal line-stencils))))
401         
402 (define-builtin-markup-command (line layout props args) (markup-list?)
403   "Put @var{args} in a horizontal line.  The property @code{word-space}
404 determines the space between each markup in @var{args}."
405   (let*
406       ((stencils (interpret-markup-list layout props args))
407        (space    (chain-assoc-get 'word-space props))
408        (text-dir (chain-assoc-get 'text-direction props RIGHT)) 
409        )
410
411     (if (= text-dir LEFT)
412         (set! stencils (reverse stencils)))
413     
414
415     (stack-stencil-line
416      space
417      (remove ly:stencil-empty? stencils))))
418
419 (define-builtin-markup-command (concat layout props args) (markup-list?)
420   "Concatenate @var{args} in a horizontal line, without spaces inbetween.
421 Strings and simple markups are concatenated on the input level, allowing
422 ligatures.  For example, @code{\\concat @{ \"f\" \\simple #\"i\" @}} is
423 equivalent to @code{\"fi\"}."
424
425   (define (concat-string-args arg-list)
426     (fold-right (lambda (arg result-list)
427                   (let ((result (if (pair? result-list)
428                                     (car result-list)
429                                   '())))
430                     (if (and (pair? arg) (eqv? (car arg) simple-markup))
431                       (set! arg (cadr arg)))
432                     (if (and (string? result) (string? arg))
433                         (cons (string-append arg result) (cdr result-list))
434                       (cons arg result-list))))
435                 '()
436                 arg-list))
437
438   (interpret-markup layout
439                     (prepend-alist-chain 'word-space 0 props)
440                     (make-line-markup (if (markup-command-list? args)
441                                           args
442                                           (concat-string-args args)))))
443
444 (define (wordwrap-stencils stencils
445                            justify base-space line-width text-dir)
446   
447   "Perform simple wordwrap, return stencil of each line."
448   
449   (define space (if justify
450                     
451                     ;; justify only stretches lines.
452                     (* 0.7 base-space)
453                     base-space))
454        
455   (define (take-list width space stencils
456                      accumulator accumulated-width)
457     "Return (head-list . tail) pair, with head-list fitting into width"
458     (if (null? stencils)
459         (cons accumulator stencils)
460         (let*
461             ((first (car stencils))
462              (first-wid (cdr (ly:stencil-extent (car stencils) X)))
463              (newwid (+ space first-wid accumulated-width))
464              )
465
466           (if
467            (or (null? accumulator)
468                (< newwid width))
469
470            (take-list width space
471                       (cdr stencils)
472                       (cons first accumulator)
473                       newwid)
474              (cons accumulator stencils))
475            )))
476
477     (let loop
478         ((lines '())
479          (todo stencils))
480
481       (let*
482           ((line-break (take-list line-width space todo
483                                  '() 0.0))
484            (line-stencils (car line-break))
485            (space-left (- line-width (apply + (map (lambda (x) (cdr (ly:stencil-extent x X)))
486                                               line-stencils))))
487
488            (line-word-space (cond
489                              ((not justify) space)
490
491                              ;; don't stretch last line of paragraph.
492                              ;; hmmm . bug - will overstretch the last line in some case. 
493                              ((null? (cdr line-break))
494                               base-space)
495                              ((null? line-stencils) 0.0)
496                              ((null? (cdr line-stencils)) 0.0)
497                              (else (/ space-left (1- (length line-stencils))))))
498
499            (line (stack-stencil-line
500                   line-word-space
501                   (if (= text-dir RIGHT)
502                       (reverse line-stencils)
503                       line-stencils))))
504
505         (if (pair? (cdr line-break))
506             (loop (cons line lines)
507                   (cdr line-break))
508
509             (begin
510               (if (= text-dir LEFT)
511                   (set! line
512                         (ly:stencil-translate-axis line
513                                                    (- line-width (interval-end (ly:stencil-extent line X)))
514                                                    X)))
515               (reverse (cons line lines))
516               
517             )))
518
519       ))
520
521
522 (define (wordwrap-markups layout props args justify)
523   (let*
524       ((prop-line-width (chain-assoc-get 'line-width props #f))
525        (line-width (if prop-line-width prop-line-width
526                        (ly:output-def-lookup layout 'line-width)))
527        (word-space (chain-assoc-get 'word-space props))
528        (text-dir (chain-assoc-get 'text-direction props RIGHT)))
529     (wordwrap-stencils (remove ly:stencil-empty?
530                                (interpret-markup-list layout props args))
531                        justify word-space line-width
532                        text-dir)))
533
534 (define-builtin-markup-command (justify layout props args) (markup-list?)
535   "Like wordwrap, but with lines stretched to justify the margins.
536 Use @code{\\override #'(line-width . @var{X})} to set the line width;
537 @var{X}@tie{}is the number of staff spaces."
538   (stack-lines DOWN 0.0 (chain-assoc-get 'baseline-skip props)
539                (wordwrap-markups layout props args #t)))
540
541 (define-builtin-markup-command (wordwrap layout props args) (markup-list?)
542   "Simple wordwrap.  Use @code{\\override #'(line-width . @var{X})} to set
543 the line width, where @var{X} is the number of staff spaces."
544   (stack-lines DOWN 0.0 (chain-assoc-get 'baseline-skip props)
545                (wordwrap-markups layout props args #f)))
546
547 (define (wordwrap-string layout props justify arg) 
548   (let*
549       ((baseline-skip (chain-assoc-get 'baseline-skip props))
550        (line-width (chain-assoc-get 'line-width props))
551        (word-space (chain-assoc-get 'word-space props))
552        
553        (para-strings (regexp-split
554                       (string-regexp-substitute "\r" "\n"
555                                                 (string-regexp-substitute "\r\n" "\n" arg))
556                       "\n[ \t\n]*\n[ \t\n]*"))
557        
558        (text-dir (chain-assoc-get 'text-direction props RIGHT)) 
559        (list-para-words (map (lambda (str)
560                                (regexp-split str "[ \t\n]+"))
561                              para-strings))
562        (para-lines (map (lambda (words)
563                           (let*
564                               ((stencils
565                                 (remove
566                                  ly:stencil-empty? (map 
567                                       (lambda (x)
568                                         (interpret-markup layout props x))
569                                       words)))
570                                (lines (wordwrap-stencils stencils
571                                                          justify word-space
572                                                          line-width text-dir
573                                                          )))
574
575                             lines))
576                         
577                         list-para-words)))
578
579     (stack-lines DOWN 0.0 baseline-skip (apply append para-lines))))
580
581
582 (define-builtin-markup-command (wordwrap-string layout props arg) (string?)
583   "Wordwrap a string.  Paragraphs may be separated with double newlines."
584   (wordwrap-string layout props  #f arg))
585   
586 (define-builtin-markup-command (justify-string layout props arg) (string?)
587   "Justify a string.  Paragraphs may be separated with double newlines"
588   (wordwrap-string layout props #t arg))
589
590
591 (define-builtin-markup-command (wordwrap-field layout props symbol) (symbol?)
592   "Wordwrap the data which has been assigned to @var{symbol}."
593   (let* ((m (chain-assoc-get symbol props)))
594     (if (string? m)
595      (interpret-markup layout props
596       (list wordwrap-string-markup m))
597      (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
598
599 (define-builtin-markup-command (justify-field layout props symbol) (symbol?)
600   "Justify the data which has been assigned to @var{symbol}."
601   (let* ((m (chain-assoc-get symbol props)))
602     (if (string? m)
603      (interpret-markup layout props
604       (list justify-string-markup m))
605      (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
606
607
608
609 (define-builtin-markup-command (combine layout props m1 m2) (markup? markup?)
610   "Print two markups on top of each other."
611   (let* ((s1 (interpret-markup layout props m1))
612          (s2 (interpret-markup layout props m2)))
613     (ly:stencil-add s1 s2)))
614
615 ;;
616 ;; TODO: should extract baseline-skip from each argument somehow..
617 ;; 
618 (define-builtin-markup-command (column layout props args) (markup-list?)
619   "Stack the markups in @var{args} vertically.  The property
620 @code{baseline-skip} determines the space between each markup in @var{args}."
621
622   (let*
623       ((arg-stencils (interpret-markup-list layout props args))
624        (skip (chain-assoc-get 'baseline-skip props)))
625
626     
627     (stack-lines
628      -1 0.0 skip
629      (remove ly:stencil-empty? arg-stencils))))
630
631
632 (define-builtin-markup-command (dir-column layout props args) (markup-list?)
633   "Make a column of args, going up or down, depending on the setting
634 of the @code{#'direction} layout property."
635   (let* ((dir (chain-assoc-get 'direction props)))
636     (stack-lines
637      (if (number? dir) dir -1)
638      0.0
639      (chain-assoc-get 'baseline-skip props)
640      (interpret-markup-list layout props args))))
641
642 (define-builtin-markup-command (center-align layout props args) (markup-list?)
643   "Put @code{args} in a centered column."
644   (let* ((mols (interpret-markup-list layout props args))
645          (cmols (map (lambda (x) (ly:stencil-aligned-to x X CENTER)) mols)))
646     
647     (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) cmols)))
648
649 (define-builtin-markup-command (vcenter layout props arg) (markup?)
650   "Align @code{arg} to its Y@tie{}center."
651   (let* ((mol (interpret-markup layout props arg)))
652     (ly:stencil-aligned-to mol Y CENTER)))
653
654 (define-builtin-markup-command (hcenter layout props arg) (markup?)
655   "Align @code{arg} to its X@tie{}center."
656   (let* ((mol (interpret-markup layout props arg)))
657     (ly:stencil-aligned-to mol X CENTER)))
658
659 (define-builtin-markup-command (right-align layout props arg) (markup?)
660   "Align @var{arg} on its right edge."
661   (let* ((m (interpret-markup layout props arg)))
662     (ly:stencil-aligned-to m X RIGHT)))
663
664 (define-builtin-markup-command (left-align layout props arg) (markup?)
665   "Align @var{arg} on its left edge."
666   (let* ((m (interpret-markup layout props arg)))
667     (ly:stencil-aligned-to m X LEFT)))
668
669 (define-builtin-markup-command (general-align layout props axis dir arg)  (integer? number? markup?)
670   "Align @var{arg} in @var{axis} direction to the @var{dir} side."
671   (let* ((m (interpret-markup layout props arg)))
672     (ly:stencil-aligned-to m axis dir)))
673
674 (define-builtin-markup-command (halign layout props dir arg) (number? markup?)
675   "Set horizontal alignment.  If @var{dir} is @code{-1}, then it is
676 left-aligned, while @code{+1} is right.  Values inbetween interpolate
677 alignment accordingly."
678   (let* ((m (interpret-markup layout props arg)))
679     (ly:stencil-aligned-to m X dir)))
680
681
682
683 (define-builtin-markup-command (with-dimensions layout props x y arg) (number-pair? number-pair? markup?)
684   "Set the dimensions of @var{arg} to @var{x} and@tie{}@var{y}."
685   
686   (let* ((m (interpret-markup layout props arg)))
687     (ly:make-stencil (ly:stencil-expr m) x y)))
688
689
690 (define-builtin-markup-command (pad-around layout props amount arg) (number? markup?)
691   "Add padding @var{amount} all around @var{arg}."
692   
693   (let*
694       ((m (interpret-markup layout props arg))
695        (x (ly:stencil-extent m X))
696        (y (ly:stencil-extent m Y)))
697     
698        
699     (ly:make-stencil (ly:stencil-expr m)
700                      (interval-widen x amount)
701                      (interval-widen y amount))
702    ))
703
704
705 (define-builtin-markup-command (pad-x layout props amount arg) (number? markup?)
706   "Add padding @var{amount} around @var{arg} in the X@tie{}direction."
707   (let*
708       ((m (interpret-markup layout props arg))
709        (x (ly:stencil-extent m X))
710        (y (ly:stencil-extent m Y)))
711     
712        
713     (ly:make-stencil (ly:stencil-expr m)
714                      (interval-widen x amount)
715                      y)
716    ))
717
718
719 (define-builtin-markup-command (put-adjacent layout props arg1 axis dir arg2) (markup? integer? ly:dir?  markup?)
720   "Put @var{arg2} next to @var{arg1}, without moving @var{arg1}."
721   (let* ((m1 (interpret-markup layout props arg1))
722          (m2 (interpret-markup layout props arg2)))
723
724     (ly:stencil-combine-at-edge m1 axis dir m2 0.0)
725   ))
726
727 (define-builtin-markup-command (transparent layout props arg) (markup?)
728   "Make the argument transparent."
729   (let*
730       ((m (interpret-markup layout props arg))
731        (x (ly:stencil-extent m X))
732        (y (ly:stencil-extent m Y)))
733     
734
735     
736     (ly:make-stencil ""
737                      x y)))
738
739
740 (define-builtin-markup-command (pad-to-box layout props x-ext y-ext arg)
741   (number-pair? number-pair? markup?)
742   "Make @var{arg} take at least @var{x-ext}, @var{y-ext} space."
743
744   (let*
745       ((m (interpret-markup layout props arg))
746        (x (ly:stencil-extent m X))
747        (y (ly:stencil-extent m Y)))
748
749     (ly:make-stencil (ly:stencil-expr m)
750                      (interval-union x-ext x)
751                      (interval-union y-ext y))))
752
753
754 (define-builtin-markup-command (hcenter-in layout props length arg)
755   (number? markup?)
756   "Center @var{arg} horizontally within a box of extending
757 @var{length}/2 to the left and right."
758
759   (interpret-markup layout props
760                     (make-pad-to-box-markup
761                      (cons (/ length -2) (/ length 2))
762                      '(0 . 0)
763                      (make-hcenter-markup arg))))
764
765
766 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
767 ;; property
768 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
769
770 (define-builtin-markup-command (fromproperty layout props symbol) (symbol?)
771   "Read the @var{symbol} from property settings, and produce a stencil
772 from the markup contained within.  If @var{symbol} is not defined, it
773 returns an empty markup."
774   (let* ((m (chain-assoc-get symbol props)))
775     (if (markup? m)
776         (interpret-markup layout props m)
777         (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
778
779
780 (define-builtin-markup-command (on-the-fly layout props procedure arg) (symbol? markup?)
781   "Apply the @var{procedure} markup command to @var{arg}.
782 @var{procedure} should take a single argument."
783   (let* ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
784     (set-object-property! anonymous-with-signature
785                           'markup-signature
786                           (list markup?))
787     (interpret-markup layout props (list anonymous-with-signature arg))))
788
789
790
791 (define-builtin-markup-command (override layout props new-prop arg) (pair? markup?)
792   "Add the first argument in to the property list.  Properties may be
793 any sort of property supported by @internalsref{font-interface} and
794 @internalsref{text-interface}, for example
795
796 @example
797 \\override #'(font-family . married) \"bla\"
798 @end example"
799   (interpret-markup layout (cons (list new-prop) props) arg))
800
801 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
802 ;; files
803 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
804
805 (define-builtin-markup-command (verbatim-file layout props name) (string?)
806   "Read the contents of a file, and include it verbatim."
807
808   (interpret-markup
809    layout props
810    (if  (ly:get-option 'safe)
811         "verbatim-file disabled in safe mode"
812         (let*
813             ((str (ly:gulp-file name))
814              (lines (string-split str #\nl)))
815
816           (make-typewriter-markup
817            (make-column-markup lines)))
818         )))
819
820 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
821 ;; fonts.
822 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
823
824
825 (define-builtin-markup-command (bigger layout props arg) (markup?)
826   "Increase the font size relative to current setting."
827   (interpret-markup layout props
828    `(,fontsize-markup 1 ,arg)))
829
830 (define-builtin-markup-command (smaller layout props arg) (markup?)
831   "Decrease the font size relative to current setting."
832   (interpret-markup layout props
833    `(,fontsize-markup -1 ,arg)))
834
835 (define-builtin-markup-command larger (markup?) bigger-markup)
836
837 (define-builtin-markup-command (finger layout props arg) (markup?)
838   "Set the argument as small numbers."
839   (interpret-markup layout
840                     (cons '((font-size . -5) (font-encoding . fetaNumber)) props)
841                     arg))
842
843
844 (define-builtin-markup-command (fontsize layout props increment arg) (number? markup?)
845   "Add @var{increment} to the font-size.  Adjust baseline skip accordingly."
846
847   (let* ((fs (chain-assoc-get 'font-size props 0))
848          (bs (chain-assoc-get 'baseline-skip props 2)) 
849          (entries (list
850                    (cons 'baseline-skip (* bs (magstep increment)))
851                    (cons 'font-size (+ fs increment )))))
852
853     (interpret-markup layout (cons entries props) arg)))
854
855 (define-builtin-markup-command (magnify layout props sz arg) (number? markup?)
856   "Set the font magnification for its argument.  In the following
857 example, the middle@tie{}A is 10% larger:
858
859 @example
860 A \\magnify #1.1 @{ A @} A
861 @end example
862
863 Note: Magnification only works if a font name is explicitly selected.
864 Use @code{\\fontsize} otherwise."
865   (interpret-markup
866    layout 
867    (prepend-alist-chain 'font-size (magnification->font-size sz) props)
868    arg))
869
870 (define-builtin-markup-command (bold layout props arg) (markup?)
871   "Switch to bold font-series."
872   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
873
874 (define-builtin-markup-command (sans layout props arg) (markup?)
875   "Switch to the sans serif family."
876   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
877
878 (define-builtin-markup-command (number layout props arg) (markup?)
879   "Set font family to @code{number}, which yields the font used for
880 time signatures and fingerings.  This font only contains numbers and
881 some punctuation.  It doesn't have any letters."
882   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
883
884 (define-builtin-markup-command (roman layout props arg) (markup?)
885   "Set font family to @code{roman}."
886   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
887
888 (define-builtin-markup-command (huge layout props arg) (markup?)
889   "Set font size to +2."
890   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
891
892 (define-builtin-markup-command (large layout props arg) (markup?)
893   "Set font size to +1."
894   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
895
896 (define-builtin-markup-command (normalsize layout props arg) (markup?)
897   "Set font size to default."
898   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
899
900 (define-builtin-markup-command (small layout props arg) (markup?)
901   "Set font size to -1."
902   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
903
904 (define-builtin-markup-command (tiny layout props arg) (markup?)
905   "Set font size to -2."
906   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
907
908 (define-builtin-markup-command (teeny layout props arg) (markup?)
909   "Set font size to -3."
910   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
911
912 (define-builtin-markup-command (fontCaps layout props arg) (markup?)
913   "Set @code{font-shape} to @code{caps}."
914   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
915
916 ;; Poor man's caps
917 (define-builtin-markup-command (smallCaps layout props text) (markup?)
918   "Turn @code{text}, which should be a string, to small caps.
919 @example
920 \\markup \\smallCaps \"Text between double quotes\"
921 @end example"
922   (define (make-small-caps-markup chars)
923     (cond ((null? chars)
924            (markup))
925           ((char-whitespace? (car chars))
926            (markup #:fontsize -2 #:simple (string-upcase (list->string (cdr chars)))))
927           (else
928            (markup #:hspace -1
929                    #:fontsize -2 #:simple (string-upcase (list->string chars))))))
930   (define (make-not-small-caps-markup chars)
931     (cond ((null? chars)
932            (markup))
933           ((char-whitespace? (car chars))
934            (markup #:simple (list->string (cdr chars))))
935           (else
936            (markup #:hspace -1
937                    #:simple (list->string chars)))))
938   (define (small-caps-aux done-markups current-chars rest-chars small? after-space?)
939     (cond ((null? rest-chars)
940            ;; the end of the string: build the markup
941            (make-line-markup (reverse! (cons ((if small?
942                                                   make-small-caps-markup
943                                                   make-not-small-caps-markup)
944                                               (reverse! current-chars))
945                                              done-markups))))
946           ((char-whitespace? (car rest-chars))
947            ;; a space char.
948            (small-caps-aux done-markups current-chars (cdr rest-chars) small? #t))
949           ((or (and small? (char-lower-case? (car rest-chars)))
950                (and (not small?) (not (char-lower-case? (car rest-chars)))))
951            ;; same case
952            ;; add the char to the current char list
953            (small-caps-aux done-markups
954                            (cons (car rest-chars)
955                                  (if after-space? 
956                                      (cons #\space current-chars)
957                                      current-chars))
958                            (cdr rest-chars) 
959                            small?
960                            #f))
961           (else
962            ;; case change
963            ;; make a markup with current chars, and start a new list with new char
964            (small-caps-aux (cons ((if small?
965                                       make-small-caps-markup
966                                       make-not-small-caps-markup)
967                                   (reverse! current-chars))
968                                  done-markups)
969                            (if after-space?
970                                (list (car rest-chars) #\space)
971                                (list (car rest-chars)))
972                            (cdr rest-chars)
973                            (not small?)
974                            #f))))
975   (interpret-markup layout props (small-caps-aux (list) 
976                                                  (list) 
977                                                  (cons #\space (string->list text))
978                                                  #f
979                                                  #f)))
980
981 (define-builtin-markup-command (caps layout props arg) (markup?)
982   "Emit @var{arg} as small caps."
983   (interpret-markup layout props (make-smallCaps-markup arg)))
984
985 (define-builtin-markup-command (dynamic layout props arg) (markup?)
986   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
987 @b{z}, @b{p}, and @b{r}.  When producing phrases, like
988 @q{pi@`{u}@tie{}@b{f}}, the normal words (like @q{pi@`{u}}) should be
989 done in a different font.  The recommended font for this is bold and italic."
990   (interpret-markup
991    layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
992
993 (define-builtin-markup-command (text layout props arg) (markup?)
994   "Use a text font instead of music symbol or music alphabet font."  
995
996   ;; ugh - latin1
997   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
998                     arg))
999
1000
1001 (define-builtin-markup-command (italic layout props arg) (markup?)
1002   "Use italic @code{font-shape} for @var{arg}."
1003   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
1004
1005 (define-builtin-markup-command (typewriter layout props arg) (markup?)
1006   "Use @code{font-family} typewriter for @var{arg}."
1007   (interpret-markup
1008    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
1009
1010 (define-builtin-markup-command (upright layout props arg) (markup?)
1011   "Set font shape to @code{upright}.  This is the opposite of @code{italic}."
1012   (interpret-markup
1013    layout (prepend-alist-chain 'font-shape 'upright props) arg))
1014
1015 (define-builtin-markup-command (medium layout props arg) (markup?)
1016   "Switch to medium font series (in contrast to bold)."
1017   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
1018                     arg))
1019
1020 (define-builtin-markup-command (normal-text layout props arg) (markup?)
1021   "Set all font related properties (except the size) to get the default
1022 normal text font, no matter what font was used earlier."
1023   ;; ugh - latin1
1024   (interpret-markup layout
1025                     (cons '((font-family . roman) (font-shape . upright)
1026                             (font-series . medium) (font-encoding . latin1))
1027                           props)
1028                     arg))
1029
1030 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1031 ;; symbols.
1032 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1033
1034 (define-builtin-markup-command (doublesharp layout props) ()
1035   "Draw a double sharp symbol."
1036
1037   (interpret-markup layout props (markup #:musicglyph (assoc-get 1 standard-alteration-glyph-name-alist ""))))
1038
1039 (define-builtin-markup-command (sesquisharp layout props) ()
1040   "Draw a 3/2 sharp symbol."
1041   (interpret-markup layout props (markup #:musicglyph (assoc-get 3/4 standard-alteration-glyph-name-alist ""))))
1042                                          
1043
1044 (define-builtin-markup-command (sharp layout props) ()
1045   "Draw a sharp symbol."
1046   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/2 standard-alteration-glyph-name-alist ""))))
1047
1048 (define-builtin-markup-command (semisharp layout props) ()
1049   "Draw a semi sharp symbol."
1050   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/4 standard-alteration-glyph-name-alist ""))))
1051
1052 (define-builtin-markup-command (natural layout props) ()
1053   "Draw a natural symbol."
1054   (interpret-markup layout props (markup #:musicglyph (assoc-get 0 standard-alteration-glyph-name-alist ""))))
1055
1056 (define-builtin-markup-command (semiflat layout props) ()
1057   "Draw a semiflat."
1058   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/4 standard-alteration-glyph-name-alist ""))))
1059
1060 (define-builtin-markup-command (flat layout props) ()
1061   "Draw a flat symbol."
1062   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/2 standard-alteration-glyph-name-alist ""))))
1063
1064 (define-builtin-markup-command (sesquiflat layout props) ()
1065   "Draw a 3/2 flat symbol."
1066   (interpret-markup layout props (markup #:musicglyph (assoc-get -3/4 standard-alteration-glyph-name-alist ""))))
1067
1068 (define-builtin-markup-command (doubleflat layout props) ()
1069   "Draw a double flat symbol."
1070   (interpret-markup layout props (markup #:musicglyph (assoc-get -1 standard-alteration-glyph-name-alist ""))))
1071
1072 (define-builtin-markup-command (with-color layout props color arg) (color? markup?)
1073   "Draw @var{arg} in color specified by @var{color}."
1074
1075   (let* ((stil (interpret-markup layout props arg)))
1076
1077     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
1078                      (ly:stencil-extent stil X)
1079                      (ly:stencil-extent stil Y))))
1080
1081 \f
1082 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1083 ;; glyphs
1084 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1085
1086
1087 (define-builtin-markup-command (arrow-head layout props axis direction filled)
1088   (integer? ly:dir? boolean?)
1089   "Produce an arrow head in specified direction and axis.
1090 Use the filled head if @var{filled} is specified."
1091   (let*
1092       ((name (format "arrowheads.~a.~a~a"
1093                      (if filled
1094                          "close"
1095                          "open")
1096                      axis
1097                      direction)))
1098     (ly:font-get-glyph
1099      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1100                                      props))
1101      name)))
1102
1103 (define-builtin-markup-command (musicglyph layout props glyph-name) (string?)
1104   "@var{glyph0name} is converted to a musical symbol; for example,
1105 @code{\\musicglyph #\"accidentals.natural\"} selects the natural sign from
1106 the music font.  See @usermanref{The Feta font} for a complete listing of
1107 the possible glyphs."
1108   (ly:font-get-glyph
1109    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1110                                    props))
1111    glyph-name))
1112
1113 (define-builtin-markup-command (lookup layout props glyph-name) (string?)
1114   "Lookup a glyph by name."
1115   (ly:font-get-glyph (ly:paper-get-font layout props)
1116                      glyph-name))
1117
1118 (define-builtin-markup-command (char layout props num) (integer?)
1119   "Produce a single character.  For example, @code{\\char #65} produces the 
1120 letter @q{A}."
1121
1122   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
1123
1124 (define number->mark-letter-vector (make-vector 25 #\A))
1125
1126 (do ((i 0 (1+ i))
1127      (j 0 (1+ j)))
1128     ((>= i 26))
1129   (if (= i (- (char->integer #\I) (char->integer #\A)))
1130       (set! i (1+ i)))
1131   (vector-set! number->mark-letter-vector j
1132                (integer->char (+ i (char->integer #\A)))))
1133
1134 (define number->mark-alphabet-vector (list->vector
1135   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
1136
1137 (define (number->markletter-string vec n)
1138   "Double letters for big marks."
1139   (let* ((lst (vector-length vec)))
1140     
1141     (if (>= n lst)
1142         (string-append (number->markletter-string vec (1- (quotient n lst)))
1143                        (number->markletter-string vec (remainder n lst)))
1144         (make-string 1 (vector-ref vec n)))))
1145
1146 (define-builtin-markup-command (markletter layout props num) (integer?)
1147   "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
1148 (skipping letter@tie{}I), and continue with double letters."
1149   (ly:text-interface::interpret-markup layout props
1150     (number->markletter-string number->mark-letter-vector num)))
1151
1152 (define-builtin-markup-command (markalphabet layout props num) (integer?)
1153    "Make a markup letter for @var{num}.  The letters start with A to@tie{}Z
1154 and continue with double letters."
1155    (ly:text-interface::interpret-markup layout props
1156      (number->markletter-string number->mark-alphabet-vector num)))
1157
1158
1159
1160 (define-builtin-markup-command (slashed-digit layout props num) (integer?)
1161   "A feta number, with slash.  This is for use in the context of
1162 figured bass notation."
1163   (let*
1164       ((mag (magstep (chain-assoc-get 'font-size props 0)))
1165        (thickness
1166         (* mag
1167            (chain-assoc-get 'thickness props 0.16)))
1168        (dy (* mag 0.15))
1169        (number-stencil (interpret-markup layout
1170                                          (prepend-alist-chain 'font-encoding 'fetaNumber props)
1171                                          (number->string num)))
1172        (num-x (interval-widen (ly:stencil-extent number-stencil X)
1173                               (* mag 0.2)))
1174        (num-y (ly:stencil-extent number-stencil Y))
1175        (is-sane (and (interval-sane? num-x) (interval-sane? num-y)))
1176        
1177        (slash-stencil
1178         (if is-sane
1179             (ly:make-stencil
1180              `(draw-line
1181                ,thickness
1182                ,(car num-x) ,(- (interval-center num-y) dy)
1183                ,(cdr num-x) ,(+ (interval-center num-y) dy))
1184              num-x num-y)
1185             #f)))
1186
1187     (set! slash-stencil
1188           (cond
1189            ((not (ly:stencil? slash-stencil)) #f)
1190            ((= num 5) (ly:stencil-translate slash-stencil
1191                                             ;;(cons (* mag -0.05) (* mag 0.42))
1192                                             (cons (* mag -0.00) (* mag -0.07))
1193
1194                                             ))
1195            ((= num 7) (ly:stencil-translate slash-stencil
1196                                             ;;(cons (* mag -0.05) (* mag 0.42))
1197                                             (cons (* mag -0.00) (* mag -0.15))
1198
1199                                             ))
1200            
1201            (else slash-stencil)))
1202
1203     (if slash-stencil
1204         (set! number-stencil
1205               (ly:stencil-add number-stencil slash-stencil))
1206         
1207         (ly:warning "invalid number for slashed digit ~a" num))
1208
1209
1210     number-stencil))
1211 \f
1212 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1213 ;; the note command.
1214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1215
1216
1217 ;; TODO: better syntax.
1218
1219 (define-builtin-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
1220   "Construct a note symbol, with stem.  By using fractional values for
1221 @var{dir}, you can obtain longer or shorter stems."
1222
1223   (define (get-glyph-name-candidates dir log style)
1224     (map (lambda (dir-name)
1225      (format "noteheads.~a~a~a" dir-name (min log 2)
1226              (if (and (symbol? style)
1227                       (not (equal? 'default style)))
1228                  (symbol->string style)
1229                  "")))
1230          (list (if (= dir UP) "u" "d")
1231                "s")))
1232                    
1233   (define (get-glyph-name font cands)
1234     (if (null? cands)
1235      ""
1236      (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
1237          (get-glyph-name font (cdr cands))
1238          (car cands))))
1239     
1240   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
1241          (size-factor (magstep (chain-assoc-get 'font-size props 0)))
1242          (style (chain-assoc-get 'style props '()))
1243          (stem-length (*  size-factor (max 3 (- log 1))))
1244          (head-glyph-name (get-glyph-name font (get-glyph-name-candidates (sign dir) log style)))
1245          (head-glyph (ly:font-get-glyph font head-glyph-name))
1246          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
1247          (stem-thickness (* size-factor 0.13))
1248          (stemy (* dir stem-length))
1249          (attach-off (cons (interval-index
1250                             (ly:stencil-extent head-glyph X)
1251                             (* (sign dir) (car attach-indices)))
1252                            (* (sign dir)        ; fixme, this is inconsistent between X & Y.
1253                               (interval-index
1254                                (ly:stencil-extent head-glyph Y)
1255                                (cdr attach-indices)))))
1256          (stem-glyph (and (> log 0)
1257                           (ly:round-filled-box
1258                            (ordered-cons (car attach-off)
1259                                          (+ (car attach-off)  (* (- (sign dir)) stem-thickness)))
1260                            (cons (min stemy (cdr attach-off))
1261                                  (max stemy (cdr attach-off)))
1262                            (/ stem-thickness 3))))
1263          
1264          (dot (ly:font-get-glyph font "dots.dot"))
1265          (dotwid (interval-length (ly:stencil-extent dot X)))
1266          (dots (and (> dot-count 0)
1267                     (apply ly:stencil-add
1268                            (map (lambda (x)
1269                                   (ly:stencil-translate-axis
1270                                    dot (* 2 x dotwid) X))
1271                                 (iota dot-count)))))
1272          (flaggl (and (> log 2)
1273                       (ly:stencil-translate
1274                        (ly:font-get-glyph font
1275                                           (string-append "flags."
1276                                                          (if (> dir 0) "u" "d")
1277                                                          (number->string log)))
1278                        (cons (+ (car attach-off) (/ stem-thickness 2)) stemy)))))
1279
1280     (if (and dots flaggl (> dir 0))
1281         (set! dots (ly:stencil-translate-axis dots 0.35 X)))
1282     (if flaggl
1283         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
1284     (if (ly:stencil? stem-glyph)
1285         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
1286         (set! stem-glyph head-glyph))
1287     (if (ly:stencil? dots)
1288         (set! stem-glyph
1289               (ly:stencil-add
1290                (ly:stencil-translate-axis
1291                 dots
1292                 (+ (cdr (ly:stencil-extent head-glyph X)) dotwid)
1293                 X)
1294                stem-glyph)))
1295     stem-glyph))
1296
1297 (define-public log2 
1298   (let ((divisor (log 2)))
1299     (lambda (z) (inexact->exact (/ (log z) divisor)))))
1300
1301 (define (parse-simple-duration duration-string)
1302   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
1303   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
1304     (if (and match (string=? duration-string (match:substring match 0)))
1305         (let ((len  (match:substring match 1))
1306               (dots (match:substring match 2)))
1307           (list (cond ((string=? len "breve") -1)
1308                       ((string=? len "longa") -2)
1309                       ((string=? len "maxima") -3)
1310                       (else (log2 (string->number len))))
1311                 (if dots (string-length dots) 0)))
1312         (ly:error (_ "not a valid duration string: ~a") duration-string))))
1313
1314 (define-builtin-markup-command (note layout props duration dir) (string? number?)
1315   "This produces a note with a stem pointing in @var{dir} direction, with
1316 the @var{duration} for the note head type and augmentation dots.  For
1317 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
1318 a shortened down stem."
1319   (let ((parsed (parse-simple-duration duration)))
1320     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
1321
1322 \f
1323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1324 ;; translating.
1325 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1326
1327 (define-builtin-markup-command (lower layout props amount arg) (number? markup?)
1328   "Lower @var{arg} by the distance @var{amount}.
1329 A negative @var{amount} indicates raising; see also @code{\\raise}."
1330   (ly:stencil-translate-axis (interpret-markup layout props arg)
1331                              (- amount) Y))
1332
1333
1334 (define-builtin-markup-command (translate-scaled layout props offset arg) (number-pair? markup?)
1335   "Translate @var{arg} by @var{offset}, scaling the offset by the
1336 @code{font-size}."
1337   (let*
1338       ((factor (magstep (chain-assoc-get 'font-size props 0)))
1339        (scaled (cons (* factor (car offset))
1340                      (* factor (cdr offset)))))
1341     
1342   (ly:stencil-translate (interpret-markup layout props arg)
1343                         scaled)))
1344
1345 (define-builtin-markup-command (raise layout props amount arg) (number? markup?)
1346   "Raise @var{arg} by the distance @var{amount}.
1347 A negative @var{amount} indicates lowering, see also @code{\\lower}.
1348 @c
1349 @lilypond[verbatim,fragment,relative=1]
1350 c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" } }
1351 @end lilypond
1352 The argument to @code{\\raise} is the vertical displacement amount,
1353 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
1354 raise objects in relation to their surrounding markups.
1355
1356 If the text object itself is positioned above or below the staff, then
1357 @code{\\raise} cannot be used to move it, since the mechanism that
1358 positions it next to the staff cancels any shift made with
1359 @code{\\raise}.  For vertical positioning, use the @code{padding}
1360 and/or @code{extra-offset} properties."
1361   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
1362
1363 (define-builtin-markup-command (fraction layout props arg1 arg2) (markup? markup?)
1364   "Make a fraction of two markups."
1365   (let* ((m1 (interpret-markup layout props arg1))
1366          (m2 (interpret-markup layout props arg2))
1367          (factor (magstep (chain-assoc-get 'font-size props 0)))
1368          (boxdimen (cons (* factor -0.05) (* factor 0.05)))
1369          (padding (* factor 0.2))
1370          (baseline (* factor 0.6))
1371          (offset (* factor 0.75)))
1372     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
1373     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
1374     (let* ((x1 (ly:stencil-extent m1 X))
1375            (x2 (ly:stencil-extent m2 X))
1376            (line (ly:round-filled-box (interval-union x1 x2) boxdimen 0.0))
1377            ;; should stack mols separately, to maintain LINE on baseline
1378            (stack (stack-lines DOWN padding baseline (list m1 line m2))))
1379       (set! stack
1380             (ly:stencil-aligned-to stack Y CENTER))
1381       (set! stack
1382             (ly:stencil-aligned-to stack X LEFT))
1383       ;; should have EX dimension
1384       ;; empirical anyway
1385       (ly:stencil-translate-axis stack offset Y))))
1386
1387 (define-builtin-markup-command (normal-size-super layout props arg) (markup?)
1388   "Set @var{arg} in superscript with a normal font size."
1389   (ly:stencil-translate-axis
1390    (interpret-markup layout props arg)
1391    (* 0.5 (chain-assoc-get 'baseline-skip props)) Y))
1392
1393 (define-builtin-markup-command (super layout props arg) (markup?)
1394   "
1395 @cindex raising text
1396 @cindex lowering text
1397 @cindex moving text
1398 @cindex translating text
1399
1400 @cindex @code{\\super}
1401
1402 Raising and lowering texts can be done with @code{\\super} and
1403 @code{\\sub}:
1404 @c
1405 @lilypond[verbatim,fragment,relative=1]
1406 c1^\\markup { E \"=\" \\concat { \"mc\" \\super \"2\" } }
1407 @end lilypond"
1408   (ly:stencil-translate-axis
1409    (interpret-markup
1410     layout
1411     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1412     arg)
1413    (* 0.5 (chain-assoc-get 'baseline-skip props))
1414    Y))
1415
1416 (define-builtin-markup-command (translate layout props offset arg) (number-pair? markup?)
1417   "This translates an object.  Its first argument is a cons of numbers.
1418
1419 @example
1420 A \\translate #(cons 2 -3) @{ B C @} D
1421 @end example
1422
1423 This moves @q{B C} 2@tie{}spaces to the right, and 3 down, relative to its
1424 surroundings.  This command cannot be used to move isolated scripts
1425 vertically, for the same reason that @code{\\raise} cannot be used for
1426 that."
1427   (ly:stencil-translate (interpret-markup  layout props arg)
1428                         offset))
1429
1430 (define-builtin-markup-command (sub layout props arg) (markup?)
1431   "Set @var{arg} in subscript."
1432   (ly:stencil-translate-axis
1433    (interpret-markup
1434     layout
1435     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1436     arg)
1437    (* -0.5 (chain-assoc-get 'baseline-skip props))
1438    Y))
1439
1440 (define-builtin-markup-command (normal-size-sub layout props arg) (markup?)
1441   "Set @var{arg} in subscript, in a normal font size."
1442   (ly:stencil-translate-axis
1443    (interpret-markup layout props arg)
1444    (* -0.5 (chain-assoc-get 'baseline-skip props))
1445    Y))
1446 \f
1447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1448 ;; brackets.
1449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1450
1451 (define-builtin-markup-command (hbracket layout props arg) (markup?)
1452   "Draw horizontal brackets around @var{arg}."  
1453   (let ((th 0.1) ;; todo: take from GROB.
1454         (m (interpret-markup layout props arg)))
1455     (bracketify-stencil m X th (* 2.5 th) th)))
1456
1457 (define-builtin-markup-command (bracket layout props arg) (markup?)
1458   "Draw vertical brackets around @var{arg}."  
1459   (let ((th 0.1) ;; todo: take from GROB.
1460         (m (interpret-markup layout props arg)))
1461     (bracketify-stencil m Y th (* 2.5 th) th)))
1462 \f
1463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1464 ;; Delayed markup evaluation
1465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1466
1467 (define-builtin-markup-command (page-ref layout props label gauge default)
1468   (symbol? markup? markup?)
1469   "Reference to a page number. @var{label} is the label set on the referenced
1470 page (using the @code{\\label} command), @var{gauge} a markup used to estimate
1471 the maximum width of the page number, and @var{default} the value to display
1472 when @var{label} is not found."
1473   (let* ((gauge-stencil (interpret-markup layout props gauge))
1474          (x-ext (ly:stencil-extent gauge-stencil X))
1475          (y-ext (ly:stencil-extent gauge-stencil Y)))
1476     (ly:make-stencil
1477      `(delay-stencil-evaluation
1478        ,(delay (ly:stencil-expr
1479                 (let* ((table (ly:output-def-lookup layout 'label-page-table))
1480                        (label-page (and (list? table) (assoc label table)))
1481                        (page-number (and label-page (cdr label-page)))
1482                        (page-markup (if page-number (format "~a" page-number) default))
1483                        (page-stencil (interpret-markup layout props page-markup))
1484                        (gap (- (interval-length x-ext)
1485                                (interval-length (ly:stencil-extent page-stencil X)))))
1486                   (interpret-markup layout props
1487                                     (markup #:concat (#:hspace gap page-markup)))))))
1488      x-ext
1489      y-ext)))
1490
1491 \f
1492 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1493 ;; Markup list commands
1494 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1495
1496 (define (space-lines baseline-skip lines)
1497   (map (lambda (line)
1498          (stack-lines DOWN 0.0 (/ baseline-skip 2.0)
1499                       (list (ly:make-stencil "" (cons 0 0) (cons 0 0))
1500                             line
1501                             (ly:make-stencil "" (cons 0 0) (cons 0 0)))))
1502        lines))
1503
1504 (define-builtin-markup-list-command (justified-lines layout props args) (markup-list?)
1505   "Like @code{\\justify}, but return a list of lines instead of a single markup.
1506 Use @code{\\override #'(line-width . @var{X})} to set the line width;
1507 @var{X}@tie{}is the number of staff spaces."
1508   (space-lines (chain-assoc-get 'baseline-skip props)
1509                (wordwrap-markups layout props args #t)))
1510
1511 (define-builtin-markup-list-command (wordwrap-lines layout props args) (markup-list?)
1512   "Like @code{\\wordwrap}, but return a list of lines instead of a single markup.
1513 Use @code{\\override #'(line-width . @var{X})} to set the line width,
1514 where @var{X} is the number of staff spaces."
1515   (space-lines (chain-assoc-get 'baseline-skip props)
1516                (wordwrap-markups layout props args #f)))
1517
1518 (define-builtin-markup-list-command (column-lines layout props args) (markup-list?)
1519   "Like @code{\\column}, but return a list of lines instead of a single markup.
1520 @code{baseline-skip} determines the space between each markup in @var{args}."
1521   (space-lines (chain-assoc-get 'baseline-skip props)
1522                (interpret-markup-list layout props args)))
1523
1524 (define-builtin-markup-list-command (override-lines layout props new-prop args)
1525   (pair? markup-list?)
1526   "Like @code{\\override}, for markup lists."
1527   (interpret-markup-list layout (cons (list new-prop) props) args))