1 30 31 package swingwt.awt; 32 33 import java.util.Hashtable ; 34 import java.util.Map ; 35 36 import swingwtx.swing.SwingUtilities; 37 38 public class Font { 39 40 private org.eclipse.swt.graphics.Font peer = null; 41 42 45 protected boolean disposeOnFinalize = true; 46 47 public static final int PLAIN = 0; 48 public static final int BOLD = 1; 49 public static final int ITALIC = 2; 50 public static final int ROMAN_BASELINE = 0; 51 public static final int CENTER_BASELINE = 1; 52 public static final int HANGING_BASELINE = 2; 53 public static final int TRUETYPE_FONT = 0; 54 55 56 protected String name = "Dialog"; 57 protected int style = PLAIN; 58 protected int size = 12; 59 60 public Font(final String name, final int style, final int size) { 61 this.name = name; 62 this.style = style; 63 this.size = size; 64 createPeer(); 65 } 66 67 public Font(final String name, final int style, final int size, boolean f) { 68 this.name = name; 69 this.style = style; 70 this.size = size; 71 createPeer(); 72 } 73 74 public Font(Map attributes) { 75 setAttributes(attributes); 76 createPeer(); 77 } 78 79 public Font(org.eclipse.swt.graphics.Font swtfont) { 80 populateFromSWTFont(swtfont); 81 } 82 83 public static Font getFont(Map attributes) { 84 return new Font(attributes); 85 } 86 87 88 protected void createPeer() { 89 SwingUtilities.invokeSync( new Runnable () { 90 public void run() { 91 int swtStyle = org.eclipse.swt.SWT.NONE; 93 if ((style & BOLD) > 0) 94 swtStyle = swtStyle | org.eclipse.swt.SWT.BOLD; 95 if ((style & ITALIC) > 0) 96 swtStyle = swtStyle | org.eclipse.swt.SWT.ITALIC; 97 peer = new org.eclipse.swt.graphics.Font(swingwtx.swing.SwingWTUtils.getDisplay(), name, size, swtStyle); 98 disposeOnFinalize = true; 101 } 102 }); 103 } 104 105 106 protected void populateFromSWTFont(org.eclipse.swt.graphics.Font swtfont) { 107 peer = swtfont; 109 disposeOnFinalize = false; 110 111 org.eclipse.swt.graphics.FontData[] fd = peer.getFontData(); 113 if (fd != null) 114 if (fd.length > 0) { 115 name = fd[0].getName(); 116 style = PLAIN; 117 if ((fd[0].getStyle() & org.eclipse.swt.SWT.BOLD) > 0) 118 style = style | BOLD; 119 if ((fd[0].getStyle() & org.eclipse.swt.SWT.ITALIC) > 0) 120 style = style | ITALIC; 121 size = fd[0].getHeight(); 122 } 123 } 124 125 public void setAttributes(Map attributes) { 126 if (attributes != null) { 127 String af = attributes.get("family").toString(); 128 if (af != null) this.name = attributes.get("family").toString(); 129 String as = attributes.get("size").toString(); 130 if (as != null) this.size = Integer.parseInt(attributes.get("size").toString()); 131 String ast = attributes.get("style").toString(); 132 if (ast != null) this.style = Integer.parseInt(attributes.get("style").toString()); 133 } 134 } 135 136 public Map getAttributes() { 137 Hashtable ht = new Hashtable (); 138 ht.put("family", this.name); 139 ht.put("size", new Integer (this.size)); 140 ht.put("style", new Integer (this.style)); 141 return ht; 142 } 143 144 public String getName() { return name; } 145 public int getStyle() { return style; } 146 public int getSize() { return size; } 147 public float getSize2D() { return (float) size; } 148 149 public boolean isBold() { return (style & BOLD) > 0; } 150 public boolean isPlain() { return style == PLAIN; } 151 public boolean isItalic() { return (style & ITALIC) > 0; } 152 153 public Font deriveFont(int style) { 154 return new Font(this.name, style, this.size); 155 } 156 157 public org.eclipse.swt.graphics.Font getSWTFont() { 158 return peer; 159 } 160 161 public void dispose() { 162 peer.dispose(); 163 } 164 165 protected void finalize() throws Throwable { 166 if (disposeOnFinalize) dispose(); 167 } 168 } 169 | Popular Tags |