View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * The set of Hershey's vector fonts.
53   *
54   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
55   *
56   */
57  public enum HersheyFont implements Font<HersheyFont> {
58  	/**
59  	 * Hershey Astrology
60  	 */
61  	ASTROLOGY("astrology.jhf", "Astrology"),
62  	/**
63  	 * Hershey Cursive
64  	 */
65  	CURSIVE("cursive.jhf", "Cursive"),
66  	/**
67  	 * Hershey Cyrillic 1
68  	 */
69  	CYRILLIC_1("cyrilc_1.jhf", "Cyrillic 1"),
70  	/**
71  	 * Hershey Cyrillic
72  	 */
73  	CYRILLIC("cyrillic.jhf", "Cyrillic"),
74  	/**
75  	 * Hershey Futura Light
76  	 */
77  	FUTURA_LIGHT("futural.jhf", "Futura Light"),
78  	/**
79  	 * Hershey Futura Medium
80  	 */
81  	FUTURA_MEDIUM("futuram.jhf", "Futura Medium"),
82  	/**
83  	 * Hershey Gothic English Triplex
84  	 */
85  	GOTHIC_ENGLISH_TRIPLEX("gothgbt.jhf", "Gothic English Triplex"),
86  	/**
87  	 * Hershey Gothic German Triplex
88  	 */
89  	GOTHIC_GERMAN_TRIPLEX("gothgrt.jhf", "Gothic German Triplex"),
90  	/**
91  	 * Hershey Gothic English
92  	 */
93  	GOTHIC_ENGLISH("gothiceng.jhf", "Gothic English"),
94  	/**
95  	 * Hershey Gothic German
96  	 */
97  	GOTHIC_GERMAN("gothicger.jhf", "Gothic German"),
98  	/**
99  	 * Hershey Gothic Italian
100 	 */
101 	GOTHIC_ITALIAN("gothicita.jhf", "Gothic Italian"),
102 	/**
103 	 * Hershey Gothic Italian Triplex
104 	 */
105 	GOTHIC_ITALIAN_TRIPLEX("gothitt.jhf", "Gothic Italian Triplex"),
106 	/**
107 	 * Hershey Greek
108 	 */
109 	GREEK("greek.jhf", "Greek"),
110 	/**
111 	 * Hershey Greek Complex
112 	 */
113 	GREEK_COMPLEX("greekc.jhf", "Greek Complex"),
114 	/**
115 	 * Hershey Greek Simplex
116 	 */
117 	GREEK_SIMPLEX("greeks.jhf", "Greeks Simplex"),
118 	/**
119 	 * Hershey Japanese
120 	 */
121 	JAPANESE("japanese.jhf", "Japanese"),
122 	/**
123 	 * Hershey Markers
124 	 */
125 	MARKERS("markers.jhf", "Markers"),
126 	/**
127 	 * Hershey Math Lower
128 	 */
129 	MATH_LOWER("mathlow.jhf", "Math Lower"),
130 	/**
131 	 * Hershey Math Upper
132 	 */
133 	MATH_UPPER("mathupp.jhf", "Math Upper"),
134 	/**
135 	 * Hershey Meteorology
136 	 */
137 	METEOROLOGY("meteorology.jhf", "Meteorology"),
138 	/**
139 	 * Hershey Music
140 	 */
141 	MUSIC("music.jhf", "Music"),
142 	/**
143 	 * Hershey Roman Duplex
144 	 */
145 	ROMAN_DUPLEX("rowmand.jhf", "Roman Duplex"),
146 	/**
147 	 * Hershey Roman Simplex
148 	 */
149 	ROMAN_SIMPLEX("rowmans.jhf", "Roman Simplex"),
150 	/**
151 	 * Hershey Roman Triplex
152 	 */
153 	ROMAN_TRIPLEX("rowmant.jhf", "Roman Triplex"),
154 	/**
155 	 * Hershey Script Complex
156 	 */
157 	SCRIPT_COMPLEX("scriptc.jhf", "Script Complex"),
158 	/**
159 	 * Hershey Script Simplex
160 	 */
161 	SCRIPT_SIMPLEX("scripts.jhf", "Script Simplex"),
162 	/**
163 	 * Hershey Symbolic
164 	 */
165 	SYMBOLIC("symbolic.jhf", "Symbolic"),
166 	/**
167 	 * Hershey Greek
168 	 */
169 	TIMES_GREEK("timesg.jhf", "Times Greek"),
170 	/**
171 	 * Hershey Times Medium Italic
172 	 */
173 	TIMES_MEDIUM_ITALIC("timesi.jhf", "Times Medium Italic"),
174 	/**
175 	 * Hershey Times Bold Italic
176 	 */
177 	TIMES_BOLD_ITALIC("timesib.jhf", "Times Bold Italic"),
178 	/**
179 	 * Hershey Times Medium
180 	 */
181 	TIMES_MEDIUM("timesr.jhf", "Times Medium"),
182 	/**
183 	 * Hershey Times Bold
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 	 * Demonstrate the font engine
218 	 * @param args not used
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 }