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 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
126 (string-join (map number-pair->string4 control-points) " "))
131 (define (dot x y radius)
132 (format #f " ~a draw_dot"
133 (numbers->string4 (list radius x y))))
135 (define (draw-line thick x1 y1 x2 y2)
136 (format #f "~a ~a ~a ~a ~a draw_line"
143 (define (embedded-ps string)
146 (define (glyph-string postscript-font-name
151 (define (glyph-spec w x y g)
152 (let ((prefix (if (string? g) "/" "")))
153 (format #f "~f ~f ~a~a"
160 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
164 "/~a ~a output-scale div selectfont
169 (string-join (map (lambda (x) (apply glyph-spec x))
170 (reverse w-x-y-named-glyphs)) "\n")
171 (length w-x-y-named-glyphs)))
174 (define (grob-cause offset grob)
175 (let* ((cause (ly:grob-property grob 'cause))
176 (music-origin (if (ly:music? cause)
177 (ly:music-property cause 'origin))))
178 (if (not (ly:input-location? music-origin))
180 (let* ((location (ly:input-file-line-char-column music-origin))
181 (raw-file (car location))
182 (file (if (is-absolute? raw-file)
184 (string-append (ly-getcwd) "/" raw-file)))
185 (x-ext (ly:grob-extent grob grob X))
186 (y-ext (ly:grob-extent grob grob Y)))
188 (if (and (< 0 (interval-length x-ext))
189 (< 0 (interval-length y-ext)))
190 (format #f "~$ ~$ ~$ ~$ (textedit://~a:~a:~a:~a) mark_URI\n"
191 (+ (car offset) (car x-ext))
192 (+ (cdr offset) (car y-ext))
193 (+ (car offset) (cdr x-ext))
194 (+ (cdr offset) (cdr y-ext))
199 ;; backslash is interpreted by GS.
200 (string-regexp-substitute "\\\\" "/"
201 (string-regexp-substitute " " "%20" file))
207 (define (lily-def key val)
208 (let ((prefix "lilypondlayout"))
210 (substring key 0 (min (string-length prefix) (string-length key)))
212 (format "/~a { ~a } bind def\n" key val)
213 (format "/~a (~a) def\n" key val))))
215 (define (named-glyph font glyph)
216 (format #f "~a /~a glyphshow " ;;Why is there a space at the end?
217 (ps-font-command font)
223 (define (placebox x y s)
231 (define (polygon points blot-diameter filled?)
232 (format #f "~a ~a ~a ~a draw_polygon"
233 (if filled? "true" "false")
234 (numbers->string4 points)
235 (number->string (- (/ (length points) 2) 1))
236 (str4 blot-diameter)))
238 (define (repeat-slash width slope beam-thickness)
239 (define (euclidean-length x y)
240 (sqrt (+ (* x x) (* y y))))
242 (let ((x-width (euclidean-length slope (/ beam-thickness slope)))
243 (height (* width slope)))
244 (format #f "~a draw_repeat_slash"
245 (numbers->string4 (list x-width width height)))))
247 ;; restore color from stack
248 (define (resetcolor) "setrgbcolor\n")
250 (define (round-filled-box left right bottom top blotdiam)
251 (let* ((halfblot (/ blotdiam 2))
252 (x (- halfblot left))
253 (width (- right (+ halfblot x)))
254 (y (- halfblot bottom))
255 (height (- top (+ halfblot y))))
256 (format #f "~a draw_round_box"
258 (list width height x y blotdiam)))))
260 ;; save current color on stack and set new color
261 (define (setcolor r g b)
262 (format #f "currentrgbcolor ~a setrgbcolor\n"
263 (numbers->string4 (list r g b))))
265 (define (text font s)
266 ;; (ly:warning (_ "TEXT backend-command encountered in Pango backend"))
267 ;; (ly:warning (_ "Arguments: ~a ~a"" font str))
269 (let* ((space-length (cdar (ly:text-dimension font " ")))
270 (space-move (string-append (number->string space-length)
271 ;; how much precision do we need here?
273 (out-vec (decode-byte-string s)))
276 (ps-font-command font) " "
284 (string-append "/" (symbol->string sym) " glyphshow")))
290 (define (url-link url x y)
291 (format #f "~$ ~$ ~$ ~$ (~a) mark_URI"
298 (define (utf-8-string pango-font-description string)
299 (ly:warning (_ "utf-8-string encountered in PS backend")))
302 (define (zigzag-line centre? zzw zzh thick dx dy)
303 (format #f "~a ~a ~a ~a 0 0 ~a ~a draw_zigzag_line"
304 (if centre? "true" "false")