trying SVG element, but no success, it does not draw
This commit is contained in:
parent
49332d85d5
commit
159fee4967
17 changed files with 205 additions and 66 deletions
|
@ -9,10 +9,10 @@ jna.version = 5.10.0
|
||||||
zxing.version = 3.4.1
|
zxing.version = 3.4.1
|
||||||
reflections.version = 0.9.11
|
reflections.version = 0.9.11
|
||||||
jfreechart.version = 1.5.2
|
jfreechart.version = 1.5.2
|
||||||
batik.version = 1.14
|
echosvg.version = 0.1.2
|
||||||
junit.version = 5.8.1
|
junit.version = 5.8.1
|
||||||
junit4.version = 4.13.2
|
junit4.version = 4.13.2
|
||||||
cglib.version = 3.3.0
|
cglib.version = 3.3.0
|
||||||
objenesis.version = 2.6
|
objenesis.version = 2.6
|
||||||
log4j.version = 2.14.0
|
log4j.version = 2.15.0
|
||||||
xbib-content.version = 4.0.0
|
xbib-content.version = 4.0.0
|
||||||
|
|
|
@ -2,6 +2,7 @@ dependencies {
|
||||||
api project(':graphics-pdfbox')
|
api project(':graphics-pdfbox')
|
||||||
api project(':graphics-barcode')
|
api project(':graphics-barcode')
|
||||||
api "org.xbib:settings-datastructures:${project.property('xbib-content.version')}"
|
api "org.xbib:settings-datastructures:${project.property('xbib-content.version')}"
|
||||||
|
//api "io.sf.carte:echosvg-bridge:${project.property('echosvg.version')}"
|
||||||
runtimeOnly "org.xbib:settings-datastructures-json:${project.property('xbib-content.version')}"
|
runtimeOnly "org.xbib:settings-datastructures-json:${project.property('xbib-content.version')}"
|
||||||
runtimeOnly "org.xbib:settings-datastructures-yaml:${project.property('xbib-content.version')}"
|
runtimeOnly "org.xbib:settings-datastructures-yaml:${project.property('xbib-content.version')}"
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,4 +18,6 @@ module org.xbib.graphics.layout.pdfbox {
|
||||||
requires org.xbib.settings.datastructures;
|
requires org.xbib.settings.datastructures;
|
||||||
requires transitive java.desktop;
|
requires transitive java.desktop;
|
||||||
requires java.logging;
|
requires java.logging;
|
||||||
|
requires io.sf.carte.echosvg.anim;
|
||||||
|
requires io.sf.carte.echosvg.bridge;
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,8 +113,10 @@ public class BarcodeElement implements Element, Drawable, Dividable, WidthRespec
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
public void draw(PDDocument pdDocument,
|
||||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
PDPageContentStream contentStream,
|
||||||
|
Position upperLeft,
|
||||||
|
DrawListener drawListener) throws IOException {
|
||||||
float x = upperLeft.getX();
|
float x = upperLeft.getX();
|
||||||
float y = upperLeft.getY() - getHeight();
|
float y = upperLeft.getY() - getHeight();
|
||||||
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdDocument, getWidth(), getHeight());
|
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdDocument, getWidth(), getHeight());
|
||||||
|
|
|
@ -22,36 +22,47 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
|
||||||
*/
|
*/
|
||||||
public final static float SCALE_TO_RESPECT_WIDTH = -1f;
|
public final static float SCALE_TO_RESPECT_WIDTH = -1f;
|
||||||
|
|
||||||
private final BufferedImage image;
|
private BufferedImage image;
|
||||||
|
|
||||||
private float width;
|
private float width;
|
||||||
|
|
||||||
private float height;
|
private float height;
|
||||||
|
|
||||||
private float scale;
|
private float scaleX = 1.0f;
|
||||||
|
|
||||||
|
private float scaleY = 1.0f;
|
||||||
|
|
||||||
private float maxWidth = -1;
|
private float maxWidth = -1;
|
||||||
|
|
||||||
private Position absolutePosition;
|
private Position absolutePosition;
|
||||||
|
|
||||||
public ImageElement(String base64) throws IOException {
|
public ImageElement() {
|
||||||
this(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageElement(BufferedImage image) {
|
public void setImage(String base64) throws IOException {
|
||||||
|
setImage(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64))));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImage(BufferedImage image) {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
this.width = image.getWidth();
|
this.width = image.getWidth();
|
||||||
this.height = image.getHeight();
|
this.height = image.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScale(float scale) {
|
public void setScaleX(float scaleX) {
|
||||||
this.scale = scale;
|
this.scaleX = scaleX;
|
||||||
setWidth(width * scale);
|
|
||||||
setHeight(height * scale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getScale() {
|
public float getScaleX() {
|
||||||
return scale;
|
return scaleX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScaleY(float scaleY) {
|
||||||
|
this.scaleY = scaleY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getScaleY() {
|
||||||
|
return scaleY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,7 +84,7 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
|
||||||
}
|
}
|
||||||
return image.getWidth();
|
return image.getWidth();
|
||||||
}
|
}
|
||||||
return width;
|
return width * scaleX;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,7 +107,7 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
|
||||||
}
|
}
|
||||||
return image.getHeight();
|
return image.getHeight();
|
||||||
}
|
}
|
||||||
return height;
|
return height * scaleY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -135,9 +146,9 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
|
||||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||||
float x = upperLeft.getX();
|
float x = upperLeft.getX();
|
||||||
float y = upperLeft.getY() - height;
|
float y = upperLeft.getY() - getHeight();
|
||||||
PDImageXObject imageXObject = LosslessFactory.createFromImage(pdDocument, image);
|
PDImageXObject imageXObject = LosslessFactory.createFromImage(pdDocument, image);
|
||||||
contentStream.drawImage(imageXObject, x, y, width, height);
|
contentStream.drawImage(imageXObject, x, y, getWidth(), getHeight());
|
||||||
if (drawListener != null) {
|
if (drawListener != null) {
|
||||||
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
|
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,90 @@
|
||||||
|
package org.xbib.graphics.pdfbox.layout.elements;
|
||||||
|
|
||||||
|
/*
|
||||||
|
import io.sf.carte.echosvg.anim.dom.SAXSVGDocumentFactory;
|
||||||
|
import io.sf.carte.echosvg.bridge.BridgeContext;
|
||||||
|
import io.sf.carte.echosvg.bridge.DocumentLoader;
|
||||||
|
import io.sf.carte.echosvg.bridge.GVTBuilder;
|
||||||
|
import io.sf.carte.echosvg.bridge.UserAgent;
|
||||||
|
import io.sf.carte.echosvg.bridge.UserAgentAdapter;
|
||||||
|
import io.sf.carte.echosvg.gvt.GraphicsNode;
|
||||||
|
*/
|
||||||
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||||
|
import org.apache.pdfbox.util.Matrix;
|
||||||
|
import org.xbib.graphics.pdfbox.PdfBoxGraphics2D;
|
||||||
|
import org.xbib.graphics.pdfbox.color.DefaultColorMapper;
|
||||||
|
import org.xbib.graphics.pdfbox.color.RGBtoCMYKColorMapper;
|
||||||
|
import org.xbib.graphics.pdfbox.font.DefaultFontDrawer;
|
||||||
|
import org.xbib.graphics.pdfbox.font.FontDrawer;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.text.Position;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.xml.sax.XMLReader;
|
||||||
|
|
||||||
|
import java.awt.color.ICC_Profile;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
public class SVGElement extends ImageElement {
|
||||||
|
|
||||||
|
private String svg;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setImage(String svg) {
|
||||||
|
this.svg = svg;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||||
|
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||||
|
if (svg == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
float x = upperLeft.getX();
|
||||||
|
float y = upperLeft.getY() - getHeight();
|
||||||
|
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdDocument, getWidth(), getHeight());
|
||||||
|
ICC_Profile icc_profile = ICC_Profile.getInstance(PDDocument.class.getResourceAsStream(
|
||||||
|
"/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc"));
|
||||||
|
DefaultColorMapper colorMapper = new RGBtoCMYKColorMapper(icc_profile, pdDocument);
|
||||||
|
pdfBoxGraphics2D.setColorMapper(colorMapper);
|
||||||
|
FontDrawer fontDrawer = new DefaultFontDrawer();
|
||||||
|
pdfBoxGraphics2D.setFontTextDrawer(fontDrawer);
|
||||||
|
pdfBoxGraphics2D.scale(getScaleX(), getScaleY());
|
||||||
|
//load(svg).paint(pdfBoxGraphics2D);
|
||||||
|
pdfBoxGraphics2D.dispose();
|
||||||
|
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
|
||||||
|
Matrix matrix = new Matrix();
|
||||||
|
matrix.translate(x, y);
|
||||||
|
contentStream.saveGraphicsState();
|
||||||
|
contentStream.transform(matrix);
|
||||||
|
contentStream.drawForm(xFormObject);
|
||||||
|
contentStream.restoreGraphicsState();
|
||||||
|
if (drawListener != null) {
|
||||||
|
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Drawable removeLeadingEmptyVerticalSpace() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*private GraphicsNode load(String svg) throws IOException {
|
||||||
|
try {
|
||||||
|
XMLReader xmlReader = SAXParserFactory.newDefaultInstance().newSAXParser().getXMLReader();
|
||||||
|
SAXSVGDocumentFactory documentFactory = new SAXSVGDocumentFactory(xmlReader);
|
||||||
|
Document document = documentFactory.createDocument(null, new StringReader(svg));
|
||||||
|
UserAgent userAgent = new UserAgentAdapter();
|
||||||
|
DocumentLoader documentLoader = new DocumentLoader(userAgent);
|
||||||
|
BridgeContext bridgeContext = new BridgeContext(userAgent, documentLoader);
|
||||||
|
bridgeContext.setDynamicState(BridgeContext.STATIC);
|
||||||
|
GVTBuilder gvtBuilder = new GVTBuilder();
|
||||||
|
return gvtBuilder.build(bridgeContext, document);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package org.xbib.graphics.pdfbox.layout.script.command;
|
package org.xbib.graphics.pdfbox.layout.script.command;
|
||||||
|
|
||||||
import org.xbib.graphics.pdfbox.layout.elements.ImageElement;
|
import org.xbib.graphics.pdfbox.layout.elements.ImageElement;
|
||||||
|
import org.xbib.graphics.pdfbox.layout.elements.SVGElement;
|
||||||
import org.xbib.graphics.pdfbox.layout.elements.render.VerticalLayoutHint;
|
import org.xbib.graphics.pdfbox.layout.elements.render.VerticalLayoutHint;
|
||||||
import org.xbib.graphics.pdfbox.layout.script.Engine;
|
import org.xbib.graphics.pdfbox.layout.script.Engine;
|
||||||
import org.xbib.graphics.pdfbox.layout.script.State;
|
import org.xbib.graphics.pdfbox.layout.script.State;
|
||||||
|
@ -16,7 +17,14 @@ import static org.xbib.graphics.pdfbox.layout.util.PdfUtil.mmToPt;
|
||||||
public class ImageCommand implements Command {
|
public class ImageCommand implements Command {
|
||||||
@Override
|
@Override
|
||||||
public void execute(Engine engine, State state, Settings settings) throws IOException {
|
public void execute(Engine engine, State state, Settings settings) throws IOException {
|
||||||
ImageElement imageElement = new ImageElement(settings.get("value"));
|
ImageElement imageElement = new ImageElement();
|
||||||
|
if (settings.containsSetting("value")) {
|
||||||
|
imageElement.setImage(settings.get("value"));
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("svg")) {
|
||||||
|
imageElement = new SVGElement();
|
||||||
|
imageElement.setImage(settings.get("svg"));
|
||||||
|
}
|
||||||
if (settings.containsSetting("x") && settings.containsSetting("y")) {
|
if (settings.containsSetting("x") && settings.containsSetting("y")) {
|
||||||
imageElement.setAbsolutePosition(new Position(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", 0f))));
|
imageElement.setAbsolutePosition(new Position(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", 0f))));
|
||||||
}
|
}
|
||||||
|
@ -26,8 +34,11 @@ public class ImageCommand implements Command {
|
||||||
if (settings.containsSetting("height")) {
|
if (settings.containsSetting("height")) {
|
||||||
imageElement.setHeight(settings.getAsFloat("height", imageElement.getHeight()));
|
imageElement.setHeight(settings.getAsFloat("height", imageElement.getHeight()));
|
||||||
}
|
}
|
||||||
if (settings.containsSetting("scale")) {
|
if (settings.containsSetting("scalex")) {
|
||||||
imageElement.setScale(settings.getAsFloat("scale", imageElement.getScale()));
|
imageElement.setScaleX(settings.getAsFloat("scalex", 1.0f));
|
||||||
|
}
|
||||||
|
if (settings.containsSetting("scaley")) {
|
||||||
|
imageElement.setScaleY(settings.getAsFloat("scaley", 1.0f));
|
||||||
}
|
}
|
||||||
Alignment alignment = Alignment.valueOf(settings.get("alignment", "left").toUpperCase(Locale.ROOT));
|
Alignment alignment = Alignment.valueOf(settings.get("alignment", "left").toUpperCase(Locale.ROOT));
|
||||||
String margin = settings.get("margin", "0 0 0 0");
|
String margin = settings.get("margin", "0 0 0 0");
|
||||||
|
|
|
@ -18,8 +18,10 @@ public class HelloCatTest {
|
||||||
Paragraph paragraph = new Paragraph();
|
Paragraph paragraph = new Paragraph();
|
||||||
paragraph.addText("Hello Cat", 12, BaseFont.HELVETICA);
|
paragraph.addText("Hello Cat", 12, BaseFont.HELVETICA);
|
||||||
document.add(paragraph);
|
document.add(paragraph);
|
||||||
ImageElement imageElement = new ImageElement(ImageIO.read(getClass().getResourceAsStream("cat.jpg")));
|
ImageElement imageElement = new ImageElement();
|
||||||
imageElement.setScale(0.1f);
|
imageElement.setImage(ImageIO.read(getClass().getResourceAsStream("cat.jpg")));
|
||||||
|
imageElement.setScaleX(0.1f);
|
||||||
|
imageElement.setScaleY(0.1f);
|
||||||
document.add(imageElement, new VerticalLayoutHint(Alignment.LEFT, 10, 10, 10, 10, true));
|
document.add(imageElement, new VerticalLayoutHint(Alignment.LEFT, 10, 10, 10, 10, true));
|
||||||
document.render().save(new FileOutputStream("build/hellocat.pdf")).close();
|
document.render().save(new FileOutputStream("build/hellocat.pdf")).close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,12 @@ public class LetterTest {
|
||||||
ImageElement image;
|
ImageElement image;
|
||||||
if (new File("arrow.png").exists()) {
|
if (new File("arrow.png").exists()) {
|
||||||
BufferedImage arrowImage = ImageIO.read(new File("arrow.png"));
|
BufferedImage arrowImage = ImageIO.read(new File("arrow.png"));
|
||||||
image = new ImageElement(arrowImage);
|
image = new ImageElement();
|
||||||
|
image.setImage(arrowImage);
|
||||||
} else {
|
} else {
|
||||||
BufferedImage arrowImage = ImageIO.read(getClass().getResourceAsStream("arrow.png"));
|
BufferedImage arrowImage = ImageIO.read(getClass().getResourceAsStream("arrow.png"));
|
||||||
image = new ImageElement(arrowImage);
|
image = new ImageElement();
|
||||||
|
image.setImage(arrowImage);
|
||||||
}
|
}
|
||||||
image.setWidth(image.getWidth() / 7);
|
image.setWidth(image.getWidth() / 7);
|
||||||
image.setHeight(image.getHeight() / 7);
|
image.setHeight(image.getHeight() / 7);
|
||||||
|
|
|
@ -23,7 +23,6 @@ public class ScriptTest {
|
||||||
engine.close();
|
engine.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void deckblatt() throws Exception {
|
public void deckblatt() throws Exception {
|
||||||
Settings settings = Settings.settingsBuilder()
|
Settings settings = Settings.settingsBuilder()
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,5 @@
|
||||||
dependencies {
|
dependencies {
|
||||||
api "org.apache.pdfbox:pdfbox:${project.property('pdfbox.version')}"
|
api "org.apache.pdfbox:pdfbox:${project.property('pdfbox.version')}"
|
||||||
testImplementation "org.jfree:jfreechart:${project.property('jfreechart.version')}"
|
testImplementation "org.jfree:jfreechart:${project.property('jfreechart.version')}"
|
||||||
testImplementation "org.apache.xmlgraphics:batik-swing:${project.property('batik.version')}"
|
testImplementation "io.sf.carte:echosvg-bridge:${project.property('echosvg.version')}"
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,15 +74,15 @@ class PdfBoxGraphics2DTestBase {
|
||||||
}
|
}
|
||||||
exporter.draw(pdfBoxGraphics2D);
|
exporter.draw(pdfBoxGraphics2D);
|
||||||
pdfBoxGraphics2D.dispose();
|
pdfBoxGraphics2D.dispose();
|
||||||
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
|
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
|
||||||
Matrix matrix = new Matrix();
|
Matrix matrix = new Matrix();
|
||||||
matrix.translate(0, 20);
|
matrix.translate(0, 20);
|
||||||
contentStream.transform(matrix);
|
contentStream.transform(matrix);
|
||||||
contentStream.drawForm(appearanceStream);
|
contentStream.drawForm(xFormObject);
|
||||||
matrix.scale(1.5f, 1.5f);
|
matrix.scale(1.5f, 1.5f);
|
||||||
matrix.translate(0, 100);
|
matrix.translate(0, 100);
|
||||||
contentStream.transform(matrix);
|
contentStream.transform(matrix);
|
||||||
contentStream.drawForm(appearanceStream);
|
contentStream.drawForm(xFormObject);
|
||||||
contentStream.close();
|
contentStream.close();
|
||||||
}
|
}
|
||||||
document.save(new File(parentDir, name + ".pdf"));
|
document.save(new File(parentDir, name + ".pdf"));
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
package org.xbib.graphics.pdfbox.test;
|
package org.xbib.graphics.pdfbox.test;
|
||||||
|
|
||||||
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
|
import io.sf.carte.echosvg.anim.dom.SAXSVGDocumentFactory;
|
||||||
import org.apache.batik.bridge.BridgeContext;
|
import io.sf.carte.echosvg.bridge.BridgeContext;
|
||||||
import org.apache.batik.bridge.DocumentLoader;
|
import io.sf.carte.echosvg.bridge.DocumentLoader;
|
||||||
import org.apache.batik.bridge.GVTBuilder;
|
import io.sf.carte.echosvg.bridge.GVTBuilder;
|
||||||
import org.apache.batik.bridge.UserAgent;
|
import io.sf.carte.echosvg.bridge.UserAgent;
|
||||||
import org.apache.batik.bridge.UserAgentAdapter;
|
import io.sf.carte.echosvg.bridge.UserAgentAdapter;
|
||||||
import org.apache.batik.gvt.GraphicsNode;
|
import io.sf.carte.echosvg.gvt.GraphicsNode;
|
||||||
import org.apache.batik.util.XMLResourceDescriptor;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
@ -63,8 +62,7 @@ public class RenderSVGsTest extends PdfBoxGraphics2DTestBase {
|
||||||
|
|
||||||
private void renderSVG(String name, final double scale) throws IOException {
|
private void renderSVG(String name, final double scale) throws IOException {
|
||||||
String uri = RenderSVGsTest.class.getResource(name).toString();
|
String uri = RenderSVGsTest.class.getResource(name).toString();
|
||||||
String parser = XMLResourceDescriptor.getXMLParserClassName();
|
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory();
|
||||||
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
|
|
||||||
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
|
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
|
||||||
UserAgent userAgent = new UserAgentAdapter();
|
UserAgent userAgent = new UserAgentAdapter();
|
||||||
DocumentLoader loader = new DocumentLoader(userAgent);
|
DocumentLoader loader = new DocumentLoader(userAgent);
|
||||||
|
@ -78,29 +76,27 @@ public class RenderSVGsTest extends PdfBoxGraphics2DTestBase {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderSVGCMYK(String name, final double scale) throws IOException {
|
private void renderSVGCMYK(String name, double scale) throws IOException {
|
||||||
String uri = RenderSVGsTest.class.getResource(name).toString();
|
String uri = RenderSVGsTest.class.getResource(name).toString();
|
||||||
String parser = XMLResourceDescriptor.getXMLParserClassName();
|
SAXSVGDocumentFactory documentFactory = new SAXSVGDocumentFactory();
|
||||||
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
|
Document document = documentFactory.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
|
||||||
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
|
|
||||||
UserAgent userAgent = new UserAgentAdapter();
|
UserAgent userAgent = new UserAgentAdapter();
|
||||||
DocumentLoader loader = new DocumentLoader(userAgent);
|
DocumentLoader documentLoader = new DocumentLoader(userAgent);
|
||||||
BridgeContext bctx = new BridgeContext(userAgent, loader);
|
BridgeContext bridgeContext = new BridgeContext(userAgent, documentLoader);
|
||||||
bctx.setDynamicState(BridgeContext.STATIC);
|
bridgeContext.setDynamicState(BridgeContext.STATIC);
|
||||||
GVTBuilder builder = new GVTBuilder();
|
GVTBuilder gvtBuilder = new GVTBuilder();
|
||||||
final GraphicsNode gvtRoot = builder.build(bctx, document);
|
GraphicsNode graphicsNode = gvtBuilder.build(bridgeContext, document);
|
||||||
PDDocument pdfDocument = new PDDocument();
|
PDDocument pdfDocument = new PDDocument();
|
||||||
|
ICC_Profile icc_profile = ICC_Profile.getInstance(PDDocument.class.getResourceAsStream(
|
||||||
|
"/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc"));
|
||||||
|
DefaultColorMapper colorMapper = new RGBtoCMYKColorMapper(icc_profile, pdfDocument);
|
||||||
File parentDir = new File("build/test/svg");
|
File parentDir = new File("build/test/svg");
|
||||||
parentDir.mkdirs();
|
parentDir.mkdirs();
|
||||||
PDPage page = new PDPage(PDRectangle.A4);
|
PDPage page = new PDPage(PDRectangle.A4);
|
||||||
pdfDocument.addPage(page);
|
pdfDocument.addPage(page);
|
||||||
PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
|
PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
|
||||||
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdfDocument, 400, 400);
|
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"));
|
|
||||||
DefaultColorMapper colorMapper = new RGBtoCMYKColorMapper(icc_profile, pdfDocument);
|
|
||||||
pdfBoxGraphics2D.setColorMapper(colorMapper);
|
pdfBoxGraphics2D.setColorMapper(colorMapper);
|
||||||
FontDrawer fontDrawer;
|
|
||||||
contentStream.beginText();
|
contentStream.beginText();
|
||||||
contentStream.setStrokingColor(0.0f, 0.0f, 0.0f, 1.0f);
|
contentStream.setStrokingColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
contentStream.setNonStrokingColor(0.0f, 0.0f, 0.0f, 1.0f);
|
contentStream.setNonStrokingColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
@ -108,16 +104,16 @@ public class RenderSVGsTest extends PdfBoxGraphics2DTestBase {
|
||||||
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
|
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
|
||||||
contentStream.showText("Mode: CMYK colorspace");
|
contentStream.showText("Mode: CMYK colorspace");
|
||||||
contentStream.endText();
|
contentStream.endText();
|
||||||
fontDrawer = new DefaultFontDrawer();
|
FontDrawer fontDrawer = new DefaultFontDrawer();
|
||||||
pdfBoxGraphics2D.setFontTextDrawer(fontDrawer);
|
pdfBoxGraphics2D.setFontTextDrawer(fontDrawer);
|
||||||
pdfBoxGraphics2D.scale(scale, scale);
|
pdfBoxGraphics2D.scale(scale, scale);
|
||||||
gvtRoot.paint(pdfBoxGraphics2D);
|
graphicsNode.paint(pdfBoxGraphics2D);
|
||||||
pdfBoxGraphics2D.dispose();
|
pdfBoxGraphics2D.dispose();
|
||||||
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
|
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
|
||||||
Matrix matrix = new Matrix();
|
Matrix matrix = new Matrix();
|
||||||
matrix.translate(0, 300);
|
matrix.translate(0, 300);
|
||||||
contentStream.transform(matrix);
|
contentStream.transform(matrix);
|
||||||
contentStream.drawForm(appearanceStream);
|
contentStream.drawForm(xFormObject);
|
||||||
contentStream.close();
|
contentStream.close();
|
||||||
String baseName = name.substring(0, name.lastIndexOf('.'));
|
String baseName = name.substring(0, name.lastIndexOf('.'));
|
||||||
pdfDocument.save(new File(parentDir, baseName + ".pdf"));
|
pdfDocument.save(new File(parentDir, baseName + ".pdf"));
|
||||||
|
|
|
@ -8,7 +8,7 @@ public class SVGGraphics2DProvider implements VectorGraphics2DProvider<SVGGraphi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name() {
|
public String name() {
|
||||||
return "eps";
|
return "svg";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -8,6 +8,8 @@ import org.xbib.graphics.io.vector.filters.StateChangeGroupingFilter;
|
||||||
import org.xbib.graphics.io.vector.PageSize;
|
import org.xbib.graphics.io.vector.PageSize;
|
||||||
|
|
||||||
public class SVGProcessor implements Processor {
|
public class SVGProcessor implements Processor {
|
||||||
|
|
||||||
|
@Override
|
||||||
public ProcessorResult process(Iterable<Command<?>> commands, PageSize pageSize) {
|
public ProcessorResult process(Iterable<Command<?>> commands, PageSize pageSize) {
|
||||||
FillPaintedShapeAsImageFilter shapesAsImages = new FillPaintedShapeAsImageFilter(commands);
|
FillPaintedShapeAsImageFilter shapesAsImages = new FillPaintedShapeAsImageFilter(commands);
|
||||||
Iterable<Command<?>> filtered = new StateChangeGroupingFilter(shapesAsImages);
|
Iterable<Command<?>> filtered = new StateChangeGroupingFilter(shapesAsImages);
|
||||||
|
|
|
@ -5,10 +5,11 @@ import java.awt.Color;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
public abstract class AbstractTest {
|
public abstract class AbstractTest {
|
||||||
|
@ -41,9 +42,8 @@ public abstract class AbstractTest {
|
||||||
referenceGraphics.clearRect(0, 0, reference.getWidth(), reference.getHeight());
|
referenceGraphics.clearRect(0, 0, reference.getWidth(), reference.getHeight());
|
||||||
referenceGraphics.setColor(Color.BLACK);
|
referenceGraphics.setColor(Color.BLACK);
|
||||||
draw(referenceGraphics);
|
draw(referenceGraphics);
|
||||||
File referenceImage = File.createTempFile(getClass().getName() + ".reference", ".png");
|
Path path = Files.createTempFile(Paths.get("build"), getClass().getName() + ".reference", "png");
|
||||||
referenceImage.deleteOnExit();
|
ImageIO.write(reference, "png", path.toFile());
|
||||||
ImageIO.write(reference, "png", referenceImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void draw(Graphics2D g);
|
public abstract void draw(Graphics2D g);
|
||||||
|
|
Loading…
Reference in a new issue