Gridarta Editor
SyntaxStyle.java
Go to the documentation of this file.
1 /*
2  * SyntaxStyle.java - A simple text style class
3  * Copyright (C) 1999 Slava Pestov
4  * Copyright (C) 2000-2015 The Gridarta Developers.
5  *
6  * You may use and modify this package for any purpose. Redistribution is
7  * permitted, in both source and binary form, provided that this notice
8  * remains intact in all source distributions of this package.
9  */
10 
11 package net.sf.gridarta.textedit.textarea;
12 
13 import java.awt.Color;
14 import java.awt.Font;
15 import java.awt.FontMetrics;
16 import java.awt.Graphics;
17 import org.jetbrains.annotations.Nullable;
18 
25 public class SyntaxStyle {
26 
30  private final Color color;
31 
35  private final boolean italic;
36 
40  private final boolean bold;
41 
46  private Font lastFont;
47 
52  private Font lastStyledFont;
53 
58  @Nullable
59  private FontMetrics fontMetrics;
60 
67  public SyntaxStyle(final Color color, final boolean italic, final boolean bold) {
68  this.color = color;
69  this.italic = italic;
70  this.bold = bold;
71  }
72 
77  public Color getColor() {
78  return color;
79  }
80 
85  public boolean isPlain() {
86  return !bold && !italic;
87  }
88 
93  public boolean isItalic() {
94  return italic;
95  }
96 
101  public boolean isBold() {
102  return bold;
103  }
104 
111  private Font getStyledFont(final Font font) {
112  if (font == null) {
113  throw new IllegalArgumentException("font parameter must not be null");
114  }
115 
116  updateStyledFont(font);
117  return lastStyledFont;
118  }
119 
126  public FontMetrics getFontMetrics(final Font font, final Graphics g) {
127  if (font == null) {
128  throw new IllegalArgumentException("font parameter must not be null");
129  }
130 
131  updateStyledFont(font);
132  if (fontMetrics == null) {
133  fontMetrics = g.getFontMetrics(lastStyledFont);
134  }
135  assert fontMetrics != null;
136  return fontMetrics;
137  }
138 
145  public void setGraphicsFlags(final Graphics gfx, final Font font) {
146  gfx.setFont(getStyledFont(font));
147  gfx.setColor(color);
148  }
149 
155  private void updateStyledFont(final Font font) {
156  if (font.equals(lastFont)) {
157  return;
158  }
159 
160  lastStyledFont = new Font(font.getFamily(), (bold ? Font.BOLD : 0) | (italic ? Font.ITALIC : 0), font.getSize());
161  lastFont = font;
162  fontMetrics = null;
163  }
164 
165  @Override
166  public String toString() {
167  return getClass().getName() + "[color=" + color + (italic ? ",italic" : "") + (bold ? ",bold" : "") + "]";
168  }
169 
170 }
Font getStyledFont(final Font font)
Returns the specified font, but with the style's bold and italic flags applied.
Color getColor()
Returns the text color.
final boolean bold
Whether the text should be bold.
SyntaxStyle(final Color color, final boolean italic, final boolean bold)
Creates a new instance.
void updateStyledFont(final Font font)
Update lastFont and lastStyledFont for the given font.
Font lastFont
The base font used to calculate lastStyledFont.
Font lastStyledFont
The font lastFont with applied italics and bold settings.
FontMetrics fontMetrics
The font metrics for lastStyledFont.
final boolean italic
Whether the text should be italics.
FontMetrics getFontMetrics(final Font font, final Graphics g)
Returns the font metrics for the styled font.
void setGraphicsFlags(final Graphics gfx, final Font font)
Sets the text color and font of the specified graphics context to that specified in this style...