Beispiel #1
0
        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SingleFileProgress progress = e.UserState as SingleFileProgress;
            {
                var value = e.ProgressPercentage;
                if (value < pgbProgress.Minimum)
                {
                    value = pgbProgress.Minimum;
                }
                if (value > pgbProgress.Maximum)
                {
                    value = pgbProgress.Maximum;
                }
                pgbProgress.Value  = value;
                this.lblTotal.Text = string.Format("Working on: {0}", progress.fontName);
            }
            {
                var value = progress.progress;
                if (value < pgbSingleFileProgress.Minimum)
                {
                    value = pgbSingleFileProgress.Minimum;
                }
                if (value > pgbSingleFileProgress.Maximum)
                {
                    value = pgbSingleFileProgress.Maximum;
                }
                pgbSingleFileProgress.Value = value;

                this.lblSingleFileProgress.Text = progress.message;
            }
        }
Beispiel #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);
            }
        }