package ij.gui; import java.awt.*; import ij.*; import ij.process.*; import ij.util.Java2; /** This class is a rectangular ROI containing text. */ public class TextRoi extends Roi { static final int MAX_LINES = 50; private String[] theText = new String[MAX_LINES]; private static String name = "SansSerif"; private static int style = Font.PLAIN; private static int size = 18; private static Font font; private static boolean antialiasedText = true; private static boolean recordSetFont = true; private double previousMag; private boolean firstChar = true; private boolean firstMouseUp = true; private int cline = 0; public TextRoi(int x, int y, ImagePlus imp) { super(x, y, imp); ImageCanvas ic = imp.getCanvas(); double mag = (ic!=null)?ic.getMagnification():1.0; if (mag>1.0) mag = 1.0; if (size<(12/mag)) size = (int)(12/mag); theText[0] = "Type, then"; theText[1] = "Ctl+D"; if (previousRoi!=null && (previousRoi instanceof TextRoi)) { firstMouseUp = false; //IJ.write(""+previousRoi.getBounds()); previousRoi = null; } } /** Adds the specified character to the end of the text string. */ public void addChar(char c) { if (!(c>=' ' || c=='\b' || c=='\n')) return; if (firstChar) { cline = 0; theText[cline] = new String(""); for (int i=1; i0) theText[cline] = theText[cline].substring(0, theText[cline].length()-1); else if (cline>0) { theText[cline] = null; cline--; } imp.draw(clipX, clipY, clipWidth, clipHeight); firstChar = false; return; } else if ((int)c=='\n') { // newline if (cline<(MAX_LINES-1)) cline++; theText[cline] = ""; adjustSize(); } else { char[] chr = {c}; theText[cline] += new String(chr); adjustSize(); updateClipRect(); imp.draw(clipX, clipY, clipWidth, clipHeight); firstChar = false; return; } } Font getCurrentFont() { double mag = ic.getMagnification(); if (font==null || mag!=previousMag) { font = new Font(name, style, (int)(size*mag)); previousMag = mag; } return font; } /** Renders the text on the image. */ public void drawPixels(ImageProcessor ip) { Font font = new Font(name, style, size); ip.setFont(font); ip.setAntialiasedText(antialiasedText); FontMetrics metrics = ip.getFontMetrics(); int fontHeight = metrics.getHeight(); int descent = metrics.getDescent(); int i = 0; int yy = 0; while (iwidth) width = w; i++; } g.dispose(); width += 2; if (x+width>xMax) x = xMax-width; height = nLines*fontHeight+2; if (height>yMax) height = yMax; if (y+height>yMax) y = yMax-height; updateClipRect(); imp.draw(clipX, clipY, clipWidth, clipHeight); } int stringWidth(String s, FontMetrics metrics, Graphics g) { return Java2.getStringWidth(s, metrics, g); } public String getMacroCode(ImageProcessor ip) { String code = ""; if (recordSetFont) { String options = ""; if (style==Font.BOLD) options += "bold"; if (style==Font.ITALIC) options += " italic"; if (antialiasedText) options += " antialiased"; if (options.equals("")) options = "plain"; code += "setFont(\""+name+"\", "+size+", \""+options+"\");\n"; recordSetFont = false; } FontMetrics metrics = ip.getFontMetrics(); int fontHeight = metrics.getHeight(); String text = ""; for (int i=0; i