]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
* scm/framework-svg.scm:
[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.
21
22 ;;; You need:
23 ;;;
24 ;;;   * Rotty's g-wrap >= 1.9.1 (or TLA)
25 ;;;   * guile-gnome-platform >= 2.5.992 (or TLA)
26 ;;;   * pango >= 1.5.2 (or CVS)
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
89
90 ;; The name of the module will change to `canvas' rsn
91 (if (resolve-module '(gnome gw canvas))
92     (use-modules (gnome gw canvas))
93     (use-modules (gnome gw libgnomecanvas)))
94
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 #f
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 (custom-utf8 i)
170   (if (< i 80)
171       (utf8 i)
172       (utf8 (+ #xee00 i))))
173
174 (define (string->utf8-string string)
175   (list->string
176    (apply append (map utf8 (map char->integer (string->list string))))))
177
178 (define (char->utf8-string char)
179   (list->string (utf8 (char->integer char))))
180
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182 ;;; stencil outputters
183 ;;;
184
185 ;;; catch-all for missing stuff
186 ;;; comment this out to see find out what functions you miss :-)
187 (define (dummy . foo) #f)
188 (map (lambda (x) (module-define! this-module x dummy))
189      (append
190       (ly:all-stencil-expressions)
191       (ly:all-output-backend-commands)))
192
193 (define (beam width slope thick blot)
194   (define cursor '(0 . 0))
195   (define (rmoveto def x y)
196     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
197     (moveto def (car cursor) (cdr cursor)))
198   (define (rlineto def x y)
199     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
200     (lineto def (car cursor) (cdr cursor)))
201   (let* ((def (make <gnome-canvas-path-def>))
202          (bezier (make <gnome-canvas-bpath>
203                    #:parent (canvas-root)
204                    #:fill-color "black"
205                    #:outline-color "black"
206                    #:width-units blot
207                    #:join-style 'round))
208          (t (- thick blot))
209          (w (- width blot))
210          (h (* w slope)))
211     
212     (reset def)
213     (rmoveto def (/ blot 2) (/ t 2))
214     (rlineto def w (- h))
215     (rlineto def 0 (- t))
216     (rlineto def (- w) h)
217     (rlineto def 0 t)
218     (closepath def)
219     (set-path-def bezier def)
220     bezier))
221
222 (define (square-beam width slope thick blot)
223   (let*
224       ((def (make <gnome-canvas-path-def>))
225        (y (* (- width) slope))
226        (props (make <gnome-canvas-bpath>
227                    #:parent (canvas-root)
228                    #:fill-color "black"
229                    #:outline-color "black"
230                    #:width-units 0.0)))
231     
232     (reset def)
233     (moveto def 0 0)
234     (lineto def width y)
235     (lineto def width (- y thick))
236     (lineto def 0 (- thick))
237     (lineto def 0 0)
238     (closepath def)
239     (set-path-def props def)
240     props))
241     
242
243 ;; two beziers
244 (define (bezier-sandwich lst thick)
245   (let* ((def (make <gnome-canvas-path-def>))
246          (bezier (make <gnome-canvas-bpath>
247                    #:parent (canvas-root)
248                    #:fill-color "black"
249                    #:outline-color "black"
250                    #:width-units thick
251                    #:join-style 'round)))
252
253     (reset def)
254
255     ;; FIXME: LST is pre-mangled for direct ps stack usage
256     ;; cl cr r l  0 1 2 3 
257     ;; cr cl l r  4 5 6 7
258     
259      (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
260      (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
261              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
262              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
263
264      (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
265      (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
266              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
267              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
268      (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
269
270     (closepath def)
271     (set-path-def bezier def)
272     bezier))
273
274 (define (char font i)
275   (text font (utf8 i)))
276
277 ;; FIXME: naming
278 (define (filledbox breapth width depth height)
279   (make <gnome-canvas-rect>
280     #:parent (canvas-root)
281     #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
282     #:fill-color "black"
283     #:join-style 'miter))
284
285 (define (grob-cause grob)
286   grob)
287
288 ;; WTF is this in every backend?
289 (define (horizontal-line x1 x2 thickness)
290   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
291
292 (define (placebox x y expr)
293   (debugf "item: ~S\n" 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
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
325 (define (list->offsets accum coords)
326   (if (null? coords)
327       accum
328       (cons (cons (car coords) (cadr coords))
329             (list->offsets accum (cddr coords))
330       )))
331
332 (define (polygon coords blotdiameter)
333   (let*
334       ((def (make <gnome-canvas-path-def>))
335        (props (make <gnome-canvas-bpath>
336                    #:parent (canvas-root)
337                    #:fill-color "black"
338                    #:outline-color "black"
339                    #:width-units blotdiameter))
340        (points (list->offsets '() coords))
341        (last-point (car (last-pair points))))
342
343     (reset def)
344     (moveto def (car last-point) (cdr last-point))
345     (for-each (lambda (x)
346                 (lineto def (car x) (cdr x))
347                 ) points)
348     (closepath def)
349     (set-path-def props def)
350     props))
351     
352
353 (define (round-filled-box breapth width depth height blot-diameter)
354   (let ((r (/ blot-diameter 2)))
355     (make <gnome-canvas-rect>
356       #:parent (canvas-root)
357       #:x1 (- r breapth) #:y1 (- depth r) #:x2 (- width r) #:y2 (- r height)
358       #:fill-color "black"
359       #:outline-color "black"
360       #:width-units blot-diameter
361       #:join-style 'round)))
362
363 (define (text font string)
364   (define (pango-font-name font)
365     (let ((name (ly:font-name font)))
366       (if name
367           (regexp-substitute/global #f "^GNU-(.*)-[.0-9]*$" name 'pre 1 'post)
368           (begin
369             (stderr "font-name: ~S\n" (ly:font-name font))
370             ;; TODO s/filename/file-name/
371             (stderr "font-filename: ~S\n" (ly:font-filename font))
372             (stderr "pango-font-size: ~S\n" (pango-font-size font))
373             "ecrm12"))))
374   
375   (define (pango-font-size font)
376     (let* ((designsize (ly:font-design-size font))
377            (magnification (* (ly:font-magnification font)))
378            
379
380            ;;font-name: "GNU-LilyPond-feta-20"
381            ;;font-filename: "feta20"
382            ;;pango-font-name: "lilypond-feta, regular 32"
383            ;;OPS:2.61
384            ;;scaling:29.7046771653543
385            ;;magnification:0.569055118110236
386            ;;design:20.0
387   
388            ;; experimental sizing:
389            ;; where does factor come from?
390            ;;
391            ;; 0.435 * (12 / 20) = 0.261
392            ;; 2.8346456692913/ 0.261 = 10.86071137659501915708
393            ;;(ops (* 0.435 (/ 12 20) (* output-scale pixels-per-unit)))
394            ;; for size-points
395            (ops 2.61)
396            
397            (scaling (* ops magnification designsize)))
398       (debugf "OPS:~S\n" ops)
399       (debugf "scaling:~S\n" scaling)
400       (debugf "magnification:~S\n" magnification)
401       (debugf "design:~S\n" designsize)
402       
403       scaling))
404
405   (make <gnome-canvas-text>
406     #:parent (canvas-root)
407
408     #:anchor 'west
409     #:x 0.0 #:y 0.0
410     
411     #:font (pango-font-name font)
412     
413     #:size-points (pango-font-size font)
414     ;;#:size ...
415     #:size-set #t
416     
417     ;;apparently no effect :-(
418     ;;#:scale 1.0
419     ;;#:scale-set #t
420     
421     #:fill-color "black"
422     #:text (if (string? string)
423                (string->utf8-string string)
424                (char->utf8-string (car string)))))
425