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
51 ;;; helper functions, not part of output interface
55 ;; ice-9 format uses a lot of memory
56 ;; using simple-format almost halves lilypond cell usage
59 (if (or (nan? num) (inf? num))
61 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
62 (if (ly:get-option 'strict-infinity-checking)
65 (ly:number->string num)))
67 (define (number-pair->string4 numpair)
68 (ly:format "~4l" numpair))
71 ;;; Lily output interface, PostScript implementation --- cleanup and docme
75 (define (bezier-sandwich lst thick)
76 (ly:format "~l ~4f draw_bezier_sandwich"
77 (map number-pair->string4 lst)
81 (ly:format "~a (\\~a) show"
82 (ps-font-command font)
83 (ly:inexact->string i 8)))
85 (define (circle radius thick fill)
87 "~a ~4f ~4f draw_circle"
93 (define (dashed-line thick on off dx dy phase)
94 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
102 ;; what the heck is this interface ?
103 (define (dashed-slur thick on off l)
104 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
105 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
106 (map number-pair->string4 control-points))
111 (define (dot x y radius)
112 (ly:format " ~4l draw_dot" (list radius x y)))
114 (define (draw-line thick x1 y1 x2 y2)
115 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
119 (define (embedded-ps string)
122 (define (glyph-string postscript-font-name
127 (define (glyph-spec w x y g)
128 (let ((prefix (if (string? g) "/" "")))
129 (ly:format "~4f ~4f ~a~a"
135 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
139 "/~a ~a output-scale div selectfont
144 (string-join (map (lambda (x) (apply glyph-spec x))
145 (reverse w-x-y-named-glyphs)) "\n")
146 (length w-x-y-named-glyphs)))
149 (define (grob-cause offset grob)
150 (if (ly:get-option 'point-and-click)
151 (let* ((cause (ly:grob-property grob 'cause))
152 (music-origin (if (ly:stream-event? cause)
153 (ly:event-property cause 'origin))))
154 (if (ly:input-location? music-origin)
155 (let* ((location (ly:input-file-line-char-column music-origin))
156 (raw-file (car location))
157 (file (if (is-absolute? raw-file)
159 (string-append (ly-getcwd) "/" raw-file)))
160 (x-ext (ly:grob-extent grob grob X))
161 (y-ext (ly:grob-extent grob grob Y)))
163 (if (and (< 0 (interval-length x-ext))
164 (< 0 (interval-length y-ext)))
165 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
166 (+ (car offset) (car x-ext))
167 (+ (cdr offset) (car y-ext))
168 (+ (car offset) (cdr x-ext))
169 (+ (cdr offset) (cdr y-ext))
174 ;; backslash is interpreted by GS.
175 (ly:string-substitute "\\" "/"
176 (ly:string-substitute " " "%20" file))
184 (define (named-glyph font glyph)
185 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
186 (ps-font-command font)
192 (define (placebox x y s)
197 (define (polygon points blot-diameter filled?)
198 (ly:format "~a ~4l ~a ~4f draw_polygon"
199 (if filled? "true" "false")
201 (- (/ (length points) 2) 1)
204 (define (repeat-slash width slope beam-thickness)
205 (define (euclidean-length x y)
206 (sqrt (+ (* x x) (* y y))))
208 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
209 (height (* width slope)))
210 (ly:format "~4l draw_repeat_slash"
211 (list x-width width height))))
214 (define (round-filled-box left right bottom top blotdiam)
215 (let* ((halfblot (/ blotdiam 2))
216 (x (- halfblot left))
217 (width (- right (+ halfblot x)))
218 (y (- halfblot bottom))
219 (height (- top (+ halfblot y))))
220 (ly:format "~4l draw_round_box"
221 (list width height x y blotdiam))))
223 ;; save current color on stack and set new color
224 (define (setcolor r g b)
225 (ly:format "gsave ~4l setrgbcolor\n"
228 ;; restore color from stack
229 (define (resetcolor) "grestore \n")
231 ;; rotation around given point
232 (define (setrotation ang x y)
233 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
236 (list (* -1 x) (* -1 y))))
238 (define (resetrotation ang x y)
242 (define (text font s)
243 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
244 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
246 (let* ((space-length (cdar (ly:text-dimension font " ")))
247 (space-move (string-append (number->string space-length)
248 ;; how much precision do we need here?
250 (out-vec (decode-byte-string s)))
253 (ps-font-command font) " "
261 (string-append "/" (symbol->string sym) " glyphshow")))
267 (define (url-link url x y)
268 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add (~a) mark_URI"
275 (define (utf-8-string pango-font-description string)
276 (ly:warning (_ "utf-8-string encountered in PS backend")))
278 (define (path thickness exps)
279 (define (convert-path-exps exps)
286 ((memq head '(rmoveto rlineto lineto moveto)) 2)
287 ((memq head '(rcurveto curveto)) 6)
289 (args (take rest arity))
292 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
297 (convert-path-exps (drop rest arity))))
302 "1 setlinecap ~a setlinewidth\n~l stroke"
304 (convert-path-exps exps) ))