private static void GetNextEnabledGlyphRange( Feature[] FeatureSet, // In: List of features to apply int featureCount, // In: Actual nubmer of features in FeatureSet int featureSetOffset, // In: offset of input chars inside feature set FontTable Table, // Layout table (GSUB or GPOS) OpenTypeLayoutWorkspace workspace, // workspace with compiled feature set LangSysTable LangSys, // Language system FeatureList Features, // List of Features in layout table ushort lookupIndex, // List of lokups definitions in layout table int CharCount, // Characters count (i.e. Charmap.Length); UshortList Charmap, // Character to glyph mapping int StartChar, int StartGlyph, int GlyphRunLength, out int FirstChar, // First char in enabled range out int AfterLastChar, // next char after enabled range out int FirstGlyph, // First char in enabled range out int AfterLastGlyph, // next char after enabled range out uint Parameter // applied feature parameter ) { FirstChar = int.MaxValue; AfterLastChar = int.MaxValue; FirstGlyph = StartGlyph; AfterLastGlyph = GlyphRunLength; Parameter = 0; if (workspace.IsRequiredFeatureFlagSet(lookupIndex)) { FirstChar = StartChar; AfterLastChar = CharCount; FirstGlyph = StartGlyph; AfterLastGlyph = GlyphRunLength; return; } for(int feature=0; feature < featureCount; feature++) { if (!workspace.IsFeatureFlagSet(lookupIndex,feature)) { continue; } Feature featureDescription = FeatureSet[feature]; // Shift values from the feature by specified offset and // work with these values from here int featureStart = featureDescription.StartIndex - featureSetOffset; if (featureStart < 0) { featureStart = 0; } int featureAfterEnd = featureDescription.StartIndex + featureDescription.Length - featureSetOffset; if (featureAfterEnd > CharCount) { featureAfterEnd = CharCount; } //If feature is disabled there should not be any flag set Debug.Assert(featureDescription.Parameter != 0); if (featureAfterEnd <= StartChar) { continue; } if (featureStart < FirstChar || ( featureStart == FirstChar && featureAfterEnd >= AfterLastChar ) ) { FirstChar = featureStart; AfterLastChar = featureAfterEnd; Parameter = featureDescription.Parameter; continue; } } //No ranges found if (FirstChar == int.MaxValue) { FirstGlyph = GlyphRunLength; AfterLastGlyph = GlyphRunLength; } else { if (StartGlyph > Charmap[FirstChar]) FirstGlyph = StartGlyph; else FirstGlyph = Charmap[FirstChar]; if (AfterLastChar < CharCount) AfterLastGlyph = Charmap[AfterLastChar]; else AfterLastGlyph = GlyphRunLength; } }
public static void ApplyFeatures( IOpenTypeFont Font, OpenTypeLayoutWorkspace workspace, OpenTypeTags TableTag, FontTable Table, LayoutMetrics Metrics, LangSysTable LangSys, FeatureList Features, LookupList Lookups, Feature[] FeatureSet, int featureCount, int featureSetOffset, int CharCount, UshortList Charmap, GlyphInfoList GlyphInfo, int* Advances, LayoutOffset* Offsets ) { UpdateGlyphFlags(Font, GlyphInfo, 0, GlyphInfo.Length, false, GlyphFlags.NotChanged); // if client did not supply us with workspace // we will create our own (temporarily) if (workspace == null) { workspace = new OpenTypeLayoutWorkspace(); } ushort lookupCount=Lookups.LookupCount(Table); //Compile feature set CompileFeatureSet( FeatureSet, featureCount, featureSetOffset, CharCount, Table, LangSys, Features, lookupCount, workspace ); OpenTypeLayoutCache.InitCache(Font, TableTag, GlyphInfo, workspace); for(ushort lookupIndex = 0; lookupIndex < lookupCount; lookupIndex++) { if (!workspace.IsAggregatedFlagSet(lookupIndex)) { continue; } int firstChar=0, afterLastChar=0, firstGlyph=0, afterLastGlyph=0; OpenTypeLayoutCache.FindNextLookup(workspace, GlyphInfo, lookupIndex, out lookupIndex, out firstGlyph); // We need to check this again, because FindNextLookup will change lookupIndex if (lookupIndex >= lookupCount) { break; } if (!workspace.IsAggregatedFlagSet(lookupIndex)) { continue; } LookupTable lookup = Lookups.Lookup(Table, lookupIndex); uint parameter=0; bool isLookupReversal = IsLookupReversal(TableTag, lookup.LookupType()); while(firstGlyph < GlyphInfo.Length) // While we have ranges to work on { if (!OpenTypeLayoutCache.FindNextGlyphInLookup(workspace, lookupIndex, isLookupReversal, ref firstGlyph, ref afterLastGlyph)) { firstGlyph = afterLastGlyph; } if (firstGlyph < afterLastGlyph) // Apply lookup while in one range { int nextGlyph; int oldLength = GlyphInfo.Length; int glyphsAfterLastChar = oldLength - afterLastGlyph; bool match = ApplyLookup( Font, // In: Font access interface TableTag, // Layout table tag (GSUB or GPOS) Table, // Layout table (GSUB or GPOS) Metrics, // In: LayoutMetrics lookup, // Lookup definition structure CharCount, Charmap, // In: Char to glyph mapping GlyphInfo, // In/out: List of GlyphInfo structs Advances, // In/out: Glyph adv.widths Offsets, // In/out: Glyph offsets firstGlyph, // where to apply it afterLastGlyph, // how long is a context we can use parameter, // lookup parameter 0, // Nesting level for contextual lookups out nextGlyph // out: next glyph index // !!!: for reversal lookup, should // return new afterLastGlyph ); if (match) { //Adjust range end if length changed, // for reversal changes happens beyond afterLast, no change needed if (!isLookupReversal) { OpenTypeLayoutCache.OnGlyphsChanged(workspace, GlyphInfo, oldLength, firstGlyph, nextGlyph); afterLastGlyph = GlyphInfo.Length - glyphsAfterLastChar; firstGlyph = nextGlyph; } else { OpenTypeLayoutCache.OnGlyphsChanged(workspace, GlyphInfo, oldLength, nextGlyph, afterLastGlyph); afterLastGlyph = nextGlyph; } } else { if (isLookupReversal) afterLastGlyph = nextGlyph; else firstGlyph = nextGlyph; } } else // End of range. Get next { GetNextEnabledGlyphRange( FeatureSet, featureCount, featureSetOffset, Table, workspace, LangSys, Features, lookupIndex, CharCount, Charmap, afterLastChar, afterLastGlyph, GlyphInfo.Length, out firstChar, out afterLastChar, out firstGlyph, out afterLastGlyph, out parameter); } } } }
private static void CompileFeatureSet( Feature[] FeatureSet, // In: List of features to apply int featureCount, // In: Actual number of features in FeatureSet int featureSetOffset, //In: Offset of character input sequence inside feature set int charCount, // In: number of characters in the input string FontTable Table, // In: Layout table (GSUB or GPOS) LangSysTable LangSys, // In: Language system FeatureList Features, // In: List of Features in layout table int lookupCount, // In: number of lookup in layout table OpenTypeLayoutWorkspace workspace // In: workspace with compiled feature set ) { workspace.InitLookupUsageFlags(lookupCount, featureCount); //Set lookup uasge flags for required feature FeatureTable requiredFeatureTable = LangSys.RequiredFeature(Table, Features); if (!requiredFeatureTable.IsNull) { int featureLookupCount = requiredFeatureTable.LookupCount(Table); for(ushort lookup = 0; lookup < featureLookupCount; lookup++) { workspace.SetRequiredFeatureFlag(requiredFeatureTable.LookupIndex(Table,lookup)); } } //Set lookup usage flags for each feature in the FeatureSet for(int feature = 0; feature < featureCount; feature++) { Feature featureDescription = FeatureSet[feature]; //Filter out features which: // Not enabled or applied completely before or after input characters if (featureDescription.Parameter == 0 || featureDescription.StartIndex >= (featureSetOffset + charCount) || (featureDescription.StartIndex+featureDescription.Length) <= featureSetOffset ) { continue; } FeatureTable featureTable = LangSys.FindFeature( Table, Features, featureDescription.Tag); if (featureTable.IsNull) { continue; } int featureLookupCount = featureTable.LookupCount(Table); for(ushort lookup = 0; lookup < featureLookupCount; lookup++) { workspace.SetFeatureFlag(featureTable.LookupIndex(Table,lookup), feature); } } }
private static void AppendLangSys( uint scriptTag, uint langSysTag, LangSysTable langSysTable, FeatureList featureList, FontTable table, uint[] featureTagsList, uint[] lookupBits, ref WritingSystem[] complexLanguages, ref int complexLanguageCount ) { ushort featureCount = langSysTable.FeatureCount(table); bool complexFeatureFound = false; // for(ushort i = 0; !complexFeatureFound && i < featureCount; i++) { ushort featureIndex = langSysTable.GetFeatureIndex(table, i); uint featureTag = featureList.FeatureTag(table,featureIndex); bool tagFound = false; for(int j = 0; !complexFeatureFound && j < featureTagsList.Length; j++) { if (featureTagsList[j] == featureTag) { tagFound = true; break; } } if (tagFound) { // We should check if any of lookups is complex FeatureTable featureTable = featureList.FeatureTable(table, featureIndex); ushort featureLookupCount = featureTable.LookupCount(table); for(ushort j = 0; j < featureLookupCount; j++) { ushort lookupIndex = featureTable.LookupIndex(table, j); if ((lookupBits[lookupIndex>>5] & (1 << (lookupIndex%32))) != 0) { complexFeatureFound = true; break; } } } } if (complexFeatureFound) { if (complexLanguages.Length == complexLanguageCount) { WritingSystem[] newComplexLanguages = new WritingSystem[complexLanguages.Length * 3 /2]; for(int i = 0; i < complexLanguages.Length; i++) { newComplexLanguages[i] = complexLanguages[i]; } complexLanguages = newComplexLanguages; } complexLanguages[complexLanguageCount].scriptTag = scriptTag; complexLanguages[complexLanguageCount].langSysTag = langSysTag; complexLanguageCount++; } }