| 1 |
// Copyright 2005, FreeHEP. |
| 2 |
package org.freehep.graphicsio.pdf.test; |
| 3 |
|
| 4 |
import java.awt.Rectangle; |
| 5 |
import java.awt.Font; |
| 6 |
import java.awt.font.FontRenderContext; |
| 7 |
import java.io.*; |
| 8 |
import java.util.Calendar; |
| 9 |
|
| 10 |
import org.freehep.graphics2d.font.CharTable; |
| 11 |
import org.freehep.graphics2d.font.Lookup; |
| 12 |
import org.freehep.graphicsio.font.FontEmbedder; |
| 13 |
import org.freehep.graphicsio.pdf.PDFFontEmbedderType1; |
| 14 |
import org.freehep.graphicsio.pdf.PDFFontEmbedderType3; |
| 15 |
import org.freehep.graphicsio.pdf.PDFWriter; |
| 16 |
import org.freehep.graphicsio.pdf.PDFDocInfo; |
| 17 |
import org.freehep.graphicsio.pdf.PDFCatalog; |
| 18 |
import org.freehep.graphicsio.pdf.PDFViewerPreferences; |
| 19 |
import org.freehep.graphicsio.pdf.PDFDictionary; |
| 20 |
import org.freehep.graphicsio.pdf.PDFPageTree; |
| 21 |
import org.freehep.graphicsio.pdf.PDFPage; |
| 22 |
import org.freehep.graphicsio.pdf.PDFStream; |
| 23 |
import org.freehep.graphicsio.pdf.PDFRedundanceTracker; |
| 24 |
|
| 25 |
public class TestPDFFontEmbedding { |
| 26 |
|
| 27 |
public static void main(String[] argv) { |
| 28 |
|
| 29 |
try { |
| 30 |
|
| 31 |
PDFWriter pdf = new PDFWriter(new FileOutputStream("fonttest.pdf")); |
| 32 |
pdf.comment("file generated by TestFontEmbedding, Freehep lib"); |
| 33 |
|
| 34 |
// info |
| 35 |
PDFDocInfo info = pdf.openDocInfo("DocInfo"); |
| 36 |
info.setTitle("Font Embedding Test"); |
| 37 |
info.setAuthor("Simon Fischer"); |
| 38 |
info.setSubject("Test File of the Font Embedding of the FreeHEP library"); |
| 39 |
info.setKeywords("PDFWriter; FreeHEP"); |
| 40 |
info.setCreator(TestPDFFontEmbedding.class.getName()); |
| 41 |
info.setCreationDate(Calendar.getInstance()); |
| 42 |
pdf.close(info); |
| 43 |
|
| 44 |
// catalog |
| 45 |
PDFCatalog catalog = pdf.openCatalog("Catalog", "RootPage"); |
| 46 |
catalog.setPageMode("UseOutlines"); |
| 47 |
catalog.setViewerPreferences("Preferences"); |
| 48 |
catalog.setOpenAction(new Object[] { pdf.ref("FontPage"), pdf.name("Fit")}); |
| 49 |
pdf.close(catalog); |
| 50 |
|
| 51 |
// preferences |
| 52 |
PDFViewerPreferences prefs = pdf.openViewerPreferences("Preferences"); |
| 53 |
prefs.setFitWindow(true); |
| 54 |
prefs.setCenterWindow(false); |
| 55 |
pdf.close(prefs); |
| 56 |
|
| 57 |
// pages |
| 58 |
PDFPageTree pages = pdf.openPageTree("RootPage", null); |
| 59 |
pages.addPage("FontPage"); |
| 60 |
pages.setMediaBox(0, 0, 612, 792); |
| 61 |
pages.setResources("Resources"); |
| 62 |
pdf.close(pages); |
| 63 |
|
| 64 |
// page |
| 65 |
PDFPage fontPage = pdf.openPage("FontPage", "RootPage"); |
| 66 |
fontPage.setContents("FontPageContent"); |
| 67 |
pdf.close(fontPage); |
| 68 |
|
| 69 |
// first page content |
| 70 |
PDFStream pageStream = pdf.openStream("FontPageContent", |
| 71 |
new String[] { "Flate", "ASCIIHex" }); |
| 72 |
|
| 73 |
pageStream.move(100,100); |
| 74 |
pageStream.line(200,200); |
| 75 |
pageStream.stroke(); |
| 76 |
|
| 77 |
pageStream.beginText(); |
| 78 |
pageStream.font(pdf.name("F1"), 12); |
| 79 |
pageStream.text(100, 200); |
| 80 |
pageStream.show("ABC abc!"); |
| 81 |
pageStream.endText(); |
| 82 |
|
| 83 |
CharTable table = Lookup.getInstance().getTable("PDFLatin"); |
| 84 |
String str = ""; |
| 85 |
double y = 500; |
| 86 |
for (int i=1; i<256; i++) { |
| 87 |
String name = table.toName(i); |
| 88 |
if (name != null) |
| 89 |
str += table.toUnicode(name); |
| 90 |
else |
| 91 |
str += "#"; |
| 92 |
if ((str.length() % 32 == 31) || (i == 255)) { |
| 93 |
pageStream.beginText(); |
| 94 |
pageStream.font(pdf.name("F1"), 12); |
| 95 |
pageStream.text(100, y); |
| 96 |
pageStream.show(str); |
| 97 |
pageStream.endText(); |
| 98 |
System.out.println(str); |
| 99 |
str = ""; |
| 100 |
y -= 20; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
pdf.close(pageStream); |
| 105 |
|
| 106 |
|
| 107 |
// resources |
| 108 |
PDFDictionary resources = pdf.openDictionary("Resources"); |
| 109 |
resources.entry("ProcSet", pdf.ref("OurPageProcSet")); |
| 110 |
PDFDictionary fontList = resources.openDictionary("Font"); |
| 111 |
fontList.entry("F1", pdf.ref("MyFont")); |
| 112 |
resources.close(fontList); |
| 113 |
pdf.close(resources); |
| 114 |
|
| 115 |
pdf.object("OurPageProcSet", new Object[] { pdf.name("PDF") }); |
| 116 |
|
| 117 |
PDFRedundanceTracker tracker = new PDFRedundanceTracker(pdf); |
| 118 |
|
| 119 |
// font embedding |
| 120 |
switch (3) { |
| 121 |
case 1: |
| 122 |
PDFDictionary fontDict = pdf.openDictionary("MyFont"); |
| 123 |
fontDict.entry("Type", pdf.name("Font")); |
| 124 |
fontDict.entry("Subtype", pdf.name("Type1")); |
| 125 |
fontDict.entry("Name", pdf.name("F1")); |
| 126 |
fontDict.entry("BaseFont", pdf.name("Helvetica")); |
| 127 |
pdf.close(fontDict); |
| 128 |
break; |
| 129 |
|
| 130 |
case 2: |
| 131 |
Font font = new Font("Monotype Corsiva", Font.PLAIN, 1000); |
| 132 |
FontRenderContext context = new FontRenderContext(null, true, true); |
| 133 |
FontEmbedder fe = new PDFFontEmbedderType3(context, pdf, "MyFont", tracker); |
| 134 |
fe.includeFont(font, Lookup.getInstance().getTable("PDFLatin"), "F1"); |
| 135 |
break; |
| 136 |
|
| 137 |
case 3: |
| 138 |
font = new Font("Impact", Font.PLAIN, 1000); |
| 139 |
context = new FontRenderContext(null, true, true); |
| 140 |
fe = PDFFontEmbedderType1.create(context, pdf, "MyFont", tracker); |
| 141 |
fe.includeFont(font, Lookup.getInstance().getTable("PDFLatin"), "F1"); |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
tracker.writeAll(); |
| 146 |
|
| 147 |
pdf.close(); |
| 148 |
System.exit(0); |
| 149 |
} catch (Exception e) { |
| 150 |
e.printStackTrace(); |
| 151 |
} |
| 152 |
} |
| 153 |
} |