-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from rototor/custom-object-drawers
Custom object drawers
- Loading branch information
Showing
9 changed files
with
271 additions
and
57 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
openhtmltopdf-core/src/main/java/com/openhtmltopdf/extend/FSObjectDrawer.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,12 @@ | ||
package com.openhtmltopdf.extend; | ||
|
||
import com.openhtmltopdf.render.RenderingContext; | ||
import org.w3c.dom.Element; | ||
|
||
/** | ||
* Handle the drawing of <object> tags | ||
*/ | ||
public interface FSObjectDrawer { | ||
void drawObject(Element e, double x, double y, double width, double height, OutputDevice outputDevice, | ||
RenderingContext ctx, int dotsPerPixel); | ||
} |
14 changes: 14 additions & 0 deletions
14
openhtmltopdf-core/src/main/java/com/openhtmltopdf/extend/FSObjectDrawerFactory.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,14 @@ | ||
package com.openhtmltopdf.extend; | ||
|
||
import org.w3c.dom.Element; | ||
|
||
/** | ||
* Factory for ObjectDrawers, i.e. classes which draw <object> tags | ||
*/ | ||
public interface FSObjectDrawerFactory { | ||
|
||
/** | ||
* Determine an object drawer for the given object tag element. | ||
*/ | ||
FSObjectDrawer createDrawer(Element e); | ||
} |
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
Oops, something went wrong.