Skip to content

Support offsetX for text placed along a line - #17576

Merged
ahocevar merged 4 commits into
openlayers:mainfrom
sobol-sudo:support-offsetx-for-text-along-line
Jul 26, 2026
Merged

ahocevar merged 4 commits into
openlayers:mainfrom
sobol-sudo:support-offsetx-for-text-along-line

Conversation

@sobol-sudo

Copy link
Copy Markdown
Contributor

Fixes #16342.

offsetX on a Text style is applied when the text is placed on a point, but silently dropped when placement is 'line'. drawChars_ reads textOffsetY_ and puts it in the DRAW_CHARS instruction, but never reads textOffsetX_, so the executor has nothing to shift by.

This carries it through the same way offsetY already travels: drawChars_ multiplies textOffsetX_ by the pixel ratio and appends it to both the regular and the hit-detection DRAW_CHARS instruction, and Executor adds it to startM, the distance along the path where the text starts. I appended the value at the end of the instruction array rather than inserting it next to offsetY, so every existing index stays where it was — DRAW_CHARS is only produced and consumed in TextBuilder.js and Executor.js, so there are no other readers.

Scaling by pixelRatio and not by the text scale matches what the point-placement path does in Executor.js, and it keeps the term dimensionally consistent with textLength in the same expression, which is already multiplied by the measure pixel ratio.

I added two cases to test/browser/spec/ol/render/canvas/textbuilder.test.js asserting the emitted instruction carries the scaled offset, and zero when none is configured. Both fail on main.

I did not add a rendering case. The reference images are generated on your Ubuntu CI and the existing text cases depend on the Ubuntu font, so a PNG I generate on macOS would almost certainly differ on antialiasing and just make the build red. Happy to add one if you can regenerate the reference, or if you'd rather I try to match the CI environment more closely.

@github-actions

Copy link
Copy Markdown

@ahocevar ahocevar left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, @sobol-sudo. This change breaks existing rendering tests.

case cases/text-style-linestring-nice/main.js': mismatch 0.016326904296875
case cases/text-style-linestring-ugly/main.js': mismatch 0.023956298828125
RENDERING TESTS FAILED

Also, please add a rendering test. With a large font size, a stright line, text without offsetX with a negative offsetY and text with offsetX with a positive offsetX. This will allow for visual comparion on the reference image. If the new rendering test fails with your reference image, I'll happily replace it with the CI generated one.

@sobol-sudo

Copy link
Copy Markdown
Contributor Author

Thanks for the pointers.

On the two broken tests — I think the mismatch is the fix doing its job. Both text-style-linestring-nice and text-style-linestring-ugly have a feature with offsetX: -10 and placement: 'line', and in nice that feature's label is literally 'negative offsetX'. Since the offset was being dropped, those reference images recorded the text unshifted. So they had to move. I've regenerated both.

I also added text-style-linestring-offset as you asked: straight line, 24px Ubuntu, one label with no offset for comparison, one with offsetY: -10 and no offsetX, one with offsetX: 40.

One caveat on the reference images, since you offered to replace them. They were generated on macOS and mine don't line up with yours: with the fix reverted and a clean build, text-style-linestring-nice still mismatches its committed reference by 0.0155 here, and webgl-text-style-linestring by 0.0182 — both without any change of mine. text-style-linestring-ugly is the only one of the three that passes clean and fails only once the fix is applied, which matches the 0.0240 you saw. So the platform delta on this machine is roughly the same magnitude as the change itself, and I'd trust CI-generated references over mine for all three. Happy for you to replace them.

Worth noting webgl-text-style-linestring didn't appear in your list — on my machine it shifts from 0.0182 to 0.0213 with the fix, but since most of that is the platform delta I've left its reference alone rather than guess.

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you compare the text-style-linestring-ugly reference image, you'll see flipped offsetY on the "set properties" and "small text" paths. These can not be explained with the fixed offsetX handling.

Regarding the new rendering test. I was envisioning a single line with two text styles, one above the line and one below (achieved by offsetY), and one of the text styles with an offsetX, one without. Both with textAlign: left, so the difference can be seen.

@sobol-sudo
sobol-sudo force-pushed the support-offsetx-for-text-along-line branch from 07a39c6 to 3dfea2a Compare July 26, 2026 14:53
@sobol-sudo

Copy link
Copy Markdown
Contributor Author

You're right, and regenerating those two references was a mistake on my part — I've reverted both, they're back to what's on main.

To isolate what the change actually touches, I replaced + offsetX with + 0 in the executor while leaving the builder change in place, rebuilt, and re-ran the case: text-style-linestring-ugly passes. So the change only ever acts through offsetX, and any feature without one is byte-identical. In that case only Set properties (setOffsetX(10)) and PLEASE OMIT ME IM UGLY (offsetX: -10) can move — Small text has offsetY: 5 and no offsetX at all, so it shouldn't have changed. If you were reading that off my regenerated reference, that's another reason to drop it.

The flip on the two that do have an offsetX is explainable though, and I think it's worth deciding on deliberately. drawTextOnPath derives beginX and endX by walking the path from startM, then sets reverse = beginX > endX (or the rotated equivalent when there's a rotation). Since I add offsetX to startM, shifting the text along a path that doubles back can flip that comparison — the run reverses, and with it the side offsetY lands on. So it isn't a separate defect, it's what happens if offsetX is allowed to move the text before the upright decision is made.

If you'd rather offsetX never be able to flip a run, the alternative is to make the reverse decision from the un-offset span and apply the offset afterwards. That's a small change and I'm happy to do it — it just needs to be your call, since it means offsetX and keepUpright stop interacting.

Rendering test rewritten to your description: one line, two text styles, both textAlign: 'left', one above with no offsetX and one below with offsetX: 60.

@ahocevar

Copy link
Copy Markdown
Member

The flip on the two that do have an offsetX is explainable though, and I think it's worth deciding on deliberately.

Ok, let's look more closely at the test/rendering/cases/text-style-linestring-ugly/expected.png reference image in 07a39c6:

There is no offsetX in the initial style nor set afterwards for feature5, which is the one with the "small text" style. So this indicates that the state handling for offsetX is wrong. The offsetX state bleeds into subsequent styles.

Looking at the way the setProperties text is rendered for feature3, the keepUpright direction change does explain the rendered result though. This raises the question you already asked, and thinking this further raises a related question: what about the repeat property? How should offsetX be applied on repeated text? To handle this consistently, the answer is to apply the offset to the anchor after all layout decisions have been made.

@sobol-sudo
sobol-sudo force-pushed the support-offsetx-for-text-along-line branch from 3dfea2a to 9021c86 Compare July 26, 2026 18:54
@sobol-sudo

Copy link
Copy Markdown
Contributor Author

You're right about the anchor, and I've reworked it that way — but I don't think the state bleeds, and I'd rather not leave that hanging.

Two checks. First, a unit test: build with a style that has offsetX: -10, draw, then set a second style with no offsetX and draw again. The second DRAW_CHARS carries 0, not -10. Second, a pixel diff of the whole text-style-linestring-ugly case rendered on the same machine, clean vs the startM version — every changed pixel is in one cluster:

That cluster is Set properties, the one with setOffsetX(10). Small text is untouched. I think the reference I pushed earlier was just misleading — sorry for the detour it caused.

On repeat and doing the offset after layout: agreed, and that's the better place regardless of the flip. startM is back to what it was, and the offset now comes off anchorX in both the stroke and fill loops, directly alongside the existing - offsetY. So it can't influence which segment a glyph lands on, the beginX > endX decision, or how repeated runs are spaced — it just moves the finished label, the same way offsetY already does, and the same way the point-placement path does it at anchorX = (labelWithAnchor.anchorX - textOffsetX) * pixelRatio.

The flip is gone with this: Set properties now renders along the path exactly as it does on main, just displaced.

text-style-linestring-nice and text-style-linestring-ugly still differ from their references, which is expected now that their offsetX: -10 actually does something — I've left both references untouched at what's on main, so please generate those two from CI. The new case's reference is unchanged by the rework, since on a straight line both approaches land in the same place.

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reference images updated. The issue with "small text" persists, but it predates this pull request. I reduced the pixel tolerance of that test to prevent future regressions.

Thanks, @sobol-sudo

@ahocevar
ahocevar merged commit 2218549 into openlayers:main Jul 26, 2026
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.

X offset of a text placed on a line is ignored

2 participants