1 package org.opentrafficsim.draw.core;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.font.TextAttribute;
6 import java.io.Serializable;
7 import java.util.Hashtable;
8 import java.util.Map;
9
10 /**
11 * Properties for text to identify animated objects in OTS.
12 * <p>
13 * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
15 * </p>
16 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17 * initial version Jan 17, 2017 <br>
18 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21 */
22 public class TextProperties implements Serializable
23 {
24 /** */
25 private static final long serialVersionUID = 20170400L;
26
27 /** the text alignment (LEFT, CENTER or RIGHT). */
28 private final TextAlignment textAlignment;
29
30 /** the color of the text, e.g., Color.RED. */
31 private final Color color;
32
33 /** the name of the font, e.g., specified as Font.SERIF. */
34 private final String font;
35
36 /** a map of text attributes to set, e.g., UNDERLINE or WEIGHT. */
37 private final Map<TextAttribute, Object> textAttributes;
38
39 /** the size of the font (in units of the animation, e.g., meters). */
40 private final float fontSize;
41
42 /**
43 * Construct a default set of text properties for animation.
44 */
45 public TextProperties()
46 {
47 this(TextAlignment.CENTER, Color.BLACK, 1.5f);
48 }
49
50 /**
51 * Construct a set of text properties for animation with alignment, color and size.
52 * @param textAlignment TextAlignment; the text alignment (LEFT, CENTER or RIGHT)
53 * @param color Color; the color of the text
54 * @param fontSize float; the size of the font (in units of the animation, e.g., meters)
55 */
56 public TextProperties(final TextAlignment textAlignment, final Color color, final float fontSize)
57 {
58 this(textAlignment, color, fontSize, Font.SANS_SERIF, new Hashtable<>());
59 }
60
61 /**
62 * Construct a set of text properties for animation with alignment, color and size.
63 * @param textAlignment TextAlignment; the text alignment (LEFT, CENTER or RIGHT)
64 * @param color Color; the color of the text, e.g., Color.RED
65 * @param fontSize float; the size of the font (in units of the animation, e.g., meters)
66 * @param font String; the name of the font, e.g., specified as Font.SERIF
67 * @param textAttributes Map<TextAttribute,Object>; a map of text attributes to set, e.g., UNDERLINE or WEIGHT
68 */
69 public TextProperties(final TextAlignment textAlignment, final Color color, final float fontSize, final String font,
70 final Map<TextAttribute, Object> textAttributes)
71 {
72 this.textAlignment = textAlignment;
73 this.color = color;
74 this.fontSize = fontSize;
75 this.font = font;
76 this.textAttributes = new Hashtable<>(textAttributes);
77 }
78
79 /**
80 * @return textAlignment
81 */
82 public final TextAlignment getTextAlignment()
83 {
84 return this.textAlignment;
85 }
86
87 /**
88 * @return color
89 */
90 public final Color getColor()
91 {
92 return this.color;
93 }
94
95 /**
96 * @return font
97 */
98 public final String getFont()
99 {
100 return this.font;
101 }
102
103 /**
104 * @return textAttributes
105 */
106 public final Map<TextAttribute, Object> getTextAttributes()
107 {
108 return this.textAttributes;
109 }
110
111 /**
112 * @return fontSize
113 */
114 public final float getFontSize()
115 {
116 return this.fontSize;
117 }
118
119 /**
120 * Set the weight to either WEIGHT_REGULAR or WEIGHT_BOLD.
121 * @param bold boolean; whether the font is bold or regular
122 */
123 public final void setBold(final boolean bold)
124 {
125 this.textAttributes.put(TextAttribute.WEIGHT, bold ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
126 }
127
128 /**
129 * Set the weight to one of multiple weight constants.
130 * @param weight TextWeight; the weight of the font to use
131 */
132 public final void setBold(final TextWeight weight)
133 {
134 this.textAttributes.put(TextAttribute.WEIGHT, weight.getValue());
135 }
136
137 /**
138 * Set the posture to either POSTURE_REGULAR or POSTURE_OBLIQUE (italic).
139 * @param italic boolean; whether the font is italic or regular
140 */
141 public final void setItalic(final boolean italic)
142 {
143 this.textAttributes.put(TextAttribute.POSTURE, italic ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR);
144 }
145
146 /**
147 * Set the width to WIDTH_CONDENSED, WIDTH_REGULAR, or WIDTH_EXTENDED.
148 * @param width TextWidth; the TextWidth to use
149 */
150 public final void setWeightBold(final TextWidth width)
151 {
152 this.textAttributes.put(TextAttribute.WIDTH, width.getValue());
153 }
154
155 /**
156 * Set the underline on or off.
157 * @param underline boolean; whether the font is underlined or regular
158 */
159 public final void setUnderline(final boolean underline)
160 {
161 this.textAttributes.put(TextAttribute.UNDERLINE, underline ? TextAttribute.UNDERLINE_ON : -1);
162 }
163
164 /**
165 * Set the strikethrough on or off.
166 * @param strikethrough boolean; whether the font is strikethrough or regular
167 */
168 public final void setStrikethrough(final boolean strikethrough)
169 {
170 this.textAttributes.put(TextAttribute.STRIKETHROUGH, strikethrough ? TextAttribute.STRIKETHROUGH_ON : -1);
171 }
172
173 /** {@inheritDoc} */
174 @Override
175 public final String toString()
176 {
177 return "TextProperties [textAlignment=" + this.textAlignment + ", color=" + this.color + ", font=" + this.font
178 + ", textAttributes=" + this.textAttributes + ", fontSize=" + this.fontSize + "]";
179 }
180
181 }