+ (let*
+ ((stencils (map (lambda (m) (interpret-markup layout props m)) args))
+ (space (chain-assoc-get 'word-space props))
+ (text-dir (chain-assoc-get 'text-direction props RIGHT))
+ )
+
+ (if (= text-dir LEFT)
+ (set! stencils (reverse stencils)))
+
+
+ (stack-stencil-line
+ space
+ (remove ly:stencil-empty? stencils))))
+
+
+(define (wordwrap-stencils stencils
+ justify base-space line-width text-dir)
+
+ "Perform simple wordwrap, return stencil of each line."
+
+ (define space (if justify
+
+ ;; justify only stretches lines.
+ (* 0.7 base-space)
+ base-space))
+
+ (define (take-list width space stencils
+ accumulator accumulated-width)
+ "Return (head-list . tail) pair, with head-list fitting into width"
+ (if (null? stencils)
+ (cons accumulator stencils)
+ (let*
+ ((first (car stencils))
+ (first-wid (cdr (ly:stencil-extent (car stencils) X)))
+ (newwid (+ space first-wid accumulated-width))
+ )
+
+ (if
+ (or (null? accumulator)
+ (< newwid width))
+
+ (take-list width space
+ (cdr stencils)
+ (cons first accumulator)
+ newwid)
+ (cons accumulator stencils))
+ )))
+
+ (let loop
+ ((lines '())
+ (todo stencils))
+
+ (let*
+ ((line-break (take-list line-width space todo
+ '() 0.0))
+ (line-stencils (car line-break))
+ (space-left (- line-width (apply + (map (lambda (x) (cdr (ly:stencil-extent x X)))
+ line-stencils))))
+
+ (line-word-space (cond
+ ((not justify) space)
+
+ ;; don't stretch last line of paragraph.
+ ;; hmmm . bug - will overstretch the last line in some case.
+ ((null? (cdr line-break))
+ base-space)
+ ((null? line-stencils) 0.0)
+ ((null? (cdr line-stencils)) 0.0)
+ (else (/ space-left (1- (length line-stencils))))))
+
+ (line (stack-stencil-line
+ line-word-space
+ (if (= text-dir RIGHT)
+ (reverse line-stencils)
+ line-stencils))))
+
+ (if (pair? (cdr line-break))
+ (loop (cons line lines)
+ (cdr line-break))
+
+ (begin
+ (if (= text-dir LEFT)
+ (set! line
+ (ly:stencil-translate-axis line
+ (- line-width (interval-end (ly:stencil-extent line X)))
+ X)))
+ (reverse (cons line lines))
+
+ )))
+
+ ))
+
+
+(define (wordwrap-markups layout props args justify)
+ (let*
+ ((baseline-skip (chain-assoc-get 'baseline-skip props))
+ (line-width (chain-assoc-get 'line-width props))
+ (word-space (chain-assoc-get 'word-space props))
+ (text-dir (chain-assoc-get 'text-direction props RIGHT))
+ (lines (wordwrap-stencils
+ (remove ly:stencil-empty?
+ (map (lambda (m) (interpret-markup layout props m)) args))
+ justify word-space line-width
+ text-dir)
+ ))
+
+ (stack-lines DOWN 0.0 baseline-skip lines)))
+
+(def-markup-command (justify layout props args) (markup-list?)
+ "Like wordwrap, but with lines stretched to justify the margins.
+Use @code{\\override #'(line-width . X)} to set line-width, where X
+is the number of staff spaces."
+
+ (wordwrap-markups layout props args #t))
+
+(def-markup-command (wordwrap layout props args) (markup-list?)
+ "Simple wordwrap. Use @code{\\override #'(line-width . X)} to set
+line-width, where X is the number of staff spaces."
+
+ (wordwrap-markups layout props args #f))
+
+(define (wordwrap-string layout props justify arg)
+ (let*
+ ((baseline-skip (chain-assoc-get 'baseline-skip props))
+ (line-width (chain-assoc-get 'line-width props))
+ (word-space (chain-assoc-get 'word-space props))
+
+ (para-strings (regexp-split
+ (string-regexp-substitute "\r" "\n"
+ (string-regexp-substitute "\r\n" "\n" arg))
+ "\n[ \t\n]*\n[ \t\n]*"))
+
+ (text-dir (chain-assoc-get 'text-direction props RIGHT))
+ (list-para-words (map (lambda (str)
+ (regexp-split str "[ \t\n]+"))
+ para-strings))
+ (para-lines (map (lambda (words)
+ (let*
+ ((stencils
+ (remove
+ ly:stencil-empty? (map
+ (lambda (x)
+ (interpret-markup layout props x))
+ words)))
+ (lines (wordwrap-stencils stencils
+ justify word-space
+ line-width text-dir
+ )))
+
+ lines))
+
+ list-para-words)))
+
+ (stack-lines DOWN 0.0 baseline-skip (apply append para-lines))))
+
+
+(def-markup-command (wordwrap-string layout props arg) (string?)
+ "Wordwrap a string. Paragraphs may be separated with double newlines"
+ (wordwrap-string layout props #f arg))
+
+(def-markup-command (justify-string layout props arg) (string?)
+ "Justify a string. Paragraphs may be separated with double newlines"
+ (wordwrap-string layout props #t arg))
+
+
+(def-markup-command (wordwrap-field layout props symbol) (symbol?)
+ (let* ((m (chain-assoc-get symbol props)))
+ (if (string? m)
+ (interpret-markup layout props
+ (list wordwrap-string-markup m))
+ (ly:make-stencil '() '(1 . -1) '(1 . -1)))))
+
+(def-markup-command (justify-field layout props symbol) (symbol?)
+- (let* ((m (chain-assoc-get symbol props)))
+ (if (string? m)
+ (interpret-markup layout props
+ (list justify-string-markup m))
+ (ly:make-stencil '() '(1 . -1) '(1 . -1)))))
+
+
+
+(def-markup-command (combine layout props m1 m2) (markup? markup?)
+ "Print two markups on top of each other."
+ (let* ((s1 (interpret-markup layout props m1))
+ (s2 (interpret-markup layout props m2)))
+ (ly:stencil-add s1 s2)))
+
+;;
+;; TODO: should extract baseline-skip from each argument somehow..
+;;
+(def-markup-command (column layout props args) (markup-list?)
+ "Stack the markups in @var{args} vertically. The property
+@code{baseline-skip} determines the space between each markup in @var{args}."
+
+ (let*
+ ((arg-stencils (map (lambda (m) (interpret-markup layout props m)) args))
+ (skip (chain-assoc-get 'baseline-skip props)))
+
+
+ (stack-lines
+ -1 0.0 skip
+ (remove ly:stencil-empty? arg-stencils))))
+
+
+(def-markup-command (dir-column layout props args) (markup-list?)
+ "Make a column of args, going up or down, depending on the setting
+of the @code{#'direction} layout property."
+ (let* ((dir (chain-assoc-get 'direction props)))
+ (stack-lines
+ (if (number? dir) dir -1)
+ 0.0
+ (chain-assoc-get 'baseline-skip props)
+ (map (lambda (x) (interpret-markup layout props x)) args))))
+
+(def-markup-command (center-align layout props args) (markup-list?)
+ "Put @code{args} in a centered column. "
+ (let* ((mols (map (lambda (x) (interpret-markup layout props x)) args))
+ (cmols (map (lambda (x) (ly:stencil-aligned-to x X CENTER)) mols)))
+
+ (stack-lines -1 0.0 (chain-assoc-get 'baseline-skip props) cmols)))
+
+(def-markup-command (vcenter layout props arg) (markup?)
+ "Align @code{arg} to its Y center. "
+ (let* ((mol (interpret-markup layout props arg)))
+ (ly:stencil-aligned-to mol Y CENTER)))
+
+(def-markup-command (hcenter layout props arg) (markup?)
+ "Align @code{arg} to its X center. "
+ (let* ((mol (interpret-markup layout props arg)))
+ (ly:stencil-aligned-to mol X CENTER)))
+
+(def-markup-command (right-align layout props arg) (markup?)
+ "Align @var{arg} on its right edge. "
+ (let* ((m (interpret-markup layout props arg)))
+ (ly:stencil-aligned-to m X RIGHT)))
+
+(def-markup-command (left-align layout props arg) (markup?)
+ "Align @var{arg} on its left edge. "
+ (let* ((m (interpret-markup layout props arg)))
+ (ly:stencil-aligned-to m X LEFT)))
+
+(def-markup-command (general-align layout props axis dir arg) (integer? number? markup?)
+ "Align @var{arg} in @var{axis} direction to the @var{dir} side."
+ (let* ((m (interpret-markup layout props arg)))
+ (ly:stencil-aligned-to m axis dir)))
+
+(def-markup-command (halign layout props dir arg) (number? markup?)
+ "Set horizontal alignment. If @var{dir} is @code{-1}, then it is
+left-aligned, while @code{+1} is right. Values in between interpolate
+alignment accordingly."
+ (let* ((m (interpret-markup layout props arg)))
+ (ly:stencil-aligned-to m X dir)))
+
+
+
+(def-markup-command (with-dimensions layout props x y arg) (number-pair? number-pair? markup?)
+ "Set the dimensions of @var{arg} to @var{x} and @var{y}."
+
+ (let* ((m (interpret-markup layout props arg)))
+ (ly:make-stencil (ly:stencil-expr m) x y)))
+
+
+(def-markup-command (pad-around layout props amount arg) (number? markup?)
+
+ "Add padding @var{amount} all around @var{arg}. "
+
+ (let*
+ ((m (interpret-markup layout props arg))
+ (x (ly:stencil-extent m X))
+ (y (ly:stencil-extent m Y)))
+
+
+ (ly:make-stencil (ly:stencil-expr m)
+ (interval-widen x amount)
+ (interval-widen y amount))
+ ))
+
+
+(def-markup-command (pad-x layout props amount arg) (number? markup?)
+
+ "Add padding @var{amount} around @var{arg} in the X-direction. "
+ (let*
+ ((m (interpret-markup layout props arg))
+ (x (ly:stencil-extent m X))
+ (y (ly:stencil-extent m Y)))
+
+
+ (ly:make-stencil (ly:stencil-expr m)
+ (interval-widen x amount)
+ y)
+ ))
+
+
+(def-markup-command (put-adjacent layout props arg1 axis dir arg2) (markup? integer? ly:dir? markup?)
+
+ "Put @var{arg2} next to @var{arg1}, without moving @var{arg1}. "
+
+ (let* ((m1 (interpret-markup layout props arg1))
+ (m2 (interpret-markup layout props arg2)))
+
+ (ly:stencil-combine-at-edge m1 axis dir m2 0.0 0.0)
+ ))
+
+(def-markup-command (transparent layout props arg) (markup?)
+ "Make the argument transparent"
+ (let*
+ ((m (interpret-markup layout props arg))
+ (x (ly:stencil-extent m X))
+ (y (ly:stencil-extent m Y)))
+
+
+
+ (ly:make-stencil ""
+ x y)))
+
+
+(def-markup-command (pad-to-box layout props x-ext y-ext arg) (number-pair? number-pair? markup?)
+ "Make @var{arg} take at least @var{x-ext}, @var{y-ext} space"
+
+ (let*
+ ((m (interpret-markup layout props arg))
+ (x (ly:stencil-extent m X))
+ (y (ly:stencil-extent m Y)))
+
+ (ly:make-stencil (ly:stencil-expr m)
+ (interval-union x-ext x)
+ (interval-union y-ext y))))
+
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; property
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;