]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
*** empty log message ***
[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--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
6 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
7
8 ;;; markup commands
9 ;;;  * each markup function should have a doc string with
10 ;;     syntax, description and example. 
11
12 (use-modules (ice-9 regex))
13
14 (define-public empty-stencil (ly:make-stencil '() '(1 . -1) '(1 . -1)))
15 (define-public point-stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
16
17 (def-markup-command (stencil layout props stil) (ly:stencil?)
18   "Stencil as markup"
19   stil)
20
21 (def-markup-command (draw-circle layout props radius thickness)
22   (number? number?)
23   "A circle of radius @var{radius} and thickness @var{thickness}"
24   (make-circle-stencil radius thickness))
25
26 (def-markup-command (circle layout props arg) (markup?)
27   "Draw a circle around @var{arg}.  Use @code{thickness},
28 @code{circle-padding} and @code{font-size} properties to determine line
29 thickness and padding around the markup."
30   (let* ((th (chain-assoc-get 'thickness props  0.1))
31          (size (chain-assoc-get 'font-size props 0))
32          (pad
33           (* (magstep size)
34              (chain-assoc-get 'circle-padding props 0.2)))
35          (m (interpret-markup layout props arg)))
36     (circle-stencil m th pad)))
37
38 (def-markup-command (with-url layout props url arg) (string? markup?)
39   "Add a link to URL @var{url} around @var{arg}. This only works in
40 the PDF backend."
41   (let* ((stil (interpret-markup layout props arg))
42          (xextent (ly:stencil-extent stil X))
43          (yextent (ly:stencil-extent stil Y))
44          (old-expr (ly:stencil-expr stil))
45          (url-expr (list 'url-link url `(quote ,xextent) `(quote ,yextent))))
46     (ly:stencil-add (ly:make-stencil url-expr xextent yextent) stil)))
47
48 (def-markup-command (score layout props score) (ly:score?)
49   "Inline an image of music."
50   (let* ((systems (ly:score-embedded-format score layout)))
51
52     (if (= (vector-length systems) 0)
53         (begin
54           (ly:warning (_"No systems found in \\score markup.  Does it have a \\layout? block"))
55           empty-markup)
56         (let* ((stencil (ly:paper-system-stencil (vector-ref systems 0)))) 
57           (ly:stencil-aligned-to stencil Y CENTER)))))
58
59 (def-markup-command (simple layout props str) (string?)
60   "A simple text string; @code{\\markup @{ foo @}} is equivalent with
61 @code{\\markup @{ \\simple #\"foo\" @}}."
62   (interpret-markup layout props str))
63
64 (def-markup-command (encoded-simple layout props sym str) (symbol? string?)
65   "A text string, encoded with encoding @var{sym}. See
66 @usermanref{Text encoding} for more information."
67   (Text_interface::interpret_string layout props sym str))
68
69
70 ;; TODO: use font recoding.
71 ;;                    (make-line-markup
72 ;;                     (map make-word-markup (string-tokenize str)))))
73
74 (define-public empty-markup
75   (make-simple-markup ""))
76
77
78 (def-markup-command (postscript layout props str) (string?)
79   "This inserts @var{str} directly into the output as a PostScript
80 command string.  Due to technicalities of the output backends,
81 different scales should be used for the @TeX{} and PostScript backend,
82 selected with @code{-f}. 
83
84
85 For the TeX backend, the following string prints a rotated text
86
87 @cindex rotated text
88
89 @verbatim
90 0 0 moveto /ecrm10 findfont 
91 1.75 scalefont setfont 90 rotate (hello) show
92 @end verbatim
93
94 @noindent
95 The magical constant 1.75 scales from LilyPond units (staff spaces) to
96 TeX dimensions.
97
98 For the postscript backend, use the following
99
100 @verbatim
101 gsave /ecrm10 findfont 
102  10.0 output-scale div 
103  scalefont setfont 90 rotate (hello) show grestore 
104 @end verbatim
105 "
106   ;; FIXME
107   (ly:make-stencil
108    (list 'embedded-ps str)
109    '(0 . 0) '(0 . 0)))
110
111 ;;(def-markup-command (fill-line layout props line-width markups)
112 ;;  (number? markup-list?)
113 ;; no parser tag -- should make number? markuk-list? thingy
114 (def-markup-command (fill-line layout props markups)
115   (markup-list?)
116   "Put @var{markups} in a horizontal line of width @var{line-width}.
117    The markups are spaced/flushed to fill the entire line.
118    If there are no arguments, return an empty stencil.
119 "
120   (let* ((orig-stencils
121           (map (lambda (x) (interpret-markup layout props x))
122                markups))
123          (stencils
124           (map (lambda (stc)
125                  (if (ly:stencil-empty? stc)
126                      point-stencil
127                      stc)) orig-stencils))
128      (text-widths
129         (map (lambda (stc)
130                 (if (ly:stencil-empty? stc)
131                         0.0
132                                 (interval-length (ly:stencil-extent stc X))))
133                         stencils))
134      (text-width (apply + text-widths))
135          (word-count (length stencils))
136          (word-space (chain-assoc-get 'word-space props))
137          (line-width (chain-assoc-get 'linewidth props))
138          (fill-space
139                 (cond
140                         ((= word-count 1) 
141                                 (list
142                                         (/ (- line-width text-width) 2)
143                                         (/ (- line-width text-width) 2)))
144                         ((= word-count 2)
145                                 (list
146                                         (- line-width text-width)))
147                         (else 
148                                 (get-fill-space word-count line-width text-widths))))
149      (fill-space-normal
150         (map (lambda (x)
151                 (if (< x word-space)
152                         word-space
153                                 x))
154                         fill-space))
155                                         
156          (line-stencils (if (= word-count 1)
157                             (list
158                              point-stencil
159                              (car stencils)
160                              point-stencil)
161                             stencils)))
162
163     (if (null? (remove ly:stencil-empty? orig-stencils))
164         empty-stencil
165         (stack-stencils-padding-list X RIGHT fill-space-normal line-stencils))))
166         
167 (define (get-fill-space word-count line-width text-widths)
168         "Calculate the necessary paddings between each two adjacent texts.
169         The lengths of all texts are stored in @var{text-widths}.
170         The normal formula for the padding between texts a and b is:
171         padding = line-width/(word-count - 1) - (length(a) + length(b))/2
172         The first and last padding have to be calculated specially using the
173         whole length of the first or last text.
174         Return a list of paddings.
175 "
176         (cond
177          ;; special case first padding
178          ((= (length text-widths) word-count)
179           (cons 
180            (- (- (/ line-width (1- word-count)) (car text-widths))
181               (/ (car (cdr text-widths)) 2))
182            (get-fill-space word-count line-width (cdr text-widths))))
183          ;; special case last padding
184          ((= (length text-widths) 2)
185           (list (- (/ line-width (1- word-count))
186                    (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
187          (else
188           (cons 
189            (- (/ line-width (1- word-count))
190               (/ (+ (car text-widths) (car (cdr text-widths))) 2))
191            (get-fill-space word-count line-width (cdr text-widths))))))
192
193 (define (font-markup qualifier value)
194   (lambda (layout props arg)
195     (interpret-markup layout (prepend-alist-chain qualifier value props) arg)))
196
197 (def-markup-command (line layout props args) (markup-list?)
198   "Put @var{args} in a horizontal line.  The property @code{word-space}
199 determines the space between each markup in @var{args}."
200   (stack-stencil-line
201    (chain-assoc-get 'word-space props)
202    (remove ly:stencil-empty?
203            (map (lambda (m) (interpret-markup layout props m)) args))))
204
205 (def-markup-command (fromproperty layout props symbol) (symbol?)
206   "Read the @var{symbol} from property settings, and produce a stencil
207   from the markup contained within. If @var{symbol} is not defined, it
208   returns an empty markup"
209   (let* ((m (chain-assoc-get symbol props)))
210     (if (markup? m)
211         (interpret-markup layout props m)
212         (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
213
214
215 (def-markup-command (on-the-fly layout props procedure arg) (symbol? markup?)
216   "Apply the @var{procedure} markup command to
217 @var{arg}. @var{procedure} should take a single argument."
218   (let* ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
219     (set-object-property! anonymous-with-signature
220                           'markup-signature
221                           (list markup?))
222     (interpret-markup layout props (list anonymous-with-signature arg))))
223
224
225 (def-markup-command (combine layout props m1 m2) (markup? markup?)
226   "Print two markups on top of each other."
227   (let* ((s1 (interpret-markup layout props m1))
228          (s2 (interpret-markup layout props m2)))
229     (ly:stencil-add s1 s2)))
230
231 (def-markup-command (finger layout props arg) (markup?)
232   "Set the argument as small numbers."
233   (interpret-markup layout
234                     (cons '((font-size . -5) (font-encoding . fetaNumber)) props)
235                     arg))
236
237 (def-markup-command (fontsize layout props mag arg) (number? markup?)
238   "Set the relative font size, e.g.
239 @example
240 A \\fontsize #2 @{ B C @} D
241 @end example
242
243
244 This will enlarge the B and the C by two steps.
245 "
246   (interpret-markup
247    layout 
248    (prepend-alist-chain 'font-size mag props)
249    arg))
250
251 (def-markup-command (magnify layout props sz arg) (number? markup?)
252   "Set the font magnification for the its argument. In the following
253 example, the middle A will be 10% larger:
254 @example
255 A \\magnify #1.1 @{ A @} A
256 @end example
257
258 Note: magnification only works if a font-name is explicitly selected.
259 Use @code{\\fontsize} otherwise."
260   (interpret-markup
261    layout 
262    (prepend-alist-chain 'font-magnification sz props)
263    arg))
264
265 (def-markup-command (bold layout props arg) (markup?)
266   "Switch to bold font-series"
267   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
268
269 (def-markup-command (sans layout props arg) (markup?)
270   "Switch to the sans serif family"
271   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
272
273 (def-markup-command (number layout props arg) (markup?)
274   "Set font family to @code{number}, which yields the font used for
275 time signatures and fingerings.  This font only contains numbers and
276 some punctuation. It doesn't have any letters.  "
277   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
278
279 (def-markup-command (roman layout props arg) (markup?)
280   "Set font family to @code{roman}."
281   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
282
283 (def-markup-command (huge layout props arg) (markup?)
284   "Set font size to +2."
285   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
286
287 (def-markup-command (large layout props arg) (markup?)
288   "Set font size to +1."
289   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
290
291 (def-markup-command (normalsize layout props arg) (markup?)
292   "Set font size to default."
293   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
294
295 (def-markup-command (small layout props arg) (markup?)
296   "Set font size to -1."
297   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
298
299 (def-markup-command (tiny layout props arg) (markup?)
300   "Set font size to -2."
301   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
302
303 (def-markup-command (teeny layout props arg) (markup?)
304   "Set font size to -3."
305   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
306
307 (def-markup-command (caps layout props arg) (markup?)
308   "Set @code{font-shape} to @code{caps}."
309   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
310
311 ;(def-markup-command (latin-i layout props arg) (markup?)
312 ;  "TEST latin1 encoding."
313 ;  (interpret-markup layout (prepend-alist-chain 'font-shape 'latin1 props) arg))
314
315 (def-markup-command (dynamic layout props arg) (markup?)
316   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
317 @b{z}, @b{p}, and @b{r}.  When producing phrases, like ``pi@`{u} @b{f}'', the
318 normal words (like ``pi@`{u}'') should be done in a different font.  The
319 recommend font for this is bold and italic"
320   (interpret-markup
321    layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
322
323 (def-markup-command (italic layout props arg) (markup?)
324   "Use italic @code{font-shape} for @var{arg}. "
325   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
326
327 (def-markup-command (typewriter layout props arg) (markup?)
328   "Use @code{font-family} typewriter for @var{arg}."
329   (interpret-markup
330    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
331
332 (def-markup-command (upright layout props arg) (markup?)
333   "Set font shape to @code{upright}."
334   (interpret-markup
335    layout (prepend-alist-chain 'font-shape 'upright props) arg))
336
337 (def-markup-command (doublesharp layout props) ()
338   "Draw a double sharp symbol."
339
340   (interpret-markup layout props (markup #:musicglyph "accidentals.4")))
341 (def-markup-command (sesquisharp layout props) ()
342   "Draw a 3/2 sharp symbol."
343   (interpret-markup layout props (markup #:musicglyph "accidentals.3")))
344
345 (def-markup-command (sharp layout props) ()
346   "Draw a sharp symbol."
347   (interpret-markup layout props (markup #:musicglyph "accidentals.2")))
348
349 (def-markup-command (semisharp layout props) ()
350   "Draw a semi sharp symbol."
351   (interpret-markup layout props (markup #:musicglyph "accidentals.1")))
352
353 (def-markup-command (natural layout props) ()
354   "Draw a natural symbol."
355   (interpret-markup layout props (markup #:musicglyph "accidentals.0")))
356
357 (def-markup-command (semiflat layout props) ()
358   "Draw a semiflat."
359   (interpret-markup layout props (markup #:musicglyph "accidentals.M1")))
360
361 (def-markup-command (flat layout props) ()
362   "Draw a flat symbol."
363   (interpret-markup layout props (markup #:musicglyph "accidentals.M2")))
364
365 (def-markup-command (sesquiflat layout props) ()
366   "Draw a 3/2 flat symbol."
367   (interpret-markup layout props (markup #:musicglyph "accidentals.M3")))
368
369 (def-markup-command (doubleflat layout props) ()
370   "Draw a double flat symbol."
371   (interpret-markup layout props (markup #:musicglyph "accidentals.M4")))
372
373 (def-markup-command (with-color layout props color arg) (color? markup?)
374   "Draw @var{arg} in color specified by @var{color}"
375
376   (let* ((stil (interpret-markup layout props arg)))
377
378     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
379                      (ly:stencil-extent stil X)
380                      (ly:stencil-extent stil Y))))
381
382 ;;
383 ;; TODO: should extract baseline-skip from each argument somehow..
384 ;; 
385 (def-markup-command (column layout props args) (markup-list?)
386   "Stack the markups in @var{args} vertically.  The property
387 @code{baseline-skip} determines the space between each markup in @var{args}."
388   (stack-lines
389    -1 0.0 (chain-assoc-get 'baseline-skip props)
390    (remove ly:stencil-empty?
391            (map (lambda (m) (interpret-markup layout props m)) args))))
392
393 (def-markup-command (dir-column layout props args) (markup-list?)
394   "Make a column of args, going up or down, depending on the setting
395 of the @code{#'direction} layout property."
396   (let* ((dir (chain-assoc-get 'direction props)))
397     (stack-lines
398      (if (number? dir) dir -1)
399      0.0
400      (chain-assoc-get 'baseline-skip props)
401      (map (lambda (x) (interpret-markup layout props x)) args))))
402
403 (def-markup-command (center-align layout props args) (markup-list?)
404   "Put @code{args} in a centered column. "
405   (let* ((mols (map (lambda (x) (interpret-markup layout props x)) args))
406          (cmols (map (lambda (x) (ly:stencil-aligned-to x X CENTER)) mols)))
407     (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) cmols)))
408
409 (def-markup-command (vcenter layout props arg) (markup?)
410   "Align @code{arg} to its Y center. "
411   (let* ((mol (interpret-markup layout props arg)))
412     (ly:stencil-aligned-to mol Y CENTER)))
413
414 (def-markup-command (hcenter layout props arg) (markup?)
415   "Align @code{arg} to its X center. "
416   (let* ((mol (interpret-markup layout props arg)))
417     (ly:stencil-aligned-to mol X CENTER)))
418
419 (def-markup-command (right-align layout props arg) (markup?)
420   "Align @var{arg} on its right edge. "
421   (let* ((m (interpret-markup layout props arg)))
422     (ly:stencil-aligned-to m X RIGHT)))
423
424 (def-markup-command (left-align layout props arg) (markup?)
425   "Align @var{arg} on its left edge. "
426   (let* ((m (interpret-markup layout props arg)))
427     (ly:stencil-aligned-to m X LEFT)))
428
429 (def-markup-command (general-align layout props axis dir arg)  (integer? number? markup?)
430   "Align @var{arg} in @var{axis} direction to the @var{dir} side."
431   (let* ((m (interpret-markup layout props arg)))
432     (ly:stencil-aligned-to m axis dir)))
433
434 (def-markup-command (halign layout props dir arg) (number? markup?)
435   "Set horizontal alignment. If @var{dir} is @code{-1}, then it is
436 left-aligned, while @code{+1} is right. Values in between interpolate
437 alignment accordingly."
438   (let* ((m (interpret-markup layout props arg)))
439     (ly:stencil-aligned-to m X dir)))
440
441 (def-markup-command (musicglyph layout props glyph-name) (string?)
442   "This is converted to a musical symbol, e.g. @code{\\musicglyph
443 #\"accidentals.0\"} will select the natural sign from the music font.
444 See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
445   (ly:font-get-glyph
446    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
447                                    props))
448    glyph-name))
449
450 (def-markup-command (lookup layout props glyph-name) (string?)
451   "Lookup a glyph by name."
452   (ly:font-get-glyph (ly:paper-get-font layout props)
453                      glyph-name))
454
455 (def-markup-command (char layout props num) (integer?)
456   "Produce a single character, e.g. @code{\\char #65} produces the 
457 letter 'A'."
458   (ly:get-glyph (ly:paper-get-font layout props) num))
459
460 (def-markup-command (raise layout props amount arg) (number? markup?)
461   "
462 This  raises  @var{arg}, by the distance @var{amount}.
463 A negative @var{amount} indicates lowering:
464 @c
465 @lilypond[verbatim,fragment,relative=1]
466  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
467 @end lilypond
468 The argument to @code{\\raise} is the vertical displacement amount,
469 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
470 raise objects in relation to their surrounding markups.
471
472 If the text object itself is positioned above or below the staff, then
473 @code{\\raise} cannot be used to move it, since the mechanism that
474 positions it next to the staff cancels any shift made with
475 @code{\\raise}. For vertical positioning, use the @code{padding}
476 and/or @code{extra-offset} properties. "
477   (ly:stencil-translate-axis (interpret-markup layout props arg)
478                              amount Y))
479
480 (def-markup-command (fraction layout props arg1 arg2) (markup? markup?)
481   "Make a fraction of two markups."
482   (let* ((m1 (interpret-markup layout props arg1))
483          (m2 (interpret-markup layout props arg2)))
484     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
485     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
486     (let* ((x1 (ly:stencil-extent m1 X))
487            (x2 (ly:stencil-extent m2 X))
488            (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
489            ;; should stack mols separately, to maintain LINE on baseline
490            (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
491       (set! stack
492             (ly:stencil-aligned-to stack Y CENTER))
493       (set! stack
494             (ly:stencil-aligned-to stack X LEFT))
495       ;; should have EX dimension
496       ;; empirical anyway
497       (ly:stencil-translate-axis stack 0.75 Y))))
498
499
500
501 (def-markup-command (filled-box layout props xext yext blot)
502   (number-pair? number-pair? number?)
503   "Draw a box with rounded corners of dimensions @var{xext} and @var{yext}."
504   (ly:round-filled-box
505    xext yext blot))
506
507 (def-markup-command (whiteout layout props arg) (markup?)
508   "Provide a white underground for @var{arg}"
509   (let* ((stil (interpret-markup layout props
510                                  (make-with-color-markup black arg)))
511          (white
512           (interpret-markup layout props
513                             (make-with-color-markup
514                              white
515                              (make-filled-box-markup
516                               (ly:stencil-extent stil X)
517                               (ly:stencil-extent stil Y)
518                               0.0)))))
519
520     (ly:stencil-add white stil)))
521
522 ;; TODO: better syntax.
523
524 (def-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
525   "Construct a note symbol, with stem.  By using fractional values for
526 @var{dir}, you can obtain longer or shorter stems."
527   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
528          (size (chain-assoc-get 'font-size props 0))
529          (stem-length (* (magstep size) (max 3 (- log 1))))
530          (head-glyph (ly:font-get-glyph
531                       font
532                       (string-append "noteheads.s" (number->string (min log 2)))))
533          (stem-thickness 0.13)
534          (stemy (* dir stem-length))
535          (attachx (if (> dir 0)
536                       (- (cdr (ly:stencil-extent head-glyph X)) stem-thickness)
537                       0))
538          (attachy (* dir 0.28))
539          (stem-glyph (and (> log 0)
540                           (ly:round-filled-box
541                            (cons attachx (+ attachx  stem-thickness))
542                            (cons (min stemy attachy)
543                                  (max stemy attachy))
544                            (/ stem-thickness 3))))
545          (dot (ly:font-get-glyph font "dots.dot"))
546          (dotwid (interval-length (ly:stencil-extent dot X)))
547          (dots (and (> dot-count 0)
548                     (apply ly:stencil-add
549                            (map (lambda (x)
550                                   (ly:stencil-translate-axis
551                                    dot  (* (+ 1 (* 2 x)) dotwid) X))
552                                 (iota dot-count 1)))))
553          (flaggl (and (> log 2)
554                       (ly:stencil-translate
555                        (ly:font-get-glyph font
556                                           (string-append "flags."
557                                                          (if (> dir 0) "u" "d")
558                                                          (number->string log)))
559                        (cons (+ attachx (/ stem-thickness 2)) stemy)))))
560     (if flaggl
561         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
562     (if (ly:stencil? stem-glyph)
563         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
564         (set! stem-glyph head-glyph))
565     (if (ly:stencil? dots)
566         (set! stem-glyph
567               (ly:stencil-add
568                (ly:stencil-translate-axis
569                 dots
570                 (+ (if (and (> dir 0) (> log 2))
571                        (* 1.5 dotwid)
572                        0)
573                    ;; huh ? why not necessary?
574                    ;;(cdr (ly:stencil-extent head-glyph X))
575                    dotwid)
576                 X)
577                stem-glyph)))
578     stem-glyph))
579
580 (define-public log2 
581   (let ((divisor (log 2)))
582     (lambda (z) (inexact->exact (/ (log z) divisor)))))
583
584 (define (parse-simple-duration duration-string)
585   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
586   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
587     (if (and match (string=? duration-string (match:substring match 0)))
588         (let ((len  (match:substring match 1))
589               (dots (match:substring match 2)))
590           (list (cond ((string=? len "breve") -1)
591                       ((string=? len "longa") -2)
592                       ((string=? len "maxima") -3)
593                       (else (log2 (string->number len))))
594                 (if dots (string-length dots) 0)))
595         (ly:error (_ "not a valid duration string: ~a") duration-string))))
596
597 (def-markup-command (note layout props duration dir) (string? number?)
598   "This produces a note with a stem pointing in @var{dir} direction, with
599 the @var{duration} for the note head type and augmentation dots. For
600 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
601 a shortened down stem."
602   (let ((parsed (parse-simple-duration duration)))
603     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
604
605 (def-markup-command (normal-size-super layout props arg) (markup?)
606   "Set @var{arg} in superscript with a normal font size."
607   (ly:stencil-translate-axis
608    (interpret-markup layout props arg)
609    (* 0.5 (chain-assoc-get 'baseline-skip props)) Y))
610
611 (def-markup-command (super layout props arg) (markup?)
612   "
613 @cindex raising text
614 @cindex lowering text
615 @cindex moving text
616 @cindex translating text
617
618 @cindex @code{\\super}
619
620
621 Raising and lowering texts can be done with @code{\\super} and
622 @code{\\sub}:
623
624 @lilypond[verbatim,fragment,relative=1]
625  c1^\\markup { E \"=\" mc \\super \"2\" }
626 @end lilypond
627
628 "
629   (ly:stencil-translate-axis
630    (interpret-markup
631     layout
632     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
633     arg)
634    (* 0.5 (chain-assoc-get 'baseline-skip props))
635    Y))
636
637 (def-markup-command (translate layout props offset arg) (number-pair? markup?)
638   "This translates an object. Its first argument is a cons of numbers
639 @example
640 A \\translate #(cons 2 -3) @{ B C @} D
641 @end example
642 This moves `B C' 2 spaces to the right, and 3 down, relative to its
643 surroundings. This command cannot be used to move isolated scripts
644 vertically, for the same reason that @code{\\raise} cannot be used for
645 that.
646
647 "
648   (ly:stencil-translate (interpret-markup  layout props arg)
649                         offset))
650
651 (def-markup-command (sub layout props arg) (markup?)
652   "Set @var{arg} in subscript."
653   (ly:stencil-translate-axis
654    (interpret-markup
655     layout
656     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
657     arg)
658    (* -0.5 (chain-assoc-get 'baseline-skip props))
659    Y))
660
661 (def-markup-command (beam layout props width slope thickness)
662   (number? number? number?)
663   "Create a beam with the specified parameters."
664   (let* ((y (* slope width))
665          (yext (cons (min 0 y) (max 0 y)))
666          (half (/ thickness 2)))
667
668     (ly:make-stencil
669      (list 'beam width
670            slope
671            thickness
672            (ly:output-def-lookup layout 'blotdiameter))
673      (cons 0 width)
674      (cons (+ (- half) (car yext))
675            (+ half (cdr yext))))))
676
677 (def-markup-command (normal-size-sub layout props arg) (markup?)
678   "Set @var{arg} in subscript, in a normal font size."
679   (ly:stencil-translate-axis
680    (interpret-markup layout props arg)
681    (* -0.5 (chain-assoc-get 'baseline-skip props))
682    Y))
683
684 (def-markup-command (hbracket layout props arg) (markup?)
685   "Draw horizontal brackets around @var{arg}."  
686   (let ((th 0.1) ;; todo: take from GROB.
687         (m (interpret-markup layout props arg)))
688     (bracketify-stencil m X th (* 2.5 th) th)))
689
690 (def-markup-command (bracket layout props arg) (markup?)
691   "Draw vertical brackets around @var{arg}."  
692   (let ((th 0.1) ;; todo: take from GROB.
693         (m (interpret-markup layout props arg)))
694     (bracketify-stencil m Y th (* 2.5 th) th)))
695
696 ;; todo: fix negative space
697 (def-markup-command (hspace layout props amount) (number?)
698   "This produces a invisible object taking horizontal space.
699 @example 
700 \\markup @{ A \\hspace #2.0 B @} 
701 @end example
702 will put extra space between A and B, on top of the space that is
703 normally inserted before elements on a line.
704 "
705   (if (> amount 0)
706       (ly:make-stencil "" (cons 0 amount) '(-1 . 1))
707       (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
708
709 (def-markup-command (override layout props new-prop arg) (pair? markup?)
710   "Add the first argument in to the property list.  Properties may be
711 any sort of property supported by @internalsref{font-interface} and
712 @internalsref{text-interface}, for example
713
714 @verbatim
715 \\override #'(font-family . married) \"bla\"
716 @end verbatim
717
718 "
719   (interpret-markup layout (cons (list new-prop) props) arg))
720
721
722 (def-markup-command (fontsize layout props increment arg) (number? markup?)
723   "Add @var{increment} to the font-size. Adjust baseline skip accordingly."
724
725   (let* ((fs (chain-assoc-get 'font-size props 0))
726          (bs (chain-assoc-get 'baseline-skip props 2)) 
727          (entries (list
728                    (cons 'baseline-skip (* bs (magstep increment)))
729                    (cons 'font-size (+ fs increment )))))
730
731     (interpret-markup layout (cons entries props) arg)))
732   
733 (def-markup-command (bigger layout props arg) (markup?)
734   "Increase the font size relative to current setting"
735   (interpret-markup layout props
736    `(,fontsize-markup 1 ,arg)))
737
738 (def-markup-command (smaller layout props arg) (markup?)
739   "Decrease the font size relative to current setting"
740   (interpret-markup layout props
741    `(,fontsize-markup -1 ,arg)))
742
743 (def-markup-command larger (markup?) bigger-markup)
744
745 (def-markup-command (box layout props arg) (markup?)
746   "Draw a box round @var{arg}.  Looks at @code{thickness},
747 @code{box-padding} and @code{font-size} properties to determine line
748 thickness and padding around the markup."
749   (let* ((th (chain-assoc-get 'thickness props  0.1))
750          (size (chain-assoc-get 'font-size props 0))
751          (pad (* (magstep size)
752                  (chain-assoc-get 'box-padding props 0.2)))
753          (m (interpret-markup layout props arg)))
754     (box-stencil m th pad)))
755
756 ;;FIXME: is this working? 
757 (def-markup-command (strut layout props) ()
758   "Create a box of the same height as the space in the current font."
759   (let ((m (Text_interface::interpret_markup layout props " ")))
760     (ly:make-stencil (ly:stencil-expr m)
761                      (ly:stencil-extent m X)
762                      '(1000 . -1000))))
763
764 (define number->mark-letter-vector (make-vector 25 #\A))
765
766 (do ((i 0 (1+ i))
767      (j 0 (1+ j)))
768     ((>= i 26))
769   (if (= i (- (char->integer #\I) (char->integer #\A)))
770       (set! i (1+ i)))
771   (vector-set! number->mark-letter-vector j
772                (integer->char (+ i (char->integer #\A)))))
773
774 (define number->mark-alphabet-vector (list->vector
775   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
776
777 (define (number->markletter-string vec n)
778   "Double letters for big marks."
779   (let* ((lst (vector-length vec)))
780     
781     (if (>= n lst)
782         (string-append (number->markletter-string vec (1- (quotient n lst)))
783                        (number->markletter-string vec (remainder n lst)))
784         (make-string 1 (vector-ref vec n)))))
785
786 (def-markup-command (markletter layout props num) (integer?)
787   "Make a markup letter for @var{num}.  The letters start with A to Z
788  (skipping I), and continues with double letters."
789   (Text_interface::interpret_markup layout props
790     (number->markletter-string number->mark-letter-vector num)))
791
792 (def-markup-command (markalphabet layout props num) (integer?)
793    "Make a markup letter for @var{num}.  The letters start with A to Z
794  and continues with double letters."
795    (Text_interface::interpret_markup layout props
796      (number->markletter-string number->mark-alphabet-vector num)))
797
798 (def-markup-command (bracketed-y-column layout props indices args)
799   (list? markup-list?)
800   "Make a column of the markups in @var{args}, putting brackets around
801 the elements marked in @var{indices}, which is a list of numbers."
802   (define (sublist lst start stop)
803     (take (drop lst start) (- (1+ stop) start)))
804
805   (define (stencil-list-extent ss axis)
806     (cons
807      (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
808      (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
809   
810   (define (stack-stencils stencils bskip last-stencil)
811     (cond
812      ((null? stencils) '())
813      ((not (ly:stencil? last-stencil))
814       (cons (car stencils)
815             (stack-stencils (cdr stencils) bskip (car stencils))))
816      (else
817       (let* ((orig (car stencils))
818              (dir (chain-assoc-get 'direction  props DOWN))
819              (new (ly:stencil-moved-to-edge last-stencil Y dir
820                                             orig
821                                             0.1 bskip)))
822
823         (cons new (stack-stencils (cdr stencils) bskip new))))))
824
825   (define (make-brackets stencils indices acc)
826     (if (and stencils
827              (pair? indices)
828              (pair? (cdr indices)))
829         (let* ((encl (sublist stencils (car indices) (cadr indices)))
830                (x-ext (stencil-list-extent encl X))
831                (y-ext (stencil-list-extent encl Y))
832                (thick 0.10)
833                (pad 0.35)
834                (protusion (* 2.5 thick))
835                (lb
836                 (ly:stencil-translate-axis 
837                  (ly:bracket Y y-ext thick protusion)
838                  (- (car x-ext) pad) X))
839                (rb (ly:stencil-translate-axis
840                     (ly:bracket Y y-ext thick (- protusion))
841                     (+ (cdr x-ext) pad) X)))
842
843           (make-brackets
844            stencils (cddr indices)
845            (append
846             (list lb rb)
847             acc)))
848         acc))
849
850   (let* ((stencils
851           (map (lambda (x)
852                  (interpret-markup
853                   layout
854                   props
855                   x)) args))
856          (leading
857           (chain-assoc-get 'baseline-skip props))
858          (stacked (stack-stencils
859                    (remove ly:stencil-empty? stencils) 1.25 #f))
860          (brackets (make-brackets stacked indices '())))
861
862     (apply ly:stencil-add
863            (append stacked brackets))))