Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4310747

Font.createFont fails to delete temp font files

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0, 1.4.0
    • client-libs
    • 2d
    • beta2
    • x86, sparc
    • solaris_8, windows_98, windows_nt, windows_2000

      Build : 'T'
      OS : Win32 (98 / NT) (Sol not yet tested)
      app : DynamicFont.java (attached and embedded)

      Font.createFont(Font.TRUETYPE_FONT. {FileInputStream obj}) fails to delete the temporary font files created in the default Windows' temp dir.

      Some TT fonts if you like: (there are 2981 fonts in these directories)
      /net/sqesvr/export/disk1/java2d/misc/FONTS

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.font.FontRenderContext;
      import java.awt.font.TextLayout;
      import java.awt.geom.GeneralPath;
      import java.awt.image.BufferedImage;
      import java.util.Vector;
      import javax.swing.*;
      import javax.swing.border.*;
      import javax.swing.event.*;
      import java.io.*;


      /**
       * DynamicFonts - load TrueType fonts from a directory.
       */
      public class DynamicFonts extends JApplet {

          Demo demo;
          String name;


          public void init() {
              getContentPane().add(demo = new Demo(name));
              getContentPane().add("North", new DemoControls(demo));
          }

          public void start() {
              demo.start();
          }
        
          public void stop() {
              demo.stop();
          }



          /**
           * The Demo class performs the scrolling and rendering.
           */
          static class Demo extends JPanel implements Runnable {
          
              private static Vector fonts = new Vector();

              /*
               * gets the available fonts from the GraphicsEnviroment
               * and puts them into a Vector
               */
              // the number of strings
              private int nStrs;

              // the height of a string
              private int strH;

              // used to index into the Vector fonts
              private int fi;
              private Thread thread;
              private BufferedImage bimg;
              private Vector v = new Vector();
              private Object AntiAlias = RenderingHints.VALUE_ANTIALIAS_ON;

              public long sleepAmt = 500;
              public int fsize = 14;
      public int fontInc = 0;
          
          
              public Demo(String name) {
                  setBackground(Color.white);
                  loadFonts(name);
              }
          

              private void loadFonts(String name) {

                  File file = new File(name);
                    if (file != null && file.isDirectory()) {
                      String files[] = file.list();
                      for (int i = 0; i < files.length; i++) {
                          File leafFile = new File(file.getAbsolutePath(), files[i]);
                          if (leafFile.isDirectory()) {
                              loadFonts(leafFile.getAbsolutePath());
                          } else {
                              if (!file.isHidden()) loadFont(leafFile);
                          }
                      }
                  } else if (file != null && file.exists()) {
      loadFont(file);
                  }

              }


              private void loadFont(File file) {


                  try {
                      System.out.println("Loading :" + fontInc +" "+ file.getName());
                      FileInputStream fis = new FileInputStream(file);
                      Font font = Font.createFont(Font.TRUETYPE_FONT, fis);
                      if (font != null) {
                          fonts.add(font);
        fontInc++;
                     }
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }

              }


              /*
               * Compute how many strings can fit into the new height.
               */
              public void reset(int w, int h) {
                  v.clear();
                  Font f = ((Font) fonts.get(0)).deriveFont(Font.PLAIN,fsize);
                  FontMetrics fm = getFontMetrics(f);
                  strH = (int) (fm.getAscent()+fm.getDescent());
                  nStrs = h/strH + 1;
                  fi = 0;
              }

          
              /*
               * copies one Font from the fonts Vector to the end of the v
               * Vector until the end of the fonts Vector is reached. Once
               * the v Vector is filled with strings equaling the height of
               * the surface, starts removing the first element of the Vector
               * v. Once the end of fonts Vector is reached, continues
               * to remove elements from the v Vector until the end of the
               * v Vector is reached. When the end of v Vector is reached,
               * begins process again.
               */
              public void step(int w, int h) {
                  if (fi < fonts.size()) {
                      v.addElement(((Font) fonts.get(fi)).deriveFont(Font.PLAIN,fsize));
                  }
                  if (v.size() == nStrs && v.size() != 0 || fi > fonts.size()) {
                      v.removeElementAt(0);
                  }
                  fi = (v.size() == 0) ? 0 : ++fi;
              }

          
              // renders all of the Vector v's elements.
              public void drawDemo(int w, int h, Graphics2D g2) {
          
                  g2.setColor(Color.black);
          
                  int yy = (fi >= fonts.size()) ? 0 : h - v.size() * strH - strH/2;
          
                  for (int i = 0; i < v.size(); i++) {
                      Font f = (Font) v.get(i);
                      int sw = getFontMetrics(f).stringWidth(f.getName());
                      g2.setFont(f);
                      g2.drawString(f.getName(), (int) (w/2-sw/2),yy += strH);
                  }
              }
          

              public void setAntiAlias(boolean aa) {
                  AntiAlias = aa
                      ? RenderingHints.VALUE_ANTIALIAS_ON
                      : RenderingHints.VALUE_ANTIALIAS_OFF;
              }

          
              public Graphics2D createGraphics2D(int w, int h) {
                  Graphics2D g2 = null;
                  if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
                      bimg = (BufferedImage) createImage(w, h);
                      reset(w, h);
                  }
                  g2 = bimg.createGraphics();
                  g2.setBackground(getBackground());
                  g2.clearRect(0, 0, w, h);
                  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
                  return g2;
              }
          
          
              public void paint(Graphics g) {
                  Dimension d = getSize();
                  step(d.width, d.height);
                  Graphics2D g2 = createGraphics2D(d.width, d.height);
                  drawDemo(d.width, d.height, g2);
                  g2.dispose();
                  g.drawImage(bimg, 0, 0, this);
              }
          
          
              public void start() {
                  thread = new Thread(this);
                  thread.setPriority(Thread.MIN_PRIORITY);
                  thread.start();
              }
          
          
              public synchronized void stop() {
                  thread = null;
              }
          
          
              public void run() {
                  Thread me = Thread.currentThread();
                  while (thread == me) {
                      repaint();
                      try {
                          thread.sleep(sleepAmt);
                      } catch (InterruptedException e) { break; }
                  }
                  thread = null;
              }
          } // End Demo class



          /**
           * The DemoControls class provides controls for selecting font size and
           * sleep amount.
           */
          static class DemoControls extends JPanel implements ActionListener, ChangeListener {

              Demo demo;
              JSlider slider;
              int fsize[] = { 8, 14, 18, 24, 32 };
              JMenuItem menuitem[] = new JMenuItem[fsize.length];
              Font font[] = new Font[fsize.length];
              JLabel label;


              public DemoControls(Demo demo) {
                  this.demo = demo;
                  setBackground(Color.gray);

                  JButton b = new JButton("AA");
                  b.setToolTipText("AntiAliasing");
                  b.setSelected(true);
                  b.setBackground(Color.green);
                  b.addActionListener(this);
                  add(b);

                  label = new JLabel(String.valueOf(demo.sleepAmt) + " ms");
                  label.setForeground(Color.black);
                  add(label);
                  slider = new JSlider(JSlider.HORIZONTAL, 100, 999, (int) demo.sleepAmt);
                  slider.setBorder(new EtchedBorder());
                  slider.setPreferredSize(new Dimension(90,22));
                  slider.addChangeListener(this);
                  add(slider);
                  JMenuBar menubar = new JMenuBar();
                  add(menubar);
                  JMenu menu = (JMenu) menubar.add(new JMenu("Font Size"));
                  menu.setIcon(new DownArrowIcon());
                  menu.setFont(new Font("serif", Font.PLAIN, 12));

                  for (int i = 0; i < fsize.length; i++) {
                      font[i] = new Font("serif", Font.PLAIN, fsize[i]);
                      menuitem[i] = menu.add(new JMenuItem(String.valueOf(fsize[i])));
                      menuitem[i].setFont(font[i]);
                      menuitem[i].addActionListener(this);
                  }
              }


              public void actionPerformed(ActionEvent e) {
                  Object object = e.getSource();
                  if (object instanceof JButton) {
                      JButton b = (JButton) object;
                      b.setSelected(!b.isSelected());
                      b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
                      demo.setAntiAlias(b.isSelected());
                      if (demo.thread == null) {
                          demo.repaint();
                      }
                  } else {
                      for (int i = 0; i < fsize.length; i++) {
                          if (object.equals(menuitem[i])) {
                              demo.fsize = fsize[i];
                              Dimension d = demo.getSize();
                              demo.reset(d.width, d.height);
                              break;
                          }
                      }
                  }
              }


              public void stateChanged(ChangeEvent e) {
                  demo.sleepAmt = (long) slider.getValue();
                  label.setText(String.valueOf(demo.sleepAmt) + " ms");
                  label.repaint();
              }



              /**
               * The DownArrowIcon class renders an icon for the control
               * menus that looks like an arrow pointing down.
               */
              static class DownArrowIcon implements Icon {
                  public void paintIcon(Component c, Graphics g, int x, int y) {
                      Graphics2D g2 = (Graphics2D) g;
                      GeneralPath p1 = new GeneralPath();
                      p1.moveTo(10,10);
                      p1.lineTo(14,18);
                      p1.lineTo(18,10);
                      p1.closePath();
                      g2.setColor(Color.black);
                      g2.fill(p1);
                  }
                  public int getIconWidth() { return 20; }
                  public int getIconHeight() { return 20; }
              } // End DownArrowIcon class

          } // End DemoControls class


          public static void main(String args[]) {
              if (args.length == 0) {
                  System.out.println("usage java DyamicFonts TrueTypeDirectory");
                  System.exit(0);
              }
              final DynamicFonts demo = new DynamicFonts();
              demo.name = args[0];
              demo.init();
              JFrame f = new JFrame("Java 2D(TM) Demo - DynamicFonts");
              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {System.exit(0);}
                  public void windowDeiconified(WindowEvent e) { demo.start(); }
                  public void windowIconified(WindowEvent e) { demo.stop(); }
              });
              f.getContentPane().add("Center", demo);
              f.pack();
              f.setSize(new Dimension(400,300));
              f.show();
              demo.start();
          }
      }

            jgodinez Jennifer Godinez (Inactive)
            rckim Robert Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: