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