import javax.microedition.lcdui.*;
public class VFont{
    //#if width==240 || width=="320" || width=="352" 
    //height 14pixel
      static byte width[]={4,6,7,8,9,10,3,4,4,6,9,3,5,3,5,8,5,8,8,8,8,8,8,8,8,3,3,8,8,8,9,16,10,9,9,9,8,8,10,9,3,8,9,8,12,9,10,8,10,10,8,9,9,10,13,9,9,9,5,5,5,7,9,4,8,8,7,8,8,6,8,8,3,5,7,3,11,8,8,8,8,6,7,6,8,8,12,7,8,6,6,2,6,8};
    static int font_ht=14;
      //#elif width=="128"
//#      static byte width[]={3,5,7,6,9,7,2,3,3,4,6,3,3,2,4,6,4,6,6,6,6,6,6,6,6,2,2,6,6,6,6,11,8,6,6,6,6,5,7,6,2,5,6,5,8,6,7,6,7,6,6,6,6,8,9,7,6,5,3,4,3,5,7,3,6,6,6,6,6,4,6,6,2,4,5,2,8,6,6,6,6,4,6,4,6,7,10,6,6,5,4,3,5,8};
//#      //{2,4,8,5,10,8,2,4,4,6,6,3,5,3,5,5,4,5,5,6,5,5,5,5,5,2,3,6,4,6,5,9,6,5,5,5,4,4,5,5,4,4,5,4,6,5,6,5,6,5,6,6,5,6,6,6,6,6,4,4,4,6,6,4,5,5,4,5,5,4,5,4,2,4,4,2,6,4,5,5,5,4,5,5,5,5,6,4,5,5,6,2,6,5,5};
//#      static int font_ht=10;
//# 
    //#else
//#       static byte width[]={3,6,8,6,9,9,3,4,5,4,7,3,4,4,4,7,6,7,7,7,7,7,7,7,7,3,3,7,7,7,7,12,8,8,8,8,7,6,8,8,3,7,8,7,10,8,9,8,9,9,8,7,8,10,12,8,9,7,4,4,4,7,8,4,7,7,7,7,7,5,7,7,3,4,7,3,11,7,7,7,7,5,7,5,7,8,10,7,8,6,6,2,6,7};
//#       static int font_ht=12;
      //#endif
      static short pos[];
      static Image font_image[];
        static int SPACE_WIDTH=4;
        static int TAB_WIDTH=8;
        static int HCENTER=1;
        static int RED_FONT=1,BLACK_FONT=0;
       
       public static void initFont()
       {
                     pos=new short[width.length];
                     for(int i=1;i<width.length;i++)
                     {
                         pos[i]=(short)(pos[i-1]+(short)width[i-1]);
                     }
                    try{
                       font_image=new Image[1];
                       font_image[0]=Image.createImage("/strip_14.png");
                    }catch(Exception e){//System.out.println("---1---");e.printStackTrace();
                     }
        }

        static int getHeight(){
                return font_ht;
        }
        static int charWidth(char ch){
            if(ch>32 && ch<127){
//                    switch(height)
//                    {
//                       case 12:
                           return width[ch-33];                       
//                    }
            }else if(ch==' '){
                  return SPACE_WIDTH;
            }else if(ch=='\t'){
                  return TAB_WIDTH;
            }
            return 0;
        }
        static int stringWidth(String str){
            int wd=0,len=str.length();
            for(int i=0;i<len;i++){
                  wd+=charWidth(str.charAt(i));
            }
            return wd;
        }
        static void drawString(Graphics g,String str,int x,int y,int a,int imgno){
              int lallign=0,rallign=0;
              if((a & Graphics.LEFT) != 0)
                    lallign=1;
              else if((a & Graphics.RIGHT) != 0)
                    lallign=2;
              else if((a & Graphics.HCENTER) != 0)
                    lallign=3;
              if((a & Graphics.TOP) != 0)
                    rallign=1;
              else if((a & Graphics.BOTTOM) != 0)
                    rallign=2;
              else if((a & Graphics.VCENTER) != 0)
                    rallign=3;
//              System.out.println("lallign "+lallign);
              switch(lallign){
                    case 2:
                         x=x-stringWidth(str);
                    break;
                    case 3:
//                         System.out.println("wd "+str);
                         x=x-(stringWidth(str)/2);
                    break;
              }
              switch(rallign){
                    case 2:
                         y=y-getHeight();
                    break;
                    case 3:
                         y=y-(getHeight()/2);
                    break;
              }
              drawString1(g,str,x,y,imgno);
        }


       public static void drawString1(Graphics g,String str,int x,int y,int imgno){
              int curx=x,len=str.length();
              for(int i=0;i<len;i++){

                      curx+=drawChar(g,str.charAt(i),curx,y,imgno);
              }
              g.setClip(0,0,WelcomeCanvas.width,WelcomeCanvas.height);
        }
       static int drawChar(Graphics g,char ch,int x,int y,int imgno){
//           if(ch==33)
//           {
//                g.setClip(x,y,width[0],pos[0]);
//                switch(imgno)
//                            {
//                                case 1:g.drawImage(AMenu.strip_red,x-pos[0],y,20);break;
//                                default:g.drawImage(font_image[0],x-pos[0],y,20);break;
//                            }
//               g.setClip(0,0,WelcomeCanvas.width,WelcomeCanvas.height);
//           }
//           else 
               if(ch>32 && ch<127){
//                    switch(height)
//                    {
//                       case 12:
                             g.setClip(x,y,width[ch-33],pos[ch-33]);
                            switch(imgno)
                            {
                                case 1:g.drawImage(AMenu.strip_red,x-pos[ch-33],y,20);break;
                                default:g.drawImage(font_image[0],x-pos[ch-33],y,20);break;
                            }
                             return width[ch-33];                       
//                    }
              }
              return charWidth(ch);
        }
}