コード例 #1
0
        /// <summary>
        /// Compiles state machine for positioning feature and saves state machine to <paramref name="positioning"/> appendix.
        /// </summary>
        /// <param name="typeface">Glyph typeface in which look for feature.</param>
        /// <param name="scriptId">ID of script in which look for feature.</param>
        /// <param name="languageId">ID of language in which look for feature.</param>
        /// <param name="featureId">ID of feature to look up.</param>
        /// <param name="positioning">Positioning appendix in which will be compiled state machine stored.</param>
        /// <param name="glyphClasses">Glyph classes appendix for use by positioning appendix.</param>
        /// <param name="availableGlyphs">Which glyphs should be restricted in state machine.</param>
        /// <param name="emSize">Requested em size.</param>
        /// <returns>All used glyphs during compilation. Can add additional glyphs to availableGlyphs.</returns>
        public virtual IEnumerable <ushort> CompileFeature(GlyphTypeface typeface, uint scriptId, uint languageId, uint featureId, PositioningAppendix positioning, GlyphClassesAppendix glyphClasses, IEnumerable <ushort> availableGlyphs, double emSize)
        {
            StateMachine machine = GetOrCompile(_gposParser, new PositioningCompiler(), typeface, scriptId, languageId, featureId);

            machine = new StateMachineOptimizer().Optimize(machine);
            machine = new StateMachineNormalizer().Normalize(machine, availableGlyphs);

            this.CompilePositioningAppendixFromMachine(typeface, featureId, positioning, glyphClasses, emSize, machine);

            return(machine.GetGeneratedGlyphIds());
        }
コード例 #2
0
        /// <summary>
        /// Compiles state machine for substitution feature and saves state machine to <paramref name="substitution"/> appendix.
        /// </summary>
        /// <param name="typeface">Glyph typeface in which look for feature.</param>
        /// <param name="scriptId">ID of script in which look for feature.</param>
        /// <param name="languageId">ID of language in which look for feature.</param>
        /// <param name="featureId">ID of feature to look up.</param>
        /// <param name="substitution">Substitution appendix in which will be compiled state machine stored.</param>
        /// <param name="glyphClasses">Glyph classes appendix for use by substitution appendix.</param>
        /// <param name="availableGlyphs">Which glyphs should be restricted in state machine.</param>
        /// <returns>All used glyphs during compilation. Can add additional glyphs to availableGlyphs.</returns>
        public virtual IEnumerable <ushort> CompileFeature(GlyphTypeface typeface, uint scriptId, uint languageId, uint featureId, SubstitutionAppendix substitution, GlyphClassesAppendix glyphClasses, IEnumerable <ushort> availableGlyphs)
        {
            StateMachine machine = GetOrCompile(_gsubParser, new SubstitutionCompiler(), typeface, scriptId, languageId, featureId);

            machine = new StateMachineOptimizer().Optimize(machine);
            machine = new StateMachineNormalizer().Normalize(machine, availableGlyphs);

            this.CompileSubstitutionAppendingFromMachine(featureId, substitution, glyphClasses, machine);

            return(machine.GetGeneratedGlyphIds());
        }
コード例 #3
0
        public void FullProcessOnCalibriiSerbian()
        {
            var allFontPaths = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "*.ttf");
            var typeface     = new GlyphTypeface(new Uri(allFontPaths.Select(p => p.ToLower()).First(p => p.Contains("calibrii"))));

            var gsubParser = new GsubParser();
            var gposParser = new GposParser();

            var script  = new Tag("cyrl");
            var langSys = new Tag("SRB ");

            // Parse the table information from the font
            var substitutionFeatures = gsubParser.GetOptionalFeatureTags(typeface, script, langSys).ToList();
            var positioningFeatures  = gposParser.GetOptionalFeatureTags(typeface, script, langSys).ToList();

            var requiredSubstitutions          = gsubParser.GetTransformationTablesForRequiredFeature(typeface, script, langSys);
            var optionalSubstitutionsByFeature =
                (from chosenOptionalFeatureTag in substitutionFeatures
                 from substitutionTable in gsubParser.GetTransformationTablesForOptionalFeature(typeface, script, langSys, chosenOptionalFeatureTag)
                 select new { Key = chosenOptionalFeatureTag, Value = substitutionTable }).ToLookup(p => p.Key, p => p.Value);
            var allSubstitutions = new [] { requiredSubstitutions }.Append(optionalSubstitutionsByFeature).ToList();

            var requiredPositioning          = gposParser.GetTransformationTablesForRequiredFeature(typeface, script, langSys);
            var optionalPositioningByFeature =
                (from chosenOptionalFeatureTag in positioningFeatures
                 from positioningTable in gposParser.GetTransformationTablesForOptionalFeature(typeface, script, langSys, chosenOptionalFeatureTag)
                 select new { Key = chosenOptionalFeatureTag, Value = positioningTable }).ToLookup(p => p.Key, p => p.Value);
            var allPositioning = new [] { requiredPositioning }.Append(optionalPositioningByFeature).ToList();

            // Compile the parsed tables into state machines
            var gsubCompiler = new SubstitutionCompiler();
            var gposCompiler = new PositioningCompiler();

            var SubstitutionActionMachines = allSubstitutions.Select(gsubCompiler.Compile);
            var positioningStateMachines   = allPositioning.Select(gposCompiler.Compile);
            var allStateMachines           = SubstitutionActionMachines.Append(positioningStateMachines).ToList();

            // Optimize the state machines
            var optimizer = new StateMachineOptimizer();
            var optimizedStateMachines = allStateMachines.Select(optimizer.Optimize).ToList();

            // ..and normalize the machines
            var normalizer            = new StateMachineNormalizer();
            var finishedStateMachines = optimizedStateMachines.Select(normalizer.Normalize).ToList();

            // Sometimes the tables contain empty features (eg. in case of required features).
            var writableStateMachines = finishedStateMachines.Where(p => p.States.Count > 1).ToList();
        }
コード例 #4
0
        /// <summary>
        /// Entry point.
        /// </summary>
        /// <param name="args">The args.</param>
        public static void Main(string[] args)
        {
            OpenTypeCompiler openTypecompiler = new OpenTypeCompiler();
            //openTypecompiler.Positioning(, TagConverter.UintFromTag(new Tag("latn")), TagConverter.UintFromTag(new Tag("dflt")), TagConverter.UintFromTag(new Tag("kern")));
            var allFontPaths = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.Fonts));

            var typeface = new GlyphTypeface(new Uri(allFontPaths.Select(p => p.ToLower()).First(p => p.Contains("pala"))));
            //var typeface = new GlyphTypeface(new Uri(allFontPaths.First(p => p.Contains("arial"))));

            var gdefParser = new GdefParser();
            var ignoreBase = gdefParser.GetGlyphIdsByLookupFlags(typeface, LookupFlags.IgnoreBaseGlyphs);

            var cmapParser = new CmapParser();

            /*Dictionary<short, char> reverse = new Dictionary<short,char>();
             * foreach (var pair in cmapParser.GetCharacterToGlyphIdMapping(typeface))
             * {
             *  reverse[pair.Value] = pair.Key;
             * }
             * var ad = gdefParser.GetGlyphClasses(typeface);
             * var ac = gdefParser.GetGlyphClasses(typeface).Where(a => a.Value != 0);
             * var ab = gdefParser.GetGlyphClasses(typeface).Where(a => a.Value != 0 && reverse.ContainsKey(a.Key)).ToDictionary(a => reverse[a.Key], a => a.Value);
             *
             * var writer = new StreamWriter("glyphs.txt");
             * foreach (var glyphClass in ab)
             * {
             *  writer.WriteLine(glyphClass.Key + " => " + glyphClass.Value);
             * }
             * writer.Close();*/


            var metadata = new GlyphMetadata
            {
                CharacterToGlyphIdMapping         = cmapParser.GetCharacterToGlyphIdMapping(typeface).GetValueOrDefault,
                GlyphIdToGlyphClassMapping        = gdefParser.GetGlyphClasses(typeface).GetValueOrDefault,
                GlyphIdToMarkAttachClassIdMapping = gdefParser.GetMarkAttachClassIds(typeface).GetValueOrDefault,
            };

            var gsubParser = new GsubParser();

            var scripts = gsubParser.GetScriptTags(typeface).ToList();

            Console.WriteLine("SCRIPTS: ");
            foreach (var tag in scripts)
            {
                Console.WriteLine("\t" + tag.Label);
            }

            var script1 = new Tag("latn");

            var langSyss = gsubParser.GetLangSysTags(typeface, script1).ToList();

            Console.WriteLine("LANGUAGE SYSTEMS: ");
            foreach (var tag in langSyss)
            {
                Console.WriteLine("\t" + tag.Label);
            }

            //var langSys = new Tag("LTH ");
            var langSys = new Tag("dflt");

            var features = gsubParser.GetOptionalFeatureTags(typeface, script1, langSys).ToList();

            Console.WriteLine("OPTIONAL FEATURES: ");
            foreach (var tag in features)
            {
                Console.WriteLine("\t" + tag.Label);
            }

            //gsubParser.Dump(typeface);

            //// var enabledOptionalFeatures = parser.GetOptionalFeatureTags(typeface, script, langSys).Skip(5).Take(5).ToList();
            //var chosenOptionalFeatureTags = new[] { new Tag("liga") };
            var chosenOptionalFeatureTags = new[] { new Tag("frac") };

            var requiredTransformations          = gsubParser.GetTransformationTablesForRequiredFeature(typeface, script1, langSys);
            var optionalTransformationsByFeature =
                (from chosenOptionalFeatureTag in chosenOptionalFeatureTags
                 from substitutionTable in gsubParser.GetTransformationTablesForOptionalFeature(typeface, script1, langSys, chosenOptionalFeatureTag)
                 select new { Key = chosenOptionalFeatureTag, Value = substitutionTable }).ToLookup(p => p.Key, p => p.Value);

            var compiler = new SubstitutionCompiler();
            var SubstitutionActionMachine = compiler.Compile(requiredTransformations);

            var optionalStateMachinesByFeature = optionalTransformationsByFeature.ToDictionary(p => p.Key, compiler.Compile);

            var machine = optionalStateMachinesByFeature.First().Value;

            var gposParser = new GposParser();

            gposParser.Dump(typeface);
            scripts = gposParser.GetScriptTags(typeface).ToList();

            Console.WriteLine("SCRIPTS: ");
            foreach (var tag in scripts)
            {
                Console.WriteLine("\t" + tag.Label);
                langSyss = gposParser.GetLangSysTags(typeface, script1).ToList();

                foreach (var y in langSyss)
                {
                    Console.WriteLine("\t\t" + y.Label);
                }
            }

            script1 = new Tag("cyrl");

            langSyss = gposParser.GetLangSysTags(typeface, script1).ToList();

            Console.WriteLine("LANGUAGE SYSTEMS: ");
            foreach (var tag in langSyss)
            {
                Console.WriteLine("\t" + tag.Label);
            }

            Console.WriteLine("OPTIONAL FEATURES: ");
            foreach (var tag in features)
            {
                Console.WriteLine("\t" + tag.Label);
            }

            langSys = new Tag("SRB ");

            chosenOptionalFeatureTags = features.ToArray();// new[] { new Tag("kern") };

            gposParser.Dump(typeface);
            var gposCompiler = new PositioningCompiler();

            var positioningFeatures = gposParser.GetOptionalFeatureTags(typeface, script1, langSys).ToList();

            var requiredPositioning         = gposParser.GetTransformationTablesForRequiredFeature(typeface, script1, langSys);
            var compiledRequiredPositioning = gposCompiler.Compile(requiredPositioning);


            var optionalPositioningByFeature =
                (from chosenOptionalFeatureTag in positioningFeatures
                 from positioningTable in gposParser.GetTransformationTablesForOptionalFeature(typeface, script1, langSys, chosenOptionalFeatureTag)
                 select new { Key = chosenOptionalFeatureTag, Value = positioningTable }).ToLookup(p => p.Key, p => p.Value);



            var compiledOptionalPositioning =
                (from positioningTables in optionalPositioningByFeature
                 select new { Value = gposCompiler.Compile(positioningTables), positioningTables.Key }).ToLookup(p => p.Key, p => p.Value);

            var machineToOptimize = compiledOptionalPositioning[new Tag("kern")].Single();

            //var normalizedMachine = normalizer.Normalize(machineToOptimize);

            var optimizer        = new StateMachineOptimizer();
            var optimizedMachine = optimizer.Optimize(machineToOptimize);

            var normalizer = new StateMachineNormalizer();
            var optimizedNormalizedMachíne = normalizer.Normalize(optimizedMachine);
        }