1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid;
20
21 import java.awt.Graphics2D;
22 import java.awt.RenderingHints;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import net.sourceforge.jeuclid.elements.generic.DocumentElement;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class MathBase {
43
44
45
46
47
48
49
50 public static final int INLINE = 0;
51
52
53
54
55 public static final int DISPLAY = 1;
56
57
58
59
60 public static final float DEFAULT_FONTSIZE = 12.0f;
61
62
63 public static final String TRUE = Boolean.TRUE.toString();
64
65
66 public static final String FALSE = Boolean.FALSE.toString();
67
68
69 public static final String VALUE_ZERO = "0";
70
71
72
73
74 private DocumentElement rootElement;
75
76 private final Map<ParameterKey, String> renderParams;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public MathBase(final Map<ParameterKey, String> params) {
94 this.renderParams = MathBase.getDefaultParameters();
95 if (params != null) {
96 this.renderParams.putAll(params);
97 }
98 this.rootElement = new DocumentElement(this);
99 }
100
101
102
103
104
105
106
107
108 public float getAscender(final Graphics2D g) {
109 return (float) Math.ceil(this.rootElement.getAscentHeight(g));
110 }
111
112
113
114
115
116
117
118
119 public float getDescender(final Graphics2D g) {
120 return (float) Math.ceil(this.rootElement.getDescentHeight(g));
121 }
122
123
124
125
126
127
128
129 public void setRootElement(final DocumentElement element) {
130 if (element == null) {
131 return;
132 }
133 this.rootElement = element;
134 this.rootElement.setMathBase(this);
135 }
136
137
138
139
140 public DocumentElement getRootElement() {
141 return this.rootElement;
142 }
143
144
145
146
147
148
149
150 public void setDebug(final boolean debug) {
151 this.renderParams
152 .put(ParameterKey.DebugMode, Boolean.toString(debug));
153 }
154
155
156
157
158
159
160 public boolean isDebug() {
161 return Boolean.parseBoolean(this.renderParams
162 .get(ParameterKey.DebugMode));
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public void paint(final Graphics2D g, final float x, final float y) {
176 if (this.rootElement != null) {
177 final RenderingHints hints = g.getRenderingHints();
178 if (Boolean.parseBoolean(this.renderParams
179 .get(ParameterKey.AntiAlias))) {
180 hints.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
181 RenderingHints.VALUE_ANTIALIAS_ON));
182 }
183 hints.add(new RenderingHints(RenderingHints.KEY_STROKE_CONTROL,
184 RenderingHints.VALUE_STROKE_NORMALIZE));
185 hints.add(new RenderingHints(RenderingHints.KEY_RENDERING,
186 RenderingHints.VALUE_RENDER_QUALITY));
187 g.setRenderingHints(hints);
188 this.rootElement.paint(g, x, y
189 + (float) Math.ceil(this.rootElement.getAscentHeight(g)));
190 }
191 }
192
193
194
195
196
197
198
199
200 public void paint(final Graphics2D g) {
201 this.paint(g, 0, 0);
202 }
203
204
205
206
207
208
209
210
211 public float getWidth(final Graphics2D g) {
212 final float realWidth;
213 if (this.rootElement != null) {
214 realWidth = this.rootElement.getWidth(g);
215 } else {
216 realWidth = 0f;
217 }
218 return Math.max(1.0f, realWidth);
219 }
220
221
222
223
224
225
226
227
228 public float getHeight(final Graphics2D g) {
229 final float realHeight;
230 if (this.rootElement != null) {
231 realHeight = (float) (Math.ceil(this.rootElement
232 .getAscentHeight(g)) + Math.ceil(this.rootElement
233 .getDescentHeight(g)));
234 } else {
235 realHeight = 0f;
236 }
237 return Math.max(1.0f, realHeight);
238 }
239
240
241
242
243 public float getFontSize() {
244 return Float.parseFloat(this.getParams().get(ParameterKey.FontSize));
245 }
246
247
248
249
250
251
252
253
254
255
256 public Map<ParameterKey, String> getParams() {
257 return this.renderParams;
258 }
259
260
261
262
263
264
265
266
267
268 public void setParam(final ParameterKey key, final String value) {
269 this.renderParams.put(key, value);
270 this.rootElement.fireChangeForSubTree();
271 }
272
273
274
275
276
277
278 public static Map<ParameterKey, String> getDefaultParameters() {
279 final Map<ParameterKey, String> params = new HashMap<ParameterKey, String>();
280 params.put(ParameterKey.FontSize, Float
281 .toString(MathBase.DEFAULT_FONTSIZE));
282 params.put(ParameterKey.DebugMode, MathBase.FALSE);
283 params.put(ParameterKey.OutFileType, "image/png");
284 params.put(ParameterKey.AntiAlias, MathBase.TRUE);
285 params.put(ParameterKey.ForegroundColor, "black");
286 params.put(ParameterKey.BackgroundColor, "transparent");
287
288
289 final String symbolCatchFonts = "OpenSymbol," + "Standard Symbols L,"
290 + "Symbol," + "Webdings," + "Wingdings," + "Wingdings 2,"
291 + "Wingdings 3," + "Arial Unicode MS," + "DejaVu Sans"
292 + "Cambria Math" + "STIXGeneral";
293 params.put(ParameterKey.FontsSanserif, "Verdana," + "Helvetica,"
294 + "Arial," + "Arial Unicode MS," + "Lucida Sans Unicode,"
295 + "Lucida Sans," + "Lucida Grande," + "DejaVu Sans,"
296 + "Bitstream Vera Sans," + "Luxi Sans," + "FreeSans,"
297 + "sansserif," + symbolCatchFonts);
298 params.put(ParameterKey.FontsSerif, "Constantina," + "Times,"
299 + "Times New Roman," + "Lucida Bright," + "DejaVu Serif,"
300 + "Bitstream Vera Serif," + "Luxi Serif," + "FreeSerif,"
301 + "serif," + symbolCatchFonts);
302 params.put(ParameterKey.FontsMonospaced, "Andale Mono," + "Courier,"
303 + "Courier Mono," + "Courier New,"
304 + "Lucida Sans Typewriter," + "DejaVu Sans Mono,"
305 + "Bitstream Vera Sans Mono," + "Luxi Mono," + "FreeMono,"
306 + "monospaced," + symbolCatchFonts);
307 params.put(ParameterKey.FontsScript, "Savoye LET,"
308 + "Brush Script MT," + "Zapfino," + "Apple Chancery,"
309 + "Edwardian Script ITC," + "Lucida Handwriting,"
310 + "Monotype Corsiva," + "Santa Fe LET," + symbolCatchFonts);
311 params
312 .put(ParameterKey.FontsFraktur, "FetteFraktur,"
313 + "Fette Fraktur," + "Euclid Fraktur,"
314 + "Lucida Blackletter," + "Blackmoor LET,"
315 + symbolCatchFonts);
316 params.put(ParameterKey.FontsDoublestruck, "Caslon Open Face,"
317 + "Caslon Openface," + "Cloister Open Face,"
318 + "Academy Engraved LET," + "Colonna MT,"
319 + "Imprint MT Shadow," + symbolCatchFonts);
320
321 return params;
322 }
323 }