forked from rototor/pdfbox-graphics2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderSVGsTest.java
152 lines (127 loc) · 5.71 KB
/
RenderSVGsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package de.rototor.pdfbox.graphics2d;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.bridge.*;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.util.Matrix;
import org.junit.Test;
import org.w3c.dom.Document;
import java.awt.*;
import java.awt.color.ICC_Profile;
import java.io.File;
import java.io.IOException;
public class RenderSVGsTest extends PdfBoxGraphics2DTestBase
{
@Test
public void testSVGs() throws IOException
{
renderSVG("barChart.svg", 0.45);
renderSVG("gump-bench.svg", 1);
renderSVG("json.svg", 150);
renderSVG("heart.svg", 200);
renderSVG("displayWebStats.svg", 200);
renderSVG("compuserver_msn_Ford_Focus.svg", 0.7);
renderSVG("watermark.svg", 0.4);
}
@Test
public void testGradientSVGEmulateObjectBoundingBox() throws IOException
{
renderSVG("long-gradient.svg", 0.55);
renderSVG("tall-gradient.svg", 0.33);
renderSVG("near-square-gradient.svg", 0.30);
renderSVG("square-gradient.svg", 0.55);
renderSVG("tall-gradient-downward-slope.svg", 0.33);
renderSVG("horizontal-gradient.svg", 0.55);
}
@Test
public void testSVGinCMYKColorspace() throws IOException
{
renderSVGCMYK("atmospheric-composiition.svg", 0.7);
}
private void renderSVG(String name, final double scale) throws IOException
{
String uri = RenderSVGsTest.class.getResource(name).toString();
// create the document
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
// create the GVT
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext bctx = new BridgeContext(userAgent, loader);
bctx.setDynamicState(BridgeContext.STATIC);
GVTBuilder builder = new GVTBuilder();
final GraphicsNode gvtRoot = builder.build(bctx, document);
this.exportGraphic("svg", name.replace(".svg", ""), new GraphicsExporter()
{
@Override
public void draw(Graphics2D gfx)
{
gfx.scale(scale, scale);
gvtRoot.paint(gfx);
}
});
}
private void renderSVGCMYK(String name, final double scale) throws IOException
{
String uri = RenderSVGsTest.class.getResource(name).toString();
// create the document
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
// create the GVT
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext bctx = new BridgeContext(userAgent, loader);
bctx.setDynamicState(BridgeContext.STATIC);
GVTBuilder builder = new GVTBuilder();
final GraphicsNode gvtRoot = builder.build(bctx, document);
try {
PDDocument pdfDocument = new PDDocument();
File parentDir = new File("target/test/svg");
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
PDPage page = new PDPage(PDRectangle.A4);
pdfDocument.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdfDocument, 400, 400);
ICC_Profile icc_profile = ICC_Profile.getInstance(
PDDocument.class.getResourceAsStream("/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc")
);
PdfBoxGraphics2DColorMapper colorMapper = new RGBtoCMYKColorMapper(icc_profile, pdfDocument);
pdfBoxGraphics2D.setColorMapper(colorMapper);
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
contentStream.beginText();
contentStream.setStrokingColor(0.0f, 0.0f, 0.0f, 1.0f);
contentStream.setNonStrokingColor(0.0f, 0.0f, 0.0f, 1.0f);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
contentStream.showText("Mode: CMYK colorspace");
contentStream.endText();
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
if (fontTextDrawer != null) {
pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
}
pdfBoxGraphics2D.scale(scale, scale);
gvtRoot.paint(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 300);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
String baseName = name.substring(0, name.lastIndexOf('.'));
pdfDocument.save(new File(parentDir, baseName + ".pdf"));
pdfDocument.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}