Using this methodology you include any kind of PC Fonts in your application.
Steps to be followed:-
1. Create a Bitmap font Image based on your Charset. Your Charset may be in any Language.
Example:-

2. Get each Character X_Offset & Width values & make an integer array.
Example:-
public int CHAR_X[] = {
0,7,13,19,26,33,39,45,53,57,61,69,72,82,89,96,103,110,115,122,127,134,140,149,156,162, 168,177,183,191,200,206,213,222,231,235,242,248,255,265,273,282,289,298,306,312,321,328,336,348,355,362,
368,376,383,391,396,405,412,418,426,432,
440,444,454,462,468,477,483,491,498,503,507,515,522
} ;
public int CHAR_W[] = {
5,5,6,6,6,4,6,6,2,2,6,2,9,6,6,6,6,4,5,4,5,5,9,6,6,6,
8,5,7,7,4,5,8,8,2,5,6,5,10,7,8,6,8,8,6,8,7,8,12,8,7,6,
7,5,7,5,7,6,5,7,5,6,2,
10,7,5,8,6,8,6,3,3,7,4,4
} ;
/////////////////////////////////////////////////////////////////////
Example Canvas class
/////////////////////////////////////////////////////////////////////
public class FontCanvas extends Canvas{
private int mWidth;
private int mHeight;
private Font mFont;
/** Creates a new instance of FontCanvas */
public FontCanvas() {
this.setFullScreenMode(true);
mFont = new Font();
this.mWidth = getWidth();
this.mHeight = getHeight();
}
protected void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, mWidth, mHeight);
//mFont.drawString(g, "Have a look at this Custom Font", 10, 10, 0);
mFont.setColor(0xFF0000);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 10, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 30, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 50, 0);
mFont.setColor(0x00AA00);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 80, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 100, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 120, 0);
mFont.setColor(0x0000FF);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 150, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 170, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 190, 0);
mFont.setColor(0x0);
mFont.drawString(g, "abcdefghijklmnopqrstuvwxyz", 10, 230, 0);
mFont.drawString(g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10, 250, 0);
mFont.drawString(g, "0123456789!@#$%^&*()_/?", 10, 270, 0);
mFont.setColor(0xFF00FF);
mFont.drawString(g, "by Albert", mWidth-70, mHeight-14, 0);
}
}
/////////////////////////////////////////////////////////////////////
Example Font Class:-
/////////////////////////////////////////////////////////////////////
class Font
{
Image fontImage, forColorImage;
int fontHeight;
public Font()
{
try{
fontImage = Image.createImage("/font.png");
forColorImage = fontImage;
fontHeight = fontImage.getHeight();
}catch(Exception e){}
}
public String CHAR_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_/?";
public int CHAR_X[] = {
0,7,13,19,26,33,39,45,53,57,61,69,72,82,89,96,103,110,115,122,127,134,140,149,156,162,
168,177,183,191,200,206,213,222,231,235,242,248,255,265,273,282,289,298,306,312,321,328,336,348,355,362,
368,376,383,391,396,405,412,418,426,432,440,444,454,462,468,477,483,
491,498,503,507,515,522
} ;
public int CHAR_W[] = {
5,5,6,6,6,4,6,6,2,2,6,2,9,6,6,6,6,4,5,4,5,5,9,6,6,6,
8,5,7,7,4,5,8,8,2,5,6,5,10,7,8,6,8,8,6,8,7,8,12,8,7,6,
7,5,7,5,7,6,5,7,5,6,2,
10,7,5,8,6,8,6,3,3,7,4,4
} ;
public int convert_RGB_to_ARGB(int rgb) {
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 0) & 0xFF;
return 0xff000000 | (r <<>
}
public void setColor(int c)
{
int w = fontImage.getWidth();
int h = fontImage.getHeight();
int len = w*h;
int temp[] = new int[len];
forColorImage.getRGB(temp, 0, w, 0, 0, w, h);
int color = convert_RGB_to_ARGB(c);
//System.out.println("COLOR: " + Integer.toHexString(color));
for(int i=0; i
if(temp[i] == -16711936)
temp[i] = color;
this.fontImage = fontImage.createRGBImage(temp, w, h, false);
temp=null;
}
public void drawString(Graphics g, String str, int x, int y, int anchor)
{
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();
char chr[] = str.toCharArray();
int len = chr.length;
int c;
for(int i=0; i
{
if(chr[i] != ' ')
{
c = CHAR_SET.indexOf(""+chr[i]);
//System.out.println(i+":"+c + "fontHeight: " + fontHeight);
g.clipRect(x, y, CHAR_W[c], 14);
g.drawImage(fontImage, x-CHAR_X[c], y, Graphics.TOP | Graphics.LEFT);
g.setClip(cx, cy, cw, ch);
x += CHAR_W[c]+1;
}
else
x += 3;
}
g.setClip(cx, cy, cw, ch);
}
}
Example Screen Shot
/////////////////////////////////////////////////////////////////////