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

Never resolve tokens for text-field or icon-image expressions #5600

Merged
merged 2 commits into from
Nov 6, 2017
Merged
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
11 changes: 2 additions & 9 deletions src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const IndexBuffer = require('../../gl/index_buffer');
const {ProgramConfigurationSet} = require('../program_configuration');
const createVertexArrayType = require('../vertex_array_type');
const {TriangleIndexArray, LineIndexArray} = require('../index_array_type');
const resolveTokens = require('../../util/token');
const transformText = require('../../symbol/transform_text');
const mergeLines = require('../../symbol/mergelines');
const scriptDetection = require('../../util/script_detection');
Expand Down Expand Up @@ -463,19 +462,13 @@ class SymbolBucket implements Bucket {

let text;
if (hasText) {
text = layer.getLayoutValue('text-field', globalProperties, feature);
if (layer.isLayoutValueFeatureConstant('text-field')) {
text = resolveTokens(feature.properties, text);
}
text = layer.getValueAndResolveTokens('text-field', globalProperties, feature);
text = transformText(text, layer, globalProperties, feature);
}

let icon;
if (hasIcon) {
icon = layer.getLayoutValue('icon-image', globalProperties, feature);
if (layer.isLayoutValueFeatureConstant('icon-image')) {
icon = resolveTokens(feature.properties, icon);
}
icon = layer.getValueAndResolveTokens('icon-image', globalProperties, feature);
}

if (!text && !icon) {
Expand Down
31 changes: 30 additions & 1 deletion src/style-spec/function/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function convertFunction(parameters, propertySpec, name) {
const zoomDependent = zoomAndFeatureDependent || !featureDependent;

const stops = parameters.stops.map((stop) => {
if (!featureDependent && (name === 'icon-image' || name === 'text-field') && typeof stop[1] === 'string') {
return [stop[0], convertTokenString(stop[1])];

}
return [stop[0], convertValue(stop[1], propertySpec)];
});

Expand Down Expand Up @@ -223,7 +227,7 @@ function appendStopPair(curve, input, output, isStep) {
curve.push(output);
}

function getFunctionType (parameters, propertySpec) {
function getFunctionType(parameters, propertySpec) {
if (parameters.type) {
return parameters.type;
} else if (propertySpec.function) {
Expand All @@ -232,3 +236,28 @@ function getFunctionType (parameters, propertySpec) {
return 'exponential';
}
}

// "String with {name} token" => ["concat", "String with ", ["get", "name"], " token"]
function convertTokenString(s) {
const result = ['concat'];
const re = /{([^{}]+)}/g;
let pos = 0;
let match;
while ((match = re.exec(s)) !== null) {
const literal = s.slice(pos, re.lastIndex - match[0].length);
pos = re.lastIndex;
if (literal.length > 0) result.push(literal);
result.push(['to-string', ['get', match[1]]]);
}

if (result.length === 1) {
return s;
}

if (pos < s.length) {
result.push(s.slice(pos));
}

return result;
}

12 changes: 12 additions & 0 deletions src/style/style_layer/symbol_style_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const StyleLayer = require('../style_layer');
const SymbolBucket = require('../../data/bucket/symbol_bucket');
const resolveTokens = require('../../util/token');
const {isExpression} = require('../../style-spec/expression');
const assert = require('assert');

import type {Feature, GlobalProperties} from '../../style-spec/expression';
Expand Down Expand Up @@ -42,6 +44,16 @@ class SymbolStyleLayer extends StyleLayer {
return !declaration || declaration.expression.isZoomConstant;
}

getValueAndResolveTokens(name: 'text-field' | 'icon-image', globals: GlobalProperties, feature: Feature) {
const value = this.getLayoutValue(name, globals, feature);
const declaration = this._layoutDeclarations[name];
if (this.isLayoutValueFeatureConstant(name) && !isExpression(declaration.value)) {
return resolveTokens(feature.properties, value);
}

return value;
}

createBucket(parameters: BucketParameters) {
// Eventually we need to make SymbolBucket conform to the Bucket interface.
// Hack around it with casts for now.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64,
"operations": []
}
},
"center": [0, 0],
"zoom": 0,
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {
"icon": "dot.sdf",
"name": "X"
},
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
}
}
}
},
"sprite": "local://sprites/sprite",
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
"layers": [{
"id": "icon-expression",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-offset": [-16, -16],
"icon-image": ["step", ["zoom"], "{icon}", 10, ""],
"icon-allow-overlap": true,
"icon-ignore-placement": true
}
}, {
"id": "text-expression",
"type": "symbol",
"source": "geojson",
"layout": {
"text-offset": [1, -1],
"text-field": ["step", ["zoom"], "{name}", 10, ""],
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-allow-overlap": true,
"text-ignore-placement": true
}
}, {
"id": "icon-function",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-offset": [-16, 16],

"icon-image": {
"stops": [
[0, "{icon}"],
[22, ""]
]
},
"icon-allow-overlap": true,
"icon-ignore-placement": true
}
}, {
"id": "text-function",
"type": "symbol",
"source": "geojson",
"layout": {
"text-offset": [1, 1],
"text-field": {
"stops": [
[0, "{name}"],
[22, ""]
]
},
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-allow-overlap": true,
"text-ignore-placement": true
}
}]
}
46 changes: 46 additions & 0 deletions test/unit/style-spec/convert_function.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const test = require('mapbox-gl-js-test').test;
const convertFunction = require('../../../src/style-spec/function/convert');

test('convertFunction', (t) => {
t.test('feature-constant text-field with token replacement', (t) => {
const functionValue = {
stops: [
[0, 'my name is {name}.'],
[1, '{a} {b} {c}'],
[2, 'no tokens']
]
};

const expression = convertFunction(functionValue, {
type: 'string',
function: 'piecewise-constant'
}, 'text-field');
t.deepEqual(expression, [
'step',
['zoom'],
[
'concat',
'my name is ',
['to-string', ['get', 'name']],
'.'
],
1,
[
'concat',
['to-string', ['get', 'a']],
' ',
['to-string', ['get', 'b']],
' ',
['to-string', ['get', 'c']]
],
2,
'no tokens'
]);

t.end();
});

t.end();
});