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