Character processing method and device
The character processing method addresses the limitations of browser text input in canvas scenarios by rendering and editing characters with rich styles and editing capabilities, enhancing web games and image editors.
Patent Information
- Application Number
- CN202111007595.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-30
- Publication Date
- 2024-12-03
- Estimated Expiration
- 2041-08-30
AI Technical Summary
The built-in text input method provided by browsers cannot be used in canvas-based character input scenarios, such as web games or web image editors, as it does not support rich character styles, and open source rendering engines lack text input capabilities, requiring significant development effort.
A character processing method that includes receiving characters, determining attribute information, splicing characters to generate a string, and rendering it on a canvas layer, supporting character cursor, selection, and editing with shortcut keys, while accommodating text rotation and scaling.
Enables complex character styles and editing in canvas-based applications, facilitating integration into web games and image editors with support for text editing and typesetting.
Smart Images

Figure CN113705156B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of Internet technology, and in particular to a character processing method, a character processing device, a computing device, and a computer-readable storage medium. Background Art
[0002] In web games or web image editors based on canvas, there are often scenarios where characters need to be input. In these scenarios, characters may have a variety of formats, such as color, stroke, shadow, text texture, border, etc., and may also be rotated and resized. These contents are directly rendered on the canvas. When editing and modifying characters, the editing program needs to display a text cursor, have the ability to select text, and support shortcut keys for text input. However, the built-in text input method provided by the browser is not based on canvas, and cannot be used in the above scenarios, nor can it restore rich character styles. If an open source rendering engine is introduced, the text input capability cannot be used alone, and personalized development based on the rendering engine is required, which consumes a lot of work. Summary of the invention
[0003] In view of this, an embodiment of the present application provides a character processing method. The present application also relates to a character processing device, a computing device, and a computer-readable storage medium to solve the problem that the text input method provided by the browser in the prior art cannot be implemented in a canvas-based character input scenario.
[0004] According to a first aspect of an embodiment of the present application, a character processing method is provided, comprising:
[0005] receiving at least one character to be displayed;
[0006] Determine character attribute information of each character to be displayed;
[0007] splicing the at least one character to be displayed to generate a character string to be displayed according to the character attribute information of each character to be displayed, and acquiring the character string typesetting information of the character string to be displayed;
[0008] The rendering result corresponding to the character string to be displayed is generated by rendering in the target canvas layer according to the character string to be displayed and the character string layout information.
[0009] According to a second aspect of an embodiment of the present application, there is provided a character processing device, comprising:
[0010] A receiving module, configured to receive at least one character to be displayed;
[0011] A determination module, configured to determine character attribute information of each character to be displayed;
[0012] a splicing module configured to splice the at least one character to be displayed to generate a character string to be displayed according to the character attribute information of each character to be displayed, and obtain the character string layout information of the character string to be displayed;
[0013] The rendering module is configured to render and generate a rendering result corresponding to the string to be displayed in a target canvas layer according to the string to be displayed and the string layout information.
[0014] According to a third aspect of an embodiment of the present application, a computing device is provided, comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein the processor implements the steps of the character processing method when executing the computer instructions.
[0015] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores computer instructions, and when the computer instructions are executed by a processor, the steps of the character processing method are implemented.
[0016] The character processing method provided by the present application receives at least one character to be displayed; determines the character attribute information of each character to be displayed; splices the at least one character to be displayed to generate a character string to be displayed according to the character attribute information of each character to be displayed, and obtains the string typesetting information of the character string to be displayed; renders the character string to be displayed and the string typesetting information in the target canvas layer to generate a rendering result corresponding to the character string to be displayed. The present application provides a set of character processing methods, a process of rendering and editing responses, realizes character typesetting and character editing of characters based on canvas, and supports complex character styles, supports editing under text rotation and scaling, and the method provided by the present application can be conveniently integrated into canvas applications to serve text editing and typesetting. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 is a flow chart of a character processing method provided by an embodiment of the present application;
[0018] Figure 2 is a schematic diagram of a rendering result coordinate system provided by an embodiment of the present application;
[0019] Figure 3 It is a processing flow chart of a character processing method applied to a web image editor provided by an embodiment of the present application;
[0020] Figure 4 is a structural schematic diagram of a character processing device provided by an embodiment of the present application;
[0021] Figure 5It is a structural block diagram of a computing device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0022] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0023] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more associated listed items.
[0024] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0025] First, the terms involved in one or more embodiments of the present application are explained.
[0026] Canvas: is an HTML element that can use scripts (usually JavaScript) to draw graphics.
[0027] In canvas-based web games and web image editors, there are often scenarios where characters need to be input. In these input scenarios, characters may have a variety of rich styles. The input method is provided by the input component and textarea component in the browser, or an open source drawing engine is implemented based on canvas. However, the input component in the browser is not based on canvas and cannot restore rich text styles. It is also difficult for open source rendering engines to apply the text input capability alone in applications.
[0028] Based on this, a character processing method is provided in the present application. The present application also relates to a character processing device, a computing device, and a computer-readable storage medium, which support character cursor, character selection, support editing shortcuts, and also support rich text styles. They can perform mouse selection and input under the conditions of screen rotation and zoom, and are easy to be implanted into a web game or a web image editor based on canvas. They will be described in detail one by one in the following embodiments.
[0029] Figure 1 The flowchart of a character processing method provided according to an embodiment of the present application is shown, which specifically includes the following steps:
[0030] Step 102: Receive at least one character to be displayed.
[0031] The character to be displayed is character information that needs to be displayed on the canvas layer (canvas). The character to be displayed can be Chinese, English, Japanese, Korean, numbers, and so on.
[0032] The character to be displayed can be a character related to the business. For example, in a web game, it is required to display the player's nickname "Zhang San abc" in the upper right corner of the screen. Then, "Zhang San abc" is the character to be displayed. The client receives the character to be displayed sent by the server and displays it in the client after text layout and text rendering.
[0033] In a specific embodiment provided by the present application, taking a web game as an example, game data sent by the game server is received, and the character to be displayed "Zhang San abc" is included in the game data.
[0034] Step 104: Determine the character attribute information of each character to be displayed.
[0035] Each character to be displayed has corresponding character attribute information, such as character position information, character color, font size, font, maximum width, stroke, shadow, horizontal alignment, numerical alignment, line spacing, and so on. According to the character attribute information of each character, it can be used to determine the style of each character during the rendering process.
[0036] In practical applications, after obtaining the character to be displayed, it is necessary to determine the display area information of the character to be displayed. For example, in which area the character to be displayed needs to be displayed and how large the display area is. Therefore, it is necessary to determine the above information according to the character attribute information of each character to be displayed. Based on this, determining the character attribute information of each character to be displayed includes:
[0037] Measure the character width information of each character to be displayed;
[0038] Obtain the character drawing information of each character to be displayed.
[0039] After receiving the characters to be displayed, it is necessary to split the consecutive characters to be displayed into individual characters. For example, if the characters to be displayed are "Zhang San abc", after character splitting, the individual characters are determined to be "Zhang", "San", "a", "b", and "c". Then, use the measureText method of the web canvas to measure the width of each character, and obtain the character width information of each character to be displayed as the character drawing information of each character to be displayed. The role of measureText is a function to check the width of each character before outputting text on the canvas.
[0040] Step 106: Concatenate the at least one character to be displayed according to the character attribute information of each character to be displayed to generate a string to be displayed, and obtain the string layout information of the string to be displayed.
[0041] After obtaining the character attribute information of each character to be displayed, it is necessary to concatenate each character to be displayed according to the character attribute information to generate a string to be displayed. The string to be displayed specifically refers to the text information to be displayed on the canvas layer. The string layout information is the line break information, the position information of each character, the width information, the information of the surrounding matrix outside the text, and the information of the text box, etc. when the string to be displayed is output to the canvas layer.
[0042] Generating the string to be displayed and obtaining the string layout information of the string to be displayed facilitate providing accurate layout information when rendering the string to be displayed subsequently.
[0043] Specifically, obtaining the string layout information of the string to be displayed includes S1062 to S1068:
[0044] S1062. Obtain the character spacing information between adjacent characters to be displayed, the string attribute information of the string to be displayed, and the string coordinate information of the string to be displayed.
[0045] In practical applications, traverse each character to be displayed, obtain the character spacing information between adjacent characters to be displayed, and at the same time obtain the string attribute information of the string to be displayed and the string coordinate information of the string to be displayed. The character spacing information between adjacent characters to be displayed is used to determine the character distance information between any two adjacent characters to be displayed; the string attribute information includes the width information of the text box corresponding to the string, the number of lines information, etc., and whether the string to be displayed includes a line break character "\n"; the string coordinate information specifically refers to the position information of the string to be displayed on the canvas layer.
[0046] S1064. Determine the string line break information of the string to be displayed according to the character width information of each character to be displayed and the character spacing information between adjacent characters to be displayed.
[0047] After traversing each character to be displayed and obtaining the character spacing information between adjacent characters to be displayed, combined with the character width information of each character to be displayed, the string line break information of the string to be displayed can be determined, wherein the string line break information specifically refers to how many lines the string to be displayed needs to be displayed and how much content is displayed in each line.
[0048] Specifically, determining the string line break information of the string to be displayed according to the character width information of each character to be displayed and the character spacing information between adjacent characters to be displayed includes:
[0049] Each character to be displayed is concatenated in sequence to obtain a character string to be displayed;
[0050] Calculating the length of the concatenated character string of the character string to be displayed according to the character width information of the character string to be displayed and the character spacing information between adjacent characters to be displayed;
[0051] When the length of the concatenated character string is greater than a preset length threshold, a line break operation is performed on the character string to be displayed, and character string line break information is generated.
[0052] In actual applications, after obtaining the character width information of each character to be displayed and the character spacing information between adjacent characters to be displayed, each character to be displayed can be spliced according to the above information to obtain the character string to be displayed. During the splicing process, if the total width of the spliced characters to be displayed in a row and the sum of the character width information between the characters to be displayed in the row is greater than the preset length threshold, it is necessary to perform line break processing on the character string to be displayed. The preset length threshold can be set according to the length parameter, or it can be determined according to the string attribute information in S1062 above. For example, if the width of the text box corresponding to the string to be displayed is determined to be t in the string attribute information, then the preset length threshold is set to t. When the length of the string to be displayed exceeds t, a line break operation is performed on the string to be displayed, and the string line break information is generated and recorded.
[0053] In the above-mentioned string attribute information in S1062, it can also be determined whether the string to be displayed includes a line break character, wherein the line break character will also trigger a line break operation, so the method further includes:
[0054] In the case that the characters to be displayed include line break characters, a line break operation is performed on the character string to be displayed, and character string line break information is generated.
[0055] If the characters to be displayed include a line break character, a line break operation is also performed on the character string to be displayed according to the line break character, and character string line break information is generated and recorded.
[0056] The character string attribute information in the above S1062 also includes the number of lines of the text box corresponding to the character string to be displayed, so the method further includes:
[0057] Counting the number of character string rows of the character string to be displayed;
[0058] When the number of lines of the character string is greater than a preset line number threshold, the characters to be displayed after the line break character of the last line of the character string to be displayed are hidden.
[0059] The number of lines of the string to be displayed can be determined based on the line break information of the string to be displayed. The number of lines of the string specifically refers to the total number of lines of the string to be displayed. When the number of lines of the string is greater than the preset line number threshold, it means that the string to be displayed cannot be displayed in its entirety. The preset line number threshold can be determined based on the line number information in the string attribute information.
[0060] When the number of lines in a string is greater than a preset line number threshold, the characters to be displayed after the line break character in the last line of the string are hidden, and an ellipsis is added at the end of the last line to indicate that the content display is not complete.
[0061] S1066: Calculate character coordinate information of each character to be displayed according to the character string line break information, the character string attribute information, the character string coordinate information, and character spacing information of adjacent characters to be displayed.
[0062] After determining the string line break information, the character coordinate information of each character to be displayed is calculated by combining the width information of the text box corresponding to the string in the string attribute information, the number of lines, and other information, the string coordinate information, and the character spacing information of adjacent characters to be displayed. For example, the string coordinate information is p1 (x1, y1), the first character to be displayed is the same as the string coordinate information, which is also p1 (x1, y1), and the character spacing information between the second string to be displayed and the first string to be displayed is q, then the coordinate information of the second character to be displayed is determined to be p1 (x2, y1), where x2 = x1 + q; according to the string line break information, it can be determined that the first character to be displayed in the second line is the 8th character, and the line spacing is h, then the coordinate information of the 8th character to be displayed is p1 (x1, y2), where y2 = y1 + h, and so on, the character coordinate information of each character to be displayed can be determined. And the character coordinate information of each character to be displayed is cached in the client's memory for subsequent rendering processing.
[0063] S1068. Generate string layout information according to the character spacing information of adjacent characters to be displayed, the string attribute information, the string coordinate information, the character width information of each character to be displayed, the character coordinate information of each character to be displayed, and the string line break information.
[0064] After obtaining the string line break information and the character coordinate information of each character to be displayed in the above steps, the string layout information of the string to be displayed is generated by combining the character spacing information of adjacent characters to be displayed, the string attribute information (horizontal alignment, vertical alignment, text box information corresponding to the string), the string coordinate information, etc., and the string layout information is cached in the client's memory for subsequent rendering processing.
[0065] In actual application, the character to be displayed and the character style information are received through the text typesetting module, and after the above steps are processed, the character string typesetting information of the character string to be displayed is output.
[0066] Step 108: Rendering in the target canvas layer according to the character string to be displayed and the character string layout information to generate a rendering result corresponding to the character string to be displayed.
[0067] After obtaining the character string to be displayed and generating the character string layout information, the character information is rendered in the target canvas layer (canvas), and the character to be displayed is rendered into the canvas.
[0068] Specifically, rendering in the target canvas layer according to the character string to be displayed and the character string layout information generates a rendering result corresponding to the character string to be displayed, including S1082 to S1086:
[0069] S1082. Create a temporary canvas layer according to the string text box information.
[0070] The string typesetting information carries the string text box information corresponding to the string, and a corresponding temporary canvas layer (temporary canvas) is created according to the string text box information. For example, if the width of the string text box is width and the height is height, a temporary canvas with a width of width and a height of height is created.
[0071] In practical applications, the scaling information of the string to be displayed must also be considered, and a temporary canvas layer is created based on the string text box information and the scaling information. For example, the width of the string text box is width, and the height is height. The horizontal scaling ratio of the character to be displayed is scaleX, and the vertical scaling ratio is scaleY. Then the width of the temporary canvas is width*scaleX, and the height is height*scaleY. Creating a temporary canvas layer based on the scaling information can ensure that the characters of the string to be displayed will not be blurred when the rendering result is generated.
[0072] S1084. Draw on the temporary canvas layer to generate a temporary rendering result corresponding to the character string to be displayed according to the character spacing information of adjacent characters to be displayed, the character string attribute information, the character string coordinate information, the character width information of each character to be displayed, and the character coordinate information of each character to be displayed.
[0073] After creating a temporary canvas, the character coordinate information of each character to be displayed, the character width information of each character to be displayed, the character spacing information of adjacent characters to be displayed, the string attribute information and the string coordinate information carried in the string typesetting information can be used to draw each character in the temporary canvas to generate a temporary rendering result corresponding to the string to be displayed.
[0074] S1086: Pass the temporary rendering result to the target canvas layer, and generate a rendering result corresponding to the to-be-displayed character string in the target canvas layer.
[0075] After the temporary rendering result is drawn in the temporary canvas, the temporary drawing result is drawn onto the final display canvas in combination with the string coordinate information and the string attribute information (string text box position information, string rotation information to be displayed, string scaling information to be displayed).
[0076] Through the method provided in this application, character typesetting and rendering are realized based on canvas, complex text styles are supported, and it can be easily integrated into canvas-based applications such as image editors, 2D canvas games, various web page drawing tools, etc.
[0077] In actual application, the text is rendered into the target canvas through the text rendering module according to the information obtained in the above steps 102 to 106.
[0078] In practical applications, the method may also edit the character rendering result rendered to the canvas layer, and the method further includes S1100-S1108.
[0079] S1100: Receive an editing instruction for the rendering result.
[0080] After the generated rendering result is displayed, the user can also edit the rendering result, and the client receives the editing instruction for the rendering result.
[0081] The user triggers an editing command for the rendering result, which usually starts with the user clicking on a character in the screen. A cursor will appear and flash at the clicked position, and keyboard data will be called at the same time, or the character content will be selected for subsequent copying, modification or deletion.
[0082] S1102. Determine editing cursor position information in the rendering result in response to the editing instruction, and determine a character to be edited.
[0083] When the user clicks on the rendering result, the coordinate information in the browser when the click occurs can be directly obtained. We need a flashing cursor to appear near the text clicked in the editing command, that is, we need to know the relative coordinates of the click position information in the text content area, and convert the browser's coordinate information into editing cursor position information. In other words, the editing cursor position information is the relative coordinate relative to the rendering result.
[0084] Specifically, determining the editing cursor position information in the rendering result in response to the editing instruction includes:
[0085] Acquire the canvas layer coordinate system of the target canvas layer and the character coordinate system of the rendering result corresponding to the character string to be displayed;
[0086] Determining a rotation matrix of the character coordinate system relative to the canvas layer coordinate system according to the canvas layer coordinate system and the character coordinate system;
[0087] Determine the canvas position coordinates of the editing instruction in the target canvas layer;
[0088] The editing cursor position information is determined according to the canvas position coordinates and the rotation matrix.
[0089] The canvas layer coordinate system is the coordinate system of the target canvas layer, and the character coordinate system is the coordinate system of the text box corresponding to the rendering result. Figure 2 , Figure 2 FIG. 1 is a schematic diagram of a rendering result coordinate system provided by an embodiment of the present application. Figure 2 As shown, when the user clicks on "Thrilling", a flashing cursor should appear between "Thrilling" and "Skydiving Trip" to indicate that the user has clicked on the text area. First, obtain the canvas layer coordinate system (X, Y) of the target canvas layer and the character coordinate system (X', Y') of the rendering result. According to the rotation information of the character string to be displayed in the character string attribute information, the canvas layer coordinate system and the character coordinate system, the rotation matrix α of the character coordinate system relative to the canvas layer coordinate system can be calculated, that is, the conversion relationship between the canvas layer coordinate system and the character coordinate system can be established.
[0090] In the above steps, when the user clicks on the canvas, the coordinates (X1, Y1) of the target canvas layer where the click occurred can be directly obtained. Then, based on the coordinates and the rotation matrix α, the editing cursor position information (X1', Y1') of the click position in the character coordinate system can be calculated.
[0091] In addition, the distance and angle between the click position and the X-axis and Y-axis of the character coordinate system of the text box can also be calculated to calculate the editing cursor position information. Specifically, the distance between the click position and the X-axis and Y-axis can be calculated using the vector method or the triangle method; the angle between the click position and the X-axis and Y-axis can be calculated using trigonometric functions. When calculating the editing cursor position information, if the angle between the click position and the X-axis of the character coordinate system is greater than 90 degrees, the x-coordinate of the editing cursor position is a negative number, otherwise the x-coordinate is a positive number; if the angle between the click position and the Y-axis of the character coordinate system is greater than 90 degrees, the y-coordinate of the editing cursor position is a negative number, otherwise the y-coordinate is a positive number. Combined with the distance between the click position and the X-axis and the Y-axis calculated above, the editing cursor position information can be calculated.
[0092] In practical applications, when receiving an editing instruction, the characters to be edited are usually determined according to the editing instruction. The characters to be edited specifically refer to characters that need to be edited during the editing process. In response to the editing instruction, the characters to be edited are determined in the rendering result, including:
[0093] Obtaining cursor start information and cursor end information in the editing instruction;
[0094] The character to be edited is determined according to the cursor start information and the cursor end information.
[0095] In the above steps, after obtaining the editing cursor position information of the character coordinate system corresponding to the editing command click position corresponding to the rendering result, the cursor start information (startCursorIndex) and cursor end information (endCursorIndex) are used to replace the cursor position. When startCursorIndex is equal to endCursorIndex, the screen shows a blinking cursor; when startCursorIndex is less than endCursorIndex, it means that the text is selected at this time. When rsorIndex and endCursorIndex are both 0, it means that the cursor is flashing on the left side of the first character; when startCursorIndex and endCursorIndex are both 2, it means that the cursor is flashing on the right side of the second character; when startCursorIndex=0, endCursorIndex=4, it means that the first 4 characters are selected; when startCursorIndex=3, endCursorIndex=6, it means that the 4th to 6th characters are selected.
[0096] S1104: calling an input component, receiving characters to be updated through the input component, and determining character attribute information of the characters to be updated.
[0097] In the above steps, we have completed the determination of the response of the editing cursor position information to the click. Next, we need to handle the response to the character input. Since canvas is not an input component in the browser, it is not suitable for responding to keyboard input events. Therefore, a native input component is needed to respond to input events. When entering the input state, you need to add a hidden textarea input component to the interface, set the text content in the textarea to the characters to be edited, and set the textarea component to the focus state (the state of accepting input). Because when the canvas layer is clicked to select an area, the focus state of the textarea will be canceled, so each time after clicking the canvas layer, the textarea component needs to be set to the focus state again.
[0098] Receive the characters to be updated in the textarea component, and obtain the character attribute information of the characters to be updated, such as font, color, size, etc.
[0099] S1106: Replace the character to be edited with the character to be updated to generate a character string to be updated, and update the character string layout information according to the character attribute information of the character to be updated to obtain the character string layout information to be updated.
[0100] In the textarea, the characters to be edited are replaced by the characters to be updated to generate a string to be updated. When the content in the textarea is changed, the text and selected content need to be rearranged and rendered.
[0101] In actual applications, in order to avoid the impact of re-rendering on the original rendered image, when entering the editing state, it is necessary to create a temporary canvas of the same size as the text box corresponding to the rendering result, so that it covers the top of the text box, and transfer the text information of the original rendered image to the temporary canvas. During the editing process, only the content in the temporary canvas is edited to avoid pollution of the original rendered image.
[0102] S1108 . Render in the target canvas layer according to the character string to be updated and the typesetting information of the character string to be updated to generate a rendering result corresponding to the character string to be updated.
[0103] After obtaining the string to be updated and the layout information of the string to be updated, the corresponding temporary rendering result is rendered in the temporary canvas. After obtaining the temporary rendering result, it is passed to the target canvas layer to replace the original rendering result and generate a new rendering result corresponding to the string to be updated. At this point, the editing of the original rendering screen is completed. Through the layout, rendering and editing of characters, using canvas to render text, cursor, selection and corresponding mouse operation events, using textarea to receive keyboard input operations and shortcut key operations, the character processing process is completed by combining canvas and textarea.
[0104] A character processing method provided in an embodiment of the present application includes receiving at least one character to be displayed; determining character attribute information of each character to be displayed; splicing the at least one character to be displayed to generate a character string to be displayed according to the character attribute information of each character to be displayed, and obtaining the string layout information of the character string to be displayed; rendering in a target canvas layer according to the character string to be displayed and the string layout information to generate a rendering result corresponding to the character string to be displayed. The present application provides a set of character rendering and editing response processes, which implements character layout and character editing of characters based on canvas, supports complex character styles, and supports editing under text rotation and scaling. The method provided in the present application can be conveniently integrated into canvas applications to serve text editing and typesetting.
[0105] The following combination Figure 3 , taking the application of the character processing method provided by the present application in a web image editor as an example, the character processing method is further described. Figure 3 A processing flow chart of a character processing method applied to a web image editor provided by an embodiment of the present application is shown, which specifically includes the following steps:
[0106] Step 302: receiving the characters to be displayed "Today's weather is nice", and receiving the character style information of the characters to be displayed.
[0107] Step 304: Segment the characters to be displayed into separate characters, measure the width of each character using the measureText method of the web canvas, and obtain character width information of each character to be displayed.
[0108] Step 306: traverse each character, and sequentially concatenate each character to be displayed according to the character width information, character spacing information, maximum line width, and line break in the character to generate a character string and corresponding line break information.
[0109] Step 308: Calculate the position information of each character to be displayed and the text box information corresponding to the string according to the line break information, the character spacing information, the horizontal alignment, the numerical alignment information, and the character positioning coordinate information.
[0110] Step 310: Create a first temporary canvas of the corresponding size according to the text box information and the scaling information of the text.
[0111] Step 312: Draw each character one by one in the first temporary canvas according to the layout information and the character style information. After the drawing is completed, combine the text box information, the text scaling information, and the text rotation information to display the rendering result in the first temporary canvas in the target canvas.
[0112] Step 314: Receive an edit instruction for the rendering result.
[0113] Step 316: In response to the edit instruction, determine that the position of the edit cursor in the rendering result is in front of "not bad", and at the same time determine that the character to be edited is "not bad", and create a second temporary canvas.
[0114] Step 318: Call the textarea component, set "not bad" into the component, modify "not bad" to "sunny" in response to the keyboard instruction, and synchronize the content in the textarea component to the second temporary canvas in real time.
[0115] Step 320: After the editing is completed, synchronize the content in the second temporary canvas to the target canvas to generate a new rendering result, and complete the editing of the original rendering result.
[0116] The character processing method provided by an embodiment of the present application is applied to a web image editor, provides a process for rendering and editing responses, realizes character layout and character editing based on canvas, supports complex character styles, and supports editing in the case of text rotation and scaling. The method provided by the present application can be conveniently integrated into canvas applications to serve text editing and typesetting.
[0117] Corresponding to the above-mentioned embodiment of the character processing method, the present application also provides an embodiment of a character processing device. Figure 4 Shows a schematic structural diagram of a character processing device provided by an embodiment of the present application. As Figure 4 shown, the device includes:
[0118] A receiving module 402, configured to receive at least one character to be displayed;
[0119] A determining module 404, configured to determine the character attribute information of each character to be displayed;
[0120] The splicing module 406 is configured to splice the at least one character to be displayed to generate a character string to be displayed according to the character attribute information of each character to be displayed, and obtain the character string layout information of the character string to be displayed;
[0121] The rendering module 408 is configured to render and generate a rendering result corresponding to the string to be displayed in the target canvas layer according to the string to be displayed and the string layout information.
[0122] Optionally, the determining module 404 is further configured to:
[0123] Measure the character width information of each character to be displayed;
[0124] Get the character drawing information for each character to be displayed.
[0125] Optionally, the splicing module 406 is further configured to:
[0126] Acquire character spacing information of adjacent characters to be displayed, character string attribute information of the character string to be displayed, and character string coordinate information of the character string to be displayed;
[0127] Determining the character string line break information of the character string to be displayed according to the character width information of each character to be displayed and the character spacing information between adjacent characters to be displayed;
[0128] Calculating the character coordinate information of each character to be displayed according to the character string line break information, the character string attribute information, the character string coordinate information and character spacing information of adjacent characters to be displayed;
[0129] The character string layout information is generated according to the character spacing information of adjacent characters to be displayed, the character string attribute information, the character string coordinate information, the character width information of each character to be displayed, the character coordinate information of each character to be displayed, and the character string line break information.
[0130] Optionally, the splicing module 406 is further configured to:
[0131] Each character to be displayed is concatenated in turn to obtain a character string to be displayed;
[0132] Calculating the length of the concatenated character string of the character string to be displayed according to the character width information of the character string to be displayed and the character spacing information between adjacent characters to be displayed;
[0133] When the length of the concatenated character string is greater than a preset length threshold, a line break operation is performed on the character string to be displayed, and character string line break information is generated.
[0134] Optionally, the splicing module 406 is further configured to:
[0135] In the case that the characters to be displayed include line break characters, a line break operation is performed on the character string to be displayed, and character string line break information is generated.
[0136] Optionally, the device further comprises:
[0137] A counting module, configured to count the number of character string rows of the character string to be displayed;
[0138] The hiding module is configured to hide the characters to be displayed after the line break character of the last line of the character string to be displayed when the number of lines of the character string is greater than a preset line number threshold.
[0139] Optionally, the rendering module 408 is further configured to:
[0140] Create a temporary canvas layer according to the string text box information;
[0141] Generating a temporary rendering result corresponding to the character string to be displayed by drawing on the temporary canvas layer according to character spacing information of adjacent characters to be displayed, the character string attribute information, the character string coordinate information, the character width information of each character to be displayed, and the character coordinate information of each character to be displayed;
[0142] The temporary rendering result is transferred to the target canvas layer, and a rendering result corresponding to the to-be-displayed character string is generated in the target canvas layer.
[0143] Optionally, the rendering module 408 is further configured to:
[0144] Get string scaling information;
[0145] A temporary canvas layer is created according to the character string text box information and the character string scaling information.
[0146] Optionally, the device further comprises:
[0147] An editing instruction receiving module, configured to receive an editing instruction for the rendering result;
[0148] a cursor determination module, configured to determine the editing cursor position information in the rendering result in response to the editing instruction, and determine the character to be edited;
[0149] A calling module is configured to call the input component, receive the character to be updated through the input component, and determine the character attribute information of the character to be updated;
[0150] A character string updating module, configured to replace the character to be edited with the character to be updated, generate a character string to be updated, and update the character string layout information according to the character attribute information of the character to be updated, so as to obtain the character string layout information to be updated;
[0151] The editing and rendering module is configured to render and generate a rendering result corresponding to the string to be updated in the target canvas layer according to the string to be updated and the layout information of the string to be updated.
[0152] Optionally, the cursor determination module is further configured to:
[0153] Acquire the canvas layer coordinate system of the target canvas layer and the character coordinate system of the rendering result corresponding to the character string to be displayed;
[0154] Determining a rotation matrix of the character coordinate system relative to the canvas layer coordinate system according to the canvas layer coordinate system and the character coordinate system;
[0155] Determine the canvas position coordinates of the editing instruction in the target canvas layer;
[0156] The editing cursor position information is determined according to the canvas position coordinates and the rotation matrix.
[0157] Optionally, the cursor determination module is further configured to:
[0158] Obtaining cursor start information and cursor end information in the editing instruction;
[0159] The character to be edited is determined according to the cursor start information and the cursor end information.
[0160] A character processing device provided in one embodiment of the present application includes receiving at least one character to be displayed; determining character attribute information of each character to be displayed; splicing the at least one character to be displayed to generate a character string to be displayed according to the character attribute information of each character to be displayed, and obtaining the string layout information of the character string to be displayed; rendering in a target canvas layer according to the character string to be displayed and the string layout information to generate a rendering result corresponding to the character string to be displayed. The present application provides a set of character rendering and editing response processes, which implements character layout and character editing of characters based on canvas, supports complex character styles, and supports editing under text rotation and scaling. The device provided by the present application can be conveniently integrated into canvas applications to serve text editing and typesetting.
[0161] The above is a schematic scheme of a character processing device of this embodiment. It should be noted that the technical scheme of the character processing device and the technical scheme of the above character processing method belong to the same concept, and the details not described in detail in the technical scheme of the character processing device can be referred to the description of the technical scheme of the above character processing method.
[0162] Figure 5 The block diagram of a computing device 500 provided according to an embodiment of the present application is shown. The components of the computing device 500 include but are not limited to a memory 510 and a processor 520. The processor 520 is connected to the memory 510 via a bus 530, and the database 550 is used to store data.
[0163] The computing device 500 also includes an access device 540 that enables the computing device 500 to communicate via one or more networks 560. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 540 may include one or more of any type of network interface (e.g., a network interface card (NIC)) whether wired or wireless, such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a World Wide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and the like.
[0164] In one embodiment of the present application, the above components of the computing device 500 and Figure 5 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Figure 5 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as needed.
[0165] The computing device 500 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or PC. The computing device 500 may also be a mobile or stationary server.
[0166] When the processor 520 executes the computer instructions, the steps of the character processing method are implemented.
[0167] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the above character processing method belong to the same concept, and the details not described in detail in the technical scheme of the computing device can be referred to the description of the technical scheme of the above character processing method.
[0168] An embodiment of the present application further provides a computer-readable storage medium storing computer instructions, which implement the steps of the character processing method as described above when executed by a processor.
[0169] The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the above-mentioned character processing method belong to the same concept, and the details not described in detail in the technical scheme of the storage medium can be referred to the description of the technical scheme of the above-mentioned character processing method.
[0170] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0171] The computer instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer-readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0172] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described action sequence, because according to the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0173] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0174] The preferred embodiments of the present application disclosed above are only used to help explain the present application. The optional embodiments do not describe all the details in detail, nor do they limit the invention to the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the present application. The present application selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present application, so that those skilled in the art can understand and use the present application well. The present application is only limited by the claims and their full scope and equivalents.
Claims
1. A character processing method, characterized in that: include: receiving at least one character to be displayed; Determine character attribute information of each character to be displayed; splicing the at least one to-be-displayed character to generate a to-be-displayed character string according to the character attribute information of each to-be-displayed character, and acquiring the character string layout information of the to-be-displayed character string, wherein the character string layout information includes character string text box information, line break information of the character string, and enclosing matrix information outside the character string; A temporary canvas layer is created according to the string text box information, a temporary rendering result corresponding to the string to be displayed is generated on the temporary canvas layer according to the string typesetting information, the temporary rendering result is passed to a target canvas layer, and a rendering result corresponding to the string to be displayed is generated in the target canvas layer, wherein the target canvas layer is created according to the string text box information and the string scaling information.
2. The character processing method according to claim 1, characterized in that: Determine the character attribute information of each character to be displayed, including: Measure the character width information of each character to be displayed; Get the character drawing information for each character to be displayed.
3. The character processing method according to claim 2, characterized in that: Obtaining string layout information of the string to be displayed, including: Acquire character spacing information of adjacent characters to be displayed, character string attribute information of the character string to be displayed, and character string coordinate information of the character string to be displayed; Determining the character string line break information of the character string to be displayed according to the character width information of each character to be displayed and the character spacing information between adjacent characters to be displayed; Calculating the character coordinate information of each character to be displayed according to the character string line break information, the character string attribute information, the character string coordinate information and character spacing information of adjacent characters to be displayed; The character string layout information is generated according to the character spacing information of adjacent characters to be displayed, the character string attribute information, the character string coordinate information, the character width information of each character to be displayed, the character coordinate information of each character to be displayed, and the character string line break information.
4. The character processing method according to claim 3, characterized in that: Determining the string line break information of the string to be displayed according to the character width information of each character to be displayed and the character spacing information between adjacent characters to be displayed includes: Each character to be displayed is concatenated in sequence to obtain a character string to be displayed; Calculating the length of the concatenated character string of the character string to be displayed according to the character width information of the character string to be displayed and the character spacing information between adjacent characters to be displayed; When the length of the concatenated character string is greater than a preset length threshold, a line break operation is performed on the character string to be displayed, and character string line break information is generated.
5. The character processing method according to claim 4, characterized in that: The method further comprises: In the case that the characters to be displayed include line break characters, a line break operation is performed on the character string to be displayed, and character string line break information is generated.
6. The character processing method according to claim 4 or 5, characterized in that: The method further comprises: Counting the number of character string rows of the character string to be displayed; When the number of lines of the character string is greater than a preset line number threshold, the characters to be displayed after the line break character of the last line of the character string to be displayed are hidden.
7. The character processing method according to claim 3, characterized in that: Drawing and generating a temporary rendering result corresponding to the to-be-displayed string on the temporary canvas layer according to the string layout information includes: A temporary rendering result corresponding to the character string to be displayed is generated by drawing on the temporary canvas layer according to the character spacing information of adjacent characters to be displayed, the character string attribute information, the character string coordinate information, the character width information of each character to be displayed, and the character coordinate information of each character to be displayed.
8. The character processing method according to claim 1, characterized in that: Creating a temporary canvas layer according to the string text box information, including: Get string scaling information; A temporary canvas layer is created according to the character string text box information and the character string scaling information.
9. The character processing method according to any one of claims 1 to 5 or 7 to 8, characterized in that: The method further comprises: receiving an editing instruction for the rendering result; In response to the editing instruction, determining editing cursor position information in the rendering result, and determining a character to be edited; Calling an input component, receiving a character to be updated through the input component, and determining character attribute information of the character to be updated; Replacing the to-be-edited character with the to-be-updated character to generate a to-be-updated character string, and updating the character string layout information according to the character attribute information of the to-be-updated character to obtain the to-be-updated character string layout information; A rendering result corresponding to the string to be updated is generated by rendering in the target canvas layer according to the string to be updated and the layout information of the string to be updated.
10. The character processing method according to claim 9, characterized in that: Determining editing cursor position information in the rendering result in response to the editing instruction includes: Acquire the canvas layer coordinate system of the target canvas layer and the character coordinate system of the rendering result corresponding to the character string to be displayed; Determining a rotation matrix of the character coordinate system relative to the canvas layer coordinate system according to the canvas layer coordinate system and the character coordinate system; Determine the canvas position coordinates of the editing instruction in the target canvas layer; The editing cursor position information is determined according to the canvas position coordinates and the rotation matrix.
11. The character processing method according to claim 9, characterized in that: Determining a character to be edited in the rendering result in response to the editing instruction includes: Obtaining cursor start information and cursor end information in the editing instruction; The character to be edited is determined according to the cursor start information and the cursor end information.
12. A character processing device, characterized in that: include: A receiving module, configured to receive at least one character to be displayed; A determination module, configured to determine character attribute information of each character to be displayed; a splicing module, configured to splice at least one to-be-displayed character to generate a to-be-displayed character string according to the character attribute information of each to-be-displayed character, and obtain character string layout information of the to-be-displayed character string, wherein the character string layout information includes character string text box information, line break information of the character string, and enclosing matrix information outside the character string; The rendering module is configured to create a temporary canvas layer according to the string text box information, draw on the temporary canvas layer according to the string layout information to generate a temporary rendering result corresponding to the string to be displayed, pass the temporary rendering result to the target canvas layer, and generate a rendering result corresponding to the string to be displayed in the target canvas layer, wherein the target canvas layer is created according to the string text box information and the string scaling information.
13. A computing device comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, characterized in that: When the processor executes the computer instructions, the steps of the method according to any one of claims 1 to 11 are implemented.
14. A computer-readable storage medium storing computer instructions, characterized in that: When the computer instructions are executed by a processor, the steps of the method described in any one of claims 1 to 11 are implemented.
15. A computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 11.
