1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 1998--2012 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 (ly:format "~a (\\~a) show"
63 (ps-font-command font)
64 (ly:inexact->string i 8)))
66 (define (circle radius thick fill)
68 "~a ~4f ~4f draw_circle"
74 (define (start-enclosing-id-node s)
77 (define (end-enclosing-id-node)
80 (define (dashed-line thick on off dx dy phase)
81 (ly:format "~4f ~4f ~4f [ ~4f ~4f ] ~4f draw_dashed_line"
89 (define (draw-line thick x1 y1 x2 y2)
90 (ly:format "~4f ~4f ~4f ~4f ~4f draw_line"
94 (define (partial-ellipse x-radius y-radius start-angle end-angle thick connect fill)
95 (ly:format "~a ~a ~4f ~4f ~4f ~4f ~4f draw_partial_ellipse"
96 (if fill "true" "false")
97 (if connect "true" "false")
104 (define (ellipse x-radius y-radius thick fill)
106 "~a ~4f ~4f ~4f draw_ellipse"
110 x-radius y-radius thick))
112 (define (embedded-ps string)
115 (define (glyph-string pango-font
121 (define (glyph-spec w h x y g) ; h not used
122 (let ((prefix (if (string? g) "/" "")))
123 (ly:format "~4f ~4f ~4f ~a~a"
129 "/~a /CIDFont findresource ~a output-scale div scalefont setfont
133 "/~a ~a output-scale div selectfont
138 (string-join (map (lambda (x) (apply glyph-spec x))
139 (reverse w-x-y-named-glyphs)) "\n")
140 (length w-x-y-named-glyphs)))
143 (define (grob-cause offset grob)
144 (if (ly:get-option 'point-and-click)
145 (let* ((cause (ly:grob-property grob 'cause))
146 (music-origin (if (ly:stream-event? cause)
147 (ly:event-property cause 'origin)))
148 (point-and-click (ly:get-option 'point-and-click)))
150 (ly:input-location? music-origin)
151 (cond ((boolean? point-and-click) point-and-click)
152 ((symbol? point-and-click)
153 (ly:in-event-class? cause point-and-click))
154 (else (any (lambda (t)
155 (ly:in-event-class? cause t))
157 (let* ((location (ly:input-file-line-char-column music-origin))
158 (raw-file (car location))
159 (file (if (is-absolute? raw-file)
161 (string-append (ly-getcwd) "/" raw-file)))
162 (x-ext (ly:grob-extent grob grob X))
163 (y-ext (ly:grob-extent grob grob Y)))
165 (if (and (< 0 (interval-length x-ext))
166 (< 0 (interval-length y-ext)))
167 (ly:format "~4f ~4f ~4f ~4f (textedit://~a:~a:~a:~a) mark_URI\n"
168 (+ (car offset) (car x-ext))
169 (+ (cdr offset) (car y-ext))
170 (+ (car offset) (cdr x-ext))
171 (+ (cdr offset) (cdr y-ext))
173 ;; Backslashes are not valid
174 ;; file URI path separators.
175 (ly:string-percent-encode
176 (ly:string-substitute "\\" "/" file))
180 (1+ (cadddr location)))
185 (define (named-glyph font glyph)
186 (ly:format "~a /~a glyphshow " ;;Why is there a space at the end?
187 (ps-font-command font)
193 (define (placebox x y s)
194 (if (not (string-null? s))
195 (ly:format "~4f ~4f moveto ~a\n" x y s)
198 (define (polygon points blot-diameter filled?)
199 (ly:format "~a ~4l ~a ~4f draw_polygon"
200 (if filled? "true" "false")
202 (- (/ (length points) 2) 1)
205 (define (round-filled-box left right bottom top blotdiam)
206 (let* ((halfblot (/ blotdiam 2))
207 (x (- halfblot left))
208 (width (- right (+ halfblot x)))
209 (y (- halfblot bottom))
210 (height (- top (+ halfblot y))))
211 (ly:format "~4l draw_round_box"
212 (list width height x y blotdiam))))
214 ;; save current color on stack and set new color
215 (define (setcolor r g b)
216 (ly:format "gsave ~4l setrgbcolor\n"
219 ;; restore color from stack
220 (define (resetcolor) "grestore\n")
222 ;; rotation around given point
223 (define (setrotation ang x y)
224 (ly:format "gsave ~4l translate ~a rotate ~4l translate\n"
227 (list (* -1 x) (* -1 y))))
229 (define (resetrotation ang x y)
235 (define (url-link url x y)
236 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add (~a) mark_URI"
243 (define (page-link page-no x y)
244 (if (number? page-no)
245 (ly:format "~a ~a currentpoint vector_add ~a ~a currentpoint vector_add ~a mark_page_link"
253 (define* (path thickness exps #:optional (cap 'round) (join 'round) (fill? #f))
254 (define (convert-path-exps exps)
261 ((memq head '(rmoveto rlineto lineto moveto)) 2)
262 ((memq head '(rcurveto curveto)) 6)
263 ((eq? head 'closepath) 0)
265 (args (take rest arity))
268 ;; WARNING: this is a vulnerability: a user can output arbitrary PS code here.
273 (convert-path-exps (drop rest arity))))
276 (let ((cap-numeric (case cap ((butt) 0) ((round) 1) ((square) 2)
278 (ly:warning (_ "unknown line-cap-style: ~S")
279 (symbol->string cap))
281 (join-numeric (case join ((miter) 0) ((round) 1) ((bevel) 2)
283 (ly:warning (_ "unknown line-join-style: ~S")
284 (symbol->string join))
287 "gsave currentpoint translate
288 ~a setlinecap ~a setlinejoin ~a setlinewidth
289 ~l gsave stroke grestore ~a grestore"
293 (convert-path-exps exps)
294 (if fill? "fill" ""))))
296 (define (setscale x y)
297 (ly:format "gsave ~4l scale\n"