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

Fix lazy load of RTL text plugin on GeoJSON sources #9091

Merged
merged 6 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ class SymbolBucket implements Bucket {
}

isEmpty() {
return this.symbolInstances.length === 0;
// When the bucket encounters only rtl-text but the plugin isnt loaded, no symbol instances will be created.
// In order for the bucket to be serialized, and not discarded as an empty bucket both checks are necessary.
return this.symbolInstances.length === 0 && !this.hasRTLText;
}

uploadPending() {
Expand Down
9 changes: 9 additions & 0 deletions src/source/rtl_text_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ export const plugin: {
};
}
};

export const lazyLoadRTLTextPlugin = function() {
if (!plugin.isLoading() &&
!plugin.isLoaded() &&
getRTLTextPluginStatus() === 'deferred'
) {
downloadRTLTextPlugin();
}
};
2 changes: 2 additions & 0 deletions src/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Texture from '../render/texture';
import browser from '../util/browser';
import EvaluationParameters from '../style/evaluation_parameters';
import SourceFeatureState from '../source/source_state';
import {lazyLoadRTLTextPlugin} from './rtl_text_plugin';

const CLOCK_SKEW_RETRY_TIMEOUT = 30000;

Expand Down Expand Up @@ -183,6 +184,7 @@ class Tile {
if (bucket instanceof SymbolBucket) {
if (bucket.hasRTLText) {
this.hasRTLText = true;
lazyLoadRTLTextPlugin();
break;
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import TileBounds from './tile_bounds';
import {ResourceType} from '../util/ajax';
import browser from '../util/browser';
import {cacheEntryPossiblyAdded} from '../util/tile_request_cache';
import {plugin as rtlTextPlugin, getRTLTextPluginStatus, downloadRTLTextPlugin} from './rtl_text_plugin';

import type {Source} from './source';
import type {OverscaledTileID} from './tile_id';
Expand Down Expand Up @@ -156,15 +155,6 @@ class VectorTileSource extends Evented implements Source {

if (this.map._refreshExpiredTiles && data) tile.setExpiryData(data);
tile.loadVectorData(data, this.map.painter);
if (tile.hasRTLText) {
const plugin = rtlTextPlugin;
if (!plugin.isLoading() &&
!plugin.isLoaded() &&
getRTLTextPluginStatus() === 'deferred'
) {
downloadRTLTextPlugin();
}
}

cacheEntryPossiblyAdded(this.dispatcher);

Expand Down
42 changes: 25 additions & 17 deletions test/unit/data/symbol_bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import Protobuf from 'pbf';
import {VectorTile} from '@mapbox/vector-tile';
import SymbolBucket from '../../../src/data/bucket/symbol_bucket';
import {CollisionBoxArray} from '../../../src/data/array_types';
import SymbolStyleLayer from '../../../src/style/style_layer/symbol_style_layer';
import featureFilter from '../../../src/style-spec/feature_filter';
import {performSymbolLayout} from '../../../src/symbol/symbol_layout';
import {Placement} from '../../../src/symbol/placement';
import Transform from '../../../src/geo/transform';
import {OverscaledTileID} from '../../../src/source/tile_id';
import Tile from '../../../src/source/tile';
import CrossTileSymbolIndex from '../../../src/symbol/cross_tile_symbol_index';
import FeatureIndex from '../../../src/data/feature_index';
import {createSymbolBucket} from '../../util/create_symbol_layer';

// Load a point feature from fixture tile.
const vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));
Expand All @@ -29,21 +28,8 @@ transform.cameraToCenterDistance = 100;

const stacks = {'Test': glyphs};

function bucketSetup() {
const layer = new SymbolStyleLayer({
id: 'test',
type: 'symbol',
layout: {'text-font': ['Test'], 'text-field': 'abcde'},
filter: featureFilter()
});
layer.recalculate({zoom: 0, zoomHistory: {}});

return new SymbolBucket({
overscaling: 1,
zoom: 0,
collisionBoxArray,
layers: [layer]
});
function bucketSetup(text = 'abcde') {
return createSymbolBucket('test', 'Test', text, collisionBoxArray);
}

test('SymbolBucket', (t) => {
Expand Down Expand Up @@ -98,3 +84,25 @@ test('SymbolBucket integer overflow', (t) => {
t.ok(console.warn.getCall(0).calledWithMatch(/Too many glyphs being rendered in a tile./));
t.end();
});

test('SymbolBucket detects rtl text', (t) => {
const rtlBucket = bucketSetup('مرحبا');
const ltrBucket = bucketSetup('hello');
const options = {iconDependencies: {}, glyphDependencies: {}};
rtlBucket.populate([{feature}], options);
ltrBucket.populate([{feature}], options);

t.ok(rtlBucket.hasRTLText);
Copy link
Contributor

Choose a reason for hiding this comment

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

should we also test t.ok(!rtlBucket.isEmpty());? It seems like that's more of what we want to know, since hasRTLText is used to determine if the bucket is empty or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a separate test in 315742d

t.notOk(ltrBucket.hasRTLText);
t.end();
});

test('SymbolBucket detects rtl text mixed with ltr text', (t) => {
const mixedBucket = bucketSetup('مرحبا translates to hello');
const options = {iconDependencies: {}, glyphDependencies: {}};
mixedBucket.populate([{feature}], options);

t.ok(mixedBucket.hasRTLText);
t.end();
});

26 changes: 24 additions & 2 deletions test/unit/source/tile.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {test} from '../../util/test';
import {createSymbolBucket} from '../../util/create_symbol_layer';
import Tile from '../../../src/source/tile';
import GeoJSONWrapper from '../../../src/source/geojson_wrapper';
import {OverscaledTileID} from '../../../src/source/tile_id';
Expand Down Expand Up @@ -263,6 +264,27 @@ test('expiring tiles', (t) => {
t.end();
});

test('rtl text detection', (t) => {
t.test('Tile#hasRTLText is true when a tile loads a symbol bucket with rtl text', (t) => {
const tile = new Tile(new OverscaledTileID(1, 0, 1, 1, 1));
const symbolBucket = createSymbolBucket('test', 'Test', 'مرحبا', new CollisionBoxArray());
symbolBucket.hasRTLText = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have to manually set symbolBucket.hasRTLText = true? Shouldn't that happen automatically like in the symbol bucket tests above? It seems like this test is kind of trivial if we manually set this to true since in tile.js, it basically just is this.hasRTLText = symbolBucket.hasRTLText.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh, its because hasRTLText can only be determined after bucket.populate has been called.
I'm only using createSymbolBucket to create a stub of a symbol bucket here. I've added a comment to explain.

Copy link
Contributor

Choose a reason for hiding this comment

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

There's a couple typos in the comment

tile.loadVectorData(
createVectorData({rawTileData: createRawTileData(), buckets: [symbolBucket]}),
createPainter({
getLayer() {
return symbolBucket.layers[0];
}
})
);

t.ok(tile.hasRTLText);
t.end();
});

t.end();
});

function createRawTileData() {
return fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'));
}
Expand All @@ -276,6 +298,6 @@ function createVectorData(options) {
}, options);
}

function createPainter() {
return {style: {}};
function createPainter(styleStub = {}) {
return {style: styleStub};
}
20 changes: 20 additions & 0 deletions test/util/create_symbol_layer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import SymbolBucket from '../../src/data/bucket/symbol_bucket';
import SymbolStyleLayer from '../../src/style/style_layer/symbol_style_layer';
import featureFilter from '../../src/style-spec/feature_filter';

export function createSymbolBucket(layerId, font, text, collisionBoxArray) {
const layer = new SymbolStyleLayer({
id: layerId,
type: 'symbol',
layout: {'text-font': [font], 'text-field': text},
filter: featureFilter()
});
layer.recalculate({zoom: 0, zoomHistory: {}});

return new SymbolBucket({
overscaling: 1,
zoom: 0,
collisionBoxArray,
layers: [layer]
});
}