]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
Merge with master
[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)
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 (define-builtin-markup-command (magnify layout props sz arg) (number? markup?)
853   "Set the font magnification for the its argument. In the following
854 example, the middle A will be 10% larger:
855 @example
856 A \\magnify #1.1 @{ A @} A
857 @end example
858
859 Note: magnification only works if a font-name is explicitly selected.
860 Use @code{\\fontsize} otherwise."
861   (interpret-markup
862    layout 
863    (prepend-alist-chain 'font-size (magnification->font-size sz) props)
864    arg))
865
866 (define-builtin-markup-command (bold layout props arg) (markup?)
867   "Switch to bold font-series"
868   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
869
870 (define-builtin-markup-command (sans layout props arg) (markup?)
871   "Switch to the sans serif family"
872   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
873
874 (define-builtin-markup-command (number layout props arg) (markup?)
875   "Set font family to @code{number}, which yields the font used for
876 time signatures and fingerings.  This font only contains numbers and
877 some punctuation. It doesn't have any letters.  "
878   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
879
880 (define-builtin-markup-command (roman layout props arg) (markup?)
881   "Set font family to @code{roman}."
882   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
883
884 (define-builtin-markup-command (huge layout props arg) (markup?)
885   "Set font size to +2."
886   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
887
888 (define-builtin-markup-command (large layout props arg) (markup?)
889   "Set font size to +1."
890   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
891
892 (define-builtin-markup-command (normalsize layout props arg) (markup?)
893   "Set font size to default."
894   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
895
896 (define-builtin-markup-command (small layout props arg) (markup?)
897   "Set font size to -1."
898   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
899
900 (define-builtin-markup-command (tiny layout props arg) (markup?)
901   "Set font size to -2."
902   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
903
904 (define-builtin-markup-command (teeny layout props arg) (markup?)
905   "Set font size to -3."
906   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
907
908 (define-builtin-markup-command (fontCaps layout props arg) (markup?)
909   "Set @code{font-shape} to @code{caps}."
910   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
911
912 ;; Poor man's caps
913 (define-builtin-markup-command (smallCaps layout props text) (markup?)
914   "Turn @code{text}, which should be a string, to small caps.
915 @example
916 \\markup \\smallCaps \"Text between double quotes\"
917 @end example
918 "
919   (define (make-small-caps-markup chars)
920     (cond ((null? chars)
921            (markup))
922           ((char-whitespace? (car chars))
923            (markup #:fontsize -2 #:simple (string-upcase (list->string (cdr chars)))))
924           (else
925            (markup #:hspace -1
926                    #:fontsize -2 #:simple (string-upcase (list->string chars))))))
927   (define (make-not-small-caps-markup chars)
928     (cond ((null? chars)
929            (markup))
930           ((char-whitespace? (car chars))
931            (markup #:simple (list->string (cdr chars))))
932           (else
933            (markup #:hspace -1
934                    #:simple (list->string chars)))))
935   (define (small-caps-aux done-markups current-chars rest-chars small? after-space?)
936     (cond ((null? rest-chars)
937            ;; the end of the string: build the markup
938            (make-line-markup (reverse! (cons ((if small?
939                                                   make-small-caps-markup
940                                                   make-not-small-caps-markup)
941                                               (reverse! current-chars))
942                                              done-markups))))
943           ((char-whitespace? (car rest-chars))
944            ;; a space char.
945            (small-caps-aux done-markups current-chars (cdr rest-chars) small? #t))
946           ((or (and small? (char-lower-case? (car rest-chars)))
947                (and (not small?) (not (char-lower-case? (car rest-chars)))))
948            ;; same case
949            ;; add the char to the current char list
950            (small-caps-aux done-markups
951                            (cons (car rest-chars)
952                                  (if after-space? 
953                                      (cons #\space current-chars)
954                                      current-chars))
955                            (cdr rest-chars) 
956                            small?
957                            #f))
958           (else
959            ;; case change
960            ;; make a markup with current chars, and start a new list with new char
961            (small-caps-aux (cons ((if small?
962                                       make-small-caps-markup
963                                       make-not-small-caps-markup)
964                                   (reverse! current-chars))
965                                  done-markups)
966                            (if after-space?
967                                (list (car rest-chars) #\space)
968                                (list (car rest-chars)))
969                            (cdr rest-chars)
970                            (not small?)
971                            #f))))
972   (interpret-markup layout props (small-caps-aux (list) 
973                                                  (list) 
974                                                  (cons #\space (string->list text))
975                                                  #f
976                                                  #f)))
977
978 (define-builtin-markup-command (caps layout props arg) (markup?)
979   (interpret-markup layout props (make-smallCaps-markup arg)))
980
981 (define-builtin-markup-command (dynamic layout props arg) (markup?)
982   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
983 @b{z}, @b{p}, and @b{r}.  When producing phrases, like ``pi@`{u} @b{f}'', the
984 normal words (like ``pi@`{u}'') should be done in a different font.  The
985 recommend font for this is bold and italic"
986   (interpret-markup
987    layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
988
989 (define-builtin-markup-command (text layout props arg) (markup?)
990   "Use a text font instead of music symbol or music alphabet font."  
991
992   ;; ugh - latin1
993   (interpret-markup layout (prepend-alist-chain 'font-encoding 'latin1 props)
994                     arg))
995
996
997 (define-builtin-markup-command (italic layout props arg) (markup?)
998   "Use italic @code{font-shape} for @var{arg}. "
999   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
1000
1001 (define-builtin-markup-command (typewriter layout props arg) (markup?)
1002   "Use @code{font-family} typewriter for @var{arg}."
1003   (interpret-markup
1004    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
1005
1006 (define-builtin-markup-command (upright layout props arg) (markup?)
1007   "Set font shape to @code{upright}.  This is the opposite of @code{italic}."
1008   (interpret-markup
1009    layout (prepend-alist-chain 'font-shape 'upright props) arg))
1010
1011 (define-builtin-markup-command (medium layout props arg) (markup?)
1012   "Switch to medium font-series (in contrast to bold)."
1013   (interpret-markup layout (prepend-alist-chain 'font-series 'medium props)
1014                     arg))
1015
1016 (define-builtin-markup-command (normal-text layout props arg) (markup?)
1017   "Set all font related properties (except the size) to get the default normal text font, no matter what font was used earlier."
1018   ;; ugh - latin1
1019   (interpret-markup layout
1020                     (cons '((font-family . roman) (font-shape . upright)
1021                             (font-series . medium) (font-encoding . latin1))
1022                           props)
1023                     arg))
1024
1025 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1026 ;; symbols.
1027 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1028
1029 (define-builtin-markup-command (doublesharp layout props) ()
1030   "Draw a double sharp symbol."
1031
1032   (interpret-markup layout props (markup #:musicglyph (assoc-get 1 standard-alteration-glyph-name-alist ""))))
1033
1034 (define-builtin-markup-command (sesquisharp layout props) ()
1035   "Draw a 3/2 sharp symbol."
1036   (interpret-markup layout props (markup #:musicglyph (assoc-get 3/4 standard-alteration-glyph-name-alist ""))))
1037                                          
1038
1039 (define-builtin-markup-command (sharp layout props) ()
1040   "Draw a sharp symbol."
1041   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/2 standard-alteration-glyph-name-alist ""))))
1042
1043 (define-builtin-markup-command (semisharp layout props) ()
1044   "Draw a semi sharp symbol."
1045   (interpret-markup layout props (markup #:musicglyph (assoc-get 1/4 standard-alteration-glyph-name-alist ""))))
1046
1047 (define-builtin-markup-command (natural layout props) ()
1048   "Draw a natural symbol."
1049   (interpret-markup layout props (markup #:musicglyph (assoc-get 0 standard-alteration-glyph-name-alist ""))))
1050
1051 (define-builtin-markup-command (semiflat layout props) ()
1052   "Draw a semiflat."
1053   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/4 standard-alteration-glyph-name-alist ""))))
1054
1055 (define-builtin-markup-command (flat layout props) ()
1056   "Draw a flat symbol."
1057   (interpret-markup layout props (markup #:musicglyph (assoc-get -1/2 standard-alteration-glyph-name-alist ""))))
1058
1059 (define-builtin-markup-command (sesquiflat layout props) ()
1060   "Draw a 3/2 flat symbol."
1061   (interpret-markup layout props (markup #:musicglyph (assoc-get -3/4 standard-alteration-glyph-name-alist ""))))
1062
1063 (define-builtin-markup-command (doubleflat layout props) ()
1064   "Draw a double flat symbol."
1065   (interpret-markup layout props (markup #:musicglyph (assoc-get -1 standard-alteration-glyph-name-alist ""))))
1066
1067 (define-builtin-markup-command (with-color layout props color arg) (color? markup?)
1068   "Draw @var{arg} in color specified by @var{color}"
1069
1070   (let* ((stil (interpret-markup layout props arg)))
1071
1072     (ly:make-stencil (list 'color color (ly:stencil-expr stil))
1073                      (ly:stencil-extent stil X)
1074                      (ly:stencil-extent stil Y))))
1075
1076 \f
1077 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1078 ;; glyphs
1079 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1080
1081
1082 (define-builtin-markup-command (arrow-head layout props axis direction filled)
1083   (integer? ly:dir? boolean?)
1084   "produce an arrow head in specified direction and axis. Use the filled head if @var{filled} is  specified."
1085   (let*
1086       ((name (format "arrowheads.~a.~a~a"
1087                      (if filled
1088                          "close"
1089                          "open")
1090                      axis
1091                      direction)))
1092     (ly:font-get-glyph
1093      (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1094                                      props))
1095      name)))
1096
1097 (define-builtin-markup-command (musicglyph layout props glyph-name) (string?)
1098   "This is converted to a musical symbol, e.g. @code{\\musicglyph
1099 #\"accidentals.natural\"} will select the natural sign from the music font.
1100 See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
1101   (ly:font-get-glyph
1102    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
1103                                    props))
1104    glyph-name))
1105
1106 (define-builtin-markup-command (lookup layout props glyph-name) (string?)
1107   "Lookup a glyph by name."
1108   (ly:font-get-glyph (ly:paper-get-font layout props)
1109                      glyph-name))
1110
1111 (define-builtin-markup-command (char layout props num) (integer?)
1112   "Produce a single character, e.g. @code{\\char #65} produces the 
1113 letter 'A'."
1114
1115   (ly:text-interface::interpret-markup layout props (ly:wide-char->utf-8 num)))
1116
1117 (define number->mark-letter-vector (make-vector 25 #\A))
1118
1119 (do ((i 0 (1+ i))
1120      (j 0 (1+ j)))
1121     ((>= i 26))
1122   (if (= i (- (char->integer #\I) (char->integer #\A)))
1123       (set! i (1+ i)))
1124   (vector-set! number->mark-letter-vector j
1125                (integer->char (+ i (char->integer #\A)))))
1126
1127 (define number->mark-alphabet-vector (list->vector
1128   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
1129
1130 (define (number->markletter-string vec n)
1131   "Double letters for big marks."
1132   (let* ((lst (vector-length vec)))
1133     
1134     (if (>= n lst)
1135         (string-append (number->markletter-string vec (1- (quotient n lst)))
1136                        (number->markletter-string vec (remainder n lst)))
1137         (make-string 1 (vector-ref vec n)))))
1138
1139 (define-builtin-markup-command (markletter layout props num) (integer?)
1140   "Make a markup letter for @var{num}.  The letters start with A to Z
1141  (skipping I), and continues with double letters."
1142   (ly:text-interface::interpret-markup layout props
1143     (number->markletter-string number->mark-letter-vector num)))
1144
1145 (define-builtin-markup-command (markalphabet layout props num) (integer?)
1146    "Make a markup letter for @var{num}.  The letters start with A to Z
1147  and continues with double letters."
1148    (ly:text-interface::interpret-markup layout props
1149      (number->markletter-string number->mark-alphabet-vector num)))
1150
1151
1152
1153 (define-builtin-markup-command (slashed-digit layout props num) (integer?)
1154   "A feta number, with slash. This is for use in the context of
1155 figured bass notation"
1156   (let*
1157       ((mag (magstep (chain-assoc-get 'font-size props 0)))
1158        (thickness
1159         (* mag
1160            (chain-assoc-get 'thickness props 0.16)))
1161        (dy (* mag 0.15))
1162        (number-stencil (interpret-markup layout
1163                                          (prepend-alist-chain 'font-encoding 'fetaNumber props)
1164                                          (number->string num)))
1165        (num-x (interval-widen (ly:stencil-extent number-stencil X)
1166                               (* mag 0.2)))
1167        (num-y (ly:stencil-extent number-stencil Y))
1168        (is-sane (and (interval-sane? num-x) (interval-sane? num-y)))
1169        
1170        (slash-stencil
1171         (if is-sane
1172             (ly:make-stencil
1173              `(draw-line
1174                ,thickness
1175                ,(car num-x) ,(- (interval-center num-y) dy)
1176                ,(cdr num-x) ,(+ (interval-center num-y) dy))
1177              num-x num-y)
1178             #f)))
1179
1180     (set! slash-stencil
1181           (cond
1182            ((not (ly:stencil? slash-stencil)) #f)
1183            ((= num 5) (ly:stencil-translate slash-stencil
1184                                             ;;(cons (* mag -0.05) (* mag 0.42))
1185                                             (cons (* mag -0.00) (* mag -0.07))
1186
1187                                             ))
1188            ((= num 7) (ly:stencil-translate slash-stencil
1189                                             ;;(cons (* mag -0.05) (* mag 0.42))
1190                                             (cons (* mag -0.00) (* mag -0.15))
1191
1192                                             ))
1193            
1194            (else slash-stencil)))
1195
1196     (if slash-stencil
1197         (set! number-stencil
1198               (ly:stencil-add number-stencil slash-stencil))
1199         
1200         (ly:warning "invalid number for slashed digit ~a" num))
1201
1202
1203     number-stencil))
1204 \f
1205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1206 ;; the note command.
1207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1208
1209
1210 ;; TODO: better syntax.
1211
1212 (define-builtin-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
1213   "Construct a note symbol, with stem.  By using fractional values for
1214 @var{dir}, you can obtain longer or shorter stems."
1215
1216   (define (get-glyph-name-candidates dir log style)
1217     (map (lambda (dir-name)
1218      (format "noteheads.~a~a~a" dir-name (min log 2)
1219              (if (and (symbol? style)
1220                       (not (equal? 'default style)))
1221                  (symbol->string style)
1222                  "")))
1223          (list (if (= dir UP) "u" "d")
1224                "s")))
1225                    
1226   (define (get-glyph-name font cands)
1227     (if (null? cands)
1228      ""
1229      (if (ly:stencil-empty? (ly:font-get-glyph font (car cands)))
1230          (get-glyph-name font (cdr cands))
1231          (car cands))))
1232     
1233   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
1234          (size-factor (magstep (chain-assoc-get 'font-size props 0)))
1235          (style (chain-assoc-get 'style props '()))
1236          (stem-length (*  size-factor (max 3 (- log 1))))
1237          (head-glyph-name (get-glyph-name font (get-glyph-name-candidates (sign dir) log style)))
1238          (head-glyph (ly:font-get-glyph font head-glyph-name))
1239          (attach-indices (ly:note-head::stem-attachment font head-glyph-name))
1240          (stem-thickness (* size-factor 0.13))
1241          (stemy (* dir stem-length))
1242          (attach-off (cons (interval-index
1243                             (ly:stencil-extent head-glyph X)
1244                             (* (sign dir) (car attach-indices)))
1245                            (* (sign dir)        ; fixme, this is inconsistent between X & Y.
1246                               (interval-index
1247                                (ly:stencil-extent head-glyph Y)
1248                                (cdr attach-indices)))))
1249          (stem-glyph (and (> log 0)
1250                           (ly:round-filled-box
1251                            (ordered-cons (car attach-off)
1252                                          (+ (car attach-off)  (* (- (sign dir)) stem-thickness)))
1253                            (cons (min stemy (cdr attach-off))
1254                                  (max stemy (cdr attach-off)))
1255                            (/ stem-thickness 3))))
1256          
1257          (dot (ly:font-get-glyph font "dots.dot"))
1258          (dotwid (interval-length (ly:stencil-extent dot X)))
1259          (dots (and (> dot-count 0)
1260                     (apply ly:stencil-add
1261                            (map (lambda (x)
1262                                   (ly:stencil-translate-axis
1263                                    dot (* 2 x dotwid) X))
1264                                 (iota dot-count)))))
1265          (flaggl (and (> log 2)
1266                       (ly:stencil-translate
1267                        (ly:font-get-glyph font
1268                                           (string-append "flags."
1269                                                          (if (> dir 0) "u" "d")
1270                                                          (number->string log)))
1271                        (cons (+ (car attach-off) (/ stem-thickness 2)) stemy)))))
1272
1273     (if (and dots flaggl (> dir 0))
1274         (set! dots (ly:stencil-translate-axis dots 0.35 X)))
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                 (+ (cdr (ly:stencil-extent head-glyph X)) dotwid)
1286                 X)
1287                stem-glyph)))
1288     stem-glyph))
1289
1290 (define-public log2 
1291   (let ((divisor (log 2)))
1292     (lambda (z) (inexact->exact (/ (log z) divisor)))))
1293
1294 (define (parse-simple-duration duration-string)
1295   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
1296   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
1297     (if (and match (string=? duration-string (match:substring match 0)))
1298         (let ((len  (match:substring match 1))
1299               (dots (match:substring match 2)))
1300           (list (cond ((string=? len "breve") -1)
1301                       ((string=? len "longa") -2)
1302                       ((string=? len "maxima") -3)
1303                       (else (log2 (string->number len))))
1304                 (if dots (string-length dots) 0)))
1305         (ly:error (_ "not a valid duration string: ~a") duration-string))))
1306
1307 (define-builtin-markup-command (note layout props duration dir) (string? number?)
1308   "This produces a note with a stem pointing in @var{dir} direction, with
1309 the @var{duration} for the note head type and augmentation dots. For
1310 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
1311 a shortened down stem."
1312   (let ((parsed (parse-simple-duration duration)))
1313     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
1314
1315 \f
1316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1317 ;; translating.
1318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1319
1320 (define-builtin-markup-command (lower layout props amount arg) (number? markup?)
1321   "
1322 Lower @var{arg}, by the distance @var{amount}.
1323 A negative @var{amount} indicates raising, see also @code{\\raise}.
1324 "
1325   (ly:stencil-translate-axis (interpret-markup layout props arg)
1326                              (- amount) Y))
1327
1328
1329 (define-builtin-markup-command (translate-scaled layout props offset arg) (number-pair? markup?)
1330   "Translate @var{arg} by @var{offset}, scaling the offset by the @code{font-size}."
1331
1332   (let*
1333       ((factor (magstep (chain-assoc-get 'font-size props 0)))
1334        (scaled (cons (* factor (car offset))
1335                      (* factor (cdr offset)))))
1336     
1337   (ly:stencil-translate (interpret-markup layout props arg)
1338                         scaled)))
1339
1340 (define-builtin-markup-command (raise layout props amount arg) (number? markup?)
1341   "
1342 Raise @var{arg}, by the distance @var{amount}.
1343 A negative @var{amount} indicates lowering, see also @code{\\lower}.
1344 @c
1345 @lilypond[verbatim,fragment,relative=1]
1346  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
1347 @end lilypond
1348 The argument to @code{\\raise} is the vertical displacement amount,
1349 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
1350 raise objects in relation to their surrounding markups.
1351
1352 If the text object itself is positioned above or below the staff, then
1353 @code{\\raise} cannot be used to move it, since the mechanism that
1354 positions it next to the staff cancels any shift made with
1355 @code{\\raise}. For vertical positioning, use the @code{padding}
1356 and/or @code{extra-offset} properties. "
1357   (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
1358
1359 (define-builtin-markup-command (fraction layout props arg1 arg2) (markup? markup?)
1360   "Make a fraction of two markups."
1361   (let* ((m1 (interpret-markup layout props arg1))
1362          (m2 (interpret-markup layout props arg2))
1363          (factor (magstep (chain-assoc-get 'font-size props 0)))
1364          (boxdimen (cons (* factor -0.05) (* factor 0.05)))
1365          (padding (* factor 0.2))
1366          (baseline (* factor 0.6))
1367          (offset (* factor 0.75)))
1368     (set! m1 (ly:stencil-aligned-to m1 X CENTER))
1369     (set! m2 (ly:stencil-aligned-to m2 X CENTER))
1370     (let* ((x1 (ly:stencil-extent m1 X))
1371            (x2 (ly:stencil-extent m2 X))
1372            (line (ly:round-filled-box (interval-union x1 x2) boxdimen 0.0))
1373            ;; should stack mols separately, to maintain LINE on baseline
1374            (stack (stack-lines DOWN padding baseline (list m1 line m2))))
1375       (set! stack
1376             (ly:stencil-aligned-to stack Y CENTER))
1377       (set! stack
1378             (ly:stencil-aligned-to stack X LEFT))
1379       ;; should have EX dimension
1380       ;; empirical anyway
1381       (ly:stencil-translate-axis stack offset Y))))
1382
1383
1384
1385
1386
1387 (define-builtin-markup-command (normal-size-super layout props arg) (markup?)
1388   "Set @var{arg} in superscript with a normal font size."
1389   (ly:stencil-translate-axis
1390    (interpret-markup layout props arg)
1391    (* 0.5 (chain-assoc-get 'baseline-skip props)) Y))
1392
1393 (define-builtin-markup-command (super layout props arg) (markup?)
1394   "
1395 @cindex raising text
1396 @cindex lowering text
1397 @cindex moving text
1398 @cindex translating text
1399
1400 @cindex @code{\\super}
1401
1402
1403 Raising and lowering texts can be done with @code{\\super} and
1404 @code{\\sub}:
1405
1406 @lilypond[verbatim,fragment,relative=1]
1407  c1^\\markup { E \"=\" mc \\super \"2\" }
1408 @end lilypond
1409
1410 "
1411   (ly:stencil-translate-axis
1412    (interpret-markup
1413     layout
1414     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1415     arg)
1416    (* 0.5 (chain-assoc-get 'baseline-skip props))
1417    Y))
1418
1419 (define-builtin-markup-command (translate layout props offset arg) (number-pair? markup?)
1420   "This translates an object. Its first argument is a cons of numbers
1421 @example
1422 A \\translate #(cons 2 -3) @{ B C @} D
1423 @end example
1424 This moves `B C' 2 spaces to the right, and 3 down, relative to its
1425 surroundings. This command cannot be used to move isolated scripts
1426 vertically, for the same reason that @code{\\raise} cannot be used for
1427 that.
1428
1429 "
1430   (ly:stencil-translate (interpret-markup  layout props arg)
1431                         offset))
1432
1433 (define-builtin-markup-command (sub layout props arg) (markup?)
1434   "Set @var{arg} in subscript."
1435   (ly:stencil-translate-axis
1436    (interpret-markup
1437     layout
1438     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
1439     arg)
1440    (* -0.5 (chain-assoc-get 'baseline-skip props))
1441    Y))
1442
1443 (define-builtin-markup-command (normal-size-sub layout props arg) (markup?)
1444   "Set @var{arg} in subscript, in a normal font size."
1445   (ly:stencil-translate-axis
1446    (interpret-markup layout props arg)
1447    (* -0.5 (chain-assoc-get 'baseline-skip props))
1448    Y))
1449 \f
1450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1451 ;; brackets.
1452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1453
1454 (define-builtin-markup-command (hbracket layout props arg) (markup?)
1455   "Draw horizontal brackets around @var{arg}."  
1456   (let ((th 0.1) ;; todo: take from GROB.
1457         (m (interpret-markup layout props arg)))
1458     (bracketify-stencil m X th (* 2.5 th) th)))
1459
1460 (define-builtin-markup-command (bracket layout props arg) (markup?)
1461   "Draw vertical brackets around @var{arg}."  
1462   (let ((th 0.1) ;; todo: take from GROB.
1463         (m (interpret-markup layout props arg)))
1464     (bracketify-stencil m Y th (* 2.5 th) th)))
1465 \f
1466
1467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1468 ;; size indications arrow
1469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1470