-;;;; fret-diagrams.scm --
-;;;;
-;;;; source file of the GNU LilyPond music typesetter
-;;;;
-;;;; (c) 2004 Carl D. Sorensen <c_sorensen@byu.edu>
-
-(define nil '())
-(define (fret-parse-string definition-string)
- "parse a fret diagram string and return an alist with the appropriate values"
- (let* ((fret-count 4)
- (string-count 6)
- (thickness 0.05)
- (finger-code 0)
- (dot-size 0.25)
- (position 0.6)
- (fret-range (list 1 fret-count))
- (barre-list '())
- (dot-list '())
- (xo-list '())
- (output-list '())
- (items (string-split definition-string #\;)))
- (let parse-item ((myitems items))
- (if (not (null? (cdr myitems)))
- (let ((test-string (car myitems)))
- (case (car (string->list (substring test-string 0 1)))
- ((#\f) (set! finger-code (get-numeric-from-key test-string)))
- ((#\t) (set! thickness (get-numeric-from-key test-string)))
- ((#\c) (set! barre-list (cons* (numerify (string-split (substring test-string 2) #\-))
- barre-list)))
- ((#\h) (set! fret-count (get-numeric-from-key test-string)))
- ((#\w) (set! string-count (get-numeric-from-key test-string)))
- ((#\d) (set! dot-size (get-numeric-from-key test-string)))
- ((#\p) (set! position (get-numeric-from-key test-string)))
- (else
- (let ((this-list (string-split test-string #\-)))
- ;(display this-list)
- (if (string->number (cadr this-list))
- (set! dot-list (cons* (numerify this-list) dot-list))
- (set! xo-list (cons* (numerify this-list) xo-list))))))
- (parse-item (cdr myitems)))))
- ; calculate fret-range
- (let ((maxfret 0) (minfret 99))
- (let updatemax ((fret-list dot-list))
- (if (null? fret-list)
- '()
- (let ((fretval (cadar fret-list)))
- (if (> fretval maxfret) (set! maxfret fretval))
- (if (< fretval minfret) (set! minfret fretval))
- (updatemax (cdr fret-list)))))
- (if (> maxfret fret-count)
- (set! fret-range (list minfret
- (let ((upfret (- (+ minfret fret-count) 1)))
- (if (> maxfret upfret) maxfret upfret)))))
- ; subtract fret from dots
- (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))
- (acons "string-count" string-count
- (acons "dot-size" dot-size
- (acons "position" position
- (acons "finger-code" finger-code
- (acons "fret-range" fret-range
- (acons "thickness" thickness
- (acons "barre-list" barre-list
- (acons "dot-list" dot-list
- (acons "xo-list" xo-list '())))))))))))
-
-(define (subtract-base-fret base-fret dot-list)
-
- (if (null? dot-list)
- '()
- (let ((this-list (car dot-list)))
- (cons* (list (car this-list) (- (cadr this-list) base-fret) (if (null? (cddr this-list))
- nil
- (caddr this-list)))
- (subtract-base-fret base-fret (cdr dot-list))))))
-
-(define (draw-strings string-count fret-range th size)
- (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
- (sl (* (+ fret-count 1) size))
- (half-thickness (* th 0.5))
- (string-stencil (ly:make-stencil (list 'draw-line th 0 0 0 sl)
- (cons (- half-thickness) half-thickness)
- (cons (- half-thickness) (+ sl half-thickness)))))
- (if (= string-count 1)
- string-stencil
- (ly:stencil-combine-at-edge
- (draw-strings (- string-count 1) fret-range th size) 0 1
- string-stencil
- (- size th) 0))))
-
-(define (draw-fret-lines fret-count string-count th size)
- (let* ((fret-length (* (- string-count 1) size))
- (half-thickness (* th 0.5))
- (fret-line (ly:make-stencil (list 'draw-line th 0 size fret-length size)
- (cons 0 fret-length)
- (cons (- size half-thickness) (+ size half-thickness)))))
- (if (= fret-count 1)
- fret-line
- (ly:stencil-combine-at-edge fret-line Y UP
- (draw-fret-lines (- fret-count 1) string-count th size)
- (- size th) 0))))
-
-(define (draw-frets paper fret-range string-count th size)
- (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
- (fret-length (* (- string-count 1) size))
- (half-thickness (* th 0.5))
- (base-fret (car fret-range)))
- (ly:stencil-combine-at-edge
- (draw-fret-lines fret-count string-count th size) Y UP
- (if (= base-fret 1)
- (draw-fret-lines 1 string-count (* th 2) size)
- (draw-fret-lines 1 string-count th size))
- (- size th) 0)))
-
-(define (draw-dots paper string-count fret-range size dot-size position finger-code dot-list)
- "Make dots for fret diagram."
- (let* ((dot-radius (* size dot-size))
- (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
- (mypair (car dot-list))
- (restlist (cdr dot-list))
- (xpos (* (- string-count (car mypair)) size))
- (ypos (* (+ 4 (- fret-count (cadr mypair) position )) size))
- (finger (caddr mypair))
- (font (ly:paper-get-font paper `(((font-magnification . ,(* 0.8 size))(font-name . "cmss8")
- (font-encoding Tex-text)))))
- (font2 (ly:paper-get-font paper `(((font-magnification . ,(* (* 2 dot-size) size))(font-name . "cmss8")
- (font-encoding Tex-text)))))
- (font3 (ly:paper-get-font paper `(((font-magnification . ,(* (* 3 dot-size) size))(font-name . "cmss8")
- (font-encoding Tex-text)))))
- (extent (cons (- (* size 0.25)) (* size 0.25)))
- (dotstencil (if (or (eq? finger nil)(eq? finger-code 0))
- (ly:make-stencil (list 'dot xpos ypos dot-radius ) extent extent)
- (if (eq? finger-code 1)
- ; TODO -- Get nice circled numbers in the font, instead of this kludge
- (ly:stencil-add
- (ly:stencil-translate-axis
- (ly:stencil-translate-axis
- (fontify-text font2 (number->string finger)) (- xpos (* size 0.3)) X)
- (- ypos (* 1 dot-size size)) Y)
- (ly:stencil-translate-axis
- (ly:stencil-translate-axis
- (fontify-text font3 "O") (- xpos (* 2.2 dot-size size)) X)
- (- ypos (* 1.7 dot-size size)) Y))
- (if (eq? finger-code 2)
- (ly:stencil-add
- (ly:make-stencil (list 'dot xpos ypos dot-radius ) extent extent)
- (ly:stencil-translate-axis
- (ly:stencil-translate-axis
- (fontify-text font (number->string finger)) (- xpos (* size 0.3)) X)
- (- size) Y)))))))
- (if (null? restlist)
- dotstencil
- (ly:stencil-add (draw-dots paper string-count fret-range size dot-size position finger-code restlist)
- dotstencil))))
-
-(define (draw-xo paper string-count fret-range size xo-list)
-"Put x and o on chord diagram."
- (let* ((dot-radius (* size 0.25))
- (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
- (font (ly:paper-get-font paper `(((font-size . ,(* -5 (+ 1 (* 2.6 (- 1 size)))))(font-family . music)))))
- (mypair (car xo-list))
- (restlist (cdr xo-list))
-;TODO -- get better glyphs in font to use for x (mute string) and o (open string)
-; Perhaps explore just using sans-serif font?
- (glyph-name (if (char=? (cadr mypair) #\x) "noteheads-2cross"
- "scripts-open"))
- (tmpdot (if (char=? (cadr mypair) #\x) 0 (* size 0.25)))
- (xpos (if (char=? (cadr mypair) #\x)
- (- (* (- string-count (car mypair)) size) (* .35 size) )
- (* (- string-count (car mypair)) size)))
- (ypos (* (+ 3.5 fret-count) size))
- (extent (cons (- (* size 0.25)) (* size 0.25)))
- (glyph-stencil (ly:stencil-translate-axis
- (ly:stencil-translate-axis (ly:find-glyph-by-name font glyph-name) ypos Y)
- xpos X)))
- (if (null? restlist)
- glyph-stencil
- (ly:stencil-add
- (draw-xo paper string-count fret-range size restlist)
- glyph-stencil))))
-
-(define (make-bezier-sandwich-list left right bottom height thickness)
- (let* ((width (+ (- right left) 1))
- (x1 (+ (* width 0.1) left))
- (x2 (- right (* width 0.1)))
- (bottom-control-point-height (+ bottom (- height thickness)))
- (top-control-point-height (+ bottom height)))
-; order of points is: left cp low, right cp low, right end low, left end low
-; right cp high, left cp high, left end high, right end high.
- (list (cons x1 bottom-control-point-height) (cons x2 bottom-control-point-height) (cons right bottom) (cons left bottom)
- (cons x2 top-control-point-height) (cons x1 top-control-point-height) (cons left bottom) (cons right bottom))))
-
-(define (draw-barre paper string-count fret-range size barre-list)
- "Create barre indications for a chord diagram"
- (if (not (null? barre-list))
- (let* ((string1 (caar barre-list))
- (string2 (cadar barre-list))
- (fret (caddar barre-list))
- (bottom (* size (+ 1.5 (- (cadr fret-range) fret))))
- (left (* size (- string-count string1)))
- (right (* size (- string-count string2)))
- (bezier-list (make-bezier-sandwich-list left right bottom (* size 0.5) (* size 0.1)))
- (sandwich-stencil (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size 0.1) )
- (cons 0 right)
- (cons 0 (+ bottom (* size 0.8))))))
- (if (not (null? (cdr barre-list)))
- (ly:stencil-add sandwich-stencil
- (draw-barre paper string-count fret-range size (cdr barre-list)))
- sandwich-stencil ))))
-
-(define (label-fret paper string-count fret-range size)
- "Label the base fret on a fret diagram"
- (let ((base-fret (car fret-range))
- (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
- (font (ly:paper-get-font paper `(((font-magnification . ,(* 0.8 size))(font-name . "cmss8")
- (font-encoding Tex-text))))))
- (ly:stencil-translate-axis
- (ly:stencil-translate-axis (fontify-text font (if (> base-fret 1)
- (format #f "~(~:@r~)" base-fret)
- " ")) (* (- string-count 0.5) size) X)
- (* (- fret-count 0.2) size) Y)))
-
-(define (get-numeric-from-key keystring)
- "Get the numeric value from a key of the form k:val"
- (string->number (substring keystring 2 (string-length keystring) )))
-
-(define (numerify mylist)
- "Convert string values to numeric or character"
- (if (null? mylist)
- '()
- (let ((numeric-value (string->number (car mylist))))
- (if numeric-value
- (cons* numeric-value (numerify (cdr mylist)))
- (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))
-
-
-(define (make-fret-diagram paper size definition-string)
- "Make a fret diagram"
- (let* ((parameters (fret-parse-string definition-string))
- (string-count (cdr (assoc "string-count" parameters)))
- (fret-range (cdr (assoc "fret-range" parameters)))
- (finger-code (cdr (assoc "finger-code" parameters)))
- (dot-size (cdr (assoc "dot-size" parameters)))
- (position (cdr (assoc "position" parameters)))
- (dot-list (cdr (assoc "dot-list" parameters)))
- (xo-list (cdr (assoc "xo-list" parameters)))
- (line-thickness (cdr (assoc "thickness" parameters)))
- (barre-list (cdr (assoc "barre-list" parameters)))
- (fret-diagram-stencil (ly:stencil-add
- (draw-strings string-count fret-range line-thickness size)
- (draw-frets paper fret-range string-count line-thickness size))))
- (if (not (null? dot-list))
- (set! fret-diagram-stencil (ly:stencil-add
- (draw-dots paper string-count fret-range size dot-size position finger-code dot-list)
- fret-diagram-stencil)))
- (if (not (null? xo-list))
- (set! fret-diagram-stencil (ly:stencil-add
- (draw-xo paper string-count fret-range size xo-list)
- fret-diagram-stencil)))
- (if (not (null? barre-list))
- (set! fret-diagram-stencil (ly:stencil-add
- (draw-barre paper string-count fret-range size barre-list)
- fret-diagram-stencil)))
- (set! fret-diagram-stencil (ly:stencil-add fret-diagram-stencil (label-fret paper string-count fret-range size)))
- (ly:stencil-align-to! fret-diagram-stencil X -.4)
- fret-diagram-stencil))
-
-(def-markup-command (fret-diagram paper props size definition-string)
- (number? string?)
- "Syntax: \\fret-diagram size definition-string
- eg: \\markup \\fret-diagram #0.75 #\"6-x;5-x;4-o;3-2;2-3;1-2;\"
- for fret spacing 3/4 of staff space, D chord diagram
- Syntax rules for @var{definition-string}:
- Diagram items are separated by semicolons.
- Possible items:
- t:number -- set the line thickness (in staff spaces). Default 0.05
- h:number -- set the height of the diagram in frets. Default 4
- w:number -- set the width of the diagram in strings. Default 6
- f:number -- set fingering label type
- (0 = none, 1 = in circle on string, 2 = below string) Default 0
- d:number -- set radius of dot, in terms of fret spacing. Default 0.25
- p:number -- set the position of the dot in the fret space. 0.5 is centered; 1 is on lower fret bar,
- 0 is on upper fret bar. Default 0.6
- c:string1-string2-fret -- include a barre mark from string1 to string2 on fret
- string-fret -- place a dot on string at fret. If fret is o, string is identified
- as open. If fret is x, string is identified as muted.
- string-fret-fingering -- place a dot on string at fret, and label with fingering as
- defined by f: code.
- Note: There is no limit to the number of fret indications per string."
- (make-fret-diagram paper size definition-string))
+;;;; fret-diagrams.scm -- \r
+;;;;\r
+;;;; source file of the GNU LilyPond music typesetter\r
+;;;; \r
+;;;; (c) 2004 Carl D. Sorensen <c_sorensen@byu.edu>\r
+\r
+\r
+(define (fret-parse-marking-list marking-list fret-count)\r
+ (let* ((fret-range (list 1 fret-count))\r
+ (barre-list '())\r
+ (dot-list '())\r
+ (xo-list '())\r
+ (output-alist '()))\r
+ (let parse-item ((mylist marking-list))\r
+ (if (not (null? mylist))\r
+ (let* ((my-item (car mylist)) (my-code (car my-item)))\r
+ (cond\r
+ ((or (eq? my-code 'open)(eq? my-code 'mute))\r
+ (set! xo-list (cons* my-item xo-list)))\r
+ ((eq? my-code 'barre)\r
+ (set! barre-list (cons* (cdr my-item) barre-list)))\r
+ ((eq? my-code 'place-fret)\r
+ (set! dot-list (cons* (cdr my-item) dot-list))))\r
+ (parse-item (cdr mylist)))))\r
+ ; calculate fret-range\r
+ (let ((maxfret 0) (minfret 99))\r
+ (let updatemax ((fret-list dot-list))\r
+ (if (null? fret-list)\r
+ '()\r
+ (let ((fretval (second (car fret-list))))\r
+ (if (> fretval maxfret) (set! maxfret fretval))\r
+ (if (< fretval minfret) (set! minfret fretval))\r
+ (updatemax (cdr fret-list)))))\r
+ (if (> maxfret fret-count)\r
+ (set! fret-range (list minfret\r
+ (let ((upfret (- (+ minfret fret-count) 1)))\r
+ (if (> maxfret upfret) maxfret upfret)))))\r
+ ; subtract fret from dots\r
+ (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))\r
+; (display "barre-list ") (display barre-list) (display "\n")\r
+ (acons 'fret-range fret-range\r
+ (acons 'barre-list barre-list\r
+ (acons 'dot-list dot-list\r
+ (acons 'xo-list xo-list '()))))))\r
+ \r
+(define (subtract-base-fret base-fret dot-list)\r
+"Subtract @var{base-fret} from every fret in @var{dot-list}" \r
+ (if (null? dot-list)\r
+ '()\r
+ (let ((this-list (car dot-list)))\r
+ (cons* (list (car this-list) (- (second this-list) base-fret) (if (null? (cddr this-list))\r
+ '()\r
+ (third this-list)))\r
+ (subtract-base-fret base-fret (cdr dot-list))))))\r
+\r
+(define (draw-strings string-count fret-range th size)\r
+"Draw the strings (vertical lines) for a fret diagram with @var{string-count} strings and frets as indicated\r
+ in @var{fret-range}. Line thickness is given by @var{th}, fret & string spacing by @var{size}. "\r
+ (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))\r
+ (sl (* (+ fret-count 1) size))\r
+ (sth (* size th))\r
+ (half-thickness (* sth 0.5))\r
+ (gap (- size sth))\r
+ (string-stencil (ly:make-stencil (list 'draw-line sth 0 0 0 sl)\r
+ (cons (- half-thickness) half-thickness)\r
+ (cons (- half-thickness) (+ sl half-thickness)))))\r
+ (if (= string-count 1)\r
+ string-stencil\r
+ (ly:stencil-combine-at-edge\r
+ (draw-strings (- string-count 1) fret-range th size) X RIGHT\r
+ string-stencil\r
+ gap 0))))\r
+\r
+(define (draw-fret-lines fret-count string-count th size)\r
+ "Draw @var{fret-count} frets (horizontal lines) for a fret diagram with @var{string-count} strings.\r
+ Line thickness is given by @var{th}, fret & string spacing by @var{size}. "\r
+ (let* ((fret-length (* (- string-count 1) size))\r
+ (sth (* size th))\r
+ (half-thickness (* sth 0.5))\r
+ (gap (- size sth))\r
+ (fret-line (ly:make-stencil (list 'draw-line sth half-thickness size (- fret-length half-thickness) size)\r
+ (cons 0 fret-length)\r
+ (cons (- size half-thickness) (+ size half-thickness)))))\r
+ (if (= fret-count 1)\r
+ fret-line\r
+ (ly:stencil-combine-at-edge fret-line Y UP\r
+ (draw-fret-lines (- fret-count 1) string-count th size)\r
+ gap 0))))\r
+ \r
+(define (draw-thick-top-fret props string-count th size)\r
+ "Draw a thick top fret for a fret diagram whose base fret is not 1."\r
+ (let* ((sth (* th size))\r
+ (top-fret-thick (* sth (chain-assoc-get 'top-fret-thickness props 3.0)))\r
+ (top-half-thick (* top-fret-thick 0.5))\r
+ (half-thick (* sth 0.5))\r
+ (x1 half-thick)\r
+ (x2 (+ half-thick (* size (- string-count 1))))\r
+ (y1 0)\r
+ (y2 top-fret-thick)\r
+ (x-extent (cons (- x1) x2))\r
+ (y-extent (cons 0 y2)))\r
+ (ly:make-stencil (list 'round-filled-box x1 x2 y1 y2 th)\r
+ x-extent y-extent))) \r
+ \r
+ \r
+(define (draw-frets paper props fret-range string-count th size)\r
+ "Draw the frets (horizontal lines) for a fret diagram with @var{string-count} strings and frets as indicated\r
+ in @var{fret-range}. Line thickness is given by @var{th}, fret & string spacing by @var{size}. "\r
+ (let* ((top-fret-thick (* th (chain-assoc-get 'top-fret-thickness props 3.0)))\r
+ (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))\r
+ (fret-length (* (- string-count 1) size))\r
+ (half-thickness (* th 0.5))\r
+ (base-fret (car fret-range)))\r
+ (ly:stencil-combine-at-edge\r
+ (draw-fret-lines fret-count string-count th size) Y UP\r
+ (if (= base-fret 1)\r
+ (draw-thick-top-fret props string-count th size)\r
+ (draw-fret-lines 1 string-count th size)) \r
+ (- size th) 0))) \r
+ \r
+(define (get-center-shift extent)\r
+"Return the amount of shift required to make @var{extent} be centered"\r
+ (let ((minval (car extent))\r
+ (maxval (cdr extent)))\r
+ (/ (- minval maxval) 2)))\r
+\r
+\r
+;; ly:stencil-get-extent is missing from 2.2 on\r
+;(define (centered-text-stencil font text)\r
+;"Create a centered text stencil of @var{text} in font @var{font}"\r
+;(let* ((text-stencil (fontify-text font text))\r
+; (x-shift (get-center-shift (ly:stencil-get-extent text-stencil X)))\r
+; (y-shift (get-center-shift y-extent (ly:stencil-get-extent text-stencil Y))))\r
+; (ly:stencil-translate-axis \r
+; (ly:stencil-translate-axis text-stencil x-shift X)\r
+; y-shift Y))) \r
+\r
+(define (draw-dots paper props string-count fret-range size dot-position dot-list)\r
+ "Make dots for fret diagram."\r
+; (display "In draw-dots" dot-list)\r
+ (let* ((finger-code (chain-assoc-get 'finger-code props 'none))\r
+ (dot-radius (* size (chain-assoc-get 'dot-radius props 0.25)))\r
+ (finger-xoffset (chain-assoc-get 'finger-xoffset props -0.25))\r
+ (finger-yoffset (chain-assoc-get 'finger-yoffset props (- size)))\r
+ (label-font-name (chain-assoc-get 'label-font-name props "cmss8"))\r
+ (dot-label-font-mag (* size (chain-assoc-get 'dot-label-font-mag props .45)))\r
+ (dot-circle-font-mag (* size (chain-assoc-get 'dot-circle-font-mag props .75)))\r
+ (string-label-font-mag (* size (chain-assoc-get 'string-label-font-mag props 0.6)))\r
+ (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))\r
+ (mypair (car dot-list))\r
+ (restlist (cdr dot-list))\r
+ (xpos (* size (- string-count (car mypair))))\r
+;TODO -- figure out what 4 is and get rid of it\r
+;UGH -- 4?\r
+ (ypos (* size (+ 4 (- fret-count (cadr mypair) dot-position ))))\r
+ (finger (caddr mypair))\r
+ (finger (if (number? finger) (number->string finger) finger))\r
+ (string-label-font (ly:paper-get-font paper `(((font-magnification . ,string-label-font-mag)\r
+ (font-name . ,label-font-name)))))\r
+ (dot-label-font (ly:paper-get-font paper `(((font-magnification . ,dot-label-font-mag)\r
+ (font-name . ,label-font-name)))))\r
+ (dot-circle-font (ly:paper-get-font paper `(((font-magnification . ,dot-circle-font-mag)\r
+ (font-name . ,label-font-name)))))\r
+ (extent (cons (- dot-radius) dot-radius))\r
+ (dotstencil (if (or (eq? finger '())(eq? finger-code 'none))\r
+ (ly:make-stencil (list 'dot xpos ypos dot-radius ) extent extent)\r
+ (if (eq? finger-code 'white-circled)\r
+; TODO -- Get nice circled numbers in the font, instead of this kludge\r
+; UGH -- Constants in here need to go\r
+; Note: for finger (this stencil) xpos should be related only to font size, ypos should be related to both font size and dot-position\r
+; These are not yet worked out, and should be. But perhaps I'd like first to get a nice font for circled numbers / letters \r
+ (ly:stencil-add \r
+ (ly:stencil-translate-axis \r
+ (ly:stencil-translate-axis \r
+ (fontify-text dot-label-font finger) (- xpos (* size 0.2)) X)\r
+ (- ypos (* 1 dot-radius size)) Y)\r
+; These lines are a better way to do it, but they require ly:stencil-get-extent, which is missing from 2.2.0\r
+; (centered-text-stencil dot-label-font finger) xpos X)\r
+; ypos Y)\r
+ ;UGH -- Constants in xpos and ypos need to go. xpos should be related to font mag,\r
+ ; ypos should be related to both font magnification and dot-position. Again, I may want to wait for the nice font with\r
+ ; convenient zero point (right at the center of the dot). \r
+ ;TODO -- Alternatively, perhaps I should query for the extent of the glyph,\r
+ ; and place it accordingly. That actually sounds better\r
+ (ly:stencil-translate-axis\r
+ (ly:stencil-translate-axis \r
+ (fontify-text dot-circle-font "O") (- xpos (* 0.6 dot-circle-font-mag)) X)\r
+ (- ypos (* 0.5 dot-circle-font-mag)) Y))\r
+; These lines are a better way to do it, but they require ly:stencil-get-exten, which is missing from 2.2.0\r
+; (centered-text-stencil dot-circle-font "O") xpos X)\r
+; ypos Y))\r
+ (if (eq? finger-code 'below-string) \r
+ (ly:stencil-add \r
+ (ly:make-stencil (list 'dot xpos ypos dot-radius ) extent extent)\r
+ (ly:stencil-translate-axis \r
+ (ly:stencil-translate-axis \r
+ (fontify-text string-label-font finger) (+ xpos (* size finger-xoffset)) X)\r
+ (* size finger-yoffset) Y)))))))\r
+ (if (null? restlist)\r
+ dotstencil\r
+ (ly:stencil-add (draw-dots paper props string-count fret-range size dot-position restlist)\r
+ dotstencil))))\r
+\r
+(define (draw-xo paper props string-count fret-range size xo-list) \r
+"Put open and mute string indications on diagram, as contained in @var{xo-list}."\r
+ (let* ((fret-count (+ (- (cadr fret-range) (car fret-range) 1)))\r
+ (xo-font-mag (* size (chain-assoc-get 'xo-font-magnification props 0.5)))\r
+ (xo-font-name (chain-assoc-get 'xo-font-name props "cmss8"))\r
+ (xo-horizontal-offset (* size (chain-assoc-get 'xo-horizontal-offset props -0.35)))\r
+ (font (ly:paper-get-font paper `(((font-magnification . ,xo-font-mag)\r
+ (font-name . ,xo-font-name)))))\r
+ (mypair (car xo-list))\r
+ (restlist (cdr xo-list))\r
+ (glyph-string (if (eq? (car mypair) 'mute) "X" "O"))\r
+ (xpos (+ (* (- string-count (cadr mypair)) size) xo-horizontal-offset ))\r
+ (glyph-stencil (ly:stencil-translate-axis (fontify-text font glyph-string) xpos X)))\r
+ (if (null? restlist)\r
+ glyph-stencil\r
+ (ly:stencil-add\r
+ (draw-xo paper props string-count fret-range size restlist)\r
+ glyph-stencil))))\r
+\r
+(define (make-bezier-sandwich-list left right bottom height thickness)\r
+" Make the argument list for a horizontal bezier sandwich from @var{left} to @var{right} with a bottom at @var{bottom}, \r
+ a height of @var{height}, and a thickness of @var{thickness}."\r
+ (let* ((width (+ (- right left) 1))\r
+ (x1 (+ (* width thickness) left))\r
+ (x2 (- right (* width thickness)))\r
+ (bottom-control-point-height (+ bottom (- height thickness)))\r
+ (top-control-point-height (+ bottom height)))\r
+ ; order of points is: left cp low, right cp low, right end low, left end low\r
+ ; right cp high, left cp high, left end high, right end high.\r
+ (list (cons x1 bottom-control-point-height) (cons x2 bottom-control-point-height) (cons right bottom) (cons left bottom)\r
+ (cons x2 top-control-point-height) (cons x1 top-control-point-height) (cons left bottom) (cons right bottom))))\r
+\r
+(define (draw-barre paper props string-count fret-range size dot-position barre-list)\r
+ "Create barre indications for a fret diagram"\r
+ (if (not (null? barre-list))\r
+ (let* ((string1 (caar barre-list))\r
+ (string2 (cadar barre-list))\r
+ (fret (caddar barre-list))\r
+ ; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant\r
+ (bottom (* size (- (+ 2 (- (cadr fret-range) fret)) dot-position)))\r
+ (left (* size (- string-count string1)))\r
+ (right (* size (- string-count string2)))\r
+ (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))\r
+ (bezier-height (chain-assoc-get 'bezier-height props 0.5))\r
+ (bezier-list (make-bezier-sandwich-list left right bottom (* size bezier-height) (* size bezier-thick)))\r
+ (sandwich-stencil (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick) )\r
+ (cons 0 right)\r
+ (cons 0 (+ bottom (* size bezier-height))))))\r
+ (if (not (null? (cdr barre-list)))\r
+ (ly:stencil-add sandwich-stencil\r
+ (draw-barre paper props string-count fret-range size dot-position (cdr barre-list)))\r
+ sandwich-stencil ))))\r
+ \r
+(define (label-fret paper props string-count fret-range size)\r
+ "Label the base fret on a fret diagram"\r
+ (let* ((base-fret (car fret-range))\r
+ (label-font-mag (chain-assoc-get 'fret-label-font-magnification props 0.8))\r
+ (label-horizontal-offset (chain-assoc-get 'fret-label-horizontal-offset props -0.5))\r
+ (label-vertical-offset (chain-assoc-get 'fret-label-vertical-offset props -0.2))\r
+ (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))\r
+ (font (ly:paper-get-font paper `(((font-magnification . ,(* label-font-mag size))(font-name . "cmss8")\r
+ (font-encoding Tex-text))))))\r
+ (ly:stencil-translate-axis \r
+ (ly:stencil-translate-axis (fontify-text font (if (> base-fret 1)\r
+ (format #f "~(~:@r~)" base-fret)\r
+ " ")) (* size (+ string-count label-horizontal-offset)) X)\r
+ (* size (+ fret-count label-vertical-offset)) Y)))\r
+ \r
+(def-markup-command (fret-diagram-verbose paper props marking-list)\r
+ (list?)\r
+ "Make a fret diagram containing the symbols indicated in @var{marking-list}\r
+ Syntax: \\fret-diagram marking-list\r
+ For example,\r
+ @verbatim\r
+ \\markup \\fret-diagram #\'((mute 6) (mute 5) (open 4) (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))\r
+ @end verbatim \r
+ will produce a standard D chord diagram without fingering indications.\r
+ Possible elements in @var{marking-list}:\r
+ (mute string-number) -- place a small 'x' at the top of string @var{string-number}\r
+ (open string-number) -- place a small 'o' at the top of string @var{string-number}\r
+ (barre start-string end-string fret-number) -- place a barre indicator (much like a tie) from string @var{start-string}\r
+ to string @var{end-string} at fret @var{fret-number}\r
+ (place-fret string-number fret-number finger-value) -- place a fret playing indication\r
+ on string @var{string-number} at fret @var{fret-number} with an optional fingering label @var{finger-value}\r
+ By default, the fret playing indicator is a solid dot. If the @var{finger} part of the place-fret element is present,\r
+ @var{finger-value} will be displayed according to the setting of the variable @var{finger-code}\r
+ There is no limit to the number of fret indications per string."\r
+ (make-fret-diagram paper props marking-list))\r
+ \r
+(define (make-fret-diagram paper props marking-list)\r
+" Make a fret diagram markup" \r
+ (let* (\r
+ ; note: here we get items from props that are needed in this routine, or that are needed in more than one\r
+ ; of the procedures called from this routine. If they're only used in one of the sub-procedure, they're \r
+ ; obtained in that procedure\r
+ \r
+ (size (chain-assoc-get 'size props 1.0)) ; needed for everything\r
+;TODO -- get string-count directly from length of stringTunings; requires FretDiagram engraver, which is not yet available\r
+ (string-count (chain-assoc-get 'string-count props 6)) ; needed for everything\r
+ (fret-count (chain-assoc-get 'fret-count props 4)) ; needed for everything\r
+ (dot-position (chain-assoc-get 'dot-position props 0.6)) ; needed for both draw-dots and draw-barre\r
+ (th (* (ly:paper-lookup paper 'linethickness)\r
+ (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings\r
+ \r
+ (alignment (chain-assoc-get 'alignment props -0.4)) ; needed only here\r
+ (xo-padding (* th (chain-assoc-get 'xo-padding props 2))) ; needed only here\r
+\r
+ (parameters (fret-parse-marking-list marking-list fret-count))\r
+ (dot-list (cdr (assoc 'dot-list parameters)))\r
+ (xo-list (cdr (assoc 'xo-list parameters)))\r
+ (fret-range (cdr (assoc 'fret-range parameters)))\r
+ (barre-list (cdr (assoc 'barre-list parameters)))\r
+ (fret-diagram-stencil (ly:stencil-add\r
+ (draw-strings string-count fret-range th size)\r
+ (draw-frets paper props fret-range string-count th size))))\r
+ (if (not (null? dot-list))\r
+ (set! fret-diagram-stencil (ly:stencil-add\r
+ (draw-dots paper props string-count fret-range size dot-position dot-list)\r
+ fret-diagram-stencil)))\r
+ (if (not (null? xo-list))\r
+ (set! fret-diagram-stencil (ly:stencil-combine-at-edge\r
+ fret-diagram-stencil Y UP\r
+ (draw-xo paper props string-count fret-range size xo-list) xo-padding 0)))\r
+ (if (not (null? barre-list))\r
+ (set! fret-diagram-stencil (ly:stencil-add\r
+ (draw-barre paper props string-count fret-range size dot-position barre-list)\r
+ fret-diagram-stencil)))\r
+ (set! fret-diagram-stencil (ly:stencil-add fret-diagram-stencil (label-fret paper props string-count fret-range size)))\r
+ (ly:stencil-align-to! fret-diagram-stencil X alignment)\r
+ fret-diagram-stencil))\r
+ \r
+(def-markup-command (fret-diagram paper props size definition-string)\r
+ (number? string?)\r
+ "Syntax: \\fret-diagram size definition-string\r
+ eg: \\markup \\fret-diagram #0.75 #\"6-x;5-x;4-o;3-2;2-3;1-2;\"\r
+ for fret spacing 3/4 of staff space, D chord diagram\r
+ Syntax rules for @var{definition-string}:\r
+ Diagram items are separated by semicolons.\r
+ Possible items:\r
+ t:number -- set the line thickness (in staff spaces). Default 0.05\r
+ h:number -- set the height of the diagram in frets. Default 4\r
+ w:number -- set the width of the diagram in strings. Default 6\r
+ f:number -- set fingering label type \r
+ (0 = none, 1 = in circle on string, 2 = below string) Default 0\r
+ d:number -- set radius of dot, in terms of fret spacing. Default 0.25\r
+ p:number -- set the position of the dot in the fret space. 0.5 is centered; 1 is on lower fret bar,\r
+ 0 is on upper fret bar. Default 0.6 \r
+ c:string1-string2-fret -- include a barre mark from string1 to string2 on fret\r
+ string-fret -- place a dot on string at fret. If fret is o, string is identified\r
+ as open. If fret is x, string is identified as muted.\r
+ string-fret-fingering -- place a dot on string at fret, and label with fingering as \r
+ defined by f: code.\r
+ Note: There is no limit to the number of fret indications per string."\r
+ (set! props (acons 'size size props))\r
+ (let ((definition-list (parse-definition-string props definition-string)))\r
+ (make-fret-diagram paper (car definition-list) (cdr definition-list))))\r
+\r
+(define (parse-definition-string props definition-string)\r
+ "parse a fret diagram string and return a pair containing:\r
+ props, modified as necessary by the definition-string\r
+ a fret-indication list with the appropriate values"\r
+ (let* ((fret-count 4)\r
+ (string-count 6)\r
+ (thickness 0.05)\r
+ (finger-code 0)\r
+ (dot-size 0.25)\r
+ (dot-position 0.6)\r
+ (fret-range (list 1 fret-count))\r
+ (barre-list '())\r
+ (dot-list '())\r
+ (xo-list '())\r
+ (output-list '())\r
+ (items (string-split definition-string #\;)))\r
+ (let parse-item ((myitems items))\r
+ (if (not (null? (cdr myitems))) \r
+ (let ((test-string (car myitems)))\r
+ (case (car (string->list (substring test-string 0 1))) \r
+ ((#\f) (let* ((finger-code (get-numeric-from-key test-string))\r
+ (finger-id (case finger-code\r
+ ((0) 'none)\r
+ ((1) 'white-circled) \r
+ ((2) 'below-string))))\r
+ (set! props\r
+ (acons 'finger-code finger-id props))))\r
+ ((#\t) (let ((thickness (get-numeric-from-key test-string)))\r
+ (set! props (acons 'thickness thickness props))))\r
+ ((#\c) (set! output-list (cons-fret (cons 'barre (numerify (string-split (substring test-string 2) #\-)))\r
+ output-list)))\r
+ ((#\h) (let ((fret-count (get-numeric-from-key test-string)))\r
+ (set! props (acons 'fret-count fret-count props))))\r
+ ((#\w) (let ((string-count (get-numeric-from-key test-string)))\r
+ (set! props (acons 'string-count string-count props))))\r
+ ((#\d) (let ((dot-size (get-numeric-from-key test-string)))\r
+ (set! props (acons 'dot-radius dot-size props))))\r
+ ((#\p) (let ((dot-position (get-numeric-from-key test-string)))\r
+ (set! props (acons 'dot-position dot-position props))))\r
+ (else \r
+ (let ((this-list (string-split test-string #\-)))\r
+ (if (string->number (cadr this-list))\r
+ (set! output-list (cons-fret (cons 'place-fret (numerify this-list)) output-list))\r
+ (if (equal? (cadr this-list) "x" )\r
+ (set! output-list (cons-fret (list 'mute (string->number (car this-list))) output-list))\r
+ (set! output-list (cons-fret (list 'open (string->number (car this-list))) output-list)))))))\r
+ (parse-item (cdr myitems)))))\r
+ `((,props) . ,output-list)))\r
+\r
+(define (cons-fret new-value old-list)\r
+" Put together a fret-list in the format desired by parse-string "\r
+ (if (eq? old-list '())\r
+ (list new-value)\r
+ (cons* new-value old-list)))\r
+ \r
+(define (get-numeric-from-key keystring)\r
+ "Get the numeric value from a key of the form k:val"\r
+ (string->number (substring keystring 2 (string-length keystring) )))\r
+ \r
+(define (numerify mylist)\r
+ "Convert string values to numeric or character"\r
+ (if (null? mylist)\r
+ '()\r
+ (let ((numeric-value (string->number (car mylist))))\r
+ (if numeric-value\r
+ (cons* numeric-value (numerify (cdr mylist)))\r
+ (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))\r
+ \r