1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2011 Jan Nieuwenhuizen <janneke@gnu.org>
4 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
6 ;;;; LilyPond is free software: you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation, either version 3 of the License, or
9 ;;;; (at your option) any later version.
11 ;;;; LilyPond is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 ;;;; Note: currently misused as testbed for titles with markup, see
20 ;;;; input/test/title-markup.ly
23 ;;;; * %% Papersize in (header ...)
24 ;;;; * text setting, kerning.
25 ;;;; * document output-interface
27 (define-module (scm output-ps)
38 ;;; helper functions, not part of output interface
42 ;; ice-9 format uses a lot of memory
43 ;; using simple-format almost halves lilypond cell usage
46 (if (or (nan? num) (inf? num))
48 (ly:warning (_ "Found infinity or nan in output. Substituting 0.0"))
49 (if (ly:get-option 'strict-infinity-checking)
52 (ly:number->string num)))
54 (define (number-pair->string4 numpair)
55 (ly:format "~4l" numpair))
58 ;;; Lily output interface, PostScript implementation --- cleanup and docme
62 (define (bezier-sandwich lst thick)
63 (ly:format "~l ~4f draw_bezier_sandwich"
64 (map number-pair->string4 lst)
68 (ly:format "~a (\\~a) show"
69 (ps-font-command font)
70 (ly:inexact->string i 8)))
72 (define (circle radius thick fill)
74 "~a ~4f ~4f draw_circle"
80 (define (dashed-line thick on off dx dy phase)
81 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
89 ;; what the heck is this interface ?
90 (define (dashed-slur thick on off l)
91 (ly:format "~l ~4f [ ~4f ~4f ] 0 draw_dashed_slur"
92 (let ((control-points (append (cddr l) (list (car l) (cadr l)))))
93 (map number-pair->string4 control-points))
98 (define (dot x y radius)
99 (ly:format " ~4l draw_dot" (list radius x y)))
101 (define (draw-line thick x1 y1 x2 y2)
102 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
106 (define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill)
107 (ly:format "~a ~a ~4f ~4f ~4f ~4f ~4f draw_partial_ellipse"
108 (if fill "true" "false")
109 (if connect "true" "false")
116 (define (ellipse x-radius y-radius thick fill)
118 "~a ~4f ~4f ~4f draw_ellipse"
122 x-radius y-radius thick))
124 (define (embedded-ps string)
127 (define (glyph-string postscript-font-name
132 (define (glyph-spec w x y g)
133 (let ((prefix (if (string? g) "/" "")))
134 (ly:format "~4f ~4f ~4f ~a~a"
140 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
144 "/~a ~a output-scale div selectfont
149 (string-join (map (lambda (x) (apply glyph-spec x))
150 (reverse w-x-y-named-glyphs)) "\n")
151 (length w-x-y-named-glyphs)))
154 (define (grob-cause offset grob)
155 (if (ly:get-option 'point-and-click)
156 (let* ((cause (ly:grob-property grob 'cause))
157 (music-origin (if (ly:stream-event? cause)
158 (ly:event-property cause 'origin)))
159 (point-and-click (ly:get-option 'point-and-click)))
161 (ly:input-location? music-origin)
162 (cond ((boolean? point-and-click) point-and-click)
163 ((symbol? point-and-click)
164 (ly:in-event-class? cause point-and-click))
165 (else (any (lambda (t)
166 (ly:in-event-class? cause t))
168 (let* ((location (ly:input-file-line-char-column music-origin))
169 (raw-file (car location))
170 (file (if (is-absolute? raw-file)
172 (string-append (ly-getcwd) "/" raw-file)))
173 (x-ext (ly:grob-extent grob grob X))
174 (y-ext (ly:grob-extent grob grob Y)))
176 (if (and (< 0 (interval-length x-ext))
177 (< 0 (interval-length y-ext)))
178 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
179 (+ (car offset) (car x-ext))
180 (+ (cdr offset) (car y-ext))
181 (+ (car offset) (cdr x-ext))
182 (+ (cdr offset) (cdr y-ext))
184 ;; Backslashes are not valid
185 ;; file URI path separators.
186 (ly:string-percent-encode
187 (ly:string-substitute "\\" "/" file))
196 (define (named-glyph font glyph)
197 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
198 (ps-font-command font)
204 (define (oval x-radius y-radius thick fill)
206 "~a ~4f ~4f ~4f draw_oval"
210 x-radius y-radius thick))
212 (define (placebox x y s)
213 (if (not (string-null? s))
214 (ly:format "~4f ~4f moveto ~a\n" x y s)
217 (define (polygon points blot-diameter filled?)
218 (ly:format "~a ~4l ~a ~4f draw_polygon"
219 (if filled? "true" "false")
221 (- (/ (length points) 2) 1)
224 (define (repeat-slash width slope beam-thickness)
225 (define (euclidean-length x y)
226 (sqrt (+ (* x x) (* y y))))
228 (let ((x-width (euclidean-length beam-thickness (/ beam-thickness slope)))
229 (height (* width slope)))
230 (ly:format "~4l draw_repeat_slash"
231 (list x-width width height))))
234 (define (round-filled-box left right bottom top blotdiam)
235 (let* ((halfblot (/ blotdiam 2))
236 (x (- halfblot left))
237 (width (- right (+ halfblot x)))
238 (y (- halfblot bottom))
239 (height (- top (+ halfblot y))))
240 (ly:format "~4l draw_round_box"
241 (list width height x y blotdiam))))
243 ;; save current color on stack and set new color
244 (define (setcolor r g b)
245 (ly:format "gsave ~4l setrgbcolor\n"
248 ;; restore color from stack
249 (define (resetcolor) "grestore\n")
251 ;; rotation around given point
252 (define (setrotation ang x y)
253 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
256 (list (* -1 x) (* -1 y))))
258 (define (resetrotation ang x y)
264 (define (url-link url x y)
265 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add (~a) mark_URI"
272 (define (page-link page-no x y)
273 (if (number? page-no)
274 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add ~a mark_page_link"
282 (define* (path thickness exps #:optional (cap 'round) (join 'round) (fill? #f))
283 (define (convert-path-exps exps)
290 ((memq head '(rmoveto rlineto lineto moveto)) 2)
291 ((memq head '(rcurveto curveto)) 6)
292 ((eq? head 'closepath) 0)
294 (args (take rest arity))
297 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
302 (convert-path-exps (drop rest arity))))
305 (let ((cap-numeric (case cap ((butt) 0) ((round) 1) ((square) 2)
307 (ly:warning (_ "unknown line-cap-style: ~S")
308 (symbol->string cap))
310 (join-numeric (case join ((miter) 0) ((round) 1) ((bevel) 2)
312 (ly:warning (_ "unknown line-join-style: ~S")
313 (symbol->string join))
316 "gsave currentpoint translate
317 ~a setlinecap ~a setlinejoin ~a setlinewidth
318 ~l gsave stroke grestore ~a grestore"
322 (convert-path-exps exps)
323 (if fill? "fill" ""))))
325 (define (setscale x y)
326 (ly:format "gsave ~4l scale\n"