Skip to content

Commit

Permalink
Upgrades sambox to 3.0.22
Browse files Browse the repository at this point in the history
Lazy load fonts ref torakiki/sambox#180
  • Loading branch information
ediweissmann committed Dec 9, 2024
1 parent 705010c commit 40fcf7f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
<hibernate-validator.version>7.0.5.Final</hibernate-validator.version>
<jakarta-el.version>4.0.2</jakarta-el.version>
<hamcrest.version>3.0</hamcrest.version>
<sambox.version>3.0.21</sambox.version>
<sambox.version>3.0.22</sambox.version>
<sejda.io.version>3.0.1</sejda.io.version>
<bouncycastle.version>1.79</bouncycastle.version>
<twelvemonkeys.version>3.11.0</twelvemonkeys.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class FilenameFooterWriter {
private PageTextWriter writer;
private PDDocument document;

private static PDFont FONT = PDType1Font.HELVETICA;
private static PDFont FONT = PDType1Font.HELVETICA();
private static double FONT_SIZE = 10;

public FilenameFooterWriter(boolean addFooter, PDDocument document) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.sejda.sambox.pdmodel.PDPageContentStream.AppendMode;
import org.sejda.sambox.pdmodel.common.PDRectangle;
import org.sejda.sambox.pdmodel.font.PDFont;
import org.sejda.sambox.pdmodel.font.PDType1Font;
import org.sejda.sambox.pdmodel.graphics.color.PDColor;
import org.sejda.sambox.pdmodel.graphics.color.PDDeviceRGB;
import org.sejda.sambox.pdmodel.graphics.state.RenderingMode;
Expand Down Expand Up @@ -264,6 +265,6 @@ private Point2D findPositionInRotatedPage(int rotation, PDRectangle pageSize, Po

public static void writeHeader(PDDocument doc, PDPage page, String text) throws TaskIOException {
PageTextWriter writer = new PageTextWriter(doc);
writer.write(page, HorizontalAlign.CENTER, VerticalAlign.TOP, text, FontUtils.HELVETICA, 10d, Color.black);
writer.write(page, HorizontalAlign.CENTER, VerticalAlign.TOP, text, PDType1Font.HELVETICA(), 10d, Color.black);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class TableOfContentsCreator {
private PDRectangle pageSize = null;
private float fontSize = DEFAULT_FONT_SIZE;
private float margin = DEFAULT_MARGIN;
private PDFont font = PDType1Font.HELVETICA;
private PDFont font = PDType1Font.HELVETICA();
private float lineHeight;
private MergeParameters params;
private PageTextWriter writer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,6 @@ private FontUtils() {
// hide
}

private static final Map<StandardType1Font, PDType1Font> STANDARD_TYPE1_FONTS;

static {
Map<StandardType1Font, PDType1Font> fontsCache = new EnumMap<>(StandardType1Font.class);
fontsCache.put(StandardType1Font.CURIER, PDType1Font.COURIER);
fontsCache.put(StandardType1Font.CURIER_BOLD, PDType1Font.COURIER_BOLD);
fontsCache.put(StandardType1Font.CURIER_BOLD_OBLIQUE, PDType1Font.COURIER_BOLD_OBLIQUE);
fontsCache.put(StandardType1Font.CURIER_OBLIQUE, PDType1Font.COURIER_OBLIQUE);
fontsCache.put(StandardType1Font.HELVETICA, PDType1Font.HELVETICA);
fontsCache.put(StandardType1Font.HELVETICA_BOLD, PDType1Font.HELVETICA_BOLD);
fontsCache.put(StandardType1Font.HELVETICA_BOLD_OBLIQUE, PDType1Font.HELVETICA_BOLD_OBLIQUE);
fontsCache.put(StandardType1Font.HELVETICA_OBLIQUE, PDType1Font.HELVETICA_OBLIQUE);
fontsCache.put(StandardType1Font.SYMBOL, PDType1Font.SYMBOL);
fontsCache.put(StandardType1Font.ZAPFDINGBATS, PDType1Font.ZAPF_DINGBATS);
fontsCache.put(StandardType1Font.TIMES_BOLD, PDType1Font.TIMES_BOLD);
fontsCache.put(StandardType1Font.TIMES_BOLD_ITALIC, PDType1Font.TIMES_BOLD_ITALIC);
fontsCache.put(StandardType1Font.TIMES_ITALIC, PDType1Font.TIMES_ITALIC);
fontsCache.put(StandardType1Font.TIMES_ROMAN, PDType1Font.TIMES_ROMAN);
STANDARD_TYPE1_FONTS = Collections.unmodifiableMap(fontsCache);
}

public static PDFont HELVETICA = PDType1Font.HELVETICA;

public static final FontResource[] TYPE0FONTS;

static {
Expand All @@ -107,7 +84,22 @@ private FontUtils() {
* @return the PDFBox font.
*/
public static PDType1Font getStandardType1Font(StandardType1Font st1Font) {
return STANDARD_TYPE1_FONTS.get(st1Font);
return switch (st1Font){
case TIMES_ROMAN -> PDType1Font.TIMES_ROMAN();
case TIMES_BOLD -> PDType1Font.TIMES_BOLD();
case TIMES_ITALIC -> PDType1Font.TIMES_ITALIC();
case TIMES_BOLD_ITALIC -> PDType1Font.TIMES_BOLD_ITALIC();
case HELVETICA -> PDType1Font.HELVETICA();
case HELVETICA_BOLD -> PDType1Font.HELVETICA_BOLD();
case HELVETICA_OBLIQUE -> PDType1Font.HELVETICA_OBLIQUE();
case HELVETICA_BOLD_OBLIQUE -> PDType1Font.HELVETICA_BOLD_OBLIQUE();
case CURIER -> PDType1Font.COURIER();
case CURIER_BOLD -> PDType1Font.COURIER_BOLD();
case CURIER_OBLIQUE -> PDType1Font.COURIER_OBLIQUE();
case CURIER_BOLD_OBLIQUE -> PDType1Font.COURIER_BOLD_OBLIQUE();
case SYMBOL -> PDType1Font.SYMBOL();
case ZAPFDINGBATS -> PDType1Font.ZAPF_DINGBATS();
};
}

/**
Expand Down Expand Up @@ -674,7 +666,7 @@ public static String removeUnsupportedCharacters(String text, PDDocument doc) {
}

public static String replaceUnsupportedCharacters(String text, PDDocument doc, String replacement) {
List<TextWithFont> resolved = resolveFonts(text, HELVETICA, doc);
List<TextWithFont> resolved = resolveFonts(text, PDType1Font.HELVETICA(), doc);
Set<String> unsupported = new HashSet<>();
resolved.forEach(tf -> {
if (tf.getFont() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@
*/
public class FontUtilsTest {

private static PDFont HELVETICA = FontUtils.HELVETICA;
private static PDFont HELVETICA = PDType1Font.HELVETICA();

@Test
public void testGetStandardType1Font() {
assertEquals(PDType1Font.COURIER, getStandardType1Font(StandardType1Font.CURIER));
assertEquals(PDType1Font.COURIER_BOLD, getStandardType1Font(StandardType1Font.CURIER_BOLD));
assertEquals(PDType1Font.COURIER_BOLD_OBLIQUE, getStandardType1Font(StandardType1Font.CURIER_BOLD_OBLIQUE));
assertEquals(PDType1Font.COURIER_OBLIQUE, getStandardType1Font(StandardType1Font.CURIER_OBLIQUE));
assertEquals(PDType1Font.HELVETICA, getStandardType1Font(StandardType1Font.HELVETICA));
assertEquals(PDType1Font.HELVETICA_BOLD, getStandardType1Font(StandardType1Font.HELVETICA_BOLD));
assertEquals(PDType1Font.HELVETICA_BOLD_OBLIQUE,
assertEquals(PDType1Font.COURIER(), getStandardType1Font(StandardType1Font.CURIER));
assertEquals(PDType1Font.COURIER_BOLD(), getStandardType1Font(StandardType1Font.CURIER_BOLD));
assertEquals(PDType1Font.COURIER_BOLD_OBLIQUE(), getStandardType1Font(StandardType1Font.CURIER_BOLD_OBLIQUE));
assertEquals(PDType1Font.COURIER_OBLIQUE(), getStandardType1Font(StandardType1Font.CURIER_OBLIQUE));
assertEquals(PDType1Font.HELVETICA(), getStandardType1Font(StandardType1Font.HELVETICA));
assertEquals(PDType1Font.HELVETICA_BOLD(), getStandardType1Font(StandardType1Font.HELVETICA_BOLD));
assertEquals(PDType1Font.HELVETICA_BOLD_OBLIQUE(),
getStandardType1Font(StandardType1Font.HELVETICA_BOLD_OBLIQUE));
assertEquals(PDType1Font.HELVETICA_OBLIQUE, getStandardType1Font(StandardType1Font.HELVETICA_OBLIQUE));
assertEquals(PDType1Font.TIMES_BOLD, getStandardType1Font(StandardType1Font.TIMES_BOLD));
assertEquals(PDType1Font.TIMES_BOLD_ITALIC, getStandardType1Font(StandardType1Font.TIMES_BOLD_ITALIC));
assertEquals(PDType1Font.TIMES_ITALIC, getStandardType1Font(StandardType1Font.TIMES_ITALIC));
assertEquals(PDType1Font.TIMES_ROMAN, getStandardType1Font(StandardType1Font.TIMES_ROMAN));
assertEquals(PDType1Font.SYMBOL, getStandardType1Font(StandardType1Font.SYMBOL));
assertEquals(PDType1Font.ZAPF_DINGBATS, getStandardType1Font(StandardType1Font.ZAPFDINGBATS));
assertEquals(PDType1Font.HELVETICA_OBLIQUE(), getStandardType1Font(StandardType1Font.HELVETICA_OBLIQUE));
assertEquals(PDType1Font.TIMES_BOLD(), getStandardType1Font(StandardType1Font.TIMES_BOLD));
assertEquals(PDType1Font.TIMES_BOLD_ITALIC(), getStandardType1Font(StandardType1Font.TIMES_BOLD_ITALIC));
assertEquals(PDType1Font.TIMES_ITALIC(), getStandardType1Font(StandardType1Font.TIMES_ITALIC));
assertEquals(PDType1Font.TIMES_ROMAN(), getStandardType1Font(StandardType1Font.TIMES_ROMAN));
assertEquals(PDType1Font.SYMBOL(), getStandardType1Font(StandardType1Font.SYMBOL));
assertEquals(PDType1Font.ZAPF_DINGBATS(), getStandardType1Font(StandardType1Font.ZAPFDINGBATS));
}

private PDFont findFontFor(String s) {
Expand Down

0 comments on commit 40fcf7f

Please sign in to comment.