1 11 package org.eclipse.jface.resource; 12 13 import org.eclipse.swt.graphics.Device; 14 import org.eclipse.swt.graphics.Font; 15 import org.eclipse.swt.graphics.FontData; 16 import org.eclipse.swt.widgets.Display; 17 18 26 public abstract class FontDescriptor extends DeviceResourceDescriptor { 27 28 42 public static FontDescriptor createFrom(Font font, Device originalDevice) { 43 return new ArrayFontDescriptor(font); 44 } 45 46 57 public static FontDescriptor createFrom(Font font) { 58 return new ArrayFontDescriptor(font); 59 } 60 61 71 public static FontDescriptor createFrom(FontData[] data) { 72 return new ArrayFontDescriptor(data); 73 } 74 75 81 public static FontDescriptor createFrom(FontData data) { 82 return new ArrayFontDescriptor(new FontData[]{data}); 83 } 84 85 95 public static FontDescriptor createFrom(String name, int height, int style) { 96 return createFrom(new FontData(name, height, style)); 97 } 98 99 106 public FontData[] getFontData() { 107 Font tempFont = createFont(Display.getCurrent()); 108 FontData[] result = tempFont.getFontData(); 109 destroyFont(tempFont); 110 return result; 111 } 112 113 121 public static FontData[] copy(FontData[] original) { 122 FontData[] result = new FontData[original.length]; 123 for (int i = 0; i < original.length; i++) { 124 FontData next = original[i]; 125 126 result[i] = copy(next); 127 } 128 129 return result; 130 } 131 132 139 public static FontData copy(FontData next) { 140 FontData result = new FontData(next.getName(), next.getHeight(), next.getStyle()); 141 result.setLocale(next.getLocale()); 142 return result; 143 } 144 145 156 public final FontDescriptor setStyle(int style) { 157 FontData[] data = getFontData(); 158 159 for (int i = 0; i < data.length; i++) { 160 FontData next = data[i]; 161 162 next.setStyle(style); 163 } 164 165 FontDescriptor result = new ArrayFontDescriptor(data); 168 if (result.equals(this)) { 169 return this; 170 } 171 172 return result; 173 } 174 175 185 public final FontDescriptor withStyle(int style) { 186 FontData[] data = getFontData(); 187 188 for (int i = 0; i < data.length; i++) { 189 FontData next = data[i]; 190 191 next.setStyle(next.getStyle() | style); 192 } 193 194 FontDescriptor result = new ArrayFontDescriptor(data); 197 if (result.equals(this)) { 198 return this; 199 } 200 201 return result; 202 } 203 204 214 public final FontDescriptor setHeight(int height) { 215 FontData[] data = getFontData(); 216 217 for (int i = 0; i < data.length; i++) { 218 FontData next = data[i]; 219 220 next.setHeight(height); 221 } 222 223 FontDescriptor result = new ArrayFontDescriptor(data); 226 if (result.equals(this)) { 227 return this; 228 } 229 230 return result; 231 } 232 233 245 public final FontDescriptor increaseHeight(int heightDelta) { 246 if (heightDelta == 0) { 247 return this; 248 } 249 FontData[] data = getFontData(); 250 251 for (int i = 0; i < data.length; i++) { 252 FontData next = data[i]; 253 254 next.setHeight(next.getHeight() + heightDelta); 255 } 256 257 return new ArrayFontDescriptor(data); 258 } 259 260 269 public abstract Font createFont(Device device) throws DeviceResourceException; 270 271 279 public abstract void destroyFont(Font previouslyCreatedFont); 280 281 284 public final Object createResource(Device device) throws DeviceResourceException { 285 return createFont(device); 286 } 287 288 291 public final void destroyResource(Object previouslyCreatedObject) { 292 destroyFont((Font)previouslyCreatedObject); 293 } 294 } 295 | Popular Tags |