001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.typography.hershey;
031
032import java.io.IOException;
033import java.text.AttributedCharacterIterator.Attribute;
034import java.text.AttributedString;
035import java.util.HashMap;
036import java.util.Map;
037
038import org.openimaj.image.DisplayUtilities;
039import org.openimaj.image.FImage;
040import org.openimaj.image.MBFImage;
041import org.openimaj.image.colour.ColourSpace;
042import org.openimaj.image.colour.RGBColour;
043import org.openimaj.image.renderer.FImageRenderer;
044import org.openimaj.image.renderer.ImageRenderer;
045import org.openimaj.image.typography.Font;
046import org.openimaj.image.typography.FontRenderer;
047import org.openimaj.image.typography.FontStyle;
048import org.openimaj.image.typography.FontStyle.VerticalAlignment;
049import org.openimaj.math.geometry.shape.Rectangle;
050
051/**
052 * The set of Hershey's vector fonts.
053 *
054 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
055 *
056 */
057public enum HersheyFont implements Font<HersheyFont> {
058        /**
059         * Hershey Astrology
060         */
061        ASTROLOGY("astrology.jhf", "Astrology"),
062        /**
063         * Hershey Cursive
064         */
065        CURSIVE("cursive.jhf", "Cursive"),
066        /**
067         * Hershey Cyrillic 1
068         */
069        CYRILLIC_1("cyrilc_1.jhf", "Cyrillic 1"),
070        /**
071         * Hershey Cyrillic
072         */
073        CYRILLIC("cyrillic.jhf", "Cyrillic"),
074        /**
075         * Hershey Futura Light
076         */
077        FUTURA_LIGHT("futural.jhf", "Futura Light"),
078        /**
079         * Hershey Futura Medium
080         */
081        FUTURA_MEDIUM("futuram.jhf", "Futura Medium"),
082        /**
083         * Hershey Gothic English Triplex
084         */
085        GOTHIC_ENGLISH_TRIPLEX("gothgbt.jhf", "Gothic English Triplex"),
086        /**
087         * Hershey Gothic German Triplex
088         */
089        GOTHIC_GERMAN_TRIPLEX("gothgrt.jhf", "Gothic German Triplex"),
090        /**
091         * Hershey Gothic English
092         */
093        GOTHIC_ENGLISH("gothiceng.jhf", "Gothic English"),
094        /**
095         * Hershey Gothic German
096         */
097        GOTHIC_GERMAN("gothicger.jhf", "Gothic German"),
098        /**
099         * 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}