Skip to content

Commit

Permalink
Add getFonts(). Fixes #26.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevage committed Apr 11, 2021
1 parent 978dfaf commit 23da831
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -10,9 +10,10 @@ Major features:
* All properties can be expressed as camelCase rather than kebab-case.
* Layer operations can act on multiple layers (given by array, regex or filter function), not just one.
* Source types, layer types and property names are incorporated into function names: `addGeoJSON()`, `addCircleLayer()`, `setCircleRadius()`...
* Some other convenience functions: `show()`, `hide()`, `onLoad()`, `setData()`,
* Adding layers and sources is idempotent: call `addLineLayer()` multiple times to create, then update the layer.
* Some other convenience functions: `show()`, `hide()`, `onLoad()`, `setData()`, `fontsInUse()`
* Better click and hover functions: `hoverPointer()`, `hoverFeatureState()`, `hoverPopup()`, `clickLayer()`
* Some functions behave better: `removeLayer()` (not an error if layer doesn't exist), `removeSource()` (removes attached layers automatically), `setFilter()` (works on multiple layers at once)
* Some functions behave better: `removeLayer()` (not an error if layer doesn't exist), `removeSource()` (removes attached layers automatically), `setFilter()` (works on multiple layers at once), `setData()` clears data if no GeoJSON provided.

```js
// Adds U property to map, containing these methods.
Expand All @@ -21,7 +22,6 @@ const U = require('mapbox-gl-utils').init(map);
// Certain methods (eg hoverPopup) require access to the mapboxgl library itself
const mapboxgl = require('mapbox-gl');
const U = require('mapbox-gl-utils').init(map, mapboxgl);

```

### Adding and removing layers
Expand Down Expand Up @@ -225,6 +225,10 @@ map.U.loadImage('marker', '/assets/marker-pin@2x.png', { pixelRatio: 2}).then(/*

// Update the map style's root "transition" property
map.U.setTransition({ delay: 1000, delay: 0});

// Get a list of fonts used in symbol layers with fontsUsed(). Useful for quickly getting some text displaying.
const fonts = map.U.fontsInUse();
map.U.addSymbolLayer('labels', 'mysource', { textFont: fonts[0], textField: '{label}' });
```
### Contrived example
Expand Down
38 changes: 38 additions & 0 deletions src/index.js
Expand Up @@ -603,6 +603,44 @@ Object.assign(Utils.prototype, {
this.map.touchZoomRotate.disableRotation();
this.map.dragRotate.disable();
},
fontsInUse() {
// TODO add tests
// TODO: find fonts burried within ['format', ... { 'text-font': ... }] expressions
function findLiterals(expr) {
if (Array.isArray(expr)) {
if (expr[0] === 'literal') {
///
fonts.push(...expr[1]);
} else {
expr.forEach(findLiterals);
}
}
}
let fonts = [];
const fontExprs = this.map
.getStyle()
.layers.map(l => l.layout && l.layout['text-font'])
.filter(Boolean);
for (const fontExpr of fontExprs) {
// if top level expression is an array of strings, it's hopefully ['Arial', ...] and not ['get', 'font']
if (fontExpr.stops) {
// old-school base/stops
// TODO verify we have got all the cases
try {
fonts.push(
...fontExpr.stops.flat().filter(Array.isArray).flat()
);
} catch {
console.log("Couldn't process font expression:", fontExpr);
}
} else if (fontExpr.every(f => typeof f === 'string')) {
fonts.push(...fontExpr);
} else {
findLiterals(fontExpr);
}
}
return [...new Set(fonts)];
},
});
/* options:
addLayers: [{ id: ..., ... }, ...]
Expand Down

0 comments on commit 23da831

Please sign in to comment.