1 23 24 package swingwt.awt; 25 26 import swingwt.awt.font.*; 27 import swingwt.awt.geom.*; 28 29 import swingwtx.swing.SwingUtilities; 30 31 public class FontMetrics { 32 33 protected Font font; 34 35 36 protected org.eclipse.swt.graphics.GC gc = null; 37 38 protected org.eclipse.swt.graphics.Image im = null; 39 40 protected FontMetrics(Font font) { this.font = font; } 41 42 private void checkGC() { 43 SwingUtilities.invokeSync(new Runnable () { 44 public void run() { 45 if (gc != null) return; 46 im = new org.eclipse.swt.graphics.Image(swingwtx.swing.SwingWTUtils.getDisplay(), 100, 100); 47 gc = new org.eclipse.swt.graphics.GC(im); 48 } 49 }); 50 } 51 52 public Font getFont() { return font; } 53 public int getLeading() { return 0; } 54 public int getAscent() { 55 final int[] ret = new int[1]; 56 SwingUtilities.invokeSync(new Runnable () { 57 public void run() { 58 checkGC(); 59 ret[0] = gc.stringExtent("I").y; 60 } 61 }); 62 return ret[0]; 63 } 64 public int getDescent() { return 0; } 65 public int getHeight() { return getLeading() + getAscent() + getDescent(); } 66 public int getMaxAscent() { return getAscent(); } 67 public int getMaxDescent() { return getDescent(); } 68 public int getMaxDecent() { return getMaxDescent(); } 69 public int getMaxAdvance() { return -1; } 70 public int charWidth(int ch) { return charWidth((char)ch); } 71 public int charWidth(char ch) { 72 if (ch < 256) { 73 return getWidths()[ch]; 74 } 75 char data[] = {ch}; 76 return charsWidth(data, 0, 1); 77 } 78 public int stringWidth(final String str) { 79 final int[] ret = new int[1]; 80 SwingUtilities.invokeSync(new Runnable () { 81 public void run() { 82 checkGC(); 83 ret[0] = gc.stringExtent(str).x; 84 } 85 }); 86 return ret[0]; 87 } 88 public int charsWidth(char data[], int off, int len) { 89 return stringWidth(new String (data, off, len)); 90 } 91 public int bytesWidth(byte data[], int off, int len) { 92 return stringWidth(new String (data).substring(off, off+len)); 93 } 94 public int[] getWidths() { 95 int widths[] = new int[256]; 96 for (char ch = 0 ; ch < 256 ; ch++) { 97 widths[ch] = charWidth(ch); 98 } 99 return widths; 100 } 101 public boolean hasUniformLineMetrics() { return false; } 102 103 public LineMetrics getLineMetrics( String str, Graphics context) { 104 return new LineMetrics(str.length(), getHeight()); 105 } 106 107 public LineMetrics getLineMetrics( String str, 108 int beginIndex, int limit, 109 Graphics context) { 110 return new LineMetrics(limit - beginIndex, getHeight()); 111 } 112 113 public LineMetrics getLineMetrics(char [] chars, 114 int beginIndex, int limit, 115 Graphics context) { 116 return getLineMetrics( 117 new String (chars), beginIndex, limit, context); 118 } 119 public Rectangle2D getStringBounds(final String str, Graphics context) { 120 final Rectangle[] ret = new Rectangle[1]; 121 SwingUtilities.invokeSync(new Runnable () { 122 public void run() { 123 checkGC(); 124 org.eclipse.swt.graphics.Point p = gc.stringExtent(str); 125 ret[0] = new Rectangle(p.x, p.y); 126 } 127 }); 128 return ret[0]; 129 } 130 public Rectangle2D getStringBounds( String str, 131 int beginIndex, int limit, 132 Graphics context) { 133 return getStringBounds(str, context); 134 } 135 public Rectangle2D getStringBounds( char [] chars, 136 int beginIndex, int limit, 137 Graphics context) { 138 return getStringBounds(new String (chars), context); 139 } 140 public Rectangle2D getMaxCharBounds(Graphics context) { 141 return new Rectangle(0, 0); 142 } 143 public String toString() { 144 return getClass().getName() + 145 "[font=" + getFont() + 146 "ascent=" + getAscent() + 147 ", descent=" + getDescent() + 148 ", height=" + getHeight() + "]"; 149 } 150 151 } 152 | Popular Tags |