Skip to content

Commit c942e04

Browse files
authored
fix text font error when textsize missing (#2087)
* fix text font error when textsize missing * update * spec
1 parent 894a8e4 commit c942e04

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/core/util/strings.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ export function getAlignPoint(size, horizontalAlignment, verticalAlignment) {
222222
return new Point(alignW, alignH);
223223
}
224224

225-
const DEFAULT_FONT = 'monospace';
225+
//export it for plugin develop
226+
export const DEFAULT_FONT = 'monospace';
227+
export const DEFAULT_TEXTSIZE = 14;
226228

227229
/**
228230
* Returns CSS Font from a symbol with text styles.
@@ -234,9 +236,13 @@ export function getFont(style) {
234236
if (style['textFont']) {
235237
return style['textFont'];
236238
} else {
239+
let textSize = style.textSize;
240+
if (isNil(textSize)) {
241+
textSize = DEFAULT_TEXTSIZE;
242+
}
237243
return (style['textStyle'] && style['textStyle'] !== 'normal' ? style['textStyle'] + ' ' : '') +
238244
(style['textWeight'] && style['textWeight'] !== 'normal' ? style['textWeight'] + ' ' : '') +
239-
style['textSize'] + 'px ' +
245+
textSize + 'px ' +
240246
(!style['textFaceName'] ? DEFAULT_FONT : (style['textFaceName'][0] === '"' ? style['textFaceName'] : '"' + style['textFaceName'] + '"'));
241247
}
242248
}

test/geometry/MarkerSpec.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ describe('Geometry.Marker', function () {
140140
var marker1 = new maptalks.Marker(
141141
center.sub(0.009, 0),
142142
{
143-
'symbol' : {
144-
'markerType' : 'ellipse',
145-
'markerWidth' : 28,
146-
'markerHeight' : 40,
147-
'markerDx' : 0,
148-
'markerDy' : 100000,
149-
'markerOpacity': 1
150-
}
143+
'symbol': {
144+
'markerType': 'ellipse',
145+
'markerWidth': 28,
146+
'markerHeight': 40,
147+
'markerDx': 0,
148+
'markerDy': 100000,
149+
'markerOpacity': 1
150+
}
151151
}
152-
).addTo(vlayer);
152+
).addTo(vlayer);
153153
});
154154

155155
it('can be text', function () {
@@ -875,4 +875,38 @@ describe('Geometry.Marker', function () {
875875
expect(newCoords).to.be.eql([118.62842615841328, 32.17932019575247]);
876876
});
877877
});
878+
879+
//https://github.com/maptalks/issues/issues/422
880+
it('marker size when textSize missing', function (done) {
881+
const texts = ['Maptalks', 'Hello\nWorld', Math.random() * 100, '中国人', '没有设置textSize时', '没有设置textSize时\nmarker的getSize方法返回的值不正确 '];
882+
function createMaker(text, textSize) {
883+
const symbol = {
884+
textName: text,
885+
textFill: 'red',
886+
textSize: textSize
887+
};
888+
if (!symbol.textSize) {
889+
delete symbol.textSize;
890+
}
891+
return new maptalks.Marker(map.getCenter(), {
892+
symbol
893+
});
894+
}
895+
896+
function getMarkerWidth(marker) {
897+
return Math.round(marker.getSize().width);
898+
}
899+
900+
texts.forEach(text => {
901+
layer.clear();
902+
const marker1 = createMaker(text, 14);
903+
marker1.addTo(layer);
904+
const marker2 = createMaker(text);
905+
marker2.addTo(layer);
906+
const w1 = getMarkerWidth(marker1);
907+
const w2 = getMarkerWidth(marker2);
908+
expect(w1).to.be.eql(w2);
909+
});
910+
done();
911+
});
878912
});

0 commit comments

Comments
 (0)