Skip to content

Commit

Permalink
Merge pull request #304 from zimmi/stop-logging-font-missing-unused-s…
Browse files Browse the repository at this point in the history
…pace

Fix warnings for icon font without space inside PDF/A, add tests
  • Loading branch information
danfickle authored Dec 7, 2018
2 parents 69c7952 + 95af01c commit 4a61489
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
import com.openhtmltopdf.layout.WhitespaceStripper;

/**
* A class which reprsents a portion of an inline element. If an inline element
* A class which represents a portion of an inline element. If an inline element
* does not contain any nested elements, then a single <code>InlineBox</code>
* object will contain the content for the entire element. Otherwise multiple
* <code>InlineBox</code> objects will be created corresponding to each
* discrete chunk of text appearing in the elment. It is not rendered directly
* discrete chunk of text appearing in the element. It is not rendered directly
* (and hence does not extend from {@link Box}), but does play an important
* role in layout (for example, when calculating min/max widths). Note that it
* does not contain children. Inline content is stored as a flat list in the
Expand Down Expand Up @@ -235,7 +235,7 @@ public int getTrailingSpaceWidth(LayoutContext c) {

private int calcMinWidthFromWordLength(
LayoutContext c, int cbWidth, boolean trimLeadingSpace, boolean includeWS) {
int spaceWidth = getSpaceWidth(c);
int spaceWidth = -1;

int last = 0;
int current = 0;
Expand Down Expand Up @@ -292,6 +292,9 @@ private int calcMinWidthFromWordLength(
break;
}
}
if (spaceCount > 0 && spaceWidth == -1) {
spaceWidth = getSpaceWidth(c);
}
}

String currentWord = text.substring(last);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.openhtmltopdf.mathmlsupport.MathMLDrawer;
import com.openhtmltopdf.objects.StandardObjectDrawerFactory;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder.PdfAConformance;
import com.openhtmltopdf.render.DefaultObjectDrawerFactory;
import com.openhtmltopdf.render.RenderingContext;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;
Expand Down Expand Up @@ -123,17 +124,24 @@ public static void main(String[] args) throws Exception {
* Will throw an exception if a SEVERE or WARNING message is logged.
*/
public static void runTestWithoutOutput(String testCaseFile) throws Exception {
runTestWithoutOutput(testCaseFile, false);
runTestWithoutOutput(testCaseFile, PdfAConformance.NONE, false);
}

/**
* Will throw an exception if a SEVERE or WARNING message is logged.
*/
public static void runTestWithoutOutput(String testCaseFile, PdfAConformance pdfaConformance) throws Exception {
runTestWithoutOutput(testCaseFile, pdfaConformance, false);
}

/**
* Will silently let ALL log messages through.
*/
public static void runTestWithoutOutputAndAllowWarnings(String testCaseFile) throws Exception {
runTestWithoutOutput(testCaseFile, true);
runTestWithoutOutput(testCaseFile, PdfAConformance.NONE, true);
}

private static void runTestWithoutOutput(String testCaseFile, boolean allowWarnings) throws Exception {
private static void runTestWithoutOutput(String testCaseFile, PdfAConformance pdfaConformance, boolean allowWarnings) throws Exception {
System.out.println("Trying to run: " + testCaseFile);

byte[] htmlBytes = IOUtils
Expand Down Expand Up @@ -166,14 +174,14 @@ public void log(String where, Level level, String msg) {
}
});

renderPDF(html, outputStream);
renderPDF(html, pdfaConformance, outputStream);

if (!warnings.isEmpty() && !allowWarnings) {
throw warnings.get(0);
}
}

private static void renderPDF(String html, OutputStream outputStream) throws Exception {
private static void renderPDF(String html, PdfAConformance pdfaConformance, OutputStream outputStream) throws Exception {
try {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
Expand All @@ -183,6 +191,7 @@ private static void renderPDF(String html, OutputStream outputStream) throws Exc
builder.useMathMLDrawer(new MathMLDrawer());
builder.addDOMMutator(LaTeXDOMMutator.INSTANCE);
builder.useObjectDrawerFactory(buildObjectDrawerFactory());
builder.usePdfAConformance(pdfaConformance);

builder.withHtmlContent(html, TestcaseRunner.class.getResource("/testcases/").toString());
builder.toStream(outputStream);
Expand Down Expand Up @@ -241,7 +250,7 @@ private static void runTestCase(String testCaseFile) throws Exception {
String testCaseOutputFile = outDir + "/" + testCaseFile + ".pdf";
String testCaseOutputPNGFile = outDir + "/" + testCaseFile + ".png";
FileOutputStream outputStream = new FileOutputStream(testCaseOutputFile);
renderPDF(html, outputStream);
renderPDF(html, PdfAConformance.NONE, outputStream);
System.out.println("Wrote " + testCaseOutputFile);

renderPNG(html, testCaseOutputPNGFile);
Expand Down
Binary file not shown.
19 changes: 19 additions & 0 deletions openhtmltopdf-examples/src/main/resources/testcases/icon-font.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
<style>
@font-face {
src: url(fonts/MaterialIcons-Regular.ttf);
font-family: 'MaterialIcons';
}
body {
font-family: serif;
}
.material-icon {
font-family: 'MaterialIcons';
}
</style>
</head>
<body>
<div style="display: table-cell;"><span class="material-icon">&#xe87c;</span></div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.openhtmltopdf.testcases;

import com.openhtmltopdf.pdfboxout.PdfRendererBuilder.PdfAConformance;
import org.junit.Test;

public class IconFontTest {

/**
* Icon fonts sometimes contain no space character. They should still be
* usable without warnings.
*/
@Test
public void testFontWithoutSpace() throws Exception {
TestcaseRunner.runTestWithoutOutput("icon-font");
}

/**
* Should also work for PDF/A.
*/
@Test
public void testFontWithoutSpacePdfA() throws Exception {
TestcaseRunner.runTestWithoutOutput("icon-font", PdfAConformance.PDFA_2_U);
}

}

0 comments on commit 4a61489

Please sign in to comment.