1 ;;;; output-ps.scm -- implement Scheme output interface for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 ;;;; Note: currently misused as testbed for titles with markup, see
9 ;;;; input/test/title-markup.ly
12 ;;;; * %% Papersize in (header ...)
13 ;;;; * text setting, kerning.
14 ;;;; * document output-interface
16 (define-module (scm output-ps)
19 ;; JUNK this -- see lily.scm: ly:all-output-backend-commands
51 ;;; helper functions, not part of output interface
55 (define (escape-parentheses s)
56 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
58 (define (ps-encoding text)
59 (escape-parentheses text))
62 (/ (round (* 100 num)) 100))
65 (/ (round (* 10000 num)) 10000))
68 (format #f "~f" (round4 num)))
70 (define (number-pair->string4 numpair)
71 (format #f "~f ~f" (round4 (car numpair)) (round4 (cdr numpair))))
73 (define (numbers->string4 numlist)
74 (string-join (map str4 numlist) " "))
77 (define-public (ps-string-def prefix key val)
78 (format #f "/ ~a~a (~a) def\n"
81 (escape-parentheses val)))
83 (define (ps-number-def prefix key val)
84 (let ((s (if (integer? val)
85 (ly:number->string val)
86 (ly:number->string (exact->inexact val)))))
87 (format #f "/~a~a ~a def\n"
89 (symbol->string key) s)))
93 ;;; Lily output interface, PostScript implementation --- cleanup and docme
97 (define (bezier-sandwich lst thick)
98 (format #f "~a ~a draw_bezier_sandwich"
99 (string-join (map number-pair->string4 lst) " ")
102 (define (char font i)
103 (format #f "~a (\\~a) show"
104 (ps-font-command font)
105 (ly:inexact->string i 8)))
107 (define (circle radius thick fill)
109 "~a ~f ~f draw_circle"
113 (round4 radius) (round4 thick)))
115 (define (dashed-line thick on off dx dy phase)
116 (format #f "~a ~a ~a [ ~a ~a ] ~a draw_dashed_line"
126 ;; what the heck is this interface ?
127 (define (dashed-slur thick on off l)
128 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
129 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
130 (string-join (map number-pair->string4 control-points) " "))
135 (define (dot x y radius)
136 (format #f " ~a draw_dot"
137 (numbers->string4 (list radius x y))))
139 (define (draw-line thick x1 y1 x2 y2)
140 (format #f "~a ~a ~a ~a ~a draw_line"
147 (define (embedded-ps string)
150 (define (glyph-string postscript-font-name
155 (define (glyph-spec w x y g)
156 (let ((prefix (if (string? g) "/" "")))
157 (format #f "~f ~f ~a~a"
164 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
168 "/~a ~a output-scale div selectfont
173 (string-join (map (lambda (x) (apply glyph-spec x))
174 (reverse w-x-y-named-glyphs)) "\n")
175 (length w-x-y-named-glyphs)))
178 (define (grob-cause offset grob)
179 (let* ((cause (ly:grob-property grob 'cause))
180 (music-origin (if (ly:stream-event? cause)
181 (ly:event-property cause 'origin))))
182 (if (not (ly:input-location? music-origin))
184 (let* ((location (ly:input-file-line-char-column music-origin))
185 (raw-file (car location))
186 (file (if (is-absolute? raw-file)
188 (string-append (ly-getcwd) "/" raw-file)))
189 (x-ext (ly:grob-extent grob grob X))
190 (y-ext (ly:grob-extent grob grob Y)))
192 (if (and (< 0 (interval-length x-ext))
193 (< 0 (interval-length y-ext)))
194 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
195 (+ (car offset) (car x-ext))
196 (+ (cdr offset) (car y-ext))
197 (+ (car offset) (cdr x-ext))
198 (+ (cdr offset) (cdr y-ext))
203 ;; backslash is interpreted by GS.
204 (string-regexp-substitute "\\\\" "/"
205 (string-regexp-substitute " " "%20" file))
211 (define (lily-def key val)
212 (let ((prefix "lilypondlayout"))
214 (substring key 0 (min (string-length prefix) (string-length key)))
216 (format "/~a { ~a } bind def\n" key val)
217 (format "/~a (~a) def\n" key val))))
219 (define (named-glyph font glyph)
220 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
221 (ps-font-command font)
227 (define (placebox x y s)
235 (define (polygon points blot-diameter filled?)
236 (format #f "~a ~a ~a ~a draw_polygon"
237 (if filled? "true" "false")
238 (numbers->string4 points)
239 (number->string (- (/ (length points) 2) 1))
240 (str4 blot-diameter)))
242 (define (repeat-slash width slope beam-thickness)
243 (define (euclidean-length x y)
244 (sqrt (+ (* x x) (* y y))))
246 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
247 (height (* width slope)))
248 (format #f "~a draw_repeat_slash"
249 (numbers->string4 (list x-width width height)))))
251 ;; restore color from stack
252 (define (resetcolor) "setrgbcolor\n")
255 (define (round-filled-box left right bottom top blotdiam)
256 (let* ((halfblot (/ blotdiam 2))
257 (x (- halfblot left))
258 (width (- right (+ halfblot x)))
259 (y (- halfblot bottom))
260 (height (- top (+ halfblot y))))
261 (format #f "~a draw_round_box"
263 (list width height x y blotdiam)))))
265 ;; save current color on stack and set new color
266 (define (setcolor r g b)
267 (format #f "currentrgbcolor ~a setrgbcolor\n"
268 (numbers->string4 (list r g b))))
270 ;; rotation around given point
271 (define (setrotation ang x y)
272 (format "gsave ~a translate ~a rotate ~a translate\n"
273 (numbers->string4 (list x y))
275 (numbers->string4 (list (* -1 x) (* -1 y)))))
277 (define (resetrotation ang x y)
281 (define (text font s)
282 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
283 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
285 (let* ((space-length (cdar (ly:text-dimension font " ")))
286 (space-move (string-append (number->string space-length)
287 ;; how much precision do we need here?
289 (out-vec (decode-byte-string s)))
292 (ps-font-command font) " "
300 (string-append "/" (symbol->string sym) " glyphshow")))
306 (define (url-link url x y)
307 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
314 (define (utf-8-string pango-font-description string)
315 (ly:warning (_ "utf-8-string encountered in PS backend")))
318 (define (zigzag-line centre? zzw zzh thick dx dy)
319 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
320 (if centre? "true" "false")
328 (define (path thickness exps)
329 (define (convert-path-exps exps)
336 ((memq head '(rmoveto rlineto lineto moveto)) 2)
337 ((memq head '(rcurveto curveto)) 6)
339 (args (take rest arity))
342 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
343 (cons (format "~a ~a "
344 (string-join (map (lambda (x) (format "~a " x)) args) " ")
346 (convert-path-exps (drop rest arity))))
351 "1 setlinecap ~a setlinewidth\n~a stroke"
353 (string-join (convert-path-exps exps) " ")))