Ejemplo n.º 1
0
        public static ZiFontV5 FromCharacterBitmaps(string fontName, byte width, byte height, CodePage codePage, List <Bitmap> characters, bool invertColour = false)
        {
            var bytesPerChar   = width * height / 8;
            var charDataLength = (uint)(bytesPerChar * characters.Count());

            var ziFont = new ZiFontV5 {
                Name = fontName,
                //FileNameLength = (byte) fontName.Length,
                CharacterWidth     = width,
                CharacterHeight    = height,
                CodePage           = codePage,
                CharDataLength     = charDataLength,
                VariableDataLength = (uint)fontName.Length + charDataLength,
            };

            ziFont._charData = ziFont.CreateCharData(characters, invertColour);

            //var charData = new List<byte>();
            //foreach (var cb in characters) {
            //    charData.AddRange(BinaryTools.BitmapTo1BppData(cb));
            //}
            //ziFont._charData = charData.ToArray();

            return(ziFont);
        }
Ejemplo n.º 2
0
        public static ZiFontV5 FromBytes(byte[] bytes)
        {
            if (!VerifyHeader(bytes))
            {
                return(null);
            }

            var ziFont = new ZiFontV5();

            ziFont.bytes = bytes;

            ziFont._header = bytes.Take(HEADER_LENGTH).ToArray();
            var fileNameLength = ziFont._header[0x11];

            ziFont.Name     = Encoding.ASCII.GetString(bytes.Skip(HEADER_LENGTH).Take(fileNameLength).ToArray());
            ziFont.FileSize = bytes.Length;

            ziFont.CharacterWidth  = ziFont._header[0x6];
            ziFont.CharacterHeight = ziFont._header[0x7];

            ziFont.VariableDataLength = BitConverter.ToUInt32(ziFont._header.Skip(0x14).Take(4).ToArray(), 0);
            ziFont.CharDataLength     = ziFont.VariableDataLength - fileNameLength;

            var dataStartAddress = BitConverter.ToUInt32(ziFont._header.Skip(0x18).Take(4).ToArray(), 0);

            ziFont._charData = bytes.Skip((int)dataStartAddress + fileNameLength).ToArray();

            var codePageId     = ziFont._header[0x4];
            var characterCount = BitConverter.ToUInt32(ziFont._header.Skip(0x0C).Take(4).ToArray(), 0);

            ziFont.Characters = new List <IZiCharacter>();
            ziFont.CodePage   = new CodePage((CodePageIdentifier)codePageId);

            var charMapData = bytes.Skip(HEADER_LENGTH + ziFont.FileNameLength).Take(10 * (int)characterCount).ToArray();
            var align8byte  = bytes.Skip(33).First();

            for (int i = 0; i < charMapData.Length; i += 10)
            {
                var dataAddressOffset = BitConverter.ToUInt32(new byte[] { charMapData[i + 5], charMapData[i + 6], charMapData[i + 7], 0x00 }, 0);
                if (align8byte == 1)
                {
                    dataAddressOffset *= 8;
                }
                var dataLength = BitConverter.ToUInt16(charMapData, i + 8);

                var data = new byte[dataLength];
                Array.Copy(bytes, HEADER_LENGTH + ziFont.FileNameLength + dataAddressOffset, data, 0, dataLength);

                var ch = new ZiCharacterV5
                         (
                    ziFont,
                    BitConverter.ToUInt16(charMapData, i),
                    data,
                    charMapData[i + 2],
                    charMapData[i + 3],
                    charMapData[i + 4]
                         );
                ziFont.Characters.Add(ch);

                //Debug.WriteLine($"i: {i} code: {ch.CodePoint} width: {ch.GetBitmap().Width} dataOffset: n/a length: {ch.GetCharacterData().Length}");
            }
            return(ziFont);
        }