Skip to content

Commit

Permalink
Methods to find source index from location
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Nov 25, 2024
1 parent 4e4f402 commit 3b5204e
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/fontra/client/core/glyph-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { addItemwise } from "./var-funcs.js";
import { StaticGlyph } from "./var-glyph.js";
import {
locationToString,
makeSparseLocation,
makeSparseNormalizedLocation,
mapAxesFromUserSpaceToSourceSpace,
normalizeLocation,
Expand All @@ -54,6 +55,7 @@ export class VariableGlyphController {
this._layerGlyphControllers = {};
this._layerNameToSourceIndex = {};
this._sourceIndexToBackgroundLayerNames = new Map();
this._locationStringToSourceIndex = null;
}

get name() {
Expand Down Expand Up @@ -192,6 +194,7 @@ export class VariableGlyphController {
this._layerGlyphControllers = {};
this._layerNameToSourceIndex = {};
this._sourceIndexToBackgroundLayerNames = new Map();
this._locationStringToSourceIndex = null;
}

get model() {
Expand Down Expand Up @@ -526,6 +529,32 @@ export class VariableGlyphController {
return backgroundLayerNames;
}

getSourceIndexForSourceLocation(sourceLocation) {
if (!this._locationStringToSourceIndex) {
this._buildLocationStringToSourceIndexMapping();
}
return this.getSourceIndexForSourceLocationString(
this.getSparseLocationStringForSourceLocation(sourceLocation)
);
}

getSourceIndexForSourceLocationString(sourceLocationString) {
return this._locationStringToSourceIndex[sourceLocationString];
}

_buildLocationStringToSourceIndexMapping() {
this._locationStringToSourceIndex = {};
for (const [sourceIndex, source] of enumerate(this.sources)) {
this._locationStringToSourceIndex[
this.getSparseLocationStringForSourceLocation(source.location)
] = sourceIndex;
}
}

getSparseLocationStringForSourceLocation(sourceLocation) {
return locationToString(makeSparseLocation(sourceLocation, this.combinedAxes));
}

expandNLIAxes(sourceLocation) {
return mapLocationExpandNLI(sourceLocation, this.axes);
}
Expand Down

0 comments on commit 3b5204e

Please sign in to comment.