public Fontheader(String fileName) { FileStream streamReader; streamReader = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); long pos = 0; List <byte> myBytes = new List <byte>(); BinaryReader br = new BinaryReader(streamReader); br.BaseStream.Seek(0, SeekOrigin.Begin); //start from 0 FileMarker = br.ReadBytes(4); //offset 0x04 FileVersion = Encoding.ASCII.GetString(br.ReadBytes(3)); if (FileVersion == "1.0") { fontVersion = FontVersion.Version10; } else if (FileVersion == "1.3") { fontVersion = FontVersion.Version13; headerSize = 71; } else if (FileVersion == "2.0") { fontVersion = FontVersion.Version20; } //read header bytes pos = br.BaseStream.Position; myHeaderBytes.AddRange(br.ReadBytes((int)fontVersion)); br.BaseStream.Seek(pos, SeekOrigin.Begin); //reset pos if (fontVersion == FontVersion.Version13 || fontVersion == FontVersion.Version20) { br.ReadByte(); //advance 1 byte for NUL terminator, except for Version 1.0 font } if (fontVersion == FontVersion.Version20) { byte[] bHeaderSize = br.ReadBytes(4); headerSize = (uint)(bHeaderSize[0] + 0xff * bHeaderSize[1] + 0xff00 * bHeaderSize[2] + 0xff0000 * bHeaderSize[3]);// Convert.ToUInt32((bHeaderSize); //read 4 bytes of header size (normally 96), only with version 2.0 font } ModuloFontName = br.ReadByte(); FontNameShort = Encoding.ASCII.GetString(br.ReadBytes(5)); if (fontVersion == FontVersion.Version13 || fontVersion == FontVersion.Version20) { br.ReadByte(); //advance 1 byte for NUL terminator, except for Version 1.0 font } FontID = br.ReadByte(); //offset 0x0d if (fontVersion == FontVersion.Version13 || fontVersion == FontVersion.Version20) { br.ReadBytes(4); //ignore PICA, Elite and there italic font IDs } AllwaysZero = br.ReadByte(); if (AllwaysZero != 0) { System.Diagnostics.Debug.WriteLine("not a font?, allways zero byte is=" + AllwaysZero.ToString()); } if (fontVersion == FontVersion.Version13 || fontVersion == FontVersion.Version20) { displayCode = br.ReadByte(); } byte[] bUint16 = null; bUint16 = br.ReadBytes(2); CharWidth = getUint16(bUint16); //offset 0x1F and 0x20 if (fontVersion == FontVersion.Version13 || fontVersion == FontVersion.Version20) { for (int i = 0; i < 4; i++) { br.ReadBytes(2); //ignore PICA, Elite and there italic font width } } bUint16 = br.ReadBytes(2); CharHeight = getUint16(bUint16); //offset 0x21 and 0x22 if (fontVersion == FontVersion.Version10 || fontVersion == FontVersion.Version13) { numBytesPerRow = br.ReadByte(); //offset 0x23 } else if (fontVersion == FontVersion.Version20) { numBytesPerRow = getUint16(br.ReadBytes(2)); // version 2.0 font uses two bytes } numBytesPerChar = (getUint16(br.ReadBytes(2))); //offset 0x24 and 0x25 codeStart = br.ReadByte(); //offset 0x26 codeEnd = br.ReadByte(); //offset 0x27 underLineRow = br.ReadByte(); //always 0 for version 1.0 font UserVersionNumber = br.ReadByte(); //offset 0x29 USER version number if (fontVersion == FontVersion.Version10) { UserDate = Encoding.ASCII.GetString(br.ReadBytes(8)); //offset 0x2A } else if (fontVersion == FontVersion.Version13) { UserDate = Encoding.ASCII.GetString(br.ReadBytes(8)); //offset 0x2A br.ReadByte(); //read nul terminator for version 1.3 and 2.0 } else if (fontVersion == FontVersion.Version20) { UserDate = Encoding.ASCII.GetString(br.ReadBytes(10)); br.ReadByte(); //read nul terminator for version 1.3 and 2.0 } byte[] buf = null; if (fontVersion == FontVersion.Version10) { buf = br.ReadBytes(20); //offset 0x32 to 0x46 } else if (fontVersion == FontVersion.Version13 || fontVersion == FontVersion.Version20) { buf = br.ReadBytes(21); //offset 0x32 to 0x46 } FontNameLong = Encoding.ASCII.GetString(buf); FontNameLong = FontNameLong.Replace('\0', ' '); if (fontVersion == FontVersion.Version20) { br.ReadBytes(15); //read fill bytes 15 bytes with 0xff } // ############## end of header ###################### //read all header bytes again pos = br.BaseStream.Position; //mark last position //read header bytes for (int c = 0; c < headerSize; c++) { myBytes.Add(br.ReadByte()); } br.BaseStream.Seek(pos, SeekOrigin.Begin); //store all bitmap bytes List <myBitmap.myBitmapChar> allCharBitmaps = new List <myBitmap.myBitmapChar>(); //store the bitmap pixels in a string StringBuilder sbFont = new StringBuilder(); for (int i = codeStart; i <= codeEnd; i++) { System.Diagnostics.Debug.WriteLine("################################"); //start a new charBitmap List <myBitmap.myBitmapRow> charBitmapRow = new List <myBitmap.myBitmapRow>(); System.Diagnostics.Debug.WriteLine("reading code point: " + i.ToString()); List <byte> bitmapRow = new List <byte>(); int thisCharWidth = this.CharWidth; if (thisCharWidth == ProportionalMark) { //is proportional font? byte[] bcw = br.ReadBytes(2); thisCharWidth = bcw[1] * 0xff + bcw[0]; //read char width } //start a new char bitmap row for (int y = 0; y < CharHeight; y++) { //System.Diagnostics.Debug.WriteLine("reading byte lines: " + y.ToString()); //read num bytes per row for (int iBytesPerRow = 0; iBytesPerRow < this.numBytesPerRow; iBytesPerRow++) { byte currByte = br.ReadByte(); //add current byte to list of bytes per row bitmapRow.Add(currByte); myBytes.Add(currByte); //add pixel to string representation sbFont.Append(Convert.ToString(currByte, 2).PadLeft(8, '0').Replace("0", " ").Replace("1", "█")); } //add row to bitamp myBitmap.myBitmapRow rowX = new myBitmap.myBitmapRow(bitmapRow.ToArray()); charBitmapRow.Add(rowX); bitmapRow.Clear(); //start a new line in the string representation sbFont.Append("\r\n"); //add one row of bytes to list of rows }//iterate thru char rows fontDisplay = sbFont.ToString(); //add bitmap matrix as char bitmap myBitmap.myBitmapChar charX = new myBitmap.myBitmapChar(charBitmapRow.ToArray(), thisCharWidth); allCharBitmaps.Add(charX); charBitmapRow.Clear(); }//iterate thru chars allChars = new myBitmap.myBitmapAll(allCharBitmaps.ToArray()); myBitmap.myAllBitmaps allBitmaps = new myBitmap.myAllBitmaps(myBytes, numBytesPerRow, CharHeight, codeStart); //just a test byte[] bTest = allBitmaps.get(codeStart); System.Drawing.Bitmap bmp = allChars.getBitmap(1); }
public void readHeader() { try { FileStream streamReader; streamReader = new FileStream(_sFile, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader br = new BinaryReader(streamReader); //read header bytes for (int c = 0; c < 0x36; c++) { myHeaderBytes.Add(br.ReadByte()); } br.BaseStream.Seek(0, SeekOrigin.Begin); //start from 0 FileMarker = br.ReadBytes(4); //offset 0x04 FileVersion = Encoding.ASCII.GetString(br.ReadBytes(3)); ModuloFontName = br.ReadByte(); //offset 0x08 FontNameShort = Encoding.ASCII.GetString(br.ReadBytes(5)); FontID = br.ReadByte(); //offset 0x0d AllwaysZero = br.ReadByte(); if (AllwaysZero != 0) { System.Diagnostics.Debug.WriteLine("not a font?, allways zero byte is=" + AllwaysZero.ToString()); } byte[] bUint16; bUint16 = br.ReadBytes(2); CharWidth = getUint16(bUint16); //offset 0x1F and 0x20 bUint16 = br.ReadBytes(2); CharHeight = getUint16(bUint16); //offset 0x21 and 0x22 numBytesPerRow = br.ReadByte(); //offset 0x23 bUint16 = br.ReadBytes(2); numBytesPerChar = (getUint16(bUint16)); //offset 0x24 and 0x25 codeStart = br.ReadByte(); //offset 0x26 codeEnd = br.ReadByte(); //offset 0x27 bReserved3 = br.ReadByte(); //offset 0x28 UserVersionNumber = br.ReadByte(); //offset 0x29 USER version number UserDate = Encoding.ASCII.GetString(br.ReadBytes(8)); //offset 0x2A byte[] buf = br.ReadBytes(20); //offset 0x32 to 0x46 FontNameLong = Encoding.ASCII.GetString(buf); FontNameLong = FontNameLong.Replace('\0', ' '); //now we have 224 or so chars to read... //assume we have CharHeight lines for one char StringBuilder sbFont = new StringBuilder(); //store all bitmap bytes List <myBitmap.myBitmapChar> allCharBitmaps = new List <myBitmap.myBitmapChar>(); for (int i = codeStart; i <= codeEnd; i++) { System.Diagnostics.Debug.WriteLine("################################"); //start a new charBitmap List <myBitmap.myBitmapRow> charBitmapRow = new List <myBitmap.myBitmapRow>(); System.Diagnostics.Debug.WriteLine("reading code point: " + i.ToString()); List <byte> bitmapRow = new List <byte>(); int thisCharWidth = this.CharWidth; if (thisCharWidth == ProportionalMark) { //is proportional font? byte[] bcw = br.ReadBytes(2); thisCharWidth = bcw[1] * 0xff + bcw[0]; //read char width } //start a new char bitmap row for (int y = 0; y < CharHeight; y++) { //System.Diagnostics.Debug.WriteLine("reading byte lines: " + y.ToString()); //read num bytes per row for (int iBytesPerRow = 0; iBytesPerRow < this.numBytesPerRow; iBytesPerRow++) { byte currByte = br.ReadByte(); //add current byte to list of bytes per row bitmapRow.Add(currByte); myBytes.Add(currByte); sbFont.Append(Convert.ToString(currByte, 2).PadLeft(8, '0').Replace("0", " ").Replace("1", "█")); } //add row to bitamp myBitmap.myBitmapRow rowX = new myBitmap.myBitmapRow(bitmapRow.ToArray()); charBitmapRow.Add(rowX); bitmapRow.Clear(); sbFont.Append("\r\n"); //add one row of bytes to list of rows }//iterate thru char rows //add bitmap matrix as char bitmap myBitmap.myBitmapChar charX = new myBitmap.myBitmapChar(charBitmapRow.ToArray(), thisCharWidth); allCharBitmaps.Add(charX); charBitmapRow.Clear(); }//iterate thru chars allChars = new myBitmap.myBitmapAll(allCharBitmaps.ToArray()); fontDisplay = sbFont.ToString(); myBitmap.myAllBitmaps allBitmaps = new myBitmap.myAllBitmaps(myBytes, numBytesPerRow, CharHeight, codeStart); int codePoint = codeStart; byte[] bTest = allBitmaps.get(codePoint); System.Drawing.Bitmap bmp = allChars.getBitmap(codePoint); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } }