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.text.AttributedCharacterIterator.Attribute;
033import java.util.Map;
034
035import org.openimaj.image.renderer.ImageRenderer;
036import org.openimaj.image.typography.FontStyle;
037
038/**
039 * Style parameters for Hershey vector fonts.
040 * 
041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 *
043 * @param <T> pixel type of image
044 */
045public class HersheyFontStyle<T> extends FontStyle<T> {
046        /**
047         * Attribute for the stroke width. Value should be a number in pixels.
048         */
049        public static final Attribute STROKE_WIDTH = new FontStyleAttribute("strokeWidth");
050        
051        /**
052         * Attribute for width scaling. Value should be a Number.
053         */
054        public static final Attribute WIDTH_SCALE = new FontStyleAttribute("widthScale");
055        
056        /**
057         * Attribute for height scaling. Value should be a Number.
058         */
059        public static final Attribute HEIGHT_SCALE = new FontStyleAttribute("heightScale");
060        
061        /**
062         * Attribute for the amount of slant for italic text.
063         */
064        public static final Attribute ITALIC_SLANT = new FontStyleAttribute("italicSlant");
065        
066        /**
067         * Stroke width for drawing the associated text 
068         */
069        private int strokeWidth = 1;
070
071        /**
072         * Scaling in the width direction 
073         */
074        private float widthScale = 1;
075        
076        /**
077         * Scaling in the height direction 
078         */
079        private float heightScale = 1;
080        
081        /**
082         * Slant for italic text 
083         */
084        private float italicSlant = 0.75f;
085        
086        @Override
087        public void parseAttributes(Map<? extends Attribute,Object> attrs) {
088                super.parseAttributes(attrs);
089                
090                if (attrs.containsKey(STROKE_WIDTH)) strokeWidth = ((Number) attrs.get(STROKE_WIDTH)).intValue();
091                if (attrs.containsKey(WIDTH_SCALE)) widthScale = ((Number) attrs.get(WIDTH_SCALE)).floatValue();
092                if (attrs.containsKey(HEIGHT_SCALE)) heightScale = ((Number) attrs.get(HEIGHT_SCALE)).floatValue();
093        }
094        
095        /**
096         * Construct with the default parameters for the given image type
097         * @param image 
098         */
099        protected HersheyFontStyle(HersheyFont font, ImageRenderer<T, ?> image) {
100                super(font, image);
101        }
102
103        /**
104         * @return the strokeWidth
105         */
106        public int getStrokeWidth() {
107                return strokeWidth;
108        }
109
110        /**
111         * @param strokeWidth the strokeWidth to set
112         */
113        public void setStrokeWidth(int strokeWidth) {
114                this.strokeWidth = strokeWidth;
115        }
116
117        /**
118         * @return the widthScale
119         */
120        public float getWidthScale() {
121                return widthScale;
122        }
123
124        /**
125         * @param widthScale the widthScale to set
126         */
127        public void setWidthScale(float widthScale) {
128                this.widthScale = widthScale;
129        }
130
131        /**
132         * @return the heightScale
133         */
134        public float getHeightScale() {
135                return heightScale;
136        }
137
138        /**
139         * @param heightScale the heightScale to set
140         */
141        public void setHeightScale(float heightScale) {
142                this.heightScale = heightScale;
143        }
144
145        /**
146         * @return the italicSlant
147         */
148        public float getItalicSlant() {
149                return italicSlant;
150        }
151
152        /**
153         * @param italicSlant the italicSlant to set
154         */
155        public void setItalicSlant(float italicSlant) {
156                this.italicSlant = italicSlant;
157        }
158        
159        /**
160         * Get the actual scale to render the font at. This
161         * is calculated by scaling to the fontSize and then 
162         * applying the widthScale.
163         * @return the actual width scaling
164         */
165        public float getActualWidthScale() {
166                HersheyFont font = (HersheyFont) this.font; 
167                float charHeight = font.data.characterSetMaxY - font.data.characterSetMinY;
168                float sizeSF = this.fontSize / charHeight;
169                return sizeSF * widthScale;
170        }
171        
172        /**
173         * Get the actual scale to render the font at. This
174         * is calculated by scaling to the fontSize and then 
175         * applying the heightScale.
176         * @return the actual height scaling
177         */
178        public float getActualHeightScale() {
179                HersheyFont font = (HersheyFont) this.font;
180                float charHeight = font.data.characterSetMaxY - font.data.characterSetMinY;
181                float sizeSF = this.fontSize / charHeight;
182                return sizeSF * heightScale;
183        }
184}