Skip to content

Commit

Permalink
Add 'colorized' versions for the getBackgroundImageXxx methods; use t…
Browse files Browse the repository at this point in the history
…his is the vis layer for bg images
  • Loading branch information
justvanrossum committed Nov 19, 2024
1 parent c858ac1 commit c2a3b3c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/fontra/client/core/font-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TaskPool } from "./task-pool.js";
import {
assert,
chain,
colorizeImage,
getCharFromCodePoint,
mapObjectValues,
throttleCalls,
Expand Down Expand Up @@ -136,6 +137,30 @@ export class FontController {
return cacheEntry.imagePromise;
}

getBackgroundImageColorized(imageIdentifier, color) {
if (!color) {
return this.getBackgroundImage(imageIdentifier);
}
const cacheEntry = this._getBackgroundImageCacheEntry(imageIdentifier);
if (cacheEntry.color !== color) {
delete cacheEntry.imageColorized;
cacheEntry.color = color;
cacheEntry.imageColorizedPromise = new Promise((resolve, reject) => {
cacheEntry.imagePromise.then((image) => {
if (image) {
colorizeImage(image, color).then((image) => {
cacheEntry.imageColorized = image;
resolve(image);
});
} else {
resolve(null);
}
});
});
}
return cacheEntry.imageColorizedPromise;
}

_getBackgroundImageCacheEntry(imageIdentifier) {
// This returns a promise for the requested background image
let cacheEntry = this._backgroundImageCache.get(imageIdentifier);
Expand All @@ -161,6 +186,19 @@ export class FontController {
return cacheEntry?.image;
}

getBackgroundImageColorizedCached(imageIdentifier, color, onLoad = null) {
if (!color) {
return this.getBackgroundImageCached(imageIdentifier, onLoad);
}
const cacheEntry = this._backgroundImageCache.get(imageIdentifier);
if ((!cacheEntry?.imageColorizedPromise || cacheEntry.color !== color) && onLoad) {
this.getBackgroundImageColorized(imageIdentifier, color).then((image) =>
onLoad(image)
);
}
return cacheEntry?.imageColorized;
}

_cacheBackgroundImageFromIdentifier(imageIdentifier) {
return this._cacheBackgroundImageFromDataURLPromise(
imageIdentifier,
Expand Down
9 changes: 8 additions & 1 deletion src/fontra/views/editor/visualization-layer-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,15 @@ registerVisualizationLayerDefinition({
return;
}

const image = model.fontController.getBackgroundImageCached(
const image = model.fontController.getBackgroundImageColorizedCached(
backgroundImage.identifier,
backgroundImage.color
? rgbaToCSS([
backgroundImage.color.red,
backgroundImage.color.green,
backgroundImage.color.blue,
])
: null,
() => controller.requestUpdate()
);

Expand Down

0 comments on commit c2a3b3c

Please sign in to comment.