private static void Worker_Open(object sender, DoWorkEventArgs e) { var Worker = (BackgroundWorker)sender; var Input = (Stream)e.Argument; StructReader Reader = new StructReader(Input); if (Reader.PeekInt() != 0x414E46) { throw new Exception("The Input file isn't a FNA File"); } FNAHeader FontInfo = new FNAHeader(); Reader.ReadStruct(ref FontInfo); if (FontInfo.Version != 0) { throw new Exception("Unsupported FNA Version"); } int LastReportedProgress = -1; int Current = 0; int Steps = (from a in FontInfo.Fonts select(from b in a.Tables select b.Gylphs.Length).Sum()).Sum(); for (int a = 0; a < FontInfo.Fonts.Length; a++) { ref var Font = ref FontInfo.Fonts[a]; for (int b = 0; b < Font.Tables.Length; b++) { ref var Table = ref Font.Tables[b]; for (uint i = 0; i < Table.Gylphs.Length; i++) { if (Worker.CancellationPending) { e.Cancel = true; return; } int Progress = (int)Math.Abs(((double)(100 * ++Current)) / Steps); if (Progress != LastReportedProgress) { LastReportedProgress = Progress; Worker.ReportProgress(Progress); } ref var Glyph = ref Table.Gylphs[i]; if (Glyph.TextureOffset != 0) { Glyph.Data = new VirtStream(Input, Glyph.TextureOffset, Glyph.TextureSize); } else { Glyph.Data = null; } Glyph.Char = IndexToChar(i); Glyph.CreateTexture(Table.Height); }
public static async Task Save(this FNAHeader Font, Stream Output, Action <int> OnProgressChanged) { BackgroundWorker Worker = new BackgroundWorker(); Worker.WorkerReportsProgress = true; Worker.WorkerSupportsCancellation = true; Worker.DoWork += Worker_Save; bool Running = true; Worker.ProgressChanged += (a, b) => OnProgressChanged?.Invoke(b.ProgressPercentage); Worker.RunWorkerCompleted += (a, b) => Running = false; Worker.RunWorkerAsync(new object[] { Font, Output }); while (Running) { await Task.Delay(100); } }