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