null if the input is null
+ * @see com.openhtmltopdf.util.OpenUtil#isCodePointPrintable(int)
+ */
+ static String getEffectivePrintableString(String input) {
+ if (input == null || input.isEmpty() || areAllCharactersPrintable(input)) {
+ return input;
+ }
+
+ StringBuilder effective = new StringBuilder(input.length());
+ input.codePoints().filter(OpenUtil::isCodePointPrintable).forEach(effective::appendCodePoint);
+
+ return effective.toString();
+ }
+
+ void setup(FontContext context);
+
+ void drawString(OutputDevice outputDevice, String string, float x, float y);
+ void drawString(
OutputDevice outputDevice, String string, float x, float y, JustificationInfo info);
- public FSFontMetrics getFSFontMetrics(
+ FSFontMetrics getFSFontMetrics(
FontContext context, FSFont font, String string );
/**
* Rarely need to use this method directly.
* Instead favor {@link Breaker} static method instead.
*/
- public int getWidth(FontContext context, FSFont font, String string);
+ int getWidth(FontContext context, FSFont font, String string);
- public void setFontScale(float scale);
+ void setFontScale(float scale);
- public float getFontScale();
+ float getFontScale();
/**
* Set the smoothing threashold. This is a font size above which
@@ -53,15 +75,15 @@ public FSFontMetrics getFSFontMetrics(
* Else, set to the threshold font size. does not take font scaling
* into account.
*/
- public void setSmoothingThreshold(float fontsize);
+ void setSmoothingThreshold(float fontsize);
- public int getSmoothingLevel();
+ int getSmoothingLevel();
/**
* @deprecated no-op, will be removed in a future release. Anti-aliasing is now controlled via the smoothing
* threshhold.
* @param level no-op
*/
- public void setSmoothingLevel(int level);
+ void setSmoothingLevel(int level);
}
diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/OpenUtil.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/OpenUtil.java
index 78f049962..46c4d3f97 100644
--- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/OpenUtil.java
+++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/OpenUtil.java
@@ -1,5 +1,7 @@
package com.openhtmltopdf.util;
+import java.util.Objects;
+
public class OpenUtil {
private OpenUtil() {}
@@ -23,7 +25,17 @@ public static boolean isCodePointPrintable(int codePoint) {
category == Character.PRIVATE_USE ||
category == Character.SURROGATE);
}
-
+
+ /**
+ * Returns true, when all characters of the given string are printable.
+ * @param str a non-null string to test
+ * @return whether all characters are printable
+ */
+ public static boolean areAllCharactersPrintable(String str) {
+ Objects.requireNonNull(str, "str");
+ return str.codePoints().allMatch(OpenUtil::isCodePointPrintable);
+ }
+
public static Integer parseIntegerOrNull(String possibleInteger) {
try {
return Integer.parseInt(possibleInteger);
diff --git a/openhtmltopdf-core/src/test/java/com/openhtmltopdf/extend/TextRendererTest.java b/openhtmltopdf-core/src/test/java/com/openhtmltopdf/extend/TextRendererTest.java
new file mode 100644
index 000000000..4221d1856
--- /dev/null
+++ b/openhtmltopdf-core/src/test/java/com/openhtmltopdf/extend/TextRendererTest.java
@@ -0,0 +1,21 @@
+package com.openhtmltopdf.extend;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+
+public class TextRendererTest {
+
+ @Test
+ public void getEffectivePrintableString() {
+ assertThat(TextRenderer.getEffectivePrintableString(null), nullValue());
+ assertThat(TextRenderer.getEffectivePrintableString(""), is(""));
+
+ assertThat(TextRenderer.getEffectivePrintableString("abc"), is("abc"));
+
+ assertThat(TextRenderer.getEffectivePrintableString("ab\u00adc"), is("abc"));
+ }
+}
diff --git a/openhtmltopdf-core/src/test/java/com/openhtmltopdf/util/OpenUtilTest.java b/openhtmltopdf-core/src/test/java/com/openhtmltopdf/util/OpenUtilTest.java
new file mode 100644
index 000000000..dec064128
--- /dev/null
+++ b/openhtmltopdf-core/src/test/java/com/openhtmltopdf/util/OpenUtilTest.java
@@ -0,0 +1,20 @@
+package com.openhtmltopdf.util;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.*;
+
+/**
+ * @author schrader
+ */
+public class OpenUtilTest {
+
+ @Test
+ public void areAllCharactersPrintable() {
+ String text = "abc 123 \uD844\uDCC1";
+ boolean printable = OpenUtil.areAllCharactersPrintable(text);
+ assertThat(printable, is(true));
+ }
+
+}
\ No newline at end of file
diff --git a/openhtmltopdf-examples/pom.xml b/openhtmltopdf-examples/pom.xml
index e18d37da6..2d43605be 100644
--- a/openhtmltopdf-examples/pom.xml
+++ b/openhtmltopdf-examples/pom.xml
@@ -23,6 +23,11 @@
+ Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular.
+ + diff --git a/openhtmltopdf-examples/src/main/resources/benchmark/render-text-soft-hyphens.html b/openhtmltopdf-examples/src/main/resources/benchmark/render-text-soft-hyphens.html new file mode 100644 index 000000000..a8628b78d --- /dev/null +++ b/openhtmltopdf-examples/src/main/resources/benchmark/render-text-soft-hyphens.html @@ -0,0 +1,11 @@ + + + + + + +Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc.
+ + diff --git a/openhtmltopdf-examples/src/main/resources/visualtest/expected/issue-482-infinite-loop-table.pdf b/openhtmltopdf-examples/src/main/resources/visualtest/expected/issue-482-infinite-loop-table.pdf index 664c98497..cb34896e2 100644 Binary files a/openhtmltopdf-examples/src/main/resources/visualtest/expected/issue-482-infinite-loop-table.pdf and b/openhtmltopdf-examples/src/main/resources/visualtest/expected/issue-482-infinite-loop-table.pdf differ diff --git a/openhtmltopdf-examples/src/main/resources/visualtest/expected/text/soft-hyphens.pdf b/openhtmltopdf-examples/src/main/resources/visualtest/expected/text/soft-hyphens.pdf index 100cf11ae..d42dc0db9 100644 Binary files a/openhtmltopdf-examples/src/main/resources/visualtest/expected/text/soft-hyphens.pdf and b/openhtmltopdf-examples/src/main/resources/visualtest/expected/text/soft-hyphens.pdf differ diff --git a/openhtmltopdf-examples/src/main/resources/visualtest/html/text/soft-hyphens.html b/openhtmltopdf-examples/src/main/resources/visualtest/html/text/soft-hyphens.html index 116faf7f9..482380855 100644 --- a/openhtmltopdf-examples/src/main/resources/visualtest/html/text/soft-hyphens.html +++ b/openhtmltopdf-examples/src/main/resources/visualtest/html/text/soft-hyphens.html @@ -114,5 +114,11 @@ +Haftpflichtversicherung
+Hähnchenmast
+