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