skip empty texts, add hebrew font

This commit is contained in:
Jörg Prante 2023-08-22 18:57:34 +02:00
parent d07aca4465
commit 97ed576828
10 changed files with 37 additions and 13 deletions

View file

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

View file

@ -157,6 +157,14 @@ public class Document implements Element, Closeable, RenderListener {
"NotoSans-BoldItalic.ttf"); "NotoSans-BoldItalic.ttf");
} }
public void registerNotoSansHebrewFont() throws IOException {
registerFont("notosanshebrew",
"NotoSansHebrew-Regular.ttf",
"NotoSansHebrew-Bold.ttf",
"NotoSansHebrew-Regular.ttf",
"NotoSansHebrew-Bold.ttf");
}
public void registerNotoSansCJKSCFont() throws IOException { public void registerNotoSansCJKSCFont() throws IOException {
registerFont("notosanscjksc", registerFont("notosanscjksc",
"NotoSansCJKsc-Regular.ttf", "NotoSansCJKsc-Regular.ttf",

View file

@ -78,6 +78,7 @@ public class DocumentCommand implements Command {
case "times" -> document.registerFont("times", BaseFont.TIMES); case "times" -> document.registerFont("times", BaseFont.TIMES);
case "courier" -> document.registerFont("courier", BaseFont.COURIER); case "courier" -> document.registerFont("courier", BaseFont.COURIER);
case "notosans" -> document.registerNotoSansFont(); case "notosans" -> document.registerNotoSansFont();
case "notosanshebrew" -> document.registerNotoSansHebrewFont();
case "notosanscjksc" -> document.registerNotoSansCJKSCFont(); case "notosanscjksc" -> document.registerNotoSansCJKSCFont();
case "opensans" -> document.registerOpenSansFont(); case "opensans" -> document.registerOpenSansFont();
case "sourcesans" -> document.registerSourceSansFont(); case "sourcesans" -> document.registerSourceSansFont();

View file

@ -20,6 +20,9 @@ public class MarkupCommand implements Command {
@Override @Override
public void execute(Engine engine, State state, Settings settings) { public void execute(Engine engine, State state, Settings settings) {
String text = settings.get("text"); String text = settings.get("text");
if (text == null) {
text = settings.get("value");
}
TextElement textElement = new TextElement(true, text); TextElement textElement = new TextElement(true, text);
String[] fonts = settings.getAsArray("font"); String[] fonts = settings.getAsArray("font");
float fontsize = settings.getAsFloat("fontsize", 10.0f); float fontsize = settings.getAsFloat("fontsize", 10.0f);

View file

@ -17,9 +17,15 @@ import static org.xbib.graphics.pdfbox.layout.util.PdfUtil.mmToPt;
public class TextCommand implements Command { public class TextCommand implements Command {
public TextCommand() {
}
@Override @Override
public void execute(Engine engine, State state, Settings settings) { public void execute(Engine engine, State state, Settings settings) {
String text = settings.get("text"); String text = settings.get("text");
if (text == null) {
text = settings.get("value");
}
TextElement textElement = new TextElement(false, text); TextElement textElement = new TextElement(false, text);
String[] fonts = settings.getAsArray("font"); String[] fonts = settings.getAsArray("font");
float fontsize = settings.getAsFloat("fontsize", 10.0f); float fontsize = settings.getAsFloat("fontsize", 10.0f);

View file

@ -68,7 +68,7 @@ public class FontDescriptor {
} }
} }
} }
throw new IllegalStateException(); throw new IllegalStateException("while searching a font for text = '" + text + "'");
} }
@Override @Override

View file

@ -124,6 +124,9 @@ public class StyledText implements TextFragment {
* @throws IOException by pdfbox. * @throws IOException by pdfbox.
*/ */
public float getAscent() throws IOException { public float getAscent() throws IOException {
if (text == null || text.isEmpty()) {
return 0f;
}
return getFontDescriptor().getSize() * getFontDescriptor().getSelectedFont(text).getFontDescriptor().getAscent() / 1000; return getFontDescriptor().getSize() * getFontDescriptor().getSelectedFont(text).getFontDescriptor().getAscent() / 1000;
} }
@ -179,6 +182,9 @@ public class StyledText implements TextFragment {
} }
private static float getWidth(FontDescriptor fontDescriptor, String text) { private static float getWidth(FontDescriptor fontDescriptor, String text) {
if (text == null || text.isEmpty()) {
return 0f;
}
try { try {
return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text).getStringWidth(text) / 1000; return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text).getStringWidth(text) / 1000;
} catch (Exception e) { } catch (Exception e) {

View file

@ -203,6 +203,7 @@ public class TextLine implements TextSequence {
if (transform != null) { if (transform != null) {
matrix = matrix.multiply(transform.getMatrix()); matrix = matrix.multiply(transform.getMatrix());
} }
if (!styledText.getText().isEmpty()) {
contentStream.beginText(); contentStream.beginText();
contentStream.setTextMatrix(matrix); contentStream.setTextMatrix(matrix);
if (!styledText.getFontDescriptor().equals(lastFontDesc)) { if (!styledText.getFontDescriptor().equals(lastFontDesc)) {
@ -213,10 +214,9 @@ public class TextLine implements TextSequence {
lastColor = styledText.getColor(); lastColor = styledText.getColor();
contentStream.setNonStrokingColor(lastColor); contentStream.setNonStrokingColor(lastColor);
} }
if (!styledText.getText().isEmpty()) {
contentStream.showText(styledText.getText()); contentStream.showText(styledText.getText());
}
contentStream.endText(); contentStream.endText();
}
if (drawListener != null) { if (drawListener != null) {
drawListener.drawn(styledText, drawListener.drawn(styledText,
new Position(x, y + styledText.getAscent()), new Position(x, y + styledText.getAscent()),