Skip to content

Commit

Permalink
Simplify formatted sections code (mapbox#9258)
Browse files Browse the repository at this point in the history
* simplify symbol formatting code

* fix linting
  • Loading branch information
mourner authored and mike-unearth committed Mar 18, 2020
1 parent 253ac8a commit 2305894
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 71 deletions.
68 changes: 13 additions & 55 deletions src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ class SymbolBucket implements Bucket {
symbolInstanceIndexes: Array<number>;
writingModes: Array<number>;
allowVerticalPlacement: boolean;
hasPaintOverrides: boolean;
hasRTLText: boolean;

constructor(options: BucketParameters<SymbolStyleLayer>) {
Expand All @@ -353,7 +352,6 @@ class SymbolBucket implements Bucket {
this.pixelRatio = options.pixelRatio;
this.sourceLayerIndex = options.sourceLayerIndex;
this.hasPattern = false;
this.hasPaintOverrides = false;
this.hasRTLText = false;
this.sortKeyRanges = [];

Expand Down Expand Up @@ -381,9 +379,6 @@ class SymbolBucket implements Bucket {
}

createArrays() {
const layout = this.layers[0].layout;
this.hasPaintOverrides = SymbolStyleLayer.hasPaintOverrides(layout);

this.text = new SymbolBuffers(new ProgramConfigurationSet(symbolLayoutAttributes.members, this.layers, this.zoom, property => /^text/.test(property)));
this.icon = new SymbolBuffers(new ProgramConfigurationSet(symbolLayoutAttributes.members, this.layers, this.zoom, property => /^icon/.test(property)));

Expand Down Expand Up @@ -614,75 +609,38 @@ class SymbolBucket implements Bucket {
associatedIconIndex: number) {
const indexArray = arrays.indexArray;
const layoutVertexArray = arrays.layoutVertexArray;
const dynamicLayoutVertexArray = arrays.dynamicLayoutVertexArray;

const segment = arrays.segments.prepareSegment(4 * quads.length, arrays.layoutVertexArray, arrays.indexArray, feature.sortKey);
const segment = arrays.segments.prepareSegment(4 * quads.length, layoutVertexArray, indexArray, feature.sortKey);
const glyphOffsetArrayStart = this.glyphOffsetArray.length;
const vertexStartIndex = segment.vertexLength;

const angle = (this.allowVerticalPlacement && writingMode === WritingMode.vertical) ? Math.PI / 2 : 0;

const addSymbol = (symbol: SymbolQuad) => {
const tl = symbol.tl,
tr = symbol.tr,
bl = symbol.bl,
br = symbol.br,
tex = symbol.tex,
pixelOffsetTL = symbol.pixelOffsetTL,
pixelOffsetBR = symbol.pixelOffsetBR,
mfsx = symbol.minFontScaleX,
mfsy = symbol.minFontScaleY;
const sections = feature.text && feature.text.sections;

for (let i = 0; i < quads.length; i++) {
const {tl, tr, bl, br, tex, pixelOffsetTL, pixelOffsetBR, minFontScaleX, minFontScaleY, glyphOffset, isSDF, sectionIndex} = quads[i];
const index = segment.vertexLength;

const y = symbol.glyphOffset[1];
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, tl.x, y + tl.y, tex.x, tex.y, sizeVertex, symbol.isSDF, pixelOffsetTL.x, pixelOffsetTL.y, mfsx, mfsy);
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, tr.x, y + tr.y, tex.x + tex.w, tex.y, sizeVertex, symbol.isSDF, pixelOffsetBR.x, pixelOffsetTL.y, mfsx, mfsy);
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, bl.x, y + bl.y, tex.x, tex.y + tex.h, sizeVertex, symbol.isSDF, pixelOffsetTL.x, pixelOffsetBR.y, mfsx, mfsy);
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, br.x, y + br.y, tex.x + tex.w, tex.y + tex.h, sizeVertex, symbol.isSDF, pixelOffsetBR.x, pixelOffsetBR.y, mfsx, mfsy);
const y = glyphOffset[1];
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, tl.x, y + tl.y, tex.x, tex.y, sizeVertex, isSDF, pixelOffsetTL.x, pixelOffsetTL.y, minFontScaleX, minFontScaleY);
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, tr.x, y + tr.y, tex.x + tex.w, tex.y, sizeVertex, isSDF, pixelOffsetBR.x, pixelOffsetTL.y, minFontScaleX, minFontScaleY);
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, bl.x, y + bl.y, tex.x, tex.y + tex.h, sizeVertex, isSDF, pixelOffsetTL.x, pixelOffsetBR.y, minFontScaleX, minFontScaleY);
addVertex(layoutVertexArray, labelAnchor.x, labelAnchor.y, br.x, y + br.y, tex.x + tex.w, tex.y + tex.h, sizeVertex, isSDF, pixelOffsetBR.x, pixelOffsetBR.y, minFontScaleX, minFontScaleY);

addDynamicAttributes(dynamicLayoutVertexArray, labelAnchor, angle);
addDynamicAttributes(arrays.dynamicLayoutVertexArray, labelAnchor, angle);

indexArray.emplaceBack(index, index + 1, index + 2);
indexArray.emplaceBack(index + 1, index + 2, index + 3);

segment.vertexLength += 4;
segment.primitiveLength += 2;

this.glyphOffsetArray.emplaceBack(symbol.glyphOffset[0]);
};

if (feature.text && feature.text.sections) {
const sections = feature.text.sections;
this.glyphOffsetArray.emplaceBack(glyphOffset[0]);

if (this.hasPaintOverrides) {
let currentSectionIndex;
const populatePaintArrayForSection = (sectionIndex?: number, lastSection: boolean) => {
if (currentSectionIndex !== undefined && (currentSectionIndex !== sectionIndex || lastSection)) {
arrays.programConfigurations.populatePaintArrays(arrays.layoutVertexArray.length, feature, feature.index, {}, sections[currentSectionIndex]);
}
currentSectionIndex = sectionIndex;
};

for (const symbol of quads) {
populatePaintArrayForSection(symbol.sectionIndex, false);
addSymbol(symbol);
}

// Populate paint arrays for the last section.
populatePaintArrayForSection(currentSectionIndex, true);
} else {
for (const symbol of quads) {
addSymbol(symbol);
}
arrays.programConfigurations.populatePaintArrays(arrays.layoutVertexArray.length, feature, feature.index, {}, sections[0]);
}

} else {
for (const symbol of quads) {
addSymbol(symbol);
if (i === quads.length - 1 || sectionIndex !== quads[i + 1].sectionIndex) {
arrays.programConfigurations.populatePaintArrays(layoutVertexArray.length, feature, feature.index, {}, sections && sections[sectionIndex]);
}
arrays.programConfigurations.populatePaintArrays(arrays.layoutVertexArray.length, feature, feature.index, {});
}

arrays.placedSymbolArray.emplaceBack(labelAnchor.x, labelAnchor.y,
Expand Down
9 changes: 0 additions & 9 deletions src/style/style_layer/symbol_style_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,6 @@ class SymbolStyleLayer extends StyleLayer {

return hasOverrides;
}

static hasPaintOverrides(layout: PossiblyEvaluated<LayoutProps>): boolean {
for (const overridable of properties.paint.overridableProperties) {
if (SymbolStyleLayer.hasPaintOverride(layout, overridable)) {
return true;
}
}
return false;
}
}

export default SymbolStyleLayer;
14 changes: 7 additions & 7 deletions test/unit/symbol/symbol_style_layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,35 @@ test('setPaintOverrides', (t) => {
test('hasPaintOverrides', (t) => {
t.test('undefined', (t) => {
const layer = createSymbolLayer({});
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), false);
t.end();
});

t.test('constant, Formatted type, overriden text-color', (t) => {
const props = {layout: {'text-field': ["format", "text", {"text-color": "red"}]}};
const layer = createSymbolLayer(props);
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), true);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), true);
t.end();
});

t.test('constant, Formatted type, no overrides', (t) => {
const props = {layout: {'text-field': ["format", "text", {"font-scale": 0.8}]}};
const layer = createSymbolLayer(props);
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), false);
t.end();
});

t.test('format expression, overriden text-color', (t) => {
const props = {layout: {'text-field': ["format", ["get", "name"], {"text-color":"red"}]}};
const layer = createSymbolLayer(props);
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), true);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), true);
t.end();
});

t.test('format expression, no overrides', (t) => {
const props = {layout: {'text-field': ["format", ["get", "name"], {}]}};
const layer = createSymbolLayer(props);
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), false);
t.end();
});

Expand All @@ -86,7 +86,7 @@ test('hasPaintOverrides', (t) => {
"default"];
const props = {layout: {'text-field': matchExpr}};
const layer = createSymbolLayer(props);
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), true);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), true);
t.end();
});

Expand All @@ -96,7 +96,7 @@ test('hasPaintOverrides', (t) => {
"default"];
const props = {layout: {'text-field': matchExpr}};
const layer = createSymbolLayer(props);
t.equal(SymbolStyleLayer.hasPaintOverrides(layer.layout), false);
t.equal(SymbolStyleLayer.hasPaintOverride(layer.layout, 'text-color'), false);
t.end();
});

Expand Down

0 comments on commit 2305894

Please sign in to comment.