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 ;;; * font selection: name, size, design size
11 ;;;; * .cff MUST NOT be in fc's fontpath.
12 ;;;; - workaround: remove mf/out from ~/.fonts.conf,
13 ;;;; instead add ~/.fonts and symlink all /mf/out/*otf there.
14 ;;;; - bug in fontconfig/freetype/pango?
16 ;;; * check: blot+scaling
17 ;;; * Figure out and fix font scaling and character placement
18 ;;; * EC font package: add missing X font directories and AFMs
19 ;;; * User-interface, keybindings
20 ;;; * Implement missing stencil functions
21 ;;; * Implement missing commands
22 ;;; * More information in stencils, e.g., location and grob tag.
24 ;;; - allow GnomeCanvas or `toplevel' GtkWindow to be created
25 ;;; outside of LilyPond
27 ;;; * Release schedule and packaging of dependencies.
28 ;;; - g-wrap-1.9.3 is already in incoming.
29 ;;; - guile-gnome-platform-2.8.0 will probably be packaged early 2005.
33 ;;; * Rotty's g-wrap >= 1.9.3
34 ;;; * guile-gnome-platform >= 2.7.97
37 ;;; See also: guile-gtk-general@gnu.org
41 ;;; * Install gnome/gtk and libffi development stuff
43 ;;; * Install [pango, g-wrap and] guile-gnome from source,
44 ;;; see buildscripts/guile-gnome.sh
46 ;;; * Build LilyPond with gui support: configure --enable-gui
48 ;;; * Supposing that LilyPond was built in ~/cvs/savannah/lilypond,
49 ;;; tell fontconfig about the feta fonts dir and run fc-cache
51 cat > ~/.fonts.conf << EOF
53 <dir>~/cvs/savannah/lilypond/mf/out</dir>
54 <dir>/usr/share/texmf/fonts/type1/public/ec-fonts-mftraced</dir>
59 ;;; or copy all your .pfa/.pfb's to ~/.fonts if your fontconfig
60 ;;; already looks there for fonts. Check if it works by doing:
62 fc-list | grep -i lily
65 ;;; * Setup environment
67 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
68 export LD_LIBRARY_PATH=$HOME/usr/pkg/pango/lib:$HOME/usr/pkg/g-wrap/lib:$HOME/usr/pkg/guile-gnome/lib:$LD_LIBRARY_PATH
69 export XEDITOR='/usr/bin/emacsclient --no-wait +%l:%c %f'
71 ;;; * Also for GNOME point-and-click, you need to set XEDITOR and add
73 #(ly:set-point-and-click 'line-column)
79 lilypond -fgnome input/simple-song.ly
81 ;;; point-and-click: (mouse-1) click on a graphical object;
82 ;;; grob-property-list: (mouse-3) click on a graphical object.
84 (debug-enable 'backtrace)
86 (define-module (scm output-gnome))
87 (define this-module (current-module))
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100 ;;; set by framework-gnome.scm
101 (define canvas-root #f)
102 (define output-scale #f)
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 (define (stderr string . rest)
108 (apply format (cons (current-error-port) (cons string rest)))
109 (force-output (current-error-port)))
111 (define (debugf string . rest)
113 (apply stderr (cons string rest))))
117 ((< i #x80) (list (integer->char i)))
118 ((< i #x800) (map integer->char
119 (list (+ #xc0 (quotient i #x40))
120 (+ #x80 (modulo i #x40)))))
122 (let ((x (quotient i #x1000))
123 (y (modulo i #x1000)))
126 (+ #x80 (quotient y #x40))
127 (+ #x80 (modulo y #x40))))))
128 (else (begin (stderr "programming-error: utf8 too big:~x\n" i)
129 (list (integer->char 32))))))
131 (define (integer->utf8-string integer)
132 (list->string (utf8 integer)))
134 (define (char->utf8-string char)
135 (list->string (utf8 (char->integer char))))
137 (define (string->utf8-string string)
140 (map (lambda (x) (char->utf8-string x)) (string->list string))))
142 (define (music-font? font)
143 (let ((family (car (font-name-style font))))
144 (string=? (substring family 0 (min (string-length family) 10))
147 (define (pango-font-name font)
148 (debugf "FONT-NAME:~S:~S\n" (ly:font-name font) (ly:font-design-size font))
149 (apply format (append '(#f "~a, ~a") (font-name-style font))))
151 (define (pango-font-size font)
152 (let* ((designsize (ly:font-design-size font))
153 (magnification (* (ly:font-magnification font)))
155 ;;(scaling (* output-scale magnification designsize)))
156 (scaling (* 1.4 output-scale magnification designsize)))
157 ;;(debugf "OPS:~S\n" ops)
158 (debugf "scaling:~S\n" scaling)
159 (debugf "magnification:~S\n" magnification)
160 (debugf "design:~S\n" designsize)
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 ;;; Wrappers from guile-gnome TLA
166 ;;; guile-gnome-devel@gnu.org--2004
167 ;;; http://arch.gna.org/guile-gnome/archive-2004
169 ;;; janneke@gnu.org--2004-gnome
170 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
172 (if (not (defined? '<gnome-canvas-path-def>))
174 (define-class <gnome-canvas-path-def> (<gobject>)
175 (closure #:init-value (gnome-canvas-path-def-new)
176 #:init-keyword #:path-def
177 #:getter get-def #:setter set-def))
179 (define-method (moveto (this <gnome-canvas-path-def>) x y)
180 (gnome-canvas-path-def-moveto (get-def this) x y))
181 (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
182 (gnome-canvas-path-def-curveto (get-def this) x1 y1 x2 y2 x3 y3))
183 (define-method (lineto (this <gnome-canvas-path-def>) x y)
184 (gnome-canvas-path-def-lineto (get-def this) x y))
185 (define-method (closepath (this <gnome-canvas-path-def>))
186 (gnome-canvas-path-def-closepath (get-def this)))
187 (define-method (reset (this <gnome-canvas-path-def>))
188 (gnome-canvas-path-def-reset (get-def this)))
190 (define -set-path-def set-path-def)
191 (define -get-path-def get-path-def)
193 (define-method (set-path-def (this <gnome-canvas-shape>)
194 (def <gnome-canvas-path-def>))
195 (-set-path-def this (get-def def)))
197 (define-method (get-path-def (this <gnome-canvas-shape>))
198 (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
200 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 ;;; stencil outputters
204 ;;; catch-all for missing stuff
205 ;;; comment this out to see find out what functions you miss :-)
206 (define (dummy . foo) #f)
207 (map (lambda (x) (module-define! this-module x dummy))
209 (ly:all-stencil-expressions)
210 (ly:all-output-backend-commands)))
212 (define (beam width slope thick blot)
213 (define cursor '(0 . 0))
214 (define (rmoveto def x y)
215 (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
216 (moveto def (car cursor) (cdr cursor)))
217 (define (rlineto def x y)
218 (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
219 (lineto def (car cursor) (cdr cursor)))
220 (let* ((def (make <gnome-canvas-path-def>))
221 (bezier (make <gnome-canvas-bpath>
222 #:parent (canvas-root)
224 #:outline-color "black"
226 #:join-style 'round))
232 (rmoveto def (/ blot 2) (/ t 2))
233 (rlineto def w (- h))
234 (rlineto def 0 (- t))
235 (rlineto def (- w) h)
238 (set-path-def bezier def)
241 (define (square-beam width slope thick blot)
242 (let* ((def (make <gnome-canvas-path-def>))
243 (y (* (- width) slope))
244 (props (make <gnome-canvas-bpath>
245 #:parent (canvas-root)
247 #:outline-color "black"
253 (lineto def width (- y thick))
254 (lineto def 0 (- thick))
257 (set-path-def props def)
261 (define (bezier-sandwich lst thick)
262 (let* ((def (make <gnome-canvas-path-def>))
263 (bezier (make <gnome-canvas-bpath>
264 #:parent (canvas-root)
266 #:outline-color "black"
268 #:join-style 'round)))
272 ;; FIXME: LST is pre-mangled for direct ps stack usage
276 (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
277 (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
278 (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
279 (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
281 (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
282 (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
283 (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
284 (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
285 (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
288 (set-path-def bezier def)
291 (define (char font i)
292 (text font (ly:font-index-to-charcode font i)))
294 (define (dashed-line thick on off dx dy)
295 (draw-line thick 0 0 dx dy))
297 (define (draw-line thick x1 y1 x2 y2)
298 (let* ((def (make <gnome-canvas-path-def>))
299 (props (make <gnome-canvas-bpath>
300 #:parent (canvas-root)
302 #:outline-color "black"
303 #:width-units thick)))
305 (moveto def x1 (- y1))
306 (lineto def x2 (- y2))
307 (set-path-def props def)
311 (define (filledbox breapth width depth height)
312 (make <gnome-canvas-rect>
313 #:parent (canvas-root)
314 #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
316 #:join-style 'miter))
318 ;; FIXME: the framework-gnome backend needs to see every item that
319 ;; gets created. All items created here must should be put in a group
320 ;; that gets returned.
321 (define (glyph-string font postscript-font-name x-y-named-glyphs)
325 ;; UGR, glyph names not found
326 (stderr "GLYPH:~S\n" (caddr x))
327 (stderr "ID:~S\n" (ly:font-glyph-name-to-charcode font (caddr x)))
328 (placebox (car x) (cadr x)
329 (make <gnome-canvas-text>
330 #:parent (canvas-root)
333 #:font (pango-font-name font)
337 (integer->utf8-string
338 (ly:font-glyph-name-to-charcode font (caddr x))))))
341 (define (grob-cause grob)
344 ;; WTF is this in every backend?
345 (define (horizontal-line x1 x2 thickness)
346 (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
348 (define (named-glyph font name)
349 (text font (ly:font-glyph-name-to-charcode font name)))
351 (define (placebox x y expr)
354 ;; FIXME ugly hack to skip #unspecified ...
355 (if (and item (not (eq? item (if #f #f))))
357 (move item (* output-scale x) (* output-scale (- y)))
358 (affine-relative item output-scale 0 0 output-scale 0 0)
362 (define (polygon coords blot-diameter)
363 (let* ((def (make <gnome-canvas-path-def>))
364 (props (make <gnome-canvas-bpath>
365 #:parent (canvas-root)
367 #:outline-color "black"
369 #:width-units blot-diameter)
370 (points (ly:list->offsets '() coords))
371 (last-point (car (last-pair points))))
374 (moveto def (car last-point) (cdr last-point))
375 (for-each (lambda (x) (lineto def (car x) (cdr x))) points)
377 (set-path-def props def)
380 (define (round-filled-box breapth width depth height blot-diameter)
381 (let ((r (/ blot-diameter 2)))
382 (make <gnome-canvas-rect>
383 #:parent (canvas-root)
384 #:x1 (- r breapth) #:y1 (- depth r) #:x2 (- width r) #:y2 (- r height)
386 #:outline-color "black"
387 #:width-units blot-diameter
388 #:join-style 'round)))
390 (define (text font s)
391 (debugf "FONT:~S\n" font)
392 (debugf "FONT:~S\n" (pango-font-name font))
394 (make <gnome-canvas-text>
395 #:parent (canvas-root)
396 ;; ugh, experimental placement corections
398 #:x 0.0 #:y (if (music-font? font) 0.15 0.69)
399 #:anchor (if (music-font? font) 'west 'south-west)
400 #:font (pango-font-name font)
402 #:size-points (pango-font-size font)
404 ;; #:size (inexact->exact (round (pango-font-size font)))
406 #:text (if (integer? s)
407 (integer->utf8-string s)
408 (string->utf8-string s))))
410 (define (utf8-string pango-font-description string)
411 (make <gnome-canvas-text>
412 #:parent (canvas-root)
415 #:font pango-font-description