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