예제 #1
0
        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            WorkerResult result = e.Result as WorkerResult;
            FileInfo     file   = new FileInfo(result.data.selectedTTFFiles[0]);

            string directory = file.DirectoryName;

            if (result.builder != null)
            {
                try
                {
                    string log = file.FullName + ".log";
                    File.WriteAllText(log, result.builder.ToString());
                    Process.Start("explorer", log);
                }
                catch (Exception ex)
                {
                    string message = string.Format("{0}", ex);
                    MessageBox.Show(message);
                }
            }

            if (e.Error != null)
            {
                MessageBox.Show(e.Error.ToString(), "发生异常");
                if (MessageBox.Show("是否打开保存结果的文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    Process.Start("explorer", directory);
                }
            }
            else if (e.Cancelled)
            {
                if (MessageBox.Show("您取消了操作,是否打开保存结果的文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    Process.Start("explorer", directory);
                }
            }
            else
            {
                if (MessageBox.Show("操作已完成,是否打开保存结果的文件夹?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    Process.Start("explorer", directory);
                }
            }

            pgbProgress.Value           = pgbProgress.Minimum;
            pgbSingleFileProgress.Value = pgbSingleFileProgress.Minimum;

            WorkingSwitch(false);
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
            }
        }