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); }
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()); } }