]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-svg.scm
* scm/output-gnome.scm (text): Hello world, again.
[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    (format #f "font-family:~a;font-size:~a;text-anchor:~S;"
111            (otf-name-mangling font (font-family font))
112            (font-size font) anchor)))
113
114 (define (fontify font expr)
115    (tagify "text" expr (cons 'style (svg-font font))))
116
117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118 ;;; stencil outputters
119 ;;;
120
121 ;;; catch-all for missing stuff
122 ;;; comment this out to see find out what functions you miss :-)
123 (define (dummy . foo) "")
124 (map (lambda (x) (module-define! this-module x dummy))
125      (append
126       (ly:all-stencil-expressions)
127       (ly:all-output-backend-commands)))
128
129 (define (beam width slope thick blot)
130   (let* ((x width)
131          (y (* slope width))
132          (z (sqrt (+ (sqr x) (sqr y)))))
133     (tagify "rect" ""
134             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot))
135             `(x . "0")
136             `(y . ,(number->string (* output-scale (- 0 (/ thick 2)))))
137             `(width . ,(number->string (* output-scale width)))
138             `(height . ,(number->string (* output-scale thick)))
139             `(ry . ,(number->string (* output-scale (/ blot 2))))
140             `(transform .
141                         ,(format #f "matrix (~f, ~f, 0, 1, 0, 0) scale (~f, ~f)"
142                                  (/ x z)
143                                  (* -1 (/ y z))
144                                  1 1)))))
145
146 (define (bezier-sandwich lst thick)
147   (let* ((first (list-tail lst 4))
148          (first-c0 (car (list-tail first 3)))
149          (second (list-head lst 4)))
150     (tagify "path" ""
151             `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" thick))
152             `(transform . ,(format #f "scale (~f, ~f)"
153                                    output-scale output-scale))
154             `(d . ,(string-append (svg-bezier first #f)
155                                   (svg-bezier second first-c0))))))
156
157 (define (char font i)
158   (dispatch
159    `(fontify ,font ,(tagify "tspan" (char->entity font (integer->char i))))))
160
161 (define (comment s)
162   (string-append "<!-- " s " !-->\n"))
163
164 (define (filledbox breapth width depth height)
165   (round-filled-box breapth width depth height 0))
166
167 (define (named-glyph font name)
168   (dispatch
169    `(fontify ,font ,(tagify "tspan"
170                             (integer->entity
171                              (ly:font-glyph-name-to-charcode font name))))))
172
173 (define (placebox x y expr)
174   (tagify "g"
175           ;; FIXME -- JCN
176           ;;(dispatch expr)
177           expr
178           `(transform . ,(format #f "translate (~f, ~f)"
179                                  (* output-scale x)
180                                  (- 0 (* output-scale y))))))
181
182 (define (round-filled-box breapth width depth height blot-diameter)
183   (tagify "rect" ""
184           `(style . ,(format "stroke-linejoin:round;stroke-linecap:round;stroke-width:~f;" blot-diameter))
185           `(x . ,(number->string (* output-scale (- 0 breapth))))
186           `(y . ,(number->string (* output-scale (- 0 height))))
187           `(width . ,(number->string (* output-scale (+ breapth width))))
188           `(height . ,(number->string (* output-scale (+ depth height))))
189           `(ry . ,(number->string (/ blot-diameter 2)))))
190
191 (define (text font string)
192   (dispatch `(fontify ,font ,(tagify "tspan" (string->entities font string)))))
193
194 ;; WTF is this in every backend?
195 (define (horizontal-line x1 x2 th)
196   (filledbox (- x1) (- x2 x1) (* .5 th) (* .5 th)))