]> git.donarmstrong.com Git - lilypond.git/blobdiff - scm/define-markup-commands.scm
update, polish.
[lilypond.git] / scm / define-markup-commands.scm
index 3039e18a97b869d767682aea36ffec61a43d394e..89f8fee98b2548985ca36a29b368151aa0e9703b 100644 (file)
@@ -5,10 +5,13 @@
 ;;;; (c) 2000--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 ;;;;                  Jan Nieuwenhuizen <janneke@gnu.org>
 
+
 ;;; markup commands
 ;;;  * each markup function should have a doc string with
 ;;     syntax, description and example. 
 
+(use-modules (ice-9 regex))
+
 (define-public empty-stencil (ly:make-stencil '() '(1 . -1) '(1 . -1)))
 (define-public point-stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
 
   "Stencil as markup"
   stil)
 
-(def-markup-command (circle layout props radius thickness)
-  (number? number?)
-  "A circle of radius @var{radius} and thickness @var{thickness}"
+(def-markup-command (draw-circle layout props radius thickness fill)
+  (number? number? boolean?)
+  "A circle of radius @var{radius}, thickness @var{thickness} and
+optionally filled."
+  (make-circle-stencil radius thickness fill))
 
-  (ly:make-stencil
-   (list 'circle radius thickness)
-   (cons (- radius) radius)
-   (cons (- radius) radius)))
+(def-markup-command (triangle layout props filled) (boolean?)
+  "A triangle, filled or not"
+  (let*
+      ((th (chain-assoc-get 'thickness props  0.1))
+       (size (chain-assoc-get 'font-size props 0))
+       (ex (* (magstep size)
+             0.8
+             (chain-assoc-get 'baseline-skip props 2))))
+
+    (ly:make-stencil
+     `(polygon '(0.0 0.0
+                    ,ex 0.0
+                    ,(* 0.5 ex)
+                    ,(* 0.86 ex))
+          ,th
+          ,filled)
+
+     (cons 0 ex)
+     (cons 0 (* .86 ex))
+     )))
+
+(def-markup-command (circle layout props arg) (markup?)
+  "Draw a circle around @var{arg}.  Use @code{thickness},
+@code{circle-padding} and @code{font-size} properties to determine line
+thickness and padding around the markup."
+  (let* ((th (chain-assoc-get 'thickness props  0.1))
+        (size (chain-assoc-get 'font-size props 0))
+        (pad
+         (* (magstep size)
+            (chain-assoc-get 'circle-padding props 0.2)))
+        (m (interpret-markup layout props arg)))
+    (circle-stencil m th pad)))
 
 (def-markup-command (with-url layout props url arg) (string? markup?)
   "Add a link to URL @var{url} around @var{arg}. This only works in
 the PDF backend."
+  (let* ((stil (interpret-markup layout props arg))
+        (xextent (ly:stencil-extent stil X))
+        (yextent (ly:stencil-extent stil Y))
+        (old-expr (ly:stencil-expr stil))
+        (url-expr (list 'url-link url `(quote ,xextent) `(quote ,yextent))))
+    (ly:stencil-add (ly:make-stencil url-expr xextent yextent) stil)))
+
+
+(define bbox-regexp
+  (make-regexp "%%BoundingBox: ([0-9-]+) ([0-9-]+) ([0-9-]+) ([0-9-]+)"))
+
+(define (get-postscript-bbox string)
+  "Extract the bbox from STRING, or return #f if not present."
   (let*
-      ((stil (interpret-markup layout props arg))
-       (xextent (ly:stencil-extent stil X))
-       (yextent (ly:stencil-extent stil Y))
-       (old-expr (ly:stencil-expr stil))
-       (url-expr (list 'url-link url `(quote ,xextent) `(quote ,yextent))))
+      ((match (regexp-exec bbox-regexp string)))
     
-    (ly:stencil-add
-     (ly:make-stencil url-expr xextent yextent)
-     stil)))
+    (if match
+       (map (lambda (x)
+              (string->number (match:substring match x)))
+            (cdr (iota 5)))
+            
+       #f)))
+
+(def-markup-command (epsfile layout props file-name) (string?)
+  "Inline an EPS image. The image is scaled such that 10 PS units is
+one staff-space."
+
+  (if (ly:get-option 'safe)
+      (interpret-markup layout props "not allowed in safe") 
+      (let*
+         ((contents (ly:gulp-file file-name))
+          (bbox (get-postscript-bbox contents))
+          (scaled-bbox
+           (if bbox
+               (map (lambda (x) (/ x 10)) bbox)
+               (begin
+                 (ly:warn (_ "can't find bounding box of `~a'")
+                          file-name)
+                 '()))))
+       
+
+       (if bbox
+           
+           (ly:make-stencil
+            (list
+             'embedded-ps
+             (string-append
+
+              ; adobe 5002.
+              "BeginEPSF "
+              "0.1 0.1 scale "
+              (format "\n%%BeginDocument: ~a\n" file-name)
+              contents
+              "%%EndDocument\n"
+              "EndEPSF\n"
+              ))
+            (cons (list-ref scaled-bbox 0) (list-ref scaled-bbox 2))
+            (cons (list-ref scaled-bbox 1) (list-ref scaled-bbox 3)))
+           
+           (ly:make-stencil "" '(0 . 0) '(0 . 0))))))  
 
 (def-markup-command (score layout props score) (ly:score?)
   "Inline an image of music."
-  (let* ((systems (ly:score-embedded-format score layout)))
+  (let* ((output (ly:score-embedded-format score layout)))
 
-    (if (= 0 (vector-length systems))
+    (if (ly:music-output? output)
+       (ly:paper-system-stencil
+        (vector-ref (ly:paper-score-paper-systems output) 0))
        (begin
-         (ly:warn "No systems found in \\score markup. Did you forget \\layout?")
-         empty-markup)
-       (let* ((stencil (ly:paper-system-stencil (vector-ref systems 0)))) 
-         
-         (ly:stencil-aligned-to stencil Y CENTER)
-         ))))
+         (ly:warning (_"no systems found in \\score markup, does it have a \\layout block?"))
+         empty-stencil))))
 
 (def-markup-command (simple layout props str) (string?)
   "A simple text string; @code{\\markup @{ foo @}} is equivalent with
@@ -60,8 +141,7 @@ the PDF backend."
 (def-markup-command (encoded-simple layout props sym str) (symbol? string?)
   "A text string, encoded with encoding @var{sym}. See
 @usermanref{Text encoding} for more information."
-  (Text_interface::interpret_string layout
-                                   props sym str))
+  (Text_interface::interpret_string layout props sym str))
 
 
 ;; TODO: use font recoding.
@@ -162,34 +242,34 @@ gsave /ecrm10 findfont
        (stack-stencils-padding-list X RIGHT fill-space-normal line-stencils))))
        
 (define (get-fill-space word-count line-width text-widths)
-       "Calculates the necessary paddings between each two adjacent texts.
+       "Calculate the necessary paddings between each two adjacent texts.
        The lengths of all texts are stored in @var{text-widths}.
        The normal formula for the padding between texts a and b is:
        padding = line-width/(word-count - 1) - (length(a) + length(b))/2
        The first and last padding have to be calculated specially using the
        whole length of the first or last text.
-       Returns a list of paddings.
+       Return a list of paddings.
 "
        (cond
-               ;; special case first padding
-               ((= (length text-widths) word-count)
-                       (cons 
-                               (- (- (/ line-width (1- word-count)) (car text-widths)) (/ (car (cdr text-widths)) 2))
-                               (get-fill-space word-count line-width (cdr text-widths))))
-               ;; special case last padding
-               ((= (length text-widths) 2)
-                       (list (- (/ line-width (1- word-count)) (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
-               (else
-                       (cons 
-                               (- (/ line-width (1- word-count)) (/ (+ (car text-widths) (car (cdr text-widths))) 2))
-                               (get-fill-space word-count line-width (cdr text-widths))))))
+        ;; special case first padding
+        ((= (length text-widths) word-count)
+         (cons 
+          (- (- (/ line-width (1- word-count)) (car text-widths))
+             (/ (car (cdr text-widths)) 2))
+          (get-fill-space word-count line-width (cdr text-widths))))
+        ;; special case last padding
+        ((= (length text-widths) 2)
+         (list (- (/ line-width (1- word-count))
+                  (+ (/ (car text-widths) 2) (car (cdr text-widths)))) 0))
+        (else
+         (cons 
+          (- (/ line-width (1- word-count))
+             (/ (+ (car text-widths) (car (cdr text-widths))) 2))
+          (get-fill-space word-count line-width (cdr text-widths))))))
 
 (define (font-markup qualifier value)
   (lambda (layout props arg)
-    (interpret-markup layout
-                     (prepend-alist-chain qualifier value props)
-                      arg)))
-
+    (interpret-markup layout (prepend-alist-chain qualifier value props) arg)))
 
 (def-markup-command (line layout props args) (markup-list?)
   "Put @var{args} in a horizontal line.  The property @code{word-space}
@@ -232,7 +312,7 @@ determines the space between each markup in @var{args}."
                     arg))
 
 (def-markup-command (fontsize layout props mag arg) (number? markup?)
-  "This sets the relative font size, e.g.
+  "Set the relative font size, e.g.
 @example
 A \\fontsize #2 @{ B C @} D
 @end example
@@ -245,8 +325,10 @@ This will enlarge the B and the C by two steps.
    (prepend-alist-chain 'font-size mag props)
    arg))
 
+
+;; FIXME -> should convert to font-size.
 (def-markup-command (magnify layout props sz arg) (number? markup?)
-  "This sets the font magnification for the its argument. In the following
+  "Set the font magnification for the its argument. In the following
 example, the middle A will be 10% larger:
 @example
 A \\magnify #1.1 @{ A @} A
@@ -367,6 +449,14 @@ recommend font for this is bold and italic"
   "Draw a double flat symbol."
   (interpret-markup layout props (markup #:musicglyph "accidentals.M4")))
 
+(def-markup-command (with-color layout props color arg) (color? markup?)
+  "Draw @var{arg} in color specified by @var{color}"
+
+  (let* ((stil (interpret-markup layout props arg)))
+
+    (ly:make-stencil (list 'color color (ly:stencil-expr stil))
+                    (ly:stencil-extent stil X)
+                    (ly:stencil-extent stil Y))))
 
 ;;
 ;; TODO: should extract baseline-skip from each argument somehow..
@@ -398,8 +488,7 @@ of the @code{#'direction} layout property."
 (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)
-    ))
+    (ly:stencil-aligned-to mol Y CENTER)))
 
 (def-markup-command (hcenter layout props arg) (markup?)
   "Align @code{arg} to its X center. "
@@ -447,10 +536,18 @@ See @usermanref{The Feta font} for  a complete listing of the possible glyphs."
 letter 'A'."
   (ly:get-glyph (ly:paper-get-font layout props) num))
 
+(def-markup-command (lower layout props amount arg) (number? markup?)
+  "
+Lower @var{arg}, by the distance @var{amount}.
+A negative @var{amount} indicates raising, see also @code{\raise}.
+"
+  (ly:stencil-translate-axis (interpret-markup layout props arg)
+                            (- amount) Y))
+
 (def-markup-command (raise layout props amount arg) (number? markup?)
   "
-This  raises  @var{arg}, by the distance @var{amount}.
-A negative @var{amount} indicates lowering:
+Raise @var{arg}, by the distance @var{amount}.
+A negative @var{amount} indicates lowering, see also @code{\\lower}.
 @c
 @lilypond[verbatim,fragment,relative=1]
  c1^\\markup { C \\small \\raise #1.0 \\bold { \"9/7+\" }}
@@ -464,8 +561,7 @@ If the text object itself is positioned above or below the staff, then
 positions it next to the staff cancels any shift made with
 @code{\\raise}. For vertical positioning, use the @code{padding}
 and/or @code{extra-offset} properties. "
-  (ly:stencil-translate-axis (interpret-markup layout props arg)
-                            amount Y))
+  (ly:stencil-translate-axis (interpret-markup layout props arg) amount Y))
 
 (def-markup-command (fraction layout props arg1 arg2) (markup? markup?)
   "Make a fraction of two markups."
@@ -487,6 +583,28 @@ and/or @code{extra-offset} properties. "
       (ly:stencil-translate-axis stack 0.75 Y))))
 
 
+
+(def-markup-command (filled-box layout props xext yext blot)
+  (number-pair? number-pair? number?)
+  "Draw a box with rounded corners of dimensions @var{xext} and @var{yext}."
+  (ly:round-filled-box
+   xext yext blot))
+
+(def-markup-command (whiteout layout props arg) (markup?)
+  "Provide a white underground for @var{arg}"
+  (let* ((stil (interpret-markup layout props
+                                (make-with-color-markup black arg)))
+        (white
+         (interpret-markup layout props
+                           (make-with-color-markup
+                            white
+                            (make-filled-box-markup
+                             (ly:stencil-extent stil X)
+                             (ly:stencil-extent stil Y)
+                             0.0)))))
+
+    (ly:stencil-add white stil)))
+
 ;; TODO: better syntax.
 
 (def-markup-command (note-by-number layout props log dot-count dir) (number? number? number?)
@@ -533,19 +651,18 @@ and/or @code{extra-offset} properties. "
     (if (ly:stencil? dots)
         (set! stem-glyph
               (ly:stencil-add
-               (ly:stencil-translate-axis dots
-                                         (+ (if (and (> dir 0) (> log 2))
-                                                (* 1.5 dotwid)
-                                                0)
-                                            ;; huh ? why not necessary?
-                                            ;;(cdr (ly:stencil-extent head-glyph X))
-                                            dotwid)
-                                         X)
+               (ly:stencil-translate-axis
+               dots
+               (+ (if (and (> dir 0) (> log 2))
+                      (* 1.5 dotwid)
+                      0)
+                  ;; huh ? why not necessary?
+                  ;;(cdr (ly:stencil-extent head-glyph X))
+                  dotwid)
+               X)
                stem-glyph)))
     stem-glyph))
 
-(use-modules (ice-9 regex))
-
 (define-public log2 
   (let ((divisor (log 2)))
     (lambda (z) (inexact->exact (/ (log z) divisor)))))
@@ -556,12 +673,12 @@ and/or @code{extra-offset} properties. "
     (if (and match (string=? duration-string (match:substring match 0)))
         (let ((len  (match:substring match 1))
               (dots (match:substring match 2)))
-          (list (cond ((string=? len "breve")  -1)
-                      ((string=? len "longa")  -2)
+          (list (cond ((string=? len "breve") -1)
+                      ((string=? len "longa") -2)
                       ((string=? len "maxima") -3)
                       (else (log2 (string->number len))))
                 (if dots (string-length dots) 0)))
-        (error "This is not a valid duration string:" duration-string))))
+        (ly:error (_ "not a valid duration string: ~a") duration-string))))
 
 (def-markup-command (note layout props duration dir) (string? number?)
   "This produces a note with a stem pointing in @var{dir} direction, with
@@ -573,11 +690,9 @@ a shortened down stem."
 
 (def-markup-command (normal-size-super layout props arg) (markup?)
   "Set @var{arg} in superscript with a normal font size."
-  (ly:stencil-translate-axis (interpret-markup
-                             layout
-                             props arg)
-                            (* 0.5  (chain-assoc-get 'baseline-skip props))
-                            Y))
+  (ly:stencil-translate-axis
+   (interpret-markup layout props arg)
+   (* 0.5 (chain-assoc-get 'baseline-skip props)) Y))
 
 (def-markup-command (super layout props arg) (markup?)
   "
@@ -629,14 +744,13 @@ that.
    (* -0.5 (chain-assoc-get 'baseline-skip props))
    Y))
 
-(def-markup-command (beam layout props width slope thickness) (number? number? number?)
+(def-markup-command (beam layout props width slope thickness)
+  (number? number? number?)
   "Create a beam with the specified parameters."
+  (let* ((y (* slope width))
+        (yext (cons (min 0 y) (max 0 y)))
+        (half (/ thickness 2)))
 
-  (let*
-      ((y (* slope width))
-       (yext (cons (min 0 y) (max 0 y)))
-       (half (/ thickness 2)))
-       
     (ly:make-stencil
      (list 'beam width
           slope
@@ -644,10 +758,7 @@ that.
           (ly:output-def-lookup layout 'blotdiameter))
      (cons 0 width)
      (cons (+ (- half) (car yext))
-          (+ half (cdr yext))))
-
-    ))
-
+          (+ half (cdr yext))))))
 
 (def-markup-command (normal-size-sub layout props arg) (markup?)
   "Set @var{arg} in subscript, in a normal font size."
@@ -723,9 +834,8 @@ any sort of property supported by @internalsref{font-interface} and
 thickness and padding around the markup."
   (let* ((th (chain-assoc-get 'thickness props  0.1))
         (size (chain-assoc-get 'font-size props 0))
-        (pad
-         (* (magstep size)
-            (chain-assoc-get 'box-padding props 0.2)))
+        (pad (* (magstep size)
+                (chain-assoc-get 'box-padding props 0.2)))
         (m (interpret-markup layout props arg)))
     (box-stencil m th pad)))