trying SVG element, but no success, it does not draw

This commit is contained in:
Jörg Prante 2021-12-10 16:34:52 +01:00
parent 49332d85d5
commit 159fee4967
17 changed files with 205 additions and 66 deletions

View file

@ -9,10 +9,10 @@ jna.version = 5.10.0
zxing.version = 3.4.1
reflections.version = 0.9.11
jfreechart.version = 1.5.2
batik.version = 1.14
echosvg.version = 0.1.2
junit.version = 5.8.1
junit4.version = 4.13.2
cglib.version = 3.3.0
objenesis.version = 2.6
log4j.version = 2.14.0
log4j.version = 2.15.0
xbib-content.version = 4.0.0

View file

@ -2,6 +2,7 @@ dependencies {
api project(':graphics-pdfbox')
api project(':graphics-barcode')
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-yaml:${project.property('xbib-content.version')}"
}

View file

@ -18,4 +18,6 @@ module org.xbib.graphics.layout.pdfbox {
requires org.xbib.settings.datastructures;
requires transitive java.desktop;
requires java.logging;
requires io.sf.carte.echosvg.anim;
requires io.sf.carte.echosvg.bridge;
}

View file

@ -113,8 +113,10 @@ public class BarcodeElement implements Element, Drawable, Dividable, WidthRespec
}
@Override
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
Position upperLeft, DrawListener drawListener) throws IOException {
public void draw(PDDocument pdDocument,
PDPageContentStream contentStream,
Position upperLeft,
DrawListener drawListener) throws IOException {
float x = upperLeft.getX();
float y = upperLeft.getY() - getHeight();
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(pdDocument, getWidth(), getHeight());

View file

@ -22,36 +22,47 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
*/
public final static float SCALE_TO_RESPECT_WIDTH = -1f;
private final BufferedImage image;
private BufferedImage image;
private float width;
private float height;
private float scale;
private float scaleX = 1.0f;
private float scaleY = 1.0f;
private float maxWidth = -1;
private Position absolutePosition;
public ImageElement(String base64) throws IOException {
this(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64))));
public ImageElement() {
}
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.width = image.getWidth();
this.height = image.getHeight();
}
public void setScale(float scale) {
this.scale = scale;
setWidth(width * scale);
setHeight(height * scale);
public void setScaleX(float scaleX) {
this.scaleX = scaleX;
}
public float getScale() {
return scale;
public float getScaleX() {
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 width;
return width * scaleX;
}
/**
@ -96,7 +107,7 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
}
return image.getHeight();
}
return height;
return height * scaleY;
}
@Override
@ -135,9 +146,9 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
Position upperLeft, DrawListener drawListener) throws IOException {
float x = upperLeft.getX();
float y = upperLeft.getY() - height;
float y = upperLeft.getY() - getHeight();
PDImageXObject imageXObject = LosslessFactory.createFromImage(pdDocument, image);
contentStream.drawImage(imageXObject, x, y, width, height);
contentStream.drawImage(imageXObject, x, y, getWidth(), getHeight());
if (drawListener != null) {
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
}

View file

@ -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);
}
}*/
}

View file

@ -1,6 +1,7 @@
package org.xbib.graphics.pdfbox.layout.script.command;
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.script.Engine;
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 {
@Override
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")) {
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")) {
imageElement.setHeight(settings.getAsFloat("height", imageElement.getHeight()));
}
if (settings.containsSetting("scale")) {
imageElement.setScale(settings.getAsFloat("scale", imageElement.getScale()));
if (settings.containsSetting("scalex")) {
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));
String margin = settings.get("margin", "0 0 0 0");

View file

@ -18,8 +18,10 @@ public class HelloCatTest {
Paragraph paragraph = new Paragraph();
paragraph.addText("Hello Cat", 12, BaseFont.HELVETICA);
document.add(paragraph);
ImageElement imageElement = new ImageElement(ImageIO.read(getClass().getResourceAsStream("cat.jpg")));
imageElement.setScale(0.1f);
ImageElement imageElement = new ImageElement();
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.render().save(new FileOutputStream("build/hellocat.pdf")).close();
}

View file

@ -25,10 +25,12 @@ public class LetterTest {
ImageElement image;
if (new File("arrow.png").exists()) {
BufferedImage arrowImage = ImageIO.read(new File("arrow.png"));
image = new ImageElement(arrowImage);
image = new ImageElement();
image.setImage(arrowImage);
} else {
BufferedImage arrowImage = ImageIO.read(getClass().getResourceAsStream("arrow.png"));
image = new ImageElement(arrowImage);
image = new ImageElement();
image.setImage(arrowImage);
}
image.setWidth(image.getWidth() / 7);
image.setHeight(image.getHeight() / 7);

View file

@ -23,7 +23,6 @@ public class ScriptTest {
engine.close();
}
@Test
public void deckblatt() throws Exception {
Settings settings = Settings.settingsBuilder()

View file

@ -1,5 +1,5 @@
dependencies {
api "org.apache.pdfbox:pdfbox:${project.property('pdfbox.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')}"
}

View file

@ -74,15 +74,15 @@ class PdfBoxGraphics2DTestBase {
}
exporter.draw(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 20);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.drawForm(xFormObject);
matrix.scale(1.5f, 1.5f);
matrix.translate(0, 100);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.drawForm(xFormObject);
contentStream.close();
}
document.save(new File(parentDir, name + ".pdf"));

View file

@ -1,13 +1,12 @@
package org.xbib.graphics.pdfbox.test;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
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 org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
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 {
String uri = RenderSVGsTest.class.getResource(name).toString();
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory();
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
UserAgent userAgent = new UserAgentAdapter();
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 parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
SAXSVGDocumentFactory documentFactory = new SAXSVGDocumentFactory();
Document document = documentFactory.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
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);
DocumentLoader documentLoader = new DocumentLoader(userAgent);
BridgeContext bridgeContext = new BridgeContext(userAgent, documentLoader);
bridgeContext.setDynamicState(BridgeContext.STATIC);
GVTBuilder gvtBuilder = new GVTBuilder();
GraphicsNode graphicsNode = gvtBuilder.build(bridgeContext, document);
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");
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"));
DefaultColorMapper colorMapper = new RGBtoCMYKColorMapper(icc_profile, pdfDocument);
pdfBoxGraphics2D.setColorMapper(colorMapper);
FontDrawer fontDrawer;
contentStream.beginText();
contentStream.setStrokingColor(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.showText("Mode: CMYK colorspace");
contentStream.endText();
fontDrawer = new DefaultFontDrawer();
FontDrawer fontDrawer = new DefaultFontDrawer();
pdfBoxGraphics2D.setFontTextDrawer(fontDrawer);
pdfBoxGraphics2D.scale(scale, scale);
gvtRoot.paint(pdfBoxGraphics2D);
graphicsNode.paint(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 300);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.drawForm(xFormObject);
contentStream.close();
String baseName = name.substring(0, name.lastIndexOf('.'));
pdfDocument.save(new File(parentDir, baseName + ".pdf"));

View file

@ -8,7 +8,7 @@ public class SVGGraphics2DProvider implements VectorGraphics2DProvider<SVGGraphi
@Override
public String name() {
return "eps";
return "svg";
}
@Override

View file

@ -8,6 +8,8 @@ import org.xbib.graphics.io.vector.filters.StateChangeGroupingFilter;
import org.xbib.graphics.io.vector.PageSize;
public class SVGProcessor implements Processor {
@Override
public ProcessorResult process(Iterable<Command<?>> commands, PageSize pageSize) {
FillPaintedShapeAsImageFilter shapesAsImages = new FillPaintedShapeAsImageFilter(commands);
Iterable<Command<?>> filtered = new StateChangeGroupingFilter(shapesAsImages);

View file

@ -5,10 +5,11 @@ import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
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;
public abstract class AbstractTest {
@ -41,9 +42,8 @@ public abstract class AbstractTest {
referenceGraphics.clearRect(0, 0, reference.getWidth(), reference.getHeight());
referenceGraphics.setColor(Color.BLACK);
draw(referenceGraphics);
File referenceImage = File.createTempFile(getClass().getName() + ".reference", ".png");
referenceImage.deleteOnExit();
ImageIO.write(reference, "png", referenceImage);
Path path = Files.createTempFile(Paths.get("build"), getClass().getName() + ".reference", "png");
ImageIO.write(reference, "png", path.toFile());
}
public abstract void draw(Graphics2D g);