add Measure() into bmf

This commit is contained in:
sillysagiri 2024-10-15 22:39:11 +07:00
parent 8b406c8a9b
commit ebf171b5d7
5 changed files with 48 additions and 1 deletions

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -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];