]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
18e45bfbf196bd6e47688639da5b88981f703df7
[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: utf8 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 ((family (font-family font)))
137     (string=? (substring family 0 (min (string-length family) 10))
138               "emmentaler")))
139
140 (define (pango-font-name font)
141   (debugf "FONT-NAME:~S:~S\n" (ly:font-name font) (ly:font-design-size font))
142   ;;(debugf "FONT-FAMILY:~S:~S\n" (font-family font) (otf-name-mangling font (font-family font)))
143   (font-family font))
144
145 (define (pango-font-size font)
146   (let* ((designsize (ly:font-design-size font))
147          (magnification (* (ly:font-magnification font)))
148          
149          ;;font-name: "GNU-LilyPond-feta-20"
150          ;;font-file-name: "feta20"
151          ;;pango-font-name: "lilypond-feta, regular 32"
152          ;;OPS:2.61
153          ;;scaling:29.7046771653543
154          ;;magnification:0.569055118110236
155          ;;design:20.0
156          
157          ;; ugh, experimental sizing
158          ;; where does factor ops come from?
159          ;; Hmm, design size: 26/20 
160          ;;(ops 2.60)
161          (ops output-scale)
162          
163          (scaling (* 1.5 ops magnification designsize)))
164     (debugf "OPS:~S\n" ops)
165     (debugf "scaling:~S\n" scaling)
166     (debugf "magnification:~S\n" magnification)
167     (debugf "design:~S\n" designsize)
168     
169     scaling))
170
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;; Wrappers from guile-gnome TLA
173 ;;; guile-gnome-devel@gnu.org--2004
174 ;;; http://arch.gna.org/guile-gnome/archive-2004
175 ;;;
176 ;;; janneke@gnu.org--2004-gnome
177 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
178 ;;;
179 (if (not (defined? '<gnome-canvas-path-def>))
180     (begin
181       (define-class <gnome-canvas-path-def> (<gobject>)
182         (closure #:init-value (gnome-canvas-path-def-new)
183                  #:init-keyword #:path-def
184                  #:getter get-def #:setter set-def))
185       
186       (define-method (moveto (this <gnome-canvas-path-def>) x y)
187         (gnome-canvas-path-def-moveto (get-def this) x y))
188       (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
189         (gnome-canvas-path-def-curveto (get-def this)  x1 y1 x2 y2 x3 y3))
190       (define-method (lineto (this <gnome-canvas-path-def>) x y)
191         (gnome-canvas-path-def-lineto (get-def this) x y))
192       (define-method (closepath (this <gnome-canvas-path-def>))
193         (gnome-canvas-path-def-closepath (get-def this)))
194       (define-method (reset (this <gnome-canvas-path-def>))
195         (gnome-canvas-path-def-reset (get-def this)))
196       
197       (define -set-path-def set-path-def)
198       (define -get-path-def get-path-def)
199       
200       (define-method (set-path-def (this <gnome-canvas-shape>)
201                                    (def <gnome-canvas-path-def>))
202         (-set-path-def this (get-def def)))
203       
204       (define-method (get-path-def (this <gnome-canvas-shape>))
205         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
206
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;;; stencil outputters
209 ;;;
210
211 ;;; catch-all for missing stuff
212 ;;; comment this out to see find out what functions you miss :-)
213 (define (dummy . foo) #f)
214 (map (lambda (x) (module-define! this-module x dummy))
215      (append
216       (ly:all-stencil-expressions)
217       (ly:all-output-backend-commands)))
218
219 (define (beam width slope thick blot)
220   (define cursor '(0 . 0))
221   (define (rmoveto def x y)
222     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
223     (moveto def (car cursor) (cdr cursor)))
224   (define (rlineto def x y)
225     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
226     (lineto def (car cursor) (cdr cursor)))
227   (let* ((def (make <gnome-canvas-path-def>))
228          (bezier (make <gnome-canvas-bpath>
229                    #:parent (canvas-root)
230                    #:fill-color "black"
231                    #:outline-color "black"
232                    #:width-units blot
233                    #:join-style 'round))
234          (t (- thick blot))
235          (w (- width blot))
236          (h (* w slope)))
237     
238     (reset def)
239     (rmoveto def (/ blot 2) (/ t 2))
240     (rlineto def w (- h))
241     (rlineto def 0 (- t))
242     (rlineto def (- w) h)
243     (rlineto def 0 t)
244     (closepath def)
245     (set-path-def bezier def)
246     bezier))
247
248 (define (square-beam width slope thick blot)
249   (let* ((def (make <gnome-canvas-path-def>))
250          (y (* (- width) slope))
251          (props (make <gnome-canvas-bpath>
252                   #:parent (canvas-root)
253                   #:fill-color "black"
254                   #:outline-color "black"
255                   #:width-units 0.0)))
256     
257     (reset def)
258     (moveto def 0 0)
259     (lineto def width y)
260     (lineto def width (- y thick))
261     (lineto def 0 (- thick))
262     (lineto def 0 0)
263     (closepath def)
264     (set-path-def props def)
265     props))
266
267 ;; two beziers
268 (define (bezier-sandwich lst thick)
269   (let* ((def (make <gnome-canvas-path-def>))
270          (bezier (make <gnome-canvas-bpath>
271                    #:parent (canvas-root)
272                    #:fill-color "black"
273                    #:outline-color "black"
274                    #:width-units thick
275                    #:join-style 'round)))
276
277     (reset def)
278
279     ;; FIXME: LST is pre-mangled for direct ps stack usage
280     ;; cl cr r l  0 1 2 3 
281     ;; cr cl l r  4 5 6 7
282     
283     (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
284     (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
285              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
286              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
287
288     (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
289     (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
290              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
291              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
292     (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
293
294     (closepath def)
295     (set-path-def bezier def)
296     bezier))
297
298 (define (char font i)
299   (text font (ly:font-index-to-charcode font i)))
300
301 (define (dashed-line thick on off dx dy)
302   (draw-line thick 0 0 dx dy))
303
304 (define (draw-line thick x1 y1 x2 y2)
305   (let* ((def (make <gnome-canvas-path-def>))
306          (props (make <gnome-canvas-bpath>
307                   #:parent (canvas-root)
308                   #:fill-color "black"
309                   #:outline-color "black"
310                   #:width-units thick)))
311     (reset def)
312     (moveto def x1 (- y1))
313     (lineto def x2 (- y2))
314     (set-path-def props def)
315     props))
316
317 ;; FIXME: naming
318 (define (filledbox breapth width depth height)
319   (make <gnome-canvas-rect>
320     #:parent (canvas-root)
321     #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
322     #:fill-color "black"
323     #:join-style 'miter))
324
325 ;; FIXME: the framework-gnome backend needs to see every item that
326 ;; gets created.  All items created here must should be put in a group
327 ;; that gets returned.
328 (define (glyph-string font postscript-font-name x-y-named-glyphs)
329   (for-each
330    (lambda (x)
331
332      ;; UGR, glyph names not found
333      (stderr "GLYPH:~S\n" (caddr x))
334      (stderr "ID:~S\n" (ly:font-glyph-name-to-charcode font (caddr x)))
335      (placebox (car x) (cadr x)
336                (make <gnome-canvas-text>
337                  #:parent (canvas-root)
338                  #:x 0.0 #:y 0.0
339                  #:anchor 'west
340                  ;;#:font postscript-font-name
341                  #:font (pango-font-name font)
342                  #:size-points 12
343                  #:size-set #t
344                  #:text
345                  (integer->utf8-string
346                   (ly:font-glyph-name-to-charcode font (caddr x))))))
347    x-y-named-glyphs))
348
349 (define (grob-cause grob)
350   grob)
351
352 ;; WTF is this in every backend?
353 (define (horizontal-line x1 x2 thickness)
354   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
355
356 (define (named-glyph font name)
357   (text font (ly:font-glyph-name-to-charcode font name)))
358
359 (define (placebox x y expr)
360   (let ((item expr))
361     ;;(if item
362     ;; FIXME ugly hack to skip #unspecified ...
363     (if (and item (not (eq? item (if #f #f))))
364         (begin
365           (move item (* output-scale x) (* output-scale (- y)))
366           (affine-relative item output-scale 0 0 output-scale 0 0)
367           item)
368         #f)))
369
370 (define (polygon coords blot-diameter)
371   (let* ((def (make <gnome-canvas-path-def>))
372          (props (make <gnome-canvas-bpath>
373                   #:parent (canvas-root)
374                   #:fill-color "black"
375                   #:outline-color "black"
376                   #:join-style 'round)
377                 #:width-units blot-diameter)
378          (points (ly:list->offsets '() coords))
379          (last-point (car (last-pair points))))
380     
381     (reset def)
382     (moveto def (car last-point) (cdr last-point))
383     (for-each (lambda (x) (lineto def (car x) (cdr x))) points)
384     (closepath def)
385     (set-path-def props def)
386     props))
387
388 (define (round-filled-box breapth width depth height blot-diameter)
389   (let ((r (/ blot-diameter 2)))
390     (make <gnome-canvas-rect>
391       #:parent (canvas-root)
392       #:x1 (- r breapth) #:y1 (- depth r) #:x2 (- width r) #:y2 (- r height)
393       #:fill-color "black"
394       #:outline-color "black"
395       #:width-units blot-diameter
396       #:join-style 'round)))
397
398 (define (text font s)
399   (stderr "FONT:~S\n" font)
400   (stderr "FONT:~S\n" (pango-font-name font))
401
402   (make <gnome-canvas-text>
403     #:parent (canvas-root)
404     ;; ugh, experimental placement corections
405     ;; #:x 0.0 #:y 0.0
406     #:x 0.0 #:y (if (music-font? font) 0.15 0.69)
407     #:anchor (if (music-font? font) 'west 'south-west)
408     #:font (pango-font-name font)
409     #:size-points (pango-font-size font)
410     #:size-set #t
411     #:text (if (integer? s)
412                (integer->utf8-string s)
413                (string->utf8-string s))))
414
415 (define (utf8-string pango-font-description string)
416   (make <gnome-canvas-text>
417     #:parent (canvas-root)
418     #:x 0.0 #:y 0.0
419     #:anchor 'west
420     #:font pango-font-description
421     ;;#:size-points 
422     #:size-set #t
423     #:text string))