use plain text and markup text in scripting engine
This commit is contained in:
parent
6f5b5f396d
commit
11bc5a9393
6 changed files with 88 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
|||
group = org.xbib.graphics
|
||||
name = graphics
|
||||
version = 4.3.4
|
||||
version = 4.4.0
|
||||
|
||||
org.gradle.warning.mode = ALL
|
||||
|
|
|
@ -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.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;
|
||||
|
@ -84,9 +83,12 @@ public class Paragraph extends TextFlow implements Drawable, Element, WidthRespe
|
|||
|
||||
@Override
|
||||
public Element add(Element element) {
|
||||
if (element instanceof TextElement) {
|
||||
TextElement textElement = (TextElement) element;
|
||||
if (element instanceof TextElement textElement) {
|
||||
if (textElement.isMarkup()) {
|
||||
addMarkup(textElement.getValue(), textElement.getFontSize(), textElement.getFont());
|
||||
} else {
|
||||
addText(textElement.getValue(), textElement.getFontSize(), textElement.getFont());
|
||||
}
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
|
|
@ -4,13 +4,16 @@ import org.xbib.graphics.pdfbox.layout.font.Font;
|
|||
|
||||
public class TextElement implements Element {
|
||||
|
||||
private final boolean markup;
|
||||
|
||||
private final String value;
|
||||
|
||||
private final Font font;
|
||||
|
||||
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.font = font;
|
||||
this.fontsize = fontsize;
|
||||
|
@ -20,6 +23,10 @@ public class TextElement implements Element {
|
|||
return value;
|
||||
}
|
||||
|
||||
public boolean isMarkup() {
|
||||
return markup;
|
||||
}
|
||||
|
||||
public Font getFont() {
|
||||
return font;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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.text.Alignment;
|
||||
import org.xbib.graphics.pdfbox.layout.position.Position;
|
||||
import org.xbib.graphics.pdfbox.layout.text.TextSequence;
|
||||
import org.xbib.settings.Settings;
|
||||
|
||||
import java.util.Locale;
|
||||
|
@ -26,8 +27,9 @@ public class TextCommand implements Command {
|
|||
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(false, value, font, fontsize);
|
||||
if (element instanceof Paragraph) {
|
||||
element.add(new TextElement(value, font, fontsize));
|
||||
element.add(textElement);
|
||||
} else if (element instanceof Document) {
|
||||
// wrap text into a standard paragraph
|
||||
Paragraph paragraph = new Paragraph();
|
||||
|
@ -46,7 +48,7 @@ public class TextCommand implements Command {
|
|||
if (settings.containsSetting("rotation")) {
|
||||
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));
|
||||
String margin = settings.get("layout.margin", "0 0 0 0");
|
||||
String[] margins = margin.split("\\s+");
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package org.xbib.graphics.pdfbox.layout.test;
|
||||
|
||||
public class TextTest {
|
||||
}
|
Loading…
Reference in a new issue