- ))
-
-
-(define-public (set-property-markup qualifier)
- (lambda (paper props . rest )
- (interpret-markup paper
- (cons (cons `(,qualifier . ,(car rest))
- (car props)) (cdr props))
- (cadr rest))
- ))
-
-(define-public (finger-markup paper props . rest)
- (interpret-markup paper
- (cons (list '(font-size . -4)
- '(font-family . number))
- props)
- (car rest)))
-
-(define-public fontsize-markup (set-property-markup 'font-size))
-(define-public magnify-markup (set-property-markup 'font-magnification))
-
-(define-public bold-markup
- (font-markup 'font-series 'bold))
-(define-public number-markup
- (font-markup 'font-family 'number))
-(define-public roman-markup
- (font-markup 'font-family 'roman))
-
-
-(define-public huge-markup
- (font-markup 'font-relative-size 2))
-(define-public large-markup
- (font-markup 'font-relative-size 1))
-(define-public small-markup
- (font-markup 'font-relative-size -1))
-(define-public tiny-markup
- (font-markup 'font-relative-size -2))
-(define-public teeny-markup
- (font-markup 'font-relative-size -3))
-(define-public dynamic-markup
- (font-markup 'font-family 'dynamic))
-(define-public italic-markup
- (font-markup 'font-shape 'italic))
-(define-public typewriter-markup
- (font-markup 'font-family 'typewriter))
-
-
-;; TODO: baseline-skip should come from the font.
-(define-public (column-markup paper props . rest)
- (stack-lines
- -1 0.0 (cdr (chain-assoc 'baseline-skip props))
- (map (lambda (x) (interpret-markup paper props x)) (car rest)))
- )
-
-(define-public (dir-column-markup paper props . rest)
- "Make a column of args, going up or down, depending on DIRECTION."
- (let*
- (
- (dir (cdr (chain-assoc 'direction props)))
- )
- (stack-lines
- (if (number? dir) dir -1)
- 0.0 (cdr (chain-assoc 'baseline-skip props))
- (map (lambda (x) (interpret-markup paper props x)) (car rest)))
- ))
-
-(define-public (center-markup paper props . rest)
- (let*
- (
- (mols (map (lambda (x) (interpret-markup paper props x)) (car rest)))
- (cmols (map (lambda (x) (ly:molecule-align-to! x X CENTER)) mols))
- )
-
- (stack-lines
- -1 0.0 (cdr (chain-assoc 'baseline-skip props))
- mols)
- ))
-
-(define-public (right-align-markup paper props . rest)
- (let* ((m (interpret-markup paper props (car rest))))
- (ly:molecule-align-to! m X RIGHT)
- m))
-
-(define-public (halign-markup paper props . rest)
- "Set horizontal alignment. Syntax: haling A MARKUP. A=-1 is LEFT,
-A=1 is right, values in between vary alignment accordingly."
- (let* ((m (interpret-markup paper props (cadr rest))))
- (ly:molecule-align-to! m X (car rest))
- m))
-
-(define-public (left-align-markup paper props . rest)
- (let* ((m (interpret-markup paper props (car rest))))
- (ly:molecule-align-to! m X RIGHT)
- m))
-
-(define-public (musicglyph-markup paper props . rest)
- (ly:find-glyph-by-name
- (ly:paper-get-font paper (cons '((font-name . ())
- (font-shape . *)
- (font-series . *)
- (font-family . music)) props))
- (car rest)))
-
-
-(define-public (lookup-markup paper props . rest)
- "Lookup a glyph by name."
- (ly:find-glyph-by-name
- (ly:paper-get-font paper props)
- (car rest))
- )
-
-(define-public (char-markup paper props . rest)
- "Syntax: \\char NUMBER. "
- (ly:get-glyph (ly:paper-get-font paper props) (car rest))
- )
-
-(define-public (raise-markup paper props . rest)
- "Syntax: \\raise AMOUNT MARKUP. "
- (ly:molecule-translate-axis (interpret-markup
- paper
- props
- (cadr rest))
- (car rest) Y))
-
-(define-public (fraction-markup paper props . rest)
- "Make a fraction of two markups.
-
-Syntax: \\fraction MARKUP1 MARKUP2."
-
- (let*
- ((m1 (interpret-markup paper props (car rest)))
- (m2 (interpret-markup paper props (cadr rest))))
-
- (ly:molecule-align-to! m1 X CENTER)
- (ly:molecule-align-to! m2 X CENTER)
-
- (let*
- ((x1 (ly:molecule-get-extent m1 X))
- (x2 (ly:molecule-get-extent m2 X))
- (line (ly:round-filled-box (interval-union x1 x2) '(-0.05 . 0.05) 0.0))
-
- ;; should stack mols separately, to maintain LINE on baseline
- (stack (stack-lines -1 0.2 0.6 (list m1 line m2))))
-
- (ly:molecule-align-to! stack Y CENTER)
- (ly:molecule-align-to! stack X LEFT)
- ;; should have EX dimension
- ;; empirical anyway
- (ly:molecule-translate-axis stack 0.75 Y)
- )))
-
-
-(define-public (note-markup paper props . rest)
- "Syntax: \\note #LOG #DOTS #DIR. By using fractional values
-for DIR, you can obtain longer or shorter stems."
-
- (let*
- (
- (log (car rest))
- (dot-count (cadr rest))
- (dir (caddr rest))
- (font (ly:paper-get-font paper (cons '((font-family . music)) props)))
- (stemlen (max 3 (- log 1)))
- (headgl
- (ly:find-glyph-by-name font (string-append "noteheads-" (number->string (min log 2)))))
-
- (stemth 0.13)
- (stemy (* dir stemlen))
- (attachx (if (> dir 0) (- (cdr (ly:molecule-get-extent headgl X)) stemth)
- 0))
- (attachy (* dir 0.28))
- (stemgl (if (> log 0)
- (ly:round-filled-box
- (cons attachx (+ attachx stemth))
- (cons (min stemy attachy)
- (max stemy attachy))
- (/ stemth 3)
- ) #f))
- (dot (ly:find-glyph-by-name font "dots-dot"))
- (dotwid (interval-length (ly:molecule-get-extent dot X)))
- (dots (if (> dot-count 0)
- (apply ly:molecule-add
- (map (lambda (x)
- (ly:molecule-translate-axis
- dot (* (+ 1 (* 2 x)) dotwid) X) )
- (iota dot-count 1)))
- #f))
-
- (flaggl (if (> log 2)
- (ly:molecule-translate
- (ly:find-glyph-by-name
- font
- (string-append "flags-"
- (if (> dir 0) "u" "d")
- (number->string log)
- ))
- (cons (+ attachx (/ stemth 2)) stemy))
-
- #f)))
-
- (if flaggl
- (set! stemgl (ly:molecule-add flaggl stemgl)))
-
- (if (ly:molecule? stemgl)
- (set! stemgl (ly:molecule-add stemgl headgl))
- (set! stemgl headgl)
- )
-
- (if (ly:molecule? dots)
- (set! stemgl
- (ly:molecule-add
- (ly:molecule-translate-axis
- dots
- (+
- (if (and (> dir 0) (> log 2))
- (* 1.5 dotwid) 0)
- ;; huh ? why not necessary?
- ;(cdr (ly:molecule-get-extent headgl X))
- dotwid
- )
- X)
- stemgl
- )
- ))
-
- stemgl
- ))
-
-(define-public (normal-size-super-markup paper props . rest)
- (ly:molecule-translate-axis (interpret-markup
- paper
- props (car rest))
- (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
- Y)
- )
-
-(define-public (super-markup paper props . rest)
- "Syntax: \\super MARKUP. "
- (ly:molecule-translate-axis (interpret-markup
- paper
- (cons '((font-size . -3)) props) (car rest))
- (* 0.5 (cdr (chain-assoc 'baseline-skip props)))
- Y)
- )
-
-(define-public (translate-markup paper props . rest)
- "Syntax: \\translate OFFSET MARKUP. "
- (ly:molecule-translate (interpret-markup paper props (cadr rest))
- (car rest))
-
- )
-
-(define-public (sub-markup paper props . rest)
- "Syntax: \\sub MARKUP."
- (ly:molecule-translate-axis (interpret-markup
- paper
- (cons '((font-size . -3)) props)
- (car rest))
- (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
- Y)
- )
-
-(define-public (normal-size-sub-markup paper props . rest)
- (ly:molecule-translate-axis (interpret-markup
- paper
- props (car rest))
- (* -0.5 (cdr (chain-assoc 'baseline-skip props)))
- Y)
- )
-
-(define-public (hbracket-markup paper props . rest)
- "Horizontal brackets around its single argument. Syntax \\hbracket MARKUP."
+(define (compile-all-markup-expressions expr)
+ "Return a list of canonical markups expressions, e.g.:
+ (#:COMMAND1 arg11 arg12 #:COMMAND2 arg21 arg22 arg23)
+ ===>
+ ((make-COMMAND1-markup arg11 arg12)
+ (make-COMMAND2-markup arg21 arg22 arg23) ...)"
+ (do ((rest expr rest)
+ (markps '() markps))
+ ((null? rest) (reverse markps))
+ (receive (m r) (compile-markup-expression rest)
+ (set! markps (cons m markps))
+ (set! rest r))))
+
+(define (keyword->make-markup key)
+ "Transform a keyword, e.g. #:COMMAND, in a make-COMMAND-markup symbol."
+ (string->symbol (string-append "make-" (symbol->string (keyword->symbol key)) "-markup")))
+
+(define (compile-markup-expression expr)
+ "Return two values: the first complete canonical markup expression found in `expr',
+e.g. (make-COMMAND-markup arg1 arg2 ...), and the rest expression."
+ (cond ((and (pair? expr)
+ (keyword? (car expr)))
+ ;; expr === (#:COMMAND arg1 ...)
+ (let* ((command (symbol->string (keyword->symbol (car expr))))
+ (sig (markup-command-signature (car (lookup-markup-command command))))
+ (sig-len (length sig)))
+ (do ((i 0 (1+ i))
+ (args '() args)
+ (rest (cdr expr) rest))
+ ((>= i sig-len)
+ (values (cons (keyword->make-markup (car expr)) (reverse args)) rest))
+ (cond ((eqv? (list-ref sig i) markup-list?)
+ ;; (car rest) is a markup list
+ (set! args (cons `(list ,@(compile-all-markup-expressions (car rest))) args))
+ (set! rest (cdr rest)))
+ (else
+ ;; pick up one arg in `rest'
+ (receive (a r) (compile-markup-arg rest)
+ (set! args (cons a args))
+ (set! rest r)))))))
+ ((and (pair? expr)
+ (pair? (car expr))
+ (keyword? (caar expr)))
+ ;; expr === ((#:COMMAND arg1 ...) ...)
+ (receive (m r) (compile-markup-expression (car expr))
+ (values m (cdr expr))))
+ ((and (pair? expr)
+ (string? (car expr))) ;; expr === ("string" ...)
+ (values `(make-simple-markup ,(car expr)) (cdr expr)))
+ (else
+ ;; expr === (symbol ...) or ((funcall ...) ...)
+ (values (car expr)
+ (cdr expr)))))
+
+(define (compile-all-markup-args expr)
+ "Transform `expr' into markup arguments"
+ (do ((rest expr rest)
+ (args '() args))
+ ((null? rest) (reverse args))
+ (receive (a r) (compile-markup-arg rest)
+ (set! args (cons a args))
+ (set! rest r))))
+
+(define (compile-markup-arg expr)
+ "Return two values: the desired markup argument, and the rest arguments"
+ (cond ((null? expr)
+ ;; no more args
+ (values '() '()))
+ ((keyword? (car expr))
+ ;; expr === (#:COMMAND ...)
+ ;; ==> build and return the whole markup expression
+ (compile-markup-expression expr))
+ ((and (pair? (car expr))
+ (keyword? (caar expr)))
+ ;; expr === ((#:COMMAND ...) ...)
+ ;; ==> build and return the whole markup expression(s)
+ ;; found in (car expr)
+ (receive (markup-expr rest-expr) (compile-markup-expression (car expr))
+ (if (null? rest-expr)
+ (values markup-expr (cdr expr))
+ (values `(list ,markup-expr ,@(compile-all-markup-args rest-expr))
+ (cdr expr)))))
+ ((and (pair? (car expr))
+ (pair? (caar expr)))
+ ;; expr === (((foo ...) ...) ...)
+ (values (cons 'list (compile-all-markup-args (car expr))) (cdr expr)))
+ (else (values (car expr) (cdr expr)))))
+
+;;;;;;;;;;;;;;;
+;;; Debugging utilities: print markup expressions in a friendly fashion
+
+(use-modules (ice-9 format))
+(define (markup->string markup-expr)
+ "Return a string describing, in LilyPond syntax, the given markup expression."
+ (define (proc->command proc)
+ (let ((cmd-markup (symbol->string (procedure-name proc))))
+ (substring cmd-markup 0 (- (string-length cmd-markup)
+ (string-length "-markup")))))
+ (define (arg->string arg)
+ (cond ((and (pair? arg) (pair? (car arg))) ;; markup list
+ (format #f "~{ ~a~}" (map markup->string arg)))
+ ((pair? arg) ;; markup
+ (markup->string arg))
+ ((string? arg) ;; scheme string argument
+ (format #f "#\"~a\"" arg))
+ (else ;; other scheme arg
+ (format #f "#~a" arg))))
+ (let ((cmd (car markup-expr))
+ (args (cdr markup-expr)))
+ (cond ((eqv? cmd simple-markup) ;; a simple string
+ (format #f "\"~a\"" (car args)))
+ ((eqv? cmd line-markup) ;; { ... }
+ (format #f "{~a}" (arg->string (car args))))
+ ((eqv? cmd center-align-markup) ;; \center < ... >
+ (format #f "\\center-align <~a>" (arg->string (car args))))
+ ((eqv? cmd column-markup) ;; \column < ... >
+ (format #f "\\column <~a>" (arg->string (car args))))
+ (else ;; \command ...
+ (format #f "\\~a~{ ~a~} " (proc->command cmd) (map arg->string args))))))
+
+(define-public (display-markup markup-expr)
+ "Print a LilyPond-syntax equivalent for the given markup expression."
+ (display "\\markup ")
+ (display (markup->string markup-expr)))
+
+;;;;;;;;;;;;;;;
+;;; Utilities for storing and accessing markup commands signature
+;;; and keyword.
+;;; Examples:
+;;;
+;;; (set! (markup-command-signature raise-markup) (list number? markup?))
+;;; ==> ((#<primitive-procedure number?> #<procedure markup? (obj)>) . scheme0-markup1)
+;;;
+;;; (markup-command-signature raise-markup)
+;;; ==> (#<primitive-procedure number?> #<procedure markup? (obj)>)
+;;;
+;;; (markup-command-keyword raise-markup) ==> "scheme0-markup1"
+;;;
+
+(define markup-command-signatures (make-hash-table 50))
+
+(define (markup-command-signature-ref markup-command)
+ "Return markup-command's signature, e.g. (number? markup?).
+markup-command may be a procedure."
+ (let ((sig-key (hashq-ref markup-command-signatures
+ markup-command)))
+ (if sig-key (car sig-key) #f)))
+
+(define-public (markup-command-keyword markup-command)
+ "Return markup-command's keyword, e.g. \"scheme0markup1\".
+markup-command may be a procedure."
+ (let ((sig-key (hashq-ref markup-command-signatures
+ markup-command)))
+ (if sig-key (cdr sig-key) #f)))
+
+(define (markup-command-signatureset! markup-command signature)
+ "Set markup-command's signature. markup-command must be a named procedure.
+Also set markup-signature and markup-keyword object properties."
+ (hashq-set! markup-command-signatures
+ markup-command
+ (cons signature (markup-signature-to-keyword signature)))
+ ;; these object properties are still in use somewhere
+ (set-object-property! markup-command 'markup-signature signature)
+ (set-object-property! markup-command 'markup-keyword (markup-signature-to-keyword signature)))