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 ;; 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))
70 (define (numbers->string4 numlist)
71 (string-join (map str4 numlist) " "))
74 ;;; Lily output interface, PostScript implementation --- cleanup and docme
78 (define (bezier-sandwich lst thick)
79 (ly:format "~l ~4f draw_bezier_sandwich"
80 (map number-pair->string4 lst)
84 (ly:format "~a (\\~a) show"
85 (ps-font-command font)
86 (ly:inexact->string i 8)))
88 (define (circle radius thick fill)
90 "~a ~4f ~4f draw_circle"
96 (define (dashed-line thick on off dx dy phase)
97 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
105 ;; what the heck is this interface ?
106 (define (dashed-slur thick on off l)
107 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
108 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
109 (map number-pair->string4 control-points))
114 (define (dot x y radius)
115 (ly:format " ~4l draw_dot" (list radius x y)))
117 (define (draw-line thick x1 y1 x2 y2)
118 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
122 (define (embedded-ps string)
125 (define (glyph-string postscript-font-name
130 (define (glyph-spec w x y g)
131 (let ((prefix (if (string? g) "/" "")))
132 (ly:format "~4f ~4f ~a~a"
138 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
142 "/~a ~a output-scale div selectfont
147 (string-join (map (lambda (x) (apply glyph-spec x))
148 (reverse w-x-y-named-glyphs)) "\n")
149 (length w-x-y-named-glyphs)))
152 (define (grob-cause offset grob)
153 (let* ((cause (ly:grob-property grob 'cause))
154 (music-origin (if (ly:stream-event? cause)
155 (ly:event-property cause 'origin))))
156 (if (not (ly:input-location? music-origin))
158 (let* ((location (ly:input-file-line-char-column music-origin))
159 (raw-file (car location))
160 (file (if (is-absolute? raw-file)
162 (string-append (ly-getcwd) "/" raw-file)))
163 (x-ext (ly:grob-extent grob grob X))
164 (y-ext (ly:grob-extent grob grob Y)))
166 (if (and (< 0 (interval-length x-ext))
167 (< 0 (interval-length y-ext)))
168 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
169 (+ (car offset) (car x-ext))
170 (+ (cdr offset) (car y-ext))
171 (+ (car offset) (cdr x-ext))
172 (+ (cdr offset) (cdr y-ext))
177 ;; backslash is interpreted by GS.
178 (ly:string-substitute "\\" "/"
179 (ly:string-substitute " " "%20" file))
186 (define (named-glyph font glyph)
187 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
188 (ps-font-command font)
194 (define (placebox x y s)
199 (define (polygon points blot-diameter filled?)
200 (ly:format "~a ~4l ~a ~4f draw_polygon"
201 (if filled? "true" "false")
203 (- (/ (length points) 2) 1)
206 (define (repeat-slash width slope beam-thickness)
207 (define (euclidean-length x y)
208 (sqrt (+ (* x x) (* y y))))
210 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
211 (height (* width slope)))
212 (ly:format "~4l draw_repeat_slash"
213 (list x-width width height))))
216 (define (round-filled-box left right bottom top blotdiam)
217 (let* ((halfblot (/ blotdiam 2))
218 (x (- halfblot left))
219 (width (- right (+ halfblot x)))
220 (y (- halfblot bottom))
221 (height (- top (+ halfblot y))))
222 (ly:format "~4l draw_round_box"
223 (list width height x y blotdiam))))
225 ;; save current color on stack and set new color
226 (define (setcolor r g b)
227 (ly:format "gsave ~4l setrgbcolor\n"
230 ;; restore color from stack
231 (define (resetcolor) "grestore \n")
233 ;; rotation around given point
234 (define (setrotation ang x y)
235 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
238 (list (* -1 x) (* -1 y))))
240 (define (resetrotation ang x y)
244 (define (text font s)
245 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
246 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
248 (let* ((space-length (cdar (ly:text-dimension font " ")))
249 (space-move (string-append (number->string space-length)
250 ;; how much precision do we need here?
252 (out-vec (decode-byte-string s)))
255 (ps-font-command font) " "
263 (string-append "/" (symbol->string sym) " glyphshow")))
269 (define (url-link url x y)
270 (ly:format "~a ~a ~a ~a (~a) mark_URI"
277 (define (utf-8-string pango-font-description string)
278 (ly:warning (_ "utf-8-string encountered in PS backend")))
280 (define (path thickness exps)
281 (define (convert-path-exps exps)
288 ((memq head '(rmoveto rlineto lineto moveto)) 2)
289 ((memq head '(rcurveto curveto)) 6)
291 (args (take rest arity))
294 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
299 (convert-path-exps (drop rest arity))))
304 "1 setlinecap ~a setlinewidth\n~l stroke"
306 (convert-path-exps exps) ))