From 4e2911f64820c38310ca2cf3eed015f450ee368b Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Wed, 6 Sep 2023 21:58:28 +0100 Subject: [PATCH] custom font fix --- .../controller/web/GeneralWebController.java | 80 +++++++++++++++++-- src/main/resources/templates/sign.html | 24 +++--- 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java index 3cc55a4e..e04626da 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java @@ -9,6 +9,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -140,27 +141,96 @@ public class GeneralWebController { @Autowired private ResourceLoader resourceLoader; - private List getFontNames() { + private List getFontNames() { + List fontNames = new ArrayList<>(); + + // Extract font names from classpath + fontNames.addAll(getFontNamesFromLocation("classpath:static/fonts/*.woff2")); + + // Extract font names from external directory + fontNames.addAll(getFontNamesFromLocation("file:customFiles/static/fonts/*")); + + return fontNames; + } + + private List getFontNamesFromLocation(String locationPattern) { try { Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader) - .getResources("classpath:static/fonts/*.woff2"); - + .getResources(locationPattern); return Arrays.stream(resources) .map(resource -> { try { String filename = resource.getFilename(); - return filename.substring(0, filename.length() - 6); // Remove .woff2 extension + if (filename != null) { + int lastDotIndex = filename.lastIndexOf('.'); + if (lastDotIndex != -1) { + String name = filename.substring(0, lastDotIndex); + String extension = filename.substring(lastDotIndex + 1); + return new FontResource(name, extension); + } + } + return null; } catch (Exception e) { throw new RuntimeException("Error processing filename", e); } }) + .filter(Objects::nonNull) .collect(Collectors.toList()); } catch (Exception e) { - throw new RuntimeException("Failed to read font directory", e); + throw new RuntimeException("Failed to read font directory from " + locationPattern, e); + } + } + + + public String getFormatFromExtension(String extension) { + switch (extension) { + case "ttf": return "truetype"; + case "woff": return "woff"; + case "woff2": return "woff2"; + case "eot": return "embedded-opentype"; + case "svg": return "svg"; + default: return ""; // or throw an exception if an unexpected extension is encountered } } + public class FontResource { + private String name; + private String extension; + private String type; + public FontResource(String name, String extension) { + this.name = name; + this.extension = extension; + this.type = getFormatFromExtension(extension); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getExtension() { + return extension; + } + + public void setExtension(String extension) { + this.extension = extension; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + + } + @GetMapping("/crop") @Hidden diff --git a/src/main/resources/templates/sign.html b/src/main/resources/templates/sign.html index 3f06f5d5..683bf162 100644 --- a/src/main/resources/templates/sign.html +++ b/src/main/resources/templates/sign.html @@ -10,15 +10,16 @@ + +