1 ;;;; output-ps.scm -- implement Scheme output interface for PostScript
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 1998--2007 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
52 ;;; helper functions, not part of output interface
56 ;; ice-9 format uses a lot of memory
57 ;; using simple-format almost halves lilypond cell usage
60 (if (or (nan? num) (inf? num))
62 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
63 (if (ly:get-option 'strict-infinity-checking)
66 (ly:number->string num)))
68 (define (number-pair->string4 numpair)
69 (ly:format "~4l" numpair))
72 ;;; Lily output interface, PostScript implementation --- cleanup and docme
76 (define (bezier-sandwich lst thick)
77 (ly:format "~l ~4f draw_bezier_sandwich"
78 (map number-pair->string4 lst)
82 (ly:format "~a (\\~a) show"
83 (ps-font-command font)
84 (ly:inexact->string i 8)))
86 (define (circle radius thick fill)
88 "~a ~4f ~4f draw_circle"
94 (define (dashed-line thick on off dx dy phase)
95 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
103 ;; what the heck is this interface ?
104 (define (dashed-slur thick on off l)
105 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
106 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
107 (map number-pair->string4 control-points))
112 (define (dot x y radius)
113 (ly:format " ~4l draw_dot" (list radius x y)))
115 (define (draw-line thick x1 y1 x2 y2)
116 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
120 (define (ellipse x-radius y-radius thick fill)
122 "~a ~4f ~4f ~4f draw_ellipse"
126 x-radius y-radius thick))
128 (define (embedded-ps string)
131 (define (glyph-string postscript-font-name
136 (define (glyph-spec w x y g)
137 (let ((prefix (if (string? g) "/" "")))
138 (ly:format "~4f ~4f ~a~a"
144 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
148 "/~a ~a output-scale div selectfont
153 (string-join (map (lambda (x) (apply glyph-spec x))
154 (reverse w-x-y-named-glyphs)) "\n")
155 (length w-x-y-named-glyphs)))
158 (define (grob-cause offset grob)
159 (if (ly:get-option 'point-and-click)
160 (let* ((cause (ly:grob-property grob 'cause))
161 (music-origin (if (ly:stream-event? cause)
162 (ly:event-property cause 'origin))))
163 (if (ly:input-location? music-origin)
164 (let* ((location (ly:input-file-line-char-column music-origin))
165 (raw-file (car location))
166 (file (if (is-absolute? raw-file)
168 (string-append (ly-getcwd) "/" raw-file)))
169 (x-ext (ly:grob-extent grob grob X))
170 (y-ext (ly:grob-extent grob grob Y)))
172 (if (and (< 0 (interval-length x-ext))
173 (< 0 (interval-length y-ext)))
174 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
175 (+ (car offset) (car x-ext))
176 (+ (cdr offset) (car y-ext))
177 (+ (car offset) (cdr x-ext))
178 (+ (cdr offset) (cdr y-ext))
183 ;; backslash is interpreted by GS.
184 (ly:string-substitute "\\" "/"
185 (ly:string-substitute " " "%20" file))
193 (define (named-glyph font glyph)
194 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
195 (ps-font-command font)
201 (define (placebox x y s)
206 (define (polygon points blot-diameter filled?)
207 (ly:format "~a ~4l ~a ~4f draw_polygon"
208 (if filled? "true" "false")
210 (- (/ (length points) 2) 1)
213 (define (repeat-slash width slope beam-thickness)
214 (define (euclidean-length x y)
215 (sqrt (+ (* x x) (* y y))))
217 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
218 (height (* width slope)))
219 (ly:format "~4l draw_repeat_slash"
220 (list x-width width height))))
223 (define (round-filled-box left right bottom top blotdiam)
224 (let* ((halfblot (/ blotdiam 2))
225 (x (- halfblot left))
226 (width (- right (+ halfblot x)))
227 (y (- halfblot bottom))
228 (height (- top (+ halfblot y))))
229 (ly:format "~4l draw_round_box"
230 (list width height x y blotdiam))))
232 ;; save current color on stack and set new color
233 (define (setcolor r g b)
234 (ly:format "gsave ~4l setrgbcolor\n"
237 ;; restore color from stack
238 (define (resetcolor) "grestore \n")
240 ;; rotation around given point
241 (define (setrotation ang x y)
242 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
245 (list (* -1 x) (* -1 y))))
247 (define (resetrotation ang x y)
251 (define (text font s)
252 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
253 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
255 (let* ((space-length (cdar (ly:text-dimension font " ")))
256 (space-move (string-append (number->string space-length)
257 ;; how much precision do we need here?
259 (out-vec (decode-byte-string s)))
262 (ps-font-command font) " "
270 (string-append "/" (symbol->string sym) " glyphshow")))
276 (define (url-link url x y)
277 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add (~a) mark_URI"
284 (define (utf-8-string pango-font-description string)
285 (ly:warning (_ "utf-8-string encountered in PS backend")))
287 (define (path thickness exps)
288 (define (convert-path-exps exps)
295 ((memq head '(rmoveto rlineto lineto moveto)) 2)
296 ((memq head '(rcurveto curveto)) 6)
298 (args (take rest arity))
301 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
306 (convert-path-exps (drop rest arity))))
311 "1 setlinecap ~a setlinewidth\n~l stroke"
313 (convert-path-exps exps) ))