Last active
December 15, 2015 01:38
-
-
Save Sciss/5181292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~jar = "itextpdf-5.4.0.jar"; | |
~url = "http://search.maven.org/remotecontent?filepath=com/itextpdf/itextpdf/5.4.0/" ++ ~jar; | |
~tmp = PathName.tmp ++ ~jar | |
("curl" + ~url + ">" ++ ~tmp).systemCmd; | |
g = SwingOSC.default; | |
g.reboot; | |
g.addClasses("file:" ++ ~tmp); | |
GUI.swing; | |
x = Array.fill(1000, { 1.0.bilinrand }); | |
GUI.skin[\plot].background = Color.white; | |
p = x.plot("Plot", Rect(0, 0, 1000, 500)); | |
p.resolution = 0.1; | |
v = p.interactionView; | |
( | |
~pdfScreenShot = { arg view, fileName, width, height; | |
var g, bounds, pageSize, doc, writerC, stream, writer, cb, tp, g2, viewJ, vw, vh; | |
g = view.server; | |
bounds = view.bounds; | |
vw = bounds.width; | |
vh = bounds.height; | |
width = width ? vw; | |
height = height ? vh; | |
pageSize = JavaObject("com.itextpdf.text.Rectangle", g, 0, 0, width, height); | |
doc = JavaObject("com.itextpdf.text.Document", g, pageSize, 0, 0, 0, 0); | |
writerC = JavaObject.getClass("com.itextpdf.text.pdf.PdfWriter", g); | |
stream = JavaObject("java.io.FileOutputStream", g, fileName); | |
writer = writerC.getInstance__( doc, stream); | |
doc.open; | |
cb = writer.getDirectContent__; | |
tp = cb.createTemplate__(vw, vh); | |
g2 = tp.createGraphics__(vw, vh); | |
viewJ = JavaObject.basicNew(view.id, g); | |
viewJ.paint(g2); | |
g2.dispose; | |
cb.addTemplate(tp, (width - vw) / 2, (height - vh) / 2); // centered | |
doc.close; | |
doc.destroy; cb.destroy; g2.destroy; tp.destroy; | |
writer.destroy; stream.destroy; writerC.destroy; pageSize.destroy; | |
}; | |
) | |
~pdf = "~/Desktop/test.pdf".standardizePath; | |
~pdfScreenShot.(v, ~pdf); | |
("open" + ~pdf).unixCmd; // this works on OS X only |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment