+
+(define-public (fontify-text font-metric text)
+ "Set TEXT with font FONT-METRIC, returning a stencil."
+ (let* ((b (ly:text-dimension font-metric text)))
+ (ly:make-stencil
+ `(text ,font-metric ,text) (car b) (cdr b))))
+
+(define-public (fontify-text-white scale font-metric text)
+ "Set TEXT with scale factor SCALE"
+ (let* ((b (ly:text-dimension font-metric text))
+ ;;urg -- workaround for using ps font
+ (c `(white-text ,(* 2 scale) ,text)))
+ ;;urg -- extent is not from ps font, but we hope it's close
+ (ly:make-stencil c (car b) (cdr b))))
+
+(define-public (stencil-with-color stencil color)
+ (ly:make-stencil
+ (list 'color color (ly:stencil-expr stencil))
+ (ly:stencil-extent stencil X)
+ (ly:stencil-extent stencil Y)))
+
+(define-public (stencil-whiteout stencil)
+ (let*
+ ((x-ext (ly:stencil-extent stencil X))
+ (y-ext (ly:stencil-extent stencil Y))
+
+ )
+
+ (ly:stencil-add
+ (stencil-with-color (ly:round-filled-box x-ext y-ext 0.0)
+ white)
+ stencil)
+ ))
+
+(define-public (dimension-arrows destination)
+ "Draw twosided arrow from here to @var{destination}"
+
+ (let*
+ ((e_x 1+0i)
+ (e_y 0+1i)
+ (rotate (lambda (z ang)
+ (* (make-polar 1 ang)
+ z)))
+ (complex-to-offset (lambda (z)
+ (list (real-part z) (imag-part z))))
+
+ (z-dest (+ (* e_x (car destination)) (* e_y (cdr destination))))
+ (e_z (/ z-dest (magnitude z-dest)))
+ (triangle-points '(-1+0.25i
+ 0
+ -1-0.25i))
+ (p1s (map (lambda (z)
+ (+ z-dest (rotate z (angle z-dest))))
+ triangle-points))
+ (p2s (map (lambda (z)
+ (rotate z (angle (- z-dest))))
+ triangle-points))
+ (null (cons 0 0))
+ (arrow-1
+ (ly:make-stencil
+ `(polygon (quote ,(concatenate (map complex-to-offset p1s)))
+ 0.0
+ #t) null null))
+ (arrow-2
+ (ly:make-stencil
+ `(polygon (quote ,(concatenate (map complex-to-offset p2s)))
+ 0.0
+ #t) null null ) )
+ (thickness 0.1)
+ (shorten-line 0.5)
+ (start (complex-to-offset (/ (* e_z shorten-line) 2)))
+ (end (complex-to-offset (- z-dest (/ (* e_z shorten-line) 2))))
+
+ (line (ly:make-stencil
+ `(draw-line ,thickness
+ ,(car start) ,(cadr start)
+ ,(car end) ,(cadr end)
+ )
+ (cons (min 0 (car destination))
+ (min 0 (cdr destination)))
+ (cons (max 0 (car destination))
+ (max 0 (cdr destination)))))
+
+ (result (ly:stencil-add arrow-2 arrow-1 line)))
+
+
+ result))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; ANNOTATIONS
+;;
+;; annotations are arrows indicating the numerical value of
+;; spacing variables
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(define*-public (annotate-y-interval layout name extent is-length
+ #:key (color darkblue))
+ (let ((text-props (cons '((font-size . -3)
+ (font-family . typewriter))
+ (layout-extract-page-properties layout)))
+ (annotation #f))
+ (define (center-stencil-on-extent stil)
+ (ly:stencil-translate (ly:stencil-aligned-to stil Y CENTER)
+ (cons 0 (interval-center extent))))
+ ;; do something sensible for 0,0 intervals.
+ (set! extent (interval-widen extent 0.001))
+ (if (not (interval-sane? extent))
+ (set! annotation (interpret-markup
+ layout text-props
+ (make-simple-markup (simple-format #f "~a: NaN/inf" name))))
+ (let ((text-stencil (interpret-markup
+ layout text-props
+ (markup #:whiteout #:simple name)))
+ (dim-stencil (interpret-markup
+ layout text-props
+ (markup #:whiteout
+ #:simple (cond
+ ((interval-empty? extent)
+ (format "empty"))
+ (is-length
+ (ly:format "~$" (interval-length extent)))
+ (else
+ (ly:format "(~$,~$)"
+ (car extent) (cdr extent)))))))
+ (arrows (ly:stencil-translate-axis
+ (dimension-arrows (cons 0 (interval-length extent)))
+ (interval-start extent) Y)))
+ (set! annotation
+ (center-stencil-on-extent text-stencil))
+ (set! annotation
+ (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5))
+ (set! annotation
+ (ly:stencil-combine-at-edge annotation X LEFT
+ (center-stencil-on-extent dim-stencil)
+ 0.5))
+ (set! annotation
+ (ly:make-stencil (list 'color color (ly:stencil-expr annotation))
+ (ly:stencil-extent annotation X)
+ (cons 10000 -10000)))))
+ annotation))
+
+
+(define-public (eps-file->stencil axis size file-name)
+ (let*
+ ((contents (ly:gulp-file file-name))
+ (bbox (get-postscript-bbox (car (string-split contents #\nul))))
+ (bbox-size (if (= axis X)
+ (- (list-ref bbox 2) (list-ref bbox 0))
+ (- (list-ref bbox 3) (list-ref bbox 1))
+ ))
+ (factor (if (< 0 bbox-size)
+ (exact->inexact (/ size bbox-size))
+ 0))
+ (scaled-bbox
+ (map (lambda (x) (* factor x)) bbox))
+ (clip-rect-string (ly:format
+ "~a ~a ~a ~a rectclip"
+ (list-ref bbox 0)
+ (list-ref bbox 1)
+ (- (list-ref bbox 2) (list-ref bbox 0))
+ (- (list-ref bbox 3) (list-ref bbox 1)))))