-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathSyntaxStyle.java
More file actions
137 lines (126 loc) · 3.98 KB
/
SyntaxStyle.java
File metadata and controls
137 lines (126 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* SyntaxStyle.java - A simple text style class
* Copyright (C) 1999 Slava Pestov
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package nodebox.client.syntax;
import java.awt.*;
/**
* A simple text style class. It can specify the color, italic flag,
* and bold flag of a run of text.
*
* @author Slava Pestov
* @version $Id: SyntaxStyle.java,v 1.1.1.1 2001/08/20 22:31:42 gfx Exp $
*/
public class SyntaxStyle {
/**
* Creates a new SyntaxStyle.
*
* @param color The text color
* @param italic True if the text should be italics
* @param bold True if the text should be bold
*/
public SyntaxStyle(Color color, boolean italic, boolean bold) {
this.color = color;
this.italic = italic;
this.bold = bold;
}
/**
* Returns the color specified in this style.
*/
public Color getColor() {
return color;
}
/**
* Returns true if no font styles are enabled.
*/
public boolean isPlain() {
return !(bold || italic);
}
/**
* Returns true if italics is enabled for this style.
*/
public boolean isItalic() {
return italic;
}
/**
* Returns true if boldface is enabled for this style.
*/
public boolean isBold() {
return bold;
}
/**
* Returns the specified font, but with the style's bold and
* italic flags applied.
*/
public Font getStyledFont(Font font) {
if (font == null)
throw new NullPointerException("font param must not"
+ " be null");
if (font.equals(lastFont))
return lastStyledFont;
lastFont = font;
lastStyledFont = new Font(font.getFamily(),
(bold ? Font.BOLD : 0)
| (italic ? Font.ITALIC : 0),
font.getSize());
return lastStyledFont;
}
/**
* Returns the font metrics for the styled font.
*/
public FontMetrics getFontMetrics(Font font) {
if (font == null)
throw new NullPointerException("font param must not"
+ " be null");
if (font.equals(lastFont) && fontMetrics != null)
return fontMetrics;
lastFont = font;
lastStyledFont = new Font(font.getFamily(),
(bold ? Font.BOLD : 0)
| (italic ? Font.ITALIC : 0),
font.getSize());
fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(lastStyledFont);
return fontMetrics;
}
/**
* Sets the foreground color and font of the specified graphics
* context to that specified in this style.
*
* @param gfx The graphics context
* @param font The font to add the styles to
*/
public void setGraphicsFlags(Graphics gfx, Font font) {
Font _font = getStyledFont(font);
gfx.setFont(_font);
gfx.setColor(color);
}
/**
* Returns a string representation of this object.
*/
public String toString() {
return getClass().getName() + "[color=" + color +
(italic ? ",italic" : "") +
(bold ? ",bold" : "") + "]";
}
// private members
private Color color;
private boolean italic;
private boolean bold;
private Font lastFont;
private Font lastStyledFont;
private FontMetrics fontMetrics;
}