use plain text and markup text in scripting engine

This commit is contained in:
Jörg Prante 2023-07-28 16:19:34 +02:00
parent 6f5b5f396d
commit 11bc5a9393
6 changed files with 88 additions and 8 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib.graphics group = org.xbib.graphics
name = graphics name = graphics
version = 4.3.4 version = 4.4.0
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -6,7 +6,6 @@ import org.xbib.graphics.pdfbox.layout.element.render.Transform;
import org.xbib.graphics.pdfbox.layout.text.Alignment; import org.xbib.graphics.pdfbox.layout.text.Alignment;
import org.xbib.graphics.pdfbox.layout.text.DrawListener; import org.xbib.graphics.pdfbox.layout.text.DrawListener;
import org.xbib.graphics.pdfbox.layout.position.Position; 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.TextFlow;
import org.xbib.graphics.pdfbox.layout.text.TextSequenceUtil; import org.xbib.graphics.pdfbox.layout.text.TextSequenceUtil;
import org.xbib.graphics.pdfbox.layout.text.WidthRespecting; import org.xbib.graphics.pdfbox.layout.text.WidthRespecting;
@ -84,9 +83,12 @@ public class Paragraph extends TextFlow implements Drawable, Element, WidthRespe
@Override @Override
public Element add(Element element) { public Element add(Element element) {
if (element instanceof TextElement) { if (element instanceof TextElement textElement) {
TextElement textElement = (TextElement) element; if (textElement.isMarkup()) {
addMarkup(textElement.getValue(), textElement.getFontSize(), textElement.getFont()); addMarkup(textElement.getValue(), textElement.getFontSize(), textElement.getFont());
} else {
addText(textElement.getValue(), textElement.getFontSize(), textElement.getFont());
}
} else { } else {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View file

@ -4,13 +4,16 @@ import org.xbib.graphics.pdfbox.layout.font.Font;
public class TextElement implements Element { public class TextElement implements Element {
private final boolean markup;
private final String value; private final String value;
private final Font font; private final Font font;
private final float fontsize; private final float fontsize;
public TextElement(String value, Font font, float fontsize) { public TextElement(boolean markup, String value, Font font, float fontsize) {
this.markup = markup;
this.value = value; this.value = value;
this.font = font; this.font = font;
this.fontsize = fontsize; this.fontsize = fontsize;
@ -20,6 +23,10 @@ public class TextElement implements Element {
return value; return value;
} }
public boolean isMarkup() {
return markup;
}
public Font getFont() { public Font getFont() {
return font; return font;
} }

View file

@ -0,0 +1,65 @@
package org.xbib.graphics.pdfbox.layout.element.scripting.command;
import org.xbib.graphics.pdfbox.layout.element.Document;
import org.xbib.graphics.pdfbox.layout.element.Element;
import org.xbib.graphics.pdfbox.layout.element.Paragraph;
import org.xbib.graphics.pdfbox.layout.element.TextElement;
import org.xbib.graphics.pdfbox.layout.element.render.VerticalLayoutHint;
import org.xbib.graphics.pdfbox.layout.element.scripting.Engine;
import org.xbib.graphics.pdfbox.layout.element.scripting.State;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.font.Fonts;
import org.xbib.graphics.pdfbox.layout.position.Position;
import org.xbib.graphics.pdfbox.layout.text.Alignment;
import org.xbib.settings.Settings;
import java.util.Locale;
import static org.xbib.graphics.pdfbox.layout.util.PdfUtil.mmToPt;
public class MarkupCommand implements Command {
@Override
public void execute(Engine engine, State state, Settings settings) {
String value = settings.get("value");
float fontsize = settings.getAsFloat("fontsize", 11.0f);
Document document = state.getDocument();
Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(document);
Element element = state.getElements().peek();
TextElement textElement = new TextElement(true, value, font, fontsize);
if (element instanceof Paragraph) {
element.add(textElement);
} else if (element instanceof Document) {
// wrap text into a standard paragraph
Paragraph paragraph = new Paragraph();
if (settings.containsSetting("x") && settings.containsSetting("y")) {
paragraph.setAbsolutePosition(new Position(mmToPt(settings.getAsFloat("x", 0f)), mmToPt(settings.getAsFloat("y", 0f))));
}
if (settings.containsSetting("width")) {
paragraph.setMaxWidth(mmToPt(settings.getAsFloat("width", 0f)));
}
if (settings.containsSetting("alignment")) {
paragraph.setAlignment(Alignment.valueOf(settings.get("alignment", "left").toUpperCase(Locale.ROOT)));
}
if (settings.containsSetting("linespacing")) {
paragraph.setLineSpacing(settings.getAsFloat("linespacing", 1.2f));
}
if (settings.containsSetting("rotation")) {
paragraph.setRotation(settings.getAsFloat("rotation", 0f));
}
paragraph.add(textElement);
Alignment alignment = Alignment.valueOf(settings.get("layout.alignment", "left").toUpperCase(Locale.ROOT));
String margin = settings.get("layout.margin", "0 0 0 0");
String[] margins = margin.split("\\s+");
float marginleft = Float.parseFloat(margins[0]);
float marginright = Float.parseFloat(margins[1]);
float margintop = Float.parseFloat(margins[2]);
float marginbottom = Float.parseFloat(margins[3]);
boolean resetY = settings.getAsBoolean("layout.resety", false);
VerticalLayoutHint verticalLayoutHint = new VerticalLayoutHint(alignment, marginleft, marginright, margintop, marginbottom, resetY);
element.add(paragraph, verticalLayoutHint);
} else {
throw new UnsupportedOperationException();
}
}
}

View file

@ -11,6 +11,7 @@ import org.xbib.graphics.pdfbox.layout.element.scripting.Engine;
import org.xbib.graphics.pdfbox.layout.element.scripting.State; import org.xbib.graphics.pdfbox.layout.element.scripting.State;
import org.xbib.graphics.pdfbox.layout.text.Alignment; import org.xbib.graphics.pdfbox.layout.text.Alignment;
import org.xbib.graphics.pdfbox.layout.position.Position; import org.xbib.graphics.pdfbox.layout.position.Position;
import org.xbib.graphics.pdfbox.layout.text.TextSequence;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
import java.util.Locale; import java.util.Locale;
@ -26,8 +27,9 @@ public class TextCommand implements Command {
Document document = state.getDocument(); Document document = state.getDocument();
Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(document); Font font = Fonts.valueOf(settings.get("font", "helvetica").toUpperCase(Locale.ROOT)).getFont(document);
Element element = state.getElements().peek(); Element element = state.getElements().peek();
TextElement textElement = new TextElement(false, value, font, fontsize);
if (element instanceof Paragraph) { if (element instanceof Paragraph) {
element.add(new TextElement(value, font, fontsize)); element.add(textElement);
} else if (element instanceof Document) { } else if (element instanceof Document) {
// wrap text into a standard paragraph // wrap text into a standard paragraph
Paragraph paragraph = new Paragraph(); Paragraph paragraph = new Paragraph();
@ -46,7 +48,7 @@ public class TextCommand implements Command {
if (settings.containsSetting("rotation")) { if (settings.containsSetting("rotation")) {
paragraph.setRotation(settings.getAsFloat("rotation", 0f)); paragraph.setRotation(settings.getAsFloat("rotation", 0f));
} }
paragraph.add(new TextElement(value, font, fontsize)); paragraph.add(textElement);
Alignment alignment = Alignment.valueOf(settings.get("layout.alignment", "left").toUpperCase(Locale.ROOT)); Alignment alignment = Alignment.valueOf(settings.get("layout.alignment", "left").toUpperCase(Locale.ROOT));
String margin = settings.get("layout.margin", "0 0 0 0"); String margin = settings.get("layout.margin", "0 0 0 0");
String[] margins = margin.split("\\s+"); String[] margins = margin.split("\\s+");

View file

@ -0,0 +1,4 @@
package org.xbib.graphics.pdfbox.layout.test;
public class TextTest {
}