Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,48 @@
import com.openhtmltopdf.render.FSFont;
import com.openhtmltopdf.render.FSFontMetrics;
import com.openhtmltopdf.render.JustificationInfo;
import com.openhtmltopdf.util.OpenUtil;

import static com.openhtmltopdf.util.OpenUtil.areAllCharactersPrintable;

public interface TextRenderer {
public void setup(FontContext context);

public void drawString(OutputDevice outputDevice, String string, float x, float y);
public void drawString(
/**
* Returns a string containing printable characters only.
*
* @param input The string can be null
* @return The cleaned string or <code>null</code> 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
Expand All @@ -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);
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.openhtmltopdf.util;

import java.util.Objects;

public class OpenUtil {

private OpenUtil() {}
Expand All @@ -23,7 +25,17 @@ public static boolean isCodePointPrintable(int codePoint) {
category == Character.PRIVATE_USE ||
category == Character.SURROGATE);
}


/**
* Returns <code>true</code>, 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}

}
62 changes: 62 additions & 0 deletions openhtmltopdf-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
</license>
</licenses>

<properties>
<jmh.version>1.25.2</jmh.version>
<uberjar.name>benchmarks</uberjar.name>
</properties>

<dependencies>
<dependency>
<groupId>com.openhtmltopdf</groupId>
Expand Down Expand Up @@ -115,6 +120,18 @@
<version>1.13.1</version>
</dependency>

<!-- JMH Benchmarks -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -154,6 +171,51 @@
</systemProperties>
</configuration>
</plugin>
<!-- workaround for: 'java.lang.IllegalStateException: endPosTable already set'.
see: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8067747
Thrown while compiling JMH classes
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<!-- Create an all-in JAR for running JMH benchmarks -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<!--
Shading signed JARs will fail without this.
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.openhtmltopdf.benchmark;

import com.openhtmltopdf.util.Diagnostic;
import com.openhtmltopdf.util.XRLogger;

import java.util.logging.Level;

/**
* @author schrader
*/
class NoopLogger implements XRLogger {
@Override
public void log(String where, Level level, String msg) {

}

@Override
public void log(String where, Level level, String msg, Throwable th) {

}

@Override
public void setLevel(String logger, Level level) {

}

@Override
public boolean isLogLevelEnabled(Diagnostic diagnostic) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.openhtmltopdf.benchmark;

import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.util.XRLog;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.util.Charsets;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author schrader
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
public class RenderTextBenchmark {

public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.include(RenderTextBenchmark.class.getSimpleName())
.forks(1)
.build();

new Runner(opt).run();
}

private Map<String, String> contents = new HashMap<>();

@Setup
public void setUp() {
XRLog.setLoggerImpl(new NoopLogger());

Arrays.asList(
"/benchmark/render-text-plain.html",
"/benchmark/render-text-soft-hyphens.html"
).forEach(path -> contents.put(path, readContent(path)));
}

@Benchmark
public void renderText_Plain() throws Exception {
runRenderer(contents.get("/benchmark/render-text-plain.html"));
}

@Benchmark
public void renderText_SoftHyphens() throws Exception {
runRenderer(contents.get("/benchmark/render-text-soft-hyphens.html"));
}

private void runRenderer(String html) throws IOException {
ByteArrayOutputStream actual = new ByteArrayOutputStream();

PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withHtmlContent(html, null);
builder.toStream(actual);
builder.useFastMode();
builder.testMode(true);

builder.run();
}

private String readContent(String path) {
try (InputStream htmlIs = RenderTextBenchmark.class.getResourceAsStream(path)) {
byte[] htmlBytes = IOUtils.toByteArray(htmlIs);
return new String(htmlBytes, Charsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html PUBLIC
"-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN" "">
<html>
<head>
<style>
</style>
</head>
<body>
<p>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.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html PUBLIC
"-//OPENHTMLTOPDF//DOC XHTML Character Entities Only 1.0//EN" "">
<html>
<head>
<style>
</style>
</head>
<body>
<p>Li Eu­ro­pan lin­gues es mem­bres del sam fa­mi­lie. Lor se­pa­rat exi­sten­tie es un myth. Por sci­en­tie, mu­si­ca, spo­rt etc.</p>
</body>
</html>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,11 @@
</tbody>
</table>

<div style="font-family: 'ExtraFont'; border: 1px solid black">
<h2>No Hyphen Signs visible</h2>
<p style="border: 1px solid gray">Haft&shy;pflicht&shy;ver&shy;si&shy;che&shy;rung</p>
<p style="border: 1px solid gray">Hähn­chen­mast</p>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ public void testJustifySpaceAtEnd() throws IOException {
*/
@Test
public void testSoftHyphens() throws IOException {
assertTrue(vtester.runTest("soft-hyphens", TestSupport.WITH_COLLAPSED_LINE_BREAKER));
assertTrue(vtester.runTest("soft-hyphens", builder -> {
TestSupport.WITH_COLLAPSED_LINE_BREAKER.configure(builder);
builder.useFont(new File("target/test/visual-tests/SourceSansPro-Regular.ttf"), "ExtraFont");
}));
}

/**
Expand Down
Loading