diff --git a/src/main/java/org/olat/core/util/pdf/PdfDocument.java b/src/main/java/org/olat/core/util/pdf/PdfDocument.java index d2d50f458c56e01e500af2ba7e5908344f3db3fc..fe4da289e1924ba9f1d15eb3aa3763c640baf4cb 100644 --- a/src/main/java/org/olat/core/util/pdf/PdfDocument.java +++ b/src/main/java/org/olat/core/util/pdf/PdfDocument.java @@ -22,6 +22,7 @@ package org.olat.core.util.pdf; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.Iterator; @@ -179,12 +180,33 @@ public class PdfDocument { public float getStringWidth(String string, float fontSize) throws IOException { - string = cleanString(string); - return fontSize * font.getStringWidth(string) / 1000; + if(string == null || string.length() == 0) return 0.0f; + + try { + string = cleanString(string); + return fontSize * font.getStringWidth(string) / 1000; + } catch (IllegalArgumentException e) { + log.error("", e); + return getStringEstimatedWidth(string.length(), fontSize); + } + } + + /** + * This method calculated the width of a string based on its length. + * + * @param length The length of the string + * @param fontSize The font size + * @return A width + * @throws IOException + */ + public float getStringEstimatedWidth(int length, float fontSize) throws IOException { + char[] onlyA = new char[length]; + Arrays.fill(onlyA, 'A'); + return fontSize * font.getStringWidth(String.valueOf(onlyA)) / 1000.0f; } public static String cleanString(String string) { - return string.replace('\n', ' ').replace('\r', ' ').replace('\t', ' '); + return string.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').replace('\u00A0', ' '); } public void drawLine(float xStart, float yStart, float xEnd, float yEnd, float lineWidth) diff --git a/src/test/java/org/olat/core/util/pdf/PdfDocumentTest.java b/src/test/java/org/olat/core/util/pdf/PdfDocumentTest.java index 81f641962789851e24cbab75b60a874c3c752e36..bf077836796010a715ba44e86a0aeda0347cf41e 100644 --- a/src/test/java/org/olat/core/util/pdf/PdfDocumentTest.java +++ b/src/test/java/org/olat/core/util/pdf/PdfDocumentTest.java @@ -19,8 +19,10 @@ */ package org.olat.core.util.pdf; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Locale; import org.junit.Assert; import org.junit.Test; @@ -44,7 +46,9 @@ public class PdfDocumentTest { { "Hello\nworld", "Hello world" }, { "Hello\r\nworld", "Hello world" }, { "Hello\tworld", "Hello world" }, - { "Hello\n\tworld", "Hello world" } + { "Hello\n\tworld", "Hello world" }, + { "Hello\n\tworld\u00A0", "Hello world " }, + { "Hello \u3044", "Hello \u3044" } }); } @@ -61,4 +65,15 @@ public class PdfDocumentTest { String val = PdfDocument.cleanString(string); Assert.assertEquals(cleanedString, val); } + + /** + * check if the + * @throws IOException + */ + @Test + public void getStringWidth() throws IOException { + PdfDocument doc = new PdfDocument(Locale.ENGLISH); + float width = doc.getStringWidth(string, 12f); + Assert.assertTrue(width > 0.0f); + } }