]> 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 (assoc-get 1 standard-alteration-glyph-name-alist ""))))
1036
1037 (define-builtin-markup-command (sesquisharp layout props) ()
1038   "Draw a 3/2 sharp symbol."
1039   (interpret-markup layout props (markup #:musicglyph (assoc-get 3/4 standard-alteration-glyph-name-alist ""))))
1040                                          
1041
1042 (define-builtin-markup-command (sharp layout props) ()
1043   "Draw a sharp symbol."
1044   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/2 standard-alteration-glyph-name-alist ""))))
1045
1046 (define-builtin-markup-command (semisharp layout props) ()
1047   "Draw a semi sharp symbol."
1048   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/4 standard-alteration-glyph-name-alist ""))))
1049
1050 (define-builtin-markup-command (natural layout props) ()
1051   "Draw a natural symbol."
1052   (interpret-markup layout props (markup #:musicglyph (assoc-get 0 standard-alteration-glyph-name-alist ""))))
1053
1054 (define-builtin-markup-command (semiflat layout props) ()
1055   "Draw a semiflat."
1056   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/4 standard-alteration-glyph-name-alist ""))))
1057
1058 (define-builtin-markup-command (flat layout props) ()
1059   "Draw a flat symbol."
1060   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/2 standard-alteration-glyph-name-alist ""))))
1061
1062 (define-builtin-markup-command (sesquiflat layout props) ()
1063   "Draw a 3/2 flat symbol."
1064   (interpret-markup layout props (markup #:musicglyph (assoc-get -3/4 standard-alteration-glyph-name-alist ""))))
1065
1066 (define-builtin-markup-command (doubleflat layout props) ()
1067   "Draw a double flat symbol."
1068   (interpret-markup layout props (markup #:musicglyph (assoc-get -1 standard-alteration-glyph-name-alist ""))))
1069
1070 (define-builtin-markup-command (with-color layout props color arg) (color? markup?)
1071   "Draw @var{arg} in color specified by @var{color}"
1072
1073   (let* ((stil (interpret-markup layout props arg)))
1074
1075     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
1076                      (ly:stencil-extent stil X)
1077                      (ly:stencil-extent stil Y))))
1078
1079 \f
1080 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1081 ;; glyphs
1082 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1083
1084
1085 (define-builtin-markup-command (arrow-head layout props axis direction filled)
1086   (integer? ly:dir? boolean?)
1087   "produce an arrow head in specified direction and axis. Use the filled head if @var{filled} is  specified."
1088   (let*
1089       ((name (format "arrowheads.~a.~a~a"
1090                      (if filled
1091                          "close"
1092                          "open")
1093                      axis
1094                      direction)))
1095     (ly:font-get-glyph
1096      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1097                                      props))
1098      name)))
1099
1100 (define-builtin-markup-command (musicglyph layout props glyph-name) (string?)
1101   "This is converted to a musical symbol, e.g. @code{\\musicglyph
1102 #\"accidentals.natural\"} will select the natural sign from the music font.
1103 See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
1104   (ly:font-get-glyph
1105    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1106                                    props))
1107    glyph-name))
1108
1109 (define-builtin-markup-command (lookup layout props glyph-name) (string?)
1110   "Lookup a glyph by name."
1111   (ly:font-get-glyph (ly:paper-get-font layout props)
1112                      glyph-name))
1113
1114 (define-builtin-markup-command (char layout props num) (integer?)
1115   "Produce a single character, e.g. @code{\\char #65} produces the 
1116 letter 'A'."
1117
1118   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
1119
1120 (define number->mark-letter-vector (make-vector 25 #\A))
1121
1122 (do ((i 0 (1+ i))
1123      (j 0 (1+ j)))
1124     ((>= i 26))
1125   (if (= i (- (char->integer #\I) (char->integer #\A)))
1126       (set! i (1+ i)))
1127   (vector-set! number->mark-letter-vector j
1128                (integer->char (+ i (char->integer #\A)))))
1129
1130 (define number->mark-alphabet-vector (list->vector
1131   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
1132
1133 (define (number->markletter-string vec n)
1134   "Double letters for big marks."
1135   (let* ((lst (vector-length vec)))
1136     
1137     (if (>= n lst)
1138         (string-append (number->markletter-string vec (1- (quotient n lst)))
1139                        (number->markletter-string vec (remainder n lst)))
1140         (make-string 1 (vector-ref vec n)))))
1141
1142 (define-builtin-markup-command (markletter layout props num) (integer?)
1143   "Make a markup letter for @var{num}.  The letters start with A to Z
1144  (skipping I), and continues with double letters."
1145   (ly:text-interface::interpret-markup layout props
1146     (number->markletter-string number->mark-letter-vector num)))
1147
1148 (define-builtin-markup-command (markalphabet layout props num) (integer?)
1149    "Make a markup letter for @var{num}.  The letters start with A to Z
1150  and continues with double letters."
1151    (ly:text-interface::interpret-markup layout props
1152      (number->markletter-string number->mark-alphabet-vector num)))
1153
1154
1155
1156 (define-builtin-markup-command (slashed-digit layout props num) (integer?)
1157   "A feta number, with slash. This is for use in the context of
1158 figured bass notation"
1159   (let*
1160       ((mag (magstep (chain-assoc-get 'font-size props 0)))
1161        (thickness
1162         (* mag
1163            (chain-assoc-get 'thickness props 0.16)))
1164        (dy (* mag 0.15))
1165        (number-stencil (interpret-markup layout
1166                                          (prepend-alist-chain 'font-encoding 'fetaNumber props)
1167                                          (number->string num)))
1168        (num-x (interval-widen (ly:stencil-extent number-stencil X)
1169                               (* mag 0.2)))
1170        (num-y (ly:stencil-extent number-stencil Y))
1171        (is-sane (and (interval-sane? num-x) (interval-sane? num-y)))
1172        
1173        (slash-stencil
1174         (if is-sane
1175             (ly:make-stencil
1176              `(draw-line
1177                ,thickness
1178                ,(car num-x) ,(- (interval-center num-y) dy)
1179                ,(cdr num-x) ,(+ (interval-center num-y) dy))
1180              num-x num-y)
1181             #f)))
1182
1183     (set! slash-stencil
1184           (cond
1185            ((not (ly:stencil? slash-stencil)) #f)
1186            ((= num 5) (ly:stencil-translate slash-stencil
1187                                             ;;(cons (* mag -0.05) (* mag 0.42))
1188                                             (cons (* mag -0.00) (* mag -0.07))
1189
1190                                             ))
1191            ((= num 7) (ly:stencil-translate slash-stencil
1192                                             ;;(cons (* mag -0.05) (* mag 0.42))
1193                                             (cons (* mag -0.00) (* mag -0.15))
1194
1195                                             ))
1196            
1197            (else slash-stencil)))
1198
1199     (if slash-stencil
1200         (set! number-stencil
1201               (ly:stencil-add number-stencil slash-stencil))
1202         
1203         (ly:warning "invalid number for slashed digit ~a" num))
1204
1205
1206     number-stencil))
1207 \f
1208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1209 ;; the note command.
1210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1211
1212
1213 ;; TODO: better syntax.
1214
1215 (define-builtin-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
1216   "Construct a note symbol, with stem.  By using fractional values for
1217 @var{dir}, you can obtain longer or shorter stems."
1218
1219   (define (get-glyph-name-candidates dir log style)
1220     (map (lambda (dir-name)
1221      (format "noteheads.~a~a~a" dir-name (min log 2)
1222              (if (and (symbol? style)
1223                       (not (equal? 'default style)))
1224                  (symbol->string style)
1225                  "")))
1226          (list (if (= dir UP) "u" "d")
1227                "s")))
1228                    
1229   (define (get-glyph-name font cands)
1230     (if (null? cands)
1231      ""
1232      (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
1233          (get-glyph-name font (cdr cands))
1234          (car cands))))
1235     
1236   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
1237          (size-factor (magstep (chain-assoc-get 'font-size props 0)))
1238          (style (chain-assoc-get 'style props '()))
1239          (stem-length (*  size-factor (max 3 (- log 1))))
1240          (head-glyph-name (get-glyph-name font (get-glyph-name-candidates (sign dir) log style)))
1241          (head-glyph (ly:font-get-glyph font head-glyph-name))
1242          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
1243          (stem-thickness (* size-factor 0.13))
1244          (stemy (* dir stem-length))
1245          (attach-off (cons (interval-index
1246                             (ly:stencil-extent head-glyph X)
1247                             (* (sign dir) (car attach-indices)))
1248                            (* (sign dir)        ; fixme, this is inconsistent between X & Y.
1249                               (interval-index
1250                                (ly:stencil-extent head-glyph Y)
1251                                (cdr attach-indices)))))
1252          (stem-glyph (and (> log 0)
1253                           (ly:round-filled-box
1254                            (ordered-cons (car attach-off)
1255                                          (+ (car attach-off)  (* (- (sign dir)) stem-thickness)))
1256                            (cons (min stemy (cdr attach-off))
1257                                  (max stemy (cdr attach-off)))
1258                            (/ stem-thickness 3))))
1259          
1260          (dot (ly:font-get-glyph font "dots.dot"))
1261          (dotwid (interval-length (ly:stencil-extent dot X)))
1262          (dots (and (> dot-count 0)
1263                     (apply ly:stencil-add
1264                            (map (lambda (x)
1265                                   (ly:stencil-translate-axis
1266                                    dot  (* (+ 1 (* 2 x)) dotwid) X))
1267                                 (iota dot-count 1)))))
1268          (flaggl (and (> log 2)
1269                       (ly:stencil-translate
1270                        (ly:font-get-glyph font
1271                                           (string-append "flags."
1272                                                          (if (> dir 0) "u" "d")
1273                                                          (number->string log)))
1274                        (cons (+ (car attach-off) (/ stem-thickness 2)) stemy)))))
1275     (if flaggl
1276         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
1277     (if (ly:stencil? stem-glyph)
1278         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
1279         (set! stem-glyph head-glyph))
1280     (if (ly:stencil? dots)
1281         (set! stem-glyph
1282               (ly:stencil-add
1283                (ly:stencil-translate-axis
1284                 dots
1285                 (+ (if (and (> dir 0) (> log 2))
1286                        (* 1.5 dotwid)
1287                        0)
1288                    ;; huh ? why not necessary?
1289                    ;;(cdr (ly:stencil-extent head-glyph X))
1290                    dotwid)
1291                 X)
1292                stem-glyph)))
1293     stem-glyph))
1294
1295 (define-public log2 
1296   (let ((divisor (log 2)))
1297     (lambda (z) (inexact->exact (/ (log z) divisor)))))
1298
1299 (define (parse-simple-duration duration-string)
1300   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
1301   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
1302     (if (and match (string=? duration-string (match:substring match 0)))
1303         (let ((len  (match:substring match 1))
1304               (dots (match:substring match 2)))
1305           (list (cond ((string=? len "breve") -1)
1306                       ((string=? len "longa") -2)
1307                       ((string=? len "maxima") -3)
1308                       (else (log2 (string->number len))))
1309                 (if dots (string-length dots) 0)))
1310         (ly:error (_ "not a valid duration string: ~a") duration-string))))
1311
1312 (define-builtin-markup-command (note layout props duration dir) (string? number?)
1313   "This produces a note with a stem pointing in @var{dir} direction, with
1314 the @var{duration} for the note head type and augmentation dots. For
1315 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
1316 a shortened down stem."
1317   (let ((parsed (parse-simple-duration duration)))
1318     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
1319
1320 \f
1321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1322 ;; translating.
1323 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1324
1325 (define-builtin-markup-command (lower layout props amount arg) (number? markup?)
1326   "
1327 Lower @var{arg}, by the distance @var{amount}.
1328 A negative @var{amount} indicates raising, see also @code{\\raise}.
1329 "
1330   (ly:stencil-translate-axis (interpret-markup layout props arg)
1331                              (- amount) Y))
1332
1333
1334 (define-builtin-markup-command (translate-scaled layout props offset arg) (number-pair? markup?)
1335   "Translate @var{arg} by @var{offset}, scaling the offset by the @code{font-size}."
1336
1337   (let*
1338       ((factor (magstep (chain-assoc-get 'font-size props 0)))
1339        (scaled (cons (* factor (car offset))
1340                      (* factor (cdr offset)))))
1341     
1342   (ly:stencil-translate (interpret-markup layout props arg)
1343                         scaled)))
1344
1345 (define-builtin-markup-command (raise layout props amount arg) (number? markup?)
1346   "
1347 Raise @var{arg}, by the distance @var{amount}.
1348 A negative @var{amount} indicates lowering, see also @code{\\lower}.
1349 @c
1350 @lilypond[verbatim,fragment,relative=1]
1351  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
1352 @end lilypond
1353 The argument to @code{\\raise} is the vertical displacement amount,
1354 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
1355 raise objects in relation to their surrounding markups.
1356
1357 If the text object itself is positioned above or below the staff, then
1358 @code{\\raise} cannot be used to move it, since the mechanism that
1359 positions it next to the staff cancels any shift made with
1360 @code{\\raise}. For vertical positioning, use the @code{padding}
1361 and/or @code{extra-offset} properties. "
1362   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
1363
1364 (define-builtin-markup-command (fraction layout props arg1 arg2) (markup? markup?)
1365   "Make a fraction of two markups."
1366   (let* ((m1 (interpret-markup layout props arg1))
1367          (m2 (interpret-markup layout props arg2))
1368          (factor (magstep (chain-assoc-get 'font-size props 0)))
1369          (boxdimen (cons (* factor -0.05) (* factor 0.05)))
1370          (padding (* factor 0.2))
1371          (baseline (* factor 0.6))
1372          (offset (* factor 0.75)))
1373     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
1374     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
1375     (let* ((x1 (ly:stencil-extent m1 X))
1376            (x2 (ly:stencil-extent m2 X))
1377            (line (ly:round-filled-box (interval-union x1 x2) boxdimen 0.0))
1378            ;; should stack mols separately, to maintain LINE on baseline
1379            (stack (stack-lines DOWN padding baseline (list m1 line m2))))
1380       (set! stack
1381             (ly:stencil-aligned-to stack Y CENTER))
1382       (set! stack
1383             (ly:stencil-aligned-to stack X LEFT))
1384       ;; should have EX dimension
1385       ;; empirical anyway
1386       (ly:stencil-translate-axis stack offset Y))))
1387
1388
1389
1390
1391
1392 (define-builtin-markup-command (normal-size-super layout props arg) (markup?)
1393   "Set @var{arg} in superscript with a normal font size."
1394   (ly:stencil-translate-axis
1395    (interpret-markup layout props arg)
1396    (* 0.5 (chain-assoc-get 'baseline-skip props)) Y))
1397
1398 (define-builtin-markup-command (super layout props arg) (markup?)
1399   "
1400 @cindex raising text
1401 @cindex lowering text
1402 @cindex moving text
1403 @cindex translating text
1404
1405 @cindex @code{\\super}
1406
1407
1408 Raising and lowering texts can be done with @code{\\super} and
1409 @code{\\sub}:
1410
1411 @lilypond[verbatim,fragment,relative=1]
1412  c1^\\markup { E \"=\" mc \\super \"2\" }
1413 @end lilypond
1414
1415 "
1416   (ly:stencil-translate-axis
1417    (interpret-markup
1418     layout
1419     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1420     arg)
1421    (* 0.5 (chain-assoc-get 'baseline-skip props))
1422    Y))
1423
1424 (define-builtin-markup-command (translate layout props offset arg) (number-pair? markup?)
1425   "This translates an object. Its first argument is a cons of numbers
1426 @example
1427 A \\translate #(cons 2 -3) @{ B C @} D
1428 @end example
1429 This moves `B C' 2 spaces to the right, and 3 down, relative to its
1430 surroundings. This command cannot be used to move isolated scripts
1431 vertically, for the same reason that @code{\\raise} cannot be used for
1432 that.
1433
1434 "
1435   (ly:stencil-translate (interpret-markup  layout props arg)
1436                         offset))
1437
1438 (define-builtin-markup-command (sub layout props arg) (markup?)
1439   "Set @var{arg} in subscript."
1440   (ly:stencil-translate-axis
1441    (interpret-markup
1442     layout
1443     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1444     arg)
1445    (* -0.5 (chain-assoc-get 'baseline-skip props))
1446    Y))
1447
1448 (define-builtin-markup-command (normal-size-sub layout props arg) (markup?)
1449   "Set @var{arg} in subscript, in a normal font size."
1450   (ly:stencil-translate-axis
1451    (interpret-markup layout props arg)
1452    (* -0.5 (chain-assoc-get 'baseline-skip props))
1453    Y))
1454 \f
1455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1456 ;; brackets.
1457 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1458
1459 (define-builtin-markup-command (hbracket layout props arg) (markup?)
1460   "Draw horizontal brackets around @var{arg}."  
1461   (let ((th 0.1) ;; todo: take from GROB.
1462         (m (interpret-markup layout props arg)))
1463     (bracketify-stencil m X th (* 2.5 th) th)))
1464
1465 (define-builtin-markup-command (bracket layout props arg) (markup?)
1466   "Draw vertical brackets around @var{arg}."  
1467   (let ((th 0.1) ;; todo: take from GROB.
1468         (m (interpret-markup layout props arg)))
1469     (bracketify-stencil m Y th (* 2.5 th) th)))
1470
1471 (define-builtin-markup-command (bracketed-y-column layout props indices args)
1472   (list? markup-list?)
1473   "Make a column of the markups in @var{args}, putting brackets around
1474 the elements marked in @var{indices}, which is a list of numbers.
1475
1476 "
1477 ;;
1478 ;; DROPME? This command is a relic from the old figured bass implementation.
1479 ;;
1480   
1481   (define (sublist lst start stop)
1482     (take (drop lst start) (- (1+ stop) start)))
1483
1484   (define (stencil-list-extent ss axis)
1485     (cons
1486      (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
1487      (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
1488   
1489
1490   (define (stack-stencils-vertically stencils bskip last-stencil)
1491     (cond
1492      ((null? stencils) '())
1493      ((not (ly:stencil? last-stencil))
1494       (cons (car stencils)
1495             (stack-stencils-vertically (cdr stencils) bskip (car stencils))))
1496      (else
1497       (let* ((orig (car stencils))
1498              (dir (chain-assoc-get 'direction  props DOWN))
1499              (new (ly:stencil-moved-to-edge last-stencil Y dir
1500                                             orig
1501                                             0.1 bskip)))
1502
1503         (cons new (stack-stencils-vertically (cdr stencils) bskip new))))))
1504
1505   (define (make-brackets stencils indices acc)
1506     (if (and stencils
1507              (pair? indices)
1508              (pair? (cdr indices)))
1509         (let* ((encl (sublist stencils (car indices) (cadr indices)))
1510                (x-ext (stencil-list-extent encl X))
1511                (y-ext (stencil-list-extent encl Y))
1512                (thick 0.10)
1513                (pad 0.35)
1514                (protusion (* 2.5 thick))
1515                (lb
1516                 (ly:stencil-translate-axis 
1517                  (ly:bracket Y y-ext thick protusion)
1518                  (- (car x-ext) pad) X))
1519                (rb (ly:stencil-translate-axis
1520                     (ly:bracket Y y-ext thick (- protusion))
1521                     (+ (cdr x-ext) pad) X)))
1522
1523           (make-brackets
1524            stencils (cddr indices)
1525            (append
1526             (list lb rb)
1527             acc)))
1528         acc))
1529
1530   (let* ((stencils
1531           (map (lambda (x)
1532                  (interpret-markup
1533                   layout
1534                   props
1535                   x)) args))
1536          (leading
1537           (chain-assoc-get 'baseline-skip props))
1538          (stacked (stack-stencils-vertically
1539                    (remove ly:stencil-empty? stencils) 1.25 #f))
1540          (brackets (make-brackets stacked indices '())))
1541
1542     (apply ly:stencil-add
1543            (append stacked brackets))))
1544 \f
1545
1546 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1547 ;; size indications arrow
1548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1549