Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

TextElementsRenderer supports Font and TextStyle updates #2008

Merged
merged 7 commits into from
Dec 14, 2020
Merged
5 changes: 1 addition & 4 deletions @here/harp-mapview/lib/MapView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3853,10 +3853,7 @@ export class MapView extends EventDispatcher {
textStyles?: TextStyleDefinition[],
defaultTextStyle?: TextStyleDefinition
): Promise<void> {
await this.m_textElementsRenderer.updateFontCatalogs(
fontCatalogs,
!this.m_options.fontCatalog
);
await this.m_textElementsRenderer.updateFontCatalogs(fontCatalogs);
await this.m_textElementsRenderer.updateTextStyles(textStyles, defaultTextStyle);
this.update();
}
Expand Down
33 changes: 23 additions & 10 deletions @here/harp-mapview/lib/text/TextElementsRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,28 @@ export class TextElementsRenderer {
* Updates the FontCatalogs used by this {@link TextElementsRenderer}.
*
* @param fontCatalogs - The new list of {@link FontCatalogConfig}s
* @param overrideDefault - Optional, if `true` removes default catalog @default `false`.
*/
async updateFontCatalogs(fontCatalogs?: FontCatalogConfig[], overrideDefault: boolean = false) {
if (this.m_defaultFontCatalogConfig && !overrideDefault) {
// Never remove the default Canvas if set per configuration
if (!fontCatalogs) {
fontCatalogs = [];
async updateFontCatalogs(fontCatalogs?: FontCatalogConfig[]) {
if (this.m_defaultFontCatalogConfig) {
if (
!fontCatalogs ||
fontCatalogs.findIndex(config => {
return config.name === DEFAULT_FONT_CATALOG_NAME;
}) === -1
) {
// not other default catalog available, keep the old one
if (!fontCatalogs) {
fontCatalogs = [];
}
// Never remove the default Canvas if set per configuration
fontCatalogs?.unshift(this.m_defaultFontCatalogConfig);
FraukeF marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (this.m_textCanvases.has(DEFAULT_FONT_CATALOG_NAME)) {
this.m_textCanvases.delete(DEFAULT_FONT_CATALOG_NAME);
}
}
fontCatalogs?.unshift(this.m_defaultFontCatalogConfig);
}

if (fontCatalogs && fontCatalogs.length > 0) {
// Remove obsolete ones
for (const [name] of this.m_textCanvases) {
Expand All @@ -508,9 +520,7 @@ export class TextElementsRenderer {
} else {
this.m_textCanvases.clear();
}
if (this.hasDefaultTextCanvas()) {
this.m_textStyleCache.initializeTextElementStyles(this.m_textCanvases);
}
this.initializeTextElementStyles();
}

async updateTextStyles(
Expand All @@ -520,6 +530,9 @@ export class TextElementsRenderer {
// TODO: this is an intermeditate solution, in the end this
// should not create a new cache, but update the former one
this.m_textStyleCache = new TextStyleCache(textStyles, defaultTextStyle);
if (defaultTextStyle !== undefined) {
await this.addDefaultTextCanvas();
}
this.initializeTextElementStyles();
await this.waitLoaded();
}
Expand Down
4 changes: 4 additions & 0 deletions @here/harp-mapview/lib/text/TextStyleCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ export class TextStyleCache {
if (style.fontCatalog !== undefined) {
const styledTextCanvas = textCanvases.get(style.fontCatalog);
style.textCanvas = styledTextCanvas;
if (textCanvases.has(style.fontCatalog) && !styledTextCanvas) {
logger.info(`fontCatalog(${style.fontCatalog}), not yet loaded`);
return;
}
}
if (style.textCanvas === undefined) {
if (style.fontCatalog !== undefined) {
Expand Down
17 changes: 7 additions & 10 deletions @here/harp-mapview/test/TextElementsRendererTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,18 +931,15 @@ describe("TextElementsRenderer", function() {
"adds catalog2 back, removed in last step"
).to.equal(5);

await fixture.textRenderer.updateFontCatalogs([], true);
await fixture.textRenderer.updateFontCatalogs([]);
expect(fixture.loadCatalogStub.callCount).to.equal(5);

await fixture.textRenderer.updateFontCatalogs(
[
{
name: DEFAULT_FONT_CATALOG_NAME,
url: "some-url"
}
],
true
);
await fixture.textRenderer.updateFontCatalogs([
{
name: DEFAULT_FONT_CATALOG_NAME,
url: "some-url"
}
]);
expect(fixture.loadCatalogStub.callCount).to.equal(6);
});
});