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