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 "A simple text-string; @code{\\markup @{ foo @}} is equivalent with
56 @code{\\markup @{ \\simple #\"foo\" @}}.
59 (Text_item::interpret_markup paper props (car rest)))
61 (define-public (stack-molecule-line space molecules)
63 (if (pair? (cdr molecules))
65 (tail (stack-molecule-line space (cdr molecules)))
66 (head (car molecules))
67 (xoff (+ space (cdr (ly:molecule-get-extent head X))))
72 (ly:molecule-translate-axis tail xoff X))
78 (define-public (line-markup paper props . rest)
79 "A horizontal line of markups. Syntax:
84 (cdr (chain-assoc 'word-space props))
85 (map (lambda (x) (interpret-markup paper props x)) (car rest)))
89 (define-public (combine-markup paper props . rest)
90 "Overstrike two markups."
92 (interpret-markup paper props (car rest))
93 (interpret-markup paper props (cadr rest))))
95 (define (font-markup qualifier value)
96 (lambda (paper props . rest)
97 (interpret-markup paper (cons (cons `(,qualifier . ,value) (car props)) (cdr props)) (car rest))
102 (define-public (set-property-markup qualifier)
103 (lambda (paper props . rest )
104 (interpret-markup paper
105 (cons (cons `(,qualifier . ,(car rest))
106 (car props)) (cdr props))
110 (define-public (finger-markup paper props . rest)
111 (interpret-markup paper
112 (cons (list '(font-size . -4)
113 '(font-family . number))
117 (define-public fontsize-markup (set-property-markup 'font-size))
118 (define-public magnify-markup (set-property-markup 'font-magnification))
120 (define-public bold-markup
121 (font-markup 'font-series 'bold))
122 (define-public sans-markup
123 (font-markup 'font-family 'sans))
124 (define-public number-markup
125 (font-markup 'font-family 'number))
126 (define-public roman-markup
127 (font-markup 'font-family 'roman))
130 (define-public huge-markup
131 (font-markup 'font-size 2))
132 (define-public large-markup
133 (font-markup 'font-size 1))
134 (define-public normalsize-markup
135 (font-markup 'font-size 0))
136 (define-public small-markup
137 (font-markup 'font-size -1))
138 (define-public tiny-markup
139 (font-markup 'font-size -2))
140 (define-public teeny-markup
141 (font-markup 'font-size -3))
142 (define-public dynamic-markup
143 (font-markup 'font-family 'dynamic))
144 (define-public italic-markup
145 (font-markup 'font-shape 'italic))
146 (define-public typewriter-markup
147 (font-markup 'font-family 'typewriter))
150 ;; TODO: baseline-skip should come from the font.
151 (define-public (column-markup paper props . rest)
153 -1 0.0 (cdr (chain-assoc 'baseline-skip props))
154 (map (lambda (x) (interpret-markup paper props x)) (car rest)))
157 (define-public (dir-column-markup paper props . rest)
158 "Make a column of args, going up or down, depending on the setting
159 of the #'direction layout property."
163 (dir (cdr (chain-assoc 'direction props)))
166 (if (number? dir) dir -1)
167 0.0 (cdr (chain-assoc 'baseline-skip props))
168 (map (lambda (x) (interpret-markup paper props x)) (car rest)))
171 (define-public (center-markup paper props . rest)
174 (mols (map (lambda (x) (interpret-markup paper props x)) (car rest)))
175 (cmols (map (lambda (x) (ly:molecule-align-to! x X CENTER)) mols))
179 -1 0.0 (cdr (chain-assoc 'baseline-skip props))
183 (define-public (right-align-markup paper props . rest)
184 (let* ((m (interpret-markup paper props (car rest))))
185 (ly:molecule-align-to! m X RIGHT)
187 (define-public (left-align-markup paper props . rest)
188 (let* ((m (interpret-markup paper props (car rest))))
189 (ly:molecule-align-to! m X LEFT)
191 (define-public (halign-markup paper props . rest)
192 "Set horizontal alignment. Syntax: halign A MARKUP. A=-1 is LEFT,
193 A=1 is right, values in between vary alignment accordingly."
194 (let* ((m (interpret-markup paper props (cadr rest))))
195 (ly:molecule-align-to! m X (car rest))
200 (define-public (musicglyph-markup paper props . rest)
201 (ly:find-glyph-by-name
202 (ly:paper-get-font paper (cons '((font-name . ())
205 (font-family . music)) props))
209 (define-public (lookup-markup paper props . rest)
210 "Lookup a glyph by name."
211 (ly:find-glyph-by-name
212 (ly:paper-get-font paper props)
216 (define-public (char-markup paper props . rest)
217 "Syntax: \\char NUMBER. "
218 (ly:get-glyph (ly:paper-get-font paper props) (car rest))
221 (define-public (raise-markup paper props . rest)
222 "Syntax: \\raise AMOUNT MARKUP. "
223 (ly:molecule-translate-axis (interpret-markup
229 (define-public (fraction-markup paper props . rest)
230 "Make a fraction of two markups.
232 Syntax: \\fraction MARKUP1 MARKUP2."
235 ((m1 (interpret-markup paper props (car rest)))
236 (m2 (interpret-markup paper props (cadr rest))))
238 (ly:molecule-align-to! m1 X CENTER)
239 (ly:molecule-align-to! m2 X CENTER)
242 ((x1 (ly:molecule-get-extent m1 X))
243 (x2 (ly:molecule-get-extent m2 X))
244 (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
246 ;; should stack mols separately, to maintain LINE on baseline
247 (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
249 (ly:molecule-align-to! stack Y CENTER)
250 (ly:molecule-align-to! stack X LEFT)
251 ;; should have EX dimension
253 (ly:molecule-translate-axis stack 0.75 Y)
257 ;; TODO: better syntax.
260 (use-modules (ice-9 optargs)
264 (let ((divisor (log 2)))
265 (lambda (z) (inexact->exact (/ (log z) divisor)))))
267 (define (parse-simple-duration duration-string)
268 "Parse the `duration-string', eg ''4..'' or ''breve.'', and return a (log dots) list."
269 (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)") duration-string)))
270 (if (and match (string=? duration-string (match:substring match 0)))
271 (let ((len (match:substring match 1))
272 (dots (match:substring match 2)))
273 (list (cond ((string=? len "breve") -1)
274 ((string=? len "longa") -2)
275 ((string=? len "maxima") -3)
276 (else (log2 (string->number len))))
277 (if dots (string-length dots) 0)))
278 (error "This is not a valid duration string:" duration-string))))
281 (define-public (note-markup paper props . rest)
282 "This produces a note with a stem pointing in @var{dir} direction, with
283 the @var{duration} for the note head type and augmentation dots. For
284 example, @code{\note #\"4.\" #-0.75} creates a dotted quarter note, with
285 a shortened down stem."
288 ((parsed (parse-simple-duration (car rest)))
290 (note-by-number-markup paper props (car parsed) (cadr parsed) dir)
293 (define-public (note-by-number-markup paper props . rest )
294 "Syntax: \\note #LOG #DOTS #DIR. By using fractional values
295 for DIR, you can obtain longer or shorter stems."
300 (dot-count (cadr rest))
302 (font (ly:paper-get-font paper (cons '((font-family . music)) props)))
303 (stemlen (max 3 (- log 1)))
305 (ly:find-glyph-by-name font (string-append "noteheads-" (number->string (min log 2)))))
308 (stemy (* dir stemlen))
309 (attachx (if (> dir 0) (- (cdr (ly:molecule-get-extent headgl X)) stemth)
311 (attachy (* dir 0.28))
312 (stemgl (if (> log 0)
314 (cons attachx (+ attachx stemth))
315 (cons (min stemy attachy)
319 (dot (ly:find-glyph-by-name font "dots-dot"))
320 (dotwid (interval-length (ly:molecule-get-extent dot X)))
321 (dots (if (> dot-count 0)
322 (apply ly:molecule-add
324 (ly:molecule-translate-axis
325 dot (* (+ 1 (* 2 x)) dotwid) X) )
329 (flaggl (if (> log 2)
330 (ly:molecule-translate
331 (ly:find-glyph-by-name
333 (string-append "flags-"
334 (if (> dir 0) "u" "d")
337 (cons (+ attachx (/ stemth 2)) stemy))
342 (set! stemgl (ly:molecule-add flaggl stemgl)))
344 (if (ly:molecule? stemgl)
345 (set! stemgl (ly:molecule-add stemgl headgl))
349 (if (ly:molecule? dots)
352 (ly:molecule-translate-axis
355 (if (and (> dir 0) (> log 2))
357 ;; huh ? why not necessary?
358 ;(cdr (ly:molecule-get-extent headgl X))
369 (define-public (normal-size-super-markup paper props . rest)
370 (ly:molecule-translate-axis (interpret-markup
373 (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
377 (define-public (super-markup paper props . rest)
378 "Syntax: \\super MARKUP. "
379 (ly:molecule-translate-axis (interpret-markup
382 ,(- (chain-assoc-get 'font-size props 0) 3))) props) (car rest))
383 (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
387 (define-public (translate-markup paper props . rest)
388 "Syntax: \\translate OFFSET MARKUP. "
389 (ly:molecule-translate (interpret-markup paper props (cadr rest))
394 (define-public (sub-markup paper props . rest)
395 "Syntax: \\sub MARKUP."
396 (ly:molecule-translate-axis
400 ,(- (chain-assoc-get 'font-size props 0) 3))) props)
402 (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
405 (define-public (normal-size-sub-markup paper props . rest)
406 (ly:molecule-translate-axis
410 (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
413 (define-public (hbracket-markup paper props . rest)
414 "Horizontal brackets around its single argument. Syntax \\hbracket MARKUP."
417 ((th 0.1) ;; todo: take from GROB.
418 (m (interpret-markup paper props (car rest))) )
420 (bracketify-molecule m X th (* 2.5 th) th)
423 (define-public (bracket-markup paper props . rest)
424 "Vertical brackets around its single argument. Syntax \\bracket MARKUP."
426 ((th 0.1) ;; todo: take from GROB.
427 (m (interpret-markup paper props (car rest))) )
429 (bracketify-molecule m Y th (* 2.5 th) th)
432 ;; todo: fix negative space
433 (define (hspace-markup paper props . rest)
434 "Syntax: \\hspace NUMBER."
436 ((amount (car rest)))
438 (ly:make-molecule "" (cons 0 amount) '(-1 . 1) )
439 (ly:make-molecule "" (cons amount amount) '(-1 . 1)))
442 (define-public (override-markup paper props . rest)
444 "Add the first argument in to the property list. Properties may be
445 any sort of property supported by @ref{font-interface} and
446 @ref{text-interface}, for example
448 \\override #'(font-family . married) \"bla\"
452 (interpret-markup paper (cons (list (car rest)) props)
455 (define-public (smaller-markup paper props . rest)
456 "Syntax: \\smaller MARKUP"
458 ((fs (chain-assoc-get 'font-size props 0))
459 (entry (cons 'font-size (- fs 1)))
462 paper (cons (list entry) props)
467 (define-public (bigger-markup paper props . rest)
468 "Syntax: \\bigger MARKUP"
470 ((fs (chain-assoc-get 'font-size props 0))
471 (entry (cons 'font-size (+ fs 1)))
474 paper (cons (list entry) props)
478 (define-public larger-markup bigger-markup)
481 (define-public (box-markup paper props . rest)
482 "Syntax: \\box MARKUP"
486 (m (interpret-markup paper props (car rest)))
488 (box-molecule m th pad)
492 (define-public (strut-markup paper props . rest)
495 A box of the same height as the space.
499 ((m (Text_item::interpret_markup paper props " ")))
501 (ly:molecule-set-extent! m X '(1000 . -1000))
505 (define number->mark-letter-vector (make-vector 25 #\A))
510 (if (= i (- (char->integer #\I) (char->integer #\A)))
512 (vector-set! number->mark-letter-vector j
513 (integer->char (+ i (char->integer #\A)))) )
515 (define (number->markletter-string n)
516 "Double letters for big marks."
518 ((l (vector-length number->mark-letter-vector)))
521 (string-append (number->markletter-string (1- (quotient n l)))
522 (number->markletter-string (remainder n l)))
523 (make-string 1 (vector-ref number->mark-letter-vector n)))))
526 (define-public (markletter-markup paper props . rest)
527 "Markup letters: skip I and do double letters for big marks.
528 Syntax: \\markletter #25"
530 (Text_item::interpret_markup paper props
531 (number->markletter-string (car rest))
534 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
537 (define (markup-signature-to-keyword sig)
538 " (A B C) -> a0-b1-c2 "
542 (string->symbol (string-join
546 (set! count (+ count 1))
549 ;; for reasons I don't get,
550 ;; (case func ((markup?) .. )
553 ((eq? func markup?) "markup")
554 ((eq? func markup-list?) "markup-list")
557 (number->string (- count 1))
565 (define (markup-function? x)
566 (object-property x 'markup-signature) )
568 (define (markup-list? arg)
569 (define (markup-list-inner? l)
572 (and (markup? (car l)) (markup-list-inner? (cdr l))) ) )
573 (and (list? arg) (markup-list-inner? arg)))
575 (define (markup-argument-list? signature arguments)
576 "Typecheck argument list."
577 (if (and (pair? signature) (pair? arguments))
578 (and ((car signature) (car arguments))
579 (markup-argument-list? (cdr signature) (cdr arguments)))
580 (and (null? signature) (null? arguments)))
584 (define (markup-argument-list-error signature arguments number)
585 "return (ARG-NR TYPE-EXPECTED ARG-FOUND) if an error is detected, or
586 #f is no error found.
588 (if (and (pair? signature) (pair? arguments))
589 (if (not ((car signature) (car arguments)))
590 (list number (type-name (car signature)) (car arguments))
591 (markup-argument-list-error (cdr signature) (cdr arguments) (+ 1 number)))
596 ;; full recursive typecheck.
598 (define (markup-typecheck? arg)
601 (markup-function? (car arg))
602 (markup-argument-list?
603 (object-property (car arg) 'markup-signature)
609 ;; typecheck, and throw an error when something amiss.
611 (define (markup-thrower-typecheck arg)
615 (throw 'markup-format "Not a pair" arg)
617 ((not (markup-function? (car arg)))
618 (throw 'markup-format "Not a markup function " (car arg)))
621 ((not (markup-argument-list?
622 (object-property (car arg) 'markup-signature)
624 (throw 'markup-format "Arguments failed typecheck for " arg)))
629 ;; good enough if you only use make-XXX-markup functions.
631 (define (cheap-markup? x)
634 (markup-function? (car x))))
638 ;; replace by markup-thrower-typecheck for more detailed diagnostics.
640 (define markup? cheap-markup?)
642 (define markup-functions-and-signatures
646 (cons teeny-markup (list markup?))
647 (cons tiny-markup (list markup?))
648 (cons small-markup (list markup?))
649 (cons dynamic-markup (list markup?))
650 (cons large-markup (list markup?))
651 (cons normalsize-markup (list markup?))
653 (cons huge-markup (list markup?))
656 (cons smaller-markup (list markup?))
657 (cons bigger-markup (list markup?))
658 (cons larger-markup (list markup?))
659 ; (cons char-number-markup (list string?))
662 (cons sub-markup (list markup?))
663 (cons normal-size-sub-markup (list markup?))
665 (cons super-markup (list markup?))
666 (cons normal-size-super-markup (list markup?))
668 (cons finger-markup (list markup?))
669 (cons bold-markup (list markup?))
670 (cons italic-markup (list markup?))
671 (cons typewriter-markup (list markup?))
672 (cons roman-markup (list markup?))
673 (cons number-markup (list markup?))
674 (cons hbracket-markup (list markup?))
675 (cons bracket-markup (list markup?))
676 (cons note-markup (list string? number?))
677 (cons note-by-number-markup (list number? number? number?))
678 (cons fraction-markup (list markup? markup?))
679 (cons markletter-markup (list number?))
680 (cons column-markup (list markup-list?))
681 (cons dir-column-markup (list markup-list?))
682 (cons center-markup (list markup-list?))
683 (cons line-markup (list markup-list?))
685 (cons right-align-markup (list markup?))
686 (cons left-align-markup (list markup?))
687 (cons halign-markup (list number? markup?))
689 (cons combine-markup (list markup? markup?))
690 (cons simple-markup (list string?))
691 (cons musicglyph-markup (list string?))
692 (cons translate-markup (list number-pair? markup?))
693 (cons override-markup (list pair? markup?))
694 (cons char-markup (list integer?))
695 (cons lookup-markup (list string?))
697 (cons hspace-markup (list number?))
699 (cons raise-markup (list number? markup?))
700 (cons magnify-markup (list number? markup?))
701 (cons fontsize-markup (list number? markup?))
703 (cons box-markup (list markup?))
704 (cons strut-markup '())
708 (define markup-module (current-module))
711 (set-object-property! (car x) 'markup-signature (cdr x))
712 (set-object-property! (car x) 'markup-keyword (markup-signature-to-keyword (cdr x)))
714 markup-functions-and-signatures)
716 (define-public markup-function-list (map car markup-functions-and-signatures))
721 ;; make-FOO-markup function that typechecks its arguments.
723 ;; TODO: should construct a message says
724 ;; Invalid argument 4 : expecting a BLADIBLA, found: (list-ref 4 args)
726 ;; right now, you get the entire argument list.
729 (define (make-markup-maker entry)
731 ((foo-markup (car entry))
732 (signature (cons 'list (cdr entry)))
733 (name (symbol->string (procedure-name foo-markup)))
734 (make-name (string-append "make-" name))
737 `(define (,(string->symbol make-name) . args)
740 (arglen (length args))
741 (siglen (length ,signature))
743 (if (and (> 0 siglen) (> 0 arglen))
744 (markup-argument-list-error ,signature args 1)))
748 (if (or (not (= arglen siglen)) (< siglen 0) (< 0 arglen))
749 (scm-error 'markup-format ,make-name "Expect ~A arguments for ~A. Found ~A: ~S"
750 (list (length ,signature)
755 (scm-error 'markup-format ,make-name "Invalid argument in position ~A\n Expect: ~A\nFound: ~S." error-msg #f)
757 (cons ,foo-markup args)
764 (define (make-markup markup-function make-name signature args)
766 " Construct a markup object from MARKUP-FUNCTION and ARGS. Typecheck
767 against SIGNATURE, reporting MAKE-NAME as the user-invoked function.
771 ((arglen (length args))
772 (siglen (length signature))
774 (if (and (> siglen 0) (> arglen 0))
775 (markup-argument-list-error signature args 1)
779 (if (or (not (= arglen siglen)) (< siglen 0) (< arglen 0))
780 (scm-error 'markup-format make-name "Expect ~A arguments for ~A. Found ~A: ~S"
787 (scm-error 'markup-format make-name "Invalid argument in position ~A\nExpect: ~A\nFound: ~S." error-msg #f)
789 (cons markup-function args)
792 (define (make-markup-maker entry)
794 (name (symbol->string (procedure-name (car entry))))
795 (make-name (string-append "make-" name))
796 (signature (object-property (car entry) 'markup-signature))
799 `(define-public (,(string->symbol make-name) . args)
800 (make-markup ,(car entry) ,make-name ,(cons 'list signature) args)
805 (cons 'begin (map make-markup-maker markup-functions-and-signatures))
810 ;; TODO: add module argument so user-defined markups can also be
813 (define-public (lookup-markup-command code)
815 ((sym (string->symbol (string-append code "-markup")))
816 (var (module-local-variable markup-module sym))
820 (cons (variable-ref var) (object-property (variable-ref var) 'markup-keyword))
825 (define-public brew-new-markup-molecule Text_item::brew_molecule)
827 (define-public empty-markup (make-simple-markup ""))
829 (define-public interpret-markup Text_item::interpret_markup)
835 (define (markup-join markups sep)
836 "Return line-markup of MARKUPS, joining them with markup SEP"
838 (make-line-markup (list-insert-separator markups sep))
842 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
845 (define (typecheck-with-error x)
848 (lambda () (markup? x))
849 (lambda (key message arg)
850 (display "\nERROR: markup format error: \n")
853 (write arg (current-output-port))
857 ;; test make-foo-markup functions
862 (display (make-line-markup (list (make-simple-markup "FOO"))))
864 (make-line-markup (make-simple-markup "FOO"))
865 (make-line-markup (make-simple-markup "FOO") (make-simple-markup "foo"))
866 (make-raise-markup "foo" (make-simple-markup "foo"))
872 ;; test typecheckers. Not wholly useful, because errors are detected
873 ;; in other places than they're made.
878 ;; To get error messages, see above to install the alternate
879 ;; typecheck routine for markup?.
883 (display (typecheck-with-error `(,simple-markup "foobar")))
884 (display (typecheck-with-error `(,simple-markup "foobar")))
885 (display (typecheck-with-error `(,simple-markup 1)))
887 (typecheck-with-error `(,line-markup ((,simple-markup "foobar"))
888 (,simple-markup 1))))
890 (typecheck-with-error `(,line-markup (,simple-markup "foobar")
891 (,simple-markup "bla"))))