]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
* scm/encoding.scm (coding-alist): Fix encodings for fetaNumber
[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
22 ;;; You need:
23 ;;;
24 ;;;   * Rotty's g-wrap >= 1.9.3 (or TLA)
25 ;;;   * guile-gnome-platform >= 2.7.95 (or TLA)
26 ;;;   * pango >= 1.6.0
27 ;;;
28 ;;; See also: guile-gtk-general@gnu.org
29
30 ;;; Try it
31 ;;;
32 ;;;   [* Get cvs and tla]
33 ;;;
34 ;;;   * Install gnome/gtk and libffi development stuff
35 ;;;
36 ;;;   * Install pango, g-wrap and guile-gnome from CVS or arch: 
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 ;;; Wrappers from guile-gnome TLA
92 ;;; guile-gnome-devel@gnu.org--2004
93 ;;; http://arch.gna.org/guile-gnome/archive-2004
94 ;;;
95 ;;; janneke@gnu.org--2004-gnome
96 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
97 ;;;
98 (if (not (defined? '<gnome-canvas-path-def>))
99     (begin
100       (define-class <gnome-canvas-path-def> (<gobject>)
101         (closure #:init-value (gnome-canvas-path-def-new)
102                  #:init-keyword #:path-def
103                  #:getter get-def #:setter set-def))
104       
105       (define-method (moveto (this <gnome-canvas-path-def>) x y)
106         (gnome-canvas-path-def-moveto (get-def this) x y))
107       (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
108         (gnome-canvas-path-def-curveto (get-def this)  x1 y1 x2 y2 x3 y3))
109       (define-method (lineto (this <gnome-canvas-path-def>) x y)
110         (gnome-canvas-path-def-lineto (get-def this) x y))
111       (define-method (closepath (this <gnome-canvas-path-def>))
112         (gnome-canvas-path-def-closepath (get-def this)))
113       (define-method (reset (this <gnome-canvas-path-def>))
114         (gnome-canvas-path-def-reset (get-def this)))
115       
116       (define -set-path-def set-path-def)
117       (define -get-path-def get-path-def)
118       
119       (define-method (set-path-def (this <gnome-canvas-shape>)
120                                    (def <gnome-canvas-path-def>))
121         (-set-path-def this (get-def def)))
122       
123       (define-method (get-path-def (this <gnome-canvas-shape>))
124         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
125
126 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
127 ;;; globals
128
129 ;;; set by framework-gnome.scm
130 (define canvas-root #f)
131 (define output-scale #f)
132
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ;; helper functions
135
136 (define (stderr string . rest)
137   (apply format (cons (current-error-port) (cons string rest)))
138   (force-output (current-error-port)))
139
140 (define (debugf string . rest)
141   (if #f
142       (apply stderr (cons string rest))))
143
144 (define (list->offsets accum coords)
145   (if (null? coords)
146       accum
147       (cons (cons (car coords) (cadr coords))
148             (list->offsets accum (cddr coords)))))
149
150 (define (utf8 i)
151   (cond
152    ((< i #x80) (list (integer->char i)))
153    ((< i #x800) (map integer->char
154                      (list (+ #xc0 (quotient i #x40))
155                            (+ #x80 (modulo i #x40)))))
156    ((< i #x10000)
157     (let ((x (quotient i #x1000))
158           (y (modulo i #x1000)))
159       (map integer->char
160            (list (+ #xe0 x)
161                  (+ #x80 (quotient y #x40))
162                  (+ #x80 (modulo y #x40))))))
163    (else FIXME)))
164   
165 (define (integer->utf8-string font integer)
166   (list->string (utf8 integer)))
167
168 (define (char->utf8-string font char)
169   (list->string (utf8 (char->unicode-index font char))))
170   
171 (define (string->utf8-string font string)
172   (apply
173    string-append
174    (map (lambda (x) (char->utf8-string font x)) (string->list string))))
175
176 (define (music-font? font)
177   (let ((encoding (ly:font-encoding font))
178         (family (font-family font)))
179     (or (memq encoding '(fetaMusic fetaBraces))
180         (string=? (substring family 0 (min (string-length family) 9))
181                   "bigcheese"))))
182
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184 ;;; stencil outputters
185 ;;;
186
187 ;;; catch-all for missing stuff
188 ;;; comment this out to see find out what functions you miss :-)
189 (define (dummy . foo) #f)
190 (map (lambda (x) (module-define! this-module x dummy))
191      (append
192       (ly:all-stencil-expressions)
193       (ly:all-output-backend-commands)))
194
195 (define (beam width slope thick blot)
196   (define cursor '(0 . 0))
197   (define (rmoveto def x y)
198     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
199     (moveto def (car cursor) (cdr cursor)))
200   (define (rlineto def x y)
201     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
202     (lineto def (car cursor) (cdr cursor)))
203   (let* ((def (make <gnome-canvas-path-def>))
204          (bezier (make <gnome-canvas-bpath>
205                    #:parent (canvas-root)
206                    #:fill-color "black"
207                    #:outline-color "black"
208                    #:width-units blot
209                    #:join-style 'round))
210          (t (- thick blot))
211          (w (- width blot))
212          (h (* w slope)))
213     
214     (reset def)
215     (rmoveto def (/ blot 2) (/ t 2))
216     (rlineto def w (- h))
217     (rlineto def 0 (- t))
218     (rlineto def (- w) h)
219     (rlineto def 0 t)
220     (closepath def)
221     (set-path-def bezier def)
222     bezier))
223
224 (define (square-beam width slope thick blot)
225   (let*
226       ((def (make <gnome-canvas-path-def>))
227        (y (* (- width) slope))
228        (props (make <gnome-canvas-bpath>
229                    #:parent (canvas-root)
230                    #:fill-color "black"
231                    #:outline-color "black"
232                    #:width-units 0.0)))
233     
234     (reset def)
235     (moveto def 0 0)
236     (lineto def width y)
237     (lineto def width (- y thick))
238     (lineto def 0 (- thick))
239     (lineto def 0 0)
240     (closepath def)
241     (set-path-def props def)
242     props))
243     
244 ;; two beziers
245 (define (bezier-sandwich lst thick)
246   (let* ((def (make <gnome-canvas-path-def>))
247          (bezier (make <gnome-canvas-bpath>
248                    #:parent (canvas-root)
249                    #:fill-color "black"
250                    #:outline-color "black"
251                    #:width-units thick
252                    #:join-style 'round)))
253
254     (reset def)
255
256     ;; FIXME: LST is pre-mangled for direct ps stack usage
257     ;; cl cr r l  0 1 2 3 
258     ;; cr cl l r  4 5 6 7
259     
260      (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
261      (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
262              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
263              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
264
265      (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
266      (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
267              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
268              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
269      (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
270
271     (closepath def)
272     (set-path-def bezier def)
273     bezier))
274
275 (define (char font i)
276   (text font (ly:font-index-to-charcode font i)))
277
278 ;; FIXME: naming
279 (define (filledbox breapth width depth height)
280   (make <gnome-canvas-rect>
281     #:parent (canvas-root)
282     #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
283     #:fill-color "black"
284     #:join-style 'miter))
285
286 (define (grob-cause grob)
287   grob)
288
289 ;; WTF is this in every backend?
290 (define (horizontal-line x1 x2 thickness)
291   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
292
293 (define (placebox x y expr)
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 (* output-scale x) (* output-scale (- y)))
300           (affine-relative item output-scale 0 0 output-scale 0 0)
301           item)
302         #f)))
303
304 (define (dashed-line thick on off dx dy)
305   (draw-line thick 0 0 dx dy)) 
306
307 (define (draw-line thick fx fy tx ty)
308   (let*
309       ((def (make <gnome-canvas-path-def>))
310        (props (make <gnome-canvas-bpath>
311                    #:parent (canvas-root)
312                    #:fill-color "black"
313                    #:outline-color "black"
314                    #:width-units thick)))
315     
316     (reset def)
317     (moveto def fx (- fy))
318     (lineto def tx (- ty))
319     (set-path-def props def)
320     props))
321
322 (define (named-glyph font name)
323   (text font (ly:font-glyph-name-to-charcode font name)))
324
325 (define (polygon coords blotdiameter)
326   (let*
327       ((def (make <gnome-canvas-path-def>))
328        (props (make <gnome-canvas-bpath>
329                    #:parent (canvas-root)
330                    #:fill-color "black"
331                    #:outline-color "black"
332                    #:width-units blotdiameter))
333        (points (list->offsets '() coords))
334        (last-point (car (last-pair points))))
335
336     (reset def)
337     (moveto def (car last-point) (cdr last-point))
338     (for-each (lambda (x)
339                 (lineto def (car x) (cdr x))
340                 ) points)
341     (closepath def)
342     (set-path-def props def)
343     props))
344     
345
346 (define (round-filled-box breapth width depth height blot-diameter)
347   (let ((r (/ blot-diameter 2)))
348     (make <gnome-canvas-rect>
349       #:parent (canvas-root)
350       #:x1 (- r breapth) #:y1 (- depth r) #:x2 (- width r) #:y2 (- r height)
351       #:fill-color "black"
352       #:outline-color "black"
353       #:width-units blot-diameter
354       #:join-style 'round)))
355
356 (define (text font s)
357
358   (define (pango-font-name font)
359     (stderr "FONT-NAME:~S:~S\n" (ly:font-name font) (ly:font-design-size font))
360     (otf-name-mangling font (font-family font)))
361
362   (define (pango-font-size font)
363     (let* ((designsize (ly:font-design-size font))
364            (magnification (* (ly:font-magnification font)))
365            
366            ;;font-name: "GNU-LilyPond-feta-20"
367            ;;font-file-name: "feta20"
368            ;;pango-font-name: "lilypond-feta, regular 32"
369            ;;OPS:2.61
370            ;;scaling:29.7046771653543
371            ;;magnification:0.569055118110236
372            ;;design:20.0
373   
374            ;; ugh, experimental sizing
375            ;; where does factor ops come from?
376            ;; Hmm, design size: 26/20 
377            (ops 2.60)
378            
379            (scaling (* ops magnification designsize)))
380       (debugf "OPS:~S\n" ops)
381       (debugf "scaling:~S\n" scaling)
382       (debugf "magnification:~S\n" magnification)
383       (debugf "design:~S\n" designsize)
384       
385       scaling))
386
387   (make <gnome-canvas-text>
388     #:parent (canvas-root)
389     ;; ugh, experimental placement corections
390     ;; #:x 0.0 #:y 0.0
391     #:x 0.0 #:y (if (music-font? font) 0.15 0.69)
392     #:anchor (if (music-font? font) 'west 'south-west)
393     #:font (pango-font-name font)
394     #:size-points (pango-font-size font)
395     #:size-set #t
396     #:text (if (integer? s)
397                (integer->utf8-string font s)
398                (string->utf8-string font s))))