Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fall back to local glyph rendering if glyph PBF is unavailable #4564

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Fix 3D map freezing when camera is adjusted against map bounds. ([#4537](https://github.com/maplibre/maplibre-gl-js/issues/4537))
- Fix `getStyle()` to return a clone so the object cannot be internally changed ([#4488](https://github.com/maplibre/maplibre-gl-js/issues/4488))
- Prefer local glyph rendering for all CJKV characters, not just those in the CJK Unified Ideographs, Hiragana, Katakana, and Hangul Syllables blocks. ([#4560](https://github.com/maplibre/maplibre-gl-js/pull/4560)))
- If a glyph PBF is unavailable or lacks a glyph for a character in a `text-field`, try to render it locally instead of crashing. ([#4564](https://github.com/maplibre/maplibre-gl-js/pull/4564))
- - _...Add new stuff here..._

## 4.5.2
Expand Down
7 changes: 7 additions & 0 deletions src/render/glyph_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ describe('GlyphManager', () => {
expect(manager._doesCharSupportLocalGlyph(0xC544)).toBe(true);
});

test('GlyphManager generates missing PBF locally', async () => {
const manager = createGlyphManager('sans-serif');

const returnedGlyphs = await manager.getGlyphs({'Arial Unicode MS': [0x10e1]});
expect(returnedGlyphs['Arial Unicode MS'][0x10e1].metrics.advance).toBe(12);
});

test('GlyphManager caches locally generated glyphs', async () => {

const manager = createGlyphManager('sans-serif');
Expand Down
24 changes: 20 additions & 4 deletions src/render/glyph_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {loadGlyphRange} from '../style/load_glyph_range';

import TinySDF from '@mapbox/tiny-sdf';
import {AlphaImage} from '../util/image';
import {warnOnce} from '../util/util';

import type {StyleGlyph} from '../style/style_glyph';
import type {RequestManager} from '../util/request_manager';
Expand Down Expand Up @@ -84,7 +85,7 @@ export class GlyphManager {
return {stack, id, glyph};
}

glyph = this._tinySDF(entry, stack, id);
glyph = this._tinySDF(entry, stack, id, false);
if (glyph) {
entry.glyphs[id] = glyph;
return {stack, id, glyph};
Expand All @@ -108,7 +109,22 @@ export class GlyphManager {
entry.requests[range] = promise;
}

const response = await entry.requests[range];
let response;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this block to a different method maybe? This method is already too long I think

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, looking at the code above this part, can we now handle the case where url is not defined? I know it's not optimal, but there are situations where you just want the text to be render without finding a glyph server. You'd need to define localIdeographFontFamily anyway, which doesn't have a default, so you'd need to know what you are doing...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately, I would love to make glyph PBFs completely optional. However, the style specification currently states that glyphs is required as long as any layer has a text-field property. Would relaxing that requirement constitute a breaking change in either the style spec or GL JS?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it will be a breaking change as changing a required field to optional doesn't sound like a breaking change (unless you have invalid styles, which I'm not sure are interesting).
Relaxing it would make a far better developer experience, IMHO, as I find these glyphs requirement non-friendly...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this should be brought in the style spec repo first...

try {
response = await entry.requests[range];
} catch (e) {
glyph = this._tinySDF(entry, stack, id, true);
const begin = range * 256;
const end = begin + 255;
const codePoint = id.toString(16).toUpperCase();
if (glyph) {
warnOnce(`Unable to load glyph range ${range}, ${begin}-${end}. Rendering codepoint U+${codePoint} locally instead. ${e}`);
entry.glyphs[id] = glyph;
return {stack, id, glyph};
} else {
warnOnce(`Unable to load glyph range ${range}, ${begin}-${end}, or render codepoint U+${codePoint} locally. ${e}`);
}
}
for (const id in response) {
if (!this._doesCharSupportLocalGlyph(+id)) {
entry.glyphs[+id] = response[+id];
Expand All @@ -129,13 +145,13 @@ export class GlyphManager {
/\p{Ideo}|\p{sc=Hang}|\p{sc=Hira}|\p{sc=Kana}/u.test(String.fromCodePoint(id));
}

_tinySDF(entry: Entry, stack: string, id: number): StyleGlyph {
_tinySDF(entry: Entry, stack: string, id: number, force: boolean): StyleGlyph {
const fontFamily = this.localIdeographFontFamily;
Copy link
Contributor Author

@1ec5 1ec5 Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m wary of choosing a font for non-ideographic text based on an option called localIdeographFontFamily. The choice should be up to the style author, in the stylesheet, and it should vary from layer to layer, just as in CSS. The iOS implementation has long attempted to match the text-font property to a local or system font. I think GL JS should attempt some matching along these lines.

Unfortunately, many non-Mapbox-hosted fontstacks are named according to the styles for which they were created, obscuring the PostScript names of the fonts from which they were derived. For example, OpenHistoricalMap has “OpenHistorical Bold”, and OSM Americana has “Americana-Bold”. These fontstacks are a pastiche of various fonts from different families. The style authors can’t simply append Open Sans, Unifont, or Noto Sans to the array, because text-font isn’t a fallback list. It simply joins the array into a comma-separated list and includes it in the request URL, so the request would fail and we’d be right back to where we started.

In order to typeset these layers in the designer’s choice of fonts, we could interpret text-font as a cascading fallback list of local font names or keywords (similar to CSS) if the glyph PBF request fails. We could short-circuit that request if glyphs is absent. Or we could introduce a new layout property, such as text-font-family or text-font-names, to represent a cascading fallback list. In the latter case, text-font would remain for backwards compatibility or where a fontstack is preferred for some reason.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fallback is better than introducing a new field which would create confusion.
If the style and the server are not aligned, presenting something is better than not presenting anything, IMHO.

if (!fontFamily) {
return;
}

if (!this._doesCharSupportLocalGlyph(id)) {
if (!force && !this._doesCharSupportLocalGlyph(id)) {
return;
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions test/integration/render/tests/text-local-glyphs/missing/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"version": 8,
"metadata": {
"test": {
"pixelRatio": 2,
"localIdeographFontFamily": "sans-serif",
"width": 800,
"height": 600
}
},
"zoom": 8,
"sources": {
"sample": {
"type": "geojson",
"data": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"properties": {
"name_en": "Georgia",
"name_ka": "საქართველო"
}
}
}
},
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
"layers": [
{
"id": "sample-text",
"type": "symbol",
"source": "sample",
"layout": {
"text-anchor": "top",
"text-field": "{name_en} ({name_ka})",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-size": 30,
"text-offset": [0, 0]
}
}
]
}