FontManager.java :  » Game » minime » minime » font » Java Open Source

Java Open Source » Game » minime 
minime » minime » font » FontManager.java
/**
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime.font;

import java.util.Hashtable;

//import javax.microedition.m2g.SVGImage;
import minime.gen.FontRscId;

/**
 * Creates a default font for each component, for example BUTTON_FONT is for
 * buton.
 * 
 * @author yishu
 * 
 */
class FontManager
{
    public static FontControler fontControler = null ;
    
    public static IFont getFont(int face, int style, int size)
  {
    if (fontControler==null)
      fontControler = new FontControler() ;
    
    // create a system font
    if (face == FontRscId.FONT_SYSTEM || face == FontRscId.FONT_MONOSPACE || face == FontRscId.FONT_PROPORTIONAL)
    {
      return fontControler.getSystemFont(face, style, size) ;
    }
    
    // create a system font
    return fontControler.getSvgFont(face, style, size) ;
  }
  
  private static class FontControler
  {
    Hashtable fontSize ;
    
    public FontControler()
    {
      fontSize = new Hashtable() ;
    }
    
    private int getSystemFontSize(int face, int style, int freeSize)
    {
      int smallSize = 0 ;
      int mediumSize = 0 ;
      int largeSize = 0 ;

      Integer fontId = new Integer(face | style | javax.microedition.lcdui.Font.SIZE_LARGE) ;
      if (fontSize.containsKey(fontId))
      {
        largeSize = ((Integer)fontSize.get(fontId)).intValue() ;
      }
      else // add medium font
      {
        javax.microedition.lcdui.Font f = javax.microedition.lcdui.Font.getFont(face, style, javax.microedition.lcdui.Font.SIZE_LARGE) ;
        largeSize = f.getHeight() ;
        fontSize.put(fontId, new Integer(largeSize)) ;        
      }

      // if size required bigger than large, return large
      int deltaLarge = freeSize - largeSize ;
      if (deltaLarge>=0)
        return javax.microedition.lcdui.Font.SIZE_LARGE ;
      
      deltaLarge *= -1 ;      // make deltaLarge positive
      
      fontId = new Integer(face | style | javax.microedition.lcdui.Font.SIZE_MEDIUM) ;
      if (fontSize.containsKey(fontId))
      {
        mediumSize = ((Integer)fontSize.get(fontId)).intValue() ;
      }
      else // add medium font
      {
        javax.microedition.lcdui.Font f = javax.microedition.lcdui.Font.getFont(face, style, javax.microedition.lcdui.Font.SIZE_MEDIUM) ;
        mediumSize = f.getHeight() ;
        fontSize.put(fontId, new Integer(mediumSize)) ;        
      }
      
      // check for meium size
      int deltaMedium = freeSize - mediumSize ;
      if (deltaMedium >= 0)
      {
        if (deltaLarge < deltaMedium)
          return javax.microedition.lcdui.Font.SIZE_LARGE ;
        
        return javax.microedition.lcdui.Font.SIZE_MEDIUM ;
      }
      
      deltaMedium *= -1 ;      // make deltaMedium positive
      
      // get the actual system font size
      fontId = new Integer(face | style | javax.microedition.lcdui.Font.SIZE_SMALL) ;
      if (fontSize.containsKey(fontId))
      {
        smallSize = ((Integer)fontSize.get(fontId)).intValue() ;
      }
      else // add small font
      {
        javax.microedition.lcdui.Font f = javax.microedition.lcdui.Font.getFont(face, style, javax.microedition.lcdui.Font.SIZE_SMALL) ;
        smallSize = f.getHeight() ;
        fontSize.put(fontId, new Integer(smallSize)) ;        
      }
      
      // compare with smaller size
      int deltaSmall = freeSize - smallSize ;
      if (deltaSmall >= 0)
      {
        if (deltaMedium < deltaSmall)
          return javax.microedition.lcdui.Font.SIZE_MEDIUM ;
      }
      
      return javax.microedition.lcdui.Font.SIZE_SMALL ;
    }
    
    public FontLcuid getSystemFont(int face, int style, int freeSize)
    {
      // convert size to lcuid size SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE
      int lcuidSize = getSystemFontSize(face, style, freeSize) ;
      javax.microedition.lcdui.Font f = javax.microedition.lcdui.Font.getFont(face, style, lcuidSize) ;
      return new FontLcuid(f) ;
    }
    
    public FontSVG getSvgFont(int face, int style, int size)
    {
      //Get the Font from the RMS
      GeneratedSVGFontFile svgfont = FontRscId.getSVGFont(face);
      return new FontSVG(svgfont,size);
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.