]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-gnome.scm
* scm/output-gnome.scm:
[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--tng TLA, possibly Janneke's if you have libffi-3.4.
25 ;;;   * guile-gnome TLA
26 ;;;   * pango CVS (ie, > 2004-06-12)
27 ;;;
28 ;;; See also: guile-gtk-general@gnu.org
29
30 ;;; Try it
31 ;;;
32 ;;;   * Install gnome/gtk development stuff
33 ;;;
34 ;;;   * Install g-wrap and guile-gnome, see buildscripts/guile-gnome.sh
35 ;;;  
36 ;;;   * Setup environment
37 "
38 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
39 export LD_LIBRARY_PATH=$HOME/usr/pkg/g-wrap/lib:$HOME/usr/pkg/guile-gnome/lib
40 export XEDITOR='/usr/bin/emacsclient --no-wait +%l:%c %f'
41 "
42 ;;;  * Also for GNOME point-and-click, you need to set XEDITOR and add
43 "
44 #(ly:set-point-and-click 'line-column)
45 "
46 ;;;    to your .ly; then click an object on the canvas.
47 ;;;
48 ;;;  * Run lily:
49 "
50 lilypond-bin -fgnome input/simple-song.ly
51 "
52
53
54 (debug-enable 'backtrace)
55
56 (define-module (scm output-gnome))
57 (define this-module (current-module))
58
59 (use-modules
60  (guile)
61  (srfi srfi-13)
62  (lily)
63  (gnome gtk))
64
65
66 ;; The name of the module will change to `canvas' rsn
67 (if (resolve-module '(gnome gw canvas))
68     (use-modules (gnome gw canvas))
69     (use-modules (gnome gw libgnomecanvas)))
70
71
72 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 ;;; globals
74
75 ;; junkme
76 (define system-origin '(0 . 0))
77
78 ;;; set by framework-gnome.scm
79 (define canvas-root #f)
80 (define output-scale #f)
81
82
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;; helper functions
85
86 (define (stderr string . rest)
87   ;; debugging
88   (if #f
89       (begin
90         (apply format (cons (current-error-port) (cons string rest)))
91         (force-output (current-error-port)))))
92
93 (define (utf8 i)
94   (cond
95    ((< i #x80) (make-string 1 (integer->char i)))
96    ((< i #x800) (list->string
97                  (map integer->char
98                       (list (+ #xc0 (quotient i #x40))
99                             (+ #x80 (modulo i #x40))))))
100    ((< i #x10000)
101     (let ((x (quotient i #x1000))
102           (y (modulo i #x1000)))
103       (list->string
104        (map integer->char
105             (list (+ #xe0 x)
106                   (+ #x80 (quotient y #x40))
107                   (+ #x80 (modulo y #x40)))))))
108    (else FIXME)))
109   
110 (define (custom-utf8 i)
111   (if (< i 80)
112       (utf8 i)
113       (utf8 (+ #xee00 i))))
114
115 (define (draw-rectangle x1 y1 x2 y2 color width-units)
116   (make <gnome-canvas-rect>
117     #:parent (canvas-root) #:x1 x1 #:y1 y1 #:x2 x2 #:y2 y2
118     #:fill-color color #:width-units width-units))
119
120
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;;; stencil outputters
123 ;;;
124
125 ;;; catch-all for missing stuff
126 ;;; comment this out to see find out what functions you miss :-)
127 (define (dummy . foo) #f)
128 (map (lambda (x) (module-define! this-module x dummy))
129      (append
130       (ly:all-stencil-expressions)
131       (ly:all-output-backend-commands)))
132
133
134
135 (define (char font i)
136   (text font (utf8 i)))
137
138 (define (placebox x y expr)
139   (stderr "item: ~S\n" expr)
140   (let ((item expr))
141     ;;(if item
142     ;; FIXME ugly hack to skip #unspecified ...
143     (if (and item (not (eq? item (if #f #f))))
144         (begin
145           (move item
146                 (* output-scale (+ (car system-origin) x))
147                 (* output-scale (- (car system-origin) y)))
148           (affine-relative item output-scale 0 0 output-scale 0 0)
149           item)
150         #f)))
151
152 (define (round-filled-box breapth width depth height blot-diameter)
153   ;; FIXME: no rounded corners on rectangle...
154   ;; FIXME: blot?
155   (draw-rectangle (- breapth) depth width (- height) "black" blot-diameter))
156
157 (define (pango-font-name font)
158   (cond
159    ((equal? (ly:font-name font) "GNU-LilyPond-feta-20")
160     "lilypond-feta, regular 32")
161    (else
162     ;; FIXME
163     "ecrm12")))
164     ;;(ly:font-name font))))
165     ;;(ly:font-filename font))))
166
167 (define (pango-font-size font)
168   (let* ((designsize (ly:font-design-size font))
169          (magnification (* (ly:font-magnification font)))
170          
171          ;; experimental sizing:
172          ;; where does factor come from?
173          ;;
174          ;; 0.435 * (12 / 20) = 0.261
175          ;; 2.8346456692913/ 0.261 = 10.86071137659501915708
176          ;;(ops (* 0.435 (/ 12 20) (* output-scale pixels-per-unit)))
177          ;; for size-points
178          (ops 2.61)
179          
180          (scaling (* ops magnification designsize)))
181     (stderr "OPS:~S\n" ops)
182     (stderr "scaling:~S\n" scaling)
183     (stderr "magnification:~S\n" magnification)
184     (stderr "design:~S\n" designsize)
185     
186     scaling))
187
188 ;;font-name: "GNU-LilyPond-feta-20"
189 ;;font-filename: "feta20"
190 ;;pango-font-name: "lilypond-feta, regular 32"
191 ;;OPS:2.61
192 ;;scaling:29.7046771653543
193 ;;magnification:0.569055118110236
194 ;;design:20.0
195
196 (define (text font string)
197   (stderr "font-name: ~S\n" (ly:font-name font))
198   ;; TODO s/filename/file-name/
199   (stderr "font-filename: ~S\n" (ly:font-filename font))
200   
201   (stderr "pango-font-name: ~S\n" (pango-font-name font))
202   (stderr "pango-font-size: ~S\n" (pango-font-size font))
203   
204   (make <gnome-canvas-text>
205     #:parent (canvas-root)
206     
207     ;; experimental text placement corrections.
208     ;; UGHR?  What happened to tex offsets?  south-west?
209     ;; is pango doing something 'smart' wrt baseline ?
210     #:anchor 'south-west
211     #:x 0.003 #:y 0.123
212     
213     ;;#:anchor 'west
214     ;;#:x 0.015 #:y -3.71
215     
216     #:font (pango-font-name font)
217     
218     #:size-points (pango-font-size font)
219     ;;#:size ...
220     #:size-set #t
221     
222     ;;apparently no effect :-(
223     ;;#:scale 1.0
224     ;;#:scale-set #t
225     
226     #:fill-color "black"
227     #:text string))
228
229 (define (filledbox a b c d)
230   (round-filled-box a b c d 0.001))
231
232 ;; WTF is this in every backend?
233 (define (horizontal-line x1 x2 thickness)
234   (filledbox (- x1) (- x2 x1) (* .5 thickness) (* .5 thickness)))
235
236 ;;(define (define-origin file line col)
237 ;;  (if (procedure? point-and-click)
238 ;;      (list 'location line col file)))
239
240 (define (grob-cause grob)
241   grob)