]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* scm/lily-library.scm (char->unicode-index): Remove.
[lilypond.git] / scm / output-svg.scm
1 ;;;; output-svg.scm -- implement Scheme output routines for SVG1
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  2002--2004 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;;;; http://www.w3.org/TR/SVG11
8
9 (debug-enable 'backtrace)
10 (define-module (scm output-svg))
11 (define this-module (current-module))
12
13 (use-modules
14  (guile)
15  (ice-9 regex)
16  (lily))
17
18 ;; GLobals
19 ;; FIXME: 2?
20 (define output-scale (* 2 scale-to-unit))
21
22 (define (stderr string . rest)
23   (apply format (cons (current-error-port) (cons string rest)))
24   (force-output (current-error-port)))
25
26 (define (debugf string . rest)
27   (if #f
28       (apply stderr (cons string rest))))
29
30 (define (dispatch expr)
31   (let ((keyword (car expr)))
32     (cond
33      ((eq? keyword 'some-func) "")
34      ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
35      (else
36       (if (module-defined? this-module keyword)
37           (apply (eval keyword this-module) (cdr expr))
38           (begin
39             (display
40              (string-append "undefined: " (symbol->string keyword) "\n"))
41             ""))))))
42   
43 ;; Helper functions
44 (define (tagify tag string . attribute-alist)
45   (string-append
46    "<"
47    tag
48    (apply string-append
49           (map (lambda (x)
50                  (string-append " " (symbol->string (car x)) "='" (cdr x) "'"))
51                attribute-alist))
52    ">"
53    string "</" tag ">\n"))
54
55 (define (control->list c)
56   (list (car c) (cdr c)))
57
58 (define (control->string c)
59   (string-append
60    (number->string (car c)) ","
61    ;; lose the -1
62    (number->string (* -1 (cdr c))) " "))
63
64 (define (control-flip-y c)
65   (cons (car c) (* -1 (cdr c))))
66
67 (define (ly:numbers->string lst)
68   (string-append
69    (number->string (car lst))
70    (if (null? (cdr lst))
71        ""
72        (string-append "," (ly:numbers->string (cdr lst))))))
73
74 (define (svg-bezier lst close)
75   (let* ((c0 (car (list-tail lst 3)))
76          (c123 (list-head lst 3)))
77     (string-append
78      (if (not close) "M " "L ")
79      (control->string c0)
80      "C " (apply string-append (map control->string c123))
81      (if (not close) "" (string-append
82                          "L " (control->string close))))))
83
84 (define (sqr x)
85   (* x x))
86
87 (define (font-size font)
88   (let* ((designsize (ly:font-design-size font))
89          (magnification (* (ly:font-magnification font)))
90          (ops 2)
91          (scaling (* ops magnification designsize)))
92     (debugf "scaling:~S\n" scaling)
93     (debugf "magnification:~S\n" magnification)
94     (debugf "design:~S\n" designsize)
95     scaling))
96
97 (define (integer->entity integer)
98   (format #f "&#x~x;" integer))
99                    
100 (define (char->entity font char)
101   (integer->entity (char->unicode-index font char)))
102                    
103 (define (string->entities font string)
104   (apply string-append
105          (map (lambda (x) (char->entity font x)) (string->list string))))
106
107 (define (svg-font font)
108   (let* ((encoding (ly:font-encoding font))
109          (anchor (if (memq encoding '(fetaMusic fetaBraces)) 'start 'start))
110          (family (font-family font)))
111    (format #f "font-family:~a;font-weight:~a;font-size:~a;text-anchor:~S;"
112            (otf-name-mangling font family)
113            (otf-weight-mangling font family)
114            (font-size font) anchor)))
115
116 (define (fontify font expr)
117    (tagify "text" expr (cons 'style (svg-font font))))
118
119 (define-public (otf-name-mangling font family)
120   ;; Hmm, family is bigcheese20/26?
121   (if (string=? (substring family 0 (min (string-length family) 9))
122                 "bigcheese")
123       "LilyPond"
124       family))
125
126 (define-public (otf-weight-mangling font family)
127   ;; Hmm, family is bigcheese20/26?
128   (if (string=? (substring family 0 (min (string-length family) 9))
129                 "bigcheese")
130       (substring family 9)
131       "Regular"))
132
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;;; stencil outputters
135 ;;;
136
137 ;;; catch-all for missing stuff
138 ;;; comment this out to see find out what functions you miss :-)
139 (define (dummy . foo) "")
140 (map (lambda (x) (module-define! this-module x dummy))
141      (append
142       (ly:all-stencil-expressions)
143       (ly:all-output-backend-commands)))
144
145 (define (beam width slope thick blot)
146   (let* ((x width)
147          (y (* slope width))
148          (z (sqrt (+ (sqr x) (sqr y)))))
149     (tagify "rect" ""
150             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
151             `(x . "0")
152             `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
153             `(width . ,(number->string (* output-scale width)))
154             `(height . ,(number->string (* output-scale thick)))
155             `(ry . ,(number->string (* output-scale (/ blot 2))))
156             `(transform .
157                         ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
158                                  (/ x z)
159                                  (* -1 (/ y z))
160                                  1 1)))))
161
162 (define (bezier-sandwich lst thick)
163   (let* ((first (list-tail lst 4))
164          (first-c0 (car (list-tail first 3)))
165          (second (list-head lst 4)))
166     (tagify "path" ""
167             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
168             `(transform . ,(format #f "scale (~f, ~f)"
169                                    output-scale output-scale))
170             `(d . ,(string-append (svg-bezier first #f)
171                                   (svg-bezier second first-c0))))))
172
173 (define (char font i)
174   (dispatch
175    `(fontify ,font ,(tagify "tspan" (char->entity font (integer->char i))))))
176
177 (define (comment s)
178   (string-append "<!-- " s " !-->\n"))
179
180 (define (filledbox breapth width depth height)
181   (round-filled-box breapth width depth height 0))
182
183 (define (named-glyph font name)
184   (dispatch
185    `(fontify ,font ,(tagify "tspan"
186                             (integer->entity
187                              (ly:font-glyph-name-to-charcode font name))))))
188
189 (define (placebox x y expr)
190   (tagify "g"
191           ;; FIXME -- JCN
192           ;;(dispatch expr)
193           expr
194           `(transform . ,(format #f "translate (~f, ~f)"
195                                  (* output-scale x)
196                                  (- 0 (* output-scale y))))))
197
198 (define (round-filled-box breapth width depth height blot-diameter)
199   (tagify "rect" ""
200           `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
201           `(x . ,(number->string (* output-scale (- 0 breapth))))
202           `(y . ,(number->string (* output-scale (- 0 height))))
203           `(width . ,(number->string (* output-scale (+ breapth width))))
204           `(height . ,(number->string (* output-scale (+ depth height))))
205           `(ry . ,(number->string (/ blot-diameter 2)))))
206
207 (define (text font string)
208   (dispatch `(fontify ,font ,(tagify "tspan" (string->entities font string)))))
209
210 ;; WTF is this in every backend?
211 (define (horizontal-line x1 x2 th)
212   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))