fix document font loading for scripting engine

This commit is contained in:
Jörg Prante 2025-01-22 18:20:55 +01:00
parent 2d6961e848
commit b13aa5f3ef
3 changed files with 28 additions and 18 deletions

View file

@ -24,6 +24,7 @@ import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -85,7 +86,7 @@ public class Document implements Element, Closeable, RenderListener {
public Document(PageFormat pageFormat) { public Document(PageFormat pageFormat) {
this.pdDocument = new PDDocument(); this.pdDocument = new PDDocument();
this.pdDocumentInformation = new PDDocumentInformation(); this.pdDocumentInformation = new PDDocumentInformation();
this.fonts = new ArrayList<>(); this.fonts = new LinkedHashSet<>();
this.elements = new ArrayList<>(); this.elements = new ArrayList<>();
this.customRenderer = new ArrayList<>(); this.customRenderer = new ArrayList<>();
this.renderListener = new ArrayList<>(); this.renderListener = new ArrayList<>();
@ -117,23 +118,31 @@ public class Document implements Element, Closeable, RenderListener {
Iterable<FontEmbedder> fontEmbedders = ServiceLoader.load(FontEmbedder.class, classLoader); Iterable<FontEmbedder> fontEmbedders = ServiceLoader.load(FontEmbedder.class, classLoader);
boolean serviceLoaded = false; boolean serviceLoaded = false;
for (String alias : aliases) { for (String alias : aliases) {
boolean found = false; // fonts times, helvetica, courier are standard fonts without embedding
for (FontEmbedder fontEmbedder : fontEmbedders) { switch (alias) {
try { case "times" -> fonts.add(BaseFont.TIMES);
if (alias == null || alias.isEmpty() || fontEmbedder.getAlias().equals(alias)) { case "helvetica" -> fonts.add(BaseFont.HELVETICA);
fontEmbedder.embed(this.pdDocument); case "courier" -> fonts.add(BaseFont.COURIER);
found = true; case null, default -> {
serviceLoaded = true; boolean found = false;
fonts.addAll(fontEmbedder.getFonts()); for (FontEmbedder fontEmbedder : fontEmbedders) {
try {
if (alias == null || alias.isEmpty() || fontEmbedder.getAlias().equals(alias)) {
fontEmbedder.embed(this.pdDocument);
fonts.addAll(fontEmbedder.getFonts());
found = true;
serviceLoaded = true;
}
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
if (!found) {
logger.log(Level.WARNING, "font " + alias + " not found using embedders " +
StreamSupport.stream(fontEmbedders.spliterator(), false).toList());
} }
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
} }
} }
if (!found) {
logger.log(Level.WARNING, "font " + alias + " not found using embedders " +
StreamSupport.stream(fontEmbedders.spliterator(), false).toList());
}
} }
if (serviceLoaded) { if (serviceLoaded) {
break; break;

View file

@ -9,6 +9,7 @@ import org.xbib.settings.Settings;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -73,9 +74,7 @@ public class DocumentCommand implements Command {
document.setModificationDate(instant); document.setModificationDate(instant);
} }
String[] fonts = settings.getAsArray("font"); String[] fonts = settings.getAsArray("font");
for (String font : fonts) { document.embedFonts(Arrays.asList(fonts));
document.embedFonts(List.of(font));
}
state.getElements().push(document); state.getElements().push(document);
engine.executeElements(settings); engine.executeElements(settings);
} }

View file

@ -0,0 +1,2 @@
org.xbib.settings.datastructures.PropertiesSettingsLoader
org.xbib.settings.datastructures.json.JsonSettingsLoader