2 Internally markup is stored as lists, whose head is a function.
4 (FUNCTION ARG1 ARG2 ... )
6 When the markup is formatted, then FUNCTION is called as follows
8 (FUNCTION GROB PROPS ARG1 ARG2 ... )
10 GROB is the current grob, PROPS is a list of alists, and ARG1.. are
11 the rest of the arguments.
13 The function should return a molecule (i.e. a formatted, ready to
20 1. It should be named COMMAND-markup
22 2. It should have an object property set that describes it's
23 signature. This is to allow the parser to figure out how many
26 (set-object-property! COMMAND-markup scm0-markup1)
28 (insert in the list below).
30 3. The command is now available in markup mode, e.g.
33 \\markup { .... \\COMMAND #1 argument ... }
38 At present, markup functions must be defined in this
39 file. Implementing user-access for markup functions is an excercise
50 ;; each markup function should have a doc string with
51 ;; syntax, description and example.
54 (define-public (simple-markup paper props . rest)
55 (Text_item::interpret_markup paper props (car rest)))
57 (define-public (stack-molecule-line space molecules)
59 (if (pair? (cdr molecules))
61 (tail (stack-molecule-line space (cdr molecules)))
62 (head (car molecules))
63 (xoff (+ space (cdr (ly:molecule-get-extent head X))))
68 (ly:molecule-translate-axis tail xoff X))
74 (define-public (line-markup paper props . rest)
75 "A horizontal line of markups. Syntax:
80 (cdr (chain-assoc 'word-space props))
81 (map (lambda (x) (interpret-markup paper props x)) (car rest)))
85 (define-public (combine-markup paper props . rest)
87 (interpret-markup paper props (car rest))
88 (interpret-markup paper props (cadr rest))))
90 (define (font-markup qualifier value)
91 (lambda (paper props . rest)
92 (interpret-markup paper (cons (cons `(,qualifier . ,value) (car props)) (cdr props)) (car rest))
97 (define-public (set-property-markup qualifier)
98 (lambda (paper props . rest )
99 (interpret-markup paper
100 (cons (cons `(,qualifier . ,(car rest))
101 (car props)) (cdr props))
105 (define-public (finger-markup paper props . rest)
106 (interpret-markup paper
107 (cons (list '(font-size . -4)
108 '(font-family . number))
112 (define-public fontsize-markup (set-property-markup 'font-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-size 2))
125 (define-public large-markup
126 (font-markup 'font-size 1))
127 (define-public small-markup
128 (font-markup 'font-size -1))
129 (define-public tiny-markup
130 (font-markup 'font-size -2))
131 (define-public teeny-markup
132 (font-markup 'font-size -3))
133 (define-public dynamic-markup
134 (font-markup 'font-family 'dynamic))
135 (define-public italic-markup
136 (font-markup 'font-shape 'italic))
137 (define-public typewriter-markup
138 (font-markup 'font-family 'typewriter))
141 ;; TODO: baseline-skip should come from the font.
142 (define-public (column-markup paper props . rest)
144 -1 0.0 (cdr (chain-assoc 'baseline-skip props))
145 (map (lambda (x) (interpret-markup paper props x)) (car rest)))
148 (define-public (dir-column-markup paper props . rest)
149 "Make a column of args, going up or down, depending on DIRECTION."
152 (dir (cdr (chain-assoc 'direction props)))
155 (if (number? dir) dir -1)
156 0.0 (cdr (chain-assoc 'baseline-skip props))
157 (map (lambda (x) (interpret-markup paper props x)) (car rest)))
160 (define-public (center-markup paper props . rest)
163 (mols (map (lambda (x) (interpret-markup paper props x)) (car rest)))
164 (cmols (map (lambda (x) (ly:molecule-align-to! x X CENTER)) mols))
168 -1 0.0 (cdr (chain-assoc 'baseline-skip props))
172 (define-public (right-align-markup paper props . rest)
173 (let* ((m (interpret-markup paper props (car rest))))
174 (ly:molecule-align-to! m X RIGHT)
176 (define-public (left-align-markup paper props . rest)
177 (let* ((m (interpret-markup paper props (car rest))))
178 (ly:molecule-align-to! m X LEFT)
180 (define-public (halign-markup paper props . rest)
181 "Set horizontal alignment. Syntax: haling A MARKUP. A=-1 is LEFT,
182 A=1 is right, values in between vary alignment accordingly."
183 (let* ((m (interpret-markup paper props (cadr rest))))
184 (ly:molecule-align-to! m X (car rest))
189 (define-public (musicglyph-markup paper props . rest)
190 (ly:find-glyph-by-name
191 (ly:paper-get-font paper (cons '((font-name . ())
194 (font-family . music)) props))
198 (define-public (lookup-markup paper props . rest)
199 "Lookup a glyph by name."
200 (ly:find-glyph-by-name
201 (ly:paper-get-font paper props)
205 (define-public (char-markup paper props . rest)
206 "Syntax: \\char NUMBER. "
207 (ly:get-glyph (ly:paper-get-font paper props) (car rest))
210 (define-public (raise-markup paper props . rest)
211 "Syntax: \\raise AMOUNT MARKUP. "
212 (ly:molecule-translate-axis (interpret-markup
218 (define-public (fraction-markup paper props . rest)
219 "Make a fraction of two markups.
221 Syntax: \\fraction MARKUP1 MARKUP2."
224 ((m1 (interpret-markup paper props (car rest)))
225 (m2 (interpret-markup paper props (cadr rest))))
227 (ly:molecule-align-to! m1 X CENTER)
228 (ly:molecule-align-to! m2 X CENTER)
231 ((x1 (ly:molecule-get-extent m1 X))
232 (x2 (ly:molecule-get-extent m2 X))
233 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
235 ;; should stack mols separately, to maintain LINE on baseline
236 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
238 (ly:molecule-align-to! stack Y CENTER)
239 (ly:molecule-align-to! stack X LEFT)
240 ;; should have EX dimension
242 (ly:molecule-translate-axis stack 0.75 Y)
246 (define-public (note-markup paper props . rest)
247 "Syntax: \\note #LOG #DOTS #DIR. By using fractional values
248 for DIR, you can obtain longer or shorter stems."
253 (dot-count (cadr rest))
255 (font (ly:paper-get-font paper (cons '((font-family . music)) props)))
256 (stemlen (max 3 (- log 1)))
258 (ly:find-glyph-by-name font (string-append "noteheads-" (number->string (min log 2)))))
261 (stemy (* dir stemlen))
262 (attachx (if (> dir 0) (- (cdr (ly:molecule-get-extent headgl X)) stemth)
264 (attachy (* dir 0.28))
265 (stemgl (if (> log 0)
267 (cons attachx (+ attachx stemth))
268 (cons (min stemy attachy)
272 (dot (ly:find-glyph-by-name font "dots-dot"))
273 (dotwid (interval-length (ly:molecule-get-extent dot X)))
274 (dots (if (> dot-count 0)
275 (apply ly:molecule-add
277 (ly:molecule-translate-axis
278 dot (* (+ 1 (* 2 x)) dotwid) X) )
282 (flaggl (if (> log 2)
283 (ly:molecule-translate
284 (ly:find-glyph-by-name
286 (string-append "flags-"
287 (if (> dir 0) "u" "d")
290 (cons (+ attachx (/ stemth 2)) stemy))
295 (set! stemgl (ly:molecule-add flaggl stemgl)))
297 (if (ly:molecule? stemgl)
298 (set! stemgl (ly:molecule-add stemgl headgl))
302 (if (ly:molecule? dots)
305 (ly:molecule-translate-axis
308 (if (and (> dir 0) (> log 2))
310 ;; huh ? why not necessary?
311 ;(cdr (ly:molecule-get-extent headgl X))
322 (define-public (normal-size-super-markup paper props . rest)
323 (ly:molecule-translate-axis (interpret-markup
326 (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
330 (define-public (super-markup paper props . rest)
331 "Syntax: \\super MARKUP. "
332 (ly:molecule-translate-axis (interpret-markup
334 (cons '((font-size . -3)) props) (car rest))
335 (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
339 (define-public (translate-markup paper props . rest)
340 "Syntax: \\translate OFFSET MARKUP. "
341 (ly:molecule-translate (interpret-markup paper props (cadr rest))
346 (define-public (sub-markup paper props . rest)
347 "Syntax: \\sub MARKUP."
348 (ly:molecule-translate-axis (interpret-markup
350 (cons '((font-size . -3)) props)
352 (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
356 (define-public (normal-size-sub-markup paper props . rest)
357 (ly:molecule-translate-axis (interpret-markup
360 (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
364 (define-public (hbracket-markup paper props . rest)
365 "Horizontal brackets around its single argument. Syntax \\hbracket MARKUP."
368 ((th 0.1) ;; todo: take from GROB.
369 (m (interpret-markup paper props (car rest))) )
371 (bracketify-molecule m X th (* 2.5 th) th)
374 (define-public (bracket-markup paper props . rest)
375 "Vertical brackets around its single argument. Syntax \\bracket MARKUP."
377 ((th 0.1) ;; todo: take from GROB.
378 (m (interpret-markup paper props (car rest))) )
380 (bracketify-molecule m Y th (* 2.5 th) th)
383 ;; todo: fix negative space
384 (define (hspace-markup paper props . rest)
385 "Syntax: \\hspace NUMBER."
387 ((amount (car rest)))
389 (ly:make-molecule "" (cons 0 amount) '(-1 . 1) )
390 (ly:make-molecule "" (cons amount amount) '(-1 . 1)))
393 (define-public (override-markup paper props . rest)
394 "Tack the 1st arg in REST onto PROPS, e.g.
396 \\override #'(font-family . married) \"bla\"
400 (interpret-markup paper (cons (list (car rest)) props)
403 (define-public (smaller-markup paper props . rest)
404 "Syntax: \\smaller MARKUP"
406 ((fs (chain-assoc-get 'font-size props 0))
407 (entry (cons 'font-size (- fs 1)))
410 paper (cons (list entry) props)
414 (define-public (bigger-markup paper props . rest)
415 "Syntax: \\bigger MARKUP"
417 ((fs (chain-assoc-get 'font-size props 0))
418 (entry (cons 'font-size (+ fs 1)))
421 paper (cons (list entry) props)
425 (define-public (box-markup paper props . rest)
426 "Syntax: \\box MARKUP"
430 (m (interpret-markup paper props (car rest)))
432 (box-molecule m th pad)
436 (define-public (strut-markup paper props . rest)
439 A box of the same height as the space.
443 ((m (Text_item::interpret_markup paper props " ")))
445 (ly:molecule-set-extent! m 0 '(1000 . -1000))
448 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
451 (define (markup-signature-to-keyword sig)
452 " (A B C) -> a0-b1-c2 "
455 (string->symbol (string-join
459 (set! count (+ count 1))
462 ;; for reasons I don't get,
463 ;; (case func ((markup?) .. )
466 ((eq? func markup?) "markup")
467 ((eq? func markup-list?) "markup-list")
470 (number->string (- count 1))
479 (define (markup-function? x)
480 (object-property x 'markup-signature)
483 (define (markup-list? arg)
484 (define (markup-list-inner? l)
487 (and (markup? (car l)) (markup-list-inner? (cdr l)))
490 (and (list? arg) (markup-list-inner? arg)))
492 (define (markup-argument-list? signature arguments)
493 "Typecheck argument list."
494 (if (and (pair? signature) (pair? arguments))
495 (and ((car signature) (car arguments))
496 (markup-argument-list? (cdr signature) (cdr arguments)))
497 (and (null? signature) (null? arguments)))
501 (define (markup-argument-list-error signature arguments number)
502 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
503 #f is no error found.
505 (if (and (pair? signature) (pair? arguments))
506 (if (not ((car signature) (car arguments)))
507 (list number (type-name (car signature)) (car arguments))
508 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
513 ;; full recursive typecheck.
515 (define (markup-typecheck? arg)
518 (markup-function? (car arg))
519 (markup-argument-list?
520 (object-property (car arg) 'markup-signature)
526 ;; typecheck, and throw an error when something amiss.
528 (define (markup-thrower-typecheck arg)
532 (throw 'markup-format "Not a pair" arg)
534 ((not (markup-function? (car arg)))
535 (throw 'markup-format "Not a markup function " (car arg)))
538 ((not (markup-argument-list?
539 (object-property (car arg) 'markup-signature)
541 (throw 'markup-format "Arguments failed typecheck for " arg)))
546 ;; good enough if you only use make-XXX-markup functions.
548 (define (cheap-markup? x)
551 (markup-function? (car x))))
555 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
557 (define markup? cheap-markup?)
559 (define markup-functions-and-signatures
563 (cons teeny-markup (list markup?))
564 (cons tiny-markup (list markup?))
565 (cons small-markup (list markup?))
566 (cons dynamic-markup (list markup?))
567 (cons large-markup (list markup?))
569 (cons huge-markup (list markup?))
572 (cons smaller-markup (list markup?))
573 (cons bigger-markup (list markup?))
574 ; (cons char-number-markup (list string?))
577 (cons sub-markup (list markup?))
578 (cons normal-size-sub-markup (list markup?))
580 (cons super-markup (list markup?))
581 (cons normal-size-super-markup (list markup?))
583 (cons finger-markup (list markup?))
584 (cons bold-markup (list markup?))
585 (cons italic-markup (list markup?))
586 (cons typewriter-markup (list markup?))
587 (cons roman-markup (list markup?))
588 (cons number-markup (list markup?))
589 (cons hbracket-markup (list markup?))
590 (cons bracket-markup (list markup?))
591 (cons note-markup (list integer? integer? ly:dir?))
592 (cons fraction-markup (list markup? markup?))
594 (cons column-markup (list markup-list?))
595 (cons dir-column-markup (list markup-list?))
596 (cons center-markup (list markup-list?))
597 (cons line-markup (list markup-list?))
599 (cons right-align-markup (list markup?))
600 (cons left-align-markup (list markup?))
601 (cons halign-markup (list number? markup?))
603 (cons combine-markup (list markup? markup?))
604 (cons simple-markup (list string?))
605 (cons musicglyph-markup (list scheme?))
606 (cons translate-markup (list number-pair? markup?))
607 (cons override-markup (list pair? markup?))
608 (cons char-markup (list integer?))
609 (cons lookup-markup (list string?))
611 (cons hspace-markup (list number?))
613 (cons raise-markup (list number? markup?))
614 (cons magnify-markup (list number? markup?))
615 (cons fontsize-markup (list number? markup?))
617 (cons box-markup (list markup?))
618 (cons strut-markup '())
622 (define markup-module (current-module))
625 (set-object-property! (car x) 'markup-signature (cdr x))
626 (set-object-property! (car x) 'markup-keyword (markup-signature-to-keyword (cdr x)))
628 markup-functions-and-signatures)
630 (define-public markup-function-list (map car markup-functions-and-signatures))
635 ;; make-FOO-markup function that typechecks its arguments.
637 ;; TODO: should construct a message says
638 ;; Invalid argument 4 : expecting a BLADIBLA, found: (list-ref 4 args)
640 ;; right now, you get the entire argument list.
643 (define (make-markup-maker entry)
645 ((foo-markup (car entry))
646 (signature (cons 'list (cdr entry)))
647 (name (symbol->string (procedure-name foo-markup)))
648 (make-name (string-append "make-" name))
651 `(define (,(string->symbol make-name) . args)
654 (arglen (length args))
655 (siglen (length ,signature))
657 (if (and (> 0 siglen) (> 0 arglen))
658 (markup-argument-list-error ,signature args 1)))
662 (if (or (not (= arglen siglen)) (< siglen 0) (< 0 arglen))
663 (scm-error 'markup-format ,make-name "Expect ~A arguments for ~A. Found ~A: ~S"
664 (list (length ,signature)
669 (scm-error 'markup-format ,make-name "Invalid argument in position ~A\n Expect: ~A\nFound: ~S." error-msg #f)
671 (cons ,foo-markup args)
678 (define (make-markup markup-function make-name signature args)
680 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
681 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
685 ((arglen (length args))
686 (siglen (length signature))
688 (if (and (> siglen 0) (> arglen 0))
689 (markup-argument-list-error signature args 1)
693 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
694 (scm-error 'markup-format make-name "Expect ~A arguments for ~A. Found ~A: ~S"
701 (scm-error 'markup-format make-name "Invalid argument in position ~A\nExpect: ~A\nFound: ~S." error-msg #f)
703 (cons markup-function args)
706 (define (make-markup-maker entry)
708 (name (symbol->string (procedure-name (car entry))))
709 (make-name (string-append "make-" name))
710 (signature (object-property (car entry) 'markup-signature))
713 `(define-public (,(string->symbol make-name) . args)
714 (make-markup ,(car entry) ,make-name ,(cons 'list signature) args)
719 (cons 'begin (map make-markup-maker markup-functions-and-signatures))
724 ;; TODO: add module argument so user-defined markups can also be
727 (define-public (lookup-markup-command code)
729 ((sym (string->symbol (string-append code "-markup")))
730 (var (module-local-variable markup-module sym))
734 (cons (variable-ref var) (object-property (variable-ref var) 'markup-keyword))
739 (define-public brew-new-markup-molecule Text_item::brew_molecule)
741 (define-public empty-markup (make-simple-markup ""))
743 (define-public interpret-markup Text_item::interpret_markup)
749 (define (markup-join markups sep)
750 "Return line-markup of MARKUPS, joining them with markup SEP"
752 (make-line-markup (list-insert-separator markups sep))
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
759 (define (typecheck-with-error x)
762 (lambda () (markup? x))
763 (lambda (key message arg)
764 (display "\nERROR: markup format error: \n")
767 (write arg (current-output-port))
771 ;; test make-foo-markup functions
776 (display (make-line-markup (list (make-simple-markup "FOO"))))
778 (make-line-markup (make-simple-markup "FOO"))
779 (make-line-markup (make-simple-markup "FOO") (make-simple-markup "foo"))
780 (make-raise-markup "foo" (make-simple-markup "foo"))
786 ;; test typecheckers. Not wholly useful, because errors are detected
787 ;; in other places than they're made.
792 ;; To get error messages, see above to install the alternate
793 ;; typecheck routine for markup?.
797 (display (typecheck-with-error `(,simple-markup "foobar")))
798 (display (typecheck-with-error `(,simple-markup "foobar")))
799 (display (typecheck-with-error `(,simple-markup 1)))
801 (typecheck-with-error `(,line-markup ((,simple-markup "foobar"))
802 (,simple-markup 1))))
804 (typecheck-with-error `(,line-markup (,simple-markup "foobar")
805 (,simple-markup "bla"))))