add global transformation to all elements that can render at absolute positions
This commit is contained in:
parent
b85fd84abd
commit
10afc65dff
41 changed files with 211 additions and 176 deletions
|
@ -7,6 +7,7 @@ import org.apache.pdfbox.util.Matrix;
|
|||
import org.xbib.graphics.barcode.Symbol;
|
||||
import org.xbib.graphics.barcode.render.BarcodeGraphicsRenderer;
|
||||
import org.xbib.graphics.pdfbox.PdfBoxGraphics2D;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;
|
||||
|
@ -116,6 +117,7 @@ public class BarcodeElement implements Element, Drawable, Dividable, WidthRespec
|
|||
public void draw(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener) throws IOException {
|
||||
float x = upperLeft.getX();
|
||||
float y = upperLeft.getY() - getHeight();
|
||||
|
@ -127,6 +129,9 @@ public class BarcodeElement implements Element, Drawable, Dividable, WidthRespec
|
|||
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.translate(x, y);
|
||||
if (transform != null) {
|
||||
matrix = matrix.multiply(transform.getMatrix());
|
||||
}
|
||||
contentStream.saveGraphicsState();
|
||||
contentStream.transform(matrix);
|
||||
contentStream.drawForm(xFormObject);
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.apache.pdfbox.util.Matrix;
|
|||
import org.xbib.graphics.chart.QuickChart;
|
||||
import org.xbib.graphics.chart.xy.XYChart;
|
||||
import org.xbib.graphics.pdfbox.PdfBoxGraphics2D;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;
|
||||
|
@ -16,8 +17,6 @@ import java.io.IOException;
|
|||
|
||||
public class ChartElement implements Element, Drawable, Dividable, WidthRespecting {
|
||||
|
||||
String chartTitle;
|
||||
|
||||
private double[] xData;
|
||||
|
||||
private double[] yData;
|
||||
|
@ -108,6 +107,7 @@ public class ChartElement implements Element, Drawable, Dividable, WidthRespecti
|
|||
public void draw(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener) throws IOException {
|
||||
float x = upperLeft.getX();
|
||||
float y = upperLeft.getY() - getHeight();
|
||||
|
@ -120,6 +120,9 @@ public class ChartElement implements Element, Drawable, Dividable, WidthRespecti
|
|||
Matrix matrix = new Matrix();
|
||||
matrix.translate(x, y);
|
||||
matrix.scale(scaleX, scaleY);
|
||||
if (transform != null) {
|
||||
matrix = matrix.multiply(transform.getMatrix());
|
||||
}
|
||||
contentStream.saveGraphicsState();
|
||||
contentStream.transform(matrix);
|
||||
contentStream.drawForm(xFormObject);
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.io.IOException;
|
||||
|
@ -52,9 +53,9 @@ public class Cutter implements Dividable, Drawable {
|
|||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
Position upperLeft, Transform transform, DrawListener drawListener) throws IOException {
|
||||
Position viewPortOrigin = upperLeft.add(0, -viewPortY);
|
||||
undividable.draw(pdDocument, contentStream, viewPortOrigin, drawListener);
|
||||
undividable.draw(pdDocument, contentStream, viewPortOrigin, transform, drawListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Layout;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.io.IOException;
|
||||
|
@ -39,13 +40,14 @@ public interface Drawable {
|
|||
* @param pdDocument the underlying pdfbox document.
|
||||
* @param contentStream the stream to draw to.
|
||||
* @param upperLeft the upper left position to start drawing.
|
||||
* @param transform the transformation for drawing.
|
||||
* @param drawListener the listener to
|
||||
* {@link DrawListener#drawn(Object, Position, float, float) notify} on
|
||||
* drawn objects.
|
||||
* @throws IOException by pdfbox
|
||||
* @throws IOException if draw fails
|
||||
*/
|
||||
void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException;
|
||||
Position upperLeft, Transform transform, DrawListener drawListener) throws IOException;
|
||||
|
||||
/**
|
||||
* @return a copy of this drawable where any leading empty vertical space is
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Rect;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Shape;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Stroke;
|
||||
|
@ -510,7 +511,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
|
|||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
Position upperLeft, Transform transform, DrawListener drawListener) throws IOException {
|
||||
setInnerMaxWidthIfNecessary();
|
||||
float halfBorderWidth = 0;
|
||||
if (getBorderWidth() > 0) {
|
||||
|
@ -521,19 +522,19 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
|
|||
float shapeWidth = getWidth() - getMarginLeft() - getMarginRight() - getBorderWidth();
|
||||
float shapeHeight = getHeight() - getMarginTop() - getMarginBottom() - getBorderWidth();
|
||||
if (getBackgroundColor() != null) {
|
||||
getShape().fill(pdDocument, contentStream, upperLeft,
|
||||
getShape().fill(pdDocument, contentStream, upperLeft, transform,
|
||||
shapeWidth, shapeHeight, getBackgroundColor(),
|
||||
drawListener);
|
||||
}
|
||||
if (hasBorder()) {
|
||||
getShape().draw(pdDocument, contentStream, upperLeft,
|
||||
getShape().draw(pdDocument, contentStream, upperLeft, transform,
|
||||
shapeWidth, shapeHeight, getBorderColor(),
|
||||
getBorderStroke(), drawListener);
|
||||
}
|
||||
}
|
||||
Position innerUpperLeft = upperLeft.add(getPaddingLeft() + halfBorderWidth, -getPaddingTop() - halfBorderWidth);
|
||||
for (Drawable inner : innerList) {
|
||||
inner.draw(pdDocument, contentStream, innerUpperLeft, drawListener);
|
||||
inner.draw(pdDocument, contentStream, innerUpperLeft, transform, drawListener);
|
||||
innerUpperLeft = innerUpperLeft.add(0, -inner.getHeight());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Stroke;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
|
@ -81,8 +82,11 @@ public class HorizontalRuler implements Drawable, Element, WidthRespecting {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
public void draw(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener) throws IOException {
|
||||
if (getColor() != null) {
|
||||
contentStream.setStrokingColor(getColor());
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
|||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
|
||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;
|
||||
|
@ -144,11 +145,18 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
|
|||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener) throws IOException {
|
||||
float x = upperLeft.getX();
|
||||
float y = upperLeft.getY() - getHeight();
|
||||
PDImageXObject imageXObject = LosslessFactory.createFromImage(pdDocument, image);
|
||||
contentStream.saveGraphicsState();
|
||||
if (transform != null) {
|
||||
contentStream.transform(transform.getMatrix());
|
||||
}
|
||||
contentStream.drawImage(imageXObject, x, y, getWidth(), getHeight());
|
||||
contentStream.restoreGraphicsState();
|
||||
if (drawListener != null) {
|
||||
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.Alignment;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
|
@ -53,8 +54,8 @@ public class Paragraph extends TextFlow implements Drawable, Element, WidthRespe
|
|||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) {
|
||||
drawText(contentStream, upperLeft, getAlignment(), drawListener);
|
||||
Position upperLeft, Transform transform, DrawListener drawListener) {
|
||||
drawText(contentStream, upperLeft, transform, getAlignment(), drawListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -70,8 +71,8 @@ public class Paragraph extends TextFlow implements Drawable, Element, WidthRespe
|
|||
@Override
|
||||
public Paragraph removeLeadingEmptyLines() {
|
||||
Paragraph result = (Paragraph) super.removeLeadingEmptyLines();
|
||||
result.setAbsolutePosition(this.getAbsolutePosition());
|
||||
result.setAlignment(this.getAlignment());
|
||||
result.setAbsolutePosition(getAbsolutePosition());
|
||||
result.setAlignment(getAlignment());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Path;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Stroke;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
|
@ -67,8 +68,9 @@ public class PathElement implements Drawable, Element {
|
|||
public void draw(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener) throws IOException {
|
||||
path.draw(pdDocument, contentStream, upperLeft, getWidth(), getHeight(),
|
||||
path.draw(pdDocument, contentStream, upperLeft, transform, getWidth(), getHeight(),
|
||||
color, stroke, drawListener);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,5 @@
|
|||
package org.xbib.graphics.pdfbox.layout.element;
|
||||
|
||||
/*
|
||||
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.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||
|
@ -18,6 +9,7 @@ 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.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
|
||||
|
@ -34,8 +26,11 @@ public class SVGElement extends ImageElement {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
public void draw(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener) throws IOException {
|
||||
if (svg == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -54,6 +49,9 @@ public class SVGElement extends ImageElement {
|
|||
PDFormXObject xFormObject = pdfBoxGraphics2D.getXFormObject();
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.translate(x, y);
|
||||
if (transform != null) {
|
||||
matrix = matrix.multiply(transform.getMatrix());
|
||||
}
|
||||
contentStream.saveGraphicsState();
|
||||
contentStream.transform(matrix);
|
||||
contentStream.drawForm(xFormObject);
|
||||
|
@ -67,20 +65,4 @@ public class SVGElement extends ImageElement {
|
|||
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);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.table.BorderStyleInterface;
|
||||
import org.xbib.graphics.pdfbox.layout.table.HorizontalAlignment;
|
||||
import org.xbib.graphics.pdfbox.layout.table.Row;
|
||||
|
@ -103,7 +104,7 @@ public class TableElement implements Element, Drawable, Dividable {
|
|||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
Position upperLeft, Transform transform, DrawListener drawListener) throws IOException {
|
||||
TableRenderer tableRenderer = TableRenderer.builder()
|
||||
.table(table.build())
|
||||
.document(pdDocument)
|
||||
|
|
|
@ -17,12 +17,12 @@ public class TransformElement implements Transform {
|
|||
|
||||
private Float scaleY;
|
||||
|
||||
private Float angle;
|
||||
|
||||
private Float rotateX;
|
||||
|
||||
private Float rotateY;
|
||||
|
||||
private Float angle;
|
||||
|
||||
public void setTranslate(Float x, Float y) {
|
||||
this.translateX = x;
|
||||
this.translateY = y;
|
||||
|
@ -39,44 +39,26 @@ public class TransformElement implements Transform {
|
|||
this.rotateY = rotateY;
|
||||
}
|
||||
|
||||
public Float getAngle() {
|
||||
return angle;
|
||||
@Override
|
||||
public Matrix getMatrix() {
|
||||
Matrix matrix = new Matrix();
|
||||
if (angle != null && rotateX != null && rotateY != null) {
|
||||
matrix = matrix.multiply(Matrix.getRotateInstance(Math.toRadians(angle), rotateX, rotateY));
|
||||
}
|
||||
|
||||
public Float getRotateX() {
|
||||
return rotateX;
|
||||
if (scaleX != null && scaleY != null) {
|
||||
matrix = matrix.multiply(Matrix.getScaleInstance(scaleX, scaleY));
|
||||
}
|
||||
|
||||
public Float getRotateY() {
|
||||
return rotateY;
|
||||
if (translateX != null && translateY != null) {
|
||||
matrix = matrix.multiply(Matrix.getTranslateInstance(translateX, translateY));
|
||||
}
|
||||
|
||||
public Float getScaleX() {
|
||||
return scaleX;
|
||||
}
|
||||
|
||||
public Float getScaleY() {
|
||||
return scaleY;
|
||||
}
|
||||
|
||||
public Float getTranslateX() {
|
||||
return translateX;
|
||||
}
|
||||
|
||||
public Float getTranslateY() {
|
||||
return translateY;
|
||||
return matrix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean render(RenderContext renderContext, Element element, LayoutHint layoutHint) throws IOException {
|
||||
if (translateX != null && translateY != null) {
|
||||
renderContext.getCurrentPageContentStream().transform(Matrix.getTranslateInstance(translateX, translateY));
|
||||
}
|
||||
if (scaleX != null && scaleY != null) {
|
||||
renderContext.getCurrentPageContentStream().transform(Matrix.getScaleInstance(scaleX, scaleY));
|
||||
}
|
||||
if (angle != null && rotateX != null && rotateY != null){
|
||||
renderContext.getCurrentPageContentStream().transform(Matrix.getRotateInstance(Math.toRadians(angle), rotateX, rotateY));
|
||||
Matrix matrix = getMatrix();
|
||||
if (matrix != null) {
|
||||
renderContext.getCurrentPageContentStream().transform(matrix);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.element;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.io.IOException;
|
||||
|
@ -40,7 +41,7 @@ public class VerticalSpacer implements Drawable, Element, Dividable {
|
|||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, DrawListener drawListener) throws IOException {
|
||||
Position upperLeft, Transform transform, DrawListener drawListener) throws IOException {
|
||||
if (drawListener != null) {
|
||||
drawListener.drawn(this, upperLeft, getWidth(), getHeight());
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.xbib.graphics.pdfbox.layout.element.render;
|
|||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.util.Matrix;
|
||||
import org.xbib.graphics.pdfbox.layout.element.ControlElement;
|
||||
import org.xbib.graphics.pdfbox.layout.element.Document;
|
||||
import org.xbib.graphics.pdfbox.layout.element.Element;
|
||||
|
@ -260,6 +261,7 @@ public class RenderContext implements Renderer, Closeable, DrawContext, DrawList
|
|||
}
|
||||
if (element instanceof Transform) {
|
||||
setTransform((Transform) element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -305,6 +307,14 @@ public class RenderContext implements Renderer, Closeable, DrawContext, DrawList
|
|||
this.contentStream = new PDPageContentStream(pdDocument, page,
|
||||
PDPageContentStream.AppendMode.APPEND, true);
|
||||
if (getPageOrientation() != getPageFormat().getOrientation()) {
|
||||
if (page.getRotation() == 90 || page.getRotation() == 270) {
|
||||
page.setRotation(0);
|
||||
} else {
|
||||
page.setRotation(90);
|
||||
}
|
||||
}
|
||||
if (page.getRotation() == 90 || page.getRotation() == 270) {
|
||||
contentStream.transform(new Matrix(0, 1, -1, 0, getPageHeight(), 0));
|
||||
page.setRotation(90);
|
||||
}
|
||||
resetPositionToUpperLeft();
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package org.xbib.graphics.pdfbox.layout.element.render;
|
||||
|
||||
import org.apache.pdfbox.util.Matrix;
|
||||
import org.xbib.graphics.pdfbox.layout.element.Element;
|
||||
|
||||
public interface Transform extends Element, Renderer {
|
||||
|
||||
Matrix getMatrix();
|
||||
}
|
||||
|
|
|
@ -91,8 +91,11 @@ public class VerticalLayout implements Layout {
|
|||
* @throws IOException by pdfbox
|
||||
*/
|
||||
protected void renderAbsolute(RenderContext renderContext, Drawable drawable) throws IOException {
|
||||
drawable.draw(renderContext.getPdDocument(), renderContext.getCurrentPageContentStream(),
|
||||
drawable.getAbsolutePosition(), renderContext);
|
||||
drawable.draw(renderContext.getPdDocument(),
|
||||
renderContext.getCurrentPageContentStream(),
|
||||
drawable.getAbsolutePosition(),
|
||||
renderContext.getTransform(),
|
||||
renderContext);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -223,7 +226,7 @@ public class VerticalLayout implements Layout {
|
|||
renderContext.getPageWidth(), renderContext.getHeight());
|
||||
contentStream.clip();
|
||||
drawable.draw(renderContext.getPdDocument(), contentStream,
|
||||
renderContext.getCurrentPosition().add(offsetX, 0), renderContext);
|
||||
renderContext.getCurrentPosition().add(offsetX, 0), null, renderContext);
|
||||
contentStream.restoreGraphicsState();
|
||||
if (movePosition) {
|
||||
renderContext.movePositionBy(0, -drawable.getHeight());
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.xbib.graphics.pdfbox.layout.element.scripting.command;
|
|||
|
||||
import java.io.IOException;
|
||||
import org.xbib.graphics.pdfbox.layout.element.TransformElement;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.LayoutHint;
|
||||
import org.xbib.graphics.pdfbox.layout.element.scripting.Engine;
|
||||
import org.xbib.graphics.pdfbox.layout.element.scripting.State;
|
||||
import org.xbib.settings.Settings;
|
||||
|
@ -23,6 +22,6 @@ public class TransformCommand implements Command {
|
|||
if (settings.containsSetting("angle") && settings.containsSetting("rotatex") && settings.containsSetting("rotatey")) {
|
||||
element.setRotate(settings.getAsFloat("angle", null), mmToPt(settings.getAsFloat("rotatex", null)), mmToPt(settings.getAsFloat("rotatey", null)));
|
||||
}
|
||||
state.elements.peek().add(element, LayoutHint.NOP);
|
||||
state.elements.peek().add(element);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.shape;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.awt.Color;
|
||||
|
@ -9,16 +10,16 @@ import java.io.IOException;
|
|||
|
||||
/**
|
||||
* Abstract base class for shapes which performs the
|
||||
* {@link #fill(PDDocument, PDPageContentStream, Position, float, float, Color, DrawListener)}
|
||||
* and {@link #draw(PDDocument, PDPageContentStream, Position, float, float, Color, Stroke, DrawListener)}
|
||||
* {@link #fill(PDDocument, PDPageContentStream, Position, Transform, float, float, Color, DrawListener)}
|
||||
* and {@link #draw(PDDocument, PDPageContentStream, Position, Transform, float, float, Color, Stroke, DrawListener)}
|
||||
*/
|
||||
public abstract class AbstractShape implements Shape {
|
||||
|
||||
@Override
|
||||
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height, Color color,
|
||||
Position upperLeft, Transform transform, float width, float height, Color color,
|
||||
Stroke stroke, DrawListener drawListener) throws IOException {
|
||||
add(pdDocument, contentStream, upperLeft, width, height);
|
||||
add(pdDocument, contentStream, upperLeft, transform, width, height);
|
||||
if (stroke != null) {
|
||||
stroke.applyTo(contentStream);
|
||||
}
|
||||
|
@ -33,9 +34,9 @@ public abstract class AbstractShape implements Shape {
|
|||
|
||||
@Override
|
||||
public void fill(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height, Color color,
|
||||
Position upperLeft, Transform transform, float width, float height, Color color,
|
||||
DrawListener drawListener) throws IOException {
|
||||
add(pdDocument, contentStream, upperLeft, width, height);
|
||||
add(pdDocument, contentStream, upperLeft, transform, width, height);
|
||||
if (color != null) {
|
||||
contentStream.setNonStrokingColor(color);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.xbib.graphics.pdfbox.layout.shape;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -15,8 +16,8 @@ public class Ellipse extends RoundRect {
|
|||
|
||||
@Override
|
||||
protected void addRoundRect(PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height, float cornerRadiusX,
|
||||
Position upperLeft, Transform transform, float width, float height, float cornerRadiusX,
|
||||
float cornerRadiusY) throws IOException {
|
||||
super.addRoundRect(contentStream, upperLeft, width, height, width / 2f, height / 2);
|
||||
super.addRoundRect(contentStream, upperLeft, transform, width, height, width / 2f, height / 2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.shape;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.position.LinePosiiton;
|
||||
import org.xbib.graphics.pdfbox.layout.position.MovePosition;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
|
@ -23,13 +24,17 @@ public class Path implements Shape {
|
|||
public void draw(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
float width,
|
||||
float height,
|
||||
Color color,
|
||||
Stroke stroke,
|
||||
DrawListener drawListener) throws IOException {
|
||||
add(pdDocument, contentStream, upperLeft, width, height);
|
||||
add(pdDocument, contentStream, upperLeft, transform, width, height);
|
||||
contentStream.saveGraphicsState();
|
||||
if (transform != null) {
|
||||
contentStream.transform(transform.getMatrix());
|
||||
}
|
||||
float x = upperLeft.getX();
|
||||
float y = upperLeft.getY() - stroke.getLineWidth() / 2;
|
||||
contentStream.setStrokingColor(color);
|
||||
|
@ -50,13 +55,13 @@ public class Path implements Shape {
|
|||
|
||||
@Override
|
||||
public void fill(PDDocument pdDocument, PDPageContentStream contentStream, Position upperLeft,
|
||||
float width, float height, Color color, DrawListener drawListener) throws IOException {
|
||||
Transform transform, float width, float height, Color color, DrawListener drawListener) throws IOException {
|
||||
// do not fill
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(PDDocument pdDocument, PDPageContentStream contentStream, Position upperLeft,
|
||||
float width, float height) throws IOException {
|
||||
Transform transform, float width, float height) throws IOException {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.shape;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -12,7 +13,7 @@ public class Rect extends AbstractShape {
|
|||
|
||||
@Override
|
||||
public void add(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height) throws IOException {
|
||||
Position upperLeft, Transform transform, float width, float height) throws IOException {
|
||||
contentStream.addRect(upperLeft.getX(), upperLeft.getY() - height, width, height);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.shape;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -40,9 +41,10 @@ public class RoundRect extends AbstractShape {
|
|||
public void add(PDDocument pdDocument,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
float width,
|
||||
float height) throws IOException {
|
||||
addRoundRect(contentStream, upperLeft, width, height, cornerRadiusX, cornerRadiusY);
|
||||
addRoundRect(contentStream, upperLeft, transform, width, height, cornerRadiusX, cornerRadiusY);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,7 +71,7 @@ public class RoundRect extends AbstractShape {
|
|||
* @throws IOException by pdfbox
|
||||
*/
|
||||
protected void addRoundRect(PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height, float cornerRadiusX,
|
||||
Position upperLeft, Transform transform, float width, float height, float cornerRadiusX,
|
||||
float cornerRadiusY) throws IOException {
|
||||
float nettoWidth = width - 2 * cornerRadiusX;
|
||||
float nettoHeight = height - 2 * cornerRadiusY;
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.shape;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import java.awt.Color;
|
||||
|
@ -9,11 +10,11 @@ import java.io.IOException;
|
|||
|
||||
/**
|
||||
* Shapes can be used to either
|
||||
* {@link #draw(PDDocument, PDPageContentStream, Position, float, float, Color, Stroke, DrawListener)
|
||||
* {@link #draw(PDDocument, PDPageContentStream, Position, Transform, float, float, Color, Stroke, DrawListener)
|
||||
* stroke} or
|
||||
* {@link #fill(PDDocument, PDPageContentStream, Position, float, float, Color, DrawListener)
|
||||
* {@link #fill(PDDocument, PDPageContentStream, Position, Transform, float, float, Color, DrawListener)
|
||||
* fill} the path of the shape, or simply
|
||||
* {@link #add(PDDocument, PDPageContentStream, Position, float, float) add the
|
||||
* {@link #add(PDDocument, PDPageContentStream, Position, Transform, float, float) add the
|
||||
* path} of the shape to the drawing context.
|
||||
*/
|
||||
public interface Shape {
|
||||
|
@ -24,6 +25,7 @@ public interface Shape {
|
|||
* @param pdDocument the underlying pdfbox document.
|
||||
* @param contentStream the stream to draw to.
|
||||
* @param upperLeft the upper left position to start drawing.
|
||||
* @param transform the transform or null
|
||||
* @param width the width of the bounding box.
|
||||
* @param height the height of the bounding box.
|
||||
* @param color the color to use.
|
||||
|
@ -34,7 +36,9 @@ public interface Shape {
|
|||
* @throws IOException by pdfbox
|
||||
*/
|
||||
void draw(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height, Color color,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
float width, float height, Color color,
|
||||
Stroke stroke, DrawListener drawListener) throws IOException;
|
||||
|
||||
/**
|
||||
|
@ -52,7 +56,7 @@ public interface Shape {
|
|||
* @throws IOException by pdfbox
|
||||
*/
|
||||
void fill(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height, Color color,
|
||||
Position upperLeft, Transform transform, float width, float height, Color color,
|
||||
DrawListener drawListener) throws IOException;
|
||||
|
||||
/**
|
||||
|
@ -66,6 +70,6 @@ public interface Shape {
|
|||
* @throws IOException by pdfbox
|
||||
*/
|
||||
void add(PDDocument pdDocument, PDPageContentStream contentStream,
|
||||
Position upperLeft, float width, float height) throws IOException;
|
||||
Position upperLeft, Transform transform, float width, float height) throws IOException;
|
||||
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import org.apache.pdfbox.pdmodel.PDDocument;
|
|||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.table.render.Renderer;
|
||||
import org.xbib.graphics.pdfbox.layout.table.render.RenderContext;
|
||||
import org.xbib.graphics.pdfbox.layout.table.render.TableRenderContext;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
import java.io.IOException;
|
||||
|
@ -39,7 +39,7 @@ public class TableRenderer {
|
|||
|
||||
protected boolean startTableInNewPage;
|
||||
|
||||
protected final List<BiConsumer<Renderer, RenderContext>> drawerList = new LinkedList<>();
|
||||
protected final List<BiConsumer<Renderer, TableRenderContext>> drawerList = new LinkedList<>();
|
||||
|
||||
{
|
||||
this.drawerList.add((drawer, drawingContext) -> {
|
||||
|
@ -150,7 +150,7 @@ public class TableRenderer {
|
|||
return pageToDrawOn;
|
||||
}
|
||||
|
||||
protected void drawWithFunction(PageData pageData, Point2D.Float startingPoint, BiConsumer<Renderer, RenderContext> consumer) {
|
||||
protected void drawWithFunction(PageData pageData, Point2D.Float startingPoint, BiConsumer<Renderer, TableRenderContext> consumer) {
|
||||
float y = startingPoint.y;
|
||||
for (int rowIndex = pageData.firstRowOnPage; rowIndex < pageData.firstRowOnNextPage; rowIndex++) {
|
||||
final Row row = table.getRows().get(rowIndex);
|
||||
|
@ -159,7 +159,7 @@ public class TableRenderer {
|
|||
}
|
||||
}
|
||||
|
||||
protected void drawRow(Point2D.Float start, Row row, int rowIndex, BiConsumer<Renderer, RenderContext> consumer) {
|
||||
protected void drawRow(Point2D.Float start, Row row, int rowIndex, BiConsumer<Renderer, TableRenderContext> consumer) {
|
||||
float x = start.x;
|
||||
int columnCounter = 0;
|
||||
for (Cell cell : row.getCells()) {
|
||||
|
@ -167,7 +167,7 @@ public class TableRenderer {
|
|||
x += table.getColumns().get(columnCounter).getWidth();
|
||||
columnCounter++;
|
||||
}
|
||||
consumer.accept(cell.getRenderer(), new RenderContext(pdDocument, contentStream, page, new Point2D.Float(x, start.y)));
|
||||
consumer.accept(cell.getRenderer(), new TableRenderContext(pdDocument, contentStream, page, new Point2D.Float(x, start.y)));
|
||||
x += cell.getWidth();
|
||||
columnCounter += cell.getColSpan();
|
||||
}
|
||||
|
|
|
@ -19,10 +19,10 @@ public abstract class AbstractCellRenderer<T extends Cell> implements Renderer {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void renderBackground(RenderContext renderContext) {
|
||||
public void renderBackground(TableRenderContext tableRenderContext) {
|
||||
if (cell.hasBackgroundColor()) {
|
||||
final PDPageContentStream contentStream = renderContext.getContentStream();
|
||||
final Point2D.Float start = renderContext.getStartingPoint();
|
||||
final PDPageContentStream contentStream = tableRenderContext.getContentStream();
|
||||
final Point2D.Float start = tableRenderContext.getStartingPoint();
|
||||
final float rowHeight = cell.getRow().getHeight();
|
||||
final float height = Math.max(cell.getHeight(), rowHeight);
|
||||
final float y = rowHeight < cell.getHeight()
|
||||
|
@ -35,12 +35,12 @@ public abstract class AbstractCellRenderer<T extends Cell> implements Renderer {
|
|||
}
|
||||
|
||||
@Override
|
||||
public abstract void renderContent(RenderContext renderContext);
|
||||
public abstract void renderContent(TableRenderContext tableRenderContext);
|
||||
|
||||
@Override
|
||||
public void renderBorders(RenderContext renderContext) {
|
||||
final Point2D.Float start = renderContext.getStartingPoint();
|
||||
final PDPageContentStream contentStream = renderContext.getContentStream();
|
||||
public void renderBorders(TableRenderContext tableRenderContext) {
|
||||
final Point2D.Float start = tableRenderContext.getStartingPoint();
|
||||
final PDPageContentStream contentStream = tableRenderContext.getContentStream();
|
||||
final float cellWidth = cell.getWidth();
|
||||
final float rowHeight = cell.getRow().getHeight();
|
||||
final float height = Math.max(cell.getHeight(), rowHeight);
|
||||
|
|
|
@ -15,17 +15,17 @@ public class DrawableCellRenderer extends AbstractCellRenderer<DrawableCell> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void renderContent(RenderContext renderContext) {
|
||||
public void renderContent(TableRenderContext tableRenderContext) {
|
||||
Drawable drawable = cell.getDrawable();
|
||||
if (drawable instanceof WidthRespecting) {
|
||||
WidthRespecting widthRespecting = (WidthRespecting) drawable;
|
||||
widthRespecting.setMaxWidth(cell.getWidth());
|
||||
}
|
||||
float x = renderContext.getStartingPoint().x + cell.getPaddingLeft();
|
||||
float y = renderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
|
||||
float x = tableRenderContext.getStartingPoint().x + cell.getPaddingLeft();
|
||||
float y = tableRenderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
|
||||
Position position = new Position(x, y);
|
||||
try {
|
||||
drawable.draw(renderContext.getPdDocument(), renderContext.getContentStream(), position,null);
|
||||
drawable.draw(tableRenderContext.getPdDocument(), tableRenderContext.getContentStream(), position,null, null);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
|
|
|
@ -15,9 +15,9 @@ public class ImageCellRenderer extends AbstractCellRenderer<ImageCell> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void renderContent(RenderContext renderContext) {
|
||||
final PDPageContentStream contentStream = renderContext.getContentStream();
|
||||
final float moveX = renderContext.getStartingPoint().x;
|
||||
public void renderContent(TableRenderContext tableRenderContext) {
|
||||
final PDPageContentStream contentStream = tableRenderContext.getContentStream();
|
||||
final float moveX = tableRenderContext.getStartingPoint().x;
|
||||
final Point2D.Float size = cell.getFitSize();
|
||||
final Point2D.Float drawAt = new Point2D.Float();
|
||||
float xOffset = moveX + cell.getPaddingLeft();
|
||||
|
@ -28,7 +28,7 @@ public class ImageCellRenderer extends AbstractCellRenderer<ImageCell> {
|
|||
xOffset = moveX + diff;
|
||||
}
|
||||
drawAt.x = xOffset;
|
||||
drawAt.y = renderContext.getStartingPoint().y + getAdaptionForVerticalAlignment() - size.y;
|
||||
drawAt.y = tableRenderContext.getStartingPoint().y + getAdaptionForVerticalAlignment() - size.y;
|
||||
try {
|
||||
contentStream.drawImage(cell.getImage(), drawAt.x, drawAt.y, size.x, size.y);
|
||||
} catch (IOException exception) {
|
||||
|
|
|
@ -29,13 +29,14 @@ public class ParagraphCellRenderer extends AbstractCellRenderer<ParagraphCell> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void renderContent(RenderContext renderContext) {
|
||||
public void renderContent(TableRenderContext tableRenderContext) {
|
||||
Paragraph paragraph = cell.getCellParagraph().getParagraph();
|
||||
float x = renderContext.getStartingPoint().x + cell.getPaddingLeft();
|
||||
float y = renderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
|
||||
AnnotationDrawListener annotationDrawListener = createAndGetAnnotationDrawListenerWith(renderContext);
|
||||
paragraph.drawText(renderContext.getContentStream(),
|
||||
float x = tableRenderContext.getStartingPoint().x + cell.getPaddingLeft();
|
||||
float y = tableRenderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
|
||||
AnnotationDrawListener annotationDrawListener = createAndGetAnnotationDrawListenerWith(tableRenderContext);
|
||||
paragraph.drawText(tableRenderContext.getContentStream(),
|
||||
new Position(x, y),
|
||||
null,
|
||||
ALIGNMENT_MAP.getOrDefault(cell.getParameters().getHorizontalAlignment(), Alignment.LEFT),
|
||||
annotationDrawListener
|
||||
);
|
||||
|
@ -46,21 +47,21 @@ public class ParagraphCellRenderer extends AbstractCellRenderer<ParagraphCell> {
|
|||
return cell.getCellParagraph().getParagraph().getHeight();
|
||||
}
|
||||
|
||||
private AnnotationDrawListener createAndGetAnnotationDrawListenerWith(RenderContext renderContext) {
|
||||
private AnnotationDrawListener createAndGetAnnotationDrawListenerWith(TableRenderContext tableRenderContext) {
|
||||
return new AnnotationDrawListener(new DrawContext() {
|
||||
@Override
|
||||
public PDDocument getPdDocument() {
|
||||
return renderContext.getPdDocument();
|
||||
return tableRenderContext.getPdDocument();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PDPage getCurrentPage() {
|
||||
return renderContext.getPage();
|
||||
return tableRenderContext.getPage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PDPageContentStream getCurrentPageContentStream() {
|
||||
return renderContext.getContentStream();
|
||||
return tableRenderContext.getContentStream();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,10 +2,10 @@ package org.xbib.graphics.pdfbox.layout.table.render;
|
|||
|
||||
public interface Renderer {
|
||||
|
||||
void renderContent(RenderContext renderContext);
|
||||
void renderContent(TableRenderContext tableRenderContext);
|
||||
|
||||
void renderBackground(RenderContext renderContext);
|
||||
void renderBackground(TableRenderContext tableRenderContext);
|
||||
|
||||
void renderBorders(RenderContext renderContext);
|
||||
void renderBorders(TableRenderContext tableRenderContext);
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|||
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
public class RenderContext {
|
||||
public class TableRenderContext {
|
||||
|
||||
private final PDDocument pdDocument;
|
||||
|
||||
|
@ -16,7 +16,7 @@ public class RenderContext {
|
|||
|
||||
private final Point2D.Float startingPoint;
|
||||
|
||||
public RenderContext(PDDocument pdDocument, PDPageContentStream contentStream, PDPage page, Point2D.Float startingPoint) {
|
||||
public TableRenderContext(PDDocument pdDocument, PDPageContentStream contentStream, PDPage page, Point2D.Float startingPoint) {
|
||||
this.pdDocument = pdDocument;
|
||||
this.contentStream = contentStream;
|
||||
this.page = page;
|
|
@ -21,12 +21,12 @@ public class TextCellRenderer<T extends AbstractTextCell> extends AbstractCellRe
|
|||
}
|
||||
|
||||
@Override
|
||||
public void renderContent(RenderContext renderContext) {
|
||||
float startX = renderContext.getStartingPoint().x;
|
||||
public void renderContent(TableRenderContext tableRenderContext) {
|
||||
float startX = tableRenderContext.getStartingPoint().x;
|
||||
Font currentFont = cell.getFont();
|
||||
float currentFontSize = cell.getFontSize();
|
||||
Color currentTextColor = cell.getTextColor();
|
||||
float yOffset = renderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
|
||||
float yOffset = tableRenderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
|
||||
float xOffset = startX + cell.getPaddingLeft();
|
||||
final List<String> lines = calculateAndGetLines(currentFont, currentFontSize, cell.getMaxWidth());
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
|
@ -39,14 +39,14 @@ public class TextCellRenderer<T extends AbstractTextCell> extends AbstractCellRe
|
|||
xOffset = startX + (cell.getWidth() - textWidth) / 2;
|
||||
} else if (cell.isHorizontallyAligned(JUSTIFY) && isNotLastLine(lines, i)) {
|
||||
try {
|
||||
renderContext.getContentStream().setCharacterSpacing(calculateCharSpacingFor(line));
|
||||
tableRenderContext.getContentStream().setCharacterSpacing(calculateCharSpacingFor(line));
|
||||
} catch (IOException exception) {
|
||||
throw new UncheckedIOException(exception);
|
||||
}
|
||||
}
|
||||
PositionedStyledText positionedStyledText = new PositionedStyledText(xOffset, yOffset, line,
|
||||
currentFont, currentFontSize, currentTextColor);
|
||||
RenderUtil.drawText(renderContext.getContentStream(), positionedStyledText);
|
||||
RenderUtil.drawText(tableRenderContext.getContentStream(), positionedStyledText);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,9 +24,9 @@ public class VerticalTextCellRenderer extends AbstractCellRenderer<VerticalTextC
|
|||
}
|
||||
|
||||
@Override
|
||||
public void renderContent(RenderContext renderContext) {
|
||||
float startX = renderContext.getStartingPoint().x;
|
||||
float startY = renderContext.getStartingPoint().y;
|
||||
public void renderContent(TableRenderContext tableRenderContext) {
|
||||
float startX = tableRenderContext.getStartingPoint().x;
|
||||
float startY = tableRenderContext.getStartingPoint().y;
|
||||
Font currentFont = cell.getFont();
|
||||
float currentFontSize = cell.getFontSize();
|
||||
Color currentTextColor = cell.getTextColor();
|
||||
|
@ -44,7 +44,7 @@ public class VerticalTextCellRenderer extends AbstractCellRenderer<VerticalTextC
|
|||
for (int i = 0; i < lines.size(); i++) {
|
||||
final String line = lines.get(i);
|
||||
xOffset += (PdfUtil.getFontHeight(currentFont, currentFontSize) + (i > 0 ? PdfUtil.getFontHeight(currentFont, currentFontSize) * cell.getLineSpacing() : 0f));
|
||||
drawText(line, currentFont, currentFontSize, currentTextColor, xOffset, yOffset, renderContext.getContentStream());
|
||||
drawText(line, currentFont, currentFontSize, currentTextColor, xOffset, yOffset, tableRenderContext.getContentStream());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.xbib.graphics.pdfbox.layout.text;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -15,12 +16,13 @@ public interface DrawableText extends Area {
|
|||
*
|
||||
* @param contentStream the content stream used to render.
|
||||
* @param upperLeft the upper left position to draw to.
|
||||
* @param transform the transformation or null
|
||||
* @param alignment the text alignment.
|
||||
* @param drawListener the listener to
|
||||
* {@link DrawListener#drawn(Object, Position, float, float)
|
||||
* notify} on drawn objects.
|
||||
* @throws IOException by pdfbox.
|
||||
*/
|
||||
void drawText(PDPageContentStream contentStream, Position upperLeft,
|
||||
void drawText(PDPageContentStream contentStream, Position upperLeft, Transform transform,
|
||||
Alignment alignment, DrawListener drawListener) throws IOException;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.xbib.graphics.pdfbox.layout.text;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.font.Font;
|
||||
import org.xbib.graphics.pdfbox.layout.font.FontDescriptor;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
|
@ -233,18 +234,12 @@ public class TextFlow implements TextSequence, WidthRespecting {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void drawText(PDPageContentStream contentStream, Position upperLeft,
|
||||
public void drawText(PDPageContentStream contentStream, Position upperLeft, Transform transform,
|
||||
Alignment alignment, DrawListener drawListener) {
|
||||
TextSequenceUtil.drawText(this, contentStream, upperLeft, drawListener,
|
||||
TextSequenceUtil.drawText(this, contentStream, upperLeft, transform, drawListener,
|
||||
alignment, getMaxWidth(), getLineSpacing(), isApplyLineSpacingToFirstLine());
|
||||
}
|
||||
|
||||
public void drawTextRightAligned(PDPageContentStream contentStream,
|
||||
Position endOfFirstLine, DrawListener drawListener) {
|
||||
drawText(contentStream, endOfFirstLine.add(-getWidth(), 0),
|
||||
Alignment.RIGHT, drawListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a copy of this text flow where all leading {@link NewLine}s are removed.
|
||||
*/
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.xbib.graphics.pdfbox.layout.text;
|
|||
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.util.Matrix;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.font.FontDescriptor;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
|
||||
|
@ -155,13 +156,14 @@ public class TextLine implements TextSequence {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void drawText(PDPageContentStream contentStream, Position upperLeft,
|
||||
public void drawText(PDPageContentStream contentStream, Position upperLeft, Transform transform,
|
||||
Alignment alignment, DrawListener drawListener) {
|
||||
drawAligned(contentStream, upperLeft, alignment, getWidth(), drawListener);
|
||||
drawAligned(contentStream, upperLeft, transform, alignment, getWidth(), drawListener);
|
||||
}
|
||||
|
||||
public void drawAligned(PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
Alignment alignment,
|
||||
float availableLineWidth,
|
||||
DrawListener drawListener) {
|
||||
|
@ -195,6 +197,9 @@ public class TextLine implements TextSequence {
|
|||
if (isRotated) {
|
||||
matrix.rotate(Math.toRadians(styledText.getRotation()));
|
||||
}
|
||||
if (transform != null) {
|
||||
matrix = matrix.multiply(transform.getMatrix());
|
||||
}
|
||||
contentStream.beginText();
|
||||
contentStream.setTextMatrix(matrix);
|
||||
if (!styledText.getFontDescriptor().equals(lastFontDesc)) {
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.xbib.graphics.pdfbox.layout.text;
|
|||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.xbib.graphics.pdfbox.layout.element.Dividable.Divided;
|
||||
import org.xbib.graphics.pdfbox.layout.element.Paragraph;
|
||||
import org.xbib.graphics.pdfbox.layout.element.render.Transform;
|
||||
import org.xbib.graphics.pdfbox.layout.font.FontDescriptor;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import org.xbib.graphics.pdfbox.layout.util.Pair;
|
||||
|
@ -384,6 +385,7 @@ public class TextSequenceUtil {
|
|||
public static void drawText(TextSequence text,
|
||||
PDPageContentStream contentStream,
|
||||
Position upperLeft,
|
||||
Transform transform,
|
||||
DrawListener drawListener,
|
||||
Alignment alignment,
|
||||
float maxWidth, final float lineSpacing, final boolean applyLineSpacingToFirstLine) {
|
||||
|
@ -401,7 +403,7 @@ public class TextSequenceUtil {
|
|||
}
|
||||
lastLineHeight = currentLineHeight;
|
||||
position = position.add(0, -lead);
|
||||
textLine.drawAligned(contentStream, position, alignment, maxLineWidth, drawListener);
|
||||
textLine.drawAligned(contentStream, position, transform, alignment, maxLineWidth, drawListener);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -101,8 +101,8 @@ public class CustomRendererTest {
|
|||
TextFlow text = TextFlowUtil.createTextFlow(content, fontDescriptor, 1.2f, 0f);
|
||||
float offset = renderContext.getPageFormat().getMarginLeft() +
|
||||
TextSequenceUtil.getOffset(text, renderContext.getWidth(), Alignment.RIGHT);
|
||||
text.drawText(renderContext.getCurrentPageContentStream(), new Position(
|
||||
offset, 30), Alignment.RIGHT, null);
|
||||
text.drawText(renderContext.getCurrentPageContentStream(),
|
||||
new Position(offset, 30), null, Alignment.RIGHT, null);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -52,8 +52,8 @@ public class ListenerTest {
|
|||
float offset = renderContext.getPageFormat().getMarginLeft()
|
||||
+ TextSequenceUtil.getOffset(text,
|
||||
renderContext.getWidth(), Alignment.RIGHT);
|
||||
text.drawText(renderContext.getCurrentPageContentStream(), new Position(
|
||||
offset, 30), Alignment.RIGHT, null);
|
||||
text.drawText(renderContext.getCurrentPageContentStream(),
|
||||
new Position(offset, 30), null, Alignment.RIGHT, null);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -7,8 +7,6 @@ import org.apache.pdfbox.util.Matrix;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.xbib.graphics.pdfbox.layout.element.PageFormat;
|
||||
import org.xbib.graphics.pdfbox.layout.font.FontDescriptor;
|
||||
import org.xbib.graphics.pdfbox.layout.position.LinePosiiton;
|
||||
import org.xbib.graphics.pdfbox.layout.position.MovePosition;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.RoundRect;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Shape;
|
||||
import org.xbib.graphics.pdfbox.layout.shape.Stroke;
|
||||
|
@ -65,7 +63,7 @@ public class LowLevelText {
|
|||
text.setMaxWidth(100);
|
||||
float xOffset = TextSequenceUtil.getOffset(text, pageWidth,
|
||||
Alignment.RIGHT);
|
||||
text.drawText(contentStream, new Position(xOffset, pageHeight - 50),
|
||||
text.drawText(contentStream, new Position(xOffset, pageHeight - 50), null,
|
||||
Alignment.RIGHT, annotationDrawListener);
|
||||
|
||||
String textBlock = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, "
|
||||
|
@ -83,7 +81,7 @@ public class LowLevelText {
|
|||
text.addMarkup(textBlock, 8, BaseFont.COURIER);
|
||||
text.setMaxWidth(200);
|
||||
xOffset = TextSequenceUtil.getOffset(text, pageWidth, Alignment.CENTER);
|
||||
text.drawText(contentStream, new Position(xOffset, pageHeight - 100),
|
||||
text.drawText(contentStream, new Position(xOffset, pageHeight - 100), null,
|
||||
Alignment.JUSTIFY, annotationDrawListener);
|
||||
|
||||
// draw a round rect box with text
|
||||
|
@ -96,12 +94,12 @@ public class LowLevelText {
|
|||
float boxHeight = text.getHeight() + 2 * paddingY;
|
||||
|
||||
Shape shape = new RoundRect(20);
|
||||
shape.fill(pdDocument, contentStream, new Position(x, y), boxWidth,
|
||||
shape.fill(pdDocument, contentStream, new Position(x, y), null, boxWidth,
|
||||
boxHeight, Color.pink, null);
|
||||
shape.draw(pdDocument, contentStream, new Position(x, y), boxWidth,
|
||||
shape.draw(pdDocument, contentStream, new Position(x, y), null, boxWidth,
|
||||
boxHeight, Color.blue, new Stroke(3), null);
|
||||
// now the text
|
||||
text.drawText(contentStream, new Position(x + paddingX, y - paddingY),
|
||||
text.drawText(contentStream, new Position(x + paddingX, y - paddingY), null,
|
||||
Alignment.CENTER, annotationDrawListener);
|
||||
|
||||
annotationDrawListener.afterPage(null);
|
||||
|
|
|
@ -12,13 +12,13 @@ public class ScriptingTest {
|
|||
@Test
|
||||
public void script() throws Exception {
|
||||
Settings settings = Settings.settingsBuilder()
|
||||
.loadFromResource("json", getClass().getResourceAsStream("elements.json"))
|
||||
.loadFromResource("json", getClass().getResourceAsStream("scripting.json"))
|
||||
.build();
|
||||
Engine engine = new Engine();
|
||||
engine.execute(settings);
|
||||
int i = 0;
|
||||
for (Document document : engine.getState().getDocuments()) {
|
||||
document.render().save(new FileOutputStream("build/elements" + (i++) + ".pdf")).close();
|
||||
document.render().save(new FileOutputStream("build/scripting" + (i++) + ".pdf")).close();
|
||||
}
|
||||
engine.close();
|
||||
}
|
||||
|
|
|
@ -3,6 +3,16 @@
|
|||
"margin": "0 0 0 0",
|
||||
"author": "Jörg Prante",
|
||||
"elements": [
|
||||
{
|
||||
"type": "transform",
|
||||
"translatex": 150,
|
||||
"translatey": 5,
|
||||
"scalex": 0.75,
|
||||
"scaley": 0.5,
|
||||
"angle": 45,
|
||||
"rotatex" : 10,
|
||||
"rotatey": 200
|
||||
},
|
||||
{
|
||||
"type": "setposition",
|
||||
"x": 10,
|
Loading…
Reference in a new issue