Skip to content

Commit

Permalink
fix text font error when textsize missing (#2087)
Browse files Browse the repository at this point in the history
* fix text font error when textsize missing

* update

* spec
  • Loading branch information
deyihu authored Sep 24, 2023
1 parent 894a8e4 commit c942e04
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/core/util/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export function getAlignPoint(size, horizontalAlignment, verticalAlignment) {
return new Point(alignW, alignH);
}

const DEFAULT_FONT = 'monospace';
//export it for plugin develop
export const DEFAULT_FONT = 'monospace';
export const DEFAULT_TEXTSIZE = 14;

/**
* Returns CSS Font from a symbol with text styles.
Expand All @@ -234,9 +236,13 @@ export function getFont(style) {
if (style['textFont']) {
return style['textFont'];
} else {
let textSize = style.textSize;
if (isNil(textSize)) {
textSize = DEFAULT_TEXTSIZE;
}
return (style['textStyle'] && style['textStyle'] !== 'normal' ? style['textStyle'] + ' ' : '') +
(style['textWeight'] && style['textWeight'] !== 'normal' ? style['textWeight'] + ' ' : '') +
style['textSize'] + 'px ' +
textSize + 'px ' +
(!style['textFaceName'] ? DEFAULT_FONT : (style['textFaceName'][0] === '"' ? style['textFaceName'] : '"' + style['textFaceName'] + '"'));
}
}
Expand Down
52 changes: 43 additions & 9 deletions test/geometry/MarkerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ describe('Geometry.Marker', function () {
var marker1 = new maptalks.Marker(
center.sub(0.009, 0),
{
'symbol' : {
'markerType' : 'ellipse',
'markerWidth' : 28,
'markerHeight' : 40,
'markerDx' : 0,
'markerDy' : 100000,
'markerOpacity': 1
}
'symbol': {
'markerType': 'ellipse',
'markerWidth': 28,
'markerHeight': 40,
'markerDx': 0,
'markerDy': 100000,
'markerOpacity': 1
}
}
).addTo(vlayer);
).addTo(vlayer);
});

it('can be text', function () {
Expand Down Expand Up @@ -875,4 +875,38 @@ describe('Geometry.Marker', function () {
expect(newCoords).to.be.eql([118.62842615841328, 32.17932019575247]);
});
});

//https://github.com/maptalks/issues/issues/422
it('marker size when textSize missing', function (done) {
const texts = ['Maptalks', 'Hello\nWorld', Math.random() * 100, '中国人', '没有设置textSize时', '没有设置textSize时\nmarker的getSize方法返回的值不正确 '];
function createMaker(text, textSize) {
const symbol = {
textName: text,
textFill: 'red',
textSize: textSize
};
if (!symbol.textSize) {
delete symbol.textSize;
}
return new maptalks.Marker(map.getCenter(), {
symbol
});
}

function getMarkerWidth(marker) {
return Math.round(marker.getSize().width);
}

texts.forEach(text => {
layer.clear();
const marker1 = createMaker(text, 14);
marker1.addTo(layer);
const marker2 = createMaker(text);
marker2.addTo(layer);
const w1 = getMarkerWidth(marker1);
const w2 = getMarkerWidth(marker2);
expect(w1).to.be.eql(w2);
});
done();
});
});

0 comments on commit c942e04

Please sign in to comment.