]> git.donarmstrong.com Git - lilypond.git/blobdiff - scm/fret-diagrams.scm
*** empty log message ***
[lilypond.git] / scm / fret-diagrams.scm
index b577c98d2b235f33ed35d17e7a765944c5fdca72..fad5f4739eb9f8513694359b2053dc646ee63b52 100644 (file)
@@ -2,10 +2,7 @@
 ;;;;
 ;;;;  source file of the GNU LilyPond music typesetter
 ;;;; 
-;;;; (c) 2004 Carl D. Sorensen <c_sorensen@byu.edu>
-
-(define ly:paper-lookup ly:output-def-lookup) ; compat for 2.3, remove  when using 2.2
-(define my-font-encoding 'ec )
+;;;; (c) 2004--2005 Carl D. Sorensen <c_sorensen@byu.edu>
 
 (define (fret-parse-marking-list marking-list fret-count)
    (let* ((fret-range (list 1 fret-count))
                                                                     (third this-list)))
              (subtract-base-fret base-fret (cdr dot-list))))))
 
+(define (sans-serif-stencil layout props mag  text)
+"create a stencil in sans-serif font based on @var{layout} and @var{props}
+with magnification @varr{mag} of the string @var{text}."
+  (let* ((my-props (prepend-alist-chain  'font-size (stepmag mag)
+                   (prepend-alist-chain  'font-family 'sans props))))
+        (interpret-markup layout my-props text)))
+
+(define (sans-serif-stencil-white layout props mag text)
+"create a stencil with white text in sans-serif font based on @var{layout} and @var{props}
+with magnification @varr{mag} of the string @var{text}."
+  (let* ((text-stencil (sans-serif-stencil layout props mag text))
+         (x-extent  (ly:stencil-extent text-stencil X))
+         (y-extent  (ly:stencil-extent text-stencil Y))
+         (c  `(white-text ,(* 2 mag) ,text))) ;urg -- workaround for using ps font
+    (ly:make-stencil c  x-extent y-extent)))  ;urg -- extent is not from ps font, but we hope it's close
+
+
 (define (draw-strings string-count fret-range th size)
 "Draw the strings (vertical lines) for a fret diagram with @var{string-count} strings and frets as indicated
    in @var{fret-range}.  Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
                             x-extent y-extent)))           
  
  
-(define (draw-frets paper props fret-range string-count th size)
+(define (draw-frets layout props fret-range string-count th size)
  "Draw the frets (horizontal lines) for a fret diagram with @var{string-count} strings and frets as indicated
    in @var{fret-range}.  Line thickness is given by @var{th}, fret & string spacing by @var{size}. "
   (let* ((fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
           output-stencil)))
 ;       (ly:stencil-align-to (ly:stencil-align-to text-stencil X 0) Y 0))))
 
-(define (draw-dots paper props string-count fret-range size finger-code dot-position dot-radius dot-list)
+(define (draw-dots layout props string-count fret-range size finger-code dot-position dot-radius dot-list)
   "Make dots for fret diagram."
   (let* ((scale-dot-radius (* size dot-radius))
          (dot-color (chain-assoc-get 'dot-color props 'black))
          (extent (cons (- scale-dot-radius) scale-dot-radius))
          (finger (caddr mypair))
          (finger (if (number? finger) (number->string finger) finger))
-          (string-label-font 
-               (ly:paper-get-font paper `(((font-family . sans)
-                                           (font-encoding . ,my-font-encoding)
-                                           (font-series . medium) 
-                                           (font-shape . upright)
-                                           (font-size . ,(stepmag  string-label-font-mag))))))
-          (dot-label-font 
-               (ly:paper-get-font paper `(((font-family . sans)
-                                           (font-encoding . ,my-font-encoding)
-                                           (font-series . medium) 
-                                           (font-shape . upright)
-                                           (font-size . ,(stepmag  dot-label-font-mag))))))
          (dotstencil  (if (eq? dot-color 'white)
                           (begin
                           (ly:make-stencil (list 'white-dot 0 0 scale-dot-radius) extent extent))
                         (ly:stencil-translate-axis 
                           (ly:stencil-translate-axis 
                               (if (eq? dot-color 'white)
-                              (centered-stencil (fontify-text dot-label-font finger))
-                              (centered-stencil (fontify-text-white dot-label-font-mag 
-                                                                    dot-label-font  finger)))
+                              (centered-stencil (sans-serif-stencil layout props dot-label-font-mag finger))
+                              (centered-stencil (sans-serif-stencil-white layout props 
+                                                 dot-label-font-mag  finger)))
                                xpos X)
                               ypos Y)
                         (ly:stencil-translate-axis
                          positioned-dot
                          (ly:stencil-translate-axis 
                              (ly:stencil-translate-axis 
-                                 (centered-stencil (fontify-text string-label-font finger)) xpos  X)
+                                 (centered-stencil (sans-serif-stencil layout props 
+                                                        string-label-font-mag finger)) xpos  X)
                              (* size finger-yoffset) Y))
                      ;unknown finger-code
                      positioned-dot)))))
     (if (null? restlist) 
         labeled-dot-stencil
         (ly:stencil-add 
-            (draw-dots paper props string-count fret-range size finger-code 
+            (draw-dots layout props string-count fret-range size finger-code 
                           dot-position dot-radius restlist)
             labeled-dot-stencil))))
 
-(define (draw-xo paper props string-count fret-range size xo-list) 
+(define (draw-xo layout props string-count fret-range size xo-list) 
 "Put open and mute string indications on diagram, as contained in @var{xo-list}."
     (let* ((fret-count (+ (- (cadr fret-range) (car fret-range) 1)))
 ;           (xo-font-mag (* size (chain-assoc-get 'xo-font-magnification props 0.5)))
            (xo-font-mag (* size 0.5))
 ;           (xo-horizontal-offset (* size (chain-assoc-get 'xo-horizontal-offset props -0.35)))
            (xo-horizontal-offset (* size -0.35))
-           (font (ly:paper-get-font paper `(((font-encoding . ,my-font-encoding)(font-family . sans)
-                                        (font-series . medium) (font-shape . upright)
-                                        (font-size . ,(stepmag (* size xo-font-mag)))))))
            (mypair (car xo-list))
            (restlist (cdr xo-list))
            (glyph-string (if (eq? (car mypair) 'mute) "X" "O"))
            (xpos (+ (* (- string-count (cadr mypair)) size) xo-horizontal-offset ))
-           (glyph-stencil (ly:stencil-translate-axis (fontify-text font glyph-string) xpos X)))
+           (glyph-stencil (ly:stencil-translate-axis 
+              (sans-serif-stencil layout props (* size xo-font-mag) glyph-string) xpos X)))
       (if (null? restlist)
           glyph-stencil
           (ly:stencil-add
-            (draw-xo paper props string-count fret-range size restlist)
+            (draw-xo layout props string-count fret-range size restlist)
             glyph-stencil))))
 
 (define (make-bezier-sandwich-list left right bottom height thickness)
        (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 props string-count fret-range size finger-code dot-position dot-radius barre-list)
+(define (draw-barre layout props string-count fret-range size finger-code dot-position dot-radius barre-list)
    "Create barre indications for a fret diagram"
    (if (not (null? barre-list))
      (let* ((string1 (caar barre-list))
             (string2 (cadar barre-list))
-            (fret    (caddar barre-list))
+            (fret (caddar barre-list))
             (barre-type (chain-assoc-get 'barre-type props 'curved))
             (scale-dot-radius (* size dot-radius))
             (barre-vertical-offset (chain-assoc-get 'barre-vertical-offset props 0.5))
-            ; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant
-            (dot-center-y (* size (- (+ 2 (- (cadr fret-range) fret))dot-position) ))
-            (bottom (+  dot-center-y (* barre-vertical-offset scale-dot-radius)))
+            ;; 2 is 1 for empty fret at bottom of figure + 1 for interval (top-fret - fret + 1) -- not an arbitrary constant
+            (dot-center-y (* size
+                            (- (+ 2 (- (cadr fret-range) fret)) dot-position)))
+            (bottom (+ dot-center-y (* barre-vertical-offset scale-dot-radius)))
             (left (* size (- string-count string1)))
             (right (* size (- string-count string2)))
-;            (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
+;;            (bezier-thick (chain-assoc-get 'bezier-thickness props 0.1))
             (bezier-thick 0.1)
-;            (bezier-height (chain-assoc-get 'bezier-height props 0.5))
+;;            (bezier-height (chain-assoc-get 'bezier-height props 0.5))
             (bezier-height 0.5)
             (bezier-list (make-bezier-sandwich-list left right bottom (* size bezier-height) (* size bezier-thick)))
             (barre-stencil (if (eq? barre-type 'straight)
                               (ly:make-stencil (list 'draw-line (* size dot-radius) left dot-center-y right dot-center-y)
                                                (cons left right)
                                                (cons (- dot-center-y scale-dot-radius) (+ dot-center-y scale-dot-radius))) 
-                              (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick) )
+                              (ly:make-stencil (list 'bezier-sandwich `(quote ,bezier-list) (* size bezier-thick))
                                   (cons left right)
                                   (cons bottom (+ bottom (* size bezier-height)))))))
         (if (not (null? (cdr barre-list)))
             (ly:stencil-add barre-stencil
-                 (draw-barre paper props string-count fret-range size finger-code 
+                 (draw-barre layout props string-count fret-range size finger-code 
                       dot-position dot-radius (cdr barre-list)))
             barre-stencil ))))
 
 "Calculate the font step necessary to get a desired magnification"
 (* 6 (/ (log mag) (log 2))))
 
-(define (label-fret paper props string-count fret-range size)
+(define (label-fret layout props string-count fret-range size)
    "Label the base fret on a fret diagram"
    (let* ((base-fret (car fret-range))
 ;          (label-font-mag (chain-assoc-get 'label-font-mag props 0.7))
           (label-vertical-offset -0.2)
          (number-type (chain-assoc-get 'number-type props 'roman-lower))
           (fret-count (+ (- (cadr fret-range) (car fret-range)) 1))
-          (font (ly:paper-get-font paper `(((font-encoding . ,my-font-encoding)
-                                            (font-family . sans)
-                                            (font-series . medium) 
-                                            (font-shape . upright)
-                                            (font-size . ,(stepmag (* size label-font-mag)))))))
            (label-text 
-              (case number-type 
-                  ('roman-lower (format #f "~(~:@r~)" base-fret))
-                  ('roman-upper (format #f "~:@r" base-fret))
-                  ('arabic  (format #f "~d" base-fret))
-                  (else (format #f  "~(~:@r~)" base-fret)))))
+              (cond
+              ((equal?   number-type  'roman-lower) (format #f "~(~:@r~)" base-fret))
+              ((equal?  number-type 'roman-upper) (format #f "~:@r" base-fret))
+              ((equal? 'arabic number-type)  (format #f "~d" base-fret))
+              (else (format #f  "~(~:@r~)" base-fret)))))
        (ly:stencil-translate-axis 
-           (fontify-text font label-text) 
+           (sans-serif-stencil layout props (* size label-font-mag) label-text) 
                        (* size (+ fret-count label-vertical-offset)) Y)))
  
-(def-markup-command (fret-diagram-verbose paper props marking-list)
+(def-markup-command (fret-diagram-verbose layout props marking-list)
   (list?)
   "Make a fret diagram containing the symbols indicated in @var{marking-list}
   
-  Syntax: \\fret-diagram   marking-list
-  
   For example,
   
 @example
@@ -335,9 +330,9 @@ part of the place-fret element is present, @var{finger-value} will be displayed
 @var{finger-code}.  There is no limit to the number of fret indications per string.
 @end table
 "
-   (make-fret-diagram paper props marking-list))
+   (make-fret-diagram layout props marking-list))
    
-(define (make-fret-diagram paper props marking-list)
+(define (make-fret-diagram layout props marking-list)
 " Make a fret diagram markup"
   (let* (
          ; note:  here we get items from props that are needed in this routine, or that are needed in more than one
@@ -354,7 +349,7 @@ part of the place-fret element is present, @var{finger-value} will be displayed
          (default-dot-position (if (eq? finger-code 'in-dot) (- 0.95 default-dot-radius) 0.6))  ; move up to make room for bigger if labeled
          (dot-radius (chain-assoc-get 'dot-radius props default-dot-radius))  ; needed for both draw-dots and draw-barre
          (dot-position (chain-assoc-get 'dot-position props default-dot-position)) ; needed for both draw-dots and draw-barre
-         (th (* (ly:paper-lookup paper 'linethickness)
+         (th (* (ly:output-def-lookup layout 'linethickness)
                 (chain-assoc-get 'thickness props 0.5))) ; needed for both draw-frets and draw-strings
                 
          (alignment (chain-assoc-get 'align-dir props -0.4)) ; needed only here
@@ -369,33 +364,36 @@ part of the place-fret element is present, @var{finger-value} will be displayed
          (barre-list (cdr (assoc 'barre-list parameters)))
          (fret-diagram-stencil (ly:stencil-add
                             (draw-strings string-count fret-range th size)
-                            (draw-frets paper props fret-range string-count th size))))
+                            (draw-frets layout props fret-range string-count th size))))
          (if (not (null? barre-list))
              (set! fret-diagram-stencil (ly:stencil-add
-                                    (draw-barre paper props string-count fret-range size finger-code  
+                                    (draw-barre layout props string-count fret-range size finger-code  
                                                 dot-position dot-radius barre-list)
                                     fret-diagram-stencil)))
          (if (not (null? dot-list))
              (set! fret-diagram-stencil (ly:stencil-add
-                                    (draw-dots paper props string-count fret-range size finger-code 
+                                    (draw-dots layout props string-count fret-range size finger-code 
                                           dot-position dot-radius dot-list)
                                     fret-diagram-stencil)))
          (if (not (null? xo-list))
              (set! fret-diagram-stencil (ly:stencil-combine-at-edge
                                     fret-diagram-stencil Y UP
-                                    (draw-xo paper props string-count fret-range size xo-list) xo-padding 0)))
+                                    (draw-xo layout props string-count fret-range size xo-list) xo-padding 0)))
          (if (> (car fret-range) 1) 
              (set! fret-diagram-stencil
                    (ly:stencil-combine-at-edge fret-diagram-stencil X label-dir
-                                              (label-fret paper props string-count fret-range size) label-space 0)))
+                                              (label-fret layout props string-count fret-range size) label-space 0)))
          (ly:stencil-align-to! fret-diagram-stencil X alignment)
          fret-diagram-stencil))
          
-(def-markup-command (fret-diagram paper props definition-string)
+(def-markup-command (fret-diagram layout props definition-string)
   (string?)
-  "Syntax: \\fret-diagram definition-string
-  
-eg: \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
+  "  
+Example
+
+@example
+ \\markup \\fret-diagram #\"s:0.75;6-x;5-x;4-o;3-2;2-3;1-2;\"
+@end example
 
 for fret spacing 3/4 of staff space, D chord diagram
 
@@ -450,7 +448,7 @@ Note:  There is no limit to the number of fret indications per string.
     
 "
        (let ((definition-list (fret-parse-definition-string props definition-string)))
-       (make-fret-diagram paper (car definition-list) (cdr definition-list))))
+       (make-fret-diagram layout (car definition-list) (cdr definition-list))))
 
 (define (fret-parse-definition-string props definition-string)
  "parse a fret diagram string and return a pair containing:
@@ -508,7 +506,7 @@ Note:  There is no limit to the number of fret indications per string.
                  
 (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) )))
+    (string->number (substring keystring 2 (string-length keystring))))
   
 (define (numerify mylist)
  "Convert string values to numeric or character"
@@ -519,13 +517,15 @@ Note:  There is no limit to the number of fret indications per string.
                 (cons* numeric-value (numerify (cdr mylist)))
                 (cons* (car (string->list (car mylist))) (numerify (cdr mylist)))))))
            
-(def-markup-command (fret-diagram-terse paper props definition-string)
+(def-markup-command (fret-diagram-terse layout props definition-string)
   (string?)
   "Make a fret diagram markup using terse string-based syntax.
 
-Syntax: \\fret-diagram-terse definition-string
-
-eg: \\markup \\fret-diagram #\"x;x;o;2;3;2;\" for a D chord diagram.
+Example
+@example
+ \\markup \\fret-diagram-terse #\"x;x;o;2;3;2;\" 
+@end example
+for a D chord diagram.
 
 Syntax rules for @var{definition-string}:
 @itemize @bullet
@@ -558,7 +558,7 @@ with \"-(\" to start a barre and \"-)\" to end the barre.
 @end itemize"
 ;TODO -- change syntax to fret\string-finger
        (let ((definition-list (fret-parse-terse-definition-string props definition-string)))
-       (make-fret-diagram paper (car definition-list) (cdr definition-list))))
+       (make-fret-diagram layout (car definition-list) (cdr definition-list))))
 
 (define (fret-parse-terse-definition-string props definition-string)
  "parse a fret diagram string that uses terse syntax; return a pair containing: