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