]> git.donarmstrong.com Git - lilypond.git/blob - scm/markup.scm
Doc-it: add chapter 1.3 of notation reference
[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   ;; This relies on the markup list returned by a markup list command
52   ;; to be modifiable
53   (reverse!
54    (fold
55     (lambda (m prev)
56       (if (markup-command-list? m)
57           (reverse! (apply (car m) layout props (cdr m)) prev)
58           (cons (interpret-markup layout props m) prev)))
59     '()
60     markup-list)))
61
62 (define-public (prepend-alist-chain key val chain)
63   (cons (acons key val (car chain)) (cdr chain)))
64
65 (define-public (stack-stencil-line space stencils)
66   "Adjoin a list of STENCILS along the X axis, leaving SPACE between the
67    end of each stencil and the reference point of the following stencil."
68   (if (and (pair? stencils)
69            (ly:stencil? (car stencils)))
70
71       (if (and (pair? (cdr stencils))
72                (ly:stencil? (cadr stencils)))
73           (let* ((tail (stack-stencil-line space (cdr stencils)))
74                  (head (car stencils))
75                  (xoff (+ space (interval-end (ly:stencil-extent head X)))))
76             (ly:stencil-add head
77                             (ly:stencil-translate-axis tail xoff X)))
78           (car stencils))
79       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
80
81
82 ;;; convert a full markup object to an approximate pure string representation
83
84 (define-public (markup->string m . argscopes)
85 (let* ((scopes (if (pair? argscopes) (car argscopes) '())))
86   ;; markup commands with one markup argument, formatting ignored
87   (define markups-first-argument '(list
88                                    bold-markup box-markup caps-markup dynamic-markup finger-markup
89                                    fontCaps-markup huge-markup italic-markup large-markup larger-markup
90                                    medium-markup normal-size-sub-markup normal-size-super-markup
91                                    normal-text-markup normalsize-markup number-markup roman-markup
92                                    sans-markup simple-markup small-markup smallCaps-markup smaller-markup
93                                    sub-markup super-markup teeny-markup text-markup tiny-markup
94                                    typewriter-markup underline-markup upright-markup bracket-markup
95                                    circle-markup hbracket-markup parenthesize-markup rounded-box-markup
96
97                                    center-align-markup center-column-markup column-markup dir-column-markup
98                                    fill-line-markup justify-markup justify-string-markup left-align-markup
99                                    left-column-markup line-markup right-align-markup right-column-markup
100                                    vcenter-markup wordwrap-markup wordwrap-string-markup ))
101
102   ;; markup commands with markup as second argument, first argument
103   ;; specifies some formatting and is ignored
104   (define markups-second-argument '(list
105                                     abs-fontsize-markup fontsize-markup magnify-markup lower-markup
106                                     pad-around-markup pad-markup-markup pad-x-markup raise-markup
107                                     halign-markup hcenter-in-markup rotate-markup translate-markup
108                                     translate-scaled-markup with-url-markup scale-markup ))
109
110   ;; helper functions to handle string cons like string lists
111   (define (markup-cons->string-cons c scopes)
112     (if (not (pair? c)) (markup->string c scopes)
113         (cons (markup->string (car c) scopes) (markup-cons->string-cons (cdr c) scopes))))
114   (define (string-cons-join c)
115     (if (not (pair? c)) c
116         (string-join (list (car c) (string-cons-join (cdr c))) "")))
117
118   (cond
119    ((string? m) m)
120    ((null? m) "")
121    ((not (pair? m)) "")
122
123    ;; handle \concat (string-join without spaces)
124    ((and (pair? m) (equal? (car m) concat-markup))
125     (string-cons-join (markup-cons->string-cons (cadr m) scopes)) )
126
127    ;; markup functions with the markup as first arg
128    ((member (car m) (primitive-eval markups-first-argument))
129     (markup->string (cadr m) scopes))
130
131    ;; markup functions with markup as second arg
132    ((member (car m) (primitive-eval markups-second-argument))
133     (markup->string (cddr m) scopes))
134
135    ;; fromproperty-markup reads property values from the header block:
136    ((equal? (car m) fromproperty-markup)
137     (let* ((varname (symbol->string (cadr m)))
138            ;; cut off the header: prefix from the variable name:
139            (newvarname (if (string-prefix? "header:" varname) (substring varname 7) varname))
140            (var (string->symbol newvarname))
141            (mod (make-module 1)))
142       ;; Prevent loops by temporarily clearing the variable we have just looked up
143       (module-define! mod var "")
144       (markup->string (ly:modules-lookup scopes var) (cons mod scopes))))
145
146    ;; ignore all other markup functions
147    ((markup-function? (car m)) "")
148
149    ;; handle markup lists
150    ((list? m)
151     (string-join (map (lambda (mm) (markup->string mm scopes)) m) " "))
152
153    (else "ERROR, unable to extract string from markup"))))