-
Notifications
You must be signed in to change notification settings - Fork 97
/
SvgRenderer.java
286 lines (254 loc) · 11.9 KB
/
SvgRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright 2014-2015 Robin Stuart, Daniel Gredler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.org.okapibarcode.output;
import static uk.org.okapibarcode.graphics.TextAlignment.CENTER;
import static uk.org.okapibarcode.graphics.TextAlignment.JUSTIFY;
import static uk.org.okapibarcode.util.Integers.normalizeRotation;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
import uk.org.okapibarcode.backend.OkapiInternalException;
import uk.org.okapibarcode.backend.Symbol;
import uk.org.okapibarcode.graphics.Circle;
import uk.org.okapibarcode.graphics.Color;
import uk.org.okapibarcode.graphics.Hexagon;
import uk.org.okapibarcode.graphics.Rectangle;
import uk.org.okapibarcode.graphics.TextAlignment;
import uk.org.okapibarcode.graphics.TextBox;
/**
* Renders symbologies to SVG (Scalable Vector Graphics).
*
* @author <a href="mailto:[email protected]">Robin Stuart</a>
* @author Daniel Gredler
*/
public class SvgRenderer implements SymbolRenderer {
/** The output stream to render to. */
private final OutputStream out;
/** The magnification factor to apply. */
private final double magnification;
/** The paper (background) color. */
private final Color paper;
/** The ink (foreground) color. */
private final Color ink;
/** Whether or not to include the XML prolog in the output. */
private final boolean xmlProlog;
/** The clockwise rotation of the symbol in degrees. */
private final int rotation;
/**
* Creates a new SVG renderer.
*
* @param out the output stream to render to
* @param magnification the magnification factor to apply
* @param paper the paper (background) color
* @param ink the ink (foreground) color
* @param xmlProlog whether or not to include the XML prolog in the output (usually {@code true} for
* standalone SVG documents, {@code false} for SVG content embedded directly in HTML documents)
*/
public SvgRenderer(OutputStream out, double magnification, Color paper, Color ink, boolean xmlProlog) {
this(out, magnification, paper, ink, xmlProlog, 0);
}
/**
* Creates a new SVG renderer.
*
* @param out the output stream to render to
* @param magnification the magnification factor to apply
* @param paper the paper (background) color
* @param ink the ink (foreground) color
* @param xmlProlog whether or not to include the XML prolog in the output (usually {@code true} for
* standalone SVG documents, {@code false} for SVG content embedded directly in HTML documents)
* @param rotation the clockwise rotation of the symbol in degrees (must be a multiple of 90)
*/
public SvgRenderer(OutputStream out, double magnification, Color paper, Color ink, boolean xmlProlog, int rotation) {
this.out = out;
this.magnification = magnification;
this.paper = paper;
this.ink = ink;
this.xmlProlog = xmlProlog;
this.rotation = normalizeRotation(rotation);
}
/** {@inheritDoc} */
@Override
public void render(Symbol symbol) throws IOException {
String content = symbol.getContent();
int width = (int) Math.ceil(symbol.getWidth() * magnification);
int height = (int) Math.ceil(symbol.getHeight() * magnification);
int marginX = (int) (symbol.getQuietZoneHorizontal() * magnification);
int marginY = (int) (symbol.getQuietZoneVertical() * magnification);
String title;
if (content.isEmpty()) {
title = "OkapiBarcode Generated Symbol";
} else {
title = content;
}
String fgColour = String.format("%02X", ink.red)
+ String.format("%02X", ink.green)
+ String.format("%02X", ink.blue);
String bgColour = String.format("%02X", paper.red)
+ String.format("%02X", paper.green)
+ String.format("%02X", paper.blue);
try (ExtendedOutputStreamWriter writer = new ExtendedOutputStreamWriter(out, "%.2f")) {
// XML Prolog
if(xmlProlog) {
writer.append("<?xml version=\"1.0\" standalone=\"no\"?>\n");
writer.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
writer.append(" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
}
// Rotation
int rotatedHeight = height;
int rotatedWidth = width;
String transform;
switch (rotation) {
case 90:
rotatedHeight = width;
rotatedWidth = height;
transform = " transform=\"rotate(" + rotation + ") translate(0,-" + rotatedWidth + ")\"";
break;
case 180:
transform = " transform=\"rotate(" + rotation + "," + (width / 2) + "," + (height / 2) + ")\"";
break;
case 270:
rotatedHeight = width;
rotatedWidth = height;
transform = " transform=\"rotate(" + rotation + ") translate(-" + rotatedHeight + ",0)\"";
break;
default:
transform = "";
break;
}
// Header
writer.append("<svg width=\"").appendInt(rotatedWidth)
.append("\" height=\"").appendInt(rotatedHeight)
.append("\" version=\"1.1")
.append("\" xmlns=\"http://www.w3.org/2000/svg\">\n");
writer.append(" <desc>").append(clean(title)).append("</desc>\n");
writer.append(" <g id=\"barcode\" fill=\"#").append(fgColour).append("\"").append(transform).append(">\n");
writer.append(" <rect x=\"0\" y=\"0\" width=\"").appendInt(width)
.append("\" height=\"").appendInt(height)
.append("\" fill=\"#").append(bgColour).append("\" />\n");
// Rectangles
for (Rectangle rect : symbol.getRectangles()) {
writer.append(" <rect x=\"").append((rect.x * magnification) + marginX)
.append("\" y=\"").append((rect.y * magnification) + marginY)
.append("\" width=\"").append(rect.width * magnification)
.append("\" height=\"").append(rect.height * magnification)
.append("\" />\n");
}
// Text
for (TextBox text : symbol.getTexts()) {
TextAlignment alignment = (text.alignment == JUSTIFY && text.text.length() == 1 ? CENTER : text.alignment);
double x;
String anchor;
switch (alignment) {
case LEFT:
case JUSTIFY:
x = (magnification * text.x) + marginX;
anchor = "start";
break;
case RIGHT:
x = (magnification * text.x) + (magnification * text.width) + marginX;
anchor = "end";
break;
case CENTER:
x = (magnification * text.x) + (magnification * text.width / 2) + marginX;
anchor = "middle";
break;
default:
throw new OkapiInternalException("Unknown alignment: " + alignment);
}
writer.append(" <text x=\"").append(x)
.append("\" y=\"").append((text.y * magnification) + marginY)
.append("\" text-anchor=\"").append(anchor).append("\"\n");
if (alignment == JUSTIFY) {
writer.append(" textLength=\"")
.append(text.width * magnification)
.append("\" lengthAdjust=\"spacing\"\n");
}
writer.append(" font-family=\"").append(clean(symbol.getFontName()))
.append("\" font-size=\"").append(symbol.getFontSize() * magnification)
.append("\" fill=\"#").append(fgColour).append("\">\n");
writer.append(" ").append(clean(text.text)).append("\n");
writer.append(" </text>\n");
}
// Circles
for (int i = 0; i < symbol.getTarget().size(); i++) {
Circle circle = symbol.getTarget().get(i);
String color;
if ((i & 1) == 0) {
color = fgColour;
} else {
color = bgColour;
}
writer.append(" <circle cx=\"").append((circle.centreX * magnification) + marginX)
.append("\" cy=\"").append((circle.centreY * magnification) + marginY)
.append("\" r=\"").append(circle.radius * magnification)
.append("\" fill=\"#").append(color).append("\" />\n");
}
// Hexagons
for (Hexagon hexagon : symbol.getHexagons()) {
writer.append(" <path d=\"");
for (int j = 0; j < 6; j++) {
if (j == 0) {
writer.append("M ");
} else {
writer.append("L ");
}
writer.append((hexagon.getX(j) * magnification) + marginX).append(" ")
.append((hexagon.getY(j) * magnification) + marginY).append(" ");
}
writer.append("Z\" />\n");
}
// Footer
writer.append(" </g>\n");
writer.append("</svg>\n");
}
}
/**
* Cleans / sanitizes the specified string for inclusion in XML. A bit convoluted, but we're
* trying to do it without adding an external dependency just for this...
*
* @param s the string to be cleaned / sanitized
* @return the cleaned / sanitized string
*/
protected String clean(String s) {
// remove control characters
s = s.replaceAll("[\u0000-\u001f]", "");
// escape XML characters
try {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Text text = document.createTextNode(s);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(text);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(source, result);
return writer.toString();
} catch (ParserConfigurationException | TransformerException | TransformerFactoryConfigurationError e) {
return s;
}
}
}