Exemple #1
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);
            }
        }
Exemple #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);
            }
        }