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 @@ -2,15 +2,15 @@

import com.openhtmltopdf.bidi.support.ICUBidiReorderer;
import com.openhtmltopdf.bidi.support.ICUBidiSplitter;
import com.openhtmltopdf.extend.FSObjectDrawer;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.OutputDeviceGraphicsDrawer;
import com.openhtmltopdf.java2d.api.BufferedImagePageProcessor;
import com.openhtmltopdf.java2d.api.DefaultPageProcessor;
import com.openhtmltopdf.java2d.api.FSPageOutputStreamSupplier;
import com.openhtmltopdf.java2d.api.Java2DRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder.TextDirection;
import com.openhtmltopdf.extend.FSObjectDrawer;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.OutputDeviceGraphicsDrawer;
import com.openhtmltopdf.render.DefaultObjectDrawerFactory;
import com.openhtmltopdf.render.RenderingContext;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;
Expand All @@ -24,6 +24,7 @@
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
Expand All @@ -35,10 +36,9 @@
public class TestcaseRunner {

/**
* Runs our set of manual test cases. You can specify an output directory
* with -DOUT_DIRECTORY=./output for example. Otherwise, the current working
* directory is used. Test cases must be placed in
* src/main/resources/testcases/
* Runs our set of manual test cases. You can specify an output directory with
* -DOUT_DIRECTORY=./output for example. Otherwise, the current working
* directory is used. Test cases must be placed in src/main/resources/testcases/
*
* @param args
* @throws Exception
Expand All @@ -49,8 +49,7 @@ public static void main(String[] args) throws Exception {
* Note: The RepeatedTableSample optionally requires the font file
* NotoSans-Regular.ttf to be placed in the resources directory.
*
* This sample demonstrates the failing repeated table header on each
* page.
* This sample demonstrates the failing repeated table header on each page.
*/
runTestCase("RepeatedTableSample");

Expand Down Expand Up @@ -81,12 +80,12 @@ public static void main(String[] args) throws Exception {
* Custom Objects
*/
runTestCase("custom-objects");

/*
* CSS3 multi-column layout
*/
runTestCase("multi-column-layout");

/* Add additional test cases here. */
}

Expand Down Expand Up @@ -234,8 +233,30 @@ public void drawObject(Element e, double x, double y, final double width, final
public void render(Graphics2D graphics2D) {
double realWidth = width / dotsPerPixel;
double realHeight = height / dotsPerPixel;

renderTree(graphics2D, realWidth / 2f, realHeight, realHeight / depth, -90, depth);
double titleBottomHeight = 10;

renderTree(graphics2D, realWidth / 2f, realHeight - titleBottomHeight, realHeight / depth,
-90, depth);

/*
* Now draw some text using different fonts to exercise all different font mappings
*/
Font font = Font.decode("Times New Roman").deriveFont(10f);
if (depth == 10)
font = Font.decode("Arial"); // Does not get mapped
if (angle == 35)
font = Font.decode("Courier"); // Would get mapped to Courier
if (depth == 6)
font = Font.decode("Dialog"); // Gets mapped to Helvetica
graphics2D.setFont(font);
String txt = "FanOut " + fanout + " Angle " + angle + " Depth " + depth;
Rectangle2D textBounds = font.getStringBounds(txt,
graphics2D.getFontRenderContext());
graphics2D.setPaint(new Color(16, 133, 30));
GradientPaint gp = new GradientPaint(10.0f, 25.0f, Color.blue, (float) textBounds.getWidth(), (float) textBounds.getHeight(), Color.red);
if (angle == 35)
graphics2D.setPaint(gp);
graphics2D.drawString(txt, (int)((realWidth - textBounds.getWidth()) / 2), (int)(realHeight - titleBottomHeight));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<style>
object {
border: 2px solid black;
margin: 1px;
}
</style>
</head>
Expand All @@ -11,12 +12,21 @@
<h1>Some binary trees</h1>

<object type="custom/binary-tree" data-fanout="3" data-depth="5" data-angle="20"
style="width:200px; height:300px;">
style="width:200px; height:300px;float:left;">
<!-- This is a simple example for a custom object drawer -->
</object>

<object type="custom/binary-tree" data-fanout="2" data-depth="12" data-angle="35"
style="width:280px; height:300px;float:right;">
<!-- This is a simple example for a custom object drawer -->
</object>

<object type="custom/binary-tree" data-fanout="2" data-depth="10" data-angle="30"
style="width:400px; height:300px;">
style="width:400px; height:300px;clear:both;float:left;">
<!-- This is a simple example for a custom object drawer -->
</object>
<object type="custom/binary-tree" data-fanout="2" data-depth="6" data-angle="40"
style="width:200px; height:300px;float:right;">
<!-- This is a simple example for a custom object drawer -->
</object>
</body>
Expand Down
4 changes: 2 additions & 2 deletions openhtmltopdf-pdfbox/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.6</version>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
Expand All @@ -54,7 +54,7 @@
<dependency>
<groupId>de.rototor.pdfbox</groupId>
<artifactId>graphics2d</artifactId>
<version>0.5</version>
<version>0.7</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import com.openhtmltopdf.bidi.BidiReorderer;
import com.openhtmltopdf.bidi.SimpleBidiReorderer;
import com.openhtmltopdf.css.constants.CSSName;
import com.openhtmltopdf.css.constants.IdentValue;
import com.openhtmltopdf.css.parser.FSCMYKColor;
import com.openhtmltopdf.css.parser.FSColor;
import com.openhtmltopdf.css.parser.FSRGBColor;
import com.openhtmltopdf.css.style.CalculatedStyle;
import com.openhtmltopdf.css.style.CssContext;
import com.openhtmltopdf.css.value.FontSpecification;
import com.openhtmltopdf.extend.FSImage;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.OutputDeviceGraphicsDrawer;
Expand All @@ -36,9 +38,8 @@
import com.openhtmltopdf.render.*;
import com.openhtmltopdf.util.Configuration;
import com.openhtmltopdf.util.XRLog;

import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D;

import de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DFontTextDrawer;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
Expand All @@ -63,7 +64,6 @@
import org.w3c.dom.Node;

import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.RenderingHints.Key;
import java.awt.geom.*;
Expand Down Expand Up @@ -219,6 +219,9 @@ public class PdfBoxOutputDevice extends AbstractOutputDevice implements OutputDe
// converting RTL text into its visual order.
private BidiReorderer _reorderer = new SimpleBidiReorderer();

// Font Mapping for the Graphics2D output
private PdfBoxGraphics2DFontTextDrawer _fontTextDrawer;

public PdfBoxOutputDevice(float dotsPerPoint, boolean testMode) {
_dotsPerPoint = dotsPerPoint;
_testMode = testMode;
Expand Down Expand Up @@ -1331,10 +1334,47 @@ public boolean isSupportsCMYKColors() {
public void drawWithGraphics(float x, float y, float width, float height, OutputDeviceGraphicsDrawer renderer) {
try {
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(_writer, (int) width, (int) height);
/*
* We *could* customize the PDF mapping here. But for now the default is enough.
* TODO: Font mapping.
*/
/*
* Create and set the fontTextDrawer to perform the font mapping.
*/
if (_fontTextDrawer == null) {
_fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer() {
@Override
protected PDFont mapFont(Font font, IFontTextDrawerEnv env)
throws IOException, FontFormatException {
FontSpecification spec = new FontSpecification();
spec.size = font.getSize();
spec.families = new String[] { font.getFamily() };
spec.fontStyle = IdentValue.NORMAL;
spec.fontWeight = IdentValue.NORMAL;
spec.variant = IdentValue.NORMAL;
if ((font.getStyle() & Font.BOLD) == Font.BOLD) {
spec.fontWeight = IdentValue.FONT_WEIGHT_700;
}
if ((font.getStyle() & Font.ITALIC) == Font.ITALIC) {
spec.fontStyle = IdentValue.ITALIC;
}
PdfBoxFSFont fsFont = (PdfBoxFSFont) getSharedContext().getFontResolver()
.resolveFont(getSharedContext(), spec);
FontDescription fontDescription = fsFont.getFontDescription().get(0);
/*
* Detect the default fallback value
*/
if (fsFont.getFontDescription().size() == 1) {
if (fontDescription.getFont().getName().equals("Times-Roman")
&& !(font.getFamily().equals("Times New Roman"))) {
/*
* We did not find the font, this is the generic default fallback font.
* So use the vectorized text shapes.
*/
return null;
}
}
return fontDescription.getFont();
}
};
}
pdfBoxGraphics2D.setFontTextDrawer(_fontTextDrawer);

/*
* Do rendering
Expand Down Expand Up @@ -1509,4 +1549,18 @@ public void setPaint(Paint paint) {
public void setAlpha(int alpha) {

}

/**
* Perform any internal cleanup needed
*/
public void close() {
if (_fontTextDrawer != null) {
try {
_fontTextDrawer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ public void setListener(PDFCreationListener listener) {
* Cleanup thread resources. MUST be called after finishing with the renderer.
*/
public void cleanup() {
_outputDevice.close();
_sharedContext.removeFromThread();
ThreadCtx.cleanup();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public PdfRendererBuilder useUnicodeToTitleTransformer(FSTextTransformer tr) {

/**
* Add a font programmatically. If the font is NOT subset, it will be downloaded when the renderer is run, otherwise
* the font will only be donwnloaded if needed. Therefore, the user could add many fonts, confidant that only those
* the font will only be downloaded if needed. Therefore, the user could add many fonts, confidant that only those
* that are used will be downloaded and processed.
*
* The InputStream returned by the supplier will be closed by the caller. Fonts should generally be subset, except
Expand Down
2 changes: 1 addition & 1 deletion openhtmltopdf-rtl-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>58.1</version>
<version>59.1</version>
</dependency>
<dependency>
<groupId>com.openhtmltopdf</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void addFontFaceFont(
String fontFamilyNameOverride, IdentValue fontWeightOverride, IdentValue fontStyleOverride, String uri, byte[] font1)
throws FontFormatException {

OpenHtmlGvtFontFamily family = null;
OpenHtmlGvtFontFamily family;

if (families.containsKey(fontFamilyNameOverride))
family = families.get(fontFamilyNameOverride);
Expand Down