RichText-rich line feed for underlying modification of cocos2d-x
For more information about character encoding, see the previous blog: string and encoding format.
Here we use cocos2dx 2.2.3. The underlying RictText line feed mechanism cannot meet Chinese line breaks and needs to be modified. Because in 3. the version of x has been optimized, and the project will be changed to 3 in the future. so here we only make a record to analyze the idea of the underlying line feed mechanism for learning.
Principle of RichText line feed:
Take Text wrap as an example (the same picture). First Use a label without line breaks to calculate the total length of the label.L, And then with the width set by the programWComparison, w < p=""> <>
[Cpp]
- Void RichText: handleTextRenderer (const char * text, const char * fontName, float fontSize, const ccColor3B & color, GLubyte opacity)
- {
- /* Label without changing rows */
- CCLabelTTF * textRenderer = CCLabelTTF: create (text, fontName, fontSize );
- /* Label width without changing rows */
- Float textRendererWidth = textRenderer-> getContentSize (). width;
- /* Use the set size-label width */
- _ LeftSpaceWidth-= textRendererWidth;
- If (_ leftSpaceWidth <0.0f)
- {
- /* If you want to wrap a line, you need to extract the original string proportionally: Calculate the proportion of the excess width */
- Float overstepPercent = (-_ leftSpaceWidth)/textRendererWidth;
- Std: string curText = text;
- /* Calculate the length of the string */
- Int stringLength = curText. length ();
- /* 1-the proportion exceeded = Current (set the proportion of width) to intercept the string */
- Int leftLength = stringLength * (1.0f-overstepPercent );
- /* Use the string function of the stl standard library to intercept the string */
- Std: string leftWords = curText. substr (0, leftLength );
- Std: string cutWords = curText. substr (leftLength, curText. length ()-1 );
- /* If the string is not empty, the label of the first line is drawn */
- If (leftLength> 0)
- {
- CCLabelTTF * leftRenderer = CCLabelTTF: create (leftWords. substr (0, leftLength). c_str (), fontName, fontSize );
- LeftRenderer-> setColor (color );
- LeftRenderer-> setOpacity (opacity );
- PushToContainer (leftRenderer );
- }
-
- /* Open a new row and perform recursive Processing */
- AddNewLine ();
- HandleTextRenderer (cutWords. c_str (), fontName, fontSize, color, opacity );
- }
- Else
- {
- TextRenderer-> setColor (color );
- TextRenderer-> setOpacity (opacity );
- PushToContainer (textRenderer );
- }
Here, the focus is on the string truncation method. The built-in substr cannot well process multi-byte character truncation. Chinese characters may contain garbled characters, so use the function you have written to implement it, this function can be found online, but there is still a problem. I made some modifications and changed the original substr to the custom "utf8_substr" function:
[Cpp]
- /* The parameters are as follows:
- * Str: original string
- * Start: start position of the string to be dropped (the start position may not be accurate and can be adjusted by judgment)
- * Leng: the length of the clip (the same length may not be accurate)
- */
- Static std: string utf8_substr (const std: string & str, unsigned long start, unsigned long leng)
- {
- If (leng = 0)
- {
- Return;
- }
- Unsigned long c, I, strLen, minIdx = std: string: npos, actualLength = std: string: npos;
- // The start position of the valid (ungarbled) and the original truncation length of the valid value have an offset difference with the parameter
- Long startOffset, lenOffset;
- For (I = 0, strLen = str. length (); I <= strLen; I ++)
- {
- // I redirects based on the number of bytes occupied by characters. When UTF-8 is a Chinese character, the I value is 0, 3, 6, 9 .....
- If (I <= start)
- {
- MinIdx = I; // ensure that the minimum index is on the initial bytes of a character (the Chinese character contains three bytes, which indicates the first byte)
- StartOffset = start-minIdx;
- }
-
- If (I <= start + leng)
- {
- LenOffset = start + leng-I;
- ActualLength = leng + abs (startOffset)-abs (lenOffset); // put excess bytes in the next segment for processing
- }
-
- /* Determine the range in the encoding table of all characters, which occupies several bytes */
- C = (unsigned char) str [I];
- If (c <= 127) I + = 0;
- Else if (c & 0xE0) = 0xC0) I + = 1;
- Else if (c & 0xF0) = 0xE0) I ++ = 2;
- Else if (c & 0xF8) = 0xF0) I + = 3;
- Else return; // invalid utf8
- }
- If (minIdx = std: string: npos | actualLength = std: string: npos)
- {
- Return;
- }
- // CCLOG (result = % s, str. substr (minIdx, actualLength). c_str ());
- Return str. substr (minIdx, actualLength );
- }
Character encoding table:
Unicode symbol range | UTF-8 encoding method
(Hexadecimal) | (Binary)
-------------------- + ---------------------------------------------
0000 0000-0000 007F | 0 xxxxxxx
0000 0080-0000 07FF | 110 xxxxx 10 xxxxxx
0000 0800-0000 FFFF | 1110 xxxx 10 xxxxxx 10 xxxxxx
0001 0000-0010 FFFF | 11110xxx 10 xxxxxx 10 xxxxxx 10 xxxxxx
Add the above utf8_substr function to RichText. cpp and replace it with the place where substr is used.