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; } }
internal static void GetComplexLanguageList( OpenTypeTags tableTag, FontTable table, uint[] featureTagsList, uint[] glyphBits, ushort minGlyphId, ushort maxGlyphId, out WritingSystem[] complexLanguages, out int complexLanguageCount ) { ScriptList scriptList = new ScriptList(0); FeatureList featureList = new FeatureList(0); LookupList lookupList = new LookupList(0); Debug.Assert(tableTag == OpenTypeTags.GSUB || tableTag == OpenTypeTags.GPOS); switch (tableTag) { case OpenTypeTags.GSUB: GSUBHeader gsubHeader = new GSUBHeader(0); scriptList = gsubHeader.GetScriptList(table); featureList = gsubHeader.GetFeatureList(table); lookupList = gsubHeader.GetLookupList(table); break; case OpenTypeTags.GPOS: GPOSHeader gposHeader = new GPOSHeader(0); scriptList = gposHeader.GetScriptList(table); featureList = gposHeader.GetFeatureList(table); lookupList = gposHeader.GetLookupList(table); break; } int scriptCount = scriptList.GetScriptCount(table); int featureCount = featureList.FeatureCount(table); int lookupCount = lookupList.LookupCount(table); // We will mark lookups that should be tested. // At the end, we will have only complex ones marked uint[] lookupBits = new uint[(lookupCount+31)>>5]; for(int i = 0; i < (lookupCount+31)>>5; i++) { lookupBits[i] = 0; } // Iterate through list of featuers in the table for(ushort featureIndex = 0; featureIndex < featureCount; featureIndex++) { uint featureTag = (uint)featureList.FeatureTag(table, featureIndex); bool tagFound = false; //Search for tag in the list of features in question for(int j = 0; j < featureTagsList.Length; j++) { if (featureTagsList[j] == featureTag) { tagFound = true; break; } } if (tagFound) { // We should mark all lookup mapped to this feature 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 (lookupIndex >= lookupCount) { //This should be invalid font. Lookup associated with the feature is not in lookup array. throw new FileFormatException(); } lookupBits[lookupIndex>>5] |= (uint)(1 << (lookupIndex%32)); } } } // // //Now test all marked lookups for(ushort lookupIndex = 0; lookupIndex < lookupCount; lookupIndex++) { if ((lookupBits[lookupIndex>>5] & (1 << (lookupIndex%32))) == 0) { continue; } LookupTable lookup = lookupList.Lookup(table,lookupIndex); ushort lookupType = lookup.LookupType(); ushort subtableCount = lookup.SubTableCount(); bool lookupIsCovered = false; ushort originalLookupType = lookupType; // We need it to recover, // if extension lookup updated lookupFormat for(ushort subtableIndex = 0; !lookupIsCovered && subtableIndex < subtableCount; subtableIndex++) { lookupType = originalLookupType; int subtableOffset = lookup.SubtableOffset(table, subtableIndex); switch (tableTag) { case OpenTypeTags.GSUB: { if (lookupType == 7) { ExtensionLookupTable extension = new ExtensionLookupTable(subtableOffset); lookupType = extension.LookupType(table); subtableOffset = extension.LookupSubtableOffset(table); } switch (lookupType) { case 1: //SingleSubst SingleSubstitutionSubtable singleSub = new SingleSubstitutionSubtable(subtableOffset); lookupIsCovered = singleSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 2: //MultipleSubst MultipleSubstitutionSubtable multipleSub = new MultipleSubstitutionSubtable(subtableOffset); lookupIsCovered = multipleSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 3: //AlternateSubst AlternateSubstitutionSubtable alternateSub = new AlternateSubstitutionSubtable(subtableOffset); lookupIsCovered = alternateSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 4: //Ligature subst LigatureSubstitutionSubtable ligaSub = new LigatureSubstitutionSubtable(subtableOffset); lookupIsCovered = ligaSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 5: //ContextualSubst ContextSubtable contextSub = new ContextSubtable(subtableOffset); lookupIsCovered = contextSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 6: //ChainingSubst ChainingSubtable chainingSub = new ChainingSubtable(subtableOffset); lookupIsCovered = chainingSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 7: //Extension lookup Debug.Assert(false,"Ext.Lookup processed earlier!"); break; case 8: //ReverseCahiningSubst ReverseChainingSubtable reverseChainingSub = new ReverseChainingSubtable(subtableOffset); lookupIsCovered = reverseChainingSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; default: // Unknown format lookupIsCovered = true; break; } break; } case OpenTypeTags.GPOS: { if (lookupType == 9) { ExtensionLookupTable extension = new ExtensionLookupTable(subtableOffset); lookupType = extension.LookupType(table); subtableOffset = extension.LookupSubtableOffset(table); } switch (lookupType) { case 1: //SinglePos SinglePositioningSubtable singlePos = new SinglePositioningSubtable(subtableOffset); lookupIsCovered = singlePos.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 2: //PairPos PairPositioningSubtable pairPos = new PairPositioningSubtable(subtableOffset); lookupIsCovered = pairPos.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 3: // CursivePos // Under construction CursivePositioningSubtable cursivePositioningSubtable = new CursivePositioningSubtable(subtableOffset); lookupIsCovered = cursivePositioningSubtable.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 4: //MarkToBasePos MarkToBasePositioningSubtable markToBasePos = new MarkToBasePositioningSubtable(subtableOffset); lookupIsCovered = markToBasePos.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 5: //MarkToLigaturePos // Under construction MarkToLigaturePositioningSubtable markToLigaPos = new MarkToLigaturePositioningSubtable(subtableOffset); lookupIsCovered = markToLigaPos.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 6: //MarkToMarkPos MarkToMarkPositioningSubtable markToMarkPos = new MarkToMarkPositioningSubtable(subtableOffset); lookupIsCovered = markToMarkPos.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 7: // Contextual ContextSubtable contextSub = new ContextSubtable(subtableOffset); lookupIsCovered = contextSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 8: // Chaining ChainingSubtable chainingSub = new ChainingSubtable(subtableOffset); lookupIsCovered = chainingSub.IsLookupCovered(table,glyphBits,minGlyphId,maxGlyphId); break; case 9: //Extension lookup Debug.Assert(false,"Ext.Lookup processed earlier!"); break; default: // Unknown format lookupIsCovered = true; break; } break; } default: Debug.Assert(false,"Unknown OpenType layout table!"); break; } } if (!lookupIsCovered) { // Clean the flag lookupBits[lookupIndex>>5] &= ~(uint)(1 << (lookupIndex%32)); } } // Check if we have any lookup left bool complexLookupFound = false; for(int i = 0; i < (lookupCount+31)>>5; i++) { if (lookupBits[i] != 0) { complexLookupFound = true; break; } } if (!complexLookupFound) { // There are no complex lookups complexLanguages = null; complexLanguageCount = 0; return; } // Now go through all langauages and fill the list complexLanguages = new WritingSystem[10]; complexLanguageCount = 0; for(ushort scriptIndex = 0; scriptIndex < scriptCount; scriptIndex++) { ScriptTable scriptTable = scriptList.GetScriptTable(table, scriptIndex); uint scriptTag = scriptList.GetScriptTag(table, scriptIndex); ushort langSysCount = scriptTable.GetLangSysCount(table); if (scriptTable.IsDefaultLangSysExists(table)) { AppendLangSys(scriptTag, (uint)OpenTypeTags.dflt, scriptTable.GetDefaultLangSysTable(table), featureList, table, featureTagsList, lookupBits, ref complexLanguages, ref complexLanguageCount ); } for(ushort langSysIndex = 0; langSysIndex < langSysCount; langSysIndex++) { uint langSysTag = scriptTable.GetLangSysTag(table, langSysIndex); AppendLangSys(scriptTag, langSysTag, scriptTable.GetLangSysTable(table, langSysIndex), featureList, table, featureTagsList, lookupBits, ref complexLanguages, ref complexLanguageCount ); } } }
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); } } }
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); } } } }
public FeatureTable RequiredFeature(FontTable Table, FeatureList Features) { ushort requiredFeatureIndex = Table.GetUShort(offset + offsetRequiredFeature); if (requiredFeatureIndex != 0xFFFF) { return Features.FeatureTable(Table,requiredFeatureIndex); } else { return new FeatureTable(FontTable.InvalidOffset); } }
public FeatureTable FindFeature(FontTable Table, FeatureList Features, uint FeatureTag) { ushort featureCount = FeatureCount(Table); for(ushort i=0;i<featureCount;i++) { ushort featureIndex = GetFeatureIndex(Table,i); if (Features.FeatureTag(Table,featureIndex) == FeatureTag) { return Features.FeatureTable(Table,featureIndex); } } return new FeatureTable(FontTable.InvalidOffset); }
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++; } }