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