Skip to content

Commit e54e0be

Browse files
authored
fix Gemetry getContainerExtent is error when markerWidth is 0 (#2176)
* fix Gemetry getContainerExtent is error when markerWidth is 0 * fix typo * updates
1 parent acd2508 commit e54e0be

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

src/core/util/marker.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ const SIZE = [];
136136
export function getVectorMarkerFixedExtent(out, symbol, size) {
137137
// const padding = getVectorPadding(symbol) * 2;
138138
size = size || calVectorMarkerSize(SIZE, symbol);
139+
if (size && (size[0] === 0 || size[1] === 0)) {
140+
emptyExtent(out);
141+
return out;
142+
}
139143
// if (padding) {
140144
// size = size.map(d => d - padding);
141145
// }
@@ -185,6 +189,11 @@ export function calVectorMarkerSize(out, symbol) {
185189
const padding = getVectorPadding(symbol);
186190
const width = getValueOrDefault(symbol['markerWidth'], DEFAULT_MARKER_SYMBOLS.markerWidth);
187191
const height = getValueOrDefault(symbol['markerHeight'], DEFAULT_MARKER_SYMBOLS.markerHeight);
192+
if (width === 0 || height === 0) {
193+
out[0] = 0;
194+
out[1] = 0;
195+
return out;
196+
}
188197
const lineWidth = getValueOrDefault(symbol['markerLineWidth'], DEFAULT_MARKER_SYMBOLS.markerLineWidth),
189198
shadow = 2 * ((symbol['shadowBlur'] || 0) + Math.max(Math.abs(symbol['shadowOffsetX'] || 0) + Math.abs(symbol['shadowOffsetY'] || 0))), // add some tolerance for shadowOffsetX/Y
190199
w = Math.round(width + lineWidth + shadow + padding * 2),
@@ -217,6 +226,10 @@ export function getImageMarkerFixedExtent(out, symbol, resources) {
217226
height = symbol['markerHeight'] || (img ? img.height : 0);
218227
TEMP_SIZE.width = width;
219228
TEMP_SIZE.height = height;
229+
if (symbol['markerWidth'] === 0 || symbol['markerHeight'] === 0) {
230+
emptyExtent(out);
231+
return out;
232+
}
220233
const alignPoint = getAlignPoint(TEMP_SIZE, symbol['markerHorizontalAlignment'] || 'middle', symbol['markerVerticalAlignment'] || 'top');
221234
return getFixedExtent(out, symbol['markerDx'] || 0, symbol['markerDy'] || 0,
222235
getMarkerRotation(symbol), alignPoint, width, height);
@@ -225,6 +238,10 @@ export function getImageMarkerFixedExtent(out, symbol, resources) {
225238

226239
export function getTextMarkerFixedExtent(out, symbol, textDesc) {
227240
const size = textDesc['size'];
241+
if (size && (size.width === 0 || size.height === 0)) {
242+
emptyExtent(out);
243+
return out;
244+
}
228245
const alignPoint = getAlignPoint(size, symbol['textHorizontalAlignment'], symbol['textVerticalAlignment']);
229246
// if (symbol['textHaloRadius']) {
230247
// const r = symbol['textHaloRadius'];
@@ -316,3 +333,13 @@ export const DYNAMIC_SYMBOL_PROPS = [
316333
export const SIZE_SYMBOL_PROPS = [
317334
'textName', 'markerType', 'markerFile', 'textHaloRadius', 'shadowBlur', 'shadowOffsetX', 'shadowOffsetY', 'textWrapWidth'
318335
];
336+
337+
export function emptyExtent(extent) {
338+
if (!extent) {
339+
return;
340+
}
341+
extent.xmin = Infinity;
342+
extent.ymin = Infinity;
343+
extent.xmax = -Infinity;
344+
extent.ymax = -Infinity;
345+
}

src/geometry/Geometry.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ const TEMP_POINT0 = new Point(0, 0);
3131
const TEMP_EXTENT = new PointExtent();
3232
const TEMP_PROPERTIES = {};
3333

34+
function validateExtent(extent) {
35+
if (!extent) {
36+
return false;
37+
}
38+
const { xmin, ymin, xmax, ymax } = extent;
39+
return (xmax - xmin > 0 && ymax - ymin > 0);
40+
}
41+
3442
/**
3543
* @property {Object} options - geometry options
3644
* @property {Boolean} [options.id=null] - id of the geometry
@@ -452,7 +460,10 @@ class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
452460
extent._combine(groundExtent);
453461
}
454462
if (extent) {
455-
extent._add(this._getFixedExtent());
463+
const fixedExtent = this._getFixedExtent();
464+
if (validateExtent(fixedExtent)) {
465+
extent._add(fixedExtent);
466+
}
456467
}
457468
const smoothness = this.options['smoothness'];
458469
if (smoothness) {

test/geometry/MarkerSpec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,75 @@ describe('Geometry.Marker', function () {
909909
});
910910
done();
911911
});
912+
913+
it('#2175 marker ContainerExtent when markerWidth/markerHeight/textSize =0', function (done) {
914+
const imageSymbol = {
915+
markerWidth: 10,
916+
markerHeight: 10,
917+
'markerFile': 'resources/tile.png',
918+
};
919+
920+
const pathSymbol = {
921+
'markerType': 'path',
922+
'markerPath': [{
923+
'path': 'M8 23l0 0 0 0 0 0 0 0 0 0c-4,-5 -8,-10 -8,-14 0,-5 4,-9 8,-9l0 0 0 0c4,0 8,4 8,9 0,4 -4,9 -8,14z M3,9 a5,5 0,1,0,0,-0.9Z',
924+
'fill': '#DE3333'
925+
}],
926+
'markerPathWidth': 16,
927+
'markerPathHeight': 23,
928+
'markerWidth': 8,
929+
'markerHeight': 20,
930+
};
931+
932+
const vectorSymbol = {
933+
markerType: 'ellipse',
934+
markerWidth: 10,
935+
markerHeight: 10
936+
};
937+
938+
const textSymbol = {
939+
textSize: 12,
940+
textName: 'hello'
941+
};
942+
943+
const symbols = [imageSymbol, pathSymbol, vectorSymbol, textSymbol];
944+
945+
let idx = 0;
946+
947+
function test() {
948+
layer.clear();
949+
if (idx < symbols.length) {
950+
const symbol = symbols[idx];
951+
const isText = symbol.textSize !== undefined;
952+
const point = new maptalks.Marker(map.getCenter(), {
953+
symbol: symbol
954+
})
955+
point.addTo(layer);
956+
setTimeout(() => {
957+
const extent = point.getContainerExtent();
958+
expect(extent.getWidth()).not.to.be.eql(0);
959+
expect(extent.getHeight()).not.to.be.eql(0);
960+
961+
if (isText) {
962+
symbol.textSize = 0;
963+
point.setSymbol(symbol)
964+
} else {
965+
symbol.markerWidth = 0;
966+
point.setSymbol(symbol)
967+
}
968+
969+
setTimeout(() => {
970+
const extent = point.getContainerExtent();
971+
expect(extent.getWidth()).to.be.eql(0);
972+
expect(extent.getHeight()).to.be.eql(0);
973+
idx++;
974+
test();
975+
}, 50);
976+
}, 50);
977+
} else {
978+
done();
979+
}
980+
}
981+
test();
982+
});
912983
});

0 commit comments

Comments
 (0)