Exemplo n.º 1
0
        public static string WriteCAT(FontData data, int maxInRow)
        {
            string result = "";
            var    bytes  = data.HeaderToBytes();

            result += $"    //CAT Header. Length: {bytes.Count}";
            result += WriteBytes(bytes, maxInRow);
            result += "\n";
            return(result);
        }
Exemplo n.º 2
0
        public static void Save()
        {
            Load();
            Console.Write("3. Preparing...   ");
            FontData Data = new FontData();

            if (EmptyCharIndex - MinChar < 0 ||
                EmptyCharIndex - MinChar >= bmps.Length)
            {
                Console.WriteLine("Wrong EmptyCharIndex value.\"{0}\"\nPress any key to continue. . .", EmptyCharIndex - MinChar);
                Console.ReadKey();
                Environment.Exit(1);
            }

            using (var progress = new ProgressBar())
            {
                {
                    byte[] bytes   = new byte[(int)Math.Ceiling(maxXSize * maxYSize / 8.0)];
                    int    currBit = 0;
                    for (int y = 0; y < bmps[0].Height; y++)
                    {
                        for (int x = 0; x < bmps[0].Width; x++)
                        {
                            var col = bmps[EmptyCharIndex - MinChar].GetPixel(x, y);
                            if (col.G == 0)
                            {
                                bytes[currBit / 8] |= (byte)(1UL << (byte)(currBit % 8));
                            }
                            currBit++;
                        }
                    }
                    Data.Elems.Add(new CATEl((char)EmptyCharIndex, true, bytes.ToList(), EmptyCharIndex - MinChar));
                }

                for (int i = 0; i <= bmps.Length - 1; i++)
                {
                    byte[] bytes   = new byte[(int)Math.Ceiling(maxXSize * maxYSize / 8.0)];
                    int    currBit = 0;
                    for (int y = 0; y < bmps[i].Height; y++)
                    {
                        for (int x = 0; x < bmps[i].Width; x++)
                        {
                            var col = bmps[i].GetPixel(x, y);
                            if (col.G == 0)
                            {
                                bytes[currBit / 8] |= (byte)(1UL << (byte)(currBit % 8));
                            }
                            currBit++;
                        }
                    }
                    currBit = 0;
                    progress.Report(i / (double)bmps.Length);
                    Data.Elems.Add(new CATEl((char)(i + MinChar), false, bytes.ToList(), i));
                }
            }
            Console.WriteLine();

            if (Encoding)
            {
                Console.Write("4. Compressing...   ");
                using (var progress = new ProgressBar())
                {
                    Data.Encode();
                    progress.Report(0.5);
                    Data.CreateHeader();
                }
            }

            if (SaveTotalImage)
            {
                SaveTotal();
            }

            if (HFile)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(Writer.WriteHeader(name));
                sb.Append(Writer.WriteDataHeader((byte)maxXSize, (byte)maxYSize, (byte)MinChar, (byte)MaxChar, Encoding, Data.AverageCompressionRatio, bmps.Length, Data));
                if (Encoding)
                {
                    sb.Append(Writer.WriteCAT(Data, Format_MaxBytes));
                }
                foreach (var a in Data.Elems)
                {
                    sb.Append(Writer.WriteSymbolData(a, Format_MaxBytes));
                }
                sb.Append(Writer.WriteFooter(name, bmps[0].Width, bmps[0].Height, (byte)MinChar, (byte)MaxChar, Encoding));

                File.WriteAllText(string.Format("font_{0}.h", name), sb.ToString());
            }
            else
            {
                var result = new List <byte>
                {
                    (byte)W,
                    (byte)H,
                    (byte)MinChar,
                    (byte)MaxChar,
                    (byte)(Encoding ? 1 : 0)
                };
                if (Encoding)
                {
                    result.AddRange(Data.HeaderToBytes());
                }
                foreach (var a in Data.Elems)
                {
                    result.AddRange(a.Bytes);
                }

                File.WriteAllBytes(string.Format("font_{0}.raw", name), result.ToArray());
            }
        }
Exemplo n.º 3
0
        public static string WriteDataHeader(byte maxXSize, byte maxYSize, byte minChar, byte maxChar,
                                             bool useEncoding, float AverageCompressionRatio, int len, FontData data)
        {
            string result = "";

            result += $"    //Font Info. Symbol X size: {maxXSize}, Symbol Y size: {maxYSize}";
            result += $"\n    //Stars from: {minChar} {(!char.IsControl((char)minChar) ? "or '" + (char)minChar + "'" : "")},";
            result += $" ends with: {maxChar} {(!char.IsControl((char)maxChar) ? "or '" + (char)maxChar + "' " : "")}\n";

            var totalSize = 0;

            if (useEncoding)
            {
                totalSize += data.HeaderElems.Last().Offset + data.HeaderElems.Last().Length;
            }
            else
            {
                totalSize = (int)Math.Ceiling(maxXSize * maxYSize / 8.0) * len + 4;
            }

            result += $"    //Total size: {totalSize} bytes";
            if (useEncoding)
            {
                result += $"\n    //Uncopressed total size: {(int)Math.Ceiling(maxXSize * maxYSize / 8.0) * len + 4} bytes";
                int _len = data.HeaderElems.FindAll(p => p.EncodeSymbol).Count();
                result += $"\n    //Using RLE compression:" +
                          $"\n    //    Data Compression Ratio:  {(AverageCompressionRatio * 100).ToString("0.00") + '%'}" +
                          $"\n    //    Total Compression Ratio: { ((1 - totalSize / ((float)Math.Ceiling(maxXSize * maxYSize / 8.0) * len + 4)) * 100).ToString("0.00") + '%' }" +
                          $"\n    //    Compressed chars:  {_len}" +
                          $"\n    //    Uncompressed chars:  {data.HeaderElems.Count - _len}";
            }
            result += $"    ";
            result += WriteBytes(new List <byte> {
                maxXSize, maxYSize, minChar, maxChar
            }, 10);
            return(result);
        }