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