fix OOB exception, add indent command, plus cosmetic changes

This commit is contained in:
Jörg Prante 2022-02-09 17:54:18 +01:00
parent c1f5ebd409
commit e3386df689
6 changed files with 39 additions and 18 deletions

View file

@ -6,6 +6,7 @@ import org.xbib.graphics.pdfbox.layout.element.render.Transform;
import org.xbib.graphics.pdfbox.layout.text.Alignment;
import org.xbib.graphics.pdfbox.layout.text.DrawListener;
import org.xbib.graphics.pdfbox.layout.position.Position;
import org.xbib.graphics.pdfbox.layout.text.Indent;
import org.xbib.graphics.pdfbox.layout.text.TextFlow;
import org.xbib.graphics.pdfbox.layout.text.TextSequenceUtil;
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;

View file

@ -0,0 +1,25 @@
package org.xbib.graphics.pdfbox.layout.element.scripting.command;
import java.io.IOException;
import org.xbib.graphics.pdfbox.layout.element.Element;
import org.xbib.graphics.pdfbox.layout.element.Paragraph;
import org.xbib.graphics.pdfbox.layout.element.scripting.Engine;
import org.xbib.graphics.pdfbox.layout.element.scripting.State;
import org.xbib.graphics.pdfbox.layout.text.Indent;
import org.xbib.graphics.pdfbox.layout.text.SpaceUnit;
import org.xbib.settings.Settings;
import static org.xbib.graphics.pdfbox.layout.util.PdfUtil.mmToPt;
public class IndentCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) throws IOException {
float value = settings.getAsFloat("value", 0.0f);
Element element = state.getElements().peek();
if (element instanceof Paragraph) {
Paragraph paragraph = (Paragraph) element;
paragraph.add(new Indent(mmToPt(value), SpaceUnit.pt));
}
}
}

View file

@ -106,9 +106,7 @@ public class Row {
if (element instanceof Cell) {
cells.add((Cell) element);
} else if (element instanceof HorizontalRuler) {
Cell cell = DrawableCell.builder()
.drawable((Drawable) element)
.build();
Cell cell = DrawableCell.builder().drawable((Drawable) element).build();
cells.add(cell);
}
return this;

View file

@ -308,10 +308,12 @@ public class Table {
while (table.isRowSpanAt(rowIndex, columnIndex)) {
columnIndex++;
}
Column column = table.getColumns().get(columnIndex);
cell.setColumn(column);
cell.setColumn(table.getColumns().get(columnIndex));
cell.setWidth(getAvailableCellWidthRespectingSpan(columnIndex, cell.getColSpan()));
columnIndex += cell.getColSpan();
if (columnIndex > table.getNumberOfColumns() - 1) {
columnIndex = table.getNumberOfColumns();
}
}
}
for (int i = 0; i < table.getColumns().size(); i++) {

View file

@ -1,6 +1,5 @@
package org.xbib.graphics.pdfbox.layout.text;
import org.xbib.graphics.pdfbox.layout.font.BaseFont;
import org.xbib.graphics.pdfbox.layout.font.FontDescriptor;
import java.awt.Color;

View file

@ -42,17 +42,18 @@ public class IndentCharacters {
try {
this.level = level == null ? 0 : level.length() + 1;
} catch (NumberFormatException e) {
//
}
try {
this.indentUnit = indentUnit == null ? SpaceUnit.em : SpaceUnit
.valueOf(indentUnit);
this.indentUnit = indentUnit == null ? SpaceUnit.em : SpaceUnit.valueOf(indentUnit);
} catch (NumberFormatException e) {
//
}
float defaultIndent = this.indentUnit == SpaceUnit.em ? 4 : 10;
try {
this.indentWidth = indentWidth == null ? defaultIndent
: Integer.parseInt(indentWidth);
this.indentWidth = indentWidth == null ? defaultIndent : Integer.parseInt(indentWidth);
} catch (NumberFormatException e) {
//
}
}
@ -256,8 +257,7 @@ public class IndentCharacters {
}
private static class IndentCharacterFactory implements
ControlCharacters.ControlCharacterFactory {
private static class IndentCharacterFactory implements ControlCharacters.ControlCharacterFactory {
private final static Pattern PATTERN = Pattern
.compile("^-(!)|^([ ]*)-(-)(\\{(\\d*)(em|pt)?\\})?|^([ ]*)-(\\+)(\\{(.+)?:(\\d*)(em|pt)?\\})?|^([ ]*)-(#)(\\{((?!:).)?(.+)?:((\\d*))((em|pt))?\\})?");
@ -272,23 +272,19 @@ public class IndentCharacters {
if ("!".equals(matcher.group(1))) {
return UNINDENT_CHARACTER;
}
if ("-".equals(matcher.group(3))) {
return new IndentCharacter(matcher.group(2), matcher.group(5),
matcher.group(6));
}
if ("+".equals(matcher.group(8))) {
return new ListCharacter(matcher.group(7), matcher.group(11),
matcher.group(12), matcher.group(10));
}
if ("#".equals(matcher.group(14))) {
return new EnumerationCharacter(matcher.group(13),
matcher.group(18), matcher.group(20),
matcher.group(16), matcher.group(17));
}
throw new IllegalArgumentException("unkown indentation " + text);
}
@ -314,9 +310,9 @@ public class IndentCharacters {
private static String getBulletCharacter(final int level) {
if (level % 2 == 1) {
return System.getProperty("pdfbox.layout.bullet.odd", BULLET);
return System.getProperty("org.xbib.graphics.pdfbox.layout.bullet.odd", BULLET);
}
return System.getProperty("pdfbox.layout.bullet.even", DOUBLE_ANGLE);
return System.getProperty("org.xbib.graphics.pdfbox.layout.bullet.even", DOUBLE_ANGLE);
}
private static final String BULLET = "\u2022";