Tab on Android-a magic character (cocos2dx crash)

Source: Internet
Author: User

Today's test found a problem with the game. If a tab is sent to the system email, the content of the email on android will crash. And they are sure that it is a tab problem.


Based on my experience for more than a month (indeed not many years ...) I have never heard that a tab will crash on android. If this problem exists, there will certainly be many people. It may have been a long time ago, and I searched for it, no available information.

So I wrote a test project and tested it. In mac and windows, I used a text editing tool to edit four txt documents. utf has bom and bom, the content is "tab abcd". It can be displayed normally and cannot be crash. As follows:

    unsigned long fSize = 0;    unsigned char *data = CCFileUtils::sharedFileUtils()->getFileData("tab.txt", "rb", &fSize);        CCLabelTTF *label = CCLabelTTF::create((const char*)data, "abc", 30);    label->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));    addChild(label);

Then I started to get angry with the code and found that a custom control was used in the project to implement the function of a rich text box. The rich text box is the one that displays various colors, URLs, and other things. However, one step in this implementation is to split a utf8 string into a single character, make the texture of each character, and then splice it into a text box. Then the tab will be split into a single character, and the final result is crash when the texture of the tab is generated. The stack is as follows:


By the way, the principle of labelTTF display is generally to generate an image by using the font and font size, and then calling the api of the corresponding platform. Note that it is a graph and then as a texture, set to a genie. From the above stack, It is crash when the image is generated. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + Signature = "brush: java;"> bool CCTexture2D: initWithString (const char * text, const char * fontName, float fontSize, const CCSize & dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment ){... ... ... ... Do {CCImage * pImage = new CCImage (); CC_BREAK_IF (NULL = pImage); bRet = pImage-> initWithString (text, (int) dimensions. width, (int) dimensions. height, eAlign, fontName, (int) fontSize); CC_BREAK_IF (! BRet); bRet = initWithImage (pImage); CC_SAFE_RELEASE (pImage) ;}while (0 );... ... ... ... Return bRet ;}The first step here is to use a new image, and then use the text to init the image, and then use the image to init the texture. Next let's look at the init image. Here, the code in android is like this:

bool CCImage::initWithString(                               const char *    pText,                                int             nWidth/* = 0*/,                                int             nHeight/* = 0*/,                               ETextAlign      eAlignMask/* = kAlignCenter*/,                               const char *    pFontName/* = nil*/,                               int             nSize/* = 0*/){    bool bRet = false;    do     {        CC_BREAK_IF(! pText);                BitmapDC &dc = sharedBitmapDC();        CC_BREAK_IF(! dc.getBitmapFromJava(pText, nWidth, nHeight, eAlignMask, pFontName, nSize));        m_pData = dc.m_pData;        CC_BREAK_IF(! m_pData);        m_nWidth    = (short)dc.m_nWidth;        m_nHeight   = (short)dc.m_nHeight;        m_bHasAlpha = true;        m_bPreMulti = true;        m_nBitsPerComponent = 8;        bRet = true;    } while (0);    return bRet;}
The focus is on the getBitmapFromJava function, as shown below:

    bool getBitmapFromJava(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize)    {    return  getBitmapFromJavaShadowStroke(text, nWidth, nHeight, eAlignMask, pFontName, fontSize );    }
From here, we can see that we must call the text, Font, and font size to the java layer to call system-related things, return a bitmap, and then go to the next step, initialize texture for the returned image. However, the next step is not required, because crash is generated here and the following log is obtained:


From this log, it probably means that when the java layer creates a bitmap, it needs the width and height of an image, but the size is equal to 0.

However, tabs can be displayed in the previous test. Think about it. There is a problem in the previous test, that is, the previous test is a mixture of tabs and other characters, because of the processing of rich text boxes, is a single tab character, then test it with a single tab. It is as crash as above.

Return to the crash log itself. The graph to be generated is the graph with the words written. The width and height of the image should depend on the content and size of the characters, why is it 0. Is it because tab is automatically ignored on android. Write another project test now:

    unsigned long fSize = 0;    unsigned char *data = CCFileUtils::sharedFileUtils()->getFileData("tab.txt", "rb", &fSize);        CCLabelTTF *label = CCLabelTTF::create((const char*)data, "abc", 30);    label->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));    CCLayerColor *col = CCLayerColor::create(ccc4(123, 255,0 , 255));    col->setContentSize(label->getContentSize());    col->ignoreAnchorPointForPosition(false);    col->setAnchorPoint(ccp(0.5, 0.5));    col->setPosition(ccp(label->getPosition().x,label->getPosition().y));    addChild(col);    addChild(label);

The difference between this test and the previous one is that a color block is added after the text, so that we can see the actual range of the texture (it should have been so long ago, in fact, it was not expected to start 2 ...), It turns out that no matter how you add a tab, the tab will not be displayed, and the texture size is the same as that without a tab.


If this is the case on cocos2dx, but he will finally call the java layer of android to generate this bitmap, which means this is not a problem with cocos2dx, then in android applications, will the tab be ignored? Start a project and test again, as shown below:

         
  
 

     
  
   tabTest
      
  
   Hello world!
      
  
   Settings
  
 

The tab is still not displayed. Haha, haha, close the job ~~


Summary:The android system ignores the tab. When we use CCLabelTTF in cocos2dx, if the text content has only one tab character, crash is triggered.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.