Contour labels are drawn in a font the map has (#965)

The contour label layer named no font, so MapLibre asked for its default
("Open Sans Regular,Arial Unicode MS Regular"), which the map does not ship.
The request failed and the labels were drawn with glyphs the browser made up
locally.  The layer now names Noto Sans Regular, in the map viewer and in the
standalone viewer.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-13 15:53:01 -07:00 • committed by GitHub
parent 4f58d2862f
commit 113e70a402
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 1 deletions

View file

@ -198,6 +198,8 @@ function buildStyle(sources, flavor = "light", hasTerrain = false, scaleUnit = "
visibility: contourVis,
"symbol-placement": "line",
"text-field": ["concat", ["get", "ele"], elevUnit],
// A font the map has glyphs for; MapLibre's default is not one of them.
"text-font": ["Noto Sans Regular"],
"text-size": 10,
},
paint: {

View file

@ -109,7 +109,20 @@ jest.mock('protomaps-themes-base', () => ({
{id: 'places_locality', type: 'symbol', source},
],
}));
jest.mock('@acalcutt/maplibre-contour-pmtiles', () => ({__esModule: true, default: {}}));
// Contours are generated in the browser from the terrain file; only the URL of that source matters here.
jest.mock('@acalcutt/maplibre-contour-pmtiles', () => ({
__esModule: true,
default: {
DemSource: class {
setupMaplibre() {
}
contourProtocolUrl() {
return 'dem-contour://{z}/{x}/{y}';
}
},
},
}));
jest.mock('../api', () => ({
...jest.requireActual('../api'),
getMapFiles: jest.fn(),
@ -363,3 +376,28 @@ describe('an overlay with a style beside it', () => {
expect(map.style.sprite).toBe(`${window.location.origin}/map-assets/sprites/light`);
});
});
describe('the text of the map', () => {
// The fonts the map has glyphs for (modules/map/static/fonts).
const FONTS = ['Noto Sans Regular', 'Noto Sans Medium', 'Noto Sans Italic'];
const TERRAIN = mapFile('terrain-planet.pmtiles', 'terrain', {tile_type: 'webp'});
test('contour labels are drawn in a font the map has', async () => {
const map = await renderViewer([PLANET, TERRAIN]);
const labels = map.getLayer('contour-labels');
expect(labels.layout['text-field']).toBeDefined();
// Without a font MapLibre asks for its default, which the map does not have.
expect(labels.layout['text-font']).toBeDefined();
expect(FONTS).toEqual(expect.arrayContaining(labels.layout['text-font']));
});
test('every layer with text names a font the map has', async () => {
const map = await renderViewer([PLANET, TERRAIN, FEATURES]);
const textLayers = map.style.layers.filter(l => l.layout?.['text-field'] !== undefined);
expect(textLayers.map(l => l.id)).toContain('contour-labels');
for (const textLayer of textLayers) {
expect([textLayer.id, textLayer.layout['text-font']]).toEqual([textLayer.id, expect.any(Array)]);
expect(FONTS).toEqual(expect.arrayContaining(textLayer.layout['text-font']));
}
});
});

View file

@ -380,6 +380,8 @@
visibility: contourVis,
"symbol-placement": "line",
"text-field": ["concat", ["get", "ele"], elevUnit],
// A font the map has glyphs for; MapLibre's default is not one of them.
"text-font": ["Noto Sans Regular"],
"text-size": 10,
},
paint: {"text-color": textColor, "text-halo-color": haloColor, "text-halo-width": 1},