]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
* scm/lily-library.scm (char->unicode-index): Remove.
[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 (define-public (otf-name-mangling font family)
148   ;; Hmm, family is bigcheese20/26?
149   (if (string=? (substring family 0 (min (string-length family) 9))
150                 "bigcheese")
151       (string-append "LilyPond " (substring family 9))
152       family))
153
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;;; Wrappers from guile-gnome TLA
156 ;;; guile-gnome-devel@gnu.org--2004
157 ;;; http://arch.gna.org/guile-gnome/archive-2004
158 ;;;
159 ;;; janneke@gnu.org--2004-gnome
160 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
161 ;;;
162 (if (not (defined? '<gnome-canvas-path-def>))
163     (begin
164       (define-class <gnome-canvas-path-def> (<gobject>)
165         (closure #:init-value (gnome-canvas-path-def-new)
166                  #:init-keyword #:path-def
167                  #:getter get-def #:setter set-def))
168       
169       (define-method (moveto (this <gnome-canvas-path-def>) x y)
170         (gnome-canvas-path-def-moveto (get-def this) x y))
171       (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
172         (gnome-canvas-path-def-curveto (get-def this)  x1 y1 x2 y2 x3 y3))
173       (define-method (lineto (this <gnome-canvas-path-def>) x y)
174         (gnome-canvas-path-def-lineto (get-def this) x y))
175       (define-method (closepath (this <gnome-canvas-path-def>))
176         (gnome-canvas-path-def-closepath (get-def this)))
177       (define-method (reset (this <gnome-canvas-path-def>))
178         (gnome-canvas-path-def-reset (get-def this)))
179       
180       (define -set-path-def set-path-def)
181       (define -get-path-def get-path-def)
182       
183       (define-method (set-path-def (this <gnome-canvas-shape>)
184                                    (def <gnome-canvas-path-def>))
185         (-set-path-def this (get-def def)))
186       
187       (define-method (get-path-def (this <gnome-canvas-shape>))
188         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
189
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ;;; stencil outputters
192 ;;;
193
194 ;;; catch-all for missing stuff
195 ;;; comment this out to see find out what functions you miss :-)
196 (define (dummy . foo) #f)
197 (map (lambda (x) (module-define! this-module x dummy))
198      (append
199       (ly:all-stencil-expressions)
200       (ly:all-output-backend-commands)))
201
202 (define (beam width slope thick blot)
203   (define cursor '(0 . 0))
204   (define (rmoveto def x y)
205     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
206     (moveto def (car cursor) (cdr cursor)))
207   (define (rlineto def x y)
208     (set! cursor (cons (+ x (car cursor)) (+ y (cdr cursor))))
209     (lineto def (car cursor) (cdr cursor)))
210   (let* ((def (make <gnome-canvas-path-def>))
211          (bezier (make <gnome-canvas-bpath>
212                    #:parent (canvas-root)
213                    #:fill-color "black"
214                    #:outline-color "black"
215                    #:width-units blot
216                    #:join-style 'round))
217          (t (- thick blot))
218          (w (- width blot))
219          (h (* w slope)))
220     
221     (reset def)
222     (rmoveto def (/ blot 2) (/ t 2))
223     (rlineto def w (- h))
224     (rlineto def 0 (- t))
225     (rlineto def (- w) h)
226     (rlineto def 0 t)
227     (closepath def)
228     (set-path-def bezier def)
229     bezier))
230
231 (define (square-beam width slope thick blot)
232   (let*
233       ((def (make <gnome-canvas-path-def>))
234        (y (* (- width) slope))
235        (props (make <gnome-canvas-bpath>
236                    #:parent (canvas-root)
237                    #:fill-color "black"
238                    #:outline-color "black"
239                    #:width-units 0.0)))
240     
241     (reset def)
242     (moveto def 0 0)
243     (lineto def width y)
244     (lineto def width (- y thick))
245     (lineto def 0 (- thick))
246     (lineto def 0 0)
247     (closepath def)
248     (set-path-def props def)
249     props))
250     
251 ;; two beziers
252 (define (bezier-sandwich lst thick)
253   (let* ((def (make <gnome-canvas-path-def>))
254          (bezier (make <gnome-canvas-bpath>
255                    #:parent (canvas-root)
256                    #:fill-color "black"
257                    #:outline-color "black"
258                    #:width-units thick
259                    #:join-style 'round)))
260
261     (reset def)
262
263     ;; FIXME: LST is pre-mangled for direct ps stack usage
264     ;; cl cr r l  0 1 2 3 
265     ;; cr cl l r  4 5 6 7
266     
267      (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
268      (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
269              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
270              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
271
272      (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
273      (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
274              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
275              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
276      (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
277
278     (closepath def)
279     (set-path-def bezier def)
280     bezier))
281
282 (define (char font i)
283   (text font (ly:font-index-to-charcode font i)))
284
285 ;; FIXME: naming
286 (define (filledbox breapth width depth height)
287   (make <gnome-canvas-rect>
288     #:parent (canvas-root)
289     #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
290     #:fill-color "black"
291     #:join-style 'miter))
292
293 (define (grob-cause grob)
294   grob)
295
296 ;; WTF is this in every backend?
297 (define (horizontal-line x1 x2 thickness)
298   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
299
300 (define (placebox x y expr)
301   (let ((item expr))
302     ;;(if item
303     ;; FIXME ugly hack to skip #unspecified ...
304     (if (and item (not (eq? item (if #f #f))))
305         (begin
306           (move item (* output-scale x) (* output-scale (- y)))
307           (affine-relative item output-scale 0 0 output-scale 0 0)
308           item)
309         #f)))
310
311 (define (dashed-line thick on off dx dy)
312   (draw-line thick 0 0 dx dy)) 
313
314 (define (draw-line thick fx fy tx ty)
315   (let*
316       ((def (make <gnome-canvas-path-def>))
317        (props (make <gnome-canvas-bpath>
318                    #:parent (canvas-root)
319                    #:fill-color "black"
320                    #:outline-color "black"
321                    #:width-units thick)))
322     
323     (reset def)
324     (moveto def fx (- fy))
325     (lineto def tx (- ty))
326     (set-path-def props def)
327     props))
328
329 (define (named-glyph font name)
330   (text font (ly:font-glyph-name-to-charcode font name)))
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 s)
364
365   (define (pango-font-name font)
366     (debugf "FONT-NAME:~S:~S\n" (ly:font-name font) (ly:font-design-size font))
367     (debugf "FONT-FAMILY:~S:~S\n" (font-family font) (otf-name-mangling font (font-family font)))
368     (otf-name-mangling font (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   (make <gnome-canvas-text>
396     #:parent (canvas-root)
397     ;; ugh, experimental placement corections
398     ;; #:x 0.0 #:y 0.0
399     #:x 0.0 #:y (if (music-font? font) 0.15 0.69)
400     #:anchor (if (music-font? font) 'west 'south-west)
401     #:font (pango-font-name font)
402     #:size-points (pango-font-size font)
403     #:size-set #t
404     #:text (if (integer? s)
405                (integer->utf8-string s)
406                (string->utf8-string s))))