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