1
2
3
4
5
6
7
8
9
10 package org.syntax.jedit;
11
12 import java.awt.Color;
13 import java.awt.Font;
14 import java.awt.FontMetrics;
15 import java.awt.Graphics;
16 import java.awt.Toolkit;
17
18 /***
19 * A simple text style class. It can specify the color, italic flag, and bold
20 * flag of a run of text.
21 *
22 * @author Slava Pestov
23 * @version $Id$
24 */
25 public class SyntaxStyle
26 {
27 /***
28 * Creates a new SyntaxStyle.
29 *
30 * @param color
31 * The text color
32 * @param italic
33 * True if the text should be italics
34 * @param bold
35 * True if the text should be bold
36 */
37 public SyntaxStyle( Color color, boolean italic, boolean bold )
38 {
39 this.color = color;
40 this.italic = italic;
41 this.bold = bold;
42 }
43
44 /***
45 * Returns the color specified in this style.
46 */
47 public Color getColor()
48 {
49 return color;
50 }
51
52 /***
53 * Returns true if no font styles are enabled.
54 */
55 public boolean isPlain()
56 {
57 return !( bold || italic );
58 }
59
60 /***
61 * Returns true if italics is enabled for this style.
62 */
63 public boolean isItalic()
64 {
65 return italic;
66 }
67
68 /***
69 * Returns true if boldface is enabled for this style.
70 */
71 public boolean isBold()
72 {
73 return bold;
74 }
75
76 /***
77 * Returns the specified font, but with the style's bold and italic flags
78 * applied.
79 */
80 public Font getStyledFont( Font font )
81 {
82 if( font == null )
83 throw new NullPointerException( "font param must not" + " be null" );
84 if( font.equals( lastFont ) )
85 return lastStyledFont;
86 lastFont = font;
87 lastStyledFont = new Font( font.getFamily(), ( bold ? Font.BOLD : 0 ) | ( italic ? Font.ITALIC : 0 ), font
88 .getSize() );
89 return lastStyledFont;
90 }
91
92 /***
93 * Returns the font metrics for the styled font.
94 */
95 public FontMetrics getFontMetrics( Font font )
96 {
97 if( font == null )
98 throw new NullPointerException( "font param must not" + " be null" );
99 if( font.equals( lastFont ) && fontMetrics != null )
100 return fontMetrics;
101 lastFont = font;
102 lastStyledFont = new Font( font.getFamily(), ( bold ? Font.BOLD : 0 ) | ( italic ? Font.ITALIC : 0 ), font
103 .getSize() );
104 fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics( lastStyledFont );
105 return fontMetrics;
106 }
107
108 /***
109 * Sets the foreground color and font of the specified graphics context to
110 * that specified in this style.
111 *
112 * @param gfx
113 * The graphics context
114 * @param font
115 * The font to add the styles to
116 */
117 public void setGraphicsFlags( Graphics gfx, Font font )
118 {
119 Font _font = getStyledFont( font );
120 gfx.setFont( _font );
121 gfx.setColor( color );
122 }
123
124 /***
125 * Returns a string representation of this object.
126 */
127 public String toString()
128 {
129 return getClass().getName() + "[color=" + color + ( italic ? ",italic" : "" ) + ( bold ? ",bold" : "" ) + "]";
130 }
131
132
133 private Color color;
134 private boolean italic;
135 private boolean bold;
136 private Font lastFont;
137 private Font lastStyledFont;
138 private FontMetrics fontMetrics;
139 }