]> git.donarmstrong.com Git - lilypond.git/blob - scm/output-ps.scm
(text_stencil): don't translate glyphs in
[lilypond.git] / scm / output-ps.scm
1 ;;;; output-ps.scm -- implement Scheme output interface for PostScript
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;; 
5 ;;;; (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;                 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 ;;;; Note: currently misused as testbed for titles with markup, see
9 ;;;;       input/test/title-markup.ly
10 ;;;; 
11 ;;;; TODO:
12 ;;;;   * %% Papersize in (header ...)
13 ;;;;   * text setting, kerning.
14 ;;;;   * document output-interface
15
16 (define-module (scm output-ps)
17   #:re-export (quote)
18
19   ;; JUNK this -- see lily.scm: ly:all-output-backend-commands
20   #:export (unknown
21             blank
22             circle
23             dot
24             dashed-slur
25             char
26             setcolor
27             resetcolor
28             named-glyph
29             dashed-line
30             zigzag-line
31             comment
32             repeat-slash
33             placebox
34             bezier-sandwich
35             embedded-ps
36             round-filled-box
37             text
38             polygon
39             draw-line
40             no-origin))
41
42
43 (use-modules (guile)
44              (ice-9 regex)
45              (srfi srfi-1)
46              (srfi srfi-13)
47              (scm framework-ps)
48              (lily))
49
50 ;;; helper functions, not part of output interface
51 (define (escape-parentheses s)
52   (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
53
54 (define (ps-encoding text)
55   (escape-parentheses text))
56
57 ;; FIXME: lily-def
58 (define-public (ps-string-def prefix key val)
59   (string-append "/" prefix (symbol->string key) " ("
60                  (escape-parentheses val)
61                  ") def\n"))
62
63
64 (define (ps-number-def prefix key val)
65   (let ((s (if (integer? val)
66                (ly:number->string val)
67                (ly:number->string (exact->inexact val)))))
68     (string-append "/" prefix (symbol->string key) " " s " def\n")))
69
70
71 ;;;
72 ;;; Lily output interface, PostScript implementation --- cleanup and docme
73 ;;;
74
75 ;; two beziers
76 (define (bezier-sandwich lst thick)
77   (string-append 
78    (string-join (map ly:number-pair->string lst) " ")
79    " "
80    (ly:number->string thick)
81    " draw_bezier_sandwich"))
82
83 (define (char font i)
84   (string-append 
85    (ps-font-command font) " setfont " 
86    "(\\" (ly:inexact->string i 8) ") show"))
87
88 (define (circle radius thick fill)
89   (format
90    "~a ~a ~a draw_circle" radius thick
91    (if fill
92        "true "
93        "false ")))
94
95 (define (dashed-line thick on off dx dy)
96   (string-append 
97    (ly:number->string dx) " "
98    (ly:number->string dy) " "
99    (ly:number->string thick)
100    " [ "
101    (ly:number->string on) " "
102    (ly:number->string off)
103    " ] 0 draw_dashed_line"))
104
105 ;; what the heck is this interface ?
106 (define (dashed-slur thick on off l)
107   (string-append 
108    (string-join (map ly:number-pair->string l) " ")
109    " "
110    (ly:number->string thick) 
111    " [ "
112    (ly:number->string on)
113    " "   
114    (ly:number->string off)
115    " ] 0 draw_dashed_slur"))
116
117 (define (dot x y radius)
118   (string-append
119    " "
120    (ly:numbers->string
121     (list x y radius)) " draw_dot"))
122
123 (define (draw-line thick x1 y1 x2 y2)
124   (string-append 
125    "1 setlinecap 1 setlinejoin "
126    (ly:number->string thick) " setlinewidth "
127    (ly:number->string x1) " "
128    (ly:number->string y1) " moveto "
129    (ly:number->string x2) " "
130    (ly:number->string y2) " lineto stroke"))
131
132 (define (embedded-ps string)
133   string)
134
135
136 (define (glyph-string
137          postscript-font-name
138          size cid?
139          x-y-named-glyphs)
140
141   (format #f "gsave 1 output-scale div 1 output-scale div scale
142   /~a ~a ~a scalefont setfont\n~a grestore"
143           postscript-font-name
144           (if cid?
145               " /CIDFont findresource "
146               " findfont")
147           
148           size
149           (apply
150            string-append
151            (map (lambda  (item)
152                   (let*
153                       ((x (car item))
154                        (y (cadr item))
155                        (g (caddr item))
156                        (prefix (if  (string? g) "/" "")))
157
158                     (if (and (= 0.0 x)
159                              (= 0.0 y))
160                         (format #f " ~a~a glyphshow\n" prefix g)
161                         (format #f " ~a ~a rmoveto ~a~a glyphshow\n"
162                                 x y
163                                 prefix
164                                 g))))
165                 x-y-named-glyphs))))
166
167 (define (grob-cause offset grob)
168   (let* ((cause (ly:grob-property grob 'cause))
169          (music-origin (if (ly:music? cause)
170                            (ly:music-property cause 'origin))))
171     (if (not (ly:input-location? music-origin))
172         ""
173         (let* ((location (ly:input-file-line-char-column music-origin))
174                (raw-file (car location))
175                (file (if (is-absolute? raw-file)
176                          raw-file
177                          (string-append (ly-getcwd) "/" raw-file)))
178                (x-ext (ly:grob-extent grob grob X))
179                (y-ext (ly:grob-extent grob grob Y)))
180
181           (if (and (< 0 (interval-length x-ext))
182                    (< 0 (interval-length y-ext)))
183               (format "~a ~a ~a ~a (textedit://~a:~a:~a:~a) mark_URI\n"
184                       (+ (car offset) (car x-ext))
185                       (+ (cdr offset) (car y-ext))
186                       (+ (car offset) (cdr x-ext))
187                       (+ (cdr offset) (cdr y-ext))
188
189                       ;; TODO
190                       ;; full escaping.
191                       (string-regexp-substitute " " "%20" file)
192                       (cadr location)
193                       (caddr location)
194                       (cadddr location))
195               "")))))
196
197 (define (lily-def key val)
198   (let ((prefix "lilypondlayout"))
199     (if (string=?
200          (substring key 0 (min (string-length prefix) (string-length key)))
201          prefix)
202         (string-append "/" key " {" val "} bind def\n")
203         (string-append "/" key " (" val ") def\n"))))
204
205 (define (named-glyph font glyph)
206   (string-append 
207    (ps-font-command font) " setfont " 
208    "/" glyph " glyphshow "))
209
210 (define (no-origin)
211   "")
212
213 (define (placebox x y s) 
214   (string-append 
215    (ly:number->string x) " " (ly:number->string y) " { " s " } place-box\n"))
216
217 (define (polygon points blotdiameter filled?)
218   (string-append
219    (ly:numbers->string points) " "
220    (ly:number->string (/ (length points) 2)) " "
221    (ly:number->string blotdiameter)
222    (if filled? " true " " false ")
223    " draw_polygon"))
224
225 (define (repeat-slash wid slope thick)
226   (string-append
227    (ly:numbers->string (list wid slope thick))
228    " draw_repeat_slash"))
229
230 ;; restore color from stack
231 (define (resetcolor)
232   (string-append "setrgbcolor\n"))
233
234 (define (round-filled-box x y width height blotdiam)
235   (string-append
236    (ly:numbers->string
237     (list x y width height blotdiam)) " draw_round_box"))
238
239 ;; save current color on stack and set new color
240 (define (setcolor r g b)
241   (string-append "currentrgbcolor "
242   (ly:numbers->string (list r g b))
243   " setrgbcolor\n"))
244
245 (define (text font s)
246   ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
247   ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
248   
249   (let* ((space-length (cdar (ly:text-dimension font " ")))
250          (space-move (string-append (number->string space-length)
251                                     " 0.0 rmoveto "))
252          (out-vec (decode-byte-string s)))
253
254     (string-append
255      (ps-font-command font) " setfont "
256      (string-join
257       (vector->list
258        (vector-for-each
259         
260         (lambda (sym)
261           (if (eq? sym 'space)
262               space-move
263               (string-append "/" (symbol->string sym) " glyphshow")))
264         out-vec))))))
265
266 (define (unknown) 
267   "\n unknown\n")
268
269 (define (url-link url x y)
270   (format "~a ~a ~a ~a (~a) mark_URI"
271           (car x)
272           (car y)
273           (cdr x)
274           (cdr y)
275           url))
276
277 (define (utf-8-string pango-font-description string)
278   (ly:warning (_ "utf-8-string encountered in PS backend")))
279
280
281
282 (define (zigzag-line centre? zzw zzh thick dx dy)
283   (string-append
284    (if centre? "true" "false") " "
285    (ly:number->string zzw) " "
286    (ly:number->string zzh) " "
287    (ly:number->string thick) " "
288    "0 0 "
289    (ly:number->string dx) " "
290    (ly:number->string dy)
291    " draw_zigzag_line"))