Ejemplo n.º 1
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; });
            });
        }
Ejemplo n.º 2
0
        private void ProcessButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(StartUniIndexTextBox.Text))
            {
                StatusBar.Text = "Cannot start processing: 'Start Uni Index' field is empty.";
                return;
            }
            if (!int.TryParse(StartUniIndexTextBox.Text, System.Globalization.NumberStyles.HexNumber, null, out var startUniIndex))
            {
                StatusBar.Text = "Cannot start processing: 'Start Uni Index' field has invalid value. It should be hex number.";
                return;
            }
            if (PlannedGlyphSymbolsList.Items.Count < 2)
            {
                StatusBar.Text = "Cannot start processing: you should add at least two symbols for processing.";
                return;
            }
            if (string.IsNullOrEmpty(MaxCustomWidthTextBox.Text))
            {
                StatusBar.Text = "Cannot start processing: 'MaxCustomWidthTextBox' field is empty.";
                return;
            }
            if (!int.TryParse(MaxCustomWidthTextBox.Text, out var maxGlyphWidth))
            {
                StatusBar.Text = "Cannot start processing: 'MaxCustomWidthTextBox' field has invalid value. It should be decimal number.";
                return;
            }
            if (ReplaceManualCheckBox.Checked && TextFilesList.Items.Count == 0)
            {
                StatusBar.Text = "Cannot start processing: replace for manual mode enabled but file(s) not selected.";
                return;
            }

            StatusBar.Text = "Processing, please wait...";

            var ttxManager = new TTXManager(
                TTXPathTextBox.Text,
                startUniIndex,
                maxGlyphWidth
                );

            List <string> symbolCodes = new List <string>();

            foreach (var item in PlannedGlyphSymbolsList.Items)
            {
                var targetKey = item.ToString();

                if (targetKey == "<пробел>")
                {
                    symbolCodes.Add("space");
                }
                else
                {
                    symbolCodes.Add(_symbolsDictionary[targetKey]);
                }
            }

            try
            {
                bool   glyphCreated;
                string glyphUniIndex;
                (glyphCreated, glyphUniIndex) = ttxManager.AddСombinedGlyph(symbolCodes.ToArray());

                if (!glyphCreated)
                {
                    StatusBar.Text = $"Conversion failed: Glyph already exists.";
                    return;
                }
                ttxManager.SaveChanges();


                if (ReplaceManualCheckBox.Checked)
                {
                    string textForReplace = string.Join("", PlannedGlyphSymbolsList.Items.Cast <string>()).Replace("<пробел>", " ");

                    var analyzer = new TextAnalyzer(
                        TextFilesList.Items.Cast <string>().ToArray(),
                        Encoding.UTF8,
                        SmallLetterCheckBox.Checked,
                        Math.Round((100.0 - (tbWidthRatio.Value * 5)) / 100.0, 2),
                        Math.Round(tbWidthRatio.Value * 5 / 100.0, 2));

                    analyzer.ReplaceWithSymbol(textForReplace, (char)int.Parse(glyphUniIndex.Substring(3), System.Globalization.NumberStyles.HexNumber));
                    analyzer.SaveChanges();
                }

                foreach (var item in PlannedGlyphSymbolsList.Items)
                {
                    var targetKey = item.ToString();

                    if (targetKey == "<пробел>")
                    {
                        TestPhrase2TextBox.Text += " ";
                    }
                    else
                    {
                        TestPhrase2TextBox.Text += targetKey;
                    }
                }
                PlannedListClearButton.PerformClick();

                SystemSounds.Beep.Play();
                StatusBar.Text = "Glyph creation successfully completed!";
            }
            catch (Exception exception)
            {
                SystemSounds.Hand.Play();
                StatusBar.Text = $"Conversion failed: {exception.Message}";

                File.AppendAllText("Errors.log", $"{DateTime.Now}: Error message - {exception.Message}{Environment.NewLine}{exception.StackTrace}{Environment.NewLine}{Environment.NewLine}");
            }
        }