]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
*** empty log message ***
[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       (define-method (reset (this <gnome-canvas-path-def>))
117         (gnome-canvas-path-def-reset (get-def this)))
118       
119       (define -set-path-def set-path-def)
120       (define -get-path-def get-path-def)
121       
122       (define-method (set-path-def (this <gnome-canvas-shape>)
123                                    (def <gnome-canvas-path-def>))
124         (-set-path-def this (get-def def)))
125       
126       (define-method (get-path-def (this <gnome-canvas-shape>))
127         (make <gnome-canvas-path-def> #:path-def (-get-path-def this)))))
128
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;;; globals
131
132 ;; junkme
133 (define system-origin '(0 . 0))
134
135 ;;; set by framework-gnome.scm
136 (define canvas-root #f)
137 (define output-scale #f)
138
139
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; helper functions
142
143 (define (stderr string . rest)
144   (apply format (cons (current-error-port) (cons string rest)))
145   (force-output (current-error-port)))
146
147 (define (debugf string . rest)
148   (if #f
149       (apply stderr (cons string rest))))
150
151 (define (utf8 i)
152   (cond
153    ((< i #x80) (list (integer->char i)))
154    ((< i #x800) (map integer->char
155                      (list (+ #xc0 (quotient i #x40))
156                            (+ #x80 (modulo i #x40)))))
157    ((< i #x10000)
158     (let ((x (quotient i #x1000))
159           (y (modulo i #x1000)))
160       (map integer->char
161            (list (+ #xe0 x)
162                  (+ #x80 (quotient y #x40))
163                  (+ #x80 (modulo y #x40))))))
164    (else FIXME)))
165   
166 (define (custom-utf8 i)
167   (if (< i 80)
168       (utf8 i)
169       (utf8 (+ #xee00 i))))
170
171 (define (string->utf8-string string)
172   (list->string
173    (apply append (map utf8 (map char->integer (string->list string))))))
174
175 (define (char->utf8-string char)
176   (list->string (utf8 (char->integer char))))
177
178 (define (draw-rectangle x1 y1 x2 y2 color width-units)
179   (make <gnome-canvas-rect>
180     #:parent (canvas-root) #:x1 x1 #:y1 y1 #:x2 x2 #:y2 y2
181     #:fill-color color #:width-units width-units))
182
183
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
185 ;;; stencil outputters
186 ;;;
187
188 ;;; catch-all for missing stuff
189 ;;; comment this out to see find out what functions you miss :-)
190 (define (dummy . foo) #f)
191 (map (lambda (x) (module-define! this-module x dummy))
192      (append
193       (ly:all-stencil-expressions)
194       (ly:all-output-backend-commands)))
195
196 ;; two beziers
197 (define (bezier-sandwich lst thick)
198   (let* ((def (make <gnome-canvas-path-def>))
199          (bezier (make <gnome-canvas-bpath>
200                    #:parent (canvas-root)
201                    #:fill-color "black"
202                    #:outline-color "black"
203                    #:width-units thick)))
204
205     (reset def)
206     
207     ;; cl cr r l  0 1 2 3 
208     ;; cr cl l r  4 5 6 7
209     
210      (moveto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
211      (curveto def (car (list-ref lst 0)) (- (cdr (list-ref lst 0)))
212              (car (list-ref lst 1)) (- (cdr (list-ref lst 1)))
213              (car (list-ref lst 2)) (- (cdr (list-ref lst 2))))
214
215      (lineto def (car (list-ref lst 7)) (- (cdr (list-ref lst 7))))
216      (curveto def (car (list-ref lst 4)) (- (cdr (list-ref lst 4)))
217              (car (list-ref lst 5)) (- (cdr (list-ref lst 5)))
218              (car (list-ref lst 6)) (- (cdr (list-ref lst 6))))
219      (lineto def (car (list-ref lst 3)) (- (cdr (list-ref lst 3))))
220
221     (closepath def)
222     (set-path-def bezier def)
223     bezier))
224
225 (define (char font i)
226   ;;(text font (utf8 i)))
227   (text font (list->string (list (integer->char i))))); (utf8 i)))
228
229 (define (placebox x y expr)
230   (debugf "item: ~S\n" expr)
231   (let ((item expr))
232     ;;(if item
233     ;; FIXME ugly hack to skip #unspecified ...
234     (if (and item (not (eq? item (if #f #f))))
235         (begin
236           (move item
237                 (* output-scale (+ (car system-origin) x))
238                 (* output-scale (- (car system-origin) y)))
239           (affine-relative item output-scale 0 0 output-scale 0 0)
240           item)
241         #f)))
242
243 (define (round-filled-box breapth width depth height blot-diameter)
244   ;; FIXME: no rounded corners on rectangle...
245   ;; FIXME: blot?
246   (draw-rectangle (- breapth) depth width (- height) "black" blot-diameter))
247
248 (define pango-font-name-alist
249   '(("GNU-LilyPond-feta-20" . "lilypond-feta, regular 32")
250     ("GNU-LilyPond-feta-nummer-5.5" . "lilypond-feta-nummer, regular 32")
251     ("GNU-LilyPond-feta-din-14" . "lilypond-feta-din, 25")
252     ("GNU-LilyPond-feta-14.14" . "lilypond-feta, regular 25")
253     ("GNU-LilyPond-feta-12.6" .  "lilypond-feta, regular 22")
254     ("GNU-LilyPond-feta-braces-f-90" . "LilyPond-feta-braces-f, 32")
255     ))
256
257 (define (pango-font-name font)
258   (let ((pango-font (assoc-get (ly:font-name font) pango-font-name-alist #f)))
259     (if pango-font
260         pango-font
261         (begin
262           (stderr "font-name: ~S\n" (ly:font-name font))
263           ;; TODO s/filename/file-name/
264           (stderr "font-filename: ~S\n" (ly:font-filename font))
265           (stderr "pango-font-size: ~S\n" (pango-font-size font))
266         "ecrm12"))))
267
268 (define (pango-font-size font)
269   (let* ((designsize (ly:font-design-size font))
270          (magnification (* (ly:font-magnification font)))
271          
272          ;; experimental sizing:
273          ;; where does factor come from?
274          ;;
275          ;; 0.435 * (12 / 20) = 0.261
276          ;; 2.8346456692913/ 0.261 = 10.86071137659501915708
277          ;;(ops (* 0.435 (/ 12 20) (* output-scale pixels-per-unit)))
278          ;; for size-points
279          (ops 2.61)
280          
281          (scaling (* ops magnification designsize)))
282     (debugf "OPS:~S\n" ops)
283     (debugf "scaling:~S\n" scaling)
284     (debugf "magnification:~S\n" magnification)
285     (debugf "design:~S\n" designsize)
286     
287     scaling))
288
289 ;;font-name: "GNU-LilyPond-feta-20"
290 ;;font-filename: "feta20"
291 ;;pango-font-name: "lilypond-feta, regular 32"
292 ;;OPS:2.61
293 ;;scaling:29.7046771653543
294 ;;magnification:0.569055118110236
295 ;;design:20.0
296
297 (define (text font string)
298   (make <gnome-canvas-text>
299     #:parent (canvas-root)
300
301     #:anchor 'west
302     #:x 0.0 #:y 0.0
303     
304     #:font (pango-font-name font)
305     
306     #:size-points (pango-font-size font)
307     ;;#:size ...
308     #:size-set #t
309     
310     ;;apparently no effect :-(
311     ;;#:scale 1.0
312     ;;#:scale-set #t
313     
314     #:fill-color "black"
315     #:text (if (string? string)
316                (string->utf8-string string)
317                (char->utf8-string (car string)))))
318
319 (define (filledbox a b c d)
320   (round-filled-box a b c d 0.001))
321
322 ;; WTF is this in every backend?
323 (define (horizontal-line x1 x2 thickness)
324   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
325
326 ;;(define (define-origin file line col)
327 ;;  (if (procedure? point-and-click)
328 ;;      (list 'location line col file)))
329
330 (define (grob-cause grob)
331   grob)