Tabs on Android (tab)--A magical character (Cocos2dx crash)

Source: Internet
Author: User
Tags addchild

Today's test found a problem with the game, System mail, if the tab, on Android on the open message content will be crash. And they're pretty sure it's the tab question.


With my multi-month experience (indeed not for many years ...) See, never heard on Android because of a tab crash, and if there is this problem, there will certainly be a lot of people encounter, estimated long noisy, search for a bit, what information is not available.

So write a test engineering test, respectively, under the Mac and Windows, with text editing tools to edit the 4 txt document, UTF has a BOM and no BOM, the content is "tab ABCD", found to be normal display, and will not 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", +);    Label->setposition (CCP (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2));    AddChild (label);

Then started to rage with the code, found that the project used a custom control, is to implement a rich text box function. The so-called rich text box is able to display a variety of colors, can display URLs, and some things can point to the kind. But the realization of this thing, one step, is to take apart a UTF8 string, split it into a single character , make a texture of each character, and then come up with a puzzle and put it into a text box . Then the tab must be split into a single character, and the last thing to find is the crash when the tab texture is generated. The stack is as follows:


By the way. Labelttf the principle of display, basically, through the font and font size, and then call the corresponding platform API, generate a picture (image), Note is a picture Ah , and then as a texture, set to a sprite. From the stack above, it was crash when the image was generated.

Then find the code, under Cctexture2d, find the last step of the stack above, as follows:

BOOL Cctexture2d::initwithstring (const char *text, const char *fontname, float fontSize, const ccsize& dimensions, CCT Extalignment 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 have the new image come out, then use the text to go to the init image, and then use this image to go to the init texture. Let's look at the init image here, the code for 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 follows

    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 to know, must be the text, font, font size these things, called to the Java layer, let them call the system related things, return a bitmap back, then is the next step, the return image to initialize the texture. But there is no need for the next step, because to this crash, get the following log:


From this log, it probably means that the Java layer, when creating bitmap, needs a picture of the width of the height, but this size is equal to 0.

But before the test is able to display tab, think about, the previous test has a problem, that is, the previous test is a combination of tab and other characters , this place because the rich text box processing, is a single tab character , then with a single tab test. Sure enough, the same crash as above.

Back to this crash log itself, the graph to be generated, that is, the picture that wrote the word, then the width of the image should depend on the character's content and the size of the character, how can be 0. is it because tab is automatically ignored on Android ? Write an engineering test now:

    unsigned long fsize = 0;    unsigned char *data = ccfileutils::sharedfileutils ()->getfiledata ("Tab.txt", "RB", &fsize);        Cclabelttf *label = cclabelttf::create ((const char*) data, "ABC", +);    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 mainly the addition of a color block of the same size behind the text, so that we can see the actual range of the texture (as long as this is the beginning of the 2 did not think ...) ),It turns out that no matter how tab,tab is added, the size of the texture is the same as the size of the Non-tab.。


If this is the case on COCOS2DX, but he will eventually call to the Java layer of the Android system to generate this bitmap, indicating that this is not a cocos2dx problem, then in the Android application, tab will be ignored , A decisive project to re-test, as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/container "    android:layout_width=" Match_parent "    Android : layout_height= "Match_parent" >        <textview        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:text= "@string/hello_world"         android:background= "#FFFF0000"/></ Linearlayout>

<resources>    <string name= "app_name" >tabTest</string>    <string name= "Hello_world" > Hello world!</string>    <string name= "Action_settings" >Settings</string></resources>

Sure enough, the tab is still not shown, hahaha haha, finish ~ ~


Summary:The Android system will ignore tab, in the COCOS2DX with Cclabelttf, if the text content only a tab character, will crash.

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.