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 package org.openimaj.image.typography.hershey;
31
32 import java.io.IOException;
33 import java.text.AttributedCharacterIterator.Attribute;
34 import java.text.AttributedString;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import org.openimaj.image.DisplayUtilities;
39 import org.openimaj.image.FImage;
40 import org.openimaj.image.MBFImage;
41 import org.openimaj.image.colour.ColourSpace;
42 import org.openimaj.image.colour.RGBColour;
43 import org.openimaj.image.renderer.FImageRenderer;
44 import org.openimaj.image.renderer.ImageRenderer;
45 import org.openimaj.image.typography.Font;
46 import org.openimaj.image.typography.FontRenderer;
47 import org.openimaj.image.typography.FontStyle;
48 import org.openimaj.image.typography.FontStyle.VerticalAlignment;
49 import org.openimaj.math.geometry.shape.Rectangle;
50
51
52
53
54
55
56
57 public enum HersheyFont implements Font<HersheyFont> {
58
59
60
61 ASTROLOGY("astrology.jhf", "Astrology"),
62
63
64
65 CURSIVE("cursive.jhf", "Cursive"),
66
67
68
69 CYRILLIC_1("cyrilc_1.jhf", "Cyrillic 1"),
70
71
72
73 CYRILLIC("cyrillic.jhf", "Cyrillic"),
74
75
76
77 FUTURA_LIGHT("futural.jhf", "Futura Light"),
78
79
80
81 FUTURA_MEDIUM("futuram.jhf", "Futura Medium"),
82
83
84
85 GOTHIC_ENGLISH_TRIPLEX("gothgbt.jhf", "Gothic English Triplex"),
86
87
88
89 GOTHIC_GERMAN_TRIPLEX("gothgrt.jhf", "Gothic German Triplex"),
90
91
92
93 GOTHIC_ENGLISH("gothiceng.jhf", "Gothic English"),
94
95
96
97 GOTHIC_GERMAN("gothicger.jhf", "Gothic German"),
98
99
100
101 GOTHIC_ITALIAN("gothicita.jhf", "Gothic Italian"),
102
103
104
105 GOTHIC_ITALIAN_TRIPLEX("gothitt.jhf", "Gothic Italian Triplex"),
106
107
108
109 GREEK("greek.jhf", "Greek"),
110
111
112
113 GREEK_COMPLEX("greekc.jhf", "Greek Complex"),
114
115
116
117 GREEK_SIMPLEX("greeks.jhf", "Greeks Simplex"),
118
119
120
121 JAPANESE("japanese.jhf", "Japanese"),
122
123
124
125 MARKERS("markers.jhf", "Markers"),
126
127
128
129 MATH_LOWER("mathlow.jhf", "Math Lower"),
130
131
132
133 MATH_UPPER("mathupp.jhf", "Math Upper"),
134
135
136
137 METEOROLOGY("meteorology.jhf", "Meteorology"),
138
139
140
141 MUSIC("music.jhf", "Music"),
142
143
144
145 ROMAN_DUPLEX("rowmand.jhf", "Roman Duplex"),
146
147
148
149 ROMAN_SIMPLEX("rowmans.jhf", "Roman Simplex"),
150
151
152
153 ROMAN_TRIPLEX("rowmant.jhf", "Roman Triplex"),
154
155
156
157 SCRIPT_COMPLEX("scriptc.jhf", "Script Complex"),
158
159
160
161 SCRIPT_SIMPLEX("scripts.jhf", "Script Simplex"),
162
163
164
165 SYMBOLIC("symbolic.jhf", "Symbolic"),
166
167
168
169 TIMES_GREEK("timesg.jhf", "Times Greek"),
170
171
172
173 TIMES_MEDIUM_ITALIC("timesi.jhf", "Times Medium Italic"),
174
175
176
177 TIMES_BOLD_ITALIC("timesib.jhf", "Times Bold Italic"),
178
179
180
181 TIMES_MEDIUM("timesr.jhf", "Times Medium"),
182
183
184
185 TIMES_BOLD("timesrb.jhf", "Times Bold"),
186 ;
187
188 protected HersheyFontData data;
189 protected String name;
190
191 private HersheyFont(final String fntName, final String name) {
192 try {
193 this.data = new HersheyFontData(fntName);
194 this.name = name;
195 } catch (final IOException e) {
196 throw new RuntimeException(e);
197 }
198 }
199
200 @SuppressWarnings("unchecked")
201 @Override
202 public <T, Q extends FontStyle<T>> FontRenderer<T, Q> getRenderer(final ImageRenderer<T,?> renderer) {
203 return (FontRenderer<T, Q>) ((Object) HersheyFontRenderer.INSTANCE);
204 }
205
206 @Override
207 public <T> HersheyFontStyle<T> createStyle(final ImageRenderer<T, ?> renderer) {
208 return new HersheyFontStyle<T>(this, renderer);
209 }
210
211 @Override
212 public String getName() {
213 return this.name;
214 }
215
216
217
218
219
220 public static void main(final String[] args) {
221 final FImage image = new FImage(500, HersheyFont.values().length * 30 + 30);
222 final FImageRenderer imRenderer = image.createRenderer();
223
224 float i = 1.5f;
225 for (final HersheyFont f : HersheyFont.values()) {
226 final FontRenderer<Float,HersheyFontStyle<Float>> renderer = f.getRenderer(imRenderer);
227 renderer.renderText(imRenderer, f.getName(), 30, (int)(i*30), f.createStyle(imRenderer));
228 final Rectangle bounds = renderer.getSize( f.getName(), f.createStyle(imRenderer) );
229 System.out.println( bounds );
230 image.drawShape( bounds, 1f );
231 i++;
232 }
233 DisplayUtilities.display(image);
234
235 final MBFImage mbfimage = new MBFImage(500,500, ColourSpace.RGB);
236 final Map<Attribute, Object> redText = new HashMap<Attribute, Object>();
237 redText.put(FontStyle.FONT, HersheyFont.TIMES_BOLD);
238 redText.put(FontStyle.COLOUR, RGBColour.RED);
239
240 final Map<Attribute, Object> cursiveText = new HashMap<Attribute, Object>();
241 cursiveText.put(FontStyle.FONT, HersheyFont.CURSIVE);
242 cursiveText.put(FontStyle.COLOUR, RGBColour.YELLOW);
243 cursiveText.put(HersheyFontStyle.HEIGHT_SCALE, 3f);
244 cursiveText.put(FontStyle.VERTICAL_ALIGNMENT, VerticalAlignment.VERTICAL_HALF);
245 cursiveText.put(HersheyFontStyle.STROKE_WIDTH, 2);
246
247 final AttributedString str = new AttributedString("hello world!");
248 str.addAttributes(redText, 4, 8);
249 str.addAttributes(cursiveText, 8, 12);
250
251 mbfimage.createRenderer().drawText(str, 150, 150);
252
253 DisplayUtilities.display(mbfimage);
254
255
256 final FImage image2 = new FImage(500,500);
257 final FImageRenderer imRenderer2 = image2.createRenderer();
258 for (i=1; i<40; i+=2) {
259 final FontRenderer<Float,HersheyFontStyle<Float>> renderer = ROMAN_TRIPLEX.getRenderer(imRenderer2);
260 final HersheyFontStyle<Float> sty = ROMAN_TRIPLEX.createStyle(imRenderer2);
261 sty.setFontSize((int)i);
262 renderer.renderText(imRenderer2, ROMAN_TRIPLEX.getName(), 30, (int)(i*30), sty);
263 }
264 DisplayUtilities.display(image2);
265 }
266 }