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