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:issues#15716, compute the actual height of the text style background #15717

Merged
merged 6 commits into from
Apr 11, 2024

Conversation

Frank-Chan
Copy link
Contributor

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
lineHeight = Math.max(lineHeight, currentHeight);

And the lineHeight variable defined in line 376 is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes,you are right.

Copy link

📦 Preview the website for this branch here: https://deploy-preview-15717--ol-site.netlify.app/.

@ahocevar
Copy link
Member

Thanks @Frank-Chan, see my comments about a suggested change.

@Frank-Chan
Copy link
Contributor Author

@ahocevar Modified and submitted, please review

@ahocevar
Copy link
Member

@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.

@ahocevar
Copy link
Member

ahocevar commented Apr 11, 2024

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 main.js:

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

node test/rendering/test.js --fix --match rich-text-multiline

when the new test directory for that main.js is test/rendering/cases/richt-text-multiline.

@Frank-Chan
Copy link
Contributor Author

Frank-Chan commented Apr 11, 2024

@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.

Yes,I hadn't thought about the single line before. I've been changed now.
Thanks for your suggestion!

@ahocevar
Copy link
Member

I just added a rendering test, and updated an existing one that actually showed the issue which is now fixed.

@ahocevar
Copy link
Member

Thanks, @Frank-Chan

@ahocevar ahocevar merged commit 433934f into openlayers:main Apr 11, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

the height of the background is not fit with text when text style set with backgrounFill
2 participants