📄 fontwriter.java
字号:
/*
* Created on 2005-02-25
*
* Copyright (c) 2005 nanoGames. All Rights Reserved.
*
*/
//package com.nano.KangooJumper;
import javax.microedition.lcdui.*;
/**
* @author plumkawka FontWriter
*/
public class FontWriter
{
public final static int SMALL = 1;
public final static int SMALL_OUTLINE = 2;
public final static int NORMAL = 3;
public final static int NORMAL_OUTLINE = 4;
public final static int CHARS_IN_FONT = 46;
public int kerning = 0;
private Image m_Image;
private int m_GlyphHeight;
private byte[] m_GlyphWidthTab;
private int m_MaxGlyphWidth;
private final static byte[] m_NormalGlyphWidthTab = { 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 6, 6, 5, 7, 6, 6, 6, 6, 6, 6, 7, 6,
7, 7, 7, 7, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 3, 4, 7, 3, 6, 6, 7, 4, 3, 5, 8 };
public FontWriter(int type, int font)
{
// load image
m_Image = Utils.LoadImage(font);
switch (type)
{
case NORMAL_OUTLINE:
m_GlyphWidthTab = m_NormalGlyphWidthTab;
break;
}
m_GlyphHeight = m_Image.getHeight() / CHARS_IN_FONT;
// find max glyph width
for (int lp = 0; lp < m_GlyphWidthTab.length; lp++)
{
if (m_GlyphWidthTab[lp] > m_MaxGlyphWidth)
m_MaxGlyphWidth = m_GlyphWidthTab[lp];
}
}
public FontWriter(int type, int font, int pal)
{
switch (type)
{
case NORMAL_OUTLINE:
m_GlyphWidthTab = m_NormalGlyphWidthTab;
break;
}
// load image and remap palette
PNGLoader loader = new PNGLoader(font, pal, 2);
m_Image = loader.getImage();
m_GlyphHeight = m_Image.getHeight() / CHARS_IN_FONT;
// find max glyph width
for (int lp = 0; lp < m_GlyphWidthTab.length; lp++)
{
if (m_GlyphWidthTab[lp] > m_MaxGlyphWidth)
m_MaxGlyphWidth = m_GlyphWidthTab[lp];
}
}
public FontWriter(int type, int font, short[] pal)
{
switch (type)
{
case NORMAL_OUTLINE:
m_GlyphWidthTab = m_NormalGlyphWidthTab;
break;
}
// load image and remap palette
PNGLoader loader = new PNGLoader(font);
loader.remapPalette(pal, 0, 4);
m_Image = loader.getImage();
m_GlyphHeight = m_Image.getHeight() / CHARS_IN_FONT;
// find max glyph width
for (int lp = 0; lp < m_GlyphWidthTab.length; lp++)
{
if (m_GlyphWidthTab[lp] > m_MaxGlyphWidth)
m_MaxGlyphWidth = m_GlyphWidthTab[lp];
}
}
public static int charOffset(char ch)
{
if (ch == ' ')
return 0;
else if (ch == '.')
return 37;
else if (ch == ':')
return 40;
else if (ch == '?')
return 42;
else if (ch == '\'')
return 44;
else if (ch == '!')
return 45;
else if (ch == '\\')
return 46;
else if (ch == '-')
return 47;
else if (ch == '|')
return 48;
else if (ch == ',')
return 39;
else if (ch >= 'A' && ch <= 'Z')
return ch - 'A' + 1;
else if (ch >= '0' && ch <= '9')
return ch - '0' + 27;
return 0;
}
public int strLen(String str)
{
int index = str.length();
int len = 0;
while (index > 0)
{
index--;
len += (m_GlyphWidthTab[charOffset(str.charAt(index))] + kerning);
}
return len;
}
public void drawText(Graphics g, String str, int x, int y)
{
if (str == null)
return;
g.setClip(x, y, strLen(str), m_GlyphHeight);
int len = str.length();
int index = 0;
while (index < len)
{
int offs = charOffset(str.charAt(index));
g.drawImage(m_Image, x, y - offs * m_GlyphHeight, Graphics.LEFT | Graphics.TOP);
x += (m_GlyphWidthTab[offs] + kerning);
index++;
}
}
public void drawTextCentered(Graphics g, String str, int x, int y)
{
drawText(g, str, x - (strLen(str) >> 1), y - (m_GlyphHeight >> 1));
}
public void drawChar(Graphics g, char ch, int x, int y, boolean centered)
{
int offs = charOffset(ch);
// align to center if needed
if (centered == true)
x -= m_GlyphWidthTab[offs] >> 1;
g.setClip(x, y, m_GlyphWidthTab[offs], m_GlyphHeight);
g.drawImage(m_Image, x, y - offs * m_GlyphHeight, Graphics.LEFT | Graphics.TOP);
}
public Image createImageWithText(String str)
{
int width = strLen(str);
Image img = Image.createImage(width, m_GlyphHeight);
drawText(img.getGraphics(), str, 0, 0);
return img;
}
public int getHeight()
{
return m_GlyphHeight;
}
public int getWidest()
{
return m_MaxGlyphWidth;
}
/**
* @param text
* @param index
* @param last
* @return
*/
public int strLen(char[] text, int start, int stop)
{
int len = 0;
while (start < stop)
{
len += (m_GlyphWidthTab[charOffset(text[start])] + kerning);
start++;
}
return len;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -