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