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