]> 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--2004  Han-Wen Nienhuys <hanwen@cs.uu.nl>
6 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
7
8 ;;; markup commands
9 ;;; TODO:
10 ;;;  * each markup function should have a doc string with
11 ;;     syntax, description and example. 
12
13
14 (def-markup-command (word paper props str) (string?)
15   "A single word."
16   (interpret-markup paper props str))
17   
18 (def-markup-command (simple paper props str) (string?)
19   "A simple text-string; @code{\\markup @{ foo @}} is equivalent with
20 @code{\\markup @{ \\simple #\"foo\" @}}."
21     (interpret-markup paper props
22                       (make-line-markup
23                        (map make-word-markup (string-tokenize str)))))
24
25 (define-public empty-markup
26   (make-simple-markup ""))
27
28 ;;(def-markup-command (fill-line paper props line-width markups)
29 ;;  (number? markup-list?)
30 ;; no parser tag -- should make number? markuk-list? thingy
31 (def-markup-command (fill-line paper props markups)
32   (markup-list?)
33   "Put @var{markups} in a horizontal line of width @var{line-width}.
34    The markups are spaced/flushed to fill the entire line."
35
36   (let* ((stencils (map (lambda (x) (interpret-markup paper props x))
37                         markups))
38          (text-width (apply + (map interval-length
39                                    (map (lambda (x)
40                                           (ly:stencil-get-extent x X))
41                                         stencils))))
42         (word-count (length markups))
43         (word-space (cdr (chain-assoc 'word-space props)))
44         (line-width (cdr (chain-assoc 'linewidth props)))
45         (fill-space (if (< line-width text-width)
46                         word-space
47                         (/ (- line-width text-width)
48                            (if (= word-count 1) 2 (- word-count 1)))))
49         (line-stencils (if (= word-count 1)
50                            (map (lambda (x) (interpret-markup paper props x))
51                                 (list (make-word-markup "")
52                                       (car markups)
53                                       (make-word-markup "")))
54                                 stencils)))
55     (stack-stencil-line fill-space line-stencils)))
56   
57 (define (font-markup qualifier value)
58   (lambda (paper props arg)
59     (interpret-markup paper
60                       (prepend-alist-chain qualifier value props)
61                       arg)))
62
63 (def-markup-command (line paper props args) (markup-list?)
64   "Put @var{args} in a horizontal line.  The property @code{word-space}
65 determines the space between each markup in @var{args}."
66   (stack-stencil-line
67    (cdr (chain-assoc 'word-space props))
68    (map (lambda (m) (interpret-markup paper props m)) args)))
69
70 (def-markup-command (combine paper props m1 m2) (markup? markup?)
71   "Print two markups on top of each other."
72   (ly:stencil-add
73    (interpret-markup paper props m1)
74    (interpret-markup paper props m2)))
75
76 (def-markup-command (finger paper props arg) (markup?)
77   "Set the argument as small numbers."
78   (interpret-markup paper
79                     (cons '((font-size . -4) (font-family . number)) props)
80                     arg))
81
82 (def-markup-command (fontsize paper props mag arg) (number? markup?)
83   "This sets the relative font size, eg.
84 @example
85 A \\fontsize #2 @{ B C @} D
86 @end example
87
88
89 This will enlarge the B and the C by two steps.
90 "
91   (interpret-markup
92    paper 
93    (prepend-alist-chain 'font-size mag props)
94    arg))
95
96 (def-markup-command (magnify paper props sz arg) (number? markup?)
97   "This sets the font magnification for the its argument. In the following
98 example, the middle A will be 10% larger:
99 @example
100 A \\magnify #1.1 @{ A @} A
101 @end example
102
103 Note: magnification only works if a font-name is explicitly selected.
104 Use @code{\\fontsize} otherwise."
105
106   (interpret-markup
107    paper 
108    (prepend-alist-chain 'font-magnification sz props)
109    arg))
110
111 (def-markup-command (bold paper props arg) (markup?)
112   "Switch to bold font-series"
113   (interpret-markup paper (prepend-alist-chain 'font-series 'bold props) arg))
114
115 (def-markup-command (sans paper props arg) (markup?)
116   "Switch to the sans-serif family"
117   (interpret-markup paper (prepend-alist-chain 'font-family 'sans props) arg))
118
119 (def-markup-command (number paper props arg) (markup?)
120   "Set font family to @code{number}, which yields the font used for
121 time signatures and fingerings.  This font only contains numbers and
122 some punctuation. It doesn't have any letters.  "
123   (interpret-markup paper (prepend-alist-chain 'font-family 'number props) arg))
124
125 (def-markup-command (roman paper props arg) (markup?)
126   "Set font family to @code{roman}."
127   (interpret-markup paper (prepend-alist-chain 'font-family 'roman props) arg))
128
129 (def-markup-command (huge paper props arg) (markup?)
130   "Set font size to +2."
131   (interpret-markup paper (prepend-alist-chain 'font-size 2 props) arg))
132
133 (def-markup-command (large paper props arg) (markup?)
134   "Set font size to +1."
135   (interpret-markup paper (prepend-alist-chain 'font-size 1 props) arg))
136
137 (def-markup-command (normalsize paper props arg) (markup?)
138   "Set font size to default."
139   (interpret-markup paper (prepend-alist-chain 'font-size 0 props) arg))
140
141 (def-markup-command (small paper props arg) (markup?)
142   "Set font size to -1."
143   (interpret-markup paper (prepend-alist-chain 'font-size -1 props) arg))
144
145 (def-markup-command (tiny paper props arg) (markup?)
146   "Set font size to -2."
147   (interpret-markup paper (prepend-alist-chain 'font-size -2 props) arg))
148
149 (def-markup-command (teeny paper props arg) (markup?)
150   "Set font size to -3."
151   (interpret-markup paper (prepend-alist-chain 'font-size -3 props) arg))
152
153 (def-markup-command (caps paper props arg) (markup?)
154   "Set font shape to @code{caps}."
155   (interpret-markup paper (prepend-alist-chain 'font-shape 'caps props) arg))
156
157 (def-markup-command (dynamic paper props arg) (markup?)
158   "Use the dynamic font.  This font only contains s, f, m, z, p, and
159 r.  When producing phrases, like ``piu f'', the normal words (like
160 ``piu'') should be done in a different font.  The recommend font for
161 this is bold and italic
162 "
163   (interpret-markup
164    paper (prepend-alist-chain 'font-family 'dynamic props) arg))
165
166 (def-markup-command (italic paper props arg) (markup?)
167   (interpret-markup paper (prepend-alist-chain 'font-shape 'italic props) arg))
168
169 (def-markup-command (typewriter paper props arg) (markup?)
170   (interpret-markup
171    paper (prepend-alist-chain 'font-family 'typewriter props) arg))
172
173 (def-markup-command (upright paper props arg) (markup?)
174   "Set font shape to @code{upright}."
175   (interpret-markup
176    paper (prepend-alist-chain 'font-shape 'upright props) arg))
177
178 (def-markup-command (doublesharp paper props) ()
179   (interpret-markup paper props (markup #:musicglyph "accidentals-4")))
180 (def-markup-command (threeqsharp paper props) ()
181   (interpret-markup paper props (markup #:musicglyph "accidentals-3")))
182 (def-markup-command (sharp paper props) ()
183   (interpret-markup paper props (markup #:musicglyph "accidentals-2")))
184 (def-markup-command (semisharp paper props) ()
185   (interpret-markup paper props (markup #:musicglyph "accidentals-1")))
186 (def-markup-command (natural paper props) ()
187   (interpret-markup paper props (markup #:musicglyph "accidentals-0")))
188 (def-markup-command (semiflat paper props) ()
189   (interpret-markup paper props (markup #:musicglyph "accidentals--1")))
190 (def-markup-command (flat paper props) ()
191   (interpret-markup paper props (markup #:musicglyph "accidentals--2")))
192 (def-markup-command (threeqflat paper props) ()
193   (interpret-markup paper props (markup #:musicglyph "accidentals--3")))
194 (def-markup-command (doubleflat paper props) ()
195   (interpret-markup paper props (markup #:musicglyph "accidentals--4")))
196
197
198 (def-markup-command (column paper props args) (markup-list?)
199   (stack-lines
200    -1 0.0 (cdr (chain-assoc 'baseline-skip props))
201    (map (lambda (m) (interpret-markup paper props m)) args)))
202
203 (def-markup-command (dir-column paper props args) (markup-list?)
204   "Make a column of args, going up or down, depending on the setting
205 of the #'direction layout property."
206   (let* ((dir (cdr (chain-assoc 'direction props))))
207     (stack-lines
208      (if (number? dir) dir -1)
209      0.0
210      (cdr (chain-assoc 'baseline-skip props))
211      (map (lambda (x) (interpret-markup paper props x)) args))))
212
213 (def-markup-command (center-align paper props args) (markup-list?)
214   (let* ((mols (map (lambda (x) (interpret-markup paper props x)) args))
215          (cmols (map (lambda (x) (ly:stencil-align-to! x X CENTER)) mols)))
216     (stack-lines -1 0.0 (cdr (chain-assoc 'baseline-skip props)) mols)))
217
218 (def-markup-command (right-align paper props arg) (markup?)
219   (let* ((m (interpret-markup paper props arg)))
220     (ly:stencil-align-to! m X RIGHT)
221     m))
222
223 (def-markup-command (left-align paper props arg) (markup?)
224   (let* ((m (interpret-markup paper props arg)))
225     (ly:stencil-align-to! m X LEFT)
226     m))
227
228 (def-markup-command (halign paper props dir arg) (number? markup?)
229   "Set horizontal alignment. @var{dir} = -1 is left, @var{dir} = 1 is
230 right, values in between vary alignment accordingly."
231
232   
233   (let* ((m (interpret-markup paper props arg)))
234     (ly:stencil-align-to! m X dir)
235     m))
236
237 (def-markup-command (musicglyph paper props glyph-name) (string?)
238   "This is converted to a musical symbol, e.g. @code{\\musicglyph
239 #\"accidentals-0\"} will select the natural sign from the music font.
240 See @usermanref{The Feta font} for  a complete listing of the possible glyphs.
241 "
242   (ly:find-glyph-by-name
243    (ly:paper-get-font paper (cons '((font-name . ())
244                                     (font-shape . *)
245                                     (font-series . *)
246                                     (font-family . music))
247                                   props))
248    glyph-name))
249
250
251 (def-markup-command (lookup paper props glyph-name) (string?)
252   "Lookup a glyph by name."
253   (ly:find-glyph-by-name (ly:paper-get-font paper props)
254                          glyph-name))
255
256 (def-markup-command (char paper props num) (integer?)
257   "This produces a single character, e.g. @code{\\char #65} produces the 
258 letter 'A'."
259   (ly:get-glyph (ly:paper-get-font paper props) num))
260
261 (def-markup-command (raise paper props amount arg) (number? markup?)
262   "
263 This  raises  @var{arg}, by the distance @var{amount}.
264 A negative @var{amount} indicates lowering:
265 @c
266 @lilypond[verbatim,fragment,relative=1,quote]
267  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
268 @end lilypond
269 The argument to @code{\\raise} is the vertical displacement amount,
270 measured in (global) staff spaces.  @code{\\raise} and @code{\\super}
271 raise objects in relation to their surrounding markups.
272
273 If the text object itself is positioned above or below the staff, then
274 @code{\\raise} cannot be used to move it, since the mechanism that
275 positions it next to the staff cancels any shift made with
276 @code{\\raise}. For vertical positioning, use the @code{padding}
277 and/or @code{extra-offset} properties. "
278
279   
280   (ly:stencil-translate-axis (interpret-markup paper props arg)
281                               amount Y))
282
283 (def-markup-command (fraction paper props arg1 arg2) (markup? markup?)
284   "Make a fraction of two markups.
285
286 Syntax: \\fraction MARKUP1 MARKUP2."
287   (let* ((m1 (interpret-markup paper props arg1))
288          (m2 (interpret-markup paper props arg2)))
289     (ly:stencil-align-to! m1 X CENTER)
290     (ly:stencil-align-to! m2 X CENTER)    
291     (let* ((x1 (ly:stencil-get-extent m1 X))
292            (x2 (ly:stencil-get-extent m2 X))
293            (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
294            ;; should stack mols separately, to maintain LINE on baseline
295            (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
296       (ly:stencil-align-to! stack Y CENTER)
297       (ly:stencil-align-to! stack X LEFT)
298       ;; should have EX dimension
299       ;; empirical anyway
300       (ly:stencil-translate-axis stack 0.75 Y))))
301
302
303 ;; TODO: better syntax.
304
305 (def-markup-command (note-by-number paper props log dot-count dir) (number? number? number?)
306   "Syntax: \\note-by-number #LOG #DOTS #DIR.  By using fractional values
307 for DIR, you can obtain longer or shorter stems."
308   (let* ((font (ly:paper-get-font paper (cons '((font-family .  music)) props)))
309          (stemlen (max 3 (- log 1)))
310          (headgl (ly:find-glyph-by-name
311                   font
312                   (string-append "noteheads-" (number->string (min log 2)))))
313          (stemth 0.13)
314          (stemy (* dir stemlen))
315          (attachx (if (> dir 0)
316                       (- (cdr (ly:stencil-get-extent headgl X)) stemth)
317                       0))
318          (attachy (* dir 0.28))
319          (stemgl (and (> log 0)
320                       (ly:round-filled-box
321                        (cons attachx (+ attachx  stemth))
322                        (cons (min stemy attachy)
323                              (max stemy attachy))
324                        (/ stemth 3))))
325          (dot (ly:find-glyph-by-name font "dots-dot"))
326          (dotwid (interval-length (ly:stencil-get-extent dot X)))
327          (dots (and (> dot-count 0)
328                     (apply ly:stencil-add
329                            (map (lambda (x)
330                                   (ly:stencil-translate-axis
331                                    dot  (* (+ 1 (* 2 x)) dotwid) X) )
332                                 (iota dot-count 1)))))
333          (flaggl (and (> log 2)
334                       (ly:stencil-translate
335                        (ly:find-glyph-by-name font
336                                               (string-append "flags-"
337                                                              (if (> dir 0) "u" "d")
338                                                              (number->string log)))
339                        (cons (+ attachx (/ stemth 2)) stemy)))))
340     (if flaggl
341         (set! stemgl (ly:stencil-add flaggl stemgl)))
342     (if (ly:stencil? stemgl)
343         (set! stemgl (ly:stencil-add stemgl headgl))
344         (set! stemgl headgl))
345     (if (ly:stencil? dots)
346         (set! stemgl
347               (ly:stencil-add
348                (ly:stencil-translate-axis dots
349                                            (+ (if (and (> dir 0) (> log 2))
350                                                   (* 1.5 dotwid)
351                                                   0)
352                                               ;; huh ? why not necessary?
353                                               ;;(cdr (ly:stencil-get-extent headgl X))
354                                               dotwid)
355                                            X)
356                stemgl)))
357     stemgl))
358
359 (use-modules (ice-9 regex))
360
361 (define-public log2 
362   (let ((divisor (log 2)))
363     (lambda (z) (inexact->exact (/ (log z) divisor)))))
364
365 (define (parse-simple-duration duration-string)
366   "Parse the `duration-string', eg ''4..'' or ''breve.'', and return a (log dots) list."
367   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
368     (if (and match (string=? duration-string (match:substring match 0)))
369         (let ((len  (match:substring match 1))
370               (dots (match:substring match 2)))
371           (list (cond ((string=? len "breve")  -1)
372                       ((string=? len "longa")  -2)
373                       ((string=? len "maxima") -3)
374                       (else (log2 (string->number len))))
375                 (if dots (string-length dots) 0)))
376         (error "This is not a valid duration string:" duration-string))))
377
378 (def-markup-command (note paper props duration dir) (string? number?)
379   "This produces a note with a stem pointing in @var{dir} direction, with
380 the @var{duration} for the note head type and augmentation dots. For
381 example, @code{\\note #\"4.\" #-0.75} creates a dotted quarter note, with
382 a shortened down stem."
383   
384   (let ((parsed (parse-simple-duration duration)))
385     (note-by-number-markup paper props (car parsed) (cadr parsed) dir)))
386
387 (def-markup-command (normal-size-super paper props arg) (markup?)
388   "A superscript which does not use a smaller font."
389   (ly:stencil-translate-axis (interpret-markup
390                                paper
391                                props arg)
392                               (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
393                               Y))
394
395 (def-markup-command (super paper props arg) (markup?)
396   "
397 @cindex raising text
398 @cindex lowering text
399 @cindex moving text
400 @cindex translating text
401
402 @cindex @code{\\super}
403
404
405 Raising and lowering texts can be done with @code{\\super} and
406 @code{\\sub}:
407
408 @lilypond[verbatim,fragment,relative=1]
409  c1^\\markup { E \"=\" mc \\super \"2\" }
410 @end lilypond
411
412 "
413   
414   (ly:stencil-translate-axis
415    (interpret-markup
416     paper
417     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
418     arg)
419    (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
420    Y))
421
422 (def-markup-command (translate paper props offset arg) (number-pair? markup?)
423   "This translates an object. Its first argument is a cons of numbers
424 @example
425 A \\translate #(cons 2 -3) @{ B C @} D
426 @end example
427 This moves `B C' 2 spaces to the right, and 3 down, relative to its
428 surroundings. This command cannot be used to move isolated scripts
429 vertically, for the same reason that @code{\\raise} cannot be used for
430 that.
431
432 . "
433   (ly:stencil-translate (interpret-markup  paper props arg)
434                          offset))
435
436 (def-markup-command (sub paper props arg) (markup?)
437   "Syntax: \\sub MARKUP."
438   (ly:stencil-translate-axis
439    (interpret-markup
440     paper
441     (cons `((font-size . ,(- (chain-assoc-get 'font-size props 0) 3))) props)
442     arg)
443    (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
444    Y))
445
446 (def-markup-command (normal-size-sub paper props arg) (markup?)
447   (ly:stencil-translate-axis
448    (interpret-markup paper props arg)
449    (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
450    Y))
451
452 (def-markup-command (hbracket paper props arg) (markup?)
453   "Horizontal brackets around @var{arg}."  
454   (let ((th 0.1) ;; todo: take from GROB.
455         (m (interpret-markup paper props arg)))
456     (bracketify-stencil m X th (* 2.5 th) th)))
457
458 (def-markup-command (bracket paper props arg) (markup?)
459   "Vertical brackets around @var{arg}."  
460   (let ((th 0.1) ;; todo: take from GROB.
461         (m (interpret-markup paper props arg)))
462     (bracketify-stencil m Y th (* 2.5 th) th)))
463
464 ;; todo: fix negative space
465 (def-markup-command (hspace paper props amount) (number?)
466   "This produces a invisible object taking horizontal space.
467 @example 
468 \\markup @{ A \\hspace #2.0 B @} 
469 @end example
470 will put extra space between A and B, on top of the space that is
471 normally inserted before elements on a line.
472 "
473   (if (> amount 0)
474       (ly:make-stencil "" (cons 0 amount) '(-1 . 1) )
475       (ly:make-stencil "" (cons amount amount) '(-1 . 1))))
476
477 (def-markup-command (override paper props new-prop arg) (pair? markup?)
478   "Add the first argument in to the property list.  Properties may be
479 any sort of property supported by @internalsref{font-interface} and
480 @internalsref{text-interface}, for example
481
482 @verbatim
483 \\override #'(font-family . married) \"bla\"
484 @end verbatim
485
486 "
487   (interpret-markup paper (cons (list new-prop) props) arg))
488
489 (def-markup-command (smaller paper props arg) (markup?)
490   "Decrease the font size relative to current setting"
491   (let* ((fs (chain-assoc-get 'font-size props 0))
492          (entry (cons 'font-size (- fs 1))))
493     (interpret-markup paper (cons (list entry) props) arg)))
494
495
496 (def-markup-command (bigger paper props arg) (markup?)
497   "Increase the font size relative to current setting"
498   (let* ((fs (chain-assoc-get 'font-size props 0))
499          (entry (cons 'font-size (+ fs 1))))
500     (interpret-markup paper (cons (list entry) props) arg)))
501
502 (def-markup-command larger (markup?)
503   bigger-markup)
504
505 (def-markup-command (box paper props arg) (markup?)
506   "Draw a box round @var{arg}"
507   
508   (let ((th 0.1)
509         (pad 0.2)
510         (m (interpret-markup paper props arg)))
511     (box-stencil m th pad)))
512
513 (def-markup-command (strut paper props) ()
514   
515   "Create a box of the same height as the space in the current font.
516
517 FIXME: is this working? 
518 "
519   
520   (let ((m (Text_item::interpret_markup paper props " ")))
521     (ly:stencil-set-extent! m X '(1000 . -1000))
522     m))
523
524 (define number->mark-letter-vector (make-vector 25 #\A))
525
526 (do ((i 0 (1+ i))
527      (j 0 (1+ j)))
528     ((>= i 26))
529   (if (= i (- (char->integer #\I) (char->integer #\A)))
530       (set! i (1+ i)))
531   (vector-set! number->mark-letter-vector j
532                (integer->char (+ i (char->integer #\A)))))
533
534 (define (number->markletter-string n)
535   "Double letters for big marks."
536   (let*
537       ((l (vector-length number->mark-letter-vector)))
538     
539   (if (>= n l)
540       (string-append (number->markletter-string (1- (quotient n l)))
541                      (number->markletter-string (remainder n l)))
542       (make-string 1 (vector-ref number->mark-letter-vector n)))))
543
544
545 (def-markup-command (markletter paper props num) (integer?)
546    "Make a markup letter for @var{num}.  The letters start with A to Z
547  (skipping I), and continues with double letters."
548  
549    (Text_item::interpret_markup paper props (number->markletter-string num)))