add script engine

This commit is contained in:
Jörg Prante 2021-12-06 11:36:24 +01:00
parent 813cba2acb
commit 0e30fc69df
41 changed files with 2117 additions and 140 deletions

View file

@ -0,0 +1,51 @@
package org.xbib.graphics.barcode;
public enum Symbols {
AustraliaPost,
AztecCode,
ChannelCode,
Codabar,
CodablockF,
Code2Of5,
Code3Of9,
Code3Of9Extended,
Code11,
Code16k,
Code32,
Code49,
Code93,
Code128,
CodeOne,
Composite,
DataBar14,
DataBarExpanded,
DataBarLimited,
DataMatrix,
Ean,
GridMatrix,
JapanPost,
KixCode,
KoreaPost,
Logmars,
MaxiCode,
MicroQrCode,
MsiPlessey,
Nve18,
Pdf417,
Pharmacode,
Pharmacode2Track,
Pharmazentralnummer,
Postnet,
QrCode,
RoyalMail4State,
Telepen,
Upc,
UspsOneCode,
UspsPackage;
@SuppressWarnings("unchecked")
public Symbol getSymbol() throws Exception {
Class<? extends AbstractSymbol> cl = (Class<? extends AbstractSymbol>) getClass().getClassLoader().loadClass(getClass().getPackageName() + "." + name());
return cl.getDeclaredConstructor().newInstance();
}
}

View file

@ -122,8 +122,8 @@ public class SymbolTest {
}
String backend = "org.xbib.graphics.barcode";
Reflections reflections = new Reflections(backend);
Set< Class< ? extends AbstractSymbol>> symbols = reflections.getSubTypesOf(AbstractSymbol.class);
List< Object[] > data = new ArrayList<>();
Set<Class<? extends AbstractSymbol>> symbols = reflections.getSubTypesOf(AbstractSymbol.class);
List<Object[]> data = new ArrayList<>();
for (Class< ? extends AbstractSymbol> symbol : symbols) {
String symbolName = symbol.getSimpleName().toLowerCase();
String dir = "src/test/resources/" + backend.replace('.', '/') + "/" + symbolName;

View file

@ -1,8 +1,11 @@
module org.xbib.graphics.layout.pdfbox {
exports org.xbib.graphics.pdfbox.layout.boxable;
exports org.xbib.graphics.pdfbox.layout.color;
exports org.xbib.graphics.pdfbox.layout.elements;
exports org.xbib.graphics.pdfbox.layout.elements.render;
exports org.xbib.graphics.pdfbox.layout.font;
exports org.xbib.graphics.pdfbox.layout.script;
exports org.xbib.graphics.pdfbox.layout.script.command;
exports org.xbib.graphics.pdfbox.layout.shape;
exports org.xbib.graphics.pdfbox.layout.table;
exports org.xbib.graphics.pdfbox.layout.table.render;
@ -11,6 +14,7 @@ module org.xbib.graphics.layout.pdfbox {
exports org.xbib.graphics.pdfbox.layout.util;
requires transitive org.xbib.graphics.barcode;
requires transitive org.xbib.graphics.pdfbox;
requires transitive org.xbib.settings.api;
requires org.xbib.settings.datastructures;
requires transitive java.desktop;
requires java.logging;

View file

@ -21,24 +21,27 @@ public class BarcodeElement implements Element, Drawable, Dividable, WidthRespec
private float height;
private float scale;
private float maxWidth = -1;
private Position absolutePosition;
public BarcodeElement(Symbol symbol) {
this.symbol = symbol;
this.width = symbol.getWidth();
this.height = symbol.getHeight();
setWidth(symbol.getWidth());
setHeight(symbol.getHeight());
setScale(1.0f);
}
public void setScale(float scale) {
this.scale = scale;
setWidth(width * scale);
setHeight(height * scale);
}
@Override
public float getWidth() throws IOException {
return width;
public float getScale() {
return scale;
}
public void setWidth(float width) {
@ -46,14 +49,19 @@ public class BarcodeElement implements Element, Drawable, Dividable, WidthRespec
}
@Override
public float getHeight() throws IOException {
return height;
public float getWidth() throws IOException {
return width;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public float getHeight() throws IOException {
return height;
}
@Override
public Divided divide(float remainingHeight, float nextPageHeight)
throws IOException {

View file

@ -11,6 +11,11 @@ public class ControlElement implements Element {
*/
public final static ControlElement NEWPAGE = new ControlElement("NEWPAGE");
/**
* Triggers flip to the next column.
*/
public final static ControlElement NEWCOLUMN = new ControlElement("NEWCOLUMN");
private final String name;
public ControlElement(final String name) {

View file

@ -98,7 +98,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
/**
* Adds a drawable to the frame.
*
* @param drawable
* @param drawable the drawable
*/
public void add(final Drawable drawable) {
innerList.add(drawable);
@ -154,8 +154,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
* stroke} and {@link #getBorderColor() color} is set.
*/
protected boolean hasBorder() {
return getShape() != null && getBorderStroke() != null
&& getBorderColor() != null;
return getShape() != null && getBorderStroke() != null && getBorderColor() != null;
}
/**
@ -490,11 +489,9 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
private void setMaxWidth(final Drawable inner, float maxWidth) {
if (inner instanceof WidthRespecting) {
if (getGivenWidth() != null) {
((WidthRespecting) inner).setMaxWidth(getGivenWidth()
- getHorizontalShapeSpacing());
((WidthRespecting) inner).setMaxWidth(getGivenWidth() - getHorizontalShapeSpacing());
} else if (maxWidth >= 0) {
((WidthRespecting) inner).setMaxWidth(maxWidth
- getHorizontalSpacing());
((WidthRespecting) inner).setMaxWidth(maxWidth - getHorizontalSpacing());
}
}
}
@ -514,22 +511,15 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
@Override
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
Position upperLeft, DrawListener drawListener) throws IOException {
setInnerMaxWidthIfNecessary();
float halfBorderWidth = 0;
if (getBorderWidth() > 0) {
halfBorderWidth = getBorderWidth() / 2f;
}
upperLeft = upperLeft.add(getMarginLeft() + halfBorderWidth,
-getMarginTop() - halfBorderWidth);
upperLeft = upperLeft.add(getMarginLeft() + halfBorderWidth, -getMarginTop() - halfBorderWidth);
if (getShape() != null) {
float shapeWidth = getWidth() - getMarginLeft() - getMarginRight()
- getBorderWidth();
float shapeHeight = getHeight() - getMarginTop()
- getMarginBottom() - getBorderWidth();
float shapeWidth = getWidth() - getMarginLeft() - getMarginRight() - getBorderWidth();
float shapeHeight = getHeight() - getMarginTop() - getMarginBottom() - getBorderWidth();
if (getBackgroundColor() != null) {
getShape().fill(pdDocument, contentStream, upperLeft,
shapeWidth, shapeHeight, getBackgroundColor(),
@ -541,10 +531,7 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
getBorderStroke(), drawListener);
}
}
Position innerUpperLeft = upperLeft.add(getPaddingLeft()
+ halfBorderWidth, -getPaddingTop() - halfBorderWidth);
Position innerUpperLeft = upperLeft.add(getPaddingLeft() + halfBorderWidth, -getPaddingTop() - halfBorderWidth);
for (Drawable inner : innerList) {
inner.draw(pdDocument, contentStream, innerUpperLeft, drawListener);
innerUpperLeft = innerUpperLeft.add(0, -inner.getHeight());
@ -562,23 +549,16 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
}
@Override
public Divided divide(float remainingHeight, float nextPageHeight)
throws IOException {
public Divided divide(float remainingHeight, float nextPageHeight) throws IOException {
setInnerMaxWidthIfNecessary();
if (remainingHeight - getVerticalSpacing() <= 0) {
return new Divided(new VerticalSpacer(remainingHeight), this);
}
// find first inner that does not fit on page
float spaceLeft = remainingHeight - getVerticalSpacing();
DividedList dividedList = divideList(innerList, spaceLeft);
float spaceLeftForDivided = spaceLeft
- getHeight(dividedList.getHead());
float spaceLeftForDivided = spaceLeft - getHeight(dividedList.getHead());
Divided divided = null;
if (dividedList.getDrawableToDivide() != null) {
Dividable innerDividable = null;
if (dividedList.getDrawableToDivide() instanceof Dividable) {
@ -590,11 +570,9 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
divided = innerDividable.divide(spaceLeftForDivided, nextPageHeight
- getVerticalSpacing());
}
Float firstHeight = getGivenHeight() == null ? null : remainingHeight;
Float tailHeight = getGivenHeight() == null ? null : getGivenHeight()
- spaceLeft;
// create head sub frame
Frame first = new Frame(getGivenWidth(), firstHeight);
copyAllButInnerAndSizeTo(first);
@ -604,7 +582,6 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
if (divided != null) {
first.add(divided.getFirst());
}
// create tail sub frame
Frame tail = new Frame(getGivenWidth(), tailHeight);
copyAllButInnerAndSizeTo(tail);
@ -614,21 +591,17 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
if (dividedList.getTail() != null) {
tail.addAll(dividedList.getTail());
}
return new Divided(first, tail);
}
private DividedList divideList(List<Drawable> items, float spaceLeft)
throws IOException {
private DividedList divideList(List<Drawable> items, float spaceLeft) throws IOException {
List<Drawable> head = null;
List<Drawable> tail = null;
Drawable toDivide = null;
float tmpHeight = 0;
int index = 0;
while (tmpHeight < spaceLeft) {
tmpHeight += items.get(index).getHeight();
if (tmpHeight == spaceLeft) {
// we can split between two drawables
head = items.subList(0, index + 1);
@ -636,7 +609,6 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
tail = items.subList(index + 1, items.size());
}
}
if (tmpHeight > spaceLeft) {
head = items.subList(0, index);
toDivide = items.get(index);
@ -644,10 +616,8 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
tail = items.subList(index + 1, items.size());
}
}
++index;
}
return new DividedList(head, toDivide, tail);
}
@ -674,7 +644,5 @@ public class Frame implements Element, Drawable, WidthRespecting, Dividable {
public List<Drawable> getTail() {
return tail;
}
}
}

View file

@ -7,8 +7,12 @@ import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
import org.xbib.graphics.pdfbox.layout.text.Position;
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
public class ImageElement implements Element, Drawable, Dividable, WidthRespecting {
@ -24,10 +28,16 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
private float height;
private float scale;
private float maxWidth = -1;
private Position absolutePosition;
public ImageElement(String base64) throws IOException {
this(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64))));
}
public ImageElement(BufferedImage image) {
this.image = image;
this.width = image.getWidth();
@ -35,19 +45,13 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
}
public void setScale(float scale) {
this.scale = scale;
setWidth(width * scale);
setHeight(height * scale);
}
@Override
public float getWidth() throws IOException {
if (width == SCALE_TO_RESPECT_WIDTH) {
if (getMaxWidth() > 0 && image.getWidth() > getMaxWidth()) {
return getMaxWidth();
}
return image.getWidth();
}
return width;
public float getScale() {
return scale;
}
/**
@ -62,15 +66,14 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
}
@Override
public float getHeight() throws IOException {
if (height == SCALE_TO_RESPECT_WIDTH) {
public float getWidth() throws IOException {
if (width == SCALE_TO_RESPECT_WIDTH) {
if (getMaxWidth() > 0 && image.getWidth() > getMaxWidth()) {
return getMaxWidth() / (float) image.getWidth()
* (float) image.getHeight();
return getMaxWidth();
}
return image.getHeight();
return image.getWidth();
}
return height;
return width;
}
/**
@ -86,8 +89,18 @@ public class ImageElement implements Element, Drawable, Dividable, WidthRespecti
}
@Override
public Divided divide(float remainingHeight, float nextPageHeight)
throws IOException {
public float getHeight() throws IOException {
if (height == SCALE_TO_RESPECT_WIDTH) {
if (getMaxWidth() > 0 && image.getWidth() > getMaxWidth()) {
return getMaxWidth() / (float) image.getWidth() * (float) image.getHeight();
}
return image.getHeight();
}
return height;
}
@Override
public Divided divide(float remainingHeight, float nextPageHeight) throws IOException {
if (getHeight() <= nextPageHeight) {
return new Divided(new VerticalSpacer(remainingHeight), this);
}

View file

@ -0,0 +1,76 @@
package org.xbib.graphics.pdfbox.layout.elements;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.xbib.graphics.pdfbox.layout.shape.Path;
import org.xbib.graphics.pdfbox.layout.shape.Stroke;
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
import org.xbib.graphics.pdfbox.layout.text.Position;
import java.awt.Color;
import java.io.IOException;
public class PathElement implements Drawable, Element {
private final Path path;
private final Stroke stroke;
private final Color color;
private final Position position;
private float width;
private float height;
public PathElement(Path path, Stroke stroke, Color color, Position position) {
this.path = path;
this.stroke = stroke;
this.color = color;
this.position = position;
setWidth(0f);
}
public Stroke getStroke() {
return stroke;
}
public Color getColor() {
return color;
}
public void setWidth(float width) {
this.width = width;
}
@Override
public float getWidth() throws IOException {
return width;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public float getHeight() throws IOException {
return height;
}
@Override
public Position getAbsolutePosition() {
return position;
}
@Override
public void draw(PDDocument pdDocument, PDPageContentStream contentStream,
Position upperLeft, DrawListener drawListener) throws IOException {
path.draw(pdDocument, contentStream, upperLeft, getWidth(), getHeight(), color, stroke, drawListener);
}
@Override
public Drawable removeLeadingEmptyVerticalSpace() {
return this;
}
}

View file

@ -24,8 +24,7 @@ public class Rectangle extends Dimension {
@Override
public String toString() {
return "Rectangle [x=" + x + ", y=" + y + ", width=" + getWidth()
+ ", height=" + getHeight() + "]";
return "Rectangle [x=" + x + ", y=" + y + ", width=" + getWidth() + ", height=" + getHeight() + "]";
}
@Override
@ -54,5 +53,4 @@ public class Rectangle extends Dimension {
}
return Float.floatToIntBits(y) == Float.floatToIntBits(other.y);
}
}

View file

@ -13,18 +13,13 @@ import java.io.IOException;
*/
public class ColumnLayout extends VerticalLayout {
/**
* Triggers flip to the next column.
*/
public final static ControlElement NEWCOLUMN = new ControlElement("NEWCOLUMN");
private final int columnCount;
private final float columnSpacing;
private int columnIndex = 0;
private Float offsetY = null;
public ColumnLayout(int columnCount) {
this(columnCount, 0);
}
@ -65,7 +60,7 @@ public class ColumnLayout extends VerticalLayout {
renderContext.newPage();
return true;
}
if (element == NEWCOLUMN) {
if (element == ControlElement.NEWCOLUMN) {
turnPage(renderContext);
return true;
}

View file

@ -0,0 +1,17 @@
package org.xbib.graphics.pdfbox.layout.font;
import org.xbib.graphics.pdfbox.layout.elements.Document;
public enum Fonts {
HELVETICA,
TIMES,
COURIER,
NOTOSANS;
public Font getFont(Document document) {
if ("notosans".equalsIgnoreCase(name())) {
return new NotoSansFont(document);
}
return BaseFont.valueOf(name());
}
}

View file

@ -4,26 +4,39 @@ import org.xbib.graphics.pdfbox.layout.script.command.Command;
import org.xbib.settings.Settings;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
public class Engine {
private final String packageName;
private final ClassLoader classLoader;
private final State state;
public Engine() {
packageName = getClass().getPackageName();
classLoader = getClass().getClassLoader();
this.state = new State();
}
public void execute(String key, Settings settings) throws IOException {
State state = new State();
String packageName = getClass().getPackageName();
ClassLoader classLoader = getClass().getClassLoader();
for (Map.Entry<String, Settings> entry : settings.getGroups(key).entrySet()) {
public void execute(Settings settings) throws IOException {
execute("document", state, settings);
}
public void execute(String prefix, State state, Settings settings) throws IOException {
Settings subSettings = settings.getByPrefix(prefix);
Set<String> set = subSettings.getAsStructuredMap().keySet();
for (String string : set) {
try {
String string = entry.getKey();
Class<?> cl = classLoader.loadClass(packageName + ".command." + string.substring(0, 1).toUpperCase() + string.substring(1) + "Command");
Settings thisSettings = settings.getAsSettings(prefix + string);
String type = thisSettings.get("type");
if (type == null) {
type = prefix;
}
String className = packageName + ".command." + type.substring(0, 1).toUpperCase() + type.substring(1) + "Command";
Class<?> cl = classLoader.loadClass(className);
Command command = (Command) cl.getConstructor().newInstance();
command.execute(this, state, entry.getValue());
command.execute(this, state, thisSettings);
} catch (Exception e) {
throw new IOException(e);
}

View file

@ -1,8 +1,15 @@
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.PathElement;
public class State {
public Document document;
public Paragraph paragraph;
public PathElement pathElement;
}

View file

@ -0,0 +1,41 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.barcode.HumanReadableLocation;
import org.xbib.graphics.barcode.Symbol;
import org.xbib.graphics.barcode.Symbols;
import org.xbib.graphics.pdfbox.layout.elements.BarcodeElement;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.graphics.pdfbox.layout.text.Position;
import org.xbib.settings.Settings;
import java.io.IOException;
public class BarcodeCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
BarcodeElement element;
try {
Symbol symbol = Symbols.valueOf(settings.get("symbol")).getSymbol();
symbol.setContent(settings.get("value"));
symbol.setBarHeight(settings.getAsInt("barheight", 150));
symbol.setHumanReadableLocation(HumanReadableLocation.valueOf(settings.get("readablelocation", "BOTTOM")));
element = new BarcodeElement(symbol);
} catch (Exception e) {
throw new IOException(e);
}
if (settings.containsSetting("x") && settings.containsSetting("y")) {
element.setAbsolutePosition(new Position(settings.getAsFloat("x", 0f), settings.getAsFloat("y", 0f)));
}
if (settings.containsSetting("width")) {
element.setWidth(settings.getAsFloat("width", element.getWidth()));
}
if (settings.containsSetting("height")) {
element.setWidth(settings.getAsFloat("height", element.getHeight()));
}
if (settings.containsSetting("scale")) {
element.setScale(settings.getAsFloat("scale", element.getScale()));
}
state.document.add(element);
}
}

View file

@ -12,11 +12,20 @@ public class DocumentCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
String margin = settings.get("margin", "0 0 0 0");
String[] margins = margin.split(" ");
PageFormat pageFormat = PageFormat.builder()
.marginLeft(Float.parseFloat(margins[0]))
.marginRight(Float.parseFloat(margins[1]))
.marginTop(Float.parseFloat(margins[2]))
.marginBottom(Float.parseFloat(margins[3]))
.pageFormat(settings.get("format", "A4"))
.orientation(settings.get("orientiation", "PORTRAIT"))
.build();
state.document = new Document(pageFormat);
engine.execute("paragraoh", settings);
engine.execute("image", state, settings);
engine.execute("barcode", state, settings);
engine.execute("path", state, settings);
engine.execute("paragraph", state, settings);
}
}

View file

@ -0,0 +1,31 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.elements.ImageElement;
import org.xbib.graphics.pdfbox.layout.elements.render.VerticalLayoutHint;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.graphics.pdfbox.layout.text.Alignment;
import org.xbib.graphics.pdfbox.layout.text.Position;
import org.xbib.settings.Settings;
import java.io.IOException;
public class ImageCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
ImageElement element = new ImageElement(settings.get("value"));
if (settings.containsSetting("x") && settings.containsSetting("y")) {
element.setAbsolutePosition(new Position(settings.getAsFloat("x", 0f), settings.getAsFloat("y", 0f)));
}
if (settings.containsSetting("width")) {
element.setWidth(settings.getAsFloat("width", element.getWidth()));
}
if (settings.containsSetting("height")) {
element.setWidth(settings.getAsFloat("height", element.getHeight()));
}
if (settings.containsSetting("scale")) {
element.setScale(settings.getAsFloat("scale", element.getScale()));
}
state.document.add(element, new VerticalLayoutHint(Alignment.LEFT, 10, 10, 10, 10, true));
}
}

View file

@ -1,7 +1,9 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.elements.Paragraph;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.graphics.pdfbox.layout.text.Position;
import org.xbib.settings.Settings;
import java.io.IOException;
@ -10,5 +12,14 @@ public class ParagraphCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
state.paragraph = new Paragraph();
if (settings.containsSetting("x") && settings.containsSetting("y")) {
state.paragraph.setAbsolutePosition(new Position(settings.getAsFloat("x", 0f), settings.getAsFloat("y", 0f)));
}
if (settings.containsSetting("width")) {
state.paragraph.setMaxWidth(settings.getAsFloat("width", state.document.getPageWidth()));
}
state.document.add(state.paragraph);
engine.execute("text", state, settings);
}
}

View file

@ -0,0 +1,51 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.color.ColorFactory;
import org.xbib.graphics.pdfbox.layout.elements.PathElement;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.graphics.pdfbox.layout.shape.Path;
import org.xbib.graphics.pdfbox.layout.shape.Stroke;
import org.xbib.graphics.pdfbox.layout.text.Position;
import org.xbib.settings.Settings;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
public class PathCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) {
String value = settings.get("value");
if (value == null) {
return;
}
List<Position> list = new ArrayList<>();
String[] s = value.split(" ");
Position position = null;
if (s.length > 0) {
if (settings.getAsBoolean("absolute", false)) {
position = new Position(Float.parseFloat(s[0]), Float.parseFloat(s[1]));
list.add(position);
} else {
Position p = new Position(Float.parseFloat(s[0]), Float.parseFloat(s[1]));
list.add(p);
}
for (int i = 2; i < s.length; i += 2) {
Position p = new Position(Float.parseFloat(s[i]), Float.parseFloat(s[i + 1]));
list.add(p);
}
}
Path path = new Path(list);
Stroke.StrokeBuilder strokeBuilder = Stroke.builder()
.capStyle(Stroke.CapStyle.valueOf(settings.get("capstyie", "Cap")))
.joinStyle(Stroke.JoinStyle.valueOf(settings.get("joinstyle", "Miter")))
.lineWidth(settings.getAsFloat("linewidth", 1f));
if (settings.containsSetting("dash")) {
strokeBuilder.dashPattern(new Stroke.DashPattern(settings.getAsFloat("dash", 1f)));
}
Color color = ColorFactory.web(settings.get("color", "black"));
state.document.add(new PathElement(path, strokeBuilder.build(), color, position));
}
}

View file

@ -0,0 +1,17 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.font.Fonts;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.settings.Settings;
public class TextCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) {
String value = settings.get("value");
float size = settings.getAsFloat("size", 12.0f);
Font font = Fonts.valueOf(settings.get("font", "HELVETICA")).getFont(state.document);
state.paragraph.addMarkup(value, size, font);
}
}

View file

@ -20,7 +20,6 @@ public class Ellipse extends RoundRect {
protected void addRoundRect(PDPageContentStream contentStream,
Position upperLeft, float width, float height, float cornerRadiusX,
float cornerRadiusY) throws IOException {
super.addRoundRect(contentStream, upperLeft, width, height, width / 2f,
height / 2);
super.addRoundRect(contentStream, upperLeft, width, height, width / 2f, height / 2);
}
}

View file

@ -7,33 +7,45 @@ import org.xbib.graphics.pdfbox.layout.text.Position;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Path implements Shape {
private final List<Position> list;
public Path() {
this.list = new ArrayList<>();
public Path(List<Position> list) {
this.list = list;
}
@Override
public void draw(PDDocument pdDocument, PDPageContentStream contentStream, Position upperLeft,
float width, float height, Color color, Stroke stroke, DrawListener drawListener) throws IOException {
public void draw(PDDocument pdDocument,
PDPageContentStream contentStream,
Position upperLeft,
float width,
float height,
Color color,
Stroke stroke,
DrawListener drawListener) throws IOException {
contentStream.saveGraphicsState();
contentStream.moveTo(upperLeft.getX(), upperLeft.getY());
float x = upperLeft.getX();
float y = upperLeft.getY() - stroke.getLineWidth() / 2;
contentStream.setStrokingColor(color);
contentStream.setLineCapStyle(stroke.getCapStyle().value());
contentStream.setLineDashPattern(stroke.getDashPattern().getPattern(), stroke.getDashPattern().getPhase());
contentStream.setLineJoinStyle(stroke.getJoinStyle().value());
contentStream.setLineWidth(stroke.getLineWidth());
stroke.applyTo(contentStream);
boolean move = true;
for (Position p : list) {
contentStream.lineTo(p.getX(), p.getY());
if (move) {
contentStream.moveTo(x + p.getX(), y + p.getY());
move = false;
} else {
contentStream.lineTo(x + p.getX(), y + p.getY());
}
}
contentStream.stroke();
contentStream.restoreGraphicsState();
if (drawListener != null) {
drawListener.drawn(this, upperLeft, width, height);
}
}
@Override
public void fill(PDDocument pdDocument, PDPageContentStream contentStream, Position upperLeft,
@ -44,6 +56,5 @@ public class Path implements Shape {
@Override
public void add(PDDocument pdDocument, PDPageContentStream contentStream, Position upperLeft,
float width, float height) throws IOException {
list.add(new Position(width, height));
}
}

View file

@ -13,8 +13,6 @@ public class Rect extends AbstractShape {
@Override
public void add(PDDocument pdDocument, PDPageContentStream contentStream,
Position upperLeft, float width, float height) throws IOException {
contentStream.addRect(upperLeft.getX(), upperLeft.getY() - height,
width, height);
contentStream.addRect(upperLeft.getX(), upperLeft.getY() - height, width, height);
}
}

View file

@ -37,9 +37,11 @@ public class RoundRect extends AbstractShape {
}
@Override
public void add(PDDocument pdDocument, PDPageContentStream contentStream,
Position upperLeft, float width, float height) throws IOException {
public void add(PDDocument pdDocument,
PDPageContentStream contentStream,
Position upperLeft,
float width,
float height) throws IOException {
addRoundRect(contentStream, upperLeft, width, height, cornerRadiusX, cornerRadiusY);
}

View file

@ -2,7 +2,6 @@ package org.xbib.graphics.pdfbox.layout.shape;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import java.io.IOException;
import java.util.Arrays;
/**
* This is a container for all information needed to perform a stroke.

View file

@ -1,6 +1,7 @@
package org.xbib.graphics.pdfbox.layout.table;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.util.PdfUtil;
import java.awt.Color;
import java.util.Comparator;

View file

@ -3,6 +3,7 @@ package org.xbib.graphics.pdfbox.layout.table;
import org.xbib.graphics.pdfbox.layout.elements.Paragraph;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.font.FontDescriptor;
import org.xbib.graphics.pdfbox.layout.util.PdfUtil;
import java.awt.Color;

View file

@ -5,7 +5,7 @@ import static org.xbib.graphics.pdfbox.layout.table.HorizontalAlignment.JUSTIFY;
import static org.xbib.graphics.pdfbox.layout.table.HorizontalAlignment.RIGHT;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.table.AbstractTextCell;
import org.xbib.graphics.pdfbox.layout.table.PdfUtil;
import org.xbib.graphics.pdfbox.layout.util.PdfUtil;
import java.awt.Color;
import java.io.IOException;

View file

@ -3,7 +3,7 @@ package org.xbib.graphics.pdfbox.layout.table.render;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.util.Matrix;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.table.PdfUtil;
import org.xbib.graphics.pdfbox.layout.util.PdfUtil;
import org.xbib.graphics.pdfbox.layout.table.VerticalTextCell;
import java.awt.Color;

View file

@ -200,7 +200,7 @@ public class IndentCharacters {
}
enumerator = EnumeratorFactory.createEnumerator(enumerationType);
this.separator = separator != null ? separator : enumerator
.getDefaultSeperator();
.getDefaultSeparator();
}
@Override

View file

@ -13,5 +13,5 @@ public interface Enumerator {
/**
* @return the default separator.
*/
String getDefaultSeperator();
String getDefaultSeparator();
}

View file

@ -34,7 +34,7 @@ public class Enumerators {
}
@Override
public String getDefaultSeperator() {
public String getDefaultSeparator() {
return ".";
}
}
@ -49,8 +49,7 @@ public class Enumerators {
* c) Stet clita ...
* </pre>
*/
public static class LowerCaseAlphabeticEnumerator extends
AlphabeticEnumerator {
public static class LowerCaseAlphabeticEnumerator extends AlphabeticEnumerator {
public LowerCaseAlphabeticEnumerator() {
super();
@ -98,7 +97,7 @@ public class Enumerators {
}
@Override
public String getDefaultSeperator() {
public String getDefaultSeparator() {
return ")";
}
@ -173,7 +172,7 @@ public class Enumerators {
}
@Override
public String getDefaultSeperator() {
public String getDefaultSeparator() {
return ".";
}
@ -181,60 +180,60 @@ public class Enumerators {
if (input < 1 || input > 3999) {
return "Invalid Roman Number Value";
}
String s = "";
StringBuilder s = new StringBuilder();
while (input >= 1000) {
s += "M";
s.append("M");
input -= 1000;
}
while (input >= 900) {
s += "CM";
s.append("CM");
input -= 900;
}
while (input >= 500) {
s += "D";
s.append("D");
input -= 500;
}
while (input >= 400) {
s += "CD";
s.append("CD");
input -= 400;
}
while (input >= 100) {
s += "C";
s.append("C");
input -= 100;
}
while (input >= 90) {
s += "XC";
s.append("XC");
input -= 90;
}
while (input >= 50) {
s += "L";
s.append("L");
input -= 50;
}
while (input >= 40) {
s += "XL";
s.append("XL");
input -= 40;
}
while (input >= 10) {
s += "X";
s.append("X");
input -= 10;
}
while (input >= 9) {
s += "IX";
s.append("IX");
input -= 9;
}
while (input >= 5) {
s += "V";
s.append("V");
input -= 5;
}
while (input >= 4) {
s += "IV";
s.append("IV");
input -= 4;
}
while (input >= 1) {
s += "I";
s.append("I");
input -= 1;
}
return s;
return s.toString();
}
}

View file

@ -1,6 +1,7 @@
package org.xbib.graphics.pdfbox.layout.table;
package org.xbib.graphics.pdfbox.layout.util;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.table.CouldNotDetermineStringWidthException;
import java.io.IOException;
import java.util.ArrayList;

View file

@ -0,0 +1,2 @@
org.xbib.settings.datastructures.PropertiesSettingsLoader
org.xbib.settings.datastructures.json.JsonSettingsLoader

View file

@ -1,6 +1,7 @@
package org.xbib.graphics.pdfbox.layout.test;
import org.junit.jupiter.api.Test;
import org.xbib.graphics.pdfbox.layout.elements.ControlElement;
import org.xbib.graphics.pdfbox.layout.elements.Document;
import org.xbib.graphics.pdfbox.layout.elements.Paragraph;
import org.xbib.graphics.pdfbox.layout.elements.render.ColumnLayout;
@ -34,7 +35,7 @@ public class LineSpacingTest {
document.add(left);
document.add(left);
document.add(left);
document.add(ColumnLayout.NEWCOLUMN);
document.add(ControlElement.NEWCOLUMN);
Paragraph right = new Paragraph();
right.setLineSpacing(1.5f);
right.setMaxWidth(document.getPageWidth() / 2);

View file

@ -0,0 +1,21 @@
package org.xbib.graphics.pdfbox.layout.test.script;
import org.junit.jupiter.api.Test;
import org.xbib.graphics.pdfbox.layout.elements.Document;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.settings.Settings;
import java.io.FileOutputStream;
public class ScriptTest {
@Test
public void script() throws Exception {
Settings settings = Settings.settingsBuilder().loadFromResource("json", getClass().getResourceAsStream("script.json"))
.build();
Engine engine = new Engine();
engine.execute(settings);
Document document = engine.getState().document;
document.render().save(new FileOutputStream("build/script.pdf"));
}
}

View file

@ -17,7 +17,7 @@ import org.xbib.graphics.pdfbox.layout.table.BorderStyle;
import org.xbib.graphics.pdfbox.layout.table.Column;
import org.xbib.graphics.pdfbox.layout.table.HorizontalAlignment;
import org.xbib.graphics.pdfbox.layout.table.ImageCell;
import org.xbib.graphics.pdfbox.layout.table.PdfUtil;
import org.xbib.graphics.pdfbox.layout.util.PdfUtil;
import org.xbib.graphics.pdfbox.layout.table.Row;
import org.xbib.graphics.pdfbox.layout.table.Table;
import org.xbib.graphics.pdfbox.layout.table.TableRenderer;

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

View file

@ -0,0 +1 @@
iVBORw0KGgoAAAANSUhEUgAAAfQAAAC0AQMAAABYN0wRAAAABlBMVEUAAAD///+l2Z/dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1gsEDjEFTKgt4wAAAi5JREFUaN7t2s1ygyAQAOClHLiVHnvojI/io+mj+Sg+Qo4eGKmwgPkfdjfTNC05ZEblm+BmxQUFL/ocoPnmhX6Fy0/zzf+kN8L8fXl/Fr8Z9O/wACq1nQAs1S9Q/Mabb/6v+qOd0+O82/3C8eFYvn6X++evrno/lwNr88033/zr+Vlnv8BA99vIOSQ/nvahzs+x58G7OBynglnX+jGO78EfIHSF6FfIv2rDoZ7qHRb0wY/xJkT0odPYawvxVkX0M+RevyMj+rANXWj2BTEURD8W/4lzG6KPjWPUPjhen/uB5t/gJOpbKGkeHu07jteP85bsp+K/ON644uMsas0hqfT9mrPWhG2TvK31g8/ebgJrxYXg/TYCoe+CMzjybRt1Xu2+9zEUuL+v9DrsEniz+zgK6dRoqPR29774Ma5x0L2n+654tXvcYHnly2lU+b547fGvlHuHaVTlhys+TWaI3hz7rtb7K/4g9BgWkR8kfhJ6TF+qt0deiTzUe3XF56tY4I3EO6HPc3muT+nL81rkY+RT+rN9St+n+ZT+PG/2VdWf92sqxfRtn8rOOz6nL8O78C31ZSmL7pdUBHUXfj5dAbztO4mPNKc/w0ea05fhJ6EfA40zIhxHiH5N5SjXu1hEcT2Enpf5H8MjcyKvhd482Vuh74R+EHov80rotdBboe+F3nM9TqU133vMnguPzylVzdPO81Y0f+/9i6d7/vP35ptvvvnmX9i/8PuHzf9f/w3g1VrR1Tf4UwAAAABJRU5ErkJggg==

View file

@ -0,0 +1,33 @@
{
"document1": {
"margin": "10 10 10 10",
"image1": {
"scale": 0.25,
"value": "iVBORw0KGgoAAAANSUhEUgAAAfQAAAC0AQMAAABYN0wRAAAABlBMVEUAAAD///+l2Z/dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1gsEDjEFTKgt4wAAAi5JREFUaN7t2s1ygyAQAOClHLiVHnvojI/io+mj+Sg+Qo4eGKmwgPkfdjfTNC05ZEblm+BmxQUFL/ocoPnmhX6Fy0/zzf+kN8L8fXl/Fr8Z9O/wACq1nQAs1S9Q/Mabb/6v+qOd0+O82/3C8eFYvn6X++evrno/lwNr88033/zr+Vlnv8BA99vIOSQ/nvahzs+x58G7OBynglnX+jGO78EfIHSF6FfIv2rDoZ7qHRb0wY/xJkT0odPYawvxVkX0M+RevyMj+rANXWj2BTEURD8W/4lzG6KPjWPUPjhen/uB5t/gJOpbKGkeHu07jteP85bsp+K/ON644uMsas0hqfT9mrPWhG2TvK31g8/ebgJrxYXg/TYCoe+CMzjybRt1Xu2+9zEUuL+v9DrsEniz+zgK6dRoqPR29774Ma5x0L2n+654tXvcYHnly2lU+b547fGvlHuHaVTlhys+TWaI3hz7rtb7K/4g9BgWkR8kfhJ6TF+qt0deiTzUe3XF56tY4I3EO6HPc3muT+nL81rkY+RT+rN9St+n+ZT+PG/2VdWf92sqxfRtn8rOOz6nL8O78C31ZSmL7pdUBHUXfj5dAbztO4mPNKc/w0ea05fhJ6EfA40zIhxHiH5N5SjXu1hEcT2Enpf5H8MjcyKvhd482Vuh74R+EHov80rotdBboe+F3nM9TqU133vMnguPzylVzdPO81Y0f+/9i6d7/vP35ptvvvnmX9i/8PuHzf9f/w3g1VrR1Tf4UwAAAABJRU5ErkJggg=="
},
"barcode1": {
"symbol": "Code3Of9",
"value": "12345678"
},
"path1": {
"absolute": false,
"value": "10 10 100 10 100 20 10 20 10 10",
"color": "red",
"dash": 1
},
"paragraph1": {
"text1": {
"value": "Hello World",
"size": 24,
"font": "HELVETICA"
}
},
"paragraph2": {
"text1": {
"value": "*Hello World*",
"size": 16,
"font": "NOTOSANS"
}
},
}
}