]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
(beam): add function.
[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:
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 "
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
88
89 ;; The name of the module will change to `canvas' rsn
90 (if (resolve-module '(gnome gw canvas))
91     (use-modules (gnome gw canvas))
92     (use-modules (gnome gw libgnomecanvas)))
93
94
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 ;;; Wrappers from guile-gnome TLA
97 ;;; guile-gnome-devel@gnu.org--2004
98 ;;; http://arch.gna.org/guile-gnome/archive-2004
99 ;;;
100 ;;; janneke@gnu.org--2004-gnome
101 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
102 ;;;
103 (if (not (defined? '<gnome-canvas-path-def>))
104     (begin
105       (define-class <gnome-canvas-path-def> (<gobject>)
106         (closure #:init-value (gnome-canvas-path-def-new)
107                  #:init-keyword #:path-def
108                  #:getter get-def #:setter set-def))
109       
110       (define-method (moveto (this <gnome-canvas-path-def>) x y)
111         (gnome-canvas-path-def-moveto (get-def this) x y))
112       (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
113         (gnome-canvas-path-def-curveto (get-def this)  x1 y1 x2 y2 x3 y3))
114       (define-method (lineto (this <gnome-canvas-path-def>) x y)
115         (gnome-canvas-path-def-lineto (get-def this) x y))
116       (define-method (closepath (this <gnome-canvas-path-def>))
117         (gnome-canvas-path-def-closepath (get-def this)))
118       (define-method (reset (this <gnome-canvas-path-def>))
119         (gnome-canvas-path-def-reset (get-def this)))
120       
121       (define -set-path-def set-path-def)
122       (define -get-path-def get-path-def)
123       
124       (define-method (set-path-def (this <gnome-canvas-shape>)
125                                    (def <gnome-canvas-path-def>))
126         (-set-path-def this (get-def def)))
127       
128       (define-method (get-path-def (this <gnome-canvas-shape>))
129         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
130
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;;; globals
133
134 ;; junkme
135 (define system-origin '(0 . 0))
136
137 ;;; set by framework-gnome.scm
138 (define canvas-root #f)
139 (define output-scale #f)
140
141
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143 ;; helper functions
144
145 (define (stderr string . rest)
146   (apply format (cons (current-error-port) (cons string rest)))
147   (force-output (current-error-port)))
148
149 (define (debugf string . rest)
150   (if #f
151       (apply stderr (cons string rest))))
152
153 (define (utf8 i)
154   (cond
155    ((< i #x80) (list (integer->char i)))
156    ((< i #x800) (map integer->char
157                      (list (+ #xc0 (quotient i #x40))
158                            (+ #x80 (modulo i #x40)))))
159    ((< i #x10000)
160     (let ((x (quotient i #x1000))
161           (y (modulo i #x1000)))
162       (map integer->char
163            (list (+ #xe0 x)
164                  (+ #x80 (quotient y #x40))
165                  (+ #x80 (modulo y #x40))))))
166    (else FIXME)))
167   
168 (define (custom-utf8 i)
169   (if (< i 80)
170       (utf8 i)
171       (utf8 (+ #xee00 i))))
172
173 (define (string->utf8-string string)
174   (list->string
175    (apply append (map utf8 (map char->integer (string->list string))))))
176
177 (define (char->utf8-string char)
178   (list->string (utf8 (char->integer char))))
179
180 (define (draw-rectangle x1 y1 x2 y2 color width-units)
181   (make <gnome-canvas-rect>
182     #:parent (canvas-root) #:x1 x1 #:y1 y1 #:x2 x2 #:y2 y2
183     #:fill-color color #:width-units width-units))
184
185
186 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187 ;;; stencil outputters
188 ;;;
189
190 ;;; catch-all for missing stuff
191 ;;; comment this out to see find out what functions you miss :-)
192 (define (dummy . foo) #f)
193 (map (lambda (x) (module-define! this-module x dummy))
194      (append
195       (ly:all-stencil-expressions)
196       (ly:all-output-backend-commands)))
197
198 ;; two beziers
199 (define (bezier-sandwich lst thick)
200   (let* ((def (make <gnome-canvas-path-def>))
201          (bezier (make <gnome-canvas-bpath>
202                    #:parent (canvas-root)
203                    #:fill-color "black"
204                    #:outline-color "black"
205                    #:width-units thick)))
206
207     (reset def)
208     
209     ;; cl cr r l  0 1 2 3 
210     ;; cr cl l r  4 5 6 7
211     
212      (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
213      (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
214              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
215              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
216
217      (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
218      (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
219              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
220              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
221      (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
222
223     (closepath def)
224     (set-path-def bezier def)
225     bezier))
226
227 (define (char font i)
228   (text font (utf8 i)))
229
230 (define (placebox x y expr)
231   (debugf "item: ~S\n" expr)
232   (let ((item expr))
233     ;;(if item
234     ;; FIXME ugly hack to skip #unspecified ...
235     (if (and item (not (eq? item (if #f #f))))
236         (begin
237           (move item
238                 (* output-scale (+ (car system-origin) x))
239                 (* output-scale (- (car system-origin) y)))
240           (affine-relative item output-scale 0 0 output-scale 0 0)
241           item)
242         #f)))
243
244 (define (beam width slope thick blot)
245   (let*
246       ((def (make <gnome-canvas-path-def>))
247        (props (make <gnome-canvas-bpath>
248                    #:parent (canvas-root)
249                    #:fill-color "black"
250                    #:outline-color "black"
251                    #:width-units 0.0)))
252     
253     (reset def)
254     (moveto def 0 0)
255     (lineto def width (* width slope))
256     (lineto def width (- thick))
257     (lineto def 0 (- thick))
258     (lineto def 0 0)
259     (closepath def)
260     (set-path-def props def)
261     props))
262     
263
264 (define (dashed-line thick on off dx dy)
265   (draw-line thick 0 0 dx dy)) 
266
267 (define (draw-line thick fx fy tx ty)
268   (let*
269       ((def (make <gnome-canvas-path-def>))
270        (props (make <gnome-canvas-bpath>
271                    #:parent (canvas-root)
272                    #:fill-color "black"
273                    #:outline-color "black"
274                    #:width-units thick)))
275     
276     (reset def)
277     (moveto def fx (- fy))
278     (lineto def tx (- ty))
279     (set-path-def props def)
280     props))
281     
282
283 (define (round-filled-box breapth width depth height blot-diameter)
284   ;; FIXME: no rounded corners on rectangle...
285   ;; FIXME: blot?
286   (draw-rectangle (- breapth) depth width (- height) "black" blot-diameter))
287
288 (define (pango-font-name font)
289   (let ((name (ly:font-name font)))
290     (if name
291         (regexp-substitute/global #f "^GNU-(.*)-[.0-9]*$" name 'pre 1 'post)
292         (begin
293           (stderr "font-name: ~S\n" (ly:font-name font))
294           ;; TODO s/filename/file-name/
295           (stderr "font-filename: ~S\n" (ly:font-filename font))
296           (stderr "pango-font-size: ~S\n" (pango-font-size font))
297           "ecrm12"))))
298
299 (define (pango-font-size font)
300   (let* ((designsize (ly:font-design-size font))
301          (magnification (* (ly:font-magnification font)))
302          
303          ;; experimental sizing:
304          ;; where does factor come from?
305          ;;
306          ;; 0.435 * (12 / 20) = 0.261
307          ;; 2.8346456692913/ 0.261 = 10.86071137659501915708
308          ;;(ops (* 0.435 (/ 12 20) (* output-scale pixels-per-unit)))
309          ;; for size-points
310          (ops 2.61)
311          
312          (scaling (* ops magnification designsize)))
313     (debugf "OPS:~S\n" ops)
314     (debugf "scaling:~S\n" scaling)
315     (debugf "magnification:~S\n" magnification)
316     (debugf "design:~S\n" designsize)
317     
318     scaling))
319
320 ;;font-name: "GNU-LilyPond-feta-20"
321 ;;font-filename: "feta20"
322 ;;pango-font-name: "lilypond-feta, regular 32"
323 ;;OPS:2.61
324 ;;scaling:29.7046771653543
325 ;;magnification:0.569055118110236
326 ;;design:20.0
327
328 (define (text font string)
329   (make <gnome-canvas-text>
330     #:parent (canvas-root)
331
332     #:anchor 'west
333     #:x 0.0 #:y 0.0
334     
335     #:font (pango-font-name font)
336     
337     #:size-points (pango-font-size font)
338     ;;#:size ...
339     #:size-set #t
340     
341     ;;apparently no effect :-(
342     ;;#:scale 1.0
343     ;;#:scale-set #t
344     
345     #:fill-color "black"
346     #:text (if (string? string)
347                (string->utf8-string string)
348                (char->utf8-string (car string)))))
349
350 (define (filledbox a b c d)
351   (round-filled-box a b c d 0.001))
352
353 ;; WTF is this in every backend?
354 (define (horizontal-line x1 x2 thickness)
355   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
356
357 ;;(define (define-origin file line col)
358 ;;  (if (procedure? point-and-click)
359 ;;      (list 'location line col file)))
360
361 (define (grob-cause grob)
362   grob)