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 the vertical clipping of BMFont labels #18733

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from

Conversation

sp-jordi-vilalta
Copy link
Contributor

In some particular cases, when using a BMFont in a label and setting dimensions which are smaller than the contents of the label, the vertical clipping isn't computed properly and the label shows garbage, corresponding to contiguous characters in the texture. I guess it may also be related to other reported issues if the referenced character is at the border of the texture (maybe #17371 or #10422). I initially faced the issue with characters that extended below the base line (like the "j") in the bottom line of the label, but then I found that it also happens when reducing the label dimension enough.

In my case I could reproduce it with the following code:

cocos2d::Label* label = cocos2d::Label::createWithBMFont("example.fnt", "j");
label->setBMFontSize(23.0f);
const cocos2d::Size& sceneSize = scene->getContentSize();
label->setPosition(sceneSize.width * 0.5f, sceneSize.height * 0.5f);
label->setVerticalAlignment(cocos2d::TextVAlignment::TOP);
label->setDimensions(20.0f, 10.0f);
scene->addChild(label, 1000);

The problem is that the clipping computation doesn't take into account the BMFont scale, making the result depend on the content scale factor. Adding this factor in the computations changed the height at which the bottom of the characters is clipped, removing some incorrect offsets and not showing garbage anymore.

I'm not sure the new clipping height is the desired one, since it falls below the bottom of the content size, but at least it shows a consistent behavior among scale factors. I've tried to fix this but it seems that there are some inconsistencies in the way tokenLowestY is computed, mixing screen-space and design-space coordinates. Overall it feels like this code needs some double-checking by someone who knows it better than me.

The following table shows the differences visually, for labels with bottom, middle and top alignments, and different vertical dimensions. The pink rectangle shows the content size of the label:

Content scale factor 1 2 3
Original 0-original-1 0-original-2 0-original-3
After fixing top clipping 1-topclip-fixed-1 1-topclip-fixed-2 1-topclip-fixed-3
After fixing bottom clipping 2-bottomclip-fixed-1 2-bottomclip-fixed-2 2-bottomclip-fixed-3

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

2 participants