Skip to content

Commit

Permalink
Ligatures: Refactor the walk.ts functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sedwards2009 committed Jan 19, 2020
1 parent 715e38d commit d92928e
Showing 1 changed file with 30 additions and 43 deletions.
73 changes: 30 additions & 43 deletions packages/extraterm-font-ligatures/src/walk.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,50 @@
import { FlattenedLookupTree, LookupResult } from './types';

export default function walkTree(tree: FlattenedLookupTree, sequence: number[], startIndex: number, index: number): LookupResult | undefined {
const glyphId = sequence[index];
let subtree = tree.get(glyphId);
if (!subtree) {
return undefined;
}

let lookup = subtree.lookup;
if (subtree.reverse) {
const reverseLookup = walkReverse(subtree.reverse, sequence, startIndex);
let bestLookup: LookupResult = undefined;

if (
(!lookup && reverseLookup) ||
(
reverseLookup && lookup && (
lookup.index > reverseLookup.index ||
(lookup.index === reverseLookup.index && lookup.subIndex > reverseLookup.subIndex)
)
)
) {
lookup = reverseLookup;
while (true) {
const subtree = tree.get(sequence[index]);
if (!subtree) {
return bestLookup;
}

bestLookup = chooseBestLookup(bestLookup, subtree.lookup);
if (subtree.reverse) {
const reverseLookup = walkReverse(subtree.reverse, sequence, startIndex);
bestLookup = chooseBestLookup(bestLookup, reverseLookup);
}

index++;
if (index >= sequence.length || !subtree.forward) {
return bestLookup;
}
tree = subtree.forward;
}
}

if (++index >= sequence.length || !subtree.forward) {
return lookup;
function chooseBestLookup(aLookup: LookupResult, bLookup: LookupResult): LookupResult {
if (bLookup == null) {
return aLookup;
}

const forwardLookup = walkTree(subtree.forward, sequence, startIndex, index);

if (
(!lookup && forwardLookup) ||
(
forwardLookup && lookup && (
lookup.index > forwardLookup.index ||
(lookup.index === forwardLookup.index && lookup.subIndex > forwardLookup.subIndex)
)
)
) {
lookup = forwardLookup;
if (aLookup == null) {
return bLookup;
}

return lookup;
if ((aLookup.index > bLookup.index) || (aLookup.index === bLookup.index && aLookup.subIndex > bLookup.subIndex)) {
return bLookup;
}
return aLookup;
}

function walkReverse(tree: FlattenedLookupTree, sequence: number[], index: number): LookupResult | undefined {
--index;
let subtree = tree.get(sequence[index]);
let lookup: LookupResult | undefined = subtree && subtree.lookup;
while (subtree) {
if (
(!lookup && subtree.lookup) ||
(subtree.lookup && lookup && lookup.index > subtree.lookup.index)
) {
lookup = subtree.lookup;
}
lookup = chooseBestLookup(lookup, subtree.lookup);

if (--index < 0 || !subtree.reverse) {
--index;
if (index < 0 || !subtree.reverse) {
break;
}

Expand Down

0 comments on commit d92928e

Please sign in to comment.