Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
TextRoi.java
1 package ij.gui;
2 import java.awt.*;
3 import ij.*;
4 import ij.process.*;
5 import ij.util.Java2;
6 
7 
9 public class TextRoi extends Roi {
10 
11  static final int MAX_LINES = 50;
12 
13  private String[] theText = new String[MAX_LINES];
14  private static String name = "SansSerif";
15  private static int style = Font.PLAIN;
16  private static int size = 18;
17  private static Font font;
18  private double previousMag;
19  private boolean firstChar = true;
20  private boolean firstMouseUp = true;
21  private int cline = 0;
22 
23  public TextRoi(int x, int y, ImagePlus imp) {
24  super(x, y, imp);
25  double mag = (imp!=null)?imp.getCanvas().getMagnification():1.0;
26  if (mag>1.0)
27  mag = 1.0;
28  if (size<(12/mag))
29  size = (int)(12/mag);
30  theText[0] = "Type, then";
31  theText[1] = "Ctl+D";
32  if (previousRoi!=null && (previousRoi instanceof TextRoi)) {
33  firstMouseUp = false;
34  //IJ.write(""+previousRoi.getBounds());
35  previousRoi = null;
36  }
37  }
38 
40  public void addChar(char c) {
41  if (!(c>=' ' || c=='\b' || c=='\n')) return;
42  if (firstChar) {
43  cline = 0;
44  theText[cline] = new String("");
45  for (int i=1; i<MAX_LINES; i++)
46  theText[i] = null;
47  }
48  if ((int)c=='\b') {
49  // backspace
50  if (theText[cline].length()>0)
51  theText[cline] = theText[cline].substring(0, theText[cline].length()-1);
52  else if (cline>0) {
53  theText[cline] = null;
54  cline--;
55  }
56  imp.draw(clipX, clipY, clipWidth, clipHeight);
57  firstChar = false;
58  return;
59  } else if ((int)c=='\n') {
60  // newline
61  if (cline<(MAX_LINES-1)) cline++;
62  theText[cline] = "";
63  adjustSize();
64  } else {
65  char[] chr = {c};
66  theText[cline] += new String(chr);
67  adjustSize();
68  updateClipRect();
69  imp.draw(clipX, clipY, clipWidth, clipHeight);
70  firstChar = false;
71  return;
72  }
73  }
74 
75  Font getCurrentFont() {
76  double mag = ic.getMagnification();
77  if (font==null || mag!=previousMag) {
78  font = new Font(name, style, (int)(size*mag));
79  previousMag = mag;
80  }
81  return font;
82  }
83 
85  public void drawPixels() {
86  if (imp==null)
87  return;
88  ImageProcessor ip = imp.getProcessor();
89  Font font = new Font(name, style, size);
90  ip.setFont(font);
91  ip.setAntialiasedText(true);
92  FontMetrics metrics = ip.getFontMetrics();
93  int fontHeight = metrics.getHeight();
94  int descent = metrics.getDescent();
95  int i = 0;
96  int yy = 0;
97  while (i<MAX_LINES && theText[i]!=null) {
98  ip.drawString(theText[i], x, y+yy+fontHeight);
99  i++;
100  yy += fontHeight;
101  }
102  }
103 
105  public void draw(Graphics g) {
106  super.draw(g); // draw the rectangle
107  g.setColor(ROIColor);
108  double mag = ic.getMagnification();
109  int sx = ic.screenX(x);
110  int sy = ic.screenY(y);
111  int swidth = (int)(width*mag);
112  int sheight = (int)(height*mag);
113  if (IJ.isJava2())
114  Java2.setAntialiasedText(g, true);
115  if (font==null)
116  adjustSize();
117  Font font = getCurrentFont();
118  FontMetrics metrics = g.getFontMetrics(font);
119  int fontHeight = metrics.getHeight();
120  int descent = metrics.getDescent();
121  g.setFont(font);
122  Rectangle r = g.getClipBounds();
123  g.setClip(sx, sy, swidth, sheight);
124  int i = 0;
125  while (i<MAX_LINES && theText[i]!=null) {
126  g.drawString(theText[i], sx, sy+fontHeight-descent);
127  i++;
128  sy += fontHeight;
129  }
130  if (r!=null) g.setClip(r.x, r.y, r.width, r.height);
131  }
132 
133  /*
134  void handleMouseUp(int screenX, int screenY) {
135  if (width<size || height<size)
136  grow(x+Math.max(size*5,width), y+Math.max((int)(size*1.5),height));
137  super.handleMouseUp(screenX, screenY);
138  }
139  */
140 
142  public static String getFont() {
143  return name;
144  }
145 
147  public static int getSize() {
148  return size;
149  }
150 
152  public static int getStyle() {
153  return style;
154  }
155 
157  public static void setFont(String fontName, int fontSize, int fontStyle) {
158  name = fontName;
159  size = fontSize;
160  style = fontStyle;
161  font = null;
162  ImagePlus imp = IJ.getInstance().getImagePlus();
163  if (imp!=null) {
164  Roi roi = imp.getRoi();
165  if (roi instanceof TextRoi)
166  imp.draw();
167  }
168  }
169 
170  //v1.24g
171  protected void handleMouseUp(int screenX, int screenY) {
172  super.handleMouseUp(screenX, screenY);
173  if (firstMouseUp) {
174  adjustSize();
175  firstMouseUp = false;
176  } else {
177  if (width<5 || height<5)
178  imp.killRoi();
179  }
180  }
181  //v1.24g
184  void adjustSize() {
185  if (ic==null)
186  return;
187  double mag = ic.getMagnification();
188  Font font = getCurrentFont();
189  Graphics g = ic.getGraphics();
190  if (IJ.isJava2())
191  Java2.setAntialiasedText(g, true);
192  FontMetrics metrics = g.getFontMetrics(font);
193  int fontHeight = (int)(metrics.getHeight()/mag);
194  int descent = metrics.getDescent();
195  int i=0, nLines=0;
196  oldX = x;
197  oldY = y;
198  oldWidth = width;
199  oldHeight = height;
200  width = 10;
201  while (i<MAX_LINES && theText[i]!=null) {
202  nLines++;
203  int w = (int)(stringWidth(theText[i],metrics,g)/mag);
204  if (w>width)
205  width = w;
206  i++;
207  }
208  g.dispose();
209  width += 2;
210  if (x+width>xMax)
211  x = xMax-width;
212  height = nLines*fontHeight+2;
213  if (height>yMax)
214  height = yMax;
215  if (y+height>yMax)
216  y = yMax-height;
217  updateClipRect();
218  imp.draw(clipX, clipY, clipWidth, clipHeight);
219  }
220 
221  int stringWidth(String s, FontMetrics metrics, Graphics g) {
222  if (IJ.isJava2())
223  return Java2.getStringWidth(s, metrics, g);
224  else
225  return metrics.stringWidth(s);
226  }
227 
228 }