Index: FontCharacter.cs =================================================================== --- FontCharacter.cs (revision 81403) +++ FontCharacter.cs (working copy) @@ -6,6 +6,150 @@ { public class FontCharacter { + private class SingleForm + { + byte[] Data; + bool Filled; + Image LoadedImage; + byte Height; + byte Width; + UInt32 Bits; + + public SingleForm(byte[] data, byte height, byte width, UInt32 bits) + { + this.Data = data; + this.Filled = false; + this.Height = height; + this.Width = width; + this.Bits = bits; + } + + public Image GetCharacter() + { + if (Filled) + { + return LoadedImage; + } + else + { + LoadedImage = LoadFromBinary(Data, Height, Width, Bits); + Filled = true; + Data = null; + return LoadedImage; + } + } + + private byte ReadByte(bool[] data) + { + byte r = 0; + for (int i = 0; i < 8; i++) + { + if (!data[i]) // The data loading seems to invert the bools. + { + r <<= 1; + r += 1; + } + } + return r; + } + + private Image LoadFromBinary(byte[] data, byte height, byte width, uint bits) + { + #region LoadData + bool[] idata = new bool[data.Length * 8]; + int bitnum = 0; + for (int inc = 0; inc < data.Length; inc++) + { + if ((data[inc] & 1) == 1) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 2) == 2) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 4) == 4) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 8) == 8) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 16) == 16) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 32) == 32) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 64) == 64) + { + idata[bitnum] = true; + } + bitnum++; + if ((data[inc] & 128) == 128) + { + idata[bitnum] = true; + } + bitnum++; + } + #endregion + + bitnum = 0; + Image i = new Image(width, height); + + //for (uint y = 0; y < height; y++) + for (uint x = 0; x < width; x++) + { + //for (uint x = 0; x < width; x++) + for (uint y = 0; y < height; y++) + { + if (bitnum >= bits) + { + break; + } + if (idata[bitnum]) + { + bitnum++; + if (idata[bitnum]) + { + bitnum++; + i.SetPixel(x, y, Colors.Black); // Color the pixel white + } + else + { + bitnum++; + bool[] tmp = new bool[8]; + Array.Copy(idata, bitnum, tmp, 0, 8); + bitnum += 8; + byte greyscale = ReadByte(tmp); + i.SetPixel(x, y, new Pixel(greyscale, greyscale, greyscale, 255)); + } + } + else + { + bitnum++; + i.SetPixel(x, y, Colors.White); // Color the pixel black + } + } + if (bitnum >= bits) + { + break; + } + } + + return i; + } + } + Image[] forms; public FontCharacter(Image character, FontFlag flags) Index: OPFF.cs =================================================================== --- OPFF.cs (revision 81403) +++ OPFF.cs (working copy) @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.IO; namespace Orvid.Graphics.FontSupport { @@ -25,6 +26,12 @@ get { return name; } } + private UInt16 ver; + public UInt16 FileFormatVersion + { + get { return ver; } + } + FontCharacterSet foundChars = new FontCharacterSet(); public OPFF(byte[] data) @@ -55,7 +62,7 @@ return r; } - private UInt32 ReadInt32(byte[] data) + private UInt32 ReadUInt32(byte[] data) { UInt32 r = 0; @@ -70,6 +77,17 @@ return r; } + private UInt16 ReadUInt16(byte[] data) + { + UInt16 r = 0; + + r += data[1]; + r <<= 8; + r += data[0]; + + return r; + } + private void Load(byte[] data) { if (data[0] == 0xFF) // this means it's been compressed in LZMA format. @@ -82,10 +100,19 @@ int curloc = 8; // There are 8 empty bytes at the start of the header. - byte[] datarr = new byte[256]; + byte[] datarr = new byte[2]; + Array.Copy(data, curloc, datarr, 0, 2); + curloc += 2; + ver = ReadUInt16(datarr); + if (ver > 47) + { + throw new Exception("Format version is to high!"); + } + + datarr = new byte[256]; Array.Copy(data, curloc, datarr, 0, 256); curloc += 256; - name = new String(ASCIIEncoding.ASCII.GetChars(datarr)); + name = new String(ASCIIEncoding.ASCII.GetChars(datarr)).Replace("\0",""); datarr = new byte[8]; Array.Copy(data, curloc, datarr, 0, 8); @@ -106,15 +133,15 @@ curloc++; byte width = data[curloc]; curloc++; - int len = (Int32)Math.Ceiling((double)((width * height) / 8)); + datarr = new byte[4]; + Array.Copy(data, curloc, datarr, 0, 4); + curloc += 4; + int bits = (int)ReadUInt32(datarr); + int len = (Int32)Math.Ceiling((double)(bits / 8)); datarr = new byte[len]; Array.Copy(data, curloc, datarr, 0, len); curloc += len; - Image im = LoadFromBinary(datarr, height, width); - if (prevCharNumber > ushort.MaxValue) - { - throw new Exception(); - } + Image im = LoadFromBinary(datarr, height, width, bits); foundChars.AddCharacter((int)prevCharNumber, im, flags); } else @@ -123,31 +150,45 @@ datarr = new byte[4]; Array.Copy(data, curloc, datarr, 0, 4); curloc += 4; - prevCharNumber = ReadInt32(datarr); - if (prevCharNumber > ushort.MaxValue) - { - throw new Exception(); - } + prevCharNumber = ReadUInt32(datarr); FontFlag flags = (FontFlag)data[curloc]; curloc++; byte height = data[curloc]; curloc++; byte width = data[curloc]; curloc++; - int len = (Int32)Math.Ceiling((double)((width * height) / 8)); + datarr = new byte[4]; + Array.Copy(data, curloc, datarr, 0, 4); + curloc += 4; + int bits = (int)ReadUInt32(datarr); + int len = (Int32)Math.Ceiling((double)(bits / 8)); datarr = new byte[len]; Array.Copy(data, curloc, datarr, 0, len); curloc += len; - Image im = LoadFromBinary(datarr, height, width); + Image im = LoadFromBinary(datarr, height, width, bits); foundChars.AddCharacter((int)prevCharNumber, im, flags); } } } - private Image LoadFromBinary(byte[] data, byte height, byte width) + private byte ReadByte(bool[] data) { + byte r = 0; + for (int i = 0; i < 8; i++) + { + if (data[i]) // The data loading seems to invert the bools. + { + r |= (byte)(((byte)1) << i); + } + } + return r; + } + + private Image LoadFromBinary(byte[] data, byte height, byte width, int bits) + { #region LoadData - bool[] idata = new bool[height * width]; + //Array.Reverse(data); + bool[] idata = new bool[data.Length * 8]; int bitnum = 0; for (int inc = 0; inc < data.Length; inc++) { @@ -166,70 +207,139 @@ // throw new Exception(); //} - if (((byte)(data[inc] << 7) >> 7) == 1) + #region HighToLowBitLoading + if ((data[inc] & 128) == 128) { idata[bitnum] = true; } bitnum++; - if (((byte)(data[inc] << 6) >> 7) == 1) + if ((data[inc] & 64) == 64) { idata[bitnum] = true; } bitnum++; - if (((byte)(data[inc] << 5) >> 7) == 1) + if ((data[inc] & 32) == 32) { idata[bitnum] = true; } bitnum++; - if (((byte)(data[inc] << 4) >> 7) == 1) + if ((data[inc] & 16) == 16) { idata[bitnum] = true; } bitnum++; - if (((byte)(data[inc] << 3) >> 7) == 1) + if ((data[inc] & 8) == 8) { idata[bitnum] = true; } bitnum++; - if (((byte)(data[inc] << 2) >> 7) == 1) + if ((data[inc] & 4) == 4) { idata[bitnum] = true; } bitnum++; - if (((byte)(data[inc] << 1) >> 7) == 1) + if ((data[inc] & 2) == 2) { idata[bitnum] = true; } bitnum++; - if ((data[inc] >> 7) == 1) + if ((data[inc] & 1) == 1) { idata[bitnum] = true; } bitnum++; + #endregion + + #region LowToHighBitLoading + //if ((data[inc] & 1) == 1) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 2) == 2) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 4) == 4) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 8) == 8) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 16) == 16) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 32) == 32) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 64) == 64) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + //if ((data[inc] & 128) == 128) + //{ + // idata[bitnum] = true; + //} + //bitnum++; + #endregion + } #endregion bitnum = 0; Image i = new Image(width, height); + //StreamWriter s = new StreamWriter(Path.GetFullPath("lg.txt")); //for (uint y = 0; y < height; y++) + //for (uint y = (uint)(height - 1); (y >= 0 && y != uint.MaxValue); y--) for (uint x = 0; x < width; x++) + //for (uint x = (uint)(width - 1); (x >= 0 && x != uint.MaxValue); x--) { //for (uint x = 0; x < width; x++) + //for (uint x = (uint)(width - 1); (x >= 0 && x != uint.MaxValue); x--) for (uint y = 0; y < height; y++) + //for (uint y = (uint)(height - 1); (y >= 0 && y != uint.MaxValue); y--) { if (idata[bitnum]) { - i.SetPixel(x, y, Colors.Black); // Color the pixel white + bitnum++; + if (idata[bitnum]) + { + bitnum++; + //s.WriteLine("Pixel at (" + x.ToString() + ", " + y.ToString() + ") is 00"); + i.SetPixel(x, y, Colors.Black); // Color the pixel black + } + else + { + bitnum++; + bool[] tmp = new bool[8]; + Array.Copy(idata, bitnum, tmp, 0, 8); + //s.WriteLine("Pixel at (" + x.ToString() + ", " + y.ToString() + ") is 01-" + (tmp[0] ? "1" : "0") + (tmp[1] ? "1" : "0") + (tmp[2] ? "1" : "0") + (tmp[3] ? "1" : "0") + (tmp[4] ? "1" : "0") + (tmp[5] ? "1" : "0") + (tmp[6] ? "1" : "0") + (tmp[7] ? "1" : "0")); + bitnum += 8; + byte greyscale = ReadByte(tmp); + i.SetPixel(x, y, new Pixel(greyscale, greyscale, greyscale, 255)); + } } else { - i.SetPixel(x, y, Colors.White); // Color the pixel black + bitnum++; + //s.WriteLine("Pixel at (" + x.ToString() + ", " + y.ToString() + ") is 1"); + i.SetPixel(x, y, Colors.White); } - bitnum++; } } - + //s.Flush(); + //s.Close(); return i; }