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