]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
*** empty log message ***
[lilypond.git] / scm / output-gnome.scm
1 ;;;; output-gnome.scm -- implement GNOME canvas output
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c)  2004 Jan Nieuwenhuizen <janneke@gnu.org>
6
7 ;;; TODO:
8 ;;;
9 ;;;  * font selection: name, size, design size
10 ;;;  * font scaling
11 ;;;  * .cff MUST NOT be in fc's fontpath?
12
13 ;;;  * check: blot+scaling
14 ;;;  * Figure out and fix font scaling and character placement
15 ;;;  * EC font package: add missing X font directories and AFMs
16 ;;;  * User-interface, keybindings
17 ;;;  * Implement missing stencil functions
18 ;;;  * Implement missing commands
19 ;;;  * More information in stencils, e.g., location and grob tag.
20 ;;;  * Embedded Lily:
21 ;;;    - allow GnomeCanvas or `toplevel' GtkWindow to be created
22 ;;;      outside of LilyPond
23 ;;;    - lilylib.
24 ;;;  * Release schedule and packaging of dependencies.
25 ;;;    - g-wrap-1.9.3 is already in incoming.
26 ;;;    - guile-gnome-platform-2.8.0 will probably be packaged early 2005.
27
28 ;;; You need:
29 ;;;
30 ;;;   * Rotty's g-wrap >= 1.9.3
31 ;;;   * guile-gnome-platform >= 2.7.97
32 ;;;   * pango >= 1.6.0
33 ;;;
34 ;;; See also: guile-gtk-general@gnu.org
35
36 ;;; Try it
37 ;;;
38 ;;;   * Install gnome/gtk and libffi development stuff
39 ;;;
40 ;;;   * Install [pango, g-wrap and] guile-gnome from source,
41 ;;;     see buildscripts/guile-gnome.sh
42 ;;;  
43 ;;;   * Build LilyPond with gui support: configure --enable-gui
44 ;;;
45 ;;;   * Supposing that LilyPond was built in ~/cvs/savannah/lilypond,
46 ;;;     tell fontconfig about the feta fonts dir and run fc-cache
47 "
48 cat > ~/.fonts.conf << EOF
49 <fontconfig>
50 <dir>~/cvs/savannah/lilypond/mf/out</dir>
51 <dir>/usr/share/texmf/fonts/type1/public/ec-fonts-mftraced</dir>
52 </fontconfig>
53 EOF
54 fc-cache
55 "
56 ;;;     or copy all your .pfa/.pfb's to ~/.fonts if your fontconfig
57 ;;;     already looks there for fonts.  Check if it works by doing:
58 "
59 fc-list | grep -i lily
60 "
61 ;;;
62 ;;;   * Setup environment
63 "
64 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
65 export LD_LIBRARY_PATH=$HOME/usr/pkg/pango/lib:$HOME/usr/pkg/g-wrap/lib:$HOME/usr/pkg/guile-gnome/lib:$LD_LIBRARY_PATH
66 export XEDITOR='/usr/bin/emacsclient --no-wait +%l:%c %f'
67 "
68 ;;;  * Also for GNOME point-and-click, you need to set XEDITOR and add
69 "
70 #(ly:set-point-and-click 'line-column)
71 "
72 ;;;    to your .ly.
73 ;;;
74 ;;;  * Run lily:
75 "
76 lilypond -fgnome input/simple-song.ly
77 "
78 ;;; point-and-click: (mouse-1) click on a graphical object;
79 ;;; grob-property-list: (mouse-3) click on a graphical object.
80
81 (debug-enable 'backtrace)
82
83 (define-module (scm output-gnome))
84 (define this-module (current-module))
85
86 (use-modules
87  (guile)
88  (ice-9 regex)
89  (srfi srfi-13)
90  (lily)
91  (gnome gtk)
92  (gnome gw canvas))
93
94 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 ;;; globals
96
97 ;;; set by framework-gnome.scm
98 (define canvas-root #f)
99 (define output-scale #f)
100
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 ;; helper functions
103
104 (define (stderr string . rest)
105   (apply format (cons (current-error-port) (cons string rest)))
106   (force-output (current-error-port)))
107
108 (define (debugf string . rest)
109   (if #f
110       (apply stderr (cons string rest))))
111
112 (define (utf8 i)
113   (cond
114    ((< i #x80) (list (integer->char i)))
115    ((< i #x800) (map integer->char
116                      (list (+ #xc0 (quotient i #x40))
117                            (+ #x80 (modulo i #x40)))))
118    ((< i #x10000)
119     (let ((x (quotient i #x1000))
120           (y (modulo i #x1000)))
121       (map integer->char
122            (list (+ #xe0 x)
123                  (+ #x80 (quotient y #x40))
124                  (+ #x80 (modulo y #x40))))))
125    (else (begin (stderr "programming-error: utf8 too big:~x\n" i)
126                 (list (integer->char 32))))))
127
128 (define (integer->utf8-string integer)
129   (list->string (utf8 integer)))
130
131 (define (char->utf8-string char)
132   (list->string (utf8 (char->integer char))))
133
134 (define (string->utf8-string string)
135   (apply
136    string-append
137    (map (lambda (x) (char->utf8-string x)) (string->list string))))
138
139 (define (music-font? font)
140   (let ((family (font-family font)))
141     (string=? (substring family 0 (min (string-length family) 10))
142               "emmentaler")))
143
144 (define (pango-font-name font)
145   (debugf "FONT-NAME:~S:~S\n" (ly:font-name font) (ly:font-design-size font))
146   ;;(debugf "FONT-FAMILY:~S:~S\n" (font-family font) (otf-name-mangling font (font-family font)))
147   (font-family font))
148
149 (define (pango-font-size font)
150   (let* ((designsize (ly:font-design-size font))
151          (magnification (* (ly:font-magnification font)))
152          ;; FIXME
153          ;;(scaling (* output-scale magnification designsize)))
154          (scaling (* 1.4 output-scale magnification designsize)))
155     ;;(debugf "OPS:~S\n" ops)
156     (debugf "scaling:~S\n" scaling)
157     (debugf "magnification:~S\n" magnification)
158     (debugf "design:~S\n" designsize)
159     
160     scaling))
161
162 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163 ;;; Wrappers from guile-gnome TLA
164 ;;; guile-gnome-devel@gnu.org--2004
165 ;;; http://arch.gna.org/guile-gnome/archive-2004
166 ;;;
167 ;;; janneke@gnu.org--2004-gnome
168 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
169 ;;;
170 (if (not (defined? '<gnome-canvas-path-def>))
171     (begin
172       (define-class <gnome-canvas-path-def> (<gobject>)
173         (closure #:init-value (gnome-canvas-path-def-new)
174                  #:init-keyword #:path-def
175                  #:getter get-def #:setter set-def))
176       
177       (define-method (moveto (this <gnome-canvas-path-def>) x y)
178         (gnome-canvas-path-def-moveto (get-def this) x y))
179       (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
180         (gnome-canvas-path-def-curveto (get-def this)  x1 y1 x2 y2 x3 y3))
181       (define-method (lineto (this <gnome-canvas-path-def>) x y)
182         (gnome-canvas-path-def-lineto (get-def this) x y))
183       (define-method (closepath (this <gnome-canvas-path-def>))
184         (gnome-canvas-path-def-closepath (get-def this)))
185       (define-method (reset (this <gnome-canvas-path-def>))
186         (gnome-canvas-path-def-reset (get-def this)))
187       
188       (define -set-path-def set-path-def)
189       (define -get-path-def get-path-def)
190       
191       (define-method (set-path-def (this <gnome-canvas-shape>)
192                                    (def <gnome-canvas-path-def>))
193         (-set-path-def this (get-def def)))
194       
195       (define-method (get-path-def (this <gnome-canvas-shape>))
196         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
197
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 ;;; stencil outputters
200 ;;;
201
202 ;;; catch-all for missing stuff
203 ;;; comment this out to see find out what functions you miss :-)
204 (define (dummy . foo) #f)
205 (map (lambda (x) (module-define! this-module x dummy))
206      (append
207       (ly:all-stencil-expressions)
208       (ly:all-output-backend-commands)))
209
210 (define (beam width slope thick blot)
211   (define cursor '(0 . 0))
212   (define (rmoveto def x y)
213     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
214     (moveto def (car cursor) (cdr cursor)))
215   (define (rlineto def x y)
216     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
217     (lineto def (car cursor) (cdr cursor)))
218   (let* ((def (make <gnome-canvas-path-def>))
219          (bezier (make <gnome-canvas-bpath>
220                    #:parent (canvas-root)
221                    #:fill-color "black"
222                    #:outline-color "black"
223                    #:width-units blot
224                    #:join-style 'round))
225          (t (- thick blot))
226          (w (- width blot))
227          (h (* w slope)))
228     
229     (reset def)
230     (rmoveto def (/ blot 2) (/ t 2))
231     (rlineto def w (- h))
232     (rlineto def 0 (- t))
233     (rlineto def (- w) h)
234     (rlineto def 0 t)
235     (closepath def)
236     (set-path-def bezier def)
237     bezier))
238
239 (define (square-beam width slope thick blot)
240   (let* ((def (make <gnome-canvas-path-def>))
241          (y (* (- width) slope))
242          (props (make <gnome-canvas-bpath>
243                   #:parent (canvas-root)
244                   #:fill-color "black"
245                   #:outline-color "black"
246                   #:width-units 0.0)))
247     
248     (reset def)
249     (moveto def 0 0)
250     (lineto def width y)
251     (lineto def width (- y thick))
252     (lineto def 0 (- thick))
253     (lineto def 0 0)
254     (closepath def)
255     (set-path-def props def)
256     props))
257
258 ;; two beziers
259 (define (bezier-sandwich lst thick)
260   (let* ((def (make <gnome-canvas-path-def>))
261          (bezier (make <gnome-canvas-bpath>
262                    #:parent (canvas-root)
263                    #:fill-color "black"
264                    #:outline-color "black"
265                    #:width-units thick
266                    #:join-style 'round)))
267
268     (reset def)
269
270     ;; FIXME: LST is pre-mangled for direct ps stack usage
271     ;; cl cr r l  0 1 2 3 
272     ;; cr cl l r  4 5 6 7
273     
274     (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
275     (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
276              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
277              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
278
279     (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
280     (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
281              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
282              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
283     (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
284
285     (closepath def)
286     (set-path-def bezier def)
287     bezier))
288
289 (define (char font i)
290   (text font (ly:font-index-to-charcode font i)))
291
292 (define (dashed-line thick on off dx dy)
293   (draw-line thick 0 0 dx dy))
294
295 (define (draw-line thick x1 y1 x2 y2)
296   (let* ((def (make <gnome-canvas-path-def>))
297          (props (make <gnome-canvas-bpath>
298                   #:parent (canvas-root)
299                   #:fill-color "black"
300                   #:outline-color "black"
301                   #:width-units thick)))
302     (reset def)
303     (moveto def x1 (- y1))
304     (lineto def x2 (- y2))
305     (set-path-def props def)
306     props))
307
308 ;; FIXME: naming
309 (define (filledbox breapth width depth height)
310   (make <gnome-canvas-rect>
311     #:parent (canvas-root)
312     #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
313     #:fill-color "black"
314     #:join-style 'miter))
315
316 ;; FIXME: the framework-gnome backend needs to see every item that
317 ;; gets created.  All items created here must should be put in a group
318 ;; that gets returned.
319 (define (glyph-string font postscript-font-name x-y-named-glyphs)
320   (for-each
321    (lambda (x)
322
323      ;; UGR, glyph names not found
324      (stderr "GLYPH:~S\n" (caddr x))
325      (stderr "ID:~S\n" (ly:font-glyph-name-to-charcode font (caddr x)))
326      (placebox (car x) (cadr x)
327                (make <gnome-canvas-text>
328                  #:parent (canvas-root)
329                  #:x 0.0 #:y 0.0
330                  #:anchor 'west
331                  ;;#:font postscript-font-name
332                  #:font (pango-font-name font)
333                  #:size-points 12
334                  #:size-set #t
335                  #:text
336                  (integer->utf8-string
337                   (ly:font-glyph-name-to-charcode font (caddr x))))))
338    x-y-named-glyphs))
339
340 (define (grob-cause grob)
341   grob)
342
343 ;; WTF is this in every backend?
344 (define (horizontal-line x1 x2 thickness)
345   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
346
347 (define (named-glyph font name)
348   (text font (ly:font-glyph-name-to-charcode font name)))
349
350 (define (placebox x y expr)
351   (let ((item expr))
352     ;;(if item
353     ;; FIXME ugly hack to skip #unspecified ...
354     (if (and item (not (eq? item (if #f #f))))
355         (begin
356           (move item (* output-scale x) (* output-scale (- y)))
357           (affine-relative item output-scale 0 0 output-scale 0 0)
358           item)
359         #f)))
360
361 (define (polygon coords blot-diameter)
362   (let* ((def (make <gnome-canvas-path-def>))
363          (props (make <gnome-canvas-bpath>
364                   #:parent (canvas-root)
365                   #:fill-color "black"
366                   #:outline-color "black"
367                   #:join-style 'round)
368                 #:width-units blot-diameter)
369          (points (ly:list->offsets '() coords))
370          (last-point (car (last-pair points))))
371     
372     (reset def)
373     (moveto def (car last-point) (cdr last-point))
374     (for-each (lambda (x) (lineto def (car x) (cdr x))) points)
375     (closepath def)
376     (set-path-def props def)
377     props))
378
379 (define (round-filled-box breapth width depth height blot-diameter)
380   (let ((r (/ blot-diameter 2)))
381     (make <gnome-canvas-rect>
382       #:parent (canvas-root)
383       #:x1 (- r breapth) #:y1 (- depth r) #:x2 (- width r) #:y2 (- r height)
384       #:fill-color "black"
385       #:outline-color "black"
386       #:width-units blot-diameter
387       #:join-style 'round)))
388
389 (define (text font s)
390   (stderr "FONT:~S\n" font)
391   (stderr "FONT:~S\n" (pango-font-name font))
392
393   (make <gnome-canvas-text>
394     #:parent (canvas-root)
395     ;; ugh, experimental placement corections
396     ;; #:x 0.0 #:y 0.0
397     #:x 0.0 #:y (if (music-font? font) 0.15 0.69)
398     #:anchor (if (music-font? font) 'west 'south-west)
399     #:font (pango-font-name font)
400     ;; FIXME: points
401     #:size-points (pango-font-size font)
402     ;;  or pixels?
403 ;;    #:size (inexact->exact (round (pango-font-size font)))
404     #:size-set #t
405     #:text (if (integer? s)
406                (integer->utf8-string s)
407                (string->utf8-string s))))
408
409 (define (utf8-string pango-font-description string)
410   (make <gnome-canvas-text>
411     #:parent (canvas-root)
412     #:x 0.0 #:y 0.0
413     #:anchor 'west
414     #:font pango-font-description
415     #:size-points 12
416     #:size-set #t
417     #:text string))