]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-markup-commands.scm
*** empty log message ***
[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--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
6 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
7
8 ;;; markup commands
9 ;;;  * each markup function should have a doc string with
10 ;;     syntax, description and example. 
11
12 (define-public empty-stencil (ly:make-stencil '() '(1 . -1) '(1 . -1)))
13 (define-public point-stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
14
15 (def-markup-command (stencil layout props stil) (ly:stencil?)
16   "Stencil as markup"
17   stil)
18
19
20 (def-markup-command (with-url layout props url arg) (string? markup?)
21   "Add a link to URL @var{url} around @var{arg}. This only works in
22 the PDF backend."
23   (let*
24       ((stil (interpret-markup layout props arg))
25        (xextent (ly:stencil-extent stil X))
26        (yextent (ly:stencil-extent stil Y))
27        (old-expr (ly:stencil-expr stil))
28        (url-expr (list 'url-link url `(quote ,xextent) `(quote ,yextent))))
29     
30     (ly:stencil-add
31      (ly:make-stencil url-expr xextent yextent)
32      stil)))
33
34 (def-markup-command (score layout props score) (ly:score?)
35   "Inline an image of music."
36   (let* ((systems (ly:score-embedded-format score layout)))
37
38     (if (= 0 (vector-length systems))
39         (begin
40           (ly:warn "No systems found in \\score markup. Did you forget \\layout?")
41           empty-markup)
42         (begin
43           (let* ((stencil (ly:paper-system-stencil (vector-ref systems 0)))) 
44
45             (ly:stencil-align-to! stencil Y CENTER)
46             stencil)))))
47
48 (def-markup-command (simple layout props str) (string?)
49   "A simple text string; @code{\\markup @{ foo @}} is equivalent with
50 @code{\\markup @{ \\simple #\"foo\" @}}."
51   (interpret-markup layout props str))
52
53 (def-markup-command (encoded-simple layout props sym str) (symbol? string?)
54   "A text string, encoded with encoding @var{sym}. See
55 @usermanref{Text encoding} for more information."
56   (Text_interface::interpret_string layout
57                                     props sym str))
58
59
60 ;; TODO: use font recoding.
61 ;;                    (make-line-markup
62 ;;                     (map make-word-markup (string-tokenize str)))))
63
64 (define-public empty-markup
65   (make-simple-markup ""))
66
67
68 (def-markup-command (postscript layout props str) (string?)
69   "This inserts @var{str} directly into the output as a PostScript
70 command string.  Due to technicalities of the output backends,
71 different scales should be used for the @TeX{} and PostScript backend,
72 selected with @code{-f}. 
73
74
75 For the TeX backend, the following string prints a rotated text
76
77 @cindex rotated text
78
79 @verbatim
80 0 0 moveto /ecrm10 findfont 
81 1.75 scalefont setfont 90 rotate (hello) show
82 @end verbatim
83
84 @noindent
85 The magical constant 1.75 scales from LilyPond units (staff spaces) to
86 TeX dimensions.
87
88 For the postscript backend, use the following
89
90 @verbatim
91 gsave /ecrm10 findfont 
92  10.0 output-scale div 
93  scalefont setfont 90 rotate (hello) show grestore 
94 @end verbatim
95 "
96   ;; FIXME
97   (ly:make-stencil
98    (list 'embedded-ps str)
99    '(0 . 0) '(0 . 0)))
100
101 ;;(def-markup-command (fill-line layout props line-width markups)
102 ;;  (number? markup-list?)
103 ;; no parser tag -- should make number? markuk-list? thingy
104 (def-markup-command (fill-line layout props markups)
105   (markup-list?)
106   "Put @var{markups} in a horizontal line of width @var{line-width}.
107    The markups are spaced/flushed to fill the entire line.
108    If there are no arguments, return an empty stencil.
109 "
110   (let* ((orig-stencils
111           (map (lambda (x) (interpret-markup layout props x))
112                markups))
113          (stencils
114           (map (lambda (stc)
115                  (if (ly:stencil-empty? stc)
116                      point-stencil
117                      stc)) orig-stencils))
118      (text-widths
119         (map (lambda (stc)
120                 (if (ly:stencil-empty? stc)
121                         0.0
122                                 (interval-length (ly:stencil-extent stc X))))
123                         stencils))
124      (text-width (apply + text-widths))
125          (word-count (length stencils))
126          (word-space (chain-assoc-get 'word-space props))
127          (line-width (chain-assoc-get 'linewidth props))
128          (fill-space
129                 (cond
130                         ((= word-count 1) 
131                                 (list
132                                         (/ (- line-width text-width) 2)
133                                         (/ (- line-width text-width) 2)))
134                         ((= word-count 2)
135                                 (list
136                                         (- line-width text-width)))
137                         (else 
138                                 (get-fill-space word-count line-width text-widths))))
139      (fill-space-normal
140         (map (lambda (x)
141                 (if (< x word-space)
142                         word-space
143                                 x))
144                         fill-space))
145                                         
146          (line-stencils (if (= word-count 1)
147                             (list
148                              point-stencil
149                              (car stencils)
150                              point-stencil)
151                             stencils)))
152
153     (if (null? (remove ly:stencil-empty? orig-stencils))
154         empty-stencil
155         (stack-stencils-padding-list X RIGHT fill-space-normal line-stencils))))
156         
157 (define (get-fill-space word-count line-width text-widths)
158         "Calculates the necessary paddings between each two adjacent texts.
159         The lengths of all texts are stored in @var{text-widths}.
160         The normal formula for the padding between texts a and b is:
161         padding = line-width/(word-count - 1) - (length(a) + length(b))/2
162         The first and last padding have to be calculated specially using the
163         whole length of the first or last text.
164         Returns a list of paddings.
165 "
166         (cond
167                 ;; special case first padding
168                 ((= (length text-widths) word-count)
169                         (cons 
170                                 (- (- (/ line-width (1- word-count)) (car text-widths)) (/ (car (cdr text-widths)) 2))
171                                 (get-fill-space word-count line-width (cdr text-widths))))
172                 ;; special case last padding
173                 ((= (length text-widths) 2)
174                         (list (- (/ line-width (1- word-count)) (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
175                 (else
176                         (cons 
177                                 (- (/ line-width (1- word-count)) (/ (+ (car text-widths) (car (cdr text-widths))) 2))
178                                 (get-fill-space word-count line-width (cdr text-widths))))))
179
180 (define (font-markup qualifier value)
181   (lambda (layout props arg)
182     (interpret-markup layout
183                       (prepend-alist-chain qualifier value props)
184                       arg)))
185
186
187 (def-markup-command (line layout props args) (markup-list?)
188   "Put @var{args} in a horizontal line.  The property @code{word-space}
189 determines the space between each markup in @var{args}."
190   (stack-stencil-line
191    (chain-assoc-get 'word-space props)
192    (remove ly:stencil-empty?
193            (map (lambda (m) (interpret-markup layout props m)) args))))
194
195 (def-markup-command (fromproperty layout props symbol) (symbol?)
196   "Read the @var{symbol} from property settings, and produce a stencil
197   from the markup contained within. If @var{symbol} is not defined, it
198   returns an empty markup"
199   (let* ((m (chain-assoc-get symbol props)))
200     (if (markup? m)
201         (interpret-markup layout props m)
202         (ly:make-stencil '()  '(1 . -1) '(1 . -1)))))
203
204
205 (def-markup-command (on-the-fly layout props procedure arg) (symbol? markup?)
206   "Apply the @var{procedure} markup command to
207 @var{arg}. @var{procedure} should take a single argument."
208   (let* ((anonymous-with-signature (lambda (layout props arg) (procedure layout props arg))))
209     (set-object-property! anonymous-with-signature
210                           'markup-signature
211                           (list markup?))
212     (interpret-markup layout props (list anonymous-with-signature arg))))
213
214
215 (def-markup-command (combine layout props m1 m2) (markup? markup?)
216   "Print two markups on top of each other."
217   (let* ((s1 (interpret-markup layout props m1))
218          (s2 (interpret-markup layout props m2)))
219     (ly:stencil-add s1 s2)))
220
221 (def-markup-command (finger layout props arg) (markup?)
222   "Set the argument as small numbers."
223   (interpret-markup layout
224                     (cons '((font-size . -5) (font-encoding . fetaNumber)) props)
225                     arg))
226
227 (def-markup-command (fontsize layout props mag arg) (number? markup?)
228   "This sets the relative font size, e.g.
229 @example
230 A \\fontsize #2 @{ B C @} D
231 @end example
232
233
234 This will enlarge the B and the C by two steps.
235 "
236   (interpret-markup
237    layout 
238    (prepend-alist-chain 'font-size mag props)
239    arg))
240
241 (def-markup-command (magnify layout props sz arg) (number? markup?)
242   "This sets the font magnification for the its argument. In the following
243 example, the middle A will be 10% larger:
244 @example
245 A \\magnify #1.1 @{ A @} A
246 @end example
247
248 Note: magnification only works if a font-name is explicitly selected.
249 Use @code{\\fontsize} otherwise."
250   (interpret-markup
251    layout 
252    (prepend-alist-chain 'font-magnification sz props)
253    arg))
254
255 (def-markup-command (bold layout props arg) (markup?)
256   "Switch to bold font-series"
257   (interpret-markup layout (prepend-alist-chain 'font-series 'bold props) arg))
258
259 (def-markup-command (sans layout props arg) (markup?)
260   "Switch to the sans serif family"
261   (interpret-markup layout (prepend-alist-chain 'font-family 'sans props) arg))
262
263 (def-markup-command (number layout props arg) (markup?)
264   "Set font family to @code{number}, which yields the font used for
265 time signatures and fingerings.  This font only contains numbers and
266 some punctuation. It doesn't have any letters.  "
267   (interpret-markup layout (prepend-alist-chain 'font-encoding 'fetaNumber props) arg))
268
269 (def-markup-command (roman layout props arg) (markup?)
270   "Set font family to @code{roman}."
271   (interpret-markup layout (prepend-alist-chain 'font-family 'roman props) arg))
272
273 (def-markup-command (huge layout props arg) (markup?)
274   "Set font size to +2."
275   (interpret-markup layout (prepend-alist-chain 'font-size 2 props) arg))
276
277 (def-markup-command (large layout props arg) (markup?)
278   "Set font size to +1."
279   (interpret-markup layout (prepend-alist-chain 'font-size 1 props) arg))
280
281 (def-markup-command (normalsize layout props arg) (markup?)
282   "Set font size to default."
283   (interpret-markup layout (prepend-alist-chain 'font-size 0 props) arg))
284
285 (def-markup-command (small layout props arg) (markup?)
286   "Set font size to -1."
287   (interpret-markup layout (prepend-alist-chain 'font-size -1 props) arg))
288
289 (def-markup-command (tiny layout props arg) (markup?)
290   "Set font size to -2."
291   (interpret-markup layout (prepend-alist-chain 'font-size -2 props) arg))
292
293 (def-markup-command (teeny layout props arg) (markup?)
294   "Set font size to -3."
295   (interpret-markup layout (prepend-alist-chain 'font-size -3 props) arg))
296
297 (def-markup-command (caps layout props arg) (markup?)
298   "Set @code{font-shape} to @code{caps}."
299   (interpret-markup layout (prepend-alist-chain 'font-shape 'caps props) arg))
300
301 ;(def-markup-command (latin-i layout props arg) (markup?)
302 ;  "TEST latin1 encoding."
303 ;  (interpret-markup layout (prepend-alist-chain 'font-shape 'latin1 props) arg))
304
305 (def-markup-command (dynamic layout props arg) (markup?)
306   "Use the dynamic font.  This font only contains @b{s}, @b{f}, @b{m},
307 @b{z}, @b{p}, and @b{r}.  When producing phrases, like ``pi@`{u} @b{f}'', the
308 normal words (like ``pi@`{u}'') should be done in a different font.  The
309 recommend font for this is bold and italic"
310   (interpret-markup
311    layout (prepend-alist-chain 'font-encoding 'fetaDynamic props) arg))
312
313 (def-markup-command (italic layout props arg) (markup?)
314   "Use italic @code{font-shape} for @var{arg}. "
315   (interpret-markup layout (prepend-alist-chain 'font-shape 'italic props) arg))
316
317 (def-markup-command (typewriter layout props arg) (markup?)
318   "Use @code{font-family} typewriter for @var{arg}."
319   (interpret-markup
320    layout (prepend-alist-chain 'font-family 'typewriter props) arg))
321
322 (def-markup-command (upright layout props arg) (markup?)
323   "Set font shape to @code{upright}."
324   (interpret-markup
325    layout (prepend-alist-chain 'font-shape 'upright props) arg))
326
327 (def-markup-command (doublesharp layout props) ()
328   "Draw a double sharp symbol."
329
330   (interpret-markup layout props (markup #:musicglyph "accidentals.4")))
331 (def-markup-command (sesquisharp layout props) ()
332   "Draw a 3/2 sharp symbol."
333   (interpret-markup layout props (markup #:musicglyph "accidentals.3")))
334
335 (def-markup-command (sharp layout props) ()
336   "Draw a sharp symbol."
337   (interpret-markup layout props (markup #:musicglyph "accidentals.2")))
338
339 (def-markup-command (semisharp layout props) ()
340   "Draw a semi sharp symbol."
341   (interpret-markup layout props (markup #:musicglyph "accidentals.1")))
342
343 (def-markup-command (natural layout props) ()
344   "Draw a natural symbol."
345   (interpret-markup layout props (markup #:musicglyph "accidentals.0")))
346
347 (def-markup-command (semiflat layout props) ()
348   "Draw a semiflat."
349   (interpret-markup layout props (markup #:musicglyph "accidentals.M1")))
350
351 (def-markup-command (flat layout props) ()
352   "Draw a flat symbol."
353   (interpret-markup layout props (markup #:musicglyph "accidentals.M2")))
354
355 (def-markup-command (sesquiflat layout props) ()
356   "Draw a 3/2 flat symbol."
357   (interpret-markup layout props (markup #:musicglyph "accidentals.M3")))
358
359 (def-markup-command (doubleflat layout props) ()
360   "Draw a double flat symbol."
361   (interpret-markup layout props (markup #:musicglyph "accidentals.M4")))
362
363 (def-markup-command (column layout props args) (markup-list?)
364   "Stack the markups in @var{args} vertically.  The property
365 @code{baseline-skip} determines the space between each markup in @var{args}."
366   (stack-lines
367    -1 0.0 (chain-assoc-get 'baseline-skip props)
368    (remove ly:stencil-empty?
369            (map (lambda (m) (interpret-markup layout props m)) args))))
370
371 (def-markup-command (dir-column layout props args) (markup-list?)
372   "Make a column of args, going up or down, depending on the setting
373 of the @code{#'direction} layout property."
374   (let* ((dir (chain-assoc-get 'direction props)))
375     (stack-lines
376      (if (number? dir) dir -1)
377      0.0
378      (chain-assoc-get 'baseline-skip props)
379      (map (lambda (x) (interpret-markup layout props x)) args))))
380
381 (def-markup-command (center-align layout props args) (markup-list?)
382   "Put @code{args} in a centered column. "
383   (let* ((mols (map (lambda (x) (interpret-markup layout props x)) args))
384          (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
385     (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) cmols)))
386
387 (def-markup-command (vcenter layout props arg) (markup?)
388   "Align @code{arg} to its Y center. "
389   (let* ((mol (interpret-markup layout props arg)))
390     (ly:stencil-align-to! mol Y CENTER)
391     mol))
392
393 (def-markup-command (hcenter layout props arg) (markup?)
394   "Align @code{arg} to its X center. "
395   (let* ((mol (interpret-markup layout props arg)))
396     (ly:stencil-align-to! mol X CENTER)
397     mol))
398
399 (def-markup-command (right-align layout props arg) (markup?)
400   "Align @var{arg} on its right edge. "
401   (let* ((m (interpret-markup layout props arg)))
402     (ly:stencil-align-to! m X RIGHT)
403     m))
404
405 (def-markup-command (left-align layout props arg) (markup?)
406   "Align @var{arg} on its left edge. "
407   (let* ((m (interpret-markup layout props arg)))
408     (ly:stencil-align-to! m X LEFT)
409     m))
410
411 (def-markup-command (general-align layout props axis dir arg)  (integer? number? markup?)
412   "Align @var{arg} in @var{axis} direction to the @var{dir} side."
413   (let* ((m (interpret-markup layout props arg)))
414     (ly:stencil-align-to! m axis dir)
415     m))
416
417 (def-markup-command (halign layout props dir arg) (number? markup?)
418   "Set horizontal alignment. If @var{dir} is @code{-1}, then it is
419 left-aligned, while @code{+1} is right. Values in between interpolate
420 alignment accordingly."
421   (let* ((m (interpret-markup layout props arg)))
422     (ly:stencil-align-to! m X dir)
423     m))
424
425 (def-markup-command (musicglyph layout props glyph-name) (string?)
426   "This is converted to a musical symbol, e.g. @code{\\musicglyph
427 #\"accidentals.0\"} will select the natural sign from the music font.
428 See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
429   (ly:font-get-glyph
430    (ly:paper-get-font layout (cons '((font-encoding . fetaMusic))
431                                    props))
432    glyph-name))
433
434 (def-markup-command (lookup layout props glyph-name) (string?)
435   "Lookup a glyph by name."
436   (ly:font-get-glyph (ly:paper-get-font layout props)
437                      glyph-name))
438
439 (def-markup-command (char layout props num) (integer?)
440   "Produce a single character, e.g. @code{\\char #65} produces the 
441 letter 'A'."
442   (ly:get-glyph (ly:paper-get-font layout props) num))
443
444 (def-markup-command (raise layout props amount arg) (number? markup?)
445   "
446 This  raises  @var{arg}, by the distance @var{amount}.
447 A negative @var{amount} indicates lowering:
448 @c
449 @lilypond[verbatim,fragment,relative=1]
450  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
451 @end lilypond
452 The argument to @code{\\raise} is the vertical displacement amount,
453 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
454 raise objects in relation to their surrounding markups.
455
456 If the text object itself is positioned above or below the staff, then
457 @code{\\raise} cannot be used to move it, since the mechanism that
458 positions it next to the staff cancels any shift made with
459 @code{\\raise}. For vertical positioning, use the @code{padding}
460 and/or @code{extra-offset} properties. "
461   (ly:stencil-translate-axis (interpret-markup layout props arg)
462                              amount Y))
463
464 (def-markup-command (fraction layout props arg1 arg2) (markup? markup?)
465   "Make a fraction of two markups."
466   (let* ((m1 (interpret-markup layout props arg1))
467          (m2 (interpret-markup layout props arg2)))
468     (ly:stencil-align-to! m1 X CENTER)
469     (ly:stencil-align-to! m2 X CENTER)    
470     (let* ((x1 (ly:stencil-extent m1 X))
471            (x2 (ly:stencil-extent m2 X))
472            (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
473            ;; should stack mols separately, to maintain LINE on baseline
474            (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
475       (ly:stencil-align-to! stack Y CENTER)
476       (ly:stencil-align-to! stack X LEFT)
477       ;; should have EX dimension
478       ;; empirical anyway
479       (ly:stencil-translate-axis stack 0.75 Y))))
480
481
482 ;; TODO: better syntax.
483
484 (def-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
485   "Construct a note symbol, with stem.  By using fractional values for
486 @var{dir}, you can obtain longer or shorter stems."
487   (let* ((font (ly:paper-get-font layout (cons '((font-encoding . fetaMusic)) props)))
488          (size (chain-assoc-get 'font-size props 0))
489          (stem-length (* (magstep size) (max 3 (- log 1))))
490          (head-glyph (ly:font-get-glyph
491                       font
492                       (string-append "noteheads.s" (number->string (min log 2)))))
493          (stem-thickness 0.13)
494          (stemy (* dir stem-length))
495          (attachx (if (> dir 0)
496                       (- (cdr (ly:stencil-extent head-glyph X)) stem-thickness)
497                       0))
498          (attachy (* dir 0.28))
499          (stem-glyph (and (> log 0)
500                           (ly:round-filled-box
501                            (cons attachx (+ attachx  stem-thickness))
502                            (cons (min stemy attachy)
503                                  (max stemy attachy))
504                            (/ stem-thickness 3))))
505          (dot (ly:font-get-glyph font "dots.dot"))
506          (dotwid (interval-length (ly:stencil-extent dot X)))
507          (dots (and (> dot-count 0)
508                     (apply ly:stencil-add
509                            (map (lambda (x)
510                                   (ly:stencil-translate-axis
511                                    dot  (* (+ 1 (* 2 x)) dotwid) X))
512                                 (iota dot-count 1)))))
513          (flaggl (and (> log 2)
514                       (ly:stencil-translate
515                        (ly:font-get-glyph font
516                                           (string-append "flags."
517                                                          (if (> dir 0) "u" "d")
518                                                          (number->string log)))
519                        (cons (+ attachx (/ stem-thickness 2)) stemy)))))
520     (if flaggl
521         (set! stem-glyph (ly:stencil-add flaggl stem-glyph)))
522     (if (ly:stencil? stem-glyph)
523         (set! stem-glyph (ly:stencil-add stem-glyph head-glyph))
524         (set! stem-glyph head-glyph))
525     (if (ly:stencil? dots)
526         (set! stem-glyph
527               (ly:stencil-add
528                (ly:stencil-translate-axis dots
529                                           (+ (if (and (> dir 0) (> log 2))
530                                                  (* 1.5 dotwid)
531                                                  0)
532                                              ;; huh ? why not necessary?
533                                              ;;(cdr (ly:stencil-extent head-glyph X))
534                                              dotwid)
535                                           X)
536                stem-glyph)))
537     stem-glyph))
538
539 (use-modules (ice-9 regex))
540
541 (define-public log2 
542   (let ((divisor (log 2)))
543     (lambda (z) (inexact->exact (/ (log z) divisor)))))
544
545 (define (parse-simple-duration duration-string)
546   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."
547   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
548     (if (and match (string=? duration-string (match:substring match 0)))
549         (let ((len  (match:substring match 1))
550               (dots (match:substring match 2)))
551           (list (cond ((string=? len "breve")  -1)
552                       ((string=? len "longa")  -2)
553                       ((string=? len "maxima") -3)
554                       (else (log2 (string->number len))))
555                 (if dots (string-length dots) 0)))
556         (error "This is not a valid duration string:" duration-string))))
557
558 (def-markup-command (note layout props duration dir) (string? number?)
559   "This produces a note with a stem pointing in @var{dir} direction, with
560 the @var{duration} for the note head type and augmentation dots. For
561 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
562 a shortened down stem."
563   (let ((parsed (parse-simple-duration duration)))
564     (note-by-number-markup layout props (car parsed) (cadr parsed) dir)))
565
566 (def-markup-command (normal-size-super layout props arg) (markup?)
567   "Set @var{arg} in superscript with a normal font size."
568   (ly:stencil-translate-axis (interpret-markup
569                               layout
570                               props arg)
571                              (* 0.5  (chain-assoc-get 'baseline-skip props))
572                              Y))
573
574 (def-markup-command (super layout props arg) (markup?)
575   "
576 @cindex raising text
577 @cindex lowering text
578 @cindex moving text
579 @cindex translating text
580
581 @cindex @code{\\super}
582
583
584 Raising and lowering texts can be done with @code{\\super} and
585 @code{\\sub}:
586
587 @lilypond[verbatim,fragment,relative=1]
588  c1^\\markup { E \"=\" mc \\super \"2\" }
589 @end lilypond
590
591 "
592   (ly:stencil-translate-axis
593    (interpret-markup
594     layout
595     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
596     arg)
597    (* 0.5 (chain-assoc-get 'baseline-skip props))
598    Y))
599
600 (def-markup-command (translate layout props offset arg) (number-pair? markup?)
601   "This translates an object. Its first argument is a cons of numbers
602 @example
603 A \\translate #(cons 2 -3) @{ B C @} D
604 @end example
605 This moves `B C' 2 spaces to the right, and 3 down, relative to its
606 surroundings. This command cannot be used to move isolated scripts
607 vertically, for the same reason that @code{\\raise} cannot be used for
608 that.
609
610 "
611   (ly:stencil-translate (interpret-markup  layout props arg)
612                         offset))
613
614 (def-markup-command (sub layout props arg) (markup?)
615   "Set @var{arg} in subscript."
616   (ly:stencil-translate-axis
617    (interpret-markup
618     layout
619     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
620     arg)
621    (* -0.5 (chain-assoc-get 'baseline-skip props))
622    Y))
623
624 (def-markup-command (beam layout props width slope thickness) (number? number? number?)
625   "Create a beam with the specified parameters."
626
627   (let*
628       ((y (* slope width))
629        (yext (cons (min 0 y) (max 0 y)))
630        (half (/ thickness 2)))
631        
632     (ly:make-stencil
633      (list 'beam width
634            slope
635            thickness
636            (ly:output-def-lookup layout 'blotdiameter))
637      (cons 0 width)
638      (cons (+ (- half) (car yext))
639            (+ half (cdr yext))))
640
641     ))
642
643
644 (def-markup-command (normal-size-sub layout props arg) (markup?)
645   "Set @var{arg} in subscript, in a normal font size."
646   (ly:stencil-translate-axis
647    (interpret-markup layout props arg)
648    (* -0.5 (chain-assoc-get 'baseline-skip props))
649    Y))
650
651 (def-markup-command (hbracket layout props arg) (markup?)
652   "Draw horizontal brackets around @var{arg}."  
653   (let ((th 0.1) ;; todo: take from GROB.
654         (m (interpret-markup layout props arg)))
655     (bracketify-stencil m X th (* 2.5 th) th)))
656
657 (def-markup-command (bracket layout props arg) (markup?)
658   "Draw vertical brackets around @var{arg}."  
659   (let ((th 0.1) ;; todo: take from GROB.
660         (m (interpret-markup layout props arg)))
661     (bracketify-stencil m Y th (* 2.5 th) th)))
662
663 ;; todo: fix negative space
664 (def-markup-command (hspace layout props amount) (number?)
665   "This produces a invisible object taking horizontal space.
666 @example 
667 \\markup @{ A \\hspace #2.0 B @} 
668 @end example
669 will put extra space between A and B, on top of the space that is
670 normally inserted before elements on a line.
671 "
672   (if (> amount 0)
673       (ly:make-stencil "" (cons 0 amount) '(-1 . 1))
674       (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
675
676 (def-markup-command (override layout props new-prop arg) (pair? markup?)
677   "Add the first argument in to the property list.  Properties may be
678 any sort of property supported by @internalsref{font-interface} and
679 @internalsref{text-interface}, for example
680
681 @verbatim
682 \\override #'(font-family . married) \"bla\"
683 @end verbatim
684
685 "
686   (interpret-markup layout (cons (list new-prop) props) arg))
687
688 (def-markup-command (smaller layout props arg) (markup?)
689   "Decrease the font size relative to current setting"
690   (let* ((fs (chain-assoc-get 'font-size props 0))
691          (entry (cons 'font-size (- fs 1))))
692     (interpret-markup layout (cons (list entry) props) arg)))
693
694 (def-markup-command (bigger layout props arg) (markup?)
695   "Increase the font size relative to current setting"
696   (let* ((fs (chain-assoc-get 'font-size props 0))
697          (entry (cons 'font-size (+ fs 1))))
698     (interpret-markup layout (cons (list entry) props) arg)))
699
700 (def-markup-command larger (markup?) bigger-markup)
701
702 (def-markup-command (box layout props arg) (markup?)
703   "Draw a box round @var{arg}.  Looks at @code{thickness},
704 @code{box-padding} and @code{font-size} properties to determine line
705 thickness and padding around the markup."
706   (let* ((th (chain-assoc-get 'thickness props  0.1))
707          (size (chain-assoc-get 'font-size props 0))
708          (pad
709           (* (magstep size)
710              (chain-assoc-get 'box-padding props 0.2)))
711          (m (interpret-markup layout props arg)))
712     (box-stencil m th pad)))
713
714 ;;FIXME: is this working? 
715 (def-markup-command (strut layout props) ()
716   "Create a box of the same height as the space in the current font."
717   (let ((m (Text_interface::interpret_markup layout props " ")))
718     (ly:stencil-set-extent! m X '(1000 . -1000))
719     m))
720
721 (define number->mark-letter-vector (make-vector 25 #\A))
722
723 (do ((i 0 (1+ i))
724      (j 0 (1+ j)))
725     ((>= i 26))
726   (if (= i (- (char->integer #\I) (char->integer #\A)))
727       (set! i (1+ i)))
728   (vector-set! number->mark-letter-vector j
729                (integer->char (+ i (char->integer #\A)))))
730
731 (define number->mark-alphabet-vector (list->vector
732   (map (lambda (i) (integer->char (+ i (char->integer #\A)))) (iota 26))))
733
734 (define (number->markletter-string vec n)
735   "Double letters for big marks."
736   (let* ((lst (vector-length vec)))
737     
738     (if (>= n lst)
739         (string-append (number->markletter-string vec (1- (quotient n lst)))
740                        (number->markletter-string vec (remainder n lst)))
741         (make-string 1 (vector-ref vec n)))))
742
743 (def-markup-command (markletter layout props num) (integer?)
744   "Make a markup letter for @var{num}.  The letters start with A to Z
745  (skipping I), and continues with double letters."
746   (Text_interface::interpret_markup layout props
747     (number->markletter-string number->mark-letter-vector num)))
748
749 (def-markup-command (markalphabet layout props num) (integer?)
750    "Make a markup letter for @var{num}.  The letters start with A to Z
751  and continues with double letters."
752    (Text_interface::interpret_markup layout props
753      (number->markletter-string number->mark-alphabet-vector num)))
754
755 (def-markup-command (bracketed-y-column layout props indices args)
756   (list? markup-list?)
757   "Make a column of the markups in @var{args}, putting brackets around
758 the elements marked in @var{indices}, which is a list of numbers."
759   (define (sublist lst start stop)
760     (take (drop lst start) (- (1+ stop) start)))
761
762   (define (stencil-list-extent ss axis)
763     (cons
764      (apply min (map (lambda (x) (car (ly:stencil-extent x axis))) ss))
765      (apply max (map (lambda (x) (cdr (ly:stencil-extent x axis))) ss))))
766   
767   (define (stack-stencils stencils bskip last-stencil)
768     (cond
769      ((null? stencils) '())
770      ((not (ly:stencil? last-stencil))
771       (cons (car stencils)
772             (stack-stencils (cdr stencils) bskip (car stencils))))
773      (else
774       (let* ((orig (car stencils))
775              (dir (chain-assoc-get 'direction  props DOWN))
776              (new (ly:stencil-moved-to-edge last-stencil Y dir
777                                             orig
778                                             0.1 bskip)))
779
780         (cons new (stack-stencils (cdr stencils) bskip new))))))
781
782   (define (make-brackets stencils indices acc)
783     (if (and stencils
784              (pair? indices)
785              (pair? (cdr indices)))
786         (let* ((encl (sublist stencils (car indices) (cadr indices)))
787                (x-ext (stencil-list-extent encl X))
788                (y-ext (stencil-list-extent encl Y))
789                (thick 0.10)
790                (pad 0.35)
791                (protusion (* 2.5 thick))
792                (lb
793                 (ly:stencil-translate-axis 
794                  (ly:bracket Y y-ext thick protusion)
795                  (- (car x-ext) pad) X))
796                (rb (ly:stencil-translate-axis
797                     (ly:bracket Y y-ext thick (- protusion))
798                     (+ (cdr x-ext) pad) X)))
799
800           (make-brackets
801            stencils (cddr indices)
802            (append
803             (list lb rb)
804             acc)))
805         acc))
806
807   (let* ((stencils
808           (map (lambda (x)
809                  (interpret-markup
810                   layout
811                   props
812                   x)) args))
813          (leading
814           (chain-assoc-get 'baseline-skip props))
815          (stacked (stack-stencils
816                    (remove ly:stencil-empty? stencils) 1.25 #f))
817          (brackets (make-brackets stacked indices '())))
818
819     (apply ly:stencil-add
820            (append stacked brackets))))