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