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@xs4all.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 (if (or (nan? num) (inf? num))
70 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
72 (format #f "~f" (round4 num))))
74 (define (number-pair->string4 numpair)
75 (format #f "~f ~f" (round4 (car numpair)) (round4 (cdr numpair))))
77 (define (numbers->string4 numlist)
78 (string-join (map str4 numlist) " "))
81 (define-public (ps-string-def prefix key val)
82 (format #f "/ ~a~a (~a) def\n"
85 (escape-parentheses val)))
87 (define (ps-number-def prefix key val)
88 (let ((s (if (integer? val)
89 (ly:number->string val)
90 (ly:number->string (exact->inexact val)))))
91 (format #f "/~a~a ~a def\n"
93 (symbol->string key) s)))
97 ;;; Lily output interface, PostScript implementation --- cleanup and docme
101 (define (bezier-sandwich lst thick)
102 (format #f "~a ~a draw_bezier_sandwich"
103 (string-join (map number-pair->string4 lst) " ")
106 (define (char font i)
107 (format #f "~a (\\~a) show"
108 (ps-font-command font)
109 (ly:inexact->string i 8)))
111 (define (circle radius thick fill)
113 "~a ~f ~f draw_circle"
117 (round4 radius) (round4 thick)))
119 (define (dashed-line thick on off dx dy phase)
120 (format #f "~a ~a ~a [ ~a ~a ] ~a draw_dashed_line"
130 ;; what the heck is this interface ?
131 (define (dashed-slur thick on off l)
132 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
133 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
134 (string-join (map number-pair->string4 control-points) " "))
139 (define (dot x y radius)
140 (format #f " ~a draw_dot"
141 (numbers->string4 (list radius x y))))
143 (define (draw-line thick x1 y1 x2 y2)
144 (format #f "~a ~a ~a ~a ~a draw_line"
151 (define (embedded-ps string)
154 (define (glyph-string postscript-font-name
159 (define (glyph-spec w x y g)
160 (let ((prefix (if (string? g) "/" "")))
161 (format #f "~f ~f ~a~a"
168 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
172 "/~a ~a output-scale div selectfont
177 (string-join (map (lambda (x) (apply glyph-spec x))
178 (reverse w-x-y-named-glyphs)) "\n")
179 (length w-x-y-named-glyphs)))
182 (define (grob-cause offset grob)
183 (let* ((cause (ly:grob-property grob 'cause))
184 (music-origin (if (ly:stream-event? cause)
185 (ly:event-property cause 'origin))))
186 (if (not (ly:input-location? music-origin))
188 (let* ((location (ly:input-file-line-char-column music-origin))
189 (raw-file (car location))
190 (file (if (is-absolute? raw-file)
192 (string-append (ly-getcwd) "/" raw-file)))
193 (x-ext (ly:grob-extent grob grob X))
194 (y-ext (ly:grob-extent grob grob Y)))
196 (if (and (< 0 (interval-length x-ext))
197 (< 0 (interval-length y-ext)))
198 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
199 (+ (car offset) (car x-ext))
200 (+ (cdr offset) (car y-ext))
201 (+ (car offset) (cdr x-ext))
202 (+ (cdr offset) (cdr y-ext))
207 ;; backslash is interpreted by GS.
208 (string-regexp-substitute "\\\\" "/"
209 (string-regexp-substitute " " "%20" file))
215 (define (lily-def key val)
216 (let ((prefix "lilypondlayout"))
218 (substring key 0 (min (string-length prefix) (string-length key)))
220 (format "/~a { ~a } bind def\n" key val)
221 (format "/~a (~a) def\n" key val))))
223 (define (named-glyph font glyph)
224 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
225 (ps-font-command font)
231 (define (placebox x y s)
239 (define (polygon points blot-diameter filled?)
240 (format #f "~a ~a ~a ~a draw_polygon"
241 (if filled? "true" "false")
242 (numbers->string4 points)
243 (number->string (- (/ (length points) 2) 1))
244 (str4 blot-diameter)))
246 (define (repeat-slash width slope beam-thickness)
247 (define (euclidean-length x y)
248 (sqrt (+ (* x x) (* y y))))
250 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
251 (height (* width slope)))
252 (format #f "~a draw_repeat_slash"
253 (numbers->string4 (list x-width width height)))))
256 (define (round-filled-box left right bottom top blotdiam)
257 (let* ((halfblot (/ blotdiam 2))
258 (x (- halfblot left))
259 (width (- right (+ halfblot x)))
260 (y (- halfblot bottom))
261 (height (- top (+ halfblot y))))
262 (format #f "~a draw_round_box"
264 (list width height x y blotdiam)))))
266 ;; save current color on stack and set new color
267 (define (setcolor r g b)
268 (format #f "gsave ~a setrgbcolor\n"
269 (numbers->string4 (list r g b))))
271 ;; restore color from stack
272 (define (resetcolor) "grestore \n")
274 ;; rotation around given point
275 (define (setrotation ang x y)
276 (format "gsave ~a translate ~a rotate ~a translate\n"
277 (numbers->string4 (list x y))
279 (numbers->string4 (list (* -1 x) (* -1 y)))))
281 (define (resetrotation ang x y)
285 (define (text font s)
286 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
287 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
289 (let* ((space-length (cdar (ly:text-dimension font " ")))
290 (space-move (string-append (number->string space-length)
291 ;; how much precision do we need here?
293 (out-vec (decode-byte-string s)))
296 (ps-font-command font) " "
304 (string-append "/" (symbol->string sym) " glyphshow")))
310 (define (url-link url x y)
311 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
318 (define (utf-8-string pango-font-description string)
319 (ly:warning (_ "utf-8-string encountered in PS backend")))
322 (define (zigzag-line centre? zzw zzh thick dx dy)
323 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
324 (if centre? "true" "false")
332 (define (path thickness exps)
333 (define (convert-path-exps exps)
340 ((memq head '(rmoveto rlineto lineto moveto)) 2)
341 ((memq head '(rcurveto curveto)) 6)
343 (args (take rest arity))
346 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
347 (cons (format "~a ~a "
348 (string-join (map (lambda (x) (format "~a " x)) args) " ")
350 (convert-path-exps (drop rest arity))))
355 "1 setlinecap ~a setlinewidth\n~a stroke"
357 (string-join (convert-path-exps exps) " ")))