]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
Fix #264
[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
76     (ly:stencil-add (ly:make-stencil url-expr xextent yextent) stil)))
77
78
79 (define-markup-command (beam layout props width slope thickness)
80   (number? number? number?)
81   "Create a beam with the specified parameters."
82   (let* ((y (* slope width))
83          (yext (cons (min 0 y) (max 0 y)))
84          (half (/ thickness 2)))
85
86     (ly:make-stencil
87      `(polygon ',(list 
88                   0 (/ thickness -2)
89                     width (+ (* width slope)  (/ thickness -2))
90                     width (+ (* width slope)  (/ thickness 2))
91                     0 (/ thickness 2))
92                ,(ly:output-def-lookup layout 'blot-diameter)
93                #t)
94      (cons 0 width)
95      (cons (+ (- half) (car yext))
96            (+ half (cdr yext))))))
97
98 (define-markup-command (box layout props arg) (markup?)
99   "Draw a box round @var{arg}.  Looks at @code{thickness},
100 @code{box-padding} and @code{font-size} properties to determine line
101 thickness and padding around the markup."
102   
103   (let* ((th (chain-assoc-get 'thickness props  0.1))
104          (size (chain-assoc-get 'font-size props 0))
105          (pad (* (magstep size)
106                  (chain-assoc-get 'box-padding props 0.2)))
107          (m (interpret-markup layout props arg)))
108     (box-stencil m th pad)))
109
110 (define-markup-command (filled-box layout props xext yext blot)
111   (number-pair? number-pair? number?)
112   "Draw a box with rounded corners of dimensions @var{xext} and
113 @var{yext}.  For example,
114 @verbatim
115 \\filled-box #'(-.3 . 1.8) #'(-.3 . 1.8) #0
116 @end verbatim
117 create a box extending horizontally from -0.3 to 1.8 and
118 vertically from -0.3 up to 1.8, with corners formed from a
119 circle of diameter 0 (ie sharp corners)."
120   (ly:round-filled-box
121    xext yext blot))
122
123 (define-markup-command (rotate layout props ang arg) (number? markup?)
124   "Rotate object with @var{ang} degrees around its center."
125   (let* ((stil (interpret-markup layout props arg)))
126     (ly:stencil-rotate stil ang 0 0)))
127
128
129 (define-markup-command (whiteout layout props arg) (markup?)
130   "Provide a white underground for @var{arg}"
131   (let* ((stil (interpret-markup layout props arg))
132          (white
133           (interpret-markup layout props
134                             (make-with-color-markup
135                              white
136                              (make-filled-box-markup
137                               (ly:stencil-extent stil X)
138                               (ly:stencil-extent stil Y)
139                               0.0)))))
140
141     (ly:stencil-add white stil)))
142
143 (define-markup-command (pad-markup layout props padding arg) (number? markup?)
144   "Add space around a markup object."
145
146   (let*
147       ((stil (interpret-markup layout props arg))
148        (xext (ly:stencil-extent stil X))
149        (yext (ly:stencil-extent stil Y)))
150
151     (ly:make-stencil
152      (ly:stencil-expr stil)
153      (interval-widen xext padding)
154      (interval-widen yext padding))))
155
156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 ;; space
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159
160 ;;FIXME: is this working? 
161 (define-markup-command (strut layout props) ()
162   "Create a box of the same height as the space in the current font."
163   (let ((m (ly:text-interface::interpret-markup layout props " ")))
164     (ly:make-stencil (ly:stencil-expr m)
165                      '(1000 . -1000)
166                      (ly:stencil-extent m X)
167                      )))
168
169
170 ;; todo: fix negative space
171 (define-markup-command (hspace layout props amount) (number?)
172   "This produces a invisible object taking horizontal space.
173 @example 
174 \\markup @{ A \\hspace #2.0 B @} 
175 @end example
176 will put extra space between A and B, on top of the space that is
177 normally inserted before elements on a line.
178 "
179   (if (> amount 0)
180       (ly:make-stencil "" (cons 0 amount) '(-1 . 1))
181       (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
182
183
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
185 ;; importing graphics.
186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187
188 (define-markup-command (stencil layout props stil) (ly:stencil?)
189   "Stencil as markup"
190   stil)
191
192 (define bbox-regexp
193   (make-regexp "%%BoundingBox:[ \t]+([0-9-]+)[ \t]+([0-9-]+)[ \t]+([0-9-]+)[ \t]+([0-9-]+)"))
194
195 (define (get-postscript-bbox string)
196   "Extract the bbox from STRING, or return #f if not present."
197   (let*
198       ((match (regexp-exec bbox-regexp string)))
199     
200     (if match
201         (map (lambda (x)
202                (string->number (match:substring match x)))
203              (cdr (iota 5)))
204              
205         #f)))
206
207 (define-markup-command (epsfile layout props axis size file-name) (number? number? string?)
208   "Inline an EPS image. The image is scaled along @var{axis} to
209 @var{size}."
210
211   (if (ly:get-option 'safe)
212       (interpret-markup layout props "not allowed in safe")
213       (eps-file->stencil axis size file-name)
214       ))
215
216 (define-markup-command (postscript layout props str) (string?)
217   "This inserts @var{str} directly into the output as a PostScript
218 command string.  Due to technicalities of the output backends,
219 different scales should be used for the @TeX{} and PostScript backend,
220 selected with @code{-f}. 
221
222
223 For the TeX backend, the following string prints a rotated text
224
225 @cindex rotated text
226
227 @verbatim
228 0 0 moveto /ecrm10 findfont 
229 1.75 scalefont setfont 90 rotate (hello) show
230 @end verbatim
231
232 @noindent
233 The magical constant 1.75 scales from LilyPond units (staff spaces) to
234 TeX dimensions.
235
236 For the postscript backend, use the following
237
238 @verbatim
239 gsave /ecrm10 findfont 
240  10.0 output-scale div 
241  scalefont setfont 90 rotate (hello) show grestore 
242 @end verbatim
243 "
244
245   ;; FIXME
246   (ly:make-stencil
247    (list 'embedded-ps
248          (format "
249 gsave currentpoint translate
250 0.1 setlinewidth
251  ~a
252 grestore
253 "
254                  str))
255    '(0 . 0) '(0 . 0)))
256
257
258 (define-markup-command (score layout props score) (ly:score?)
259   "Inline an image of music."
260   (let* ((output (ly:score-embedded-format score layout)))
261
262     (if (ly:music-output? output)
263         (paper-system-stencil
264          (vector-ref (ly:paper-score-paper-systems output) 0))
265         (begin
266           (ly:warning (_"no systems found in \\score markup, does it have a \\layout block?"))
267           empty-stencil))))
268
269 (define-markup-command (null layout props) ()
270   "An empty markup with extents of a single point"
271
272   point-stencil)
273
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 ;; basic formatting.
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277
278
279
280 (define-markup-command (simple layout props str) (string?)
281   "A simple text string; @code{\\markup @{ foo @}} is equivalent with
282 @code{\\markup @{ \\simple #\"foo\" @}}."
283   (interpret-markup layout props str))
284
285 (define-markup-command (tied-lyric layout props str) (string?)
286   
287   "Like simple-markup, but use tie characters for ~ tilde symbols."
288
289   (if (string-contains str "~")
290       (let*
291           ((parts (string-split str #\~))
292            (tie-str (ly:wide-char->utf-8 #x203f))
293            (joined  (list-join parts tie-str))
294            (join-stencil (interpret-markup layout props tie-str))
295            )
296
297         (interpret-markup layout 
298                           (prepend-alist-chain
299                            'word-space
300                            (/ (interval-length (ly:stencil-extent join-stencil X)) -3.5)
301                            props)
302                           (make-line-markup joined)))
303                            ;(map (lambda (s) (interpret-markup layout props s)) parts))
304       (interpret-markup layout props str)))
305
306
307 ;; TODO: use font recoding.
308 ;;                    (make-line-markup
309 ;;                     (map make-word-markup (string-tokenize str)))))
310
311 (define-public empty-markup
312   (make-simple-markup ""))
313
314 ;; helper for justifying lines.
315 (define (get-fill-space word-count line-width text-widths)
316   "Calculate the necessary paddings between each two adjacent texts.
317         The lengths of all texts are stored in @var{text-widths}.
318         The normal formula for the padding between texts a and b is:
319         padding = line-width/(word-count - 1) - (length(a) + length(b))/2
320         The first and last padding have to be calculated specially using the
321         whole length of the first or last text.
322         Return a list of paddings.
323 "
324   (cond
325    ((null? text-widths) '())
326    
327    ;; special case first padding
328    ((= (length text-widths) word-count)
329     (cons 
330      (- (- (/ line-width (1- word-count)) (car text-widths))
331         (/ (car (cdr text-widths)) 2))
332      (get-fill-space word-count line-width (cdr text-widths))))
333    ;; special case last padding
334    ((= (length text-widths) 2)
335     (list (- (/ line-width (1- word-count))
336              (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
337    (else
338     (cons 
339      (- (/ line-width (1- word-count))
340         (/ (+ (car text-widths) (car (cdr text-widths))) 2))
341      (get-fill-space word-count line-width (cdr text-widths))))))
342
343 (define-markup-command (fill-line layout props markups)
344   (markup-list?)
345   "Put @var{markups} in a horizontal line of width @var{line-width}.
346    The markups are spaced/flushed to fill the entire line.
347    If there are no arguments, return an empty stencil."
348  
349   (let* ((orig-stencils
350           (map (lambda (x) (interpret-markup layout props x))
351                markups))
352          (stencils
353           (map (lambda (stc)
354                  (if (ly:stencil-empty? stc)
355                      point-stencil
356                      stc)) orig-stencils))
357          (text-widths
358           (map (lambda (stc)
359                  (if (ly:stencil-empty? stc)
360                      0.0
361                      (interval-length (ly:stencil-extent stc X))))
362                stencils))
363          (text-width (apply + text-widths))
364          (text-dir (chain-assoc-get 'text-direction props RIGHT))
365          (word-count (length stencils))
366          (word-space (chain-assoc-get 'word-space props 1))
367          (prop-line-width (chain-assoc-get 'line-width props #f))
368          (line-width (if prop-line-width prop-line-width
369                          (ly:output-def-lookup layout 'line-width)))
370          (fill-space
371                 (cond
372                         ((= word-count 1) 
373                                 (list
374                                         (/ (- line-width text-width) 2)
375                                         (/ (- line-width text-width) 2)))
376                         ((= word-count 2)
377                                 (list
378                                         (- line-width text-width)))
379                         (else 
380                                 (get-fill-space word-count line-width text-widths))))
381          (fill-space-normal
382           (map (lambda (x)
383                  (if (< x word-space)
384                      word-space
385                      x))
386                fill-space))
387                                         
388          (line-stencils (if (= word-count 1)
389                             (list
390                              point-stencil
391                              (car stencils)
392                              point-stencil)
393                             stencils)))
394
395     (if (= text-dir LEFT)
396         (set! line-stencils (reverse line-stencils)))
397
398     (if (null? (remove ly:stencil-empty? orig-stencils))
399         empty-stencil
400         (stack-stencils-padding-list X
401                                      RIGHT fill-space-normal line-stencils))))
402         
403 (define-markup-command (line layout props args) (markup-list?)
404   "Put @var{args} in a horizontal line.  The property @code{word-space}
405 determines the space between each markup in @var{args}."
406   (let*
407       ((stencils (map (lambda (m) (interpret-markup layout props m)) args))
408        (space    (chain-assoc-get 'word-space props))
409        (text-dir (chain-assoc-get 'text-direction props RIGHT)) 
410        )
411
412     (if (= text-dir LEFT)
413         (set! stencils (reverse stencils)))
414     
415
416     (stack-stencil-line
417      space
418      (remove ly:stencil-empty? stencils))))
419
420
421 (define (wordwrap-stencils stencils
422                            justify base-space line-width text-dir)
423   
424   "Perform simple wordwrap, return stencil of each line."
425   
426   (define space (if justify
427                     
428                     ;; justify only stretches lines.
429                     (* 0.7 base-space)
430                     base-space))
431        
432   (define (take-list width space stencils
433                      accumulator accumulated-width)
434     "Return (head-list . tail) pair, with head-list fitting into width"
435     (if (null? stencils)
436         (cons accumulator stencils)
437         (let*
438             ((first (car stencils))
439              (first-wid (cdr (ly:stencil-extent (car stencils) X)))
440              (newwid (+ space first-wid accumulated-width))
441              )
442
443           (if
444            (or (null? accumulator)
445                (< newwid width))
446
447            (take-list width space
448                       (cdr stencils)
449                       (cons first accumulator)
450                       newwid)
451              (cons accumulator stencils))
452            )))
453
454     (let loop
455         ((lines '())
456          (todo stencils))
457
458       (let*
459           ((line-break (take-list line-width space todo
460                                  '() 0.0))
461            (line-stencils (car line-break))
462            (space-left (- line-width (apply + (map (lambda (x) (cdr (ly:stencil-extent x X)))
463                                               line-stencils))))
464
465            (line-word-space (cond
466                              ((not justify) space)
467
468                              ;; don't stretch last line of paragraph.
469                              ;; hmmm . bug - will overstretch the last line in some case. 
470                              ((null? (cdr line-break))
471                               base-space)
472                              ((null? line-stencils) 0.0)
473                              ((null? (cdr line-stencils)) 0.0)
474                              (else (/ space-left (1- (length line-stencils))))))
475
476            (line (stack-stencil-line
477                   line-word-space
478                   (if (= text-dir RIGHT)
479                       (reverse line-stencils)
480                       line-stencils))))
481
482         (if (pair? (cdr line-break))
483             (loop (cons line lines)
484                   (cdr line-break))
485
486             (begin
487               (if (= text-dir LEFT)
488                   (set! line
489                         (ly:stencil-translate-axis line
490                                                    (- line-width (interval-end (ly:stencil-extent line X)))
491                                                    X)))
492               (reverse (cons line lines))
493               
494             )))
495
496       ))
497
498
499 (define (wordwrap-markups layout props args justify)
500   (let*
501       ((baseline-skip (chain-assoc-get 'baseline-skip props))
502        (prop-line-width (chain-assoc-get 'line-width props #f))
503        (line-width (if prop-line-width prop-line-width
504                        (ly:output-def-lookup layout 'line-width)))
505        (word-space (chain-assoc-get 'word-space props))
506        (text-dir (chain-assoc-get 'text-direction props RIGHT)) 
507        (lines (wordwrap-stencils
508                (remove ly:stencil-empty?
509                        (map (lambda (m) (interpret-markup layout props m)) args))
510                justify word-space line-width
511                text-dir)
512                ))
513
514     (stack-lines DOWN 0.0 baseline-skip lines)))
515
516 (define-markup-command (justify layout props args) (markup-list?)
517   "Like wordwrap, but with lines stretched to justify the margins.
518 Use @code{\\override #'(line-width . X)} to set line-width, where X
519 is the number of staff spaces."
520
521   (wordwrap-markups layout props args #t))
522
523 (define-markup-command (wordwrap layout props args) (markup-list?)
524   "Simple wordwrap.  Use @code{\\override #'(line-width . X)} to set
525 line-width, where X is the number of staff spaces."
526
527   (wordwrap-markups layout props args #f))
528
529 (define (wordwrap-string layout props justify arg) 
530   (let*
531       ((baseline-skip (chain-assoc-get 'baseline-skip props))
532        (line-width (chain-assoc-get 'line-width props))
533        (word-space (chain-assoc-get 'word-space props))
534        
535        (para-strings (regexp-split
536                       (string-regexp-substitute "\r" "\n"
537                                                 (string-regexp-substitute "\r\n" "\n" arg))
538                       "\n[ \t\n]*\n[ \t\n]*"))
539        
540        (text-dir (chain-assoc-get 'text-direction props RIGHT)) 
541        (list-para-words (map (lambda (str)
542                                (regexp-split str "[ \t\n]+"))
543                              para-strings))
544        (para-lines (map (lambda (words)
545                           (let*
546                               ((stencils
547                                 (remove
548                                  ly:stencil-empty? (map 
549                                       (lambda (x)
550                                         (interpret-markup layout props x))
551                                       words)))
552                                (lines (wordwrap-stencils stencils
553                                                          justify word-space
554                                                          line-width text-dir
555                                                          )))
556
557                             lines))
558                         
559                         list-para-words)))
560
561     (stack-lines DOWN 0.0 baseline-skip (apply append para-lines))))
562
563
564 (define-markup-command (wordwrap-string layout props arg) (string?)
565   "Wordwrap a string. Paragraphs may be separated with double newlines"
566   (wordwrap-string layout props  #f arg))
567   
568 (define-markup-command (justify-string layout props arg) (string?)
569   "Justify a string. Paragraphs may be separated with double newlines"
570   (wordwrap-string layout props #t arg))
571
572
573 (define-markup-command (wordwrap-field layout props symbol) (symbol?)
574    (let* ((m (chain-assoc-get symbol props)))
575      (if (string? m)
576       (interpret-markup layout props
577        (list wordwrap-string-markup m))
578       (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
579
580 (define-markup-command (justify-field layout props symbol) (symbol?)
581 -   (let* ((m (chain-assoc-get symbol props)))
582      (if (string? m)
583       (interpret-markup layout props
584        (list justify-string-markup m))
585       (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
586
587
588
589 (define-markup-command (combine layout props m1 m2) (markup? markup?)
590   "Print two markups on top of each other."
591   (let* ((s1 (interpret-markup layout props m1))
592          (s2 (interpret-markup layout props m2)))
593     (ly:stencil-add s1 s2)))
594
595 ;;
596 ;; TODO: should extract baseline-skip from each argument somehow..
597 ;; 
598 (define-markup-command (column layout props args) (markup-list?)
599   "Stack the markups in @var{args} vertically.  The property
600 @code{baseline-skip} determines the space between each markup in @var{args}."
601
602   (let*
603       ((arg-stencils (map (lambda (m) (interpret-markup layout props m)) args))
604        (skip (chain-assoc-get 'baseline-skip props)))
605
606     
607     (stack-lines
608      -1 0.0 skip
609      (remove ly:stencil-empty? arg-stencils))))
610
611
612 (define-markup-command (dir-column layout props args) (markup-list?)
613   "Make a column of args, going up or down, depending on the setting
614 of the @code{#'direction} layout property."
615   (let* ((dir (chain-assoc-get 'direction props)))
616     (stack-lines
617      (if (number? dir) dir -1)
618      0.0
619      (chain-assoc-get 'baseline-skip props)
620      (map (lambda (x) (interpret-markup layout props x)) args))))
621
622 (define-markup-command (center-align layout props args) (markup-list?)
623   "Put @code{args} in a centered column. "
624   (let* ((mols (map (lambda (x) (interpret-markup layout props x)) args))
625          (cmols (map (lambda (x) (ly:stencil-aligned-to x X CENTER)) mols)))
626     
627     (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) cmols)))
628
629 (define-markup-command (vcenter layout props arg) (markup?)
630   "Align @code{arg} to its Y center. "
631   (let* ((mol (interpret-markup layout props arg)))
632     (ly:stencil-aligned-to mol Y CENTER)))
633
634 (define-markup-command (hcenter layout props arg) (markup?)
635   "Align @code{arg} to its X center. "
636   (let* ((mol (interpret-markup layout props arg)))
637     (ly:stencil-aligned-to mol X CENTER)))
638
639 (define-markup-command (right-align layout props arg) (markup?)
640   "Align @var{arg} on its right edge. "
641   (let* ((m (interpret-markup layout props arg)))
642     (ly:stencil-aligned-to m X RIGHT)))
643
644 (define-markup-command (left-align layout props arg) (markup?)
645   "Align @var{arg} on its left edge. "
646   (let* ((m (interpret-markup layout props arg)))
647     (ly:stencil-aligned-to m X LEFT)))
648
649 (define-markup-command (general-align layout props axis dir arg)  (integer? number? markup?)
650   "Align @var{arg} in @var{axis} direction to the @var{dir} side."
651   (let* ((m (interpret-markup layout props arg)))
652     (ly:stencil-aligned-to m axis dir)))
653
654 (define-markup-command (halign layout props dir arg) (number? markup?)
655   "Set horizontal alignment. If @var{dir} is @code{-1}, then it is
656 left-aligned, while @code{+1} is right. Values in between interpolate
657 alignment accordingly."
658   (let* ((m (interpret-markup layout props arg)))
659     (ly:stencil-aligned-to m X dir)))
660
661
662
663 (define-markup-command (with-dimensions layout props x y arg) (number-pair? number-pair? markup?)
664   "Set the dimensions of @var{arg} to @var{x} and @var{y}."
665   
666   (let* ((m (interpret-markup layout props arg)))
667     (ly:make-stencil (ly:stencil-expr m) x y)))
668
669
670 (define-markup-command (pad-around layout props amount arg) (number? markup?)
671
672   "Add padding @var{amount} all around @var{arg}. "
673   
674   (let*
675       ((m (interpret-markup layout props arg))
676        (x (ly:stencil-extent m X))
677        (y (ly:stencil-extent m Y)))
678     
679        
680     (ly:make-stencil (ly:stencil-expr m)
681                      (interval-widen x amount)
682                      (interval-widen y amount))
683    ))
684
685
686 (define-markup-command (pad-x layout props amount arg) (number? markup?)
687
688   "Add padding @var{amount} around @var{arg} in the X-direction. "
689   (let*
690       ((m (interpret-markup layout props arg))
691        (x (ly:stencil-extent m X))
692        (y (ly:stencil-extent m Y)))
693     
694        
695     (ly:make-stencil (ly:stencil-expr m)
696                      (interval-widen x amount)
697                      y)
698    ))
699
700
701 (define-markup-command (put-adjacent layout props arg1 axis dir arg2) (markup? integer? ly:dir?  markup?)
702
703   "Put @var{arg2} next to @var{arg1}, without moving @var{arg1}.  "
704   
705   (let* ((m1 (interpret-markup layout props arg1))
706          (m2 (interpret-markup layout props arg2)))
707
708     (ly:stencil-combine-at-edge m1 axis dir m2 0.0 0.0)
709   ))
710
711 (define-markup-command (transparent layout props arg) (markup?)
712   "Make the argument transparent"
713   (let*
714       ((m (interpret-markup layout props arg))
715        (x (ly:stencil-extent m X))
716        (y (ly:stencil-extent m Y)))
717     
718
719     
720     (ly:make-stencil ""
721                      x y)))
722
723
724 (define-markup-command (pad-to-box layout props x-ext y-ext arg)
725   (number-pair? number-pair? markup?)
726   "Make @var{arg} take at least @var{x-ext}, @var{y-ext} space"
727
728   (let*
729       ((m (interpret-markup layout props arg))
730        (x (ly:stencil-extent m X))
731        (y (ly:stencil-extent m Y)))
732
733     (ly:make-stencil (ly:stencil-expr m)
734                      (interval-union x-ext x)
735                      (interval-union y-ext y))))
736
737
738 (define-markup-command (hcenter-in layout props length arg)
739   (number? markup?)
740   "Center @var{arg} horizontally within a box of extending
741 @var{length}/2 to the left and right."
742
743   (interpret-markup layout props
744                     (make-pad-to-box-markup
745                      (cons (/ length -2) (/ length 2))
746                      '(0 . 0)
747                      (make-hcenter-markup arg))))
748
749
750 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
751 ;; property
752 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
753
754 (define-markup-command (fromproperty layout props symbol) (symbol?)
755   "Read the @var{symbol} from property settings, and produce a stencil
756   from the markup contained within. If @var{symbol} is not defined, it
757   returns an empty markup"
758   (let* ((m (chain-assoc-get symbol props)))
759     (if (markup? m)
760         (interpret-markup layout props m)
761         (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
762
763
764 (define-markup-command (on-the-fly layout props procedure arg) (symbol? markup?)
765   "Apply the @var{procedure} markup command to
766 @var{arg}. @var{procedure} should take a single argument."
767   (let* ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
768     (set-object-property! anonymous-with-signature
769                           'markup-signature
770                           (list markup?))
771     (interpret-markup layout props (list anonymous-with-signature arg))))
772
773
774
775 (define-markup-command (override layout props new-prop arg) (pair? markup?)
776   "Add the first argument in to the property list.  Properties may be
777 any sort of property supported by @internalsref{font-interface} and
778 @internalsref{text-interface}, for example
779
780 @verbatim
781 \\override #'(font-family . married) \"bla\"
782 @end verbatim
783
784 "
785   (interpret-markup layout (cons (list new-prop) props) arg))
786
787 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
788 ;; files
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790
791 (define-markup-command (verbatim-file layout props name) (string?)
792   "Read the contents of a file, and include verbatimly"
793
794   (interpret-markup
795    layout props
796    (if  (ly:get-option 'safe)
797         "verbatim-file disabled in safe mode"
798         (let*
799             ((str (ly:gulp-file name))
800              (lines (string-split str #\nl)))
801
802           (make-typewriter-markup
803            (make-column-markup lines)))
804         )))
805
806 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
807 ;; fonts.
808 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
809
810
811 (define-markup-command (bigger layout props arg) (markup?)
812   "Increase the font size relative to current setting"
813   (interpret-markup layout props
814    `(,fontsize-markup 1 ,arg)))
815
816 (define-markup-command (smaller layout props arg) (markup?)
817   "Decrease the font size relative to current setting"
818   (interpret-markup layout props
819    `(,fontsize-markup -1 ,arg)))
820
821 (define-markup-command larger (markup?) bigger-markup)
822
823 (define-markup-command (finger layout props arg) (markup?)
824   "Set the argument as small numbers."
825   (interpret-markup layout
826                     (cons '((font-size . -5) (font-encoding . fetaNumber)) props)
827                     arg))
828
829
830 (define-markup-command (fontsize layout props increment arg) (number? markup?)
831   "Add @var{increment} to the font-size. Adjust baseline skip accordingly."
832
833   (let* ((fs (chain-assoc-get 'font-size props 0))
834          (bs (chain-assoc-get 'baseline-skip props 2)) 
835          (entries (list
836                    (cons 'baseline-skip (* bs (magstep increment)))
837                    (cons 'font-size (+ fs increment )))))
838
839     (interpret-markup layout (cons entries props) arg)))
840   
841
842
843 ;; FIXME -> should convert to font-size.
844 (define-markup-command (magnify layout props sz arg) (number? markup?)
845   "Set the font magnification for the its argument. In the following
846 example, the middle A will be 10% larger:
847 @example
848 A \\magnify #1.1 @{ A @} A
849 @end example
850
851 Note: magnification only works if a font-name is explicitly selected.
852 Use @code{\\fontsize} otherwise."
853   (interpret-markup
854    layout 
855    (prepend-alist-chain 'font-magnification sz props)
856    arg))
857
858 (define-markup-command (bold layout props arg) (markup?)
859   "Switch to bold font-series"
860   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
861
862 (define-markup-command (sans layout props arg) (markup?)
863   "Switch to the sans serif family"
864   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
865
866 (define-markup-command (number layout props arg) (markup?)
867   "Set font family to @code{number}, which yields the font used for
868 time signatures and fingerings.  This font only contains numbers and
869 some punctuation. It doesn't have any letters.  "
870   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
871
872 (define-markup-command (roman layout props arg) (markup?)
873   "Set font family to @code{roman}."
874   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
875
876 (define-markup-command (huge layout props arg) (markup?)
877   "Set font size to +2."
878   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
879
880 (define-markup-command (large layout props arg) (markup?)
881   "Set font size to +1."
882   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
883
884 (define-markup-command (normalsize layout props arg) (markup?)
885   "Set font size to default."
886   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
887
888 (define-markup-command (small layout props arg) (markup?)
889   "Set font size to -1."
890   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
891
892 (define-markup-command (tiny layout props arg) (markup?)
893   "Set font size to -2."
894   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
895
896 (define-markup-command (teeny layout props arg) (markup?)
897   "Set font size to -3."
898   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
899
900 (define-markup-command (fontCaps layout props arg) (markup?)
901   "Set @code{font-shape} to @code{caps}."
902   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
903
904 ;; Poor man's caps
905 (define-markup-command (smallCaps layout props text) (markup?)
906   "Turn @code{text}, which should be a string, to small caps.
907 @example
908 \\markup \\smallCaps \"Text between double quotes\"
909 @end example
910 "
911   (define (make-small-caps-markup chars)
912     (cond ((null? chars)
913            (markup))
914           ((char-whitespace? (car chars))
915            (markup #:fontsize -2 #:simple (string-upcase (list->string (cdr chars)))))
916           (else
917            (markup #:hspace -1
918                    #:fontsize -2 #:simple (string-upcase (list->string chars))))))
919   (define (make-not-small-caps-markup chars)
920     (cond ((null? chars)
921            (markup))
922           ((char-whitespace? (car chars))
923            (markup #:simple (list->string (cdr chars))))
924           (else
925            (markup #:hspace -1
926                    #:simple (list->string chars)))))
927   (define (small-caps-aux done-markups current-chars rest-chars small? after-space?)
928     (cond ((null? rest-chars)
929            ;; the end of the string: build the markup
930            (make-line-markup (reverse! (cons ((if small?
931                                                   make-small-caps-markup
932                                                   make-not-small-caps-markup)
933                                               (reverse! current-chars))
934                                              done-markups))))
935           ((char-whitespace? (car rest-chars))
936            ;; a space char.
937            (small-caps-aux done-markups current-chars (cdr rest-chars) small? #t))
938           ((or (and small? (char-lower-case? (car rest-chars)))
939                (and (not small?) (not (char-lower-case? (car rest-chars)))))
940            ;; same case
941            ;; add the char to the current char list
942            (small-caps-aux done-markups
943                            (cons (car rest-chars)
944                                  (if after-space? 
945                                      (cons #\space current-chars)
946                                      current-chars))
947                            (cdr rest-chars) 
948                            small?
949                            #f))
950           (else
951            ;; case change
952            ;; make a markup with current chars, and start a new list with new char
953            (small-caps-aux (cons ((if small?
954                                       make-small-caps-markup
955                                       make-not-small-caps-markup)
956                                   (reverse! current-chars))
957                                  done-markups)
958                            (if after-space?
959                                (list (car rest-chars) #\space)
960                                (list (car rest-chars)))
961                            (cdr rest-chars)
962                            (not small?)
963                            #f))))
964   (interpret-markup layout props (small-caps-aux (list) 
965                                                  (list) 
966                                                  (cons #\space (string->list text))
967                                                  #f
968                                                  #f)))
969
970 (define-markup-command (caps layout props arg) (markup?)
971   (interpret-markup layout props (make-smallCaps-markup arg)))
972
973 (define-markup-command (dynamic layout props arg) (markup?)
974   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
975 @b{z}, @b{p}, and @b{r}.  When producing phrases, like ``pi@`{u} @b{f}'', the
976 normal words (like ``pi@`{u}'') should be done in a different font.  The
977 recommend font for this is bold and italic"
978   (interpret-markup
979    layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
980
981 (define-markup-command (text layout props arg) (markup?)
982   "Use a text font instead of music symbol or music alphabet font."  
983
984   ;; ugh - latin1
985   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
986                     arg))
987
988
989 (define-markup-command (italic layout props arg) (markup?)
990   "Use italic @code{font-shape} for @var{arg}. "
991   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
992
993 (define-markup-command (typewriter layout props arg) (markup?)
994   "Use @code{font-family} typewriter for @var{arg}."
995   (interpret-markup
996    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
997
998 (define-markup-command (upright layout props arg) (markup?)
999   "Set font shape to @code{upright}.  This is the opposite of @code{italic}."
1000   (interpret-markup
1001    layout (prepend-alist-chain 'font-shape 'upright props) arg))
1002
1003 (define-markup-command (medium layout props arg) (markup?)
1004   "Switch to medium font-series (in contrast to bold)."
1005   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
1006                     arg))
1007
1008 (define-markup-command (normal-text layout props arg) (markup?)
1009   "Set all font related properties (except the size) to get the default normal text font, no matter what font was used earlier."
1010   ;; ugh - latin1
1011   (interpret-markup layout
1012                     (cons '((font-family . roman) (font-shape . upright)
1013                             (font-series . medium) (font-encoding . latin1))
1014                           props)
1015                     arg))
1016
1017 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1018 ;; symbols.
1019 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1020
1021 (define-markup-command (doublesharp layout props) ()
1022   "Draw a double sharp symbol."
1023
1024   (interpret-markup layout props (markup #:musicglyph "accidentals.4")))
1025
1026 (define-markup-command (sesquisharp layout props) ()
1027   "Draw a 3/2 sharp symbol."
1028   (interpret-markup layout props (markup #:musicglyph "accidentals.3")))
1029
1030 (define-markup-command (sharp layout props) ()
1031   "Draw a sharp symbol."
1032   (interpret-markup layout props (markup #:musicglyph "accidentals.2")))
1033
1034 (define-markup-command (semisharp layout props) ()
1035   "Draw a semi sharp symbol."
1036   (interpret-markup layout props (markup #:musicglyph "accidentals.1")))
1037
1038 (define-markup-command (natural layout props) ()
1039   "Draw a natural symbol."
1040   (interpret-markup layout props (markup #:musicglyph "accidentals.0")))
1041
1042 (define-markup-command (semiflat layout props) ()
1043   "Draw a semiflat."
1044   (interpret-markup layout props (markup #:musicglyph "accidentals.M1")))
1045
1046 (define-markup-command (flat layout props) ()
1047   "Draw a flat symbol."
1048   (interpret-markup layout props (markup #:musicglyph "accidentals.M2")))
1049
1050 (define-markup-command (sesquiflat layout props) ()
1051   "Draw a 3/2 flat symbol."
1052   (interpret-markup layout props (markup #:musicglyph "accidentals.M3")))
1053
1054 (define-markup-command (doubleflat layout props) ()
1055   "Draw a double flat symbol."
1056   (interpret-markup layout props (markup #:musicglyph "accidentals.M4")))
1057
1058 (define-markup-command (with-color layout props color arg) (color? markup?)
1059   "Draw @var{arg} in color specified by @var{color}"
1060
1061   (let* ((stil (interpret-markup layout props arg)))
1062
1063     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
1064                      (ly:stencil-extent stil X)
1065                      (ly:stencil-extent stil Y))))
1066
1067 \f
1068 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1069 ;; glyphs
1070 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1071
1072
1073 (define-markup-command (arrow-head layout props axis direction filled)
1074   (integer? ly:dir? boolean?)
1075   "produce an arrow head in specified direction and axis. Use the filled head if @var{filled} is  specified."
1076   (let*
1077       ((name (format "arrowheads.~a.~a~a"
1078                      (if filled
1079                          "close"
1080                          "open")
1081                      axis
1082                      direction)))
1083     (ly:font-get-glyph
1084      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1085                                      props))
1086      name)))
1087
1088 (define-markup-command (musicglyph layout props glyph-name) (string?)
1089   "This is converted to a musical symbol, e.g. @code{\\musicglyph
1090 #\"accidentals.0\"} will select the natural sign from the music font.
1091 See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
1092   (ly:font-get-glyph
1093    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1094                                    props))
1095    glyph-name))
1096
1097 (define-markup-command (lookup layout props glyph-name) (string?)
1098   "Lookup a glyph by name."
1099   (ly:font-get-glyph (ly:paper-get-font layout props)
1100                      glyph-name))
1101
1102 (define-markup-command (char layout props num) (integer?)
1103   "Produce a single character, e.g. @code{\\char #65} produces the 
1104 letter 'A'."
1105
1106   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
1107
1108 (define number->mark-letter-vector (make-vector 25 #\A))
1109
1110 (do ((i 0 (1+ i))
1111      (j 0 (1+ j)))
1112     ((>= i 26))
1113   (if (= i (- (char->integer #\I) (char->integer #\A)))
1114       (set! i (1+ i)))
1115   (vector-set! number->mark-letter-vector j
1116                (integer->char (+ i (char->integer #\A)))))
1117
1118 (define number->mark-alphabet-vector (list->vector
1119   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
1120
1121 (define (number->markletter-string vec n)
1122   "Double letters for big marks."
1123   (let* ((lst (vector-length vec)))
1124     
1125     (if (>= n lst)
1126         (string-append (number->markletter-string vec (1- (quotient n lst)))
1127                        (number->markletter-string vec (remainder n lst)))
1128         (make-string 1 (vector-ref vec n)))))
1129
1130 (define-markup-command (markletter layout props num) (integer?)
1131   "Make a markup letter for @var{num}.  The letters start with A to Z
1132  (skipping I), and continues with double letters."
1133   (ly:text-interface::interpret-markup layout props
1134     (number->markletter-string number->mark-letter-vector num)))
1135
1136 (define-markup-command (markalphabet layout props num) (integer?)
1137    "Make a markup letter for @var{num}.  The letters start with A to Z
1138  and continues with double letters."
1139    (ly:text-interface::interpret-markup layout props
1140      (number->markletter-string number->mark-alphabet-vector num)))
1141
1142
1143
1144 (define-markup-command (slashed-digit layout props num) (integer?)
1145   "A feta number, with slash. This is for use in the context of
1146 figured bass notation"
1147   (let*
1148       ((mag (magstep (chain-assoc-get 'font-size props 0)))
1149        (thickness
1150         (* mag
1151            (chain-assoc-get 'thickness props 0.16)))
1152        (dy (* mag 0.15))
1153        (number-stencil (interpret-markup layout
1154                                          (prepend-alist-chain 'font-encoding 'fetaNumber props)
1155                                          (number->string num)))
1156        (num-x (interval-widen (ly:stencil-extent number-stencil X)
1157                               (* mag 0.2)))
1158        (num-y (ly:stencil-extent number-stencil Y))
1159        (slash-stencil 
1160         (ly:make-stencil
1161          `(draw-line
1162            ,thickness
1163            ,(car num-x) ,(- (interval-center num-y) dy)
1164            ,(cdr num-x) ,(+ (interval-center num-y) dy))
1165          num-x num-y
1166          )))
1167
1168     (ly:stencil-add number-stencil
1169                     (cond
1170                      ((= num 5) (ly:stencil-translate slash-stencil
1171                                                       ;;(cons (* mag -0.05) (* mag 0.42))
1172                                                       (cons (* mag -0.00) (* mag -0.07))
1173
1174                                                       ))
1175                      ((= num 7) (ly:stencil-translate slash-stencil
1176                                                       ;;(cons (* mag -0.05) (* mag 0.42))
1177                                                       (cons (* mag -0.00) (* mag -0.15))
1178
1179                                                       ))
1180                      
1181                      (else slash-stencil)))
1182     ))
1183 \f
1184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1185 ;; the note command.
1186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1187
1188
1189 ;; TODO: better syntax.
1190
1191 (define-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
1192   "Construct a note symbol, with stem.  By using fractional values for
1193 @var{dir}, you can obtain longer or shorter stems."
1194
1195   (define (get-glyph-name-candidates dir log style)
1196     (map (lambda (dir-name)
1197      (format "noteheads.~a~a~a" dir-name (min log 2)
1198              (if (and (symbol? style)
1199                       (not (equal? 'default style)))
1200                  (symbol->string style)
1201                  "")))
1202          (list (if (= dir UP) "u" "d")
1203                "s")))
1204                    
1205   (define (get-glyph-name font cands)
1206     (if (null? cands)
1207      ""
1208      (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
1209          (get-glyph-name font (cdr cands))
1210          (car cands))))
1211     
1212   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
1213          (size-factor (magstep (chain-assoc-get 'font-size props 0)))
1214          (style (chain-assoc-get 'style props '()))
1215          (stem-length (*  size-factor (max 3 (- log 1))))
1216          (head-glyph-name (get-glyph-name font (get-glyph-name-candidates (sign dir) log style)))
1217          (head-glyph (ly:font-get-glyph font head-glyph-name))
1218          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
1219          (stem-thickness (* size-factor 0.13))
1220          (stemy (* dir stem-length))
1221          (attach-off (cons (interval-index
1222                             (ly:stencil-extent head-glyph X)
1223                             (* (sign dir) (car attach-indices)))
1224                            (* (sign dir)        ; fixme, this is inconsistent between X & Y.
1225                               (interval-index
1226                                (ly:stencil-extent head-glyph Y)
1227                                (cdr attach-indices)))))
1228          (stem-glyph (and (> log 0)
1229                           (ly:round-filled-box
1230                            (ordered-cons (car attach-off)
1231                                          (+ (car attach-off)  (* (- (sign dir)) stem-thickness)))
1232                            (cons (min stemy (cdr attach-off))
1233                                  (max stemy (cdr attach-off)))
1234                            (/ stem-thickness 3))))
1235          
1236          (dot (ly:font-get-glyph font "dots.dot"))
1237          (dotwid (interval-length (ly:stencil-extent dot X)))
1238          (dots (and (> dot-count 0)
1239                     (apply ly:stencil-add
1240                            (map (lambda (x)
1241                                   (ly:stencil-translate-axis
1242                                    dot (* 2 x dotwid) X))
1243                                 (iota dot-count)))))
1244          (flaggl (and (> log 2)
1245                       (ly:stencil-translate
1246                        (ly:font-get-glyph font
1247                                           (string-append "flags."
1248                                                          (if (> dir 0) "u" "d")
1249                                                          (number->string log)))
1250                        (cons (+ (car attach-off) (/ stem-thickness 2)) stemy)))))
1251
1252     (if (and dots flaggl (> dir 0))
1253         (set! dots (ly:stencil-translate-axis dots 0.35 X)))
1254     (if flaggl
1255         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
1256     (if (ly:stencil? stem-glyph)
1257         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
1258         (set! stem-glyph head-glyph))
1259     (if (ly:stencil? dots)
1260         (set! stem-glyph
1261               (ly:stencil-add
1262                (ly:stencil-translate-axis
1263                 dots
1264                 (+ (cdr (ly:stencil-extent head-glyph X)) 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