1 40 41 package org.jfree.experimental.swt; 42 43 import javax.swing.JPanel ; 44 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.graphics.Color; 47 import org.eclipse.swt.graphics.Device; 48 import org.eclipse.swt.graphics.Font; 49 import org.eclipse.swt.graphics.FontData; 50 import org.eclipse.swt.graphics.GC; 51 52 57 public class SWTUtils { 58 59 private final static String Az = "ABCpqr"; 60 61 82 public static FontData toSwtFontData(Device device, java.awt.Font font, 83 boolean ensureSameSize) { 84 FontData fontData = new FontData(); 85 fontData.setName(font.getFamily()); 86 int style = SWT.NORMAL; 87 switch (font.getStyle()) { 88 case java.awt.Font.PLAIN: 89 style |= SWT.NORMAL; 90 break; 91 case java.awt.Font.BOLD: 92 style |= SWT.BOLD; 93 break; 94 case java.awt.Font.ITALIC: 95 style |= SWT.ITALIC; 96 break; 97 case (java.awt.Font.ITALIC + java.awt.Font.BOLD): 98 style |= SWT.ITALIC | SWT.BOLD; 99 break; 100 } 101 fontData.setStyle(style); 102 int height = (int) Math.round(font.getSize() * 72.0 104 / device.getDPI().y); 105 fontData.setHeight(height); 106 if (ensureSameSize) { 109 GC tmpGC = new GC(device); 110 JPanel DUMMY_PANEL = new JPanel (); 111 Font tmpFont = new Font(device, fontData); 112 tmpGC.setFont(tmpFont); 113 if (tmpGC.textExtent(Az).x 114 > DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { 115 while (tmpGC.textExtent(Az).x 116 > DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { 117 tmpFont.dispose(); 118 height--; 119 fontData.setHeight(height); 120 tmpFont = new Font(device, fontData); 121 tmpGC.setFont(tmpFont); 122 } 123 } 124 else if (tmpGC.textExtent(Az).x 125 < DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { 126 while (tmpGC.textExtent(Az).x 127 < DUMMY_PANEL.getFontMetrics(font).stringWidth(Az)) { 128 tmpFont.dispose(); 129 height++; 130 fontData.setHeight(height); 131 tmpFont = new Font(device, fontData); 132 tmpGC.setFont(tmpFont); 133 } 134 } 135 tmpFont.dispose(); 136 tmpGC.dispose(); 137 } 138 return fontData; 139 } 140 141 157 public static java.awt.Font toAwtFont(Device device, FontData fontData, 158 boolean ensureSameSize) { 159 int style; 160 switch (fontData.getStyle()) { 161 case SWT.NORMAL: 162 style = java.awt.Font.PLAIN; 163 break; 164 case SWT.ITALIC: 165 style = java.awt.Font.ITALIC; 166 break; 167 case SWT.BOLD: 168 style = java.awt.Font.BOLD; 169 break; 170 default: 171 style = java.awt.Font.PLAIN; 172 break; 173 } 174 int height = (int) Math.round(fontData.height * device.getDPI().y 175 / 72.0); 176 if (ensureSameSize) { 179 GC tmpGC = new GC(device); 180 Font tmpFont = new Font(device, fontData); 181 tmpGC.setFont(tmpFont); 182 JPanel DUMMY_PANEL = new JPanel (); 183 java.awt.Font tmpAwtFont = new java.awt.Font (fontData.getName(), 184 style, height); 185 if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 186 > tmpGC.textExtent(Az).x) { 187 while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 188 > tmpGC.textExtent(Az).x) { 189 height--; 190 tmpAwtFont = new java.awt.Font (fontData.getName(), style, 191 height); 192 } 193 } 194 else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 195 < tmpGC.textExtent(Az).x) { 196 while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 197 < tmpGC.textExtent(Az).x) { 198 height++; 199 tmpAwtFont = new java.awt.Font (fontData.getName(), style, 200 height); 201 } 202 } 203 tmpFont.dispose(); 204 tmpGC.dispose(); 205 } 206 return new java.awt.Font (fontData.getName(), style, height); 207 } 208 209 217 public static java.awt.Font toAwtFont(Device device, Font font) { 218 FontData fontData = font.getFontData()[0]; 219 return toAwtFont(device, fontData, true); 220 } 221 222 229 public static java.awt.Color toAwtColor(Color color) { 230 return new java.awt.Color (color.getRed(), color.getGreen(), 231 color.getBlue()); 232 } 233 234 244 public static Color toSwtColor(Device device, java.awt.Paint paint) { 245 java.awt.Color color; 246 if (paint instanceof java.awt.Color ) { 247 color = (java.awt.Color ) paint; 248 } 249 else { 250 try { 251 throw new Exception ("only color is supported at present... " 252 + "setting paint to uniform black color" ); 253 } 254 catch (Exception e) { 255 e.printStackTrace(); 256 color = new java.awt.Color (0, 0, 0); 257 } 258 } 259 return new org.eclipse.swt.graphics.Color(device, 260 color.getRed(), color.getGreen(), color.getBlue()); 261 } 262 263 273 public static Color toSwtColor(Device device, java.awt.Color color) { 274 return new org.eclipse.swt.graphics.Color(device, 275 color.getRed(), color.getGreen(), color.getBlue()); 276 } 277 } 278 | Popular Tags |