1 ;;;; fret-diagrams.scm --
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2008 Carl D. Sorensen <c_sorensen@byu.edu>
7 (define (fret-parse-marking-list marking-list fret-count)
8 (let* ((fret-range (list 1 fret-count))
13 (let parse-item ((mylist marking-list))
14 (if (not (null? mylist))
15 (let* ((my-item (car mylist)) (my-code (car my-item)))
17 ((or (eq? my-code 'open)(eq? my-code 'mute))
18 (set! xo-list (cons* my-item xo-list)))
20 (set! barre-list (cons* (cdr my-item) barre-list)))
21 ((eq? my-code 'place-fret)
22 (set! dot-list (cons* (cdr my-item) dot-list))))
23 (parse-item (cdr mylist)))))
24 ;; calculate fret-range
25 (let ((maxfret 0) (minfret 99))
26 (let updatemax ((fret-list dot-list))
29 (let ((fretval (second (car fret-list))))
30 (if (> fretval maxfret) (set! maxfret fretval))
31 (if (< fretval minfret) (set! minfret fretval))
32 (updatemax (cdr fret-list)))))
33 (if (> maxfret fret-count)
36 (let ((upfret (- (+ minfret fret-count) 1)))
37 (if (> maxfret upfret) maxfret upfret)))))
38 ; subtract fret from dots
39 (set! dot-list (subtract-base-fret (- (car fret-range) 1) dot-list)))
40 (acons 'fret-range fret-range
41 (acons 'barre-list barre-list
42 (acons 'dot-list dot-list
43 (acons 'xo-list xo-list '()))))))
45 (define (subtract-base-fret base-fret dot-list)
46 "Subtract @var{base-fret} from every fret in @var{dot-list}"
49 (let ((this-list (car dot-list)))
50 (cons* (list (car this-list) (- (second this-list) base-fret)
51 (if (null? (cddr this-list))
54 (subtract-base-fret base-fret (cdr dot-list))))))
56 (define (sans-serif-stencil layout props mag text)
57 "Create a stencil in sans-serif font based on @var{layout} and @var{props}
58 with magnification @var{mag} of the string @var{text}."
61 'font-size (stepmag mag)
62 (prepend-alist-chain 'font-family 'sans props))))
63 (interpret-markup layout my-props text)))
65 (define (draw-strings string-count fret-range th size orientation)
66 "Draw the string lines for a fret diagram with
67 @var{string-count} strings and frets as indicated in @var{fret-range}.
68 Line thickness is given by @var{th}, fret & string spacing by
69 @var{size}. Orientation is determined by @var{orientation}. "
70 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
71 (sl (* (+ fret-count 1) size))
75 (if (eq? orientation 'normal)
76 (make-line-stencil sth 0 0 0 sl)
77 (make-line-stencil sth 0 0 sl 0))))
78 (if (= string-count 1)
80 (if (eq? orientation 'normal)
81 (ly:stencil-combine-at-edge
82 (draw-strings (- string-count 1) fret-range th size orientation)
86 (ly:stencil-combine-at-edge
87 (draw-strings (- string-count 1) fret-range th size orientation)
92 (define (draw-fret-lines fret-count string-count th size orientation)
93 "Draw @var{fret-count} fret lines for a fret diagram
94 with @var{string-count} strings. Line thickness is given by @var{th},
95 fret & string spacing by @var{size}. Orientation is given by @var{orientation}"
96 (let* ((sth (* size th))
98 (fret-line (draw-fret-line string-count th size orientation)))
101 (if (eq? orientation 'normal)
102 (ly:stencil-combine-at-edge
104 (- fret-count 1) string-count th size orientation)
108 (ly:stencil-combine-at-edge
110 (- fret-count 1) string-count th size orientation)
115 (define (draw-fret-line string-count th size orientation)
116 "Draw a fret line for a fret diagram."
117 (let* ((fret-length (* (- string-count 1) size))
119 (half-thickness (* sth 0.5)))
120 (if (eq? orientation 'normal)
121 (make-line-stencil sth half-thickness size
122 (- fret-length half-thickness) size)
123 (make-line-stencil sth 0 half-thickness
124 0 (- fret-length half-thickness)))))
126 (define (draw-thick-zero-fret details string-count th size orientation)
127 "Draw a thick zeroth fret for a fret diagram whose base fret is not 1."
128 (let* ((sth (* th size))
130 (* sth (assoc-get 'top-fret-thickness details 3.0)))
131 (half-thick (* sth 0.5))
133 (x2 (+ half-thick (* size (- string-count 1))))
135 (y2 (+ top-fret-thick half-thick))
136 (x-extent (cons (- x1) x2))
137 (y-extent (cons sth top-fret-thick)))
138 (if (eq? orientation 'normal)
139 (ly:make-stencil (list 'round-filled-box x1 x2 y1 y2 sth)
141 (ly:make-stencil (list 'round-filled-box y1 y2 x1 x2 sth)
142 y-extent x-extent))))
144 (define (draw-frets fret-range string-count th size orientation)
145 "Draw the fret lines for a fret diagram with
146 @var{string-count} strings and frets as indicated in @var{fret-range}.
147 Line thickness is given by @var{th}, fret & string spacing by
148 @var{size}. Orientation is given by @var{orientation}."
149 (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
150 (fret-length (* (- string-count 1) size))
151 (half-thickness (* th 0.5))
152 (base-fret (car fret-range))
153 (fret-zero (draw-fret-line string-count th size orientation)))
154 (if (eq? orientation 'normal)
155 (ly:stencil-combine-at-edge
156 (draw-fret-lines fret-count string-count th size orientation)
160 (ly:stencil-combine-at-edge
162 (draw-fret-lines fret-count string-count th size orientation)
165 (define (draw-dots layout props string-count fret-count
166 fret-range size finger-code
167 dot-position dot-radius dot-thickness dot-list orientation)
168 "Make dots for fret diagram."
170 (let* ((details (chain-assoc-get 'fret-diagram-details props '()))
171 (scale-dot-radius (* size dot-radius))
172 (scale-dot-thick (* size dot-thickness))
173 (dot-color (assoc-get 'dot-color details 'black))
174 (finger-xoffset -0.25)
175 (finger-yoffset (* -0.5 size ))
177 (* scale-dot-radius (assoc-get 'dot-label-font-mag details 1.0)))
178 (string-label-font-mag
179 (* size (assoc-get 'string-label-font-mag details 0.6)))
180 (mypair (car dot-list))
181 (restlist (cdr dot-list))
182 (string (car mypair))
184 (xpos (* size (if (eq? orientation 'normal)
185 (- string-count string)
186 (+ (- fret 1 ) dot-position))))
187 (ypos (* size (if (eq? orientation 'normal)
188 (+ 2 (- fret-count fret dot-position ))
189 (- string-count string))))
190 (extent (cons (- scale-dot-radius) scale-dot-radius))
191 (finger (caddr mypair))
192 (finger (if (number? finger) (number->string finger) finger))
193 (dotstencil (if (eq? dot-color 'white)
196 scale-dot-radius scale-dot-thick #t)
199 (- scale-dot-radius (* 0.5 scale-dot-thick))
203 scale-dot-radius scale-dot-thick #t)))
204 (positioned-dot (begin
205 (ly:stencil-translate-axis
206 (ly:stencil-translate-axis dotstencil xpos X)
209 (if (or (eq? finger '())(eq? finger-code 'none))
211 (if (eq? finger-code 'in-dot)
215 layout props dot-label-font-mag finger))))
216 (ly:stencil-translate-axis
217 (ly:stencil-translate-axis
220 (if (eq? dot-color 'white)
222 (ly:stencil-in-color finger-label 1 1 1)))
225 (if (eq? finger-code 'below-string)
228 (if (eq? orientation 'normal)
229 (ly:stencil-translate-axis
230 (ly:stencil-translate-axis
233 layout props string-label-font-mag finger))
235 (* size finger-yoffset) Y)
236 (ly:stencil-translate-axis
237 (ly:stencil-translate-axis
240 layout props string-label-font-mag finger))
241 (* size (+ 2 fret-count finger-yoffset)) X)
249 layout props string-count fret-count fret-range size finger-code
250 dot-position dot-radius dot-thickness restlist orientation)
251 labeled-dot-stencil))))
253 (define (draw-xo layout props string-count fret-range size xo-list orientation)
254 "Put open and mute string indications on diagram, as contained in
256 (let* ((details (chain-assoc-get 'fret-diagram-details props '()))
257 (fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
259 (* size (assoc-get 'xo-font-magnification details 0.5)))
260 (xo-horizontal-offset (* size -0.35))
261 (mypair (car xo-list))
262 (restlist (cdr xo-list))
263 (glyph-string (if (eq? (car mypair) 'mute)
264 (assoc-get 'mute-string details "X")
265 (assoc-get 'open-string details "O")))
267 (+ (* (- string-count (cadr mypair)) size) xo-horizontal-offset ))
268 (glyph-stencil (if (eq? orientation 'normal)
269 (ly:stencil-translate-axis
271 layout props (* size xo-font-mag) glyph-string)
273 (ly:stencil-translate-axis
275 layout props (* size xo-font-mag) glyph-string)
281 layout props string-count fret-range size restlist orientation)
284 (define (make-bezier-sandwich-list start stop base height thickness orientation)
285 "Make the argument list for a bezier sandwich from
286 @var{start} to @var{stop} with a baseline at @var{base}, a height of
287 @var{height}, and a thickness of @var{thickness}. If @var{orientation} is
288 @var{'normal}, @var{base} is a y coordinate, otherwise it's an x coordinate."
289 (let* ((width (+ (- stop start) 1))
290 (x1 (+ (* width thickness) start))
291 (x2 (- stop (* width thickness)))
292 (bottom-control-point-height
293 (if (eq? orientation 'normal)
294 (+ base (- height thickness))
295 (- base (- height thickness))))
296 (top-control-point-height
297 (if (eq? orientation 'normal)
300 ; order of bezier control points is:
301 ; left cp low, right cp low, right end low, left end low
302 ; right cp high, left cp high, left end high, right end high.
303 (if (eq? orientation 'normal)
304 (list (cons x1 bottom-control-point-height)
305 (cons x2 bottom-control-point-height)
308 (cons x2 top-control-point-height)
309 (cons x1 top-control-point-height)
312 (list (cons bottom-control-point-height x1)
313 (cons bottom-control-point-height x2)
316 (cons top-control-point-height x2)
317 (cons top-control-point-height x1)
321 (define (draw-barre layout props string-count fret-range
322 size finger-code dot-position dot-radius
323 barre-list orientation)
324 "Create barre indications for a fret diagram"
325 (if (not (null? barre-list))
326 (let* ((details (chain-assoc-get 'fret-diagram-details props '()))
327 (string1 (caar barre-list))
328 (string2 (cadar barre-list))
329 (fret (caddar barre-list))
330 (top-fret (cadr fret-range))
331 (low-fret (car fret-range))
332 (barre-type (assoc-get 'barre-type details 'curved))
333 (scale-dot-radius (* size dot-radius))
334 (barre-vertical-offset 0.5)
335 ;; 2 is 1 for empty fret at bottom of figure + 1 for interval
336 ;; (top-fret - fret + 1) -- not an arbitrary constant
338 (* size (- (+ 2 (- (cadr fret-range) fret)) dot-position)))
339 (dot-center-fret-coordinate (+ (- fret low-fret) dot-position))
340 (barre-fret-coordinate
341 (+ dot-center-fret-coordinate
342 (* (- barre-vertical-offset 0.5) dot-radius)))
343 (barre-start-string-coordinate (- string-count string1))
344 (barre-end-string-coordinate (- string-count string2))
346 (+ dot-center-y (* barre-vertical-offset scale-dot-radius)))
347 (left (* size (- string-count string1)))
348 (right (* size (- string-count string2)))
352 (if (eq? orientation 'normal)
353 (make-bezier-sandwich-list
354 (* size barre-start-string-coordinate)
355 (* size barre-end-string-coordinate)
356 (* size (+ 2 (- top-fret
357 (+ low-fret barre-fret-coordinate))))
358 (* size bezier-height)
359 (* size bezier-thick)
361 (make-bezier-sandwich-list
362 (* size barre-start-string-coordinate)
363 (* size barre-end-string-coordinate)
364 (* size barre-fret-coordinate)
365 (* size bezier-height)
366 (* size bezier-thick)
369 (if (eq? barre-type 'straight)
370 (if (eq? orientation 'normal)
371 (make-line-stencil scale-dot-radius left dot-center-y
373 (make-line-stencil scale-dot-radius
374 (* size barre-fret-coordinate)
375 (* size barre-start-string-coordinate)
376 (* size barre-fret-coordinate)
377 (* size barre-end-string-coordinate)))
378 (if (eq? orientation 'normal)
380 (list 'bezier-sandwich
381 `(quote ,bezier-list)
382 (* size bezier-thick))
384 (cons bottom (+ bottom (* size bezier-height))))
386 (list 'bezier-sandwich
387 `(quote ,bezier-list)
388 (* size bezier-thick))
389 (cons bottom (+ bottom (* size bezier-height)))
390 (cons left right))))))
391 (if (not (null? (cdr barre-list)))
394 (draw-barre layout props string-count fret-range size finger-code
395 dot-position dot-radius (cdr barre-list)))
398 (define (stepmag mag)
399 "Calculate the font step necessary to get a desired magnification"
400 (* 6 (/ (log mag) (log 2))))
402 (define (label-fret layout props string-count fret-range size orientation)
403 "Label the base fret on a fret diagram"
404 (let* ((details (chain-assoc-get 'fret-diagram-details props '()))
405 (base-fret (car fret-range))
406 (label-font-mag (assoc-get 'fret-label-font-mag details 0.5))
407 (label-vertical-offset
408 (assoc-get 'fret-label-vertical-offset details -0.2))
409 (number-type (assoc-get 'number-type details 'roman-lower))
410 (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
413 ((equal? number-type 'roman-lower)
414 (fancy-format #f "~(~@r~)" base-fret))
415 ((equal? number-type 'roman-upper)
416 (fancy-format #f "~@r" base-fret))
417 ((equal? 'arabic number-type)
418 (fancy-format #f "~d" base-fret))
419 (else (fancy-format #f "~(~@r~)" base-fret)))))
420 (if (eq? orientation 'normal)
421 (ly:stencil-translate-axis
422 (sans-serif-stencil layout props (* size label-font-mag) label-text)
423 (* size (+ fret-count label-vertical-offset)) Y)
424 (ly:stencil-translate-axis
425 (sans-serif-stencil layout props (* size label-font-mag) label-text)
426 (* size (+ 1 label-vertical-offset)) X))))
428 (define-builtin-markup-command (fret-diagram-verbose layout props marking-list)
429 (list?) ; argument type
430 fret-diagram ; markup type
431 ((align-dir -0.4) ; properties and defaults
433 (fret-diagram-details)
435 "Make a fret diagram containing the symbols indicated in @var{marking-list}.
440 \\markup \\fret-diagram-verbose
441 #'((mute 6) (mute 5) (open 4)
442 (place-fret 3 2) (place-fret 2 3) (place-fret 1 2))
446 produces a standard D@tie{}chord diagram without fingering indications.
448 Possible elements in @var{marking-list}:
451 @item (mute @var{string-number})
452 Place a small @q{x} at the top of string @var{string-number}.
454 @item (open @var{string-number})
455 Place a small @q{o} at the top of string @var{string-number}.
457 @item (barre @var{start-string} @var{end-string} @var{fret-number})
458 Place a barre indicator (much like a tie) from string @var{start-string}
459 to string @var{end-string} at fret @var{fret-number}.
461 @item (place-fret @var{string-number} @var{fret-number} @var{finger-value})
462 Place a fret playing indication on string @var{string-number} at fret
463 @var{fret-number} with an optional fingering label @var{finger-value}.
464 By default, the fret playing indicator is a solid dot. This can be
465 changed by setting the value of the variable @var{dot-color}. If the
466 @var{finger} part of the @code{place-fret} element is present,
467 @var{finger-value} will be displayed according to the setting of the
468 variable @var{finger-code}. There is no limit to the number of fret
469 indications per string.
472 (make-fret-diagram layout props marking-list))
474 (define (make-fret-diagram layout props marking-list)
475 "Make a fret diagram markup"
477 ; note: here we get items from props that are needed in this routine,
478 ; or that are needed in more than one of the procedures
479 ; called from this routine. If they're only used in one of the
480 ; sub-procedure, they're obtained in that procedure
481 (size (chain-assoc-get 'size props 1.0)) ; needed for everything
482 ;TODO -- get string-count directly from length of stringTunings;
483 ; from FretBoard engraver, but not from markup call
484 ;TODO -- adjust padding for fret label? it appears to be too close to dots
487 'fret-diagram-details props '())) ; fret diagram details
489 (assoc-get 'string-count details 6)) ; needed for everything
491 (assoc-get 'fret-count details 4)) ; needed for everything
493 (assoc-get 'orientation details 'normal)) ; needed for everything
496 'finger-code details 'none)) ; needed for draw-dots and draw-barre
498 (if (eq? finger-code 'in-dot) 0.425 0.25)) ; bigger dots if labeled
499 (default-dot-position
500 (if (eq? finger-code 'in-dot)
501 (- 0.95 default-dot-radius)
502 0.6)) ; move up to make room for bigger if labeled
505 'dot-radius details default-dot-radius)) ; needed for draw-dots
509 'dot-position details default-dot-position)) ; needed for draw-dots
512 (* (ly:output-def-lookup layout 'line-thickness)
513 (chain-assoc-get 'thickness props 0.5))) ; needed for draw-frets
516 (chain-assoc-get 'align-dir props -0.4)) ; needed only here
518 (* size (assoc-get 'xo-padding details 0.2))) ; needed only here
519 (label-space (* 0.25 size))
520 (label-dir (assoc-get 'label-dir details RIGHT))
521 (parameters (fret-parse-marking-list marking-list fret-count))
522 (dot-list (cdr (assoc 'dot-list parameters)))
523 (xo-list (cdr (assoc 'xo-list parameters)))
524 (fret-range (cdr (assoc 'fret-range parameters)))
525 (barre-list (cdr (assoc 'barre-list parameters)))
527 (assoc-get 'barre-type details 'curved))
528 (fret-diagram-stencil
530 (draw-strings string-count fret-range th size orientation)
531 (draw-frets fret-range string-count th size orientation))))
532 (if (and (not (null? barre-list))
533 (not (eq? 'none barre-type)))
534 (set! fret-diagram-stencil
536 (draw-barre layout props string-count fret-range size
537 finger-code dot-position dot-radius
538 barre-list orientation)
539 fret-diagram-stencil)))
540 (if (not (null? dot-list))
541 (set! fret-diagram-stencil
544 (draw-dots layout props string-count fret-count fret-range
545 size finger-code dot-position dot-radius
546 th dot-list orientation))))
547 (if (= (car fret-range) 1)
548 (set! fret-diagram-stencil
549 (if (eq? orientation 'normal)
550 (ly:stencil-combine-at-edge
551 fret-diagram-stencil Y UP
552 (draw-thick-zero-fret
553 props string-count th size orientation))
554 (ly:stencil-combine-at-edge
555 fret-diagram-stencil X LEFT
556 (draw-thick-zero-fret
557 props string-count th size orientation)))))
558 (if (not (null? xo-list))
559 (set! fret-diagram-stencil
560 (if (eq? orientation 'normal)
561 (ly:stencil-combine-at-edge
562 fret-diagram-stencil Y UP
563 (draw-xo layout props string-count fret-range
564 size xo-list orientation)
566 (ly:stencil-combine-at-edge
567 fret-diagram-stencil X LEFT
568 (draw-xo layout props string-count fret-range
569 size xo-list orientation)
571 (if (> (car fret-range) 1)
572 (set! fret-diagram-stencil
573 (if (eq? orientation 'normal)
574 (ly:stencil-combine-at-edge
575 fret-diagram-stencil X label-dir
576 (label-fret layout props string-count fret-range
579 (ly:stencil-combine-at-edge
580 fret-diagram-stencil Y label-dir
581 (label-fret layout props string-count fret-range
584 (ly:stencil-aligned-to fret-diagram-stencil X alignment)))
586 (define-builtin-markup-command (fret-diagram layout props definition-string)
587 (string?) ; argument type
588 fret-diagram ; markup category
589 (fret-diagram-verbose-markup) ; properties and defaults
590 "Make a (guitar) fret diagram. For example, say
593 \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
597 for fret spacing 3/4 of staff space, D chord diagram
599 Syntax rules for @var{definition-string}:
603 Diagram items are separated by semicolons.
610 @code{s:}@var{number} -- Set the fret spacing of the diagram (in staff
615 @code{t:}@var{number} -- Set the line thickness (in staff spaces).
619 @code{h:}@var{number} -- Set the height of the diagram in frets.
623 @code{w:}@var{number} -- Set the width of the diagram in strings.
627 @code{f:}@var{number} -- Set fingering label type
628 (0@tie{}= none, 1@tie{}= in circle on string, 2@tie{}= below string).
632 @code{d:}@var{number} -- Set radius of dot, in terms of fret spacing.
636 @code{p:}@var{number} -- Set the position of the dot in the fret space.
637 0.5 is centered; 1@tie{}is on lower fret bar, 0@tie{}is on upper fret bar.
641 @code{c:}@var{string1}@code{-}@var{string2}@code{-}@var{fret} -- Include a
642 barre mark from @var{string1} to @var{string2} on @var{fret}.
645 @var{string}@code{-}@var{fret} -- Place a dot on @var{string} at @var{fret}.
646 If @var{fret} is @samp{o}, @var{string} is identified as open.
647 If @var{fret} is @samp{x}, @var{string} is identified as muted.
650 @var{string}@code{-}@var{fret}@code{-}@var{fingering} -- Place a dot on
651 @var{string} at @var{fret}, and label with @var{fingering} as defined
652 by the @code{f:} code.
656 Note: There is no limit to the number of fret indications per string.
658 (let ((definition-list
659 (fret-parse-definition-string props definition-string)))
660 (fret-diagram-verbose-markup
661 layout (car definition-list) (cdr definition-list))))
663 (define (fret-parse-definition-string props definition-string)
664 "Parse a fret diagram string and return a pair containing:
665 props, modified as necessary by the definition-string
666 a fret-indication list with the appropriate values"
667 (let* ((fret-count 4)
669 (fret-range (list 1 fret-count))
675 (details (merge-details 'fret-diagram-details props '()))
676 (items (string-split definition-string #\;)))
677 (let parse-item ((myitems items))
678 (if (not (null? (cdr myitems)))
679 (let ((test-string (car myitems)))
680 (case (car (string->list (substring test-string 0 1)))
681 ((#\s) (let ((size (get-numeric-from-key test-string)))
682 (set! props (prepend-alist-chain 'size size props))))
683 ((#\f) (let* ((finger-code (get-numeric-from-key test-string))
684 (finger-id (case finger-code
687 ((2) 'below-string))))
689 (acons 'finger-code finger-id details))))
690 ((#\c) (set! output-list
695 (string-split (substring test-string 2) #\-)))
697 ((#\h) (let ((fret-count (get-numeric-from-key test-string)))
699 (acons 'fret-count fret-count details))))
700 ((#\w) (let ((string-count (get-numeric-from-key test-string)))
702 (acons 'string-count string-count details))))
703 ((#\d) (let ((dot-size (get-numeric-from-key test-string)))
705 (acons 'dot-radius dot-size details))))
706 ((#\p) (let ((dot-position (get-numeric-from-key test-string)))
708 (acons 'dot-position dot-position details))))
710 (let ((this-list (string-split test-string #\-)))
711 (if (string->number (cadr this-list))
714 (cons 'place-fret (numerify this-list))
716 (if (equal? (cadr this-list) "x" )
719 (list 'mute (string->number (car this-list)))
723 (list 'open (string->number (car this-list)))
725 (parse-item (cdr myitems)))))
726 ; add the modified details
728 (prepend-alist-chain 'fret-diagram-details details props))
729 `(,props . ,output-list))) ;ugh -- hard-coded spell -- procedure better
731 (define (cons-fret new-value old-list)
732 "Put together a fret-list in the format desired by parse-string"
733 (if (eq? old-list '())
735 (cons* new-value old-list)))
737 (define (get-numeric-from-key keystring)
738 "Get the numeric value from a key of the form k:val"
739 (string->number (substring keystring 2 (string-length keystring))))
741 (define (numerify mylist)
742 "Convert string values to numeric or character"
745 (let ((numeric-value (string->number (car mylist))))
747 (cons* numeric-value (numerify (cdr mylist)))
748 (cons* (car (string->list (car mylist)))
749 (numerify (cdr mylist)))))))
751 (define-builtin-markup-command
752 (fret-diagram-terse layout props definition-string)
753 (string?) ; argument type
754 fret-diagram ; markup category
755 (fret-diagram-verbose-markup) ; properties
756 "Make a fret diagram markup using terse string-based syntax.
761 \\markup \\fret-diagram-terse #\"x;x;o;2;3;2;\"
765 for a D@tie{}chord diagram.
767 Syntax rules for @var{definition-string}:
772 Strings are terminated by semicolons; the number of semicolons
773 is the number of strings in the diagram.
776 Mute strings are indicated by @samp{x}.
779 Open strings are indicated by @samp{o}.
782 A number indicates a fret indication at that fret.
785 If there are multiple fret indicators desired on a string, they
786 should be separated by spaces.
789 Fingerings are given by following the fret number with a @code{-},
790 followed by the finger indicator, e.g. @samp{3-2} for playing the third
791 fret with the second finger.
794 Where a barre indicator is desired, follow the fret (or fingering) symbol
795 with @code{-(} to start a barre and @code{-)} to end the barre.
798 ;; TODO -- change syntax to fret\string-finger
799 (let ((definition-list
800 (fret-parse-terse-definition-string props definition-string)))
801 (fret-diagram-verbose-markup layout
802 (car definition-list)
803 (cdr definition-list))))
806 (fret-parse-terse-definition-string props definition-string)
807 "Parse a fret diagram string that uses terse syntax; return a pair containing:
808 props, modified to include the string-count determined by the
809 definition-string, and
810 a fret-indication list with the appropriate values"
811 ;TODO -- change syntax to fret\string-finger
813 (let* ((details (merge-details 'fret-diagram-details props '()))
814 (barre-start-list '())
817 (items (string-split definition-string #\;))
818 (string-count (- (length items) 1)))
819 (let parse-item ((myitems items))
820 (if (not (null? (cdr myitems)))
821 (let* ((test-string (car myitems))
822 (current-string (- (length myitems) 1))
823 (indicators (string-split test-string #\ )))
824 (let parse-indicators ((myindicators indicators))
825 (if (not (eq? '() myindicators))
826 (let* ((this-list (string-split (car myindicators) #\-))
827 (max-element-index (- (length this-list) 1))
829 (car (list-tail this-list max-element-index)))
831 (if (string->number (car this-list))
832 (string->number (car this-list))
834 (if (equal? last-element "(")
836 (set! barre-start-list
837 (cons-fret (list current-string fret)
840 (list-head this-list max-element-index))))
841 (if (equal? last-element ")")
843 (get-sub-list fret barre-start-list))
844 (insert-index (- (length this-barre) 1)))
846 (cons-fret (cons* 'barre
852 (list-head this-list max-element-index))))
859 (drop-paren (numerify this-list)))
861 (if (equal? (car this-list) "x" )
865 (list 'mute current-string)
870 (list 'open current-string)
872 (parse-indicators (cdr myindicators)))))
873 (parse-item (cdr myitems)))))
874 (set! details (acons 'string-count string-count details))
875 (set! props (prepend-alist-chain 'fret-diagram-details details props))
876 `(,props . ,output-list))) ; ugh -- hard coded; proc is better
878 (define (drop-paren item-list)
879 "Drop a final parentheses from a fret indication list
880 resulting from a terse string specification of barre."
881 (if (> (length item-list) 0)
882 (let* ((max-index (- (length item-list) 1))
883 (last-element (car (list-tail item-list max-index))))
884 (if (or (equal? last-element ")") (equal? last-element "("))
885 (list-head item-list max-index)
889 (define (get-sub-list value master-list)
890 "Get a sub-list whose cadr is equal to @var{value} from @var{master-list}"
891 (if (eq? master-list '())
893 (let ((sublist (car master-list)))
894 (if (equal? (cadr sublist) value)
896 (get-sub-list value (cdr master-list))))))
898 (define (merge-details key alist-list . default)
899 "Return ALIST-LIST entries for key, in one combined alist.
900 There can be two ALIST-LIST entries for a given key. The first
901 comes from the override-markup function, the second comes
902 from property settings during a regular override.
903 This is necessary because some details can be set in one
904 place, while others are set in the other. Both details
905 lists must be merged into a single alist.
906 Return DEFAULT (optional, else #f) if not
909 (define (helper key alist-list default)
910 (if (null? alist-list)
912 (let* ((handle (assoc key (car alist-list))))
914 (append (cdr handle) (chain-assoc-get key (cdr alist-list) '()))
915 (helper key (cdr alist-list) default)))))
917 (helper key alist-list
918 (if (pair? default) (car default) #f)))