1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 17 30 31 public final class Font extends Resource { 32 33 43 public int handle; 44 45 48 Font() { 49 } 50 51 69 public Font(Device device, FontData fd) { 70 if (device == null) device = Device.getDevice(); 71 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 72 init(device, fd); 73 if (device.tracking) device.new_Object(this); 74 } 75 76 99 public Font(Device device, FontData[] fds) { 100 if (device == null) device = Device.getDevice(); 101 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 102 if (fds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 103 if (fds.length == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 104 for (int i=0; i<fds.length; i++) { 105 if (fds[i] == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 106 } 107 init(device, fds[0]); 108 if (device.tracking) device.new_Object(this); 109 } 110 111 133 public Font(Device device, String name, int height, int style) { 134 if (device == null) device = Device.getDevice(); 135 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 136 if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 137 init(device, new FontData (name, height, style)); 138 if (device.tracking) device.new_Object(this); 139 } 140 141 Font(Device device, String name, float height, int style) { 142 if (device == null) device = Device.getDevice(); 143 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 144 if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 145 init(device, new FontData (name, height, style)); 146 if (device.tracking) device.new_Object(this); 147 } 148 149 154 public void dispose() { 155 if (handle == 0) return; 156 if (device.isDisposed()) return; 157 OS.DeleteObject(handle); 158 handle = 0; 159 if (device.tracking) device.dispose_Object(this); 160 device = null; 161 } 162 163 173 public boolean equals(Object object) { 174 if (object == this) return true; 175 if (!(object instanceof Font)) return false; 176 Font font = (Font) object; 177 return device == font.device && handle == font.handle; 178 } 179 180 192 public FontData[] getFontData() { 193 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 194 LOGFONT logFont = OS.IsUnicode ? (LOGFONT)new LOGFONTW() : new LOGFONTA(); 195 OS.GetObject(handle, LOGFONT.sizeof, logFont); 196 return new FontData[] {FontData.win32_new(logFont, device.computePoints(logFont, handle))}; 197 } 198 199 209 public int hashCode () { 210 return handle; 211 } 212 213 void init (Device device, FontData fd) { 214 if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 215 this.device = device; 216 LOGFONT logFont = fd.data; 217 int lfHeight = logFont.lfHeight; 218 logFont.lfHeight = device.computePixels(fd.height); 219 handle = OS.CreateFontIndirect(logFont); 220 logFont.lfHeight = lfHeight; 221 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 222 } 223 224 234 public boolean isDisposed() { 235 return handle == 0; 236 } 237 238 244 public String toString () { 245 if (isDisposed()) return "Font {*DISPOSED*}"; 246 return "Font {" + handle + "}"; 247 } 248 249 263 public static Font win32_new(Device device, int handle) { 264 if (device == null) device = Device.getDevice(); 265 Font font = new Font(); 266 font.handle = handle; 267 font.device = device; 268 return font; 269 } 270 271 } 272 | Popular Tags |