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