]> git.donarmstrong.com Git - lilypond.git/blob - scm/new-markup.scm
(stack-stencil-line): robustness.
[lilypond.git] / scm / new-markup.scm
1 ;;;; new-markup.scm -- 
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  2003--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
6
7 "
8 Internally markup is stored as lists, whose head is a function.
9
10   (FUNCTION ARG1 ARG2 ... )
11
12 When the markup is formatted, then FUNCTION is called as follows
13
14   (FUNCTION GROB PROPS ARG1 ARG2 ... ) 
15
16 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
17 the rest of the arguments.
18
19 The function should return a stencil (i.e. a formatted, ready to
20 print object).
21
22
23 To add a function, use the def-markup-command utility.
24
25   (def-markup-command (mycommand paper prop arg1 ...) (arg1-type? ...)
26     \"my command usage and description\"
27     ...function body...)
28
29 The command is now available in markup mode, e.g.
30
31
32   \\markup { .... \\MYCOMMAND #1 argument ... }
33
34 " ; "
35
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;; markup definer utilities
38 ;;; `def-markup-command' can be used both for built-in markup
39 ;;; definitions and user defined markups.
40
41 (defmacro-public def-markup-command (command-and-args signature . body)
42   "
43
44 * Define a COMMAND-markup function after command-and-args and body,
45 register COMMAND-markup and its signature,
46
47 * add COMMAND-markup to markup-function-list,
48
49 * sets COMMAND-markup markup-signature and markup-keyword object properties,
50
51 * define a make-COMMAND-markup function.
52
53 Syntax:
54   (def-markup-command (COMMAND paper props arg1 arg2 ...) (arg1-type? arg2-type? ...)
55     \"documentation string\"
56     ...command body...)
57  or:
58   (def-markup-command COMMAND (arg1-type? arg2-type? ...)
59     function)
60 "
61   (let* ((command (if (pair? command-and-args) (car command-and-args) command-and-args))
62          (args (if (pair? command-and-args) (cdr command-and-args) '()))
63          (command-name (string->symbol (string-append (symbol->string command) "-markup")))
64          (make-markup-name (string->symbol (string-append "make-" (symbol->string command-name)))))
65     `(begin
66        (define-public ,(if (pair? args)
67                            (cons command-name args)
68                            command-name)
69          ,@body)
70        (set! (markup-command-signature ,command-name) (list ,@signature))
71        (if (not (member ,command-name markup-function-list))
72            (set! markup-function-list (cons ,command-name markup-function-list)))
73        (define-public (,make-markup-name . args)
74          (let ((sig (list ,@signature)))
75            (make-markup ,command-name ,(symbol->string make-markup-name) sig args))))))
76
77 (define-public (make-markup markup-function make-name signature args)
78   " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
79 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
80 "
81   (let* ((arglen (length args))
82          (siglen (length signature))
83          (error-msg (if (and (> siglen 0) (> arglen 0))
84                         (markup-argument-list-error signature args 1)
85                         #f)))
86     (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
87         (scm-error 'markup-format make-name
88                    "Expect ~A arguments for ~A. Found ~A: ~S"
89                    (list siglen make-name arglen args)
90                    #f))
91     (if error-msg
92         (scm-error 'markup-format make-name
93                    "Invalid argument in position ~A\nExpect: ~A\nFound: ~S."
94                    error-msg #f)
95         (cons markup-function args))))
96
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
98 ;;; markup constructors
99 ;;; lilypond-like syntax for markup construction in scheme.
100
101 (use-modules (ice-9 optargs)
102              (ice-9 receive))
103
104 (defmacro*-public markup (#:rest body)
105   "The `markup' macro provides a lilypond-like syntax for building markups.
106
107  - #:COMMAND is used instead of \\COMMAND
108  - #:lines ( ... ) is used instead of { ... }
109  - #:center ( ... ) is used instead of \\center < ... >
110  - etc.
111
112 Example:
113   \\markup { foo
114             \\raise #0.2 \\hbracket \\bold bar
115             \\override #'(baseline-skip . 4)
116             \\bracket \\column < baz bazr bla >
117   }
118          <==>
119   (markup \"foo\"
120           #:raise 0.2 #:hbracket #:bold \"bar\"
121           #:override '(baseline-skip . 4) 
122           #:bracket #:column (\"baz\" \"bazr\" \"bla\"))
123 Use `markup*' in a \\notes block."
124   
125   (car (compile-all-markup-expressions `(#:line ,body))))
126
127 (defmacro*-public markup* (#:rest body)
128   "Same as `markup', for use in a \\notes block."
129   `(ly:export (markup ,@body)))
130   
131   
132 (define (compile-all-markup-expressions expr)
133   "Return a list of canonical markups expressions, eg:
134   (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
135   ===>
136   ((make-COMMAND1-markup arg11 arg12)
137    (make-COMMAND2-markup arg21 arg22 arg23) ...)"
138   (do ((rest expr rest)
139        (markps '() markps))
140       ((null? rest) (reverse markps))
141     (receive (m r) (compile-markup-expression rest)
142              (set! markps (cons m markps))
143              (set! rest r))))
144
145 (define (keyword->make-markup key)
146   "Transform a keyword, eg. #:COMMAND, in a make-COMMAND-markup symbol."
147   (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
148
149 (define (compile-markup-expression expr)
150   "Return two values: the first complete canonical markup expression found in `expr',
151 eg (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
152   (cond ((and (pair? expr)
153               (keyword? (car expr)))
154          ;; expr === (#:COMMAND arg1 ...)
155          (let* ((command (symbol->string (keyword->symbol (car expr))))
156                 (sig (markup-command-signature (car (lookup-markup-command command))))
157                 (sig-len (length sig)))
158            (do ((i 0 (1+ i))
159                 (args '() args)
160                 (rest (cdr expr) rest))
161                ((>= i sig-len)
162                 (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
163              (cond ((eqv? (list-ref sig i) markup-list?)
164                     ;; (car rest) is a markup list
165                     (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
166                     (set! rest (cdr rest)))
167                    (else
168                     ;; pick up one arg in `rest'
169                     (receive (a r) (compile-markup-arg rest)
170                              (set! args (cons a args))
171                              (set! rest r)))))))
172         ((and (pair? expr)
173               (pair? (car expr))
174               (keyword? (caar expr)))
175          ;; expr === ((#:COMMAND arg1 ...) ...)
176          (receive (m r) (compile-markup-expression (car expr))
177                   (values m (cdr expr))))
178         (else
179          ;; expr === (symbol ...) or ("string" ...) or ((funcall ...) ...)
180          (values (car expr)
181                  (cdr expr)))))
182
183 (define (compile-all-markup-args expr)
184   "Transform `expr' into markup arguments"
185   (do ((rest expr rest)
186        (args '() args))
187       ((null? rest) (reverse args))
188     (receive (a r) (compile-markup-arg rest)
189              (set! args (cons a args))
190              (set! rest r))))
191
192 (define (compile-markup-arg expr)
193   "Return two values: the desired markup argument, and the rest arguments"
194   (cond ((null? expr)
195          ;; no more args
196          (values '() '()))
197         ((keyword? (car expr))
198          ;; expr === (#:COMMAND ...)
199          ;; ==> build and return the whole markup expression
200          (compile-markup-expression expr))
201         ((and (pair? (car expr))
202               (keyword? (caar expr)))
203          ;; expr === ((#:COMMAND ...) ...)
204          ;; ==> build and return the whole markup expression(s)
205          ;; found in (car expr)
206          (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
207                   (if (null? rest-expr)
208                       (values markup-expr (cdr expr))
209                       (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
210                               (cdr expr)))))
211         ((and (pair? (car expr))
212               (pair? (caar expr)))
213          ;; expr === (((foo ...) ...) ...)
214          (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
215         (else (values (car expr) (cdr expr)))))
216
217 ;;;;;;;;;;;;;;;
218 ;;; Utilities for storing and accessing markup commands signature
219 ;;; and keyword.
220 ;;; Examples:
221 ;;;
222 ;;; (set! (markup-command-signature raise-markup) (list number? markup?))
223 ;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
224 ;;;
225 ;;; (markup-command-signature raise-markup)
226 ;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
227 ;;;
228 ;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
229 ;;; 
230
231 (define markup-command-signatures (make-hash-table 50))
232
233 (define (markup-command-signature-ref markup-command)
234   "Return markup-command's signature, e.g. (number? markup?).
235 markup-command may be a procedure."
236   (let ((sig-key (hashq-ref markup-command-signatures
237                             markup-command)))
238     (if sig-key (car sig-key) #f)))
239
240 (define-public (markup-command-keyword markup-command)
241   "Return markup-command's keyword, e.g. \"scheme0markup1\".
242 markup-command may be a procedure."
243   (let ((sig-key (hashq-ref markup-command-signatures
244                             markup-command)))
245     (if sig-key (cdr sig-key) #f)))
246
247 (define (markup-command-signatureset! markup-command signature)
248   "Set markup-command's signature. markup-command must be a named procedure.
249 Also set markup-signature and markup-keyword object properties."
250   (hashq-set! markup-command-signatures
251               markup-command
252               (cons signature (markup-signature-to-keyword signature)))
253   ;; these object properties are still in use somewhere
254   (set-object-property! markup-command 'markup-signature signature)
255   (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))
256   
257 (define-public markup-command-signature
258   (make-procedure-with-setter markup-command-signature-ref markup-command-signatureset!))
259
260 (define (markup-symbol-to-proc markup-sym)
261   "Return the markup command procedure which name is `markup-sym', if any."
262   (hash-fold (lambda (key val prev)
263                             (or prev
264                                 (if (eqv? (procedure-name key) markup-sym) key #f)))
265              #f
266              markup-command-signatures))
267
268 (define-public markup-function-list '())
269
270 (define-public (markup-signature-to-keyword sig)
271   " (A B C) -> a0-b1-c2 "
272   (if (null? sig)
273       'empty
274       (string->symbol (string-join (map
275                                     (let* ((count 0))
276                                       (lambda (func)
277                                         (set! count (+ count 1))
278                                         (string-append
279                                          ;; for reasons I don't get,
280                                          ;; (case func ((markup?) .. )
281                                          ;; doesn't work.
282                                          (cond 
283                                           ((eq? func markup?) "markup")
284                                           ((eq? func markup-list?) "markup-list")
285                                           (else "scheme"))
286                                          (number->string (- count 1)))))
287                                     sig)
288                          "-"))))
289
290 (define-public (lookup-markup-command code)
291   (let ((proc (markup-symbol-to-proc (string->symbol (string-append code "-markup")))))
292     (and proc (cons proc (markup-command-keyword proc)))))
293
294 ;;;;;;;;;;;;;;;;;;;;;;
295 ;;; markup type predicates
296
297 (define (markup-function? x)
298   (not (not (markup-command-signature x))))
299
300 (define (markup-list? arg)
301   (define (markup-list-inner? l)
302     (or (null? l)
303         (and (markup? (car l)) (markup-list-inner? (cdr l)))))
304   (and (list? arg) (markup-list-inner? arg)))
305
306 (define (markup-argument-list? signature arguments)
307   "Typecheck argument list."
308   (if (and (pair? signature) (pair? arguments))
309       (and ((car signature) (car arguments))
310            (markup-argument-list? (cdr signature) (cdr arguments)))
311       (and (null? signature) (null? arguments))))
312
313
314 (define (markup-argument-list-error signature arguments number)
315   "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
316 #f is no error found.
317 "
318   (if (and (pair? signature) (pair? arguments))
319       (if (not ((car signature) (car arguments)))
320           (list number (type-name (car signature)) (car arguments))
321           (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
322       #f))
323
324 ;;
325 ;; full recursive typecheck.
326 ;;
327 (define (markup-typecheck? arg)
328   (or (string? arg)
329       (and (pair? arg)
330            (markup-function? (car arg))
331            (markup-argument-list? (markup-command-signature (car arg))
332                                   (cdr arg)))))
333
334 ;; 
335 ;; typecheck, and throw an error when something amiss.
336 ;; 
337 (define (markup-thrower-typecheck arg)
338   (cond ((string? arg) #t)
339         ((not (pair? arg))
340          (throw 'markup-format "Not a pair" arg))
341         ((not (markup-function? (car arg)))
342          (throw 'markup-format "Not a markup function " (car arg)))
343         ((not (markup-argument-list? (markup-command-signature (car arg))
344                                      (cdr arg)))
345          (throw 'markup-format "Arguments failed  typecheck for " arg)))
346   #t)
347
348 ;;
349 ;; good enough if you only  use make-XXX-markup functions.
350 ;; 
351 (define (cheap-markup? x)
352   (or (string? x)
353       (and (pair? x)
354            (markup-function? (car x)))))
355
356 ;;
357 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
358 ;; 
359 (define-public markup? cheap-markup?)
360
361 ;; utility
362
363 (define (markup-join markups sep)
364   "Return line-markup of MARKUPS, joining them with markup SEP"
365   (if (pair? markups)
366       (make-line-markup (list-insert-separator markups sep))
367       empty-markup))
368
369 (define-public brew-new-markup-stencil Text_item::print)
370 (define-public interpret-markup Text_item::interpret_markup)
371 (define-public (prepend-alist-chain key val chain)
372   (cons (acons key val (car chain)) (cdr chain)))
373
374
375
376
377 (define-public (stack-stencil-line space stencils)
378   (if (and (pair? stencils)
379            (ly:stencil? (car stencils)))
380       
381       (if (and (pair? (cdr stencils))
382                (ly:stencil? (cadr stencils)))
383           (let* ((tail (stack-stencil-line  space (cdr stencils)))
384                  (head (car stencils))
385                  (xoff (+ space (cdr (ly:stencil-get-extent head X)))))
386             (ly:stencil-add head
387                              (ly:stencil-translate-axis tail xoff X)))
388           (car stencils))
389       (ly:make-stencil '() '(0 . 0) '(0 . 0))))
390
391
392
393
394
395
396