1 ;;;; output-gnome.scm -- implement GNOME canvas output
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
9 ;;; * Figure out and fix font scaling and character placement
10 ;;; * EC font package: add missing X font directories and AFMs
11 ;;; * User-interface, keybindings
12 ;;; * Implement missing stencil functions
13 ;;; * Implement missing commands
14 ;;; * More information in stencils, e.g., location and grob tag.
16 ;;; - allow GnomeCanvas or `toplevel' GtkWindow to be created
17 ;;; outside of LilyPond
19 ;;; * Release schedule and packaging of dependencies.
20 ;;; - g-wrap-1.9.3 is already in incoming.
21 ;;; - guile-gnome-platform-2.8.0 will probably be packaged early 2005.
25 ;;; * Rotty's g-wrap >= 1.9.3
26 ;;; * guile-gnome-platform >= 2.7.97
29 ;;; See also: guile-gtk-general@gnu.org
33 ;;; * Install gnome/gtk and libffi development stuff
35 ;;; * Install [pango, g-wrap and] guile-gnome from source,
36 ;;; see buildscripts/guile-gnome.sh
38 ;;; * Build LilyPond with gui support: configure --enable-gui
40 ;;; * Supposing that LilyPond was built in ~/cvs/savannah/lilypond,
41 ;;; tell fontconfig about the feta fonts dir and run fc-cache
43 cat > ~/.fonts.conf << EOF
45 <dir>~/cvs/savannah/lilypond/mf/out</dir>
46 <dir>/usr/share/texmf/fonts/type1/public/ec-fonts-mftraced</dir>
51 ;;; or copy all your .pfa/.pfb's to ~/.fonts if your fontconfig
52 ;;; already looks there for fonts. Check if it works by doing:
54 fc-list | grep -i lily
57 ;;; * Setup environment
59 export GUILE_LOAD_PATH=$HOME/usr/pkg/g-wrap/share/guile/site:$HOME/usr/pkg/g-wrap/share/guile/site/g-wrap:$HOME/usr/pkg/guile-gnome/share/guile:$GUILE_LOAD_PATH
60 export LD_LIBRARY_PATH=$HOME/usr/pkg/pango/lib:$HOME/usr/pkg/g-wrap/lib:$HOME/usr/pkg/guile-gnome/lib:$LD_LIBRARY_PATH
61 export XEDITOR='/usr/bin/emacsclient --no-wait +%l:%c %f'
63 ;;; * Also for GNOME point-and-click, you need to set XEDITOR and add
65 #(ly:set-point-and-click 'line-column)
71 lilypond -fgnome input/simple-song.ly
73 ;;; point-and-click: (mouse-1) click on a graphical object;
74 ;;; grob-property-list: (mouse-3) click on a graphical object.
76 (debug-enable 'backtrace)
78 (define-module (scm output-gnome))
79 (define this-module (current-module))
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;;; set by framework-gnome.scm
93 (define canvas-root #f)
94 (define output-scale #f)
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99 (define (stderr string . rest)
100 (apply format (cons (current-error-port) (cons string rest)))
101 (force-output (current-error-port)))
103 (define (debugf string . rest)
105 (apply stderr (cons string rest))))
107 (define (list->offsets accum coords)
110 (cons (cons (car coords) (cadr coords))
111 (list->offsets accum (cddr coords)))))
115 ((< i #x80) (list (integer->char i)))
116 ((< i #x800) (map integer->char
117 (list (+ #xc0 (quotient i #x40))
118 (+ #x80 (modulo i #x40)))))
120 (let ((x (quotient i #x1000))
121 (y (modulo i #x1000)))
124 (+ #x80 (quotient y #x40))
125 (+ #x80 (modulo y #x40))))))
126 (else (begin (stderr "programming-error: utf-8 too big:~x\n" i)
127 (list (integer->char 32))))))
129 (define (integer->utf8-string integer)
130 (list->string (utf8 integer)))
132 (define (char->utf8-string char)
133 (list->string (utf8 (char->integer char))))
135 (define (string->utf8-string string)
138 (map (lambda (x) (char->utf8-string x)) (string->list string))))
140 (define (music-font? font)
141 (let ((encoding (ly:font-encoding font))
142 (family (font-family font)))
143 (or (memq encoding '(fetaMusic fetaBraces))
144 (string=? (substring family 0 (min (string-length family) 9))
147 (define-public (otf-name-mangling font family)
148 ;; Hmm, family is bigcheese20/26?
149 (if (string=? (substring family 0 (min (string-length family) 9))
151 (string-append "LilyPond " (substring family 9))
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;;; Wrappers from guile-gnome TLA
156 ;;; guile-gnome-devel@gnu.org--2004
157 ;;; http://arch.gna.org/guile-gnome/archive-2004
159 ;;; janneke@gnu.org--2004-gnome
160 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
162 (if (not (defined? '<gnome-canvas-path-def>))
164 (define-class <gnome-canvas-path-def> (<gobject>)
165 (closure #:init-value (gnome-canvas-path-def-new)
166 #:init-keyword #:path-def
167 #:getter get-def #:setter set-def))
169 (define-method (moveto (this <gnome-canvas-path-def>) x y)
170 (gnome-canvas-path-def-moveto (get-def this) x y))
171 (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
172 (gnome-canvas-path-def-curveto (get-def this) x1 y1 x2 y2 x3 y3))
173 (define-method (lineto (this <gnome-canvas-path-def>) x y)
174 (gnome-canvas-path-def-lineto (get-def this) x y))
175 (define-method (closepath (this <gnome-canvas-path-def>))
176 (gnome-canvas-path-def-closepath (get-def this)))
177 (define-method (reset (this <gnome-canvas-path-def>))
178 (gnome-canvas-path-def-reset (get-def this)))
180 (define -set-path-def set-path-def)
181 (define -get-path-def get-path-def)
183 (define-method (set-path-def (this <gnome-canvas-shape>)
184 (def <gnome-canvas-path-def>))
185 (-set-path-def this (get-def def)))
187 (define-method (get-path-def (this <gnome-canvas-shape>))
188 (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ;;; stencil outputters
194 ;;; catch-all for missing stuff
195 ;;; comment this out to see find out what functions you miss :-)
196 (define (dummy . foo) #f)
197 (map (lambda (x) (module-define! this-module x dummy))
199 (ly:all-stencil-expressions)
200 (ly:all-output-backend-commands)))
202 (define (beam width slope thick blot)
203 (define cursor '(0 . 0))
204 (define (rmoveto def x y)
205 (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
206 (moveto def (car cursor) (cdr cursor)))
207 (define (rlineto def x y)
208 (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
209 (lineto def (car cursor) (cdr cursor)))
210 (let* ((def (make <gnome-canvas-path-def>))
211 (bezier (make <gnome-canvas-bpath>
212 #:parent (canvas-root)
214 #:outline-color "black"
216 #:join-style 'round))
222 (rmoveto def (/ blot 2) (/ t 2))
223 (rlineto def w (- h))
224 (rlineto def 0 (- t))
225 (rlineto def (- w) h)
228 (set-path-def bezier def)
231 (define (square-beam width slope thick blot)
233 ((def (make <gnome-canvas-path-def>))
234 (y (* (- width) slope))
235 (props (make <gnome-canvas-bpath>
236 #:parent (canvas-root)
238 #:outline-color "black"
244 (lineto def width (- y thick))
245 (lineto def 0 (- thick))
248 (set-path-def props def)
252 (define (bezier-sandwich lst thick)
253 (let* ((def (make <gnome-canvas-path-def>))
254 (bezier (make <gnome-canvas-bpath>
255 #:parent (canvas-root)
257 #:outline-color "black"
259 #:join-style 'round)))
263 ;; FIXME: LST is pre-mangled for direct ps stack usage
267 (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
268 (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
269 (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
270 (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
272 (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
273 (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
274 (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
275 (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
276 (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
279 (set-path-def bezier def)
282 (define (char font i)
283 (text font (ly:font-index-to-charcode font i)))
286 (define (filledbox breapth width depth height)
287 (make <gnome-canvas-rect>
288 #:parent (canvas-root)
289 #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
291 #:join-style 'miter))
293 (define (grob-cause grob)
296 ;; WTF is this in every backend?
297 (define (horizontal-line x1 x2 thickness)
298 (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
300 (define (placebox x y expr)
303 ;; FIXME ugly hack to skip #unspecified ...
304 (if (and item (not (eq? item (if #f #f))))
306 (move item (* output-scale x) (* output-scale (- y)))
307 (affine-relative item output-scale 0 0 output-scale 0 0)
311 (define (dashed-line thick on off dx dy)
312 (draw-line thick 0 0 dx dy))
314 (define (draw-line thick fx fy tx ty)
316 ((def (make <gnome-canvas-path-def>))
317 (props (make <gnome-canvas-bpath>
318 #:parent (canvas-root)
320 #:outline-color "black"
321 #:width-units thick)))
324 (moveto def fx (- fy))
325 (lineto def tx (- ty))
326 (set-path-def props def)
329 (define (named-glyph font name)
330 (text font (ly:font-glyph-name-to-charcode font name)))
332 (define (polygon coords blotdiameter)
334 ((def (make <gnome-canvas-path-def>))
335 (props (make <gnome-canvas-bpath>
336 #:parent (canvas-root)
338 #:outline-color "black"
339 #:width-units blotdiameter))
340 (points (list->offsets '() coords))
341 (last-point (car (last-pair points))))
344 (moveto def (car last-point) (cdr last-point))
345 (for-each (lambda (x)
346 (lineto def (car x) (cdr x))
349 (set-path-def props def)
353 (define (round-filled-box breapth width depth height blot-diameter)
354 (let ((r (/ blot-diameter 2)))
355 (make <gnome-canvas-rect>
356 #:parent (canvas-root)
357 #:x1 (- r breapth) #:y1 (- depth r) #:x2 (- width r) #:y2 (- r height)
359 #:outline-color "black"
360 #:width-units blot-diameter
361 #:join-style 'round)))
363 (define (text font s)
365 (define (pango-font-name font)
366 (debugf "FONT-NAME:~S:~S\n" (ly:font-name font) (ly:font-design-size font))
367 (debugf "FONT-FAMILY:~S:~S\n" (font-family font) (otf-name-mangling font (font-family font)))
368 (otf-name-mangling font (font-family font)))
370 (define (pango-font-size font)
371 (let* ((designsize (ly:font-design-size font))
372 (magnification (* (ly:font-magnification font)))
374 ;;font-name: "GNU-LilyPond-feta-20"
375 ;;font-file-name: "feta20"
376 ;;pango-font-name: "lilypond-feta, regular 32"
378 ;;scaling:29.7046771653543
379 ;;magnification:0.569055118110236
382 ;; ugh, experimental sizing
383 ;; where does factor ops come from?
384 ;; Hmm, design size: 26/20
387 (scaling (* ops magnification designsize)))
388 (debugf "OPS:~S\n" ops)
389 (debugf "scaling:~S\n" scaling)
390 (debugf "magnification:~S\n" magnification)
391 (debugf "design:~S\n" designsize)
395 (make <gnome-canvas-text>
396 #:parent (canvas-root)
397 ;; ugh, experimental placement corections
399 #:x 0.0 #:y (if (music-font? font) 0.15 0.69)
400 #:anchor (if (music-font? font) 'west 'south-west)
401 #:font (pango-font-name font)
402 #:size-points (pango-font-size font)
404 #:text (if (integer? s)
405 (integer->utf8-string s)
406 (string->utf8-string s))))