3 Internally markup is stored as lists, whose head is a function.
5 (FUNCTION ARG1 ARG2 ... )
7 When the markup is formatted, then FUNCTION is called as follows
9 (FUNCTION GROB PROPS ARG1 ARG2 ... )
11 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
12 the rest of the arguments.
14 The function should return a molecule (i.e. a formatted, ready to
21 1. It should be named COMMAND-markup
23 2. It should have an object property set that describes it's
24 signature. This is to allow the parser to figure out how many
27 (set-object-property! COMMAND-markup scm0-markup1)
29 (insert in the list below).
31 3. The command is now available in markup mode, e.g.
34 \markup { .... \COMMAND #1 argument ... }
39 At present, markup functions must be defined in this
40 file. Implementing user-access for markup functions is an excercise
48 (define-public (simple-markup grob props . rest)
49 (Text_item::text_to_molecule grob props (car rest))
53 (define-public (stack-molecule-line space molecules)
55 (if (pair? (cdr molecules))
57 (tail (stack-molecule-line space (cdr molecules)))
58 (head (car molecules))
59 (xoff (+ space (cdr (ly:molecule-get-extent head X))))
64 (ly:molecule-translate-axis tail xoff X))
70 (define-public (line-markup grob props . rest)
72 (cdr (chain-assoc 'word-space props))
73 (map (lambda (x) (interpret-markup grob props x)) (car rest)))
76 (define (combine-molecule-list lst)
77 (if (null? (cdr lst)) (car lst)
78 (ly:molecule-add (car lst) (combine-molecule-list (cdr lst)))
81 (define-public (combine-markup grob props . rest)
83 (interpret-markup grob props (car rest))
84 (interpret-markup grob props (cadr rest))))
86 ; (combine-molecule-list (map (lambda (x) (interpret-markup grob props x)) (car rest))))
88 (define (font-markup qualifier value)
89 (lambda (grob props . rest)
90 (interpret-markup grob (cons (cons `(,qualifier . ,value) (car props)) (cdr props)) (car rest))
95 (define-public (set-property-markup qualifier)
96 (lambda (grob props . rest )
97 (interpret-markup grob
98 (cons (cons `(,qualifier . ,(car rest))
99 (car props)) (cdr props))
104 (define-public (finger-markup grob props . rest)
105 (interpret-markup grob
106 (cons (list '(font-relative-size . -3)
107 '(font-family . number))
112 (define-public fontsize-markup (set-property-markup 'font-relative-size))
113 (define-public magnify-markup (set-property-markup 'font-magnification))
115 (define-public bold-markup
116 (font-markup 'font-series 'bold))
117 (define-public number-markup
118 (font-markup 'font-family 'number))
119 (define-public roman-markup
120 (font-markup 'font-family 'roman))
123 (define-public huge-markup
124 (font-markup 'font-relative-size 2))
125 (define-public large-markup
126 (font-markup 'font-relative-size 1))
127 (define-public small-markup
128 (font-markup 'font-relative-size -1))
129 (define-public tiny-markup
130 (font-markup 'font-relative-size -2))
131 (define-public teeny-markup
132 (font-markup 'font-relative-size -3))
133 (define-public dynamic-markup
134 (font-markup 'font-family 'dynamic))
135 (define-public italic-markup
136 (font-markup 'font-shape 'italic))
139 ;; TODO: baseline-skip should come from the font.
140 (define-public (column-markup grob props . rest)
142 -1 0.0 (cdr (chain-assoc 'baseline-skip props))
143 (map (lambda (x) (interpret-markup grob props x)) (car rest)))
146 (define-public (dir-column-markup grob props . rest)
147 "Make a column of args, going up or down, depending on DIRECTION."
150 (dir (cdr (chain-assoc 'direction props)))
153 (if (number? dir) dir -1)
154 0.0 (cdr (chain-assoc 'baseline-skip props))
155 (map (lambda (x) (interpret-markup grob props x)) (car rest)))
158 (define-public (center-markup grob props . rest)
161 (mols (map (lambda (x) (interpret-markup grob props x)) (car rest)))
162 (cmols (map (lambda (x) (ly:molecule-align-to! x X CENTER)) mols))
166 -1 0.0 (cdr (chain-assoc 'baseline-skip props))
170 (define-public (musicglyph-markup grob props . rest)
171 (ly:find-glyph-by-name
172 (ly:get-font grob (cons '((font-family . music)) props))
177 (define-public (lookup-markup grob props . rest)
178 "Lookup a glyph by name."
179 (ly:find-glyph-by-name
180 (ly:get-font grob props)
184 (define-public (char-markup grob props . rest)
185 "Syntax: \\char NUMBER. "
186 (ly:get-glyph (ly:get-font grob props) (car rest))
189 (define-public (raise-markup grob props . rest)
190 "Syntax: \\raise AMOUNT MARKUP. "
191 (ly:molecule-translate-axis (interpret-markup
198 (define-public (normal-size-super-markup grob props . rest)
199 (ly:molecule-translate-axis (interpret-markup
202 (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
206 (define-public (super-markup grob props . rest)
207 "Syntax: \\super MARKUP. "
208 (ly:molecule-translate-axis (interpret-markup
210 (cons '((font-relative-size . -2)) props) (car rest))
211 (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
215 (define-public (translate-markup grob props . rest)
216 "Syntax: \\translate OFFSET MARKUP. "
217 (ly:molecule-translate (interpret-markup grob props (cadr rest))
222 (define-public (sub-markup grob props . rest)
223 "Syntax: \\sub MARKUP."
224 (ly:molecule-translate-axis (interpret-markup
226 (cons '((font-relative-size . -2)) props)
228 (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
232 (define-public (normal-size-sub-markup grob props . rest)
233 (ly:molecule-translate-axis (interpret-markup
236 (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
240 (define-public (hbracket-markup grob props . rest)
243 (th 0.1) ;; todo: take from GROB.
244 (m (interpret-markup grob props (car rest)))
247 (bracketify-molecule m X th (* 2.5 th) th)
250 (define-public (bracket-markup grob props . rest)
253 (th 0.1) ;; todo: take from GROB.
254 (m (interpret-markup grob props (car rest)))
257 (bracketify-molecule m Y th (* 2.5 th) th)
261 ;; todo: fix negative space
262 (define (hspace-markup grob props . rest)
263 "Syntax: \\hspace NUMBER."
265 ((amount (car rest)))
267 (ly:make-molecule "" (cons 0 amount) '(-1 . 1) )
268 (ly:make-molecule "" (cons amount amount) '(-1 . 1)))
271 (define-public (override-markup grob props . rest)
272 "Tack the 1st arg in REST onto PROPS, e.g.
274 \override #'(font-family . married) \"bla\"
278 (interpret-markup grob (cons (list (car rest)) props)
281 (define-public (smaller-markup grob props . rest)
282 "Syntax: \\smaller MARKUP"
285 (fs (cdr (chain-assoc 'font-relative-size props)))
286 (entry (cons 'font-relative-size (- fs 1)))
289 grob (cons (list entry) props)
294 (define-public (bigger-markup grob props . rest)
295 "Syntax: \\bigger MARKUP"
298 (fs (cdr (chain-assoc 'font-relative-size props)))
299 (entry (cons 'font-relative-size (+ fs 1)))
302 grob (cons (list entry) props)
306 (define (markup-signature-to-keyword sig)
307 " (A B C) -> a0-b1-c2 "
310 (string->symbol (string-join
314 (set! count (+ count 1))
317 ;; for reasons I don't get,
318 ;; (case func ((markup?) .. )
321 ((eq? func markup?) "markup")
322 ((eq? func markup-list?) "markup-list")
325 (number->string (- count 1))
334 (define (markup-function? x)
335 (object-property x 'markup-signature)
338 (define (markup-list? arg)
339 (define (markup-list-inner? l)
342 (and (markup? (car l)) (markup-list-inner? (cdr l)))
345 (and (list? arg) (markup-list-inner? arg)))
347 (define (markup-argument-list? signature arguments)
348 "Typecheck argument list."
349 (if (and (pair? signature) (pair? arguments))
350 (and ((car signature) (car arguments))
351 (markup-argument-list? (cdr signature) (cdr arguments)))
352 (and (null? signature) (null? arguments)))
356 (define (markup-argument-list-error signature arguments number)
357 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
358 #f is no error found.
360 (if (and (pair? signature) (pair? arguments))
361 (if (not ((car signature) (car arguments)))
362 (list number (type-name (car signature)) (car arguments))
363 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
368 ;; full recursive typecheck.
370 (define (markup-typecheck? arg)
373 (markup-function? (car arg))
374 (markup-argument-list?
375 (object-property (car arg) 'markup-signature)
381 ;; typecheck, and throw an error when something amiss.
383 (define (markup-thrower-typecheck arg)
387 (throw 'markup-format "Not a pair" arg)
389 ((not (markup-function? (car arg)))
390 (throw 'markup-format "Not a markup function " (car arg)))
393 ((not (markup-argument-list?
394 (object-property (car arg) 'markup-signature)
396 (throw 'markup-format "Arguments failed typecheck for " arg)))
401 ;; good enough if you only use make-XXX-markup functions.
403 (define (cheap-markup? x)
406 (markup-function? (car x))))
410 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
412 (define markup? cheap-markup?)
414 (define markup-function-list
418 (cons teeny-markup (list markup?))
419 (cons tiny-markup (list markup?))
420 (cons small-markup (list markup?))
421 (cons dynamic-markup (list markup?))
422 (cons large-markup (list markup?))
424 (cons huge-markup (list markup?))
427 (cons smaller-markup (list markup?))
428 (cons bigger-markup (list markup?))
431 (cons sub-markup (list markup?))
432 (cons normal-size-sub-markup (list markup?))
434 (cons super-markup (list markup?))
435 (cons normal-size-super-markup (list markup?))
437 (cons finger-markup (list markup?))
438 (cons bold-markup (list markup?))
439 (cons italic-markup (list markup?))
440 (cons roman-markup (list markup?))
441 (cons number-markup (list markup?))
442 (cons hbracket-markup (list markup?))
443 (cons bracket-markup (list markup?))
445 (cons column-markup (list markup-list?))
446 (cons dir-column-markup (list markup-list?))
447 (cons center-markup (list markup-list?))
448 (cons line-markup (list markup-list?))
450 (cons combine-markup (list markup? markup?))
451 (cons simple-markup (list string?))
452 (cons musicglyph-markup (list scheme?))
453 (cons translate-markup (list number-pair? markup?))
454 (cons override-markup (list pair? markup?))
455 (cons char-markup (list integer?))
456 (cons lookup-markup (list string?))
458 (cons hspace-markup (list number?))
460 (cons raise-markup (list number? markup?))
461 (cons magnify-markup (list number? markup?))
462 (cons fontsize-markup (list number? markup?))
467 (define markup-module (current-module))
470 (set-object-property! (car x) 'markup-signature (cdr x))
471 (set-object-property! (car x) 'markup-keyword (markup-signature-to-keyword (cdr x)))
473 markup-function-list)
478 ;; make-FOO-markup function that typechecks its arguments.
480 ;; TODO: should construct a message says
481 ;; Invalid argument 4 : expecting a BLADIBLA, found: (list-ref 4 args)
483 ;; right now, you get the entire argument list.
486 (define (make-markup-maker entry)
488 ((foo-markup (car entry))
489 (signature (cons 'list (cdr entry)))
490 (name (symbol->string (procedure-name foo-markup)))
491 (make-name (string-append "make-" name))
494 `(define (,(string->symbol make-name) . args)
497 (arglen (length args))
498 (siglen (length ,signature))
500 (if (and (> 0 siglen) (> 0 arglen))
501 (markup-argument-list-error ,signature args 1)))
505 (if (or (not (= arglen siglen)) (< siglen 0) (< 0 arglen))
506 (scm-error 'markup-format ,make-name "Expect ~A arguments for ~A. Found ~A: ~S"
507 (list (length ,signature)
512 (scm-error 'markup-format ,make-name "Invalid argument in position ~A\n Expect: ~A\nFound: ~S." error-msg #f)
514 (cons ,foo-markup args)
521 (define (make-markup markup-function make-name signature args)
523 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
524 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
529 (arglen (length args))
530 (siglen (length signature))
532 (if (and (> siglen 0) (> arglen 0))
533 (markup-argument-list-error signature args 1)))
537 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
538 (scm-error 'markup-format make-name "Expect ~A arguments for ~A. Found ~A: ~S"
545 (scm-error 'markup-format make-name "Invalid argument in position ~A\nExpect: ~A\nFound: ~S." error-msg #f)
547 (cons markup-function args)
550 (define (make-markup-maker entry)
552 (name (symbol->string (procedure-name (car entry))))
553 (make-name (string-append "make-" name))
554 (signature (object-property (car entry) 'markup-signature))
557 `(define-public (,(string->symbol make-name) . args)
558 (make-markup ,(car entry) ,make-name ,(cons 'list signature) args)
563 (cons 'begin (map make-markup-maker markup-function-list))
567 (define-public (lookup-markup-command code)
569 ( (sym (string->symbol (string-append code "-markup")))
570 (var (module-local-variable markup-module sym))
574 (cons (variable-ref var) (object-property (variable-ref var) 'markup-keyword))
579 (define-public (brew-new-markup-molecule grob)
581 ((t (ly:get-grob-property grob 'text))
585 (interpret-markup grob
586 (Font_interface::get_property_alist_chain grob)
591 (define-public empty-markup `(,simple-markup ""))
593 (define (interpret-markup grob props markup)
595 (simple-markup grob props markup)
602 (apply func (cons grob (cons props args)) )
609 (define (markup-join markups sep)
610 "Return line-markup of MARKUPS, joining them with markup SEP"
612 (make-line-markup (list-insert-separator markups sep))
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
619 (define (typecheck-with-error x)
622 (lambda () (markup? x))
623 (lambda (key message arg)
624 (display "\nERROR: markup format error: \n")
627 (write arg (current-output-port))
631 ;; test make-foo-markup functions
636 (display (make-line-markup (list (make-simple-markup "FOO"))))
638 (make-line-markup (make-simple-markup "FOO"))
639 (make-line-markup (make-simple-markup "FOO") (make-simple-markup "foo"))
640 (make-raise-markup "foo" (make-simple-markup "foo"))
646 ;; test typecheckers. Not wholly useful, because errors are detected
647 ;; in other places than they're made.
652 ;; To get error messages, see above to install the alternate
653 ;; typecheck routine for markup?.
657 (display (typecheck-with-error `(,simple-markup "foobar")))
658 (display (typecheck-with-error `(,simple-markup "foobar")))
659 (display (typecheck-with-error `(,simple-markup 1)))
661 (typecheck-with-error `(,line-markup ((,simple-markup "foobar"))
662 (,simple-markup 1))))
664 (typecheck-with-error `(,line-markup (,simple-markup "foobar")
665 (,simple-markup "bla"))))