forked from danfickle/openhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ObjectDrawer for <object> tags.
Implement a default factory for FSObjectDrawer, which allows to register custom drawers. The testrunner includes a simple example how to implement a FSObjectDrawer. Currently its only possible with the PdfBoxRendererBuilder to register a FSObjectDrawerFactory. As soon as danfickle#77 is merged I will implement the same for Java2D. This is the final feature I need to migrate all my reports to OpenHtmlToPdf, as we do some custom graphs in some reports.
- Loading branch information
Showing
8 changed files
with
247 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/DefaultObjectDrawerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.openhtmltopdf.render; | ||
|
||
import com.openhtmltopdf.extend.FSObjectDrawer; | ||
import com.openhtmltopdf.extend.FSObjectDrawerFactory; | ||
import org.w3c.dom.Element; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Default FSObjectDrawer factory, which allows to register drawer for specified | ||
* content type | ||
*/ | ||
public class DefaultObjectDrawerFactory implements FSObjectDrawerFactory { | ||
|
||
/** | ||
* Maps content type => Drawer | ||
*/ | ||
private final Map<String, FSObjectDrawer> drawerMap = new HashMap<String, FSObjectDrawer>(); | ||
|
||
@Override | ||
public FSObjectDrawer createDrawer(Element e) { | ||
return drawerMap.get(e.getAttribute("type")); | ||
} | ||
|
||
/** | ||
* @param contentType the content type this drawer is for | ||
* @param drawer Drawer | ||
*/ | ||
public void registerDrawer(String contentType, FSObjectDrawer drawer) { | ||
drawerMap.put(contentType,drawer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
openhtmltopdf-examples/src/main/resources/testcases/custom-objects.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<html> | ||
<head> | ||
<style> | ||
object { | ||
border: 2px solid black; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<h1>Some binary trees</h1> | ||
|
||
<object type="custom/binary-tree" data-fanout="3" data-depth="5" data-angle="20" | ||
style="width:200px; height:300px;"> | ||
<!-- 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;"> | ||
<!-- This is a simple example for a custom object drawer --> | ||
</object> | ||
</body> | ||
</html> |
74 changes: 74 additions & 0 deletions
74
...f-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxObjectDrawerReplacedElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.openhtmltopdf.pdfboxout; | ||
|
||
import com.openhtmltopdf.extend.FSObjectDrawer; | ||
import com.openhtmltopdf.layout.LayoutContext; | ||
import com.openhtmltopdf.render.BlockBox; | ||
import com.openhtmltopdf.render.RenderingContext; | ||
import org.w3c.dom.Element; | ||
|
||
import java.awt.*; | ||
|
||
/** | ||
* FSObjectDrawer Element for PDFBox | ||
*/ | ||
public class PdfBoxObjectDrawerReplacedElement implements PdfBoxReplacedElement { | ||
private final Element e; | ||
private Point point = new Point(0, 0); | ||
private final FSObjectDrawer drawer; | ||
private final int width; | ||
private final int height; | ||
private final int dotsPerPixel; | ||
|
||
public PdfBoxObjectDrawerReplacedElement(Element e, FSObjectDrawer drawer, int cssWidth, int cssHeight, | ||
int dotsPerPixel) { | ||
this.e = e; | ||
this.drawer = drawer; | ||
this.width = cssWidth; | ||
this.height = cssHeight; | ||
this.dotsPerPixel = dotsPerPixel; | ||
} | ||
|
||
@Override | ||
public int getIntrinsicWidth() { | ||
return this.width; | ||
} | ||
|
||
@Override | ||
public int getIntrinsicHeight() { | ||
return this.height; | ||
} | ||
|
||
@Override | ||
public Point getLocation() { | ||
return point; | ||
} | ||
|
||
@Override | ||
public void setLocation(int x, int y) { | ||
point.setLocation(x, y); | ||
} | ||
|
||
@Override | ||
public void detach(LayoutContext c) { | ||
} | ||
|
||
@Override | ||
public boolean isRequiresInteractivePaint() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean hasBaseline() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public int getBaseline() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void paint(RenderingContext c, PdfBoxOutputDevice outputDevice, BlockBox box) { | ||
drawer.drawObject(e, point.getX(), point.getY(), getIntrinsicWidth(), getIntrinsicHeight(), outputDevice, c, dotsPerPixel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.