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