]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
561726558b450addae120a95b422084207a1987d
[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
8 ;;; HIP -- hack in progress
9 ;;;
10 ;;; You need:
11 ;;;
12 ;;;   * guile-1.6.4 (NOT CVS)
13 ;;;   * Rotty's g-wrap--tng, possibly Janneke's if you have libffi-3.4.
14 ;;;   * lilypond branch: lilypond_2_3_2b; the framework-* backend
15 ;;;     loads output-gnome.scm at startup, which seems to break g-wrapped
16 ;;;     goops.
17 ;;;
18 ;;; see also: guile-gtk-general@gnu.org
19 ;;;
20 ;;; Try it
21 ;;;
22 ;;;   * Install g-wrap, guile-gnome (see script below)
23 ;;;  
24 ;;;   * Use latin1 encoding for gnome backend, do
25 ;;;       make -C mf clean
26 ;;;       make -C mf ENCODING_FILE=$(kpsewhich cork.enc)
27 ;;;       (cd mf/out && mkfontdir)
28 ;;;       xset +fp $(pwd)/mf/out
29 ;;;
30 ;;;   * Setup PATHs:
31
32 "
33 # do not use guile CVS:
34 export PATH=/usr/bin/:$PATH
35 # use g-wrap and guile-gnome from usr/pkg
36 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
37 export LD_LIBRARY_PATH=$HOME/usr/pkg/g-wrap/lib:$HOME/usr/pkg/guile-gnome/lib
38 "
39
40 ;;;  * Set XEDITOR and add
41 ;;;     #(ly:set-point-and-click 'line-column)
42 ;;;    to your .ly to get point-and-click
43 ;;;
44 ;;;  * Run lily: lilypond-bin -fgnome input/simple-song.ly
45 ;;;
46 ;;;
47 ;;;      todo: hmm --output-base broken?
48 ;;;   ### cd mf && mftrace --encoding=$(kpsewhich cork.enc) --autotrace --output-base=feta-cork-20 feta20.mf && mv feta20.pfa out
49
50 ;;; TODO:
51 ;;;  * pango+feta font (see archives gtk-i18n-list@gnome.org and
52 ;;;    lilypond-devel)
53 ;;;    - wait for/help with pango 1.6
54 ;;;    - convert feta to OpenType (CFF) or TrueType (fontforge?)
55 ;;;    - hack feta20/feta20.pfa?:
56 ;;;  * font, canvas, scaling?
57 ;;;  * implement missing stencil functions
58 ;;;  * implement missing commands
59 ;;;  * user-interface, keybindings
60 ;;;  * cleanups: (too many) global vars
61
62 ;;; Note: this install information is volatile
63 ;;;       you'll probably want to pull all from
64 ;;;       from guile-gnome-devel@gnu.org--2004 soon
65 ;;;   
66
67 "
68 #!/bin/bash
69
70 set -ex
71
72 rm -rf test
73 mkdir test
74 cd test
75
76 ## 1.  install gnome-devel (Debian/unstable: apt-get install gnome-devel)
77
78 ## 2.  *** NOTE: use guile-1.6 for g-wrap and guile-gnome ***
79 ##### using GUILE CVS g-wrap/guile-gnome is experimental (read: segfaults)
80 PATH=/usr/bin:$PATH
81
82
83 ## 3.  get g-wrap 2.0
84 tla register-archive a.rottmann@gmx.at--2004-main http://people.debian.org/~rotty/arch/a.rottmann@gmx.at/2004-main || true
85
86 rm -rf g-wrap
87 ## tla get a.rottmann@gmx.at--2004-main/g-wrap--tng g-wrap
88 ## pull latest g-wrap from janneke -- this step is probably no longer
89 ## necessary when you read this
90 tla register-archive janneke@gnu.org--2004-gnome http://lilypond.org/~janneke/{arch}/2004-gnome || true
91 tla get janneke@gnu.org--2004-gnome/g-wrap--janneke g-wrap
92 cd g-wrap
93
94 rm -rf $HOME/usr/pkg/g-wrap
95 AUTOMAKE=automake-1.8 AUTOCONF=autoconf2.50 sh autogen.sh --noconfigure
96 mkdir =build
97 cd =build
98 ../configure --prefix=$HOME/usr/pkg/g-wrap
99 make install
100
101 # cp srfi-34.scm from CVS head ?  --hwn
102 (cd $HOME/usr/pkg/g-wrap/share/guile/site
103  mv srfi-34.scm srfi-34.scm-g-wrap
104  cp $HOME/usr/pkg/guile/share/guile-1.7/srfi/srfi-34.scm .)
105
106 cd ../..
107
108 ## 4.  get guile-gnome
109 tla register-archive guile-gnome-devel@gnu.org--2004 http://people.debian.org/~rotty/arch/guile-gnome-devel@gnu.org/2004/ || true
110 rm -rf guile-gnome
111 tla get guile-gnome-devel@gnu.org--2004/dists--dev guile-gnome
112 cd guile-gnome
113 tla build-config -r configs/gnu.org/dev
114 cd src
115
116 ## 5.  get the gnome canvas module
117 tla get guile-gnome-devel@gnu.org--2004/libgnomecanvas--dev libgnomecanvas
118
119 ## pull latest defs from janneke -- this step is probably no longer
120 ## necessary when you read this
121 ## tla register-archive janneke@gnu.org--2004-gnome http://lilypond.org/~janneke/{arch}/2004-gnome || true
122 ## rm -rf defs
123 ## tla get janneke@gnu.org--2004-gnome/defs--janneke defs
124
125 rm -rf $HOME/usr/pkg/guile-gnome
126 AUTOMAKE=automake-1.8 AUTOCONF=autoconf2.50 sh autogen.sh --noconfigure
127 mkdir ../=build
128 cd ../=build
129
130 export GUILE_LOAD_PATH=$HOME/usr/pkg/g-wrap/share/guile/site:$GUILE_LOAD_PATH
131 export LD_LIBRARY_PATH=$HOME/usr/pkg/g-wrap/lib:$LD_LIBRARY_PATH
132 export PKG_CONFIG_PATH=$HOME/usr/pkg/g-wrap/lib/pkgconfig:$PKG_CONFIG_PATH
133
134 ../src/configure --prefix=$HOME/usr/pkg/guile-gnome
135
136 G_WRAP_MODULE_DIR=$HOME/usr/pkg/g-wrap/share/guile/site make install
137
138 export GUILE_LOAD_PATH=$HOME/usr/pkg/guile-gnome/share/guile:$GUILE_LOAD_PATH
139 export LD_LIBRARY_PATH=$HOME/usr/pkg/guile-gnome/lib:$LD_LIBRARY_PATH
140 guile -s ../src/libgnomecanvas/examples/canvas.scm
141
142
143 # simple test
144 guile -s ../src/libgnomecanvas/examples/canvas.scm
145
146 "
147
148 (debug-enable 'backtrace)
149
150 (define-module (scm output-gnome))
151
152 (define this-module (current-module))
153
154 (use-modules
155  (guile)
156  (ice-9 regex)
157  (srfi srfi-13)
158  (lily)
159  (gnome gtk)
160  (gnome gtk gdk-event)
161  ;; the name of the module will change to canvas rsn
162  ;;(gnome gw libgnomecanvas))
163  (gnome gw canvas))
164
165
166 ;;; Lily output interface --- fix silly names and docme
167
168 "
169  The output interface has functions for
170   * formatting stencils, and
171   * output commands
172
173  Stencils:
174  beam
175  bezier-sandwich
176  bracket
177  ...
178
179  Commands:
180  define-fonts
181  header
182  placebox
183  ...
184
185
186  The Bare minimum interface for \score { \notes c } } should
187  implement:
188
189     INTERFACE-output-expression
190     char
191     filledbox
192     placebox
193
194  and should intercept:
195 "
196
197 (define (dummy . foo) #f)
198
199 ;; minimal intercept list:
200 (define output-interface-intercept
201   '(comment
202     define-fonts
203     end-output
204     header
205     header-end
206     lily-def
207     no-origin
208     output-scopes
209     start-page
210     stop-page
211     start-system
212     stop-system))
213
214 (map (lambda (x) (module-define! this-module x dummy))
215      output-interface-intercept)
216
217 (define-public (gnome-output-expression expr port)
218   (display (dispatch expr) port))
219
220 (define (dispatch expr)
221   (if (pair? expr)
222       (let ((keyword (car expr)))
223         (cond
224          ((eq? keyword 'some-func) "")
225          ;;((eq? keyword 'placebox) (dispatch (cadddr expr)))
226          (else
227           (if (module-defined? this-module keyword)
228               (apply (eval keyword this-module) (cdr expr))
229               (begin
230                 (display
231                  (string-append "undefined: " (symbol->string keyword) "\n"))
232                 "")))))
233       expr))
234
235 ;;; Global vars
236 (define main-window #f)
237 (define main-scrolled #f)
238 (define main-canvas #f)
239 (define canvas-root #f)
240 (define page-number 0)
241
242 (define page-stencils #f)
243 (define output-canvas #f)
244
245 (define system-origin '(0 . 0))
246
247 ;; UGHr
248 (define item-locations (make-hash-table 31))
249 (define location #f)
250
251 (define canvas-width 400)
252 (define canvas-height
253   (inexact->exact (round (* 1.42 canvas-width))))
254
255 (define font-paper #f)
256
257 ;;(define pixels-per-unit 1.0)
258 (define pixels-per-unit 2.0)
259
260 ;; TODO: use canvas scaling, use output-scale for paper/canvas dimensions?
261 ;;(define output-scale (* 2 2.83464566929134))
262 ;;(define output-scale 2.83464566929134)
263 (define OUTPUT-SCALE 2.83464566929134)
264 (define output-scale (* OUTPUT-SCALE pixels-per-unit))
265 ;;(define output-scale 1)
266
267 ;; helper functions
268 (define (stderr string . rest)
269   (apply format (cons (current-error-port) (cons string rest)))
270   (force-output (current-error-port)))
271
272 (define (utf8 i)
273   (cond
274    ((< i #x80) (make-string 1 (integer->char i)))
275    ((< i #x800) (list->string
276                  (map integer->char
277                       (list (+ #xc0 (quotient i #x40))
278                             (+ #x80 (modulo i #x40))))))
279    ((< i #x10000)
280     (let ((x (quotient i #x1000))
281           (y (modulo i #x1000)))
282       (list->string
283        (map integer->char
284             (list (+ #xe0 x)
285                   (+ #x80 (quotient y #x40))
286                   (+ #x80 (modulo y #x40)))))))
287    (else FIXME)))
288   
289 (define (custom-utf8 i)
290   (if (< i 80)
291       (utf8 i)
292       (utf8 (+ #xee00 i))))
293
294 (define x-editor #f)
295 (define (get-x-editor)
296   (if (not x-editor)
297       (set! x-editor (getenv "XEDITOR")))
298   x-editor)
299
300 (define ifs #f)
301 (define (get-ifs)
302   (if (not ifs)
303       (set! ifs (getenv "IFS")))
304   (if (not ifs)
305       (set! ifs "       "))
306   ifs)
307       
308 (define (spawn-editor location)
309   (let* ((line (car location))
310          (column (cadr location))
311          (file-name (caddr location))
312          (template (substring (get-x-editor) 0))
313          
314          ;; Adhere to %l %c %f?
315          (command
316           (regexp-substitute/global
317            #f "%l" (regexp-substitute/global
318                     #f "%c"
319                     (regexp-substitute/global
320                      #f "%f" template 'pre file-name 'post)
321                     'pre (number->string column)
322                     'post)
323            'pre (number->string line) 'post)))
324     
325     (stderr "spawning: ~s\n" command)
326     (if (= (primitive-fork) 0)
327         (let ((command-list (string-split command #\ )));; (get-ifs))))
328           (apply execlp command-list)
329           (primitive-exit)))))
330           
331 (define location-callback spawn-editor)
332
333 (define (item-event item event . data)
334   (case (gdk-event:type event)
335     ((enter-notify) (gobject-set-property item 'fill-color "white"))
336     ((leave-notify) (gobject-set-property item 'fill-color "black"))
337     ((button-press)
338      (let ((location (hashq-ref item-locations item #f)))
339        (if location
340            (location-callback location)
341            (stderr "no location\n"))))
342     ((2button-press) (gobject-set-property item 'fill-color "red")))
343   #t)
344
345 ;; TODO: one list per-page
346 (define text-items '())
347 (define (scale-canvas factor)
348   (set! pixels-per-unit (* pixels-per-unit factor))
349   (set-pixels-per-unit main-canvas pixels-per-unit)
350   (for-each
351    (lambda (x)
352      (let ((scale (gobject-get-property x 'scale))
353            (points (gobject-get-property x 'size-points)))
354        ;;(gobject-set-property x 'scale pixels-per-unit)
355        (gobject-set-property x 'size-points (* points factor))))
356      text-items))
357
358 (define (key-press-event item event . data)
359   (let ((keyval (gdk-event-key:keyval event))
360         (mods (gdk-event-key:modifiers event)))
361     (cond ((and (or (eq? keyval gdk:q)
362                     (eq? keyval gdk:w))
363                 (equal? mods '(control-mask modifier-mask)))
364            (gtk-main-quit))
365           ((and #t ;;(null? mods)
366                 (eq? keyval gdk:plus))
367            (scale-canvas 2))
368           ((and #t ;; (null? mods)
369                 (eq? keyval gdk:minus))
370            (scale-canvas 0.5))
371           ((or (eq? keyval gdk:Page-Up)
372                (eq? keyval gdk:BackSpace))
373            (dump-page (1- page-number)))
374           ((or (eq? keyval gdk:Page-Down)
375                (eq? keyval gdk:space))
376            (dump-page (1+ page-number))))
377     #f))
378
379 (define (char font i)
380   (text font (utf8 i)))
381
382 (define (placebox x y expr)
383   (stderr "item: ~S\n" expr)
384   (let ((item expr))
385     ;;(if item
386     ;; FIXME ugly hack to skip #unspecified ...
387     (if (and item (not (eq? item (if #f #f))))
388         (begin
389           (move item
390                 (* output-scale (+ (car system-origin) x))
391                 (* output-scale (- (car system-origin) y)))
392           (affine-relative item output-scale 0 0 output-scale 0 0)
393           
394           (gtype-instance-signal-connect item 'event item-event)
395           (if location
396               (hashq-set! item-locations item location))
397           item)
398         #f)))
399
400 (define (round-filled-box breapth width depth height blot-diameter)
401   ;; FIXME: no rounded corners on rectangle
402   (make <gnome-canvas-rect>
403     #:parent canvas-root
404     #:x1 (- breapth) #:y1 depth #:x2 width #:y2 (- height)
405     #:fill-color "black" #:width-units blot-diameter))
406
407 ;;(define (fontify font expr)
408 ;;  #f)
409
410 (define (main outputter pages)
411   (let* ((window (make <gtk-window> #:type 'toplevel))
412          (button (make <gtk-button> #:label "Exit"))
413          (next (make <gtk-button> #:label "Next"))
414          (prev (make <gtk-button> #:label "Previous"))
415          (canvas (make <gnome-canvas>))
416          (vbox (make <gtk-vbox> #:homogeneous #f))
417          (hbox (make <gtk-hbox> #:homogeneous #f))
418          (scrolled (make <gtk-scrolled-window>)))
419
420     (add window vbox)
421     (add vbox scrolled)
422     (add scrolled canvas)
423
424     ;;(set-size-request button canvas-width 20)
425     ;;(add vbox button)
426     ;;(set-child-packing vbox button #f #f 0 'end)
427     
428     (add vbox hbox)
429     (set-size-request hbox canvas-width 25)
430     (set-child-packing vbox hbox #f #f 0 'end)
431     
432     (set-child-packing hbox button #f #f 0 'end)
433     ;;(set-size-request next 40 25)
434     ;;(set-size-request prev 40 25)
435     (set-size-request button (/ canvas-width 2) 25)
436     
437     (add hbox next)
438     (add hbox prev)
439     (add hbox button)
440     
441     
442     (gtype-instance-signal-connect button 'clicked
443                                    (lambda (b) (gtk-main-quit)))
444     (gtype-instance-signal-connect next 'clicked
445                                    (lambda (b) (dump-page (1+ page-number))))
446     (gtype-instance-signal-connect prev 'clicked
447                                    (lambda (b) (dump-page (1- page-number))))
448     
449     ;; papersize
450     (set-size-request canvas canvas-width canvas-height)
451     (set-scroll-region canvas 0 0 2000 4000)
452     (set-pixels-per-unit canvas pixels-per-unit)
453     
454     (gtype-instance-signal-connect window 'key-press-event key-press-event)
455     (show-all window)
456
457     ;; HMMM
458     (set! canvas-root (root canvas))
459     (set! main-canvas canvas)
460     (set! main-window window)
461     (set! output-canvas outputter)
462     (set! page-stencils pages)
463     (set! main-scrolled scrolled)
464     
465     (dump-page 0)
466     (gtk-main)))
467
468 (define (pango-font-name font)
469   (cond
470    ((equal? (ly:font-name font) "GNU-LilyPond-feta-20")
471     "lilypond-feta, regular 32")
472    (else
473     (ly:font-filename font))))
474
475 (define (pango-font-size font)
476   (let* ((designsize (ly:font-design-size font))
477          (magnification (* (ly:font-magnification font)))
478          ;;(ops (ly:paper-lookup paper 'outputscale))
479          ;;(ops (* pixels-per-unit OUTPUT-SCALE))
480          ;;(ops (* pixels-per-unit pixels-per-unit))
481          (ops (* (/ 12 20) (* pixels-per-unit pixels-per-unit)))
482          (scaling (* ops magnification designsize)))
483     scaling))
484
485 (define (text font string)
486   (stderr "font-name: ~S\n" (ly:font-name font))
487   ;; TODO s/filename/file-name/
488   (stderr "font-filename: ~S\n" (ly:font-filename font))
489   
490   (stderr "pango-font-name: ~S\n" (pango-font-name font))
491   (stderr "pango-font-size: ~S\n" (pango-font-size font))
492   (set!
493    text-items
494    (cons
495     (make <gnome-canvas-text>
496       #:parent canvas-root
497       #:x 0 #:y 0
498       ;;    #:font "new century schoolbook, i bold 20"
499       #:font (pango-font-name font)
500       ;; #:size-points 12
501       #:size-points (pango-font-size font)
502       ;;#:size (pango-font-size font)
503       #:size-set #t
504
505       ;;apparently no effect :-(
506       ;;#:scale 1.0
507       ;;#:scale-set #t
508       
509       #:fill-color "black"
510       #:text string
511       #:anchor 'west)
512     text-items))
513   (car text-items))
514
515 (define (filledbox a b c d)
516   (round-filled-box a b c d 0.001))
517
518 ;; WTF is this in every backend?
519 (define (horizontal-line x1 x2 thickness)
520   ;;(let ((thickness 2))
521   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
522
523 (define (start-system origin . rest)
524   (set! system-origin origin))
525
526 ;; origin -- bad name
527 (define (define-origin file line col)
528   ;; ughr, why is this not passed as [part of] stencil object
529   (set! location (if (procedure? point-and-click)
530                      ;; duh, only silly string append
531                      ;; (point-and-click line col file)
532                      (list line col file)
533                      #f)))
534
535 (define (dump-page number)
536   (if (or (not page-stencils)
537           (< number 0)
538           (>= number (vector-length page-stencils)))
539       (stderr "No such page: ~S\n" (1+ number))
540       (begin
541         (set! page-number number)
542
543         ;; no destroy method for gnome-canvas-text?
544         ;;(map destroy (gtk-container-get-children main-canvas))
545         ;;(map destroy text-items)
546
547         ;; UGHR - destroying the whole canvas....
548         (if (and main-canvas
549                  (not (null? text-items)))
550             (let* ((canvas (make <gnome-canvas>))
551                    (root (root canvas)))
552               
553               (destroy main-canvas)
554               (add main-scrolled canvas)
555
556               ;; papersize
557               (set-size-request canvas canvas-width canvas-height)
558               (set-scroll-region canvas 0 0 2000 4000)
559               (set-pixels-per-unit canvas pixels-per-unit)
560               (show canvas)
561               
562               (set! main-canvas canvas)
563               (set! canvas-root root)
564               (set! text-items '())))
565         
566         (ly:outputter-dump-stencil output-canvas
567                                    (vector-ref page-stencils page-number)))))
568