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