home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / AWT / FontTest / FontTest.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  12.0 KB  |  439 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import FontTestFrame;
  4. import com.ms.awt.*;
  5. import com.ms.lang.*;
  6. import java.awt.peer.ComponentPeer;
  7. import com.ms.awt.peer.ComponentPeerX;
  8.  
  9. public class FontTest extends Applet
  10. {
  11.     boolean        m_fStandAlone = false;
  12.     myCanvas    textArea;
  13.     Choice         KeyboardList;
  14.     TextField     fontInfo;
  15.     Choice        fontInfoChoice;
  16.     Choice        fontMetricsList;
  17.     TextField     metricInfo;
  18.     TextField    currentKbd;
  19.     Button        btnChooseFont;    
  20. //    Button        btnGetGlyph;
  21.     String        kbdLanguages[];
  22.     int            kbdIDs[];
  23.     int         nLanguages;
  24.     Checkbox    chkFudgeOutline;
  25.     
  26.     String fontTypes[] = 
  27.     {
  28.        "getFamily",
  29.        "getName",
  30.        "getSize",
  31.        "getStyle",
  32.        "hashCode",
  33.        "isBold",
  34.        "isItalic",
  35.        "isPlain",
  36.        "isStrikeOut",
  37.        "isUnderline",
  38.        "isTypeable",
  39.        "toString"
  40.     };
  41.  
  42.     String metricTypes[] =
  43.     {
  44.        "bytesWidth",
  45.        "charWidth",
  46.        "charsWidth",
  47.        "faceName",
  48.        "getAscent",
  49.        "getDescent",
  50.        "getFont",
  51.        "getHeight",
  52.        "getLeading",
  53.        "getMaxAdvance",
  54.        "getMaxDecent",
  55.        "getMaxDescent",
  56.        "getWidths",
  57.        "stringWidth",
  58.        "toString"
  59.     };
  60.  
  61.     ////////////////////////////////////////////////////////////////////////////
  62.  
  63.      private void ChangeKeyboard()
  64.      {
  65.          String kbd;
  66.         int lcid = SystemX.LANG_ENGLISH;
  67.         
  68.          kbd = KeyboardList.getSelectedItem();
  69.         lcid = 911;
  70.          for(int i=0; i<nLanguages; i++)
  71.          {
  72.              if( kbd.equals(kbdLanguages[i]))
  73.              {
  74.                  lcid = kbdIDs[i];
  75.                  break;
  76.              }
  77.          }
  78.          // note, we may still have 911 here, this will test the API will not fault
  79.          SystemX.setKeyboardLanguage(this, lcid);
  80.         getCurrentKeyboard();
  81.      }
  82.      
  83.     ////////////////////////////////////////////////////////////////////////////
  84.  
  85.     void setKbdLanguages()
  86.     {
  87.         nLanguages   = SystemX.getNumKeyboardLanguages();
  88.         kbdIDs          = SystemX.getKeyboardLanguages();
  89.         kbdLanguages = new String[nLanguages];
  90.         for(int i=0; i<nLanguages; i++)
  91.         {
  92.             kbdLanguages[i] = SystemX.getKeyboardLanguageName(kbdIDs[i]);
  93.             if( kbdLanguages[i] != null )
  94.                 KeyboardList.addItem(kbdLanguages[i]);
  95.         }
  96.         KeyboardList.addItem("oops!");
  97.     }
  98.     
  99.     ////////////////////////////////////////////////////////////////////////////
  100.      
  101.     public void getCurrentKeyboard()
  102.     {
  103.         int id = SystemX.getKeyboardLanguage();
  104.         currentKbd.setText(SystemX.getKeyboardLanguageName(id));
  105.     }
  106.  
  107.     ////////////////////////////////////////////////////////////////////////////
  108.  
  109.     public static void main(String args[])
  110.     {
  111.         FontTestFrame frame = new FontTestFrame("FontTest");
  112.  
  113.         // Must show Frame before we size it so insets() will return valid values
  114.         //----------------------------------------------------------------------
  115.         frame.show();
  116.         frame.hide();
  117.         frame.resize(frame.insets().left + frame.insets().right  + 600,
  118.                      frame.insets().top  + frame.insets().bottom + 400);
  119.  
  120.         FontTest applet_FontTest = new FontTest();
  121.  
  122.         frame.add("Center", applet_FontTest);
  123.         applet_FontTest.m_fStandAlone = true;
  124.         applet_FontTest.init();
  125.         applet_FontTest.start();
  126.         frame.show();
  127.     }
  128.  
  129.     ////////////////////////////////////////////////////////////////////////////
  130.  
  131.     public FontTest()
  132.     {
  133.     }
  134.  
  135.     ////////////////////////////////////////////////////////////////////////////
  136.  
  137.     public String getAppletInfo()
  138.     {
  139.         return "Name: FontTest\r\n" +
  140.                "Author: David Silver\r\n" +
  141.                "";
  142.     }
  143.  
  144.     ////////////////////////////////////////////////////////////////////////////
  145.  
  146.     public void init()
  147.     {
  148.         resize(600, 400);
  149.         System.out.println("FontTest::FontTest --> starting");
  150.         
  151.         // local variables
  152.         int i;
  153.         String fontNames[];
  154.         Panel fontsPanel = new Panel();
  155.         Panel tempPanel ;
  156.  
  157.         fontsPanel.setLayout( new GridLayout(3, 1) );
  158.  
  159.         // create fonts panel
  160.         tempPanel = new Panel();
  161.         tempPanel.add( btnChooseFont = new Button("Choose Font"));
  162.         tempPanel.add( chkFudgeOutline = new Checkbox("Force Outline"));
  163.         tempPanel.add( new Label("Font:") );
  164.         tempPanel.add( fontInfoChoice = new Choice() );
  165.         tempPanel.add( fontInfo = new TextField("Unknown method", 40) );
  166.         fontInfo.setEditable(false);
  167.         fontsPanel.add( tempPanel );
  168.  
  169.         // create keyboards panel
  170.         tempPanel = new Panel();
  171.         tempPanel.add( new Label("Keyboards:") );
  172.         tempPanel.add( KeyboardList = new Choice() );
  173. //        tempPanel.add( btnGetGlyph = new Button("Get Glyph"));
  174.         tempPanel.add( new Label("Current Keyboard:"));
  175.         tempPanel.add( currentKbd = new TextField("Unknown keyboard", 40));
  176.         currentKbd.setEditable(false);
  177.         fontsPanel.add( tempPanel );
  178.  
  179.         // font metrics stuff
  180.         tempPanel = new Panel();
  181.         tempPanel.add( new Label("Font Metrics:") );
  182.         tempPanel.add( fontMetricsList = new Choice() );
  183.         tempPanel.add( metricInfo = new TextField("Unknown metric", 40) );
  184.         metricInfo.setEditable(false);
  185.         fontsPanel.add( tempPanel );
  186.  
  187.         // set font choice types
  188.         for (i = 0; i < fontTypes.length; ++i)
  189.             fontInfoChoice.addItem( fontTypes[i] );
  190.  
  191.         // set Metrics info choice
  192.         for (i = 0; i < metricTypes.length; ++i)
  193.             fontMetricsList.addItem( metricTypes[i] );
  194.  
  195.         // set keyboard list choice
  196.         setKbdLanguages();
  197.         getCurrentKeyboard();    
  198.         // place panels into applet
  199.         setLayout( new BorderLayout() );
  200.         add( "North", fontsPanel );
  201.         add( "Center", textArea = new myCanvas() );
  202.  
  203.         System.out.println("Components heirarchy....");
  204.         Component c = this;
  205.         while (c != null)
  206.         {
  207.             System.out.println("   " + c.toString() + " : " + c.getClass().getName());
  208.             c = c.getParent();
  209.         }
  210.         System.out.println("FontTest::FontTest --> Leaving");
  211.     }
  212.  
  213.     ////////////////////////////////////////////////////////////////////////////
  214.  
  215.     public void paint(Graphics g)
  216.     {
  217.         // EventX currently not implemented for this release, check outside world
  218.         // didnt' change on us.
  219.         getCurrentKeyboard();
  220.     }
  221.  
  222.     ////////////////////////////////////////////////////////////////////////////
  223.      
  224.     public boolean action(Event evt, Object obj)
  225.     {
  226.         System.out.println("FontTest::action()");
  227.         Object target = evt.target;
  228.  
  229.         if( (evt.id == Event.GOT_FOCUS) && (target == this))
  230.             textArea.requestFocus();            
  231.  
  232.         else if (target.equals(fontMetricsList) )
  233.             getMetrics();
  234.  
  235.         else if (target.equals(fontInfoChoice) )
  236.             getFontInfo();
  237.  
  238.         else if (target.equals(KeyboardList) )
  239.             ChangeKeyboard();
  240.  
  241.         else if (target.equals(btnChooseFont))
  242.             setTextFont();  
  243.  
  244. //        else if (target.equals(btnGetGlyph))
  245. //            useGlyphOutline();  
  246.  
  247.         else
  248.             return super.action(evt, obj);
  249.  
  250.         return true;
  251.     }
  252.  
  253.     ////////////////////////////////////////////////////////////////////////////
  254.  
  255.     private void getFontInfo()
  256.     {
  257.         Font  tempFont  = textArea.getFont();
  258.         FontX tempFontX = null;
  259.         
  260.         if( tempFont instanceof FontX )
  261.             tempFontX = (FontX)tempFont;
  262.             
  263.         String label = fontInfoChoice.getSelectedItem();
  264.        
  265.         // which string is it
  266.         if ( label.equals("getFamily") )
  267.             fontInfo.setText( tempFont.getFamily() );
  268.  
  269.         else if ( label.equals("getName") )
  270.             fontInfo.setText( tempFont.getName() );
  271.  
  272.         else if ( label.equals("getSize") )
  273.         {
  274.             int i = tempFont.getSize();
  275.             fontInfo.setText( Integer.toString( i ) );
  276.             textArea.setHeight(i);            
  277.         }
  278.         else if ( label.equals("getStyle") )
  279.         {
  280.             if (tempFont.getStyle() == Font.PLAIN)
  281.                 fontInfo.setText("Plain");
  282.             else
  283.             {
  284.                 if ( (tempFont.getStyle() & Font.BOLD) != 0 && (tempFont.getStyle() & Font.ITALIC) != 0 )
  285.                     fontInfo.setText( "Bold, Italic "); 
  286.                 else if ( (tempFont.getStyle() & Font.ITALIC) != 0 )
  287.                     fontInfo.setText( "Italic" );
  288.                 else if ( (tempFont.getStyle() & Font.BOLD) != 0 )
  289.                     fontInfo.setText("Bold");
  290.                 else
  291.                     fontInfo.setText("Unknown Style");
  292.             }
  293.         }
  294.  
  295.         else if ( label.equals("hashCode") )
  296.             fontInfo.setText( Integer.toString( tempFont.hashCode() ) );
  297.  
  298.         else if ( label.equals("isBold") )
  299.             fontInfo.setText( new Boolean( tempFont.isBold() ).toString() );
  300.  
  301.         else if ( label.equals("isItalic") )       
  302.             fontInfo.setText( new Boolean( tempFont.isItalic() ).toString() );
  303.  
  304.         else if ( label.equals("isPlain") )
  305.             fontInfo.setText( new Boolean( tempFont.isPlain() ).toString() );
  306.  
  307.         else if ( label.equals("toString") )
  308.             fontInfo.setText( tempFont.toString() );
  309.  
  310.         else if (tempFontX != null )
  311.         {
  312.             if ( label.equals("isTypeable") )
  313.                 fontInfo.setText(tempFontX.isTypeable(SystemX.getKeyboardLanguage()) ?"true" :"false");
  314.             else if ( label.equals("isStrikeOut") )       
  315.                 fontInfo.setText( ((tempFontX.getFlags() & FontX.STRIKEOUT) != 0) ?"yes" :"no");
  316.             else if ( label.equals("isUnderline") )       
  317.                 fontInfo.setText( ((tempFontX.getFlags() & FontX.UNDERLINE) != 0) ?"yes" :"no");
  318.             else
  319.                 fontInfo.setText("Unknown method call");
  320.         }
  321.         
  322.         else
  323.             fontInfo.setText( "(font is not FontX)" );
  324.  
  325.     }
  326.  
  327.     ////////////////////////////////////////////////////////////////////////////
  328.      
  329.     private void getMetrics()
  330.     {
  331.         FontMetrics fm = getToolkit().getFontMetrics( textArea.getFont() );
  332.         System.out.println("Metrics class is " + fm.getClass().getName());
  333.  
  334.         String label = fontMetricsList.getSelectedItem();
  335.         byte bytes[] = { 'a', 'b', 'c', 'd', 'e' };
  336.         char chars[] = { 'A', 'B', 'C', 'D', 'E' };
  337.         int widths[];
  338.         int i;
  339.         StringBuffer tempString = new StringBuffer("");
  340.  
  341.         if ( label.equals("bytesWidth") )
  342.             metricInfo.setText( Integer.toString( fm.bytesWidth(bytes,0, bytes.length) ) );
  343.  
  344.         else if ( label.equals("charWidth") )
  345.             metricInfo.setText( Integer.toString( fm.charWidth('W') ) );
  346.  
  347.         else if ( label.equals("charsWidth") )
  348.         {
  349.             char[] c = (textArea.getText()).toCharArray();
  350.             metricInfo.setText( Integer.toString( fm.charsWidth(c, 0, textArea.nChars) ) );
  351.         }
  352.     
  353.        else if ( label.equals("stringWidth") )
  354.             metricInfo.setText( Integer.toString( fm.stringWidth( textArea.getText() ) ) );
  355.  
  356.         else if ( label.equals("getAscent") )
  357.             metricInfo.setText( Integer.toString( fm.getAscent() ) );
  358.  
  359.         else if ( label.equals("getDescent") )
  360.             metricInfo.setText( Integer.toString( fm.getDescent() ) );
  361.  
  362.         else if ( label.equals("getFont") )
  363.             metricInfo.setText( fm.getFont().toString() );
  364.  
  365.         else if ( label.equals("getHeight") )
  366.             metricInfo.setText( Integer.toString( fm.getHeight() ) );
  367.  
  368.         else if ( label.equals("getLeading") )
  369.             metricInfo.setText( Integer.toString( fm.getLeading() ) ) ;
  370.  
  371.         else if ( label.equals("getMaxAdvance") )
  372.             metricInfo.setText( Integer.toString( fm.getMaxAdvance() ) );
  373.  
  374.        else if ( label.equals("getMaxDecent") ) 
  375.             metricInfo.setText( Integer.toString( fm.getMaxDecent() ) );
  376.  
  377.        else if ( label.equals("getMaxDescent") )
  378.             metricInfo.setText( Integer.toString( fm.getMaxDescent() ) );
  379.  
  380.         else if ( label.equals("getWidths") )
  381.         {
  382.             widths = fm.getWidths();
  383.             for (i = 0; i < widths.length; ++i)
  384.                 tempString.append( Integer.toString(widths[i]) + ", " );
  385.  
  386.             metricInfo.setText( tempString.toString() );
  387.         }
  388.  
  389.         else if ( label.equals("toString") )
  390.             metricInfo.setText( fm.toString() );
  391.  
  392.         else if( fm instanceof FontMetricsX )
  393.         {
  394.             FontMetricsX fmX = (FontMetricsX)fm;
  395.  
  396.             if ( label.equals("faceName") )
  397.                 metricInfo.setText(fmX.getFace());
  398.  
  399.             else
  400.                 metricInfo.setText("(unknown method)");
  401.         }
  402.         else
  403.             metricInfo.setText("(not FontMetricsX)");
  404.     }
  405.  
  406.     ////////////////////////////////////////////////////////////////////////////
  407.  
  408.     private void setTextFont()
  409.     {
  410.         FontX    fontX ;
  411.         Font    font;
  412.  
  413.         font = textArea.getFont();
  414.         if( font instanceof com.ms.awt.FontX )
  415.             fontX = FontX.ChooseFont(this, (FontX)font);
  416.         else
  417.             fontX = FontX.ChooseFont(this);
  418.  
  419.         if(fontX != null)
  420.         {
  421.             // hackability
  422.             if( chkFudgeOutline.getState() )
  423.                 fontX = new FontX(fontX.getName(), fontX.getStyle(), fontX.getSize(), 
  424.                     fontX.getFlags() | FontX.OUTLINE);
  425.             //
  426.             // set the text area font
  427.             if (! fontX.equals(textArea.getFont()) )
  428.                 textArea.setFont(fontX);
  429.  
  430.             // now report back the info from this font
  431.             getFontInfo();
  432.             getMetrics();
  433.         }
  434.     }
  435.  
  436.     ////////////////////////////////////////////////////////////////////////////
  437.  
  438. }
  439.