FontViewer.java

Go to the documentation of this file.
00001 // (C) 2003 by Dominique Unruh. GPL 00002 00003 import javax.swing.*; 00004 import java.awt.*; 00005 import javax.swing.text.*; 00006 00011 public class FontViewer extends JFrame { 00013 final static int FONTSIZE = 18; 00014 final static String EXAMPLE_STRING = "aA äÄ щЩ γΓ ש ดี 本 は 한 ≠ ⠃⠗⠇"; 00015 final static int UNICODE_MAX = 0xFFFF; 00016 00017 JTextPane pane = null; 00018 00022 public FontViewer() { 00023 init(800,600); 00024 } 00025 00031 public FontViewer(int width, int height) { 00032 init(width,height); 00033 } 00034 00035 private void init(int width, int height) { 00036 setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 00037 setTitle("Font Viewer"); 00038 pane = new JTextPane(); 00039 JScrollPane scroll = new JScrollPane(); 00040 scroll.setViewportView(pane); 00041 pane.setEditable(false); 00042 getContentPane().add(scroll); 00043 setSize(width,height); 00044 setVisible(true); 00045 pane.requestFocus(); 00046 } 00047 00048 public void showSample(String text) { 00049 if (text==null || text.length()==0) text = EXAMPLE_STRING; 00050 addFonts(text); 00051 pane.setCaretPosition(0); 00052 }; 00053 00054 void showFont(String fontName) { 00055 Font font = new Font(fontName,Font.PLAIN,FONTSIZE); 00056 SimpleAttributeSet defaultStyle = new SimpleAttributeSet(); 00057 StyleConstants.setFontSize(defaultStyle,FONTSIZE); 00058 SimpleAttributeSet underlinedStyle = 00059 new SimpleAttributeSet(defaultStyle); 00060 StyleConstants.setUnderline(underlinedStyle,true); 00061 SimpleAttributeSet fontStyle = new SimpleAttributeSet(defaultStyle); 00062 StyleConstants.setFontFamily(fontStyle,fontName); 00063 Document doc = pane.getDocument(); 00064 00065 try { 00066 doc.insertString(doc.getLength(),"Repertoire of ",defaultStyle); 00067 doc.insertString(doc.getLength(),fontName,underlinedStyle); 00068 doc.insertString 00069 (doc.getLength()," upto Unicode code position 0x"+ 00070 Integer.toString(UNICODE_MAX,16).toUpperCase()+":\n\n", 00071 defaultStyle); 00072 } catch (BadLocationException e) {}; 00073 00074 int blockStart = 32; 00075 StringBuffer block = new StringBuffer(); 00076 for (int i=blockStart; i<UNICODE_MAX; i++) { 00077 if (Character.getType((char)i)!=Character.CONTROL) 00078 block.append((char)i); 00079 if ((i+1)%0x20==0 || i>=UNICODE_MAX) { 00080 String blockStr = block.toString(); 00081 if (font.canDisplayUpTo(blockStr)!=0 && blockStr.length()!=0) { 00082 try { 00083 doc.insertString(doc.getLength(), 00084 Integer.toString(blockStart,16). 00085 toUpperCase()+": ", 00086 defaultStyle); 00087 doc.insertString(doc.getLength(), 00088 "\u202D"+blockStr+"\u202C\n", 00089 fontStyle); 00090 } catch (BadLocationException e) {}; 00091 } 00092 block.setLength(0); blockStart = i+1; 00093 } 00094 } 00095 pane.setCaretPosition(0); 00096 } 00097 00098 void addFonts(String text) { 00099 String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment(). 00100 getAvailableFontFamilyNames(); 00101 int fontQual[] = new int[fonts.length]; 00102 Document doc = pane.getDocument(); 00103 SimpleAttributeSet defaultStyle = new SimpleAttributeSet(); 00104 StyleConstants.setFontSize(defaultStyle,FONTSIZE); 00105 SimpleAttributeSet underlinedStyle = 00106 new SimpleAttributeSet(defaultStyle); 00107 StyleConstants.setUnderline(underlinedStyle,true); 00108 SimpleAttributeSet boldStyle = new SimpleAttributeSet(defaultStyle); 00109 StyleConstants.setBold(boldStyle,true); 00110 int maxQual = 0, possibleQual = 0; 00111 for (int i=0; i<fonts.length; i++) { 00112 int qual = 0; possibleQual = 0; 00113 Font font = new Font(fonts[i],Font.PLAIN,FONTSIZE); 00114 for (int j=0; j<text.length(); j++) { 00115 char c = text.charAt(j); 00116 if (c!=' ' && c!='\t' && c!='\n') { 00117 possibleQual++; 00118 if (font.canDisplay(c)) { 00119 qual++; 00120 }}}; 00121 fontQual[i] = qual; 00122 if (maxQual < qual) maxQual = qual; 00123 } 00124 00125 while (maxQual>0) { 00126 int newMaxQual = 0; 00127 String info = 00128 "Fonts displaying "+ 00129 ((maxQual==possibleQual)?"all":""+maxQual)+ 00130 ((maxQual==1 && maxQual!=possibleQual)? 00131 " character":" characters")+ 00132 ":\n"; 00133 try { 00134 doc.insertString(doc.getLength(),info,underlinedStyle); 00135 } catch (BadLocationException e) {}; 00136 00137 for (int i=0; i<fonts.length; i++) { 00138 String name = fonts[i]; 00139 if (name==null) continue; 00140 if (fontQual[i]<maxQual) { 00141 if (fontQual[i]>newMaxQual) newMaxQual = fontQual[i]; 00142 continue; 00143 } 00144 fonts[i] = null; 00145 00146 try { 00147 doc.insertString(doc.getLength(), 00148 ((name.length()==0)?"<empty>":name)+": ", 00149 boldStyle); 00150 SimpleAttributeSet fontStyle = 00151 new SimpleAttributeSet(defaultStyle); 00152 StyleConstants.setFontFamily(fontStyle,name); 00153 doc.insertString(doc.getLength(), text, fontStyle); 00154 doc.insertString(doc.getLength(), "\n", defaultStyle); 00155 } catch (BadLocationException e) { 00156 e.printStackTrace(); 00157 } 00158 } 00159 maxQual = newMaxQual; 00160 try { 00161 doc.insertString(doc.getLength(), "\n", defaultStyle); 00162 } catch (BadLocationException e) {}; 00163 } 00164 00165 StringBuffer rest = new StringBuffer(); 00166 int numRest = 0; 00167 for (int i=0; i<fonts.length; i++) { 00168 String name = fonts[i]; 00169 if (name==null) continue; 00170 if (numRest!=0) rest.append(", "); 00171 rest.append(name); 00172 numRest++; 00173 } 00174 00175 try { 00176 if (numRest>0) { 00177 doc.insertString(doc.getLength(), 00178 "Font"+((numRest>1)?"s":"")+ 00179 " displaying nothing at all:\n", 00180 underlinedStyle); 00181 doc.insertString(doc.getLength(),rest.toString(), 00182 boldStyle); 00183 } 00184 } catch (BadLocationException e) {}; 00185 00186 } 00187 00188 public static void main(String args[]) { 00189 StringBuffer arg = new StringBuffer(); 00190 for (int i=0; i<args.length; i++) { 00191 if (i>0) arg.append(" "); 00192 arg.append(args[i]); 00193 } 00194 FontViewer fv = new FontViewer(); 00195 //fv.showSample(arg.toString()); 00196 fv.showFont(arg.toString()); 00197 } 00198 }

Generated on Sun Aug 15 11:56:53 2004 for International Input by doxygen 1.3.7