KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > samples > FontSample


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.ContentBuilder;
14 import it.stefanochizzolini.clown.documents.contents.fonts.Font;
15 import it.stefanochizzolini.clown.documents.contents.fonts.OpenTypeFont;
16 import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
17 import it.stefanochizzolini.clown.files.File;
18 import it.stefanochizzolini.clown.files.SerializationModeEnum;
19 import it.stefanochizzolini.clown.objects.PdfName;
20 import java.awt.geom.Point2D JavaDoc;
21 import java.awt.geom.Rectangle2D JavaDoc;
22 import java.io.RandomAccessFile JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.awt.geom.RectangularShape JavaDoc;
27
28 /**
29   This sample merely checks the font support within PDF Clown.
30   <h3>Remarks</h3>
31   <p>Next versions will enrich its contents to fully demontrate its capabilities.</p>
32 */

33 public class FontSample
34   implements ISample
35 {
36   private static final String JavaDoc Text_BottomLine = "OpenType/TrueType sample";
37   private static final String JavaDoc Text_TopLine = "Standard Type 1 sample";
38
39   public void run(
40     PDFClownSampleLoader loader
41     )
42   {
43     File file = null;
44
45     // 1. Document editing.
46
try
47     {
48       // Instantiate a new PDF file!
49
file = new File();
50       Document document = file.getDocument();
51
52       // Assign a common resources collection!
53
Resources resources = new Resources(document); // Instantiate the resources collection inside the document context.
54
document.setResources(resources); // Put the resources collection in the common resources role.
55

56       // Set default page size (A4)!
57
document.setPageSize(PageFormat.getSize());
58
59       // Add the page to the document!
60
Page page = new Page(document); // Instantiate the page inside the document context.
61
document.getPages().add(page); // Put the page in the pages collection.
62

63       // Add a content stream to the page!
64
/*
65         NOTE: content streams are interdependent chunks of graphics contents that
66         overlap on the same canvas accordingly to their respective position in the
67         page's collection, i.e. the latter the higher (foreground), the ealier the
68         lower (background).
69       */

70       ContentStream content = new ContentStream(document);
71       page.getContents().add(content);
72       // Create a content fragment for the content stream!
73
ContentBuilder contentBuilder = new ContentBuilder(content,new Buffer());
74       contentBuilder.beginLocalState();
75       contentBuilder.setFillColor(
76         new DeviceRGBColor(115f/255,164f/255,232f/255)
77         );
78       // Add the font to the document resource collection!
79
Fonts fonts = new Fonts(document);
80       resources.setFonts(fonts);
81
82       contentBuilder.beginText();
83       PdfName fontName = new PdfName("secondfont");
84       Font font = new StandardType1Font(
85         document,
86         StandardType1Font.FamilyNameEnum.Courier,
87         true,
88         false
89         );
90       fonts.put(fontName,font);
91       contentBuilder.setFont(fontName,40);
92       contentBuilder.showText(
93         Text_TopLine,
94         new Point2D.Double JavaDoc(0,0)
95         ); contentBuilder.translateTextToNextLine();
96       contentBuilder.endText();
97
98       contentBuilder.beginText();
99       fontName = new PdfName("Times");
100       font = new OpenTypeFont(
101         document,
102         new FileInputStream(
103           new RandomAccessFile JavaDoc(loader.getInputPath() + java.io.File.separator + "fonts" + java.io.File.separator + "angelina.ttf","r")
104           )
105         );
106       fonts.put(fontName,font);
107       contentBuilder.setFont(fontName,48);
108       double height = font.getLineHeight(48);
109       double width = font.getKernedWidth(Text_BottomLine,48);
110       contentBuilder.showText(
111         Text_BottomLine,
112         new Point2D.Double JavaDoc((page.getWidth() - width)/2, page.getHeight() - height)
113         );
114       contentBuilder.endText();
115
116       contentBuilder.setStrokeColor(
117         new DeviceRGBColor(115f/255,164f/255,232f/255)
118         );
119       contentBuilder.drawRectangle(
120         new Rectangle2D.Double JavaDoc(
121           (page.getWidth() - width)/2,
122           page.getHeight()-height,
123           width,
124           height
125           )
126       );
127       contentBuilder.stroke();
128       contentBuilder.endLocalState();
129       // Append the content fragment to the content stream!
130
content.append(contentBuilder.getBuffer());
131     }
132     catch(Exception JavaDoc e)
133     {throw new RuntimeException JavaDoc(e);}
134
135     // 2. Serialization.
136
loader.serialize(file,"FontSample",false);
137   }
138 }
Popular Tags