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)
116 (format #f "~a ~a ~a [ ~a ~a ] 0 draw_dashed_line"
123 ;; what the heck is this interface ?
124 (define (dashed-slur thick on off l)
125 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
126 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
127 (string-join (map number-pair->string4 control-points) " "))
132 (define (dot x y radius)
133 (format #f " ~a draw_dot"
134 (numbers->string4 (list radius x y))))
136 (define (draw-line thick x1 y1 x2 y2)
137 (format #f "~a ~a ~a ~a ~a draw_line"
144 (define (embedded-ps string)
147 (define (glyph-string postscript-font-name
152 (define (glyph-spec w x y g)
153 (let ((prefix (if (string? g) "/" "")))
154 (format #f "~f ~f ~a~a"
161 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
165 "/~a ~a output-scale div selectfont
170 (string-join (map (lambda (x) (apply glyph-spec x))
171 (reverse w-x-y-named-glyphs)) "\n")
172 (length w-x-y-named-glyphs)))
175 (define (grob-cause offset grob)
176 (let* ((cause (ly:grob-property grob 'cause))
177 (music-origin (if (ly:music? cause)
178 (ly:music-property cause 'origin))))
179 (if (not (ly:input-location? music-origin))
181 (let* ((location (ly:input-file-line-char-column music-origin))
182 (raw-file (car location))
183 (file (if (is-absolute? raw-file)
185 (string-append (ly-getcwd) "/" raw-file)))
186 (x-ext (ly:grob-extent grob grob X))
187 (y-ext (ly:grob-extent grob grob Y)))
189 (if (and (< 0 (interval-length x-ext))
190 (< 0 (interval-length y-ext)))
191 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
192 (+ (car offset) (car x-ext))
193 (+ (cdr offset) (car y-ext))
194 (+ (car offset) (cdr x-ext))
195 (+ (cdr offset) (cdr y-ext))
200 ;; backslash is interpreted by GS.
201 (string-regexp-substitute "\\\\" "/"
202 (string-regexp-substitute " " "%20" file))
208 (define (lily-def key val)
209 (let ((prefix "lilypondlayout"))
211 (substring key 0 (min (string-length prefix) (string-length key)))
213 (format "/~a { ~a } bind def\n" key val)
214 (format "/~a (~a) def\n" key val))))
216 (define (named-glyph font glyph)
217 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
218 (ps-font-command font)
224 (define (placebox x y s)
232 (define (polygon points blot-diameter filled?)
233 (format #f "~a ~a ~a ~a draw_polygon"
234 (if filled? "true" "false")
235 (numbers->string4 points)
236 (number->string (- (/ (length points) 2) 1))
237 (str4 blot-diameter)))
239 (define (repeat-slash width slope beam-thickness)
240 (define (euclidean-length x y)
241 (sqrt (+ (* x x) (* y y))))
243 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
244 (height (* width slope)))
245 (format #f "~a draw_repeat_slash"
246 (numbers->string4 (list x-width width height)))))
248 ;; restore color from stack
249 (define (resetcolor) "setrgbcolor\n")
252 (define (round-filled-box left right bottom top blotdiam)
253 (let* ((halfblot (/ blotdiam 2))
254 (x (- halfblot left))
255 (width (- right (+ halfblot x)))
256 (y (- halfblot bottom))
257 (height (- top (+ halfblot y))))
258 (format #f "~a draw_round_box"
260 (list width height x y blotdiam)))))
262 ;; save current color on stack and set new color
263 (define (setcolor r g b)
264 (format #f "currentrgbcolor ~a setrgbcolor\n"
265 (numbers->string4 (list r g b))))
267 ;; rotation around given point
268 (define (setrotation ang x y)
269 (format "gsave ~a translate ~a rotate ~a translate\n"
270 (numbers->string4 (list x y))
272 (numbers->string4 (list (* -1 x) (* -1 y)))))
274 (define (resetrotation ang x y)
278 (define (text font s)
279 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
280 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
282 (let* ((space-length (cdar (ly:text-dimension font " ")))
283 (space-move (string-append (number->string space-length)
284 ;; how much precision do we need here?
286 (out-vec (decode-byte-string s)))
289 (ps-font-command font) " "
297 (string-append "/" (symbol->string sym) " glyphshow")))
303 (define (url-link url x y)
304 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
311 (define (utf-8-string pango-font-description string)
312 (ly:warning (_ "utf-8-string encountered in PS backend")))
315 (define (zigzag-line centre? zzw zzh thick dx dy)
316 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
317 (if centre? "true" "false")
325 (define (path thickness exps)
326 (define (path-exps->ps-path-exps exps)
333 ((memq head '(rmoveto rlineto lineto moveto)) 2)
334 ((memq head '(rcurveto curveto)) 6)
336 (args (take rest arity))
339 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
340 (cons (format "~a ~a "
341 (string-join (map (lambda (x) (format "~a " x)) args) " ")
343 (path-exps->ps-path-exps (drop rest arity))))
348 "1 setlinecap ~a setlinewidth\n~a stroke"
350 (string-join (path-exps->ps-path-exps exps) " ")))