1 package de.japrost.excerpt;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.Rectangle;
6 import java.awt.RenderingHints;
7 import java.awt.image.BufferedImage;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Properties;
11 import java.util.StringTokenizer;
12
13
14
15
16
17
18 public class ImageMaker {
19
20 private Properties props;
21
22 private boolean debug = true;
23
24 private int width = 800;
25 private int height = 600;
26 private int sizeBase = 48;
27
28 private int marginRight = 5;
29 private int marginBottom = 5;
30 private int marginLeft = 5;
31 private int marginTop = 5;
32 private String entries = "";
33
34 double spacerHight;
35
36 BufferedImage createImage() {
37
38
39 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
40 img.createGraphics();
41 Graphics2D g = (Graphics2D)img.getGraphics();
42 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
43 List<RenderLine> renderLines = new ArrayList<RenderLine>();
44
45 debug = Boolean.parseBoolean(props.getProperty("debug"));
46 PropertyTextBlockBuilder ptb = new PropertyTextBlockBuilder(g, this);
47 PropertySpacerBuilder psb = new PropertySpacerBuilder();
48 PropertyDecoratorBuilder pdb = new PropertyDecoratorBuilder(g, this);
49 PropertyFixedSpaceBuilder pfb = new PropertyFixedSpaceBuilder();
50
51 StringTokenizer st = new StringTokenizer(entries, ",");
52
53 while (st.hasMoreTokens()) {
54 String entry = st.nextToken().trim();
55 System.out.println("'" + entry + "'");
56 if (entry.startsWith("t.")) {
57
58 renderLines.add(ptb.buildTextBlock("textblock", entry.substring(2), props));
59 } else if (entry.startsWith("s.")) {
60
61 renderLines.add(psb.buildSpacer("spacer." + entry.substring(2) + ".", props));
62 } else if (entry.startsWith("f.")) {
63
64 renderLines.add(pfb.buildFixedSpace("fixedspace." + entry.substring(2) + ".", props));
65 } else if (entry.startsWith("d.")) {
66
67 renderLines.add(pdb.buildDecorator("decorator." + entry.substring(2) + ".", props));
68 } else {
69 System.out.println("Whatz to do wiz '" + entry + "'?");
70 }
71 }
72 spacerHight = calculateSpacerHeight(renderLines);
73
74 int topSpace = marginTop;
75 g.setColor(Color.white);
76 if (debug) {
77 debugDrawMargin(g);
78 }
79 if (debug) {
80 debugDrawCross(g);
81 }
82 for (RenderLine renderLine : renderLines) {
83 Rectangle borders = new Rectangle();
84
85 int boxHight = renderLine.height() + (int)(spacerHight * renderLine.expand());
86
87 int boxWidth = width - marginLeft - marginRight;
88
89 int boxUpperLeftX = marginLeft;
90 int boxUpperLeftY = topSpace;
91
92 borders.setBounds(boxUpperLeftX, boxUpperLeftY, boxWidth, boxHight);
93 renderLine.render(borders);
94 if (debug) {
95 debugDrawBox(g, borders, renderLine);
96 }
97
98 topSpace = advanceTopSpace(topSpace, renderLine);
99 }
100 return img;
101 }
102
103 public int getPageWidth() {
104 return width - marginLeft - marginRight;
105 }
106
107 private double calculateSpacerHeight(final List<RenderLine> renderLines) {
108 System.out.println("Calculating SpacerHeigth");
109 int textHeigth = 0;
110 int spacers = 0;
111 for (RenderLine renderLine : renderLines) {
112 int height = renderLine.height();
113 if (height > 0) {
114 textHeigth += height;
115 }
116 spacers += renderLine.expand();
117 }
118 int pageHeight = height - marginTop - marginBottom;
119 int heightSpace = pageHeight - textHeigth;
120 System.out.println("-> Spacer PageHeight=" + pageHeight + " TextHeight=" + textHeigth);
121 if (spacers == 0) {
122 return 0;
123 }
124 System.out.println("-> SpacerHeigth:" + ((double)heightSpace / spacers));
125 return (double)heightSpace / spacers;
126
127 }
128
129 private int advanceTopSpace(final int topSpace, final RenderLine renderLine) {
130 return topSpace + renderLine.height() + (int)(spacerHight * renderLine.expand());
131 }
132
133 private void debugDrawBox(final Graphics2D g, final Rectangle box, final RenderLine renderLine) {
134
135
136 Color c = g.getColor();
137 String addon = "UKN";
138 if (renderLine instanceof TextBlock) {
139 g.setColor(Color.black);
140 addon = "TB" + ((TextBlock)renderLine).getText();
141 } else if (renderLine instanceof Spacer) {
142 g.setColor(Color.pink);
143 addon = "SP";
144 } else if (renderLine instanceof Decorator) {
145 g.setColor(Color.yellow);
146 addon = "DE";
147 } else if (renderLine instanceof FixedSpace) {
148 g.setColor(Color.blue);
149 addon = "FS";
150 } else {
151 g.setColor(Color.magenta);
152 addon = "UN";
153 }
154 g.drawRect(box.x, box.y, box.width, box.height);
155
156 g.setColor(Color.white);
157 g.drawString(box.toString() + " " + addon, box.x + 10, box.y);
158 g.setColor(c);
159
160 }
161
162 private void debugDrawCross(final Graphics2D g) {
163 Color c = g.getColor();
164 g.setColor(Color.red);
165 g.drawLine(0, height / 2, width, height / 2);
166 g.drawLine(width / 2, 0, width / 2, height);
167 g.setColor(c);
168
169 }
170
171 private void debugDrawMargin(final Graphics2D g) {
172 Color c = g.getColor();
173 g.setColor(Color.green);
174 g.drawRect(marginLeft, marginTop, getPageWidth(), height - marginBottom - marginTop);
175 g.setColor(c);
176
177 }
178
179 public int getWidth() {
180 return width;
181 }
182
183 public void setWidth(final int width) {
184 this.width = width;
185 }
186
187 public int getHeight() {
188 return height;
189 }
190
191 public void setHeight(final int height) {
192 this.height = height;
193 }
194
195 public int getSizeBase() {
196 return sizeBase;
197 }
198
199 public void setSizeBase(final int sizeBase) {
200 this.sizeBase = sizeBase;
201 }
202
203 public int getMarginRight() {
204 return marginRight;
205 }
206
207 public void setMarginRight(final int marginRight) {
208 this.marginRight = marginRight;
209 }
210
211 public int getMarginBottom() {
212 return marginBottom;
213 }
214
215 public void setMarginBottom(final int marginBottom) {
216 this.marginBottom = marginBottom;
217 }
218
219 public int getMarginLeft() {
220 return marginLeft;
221 }
222
223 public void setMarginLeft(final int marginLeft) {
224 this.marginLeft = marginLeft;
225 }
226
227 public int getMarginTop() {
228 return marginTop;
229 }
230
231 public void setMarginTop(final int marginTop) {
232 this.marginTop = marginTop;
233 }
234
235 public void setProps(final Properties props) {
236 this.props = props;
237 }
238
239 public String getEntries() {
240 return entries;
241 }
242
243 public void setEntries(final String entries) {
244 this.entries = entries;
245 }
246
247 }