Example #1
0
        private void RestoreOriginalFont()
        {
            File.Delete(_modifiedFontLocation);
            File.Delete(_ttxLocation);
            File.Copy(_originalFontLocation, _modifiedFontLocation);
            TTXUtils.ExecuteTTXConversion(_modifiedFontLocation);
            File.Delete(_modifiedFontLocation);
            File.Delete(TTXUtils.GetHistoryGlyphFileLocation(_ttxLocation));

            picTestFont.Refresh();
        }
Example #2
0
        private void ConvertTTXtoTTF_Click(object sender, EventArgs e)
        {
            StatusBar.Text = "Wait for conversion complete...";

            var ttfFilePath = Path.Combine(Path.GetDirectoryName(TTXPathTextBox.Text), Path.GetFileNameWithoutExtension(TTXPathTextBox.Text) + ".ttf");

            if (File.Exists(ttfFilePath))
            {
                var dialogResult = MessageBox.Show(this, $"File {Path.GetFileName(ttfFilePath)} already exists. Do you want to override it?", "Override file", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    File.Delete(ttfFilePath);
                }
            }

            TTXUtils.ExecuteTTXConversion(TTXPathTextBox.Text);
            picTestFont.Refresh();

            StatusBar.Text = "Сonversion successfully complete!";
        }
Example #3
0
        private void CheckOrCreateTTXFile()
        {
            //Check ttx file exists
            if (!File.Exists(_ttxLocation))
            {
                MessageBox.Show(this, $"TTX file not found. It will now be created with 'fonttools' util 'ttx'.", "TTX file doesn't exist", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (File.Exists(_modifiedFontLocation))
                {
                    File.Delete(_modifiedFontLocation);
                }
                File.Copy(_originalFontLocation, _modifiedFontLocation);
                TTXUtils.ExecuteTTXConversion(_modifiedFontLocation);
                File.Delete(_modifiedFontLocation);

                if (!File.Exists(_ttxLocation))
                {
                    MessageBox.Show(this, $"TTX file couldn't create. Please install 'fonttools' and try again.", "TTX wasn't created", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Environment.Exit(0);
                }
            }
        }
Example #4
0
        private async Task ExecuteFontCreation(
            string ttxPath,
            int startUniIndex,
            int maxGlyphWidth,
            int combinationCount,
            string[] textFiles,
            bool onlySmallLetters,
            double frequencyRatio,
            double totalWidthRatio)
        {
            await Task.Run(() =>
            {
                var ttxManager = new TTXManager(
                    ttxPath,
                    startUniIndex,
                    maxGlyphWidth
                    );

                var allCombinationProcessed = false;
                var excludedCombinations    = new List <string>();

                var symbolWidthsByUniIndexes = ttxManager.GetSymbolsWidth(_symbolsDictionary);

                var symbolWidthsBySymbols = new Dictionary <string, int>();
                foreach (var uniWidthPair in symbolWidthsByUniIndexes)
                {
                    var pairKey = _symbolsDictionary.First(x => x.Value == uniWidthPair.Key).Key;
                    if (pairKey == "<пробел>")
                    {
                        pairKey = " ";
                    }

                    symbolWidthsBySymbols.Add(pairKey, uniWidthPair.Value);
                }

                var analyzer = new TextAnalyzer(textFiles, Encoding.UTF8, onlySmallLetters, frequencyRatio, totalWidthRatio);
                for (int customSymbolIndex = 0; customSymbolIndex < combinationCount; customSymbolIndex++)
                {
                    var historyCombinations = new List <string>();
                    foreach (var dictPair in ttxManager.GlyphHistoryDictionary)
                    {
                        var historyCombination = string.Empty;
                        for (int dictPairValueIndex = 0; dictPairValueIndex < dictPair.Value.Length; dictPairValueIndex++)
                        {
                            if (dictPair.Value[dictPairValueIndex] == "space")
                            {
                                historyCombination += " ";
                            }
                            else
                            {
                                historyCombination += _symbolsDictionary.First(x => x.Value == dictPair.Value[dictPairValueIndex]).Key;
                            }
                        }
                        if (!string.IsNullOrEmpty(historyCombination))
                        {
                            historyCombinations.Add(historyCombination);
                        }
                    }

                    historyCombinations.AddRange(excludedCombinations);
                    analyzer.Analyze(historyCombinations.ToArray(), symbolWidthsBySymbols, maxGlyphWidth);

                    if (analyzer.CombinationInfos.Count == 0)
                    {
                        break;
                    }

                    var combinationProcessed      = false;
                    var combinationHistogramIndex = 0;
                    do
                    {
                        var replacedCombination = analyzer.CombinationInfos.ElementAt(combinationHistogramIndex).Value;

                        var combinedSymbols = new List <string>();
                        foreach (var sChar in replacedCombination)
                        {
                            if (sChar.ToString() != " ")
                            {
                                combinedSymbols.Add(_symbolsDictionary[sChar.ToString()]);
                            }
                            else
                            {
                                combinedSymbols.Add("space");
                            }
                        }

                        bool glyphCreated;
                        string glyphUniIndex;
                        (glyphCreated, glyphUniIndex) = ttxManager.AddСombinedGlyph(combinedSymbols.ToArray());
                        if (glyphCreated)
                        {
                            analyzer.ReplaceWithSymbol(replacedCombination, (char)(int.Parse(glyphUniIndex.Substring(3), System.Globalization.NumberStyles.HexNumber)));
                            combinationProcessed = true;
                        }
                        else
                        {
                            if (combinationHistogramIndex == analyzer.CombinationInfos.Count - 1)
                            {
                                allCombinationProcessed = true;
                                break;
                            }

                            combinationHistogramIndex++;
                            excludedCombinations.Add(replacedCombination);
                        }
                    } while (!combinationProcessed);
                    if (allCombinationProcessed)
                    {
                        break;
                    }

                    StatusStrip.Invoke((MethodInvoker) delegate { StatusProgressBar.Value = customSymbolIndex + 1; });
                }

                ttxManager.SaveChanges();

                var ttfFilePath = Path.Combine(Path.GetDirectoryName(ttxPath), Path.GetFileNameWithoutExtension(ttxPath) + ".ttf");
                if (File.Exists(ttfFilePath))
                {
                    File.Delete(ttfFilePath);
                }

                TTXUtils.ExecuteTTXConversion(ttxPath);
                analyzer.SaveChanges();

                StatusStrip.Invoke((MethodInvoker) delegate { StatusProgressBar.Value = StatusProgressBar.Maximum; });
            });
        }