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