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 | if (familyName == null) familyName = DEFAULT_FAMILY_NAME;
|
---|
42 | if (style == null) style = DEFAULT_STYLE;
|
---|
43 |
|
---|
44 | _familyName = familyName;
|
---|
45 | _style = style;
|
---|
46 | _size = size;
|
---|
47 | _spacing = spacing;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /** Convenience constructor. */
|
---|
51 | public Font(String familyName, Style style, int size) {
|
---|
52 | this(familyName, style, size, DEFAULT_SPACING);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Convenience constructor. */
|
---|
56 | public Font(String familyName, Style style) {
|
---|
57 | this(familyName, style, DEFAULT_SIZE);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Convenience constructor. */
|
---|
61 | public Font(String familyName) {
|
---|
62 | this(familyName, DEFAULT_STYLE);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Convenience constructor. */
|
---|
66 | public Font() {
|
---|
67 | this(DEFAULT_FAMILY_NAME);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Copy constructor. */
|
---|
71 | public Font(Font other) {
|
---|
72 | _familyName = other._familyName;
|
---|
73 | _style = other._style;
|
---|
74 | _size = other._size;
|
---|
75 | _spacing = other._spacing;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Gets the family name of this font. */
|
---|
79 | public String getFamilyName() {
|
---|
80 | return _familyName;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Sets the style of this font. */
|
---|
84 | public Font setStyle(Style style) {
|
---|
85 | _style = style;
|
---|
86 | return this;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Gets the style of this font. */
|
---|
90 | public Style getStyle() {
|
---|
91 | return _style;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Returns true if this font has bold styling. */
|
---|
95 | public boolean isBold() {
|
---|
96 | return _style == Style.BOLD || _style == Style.BOLD_ITALIC;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Returns true if this font has italic styling. */
|
---|
100 | public boolean isItalic() {
|
---|
101 | return _style == Style.ITALIC || _style == Style.BOLD_ITALIC;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Returns true if this font has no bold/italic styling. */
|
---|
105 | public boolean isPlain() {
|
---|
106 | return _style == Style.PLAIN;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Removes bold styling if this font has it, or adds if if not. */
|
---|
110 | public void toggleBold() {
|
---|
111 | switch (_style) {
|
---|
112 | case PLAIN:
|
---|
113 | _style = Style.BOLD;
|
---|
114 | break;
|
---|
115 | case BOLD:
|
---|
116 | _style = Style.PLAIN;
|
---|
117 | break;
|
---|
118 | case ITALIC:
|
---|
119 | _style = Style.BOLD_ITALIC;
|
---|
120 | break;
|
---|
121 | case BOLD_ITALIC:
|
---|
122 | _style = Style.ITALIC;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Removes italic styling if this font has it, or adds if if not. */
|
---|
128 | public void toggleItalic() {
|
---|
129 | switch (_style) {
|
---|
130 | case PLAIN:
|
---|
131 | _style = Style.ITALIC;
|
---|
132 | break;
|
---|
133 | case BOLD:
|
---|
134 | _style = Style.BOLD_ITALIC;
|
---|
135 | break;
|
---|
136 | case ITALIC:
|
---|
137 | _style = Style.PLAIN;
|
---|
138 | break;
|
---|
139 | case BOLD_ITALIC:
|
---|
140 | _style = Style.BOLD;
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** Sets the size of this font. */
|
---|
146 | public void setSize(int size) {
|
---|
147 | _size = size;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Gets the size of this font. */
|
---|
151 | public int getSize() {
|
---|
152 | return _size;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Sets the spacing of this font. */
|
---|
156 | public void setSpacing(float spacing) {
|
---|
157 | _spacing = spacing;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Gets the spacing of this font. */
|
---|
161 | public float getSpacing() {
|
---|
162 | return _spacing;
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public Font clone() {
|
---|
167 | return new Font(this);
|
---|
168 | }
|
---|
169 |
|
---|
170 | // Automatically generated.
|
---|
171 | @Override
|
---|
172 | public int hashCode() {
|
---|
173 | final int prime = 31;
|
---|
174 | int result = 1;
|
---|
175 | result = prime * result + ((_familyName == null) ? 0 : _familyName.hashCode());
|
---|
176 | result = prime * result + _size;
|
---|
177 | result = prime * result + Float.floatToIntBits(_spacing);
|
---|
178 | result = prime * result + ((_style == null) ? 0 : _style.hashCode());
|
---|
179 | return result;
|
---|
180 | }
|
---|
181 |
|
---|
182 | // Automatically generated.
|
---|
183 | @Override
|
---|
184 | public boolean equals(Object obj) {
|
---|
185 | if (this == obj)
|
---|
186 | return true;
|
---|
187 | if (obj == null)
|
---|
188 | return false;
|
---|
189 | if (getClass() != obj.getClass())
|
---|
190 | return false;
|
---|
191 | Font other = (Font) obj;
|
---|
192 | if (_familyName == null) {
|
---|
193 | if (other._familyName != null)
|
---|
194 | return false;
|
---|
195 | } else if (!_familyName.equals(other._familyName))
|
---|
196 | return false;
|
---|
197 | if (_size != other._size)
|
---|
198 | return false;
|
---|
199 | if (Float.floatToIntBits(_spacing) != Float.floatToIntBits(other._spacing))
|
---|
200 | return false;
|
---|
201 | if (_style != other._style)
|
---|
202 | return false;
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 | }
|
---|