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