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