package cc.glsn.v15.print; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.Random; import java.util.Scanner; import javax.imageio.ImageIO; public class PrettyPageImageCreator { public static final int PageWidth=1536; public static final int PageHeight=1897; public static final String ImageFileDir="c:/trash/images"; public static final String FontName="Lucida Console"; public static final int FontSizeHead=28; public static final int FontSizeText=22; LinkedList PageList; LinkedList Lines; Random PRNG; String Title; String Client; int JobID; int PageTotal; ArrayList Remarks; String DateStr; public PrettyPageImageCreator(String Title, String Client,InputStream IS) throws IOException { this(Title,Client,readInputStream(IS)); } public PrettyPageImageCreator(String Title, String Client, String Doc) throws IOException { DateStr=new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()); PRNG=new Random(); setupRemarks(); PageList=new LinkedList(); Scanner DocScanner=new Scanner(Doc); JobID=PRNG.nextInt(); this.Title=Title; this.Client=Client; PageTotal=0; Lines=new LinkedList(); while(DocScanner.hasNextLine()) { Lines.add(DocScanner.nextLine()); } while(Lines.size()>0) { new Page().createPage(); } for(Page P : PageList) { P.updatePageNo(); //P.save(); } } public static void main(String Args[]) throws Exception { PrettyPageImageCreator IC=new PrettyPageImageCreator("testdoc.cpp","clash",new FileInputStream("c:/trash/prob2.cpp")); } public LinkedList getImages() { LinkedList LL=new LinkedList(); for(Page P : PageList) { LL.add(P.BI); } return LL; } private void setupRemarks() { Remarks=new ArrayList(); Remarks.add("Never pet a burning dog."); Remarks.add("Laziness is counter-revolutionary."); Remarks.add("Is that a dangling pointer or are you happy to see me?"); Remarks.add("Alex is the coolest guy I know."); Remarks.add("pithy: tersely cogent"); Remarks.add("Soup is good food."); Remarks.add("Program for great justice!"); Remarks.add("Move Zig!"); Remarks.add("It pays to be obvious, especially if you have a reputation for subtlety. -Isaac Asimov"); Remarks.add("I don't give a damn for a man that can only spell a word one way. -Samuel Clemens"); Remarks.add("I have never let my schooling interfere with my education. -Samuel Clemens"); Remarks.add("A person should want to live, if only out of curiosity."); Remarks.add("He who knows does not speak. He who speaks does not know. -Lao-tzu"); Remarks.add("Your ad here"); Remarks.add("map > > >"); Remarks.add("My hat has three corners."); Remarks.add("Vinh Is Not Hairy"); Remarks.add("Maybe you should solve some problems?"); Remarks.add("The bug is on this page."); Remarks.add("I hear the only place you're ever invited is outside."); Remarks.add("Dude, WTF?"); Remarks.add("I am here to do homework and kick ass. And I just finished my homework."); Remarks.add("I hate winipeg."); Remarks.add("bigger than a duck of equal size"); Remarks.add("If all the girls who attended the Yale prom were laid end to end, I wouldn't be a bit surprised. -Dorothy Parker"); Remarks.add("This is not a novel to be tossed aside lightly. It should be thrown with great force. -Dorothy Parker"); Remarks.add("I don't care what is written about me so long as it isn't true. -Dorothy Parker"); Remarks.add("If you want to know what God thinks of money, just look at the people he gave it to. -Dorothy Parker"); Remarks.add("It is a mistake to think you can solve any major problems just with potatoes. -Douglas Adams"); Remarks.add("The ships hung in the sky in much the same way that bricks don't. -Douglas Adams"); Remarks.add("Time is an illusion. Lunchtime doubly so. -Douglas Adams"); Remarks.add("Every normal man must be tempted at times to spit on his hands, hoist the black flag, and begin to slit throats."); Remarks.add("Nothing you can't spell will ever work. -Will Rogers"); Remarks.add("We don't know what we want, but we are ready to bite somebody to get it. -Will Rogers"); Remarks.add("I was gratified to be able to answer promptly. I said I don't know. -Samual Clemens"); Remarks.add("The man who doesn't read good books has no advantage over the man who can't read them. -Samuel Clemens"); Remarks.add("When I was younger, I could remember anything, whether it had happened or not. -Samuel Clemens"); Remarks.add("The rule is perfect: in all matters of opinion our adversaries are insane. -Samuel Clemens"); Remarks.add("All you need in this life is ignorance and confidence; then success is sure. -Samuel Clemens"); //Remarks.add(""); Remarks.add("zootie-zoot-zoot"); Remarks.add("A lot of people are afraid of heights. Not me. I'm afraid of widths. -Steve Wright"); Remarks.add("If value corrupts then absolute value corrupts absolutely"); Remarks.add("Don't you feel more like you do now than you did when you came in?"); Remarks.add("Does a recursive function have budda nature?"); Remarks.add("For every x, where x>5, I'll cut you."); } private String getRemark() { int i=PRNG.nextInt(Remarks.size()); return Remarks.get(i); } private class Page { BufferedImage BI; Graphics2D G; int PageNo; Font FontHead; Font FontText; int PageMargin; int HeadSpace; int TextSpace; int LinePad; FontRenderContext FRC; public Page() { BI=new BufferedImage(PageWidth,PageHeight,BufferedImage.TYPE_4BYTE_ABGR ); G=BI.createGraphics(); G.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); G.setColor(Color.WHITE); G.fillRect(0,0,PageWidth,PageHeight); FontHead=new Font(FontName,Font.PLAIN,FontSizeHead); FontText=new Font(FontName,Font.PLAIN,FontSizeText); FRC= G.getFontRenderContext(); { Rectangle2D R2d=FontHead.getStringBounds(Title + Client + " Page01234567890/[]",FRC); PageMargin=(int)Math.round( R2d.getHeight()*1.5 ); HeadSpace=(int)Math.round( R2d.getHeight()*1.0 ); } { Rectangle2D R=FontText.getStringBounds("ABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvwxyz[],.!/?()#*{}|",FRC); TextSpace=(int)Math.round(R.getHeight()*1.0); LinePad=(int)Math.round(R.getHeight() * 0.2); } PageList.add(this); } public void save() throws IOException { String Filename=ImageFileDir + "/" + Client + "-" + Title.replace(".","_") + "-" + JobID +"-page" + PageNo + ".png"; File F=new File(Filename); F.delete(); ImageIO.write(BI,"PNG",F); } private void drawRemarks() { G.setColor(Color.BLUE); G.setFont(FontText); String Remark=getRemark(); G.drawString(Remark,HeadSpace,PageHeight-HeadSpace); } private void drawMargin() { G.setColor(Color.BLUE); //Draw page Margin G.drawLine(PageMargin,PageMargin, PageWidth-PageMargin,PageMargin); G.drawLine(PageMargin,PageMargin, PageMargin,PageHeight-PageMargin); G.drawLine(PageMargin,PageHeight-PageMargin,PageWidth-PageMargin,PageHeight-PageMargin); G.drawLine(PageWidth-PageMargin,PageMargin, PageWidth-PageMargin,PageHeight-PageMargin); } private void createPage() { PageTotal++; PageNo=PageTotal; //drawMargin(); drawRemarks(); G.setColor(Color.BLACK); G.setFont(FontHead); G.drawString(Client + " " + DateStr ,HeadSpace, HeadSpace ); { Rectangle2D R=FontHead.getStringBounds(Title,FRC); G.drawString(Title,Math.round(PageWidth/2.0 - R.getWidth()/2.0),HeadSpace); } int CurrY=PageMargin + TextSpace; int StartX=PageMargin + TextSpace; G.setFont(FontText); double MaxY=PageHeight*1.0 - PageMargin*1.0 - TextSpace*0.5 - LinePad*1.0; while((CurrY < MaxY) && (Lines.size()>0)) { String Line=Lines.getFirst(); Lines.removeFirst(); Line=Line.replace("\t"," "); Rectangle2D R=FontText.getStringBounds(Line,FRC); //System.out.println(Line + " " + R.getWidth()); if (R.getWidth() + StartX > PageWidth - PageMargin - TextSpace) { int idx=Line.length(); while(true) { idx--; String SubLine=Line.substring(0,idx); R=FontText.getStringBounds(SubLine,FRC); //System.out.println(SubLine + " " + R.getWidth()); if (R.getWidth() + StartX <= PageWidth - PageMargin - TextSpace) { String LineReturn=Line.substring(idx); Lines.addFirst(LineReturn); Line=SubLine; break; } } } G.drawString(Line,StartX,CurrY); CurrY+=TextSpace + LinePad; } } private void updatePageNo() { G.setColor(Color.BLACK); G.setFont(FontHead); String Pg="Page " + PageNo + "/" + PageTotal; Rectangle2D R=FontHead.getStringBounds(Pg,FRC); G.drawString(Pg,Math.round(PageWidth - R.getWidth() - HeadSpace),HeadSpace); } } private static String readInputStream(InputStream IS) throws IOException { StringBuilder SB=new StringBuilder(); byte[] Buff=new byte[10240]; while(true) { int sz=IS.read(Buff,0,10240); if (sz==-1) { break; } else { if (sz>0) { char[] C=new char[sz]; for(int i=0; i