add Measure() into bmf
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 11 KiB |
@ -126,6 +126,53 @@ public class BMF {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: handle multiline
|
||||||
|
public short[] Measure(String str)
|
||||||
|
{
|
||||||
|
short left = 0;
|
||||||
|
short right = 0;
|
||||||
|
short top = 0;
|
||||||
|
short bottom = 0;
|
||||||
|
short[] cursor = new short[]{0, 0};
|
||||||
|
boolean isFirst = true;
|
||||||
|
|
||||||
|
for (int i=0; i<str.length(); i++)
|
||||||
|
{
|
||||||
|
char index = str.charAt(i);
|
||||||
|
if (index != '\n' && (index < 32 || index > 127)) index = 127;
|
||||||
|
|
||||||
|
if (index == '\n')
|
||||||
|
{
|
||||||
|
isFirst = true;
|
||||||
|
cursor[1] += lineHeight;
|
||||||
|
cursor[0] = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
short[] ch = chars[index-32];
|
||||||
|
int[] offset = new int[]{ch[XOFF]+cursor[0], ch[YOFF]+cursor[1]};
|
||||||
|
|
||||||
|
// check first XOFF
|
||||||
|
if (isFirst)
|
||||||
|
{
|
||||||
|
left = (short)Math.min(ch[XOFF], left);
|
||||||
|
isFirst = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
right = (short)Math.max(right, offset[0]+ch[WIDTH]);
|
||||||
|
top = (short)Math.min(top, offset[1]);
|
||||||
|
bottom = (short)Math.max(bottom, offset[1]+ch[HEIGHT]);
|
||||||
|
|
||||||
|
cursor[0] += ch[XADV];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new short[]{
|
||||||
|
(short)(right+Math.abs(left)), // x
|
||||||
|
(short)(bottom+Math.abs(top)), // y
|
||||||
|
left, top // xoff, yoff
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public void Draw(Graphics g, String str, int x, int y)
|
public void Draw(Graphics g, String str, int x, int y)
|
||||||
{
|
{
|
||||||
short[] cursor = new short[]{0, 0};
|
short[] cursor = new short[]{0, 0};
|
||||||
@ -148,7 +195,7 @@ public class BMF {
|
|||||||
img,
|
img,
|
||||||
ch[X], ch[Y], ch[WIDTH], ch[HEIGHT],
|
ch[X], ch[Y], ch[WIDTH], ch[HEIGHT],
|
||||||
Sprite.TRANS_NONE,
|
Sprite.TRANS_NONE,
|
||||||
x+offset[0], y+offset[1],
|
offset[0], offset[1],
|
||||||
Graphics.TOP | Graphics.LEFT);
|
Graphics.TOP | Graphics.LEFT);
|
||||||
|
|
||||||
cursor[0] += ch[XADV];
|
cursor[0] += ch[XADV];
|
||||||
|