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