add layout engine

This commit is contained in:
Jörg Prante 2021-12-03 16:15:43 +01:00
parent 854235cba9
commit 813cba2acb
36 changed files with 408 additions and 339 deletions

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
/.idea
.DS_Store
/.settings
/.parameters
/.classpath
/.project
/.gradle

View file

@ -15,3 +15,4 @@ junit4.version = 4.13.2
cglib.version = 3.3.0
objenesis.version = 2.6
log4j.version = 2.14.0
xbib-content.version = 4.0.0

View file

@ -1,4 +1,7 @@
dependencies {
api project(':graphics-pdfbox')
api project(':graphics-barcode')
api "org.xbib:settings-datastructures:${project.property('xbib-content.version')}"
runtimeOnly "org.xbib:settings-datastructures-json:${project.property('xbib-content.version')}"
runtimeOnly "org.xbib:settings-datastructures-yaml:${project.property('xbib-content.version')}"
}

View file

@ -11,6 +11,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 org.xbib.settings.datastructures;
requires transitive java.desktop;
requires java.logging;
}

View file

@ -34,10 +34,10 @@ public class Document implements Closeable, RenderListener {
private final List<RenderListener> renderListener = new ArrayList<>();
private PDDocument pdDocument;
private final PageFormat pageFormat;
private final PDDocument pdDocument;
/**
* Creates a Document using the {@link #DEFAULT_PAGE_FORMAT}.
*/
@ -65,7 +65,7 @@ public class Document implements Closeable, RenderListener {
float marginRight,
float marginTop,
float marginBottom, boolean memory) {
this(PageFormat.with().margins(marginLeft, marginRight, marginTop, marginBottom).build(), memory);
this(PageFormat.builder().margins(marginLeft, marginRight, marginTop, marginBottom).build(), memory);
}
/**
@ -201,7 +201,6 @@ public class Document implements Closeable, RenderListener {
public synchronized void save(OutputStream outputStream) throws IOException {
if (pdDocument != null) {
pdDocument.save(outputStream);
pdDocument = null;
}
}

View file

@ -3,16 +3,16 @@ package org.xbib.graphics.pdfbox.layout.elements;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.xbib.graphics.pdfbox.layout.elements.render.VerticalLayout;
import java.util.Locale;
/**
* Defines the size and orientation of a page. The default is A4 portrait
* without margins.
* Defines the size and orientation of a page. The default is A4 portrait without margins.
*/
public class PageFormat implements Element {
private static final int DEFAULT_USER_SPACE_UNIT_DPI = 72;
public static final int DEFAULT_USER_SPACE_UNIT_DPI = 72;
private static final float MM_TO_UNITS = 1 / (10 * 2.54f)
* DEFAULT_USER_SPACE_UNIT_DPI;
public static final float MM_TO_UNITS = 1 / (10 * 2.54f) * DEFAULT_USER_SPACE_UNIT_DPI;
public static final PDRectangle A0 = new PDRectangle(841 * MM_TO_UNITS,
1189 * MM_TO_UNITS);
@ -113,6 +113,8 @@ public class PageFormat implements Element {
this.marginBottom = marginBottom;
}
/**
* @return the orientation to use.
*/
@ -172,7 +174,7 @@ public class PageFormat implements Element {
* @return a page format builder. The default of the builder is A4 portrait
* without margins.
*/
public static PageFormatBuilder with() {
public static PageFormatBuilder builder() {
return new PageFormatBuilder();
}
@ -188,16 +190,6 @@ public class PageFormat implements Element {
protected PageFormatBuilder() {
}
/**
* Actually builds the PageFormat.
*
* @return the resulting PageFormat.
*/
public PageFormat build() {
return new PageFormat(mediaBox, orientation, rotation, marginLeft,
marginRight, marginTop, marginBottom);
}
/**
* Sets the left margin.
*
@ -362,6 +354,11 @@ public class PageFormat implements Element {
return this;
}
public PageFormatBuilder orientation(String orientation) {
this.orientation = Orientation.valueOf(orientation);
return this;
}
/**
* Sets the orientation to {@link Orientation#PORTRAIT}.
*
@ -392,6 +389,45 @@ public class PageFormat implements Element {
this.rotation = angle;
return this;
}
public PageFormatBuilder pageFormat(String format) {
switch (format.toLowerCase(Locale.ROOT)) {
case "A0" :
A0();
break;
case "A1" :
A1();
break;
case "A2" :
A2();
break;
case "A3" :
A3();
break;
case "A4" :
A4();
break;
case "A5" :
A5();
break;
case "A6" :
A6();
break;
case "LETTER" :
letter();
break;
}
return this;
}
/**
* Actually builds the PageFormat.
*
* @return the resulting PageFormat.
*/
public PageFormat build() {
return new PageFormat(mediaBox, orientation, rotation, marginLeft,
marginRight, marginTop, marginBottom);
}
}
}

View file

@ -1,21 +0,0 @@
package org.xbib.graphics.pdfbox.layout.script;
public abstract class Command<T> {
private final T value;
public Command(T value) {
this.value = value;
}
public abstract String getKey();
public T getValue() {
return value;
}
@Override
public String toString() {
return String.format("%s[value=%s]", getKey(), getValue());
}
}

View file

@ -1,18 +0,0 @@
package org.xbib.graphics.pdfbox.layout.script;
import org.xbib.graphics.pdfbox.layout.elements.Document;
import org.xbib.graphics.pdfbox.layout.elements.PageFormat;
import java.io.IOException;
public class DocumentProcessor implements Processor {
@Override
public ProcessorResult process(Iterable<Command<?>> commands, PageFormat pageFormat) throws IOException {
ProcessorResult processorResult = new DocumentProcessorResult(new Document(pageFormat));
for (Command<?> command : commands) {
processorResult.handle(command);
}
processorResult.close();
return processorResult;
}
}

View file

@ -1,33 +0,0 @@
package org.xbib.graphics.pdfbox.layout.script;
import org.xbib.graphics.pdfbox.layout.elements.Document;
import org.xbib.graphics.pdfbox.layout.script.commands.ParagraphCommand;
import java.io.IOException;
import java.io.OutputStream;
public class DocumentProcessorResult implements ProcessorResult {
private final Document document;
public DocumentProcessorResult(Document document) {
this.document = document;
}
@Override
public void handle(Command<?> command) throws IOException {
if (command instanceof ParagraphCommand) {
ParagraphCommand paragraphCommand = (ParagraphCommand) command;
document.add(paragraphCommand.getValue());
}
}
@Override
public void write(OutputStream out) throws IOException {
document.render().save(out);
}
@Override
public void close() throws IOException {
document.close();
}
}

View file

@ -0,0 +1,36 @@
package org.xbib.graphics.pdfbox.layout.script;
import org.xbib.graphics.pdfbox.layout.script.command.Command;
import org.xbib.settings.Settings;
import java.io.IOException;
import java.util.Map;
public class Engine {
private final State state;
public Engine() {
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()) {
try {
String string = entry.getKey();
Class<?> cl = classLoader.loadClass(packageName + ".command." + string.substring(0, 1).toUpperCase() + string.substring(1) + "Command");
Command command = (Command) cl.getConstructor().newInstance();
command.execute(this, state, entry.getValue());
} catch (Exception e) {
throw new IOException(e);
}
}
}
public State getState() {
return state;
}
}

View file

@ -1,9 +0,0 @@
package org.xbib.graphics.pdfbox.layout.script;
import org.xbib.graphics.pdfbox.layout.elements.PageFormat;
import java.io.IOException;
public interface Processor {
ProcessorResult process(Iterable<Command<?>> commands, PageFormat pageFormat) throws IOException;
}

View file

@ -1,13 +0,0 @@
package org.xbib.graphics.pdfbox.layout.script;
import java.io.IOException;
import java.io.OutputStream;
public interface ProcessorResult {
void handle(Command<?> command) throws IOException;
void write(OutputStream out) throws IOException;
void close() throws IOException;
}

View file

@ -0,0 +1,8 @@
package org.xbib.graphics.pdfbox.layout.script;
import org.xbib.graphics.pdfbox.layout.elements.Document;
public class State {
public Document document;
}

View file

@ -0,0 +1,11 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.settings.Settings;
import java.io.IOException;
public interface Command {
void execute(Engine engine, State state, Settings settings) throws IOException;
}

View file

@ -0,0 +1,22 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.elements.Document;
import org.xbib.graphics.pdfbox.layout.elements.PageFormat;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.settings.Settings;
import java.io.IOException;
public class DocumentCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
PageFormat pageFormat = PageFormat.builder()
.pageFormat(settings.get("format", "A4"))
.orientation(settings.get("orientiation", "PORTRAIT"))
.build();
state.document = new Document(pageFormat);
engine.execute("paragraoh", settings);
}
}

View file

@ -0,0 +1,14 @@
package org.xbib.graphics.pdfbox.layout.script.command;
import org.xbib.graphics.pdfbox.layout.script.Engine;
import org.xbib.graphics.pdfbox.layout.script.State;
import org.xbib.settings.Settings;
import java.io.IOException;
public class ParagraphCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
}
}

View file

@ -1,16 +0,0 @@
package org.xbib.graphics.pdfbox.layout.script.commands;
import org.xbib.graphics.pdfbox.layout.elements.Paragraph;
import org.xbib.graphics.pdfbox.layout.script.Command;
public class ParagraphCommand extends Command<Paragraph> {
public ParagraphCommand(Paragraph paragraph) {
super(paragraph);
}
@Override
public String getKey() {
return null;
}
}

View file

@ -0,0 +1,49 @@
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.text.DrawListener;
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<>();
}
@Override
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());
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());
for (Position p : list) {
contentStream.lineTo(p.getX(), p.getY());
}
contentStream.restoreGraphicsState();
drawListener.drawn(this, upperLeft, width, height);
}
@Override
public void fill(PDDocument pdDocument, PDPageContentStream contentStream, Position upperLeft,
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 {
list.add(new Position(width, height));
}
}

View file

@ -22,7 +22,7 @@ public abstract class AbstractCell {
private float minHeight = DEFAULT_MIN_HEIGHT;
protected Settings settings;
protected Parameters parameters;
public void setColSpan(int colSpan) {
this.colSpan = colSpan;
@ -33,91 +33,91 @@ public abstract class AbstractCell {
}
public float getPaddingBottom() {
return settings.getPaddingBottom();
return parameters.getPaddingBottom();
}
public float getPaddingTop() {
return settings.getPaddingTop();
return parameters.getPaddingTop();
}
public float getPaddingLeft() {
return settings.getPaddingLeft();
return parameters.getPaddingLeft();
}
public float getPaddingRight() {
return settings.getPaddingRight();
return parameters.getPaddingRight();
}
public float getHorizontalPadding() {
return settings.getPaddingLeft() + settings.getPaddingRight();
return parameters.getPaddingLeft() + parameters.getPaddingRight();
}
public float getVerticalPadding() {
return settings.getPaddingTop() + settings.getPaddingBottom();
return parameters.getPaddingTop() + parameters.getPaddingBottom();
}
public float getBorderWidthTop() {
return hasBorderTop() ? settings.getBorderWidthTop() : 0;
return hasBorderTop() ? parameters.getBorderWidthTop() : 0;
}
public boolean hasBorderTop() {
return settings.getBorderWidthTop() != null && settings.getBorderWidthTop() > 0;
return parameters.getBorderWidthTop() != null && parameters.getBorderWidthTop() > 0;
}
public float getBorderWidthBottom() {
return hasBorderBottom() ? settings.getBorderWidthBottom() : 0;
return hasBorderBottom() ? parameters.getBorderWidthBottom() : 0;
}
public boolean hasBorderBottom() {
return settings.getBorderWidthBottom() != null && settings.getBorderWidthBottom() > 0;
return parameters.getBorderWidthBottom() != null && parameters.getBorderWidthBottom() > 0;
}
public float getBorderWidthLeft() {
return hasBorderLeft() ? settings.getBorderWidthLeft() : 0;
return hasBorderLeft() ? parameters.getBorderWidthLeft() : 0;
}
public boolean hasBorderLeft() {
return settings.getBorderWidthLeft() != null && settings.getBorderWidthLeft() > 0;
return parameters.getBorderWidthLeft() != null && parameters.getBorderWidthLeft() > 0;
}
public float getBorderWidthRight() {
return hasBorderRight() ? settings.getBorderWidthRight() : 0;
return hasBorderRight() ? parameters.getBorderWidthRight() : 0;
}
public boolean hasBorderRight() {
return settings.getBorderWidthRight() != null && settings.getBorderWidthRight() > 0;
return parameters.getBorderWidthRight() != null && parameters.getBorderWidthRight() > 0;
}
public BorderStyleInterface getBorderStyleTop() {
return settings.getBorderStyleTop();
return parameters.getBorderStyleTop();
}
public BorderStyleInterface getBorderStyleBottom() {
return settings.getBorderStyleBottom();
return parameters.getBorderStyleBottom();
}
public BorderStyleInterface getBorderStyleLeft() {
return settings.getBorderStyleLeft();
return parameters.getBorderStyleLeft();
}
public BorderStyleInterface getBorderStyleRight() {
return settings.getBorderStyleRight();
return parameters.getBorderStyleRight();
}
public boolean hasBackgroundColor() {
return settings.getBackgroundColor() != null;
return parameters.getBackgroundColor() != null;
}
public Color getBackgroundColor() {
return settings.getBackgroundColor();
return parameters.getBackgroundColor();
}
public Color getBorderColor() {
return settings.getBorderColor();
return parameters.getBorderColor();
}
public boolean isWordBreak() {
return settings.isWordBreak();
return parameters.isWordBreak();
}
public Column getColumn() {
@ -148,8 +148,8 @@ public abstract class AbstractCell {
return row;
}
public Settings getSettings() {
return settings;
public Parameters getParameters() {
return parameters;
}
public void setColumn(Column column) {
@ -168,8 +168,8 @@ public abstract class AbstractCell {
this.row = row;
}
public void setSettings(Settings settings) {
this.settings = settings;
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
public void setWidth(float width) {
@ -194,7 +194,6 @@ public abstract class AbstractCell {
result += currentRow.getNext().getHeight();
currentRow = currentRow.getNext();
}
return result;
}
@ -205,10 +204,10 @@ public abstract class AbstractCell {
}
public boolean isHorizontallyAligned(HorizontalAlignment alignment) {
return getSettings().getHorizontalAlignment() == alignment;
return getParameters().getHorizontalAlignment() == alignment;
}
public boolean isVerticallyAligned(VerticalAlignment alignment) {
return getSettings().getVerticalAlignment() == alignment;
return getParameters().getVerticalAlignment() == alignment;
}
}

View file

@ -11,15 +11,15 @@ public abstract class AbstractTextCell extends AbstractCell {
protected float lineSpacing = 1f;
public Font getFont() {
return settings.getFont();
return parameters.getFont();
}
public Integer getFontSize() {
return settings.getFontSize();
return parameters.getFontSize();
}
public Color getTextColor() {
return settings.getTextColor();
return parameters.getTextColor();
}
private Float textHeight;
@ -40,7 +40,7 @@ public abstract class AbstractTextCell extends AbstractCell {
return this.textHeight;
}
this.textHeight = PdfUtil.getFontHeight(getFont(), getFontSize());
if (settings.isWordBreak()) {
if (parameters.isWordBreak()) {
final int size = PdfUtil.getOptimalTextBreakLines(getText(), getFont(), getFontSize(), getMaxWidth()).size();
final float heightOfTextLines = size * this.textHeight;
final float heightOfLineSpacing = (size - 1) * this.textHeight * getLineSpacing();
@ -52,7 +52,7 @@ public abstract class AbstractTextCell extends AbstractCell {
public float getWidthOfText() {
assertIsRendered();
final float notBrokenTextWidth = PdfUtil.getStringWidth(getText(), getFont(), getFontSize());
if (settings.isWordBreak()) {
if (parameters.isWordBreak()) {
final float maxWidth = getMaxWidthOfText() - getHorizontalPadding();
List<String> textLines = PdfUtil.getOptimalTextBreakLines(getText(), getFont(), getFontSize(), maxWidth);
return textLines.stream()

View file

@ -72,11 +72,11 @@ public class Hyperlink implements ParagraphProcessor {
}
@Override
public void process(Paragraph paragraph, Settings settings) {
public void process(Paragraph paragraph, Parameters parameters) {
Annotations.HyperlinkAnnotation hyperlink =
new Annotations.HyperlinkAnnotation(getUrl(), Annotations.HyperlinkAnnotation.LinkStyle.ul);
FontDescriptor fontDescriptor = new FontDescriptor(getFont() != null ? getFont() : settings.getFont(),
getFontSize() != null ? getFontSize() : settings.getFontSize());
FontDescriptor fontDescriptor = new FontDescriptor(getFont() != null ? getFont() : parameters.getFont(),
getFontSize() != null ? getFontSize() : parameters.getFontSize());
paragraph.add(new AnnotatedStyledText(getText(), fontDescriptor,
getColor(), getBaselineOffset(), 0, 0, Collections.singleton(hyperlink)));
}

View file

@ -50,8 +50,8 @@ public class Markup implements ParagraphProcessor {
}
@Override
public void process(Paragraph paragraph, Settings settings) throws IOException {
float fontSize = getFontSize() != null ? getFontSize() : settings.getFontSize();
public void process(Paragraph paragraph, Parameters parameters) throws IOException {
float fontSize = getFontSize() != null ? getFontSize() : parameters.getFontSize();
paragraph.addMarkup(getMarkup(), fontSize, FONT_MAP.get(getFont()));
}

View file

@ -16,7 +16,7 @@ public class NewLine implements ParagraphProcessor {
}
@Override
public void process(Paragraph paragraph, Settings settings) {
public void process(Paragraph paragraph, Parameters parameters) {
paragraph.add(new org.xbib.graphics.pdfbox.layout.text.NewLine(new FontDescriptor(font, fontSize)));
}

View file

@ -36,7 +36,7 @@ public class ParagraphCell extends AbstractCell {
}
for (ParagraphProcessor p : getParagraph().getProcessables()) {
try {
p.process(getParagraph().getWrappedParagraph(), getSettings());
p.process(getParagraph().getWrappedParagraph(), getParameters());
} catch (IOException exception) {
throw new UncheckedIOException(exception);
}

View file

@ -5,6 +5,6 @@ import java.io.IOException;
public interface ParagraphProcessor {
void process(Paragraph paragraph, Settings settings) throws IOException;
void process(Paragraph paragraph, Parameters parameters) throws IOException;
}

View file

@ -3,7 +3,7 @@ package org.xbib.graphics.pdfbox.layout.table;
import org.xbib.graphics.pdfbox.layout.font.Font;
import java.awt.Color;
public class Settings {
public class Parameters {
private Font font;
@ -55,14 +55,14 @@ public class Settings {
this.wordBreak = wordBreak;
}
public void fillingMergeBy(Settings settings) {
fillingMergeFontSettings(settings);
fillingMergePaddingSettings(settings);
fillingMergeBorderWidthSettings(settings);
fillingMergeBorderStyleSettings(settings);
fillingMergeColorSettings(settings);
fillingMergeAlignmentSettings(settings);
fillingMergeWordBreakSetting(settings);
public void fillingMergeBy(Parameters parameters) {
fillingMergeFontSettings(parameters);
fillingMergePaddingSettings(parameters);
fillingMergeBorderWidthSettings(parameters);
fillingMergeBorderStyleSettings(parameters);
fillingMergeColorSettings(parameters);
fillingMergeAlignmentSettings(parameters);
fillingMergeWordBreakSetting(parameters);
}
public Boolean getWordBreak() {
@ -225,97 +225,97 @@ public class Settings {
this.wordBreak = wordBreak;
}
private void fillingMergeWordBreakSetting(Settings settings) {
private void fillingMergeWordBreakSetting(Parameters parameters) {
// Note that we use the boxed Boolean only here internally!
if (wordBreak == null && settings.wordBreak != null) {
wordBreak = settings.getWordBreak();
if (wordBreak == null && parameters.wordBreak != null) {
wordBreak = parameters.getWordBreak();
}
}
private void fillingMergePaddingSettings(Settings settings) {
if (getPaddingBottom() == null && settings.getPaddingBottom() != null) {
paddingBottom = settings.getPaddingBottom();
private void fillingMergePaddingSettings(Parameters parameters) {
if (getPaddingBottom() == null && parameters.getPaddingBottom() != null) {
paddingBottom = parameters.getPaddingBottom();
}
if (getPaddingTop() == null && settings.getPaddingTop() != null) {
paddingTop = settings.getPaddingTop();
if (getPaddingTop() == null && parameters.getPaddingTop() != null) {
paddingTop = parameters.getPaddingTop();
}
if (getPaddingLeft() == null && settings.getPaddingLeft() != null) {
paddingLeft = settings.getPaddingLeft();
if (getPaddingLeft() == null && parameters.getPaddingLeft() != null) {
paddingLeft = parameters.getPaddingLeft();
}
if (getPaddingRight() == null && settings.getPaddingRight() != null) {
paddingRight = settings.getPaddingRight();
if (getPaddingRight() == null && parameters.getPaddingRight() != null) {
paddingRight = parameters.getPaddingRight();
}
}
private void fillingMergeBorderWidthSettings(Settings settings) {
if (getBorderWidthBottom() == null && settings.getBorderWidthBottom() != null) {
borderWidthBottom = settings.getBorderWidthBottom();
private void fillingMergeBorderWidthSettings(Parameters parameters) {
if (getBorderWidthBottom() == null && parameters.getBorderWidthBottom() != null) {
borderWidthBottom = parameters.getBorderWidthBottom();
}
if (getBorderWidthTop() == null && settings.getBorderWidthTop() != null) {
borderWidthTop = settings.getBorderWidthTop();
if (getBorderWidthTop() == null && parameters.getBorderWidthTop() != null) {
borderWidthTop = parameters.getBorderWidthTop();
}
if (getBorderWidthLeft() == null && settings.getBorderWidthLeft() != null) {
borderWidthLeft = settings.getBorderWidthLeft();
if (getBorderWidthLeft() == null && parameters.getBorderWidthLeft() != null) {
borderWidthLeft = parameters.getBorderWidthLeft();
}
if (getBorderWidthRight() == null && settings.getBorderWidthRight() != null) {
borderWidthRight = settings.getBorderWidthRight();
if (getBorderWidthRight() == null && parameters.getBorderWidthRight() != null) {
borderWidthRight = parameters.getBorderWidthRight();
}
}
private void fillingMergeBorderStyleSettings(Settings settings) {
if (getBorderStyleBottom() == null && settings.getBorderStyleBottom() != null) {
borderStyleBottom = settings.getBorderStyleBottom();
private void fillingMergeBorderStyleSettings(Parameters parameters) {
if (getBorderStyleBottom() == null && parameters.getBorderStyleBottom() != null) {
borderStyleBottom = parameters.getBorderStyleBottom();
}
if (getBorderStyleTop() == null && settings.getBorderStyleTop() != null) {
borderStyleTop = settings.getBorderStyleTop();
if (getBorderStyleTop() == null && parameters.getBorderStyleTop() != null) {
borderStyleTop = parameters.getBorderStyleTop();
}
if (getBorderStyleLeft() == null && settings.getBorderStyleLeft() != null) {
borderStyleLeft = settings.getBorderStyleLeft();
if (getBorderStyleLeft() == null && parameters.getBorderStyleLeft() != null) {
borderStyleLeft = parameters.getBorderStyleLeft();
}
if (getBorderStyleRight() == null && settings.getBorderStyleRight() != null) {
borderStyleRight = settings.getBorderStyleRight();
if (getBorderStyleRight() == null && parameters.getBorderStyleRight() != null) {
borderStyleRight = parameters.getBorderStyleRight();
}
}
private void fillingMergeColorSettings(Settings settings) {
if (getTextColor() == null && settings.getTextColor() != null) {
textColor = settings.getTextColor();
private void fillingMergeColorSettings(Parameters parameters) {
if (getTextColor() == null && parameters.getTextColor() != null) {
textColor = parameters.getTextColor();
}
if (getBackgroundColor() == null && settings.getBackgroundColor() != null) {
backgroundColor = settings.getBackgroundColor();
if (getBackgroundColor() == null && parameters.getBackgroundColor() != null) {
backgroundColor = parameters.getBackgroundColor();
}
if (getBorderColor() == null && settings.getBorderColor() != null) {
borderColor = settings.getBorderColor();
if (getBorderColor() == null && parameters.getBorderColor() != null) {
borderColor = parameters.getBorderColor();
}
}
private void fillingMergeAlignmentSettings(Settings settings) {
if (getHorizontalAlignment() == null && settings.getHorizontalAlignment() != null) {
horizontalAlignment = settings.getHorizontalAlignment();
private void fillingMergeAlignmentSettings(Parameters parameters) {
if (getHorizontalAlignment() == null && parameters.getHorizontalAlignment() != null) {
horizontalAlignment = parameters.getHorizontalAlignment();
}
if (getVerticalAlignment() == null && settings.getVerticalAlignment() != null) {
verticalAlignment = settings.getVerticalAlignment();
if (getVerticalAlignment() == null && parameters.getVerticalAlignment() != null) {
verticalAlignment = parameters.getVerticalAlignment();
}
}
private void fillingMergeFontSettings(Settings settings) {
if (getFont() == null && settings.getFont() != null) {
font = settings.getFont();
private void fillingMergeFontSettings(Parameters parameters) {
if (getFont() == null && parameters.getFont() != null) {
font = parameters.getFont();
}
if (getFontSize() == null && settings.getFontSize() != null) {
fontSize = settings.getFontSize();
if (getFontSize() == null && parameters.getFontSize() != null) {
fontSize = parameters.getFontSize();
}
}
}

View file

@ -16,7 +16,7 @@ public class Row {
private List<AbstractCell> cells;
private Settings settings;
private Parameters parameters;
private Float height;
@ -27,12 +27,12 @@ public class Row {
this.cells = cells;
}
public void setSettings(Settings settings) {
this.settings = settings;
public void setSettings(Parameters parameters) {
this.parameters = parameters;
}
public Settings getSettings() {
return settings;
public Parameters getSettings() {
return parameters;
}
public Row getNext() {
@ -96,7 +96,7 @@ public class Row {
private final List<AbstractCell> cells = new ArrayList<>();
private final Settings settings = new Settings();
private final Parameters parameters = new Parameters();
private Builder() {
}
@ -107,72 +107,72 @@ public class Row {
}
public Builder font(Font font) {
settings.setFont(font);
parameters.setFont(font);
return this;
}
public Builder fontSize(final Integer fontSize) {
settings.setFontSize(fontSize);
parameters.setFontSize(fontSize);
return this;
}
public Builder textColor(final Color textColor) {
settings.setTextColor(textColor);
parameters.setTextColor(textColor);
return this;
}
public Builder backgroundColor(final Color backgroundColor) {
settings.setBackgroundColor(backgroundColor);
parameters.setBackgroundColor(backgroundColor);
return this;
}
public Builder padding(final float padding) {
settings.setPaddingTop(padding);
settings.setPaddingBottom(padding);
settings.setPaddingLeft(padding);
settings.setPaddingRight(padding);
parameters.setPaddingTop(padding);
parameters.setPaddingBottom(padding);
parameters.setPaddingLeft(padding);
parameters.setPaddingRight(padding);
return this;
}
public Builder borderWidth(final float borderWidth) {
settings.setBorderWidthTop(borderWidth);
settings.setBorderWidthBottom(borderWidth);
settings.setBorderWidthLeft(borderWidth);
settings.setBorderWidthRight(borderWidth);
parameters.setBorderWidthTop(borderWidth);
parameters.setBorderWidthBottom(borderWidth);
parameters.setBorderWidthLeft(borderWidth);
parameters.setBorderWidthRight(borderWidth);
return this;
}
public Builder borderStyle(final BorderStyleInterface borderStyle) {
settings.setBorderStyleTop(borderStyle);
settings.setBorderStyleBottom(borderStyle);
settings.setBorderStyleLeft(borderStyle);
settings.setBorderStyleRight(borderStyle);
parameters.setBorderStyleTop(borderStyle);
parameters.setBorderStyleBottom(borderStyle);
parameters.setBorderStyleLeft(borderStyle);
parameters.setBorderStyleRight(borderStyle);
return this;
}
public Builder borderColor(final Color borderColor) {
settings.setBorderColor(borderColor);
parameters.setBorderColor(borderColor);
return this;
}
public Builder horizontalAlignment(HorizontalAlignment alignment) {
settings.setHorizontalAlignment(alignment);
parameters.setHorizontalAlignment(alignment);
return this;
}
public Builder verticalAlignment(VerticalAlignment alignment) {
settings.setVerticalAlignment(alignment);
parameters.setVerticalAlignment(alignment);
return this;
}
public Builder wordBreak(Boolean wordBreak) {
settings.setWordBreak(wordBreak);
parameters.setWordBreak(wordBreak);
return this;
}
public Row build() {
final Row row = new Row(cells);
row.setSettings(settings);
row.setSettings(parameters);
//row.setHeight(height);
return row;
}

View file

@ -49,10 +49,10 @@ public class StyledText implements ParagraphProcessor {
}
@Override
public void process(Paragraph paragraph, Settings settings) {
final float actualFontSize = getFontSize() != null ? getFontSize() : settings.getFontSize();
final Font actualFont = getFont() != null ? getFont() : settings.getFont();
final Color actualColor = getColor() != null ? getColor() : settings.getTextColor();
public void process(Paragraph paragraph, Parameters parameters) {
final float actualFontSize = getFontSize() != null ? getFontSize() : parameters.getFontSize();
final Font actualFont = getFont() != null ? getFont() : parameters.getFont();
final Color actualColor = getColor() != null ? getColor() : parameters.getTextColor();
// TODO this is a complete mess to handle new lines!!!
String[] lines = getText().split(PdfUtil.NEW_LINE_REGEX);
for (int i = 0; i < lines.length; i++) {

View file

@ -36,7 +36,7 @@ public class Table {
private final Set<Point> rowSpanCells;
private Settings settings;
private Parameters parameters;
private int numberOfColumns;
@ -48,12 +48,12 @@ public class Table {
this.rowSpanCells = rowSpanCells;
}
public void setSettings(Settings settings) {
this.settings = settings;
public void setSettings(Parameters parameters) {
this.parameters = parameters;
}
public Settings getSettings() {
return settings;
public Parameters getSettings() {
return parameters;
}
public void setWidth(float width) {
@ -130,7 +130,7 @@ public class Table {
public static class Builder {
private final Settings settings = new Settings();
private final Parameters parameters = new Parameters();
private final List<Row> rows = new ArrayList<>();
@ -143,19 +143,19 @@ public class Table {
private float width;
private Builder() {
settings.setFont(DEFAULT_FONT);
settings.setFontSize(DEFAULT_FONT_SIZE);
settings.setTextColor(DEFAULT_TEXT_COLOR);
settings.setBorderColor(DEFAULT_BORDER_COLOR);
settings.setBorderStyleTop(DEFAULT_BORDER_STYLE);
settings.setBorderStyleBottom(DEFAULT_BORDER_STYLE);
settings.setBorderStyleLeft(DEFAULT_BORDER_STYLE);
settings.setBorderStyleRight(DEFAULT_BORDER_STYLE);
settings.setPaddingTop(DEFAULT_PADDING);
settings.setPaddingBottom(DEFAULT_PADDING);
settings.setPaddingLeft(DEFAULT_PADDING);
settings.setPaddingRight(DEFAULT_PADDING);
settings.setWordBreak(true);
parameters.setFont(DEFAULT_FONT);
parameters.setFontSize(DEFAULT_FONT_SIZE);
parameters.setTextColor(DEFAULT_TEXT_COLOR);
parameters.setBorderColor(DEFAULT_BORDER_COLOR);
parameters.setBorderStyleTop(DEFAULT_BORDER_STYLE);
parameters.setBorderStyleBottom(DEFAULT_BORDER_STYLE);
parameters.setBorderStyleLeft(DEFAULT_BORDER_STYLE);
parameters.setBorderStyleRight(DEFAULT_BORDER_STYLE);
parameters.setPaddingTop(DEFAULT_PADDING);
parameters.setPaddingBottom(DEFAULT_PADDING);
parameters.setPaddingLeft(DEFAULT_PADDING);
parameters.setPaddingRight(DEFAULT_PADDING);
parameters.setWordBreak(true);
}
public Builder addRow(final Row row) {
@ -225,66 +225,66 @@ public class Table {
}
public Builder font(Font font) {
settings.setFont(font);
parameters.setFont(font);
return this;
}
public Builder fontSize(final Integer fontSize) {
settings.setFontSize(fontSize);
parameters.setFontSize(fontSize);
return this;
}
public Builder textColor(final Color textColor) {
settings.setTextColor(textColor);
parameters.setTextColor(textColor);
return this;
}
public Builder backgroundColor(final Color backgroundColor) {
settings.setBackgroundColor(backgroundColor);
parameters.setBackgroundColor(backgroundColor);
return this;
}
public Builder borderWidth(final float borderWidth) {
settings.setBorderWidthTop(borderWidth);
settings.setBorderWidthBottom(borderWidth);
settings.setBorderWidthLeft(borderWidth);
settings.setBorderWidthRight(borderWidth);
parameters.setBorderWidthTop(borderWidth);
parameters.setBorderWidthBottom(borderWidth);
parameters.setBorderWidthLeft(borderWidth);
parameters.setBorderWidthRight(borderWidth);
return this;
}
public Builder borderStyle(final BorderStyleInterface borderStyle) {
settings.setBorderStyleTop(borderStyle);
settings.setBorderStyleBottom(borderStyle);
settings.setBorderStyleLeft(borderStyle);
settings.setBorderStyleRight(borderStyle);
parameters.setBorderStyleTop(borderStyle);
parameters.setBorderStyleBottom(borderStyle);
parameters.setBorderStyleLeft(borderStyle);
parameters.setBorderStyleRight(borderStyle);
return this;
}
public Builder padding(final float padding) {
settings.setPaddingTop(padding);
settings.setPaddingBottom(padding);
settings.setPaddingLeft(padding);
settings.setPaddingRight(padding);
parameters.setPaddingTop(padding);
parameters.setPaddingBottom(padding);
parameters.setPaddingLeft(padding);
parameters.setPaddingRight(padding);
return this;
}
public Builder borderColor(final Color borderColor) {
settings.setBorderColor(borderColor);
parameters.setBorderColor(borderColor);
return this;
}
public Builder horizontalAlignment(HorizontalAlignment alignment) {
settings.setHorizontalAlignment(alignment);
parameters.setHorizontalAlignment(alignment);
return this;
}
public Builder verticalAlignment(VerticalAlignment alignment) {
settings.setVerticalAlignment(alignment);
parameters.setVerticalAlignment(alignment);
return this;
}
public Builder wordBreak(Boolean wordBreak) {
settings.setWordBreak(wordBreak);
parameters.setWordBreak(wordBreak);
return this;
}
@ -294,7 +294,7 @@ public class Table {
"This could be due to row or col spanning not being correct");
}
Table table = new Table(rows, columns, rowSpanCells);
table.setSettings(settings);
table.setSettings(parameters);
table.setWidth(width);
table.setNumberOfColumns(numberOfColumns);
setupConnectionsBetweenElementsFor(table);
@ -311,7 +311,7 @@ public class Table {
}
int columnIndex = 0;
for (AbstractCell cell : row.getCells()) {
cell.getSettings().fillingMergeBy(row.getSettings());
cell.getParameters().fillingMergeBy(row.getSettings());
cell.setRow(row);
while (table.isRowSpanAt(rowIndex, columnIndex)) {
columnIndex++;

View file

@ -28,7 +28,7 @@ public class TextCell extends AbstractTextCell {
public static class Builder {
private final Settings settings;
private final Parameters parameters;
private String text;
@ -37,7 +37,7 @@ public class TextCell extends AbstractTextCell {
private int rowSpan;
private Builder() {
settings = new Settings();
parameters = new Parameters();
}
public Builder text(String text) {
@ -46,65 +46,65 @@ public class TextCell extends AbstractTextCell {
}
public Builder font(Font font) {
settings.setFont(font);
parameters.setFont(font);
return this;
}
public Builder fontSize(Integer fontSize) {
settings.setFontSize(fontSize);
parameters.setFontSize(fontSize);
return this;
}
public Builder textColor(Color textColor) {
settings.setTextColor(textColor);
parameters.setTextColor(textColor);
return this;
}
public Builder borderWidth(float borderWidth) {
settings.setBorderWidthTop(borderWidth);
settings.setBorderWidthBottom(borderWidth);
settings.setBorderWidthLeft(borderWidth);
settings.setBorderWidthRight(borderWidth);
parameters.setBorderWidthTop(borderWidth);
parameters.setBorderWidthBottom(borderWidth);
parameters.setBorderWidthLeft(borderWidth);
parameters.setBorderWidthRight(borderWidth);
return this;
}
public Builder borderWidthTop(final float borderWidth) {
settings.setBorderWidthTop(borderWidth);
parameters.setBorderWidthTop(borderWidth);
return this;
}
public Builder borderWidthBottom(final float borderWidth) {
settings.setBorderWidthBottom(borderWidth);
parameters.setBorderWidthBottom(borderWidth);
return this;
}
public Builder borderWidthLeft(final float borderWidth) {
settings.setBorderWidthLeft(borderWidth);
parameters.setBorderWidthLeft(borderWidth);
return this;
}
public Builder borderWidthRight(final float borderWidth) {
settings.setBorderWidthRight(borderWidth);
parameters.setBorderWidthRight(borderWidth);
return this;
}
public Builder borderStyleTop(final BorderStyleInterface style) {
settings.setBorderStyleTop(style);
parameters.setBorderStyleTop(style);
return this;
}
public Builder borderStyleBottom(final BorderStyleInterface style) {
settings.setBorderStyleBottom(style);
parameters.setBorderStyleBottom(style);
return this;
}
public Builder borderStyleLeft(final BorderStyleInterface style) {
settings.setBorderStyleLeft(style);
parameters.setBorderStyleLeft(style);
return this;
}
public Builder borderStyleRight(final BorderStyleInterface style) {
settings.setBorderStyleRight(style);
parameters.setBorderStyleRight(style);
return this;
}
@ -123,47 +123,47 @@ public class TextCell extends AbstractTextCell {
}
public Builder paddingTop(final float padding) {
settings.setPaddingTop(padding);
parameters.setPaddingTop(padding);
return this;
}
public Builder paddingBottom(final float padding) {
settings.setPaddingBottom(padding);
parameters.setPaddingBottom(padding);
return this;
}
public Builder paddingLeft(final float padding) {
settings.setPaddingLeft(padding);
parameters.setPaddingLeft(padding);
return this;
}
public Builder paddingRight(final float padding) {
settings.setPaddingRight(padding);
parameters.setPaddingRight(padding);
return this;
}
public Builder horizontalAlignment(final HorizontalAlignment alignment) {
settings.setHorizontalAlignment(alignment);
parameters.setHorizontalAlignment(alignment);
return this;
}
public Builder verticalAlignment(final VerticalAlignment alignment) {
settings.setVerticalAlignment(alignment);
parameters.setVerticalAlignment(alignment);
return this;
}
public Builder backgroundColor(final Color backgroundColor) {
settings.setBackgroundColor(backgroundColor);
parameters.setBackgroundColor(backgroundColor);
return this;
}
public Builder borderColor(final Color borderColor) {
settings.setBorderColor(borderColor);
parameters.setBorderColor(borderColor);
return this;
}
public Builder wordBreak(final Boolean wordBreak) {
settings.setWordBreak(wordBreak);
parameters.setWordBreak(wordBreak);
return this;
}
@ -179,7 +179,7 @@ public class TextCell extends AbstractTextCell {
public TextCell build() {
TextCell cell = new TextCell();
cell.setSettings(settings);
cell.setParameters(parameters);
cell.setText(text);
if (colSpan > 0) {
cell.setColSpan(colSpan);

View file

@ -21,9 +21,9 @@ public class ImageCellRenderer extends AbstractCellRenderer<ImageCell> {
final Point2D.Float size = cell.getFitSize();
final Point2D.Float drawAt = new Point2D.Float();
float xOffset = moveX + cell.getPaddingLeft();
if (cell.getSettings().getHorizontalAlignment() == HorizontalAlignment.RIGHT) {
if (cell.getParameters().getHorizontalAlignment() == HorizontalAlignment.RIGHT) {
xOffset = moveX + (cell.getWidth() - (size.x + cell.getPaddingRight()));
} else if (cell.getSettings().getHorizontalAlignment() == HorizontalAlignment.CENTER) {
} else if (cell.getParameters().getHorizontalAlignment() == HorizontalAlignment.CENTER) {
final float diff = (cell.getWidth() - size.x) / 2;
xOffset = moveX + diff;
}

View file

@ -42,7 +42,7 @@ public class ParagraphCellRenderer extends AbstractCellRenderer<ParagraphCell> {
float y = renderContext.getStartingPoint().y + getAdaptionForVerticalAlignment();
paragraph.drawText(renderContext.getContentStream(),
new Position(x, y),
ALIGNMENT_MAP.getOrDefault(cell.getSettings().getHorizontalAlignment(), Alignment.LEFT),
ALIGNMENT_MAP.getOrDefault(cell.getParameters().getHorizontalAlignment(), Alignment.LEFT),
annotationDrawListener
);
annotationDrawListener.afterPage(null);

View file

@ -188,7 +188,7 @@ public class CustomAnnotationTest {
// register our custom highlight annotation processor
AnnotationProcessorFactory.register(HighlightAnnotationProcessor.class);
Document document = new Document(PageFormat.with().A4()
Document document = new Document(PageFormat.builder().A4()
.margins(40, 60, 40, 60).portrait().build());
Paragraph paragraph = new Paragraph();

View file

@ -46,8 +46,8 @@ public class Landscape {
Paragraph titleA5 = new Paragraph();
titleA5.addMarkup("*Format A5 in Landscape*", 20, BaseFont.TIMES);
PageFormat a5_landscape = PageFormat.with().A5().landscape().margins(10, 50, 0, 30).build();
PageFormat a4_portrait = PageFormat.with().margins(40, 50, 40, 60).build();
PageFormat a5_landscape = PageFormat.builder().A5().landscape().margins(10, 50, 0, 30).build();
PageFormat a4_portrait = PageFormat.builder().margins(40, 50, 40, 60).build();
Document document = new Document(a4_portrait);
document.add(titleA4, VerticalLayoutHint.CENTER);

View file

@ -46,8 +46,8 @@ public class RotationTest {
Paragraph titleA5 = new Paragraph();
titleA5.addMarkup("*Format A4 Landscape rotated by -90 degrees*", 20, BaseFont.TIMES);
PageFormat a4_landscape = PageFormat.with().margins(40, 50, 40, 60).landscape().build();
PageFormat a4_landscape_rotated = PageFormat.with().margins(40, 50, 40, 60).landscape().rotation(-90).build();
PageFormat a4_landscape = PageFormat.builder().margins(40, 50, 40, 60).landscape().build();
PageFormat a4_landscape_rotated = PageFormat.builder().margins(40, 50, 40, 60).landscape().rotation(-90).build();
Document document = new Document(a4_landscape);

View file

@ -9,7 +9,7 @@ without fee is hereby granted.
These 15 images are part of the much larger PngSuite test-set of
images, available for developers of PNG supporting software. The
complete set, available at http:/www.schaik.com/pngsuite/, contains
a variety of images to test interlacing, gamma settings, ancillary
a variety of images to test interlacing, gamma parameters, ancillary
chunks, etc.
The images in this directory represent the basic PNG color-types: