| 1 |
package org.kravemir.svg.labels.rendering; |
| 2 |
|
| 3 |
import org.apache.batik.anim.dom.SVGDOMImplementation; |
| 4 |
import org.kravemir.svg.labels.model.DocumentRenderOptions; |
| 5 |
import org.kravemir.svg.labels.model.TiledPaper; |
| 6 |
import org.kravemir.svg.labels.utils.RenderingUtils; |
| 7 |
import org.w3c.dom.DOMImplementation; |
| 8 |
import org.w3c.dom.Element; |
| 9 |
import org.w3c.dom.svg.SVGDocument; |
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.Collection; |
| 13 |
import java.util.List; |
| 14 |
|
| 15 |
public class LabelDocumentBuilder { |
| 16 |
private final DocumentRenderOptions options; |
| 17 |
|
| 18 |
private final TilePositionGenerator positionGenerator; |
| 19 |
private final List<SVGDocument> documents; |
| 20 |
private SVGDocument document; |
| 21 |
|
| 22 |
public LabelDocumentBuilder(TiledPaper p, DocumentRenderOptions options) { |
| 23 |
this.options = options; |
| 24 |
|
| 25 |
positionGenerator = new TilePositionGenerator(p); |
| 26 |
documents = new ArrayList<>(); |
| 27 |
} |
| 28 |
|
| 29 |
private void startDocument() { |
| 30 |
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); |
| 31 |
document = (SVGDocument) impl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null); |
| 32 |
Element root = document.getDocumentElement(); |
| 33 |
|
| 34 |
//set page size |
| 35 |
root.setAttributeNS(null, "width", RenderingUtils.length(getPaperWidth())); |
| 36 |
root.setAttributeNS(null, "height", RenderingUtils.length(getPaperHeight())); |
| 37 |
//root.setAttributeNS(null, "viewBox", "0 0 " + lw.getValueAsString() + " " + lh.getValueAsString()); |
| 38 |
|
| 39 |
if (options.getRenderPageBorders()) |
| 40 |
root.appendChild(RenderingUtils.createRect(document, 0, 0, getPaperWidth(), getPaperHeight())); |
| 41 |
|
| 42 |
positionGenerator.start(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public void placeLabel(LabelTemplate template) { |
| 47 |
if (!isSpaceLeftOnCurrentPage()) { |
| 48 |
startDocument(); |
| 49 |
documents.add(document); |
| 50 |
} |
| 51 |
|
| 52 |
placeLabelClone(template); |
| 53 |
|
| 54 |
if (options.getRenderTileBorders()) |
| 55 |
renderTileBorders(); |
| 56 |
|
| 57 |
if (options.getRenderLabelBorders()) |
| 58 |
renderLabelBorders(template); |
| 59 |
|
| 60 |
positionGenerator.nextPosition(); |
| 61 |
} |
| 62 |
|
| 63 |
private void placeLabelClone(LabelTemplate template) { |
| 64 |
Element label = (Element) template.templateRoot.cloneNode(true); |
| 65 |
document.adoptNode(label); |
| 66 |
label.setAttributeNS(null, "x", RenderingUtils.length(getX() + template.labelOffsetX)); |
| 67 |
label.setAttributeNS(null, "y", RenderingUtils.length(getY() + template.labelOffsetY)); |
| 68 |
label.setAttributeNS(null, "width", RenderingUtils.length(template.labelW)); |
| 69 |
label.setAttributeNS(null, "height", RenderingUtils.length(template.labelH)); |
| 70 |
document.getRootElement().appendChild(label); |
| 71 |
} |
| 72 |
|
| 73 |
private void renderTileBorders() { |
| 74 |
document.getRootElement().appendChild(RenderingUtils.createRect( |
| 75 |
document, |
| 76 |
getX(), |
| 77 |
getY(), |
| 78 |
positionGenerator.getTileWidth(), |
| 79 |
positionGenerator.getTileHeight()) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
private void renderLabelBorders(LabelTemplate template) { |
| 84 |
document.getRootElement().appendChild(RenderingUtils.createRect( |
| 85 |
document, |
| 86 |
getX() + template.labelOffsetX, |
| 87 |
getY() + template.labelOffsetY, |
| 88 |
template.labelW, |
| 89 |
template.labelH) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
public Collection<SVGDocument> getDocuments() { |
| 94 |
return documents; |
| 95 |
} |
| 96 |
|
| 97 |
public int getDocumentsCount() { |
| 98 |
return documents.size(); |
| 99 |
} |
| 100 |
|
| 101 |
public boolean isSpaceLeftOnCurrentPage() { |
| 102 |
return document != null && !positionGenerator.isFull(); |
| 103 |
} |
| 104 |
|
| 105 |
private double getX() { |
| 106 |
return positionGenerator.getX(); |
| 107 |
} |
| 108 |
|
| 109 |
private double getY() { |
| 110 |
return positionGenerator.getY(); |
| 111 |
} |
| 112 |
|
| 113 |
private double getPaperWidth() { |
| 114 |
return positionGenerator.getPaperWidth(); |
| 115 |
} |
| 116 |
|
| 117 |
private double getPaperHeight() { |
| 118 |
return positionGenerator.getPaperHeight(); |
| 119 |
} |
| 120 |
} |