From 340f8d55709fef1ff71e545ed5fa16cfe4b5f53b Mon Sep 17 00:00:00 2001
From: hanwen <hanwen>
Date: Sun, 19 Jun 2005 14:52:33 +0000
Subject: [PATCH] * scm/ps-to-png.scm (make-ps-images): cleanup multipage vs.
 single page switch.

* make/lilypond-vars.make (LILYPOND_BOOK_FLAGS): set
anti-alias-factor for lilypond-book runs.

* scm/ps-to-png.scm (scale-down-image): new function.
(my-system): new function.
(make-ps-images): blow up GS resolution by anti-alias-factor,
scale down image by anti-alias-factor.  This improves appearance
of
(make-ps-images): remove showpage. Fixes spurious empty png at end.

* scm/lily.scm (lambda): default resolution 116 (8 pixels per
space).
---
 ChangeLog               |  13 +++++
 make/lilypond-vars.make |   2 +-
 scm/backend-library.scm |   4 +-
 scm/lily.scm            |   3 +-
 scm/ps-to-png.scm       | 118 ++++++++++++++++++++++++++--------------
 5 files changed, 96 insertions(+), 44 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ff354c8011..683ea3c586 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2005-06-19  Han-Wen Nienhuys  <hanwen@xs4all.nl>
 
+	* scm/ps-to-png.scm (make-ps-images): cleanup multipage vs. single
+	page switch.
+
+	* make/lilypond-vars.make (LILYPOND_BOOK_FLAGS): set
+	anti-alias-factor for lilypond-book runs.
+
+	* scm/ps-to-png.scm (scale-down-image): new function.
+	(my-system): new function.
+	(make-ps-images): blow up GS resolution by anti-alias-factor,
+	scale down image by anti-alias-factor.  This improves appearance
+	of
+	(make-ps-images): remove showpage. Fixes spurious empty png at end.
+
 	* scm/framework-ps.scm (write-preamble): downcase filename before
 	string-matching. Should fix .TTF files (as opposed to ttf files)  
 
diff --git a/make/lilypond-vars.make b/make/lilypond-vars.make
index b099a45d33..448515c619 100644
--- a/make/lilypond-vars.make
+++ b/make/lilypond-vars.make
@@ -18,7 +18,7 @@ CONVERT_LY = $(script-dir)/convert-ly.py
 LILYPOND = $(builddir)/lily/$(outconfbase)/lilypond
 LILYPOND_BOOK = $(script-dir)/lilypond-book.py
 LILYPOND_BOOK_INCLUDES = -I $(pwd) -I $(outdir) -I$(input-dir) -I $(input-dir)/regression/ -I $(input-dir)/test/ -I $(input-dir)/tutorial/ -I $(builddir)/mf/$(outconfbase)/  -I $(builddir)/mf/out/
-LILYPOND_BOOK_FLAGS = --process="$(LILYPOND) --backend=eps --formats=ps,png --header=texidoc -I $(srcdir)/input/test -dinternal-type-checking"
+LILYPOND_BOOK_FLAGS = --process="$(LILYPOND) --backend=eps --formats=ps,png --header=texidoc -I $(srcdir)/input/test -dinternal-type-checking -danti-alias-factor=2 "
 
 
 #texi-html for www only:
diff --git a/scm/backend-library.scm b/scm/backend-library.scm
index d7658ef23f..6943b97610 100644
--- a/scm/backend-library.scm
+++ b/scm/backend-library.scm
@@ -85,6 +85,7 @@
     ))
 
 (use-modules (scm ps-to-png))
+
 (define-public (postscript->png resolution paper-size-name name)
     ;; Do not try to guess the name of the png file,
     ;; GS produces PNG files like BASE-page%d.png.
@@ -94,7 +95,8 @@
 	(verbose (ly:get-option 'verbose))
 	(rename-page-1 #f))
     (ly:message (_ "Converting to ~a...") "PNG")
-    (make-ps-images name resolution paper-size rename-page-1 verbose)
+    (make-ps-images name resolution paper-size rename-page-1 verbose
+		    (ly:get-option 'anti-alias-factor))
     (ly:progress "\n")))
 
 (define-public (postprocess-output paper-book module filename formats)
diff --git a/scm/lily.scm b/scm/lily.scm
index f59bc6a61e..06e41889fc 100644
--- a/scm/lily.scm
+++ b/scm/lily.scm
@@ -22,7 +22,8 @@ similar to chord syntax")
 	    ;; at 101.178, a staff space is exactly 7 pixels.
 	    ;; 115.63 = 8 pixels
 	    ;; 86  = 6 pixels
-	    (resolution 116 "resolution for generating bitmaps")
+	    (resolution 90 "resolution for generating bitmaps")
+	    (anti-alias-factor 1 "blow up resolution and scale to prevent jaggies in PNG")
 	    (preview-include-book-title #t "include book-titles in preview images.")
 	    (gs-font-load #f
 			  "load fonts via Ghostscript.")
diff --git a/scm/ps-to-png.scm b/scm/ps-to-png.scm
index 18462c724a..83f9160385 100644
--- a/scm/ps-to-png.scm
+++ b/scm/ps-to-png.scm
@@ -78,12 +78,48 @@
 	 (map (lambda (x) (string->number (car x))) (vector->list m)))
 	#f)))
 
+
+;; copy of ly:system. ly:* not available via lilypond-ps2png.scm
+(define (my-system be-verbose exit-on-error cmd)
+  (define status 0)
+  (if be-verbose
+      (begin
+	(format (current-error-port) (_ "Invoking `~a'...") cmd)
+	(newline (current-error-port))))
+
+  
+  (set! status (system cmd))
+  
+  (if (not (= status 0))
+      (begin
+	(format (current-error-port)
+		(format #f (_ "~a exited with status: ~S") "GS" status))
+	(if exit-on-error
+	    (exit 1))))
+
+  status)
+
+(define (scale-down-image be-verbose factor file)
+  (let* ((status 0)
+	 (percentage (* 100 (/ 1.0 factor)))
+	 (old (string-append file ".old")))
+  
+  (rename-file file old)
+  (my-system be-verbose
+	     #t
+	     (format #f "convert -scale '~a%' ~a ~a" percentage old file))
+  (delete-file old)
+  ))
+
 (define-public (make-ps-images ps-name . rest)
   (let-optional
    rest ((resolution 90)
 	 (paper-size "a4")
 	 (rename-page-1? #f)
-	 (verbose? #f))
+	 (verbose? #f)
+	 (aa-factor 1) 
+	 )
+   
    (let* ((base (basename (re-sub "[.]e?ps" "" ps-name)))
 	  (header (gulp-port (open-file ps-name "r") 10240))
 	  (png1 (string-append base ".png"))
@@ -94,29 +130,16 @@
 	  (output-file (if multi-page? pngn png1))
 
 	  ;;png16m is because Lily produces color nowadays.
-	  (cmd (if multi-page?
-		   (format #f "~a\
+	  (gs-variable-options
+	    (if multi-page?
+		(format #f "-sPAPERSIZE=~a" paper-size)
+		"-dEPSCrop"))
+	  (cmd (format #f "~a\
  ~a\
- -dGraphicsAlphaBits=4\
- -dNOPAUSE\
- -dTextAlphaBits=4\
- -sDEVICE=png16m\
- -sOutputFile=~S\
- -sPAPERSIZE=~a\
- -r~S\
- ~S\
- -c showpage\
- -c quit"
-			   (search-gs)
-			   (if verbose? "" "-q")
-			   output-file paper-size resolution ps-name)
-		   (format #f "~a\
  ~a\
- -s\
  -dGraphicsAlphaBits=4\
- -dEPSCrop\
- -dNOPAUSE\
  -dTextAlphaBits=4\
+ -dNOPAUSE\
  -sDEVICE=png16m\
  -sOutputFile=~S\
  -r~S\
@@ -124,28 +147,41 @@
  -c quit"
 			   (search-gs)
 			   (if verbose? "" "-q")
-			   output-file resolution ps-name)))
-	  (foo (for-each delete-file (append (dir-re "." png1)
-					     (dir-re "." pngn-re))))
-	  (bar (if verbose?
-		   (begin
-		     (format (current-error-port) (_ "Invoking `~a'...") cmd)
-		     (newline (current-error-port)))))
-	  (baz 
-	   ;; The wrapper on windows cannot handle `=' signs,
-	   ;; gs has a workaround with #.
-	   (if (eq? PLATFORM 'windows)
-	       (begin
-		 (set! cmd (re-sub "=" "#" cmd))
-		 (set! cmd (re-sub "-dSAFER " "" cmd)))))
-	  (status (system cmd)))
-     (if (not (= status 0))
+			   gs-variable-options
+			   output-file 
+			   (* aa-factor resolution) ps-name))
+	  (status 0)
+	  (files '()))
+     
+     (for-each delete-file (append (dir-re "." png1)
+				   (dir-re "." pngn-re)))
+     
+     ;; The wrapper on windows cannot handle `=' signs,
+     ;; gs has a workaround with #.
+     (if (eq? PLATFORM 'windows)
 	 (begin
-	   (map delete-file
-		(append (dir-re "." png1) (dir-re "." pngn-re)))
-	   (format (current-error-port)
-		   (format #f (_ "~a exited with status: ~S") "GS" status))
+	   (set! cmd (re-sub "=" "#" cmd))
+	   (set! cmd (re-sub "-dSAFER " "" cmd))))
+
+     (set! status (my-system verbose? #f cmd))
+
+     (set! files
+	   (append (dir-re "." png1) (dir-re "." pngn-re)))
+
+     (if (not (= 0 status))
+	 (begin
+	   (map delete-file files)
 	   (exit 1)))
+     
      (if (and rename-page-1? multi-page?)
 	 (rename-file (re-sub "%d" "1" pngn) png1))
-     (append (dir-re "." png1) (dir-re "." pngn-re)))))
+     
+     (set! files
+	   (append (dir-re "." png1) (dir-re "." pngn-re)))
+
+     
+     (if (not (= 1 aa-factor))
+	 (for-each  (lambda (f) (scale-down-image verbose? aa-factor f))
+		    files))
+			    
+     files)))
-- 
2.39.5