A Double-Buffering Text Rendering Method Based on FreeType
Through the double-cache text rendering method, large texture storage areas and texture update cache areas are used to manage character textures, solving the problem of low rendering efficiency caused by multi-text drawing and realizing fast rendering of text.
Patent Information
- Application Number
- CN202211319499.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-26
- Publication Date
- 2025-07-29
- Estimated Expiration
- 2042-10-26
AI Technical Summary
On low-performance systems, multi-text drawing leads to low rendering efficiency, especially poor performance in text display speed.
The double-cache text rendering method is adopted, including large texture storage area, texture update cache area and bitmap data cache area. The OpenGL function is used to generate and update character textures, and the rendering process is managed through the state value to reduce frequent changes in OpenGL state.
Improves text rendering performance, reduces the rendering process, and improves text drawing speed and efficiency.
Smart Images

Figure CN115631262B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of graphic rendering application software, and particularly relates to a double-buffer text rendering method based on FreeType. Background Art
[0002] Due to the increasing demand for text display, the rendering efficiency is low when a large amount of text is drawn simultaneously. Especially on systems with low performance, the problem of slow text display speed is more obvious. How to improve text rendering performance has become an urgent problem to be solved. Summary of the Invention
[0003] In view of this, the embodiments of the present application provide a double-buffer text rendering method based on FreeType, which realizes fast rendering of text and solves the problem of low rendering efficiency caused by simultaneous drawing of a large amount of text.
[0004] The embodiments of the present application provide the following technical solutions: A double-buffer text rendering method based on FreeType, including:
[0005] Setting a large texture storage area, a texture update buffer area, and a bitmap data buffer area during program initialization; the large texture storage area is used to store the character textures of the drawn text, the texture update buffer area is used to store the character codes of the drawn text, and the bitmap data buffer area is used to store the bitmap data objects corresponding to the character codes;
[0006] Generating corresponding bitmap data by using FreeType according to the character code of the text to be drawn and the set font size, and converting the bitmap data into a character texture by using OpenGL functions; storing the character texture in the large texture storage area, storing the character code of the text to be drawn in the texture update buffer area, and storing the bitmap data in the corresponding bitmap data object in the bitmap data buffer area;
[0007] Setting the status value of the bitmap data object according to whether the text has been drawn and whether the character texture of the text is in the large texture storage area. If the text has not been drawn, the status value is set to the initial value 0; if the text has been drawn and the character texture is in the large texture storage area, the status value is set to 1; if the text has been drawn but the character texture has been removed from the large texture storage area, the status value is set to 2;
[0008] Indexing to the bitmap data object in the bitmap data buffer area according to the character code of the text to be drawn, and obtaining the bitmap data, the status value, and the position information in the large texture storage area;
[0009] When the status value is 1, the character texture can be directly obtained according to the position information, and the corresponding character code can be found in the texture update buffer, and the character code is updated to the first position of the texture update buffer;
[0010] When the status value is 0, the bitmap data of the text is obtained using FreeType, and then the bitmap data is used to generate a character texture using OpenGL;
[0011] When the status value is 2, the bitmap data in the bitmap data object is taken out, and the bitmap data is used to generate a character texture using OpenGL;
[0012] The character texture is texture mapped using OpenGL functions to complete the rendering of the text.
[0013] According to one embodiment, the storage capacities of the large texture storage area and the texture update buffer are equal, and the character textures stored in the large texture storage area correspond one-to-one with the character codes stored in the texture update buffer.
[0014] According to one embodiment, it further includes: when the status value is 0 or 2, updating the bitmap data buffer and the texture update buffer according to the space status of the large texture storage area, specifically including:
[0015] When the space status of the large texture storage area is not full, the generated character texture is stored in the large texture storage area, the position attribute of the bitmap data object corresponding to the text is updated according to the stored position information, and its status value is set to 1, and the corresponding character code is placed at the first position of the texture update buffer;
[0016] When the space status of the large texture storage area is full, first take out the last character code in the texture update buffer, and index to the corresponding bitmap data object in the bitmap data buffer according to the character code, set its status value to 2, then find the position information of the character texture in the large texture storage area according to the position attribute of the bitmap data object, replace it with the newly generated character texture in the large texture storage area, then set the position attribute of the bitmap data object corresponding to the newly drawn text according to the position information, and set its status value to 1, and finally place the character code of the newly drawn text at the first position of the texture update buffer.
[0017] According to one embodiment, it further includes:
[0018] When the status value is set to 0, the bitmap data and position information in the bitmap data object of the text are invalid;
[0019] When the status value is set to 1, the bitmap data and position information in the bitmap data object of the text are valid;
[0020] When the status value is set to 2, the position information in the bitmap data object of this text is invalid, and the bitmap data is valid.
[0021] According to one embodiment, when storing the character texture in the large texture storage area, the width and height of the character texture are set to the size of the font.
[0022] According to one embodiment, the character texture is stored in the large texture storage area based on the starting position coordinates indicating the storage of the character texture, and the starting position coordinates increase in steps of the font size.
[0023] According to one embodiment, it further includes: applying for a texture cache array during program initialization, and the texture cache array includes: a texture coordinate array, a color array, and a drawing position coordinate array;
[0024] After the character texture is generated, first store the position of the character texture in the large texture storage area, the text color, and its drawing position on the screen into the texture coordinate array, the color array, and the drawing position coordinate array respectively; then use the texture cache array to perform real-time texture mapping on each character texture using OpenGL functions and uniformly render all the text.
[0025] Compared with the prior art, the embodiment of the present invention reduces the rendering process of text, avoids excessive frequent changes of OpenGL states during real-time texture mapping of character textures, thereby improving the ability of fast text rendering and the performance of text rendering. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings required for the embodiments. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0027] Figure 1 It is the flowchart of double-buffer text rendering of the present invention;
[0028] Figure 2 It is the flowchart of bitmap data generation of the present invention;
[0029] Figure 3 It is the flowchart of the creation and drawing of the texture of the present invention;
[0030] Figure 4 It is the schematic diagram of character texture storage of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0031] The following will describe the embodiments of the present application in detail with reference to the drawings.
[0032] It should be noted that, without conflict, the embodiments in the present application and the features in the embodiments may be combined with each other. The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with the embodiments. The technical solutions of the present invention will be clearly and completely described. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without making creative efforts fall within the scope of protection of the present invention.
[0033] As Figure 1 shown, an embodiment of the present invention provides a double-buffered text rendering method based on FreeType, including:
[0034] Setting a large texture storage area, a texture update buffer area, and a bitmap data buffer area during program initialization; the large texture storage area is used to store the character textures of the drawn text, the texture update buffer area is used to store the character codes of the drawn text, and the bitmap data buffer area is used to store bitmap data objects corresponding to the character codes; wherein, the storage capacities of the large texture storage area and the texture update buffer area are equal, and the character textures stored in the large texture storage area and the character codes stored in the texture update buffer area are in one-to-one correspondence.
[0035] According to the character code of the text to be drawn and the set font size, generating corresponding bitmap data using FreeType, and converting the bitmap data into a character texture using OpenGL functions; storing the character texture in the large texture storage area, storing the character code of the text to be drawn in the texture update buffer area, and setting a bitmap data object for each character code of the text to be drawn and storing it in the bitmap data buffer area; the bitmap data object includes attributes such as the bitmap data of the text, the status value indicating whether its character texture is in the large texture, and the position information in the large texture.
[0036] Setting a status value according to whether the text has been drawn and whether the character texture of the text is in the large texture storage area. If the text has not been drawn, the status value is set to 0, and the bitmap data and position information in the bitmap data object are invalid; if the text has been drawn and the character texture is in the large texture storage area, the status value is set to 1, and the bitmap data and position information in the bitmap data object are valid; if the text has been drawn but the character texture has been removed from the large texture storage area, the status value is set to 2, and the position information in the bitmap data object is invalid, but the bitmap data is valid.
[0037] When rendering text, indexing to the bitmap data object in the bitmap data buffer area according to the character code of the text to be drawn, and obtaining the bitmap data, the status value, and the position information in the large texture storage area;
[0038] When the status value is 1, the character texture can be directly obtained according to the position information, and the corresponding character code can be found in the texture update buffer, and the character code is updated to the first position of the texture update buffer;
[0039] When the status value is 0, use FreeType to obtain the bitmap data of the text, and then use OpenGL to generate the character texture from the bitmap data;
[0040] When the status value is 2, take out the bitmap data in the bitmap data object, and use OpenGL to generate the character texture from the bitmap data;
[0041] Use OpenGL functions to perform texture mapping on the character texture to complete the rendering of the text.
[0042] When the status value is 2, the bitmap data of the bitmap data object can be directly taken out, avoiding the process of generating bitmap data by FreeType, and then using OpenGL to generate the character texture from the bitmap data. When the status is set to 1, the character texture can be directly obtained, which effectively improves the rendering efficiency.
[0043] When the status value is 0 or 2, update the bitmap data buffer and the texture update buffer according to the space status of the large texture storage area, specifically including:
[0044] When the space status of the large texture storage area is not full, store the generated character texture in the large texture storage area, update the position attribute of the bitmap data object corresponding to the text according to the stored position information and set its status value to 1, and place the corresponding character code at the first position of the texture update buffer;
[0045] When the space status of the large texture storage area is full, first take out the last character code in the texture update buffer, and index to the corresponding bitmap data object in the bitmap data buffer according to the character code, set its status value to 2, then find the position information of the character texture in the large texture storage area according to the position attribute of the bitmap data object, replace it with the newly generated character texture in the large texture storage area, then set the position attribute of the bitmap data object corresponding to the newly drawn text according to the position information, and set its status value to 1, and finally place the character code of the newly drawn text at the first position of the texture update buffer.
[0046] In one embodiment of the present application, when the character texture is stored in the large texture storage area, the width and height of the character texture are set to the size of the font, rather than the actual width and height of the character in the glyph data. This can avoid the situation where, when the texture is updated, the actual width and height of the replaced character is smaller than the actual width and height of the new texture, other surrounding character textures may be covered; if the actual width and height of the replaced character is larger than the actual width and height of the new texture, the replaced character texture will remain. The character texture is stored in the large texture based on the coordinates indicating the starting position of the character texture storage, and then the starting position coordinates are increased in steps of the font size. The corresponding character texture needs to be retrieved during the texture mapping stage, and at this time, it needs to be retrieved based on the actual width and height of the character texture, so that the accuracy of the text drawing can be guaranteed.
[0047] In one embodiment of the present application, a texture cache array is applied for when the program is initialized, and the texture cache array includes: a texture coordinate array, a color array, and a drawing position coordinate array;
[0048] When the character texture is generated, the position of the character texture in the large texture storage area, the text color and the position of its drawing on the screen are first stored in the texture coordinate array, the color array and the drawing position coordinate array respectively; when the character textures of the text to be drawn are all generated, the texture cache array has the corresponding information of these texts, and then the texture cache array is used to perform texture mapping process, and the OpenGL function is used to perform real-time texture mapping on each character texture, and all texts are rendered uniformly, reducing the problem of long text rendering time caused by frequent switching of texture switches during text drawing.
[0049] The method of the present invention generates bitmap data based on FreeType, then uses OpenGL functions to convert the bitmap data into character textures, and finally uses OpenGL functions to perform texture mapping to complete the rendering of text. The present invention uses a large empty texture to save the drawn character texture, and then uses a cache to implement the update of the large texture character texture, and another cache to save the generated bitmap data and other information. When the text is drawn again, the character texture of the text in the large texture is retrieved or the bitmap data of the text is retrieved, thereby simplifying the text rendering process and improving the rendering rate. The present invention also constructs a texture coordinate array, a color array, and a drawing position coordinate array for storing the position of the character texture corresponding to the text in the large texture, the text color, and the position of the text drawn on the screen. By uniformly rendering all text, the number of excessive OpenGL state changes during real-time texture mapping of the character texture is reduced, thereby improving the rendering performance.
[0050] The specific implementation of the present invention is further described in detail below with reference to the accompanying drawings and specific examples.
[0051] The double-buffer text rendering method of this embodiment is as follows:
[0052] (1) Use FreeType to read the simhei.ttf font file and generate a font object according to the set font size (16);
[0053] (2) Use the FT_Get_Char_Index function to index the glyph index in the font object according to the character encoding of the text to be drawn, then load the corresponding glyph image through the FT_Load_Glyph function, and finally render the outline into bitmap data through the FT_Render_Glyph function. The flowchart for generating the bitmap data is as shown in Figure 2 Shown. Use the glTexImage2D function of OpenGL to generate character textures from the bitmap data. The width and height of the font texture are both the font size (16). The flowchart for creating and drawing the texture is as shown in Figure 3 Shown;
[0054] (3) When the program is initialized, apply for a large empty texture with a size of 1024 * 1024, apply for a list of texture update caches with a size of 64 * 64, and apply for a bitmap data cache. The size of the bitmap data cache should not be less than the number of glyphs in the font object (65536), and its index is the character encoding;
[0055] (4) Use the position coordinates x and y to represent the position where the character texture is stored in the large texture, initially 0;
[0056] (5) When drawing text, use the template function of the list to place its character encoding at the front, store the character texture at the x.y position of the large texture and update the values of x and y according to the font size. The schematic diagram of the storage of the character texture in the large texture is as shown in Figure 4 Shown. There will be some blank areas when the character texture is stored in an area larger than itself in width and height. Find the bitmap data object in the bitmap data cache according to the character encoding, and update its bitmap data, status value, and position in the large texture;
[0057] (6) Store the position of the character texture of the text to be drawn in the large texture, the text color, and its position drawn on the screen into the texture cache array;
[0058] (7) Enable the texture, set the texture environment, and perform unified rendering according to the texture cache array.
[0059] As described above, it is only the specific implementation manner of this application, but the protection scope of this application is not limited thereto. Any changes or substitutions that can be easily thought of by those skilled in the art within the technical scope disclosed by this application should be covered within the protection scope of this application. Therefore, the protection scope of this application should be subject to the protection scope of the claims.
Claims
1. A double-buffered text rendering method based on FreeType, characterized in that Including: Set up a large texture storage area, a texture update buffer, and a bitmap data buffer during program initialization; The large texture storage area is used to store the character textures of the drawn text, the texture update buffer is used to store the character codes of the drawn text, and the bitmap data buffer is used to store the bitmap data objects corresponding to the character codes; According to the character code of the text to be drawn and the set font size, use FreeType to generate the corresponding bitmap data, and use OpenGL functions to convert the bitmap data into a character texture; store the character texture in the large texture storage area, store the character code of the text to be drawn in the texture update buffer, and store the bitmap data in the corresponding bitmap data object in the bitmap data buffer; Set the status value of the bitmap data object according to whether the text has been drawn and whether the character texture of the text is in the large texture storage area. If the text has not been drawn, the status value is the initial value 0; if the text has been drawn and the character texture is in the large texture storage area, the status value is set to 1; if the text has been drawn but the character texture has been removed from the large texture storage area, the status value is set to 2; Index to the bitmap data object in the bitmap data buffer according to the character code of the text to be drawn, and obtain the bitmap data, status value, and position information in the large texture storage area; When the status value is 1, the character texture can be directly obtained according to the position information, and the corresponding character code can be found in the texture update buffer, and the character code is updated to the first position of the texture update buffer; When the status value is 0, use FreeType to obtain the bitmap data of the text, and then use OpenGL to generate a character texture from the bitmap data; When the status value is 2, take out the bitmap data in the bitmap data object, and use OpenGL to generate a character texture from the bitmap data; Use OpenGL functions to perform texture mapping on the character texture to complete the rendering of the text; Also including: When the status value is 0 or 2, update the bitmap data buffer and the texture update buffer according to the space status of the large texture storage area. Specifically, it includes: When the space status of the large texture storage area is not full, store the generated character texture in the large texture storage area, update the position attribute of the bitmap data object corresponding to the text according to the stored position information and set its status value to 1, and place the corresponding character code at the first position of the texture update buffer; When the space status of the large texture storage area is full, first take out the last character code in the texture update buffer, and index to the corresponding bitmap data object in the bitmap data buffer according to the character code, set its status value to 2, then find the position information of the character texture in the large texture storage area according to the position attribute of the bitmap data object, replace it with the newly generated character texture in the large texture storage area, then set the position attribute of the bitmap data object corresponding to the newly drawn text according to the position information, and set its status value to 1, and finally place the character code of the newly drawn text at the first position of the texture update buffer.
2. The double-buffer text rendering method according to claim 1, wherein The storage capacities of the large texture storage area and the texture update cache area are equal, and the character textures stored in the large texture storage area correspond one by one to the character encodings stored in the texture update cache area.
3. The double-buffer text rendering method according to claim 1, wherein It further includes: When the status value is set to 0, the bitmap data and position information in the bitmap data object of this text are invalid; When the status value is set to 1, the bitmap data and position information in the bitmap data object of this text are valid; When the status value is set to 2, the position information in the bitmap data object of this text is invalid and the bitmap data is valid.
4. The double-buffer text rendering method according to claim 1, wherein, It further includes: When storing the character texture in the large texture storage area, the width and height of the character texture are set to the size of the font.
5. The double-buffer text rendering method according to claim 4, wherein The character texture is stored in the large texture storage area based on the starting position coordinates indicating the storage of the character texture, and the starting position coordinates increase in steps of the font size.
6. The double-buffer text rendering method according to claim 1, wherein, It further includes: Apply for a texture cache array during program initialization. The texture cache array includes: a texture coordinate array, a color array, and a drawing position coordinate array; After the character texture is generated, first store the position of the character texture in the large texture storage area, the text color, and its drawing position on the screen into the texture coordinate array, color array, and drawing position coordinate array respectively; then use the texture cache array to perform real-time texture mapping on each character texture using OpenGL functions and uniformly render all the text.
Citation Information
Patent Citations
Method and system for generating inverted index and retrieving by using inverted index, and storage medium
CN110825936A
Data processing method and device
CN110908996A