1 package it.stefanochizzolini.clown.samples; 2 3 import it.stefanochizzolini.clown.bytes.Buffer; 4 import it.stefanochizzolini.clown.bytes.FileInputStream; 5 import it.stefanochizzolini.clown.bytes.OutputStream; 6 import it.stefanochizzolini.clown.documents.Document; 7 import it.stefanochizzolini.clown.documents.Page; 8 import it.stefanochizzolini.clown.documents.PageFormat; 9 import it.stefanochizzolini.clown.documents.contents.ContentStream; 10 import it.stefanochizzolini.clown.documents.contents.Fonts; 11 import it.stefanochizzolini.clown.documents.contents.Resources; 12 import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor; 13 import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum; 14 import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum; 15 import it.stefanochizzolini.clown.documents.contents.composition.BlockFilter; 16 import it.stefanochizzolini.clown.documents.contents.composition.ContentBuilder; 17 import it.stefanochizzolini.clown.documents.contents.fonts.Font; 18 import it.stefanochizzolini.clown.documents.contents.fonts.OpenTypeFont; 19 import it.stefanochizzolini.clown.documents.interchange.metadata.Information; 20 import it.stefanochizzolini.clown.files.File; 21 import it.stefanochizzolini.clown.files.SerializationModeEnum; 22 import it.stefanochizzolini.clown.objects.PdfName; 23 24 import java.awt.Dimension ; 25 import java.awt.geom.Point2D ; 26 import java.awt.geom.Rectangle2D ; 27 import java.io.BufferedOutputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.RandomAccessFile ; 31 import java.util.Date ; 32 33 36 public class TypesettingSample 37 implements ISample 38 { 39 private static final PdfName FontName_Body = new PdfName("body"); 42 private static final PdfName FontName_Decorative = new PdfName("decorative"); 43 private static final PdfName FontName_Emphasis = new PdfName("emphasis"); 44 45 private static final int Margin_X = 100; 46 private static final int Margin_Y = 50; 47 50 public void run( 54 PDFClownSampleLoader loader 55 ) 56 { 57 File file = new File(); 59 60 build(file,loader); 62 63 loader.serialize(file,this.getClass().getSimpleName(),false); 65 } 66 68 private void build( 70 File file, 71 PDFClownSampleLoader loader 72 ) 73 { 74 try 75 { 76 80 Document document = file.getDocument(); 81 82 build_resources(document,loader); 84 85 document.setPageSize(PageFormat.getSize()); 87 88 build_content(document); 90 91 build_metadata(document); 93 } 94 catch(Exception e) 95 {throw new RuntimeException (e);} 96 } 97 98 private void build_content( 99 Document document 100 ) 101 throws Exception 102 { 103 Page page = new Page(document); document.getPages().add(page); 107 114 ContentStream content = new ContentStream(document); page.getContents().add(content); 120 ContentBuilder contentBuilder = new ContentBuilder(content,new Buffer()); 121 133 BlockFilter blockFilter = new BlockFilter(contentBuilder); 134 135 contentBuilder.beginLocalState(); 136 contentBuilder.setFillColor( 137 new DeviceRGBColor(115f/255,164f/255,232f/255) 138 ); 139 Rectangle2D frame = new Rectangle2D.Double ( 141 Margin_X, 142 Margin_Y, 143 page.getWidth() - Margin_X * 2, 144 page.getHeight() - Margin_Y * 2 145 ); 146 contentBuilder.setStrokeColor( 147 new DeviceRGBColor(0,89f/255,156f/255) 148 ); 149 contentBuilder.drawRectangle(frame); 150 contentBuilder.stroke(); 151 blockFilter.begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top); 153 contentBuilder.setFont(FontName_Decorative,56); 154 blockFilter.showText("Chapter 1"); 155 blockFilter.showBreak(); 156 blockFilter.showText("Down the Rabbit-Hole"); 157 blockFilter.end(); 159 frame = new Rectangle2D.Double ( 161 blockFilter.getBoundBox().getX(), 162 blockFilter.getBoundBox().getY() + blockFilter.getBoundBox().getHeight(), 163 blockFilter.getBoundBox().getWidth(), 164 page.getHeight() - Margin_Y - (blockFilter.getBoundBox().getY() + blockFilter.getBoundBox().getHeight()) 165 ); 166 blockFilter.begin(frame,AlignmentXEnum.Justify,AlignmentYEnum.Bottom); 168 contentBuilder.setFont(FontName_Decorative,28); 169 blockFilter.showText("A"); 170 contentBuilder.setFont(FontName_Body,12); 171 blockFilter.showText("lice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?'"); 172 Dimension breakSize = new Dimension (24,0); blockFilter.showBreak(breakSize); 176 contentBuilder.setFont(FontName_Decorative,28); 177 blockFilter.showText("S"); 178 contentBuilder.setFont(FontName_Body,12); 179 blockFilter.showText("o she was considering in her own mind "); 180 contentBuilder.setFont(FontName_Emphasis,12); 181 blockFilter.showText("(as well as she could, for the hot day made her feel very sleepy and stupid)"); 182 contentBuilder.setFont(FontName_Body,12); 183 blockFilter.showText(", whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her."); 184 blockFilter.showBreak(breakSize); 186 contentBuilder.setFont(FontName_Decorative,28); 187 blockFilter.showText("T"); 188 contentBuilder.setFont(FontName_Body,12); 189 blockFilter.showText("here was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' "); 190 contentBuilder.setFont(FontName_Emphasis,12); 191 blockFilter.showText("(when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural)"); 192 contentBuilder.setFont(FontName_Body,12); 193 blockFilter.showText("; but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge."); 194 blockFilter.end(); 196 contentBuilder.endLocalState(); 197 content.append(contentBuilder.getBuffer()); 199 } 200 201 private void build_metadata( 202 Document document 203 ) 204 throws Exception 205 { 206 Information info = new Information(document); 207 document.setInformation(info); 208 info.setAuthor("Stefano Chizzolini"); 209 info.setCreationDate(new Date ()); 210 info.setCreator(this.getClass().getName()); 211 info.setTitle("Sample document"); 212 info.setSubject("PDF creation sample"); 213 } 214 215 private void build_resources( 216 Document document, 217 PDFClownSampleLoader loader 218 ) 219 throws Exception 220 { 221 Resources resources = new Resources(document); document.setResources(resources); Fonts fonts = new Fonts(document); resources.setFonts(fonts); Font font = new OpenTypeFont( 228 document, 229 new FileInputStream( 230 new RandomAccessFile (loader.getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "TravelingTypewriter.otf","r") 231 ) 232 ); 233 fonts.put(FontName_Body,font); 234 font = new OpenTypeFont( 235 document, 236 new FileInputStream( 237 new RandomAccessFile (loader.getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "josschrift.ttf","r") 238 ) 239 ); 240 fonts.put(FontName_Emphasis,font); 241 font = new OpenTypeFont( 242 document, 243 new FileInputStream( 244 new RandomAccessFile (loader.getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "CloisterBlack.ttf","r") 245 ) 246 ); 247 fonts.put(FontName_Decorative,font); 248 } 249 } | Popular Tags |