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 (string-append (str4 (car numpair))
77 (str4 (cdr numpair))))
79 (define (numbers->string4 numlist)
80 (string-join (map str4 numlist) " "))
83 (define-public (ps-string-def prefix key val)
84 (format #f "/ ~a~a (~a) def\n"
87 (escape-parentheses val)))
89 (define (ps-number-def prefix key val)
90 (let ((s (if (integer? val)
91 (ly:number->string val)
92 (ly:number->string (exact->inexact val)))))
93 (format #f "/~a~a ~a def\n"
95 (symbol->string key) s)))
99 ;;; Lily output interface, PostScript implementation --- cleanup and docme
103 (define (bezier-sandwich lst thick)
104 (format #f "~a ~a draw_bezier_sandwich"
105 (string-join (map number-pair->string4 lst) " ")
108 (define (char font i)
109 (format #f "~a (\\~a) show"
110 (ps-font-command font)
111 (ly:inexact->string i 8)))
113 (define (circle radius thick fill)
115 "~a ~f ~f draw_circle"
119 (round4 radius) (round4 thick)))
121 (define (dashed-line thick on off dx dy phase)
122 (format #f "~a ~a ~a [ ~a ~a ] ~a draw_dashed_line"
132 ;; what the heck is this interface ?
133 (define (dashed-slur thick on off l)
134 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
135 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
136 (string-join (map number-pair->string4 control-points) " "))
141 (define (dot x y radius)
142 (format #f " ~a draw_dot"
143 (numbers->string4 (list radius x y))))
145 (define (draw-line thick x1 y1 x2 y2)
146 (format #f "~a ~a ~a ~a ~a draw_line"
153 (define (embedded-ps string)
156 (define (glyph-string postscript-font-name
161 (define (glyph-spec w x y g)
162 (let ((prefix (if (string? g) "/" "")))
163 (format #f "~f ~f ~a~a"
170 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
174 "/~a ~a output-scale div selectfont
179 (string-join (map (lambda (x) (apply glyph-spec x))
180 (reverse w-x-y-named-glyphs)) "\n")
181 (length w-x-y-named-glyphs)))
184 (define (grob-cause offset grob)
185 (let* ((cause (ly:grob-property grob 'cause))
186 (music-origin (if (ly:stream-event? cause)
187 (ly:event-property cause 'origin))))
188 (if (not (ly:input-location? music-origin))
190 (let* ((location (ly:input-file-line-char-column music-origin))
191 (raw-file (car location))
192 (file (if (is-absolute? raw-file)
194 (string-append (ly-getcwd) "/" raw-file)))
195 (x-ext (ly:grob-extent grob grob X))
196 (y-ext (ly:grob-extent grob grob Y)))
198 (if (and (< 0 (interval-length x-ext))
199 (< 0 (interval-length y-ext)))
200 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
201 (+ (car offset) (car x-ext))
202 (+ (cdr offset) (car y-ext))
203 (+ (car offset) (cdr x-ext))
204 (+ (cdr offset) (cdr y-ext))
209 ;; backslash is interpreted by GS.
210 (string-regexp-substitute "\\\\" "/"
211 (string-regexp-substitute " " "%20" file))
217 (define (lily-def key val)
218 (let ((prefix "lilypondlayout"))
220 (substring key 0 (min (string-length prefix) (string-length key)))
222 (format "/~a { ~a } bind def\n" key val)
223 (format "/~a (~a) def\n" key val))))
225 (define (named-glyph font glyph)
226 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
227 (ps-font-command font)
233 (define (placebox x y s)
241 (define (polygon points blot-diameter filled?)
242 (format #f "~a ~a ~a ~a draw_polygon"
243 (if filled? "true" "false")
244 (numbers->string4 points)
245 (number->string (- (/ (length points) 2) 1))
246 (str4 blot-diameter)))
248 (define (repeat-slash width slope beam-thickness)
249 (define (euclidean-length x y)
250 (sqrt (+ (* x x) (* y y))))
252 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
253 (height (* width slope)))
254 (format #f "~a draw_repeat_slash"
255 (numbers->string4 (list x-width width height)))))
258 (define (round-filled-box left right bottom top blotdiam)
259 (let* ((halfblot (/ blotdiam 2))
260 (x (- halfblot left))
261 (width (- right (+ halfblot x)))
262 (y (- halfblot bottom))
263 (height (- top (+ halfblot y))))
264 (format #f "~a draw_round_box"
266 (list width height x y blotdiam)))))
268 ;; save current color on stack and set new color
269 (define (setcolor r g b)
270 (format #f "gsave ~a setrgbcolor\n"
271 (numbers->string4 (list r g b))))
273 ;; restore color from stack
274 (define (resetcolor) "grestore \n")
276 ;; rotation around given point
277 (define (setrotation ang x y)
278 (format "gsave ~a translate ~a rotate ~a translate\n"
279 (numbers->string4 (list x y))
281 (numbers->string4 (list (* -1 x) (* -1 y)))))
283 (define (resetrotation ang x y)
287 (define (text font s)
288 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
289 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
291 (let* ((space-length (cdar (ly:text-dimension font " ")))
292 (space-move (string-append (number->string space-length)
293 ;; how much precision do we need here?
295 (out-vec (decode-byte-string s)))
298 (ps-font-command font) " "
306 (string-append "/" (symbol->string sym) " glyphshow")))
312 (define (url-link url x y)
313 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
320 (define (utf-8-string pango-font-description string)
321 (ly:warning (_ "utf-8-string encountered in PS backend")))
324 (define (zigzag-line centre? zzw zzh thick dx dy)
325 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
326 (if centre? "true" "false")
334 (define (path thickness exps)
335 (define (convert-path-exps exps)
342 ((memq head '(rmoveto rlineto lineto moveto)) 2)
343 ((memq head '(rcurveto curveto)) 6)
345 (args (take rest arity))
348 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
349 (cons (format "~a ~a "
350 (string-join (map (lambda (x) (format "~a " x)) args) " ")
352 (convert-path-exps (drop rest arity))))
357 "1 setlinecap ~a setlinewidth\n~a stroke"
359 (string-join (convert-path-exps exps) " ")))