-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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:issues#15716, compute the actual height of the text style background #15717
Fix:issues#15716, compute the actual height of the text style background #15717
Conversation
…und, when the text hase multilines
src/ol/render/canvas.js
Outdated
@@ -389,6 +388,7 @@ export function getTextDimensions(baseStyle, chunks) { | |||
lineWidth += currentWidth; | |||
const currentHeight = measureTextHeight(font); | |||
heights.push(currentHeight); | |||
height += currentHeight; | |||
lineHeight = Math.max(lineHeight, currentHeight); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lineHeight = Math.max(lineHeight, currentHeight); |
And the lineHeight
variable defined in line 376 is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,you are right.
📦 Preview the website for this branch here: https://deploy-preview-15717--ol-site.netlify.app/. |
Thanks @Frank-Chan, see my comments about a suggested change. |
@ahocevar Modified and submitted, please review |
@Frank-Chan Look at the failing rendering tests. Seems the test fixes your issue, but breaks things in cases where the font changes within a single line. The best thing would be to add a rendering test for your case first, and then work on a solution that fixes your case without breaking the existing ones. |
Looks like from the code before your pull request, the only change needed is --- a/src/ol/render/canvas.js
+++ b/src/ol/render/canvas.js
@@ -381,6 +381,7 @@ export function getTextDimensions(baseStyle, chunks) {
lineWidths.push(lineWidth);
lineWidth = 0;
height += lineHeight;
+ lineHeight = 0;
continue;
}
const font = chunks[i + 1] || baseStyle.font; And for a rendering test, you could use the following as import Feature from '../../../../src/ol/Feature.js';
import Fill from '../../../../src/ol/style/Fill.js';
import Map from '../../../../src/ol/Map.js';
import Point from '../../../../src/ol/geom/Point.js';
import Stroke from '../../../../src/ol/style/Stroke.js';
import Style from '../../../../src/ol/style/Style.js';
import Text from '../../../../src/ol/style/Text.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import View from '../../../../src/ol/View.js';
const vectorSource = new VectorSource({
features: [new Feature(new Point([0, 0]))],
});
const labelStyle = new Style({
text: new Text({
font: '13px Ubuntu,sans-serif',
fill: new Fill({
color: '#000',
}),
stroke: new Stroke({
color: '#fff',
width: 4,
}),
padding: [0, 0, 0, 0],
overflow: false,
}),
});
new Map({
pixelRatio: 1,
layers: [
new VectorLayer({
source: vectorSource,
style: function (feature) {
const textStyle = labelStyle.getText();
textStyle.setFont('13px Ubuntu,sans-serif');
textStyle.setBackgroundFill(
new Fill({
color: 'blue',
}),
);
textStyle.setBackgroundStroke(
new Stroke({
color: 'red',
width: 2,
}),
);
textStyle.setStroke(
new Stroke({
color: '#b4ebaf',
width: 4,
}),
);
textStyle.setText([
'30',
'bold 23px/23px serif',
'\n',
'',
' Montana',
'900 11px/11px serif',
'\n',
'',
'6.858 people/mi²',
'italic 11px/11px Ubuntu,sans-serif',
]);
return labelStyle;
},
}),
],
target: 'map',
view: new View({
center: [0, 0],
resolution: 1,
}),
});
render({tolerance: 0.01}); You can generate the reference image with
when the new test directory for that |
Yes,I hadn't thought about the single line before. I've been changed now. |
I just added a rendering test, and updated an existing one that actually showed the issue which is now fixed. |
Thanks, @Frank-Chan |
Fix issue#15716