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