Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the <object> drawer also for Java2D output. #87

Merged
merged 1 commit into from
Apr 19, 2017
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 @@ -152,10 +152,7 @@ private static void renderPDF(String html, OutputStream outputStream) throws Exc
builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
builder.defaultTextDirection(TextDirection.LTR);
builder.useSVGDrawer(new BatikSVGDrawer());

DefaultObjectDrawerFactory objectDrawerFactory = new DefaultObjectDrawerFactory();
objectDrawerFactory.registerDrawer("custom/binary-tree", new SampleObjectDrawerBinaryTree());
builder.useObjectDrawerFactory(objectDrawerFactory);
builder.useObjectDrawerFactory(buildObjectDrawerFactory());

builder.withHtmlContent(html, TestcaseRunner.class.getResource("/testcases/").toString());
builder.toStream(outputStream);
Expand All @@ -165,9 +162,16 @@ private static void renderPDF(String html, OutputStream outputStream) throws Exc
}
}

private static DefaultObjectDrawerFactory buildObjectDrawerFactory() {
DefaultObjectDrawerFactory objectDrawerFactory = new DefaultObjectDrawerFactory();
objectDrawerFactory.registerDrawer("custom/binary-tree", new SampleObjectDrawerBinaryTree());
return objectDrawerFactory;
}

private static void renderPNG(String html, final String filename) throws Exception {
Java2DRendererBuilder builder = new Java2DRendererBuilder();
builder.useSVGDrawer(new BatikSVGDrawer());
builder.useObjectDrawerFactory(buildObjectDrawerFactory());
builder.withHtmlContent(html, TestcaseRunner.class.getResource("/testcases/").toString());
BufferedImagePageProcessor bufferedImagePageProcessor = new BufferedImagePageProcessor(
BufferedImage.TYPE_INT_RGB, 2.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.openhtmltopdf.java2d;

import com.openhtmltopdf.extend.FSObjectDrawer;
import com.openhtmltopdf.extend.OutputDevice;
import com.openhtmltopdf.extend.ReplacedElement;
import com.openhtmltopdf.java2d.api.Java2DRendererBuilder;
import com.openhtmltopdf.render.RenderingContext;
import org.w3c.dom.Element;

public class Java2DObjectDrawerReplacedElement extends Java2DRendererBuilder.Graphics2DPaintingReplacedElement
implements ReplacedElement {
private final Element e;
private final int dotsPerPixel;
private final FSObjectDrawer drawer;

public Java2DObjectDrawerReplacedElement(Element e, FSObjectDrawer drawer, int width, int height,
int dotsPerPixel) {
super(width, height);
this.e = e;
this.drawer = drawer;
this.dotsPerPixel = dotsPerPixel;
}

@Override
public void paint(OutputDevice outputDevice, RenderingContext ctx, double x, double y, final double width,
final double height) {
drawer.drawObject(e, x, y, width, height, outputDevice, ctx, dotsPerPixel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class Java2DRenderer implements IJava2DRenderer {
private BidiReorderer _reorderer;

private final SVGDrawer _svgImpl;
private final FSObjectDrawerFactory _objectDrawerFactory;
private final FSPageProcessor _pageProcessor;

private static final int DEFAULT_DOTS_PER_PIXEL = 1;
Expand All @@ -74,12 +75,14 @@ public Java2DRenderer(
boolean testMode,
FSPageProcessor pageProcessor,
Graphics2D layoutGraphics,
int initialPageNumber, short pagingMode) {
int initialPageNumber, short pagingMode,
FSObjectDrawerFactory objectDrawerFactory) {

_pagingMode = pagingMode;
_pageProcessor = pageProcessor;
_initialPageNo = initialPageNumber;
_svgImpl = svgImpl;
_objectDrawerFactory = objectDrawerFactory;
_outputDevice = new Java2DOutputDevice(layoutGraphics);

NaiveUserAgent uac = new NaiveUserAgent();
Expand Down Expand Up @@ -107,7 +110,7 @@ public Java2DRenderer(
Java2DFontResolver fontResolver = new Java2DFontResolver(_sharedContext);
_sharedContext.setFontResolver(fontResolver);

Java2DReplacedElementFactory replacedFactory = new Java2DReplacedElementFactory(_svgImpl);
Java2DReplacedElementFactory replacedFactory = new Java2DReplacedElementFactory(_svgImpl, _objectDrawerFactory);
_sharedContext.setReplacedElementFactory(replacedFactory);

_sharedContext.setTextRenderer(new Java2DTextRenderer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import org.w3c.dom.Element;

import com.openhtmltopdf.extend.ReplacedElement;
import com.openhtmltopdf.extend.SVGDrawer;
import com.openhtmltopdf.extend.UserAgentCallback;
import com.openhtmltopdf.extend.*;
import com.openhtmltopdf.layout.LayoutContext;
import com.openhtmltopdf.render.BlockBox;
import com.openhtmltopdf.swing.SwingReplacedElementFactory;

public class Java2DReplacedElementFactory extends SwingReplacedElementFactory {

private SVGDrawer _svgImpl;
private final SVGDrawer _svgImpl;
private final FSObjectDrawerFactory _objectDrawerFactory;

public Java2DReplacedElementFactory(SVGDrawer svgImpl) {
public Java2DReplacedElementFactory(SVGDrawer svgImpl, FSObjectDrawerFactory objectDrawerFactory) {
this._svgImpl = svgImpl;
this._objectDrawerFactory = objectDrawerFactory;
}

@Override
Expand All @@ -26,8 +26,14 @@ public ReplacedElement createReplacedElement(LayoutContext context, BlockBox box
}

String nodeName = e.getNodeName();
if (nodeName.equals("svg") && _svgImpl != null)
if (nodeName.equals("svg") && _svgImpl != null) {
return new Java2DSVGReplacedElement(e, _svgImpl, cssWidth, cssHeight);
} else if (nodeName.equals("object") && _objectDrawerFactory != null) {
FSObjectDrawer drawer = _objectDrawerFactory.createDrawer(e);
if (drawer != null)
return new Java2DObjectDrawerReplacedElement(e, drawer, cssWidth, cssHeight,
context.getSharedContext().getDotsPerPixel());
}

/*
* Default: Just let the base class handle everything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class Java2DRendererBuilder {
private Graphics2D _layoutGraphics;
private int _initialPageNumber;
private short _pagingMode = Layer.PAGED_MODE_PRINT;
private FSObjectDrawerFactory _objectDrawerFactory;

public static enum TextDirection { RTL, LTR; }
public static enum PageSizeUnits { MM, INCHES }
Expand Down Expand Up @@ -411,9 +412,19 @@ public Java2DRenderer buildJava2DRenderer() {
_layoutGraphics = bf.createGraphics();
}

return new Java2DRenderer(doc, unicode, _httpStreamFactory, _resolver, _cache, _svgImpl, pageSize, _replacementText, _testMode, _pageProcessor, _layoutGraphics, _initialPageNumber, _pagingMode);
return new Java2DRenderer(doc, unicode, _httpStreamFactory, _resolver, _cache, _svgImpl, pageSize, _replacementText, _testMode, _pageProcessor, _layoutGraphics, _initialPageNumber, _pagingMode, _objectDrawerFactory);
}

/**
* Set a factory for &lt;object&gt; drawers
* @param objectDrawerFactory Object Drawer Factory
* @return this for method chaining
*/
public Java2DRendererBuilder useObjectDrawerFactory(FSObjectDrawerFactory objectDrawerFactory) {
this._objectDrawerFactory = objectDrawerFactory;
return this;
}

public static abstract class Graphics2DPaintingReplacedElement extends EmptyReplacedElement {
protected Graphics2DPaintingReplacedElement(int width, int height) {
super(width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ public PdfRendererBuilder useFont(FSSupplier<InputStream> supplier, String fontF

/**
* Set a factory for &lt;object&gt; drawers
* @param _objectDrawerFactory Object Drawer Factory
* @param objectDrawerFactory Object Drawer Factory
* @return this for method chaining
*/
public PdfRendererBuilder useObjectDrawerFactory(FSObjectDrawerFactory _objectDrawerFactory) {
this._objectDrawerFactory = _objectDrawerFactory;
public PdfRendererBuilder useObjectDrawerFactory(FSObjectDrawerFactory objectDrawerFactory) {
this._objectDrawerFactory = objectDrawerFactory;
return this;
}
}