private void btnStart_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtTTFFullname.Text) || this.selectedTTFFiles == null) { string message = string.Format("{0}", "Please select a TTF file first!"); MessageBox.Show(message); return; } WorkingSwitch(true); int fontHeight = (int)numFontHeight.Value; int maxTexturWidth = (int)numMaxTexturWidth.Value; char firstChar = (char)int.Parse(this.txtFirstIndex.Text); char lastChar = (char)int.Parse(this.txtLastIndex.Text); bool generateGlyphList = this.chkDumpGlyphList.Checked; bool drawBBox = this.chkDrawBBox.Checked; WorkerData data = new WorkerData(fontHeight, maxTexturWidth, firstChar, lastChar, this.selectedTTFFiles, generateGlyphList, drawBBox); this.bgWorker.RunWorkerAsync(data); }
public WorkerResult(StringBuilder builder, WorkerData data) { this.builder = builder; this.data = data; }
private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerData data = e.Argument as WorkerData; int fontFileCount = data.selectedTTFFiles.Length; int fontFileIndex = 1; const int magicNumber = 100; WorkerResult result = new WorkerResult(null, data); e.Result = result; StringBuilder builder = new StringBuilder(); foreach (var fontFullname in this.selectedTTFFiles) { builder.Append(fontFileIndex); builder.Append("/"); builder.Append(fontFileCount); builder.Append(": "); builder.AppendLine(fontFullname); FileInfo fileInfo = new FileInfo(fontFullname); string fontName = fileInfo.Name; string destFullname = fontFullname + ".png"; FontTexture ttfTexture = null; try { { int lastPercent = 0; foreach (var progress in FontTextureYieldHelper.GetTTFTexture( fontFullname, data.fontHeight, data.maxTexturWidth, data.firstChar, data.lastChar)) { ttfTexture = progress.ttfTexture; if (progress.percent != lastPercent) { var singleFileProgress = new SingleFileProgress() { fontName = fontName, progress = progress.percent, message = progress.message }; bgWorker.ReportProgress(fontFileIndex * magicNumber / fontFileCount, singleFileProgress); lastPercent = progress.percent; } } } System.Drawing.Bitmap bigBitmap = ttfTexture.BigBitmap; if (data.drawBBox) { Graphics g = Graphics.FromImage(bigBitmap); int vertialLineIndex = 0; int characterCount = ttfTexture.CharInfoDict.Values.Count; int lastPercent = 0; foreach (var item in ttfTexture.CharInfoDict.Values) { int left = item.xoffset; int right = item.xoffset + item.width - 1; int top = item.yoffset; int bottom = item.yoffset + ttfTexture.FontHeight - 1; Point[] points = new Point[] { new Point(left, top), new Point(right, top), new Point(right, bottom), new Point(left, bottom), new Point(left, top) }; g.DrawLines(pen, points); int percent = vertialLineIndex++ *100 / characterCount; if (percent != lastPercent) { var singleFileProgress = new SingleFileProgress() { fontName = fontName, progress = percent, message = "drawing bboxes" }; bgWorker.ReportProgress(fontFileIndex * magicNumber / fontFileCount, singleFileProgress); lastPercent = percent; } } g.Dispose(); bigBitmap.Save(destFullname); } bigBitmap.Save(destFullname); { FontTextureXmlPrinter printer = new FontTextureXmlPrinter(ttfTexture); printer.Print(fontFullname); } if (data.generateGlyphList) { FontTexturePNGPrinter printer = new FontTexturePNGPrinter(ttfTexture); foreach (var progress in printer.Print(fontFullname, data.maxTexturWidth)) { bgWorker.ReportProgress(fontFileIndex * magicNumber / fontFileCount, progress); } } } catch (Exception ex) { string message = string.Format("{0}", ex); builder.AppendLine(message); result.builder = builder; } if (ttfTexture != null) { ttfTexture.Dispose(); } if (result.builder != builder) { builder.AppendLine("sucessfully done!"); } builder.AppendLine(); SingleFileProgress thisFileDone = new SingleFileProgress() { fontName = fontName, progress = magicNumber, message = string.Format("All is done for {0}", fileInfo.Name), }; bgWorker.ReportProgress(fontFileIndex++ *magicNumber / fontFileCount, thisFileDone); } }