Ucgui Chinese character processing problems
Some friends asked me questions about Chinese Character Processing When I used ucgui. Now I have written it down. In fact, I have used this version to handle Chinese characters very well.
Ucgui version 3.94
Applications of this version and above support dual-byte processing, such as Chinese and Japanese.
I have handled three problems in this version. Basically, all Chinese characters can be processed, regardless of display, modification, and input method.
1: Chinese Character Library
To display double-byte Chinese characters, you only need to use the corresponding tools to convert a font to a dot matrix.
The corresponding font of the specified control can be displayed, but there is another problem. Although it can be displayed normally, when the control is input using Chinese characters to edit, move the cursor, we can see that moving is half a Chinese character, so there is a problem. This problem occurs when an error occurs when calculating the number of characters.
We also need to do the following:
2: Call gui_uc_setencodegbk ()
You can call gui_uc_setencodegbk () at the beginning of the program. In fact, it is a pointer to initialize gui_context.puc_api, which contains the width and size of the calculated characters.
The following code:
/*************************************** ******************************
*
* _ Api_table
*/
Static const gui_uc_enc_apilist _ api_table_gbk = {
_ Getcharcode,/* return character code as 2010( Unicode )*/
_ Getcharsize,/* return size of character: 1/2/3 */
_ Calcsizeofchar,/* return size of character: 1/2/3 */
_ Encode/* encode character into 1/2/3 bytes */
};
/*************************************** ******************************
*
* Exported code
*
**************************************** ******************************
*/
/*************************************** ******************************
*
* Gui_uc_setencodegbk
*/
Void gui_uc_setencodegbk (void ){
Gui_lock ();
Gui_context.puc_api = & _ api_table_gbk;
Gui_unlock ();
}
3: handle a bug
After the above processing, the edit and other controls can be normally displayed and modified into Chinese characters.
However, I found a bug during my use. When I modified the value of the edit control several times (including Chinese characters), garbled characters appeared at the end of the character. Normal characters are displayed.
Through tracking, you need to change the method:
/*************************************** ******************************
*
* Guicharline. c gui_getlinedistx
*
* This routine is used to calculate the length of a line in pixels.
*/
Int GUI _ getlinedistx (const char gui_uni_ptr * s, int maxnumchars ){
Int Dist = 0;
If (s ){
2010char;
# If gui_compiler_supports_fp
If (gui_context.pafont-> pafencode ){
Return gui_context.pafont-> pafencode-> pfgetlinedistx (S, maxnumchars );
}
# Endif
While (-- maxnumchars> = 0 ){
Char = gui_uc _ getcharcodeinc (& S );
If (char
= 0) // jhting: 2012-09-26 edit _ display GBK bug
Break;
Dist + = gui_getchardistx (char );
}
}
Return Dist;
}
The red font is added by me. The length transferred in this method is calculated by a single byte. If the double byte is not considered, the value after the end of the string will appear in the while, such controls as edit display messy characters.