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

MOAITextLabel:getTextBounds it was the execution result, the return value is incorrect. #1006

Open
makotok opened this issue Oct 25, 2014 · 8 comments
Assignees
Labels

Comments

@makotok
Copy link
Member

makotok commented Oct 25, 2014

MOAITextLabel:getTextBounds it was the execution result, the return value is incorrect.

  • Test Code
    font = MOAIFont.new()
    font:load( 'arial-rounded.TTF')

    textbox = MOAITextBox.new()
    textbox:setString("Test Text String")
    textbox:setFont(font)
    textbox:setTextSize(7.5, 163)
    textbox:setRect(0, 0, 100, 200)
    textbox:setAlignment(MOAITextBox.LEFT_JUSTIFY)
    textbox:setYFlip(true)

    layer = MOAILayer.new()
    layer:insertProp(textbox)

    xMin, yMin, xMax, yMax = textbox:getStringBounds(1, #textbox:getString())
    print("TestMOAITextBox:test_getStringBounds", xMin, yMin, xMax, yMax)
  • V1.5 Console
TestMOAITextBox:test_getStringBounds    0   17  77  57
  • develop Console
TestMOAITextBox:test_getStringBounds    -50 -95 27  -52
@Vavius
Copy link
Contributor

Vavius commented Oct 25, 2014

Looks like the new implementation returns bounds relative to center of the rect - a bug.

Another issue that I was facing is how the height is calculated. For many use cases (like crossing text) height should be taken as line height instead of each glyph's bounding box.

@patrickmeehan
Copy link
Member

Will look into this, guys. Thanks!

@patrickmeehan patrickmeehan self-assigned this Oct 27, 2014
@Vavius
Copy link
Contributor

Vavius commented Oct 27, 2014

Here is what I use locally (to get constant line height irrespective to the actual rendered glyphs):

for ( u32 i = spanIdx; i < end; ++i ) {
    MOAITextSprite& sprite = this->mSprites [ i ];
    MOAIGlyph& glyph = *sprite.mGlyph;

    if ( glyph.mWidth > 0.0f ) {

        ZLRect glyphRect = glyph.GetRect ( sprite.mPen.mX, sprite.mPen.mY, sprite.mScale.mX, sprite.mScale.mY );

        // Update the glyphRect with glyphset's metrics,
        // Taking into the account line ascent and lineHeight to properly
        // handle descenders and ascenders: p j q g l i
        float fontSize = sprite.mStyle->GetSize ();
        MOAIGlyphSet* glyphSet = sprite.mStyle->GetFont ()->GetGlyphSet ( fontSize );
        float deckHeight = glyphSet->GetHeight () * sprite.mScale.mY;
        float deckAscent = glyphSet->GetAscent () * sprite.mScale.mY;
        glyphRect.mYMax = sprite.mPen.mY - deckAscent;
        glyphRect.mYMin = glyphRect.mYMax - deckHeight;

        if ( result ) {
            rect.Grow ( glyphRect );
        }
        else {
            rect = glyphRect;
            result = true;
        }
    }
}

@patrickmeehan
Copy link
Member

I'll need to expose a way to get either. I redid some of that stuff so I could line up billboarded text correctly with the original text in an SVG, but I understand why you'd need the other height.

@patrickmeehan
Copy link
Member

OK, re the centered textbox, the behavior did but the value is correct (for the new implementation). getStringBounds/getTextBounds returns the bounds in model space. To make the 'autoflip' textbox behavior easier to implement, I now center the glyphs around the origin.

Would it be worth modifying that method to return the coords in 'text space' vs model space?

@Vavius
Copy link
Contributor

Vavius commented Oct 28, 2014

Is 'text space' a top-left origin, y facing down? Then definitely not.

I expect values to be in model space. In Makoto's post he have textbox rect non-centered, with origin in top-left (or bottom-right). But getStringBounds returns bounds as if origin was centered. Maybe we should apply offset before returning bounds? For centered bounds (what I use) the behaviour will be the same.

@makotok
Copy link
Member Author

makotok commented Oct 28, 2014

I was a little more testing.

  • Test Code
    self.textbox:setRect(0, 0, 100, 200)
    self.textbox:setAlignment(MOAITextBox.LEFT_JUSTIFY, MOAITextBox.TOP_JUSTIFY)
    xMin, yMin, xMax, yMax = self.textbox:getStringBounds(1, #self.textbox:getString())
    print("TestMOAITextBox:test_getStringBounds_1", xMin, yMin, xMax, yMax)

    self.textbox:setRect(0, 0, 100, 200)
    self.textbox:setAlignment(MOAITextBox.RIGHT_JUSTIFY, MOAITextBox.BOTTOM_JUSTIFY)
    xMin, yMin, xMax, yMax = self.textbox:getStringBounds(1, #self.textbox:getString())
    print("TestMOAITextBox:test_getStringBounds_2", xMin, yMin, xMax, yMax)

    self.textbox:setRect(-50, -50, 50, 150)
    self.textbox:setAlignment(MOAITextBox.LEFT_JUSTIFY, MOAITextBox.TOP_JUSTIFY)
    xMin, yMin, xMax, yMax = self.textbox:getStringBounds(1, #self.textbox:getString())
    print("TestMOAITextBox:test_getStringBounds_3", xMin, yMin, xMax, yMax)

    self.textbox:setRect(-50, -50, 50, 150)
    self.textbox:setAlignment(MOAITextBox.RIGHT_JUSTIFY, MOAITextBox.BOTTOM_JUSTIFY)
    xMin, yMin, xMax, yMax = self.textbox:getStringBounds(1, #self.textbox:getString())
    print("TestMOAITextBox:test_getStringBounds_4", xMin, yMin, xMax, yMax)
  • develop branch output
TestMOAITextBox:test_getStringBounds_1  -50 -95 27  -52
TestMOAITextBox:test_getStringBounds_2  -27 65  50  108
TestMOAITextBox:test_getStringBounds_3  -50 -95 27  -52
TestMOAITextBox:test_getStringBounds_4  -27 65  50  108
  • V1.5-stable branch output
TestMOAITextBox:test_getStringBounds_1  0   17  77  57
TestMOAITextBox:test_getStringBounds_2  23  17  100 57
TestMOAITextBox:test_getStringBounds_3  -50 -33 27  7
TestMOAITextBox:test_getStringBounds_4  -27 -33 50  7

I think that is better to behave as before desirable. If possible.

I by this problem, some of the components (TextInput) did not work.
Perhaps there is a possibility that the same thing happens even others.

As a workaround, setRect always center is set to 0.
Or, I add the difference between the width deviated from the center.

@makotok
Copy link
Member Author

makotok commented Oct 28, 2014

Thank you everyone.
Anyway, my component was able to cope with this by adding an offset.

As a new external specification, what should be the getTextBounds / getStringBounds function?

@halfnelson halfnelson added the Bug label Jun 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants