Results 1 to 4 of 4
  1. #1
    Nerdopolis is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default Help With Custom Font Translator in Java?

    I am trying to make a program in Java that converts any font recognized by Java, to turn it into a custom format. It includes a bitmap with a string of every character in numerical order horizontally, and an XML entry that looks like this for every character:
    Java Code:
    <entry name="title of char (not really used)" char="numerical representation of char" offset="distance in pixels from left of bitmap" width="width in pixels of char"/>
    I run into 2 problems. The first is that the bitmap maxes out at 32767 pixels wide before its cut off (I only managed to get a blank one at the size I need) and the second is that it seems like after character number 502, it does not measure the strings correctly (offset and width are off by many pixels).

    Here is my code:

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.font.*;
    import java.io.*;
    
    
    
    import javax.swing.JFrame;
    
    
    import java.awt.font.TextAttribute;
    import java.text.AttributedString;
    import java.awt.image.BufferedImage;
    
    
    import javax.imageio.*;
    public class MainClass extends Canvas {
    
    String args[];
    int Loop_Offset = 0;
    int Times_XML_Ran = 0;
    int i;
     FontMetrics metrics;
     int Text_Height;
    BufferedWriter out;
    String fontfilename;
    int XML_Char_Number = 0;
    //Times_To_Run is the number of chars you want converted.
    int Times_To_Run = 65536;
    int Times_Ran =0;
    int Char_Value_Adder = 0;
    String Offset_String ="";
    int Pixel_Helper;
    Graphics g;
    Frame f;
    String Return_Grabber = "";
    float height = 0.0F;
    Font myfont;
    Graphics2D g2d;
    float Char_0_Width_Layout;
    int SIZE = 0;
    int Char_Width_Helper = 0;
    String Font_Size = "";
    String Font_Name = "";
    String Font_Style_In = "";
    int Font_Style = 0;
    String Attribute_Name = "";
    String Line_Status_In = "";
    String Underline_Status_Name = "";
    int Line_Status = 0;
    int Window_Height = 0;
    int Window_Width = 0;
    FontRenderContext frc;
      
    int Char_0_Width;
      
    
    
    int Char_0_Offset = 0;
    
    
    char Char_0;
    
    String String_0;
      
    
    String SAMPLE = "|";
    
    
    AttributedString as;
    
    int test;
    void User()
    {
    
    int Preset_Offset = 0;
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
    
    System.out.println("Unicode Font Converter");
    System.out.println(" ");
    System.out.println("This will create an XML, and a BMP file in the folder that this program is in");
    System.out.println("with your specified font, and font size, with given attributes like bold or ");
    System.out.println("italic. Unforuntantly, thanks to a flaw in Java, Italics are not converted");
    System.out.println("perfectly, and may have some pixels chopped off at either end. However");
    System.out.println("using the italic variations of fonts will give better results. When possible");
    System.out.println("use fonts like \"Times New Roman Italic\" instead of \"Times New Roman\" ");
    System.out.println("with the italic attribute set on \" Times New Roman \" ");
    System.out.println(" ");
    System.out.println("Unless if you use a DPI of 72, the font will be slightly smaller then expected");
    System.out.println("because Java looks at your display like it is at 72 DPI");
    System.out.println(" ");
    System.out.println("Font Names must be typed the same way they appear in your System Fonts folder");
    System.out.println("On Windows computers it is usualy C:\\Windows\\Fonts\\ . Font names ARE case");
    System.out.println("sensitive!!");
    System.out.println(" ");
    System.out.println("Accidently typing a symbol or letter in a field that requires a ");
    System.out.println("number will cause an error which will show some Java debug information.");
    System.out.println("If you \"Exception\" output, just ignore it and try again.");
    System.out.println(" ");
    System.out.println("This will convert Unicode fonts into the XML format. If the font you are");
    System.out.println("converting does not have Unicode, please use the ASCII font converter ");
    System.out.println("as it will have better performace, and will create smaller files.");
    System.out.println(" ");
    System.out.println("Converting a font that does not exist, whether it is spelled wrong, not");
    System.out.println("installed, or not compatible with Java is not detected, and will create");
    System.out.println("a plain font. (Although it will have your attributes.)");
    System.out.println(" ");
    System.out.println("A blank white rectangle will appear and take up your entire monitor after");
    System.out.println("you finish all the confirmations. It's normal and will resize after at most");
    System.out.println("30 seconds to display a font preview. It will disappear after you exit.");
    System.out.println(" ");
    System.out.println("PLEASE READ ALL THE TEXT ABOVE!!!!!");
    System.out.println(" ");
    
    
    System.out.println("Enter the name of the font you want to convert:");
    try
    {
    Font_Name = reader.readLine();
    
      
    }
    catch(Exception e){}
    System.out.println("Enter the size you want to convert the font with:");
    try
    {
    Font_Size = reader.readLine(); 
    }
    catch(Exception e){}
    
    
    
    System.out.println("Enter your font attributes: (0=Plain, 1=Bold, 2=Italic, 3=Bold and Italic)");
    
    try
    {
    Font_Style_In = reader.readLine();   
    }
    catch(Exception e){}
    Font_Style = Integer.parseInt(Font_Style_In);
    if(Font_Style == 0) Attribute_Name = "Plain";
    if(Font_Style == 1) Attribute_Name = "Bold";
    if(Font_Style == 2) Attribute_Name = "Itallic";
    if(Font_Style == 3) Attribute_Name = "Bold Itallic";
    System.out.println("Enter your line attributes: (0=No Lines, 1=Underline, 2=Strikethrough, 3=Underline and Strikethrough)");
    try
    {
    Line_Status_In = reader.readLine();   
    }
    catch(Exception e){}
    Line_Status = Integer.parseInt(Line_Status_In);
    SIZE = Integer.parseInt(Font_Size);
    }
    
      
      
    
    void Run()
    {
      
    
    if (Times_Ran == Times_To_Run)
     
    {
    
    
    
    f = new Frame("Font Test Window");
    f.setUndecorated(true);
    int Font_Size;
    
    
    setSize(1800, 10000000);
    f.add(this);
    f.pack();
    
    Toolkit tk = Toolkit.getDefaultToolkit ();
    int nativeResolution = tk.getScreenResolution();
    final double javaResolution = 72.0; 
    
    myfont = new Font(Font_Name, Font_Style, (int)SIZE);  
    
    if (fontfilename != null) try {
    FileInputStream fis;
    fis = new FileInputStream(fontfilename);
    Font font1 = Font.createFont(Font.TRUETYPE_FONT, fis);
    myfont = font1.deriveFont(Font_Style, SIZE);
    }catch (Exception e) {}
    
    setBackground(Color.white);
    f.setVisible(true); 
    }
     
    
    try
    {
     
     out = new BufferedWriter(new FileWriter(Font_Name + " " + Font_Size  + " " + Attribute_Name + Underline_Status_Name + ".xml"));
    out.write("<?xml version=\"1.0\">\r\n");
    out.write("<font name=\"" + Font_Name + " " + Font_Size  + " " + Attribute_Name + Underline_Status_Name +"\">\r\n");
    out.close();
     
    }catch (IOException e) {
    }
    }
    
    
    
    
    
      
    
    public void paint(Graphics g) {
    
    if (0 == 0) try {
    Rectangle2D maxsize;
    
    frc = ((Graphics2D)g).getFontRenderContext();
    maxsize = myfont.getStringBounds(SAMPLE,frc);
    FontMetrics metrics = getFontMetrics( myfont );
    
    
    } catch (Exception e) {
    System.err.println("Error getting max bounds: " + e);
      
    }
    g.setFont(myfont);
    g.setColor(Color.black); 
    metrics = getFontMetrics( myfont );
      
    Text_Height = metrics.getAscent();
     
      
    
     
     
    
    SAMPLE = "";
    Times_XML_Ran = 0;
    for (Times_XML_Ran = 0; Times_XML_Ran <= Times_To_Run - 1; Times_XML_Ran++)
    {
    Pixel_Helper = 0;
    Char_Value_Adder=1 * Times_XML_Ran;
    
    Char_0 = (char)(0 + Char_Value_Adder);
    
     
    String_0 = Character.toString(Char_0);
      
    Char_0_Width = (int)myfont.getStringBounds(String_0, frc).getWidth() + Pixel_Helper;
    
     
     
    Offset_String = String_0 + " " ;
    System.out.println(Times_XML_Ran + " of " + Times_To_Run);
      
    
    
    SAMPLE = SAMPLE + Offset_String;
      
    
      
     
    
      
    
    try {
      
     
    
    out = new BufferedWriter(new FileWriter(Font_Name + " " + Font_Size  + " " + Attribute_Name + Underline_Status_Name + ".xml", true));
      
    out.write("\t<entry name=\"Char_Name\" char=\"" + (int)Char_0+ "\" offset=\"" + Char_0_Offset +"\" width=\""+ Char_0_Width +"\"/>\r\n");
    
     
      out.close();
     
     
    } catch (IOException e) {
    }
    
     
      
    Char_0_Offset = (int)myfont.getStringBounds(Offset_String, frc).getWidth() + Loop_Offset;
    Loop_Offset = Char_0_Offset;
    
    } 
     
     
     
      
    as = new AttributedString(SAMPLE); 
    if(Line_Status == 0)
    {
    Underline_Status_Name = "";
    }
    if(Line_Status == 1)
    {
    as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    Underline_Status_Name = " Underline";
    }
    if(Line_Status == 2)
    {
    as.addAttribute(TextAttribute.STRIKETHROUGH,
    TextAttribute.STRIKETHROUGH_ON);
    Underline_Status_Name = " Strikethrough";
    }
    if(Line_Status == 3)
    {
    as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    as.addAttribute(TextAttribute.STRIKETHROUGH,
    TextAttribute.STRIKETHROUGH_ON);
    Underline_Status_Name = " Underline Strikethrough";
    } 
    
    as.addAttribute(TextAttribute.FONT, myfont);
    
    Graphics2D g2d = (Graphics2D)g; 
    
    
    Window_Height =  metrics.getHeight() ;
    
    Window_Width = metrics.stringWidth(SAMPLE);
    f.setSize(Window_Width,Window_Height);
    g.drawString(as.getIterator(), 0, Text_Height);
    BufferedImage awtImage = new BufferedImage(f.getWidth(),f.getHeight(),BufferedImage.TYPE_INT_RGB);
    Graphics2D gp = awtImage.createGraphics();
    gp.setColor(Color.white);
    gp.fillRect(0, 0, f.getWidth(), f.getHeight());
    gp.setColor(Color.black); 
    gp.drawString(as.getIterator(), 0, Text_Height);
    try { ImageIO.write(awtImage, "bmp", new File(Font_Name + " " + Font_Size  + " " + Attribute_Name + Underline_Status_Name + ".bmp")); }
    catch(Throwable thr) { thr.printStackTrace(); }
    
    
    System.out.println("Finished!!!");
    
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input); 
    
    
    
    
    
    System.out.println("Press the enter key to continue...");
    
    
    
    
    
    try
    {
    
    Return_Grabber = reader.readLine(); 
    
    }
    catch(Exception e){}
    System.exit(0);
    } 
    
    
    
    
    
    
    
    public MainClass() {
    User();
    System.out.println("Starting Step 1 of 2...");
    int Times_For_Loop_Ran = 1;
    
    
      
    
    
    Times_Ran = Times_To_Run;
    System.out.println("Preparing...");
    Run();
    
    
    
    
    } 
    
    
    
    
    
    
    
    
    
    
    public static void main(String args[]) {
    
    MainClass f1 = new MainClass();
    
    }
    }
    This code was compiled in Eclipse. I can't attach the output files because they are too large for the attachment mechanism, even when I compress them.

    EDIT: I almost forgot, how do I reduce the bitmap to only 256 colors instead of 24k?
    Any help will be appreciated. Thanks
    Last edited by Nerdopolis; 12-14-2008 at 07:15 PM.

  2. #2
    Nerdopolis is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Anyone know where the errors come from or any workarounds for them?

  3. #3
    Nerdopolis is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Nobody has any ideas?

  4. #4
    Nerdopolis is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    For anyone that may view this thread in the future, I have solved the problems I was having

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 09:00 PM
  2. How to change The Font
    By kader in forum New To Java
    Replies: 2
    Last Post: 09-15-2008, 05:09 AM
  3. SWT Font Registry Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:46 PM
  4. how to set font size for a xml value
    By bala_kj in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2008, 12:00 PM
  5. JTable with Font
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 1
    Last Post: 12-12-2007, 06:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •