private void CreateFontSubset(string fontFolder, string outputFolder, Dictionary <string, string> textsInAss,
                                      Dictionary <string, FontFileInfo> fontFiles, ref List <SubsetFontInfo> subsetFonts)
        {
            var processors = new List <Dictionary <string, string> >();

            foreach (var text in textsInAss)
            {
                var fontName   = text.Key;
                var characters = text.Value;

                var charactersFile = $@"{fontFolder}\{fontName}.txt";
                using (StreamWriter sw = new StreamWriter(charactersFile, false, new UTF8Encoding(false))) {
                    sw.Write(characters);
                }

                int    index    = fontFiles[fontName].FontNumberInCollection;
                string fontFile = fontFiles[fontName].FileName;

                string outputFile = $@"{outputFolder}\";
                if (fontFile.EndsWith(".ttc"))
                {
                    outputFile += $"{Path.GetFileNameWithoutExtension(fontFile)}.ttf";
                }
                else
                {
                    outputFile += $"{Path.GetFileName(fontFile)}";
                }

                var randomString   = this.RandomString(8);
                var subsetFontInfo = new SubsetFontInfo {
                    FontNameInAss    = fontName,
                    OriginalFontFile = fontFile,
                    SubsetFontFile   = outputFile + $".{randomString}._tmp_",
                    SubsetFontName   = randomString,
                    DumpedXmlFile    = $@"{outputFolder}\{Path.GetFileNameWithoutExtension(outputFile)}.{randomString}.ttx",
                    TrackIndex       = index
                };
                subsetFonts.Add(subsetFontInfo);

                var args = new Dictionary <string, string> {
                    { " ", fontFile },
                    { "--text-file=", charactersFile },
                    { "--output-file=", subsetFontInfo.SubsetFontFile },
                    { "--name-languages=", "0x0409" }
                };
                if (index > -1)
                {
                    args.Add("--font-number=", index.ToString());
                }

                processors.Add(args);
            }

            string exe = "pyftsubset.exe";

            Parallel.ForEach(processors, args => this.StartProcess(exe, args));
        }
示例#2
0
        private void CreateFontSubset(string fontFolder, string outputFolder, Dictionary <string, string> textsInAss,
                                      List <FontFileInfo> fontFiles, ref List <SubsetFontInfo> subsetFonts, ref Dictionary <string, string> rdNameLookUp)
        {
            var processors = new List <Dictionary <string, string> >();

            foreach (var font in fontFiles)
            {
                var fontName   = font.FontName;
                var characters = textsInAss[fontName];

                // fix font fallback on ellipsis.
                characters  = Regex.Replace(characters, @"[a-zA-Z0-9]", "", RegexOptions.Compiled);
                characters += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

                // remove all full width numeric characters and replace them with a full set of them.
                var fullwidth_numerical = new Regex(@"[1234567890]");
                if (fullwidth_numerical.IsMatch(characters))
                {
                    characters  = fullwidth_numerical.Replace(characters, "");
                    characters += "1234567890";
                }

                var charactersFile = $@"{fontFolder}\{fontName}.txt";
                using (StreamWriter sw = new StreamWriter(charactersFile, false, new UTF8Encoding(false))) {
                    sw.Write(characters);
                }

                int    index    = font.FontNumberInCollection;
                string fontFile = font.FileName;

                string outputFile = $@"{outputFolder}\";
                if (fontFile.EndsWith(".ttc"))
                {
                    outputFile += $"{Path.GetFileNameWithoutExtension(fontFile)}.ttf";
                }
                else
                {
                    outputFile += $"{Path.GetFileName(fontFile)}";
                }

                // assign same randomized name for fonts with same family name
                string randomString = "";
                if (rdNameLookUp.ContainsKey(fontName))
                {
                    randomString = rdNameLookUp[fontName];
                }
                else
                {
                    do
                    {
                        randomString = this.RandomString(8);
                    } while (rdNameLookUp.ContainsValue(randomString)); // do while loop to avoid random string collision

                    rdNameLookUp.Add(fontName, randomString);
                }

                var subsetFontInfo = new SubsetFontInfo {
                    FontNameInAss    = fontName,
                    OriginalFontFile = fontFile,
                    SubsetFontFile   = outputFile + $".{randomString}._tmp_",
                    SubsetFontName   = randomString,
                    DumpedXmlFile    = $@"{outputFolder}\{Path.GetFileNameWithoutExtension(outputFile)}.{randomString}.ttx",
                    TrackIndex       = index
                };
                subsetFonts.Add(subsetFontInfo);

                var args = new Dictionary <string, string> {
                    { " ", fontFile },
                    { "--text-file=", charactersFile },
                    { "--output-file=", subsetFontInfo.SubsetFontFile },
                    { "--name-languages=", "*" },
                    { "--font-number=", index.ToString() }
                };

                processors.Add(args);
            }

            string exe = "pyftsubset.exe";

            Parallel.ForEach(processors, args => this.StartProcess(exe, args));
        }
示例#3
0
        private void CreateFontSubset(string fontFolder, string outputFolder, Dictionary <string, string> textsInAss,
                                      Dictionary <string, FontFileInfo> fontFiles, ref List <SubsetFontInfo> subsetFonts)
        {
            var processors = new List <Dictionary <string, string> >();

            foreach (var text in textsInAss)
            {
                var fontName   = text.Key;
                var characters = text.Value;

                // get around unknown bugs in subset fonts
                characters = characters.Replace("…", "……");

                // remove all regular numeric characters and replace them with a full set of them.
                var halfwidth_numerical = new Regex(@"[0-9]");
                if (halfwidth_numerical.IsMatch(characters))
                {
                    characters  = halfwidth_numerical.Replace(characters, "");
                    characters += "0123456789";
                }


                // remove all full width numeric characters and replace them with a full set of them.
                var fullwidth_numerical = new Regex(@"1234567890");
                if (fullwidth_numerical.IsMatch(characters))
                {
                    characters  = fullwidth_numerical.Replace(characters, "");
                    characters += "1234567890";
                }


                var charactersFile = $@"{fontFolder}\{fontName}.txt";
                using (StreamWriter sw = new StreamWriter(charactersFile, false, new UTF8Encoding(false))) {
                    sw.Write(characters);
                }

                int    index    = fontFiles[fontName].FontNumberInCollection;
                string fontFile = fontFiles[fontName].FileName;

                string outputFile = $@"{outputFolder}\";
                if (fontFile.EndsWith(".ttc"))
                {
                    outputFile += $"{Path.GetFileNameWithoutExtension(fontFile)}.ttf";
                }
                else
                {
                    outputFile += $"{Path.GetFileName(fontFile)}";
                }

                var randomString   = this.RandomString(8);
                var subsetFontInfo = new SubsetFontInfo {
                    FontNameInAss    = fontName,
                    OriginalFontFile = fontFile,
                    SubsetFontFile   = outputFile + $".{randomString}._tmp_",
                    SubsetFontName   = randomString,
                    DumpedXmlFile    = $@"{outputFolder}\{Path.GetFileNameWithoutExtension(outputFile)}.{randomString}.ttx",
                    TrackIndex       = index
                };
                subsetFonts.Add(subsetFontInfo);

                var args = new Dictionary <string, string> {
                    { " ", fontFile },
                    { "--text-file=", charactersFile },
                    { "--output-file=", subsetFontInfo.SubsetFontFile },
                    { "--name-languages=", "0x0409" }
                };
                if (index > -1)
                {
                    args.Add("--font-number=", index.ToString());
                }

                processors.Add(args);
            }

            string exe = "pyftsubset.exe";

            Parallel.ForEach(processors, args => this.StartProcess(exe, args));
        }