Skip to content

Commit

Permalink
For #23 - Experimental code for SVG transforms - not working and disa…
Browse files Browse the repository at this point in the history
…bled due to issues with the transform origin.
  • Loading branch information
danfickle committed Jun 19, 2016
1 parent 91da4d2 commit b99b747
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@
public interface OutputDevice {

// Required for SVG output.
public void drawText(RenderingContext c, String text, float x, float y);

public void saveState();
public void restoreState();

public void setTransform(AffineTransform transform);
public AffineTransform getTransform();
public void setDeviceTransform(AffineTransform transform);
public AffineTransform getDeviceTransform();

public void setPaint(Paint paint);
public void setAlpha(int alpha);
Expand Down Expand Up @@ -111,7 +109,4 @@ public void paintBackground(
public boolean isSupportsSelection();

public boolean isSupportsCMYKColors();



}
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@ public boolean isSupportsCMYKColors() {
return true;
}

@Override
public void drawText(RenderingContext c, String text, float x, float y) {
// TODO Auto-generated method stub

}

@Override
public void saveState() {
// TODO Auto-generated method stub
Expand All @@ -310,13 +304,12 @@ public void restoreState() {
}

@Override
public void setTransform(AffineTransform transform) {
public void setDeviceTransform(AffineTransform transform) {
// TODO Auto-generated method stub

}

@Override
public AffineTransform getTransform() {
public AffineTransform getDeviceTransform() {
// TODO
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -64,6 +65,7 @@
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
import org.apache.pdfbox.util.Matrix;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -146,6 +148,9 @@ public class PdfBoxOutputDevice extends AbstractOutputDevice implements OutputDe
private RenderingContext _renderingContext;

private BidiReorderer _reorderer = new SimpleBidiReorderer();

private AffineTransform _deviceTransform;
private AffineTransform _setDeviceTransform = new AffineTransform();

public PdfBoxOutputDevice(float dotsPerPoint, boolean testMode) {
_dotsPerPoint = dotsPerPoint;
Expand Down Expand Up @@ -215,7 +220,7 @@ public void drawBorderLine(Shape bounds, int side, int lineWidth, boolean solid)

public void setColor(FSColor color) {
if (color instanceof FSRGBColor) {
_color = color;
_color = color;
} else if (color instanceof FSCMYKColor) {
_color = color;
} else {
Expand Down Expand Up @@ -524,7 +529,7 @@ private PdfTextArray makeJustificationArray(String s, JustificationInfo info) {
return array;
}
*/
public AffineTransform getTransform() {
private AffineTransform getTransform() {
return _transform;
}

Expand All @@ -542,7 +547,7 @@ private void ensureFillColor() {
else {
assert(_fillColor instanceof FSRGBColor || _fillColor instanceof FSCMYKColor);
}
}
}
}

private void ensureStrokeColor() {
Expand All @@ -569,7 +574,7 @@ public PdfContentStreamAdapter getCurrentPage() {
private void followPath(Shape s, int drawType) {
if (s == null)
return;

if (drawType == STROKE) {
if (!(_stroke instanceof BasicStroke)) {
s = _stroke.createStrokedShape(s);
Expand All @@ -585,6 +590,8 @@ private void followPath(Shape s, int drawType) {
ensureFillColor();
}

ensureDeviceTransform();

PathIterator points;
if (drawType == CLIP) {
points = s.getPathIterator(IDENTITY);
Expand Down Expand Up @@ -620,7 +627,7 @@ private void followPath(Shape s, int drawType) {
}
points.next();
}

switch (drawType) {
case FILL:
if (traces > 0) {
Expand Down Expand Up @@ -770,6 +777,7 @@ public void setClip(Shape s) {
_clip = new Area(s);
followPath(s, CLIP);
}

_fillColor = null;
_strokeColor = null;
_oldStroke = null;
Expand Down Expand Up @@ -1260,10 +1268,6 @@ public void setBidiReorderer(BidiReorderer reorderer) {
_reorderer = reorderer;
}

@Override
public void drawText(RenderingContext c, String text, float x, float y) {
}

@Override
public void saveState() {
_cp.saveGraphics();
Expand All @@ -1274,10 +1278,32 @@ public void restoreState() {
_cp.restoreGraphics();
}

/**
* This converts the x and y translate variables to pdf device units.
* Remember, PDFs use a bottom to top coordinate system.
*/
private AffineTransform normalizeDeviceMatrix(AffineTransform current) {
double[] mx = new double[6];
current.getMatrix(mx);
mx[5] *= -1;
return new AffineTransform(mx);
}

private void ensureDeviceTransform() {
if (!(_deviceTransform == null || _deviceTransform.isIdentity())) {
_setDeviceTransform = _deviceTransform;
_cp.setPdfMatrix(normalizeDeviceMatrix(_setDeviceTransform));
}
}

@Override
public void setTransform(AffineTransform transform) {
_transform = transform;

public AffineTransform getDeviceTransform() {
return _deviceTransform;
}

@Override
public void setDeviceTransform(AffineTransform transform) {
_deviceTransform = transform;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.openhtmltopdf.pdfboxout;

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

import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
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;

public class PdfContentStreamAdapter {
private final PDPageContentStream cs;

Expand All @@ -19,7 +23,7 @@ public PdfException(String method, Exception cause) {
}

private void logAndThrow(String method, IOException e) {
// TODO: Logging
XRLog.exception("Exception in PDF writing method: " + method, e);
throw new PdfException(method, e);
}

Expand Down Expand Up @@ -287,4 +291,12 @@ public void setTextSpacing(float nonSpaceAdjust) {
public void setSpaceSpacing(float spaceAdjust) {
// TODO Not currently supported in PDF-BOX.
}

public void setPdfMatrix(AffineTransform transform) {
try {
cs.transform(new Matrix(transform));
} catch (IOException e) {
logAndThrow("setPdfMatrix", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.apache.batik.gvt.font.GVTFont;
import org.apache.batik.gvt.font.GVTGlyphMetrics;
import org.apache.batik.gvt.font.GVTGlyphVector;
import org.apache.batik.gvt.text.TextPaintInfo;
import org.apache.batik.gvt.text.GVTAttributedCharacterIterator.TextAttribute;


public class OpenHtmlGvtGlyphVector implements GVTGlyphVector {

Expand All @@ -27,6 +30,12 @@ public OpenHtmlGvtGlyphVector(java.awt.font.GlyphVector vec, GVTFont font, FontR

@Override
public void draw(Graphics2D g2d, AttributedCharacterIterator arg1) {
if (arg1.getAttribute(TextAttribute.PAINT_INFO) != null) {
TextPaintInfo info = (TextPaintInfo) arg1.getAttribute(TextAttribute.PAINT_INFO);
// TODO: Rest of stuff in info.
g2d.setPaint(info.fillPaint);
}

g2d.fill(this.vec.getOutline());
}

Expand Down
Loading

0 comments on commit b99b747

Please sign in to comment.