1 package text; 2 3 import java.awt.*; 4 import javax.swing.*; 5 import java.util.*; 6 7 import rero.util.*; 8 9 import rero.config.*; 10 import java.io.*; 11 import rero.gui.*; 12 13 import java.awt.font.*; 14 15 public class TextSource 16 { 17 18 public static Font clientFont; 19 public static FontMetrics fontMetrics; 20 private static Object antiAliasHint; 21 22 public static final int UNIVERSAL_TWEAK = 2; 24 protected static TextSourceListener listener; 26 public static Color colorTable[]; 27 28 public static void saveColorMap() 29 { 30 try 31 { 32 FileOutputStream ostream = new FileOutputStream(new File(ClientState.getBaseDirectory() ,"color.map")); 33 ObjectOutputStream o = new ObjectOutputStream(ostream); 34 o.writeObject(colorTable); 35 } 36 catch (Exception ex) 37 { 38 ex.printStackTrace(); 39 } 40 41 ClientState.getClientState().fireChange("color.map", null); 42 } 43 44 static 45 { 46 try 47 { 48 ObjectInputStream p = new ObjectInputStream(ClientState.getClientState().getResourceAsStream("color.map")); 49 colorTable = (Color[])p.readObject(); 50 } 51 catch (Exception ex) 52 { 53 ex.printStackTrace(); 54 } 55 56 if (colorTable == null) 57 { 58 colorTable = new Color[100]; 59 colorTable[0] = Color.lightGray; 60 colorTable[1] = new Color(0, 0, 0); 61 colorTable[2] = new Color(0, 0, 128); 62 colorTable[3] = new Color(0, 144, 0); 63 colorTable[4] = new Color(255, 0, 0); 64 colorTable[5] = new Color(128, 0, 0); 65 colorTable[6] = new Color(160, 0, 160); 66 colorTable[7] = new Color(255, 128, 0); 67 colorTable[8] = new Color(255, 255, 0); 68 colorTable[9] = new Color(0, 255, 0); 69 colorTable[10] = new Color(0, 144, 144); 70 colorTable[11] = new Color(0, 255, 255); 71 colorTable[12] = new Color(0, 0, 255); 72 colorTable[13] = new Color(255, 0, 255); 73 colorTable[14] = new Color(128, 128, 128); 74 colorTable[15] = Color.lightGray; 75 colorTable[16] = new Color(255, 255, 255); 76 77 for (int x = 17; x < colorTable.length; x++) 78 { 79 colorTable[x] = colorTable[0]; 80 } 81 82 saveColorMap(); 83 } 84 85 listener = new TextSourceListener(); } 87 88 public static void initGraphics(Graphics g) 89 { 90 g.setFont(clientFont); 91 92 Graphics2D g2 = (Graphics2D)g; 93 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliasHint); 95 } 96 97 public static int translateToLineNumber(int pixely) 98 { 99 return (pixely - 5) / (TextSource.fontMetrics.getHeight() + 2); 100 } 101 102 public static void drawForeground(Graphics g, AttributedText text, int x_component, int baseline) 103 { 104 int height = TextSource.fontMetrics.getHeight(); 105 106 while (text != null) 107 { 108 g.setColor(TextSource.colorTable[text.foreIndex]); 109 110 if (text.isBold) 111 { 112 g.setColor(g.getColor().brighter()); 113 } 114 115 if (text.isReverse) 116 { 117 g.setColor(g.getColor().darker()); 118 } 119 120 if (text.isUnderline) 121 { 122 g.drawLine(x_component, baseline + 1, x_component + text.width, baseline + 1); 123 } 124 125 g.drawString(text.text, x_component, baseline); 126 127 x_component += text.width; 128 text = text.next; 129 } 130 } 131 132 public static void drawBackground(Graphics g, AttributedText text, int x_component, int baseline) 133 { 134 int height = TextSource.fontMetrics.getHeight(); 135 136 while (text != null) 137 { 138 if (text.backIndex != -1) 139 { 140 g.setColor(TextSource.colorTable[text.backIndex]); 141 g.fillRect(x_component, baseline - height + 2, text.width, height + 2); } 143 144 x_component += text.width; 145 text = text.next; 146 } 147 } 148 149 public static void drawText(Graphics g, AttributedText text, int x_component, int baseline) 150 { 151 drawBackground(g, text, x_component, baseline); 152 drawForeground(g, text, x_component, baseline); 153 } 154 155 protected static class TextSourceListener implements ClientStateListener 156 { 157 public TextSourceListener() 158 { 159 ClientState.getClientState().addClientStateListener("ui.font", this); 160 ClientState.getClientState().addClientStateListener("ui.antialias", this); 161 rehashValue(); 162 } 163 164 public void rehashValue() 165 { 166 if (ClientState.getClientState().isOption("ui.antialias", ClientDefaults.ui_antialias)) 167 { 168 antiAliasHint = RenderingHints.VALUE_TEXT_ANTIALIAS_ON; 169 } 170 else 171 { 172 antiAliasHint = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF; 173 } 174 175 clientFont = ClientState.getClientState().getFont("ui.font", ClientDefaults.ui_font); 176 fontMetrics = new AdjustedFontMetrics(clientFont, Toolkit.getDefaultToolkit().getFontMetrics(clientFont)); 177 } 178 179 public void propertyChanged(String value, String parms) 180 { 181 rehashValue(); 182 SessionManager.getGlobalCapabilities().frame.validate(); 183 SessionManager.getGlobalCapabilities().frame.repaint(); 184 } 185 } 186 187 private static class AdjustedFontMetrics extends FontMetrics 188 { 189 private int adj_ascent, adj_descent, height; 190 private FontMetrics metrics; 191 private FontRenderContext context; 192 private Font font; 193 194 public AdjustedFontMetrics(Font wayneFonts, FontMetrics _metrics) 195 { 196 super(wayneFonts); 197 198 metrics = _metrics; 199 200 adj_ascent = Math.abs(metrics.getMaxAscent()); adj_descent = Math.abs(metrics.getMaxDescent()); 203 context = new FontRenderContext(null, antiAliasHint == RenderingHints.VALUE_TEXT_ANTIALIAS_ON, false); 204 font = wayneFonts; 205 206 height = adj_ascent + adj_descent; 207 } 208 209 public int getAscent() 210 { 211 return adj_ascent; 212 } 213 214 public int getDescent() 215 { 216 return adj_descent; 217 } 218 219 public int getHeight() 220 { 221 return height; 222 } 223 224 public int stringWidth(String str) 225 { 226 return (int)Math.round(font.getStringBounds(str, context).getWidth()); 227 } 228 } 229 } 230 | Popular Tags |