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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
package ij.plugin.frame;
import java.awt.*;
import java.awt.event.*;
import ij.*;
import ij.plugin.*;
import ij.gui.*;
/** Displays a window that allows the user to set the font, size and style. */
public class Fonts extends PlugInFrame implements PlugIn, ItemListener {
public static final String LOC_KEY = "fonts.loc";
private static String[] sizes = {"8","9","10","12","14","18","24","28","36","48","60","72","100","150","225","350"};
private static int[] isizes = {8,9,10,12,14,18,24,28,36,48,60,72,100,150,225,350};
private Panel panel;
private Choice font;
private Choice size;
private Choice style;
private Checkbox checkbox;
private static Frame instance;
public Fonts() {
super("Fonts");
if (instance!=null) {
WindowManager.toFront(instance);
return;
}
WindowManager.addWindow(this);
instance = this;
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
font = new Choice();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
font.add("SansSerif");
font.add("Serif");
font.add("Monospaced");
for (int i=0; i<fonts.length; i++) {
String f = fonts[i];
if (!(f.equals("SansSerif")||f.equals("Serif")||f.equals("Monospaced")))
font.add(f);
}
font.select(TextRoi.getFont());
font.addItemListener(this);
add(font);
size = new Choice();
for (int i=0; i<sizes.length; i++)
size.add(sizes[i]);
size.select(getSizeIndex());
size.addItemListener(this);
add(size);
style = new Choice();
style.add("Plain");
style.add("Bold");
style.add("Italic");
style.add("Bold+Italic");
style.add("Center");
style.add("Right");
style.add("Center+Bold");
style.add("Right+Bold");
int i = TextRoi.getStyle();
int justificaton = TextRoi.getGlobalJustification();
String s = "Plain";
if (i==Font.BOLD) {
if (justificaton==TextRoi.CENTER)
s = "Center+Bold";
else if (justificaton==TextRoi.RIGHT)
s = "Right+Bold";
else
s = "Bold";
} else if (i==Font.ITALIC)
s = "Italic";
else if (i==(Font.BOLD+Font.ITALIC))
s = "Bold+Italic";
else if (i==Font.PLAIN) {
if (justificaton==TextRoi.CENTER)
s = "Center";
else if (justificaton==TextRoi.RIGHT)
s = "Right";
}
style.select(s);
style.addItemListener(this);
add(style);
checkbox = new Checkbox("Smooth", TextRoi.isAntialiased());
add(checkbox);
checkbox.addItemListener(this);
pack();
Point loc = Prefs.getLocation(LOC_KEY);
if (loc!=null)
setLocation(loc);
else
GUI.center(this);
show();
IJ.register(Fonts.class);
}
int getSizeIndex() {
int size = TextRoi.getSize();
int index=0;
for (int i=0; i<isizes.length; i++) {
if (size>=isizes[i])
index = i;
}
return index;
}
public void itemStateChanged(ItemEvent e) {
String fontName = font.getSelectedItem();
int fontSize = Integer.parseInt(size.getSelectedItem());
String styleName = style.getSelectedItem();
int fontStyle = Font.PLAIN;
int justification = TextRoi.LEFT;
if (styleName.endsWith("Bold"))
fontStyle = Font.BOLD;
else if (styleName.equals("Italic"))
fontStyle = Font.ITALIC;
else if (styleName.equals("Bold+Italic"))
fontStyle = Font.BOLD+Font.ITALIC;
if (styleName.startsWith("Center"))
justification = TextRoi.CENTER;
else if (styleName.startsWith("Right"))
justification = TextRoi.RIGHT;
TextRoi.setFont(fontName, fontSize, fontStyle, checkbox.getState());
TextRoi.setGlobalJustification(justification);
IJ.showStatus(fontSize+" point "+fontName + " " + styleName);
}
public void close() {
super.close();
instance = null;
Prefs.saveLocation(LOC_KEY, getLocation());
}
}
|