File: Text.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (120 lines) | stat: -rw-r--r-- 4,256 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ij.plugin;
import ij.*;
import ij.gui.*;
import java.awt.*;
import java.util.*;

/** This plugin implements the Edit/Options/Fonts command and 
	the dialog displayed when you double click on the text tool. */
public class Text implements PlugIn, DialogListener {
	private static final String LOC_KEY = "fonts.loc";
	private static final String[] styles = {"Plain", "Bold", "Italic", "Bold+Italic"};
	private static final String[] justifications = {"Left", "Center", "Right"};
	private static GenericDialog gd;
	private String font = TextRoi.getDefaultFontName();
	private int fontSize = TextRoi.getDefaultFontSize();
	private int style = TextRoi.getDefaultFontStyle();
	private int justification = TextRoi.getGlobalJustification();
	private int angle;
	private boolean antialiased = TextRoi.isAntialiased();
	private Color color = Toolbar.getForegroundColor();
	private String colorName;

 	public synchronized void run(String arg) {
 		if (gd!=null && gd.isVisible())
 			gd.toFront();
 		else
			showDialog();
	}
				
	private void showDialog() {
		ImagePlus imp = WindowManager.getCurrentImage();
		Roi roi = imp!=null?imp.getRoi():null;
		TextRoi textRoi = roi!=null&&(roi instanceof TextRoi)?(TextRoi)roi:null;
		String fillc = "None";
		TextRoi.setDefaultFillColor(null);
		TextRoi.setDefaultAngle(0.0);
		if (textRoi!=null) {
			Font font = textRoi.getCurrentFont();
			fontSize = font.getSize();
			angle = (int)textRoi.getAngle();
			style = font.getStyle();
			justification = textRoi.getJustification();
			Color c = textRoi.getStrokeColor();
			if (c!=null) color=c;
			fillc = Colors.colorToString2(textRoi.getFillColor());
			antialiased = textRoi.getAntialiased();
		}
		colorName = Colors.colorToString2(color);
		gd = new NonBlockingGenericDialog("Fonts");
		gd.addChoice("Font:", getFonts(), font);
		gd.addChoice("Style:", styles, styles[style]);
		gd.addChoice("Just:", justifications, justifications[justification]);
		gd.addChoice("Color:", Colors.getColors(colorName), colorName);
		gd.addChoice("Bkgd:", Colors.getColors("None",!"None".equals(fillc)?fillc:null), fillc);
		gd.addSlider("Size:", 9, 200, fontSize);
		gd.addSlider("Angle:", -90, 90, angle);
		gd.addCheckbox("Antialiased text", antialiased);
		Point loc = Prefs.getLocation(LOC_KEY);
		if (IJ.debugMode) {
			Dimension screen = IJ.getScreenSize();
			IJ.log("Fonts: "+loc+" "+screen);
		}
		if (loc!=null) {
			gd.centerDialog(false);
			gd.setLocation (loc);
		}
		gd.addDialogListener(this);
		gd.setOKLabel("Close");
		gd.hideCancelButton();
		gd.showDialog();
		Prefs.saveLocation(LOC_KEY, gd.getLocation());
	}

	String[] getFonts() {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] fonts = ge.getAvailableFontFamilyNames();
		ArrayList names = new ArrayList();
		names.add("SansSerif");
		names.add("Serif");
		names.add("Monospaced");
		for (int i=0; i<fonts.length; i++) {
			String f = fonts[i];
			if (f.length()<=20 && !(f.equals("SansSerif")||f.equals("Serif")||f.equals("Monospaced")))
				names.add(f);
		}
		return (String[])names.toArray(new String[names.size()]);
	}
	
	public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
		ImagePlus imp = WindowManager.getCurrentImage();
		Roi roi = imp!=null?imp.getRoi():null;
		TextRoi textRoi = roi!=null&&(roi instanceof TextRoi)?(TextRoi)roi:null;
		font = gd.getNextChoice();
		style = gd.getNextChoiceIndex();
		justification = gd.getNextChoiceIndex();
		String colorName2 = gd.getNextChoice();
		String fillc = gd.getNextChoice();
		fontSize = (int)gd.getNextNumber();
		angle = (int)gd.getNextNumber();
		antialiased = gd.getNextBoolean();
		if (colorName!=null && !colorName2.equals(colorName)) {
			Color color = Colors.decode(colorName2, null);
			Toolbar.setForegroundColor(color);
			colorName = colorName2;
		}
		TextRoi.setFont(font, fontSize, style, antialiased);
		TextRoi.setGlobalJustification(justification);
		Color fillColor = Colors.decode(fillc, null);
		TextRoi.setDefaultFillColor(fillColor);
		TextRoi.setDefaultAngle(angle);
		if (textRoi!=null) {
			textRoi.setAngle(angle);
			textRoi.setJustification(justification);
			textRoi.setFillColor(fillColor);
			textRoi.setAntialiased(antialiased);
		}
		return true;
	}
	
}