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
50 ;;; helper functions, not part of output interface
54 (define (escape-parentheses s)
55 (regexp-substitute/global #f "(^|[^\\])([\\(\\)])" s 'pre 1 "\\" 2 'post))
57 (define (ps-encoding text)
58 (escape-parentheses text))
61 (/ (round (* 100 num)) 100))
64 (/ (round (* 10000 num)) 10000))
67 (format #f "~f" (round4 num)))
69 (define (number-pair->string4 numpair)
70 (format #f "~f ~f" (round4 (car numpair)) (round4 (cdr numpair))))
72 (define (numbers->string4 numlist)
73 (string-join (map str4 numlist) " "))
76 (define-public (ps-string-def prefix key val)
77 (format #f "/ ~a~a (~a) def\n"
80 (escape-parentheses val)))
82 (define (ps-number-def prefix key val)
83 (let ((s (if (integer? val)
84 (ly:number->string val)
85 (ly:number->string (exact->inexact val)))))
86 (format #f "/~a~a ~a def\n"
88 (symbol->string key) s)))
92 ;;; Lily output interface, PostScript implementation --- cleanup and docme
96 (define (bezier-sandwich lst thick)
97 (format #f "~a ~a draw_bezier_sandwich"
98 (string-join (map number-pair->string4 lst) " ")
101 (define (char font i)
102 (format #f "~a (\\~a) show"
103 (ps-font-command font)
104 (ly:inexact->string i 8)))
106 (define (circle radius thick fill)
108 "~a ~f ~f draw_circle"
112 (round4 radius) (round4 thick)))
114 (define (dashed-line thick on off dx dy)
115 (format #f "~a ~a ~a [ ~a ~a ] 0 draw_dashed_line"
122 ;; what the heck is this interface ?
123 (define (dashed-slur thick on off l)
124 (format #f "~a ~a [ ~a ~a ] 0 draw_dashed_slur"
125 (string-join (map number-pair->string4 l) " ")
130 (define (dot x y radius)
131 (format #f " ~a draw_dot"
132 (numbers->string4 (list x y radius))))
134 (define (draw-line thick x1 y1 x2 y2)
135 (format #f "1 setlinecap 1 setlinejoin ~a setlinewidth ~a ~a moveto ~a ~a lineto stroke"
142 (define (embedded-ps string)
145 (define (glyph-string postscript-font-name
150 (define (glyph-spec w x y g)
151 (let ((prefix (if (string? g) "/" "")))
152 (format #f "~f ~f ~a~a"
160 /~a /CIDFont findresource ~a output-scale div scalefont setfont
165 "gsave\n/~a ~a output-scale div selectfont
171 (string-join (map (lambda (x) (apply glyph-spec x))
172 (reverse w-x-y-named-glyphs)) "\n")
173 (length w-x-y-named-glyphs)))
176 (define (grob-cause offset grob)
177 (let* ((cause (ly:grob-property grob 'cause))
178 (music-origin (if (ly:music? cause)
179 (ly:music-property cause 'origin))))
180 (if (not (ly:input-location? music-origin))
182 (let* ((location (ly:input-file-line-char-column music-origin))
183 (raw-file (car location))
184 (file (if (is-absolute? raw-file)
186 (string-append (ly-getcwd) "/" raw-file)))
187 (x-ext (ly:grob-extent grob grob X))
188 (y-ext (ly:grob-extent grob grob Y)))
190 (if (and (< 0 (interval-length x-ext))
191 (< 0 (interval-length y-ext)))
192 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
193 (+ (car offset) (car x-ext))
194 (+ (cdr offset) (car y-ext))
195 (+ (car offset) (cdr x-ext))
196 (+ (cdr offset) (cdr y-ext))
201 ;; backslash is interpreted by GS.
202 (string-regexp-substitute "\\\\" "/"
203 (string-regexp-substitute " " "%20" file))
209 (define (lily-def key val)
210 (let ((prefix "lilypondlayout"))
212 (substring key 0 (min (string-length prefix) (string-length key)))
214 (format "/~a { ~a } bind def\n" key val)
215 (format "/~a (~a) def\n" key val))))
217 (define (named-glyph font glyph)
218 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
219 (ps-font-command font)
225 (define (placebox x y s)
227 "gsave ~a ~a translate
236 (define (polygon points blot-diameter filled?)
237 (format #f "~a ~a ~a ~a draw_polygon"
238 (if filled? "true" "false")
239 (numbers->string4 points)
240 (number->string (- (/ (length points) 2) 1))
241 (str4 blot-diameter)))
243 (define (repeat-slash width slope beam-thickness)
244 (define (euclidean-length x y)
245 (sqrt (+ (* x x) (* y y))))
247 (let ((x-width (euclidean-length slope (/ beam-thickness slope)))
248 (height (* width slope)))
249 (format #f "~a draw_repeat_slash"
250 (numbers->string4 (list x-width width height)))))
252 ;; restore color from stack
253 (define (resetcolor) "setrgbcolor\n")
255 (define (round-filled-box left right bottom top blotdiam)
256 (let* ((halfblot (/ blotdiam 2))
257 (x (- halfblot left))
258 (width (- right (+ halfblot x)))
259 (y (- halfblot bottom))
260 (height (- top (+ halfblot y))))
261 (format #f "~a draw_round_box"
263 (list x y width height blotdiam)))))
265 ;; save current color on stack and set new color
266 (define (setcolor r g b)
267 (format #f "currentrgbcolor ~a setrgbcolor\n"
268 (numbers->string4 (list r g b))))
270 (define (text font s)
271 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
272 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
274 (let* ((space-length (cdar (ly:text-dimension font " ")))
275 (space-move (string-append (number->string space-length)
276 ;; how much precision do we need here?
278 (out-vec (decode-byte-string s)))
281 (ps-font-command font) " "
289 (string-append "/" (symbol->string sym) " glyphshow")))
295 (define (url-link url x y)
296 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
303 (define (utf-8-string pango-font-description string)
304 (ly:warning (_ "utf-8-string encountered in PS backend")))
307 (define (zigzag-line centre? zzw zzh thick dx dy)
308 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
309 (if centre? "true" "false")