Opencascade Chinese text rendering

Source: Internet
Author: User
Tags vector font

Opencascade Chinese text rendering

[Email protected]

Abstract.Opencascade uses advanced text rendering powered by ftgl library. the FreeType provides vector text rendering, As a result the text can be rotated and zoomed without quality loss. freeType also support Unicode charset. the paper focus on the Chinese text rendering.

Key words.Opencascade, FreeType, Chinese text, Chinese characters, Unicode

1. Introduction

OpenGL does not provide direct text rendering support. A common two-dimensional text solution is to use gldrawpixels () to display bitmap fonts, the premise is that the user has generated a series of character libraries in the form of locations in advance, which is also a common practice of many early computer games. The main problem of using bitmap to draw text is that the image pattern in the display process cannot be controlled. Because the size of the text image is always certain, the deformation after scaling is obvious. If the image is scaled in real time based on the viewpoint, a large amount of system resources will be consumed. A better solution is to use textures to express vector-type text. The advantage of vector text is that each font is described using a mathematical formula, and smooth curves are used to connect strokes. Therefore, if Vector text is expressed by texture, as long as the preset texture resolution meets the needs, when the texture surface is scaled or the user's viewpoint is changed, will not cause obvious text distortion and deformation.

The famous open-source cross-platform development library FreeType is the first release of vector text processing. This is a professional font data parsing tool that can Parse Multiple vector font formats such as TrueType and type1 and provide them to user programs through a unified function interface. FreeType does not support text layout and graphical display. Therefore, you can directly apply the result of parsing the font file to OpenGL.

The text display of opencascade uses the FreeType library to convert the text into a vector image. Therefore, any scaling of the text will not affect the display quality. It also supports Unicode text display, including Chinese text display. This article mainly introduces the precautions for displaying Chinese Characters in opencascade, and also introduces the function of converting text into topods_shape in opencascade.

2. Render Chinese Text

Opencascade has the command vdrawtext for displaying text in draw test harness, as shown in:

Figure 2.2 text in draw test harness

Shows the implemented TCL command:

Figure 2.2 draw text TCL command

Figure 2.3 vdrawtext command

From the vdrawtext command, we can see that the last parameter is about multi-byte string display processing. Enter the following command to display a string containing Chinese characters:

Vdrawtext Hello opencascade 100 300-400 000 255 255 0 0 000 1 50 1 simsun 1

The result is as follows:

Figure 2.4 render Chinese text by vdrawtext command

The figure shows that the result is incorrect. Find the source code of the vdrawtext command implementation part and find the implementation code in the veiwertest_objectcommands.cxx file. After modifying the string conversion algorithm, the Code is as follows:

static int VDrawText (Draw_Interpretor& di, Standard_Integer argc, const char** argv){  // Check arguments  if (argc < 14)  {    di<<"Error: "<<argv[0]<<" - invalid number of arguments\n";    di<<"Usage: type help "<<argv[0]<<"\n";    return 1; //TCL_ERROR  }  Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();  // Create 3D view if it doesn‘t exist  if ( aContext.IsNull() )  {    ViewerTest::ViewerInit();    aContext = ViewerTest::GetAISContext();    if( aContext.IsNull() )    {      di << "Error: Cannot create a 3D view\n";      return 1; //TCL_ERROR    }  }  // Text position  const Standard_Real X = Draw::Atof(argv[2]);  const Standard_Real Y = Draw::Atof(argv[3]);  const Standard_Real Z = Draw::Atof(argv[4]);  const gp_Pnt pnt(X,Y,Z);  // Text color  const Quantity_Parameter R = Draw::Atof(argv[5])/255.;  const Quantity_Parameter G = Draw::Atof(argv[6])/255.;  const Quantity_Parameter B = Draw::Atof(argv[7])/255.;  const Quantity_Color aColor( R, G, B, Quantity_TOC_RGB );  // Text alignment  const int hor_align = Draw::Atoi(argv[8]);  const int ver_align = Draw::Atoi(argv[9]);  // Text angle  const Standard_Real angle = Draw::Atof(argv[10]);  // Text zooming  const Standard_Boolean zoom = Draw::Atoi(argv[11]);  // Text height  const Standard_Real height = Draw::Atof(argv[12]);  // Text aspect  const Font_FontAspect aspect = Font_FontAspect(Draw::Atoi(argv[13]));  // Text font  TCollection_AsciiString font;  if(argc < 15)    font.AssignCat("Courier");  else    font.AssignCat(argv[14]);  // Text is multibyte  const Standard_Boolean isMultibyte = (argc < 16)? Standard_False : (Draw::Atoi(argv[15]) != 0);  // Read text string  TCollection_ExtendedString name;  if (isMultibyte)  {      /* eryar modified 20140817 11:11    const char *str = argv[1];    while ( *str || *(str+1)==‘\x0A‘ || *(str+1)==‘\x0B‘ || *(str+1)==‘\x0C‘ || *(str+1)==‘\x0D‘                 || *(str+1)==‘\x07‘ || *(str+1)==‘\x08‘ || *(str+1)==‘\x09‘ )    {      unsigned short c1 = *str++;      unsigned short c2 = *str++;      if (!c2) break;      name += (Standard_ExtCharacter)((c1 << 8) | c2);    }    */      Resource_Unicode::ConvertGBToUnicode(argv[1], name);  }  else  {    name += argv[1];  }  if (name.Length())  {    Handle(MyTextClass) myT = new MyTextClass(name,pnt,aColor,hor_align,ver_align,angle,zoom,height,aspect,font.ToCString());    aContext->Display(myT,Standard_True);  }  return 0;}

It is mainly a multi-byte string. The resource_unicode: convertgbtounicode () function is used to convert the string. After modification, enter the preceding command to display the Chinese font:

Figure 2.5 render Chinese text by vdrawtext command

To sum up, we can see from the code in draw that the following points should be noted to display Chinese Characters in opencascade:

V because opencascade does not provide a class for directly displaying text, you need to derive a text display class from ais_interactiveobject and reload the related function compute ();

To convert a V string, use resource_unicode: convertegbtounicode () to convert a Chinese string to a unicode string;

V must select the correct Chinese font; otherwise, the display is incorrect.

Figure 2.6 A Chinese quote

3. convert text to topods_shape

With the help of the treetype library opencascade, you can convert the text into a spline and generate topods_shape, that is, 3D text effect. The related draw command is text2brep, as shown in:

Figure 3.1 3D text in draw test harness

The TCL script that generates the above results is as follows:

Text2brep text2d [email protected] 163.com times-Roman 18 bold composite = 0
Prism text text2d 0 0 2
Vdisplay text
Vsetdispmode 1
Vfit

4. Conclusion

Opencascade uses FreeType to achieve high-quality text display. Because it is a vector image, arbitrary scaling does not affect the quality of text.

When displaying Chinese Characters in opencascade, You must select the correct conversion function when converting strings to Unicode, and select the correct font format, that is, the Chinese font. This document uses simsun as an example.

Opencascade can also convert text to topods_shape to display 3D fonts.

To sum up, we can see that the FreeType library is still very powerful.

5. References

1. Wang Rui, Qian Xuelei, and openscenegraph 3D rendering engine design and practice, Tsinghua University Press

2. How to display Chinese characters in the 2D window of opencascade,

Http://www.cadcaecam.com/forum.php? MoD = viewthread & tid = 15444.

3. opencascade draw test harness code

 

Pdf version: opencascade Chinese text rendering

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.