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"))
71 (if (ly:get-option 'strict-infinity-checking)
74 (format #f "~f" (round4 num))))
76 (define (number-pair->string4 numpair)
77 (string-append (str4 (car numpair))
79 (str4 (cdr numpair))))
81 (define (numbers->string4 numlist)
82 (string-join (map str4 numlist) " "))
85 (define-public (ps-string-def prefix key val)
86 (format #f "/ ~a~a (~a) def\n"
89 (escape-parentheses val)))
91 (define (ps-number-def prefix key val)
92 (let ((s (if (integer? val)
93 (ly:number->string val)
94 (ly:number->string (exact->inexact val)))))
95 (format #f "/~a~a ~a def\n"
97 (symbol->string key) s)))
101 ;;; Lily output interface, PostScript implementation --- cleanup and docme
105 (define (bezier-sandwich lst thick)
106 (format #f "~a ~a draw_bezier_sandwich"
107 (string-join (map number-pair->string4 lst) " ")
110 (define (char font i)
111 (format #f "~a (\\~a) show"
112 (ps-font-command font)
113 (ly:inexact->string i 8)))
115 (define (circle radius thick fill)
117 "~a ~f ~f draw_circle"
121 (round4 radius) (round4 thick)))
123 (define (dashed-line thick on off dx dy phase)
124 (format #f "~a ~a ~a [ ~a ~a ] ~a draw_dashed_line"
134 ;; what the heck is this interface ?
135 (define (dashed-slur thick on off l)
136 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
137 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
138 (string-join (map number-pair->string4 control-points) " "))
143 (define (dot x y radius)
144 (format #f " ~a draw_dot"
145 (numbers->string4 (list radius x y))))
147 (define (draw-line thick x1 y1 x2 y2)
148 (format #f "~a ~a ~a ~a ~a draw_line"
155 (define (embedded-ps string)
158 (define (glyph-string postscript-font-name
163 (define (glyph-spec w x y g)
164 (let ((prefix (if (string? g) "/" "")))
165 (format #f "~f ~f ~a~a"
172 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
176 "/~a ~a output-scale div selectfont
181 (string-join (map (lambda (x) (apply glyph-spec x))
182 (reverse w-x-y-named-glyphs)) "\n")
183 (length w-x-y-named-glyphs)))
186 (define (grob-cause offset grob)
187 (let* ((cause (ly:grob-property grob 'cause))
188 (music-origin (if (ly:stream-event? cause)
189 (ly:event-property cause 'origin))))
190 (if (not (ly:input-location? music-origin))
192 (let* ((location (ly:input-file-line-char-column music-origin))
193 (raw-file (car location))
194 (file (if (is-absolute? raw-file)
196 (string-append (ly-getcwd) "/" raw-file)))
197 (x-ext (ly:grob-extent grob grob X))
198 (y-ext (ly:grob-extent grob grob Y)))
200 (if (and (< 0 (interval-length x-ext))
201 (< 0 (interval-length y-ext)))
202 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
203 (+ (car offset) (car x-ext))
204 (+ (cdr offset) (car y-ext))
205 (+ (car offset) (cdr x-ext))
206 (+ (cdr offset) (cdr y-ext))
211 ;; backslash is interpreted by GS.
212 (string-regexp-substitute "\\\\" "/"
213 (string-regexp-substitute " " "%20" file))
219 (define (lily-def key val)
220 (let ((prefix "lilypondlayout"))
222 (substring key 0 (min (string-length prefix) (string-length key)))
224 (format "/~a { ~a } bind def\n" key val)
225 (format "/~a (~a) def\n" key val))))
227 (define (named-glyph font glyph)
228 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
229 (ps-font-command font)
235 (define (placebox x y s)
243 (define (polygon points blot-diameter filled?)
244 (format #f "~a ~a ~a ~a draw_polygon"
245 (if filled? "true" "false")
246 (numbers->string4 points)
247 (number->string (- (/ (length points) 2) 1))
248 (str4 blot-diameter)))
250 (define (repeat-slash width slope beam-thickness)
251 (define (euclidean-length x y)
252 (sqrt (+ (* x x) (* y y))))
254 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
255 (height (* width slope)))
256 (format #f "~a draw_repeat_slash"
257 (numbers->string4 (list x-width width height)))))
260 (define (round-filled-box left right bottom top blotdiam)
261 (let* ((halfblot (/ blotdiam 2))
262 (x (- halfblot left))
263 (width (- right (+ halfblot x)))
264 (y (- halfblot bottom))
265 (height (- top (+ halfblot y))))
266 (format #f "~a draw_round_box"
268 (list width height x y blotdiam)))))
270 ;; save current color on stack and set new color
271 (define (setcolor r g b)
272 (format #f "gsave ~a setrgbcolor\n"
273 (numbers->string4 (list r g b))))
275 ;; restore color from stack
276 (define (resetcolor) "grestore \n")
278 ;; rotation around given point
279 (define (setrotation ang x y)
280 (format "gsave ~a translate ~a rotate ~a translate\n"
281 (numbers->string4 (list x y))
283 (numbers->string4 (list (* -1 x) (* -1 y)))))
285 (define (resetrotation ang x y)
289 (define (text font s)
290 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
291 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
293 (let* ((space-length (cdar (ly:text-dimension font " ")))
294 (space-move (string-append (number->string space-length)
295 ;; how much precision do we need here?
297 (out-vec (decode-byte-string s)))
300 (ps-font-command font) " "
308 (string-append "/" (symbol->string sym) " glyphshow")))
314 (define (url-link url x y)
315 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
322 (define (utf-8-string pango-font-description string)
323 (ly:warning (_ "utf-8-string encountered in PS backend")))
326 (define (zigzag-line centre? zzw zzh thick dx dy)
327 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
328 (if centre? "true" "false")
336 (define (path thickness exps)
337 (define (convert-path-exps exps)
344 ((memq head '(rmoveto rlineto lineto moveto)) 2)
345 ((memq head '(rcurveto curveto)) 6)
347 (args (take rest arity))
350 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
351 (cons (format "~a ~a "
352 (string-join (map (lambda (x) (format "~a " x)) args) " ")
354 (convert-path-exps (drop rest arity))))
359 "1 setlinecap ~a setlinewidth\n~a stroke"
361 (string-join (convert-path-exps exps) " ")))