-
Notifications
You must be signed in to change notification settings - Fork 97
/
PostScriptRenderer.java
298 lines (270 loc) · 13.4 KB
/
PostScriptRenderer.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
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright 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.Doubles.roughlyEqual;
import static uk.org.okapibarcode.util.Integers.normalizeRotation;
import java.io.IOException;
import java.io.OutputStream;
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 EPS (Encapsulated PostScript).
*
* @author <a href="mailto:[email protected]">Robin Stuart</a>
* @author Daniel Gredler
*/
public class PostScriptRenderer 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;
/** The clockwise rotation of the symbol in degrees. */
private final int rotation;
/**
* Creates a new PostScript 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
*/
public PostScriptRenderer(OutputStream out, double magnification, Color paper, Color ink) {
this(out, magnification, paper, ink, 0);
}
/**
* Creates a new PostScript 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 rotation the clockwise rotation of the symbol in degrees (must be a multiple of 90)
*/
public PostScriptRenderer(OutputStream out, double magnification, Color paper, Color ink, int rotation) {
this.out = out;
this.magnification = magnification;
this.paper = paper;
this.ink = ink;
this.rotation = normalizeRotation(rotation);
}
/** {@inheritDoc} */
@Override
public void render(Symbol symbol) throws IOException {
// All y dimensions are reversed because EPS origin (0,0) is at the bottom left, not top left
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);
int rotatedHeight = height;
int rotatedWidth = width;
switch (rotation) {
case 90:
case 270:
rotatedHeight = width;
rotatedWidth = height;
break;
default:
break;
}
String title;
if (content.isEmpty()) {
title = "OkapiBarcode Generated Symbol";
} else {
title = content;
}
try (ExtendedOutputStreamWriter writer = new ExtendedOutputStreamWriter(out, "%.2f")) {
// Header
writer.append("%!PS-Adobe-3.0 EPSF-3.0\n");
writer.append("%%Creator: OkapiBarcode\n");
writer.append("%%Title: ").append(title).append('\n');
writer.append("%%Pages: 0\n");
writer.append("%%BoundingBox: 0 0 ").appendInt(rotatedWidth).append(" ").appendInt(rotatedHeight).append("\n");
writer.append("%%EndComments\n");
// Definitions
writer.append("/TL { setlinewidth moveto lineto stroke } bind def\n");
writer.append("/TC { moveto 0 360 arc 360 0 arcn fill } bind def\n");
writer.append("/TH { 0 setlinewidth moveto lineto lineto lineto lineto lineto closepath fill } bind def\n");
writer.append("/TB { 2 copy } bind def\n");
writer.append("/TR { newpath 4 1 roll exch moveto 1 index 0 rlineto 0 exch rlineto neg 0 rlineto closepath fill } bind def\n");
writer.append("/TE { pop pop } bind def\n");
// Set orientation
switch (rotation) {
case 90:
writer.append("gsave\n")
.append(-rotation).append(" rotate\n")
.append(-width).append(" 0.00 translate\n");
break;
case 180:
writer.append("gsave\n")
.append(-rotation).append(" rotate\n")
.append(-width).append(" ").append(-height).append(" translate\n");
break;
case 270:
writer.append("gsave\n")
.append(-rotation).append(" rotate\n")
.append("0.00 ").append(-height).append(" translate\n");
break;
default:
break;
}
// Background
writer.append("newpath\n");
writer.append(ink.red / 255.0).append(" ")
.append(ink.green / 255.0).append(" ")
.append(ink.blue / 255.0).append(" setrgbcolor\n");
writer.append(paper.red / 255.0).append(" ")
.append(paper.green / 255.0).append(" ")
.append(paper.blue / 255.0).append(" setrgbcolor\n");
writer.append(height).append(" 0.00 TB 0.00 ").append(width).append(" TR\n");
// Rectangles
for (int i = 0; i < symbol.getRectangles().size(); i++) {
Rectangle rect = symbol.getRectangles().get(i);
if (i == 0) {
writer.append("TE\n");
writer.append(ink.red / 255.0).append(" ")
.append(ink.green / 255.0).append(" ")
.append(ink.blue / 255.0).append(" setrgbcolor\n");
writer.append(rect.height * magnification).append(" ")
.append(height - ((rect.y + rect.height) * magnification) - marginY).append(" TB ")
.append((rect.x * magnification) + marginX).append(" ")
.append(rect.width * magnification).append(" TR\n");
} else {
Rectangle prev = symbol.getRectangles().get(i - 1);
if (!roughlyEqual(rect.height, prev.height) || !roughlyEqual(rect.y, prev.y)) {
writer.append("TE\n");
writer.append(ink.red / 255.0).append(" ")
.append(ink.green / 255.0).append(" ")
.append(ink.blue / 255.0).append(" setrgbcolor\n");
writer.append(rect.height * magnification).append(" ")
.append(height - ((rect.y + rect.height) * magnification) - marginY).append(" ");
}
writer.append("TB ").append((rect.x * magnification) + marginX).append(" ").append(rect.width * magnification).append(" TR\n");
}
}
// Text
for (int i = 0; i < symbol.getTexts().size(); i++) {
TextBox text = symbol.getTexts().get(i);
TextAlignment alignment = (text.alignment == JUSTIFY && text.text.length() == 1 ? CENTER : text.alignment);
if (i == 0) {
writer.append("TE\n");
writer.append(ink.red / 255.0).append(" ")
.append(ink.green / 255.0).append(" ")
.append(ink.blue / 255.0).append(" setrgbcolor\n");
}
writer.append("matrix currentmatrix\n");
writer.append("/").append(symbol.getFontName()).append(" findfont\n");
writer.append(symbol.getFontSize() * magnification).append(" scalefont setfont\n");
double y = height - (text.y * magnification) - marginY;
switch (alignment) {
case LEFT:
double leftX = (magnification * text.x) + marginX;
writer.append(" 0 0 moveto ").append(leftX).append(" ").append(y)
.append(" translate 0.00 rotate 0 0 moveto\n");
writer.append(" (").append(text.text).append(") show\n");
break;
case JUSTIFY:
double textX = (magnification * text.x) + marginX;
double textW = (magnification * text.width);
writer.append(" 0 0 moveto ").append(textX).append(" ").append(y)
.append(" translate 0.00 rotate 0 0 moveto\n");
writer.append(" (").append(text.text).append(") dup stringwidth pop ")
.append(textW).append(" sub neg 1 index length 1 sub div 0")
.append(" 3 -1 roll ashow\n");
break;
case RIGHT:
double rightX = (magnification * text.x) + (magnification * text.width) + marginX;
writer.append(" 0 0 moveto ").append(rightX).append(" ").append(y)
.append(" translate 0.00 rotate 0 0 moveto\n");
writer.append(" (").append(text.text).append(") stringwidth\n");
writer.append("pop\n");
writer.append("-1 mul 0 rmoveto\n");
writer.append(" (").append(text.text).append(") show\n");
break;
case CENTER:
double centerX = (magnification * text.x) + (magnification * text.width / 2) + marginX;
writer.append(" 0 0 moveto ").append(centerX).append(" ").append(y)
.append(" translate 0.00 rotate 0 0 moveto\n");
writer.append(" (").append(text.text).append(") stringwidth\n");
writer.append("pop\n");
writer.append("-2 div 0 rmoveto\n");
writer.append(" (").append(text.text).append(") show\n");
break;
default:
throw new OkapiInternalException("Unknown alignment: " + alignment);
}
writer.append("setmatrix\n");
}
// Circles
// Because MaxiCode size is fixed, this ignores magnification
for (int i = 0; i < symbol.getTarget().size(); i += 2) {
Circle circle1 = symbol.getTarget().get(i);
Circle circle2 = symbol.getTarget().get(i + 1);
if (i == 0) {
writer.append("TE\n");
writer.append(ink.red / 255.0).append(" ")
.append(ink.green / 255.0).append(" ")
.append(ink.blue / 255.0).append(" setrgbcolor\n");
writer.append(ink.red / 255.0).append(" ")
.append(ink.green / 255.0).append(" ")
.append(ink.blue / 255.0).append(" setrgbcolor\n");
}
double x1 = circle1.centreX;
double x2 = circle2.centreX;
double y1 = height - circle1.centreY;
double y2 = height - circle2.centreY;
double r1 = circle1.radius;
double r2 = circle2.radius;
writer.append(x1 + marginX)
.append(" ").append(y1 - marginY)
.append(" ").append(r1)
.append(" ").append(x2 + marginX)
.append(" ").append(y2 - marginY)
.append(" ").append(r2)
.append(" ").append(x2 + r2 + marginX)
.append(" ").append(y2 - marginY)
.append(" TC\n");
}
// Hexagons
// Because MaxiCode size is fixed, this ignores magnification
for (Hexagon hexagon : symbol.getHexagons()) {
for (int j = 0; j < 6; j++) {
writer.append(hexagon.getX(j) + marginX).append(" ")
.append((height - hexagon.getY(j)) - marginY).append(" ");
}
writer.append(" TH\n");
}
// Restore original transformation if rotated
if (rotation != 0) {
writer.append("grestore\n");
}
// Footer
writer.append("\nshowpage\n");
}
}
}