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