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

gdx-ttf fonts bleed black color onto glyphs when they have shadows of any color #7391

Merged
merged 7 commits into from
Apr 27, 2024

Conversation

WinterAlexander
Copy link
Contributor

@WinterAlexander WinterAlexander commented Apr 24, 2024

Problem:

An oversight in the design of shadows in the gdx-ttf extension: When drawing fonts with shadows, it initialized an additional pixmap "shadowPixmap" without changing it's default fill color, leading it to be transparent black (0, 0, 0, 0). Then it renders the font on top which leads to bleeding the black color on the font

Example:

image
image

Fix:

Fill the shadow pixmap with the transparent color before using it, leaving transparent pixels to be the color choosen by the user.

image

Workaround:

While we wait for this to get merged, here's the fix in the form of a 3 classes: https://gist.github.com/WinterAlexander/5ec4bc65de744fdd90354013e5c0c0d7

tommyettinger
tommyettinger previously approved these changes Apr 24, 2024
Copy link
Member

@tommyettinger tommyettinger left a comment

Choose a reason for hiding this comment

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

This looks very reasonable, and the whole fix is just two lines! I looked through the process you went through to discover this in the scene2d channel on the Discord, and it seems very thorough. There's a very slight performance cost for filling a Pixmap, but since it should be done rather rarely, and certainly not per-frame, the quality consideration is more important. I can't think of any way that would meaningfully speed this up other than maybe checking if the transparent color is already (0,0,0,0), which would mean the fill isn't needed. Even that check is probably not that helpful for performance... I'm approving as-is, and I'd approve again if you want to add in the check for if the transparent color is already transparent black.

tommyettinger
tommyettinger previously approved these changes Apr 24, 2024
Copy link
Member

@tommyettinger tommyettinger left a comment

Choose a reason for hiding this comment

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

Approved again! I wouldn't worry too much about changing "gdx-ttf" since it's not like there's some other extension that could refer to, heh.

CHANGES Outdated
@@ -12,6 +12,7 @@
- Android: Add configuration option to render under the cutout if available on the device.
- Fix: Keep SelectBox popup from extending past right edge of stage.
- Added Framebuffer multisample support (see GL31FrameBufferMultisampleTest.java for basic usage)
- Fix: Fonts generated with gdx-ttf no longer bleed when drawn with a shadow
Copy link
Member

Choose a reason for hiding this comment

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

I think it's called gdx-freetype, but I don't use it much so it might be called gdx-ttf in some places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct and not a big change at all, applied :)

Pixmap shadowPixmap = new Pixmap(shadowW, shadowH, mainPixmap.getFormat());
// use the Gdx2DPixmap constructor to avoid filling the pixmap twice
Pixmap shadowPixmap = new Pixmap(
new Gdx2DPixmap(shadowW, shadowH, Pixmap.Format.toGdx2DPixmapFormat(mainPixmap.getFormat())));
Copy link
Member

Choose a reason for hiding this comment

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

I like this much better; it replaces one fill with transparent black with one fill by the intended color. I really doubt there's any measurable cost difference from before the PR, but the edge quality on the font should be quite a lot better now.

Copy link
Member

@tommyettinger tommyettinger left a comment

Choose a reason for hiding this comment

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

Good good, I'm sure gdx-freetype is happy to have its name used correctly!

@NathanSweet
Copy link
Member

Good find!

@NathanSweet NathanSweet merged commit 748ae62 into libgdx:master Apr 27, 2024
2 checks passed
@WinterAlexander WinterAlexander deleted the patch-1 branch April 28, 2024 02:37
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.

None yet

3 participants