]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
bdb49bc385a1581a96441ef408910f667d3f323b
[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 </fontconfig>
48 EOF
49 "
50 ;;;     or copy all your .pfa/.pfb's to ~/.fonts if your fontconfig
51 ;;;     already looks there for fonts.  Check if it works by doing:
52 "
53 fc-list | grep -i lily
54 "
55 ;;;
56 ;;;   * Setup environment
57 "
58 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
59 export LD_LIBRARY_PATH=$HOME/usr/pkg/pango/lib:$HOME/usr/pkg/g-wrap/lib:$HOME/usr/pkg/guile-gnome/lib:$LD_LIBRARY_PATH
60 export XEDITOR='/usr/bin/emacsclient --no-wait +%l:%c %f'
61 "
62 ;;;  * Also for GNOME point-and-click, you need to set XEDITOR and add
63 "
64 #(ly:set-point-and-click 'line-column)
65 "
66 ;;;    to your .ly.
67 ;;;
68 ;;;  * Run lily:
69 "
70 lilypond -fgnome input/simple-song.ly
71 "
72 ;;; point-and-click: (mouse-1) click on a graphical object;
73 ;;; grob-property-list: (mouse-3) click on a graphical object.
74
75 (debug-enable 'backtrace)
76
77 (define-module (scm output-gnome))
78 (define this-module (current-module))
79
80 (use-modules
81  (guile)
82  (srfi srfi-13)
83  (lily)
84  (gnome gtk))
85
86
87 ;; The name of the module will change to `canvas' rsn
88 (if (resolve-module '(gnome gw canvas))
89     (use-modules (gnome gw canvas))
90     (use-modules (gnome gw libgnomecanvas)))
91
92
93 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;;; Wrappers from guile-gnome TLA
95 ;;; guile-gnome-devel@gnu.org--2004
96 ;;; http://arch.gna.org/guile-gnome/archive-2004
97 ;;;
98 ;;; janneke@gnu.org--2004-gnome
99 ;;; http://lilypond.org/~janneke/{arch}/2004-gnome
100 ;;;
101 (if (not (defined? '<gnome-canvas-path-def>))
102     (begin
103       (define-class <gnome-canvas-path-def> (<gobject>)
104         (closure #:init-value (gnome-canvas-path-def-new)
105                  #:init-keyword #:path-def
106                  #:getter get-def #:setter set-def))
107       
108       (define-method (moveto (this <gnome-canvas-path-def>) x y)
109         (gnome-canvas-path-def-moveto (get-def this) x y))
110       (define-method (curveto (this <gnome-canvas-path-def>) x1 y1 x2 y2 x3 y3)
111         (gnome-canvas-path-def-curveto (get-def this)  x1 y1 x2 y2 x3 y3))
112       (define-method (lineto (this <gnome-canvas-path-def>) x y)
113         (gnome-canvas-path-def-lineto (get-def this) x y))
114       (define-method (closepath (this <gnome-canvas-path-def>))
115         (gnome-canvas-path-def-closepath (get-def this)))
116       
117       (define -set-path-def set-path-def)
118       (define -get-path-def get-path-def)
119       
120       (define-method (set-path-def (this <gnome-canvas-shape>)
121                                    (def <gnome-canvas-path-def>))
122         (-set-path-def this (get-def def)))
123       
124       (define-method (get-path-def (this <gnome-canvas-shape>))
125         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
126
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 ;;; globals
129
130 ;; junkme
131 (define system-origin '(0 . 0))
132
133 ;;; set by framework-gnome.scm
134 (define canvas-root #f)
135 (define output-scale #f)
136
137
138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139 ;; helper functions
140
141 (define (stderr string . rest)
142   (apply format (cons (current-error-port) (cons string rest)))
143   (force-output (current-error-port)))
144
145 (define (debugf string . rest)
146   (if #f
147       (apply stderr (cons string rest))))
148
149 (define (utf8 i)
150   (cond
151    ((< i #x80) (list (integer->char i)))
152    ((< i #x800) (map integer->char
153                      (list (+ #xc0 (quotient i #x40))
154                            (+ #x80 (modulo i #x40)))))
155    ((< i #x10000)
156     (let ((x (quotient i #x1000))
157           (y (modulo i #x1000)))
158       (map integer->char
159            (list (+ #xe0 x)
160                  (+ #x80 (quotient y #x40))
161                  (+ #x80 (modulo y #x40))))))
162    (else FIXME)))
163   
164 (define (custom-utf8 i)
165   (if (< i 80)
166       (utf8 i)
167       (utf8 (+ #xee00 i))))
168
169 (define (string->utf8-string string)
170   (list->string
171    (apply append (map utf8 (map char->integer (string->list string))))))
172
173 (define (char->utf8-string char)
174   (list->string (utf8 (char->integer char))))
175
176 (define (draw-rectangle x1 y1 x2 y2 color width-units)
177   (make <gnome-canvas-rect>
178     #:parent (canvas-root) #:x1 x1 #:y1 y1 #:x2 x2 #:y2 y2
179     #:fill-color color #:width-units width-units))
180
181
182 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;;; stencil outputters
184 ;;;
185
186 ;;; catch-all for missing stuff
187 ;;; comment this out to see find out what functions you miss :-)
188 (define (dummy . foo) #f)
189 (map (lambda (x) (module-define! this-module x dummy))
190      (append
191       (ly:all-stencil-expressions)
192       (ly:all-output-backend-commands)))
193
194 ;; two beziers
195 (define (bezier-sandwich lst thick)
196   (let* ((def (make <gnome-canvas-path-def>))
197          (bezier (make <gnome-canvas-bpath>
198                    #:parent (canvas-root)
199                    #:fill-color "black"
200                    #:outline-color "black"
201                    #:width-units thick)))
202     
203     ;; cl cr r l  0 1 2 3 
204     ;; cr cl l r  4 5 6 7
205     
206      (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
207      (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
208              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
209              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
210
211      (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
212      (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
213              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
214              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
215      (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
216
217     (closepath def)
218     (set-path-def bezier def)
219     bezier))
220
221 (define (char font i)
222   (text font (utf8 i)))
223
224 (define (placebox x y expr)
225   (debugf "item: ~S\n" expr)
226   (let ((item expr))
227     ;;(if item
228     ;; FIXME ugly hack to skip #unspecified ...
229     (if (and item (not (eq? item (if #f #f))))
230         (begin
231           (move item
232                 (* output-scale (+ (car system-origin) x))
233                 (* output-scale (- (car system-origin) y)))
234           (affine-relative item output-scale 0 0 output-scale 0 0)
235           item)
236         #f)))
237
238 (define (round-filled-box breapth width depth height blot-diameter)
239   ;; FIXME: no rounded corners on rectangle...
240   ;; FIXME: blot?
241   (draw-rectangle (- breapth) depth width (- height) "black" blot-diameter))
242
243 (define (pango-font-name font)
244   (cond
245    ((equal? (ly:font-name font) "GNU-LilyPond-feta-20")
246     "lilypond-feta, regular 32")
247    (else
248     ;; FIXME
249     "ecrm12")))
250     ;;(ly:font-name font))))
251     ;;(ly:font-filename font))))
252
253 (define (pango-font-size font)
254   (let* ((designsize (ly:font-design-size font))
255          (magnification (* (ly:font-magnification font)))
256          
257          ;; experimental sizing:
258          ;; where does factor come from?
259          ;;
260          ;; 0.435 * (12 / 20) = 0.261
261          ;; 2.8346456692913/ 0.261 = 10.86071137659501915708
262          ;;(ops (* 0.435 (/ 12 20) (* output-scale pixels-per-unit)))
263          ;; for size-points
264          (ops 2.61)
265          
266          (scaling (* ops magnification designsize)))
267     (debugf "OPS:~S\n" ops)
268     (debugf "scaling:~S\n" scaling)
269     (debugf "magnification:~S\n" magnification)
270     (debugf "design:~S\n" designsize)
271     
272     scaling))
273
274 ;;font-name: "GNU-LilyPond-feta-20"
275 ;;font-filename: "feta20"
276 ;;pango-font-name: "lilypond-feta, regular 32"
277 ;;OPS:2.61
278 ;;scaling:29.7046771653543
279 ;;magnification:0.569055118110236
280 ;;design:20.0
281
282 (define (text font string)
283   (stderr "font-name: ~S\n" (ly:font-name font))
284   ;; TODO s/filename/file-name/
285   (stderr "font-filename: ~S\n" (ly:font-filename font))
286   
287   (stderr "pango-font-name: ~S\n" (pango-font-name font))
288   (stderr "pango-font-size: ~S\n" (pango-font-size font))
289   
290   (make <gnome-canvas-text>
291     #:parent (canvas-root)
292
293     #:anchor 'west
294     #:x 0.0 #:y 0.0
295     
296     #:font (pango-font-name font)
297     
298     #:size-points (pango-font-size font)
299     ;;#:size ...
300     #:size-set #t
301     
302     ;;apparently no effect :-(
303     ;;#:scale 1.0
304     ;;#:scale-set #t
305     
306     #:fill-color "black"
307     #:text (if (string? string)
308                (string->utf8-string string)
309                (char->utf8-string (car string)))))
310
311 (define (filledbox a b c d)
312   (round-filled-box a b c d 0.001))
313
314 ;; WTF is this in every backend?
315 (define (horizontal-line x1 x2 thickness)
316   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
317
318 ;;(define (define-origin file line col)
319 ;;  (if (procedure? point-and-click)
320 ;;      (list 'location line col file)))
321
322 (define (grob-cause grob)
323   grob)