Skip to content

Commit

Permalink
Merge pull request #1855 from googlefonts/issue-1850-refactor-sortedS…
Browse files Browse the repository at this point in the history
…ourceIdentifiers

Move sortedSourceIdentifiers to FontController
  • Loading branch information
ollimeier authored Dec 15, 2024
2 parents ab770ca + fb3c9dd commit dba985b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
23 changes: 22 additions & 1 deletion src/fontra/client/core/font-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import {
uniqueID,
} from "./utils.js";
import { StaticGlyph, VariableGlyph } from "./var-glyph.js";
import { locationToString, mapBackward, mapForward } from "./var-model.js";
import {
locationToString,
mapAxesFromUserSpaceToSourceSpace,
mapBackward,
mapForward,
} from "./var-model.js";

const GLYPH_CACHE_SIZE = 2000;
const BACKGROUND_IMAGE_CACHE_SIZE = 100;
Expand Down Expand Up @@ -132,6 +137,22 @@ export class FontController {
return this._rootObject.sources;
}

async getSortedSourceIdentifiers() {
const fontAxesSourceSpace = mapAxesFromUserSpaceToSourceSpace(this.fontAxes);
const sortFunc = (identifierA, identifierB) => {
for (const axis of fontAxesSourceSpace) {
const valueA = this.sources[identifierA].location[axis.name];
const valueB = this.sources[identifierB].location[axis.name];
if (valueA === valueB) {
continue;
}
return valueA < valueB ? -1 : 0;
}
return 0;
};
return Object.keys(this.sources).sort(sortFunc);
}

getBackgroundImage(imageIdentifier) {
// This returns a promise for the requested background image
const cacheEntry = this._getBackgroundImageCacheEntry(imageIdentifier);
Expand Down
20 changes: 1 addition & 19 deletions src/fontra/views/fontinfo/panel-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ export class SourcesPanel extends BaseInfoPanel {
style: "display: grid; gap: 0.5em;",
});

for (const identifier of sortedSourceIdentifiers(
sources,
this.fontAxesSourceSpace
)) {
for (const identifier of await this.fontController.getSortedSourceIdentifiers()) {
if (!cardsInfos[identifier]) {
cardsInfos[identifier] = {};
}
Expand Down Expand Up @@ -620,21 +617,6 @@ class SourceBox extends HTMLElement {

customElements.define("source-box", SourceBox);

function sortedSourceIdentifiers(sources, fontAxes) {
const sortFunc = (identifierA, identifierB) => {
for (const axis of fontAxes) {
const valueA = sources[identifierA].location[axis.name];
const valueB = sources[identifierB].location[axis.name];
if (valueA === valueB) {
continue;
}
return valueA < valueB ? -1 : 0;
}
return 0;
};
return Object.keys(sources).sort(sortFunc);
}

function buildElement(controller) {
let items = [];
for (const key in controller.model) {
Expand Down

0 comments on commit dba985b

Please sign in to comment.