Here is a way to download fonts that are packaged in a jar + xml file to read the font size, font name and font file name :
1) FontLibrary class:
package be.nicholas.font.loading;
import java.awt.Font;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import be.nicholas.font.CustomFont;
import be.nicholas.utils.Logger;
import be.nicholas.utils.Misc;
public class FontLibrary {
public static Map<String, Font> fonts = null;
private static Logger logger = Logger.getInstance();
public FontLibrary() { }
public static Font getFont(String fontname)
{
Font f = FontLibrary.fonts.get(fontname);
if(f == null)
logger.error("Font ("+fontname+") could not be found.");
return f;
}
public static Font getFont(String fontname, int fontsize)
{
Font f = FontLibrary.fonts.get(fontname).deriveFont((float) fontsize);
if(f == null)
logger.error("Font ("+fontname+") could not be found.");
return f;
}
public static void load()
{
try
{
InputStream fontStream = FontLibrary.class.getResourceAsStream("/data/fonts/fonts.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fontStream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("font");
fonts = new HashMap<String, Font>();
logger.error("Starting to load fonts...");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element fontElement = (Element) nNode;
CustomFont f = new CustomFont();
f.setName(getTagValue("name", fontElement));
f.setFont(CustomFont.load(getTagValue("filename", fontElement), Integer.parseInt(getTagValue("fontsize", fontElement))));
f.setSize(Integer.parseInt(getTagValue("fontsize", fontElement)));
fonts.put(f.getName(), f.getFont());
int percent = Misc.getPercentage(fonts.size(), nList.getLength());
String msg = percent+"% of fonts loaded("+fonts.size()+" of "+nList.getLength()+")";
}
}
} catch (Exception e) {
logger.error("Error: "+e);
}
logger.error("All fonts loaded!"+"("+fonts.size()+" fonts int total).");
}
private static String getTagValue(String sTag, Element eElement)
{
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
2) CustomFont class:
package be.nicholas.font;
import java.awt.Font;
import java.io.InputStream;
import be.nicholas.font.loading.FontLibrary;
import be.nicholas.utils.Misc;
public class CustomFont {
public String name;
public Font fontObject;
public int size;
public CustomFont() {
}
public void setFont(Font fontObject)
{
this.fontObject = fontObject;
}
public Font getFont()
{
return this.fontObject;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setSize(int size)
{
this.size = size;
}
public int getSize()
{
return this.size;
}
public static Font load(String filename,int fontSize)
{
InputStream fontStream = CustomFont.class.getResourceAsStream("/be/nicholas/fonts/"+filename);
if (fontStream != null)
{
try
{
Font labelFont;
labelFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
labelFont = labelFont.deriveFont((float) fontSize);
return labelFont;
} catch (Exception e)
{
System.out.println("error " + e);
}
}
return null;
}
}
3) XML structure:
<fonts>
<font>
<name>mycustomfontname</name>
<filename>myfont.ttf</filename>
<fontsize>16</fontsize>
</font>
<font>
<name>champagne</name>
<filename>ChampagneAndLimousines.ttf</filename>
<fontsize>50</fontsize>
</font>
</fonts>
4) Download the font:
JLabel label = new JLabel('testText');
label.setFont(FontLibrary.fonts.get("mycustomfontname"));
label.setFont(FontLibrary.fonts.get("mycustomfontname").deriveFont(22));
Hope this helps:) ... I'm a real newbie to java, and I did it on my fifth day playing with java, so this is far from perfect, but it will do the job
source
share