Skip to content

Commit

Permalink
[lib] Restricted lookup nesting to max sixteen levels
Browse files Browse the repository at this point in the history
  • Loading branch information
mta452 committed Dec 31, 2018
1 parent 68494b5 commit bef583c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
63 changes: 37 additions & 26 deletions Source/GlyphManipulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "GlyphSubstitution.h"
#include "TextProcessor.h"

#define MAX_LOOKUP_NESTING 16

enum {
GlyphZoneInput = 0,
GlyphZoneBacktrack = 1,
Expand Down Expand Up @@ -425,35 +427,44 @@ static SFBoolean ApplyChainRuleTable(TextProcessorRef textProcessor,
static SFBoolean ApplyContextLookups(TextProcessorRef textProcessor,
Data lookupArray, SFUInteger lookupCount, SFUInteger contextStart, SFUInteger contextEnd)
{
LocatorRef locator = &textProcessor->_locator;
LocatorFilter orgFilter = locator->filter;
SFRange orgRange = locator->range;
SFUInteger contextLength = (contextEnd - contextStart) + 1;
SFUInteger lookupIndex;

/* Make the locator cover only context range. */
LocatorReset(locator, contextStart, contextLength);

/* Apply the lookup records sequentially as they are ordered by preference. */
for (lookupIndex = 0; lookupIndex < lookupCount; lookupIndex++) {
Data lookupRecord = LookupArray_Value(lookupArray, lookupIndex);
SFUInt16 sequenceIndex = LookupRecord_SequenceIndex(lookupRecord);
SFUInt16 lookupListIndex = LookupRecord_LookupListIndex(lookupRecord);

/* Jump the locator to context index. */
LocatorJumpTo(locator, contextStart);

/* Skip the glyphs till sequence index. */
if (LocatorSkip(locator, sequenceIndex + 1)) {
/* Apply the specified lookup. */
ApplyLookup(textProcessor, lookupListIndex);
/* Restore the original filter. */
LocatorUpdateFilter(locator, &orgFilter);
/* Increse the nesting level. */
textProcessor->_lookupNesting += 1;

/* Make sure that nesting does not cause infinite recursion. */
if (textProcessor->_lookupNesting < MAX_LOOKUP_NESTING) {
LocatorRef locator = &textProcessor->_locator;
LocatorFilter orgFilter = locator->filter;
SFRange orgRange = locator->range;
SFUInteger contextLength = (contextEnd - contextStart) + 1;
SFUInteger lookupIndex;

/* Make the locator cover only context range. */
LocatorReset(locator, contextStart, contextLength);

/* Apply the lookup records sequentially as they are ordered by preference. */
for (lookupIndex = 0; lookupIndex < lookupCount; lookupIndex++) {
Data lookupRecord = LookupArray_Value(lookupArray, lookupIndex);
SFUInt16 sequenceIndex = LookupRecord_SequenceIndex(lookupRecord);
SFUInt16 lookupListIndex = LookupRecord_LookupListIndex(lookupRecord);

/* Jump the locator to context index. */
LocatorJumpTo(locator, contextStart);

/* Skip the glyphs till sequence index. */
if (LocatorSkip(locator, sequenceIndex + 1)) {
/* Apply the specified lookup. */
ApplyLookup(textProcessor, lookupListIndex);
/* Restore the original filter. */
LocatorUpdateFilter(locator, &orgFilter);
}
}

/* Adjust the locator range. */
LocatorAdjustRange(locator, orgRange.start, orgRange.count + locator->range.count - contextLength);
}

/* Adjust the locator range. */
LocatorAdjustRange(locator, orgRange.start, orgRange.count + locator->range.count - contextLength);
/* Decrese the nesting level. */
textProcessor->_lookupNesting -= 1;

return SFTrue;
}
Expand Down
1 change: 1 addition & 0 deletions Source/TextProcessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ static void ApplyFeatureRange(TextProcessorRef textProcessor, SFFeatureKind feat

lookupType = PrepareLookup(textProcessor, lookupInfo->index, &lookupTable);
textProcessor->_lookupValue = lookupInfo->value;
textProcessor->_lookupNesting = 0;

/* Apply current lookup on all glyphs. */
if (!reversible || lookupType != LookupTypeReverseChainingContext) {
Expand Down
1 change: 1 addition & 0 deletions Source/TextProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct _TextProcessor {
Data _itemVarStore;
Data _lookupList;
SFUInt16 _lookupValue;
SFUInt16 _lookupNesting;
SFBoolean (*_lookupOperation)(struct _TextProcessor *, LookupType, Data);
SFTextDirection _textDirection;
SFUInt16 _ppemWidth;
Expand Down

0 comments on commit bef583c

Please sign in to comment.