Skip to content

Commit

Permalink
danfickle#23: Allow drawing on the output device using a Graphics2D. …
Browse files Browse the repository at this point in the history
…This implements

the infrastructure, but does not yet use it to render SVGs.
  • Loading branch information
rototor committed Feb 2, 2017
1 parent 3376335 commit 5635527
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public void paintBackground(

public boolean isSupportsCMYKColors();

/**
* Draw something using a Graphics2D at the given rectangle.
*/
public void drawWithGraphics(float x, float y, float width, float height, OutputDeviceGraphicsDrawer renderer);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.openhtmltopdf.extend;

import java.awt.*;

/**
* Render something on a Graphics2D on the OutputDevice.
*
* @FunctionalInterface
*/
public interface OutputDeviceGraphicsDrawer {

/**
* Draw something using the given graphics. For PDFs it will be converted to vector drawings.
* @param graphics2D the graphics you can use to draw
*/
public void render(Graphics2D graphics2D);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

import com.openhtmltopdf.css.parser.FSColor;
import com.openhtmltopdf.css.parser.FSRGBColor;
import com.openhtmltopdf.extend.FSGlyphVector;
import com.openhtmltopdf.extend.FSImage;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.ReplacedElement;
import com.openhtmltopdf.extend.*;
import com.openhtmltopdf.render.*;

import javax.swing.*;
Expand Down Expand Up @@ -280,7 +277,14 @@ public boolean isSupportsCMYKColors() {
return true;
}

private Stack<AffineTransform> transformStack = new Stack<AffineTransform>();
@Override
public void drawWithGraphics(float x, float y, float width, float height, OutputDeviceGraphicsDrawer renderer) {
Graphics2D graphics = (Graphics2D) _graphics.create((int) x, (int) y, (int) width, (int) height);
renderer.render(graphics);
graphics.dispose();
}

private Stack<AffineTransform> transformStack = new Stack<AffineTransform>();
private Stack<Shape> clipStack= new Stack<Shape>();

@Override
Expand Down
5 changes: 5 additions & 0 deletions openhtmltopdf-pdfbox/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<artifactId>openhtmltopdf-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>de.rototor.pdfbox</groupId>
<artifactId>graphics2d</artifactId>
<version>0.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
import com.openhtmltopdf.css.style.CssContext;
import com.openhtmltopdf.extend.FSImage;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.OutputDeviceGraphicsDrawer;
import com.openhtmltopdf.layout.SharedContext;
import com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.FontDescription;
import com.openhtmltopdf.pdfboxout.PdfBoxForm.CheckboxStyle;
import com.openhtmltopdf.render.*;
import com.openhtmltopdf.util.Configuration;
import com.openhtmltopdf.util.XRLog;

import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
Expand All @@ -59,7 +60,6 @@
import org.w3c.dom.Node;

import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.RenderingHints.Key;
import java.awt.geom.*;
Expand Down Expand Up @@ -1315,6 +1315,33 @@ public boolean isSupportsCMYKColors() {
return true;
}

@Override
public void drawWithGraphics(float x, float y, float width, float height, OutputDeviceGraphicsDrawer renderer) {
try {
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(_writer, (int) width, (int) height);
/*
* We *could* customize the PDF mapping here. But for now the default is enough.
*/

/*
* Do rendering
*/
renderer.render(pdfBoxGraphics2D);
/*
* Dispose to close the XStream
*/
pdfBoxGraphics2D.dispose();

/*
* And then stamp it
*/
_cp.placeXForm(x,y,pdfBoxGraphics2D.getXFormObject());
}
catch(IOException e){
throw new RuntimeException("Error while drawing on Graphics2D", e);
}
}

public List findPagePositionsByID(CssContext c, Pattern pattern) {
Map idMap = _sharedContext.getIdMap();
if (idMap == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.openhtmltopdf.pdfboxout;

import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.Locale;

import com.openhtmltopdf.util.XRLog;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;

import com.openhtmltopdf.util.XRLog;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.Locale;

public class PdfContentStreamAdapter {
private final PDPageContentStream cs;


public static class PdfException extends RuntimeException {
private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -48,11 +49,11 @@ public void addRect(float x, float y, float w, float h) {
logAndThrow("addRect", e);
}
}

public void newPath() {
// I think PDF-BOX does this automatically.
}

public void setExtGState(PDExtendedGraphicsState gs) {
try {
cs.setGraphicsStateParameters(gs);
Expand Down Expand Up @@ -283,6 +284,12 @@ public void drawImage(PDImageXObject xobject, float x, float y, float w,

public void setMiterLimit(float miterLimit) {
// TODO Not currently supported by PDF-BOX.
// TODO: Use official API when the next version is released. See PDFBOX-3669
try {
cs.appendRawCommands(miterLimit + " M ");
} catch (IOException e) {
logAndThrow("drawImage", e);
}
}

public void setTextSpacing(float nonSpaceAdjust) {
Expand All @@ -308,4 +315,17 @@ public void setPdfMatrix(AffineTransform transform) {
logAndThrow("setPdfMatrix", e);
}
}

public void placeXForm(float x, float y, PDFormXObject xFormObject) {
try {
cs.saveGraphicsState();
AffineTransform tf = new AffineTransform();
tf.translate(x,y);
cs.transform(new Matrix(tf));
cs.drawForm(xFormObject);
cs.restoreGraphicsState();
} catch (IOException e) {
logAndThrow("placeXForm", e);
}
}
}

0 comments on commit 5635527

Please sign in to comment.