]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
73e243a6a185d879e8f24238ff2f7828c969bae5
[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)) -3.5)
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 (fontCaps 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 (caps layout props arg) (markup?)
970   (interpret-markup layout props (make-smallCaps-markup arg)))
971
972 (define-markup-command (dynamic layout props arg) (markup?)
973   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
974 @b{z}, @b{p}, and @b{r}.  When producing phrases, like ``pi@`{u} @b{f}'', the
975 normal words (like ``pi@`{u}'') should be done in a different font.  The
976 recommend font for this is bold and italic"
977   (interpret-markup
978    layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
979
980 (define-markup-command (text layout props arg) (markup?)
981   "Use a text font instead of music symbol or music alphabet font."  
982
983   ;; ugh - latin1
984   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
985                     arg))
986
987
988 (define-markup-command (italic layout props arg) (markup?)
989   "Use italic @code{font-shape} for @var{arg}. "
990   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
991
992 (define-markup-command (typewriter layout props arg) (markup?)
993   "Use @code{font-family} typewriter for @var{arg}."
994   (interpret-markup
995    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
996
997 (define-markup-command (upright layout props arg) (markup?)
998   "Set font shape to @code{upright}.  This is the opposite of @code{italic}."
999   (interpret-markup
1000    layout (prepend-alist-chain 'font-shape 'upright props) arg))
1001
1002 (define-markup-command (medium layout props arg) (markup?)
1003   "Switch to medium font-series (in contrast to bold)."
1004   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
1005                     arg))
1006
1007 (define-markup-command (normal-text layout props arg) (markup?)
1008   "Set all font related properties (except the size) to get the default normal text font, no matter what font was used earlier."
1009   ;; ugh - latin1
1010   (interpret-markup layout
1011                     (cons '((font-family . roman) (font-shape . upright)
1012                             (font-series . medium) (font-encoding . latin1))
1013                           props)
1014                     arg))
1015
1016 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1017 ;; symbols.
1018 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1019
1020 (define-markup-command (doublesharp layout props) ()
1021   "Draw a double sharp symbol."
1022
1023   (interpret-markup layout props (markup #:musicglyph "accidentals.4")))
1024
1025 (define-markup-command (sesquisharp layout props) ()
1026   "Draw a 3/2 sharp symbol."
1027   (interpret-markup layout props (markup #:musicglyph "accidentals.3")))
1028
1029 (define-markup-command (sharp layout props) ()
1030   "Draw a sharp symbol."
1031   (interpret-markup layout props (markup #:musicglyph "accidentals.2")))
1032
1033 (define-markup-command (semisharp layout props) ()
1034   "Draw a semi sharp symbol."
1035   (interpret-markup layout props (markup #:musicglyph "accidentals.1")))
1036
1037 (define-markup-command (natural layout props) ()
1038   "Draw a natural symbol."
1039   (interpret-markup layout props (markup #:musicglyph "accidentals.0")))
1040
1041 (define-markup-command (semiflat layout props) ()
1042   "Draw a semiflat."
1043   (interpret-markup layout props (markup #:musicglyph "accidentals.M1")))
1044
1045 (define-markup-command (flat layout props) ()
1046   "Draw a flat symbol."
1047   (interpret-markup layout props (markup #:musicglyph "accidentals.M2")))
1048
1049 (define-markup-command (sesquiflat layout props) ()
1050   "Draw a 3/2 flat symbol."
1051   (interpret-markup layout props (markup #:musicglyph "accidentals.M3")))
1052
1053 (define-markup-command (doubleflat layout props) ()
1054   "Draw a double flat symbol."
1055   (interpret-markup layout props (markup #:musicglyph "accidentals.M4")))
1056
1057 (define-markup-command (with-color layout props color arg) (color? markup?)
1058   "Draw @var{arg} in color specified by @var{color}"
1059
1060   (let* ((stil (interpret-markup layout props arg)))
1061
1062     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
1063                      (ly:stencil-extent stil X)
1064                      (ly:stencil-extent stil Y))))
1065
1066 \f
1067 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1068 ;; glyphs
1069 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1070
1071
1072 (define-markup-command (arrow-head layout props axis direction filled)
1073   (integer? ly:dir? boolean?)
1074   "produce an arrow head in specified direction and axis. Use the filled head if @var{filled} is  specified."
1075   (let*
1076       ((name (format "arrowheads.~a.~a~a"
1077                      (if filled
1078                          "close"
1079                          "open")
1080                      axis
1081                      direction)))
1082     (ly:font-get-glyph
1083      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1084                                      props))
1085      name)))
1086
1087 (define-markup-command (musicglyph layout props glyph-name) (string?)
1088   "This is converted to a musical symbol, e.g. @code{\\musicglyph
1089 #\"accidentals.0\"} will select the natural sign from the music font.
1090 See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
1091   (ly:font-get-glyph
1092    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1093                                    props))
1094    glyph-name))
1095
1096 (define-markup-command (lookup layout props glyph-name) (string?)
1097   "Lookup a glyph by name."
1098   (ly:font-get-glyph (ly:paper-get-font layout props)
1099                      glyph-name))
1100
1101 (define-markup-command (char layout props num) (integer?)
1102   "Produce a single character, e.g. @code{\\char #65} produces the 
1103 letter 'A'."
1104
1105   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
1106
1107 (define number->mark-letter-vector (make-vector 25 #\A))
1108
1109 (do ((i 0 (1+ i))
1110      (j 0 (1+ j)))
1111     ((>= i 26))
1112   (if (= i (- (char->integer #\I) (char->integer #\A)))
1113       (set! i (1+ i)))
1114   (vector-set! number->mark-letter-vector j
1115                (integer->char (+ i (char->integer #\A)))))
1116
1117 (define number->mark-alphabet-vector (list->vector
1118   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
1119
1120 (define (number->markletter-string vec n)
1121   "Double letters for big marks."
1122   (let* ((lst (vector-length vec)))
1123     
1124     (if (>= n lst)
1125         (string-append (number->markletter-string vec (1- (quotient n lst)))
1126                        (number->markletter-string vec (remainder n lst)))
1127         (make-string 1 (vector-ref vec n)))))
1128
1129 (define-markup-command (markletter layout props num) (integer?)
1130   "Make a markup letter for @var{num}.  The letters start with A to Z
1131  (skipping I), and continues with double letters."
1132   (ly:text-interface::interpret-markup layout props
1133     (number->markletter-string number->mark-letter-vector num)))
1134
1135 (define-markup-command (markalphabet layout props num) (integer?)
1136    "Make a markup letter for @var{num}.  The letters start with A to Z
1137  and continues with double letters."
1138    (ly:text-interface::interpret-markup layout props
1139      (number->markletter-string number->mark-alphabet-vector num)))
1140
1141
1142
1143 (define-markup-command (slashed-digit layout props num) (integer?)
1144   "A feta number, with slash. This is for use in the context of
1145 figured bass notation"
1146   (let*
1147       ((mag (magstep (chain-assoc-get 'font-size props 0)))
1148        (thickness
1149         (* mag
1150            (chain-assoc-get 'thickness props 0.16)))
1151        (dy (* mag 0.15))
1152        (number-stencil (interpret-markup layout
1153                                          (prepend-alist-chain 'font-encoding 'fetaNumber props)
1154                                          (number->string num)))
1155        (num-x (interval-widen (ly:stencil-extent number-stencil X)
1156                               (* mag 0.2)))
1157        (num-y (ly:stencil-extent number-stencil Y))
1158        (slash-stencil 
1159         (ly:make-stencil
1160          `(draw-line
1161            ,thickness
1162            ,(car num-x) ,(- (interval-center num-y) dy)
1163            ,(cdr num-x) ,(+ (interval-center num-y) dy))
1164          num-x num-y
1165          )))
1166
1167     (ly:stencil-add number-stencil
1168                     (cond
1169                      ((= num 5) (ly:stencil-translate slash-stencil
1170                                                       ;;(cons (* mag -0.05) (* mag 0.42))
1171                                                       (cons (* mag -0.00) (* mag -0.07))
1172
1173                                                       ))
1174                      ((= num 7) (ly:stencil-translate slash-stencil
1175                                                       ;;(cons (* mag -0.05) (* mag 0.42))
1176                                                       (cons (* mag -0.00) (* mag -0.15))
1177
1178                                                       ))
1179                      
1180                      (else slash-stencil)))
1181     ))
1182 \f
1183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1184 ;; the note command.
1185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1186
1187
1188 ;; TODO: better syntax.
1189
1190 (define-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
1191   "Construct a note symbol, with stem.  By using fractional values for
1192 @var{dir}, you can obtain longer or shorter stems."
1193   (define (get-glyph-name-candidates dir log style)
1194     (map (lambda (dir-name)
1195      (format "noteheads.~a~a~a" dir-name (min log 2)
1196              (if (and (symbol? style)
1197                       (not (equal? 'default style)))
1198                  (symbol->string style)
1199                  "")))
1200          (list (if (= dir UP) "u" "d")
1201                "s")))
1202                    
1203   (define (get-glyph-name font cands)
1204     (if (null? cands)
1205      ""
1206      (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
1207          (get-glyph-name font (cdr cands))
1208          (car cands))))
1209     
1210   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
1211          (size-factor (magstep (chain-assoc-get 'font-size props 0)))
1212          (style (chain-assoc-get 'style props '()))
1213          (stem-length (*  size-factor (max 3 (- log 1))))
1214          (head-glyph-name (get-glyph-name font (get-glyph-name-candidates dir log style)))
1215          (head-glyph (ly:font-get-glyph font head-glyph-name))
1216          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
1217          (stem-thickness (* size-factor 0.13))
1218          (stemy (* dir stem-length))
1219          (attach-off (cons (interval-index
1220                             (ly:stencil-extent head-glyph X)
1221                             (* dir (car attach-indices)))
1222                            (* dir       ; fixme, this is inconsistent between X & Y.
1223                               (interval-index
1224                                (ly:stencil-extent head-glyph Y)
1225                                (cdr attach-indices)))))
1226          (stem-glyph (and (> log 0)
1227                           (ly:round-filled-box
1228                            (ordered-cons (car attach-off)
1229                                          (+ (car attach-off)  (* (- dir) stem-thickness)))
1230                            (cons (min stemy (cdr attach-off))
1231                                  (max stemy (cdr attach-off)))
1232                            (/ stem-thickness 3))))
1233          
1234          (dot (ly:font-get-glyph font "dots.dot"))
1235          (dotwid (interval-length (ly:stencil-extent dot X)))
1236          (dots (and (> dot-count 0)
1237                     (apply ly:stencil-add
1238                            (map (lambda (x)
1239                                   (ly:stencil-translate-axis
1240                                    dot  (* (+ 1 (* 2 x)) dotwid) X))
1241                                 (iota dot-count 1)))))
1242          (flaggl (and (> log 2)
1243                       (ly:stencil-translate
1244                        (ly:font-get-glyph font
1245                                           (string-append "flags."
1246                                                          (if (> dir 0) "u" "d")
1247                                                          (number->string log)))
1248                        (cons (+ (car attach-off) (/ stem-thickness 2)) stemy)))))
1249     (if flaggl
1250         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
1251     (if (ly:stencil? stem-glyph)
1252         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
1253         (set! stem-glyph head-glyph))
1254     (if (ly:stencil? dots)
1255         (set! stem-glyph
1256               (ly:stencil-add
1257                (ly:stencil-translate-axis
1258                 dots
1259                 (+ (if (and (> dir 0) (> log 2))
1260                        (* 1.5 dotwid)
1261                        0)
1262                    ;; huh ? why not necessary?
1263                    ;;(cdr (ly:stencil-extent head-glyph X))
1264                    dotwid)
1265                 X)
1266                stem-glyph)))
1267     stem-glyph))
1268
1269 (define-public log2 
1270   (let ((divisor (log 2)))
1271     (lambda (z) (inexact->exact (/ (log z) divisor)))))
1272
1273 (define (parse-simple-duration duration-string)
1274   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
1275   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
1276     (if (and match (string=? duration-string (match:substring match 0)))
1277         (let ((len  (match:substring match 1))
1278               (dots (match:substring match 2)))
1279           (list (cond ((string=? len "breve") -1)
1280                       ((string=? len "longa") -2)
1281                       ((string=? len "maxima") -3)
1282                       (else (log2 (string->number len))))
1283                 (if dots (string-length dots) 0)))
1284         (ly:error (_ "not a valid duration string: ~a") duration-string))))
1285
1286 (define-markup-command (note layout props duration dir) (string? number?)
1287   "This produces a note with a stem pointing in @var{dir} direction, with
1288 the @var{duration} for the note head type and augmentation dots. For
1289 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
1290 a shortened down stem."
1291   (let ((parsed (parse-simple-duration duration)))
1292     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
1293
1294 \f
1295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1296 ;; translating.
1297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1298
1299 (define-markup-command (lower layout props amount arg) (number? markup?)
1300   "
1301 Lower @var{arg}, by the distance @var{amount}.
1302 A negative @var{amount} indicates raising, see also @code{\\raise}.
1303 "
1304   (ly:stencil-translate-axis (interpret-markup layout props arg)
1305                              (- amount) Y))
1306
1307
1308 (define-markup-command (translate-scaled layout props offset arg) (number-pair? markup?)
1309   "Translate @var{arg} by @var{offset}, scaling the offset by the @code{font-size}."
1310
1311   (let*
1312       ((factor (magstep (chain-assoc-get 'font-size props 0)))
1313        (scaled (cons (* factor (car offset))
1314                      (* factor (cdr offset)))))
1315     
1316   (ly:stencil-translate (interpret-markup layout props arg)
1317                         scaled)))
1318
1319 (define-markup-command (raise layout props amount arg) (number? markup?)
1320   "
1321 Raise @var{arg}, by the distance @var{amount}.
1322 A negative @var{amount} indicates lowering, see also @code{\\lower}.
1323 @c
1324 @lilypond[verbatim,fragment,relative=1]
1325  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
1326 @end lilypond
1327 The argument to @code{\\raise} is the vertical displacement amount,
1328 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
1329 raise objects in relation to their surrounding markups.
1330
1331 If the text object itself is positioned above or below the staff, then
1332 @code{\\raise} cannot be used to move it, since the mechanism that
1333 positions it next to the staff cancels any shift made with
1334 @code{\\raise}. For vertical positioning, use the @code{padding}
1335 and/or @code{extra-offset} properties. "
1336   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
1337
1338 (define-markup-command (fraction layout props arg1 arg2) (markup? markup?)
1339   "Make a fraction of two markups."
1340   (let* ((m1 (interpret-markup layout props arg1))
1341          (m2 (interpret-markup layout props arg2)))
1342     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
1343     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
1344     (let* ((x1 (ly:stencil-extent m1 X))
1345            (x2 (ly:stencil-extent m2 X))
1346            (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
1347            ;; should stack mols separately, to maintain LINE on baseline
1348            (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
1349       (set! stack
1350             (ly:stencil-aligned-to stack Y CENTER))
1351       (set! stack
1352             (ly:stencil-aligned-to stack X LEFT))
1353       ;; should have EX dimension
1354       ;; empirical anyway
1355       (ly:stencil-translate-axis stack 0.75 Y))))
1356
1357
1358
1359
1360
1361 (define-markup-command (normal-size-super layout props arg) (markup?)
1362   "Set @var{arg} in superscript with a normal font size."
1363   (ly:stencil-translate-axis
1364    (interpret-markup layout props arg)
1365    (* 0.5 (chain-assoc-get 'baseline-skip props)) Y))
1366
1367 (define-markup-command (super layout props arg) (markup?)
1368   "
1369 @cindex raising text
1370 @cindex lowering text
1371 @cindex moving text
1372 @cindex translating text
1373
1374 @cindex @code{\\super}
1375
1376
1377 Raising and lowering texts can be done with @code{\\super} and
1378 @code{\\sub}:
1379
1380 @lilypond[verbatim,fragment,relative=1]
1381  c1^\\markup { E \"=\" mc \\super \"2\" }
1382 @end lilypond
1383
1384 "
1385   (ly:stencil-translate-axis
1386    (interpret-markup
1387     layout
1388     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1389     arg)
1390    (* 0.5 (chain-assoc-get 'baseline-skip props))
1391    Y))
1392
1393 (define-markup-command (translate layout props offset arg) (number-pair? markup?)
1394   "This translates an object. Its first argument is a cons of numbers
1395 @example
1396 A \\translate #(cons 2 -3) @{ B C @} D
1397 @end example
1398 This moves `B C' 2 spaces to the right, and 3 down, relative to its
1399 surroundings. This command cannot be used to move isolated scripts
1400 vertically, for the same reason that @code{\\raise} cannot be used for
1401 that.
1402
1403 "
1404   (ly:stencil-translate (interpret-markup  layout props arg)
1405                         offset))
1406
1407 (define-markup-command (sub layout props arg) (markup?)
1408   "Set @var{arg} in subscript."
1409   (ly:stencil-translate-axis
1410    (interpret-markup
1411     layout
1412     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1413     arg)
1414    (* -0.5 (chain-assoc-get 'baseline-skip props))
1415    Y))
1416
1417 (define-markup-command (normal-size-sub layout props arg) (markup?)
1418   "Set @var{arg} in subscript, in a normal font size."
1419   (ly:stencil-translate-axis
1420    (interpret-markup layout props arg)
1421    (* -0.5 (chain-assoc-get 'baseline-skip props))
1422    Y))
1423 \f
1424 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1425 ;; brackets.
1426 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1427
1428 (define-markup-command (hbracket layout props arg) (markup?)
1429   "Draw horizontal brackets around @var{arg}."  
1430   (let ((th 0.1) ;; todo: take from GROB.
1431         (m (interpret-markup layout props arg)))
1432     (bracketify-stencil m X th (* 2.5 th) th)))
1433
1434 (define-markup-command (bracket layout props arg) (markup?)
1435   "Draw vertical brackets around @var{arg}."  
1436   (let ((th 0.1) ;; todo: take from GROB.
1437         (m (interpret-markup layout props arg)))
1438     (bracketify-stencil m Y th (* 2.5 th) th)))
1439
1440 (define-markup-command (bracketed-y-column layout props indices args)
1441   (list? markup-list?)
1442   "Make a column of the markups in @var{args}, putting brackets around
1443 the elements marked in @var{indices}, which is a list of numbers.
1444
1445 "
1446 ;;
1447 ;; DROPME? This command is a relic from the old figured bass implementation.
1448 ;;
1449   
1450   (define (sublist lst start stop)
1451     (take (drop lst start) (- (1+ stop) start)))
1452
1453   (define (stencil-list-extent ss axis)
1454     (cons
1455      (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
1456      (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
1457   
1458
1459   (define (stack-stencils-vertically stencils bskip last-stencil)
1460     (cond
1461      ((null? stencils) '())
1462      ((not (ly:stencil? last-stencil))
1463       (cons (car stencils)
1464             (stack-stencils-vertically (cdr stencils) bskip (car stencils))))
1465      (else
1466       (let* ((orig (car stencils))
1467              (dir (chain-assoc-get 'direction  props DOWN))
1468              (new (ly:stencil-moved-to-edge last-stencil Y dir
1469                                             orig
1470                                             0.1 bskip)))
1471
1472         (cons new (stack-stencils-vertically (cdr stencils) bskip new))))))
1473
1474   (define (make-brackets stencils indices acc)
1475     (if (and stencils
1476              (pair? indices)
1477              (pair? (cdr indices)))
1478         (let* ((encl (sublist stencils (car indices) (cadr indices)))
1479                (x-ext (stencil-list-extent encl X))
1480                (y-ext (stencil-list-extent encl Y))
1481                (thick 0.10)
1482                (pad 0.35)
1483                (protusion (* 2.5 thick))
1484                (lb
1485                 (ly:stencil-translate-axis 
1486                  (ly:bracket Y y-ext thick protusion)
1487                  (- (car x-ext) pad) X))
1488                (rb (ly:stencil-translate-axis
1489                     (ly:bracket Y y-ext thick (- protusion))
1490                     (+ (cdr x-ext) pad) X)))
1491
1492           (make-brackets
1493            stencils (cddr indices)
1494            (append
1495             (list lb rb)
1496             acc)))
1497         acc))
1498
1499   (let* ((stencils
1500           (map (lambda (x)
1501                  (interpret-markup
1502                   layout
1503                   props
1504                   x)) args))
1505          (leading
1506           (chain-assoc-get 'baseline-skip props))
1507          (stacked (stack-stencils-vertically
1508                    (remove ly:stencil-empty? stencils) 1.25 #f))
1509          (brackets (make-brackets stacked indices '())))
1510
1511     (apply ly:stencil-add
1512            (append stacked brackets))))
1513 \f
1514
1515 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1516 ;; size indications arrow
1517 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1518