From 6711aa9cecd9d8a539ed876ad67263ba39e1d26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Wed, 8 Dec 2021 00:19:18 +0100 Subject: [PATCH] use one element stack --- .../layout/elements/ControlElement.java | 1 - .../pdfbox/layout/elements/Cutter.java | 2 ++ .../pdfbox/layout/elements/Dividable.java | 2 -- .../pdfbox/layout/elements/Document.java | 6 ++-- .../pdfbox/layout/elements/Element.java | 3 ++ .../pdfbox/layout/elements/Frame.java | 16 +++++----- .../layout/elements/HorizontalRuler.java | 1 - .../pdfbox/layout/elements/Paragraph.java | 10 ++++++ .../pdfbox/layout/elements/TableElement.java | 11 +++++-- .../pdfbox/layout/elements/TextElement.java | 31 +++++++++++++++++++ .../layout/elements/VerticalSpacer.java | 1 - .../elements/render/VerticalLayout.java | 1 - .../graphics/pdfbox/layout/script/State.java | 26 +++++++++------- .../layout/script/command/BarcodeCommand.java | 2 +- .../layout/script/command/CellCommand.java | 6 ++-- .../script/command/ColumnlayoutCommand.java | 2 +- .../script/command/DocumentCommand.java | 4 +-- .../layout/script/command/FrameCommand.java | 28 +++++++++++++++++ .../command/HorizontalrulerCommand.java | 2 +- .../layout/script/command/ImageCommand.java | 2 +- .../script/command/MarkpositionCommand.java | 2 +- .../script/command/MovepositionCommand.java | 2 +- .../script/command/NewcolumnCommand.java | 2 +- .../layout/script/command/NewpageCommand.java | 2 +- .../script/command/ParagraphCommand.java | 7 +++-- .../layout/script/command/PathCommand.java | 2 +- .../script/command/ResetpositionCommand.java | 2 +- .../layout/script/command/RowCommand.java | 6 ++-- .../script/command/SetpositionCommand.java | 2 +- .../layout/script/command/TableCommand.java | 6 ++-- .../layout/script/command/TextCommand.java | 7 +++-- .../script/command/VerticalspacerCommand.java | 2 +- .../pdfbox/layout/table/AbstractCell.java | 3 +- .../graphics/pdfbox/layout/table/Row.java | 8 +++-- .../pdfbox/layout/test/FramesTest.java | 2 +- .../pdfbox/layout/test/script/elements.json | 3 +- 36 files changed, 152 insertions(+), 63 deletions(-) create mode 100644 graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TextElement.java create mode 100644 graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/FrameCommand.java diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/ControlElement.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/ControlElement.java index 3485583..edf0cdc 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/ControlElement.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/ControlElement.java @@ -26,5 +26,4 @@ public class ControlElement implements Element { public String toString() { return "ControlElement [NEWPAGE=" + NEWPAGE + ", name=" + name + "]"; } - } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Cutter.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Cutter.java index 4e76f4f..b4be3f9 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Cutter.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Cutter.java @@ -13,7 +13,9 @@ import java.io.IOException; public class Cutter implements Dividable, Drawable { private final Drawable undividable; + private final float viewPortY; + private final float viewPortHeight; public Cutter(Drawable undividableElement) throws IOException { diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Dividable.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Dividable.java index 9468d01..90976dc 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Dividable.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Dividable.java @@ -45,7 +45,5 @@ public interface Dividable { public Drawable getTail() { return tail; } - } - } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Document.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Document.java index 4f4d87e..109644c 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Document.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Document.java @@ -27,7 +27,7 @@ import java.util.Map.Entry; /** * The central class for creating a document. */ -public class Document implements Closeable, RenderListener { +public class Document implements Element, Closeable, RenderListener { private final List> elements = new ArrayList<>(); @@ -141,8 +141,10 @@ public class Document implements Closeable, RenderListener { * * @param element the element to add */ - public void add(Element element) { + @Override + public Element add(Element element) { add(element, new VerticalLayoutHint()); + return this; } /** diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Element.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Element.java index 81f40db..de61d86 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Element.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Element.java @@ -5,4 +5,7 @@ package org.xbib.graphics.pdfbox.layout.elements; */ public interface Element { + default Element add(Element element) { + throw new UnsupportedOperationException(); + } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Frame.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Frame.java index 28fba68..a3e3700 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Frame.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Frame.java @@ -77,7 +77,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable { */ public Frame(final Drawable inner, final Float width, final Float height) { this(width, height); - add(inner); + addDrawable(inner); } /** @@ -90,7 +90,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable { * @param height the height to constrain the border-box of the frame to, or * null. */ - public Frame(final Float width, final Float height) { + public Frame(Float width, Float height) { this.givenWidth = width; this.givenHeight = height; } @@ -100,7 +100,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable { * * @param drawable the drawable */ - public void add(final Drawable drawable) { + public void addDrawable(Drawable drawable) { innerList.add(drawable); } @@ -580,13 +580,13 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable { first.addAll(dividedList.getHead()); } if (divided != null) { - first.add(divided.getFirst()); + first.addDrawable(divided.getFirst()); } // create tail sub frame Frame tail = new Frame(getGivenWidth(), tailHeight); copyAllButInnerAndSizeTo(tail); if (divided != null) { - tail.add(divided.getTail()); + tail.addDrawable(divided.getTail()); } if (dividedList.getTail() != null) { tail.addAll(dividedList.getTail()); @@ -622,12 +622,14 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable { } public static class DividedList { + private final List head; + private final Drawable drawableToDivide; + private final List tail; - public DividedList(List head, Drawable drawableToDivide, - List tail) { + public DividedList(List head, Drawable drawableToDivide, List tail) { this.head = head; this.drawableToDivide = drawableToDivide; this.tail = tail; diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/HorizontalRuler.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/HorizontalRuler.java index 933630b..52b8ff6 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/HorizontalRuler.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/HorizontalRuler.java @@ -92,5 +92,4 @@ public class HorizontalRuler implements Drawable, Element, WidthRespecting { public Drawable removeLeadingEmptyVerticalSpace() { return this; } - } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Paragraph.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Paragraph.java index 57d472a..5ea9bd1 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Paragraph.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/Paragraph.java @@ -80,4 +80,14 @@ public class Paragraph extends TextFlow implements Drawable, Element, WidthRespe return new Paragraph(); } + @Override + public Element add(Element element) { + if (element instanceof TextElement) { + TextElement textElement = (TextElement) element; + addMarkup(textElement.getValue(), textElement.getSize(), textElement.getFont()); + } else { + throw new UnsupportedOperationException(); + } + return this; + } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TableElement.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TableElement.java index 8314111..b9b293d 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TableElement.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TableElement.java @@ -24,8 +24,15 @@ public class TableElement implements Element, Drawable, Dividable { this.table = Table.builder(); } - public void add(Row row) { - table.addRow(row); + @Override + public Element add(Element element) { + if (element instanceof Row.Builder) { + Row row = ((Row.Builder) element).build(); + table.addRow(row); + } else { + throw new UnsupportedOperationException(); + } + return this; } public void addColumnOfWidth(float width) { diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TextElement.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TextElement.java new file mode 100644 index 0000000..db51386 --- /dev/null +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/TextElement.java @@ -0,0 +1,31 @@ +package org.xbib.graphics.pdfbox.layout.elements; + +import org.xbib.graphics.pdfbox.layout.font.Font; + +public class TextElement implements Element { + + private final String value; + + private final Font font; + + private final float size; + + public TextElement(String value, Font font, float size) { + this.value = value; + this.font = font; + this.size = size; + } + + public String getValue() { + return value; + } + + public Font getFont() { + return font; + } + + public float getSize() { + return size; + } + +} diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/VerticalSpacer.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/VerticalSpacer.java index 4716d60..1931891 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/VerticalSpacer.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/VerticalSpacer.java @@ -57,5 +57,4 @@ public class VerticalSpacer implements Drawable, Element, Dividable { public Drawable removeLeadingEmptyVerticalSpace() { return this; } - } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/render/VerticalLayout.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/render/VerticalLayout.java index 08a369f..1742088 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/render/VerticalLayout.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/elements/render/VerticalLayout.java @@ -264,5 +264,4 @@ public class VerticalLayout implements Layout { } return drawable; } - } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/State.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/State.java index 3ec259a..c718018 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/State.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/State.java @@ -1,24 +1,26 @@ package org.xbib.graphics.pdfbox.layout.script; import org.xbib.graphics.pdfbox.layout.elements.Document; -import org.xbib.graphics.pdfbox.layout.elements.Paragraph; -import org.xbib.graphics.pdfbox.layout.elements.TableElement; -import org.xbib.graphics.pdfbox.layout.table.Row; +import org.xbib.graphics.pdfbox.layout.elements.Element; -import java.util.Collection; +import java.util.List; import java.util.Stack; +import java.util.stream.Collectors; public class State { - public Stack documents = new Stack<>(); + public Stack elements = new Stack<>(); - public Stack paragraphs = new Stack<>(); + public Document getDocument() { + List list = getDocuments(); + int size = list.size(); + return size > 0 ? list.get(size - 1) : null; + } - public Stack tables = new Stack<>(); - - public Stack rows = new Stack<>(); - - public Collection getDocuments() { - return documents; + public List getDocuments() { + return elements.stream() + .filter(e -> e instanceof Document) + .map(e -> (Document) e) + .collect(Collectors.toList()); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/BarcodeCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/BarcodeCommand.java index 64bdbab..6c5f562 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/BarcodeCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/BarcodeCommand.java @@ -38,6 +38,6 @@ public class BarcodeCommand implements Command { if (settings.containsSetting("scale")) { element.setScale(settings.getAsFloat("scale", element.getScale())); } - state.documents.peek().add(element); + state.elements.peek().add(element); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/CellCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/CellCommand.java index c38ac56..79918a9 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/CellCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/CellCommand.java @@ -1,6 +1,7 @@ package org.xbib.graphics.pdfbox.layout.script.command; import org.xbib.graphics.pdfbox.layout.color.ColorFactory; +import org.xbib.graphics.pdfbox.layout.elements.Document; import org.xbib.graphics.pdfbox.layout.font.Font; import org.xbib.graphics.pdfbox.layout.font.Fonts; import org.xbib.graphics.pdfbox.layout.script.Engine; @@ -21,7 +22,8 @@ public class CellCommand implements Command { TextCell.Builder cell = TextCell.builder(); cell.text(settings.get("value")); cell.fontSize(settings.getAsFloat("size", 11.0f)); - Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(state.documents.peek()); + Document document = state.getDocument(); + Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(document); cell.font(font); cell.padding(settings.getAsFloat("padding", 0f)); Color color = ColorFactory.web(settings.get("color", "black")); @@ -39,6 +41,6 @@ public class CellCommand implements Command { cell.borderStyle(styleInterface); cell.colSpan(settings.getAsInt("colspan", 1)); cell.rowSpan(settings.getAsInt("rowspan", 1)); - state.rows.peek().add(cell.build()); + state.elements.peek().add(cell.build()); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ColumnlayoutCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ColumnlayoutCommand.java index 0ad1ddf..6032972 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ColumnlayoutCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ColumnlayoutCommand.java @@ -12,6 +12,6 @@ public class ColumnlayoutCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { ColumnLayout columnLayout = new ColumnLayout(settings.getAsInt("columns", 2), settings.getAsFloat("spacing", 10f)); - state.documents.peek().add(columnLayout); + state.elements.peek().add(columnLayout); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/DocumentCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/DocumentCommand.java index f10d318..d1c1044 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/DocumentCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/DocumentCommand.java @@ -14,8 +14,6 @@ public class DocumentCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { - state.paragraphs.clear(); - state.tables.clear(); String margin = settings.get("margin", "0 0 0 0"); String[] margins = margin.split(" "); PageFormat pageFormat = PageFormat.builder() @@ -42,7 +40,7 @@ public class DocumentCommand implements Command { if (settings.containsSetting("title")) { document.setTitle(settings.get("title")); } - state.documents.push(document); + state.elements.push(document); engine.executeElements(settings); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/FrameCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/FrameCommand.java new file mode 100644 index 0000000..0f096eb --- /dev/null +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/FrameCommand.java @@ -0,0 +1,28 @@ +package org.xbib.graphics.pdfbox.layout.script.command; + +import org.xbib.graphics.pdfbox.layout.color.ColorFactory; +import org.xbib.graphics.pdfbox.layout.elements.Frame; +import org.xbib.graphics.pdfbox.layout.script.Engine; +import org.xbib.graphics.pdfbox.layout.script.State; +import org.xbib.settings.Settings; + +import java.awt.Color; +import java.io.IOException; + +public class FrameCommand implements Command { + + @Override + public void execute(Engine engine, State state, Settings settings) throws IOException { + Frame frame = new Frame(); + //frame.setPadding(settings.getAsFloat("padding", 0f)); + if (settings.containsSetting("backgroundcolor")) { + Color backgroundColor = ColorFactory.web(settings.get("backgroundcolor", "black")); + frame.setBackgroundColor(backgroundColor); + } + if (settings.containsSetting("bordercolor")) { + Color borderColor = ColorFactory.web(settings.get("bordercolor", "black")); + frame.setBorderColor(borderColor); + } + state.elements.peek().add(frame); + } +} diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/HorizontalrulerCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/HorizontalrulerCommand.java index f598698..3c69a3b 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/HorizontalrulerCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/HorizontalrulerCommand.java @@ -23,6 +23,6 @@ public class HorizontalrulerCommand implements Command { } Color color = ColorFactory.web(settings.get("color", "black")); HorizontalRuler horizontalRuler = new HorizontalRuler(strokeBuilder.build(), color); - state.documents.peek().add(horizontalRuler); + state.elements.peek().add(horizontalRuler); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ImageCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ImageCommand.java index b66ab61..0c99eb9 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ImageCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ImageCommand.java @@ -35,6 +35,6 @@ public class ImageCommand implements Command { float margintop = Float.parseFloat(margins[2]); float marginbottom = Float.parseFloat(margins[3]); VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, true); - state.documents.peek().add(imageElement, verticalLayoutHint); + state.elements.peek().add(imageElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MarkpositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MarkpositionCommand.java index e0b9079..22878dd 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MarkpositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MarkpositionCommand.java @@ -12,6 +12,6 @@ public class MarkpositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { PositionControl.MarkPosition markPosition = PositionControl.createMarkPosition(); - state.documents.peek().add(markPosition); + state.elements.peek().add(markPosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MovepositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MovepositionCommand.java index 9598fff..c355ac0 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MovepositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/MovepositionCommand.java @@ -12,6 +12,6 @@ public class MovepositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { PositionControl.MovePosition movePosition = PositionControl.createMovePosition(settings.getAsFloat("x", null), settings.getAsFloat("y", null)); - state.documents.peek().add(movePosition); + state.elements.peek().add(movePosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewcolumnCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewcolumnCommand.java index 85c6811..a0723b7 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewcolumnCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewcolumnCommand.java @@ -11,6 +11,6 @@ public class NewcolumnCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { ControlElement controlElement = ControlElement.NEWCOLUMN; - state.documents.peek().add(controlElement); + state.elements.peek().add(controlElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewpageCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewpageCommand.java index 4f45b63..939624f 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewpageCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/NewpageCommand.java @@ -11,6 +11,6 @@ public class NewpageCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { ControlElement controlElement = ControlElement.NEWPAGE; - state.documents.peek().add(controlElement); + state.elements.peek().add(controlElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ParagraphCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ParagraphCommand.java index dbcb10b..95a626c 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ParagraphCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ParagraphCommand.java @@ -15,17 +15,18 @@ public class ParagraphCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { Paragraph paragraph = new Paragraph(); - state.paragraphs.push(paragraph); if (settings.containsSetting("x") && settings.containsSetting("y")) { paragraph.setAbsolutePosition(new Position(settings.getAsFloat("x", 0f), settings.getAsFloat("y", 0f))); } if (settings.containsSetting("width")) { - paragraph.setMaxWidth(settings.getAsFloat("width", state.documents.peek().getPageWidth())); + paragraph.setMaxWidth(settings.getAsFloat("width", 0f)); } if (settings.containsSetting("alignment")) { paragraph.setAlignment(Alignment.valueOf(settings.get("alignment", "left").toUpperCase(Locale.ROOT))); } - state.documents.peek().add(paragraph); + state.elements.push(paragraph); engine.executeElements(settings); + state.elements.pop(); + state.elements.peek().add(paragraph); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/PathCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/PathCommand.java index 9e39864..545dcf3 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/PathCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/PathCommand.java @@ -47,6 +47,6 @@ public class PathCommand implements Command { strokeBuilder.dashPattern(new Stroke.DashPattern(settings.getAsFloat("dash", 1f))); } Color color = ColorFactory.web(settings.get("color", "black")); - state.documents.peek().add(new PathElement(path, strokeBuilder.build(), color, position)); + state.elements.peek().add(new PathElement(path, strokeBuilder.build(), color, position)); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ResetpositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ResetpositionCommand.java index dba92c4..db89928 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ResetpositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/ResetpositionCommand.java @@ -12,6 +12,6 @@ public class ResetpositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { PositionControl.ResetPosition resetPosition = PositionControl.createResetPosition(); - state.documents.peek().add(resetPosition); + state.elements.peek().add(resetPosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/RowCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/RowCommand.java index b1aa147..cd3541d 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/RowCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/RowCommand.java @@ -13,9 +13,9 @@ public class RowCommand implements Command { public void execute(Engine engine, State state, Settings settings) throws IOException { Row.Builder row = Row.builder(); row.padding(settings.getAsFloat("padding", 0f)); - - state.rows.push(row); + state.elements.push(row); engine.executeElements(settings); - state.tables.peek().add(row.build()); + state.elements.pop(); + state.elements.peek().add(row); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/SetpositionCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/SetpositionCommand.java index 2248a06..a092a5f 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/SetpositionCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/SetpositionCommand.java @@ -12,6 +12,6 @@ public class SetpositionCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { PositionControl.SetPosition setPosition = PositionControl.createSetPosition(settings.getAsFloat("x", null), settings.getAsFloat("y", null)); - state.documents.peek().add(setPosition); + state.elements.peek().add(setPosition); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TableCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TableCommand.java index 7c871b4..da8e139 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TableCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TableCommand.java @@ -11,7 +11,6 @@ public class TableCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { - state.rows.clear(); TableElement tableElement = new TableElement(); if (settings.containsSetting("columnwidths")) { String columnWidths = settings.get("columnwidths"); @@ -19,8 +18,9 @@ public class TableCommand implements Command { tableElement.addColumnOfWidth(Float.parseFloat(columnWidth)); } } - state.documents.peek().add(tableElement); - state.tables.push(tableElement); + state.elements.push(tableElement); engine.executeElements(settings); + state.elements.pop(); + state.elements.peek().add(tableElement); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TextCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TextCommand.java index 3089253..a3f3390 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TextCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/TextCommand.java @@ -1,5 +1,7 @@ package org.xbib.graphics.pdfbox.layout.script.command; +import org.xbib.graphics.pdfbox.layout.elements.Document; +import org.xbib.graphics.pdfbox.layout.elements.TextElement; import org.xbib.graphics.pdfbox.layout.font.Font; import org.xbib.graphics.pdfbox.layout.font.Fonts; import org.xbib.graphics.pdfbox.layout.script.Engine; @@ -14,7 +16,8 @@ public class TextCommand implements Command { public void execute(Engine engine, State state, Settings settings) { String value = settings.get("value"); float size = settings.getAsFloat("size", 11.0f); - Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(state.documents.peek()); - state.paragraphs.peek().addMarkup(value, size, font); + Document document = state.getDocument(); + Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(document); + state.elements.peek().add(new TextElement(value, font, size)); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/VerticalspacerCommand.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/VerticalspacerCommand.java index 7ff35f5..fadb790 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/VerticalspacerCommand.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/script/command/VerticalspacerCommand.java @@ -11,6 +11,6 @@ public class VerticalspacerCommand implements Command { @Override public void execute(Engine engine, State state, Settings settings) throws IOException { VerticalSpacer verticalSpacer = new VerticalSpacer(settings.getAsFloat("height", 0f)); - state.documents.peek().add(verticalSpacer); + state.elements.peek().add(verticalSpacer); } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/AbstractCell.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/AbstractCell.java index 9badc3c..d9f9452 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/AbstractCell.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/AbstractCell.java @@ -1,10 +1,11 @@ package org.xbib.graphics.pdfbox.layout.table; +import org.xbib.graphics.pdfbox.layout.elements.Element; import org.xbib.graphics.pdfbox.layout.table.render.AbstractCellRenderer; import org.xbib.graphics.pdfbox.layout.table.render.Renderer; import java.awt.Color; -public abstract class AbstractCell { +public abstract class AbstractCell implements Element { private static final float DEFAULT_MIN_HEIGHT = 10f; diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/Row.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/Row.java index 2c1fe7e..9837efd 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/Row.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/table/Row.java @@ -1,5 +1,6 @@ package org.xbib.graphics.pdfbox.layout.table; +import org.xbib.graphics.pdfbox.layout.elements.Element; import org.xbib.graphics.pdfbox.layout.font.Font; import java.awt.Color; @@ -89,7 +90,7 @@ public class Row { return new Builder(); } - public static class Builder { + public static class Builder implements Element { private final List cells = new ArrayList<>(); @@ -98,8 +99,9 @@ public class Row { private Builder() { } - public Builder add(final AbstractCell cell) { - cells.add(cell); + @Override + public Builder add(Element element) { + cells.add((AbstractCell) element); return this; } diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/FramesTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/FramesTest.java index 6523077..ed8c9b3 100644 --- a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/FramesTest.java +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/FramesTest.java @@ -76,7 +76,7 @@ public class FramesTest { paragraph = new Paragraph(); paragraph.addMarkup(text2, 11, BaseFont.TIMES); paragraph.addMarkup(text2, 11, BaseFont.TIMES); - frame.add(paragraph); + frame.addDrawable(paragraph); document.add(frame); document.render().save(new FileOutputStream("build/frames.pdf")).close(); } diff --git a/graphics-pdfbox-layout/src/test/resources/org/xbib/graphics/pdfbox/layout/test/script/elements.json b/graphics-pdfbox-layout/src/test/resources/org/xbib/graphics/pdfbox/layout/test/script/elements.json index 4603de2..7ac8306 100644 --- a/graphics-pdfbox-layout/src/test/resources/org/xbib/graphics/pdfbox/layout/test/script/elements.json +++ b/graphics-pdfbox-layout/src/test/resources/org/xbib/graphics/pdfbox/layout/test/script/elements.json @@ -1,6 +1,6 @@ { "type": "document", - "margin": "10 10 10 10", + "margin": "20 20 20 20", "author": "Jörg Prante", "elements": [ { @@ -80,6 +80,7 @@ }, { "type": "table", + "padding": 10, "columnwidths": "50 50 50", "elements": [ {