1 | package org.expeditee.core;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Handle-type for fonts managed by the font manager. Should be able to
|
---|
5 | * instantiate your own font objects, and have the font manager use an appropriate
|
---|
6 | * internal font when necessary (i.e. Font objects don't need to be constructed by
|
---|
7 | * the font manager, they are interpreted on use).
|
---|
8 | *
|
---|
9 | * @author cts16
|
---|
10 | */
|
---|
11 | public class Font {
|
---|
12 |
|
---|
13 | /** Enumeration of the possible styles of font (bold/italic). */
|
---|
14 | public enum Style {
|
---|
15 | PLAIN,
|
---|
16 | BOLD,
|
---|
17 | ITALIC,
|
---|
18 | BOLD_ITALIC
|
---|
19 | }
|
---|
20 |
|
---|
21 | /** The default family for fonts which don't explicitly specify one. */
|
---|
22 | public static final String DEFAULT_FAMILY_NAME = "Serif";
|
---|
23 | /** The default style for fonts which don't explicitly specify one. */
|
---|
24 | public static final Font.Style DEFAULT_STYLE = Font.Style.PLAIN;
|
---|
25 | /** The default size for fonts which don't explicitly specify one. */
|
---|
26 | public static final int DEFAULT_SIZE = 12;
|
---|
27 | /** The default spacing for fonts which don't explicitly specify one. */
|
---|
28 | public static final float DEFAULT_SPACING = 0.0f;
|
---|
29 |
|
---|
30 | /** The family name of this font. */
|
---|
31 | private String _familyName;
|
---|
32 | /** The style of this font (bold/italic). */
|
---|
33 | private Style _style;
|
---|
34 | /** The size of this font. */
|
---|
35 | private int _size;
|
---|
36 | /** The spacing of this font. */
|
---|
37 | private float _spacing;
|
---|
38 |
|
---|
39 | /** Standard constructor. */
|
---|
40 | public Font(String familyName, Style style, int size, float spacing)
|
---|
41 | {
|
---|
42 | if (familyName == null) familyName = DEFAULT_FAMILY_NAME;
|
---|
43 | if (style == null) style = DEFAULT_STYLE;
|
---|
44 |
|
---|
45 | _familyName = familyName;
|
---|
46 | _style = style;
|
---|
47 | _size = size;
|
---|
48 | _spacing = spacing;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /** Convenience constructor. */
|
---|
52 | public Font(String familyName, Style style, int size)
|
---|
53 | {
|
---|
54 | this(familyName, style, size, DEFAULT_SPACING);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Convenience constructor. */
|
---|
58 | public Font(String familyName, Style style)
|
---|
59 | {
|
---|
60 | this(familyName, style, DEFAULT_SIZE);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Convenience constructor. */
|
---|
64 | public Font(String familyName)
|
---|
65 | {
|
---|
66 | this(familyName, DEFAULT_STYLE);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Convenience constructor. */
|
---|
70 | public Font()
|
---|
71 | {
|
---|
72 | this(DEFAULT_FAMILY_NAME);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Copy constructor. */
|
---|
76 | public Font(Font other)
|
---|
77 | {
|
---|
78 | _familyName = other._familyName;
|
---|
79 | _style = other._style;
|
---|
80 | _size = other._size;
|
---|
81 | _spacing = other._spacing;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Gets the family name of this font. */
|
---|
85 | public String getFamilyName()
|
---|
86 | {
|
---|
87 | return _familyName;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Sets the style of this font. */
|
---|
91 | public Font setStyle(Style style)
|
---|
92 | {
|
---|
93 | _style = style;
|
---|
94 |
|
---|
95 | return this;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Gets the style of this font. */
|
---|
99 | public Style getStyle()
|
---|
100 | {
|
---|
101 | return _style;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Returns true if this font has bold styling. */
|
---|
105 | public boolean isBold()
|
---|
106 | {
|
---|
107 | return _style == Style.BOLD || _style == Style.BOLD_ITALIC;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Returns true if this font has italic styling. */
|
---|
111 | public boolean isItalic()
|
---|
112 | {
|
---|
113 | return _style == Style.ITALIC || _style == Style.BOLD_ITALIC;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Returns true if this font has no bold/italic styling. */
|
---|
117 | public boolean isPlain()
|
---|
118 | {
|
---|
119 | return _style == Style.PLAIN;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** Removes bold styling if this font has it, or adds if if not. */
|
---|
123 | public void toggleBold()
|
---|
124 | {
|
---|
125 | switch (_style) {
|
---|
126 | case PLAIN:
|
---|
127 | _style = Style.BOLD;
|
---|
128 | break;
|
---|
129 | case BOLD:
|
---|
130 | _style = Style.PLAIN;
|
---|
131 | break;
|
---|
132 | case ITALIC:
|
---|
133 | _style = Style.BOLD_ITALIC;
|
---|
134 | break;
|
---|
135 | case BOLD_ITALIC:
|
---|
136 | _style = Style.ITALIC;
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Removes italic styling if this font has it, or adds if if not. */
|
---|
142 | public void toggleItalic()
|
---|
143 | {
|
---|
144 | switch (_style) {
|
---|
145 | case PLAIN:
|
---|
146 | _style = Style.ITALIC;
|
---|
147 | break;
|
---|
148 | case BOLD:
|
---|
149 | _style = Style.BOLD_ITALIC;
|
---|
150 | break;
|
---|
151 | case ITALIC:
|
---|
152 | _style = Style.PLAIN;
|
---|
153 | break;
|
---|
154 | case BOLD_ITALIC:
|
---|
155 | _style = Style.BOLD;
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Sets the size of this font. */
|
---|
161 | public void setSize(int size)
|
---|
162 | {
|
---|
163 | _size = size;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** Gets the size of this font. */
|
---|
167 | public int getSize()
|
---|
168 | {
|
---|
169 | return _size;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /** Sets the spacing of this font. */
|
---|
173 | public void setSpacing(float spacing)
|
---|
174 | {
|
---|
175 | _spacing = spacing;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Gets the spacing of this font. */
|
---|
179 | public float getSpacing()
|
---|
180 | {
|
---|
181 | return _spacing;
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | public Font clone()
|
---|
186 | {
|
---|
187 | return new Font(this);
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Automatically generated.
|
---|
191 | @Override
|
---|
192 | public int hashCode() {
|
---|
193 | final int prime = 31;
|
---|
194 | int result = 1;
|
---|
195 | result = prime * result + ((_familyName == null) ? 0 : _familyName.hashCode());
|
---|
196 | result = prime * result + _size;
|
---|
197 | result = prime * result + Float.floatToIntBits(_spacing);
|
---|
198 | result = prime * result + ((_style == null) ? 0 : _style.hashCode());
|
---|
199 | return result;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // Automatically generated.
|
---|
203 | @Override
|
---|
204 | public boolean equals(Object obj) {
|
---|
205 | if (this == obj)
|
---|
206 | return true;
|
---|
207 | if (obj == null)
|
---|
208 | return false;
|
---|
209 | if (getClass() != obj.getClass())
|
---|
210 | return false;
|
---|
211 | Font other = (Font) obj;
|
---|
212 | if (_familyName == null) {
|
---|
213 | if (other._familyName != null)
|
---|
214 | return false;
|
---|
215 | } else if (!_familyName.equals(other._familyName))
|
---|
216 | return false;
|
---|
217 | if (_size != other._size)
|
---|
218 | return false;
|
---|
219 | if (Float.floatToIntBits(_spacing) != Float.floatToIntBits(other._spacing))
|
---|
220 | return false;
|
---|
221 | if (_style != other._style)
|
---|
222 | return false;
|
---|
223 | return true;
|
---|
224 | }
|
---|
225 | }
|
---|