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

Commit

Permalink
HARP-11679: Hide replacement glyphs in prodction
Browse files Browse the repository at this point in the history
Signed-off-by: stefan.dachwitz <stefan.dachwitz@here.com>
Signed-off-by: StefanDachwitz <stefan.dachwitz@here.com>
  • Loading branch information
StefanDachwitz committed Sep 30, 2020
1 parent c6944bb commit 6157fa0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
22 changes: 21 additions & 1 deletion @here/harp-mapview/lib/text/TextElementsRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,24 @@ export class TextElementsRenderer {
this.m_options.delayLabelsUntilMovementFinished = delay;
}

/**
* If `true`, a replacement glyph ("?") is rendered for every missing glyph.
*/
get showReplacementGlyphs() {
return this.m_options.showReplacementGlyphs === true;
}

/**
* If `true`, a replacement glyph ("?") is rendered for every missing glyph.
*/
set showReplacementGlyphs(value: boolean) {
this.m_options.showReplacementGlyphs = value;

this.m_textRenderers.forEach(textRenderer => {
textRenderer.textCanvas.fontCatalog.showReplacementGlyphs = value;
});
}

/**
* Render the text using the specified camera into the current canvas.
*
Expand Down Expand Up @@ -901,7 +919,7 @@ export class TextElementsRenderer {
): boolean {
// Trigger the glyph load if needed.
if (textElement.loadingState === LoadingState.Initialized) {
return true;
return textElement.glyphs !== undefined;
}

assert(textElementStyle.textCanvas !== undefined);
Expand Down Expand Up @@ -978,6 +996,8 @@ export class TextElementsRenderer {
const catalogCallback = (name: string, catalog: FontCatalog) => {
const loadedTextCanvas = this.m_textCanvasFactory.createTextCanvas(catalog);

catalog.showReplacementGlyphs = this.showReplacementGlyphs;

this.m_textRenderers.push({
fontCatalog: name,
textCanvas: loadedTextCanvas,
Expand Down
10 changes: 10 additions & 0 deletions @here/harp-mapview/lib/text/TextElementsRendererOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export interface TextElementsRendererOptions {
* @default `true`
*/
delayLabelsUntilMovementFinished?: boolean;

/**
* If `true`, a replacement glyph ("?") is rendered for every missing glyph.
* @default `false`
*/
showReplacementGlyphs?: boolean;
}

/**
Expand Down Expand Up @@ -151,4 +157,8 @@ export function initializeDefaultOptions(options: TextElementsRendererOptions) {
if (options.delayLabelsUntilMovementFinished === undefined) {
options.delayLabelsUntilMovementFinished = true;
}

if (options.showReplacementGlyphs === undefined) {
options.showReplacementGlyphs = false;
}
}
5 changes: 2 additions & 3 deletions @here/harp-mapview/test/stubFontCatalogLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ export function stubFontCatalogLoader(
sandbox.stub(fontCatalogLoaderStub, "loading").get(() => {
return false;
});
fontCatalogLoaderStub.loadCatalogs
.yields([DEFAULT_FONT_CATALOG_NAME, fontCatalog])
.resolves([]);

fontCatalogLoaderStub.loadCatalogs.yields(DEFAULT_FONT_CATALOG_NAME, fontCatalog).resolves([]);

return (fontCatalogLoaderStub as unknown) as FontCatalogLoader;
}
11 changes: 9 additions & 2 deletions @here/harp-text-canvas/lib/rendering/FontCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export class FontCatalog {
1.0,
1.0,
replacementTexture,
replacementFont!
replacementFont!,
true
);

const fontCatalogInfo = new FontCatalog(
Expand Down Expand Up @@ -161,6 +162,9 @@ export class FontCatalog {
private readonly m_loadedPages: Map<string, THREE.Texture>;
private readonly m_loadedGlyphs: Map<string, Map<number, GlyphData>>;

/** If `true`, a replacement glyph is returned for every missing glyph. */
public showReplacementGlyphs = false;

/**
* @hidden
* Creates a new FontCatalog.
Expand Down Expand Up @@ -462,7 +466,10 @@ export class FontCatalog {
const codePoint = char.codePointAt(0)!;
const font = this.getFont(codePoint, fontName);
const glyphData = this.getGlyph(codePoint, font, fontStyle);
if (glyphData !== undefined) {
if (
glyphData !== undefined &&
(!glyphData.isReplacement || this.showReplacementGlyphs)
) {
result.push(glyphData);
if (letterCaseArray !== undefined) {
letterCaseArray.push(char !== character);
Expand Down
7 changes: 5 additions & 2 deletions @here/harp-text-canvas/lib/rendering/GlyphData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class GlyphData {
* @param v1 - Glyph' top texture coordinate.
* @param texture - Glyph' source texture atlas page.
* @param font - Glyph' font.
* @param isReplacement - `true` if glyph is a replacement for a missing glyph.
*
* @returns New `GlyphData`.
*/
Expand All @@ -83,7 +84,8 @@ export class GlyphData {
u1: number,
v1: number,
readonly texture: THREE.Texture,
readonly font: Font
readonly font: Font,
readonly isReplacement: boolean = false
) {
this.character = String.fromCodePoint(codePoint);
this.direction = UnicodeUtils.getDirection(codePoint, block);
Expand Down Expand Up @@ -134,7 +136,8 @@ export class GlyphData {
this.sourceTextureCoordinates[3].x,
this.sourceTextureCoordinates[3].y,
this.texture,
this.font
this.font,
this.isReplacement
);
}
}
34 changes: 34 additions & 0 deletions @here/harp-text-canvas/test/FontCatalogTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,39 @@ describe("FontCatalog", () => {
}
assert(true);
});

it("Retrieves replacement glyph data.", () => {
const char = "L";

const codePoint = char.codePointAt(0)!;
const font = fontCatalog.getFont(codePoint);
assert.isTrue(
fontCatalog!.getGlyph(char.codePointAt(0)!, font, FontStyle.Regular) !== undefined
);

const glyph = fontCatalog.getGlyph(codePoint, font, FontStyle.Regular);
const renderStyle = new TextRenderStyle();
try {
// Make this glyph a replacement, which should lead to the glyph not being rendered on
// production. Normally, the property `isReplacement` is only true for the replacement
// glyph, the "<?>".
(glyph as any).isReplacement = true;
fontCatalog.showReplacementGlyphs = false;

// should be undefined in production...
assert.isUndefined(fontCatalog.getGlyphs(char, renderStyle));

// enable debugging
fontCatalog.showReplacementGlyphs = true;
assert.isDefined(fontCatalog.getGlyphs(char, renderStyle));
// should NOT be empty now...
assert.strictEqual(fontCatalog.getGlyphs(char, renderStyle)!.length, 1);
assert.strictEqual(fontCatalog.getGlyphs(char, renderStyle)![0], glyph);
} finally {
(glyph as any).isReplacement = false;
}
});

it("Is cleared, and fails on glyph data retrieval.", () => {
fontCatalog.clear();
for (const sample of textSamples) {
Expand All @@ -230,6 +263,7 @@ describe("FontCatalog", () => {
}
assert(true);
});

it("Is disposed, and fails on asset loading.", async () => {
fontCatalog.dispose();
assert.strictEqual(fontCatalog.fonts.length, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe("TextCanvas", function() {
}
});
await fontCatalog.loadCharset("ß\n", renderingStyle);
fontCatalog.showReplacementGlyphs = true;
textCanvas.textRenderStyle = renderingStyle;
textCanvas.textLayoutStyle = new TextLayoutStyle({
horizontalAlignment: HorizontalAlignment.Center
Expand Down Expand Up @@ -152,6 +153,7 @@ describe("TextCanvas", function() {
}
});
await fontCatalog.loadCharset("ß-\n", renderingStyle);
fontCatalog.showReplacementGlyphs = true;
textCanvas.textRenderStyle = renderingStyle;
textCanvas.textLayoutStyle = new TextLayoutStyle({
horizontalAlignment: HorizontalAlignment.Center
Expand Down

0 comments on commit 6157fa0

Please sign in to comment.