]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
64a113973880e2f11e4b157aba24b6b368b5469b
[lilypond.git] / scm / markup.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2003--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 (defmacro*-public markup (#:rest body)
19   "The `markup' macro provides a lilypond-like syntax for building markups.
20
21  - #:COMMAND is used instead of \\COMMAND
22  - #:line ( ... ) is used instead of \\line { ... }
23  - etc.
24
25 Example:
26   \\markup { foo
27             \\raise #0.2 \\hbracket \\bold bar
28             \\override #'(baseline-skip . 4)
29             \\bracket \\column { baz bazr bla }
30   }
31          <==>
32   (markup \"foo\"
33           #:raise 0.2 #:hbracket #:bold \"bar\"
34           #:override '(baseline-skip . 4)
35           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))"
36
37   (car (compile-all-markup-expressions `(#:line ,body))))
38
39 ;; utility
40
41 (define (markup-join markups sep)
42   "Return line-markup of MARKUPS, joining them with markup SEP"
43   (if (pair? markups)
44       (make-line-markup (list-insert-separator markups sep))
45       empty-markup))
46
47
48 (define-public interpret-markup ly:text-interface::interpret-markup)
49
50 (define-public (interpret-markup-list layout props markup-list)
51   (fold-right
52    (lambda (m prev)
53      (if (markup-command-list? m)
54          (append (apply (car m) layout props (cdr m)) prev)
55          (cons (interpret-markup layout props m) prev)))
56    '()
57    markup-list))
58
59 (define-public (prepend-alist-chain key val chain)
60   (cons (acons key val (car chain)) (cdr chain)))
61
62 (define-public (stack-stencil-line space stencils)
63   "Adjoin a list of @var{stencils} along the X axis, leaving
64 @var{space} between the end of each stencil and the beginning of the
65 following stencil.  Stencils with empty Y extent are not given
66 @var{space} before them and don't avoid overlapping other stencils."
67   (stack-stencils X RIGHT space (filter ly:stencil? stencils)))
68
69 ;;; convert a full markup object to an approximate pure string representation
70
71 (define-public (markup->string m . argscopes)
72   (let* ((scopes (if (pair? argscopes) (car argscopes) '())))
73     ;; markup commands with one markup argument, formatting ignored
74     (define markups-first-argument '(list
75                                      bold-markup box-markup caps-markup dynamic-markup finger-markup
76                                      fontCaps-markup huge-markup italic-markup large-markup larger-markup
77                                      medium-markup normal-size-sub-markup normal-size-super-markup
78                                      normal-text-markup normalsize-markup number-markup roman-markup
79                                      sans-markup simple-markup small-markup smallCaps-markup smaller-markup
80                                      sub-markup super-markup teeny-markup text-markup tiny-markup
81                                      typewriter-markup underline-markup upright-markup bracket-markup
82                                      circle-markup hbracket-markup parenthesize-markup rounded-box-markup
83
84                                      center-align-markup center-column-markup column-markup dir-column-markup
85                                      fill-line-markup justify-markup justify-string-markup left-align-markup
86                                      left-column-markup line-markup right-align-markup right-column-markup
87                                      vcenter-markup wordwrap-markup wordwrap-string-markup ))
88
89     ;; markup commands with markup as second argument, first argument
90     ;; specifies some formatting and is ignored
91     (define markups-second-argument '(list
92                                       abs-fontsize-markup fontsize-markup magnify-markup lower-markup
93                                       pad-around-markup pad-markup-markup pad-x-markup raise-markup
94                                       halign-markup hcenter-in-markup rotate-markup translate-markup
95                                       translate-scaled-markup with-url-markup scale-markup ))
96
97     ;; helper functions to handle string cons like string lists
98     (define (markup-cons->string-cons c scopes)
99       (if (not (pair? c)) (markup->string c scopes)
100           (cons (markup->string (car c) scopes) (markup-cons->string-cons (cdr c) scopes))))
101     (define (string-cons-join c)
102       (if (not (pair? c)) c
103           (string-join (list (car c) (string-cons-join (cdr c))) "")))
104
105     (cond
106      ((string? m) m)
107      ((null? m) "")
108      ((not (pair? m)) "")
109
110      ;; handle \concat (string-join without spaces)
111      ((and (pair? m) (equal? (car m) concat-markup))
112       (string-cons-join (markup-cons->string-cons (cadr m) scopes)) )
113
114      ;; markup functions with the markup as first arg
115      ((member (car m) (primitive-eval markups-first-argument))
116       (markup->string (cadr m) scopes))
117
118      ;; markup functions with markup as second arg
119      ((member (car m) (primitive-eval markups-second-argument))
120       (markup->string (cddr m) scopes))
121
122      ;; fromproperty-markup reads property values from the header block:
123      ((equal? (car m) fromproperty-markup)
124       (let* ((varname (symbol->string (cadr m)))
125              ;; cut off the header: prefix from the variable name:
126              (newvarname (if (string-prefix? "header:" varname) (substring varname 7) varname))
127              (var (string->symbol newvarname))
128              (mod (make-module 1)))
129         ;; Prevent loops by temporarily clearing the variable we have just looked up
130         (module-define! mod var "")
131         (markup->string (ly:modules-lookup scopes var) (cons mod scopes))))
132
133      ;; ignore all other markup functions
134      ((markup-function? (car m)) "")
135
136      ;; handle markup lists
137      ((list? m)
138       (string-join (map (lambda (mm) (markup->string mm scopes)) m) " "))
139
140      (else "ERROR, unable to extract string from markup"))))