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