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