]> 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 ;;; 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/out-g16 && mkfontdir)
32        xset +fp $(pwd)/mf/out-g16
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 ;;; output-scale and font-size fun
180 ;; This used to be:
181 (define USED-TO-BE-OUTPUT-SCALE 2.83464566929134)
182 ;; However, it seems that we currently have:
183 (define 2.3.4-OUTPUT-SCALE 1.75729901757299)
184 ;; to go from ly-units to <MM/points/whatever?>
185 ;; Hmm, is this the source of font size problems wrt titling's right margin?
186
187 ;;(define pixels-per-unit 1.0)
188 ;;(define ARBITRARY-OUTPUT-SCALE 5)
189
190 ;; Anyway, for on-screen this does not matter: 2 * 2.5 looks fine
191 (define pixels-per-unit 2.0)
192 (define ARBITRARY-OUTPUT-SCALE 2.5)
193
194 ;;(define output-scale (* OUTPUT-SCALE pixels-per-unit))
195 (define output-scale (* ARBITRARY-OUTPUT-SCALE pixels-per-unit))
196
197
198 ;; helper functions -- sort this out
199 (define (stderr string . rest)
200   ;; debugging
201   (if #f
202       (begin
203         (apply format (cons (current-error-port) (cons string rest)))
204         (force-output (current-error-port)))))
205
206 (define (utf8 i)
207   (cond
208    ((< i #x80) (make-string 1 (integer->char i)))
209    ((< i #x800) (list->string
210                  (map integer->char
211                       (list (+ #xc0 (quotient i #x40))
212                             (+ #x80 (modulo i #x40))))))
213    ((< i #x10000)
214     (let ((x (quotient i #x1000))
215           (y (modulo i #x1000)))
216       (list->string
217        (map integer->char
218             (list (+ #xe0 x)
219                   (+ #x80 (quotient y #x40))
220                   (+ #x80 (modulo y #x40)))))))
221    (else FIXME)))
222   
223 (define (custom-utf8 i)
224   (if (< i 80)
225       (utf8 i)
226       (utf8 (+ #xee00 i))))
227
228 ;;; hmm?
229 (define (draw-rectangle x1 y1 x2 y2 color width-units)
230   (make <gnome-canvas-rect>
231     #:parent (root main-canvas) #:x1 x1 #:y1 y1 #:x2 x2 #:y2 y2
232     #:fill-color color #:width-units width-units))
233
234
235 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236 ;;;; stencil outputters
237 ;;;;
238
239 (define (char font i)
240   (text font (utf8 i)))
241
242 (define (placebox x y expr)
243   (stderr "item: ~S\n" expr)
244   (let ((item expr))
245     ;;(if item
246     ;; FIXME ugly hack to skip #unspecified ...
247     (if (and item (not (eq? item (if #f #f))))
248         (begin
249           (move item
250                 (* output-scale (+ (car system-origin) x))
251                 (* output-scale (- (car system-origin) y)))
252           (affine-relative item output-scale 0 0 output-scale 0 0)
253           
254           (gtype-instance-signal-connect item 'event item-event)
255           (if location
256               (hashq-set! item-locations item location))
257           item)
258         #f)))
259
260 (define (round-filled-box breapth width depth height blot-diameter)
261   ;; FIXME: no rounded corners on rectangle...
262   ;; FIXME: blot?
263   (draw-rectangle (- breapth) depth width (- height) "black" blot-diameter))
264
265 (define (pango-font-name font)
266   (cond
267    ((equal? (ly:font-name font) "GNU-LilyPond-feta-20")
268     "lilypond-feta, regular 32")
269    (else
270     "ecrm12")))
271     ;;(ly:font-name font))))
272     ;;(ly:font-filename font))))
273
274 (define (pango-font-size font)
275   (let* ((designsize (ly:font-design-size font))
276          (magnification (* (ly:font-magnification font)))
277          
278          ;; experimental sizing:
279          ;; where does factor come from?
280          ;;
281          ;; 0.435 * (12 / 20) = 0.261
282          ;; 2.8346456692913/ 0.261 = 10.86071137659501915708
283          ;;(ops (* 0.435 (/ 12 20) (* output-scale pixels-per-unit)))
284          ;; for size-points
285          (ops 2.61)
286          
287          (scaling (* ops magnification designsize)))
288     (stderr "OPS:~S\n" ops)
289     (stderr "scaling:~S\n" scaling)
290     (stderr "magnification:~S\n" magnification)
291     (stderr "design:~S\n" designsize)
292     
293     scaling))
294
295 ;;font-name: "GNU-LilyPond-feta-20"
296 ;;font-filename: "feta20"
297 ;;pango-font-name: "lilypond-feta, regular 32"
298 ;;OPS:2.61
299 ;;scaling:29.7046771653543
300 ;;magnification:0.569055118110236
301 ;;design:20.0
302
303 (define (text font string)
304   (stderr "font-name: ~S\n" (ly:font-name font))
305   ;; TODO s/filename/file-name/
306   (stderr "font-filename: ~S\n" (ly:font-filename font))
307   
308   (stderr "pango-font-name: ~S\n" (pango-font-name font))
309   (stderr "pango-font-size: ~S\n" (pango-font-size font))
310   (set!
311    text-items
312    (cons
313     (make <gnome-canvas-text>
314       #:parent (root main-canvas)
315       
316       ;; experimental text placement corrections.
317       ;; UGHR?  What happened to tex offsets?  south-west?
318       ;; is pango doing something 'smart' wrt baseline ?
319       #:anchor 'south-west
320       #:x 0.003 #:y 0.123
321       
322       ;;
323       ;;#:anchor 'west
324       ;;#:x 0.015 #:y -3.71
325       
326       #:font (pango-font-name font)
327       
328       #:size-points (pango-font-size font)
329       ;;#:size ...
330       #:size-set #t
331
332       ;;apparently no effect :-(
333       ;;#:scale 1.0
334       ;;#:scale-set #t
335       
336       #:fill-color "black"
337       #:text string)
338     text-items))
339   (car text-items))
340
341 (define (filledbox a b c d)
342   (round-filled-box a b c d 0.001))
343
344 ;; WTF is this in every backend?
345 (define (horizontal-line x1 x2 thickness)
346   ;;(let ((thickness 2))
347   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
348
349 ;; origin -- bad name
350 (define (define-origin file line col)
351   ;; ughr, why is this not passed as [part of] stencil object
352   (set! location (if (procedure? point-and-click)
353                      ;; duh, only silly string append
354                      ;; (point-and-click line col file)
355                      (list line col file)
356                      #f)))
357
358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359 ;;;; gnome stuff  --- move to framework-gnome
360 (define (dump-page number)
361   (if (or (not page-stencils)
362           (< number 0)
363           (>= number (vector-length page-stencils)))
364       (stderr "No such page: ~S\n" (1+ number))
365       
366       (let* ((old-canvas main-canvas)
367              (canvas (new-canvas)))
368         (set! page-number number)
369         
370         ;; no destroy method for gnome-canvas-text?
371         ;;(map destroy (gtk-container-get-children main-canvas))
372         ;;(map destroy text-items)
373
374         ;;Hmm
375         ;;(set! main-canvas canvas)
376         (set! text-items '())
377         (ly:outputter-dump-stencil output-canvas
378                                    (vector-ref page-stencils page-number))
379         
380         (if old-canvas (destroy old-canvas))
381         (add main-scrolled canvas)
382         (show canvas))))
383
384 (define x-editor #f)
385 (define (get-x-editor)
386   (if (not x-editor)
387       (set! x-editor (getenv "XEDITOR")))
388   x-editor)
389
390 (define ifs #f)
391 (define (get-ifs)
392   (if (not ifs)
393       (set! ifs (getenv "IFS")))
394   (if (not ifs)
395       (set! ifs "       "))
396   ifs)
397       
398 (define (spawn-editor location)
399   (let* ((line (car location))
400          (column (cadr location))
401          (file-name (caddr location))
402          (template (substring (get-x-editor) 0))
403          
404          ;; Adhere to %l %c %f?
405          (command
406           (regexp-substitute/global
407            #f "%l" (regexp-substitute/global
408                     #f "%c"
409                     (regexp-substitute/global
410                      #f "%f" template 'pre file-name 'post)
411                     'pre (number->string column)
412                     'post)
413            'pre (number->string line) 'post)))
414     
415     (stderr "spawning: ~s\n" command)
416     (if (= (primitive-fork) 0)
417         (let ((command-list (string-split command #\ )));; (get-ifs))))
418           (apply execlp command-list)
419           (primitive-exit)))))
420           
421 (define location-callback spawn-editor)
422
423 (define (item-event item event . data)
424   (case (gdk-event:type event)
425     ((enter-notify) (gobject-set-property item 'fill-color "red"))
426     ((leave-notify) (gobject-set-property item 'fill-color "black"))
427     ((button-press)
428      (let ((location (hashq-ref item-locations item #f)))
429        (if location
430            (location-callback location)
431            (stderr "no location\n"))))
432     ((2button-press) (gobject-set-property item 'fill-color "red")))
433   #t)
434
435 ;; TODO: one list per-page
436 (define text-items '())
437 (define (scale-canvas factor)
438   (set! pixels-per-unit (* pixels-per-unit factor))
439   (set-pixels-per-unit main-canvas pixels-per-unit)
440   (for-each
441    (lambda (x)
442      (let ((scale (gobject-get-property x 'scale))
443            (points (gobject-get-property x 'size-points)))
444        ;;(gobject-set-property x 'scale pixels-per-unit)
445        (gobject-set-property x 'size-points (* points factor))))
446      text-items))
447
448 (define (key-press-event item event . data)
449   (let ((keyval (gdk-event-key:keyval event))
450         (mods (gdk-event-key:modifiers event)))
451     (cond ((and (or (eq? keyval gdk:q)
452                     (eq? keyval gdk:w))
453                 (equal? mods '(control-mask modifier-mask)))
454            (gtk-main-quit))
455           ((and #t ;;(null? mods)
456                 (eq? keyval gdk:plus))
457            (scale-canvas 2))
458           ((and #t ;; (null? mods)
459                 (eq? keyval gdk:minus))
460            (scale-canvas 0.5))
461           ((or (eq? keyval gdk:Page-Up)
462                (eq? keyval gdk:BackSpace))
463            (dump-page (1- page-number)))
464           ((or (eq? keyval gdk:Page-Down)
465                (eq? keyval gdk:space))
466            (dump-page (1+ page-number))))
467     #f))
468
469 (define (papersize window paper)
470   (let* ((hsize (ly:output-def-lookup paper 'hsize))
471          (vsize (ly:output-def-lookup paper 'vsize))
472          (width (inexact->exact (ceiling (* output-scale hsize))))
473          (height (inexact->exact (ceiling (* output-scale vsize))))
474          (max-width (gdk-screen-width))
475          (max-height (gdk-screen-height))
476          (scrollbar-size 20))
477
478     ;; ughr: panels?
479     (set! max-height (- max-height 80))
480
481     (stderr "bookpaper-outputscale:~S\n" (ly:bookpaper-outputscale paper))
482     
483     ;; hmm?
484     ;;(set! OUTPUT-SCALE (ly:bookpaper-outputscale paper))
485     ;;(set! output-scale (* OUTPUT-SCALE pixels-per-unit))
486
487     ;; huh, *2?
488     
489     (set! window-width (min (+ scrollbar-size (* width 2)) max-width))
490     (set! window-height (min (+ button-height scrollbar-size (* height 2))
491                              max-height))
492     
493     (set! canvas-width width)
494     (set! canvas-height height)))
495
496
497 (define (new-canvas)
498   (let* ((canvas (make <gnome-canvas>))
499          (root (root canvas)))
500
501     (set-size-request canvas window-width window-height)
502     (set-scroll-region canvas 0 0 canvas-width canvas-height)
503     
504     (set-pixels-per-unit canvas pixels-per-unit)
505
506     (set! main-canvas canvas)
507     (draw-rectangle 0 0 canvas-width canvas-height "white" 0)
508     
509     canvas))
510
511 (define (main outputter bookpaper pages)
512   (let* ((window (make <gtk-window> #:type 'toplevel))
513          (button (make <gtk-button> #:label "Exit"))
514          (next (make <gtk-button> #:label "Next"))
515          (prev (make <gtk-button> #:label "Previous"))
516          (vbox (make <gtk-vbox> #:homogeneous #f))
517          (hbox (make <gtk-hbox> #:homogeneous #f))
518          (scrolled (make <gtk-scrolled-window>))
519          (canvas (new-canvas)))
520
521     (papersize window bookpaper)
522     (set-size-request window window-width window-height)
523     
524     (add window vbox)
525     (add vbox scrolled)
526     (add scrolled canvas)
527     
528     (add vbox hbox)
529     (set-size-request hbox window-width button-height)
530     (set-child-packing vbox hbox #f #f 0 'end)
531     
532     (set-child-packing hbox button #f #f 0 'end)
533     (set-size-request button (quotient window-width 2) button-height)
534     
535     (add hbox next)
536     (add hbox prev)
537     (add hbox button)
538     
539     (gtype-instance-signal-connect button 'clicked
540                                    (lambda (b) (gtk-main-quit)))
541     (gtype-instance-signal-connect next 'clicked
542                                    (lambda (b) (dump-page (1+ page-number))))
543     (gtype-instance-signal-connect prev 'clicked
544                                    (lambda (b) (dump-page (1- page-number))))
545
546     (gtype-instance-signal-connect window 'key-press-event key-press-event)
547     (show-all window)
548
549     ;; HMMM.  Make some class for these vars?
550     (set! main-window window)
551     (set! main-scrolled scrolled)
552     (set! output-canvas outputter)
553     (set! page-stencils pages)
554     
555     (dump-page 0)
556     
557     (gtk-main)))
558