Cocos2d-x project porting to WP8 Series 8: cclabelttf displays Chinese without line breaks

Source: Internet
Author: User

 

On the WP8 platform, cclabettf does not automatically wrap Chinese characters. After reading the source code, the original underlying implementation is determined based on the text space, if a space exceeded the width of the label, a new line is generated. If the text is Chinese, a new line is generated ~~

The following implementation all reference http://blog.csdn.net/hopingwhite/article/details/38414917, the code after finishing is as follows:

Add two methods to the ccfreetypefont class in ccfreetypefont. h:

Ft_error addline (const STD: string & line );
Void endline_chinese ();

The implementation is as follows:

  1 void CCFreeTypeFont::endLine_chinese()   2 {  3     if(m_currentLine)  4     {  5         m_lines.push_back(m_currentLine);  6         compute_bbox(m_currentLine->glyphs, &m_currentLine->bbox);  7         m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin;  8         m_textWidth = max(m_textWidth,m_currentLine->bbox.xMax - m_currentLine->bbox.xMin);  9         m_textHeight += m_lineHeight; 10         m_currentLine = NULL; 11     } 12 } 13  14  15  16 FT_Error CCFreeTypeFont::addLine(const std::string& line) 17 { 18         wchar_t *       pwszBuffer = nullptr; 19  20         int num_chars = line.size(); 21         int nBufLen = num_chars + 1; 22         pwszBuffer = new wchar_t[nBufLen]; 23         if (!pwszBuffer) 24         { 25                 return -1; 26         } 27  28         memset(pwszBuffer, 0, nBufLen); 29         num_chars = MultiByteToWideChar(CP_UTF8, 0, line.c_str(), num_chars, pwszBuffer, nBufLen); 30         pwszBuffer[num_chars] = ‘\0‘; 31  32         int maxWidth = m_inWidth ? m_inWidth : m_windowWidth; 33  34         newLine(); 35         FT_Vector pen = m_currentLine->pen; 36         FT_GlyphSlot    slot = m_face->glyph; 37         FT_UInt                 glyph_index; 38         FT_UInt                 previous = 0; 39         FT_Error                error = 0; 40         unsigned int    numGlyphs = 0; 41  42         FT_Bool useKerning = FT_HAS_KERNING(m_face); 43  44         int n = 0; 45         while (n < num_chars) 46         { 47                 TGlyph glyph; 48  49                 /* convert character code to glyph index */ 50                 FT_ULong c = pwszBuffer[n++]; 51                 glyph_index = FT_Get_Char_Index(m_face, c); 52  53                 if (useKerning && previous && glyph_index) 54                 { 55                         FT_Vector  delta; 56                         FT_Get_Kerning(m_face, previous, glyph_index, 57                                 FT_KERNING_DEFAULT, &delta); 58                         pen.x += delta.x >> 6; 59                 } 60  61                 /* store current pen position */ 62                 glyph.pos = pen; 63                 glyph.index = glyph_index; 64  65                 /* load glyph image into the slot without rendering */ 66                 error = FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT); 67                 if (error) 68                         continue;  /* ignore errors, jump to next glyph */ 69  70                 /* extract glyph image and store it in our table */ 71                 error = FT_Get_Glyph(m_face->glyph, &glyph.image); 72                 if (error) 73                         continue;  /* ignore errors, jump to next glyph */ 74  75                 /* translate the glyph image now */ 76                 FT_Glyph_Transform(glyph.image, 0, &glyph.pos); 77  78                 /* increment pen position */ 79                 pen.x += slot->advance.x >> 6; 80  81                 if (pen.x > maxWidth) 82                 { 83                         m_currentLine->pen = pen; 84                         85                         // endLine(); 86                         endLine_chinese(); 87  88                         newLine(); 89                         pen = m_currentLine->pen; 90                         previous = 0; 91                         n--; 92                 } 93                 else 94                 { 95                         m_currentLine->glyphs.push_back(glyph); 96                         /* record current glyph index */ 97                         previous = glyph_index; 98                 } 99         }100 101         if (m_currentLine)102         {103                 m_currentLine->pen = pen;104                 105                 //endLine();106                 endLine_chinese();107         }108 109         CC_SAFE_DELETE_ARRAY(pwszBuffer);110         return error;111 }

 

Ft_error ccfreetypefont: initglyphs (const char * Text) is implemented as follows:

 1 FT_Error CCFreeTypeFont::initGlyphs(const char* text)  2 { 3     FT_Error error = 0; 4     std::stringstream stringStream(text); 5     std::string line; 6     vector<std::string> lines; 7     vector<std::string> words; 8  9     m_textWidth = 0;10     m_textHeight = 0;11     // the height of a line of text based on the max height of a glyph in the font size12     m_lineHeight = ((m_face->size->metrics.ascender) >> 6) - ((m_face->size->metrics.descender) >> 6);13 14     m_lines.clear();15 16     while(std::getline(stringStream, line) && !error) 17     {18       /*  newLine();19 20         std::size_t prev = 0, pos;21         while ((pos = line.find_first_of(" ", prev)) != std::string::npos)22         {23             if (pos > prev)24             {25                 addWord(line.substr(prev, pos-prev));26             }27             prev = pos + 1;28         }29         if (prev < line.length())30         {31             addWord(line.substr(prev, std::string::npos));32         }33         endLine();34         */35         addLine(line);36     }37 38     return error;39 }


Thanks again http://blog.csdn.net/hopingwhite/article/details/38414917

Reprinted please indicate the source: http://www.cnblogs.com/zouzf/p/3985330.html

 

Cocos2d-x project porting to WP8 Series 8: cclabelttf displays Chinese without line breaks

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.