Method of converting text and related devices
By identifying tag hierarchy relationships and creating a scope stack, the problem of low efficiency in text conversion between different programming languages is solved, achieving fast text conversion and resource saving, and is suitable for SVG graphics rendering in WebGL environments.
Patent Information
- Application Number
- CN202210233964.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-10
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2042-03-10
AI Technical Summary
Source code text written in different programming languages cannot be directly converted between each other, and existing conversion processes are inefficient and resource-intensive.
By identifying tags and tag hierarchy relationships in the text, a scope stack is created to store tag names, attribute names, and attribute values. The target object in JSON format is used for text conversion, avoiding traversal of the DOM tree.
It achieves fast text conversion, reduces resource consumption, and improves conversion efficiency, especially significantly improving performance when drawing SVG graphics in the WebGL environment.
Smart Images

Figure CN114896946B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of computer, and particularly relates to a method for converting text and related equipment. BACKGROUND
[0002] With the development of computer technology, there are various source code texts written by programming languages in the field. Generally, texts written by different programming languages cannot be directly converted, and a tool is needed to complete the conversion from one text to another. The conversion process is usually inefficient. SUMMARY
[0003] The present disclosure provides a method for converting text and related equipment to solve or partially solve the above problems.
[0004] In a first aspect, the present disclosure provides a method for converting text, wherein the text comprises a first label and a second label, the second label is a child label of the first label, and the method comprises:
[0005] creating a target object for saving a text conversion result;
[0006] in response to identifying the first label and the second label in the text, creating a first scope and a second scope corresponding to the first label and the second label respectively in the target object according to a hierarchical relationship between the first label and the second label, the second scope being a child scope of the first scope; and
[0007] storing label names, attribute names and attribute values corresponding to the first label and the second label in the first scope and the second scope respectively.
[0008] In a second aspect, the present disclosure provides an apparatus for converting text, wherein the text comprises a first label and a second label, the second label is a child label of the first label, and the apparatus comprises:
[0009] a creating module configured to create a target object for saving a conversion result;
[0010] a converting module configured to:
[0011] in response to identifying the first label and the second label in the text, creating a first scope and a second scope corresponding to the first label and the second label respectively in the target object according to a hierarchical relationship between the first label and the second label, the second scope being a child scope of the first scope; and
[0012] The first label and the second label are respectively stored in the first scope and the second scope.
[0013] In a third aspect, the present disclosure provides a computer device, comprising one or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and executed by the one or more processors, and the programs comprise instructions for performing the method according to the first aspect.
[0014] In a fourth aspect, the present disclosure provides a non-transitory computer-readable storage medium containing a computer program, which, when executed by one or more processors, causes the processors to perform the method according to the first aspect.
[0015] In a fifth aspect, the present disclosure provides a computer program product, comprising computer program instructions, which, when executed on a computer, cause the computer to perform the method according to the first aspect.
[0016] The method for converting text and the related device provided by the present disclosure can realize fast conversion of text and less resource occupation through label identification and establishment of scope hierarchical relationship. BRIEF DESCRIPTION OF DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the present disclosure or the related art, the following will briefly introduce the drawings needed to be used in the embodiments or related art descriptions. Obviously, the drawings in the following description are only embodiments of the present disclosure, and for those skilled in the art, other drawings can also be obtained without creative labor based on these drawings.
[0018] Figure 1 A hardware structure schematic diagram of an exemplary computer device provided by the embodiments of the present disclosure is shown.
[0019] Figure 2 A schematic diagram of an exemplary text according to the embodiments of the present disclosure is shown.
[0020] Figure 3A A schematic diagram of an exemplary target object according to the embodiments of the present disclosure is shown.
[0021] Figure 3B A schematic diagram of another exemplary target object according to the embodiments of the present disclosure is shown.
[0022] Figure 3C A schematic diagram of still another exemplary target object according to the embodiments of the present disclosure is shown.
[0023] Figure 3DA schematic diagram of yet another example target object according to embodiments of the present disclosure is shown.
[0024] Figure 3E A schematic diagram of another example target object according to embodiments of the present disclosure is shown.
[0025] Figure 3F A schematic diagram of yet another example target object according to embodiments of the present disclosure is shown.
[0026] Figure 4A A schematic diagram of an example scope stack according to embodiments of the present disclosure is shown.
[0027] Figure 4B A schematic diagram of another example scope stack according to embodiments of the present disclosure is shown.
[0028] Figure 4C A schematic diagram of yet another example scope stack according to embodiments of the present disclosure is shown.
[0029] Figure 4D A schematic diagram of yet another example scope stack according to embodiments of the present disclosure is shown.
[0030] Figure 4E A schematic diagram of another example scope stack according to embodiments of the present disclosure is shown.
[0031] Figure 4F A schematic diagram of yet another example scope stack according to embodiments of the present disclosure is shown.
[0032] Figure 4G A schematic diagram of yet another example scope stack according to embodiments of the present disclosure is shown.
[0033] Figure 5 A schematic diagram of an example method provided by embodiments of the present disclosure is shown.
[0034] Figure 6 A schematic diagram of an example apparatus provided by embodiments of the present disclosure is shown. DETAILED DESCRIPTION
[0035] In order to make the purposes, technical solutions, and advantages of the present disclosure clearer, the present disclosure is further described in detail below with reference to specific embodiments and the accompanying drawings.
[0036] It should be noted that, unless otherwise defined, technical terms or scientific terms used in the embodiments of the present disclosure shall have the common meaning understood by one of ordinary skill in the art to which the present disclosure belongs. The terms "first", "second", and similar terms used in the embodiments of the present disclosure do not denote any order, quantity, or importance, but are used to distinguish different components. The terms "include", "contain", and similar terms mean that the elements or objects before the terms encompass the elements or objects listed after the terms and their equivalents, and do not exclude other elements or objects. The terms "connect" or "connected" and similar terms are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. The terms "upper", "lower", "left", "right", and the like are used only to represent relative positional relationships, and when the absolute positions of the described objects are changed, the relative positional relationships can also be changed accordingly.
[0037] Scalable Vector Graphics (SVG) is a vector image format, an open standard vector graphics language, which can be used to design high-resolution Web graphics pages, and is usually applied in web pages as Document Object Model (DOM) nodes.
[0038] WebGL (Web Graphics Library) is a 3D drawing protocol that eliminates the need to develop a dedicated rendering plug-in for web pages. It can be used to create website pages with complex 3D structures, and even to design 3D web games and the like.
[0039] In some scenarios, it can be necessary to render SVG format pictures using WebGL, but since the principles of DOM and WebGL rendering are different, there is no direct method in the browser to convert SVG text into a data format that can be processed by WebGL, resulting in the inability of SVG DOM nodes to be directly rendered in WebGL.
[0040] One possible implementation is to parse the SVG text into data that can be processed by WebGL (for example, JSON (JavaScript Object Notation) format data) according to the graphical rules described in the SVG text, and then WebGL draws the image according to the JSON format data.
[0041] For example, the SVG text can be converted into a DOM object using the DOM Parser (a native API of the browser), and then the DOM object can be converted into a JSON object that can be rendered by WebGL by traversing the DOM object and obtaining the attributes of the DOM object.
[0042] A typical process of converting SVG text into DOM objects by a DOM Parser can be loading SVG text (usually in XML format) into memory and parsing it into a tree structure, i.e., a so-called DOM tree. And each node of the DOM tree is a DOM object and encapsulates data therein and provides corresponding operation methods.
[0043] Ideally, a DOM should be simple. However, in actual use, a large amount of information irrelevant to drawing images is generated in the DOM tree. For example, in order to improve readability of text, many line breaks and white spaces are usually inserted in SVG text, which also appear in the DOM tree in the form of nodes. Meanwhile, a node of the DOM tree also has data such as attributes, which cannot be directly read but need to be obtained by parsing. Therefore, it can be understood that for a complex DOM tree, the efficiency of traversing DOM objects and obtaining DOM attributes is relatively low, and the process needs to occupy more resources.
[0044] In view of this, the embodiments of the present disclosure provide a method for converting text and related devices, which can realize fast conversion of text and occupy fewer resources.
[0045] Figure 1 A hardware structure schematic diagram of an exemplary computer device 100 provided by the embodiments of the present disclosure is shown. The computer device 100 can be used to implement the method for converting text of the embodiments of the present disclosure. As shown, the computer device 100 can include a processor 102, a memory 104, a network module 106, a peripheral interface 108 and a bus 110. Among them, the processor 102, the memory 104, the network module 106 and the peripheral interface 108 are connected to each other through the bus 110 for internal communication connection in the computer device 100. Figure 1
[0046] The processor 102 can be a central processing unit (CPU), an image processor, a neural network processor (NPU), a microcontroller (MCU), a programmable logic device, a digital signal processor (DSP), an application specific integrated circuit (ASIC), or one or more integrated circuits. The processor 102 can be used to perform functions related to the technologies described in the present disclosure. In some embodiments, the processor 102 can also include multiple processors integrated as a single logic component. For example, as shown, the processor 102 can include multiple processors 102a, 102b and 102c. Figure 5
[0047] The memory 104 can be configured to store data (e.g., instructions, computer code, etc.). As shown, the memory 104 stores data including program instructions (e.g., program instructions for implementing the method of converting text according to embodiments of the present disclosure) and data to be processed (e.g., the memory can store configuration files of other modules, etc.). The processor 102 can also access the program instructions and data stored in the memory 104 and execute the program instructions to operate on the data to be processed. The memory 104 can include volatile storage or non-volatile storage. In some embodiments, the memory 104 can include random access memory (RAM), read-only memory (ROM), optical disc, magnetic disc, hard disc, solid state disc (SSD), flash memory, memory stick, etc. Figure 1
[0048] The network interface 106 can be configured to provide communication with other external devices to the computer device 100 via a network. The network can be any wired or wireless network capable of transmitting and receiving data. For example, the network can be a wired network, a local wireless network (e.g., Bluetooth, WiFi, near field communication (NFC), etc.), a cellular network, the Internet, or a combination thereof. It can be understood that the type of network is not limited to the specific examples described above.
[0049] The peripheral interface 108 can be configured to connect the computer device 100 with one or more peripheral devices to achieve information input and output. For example, the peripheral devices can include input devices such as a keyboard, a mouse, a touchpad, a touch screen, a microphone, various sensors, etc. and output devices such as a display, a speaker, a vibrator, an indicator light, etc.
[0050] The bus 110 can be configured to transmit information between various components (e.g., the processor 102, the memory 104, the network interface 106, and the peripheral interface 108) of the computer device 100, such as an internal bus (e.g., a processor-memory bus), an external bus (a USB port, a PCI-E bus), etc.
[0051] It should be noted that although the architecture of the computer device 100 described above only shows the processor 102, the memory 104, the network interface 106, the peripheral interface 108, and the bus 110, in the specific implementation process, the architecture of the computer device 100 can also include other components necessary for normal operation. In addition, those skilled in the art can understand that the architecture of the computer device 100 described above can also only include components necessary for implementing the embodiments of the present disclosure, and does not necessarily include all the components shown in the figure.
[0052] In the initial state, a set of strings conforming to the tag naming specification can be defined first, a scope stack for storing scope information can be defined, and a target object for saving the conversion result can be defined. In some embodiments, the target object can be a JSON format object.
[0053] In some embodiments, for example, taking SVG text as an example, which constitutes a vector image, it can be implemented by writing code (in XML format), in which various shape tags can be used. In order to be able to identify these tags, the tag name strings corresponding to these tags can all be defined as a set of strings conforming to the tag naming specification. For example, the strings conforming to the tag naming specification can include:
[0054] svg, the root element in the SVG text, refers to the root of the SVG text; generally, each SVG text contains the outermost <svg>< / svg> tag, which is used to declare the beginning and end of the SVG text;
[0055] g, the grouping element in the SVG text, refers to the basic repeating unit of the SVG text; in the SVG text, the <g>The tags can be nested to further represent primitives and sub-primitives, in the tag <g>Within it, any basic geometric primitive element that can be visualized can be defined;
[0056] The path element in SVG text is used to describe paths in geometric primitives; in SVG text, the elliptical arc can be redefined using the elliptical arc curve command A of the path element.
[0057] circle, ellipse, line, polyline, rect, polygon are graphic elements in SVG text used to describe geometric primitives such as circles, ellipses, lines, polylines, rectangles, and polygons. In SVG text, the polygon element can be used to redefine a polygon.
[0058] After completing the initial preparations, the computer device 100 can first determine the text that needs to be converted. For example, the text that the user provides that needs to be converted.
[0059] Figure 2 A schematic diagram of exemplary text 200 according to an embodiment of this disclosure is shown.
[0060] like Figure 2 As shown, the text 200 can be an SVG text in XML format. The syntax and formatting definitions in SVG text follow the XML standard, and SVG objects are described through various element tags and their corresponding attributes. This embodiment of the disclosure utilizes this feature to directly parse SVG text.
[0061] Next, computer device 100 can create a target object, such as a JSON object, for storing the text conversion result in the text 200. It can be understood that the created target object can be stored in... Figure 1 In memory 104.
[0062] Figure 3A A schematic diagram of an exemplary target object 300 according to an embodiment of the present disclosure is shown.
[0063] like Figure 3A As shown, when creating the target object 300, since text parsing has not yet begun, the target object 300 can be an empty object (result undefined). The computer device 100 can then use this empty object to create a scope. The scope is used to store the tag information of the text 200, such as the tag name, tag attributes and their corresponding attribute values, and the child tag information of the current tag (and the child tag information of the child tags, etc.). Since the target object 300 corresponds to the entire text, it can store all the tag information of the entire text.
[0064] In some embodiments, in order to correspond to the storage of the label name, the label attribute name and the corresponding attribute value, and the sub-label information of the current label, the scope created in the target object 300 can include some special fields. Figure 3B A schematic diagram of an exemplary scope according to an embodiment of the present disclosure is shown. As shown in Figure 3B The scope can include three special fields: label, props, and _children. The value of label is a string, which is used to record the label name of the label corresponding to the scope; the value of props is an empty object, which is used to store the corresponding label attribute name and attribute value; and the _children field is an array, which is used to store the scope of the child label of the label corresponding to the scope.
[0065] After the target object is created, the computer device 100 can start to parse and convert the text 200.
[0066] Taking the SVG text as an example, it is generally described by XML, and the information of each label in the text can be obtained before obtaining the graphic information recorded by the SVG text. Therefore, the computer device 100 can complete the text parsing by identifying the labels of the text 200.
[0067] Since the start tag <> and the end tag and the content between the start tag and the end tag are generally used to represent a label in XML, in some embodiments, the label can be identified first by identifying the start tag.
[0068] Taking the text 200 of Figure 2 , for example, the first label svg of the text 200 is the first label 202, which can include a first label start character 2022, which can be, for example, “<”. In some embodiments, if the computer device 100 adopts the character-by-character identification method, when the computer device 100 identifies the characters “<” and “s” in sequence, because the character “s” meets the label name of the label (for example, svg) in the label naming specification, it indicates that the character “<” starts a new label, therefore, the computer device 100 can determine that the character “<” is the start character of a label, that is, the first label start character 2022.
[0069] When the computer device 100 identifies the first label start character 2022 of the first label 202 in the text 200, it can start to create a corresponding first scope for the first label 202 in the target object 300. Figure 3C A schematic diagram of another exemplary target object 300 according to an embodiment of the present disclosure is shown. As shown in Figure 3C As shown, the computer device 100 can create a first scope 304 for the first tag 202 in the target object 300 to store the tag information of the first tag 202. In some embodiments, when creating the first scope 304, fields label, props, and _children can also be added thereto, which are used to record the string corresponding to the tag name of the first tag 202, the attribute name and attribute value in the first tag 202, and the child tag information of the first tag 202, respectively.
[0070] Assuming the parsing process is character-by-character, but XML nodes (tags) have hierarchical relationships (or parent-child relationships), in some embodiments, the computer device 100 can utilize a scope stack to store scope information, thereby using the stack to maintain the hierarchical relationship (or parent-child relationship) of each tag. The stack is initialized as an empty array. Figure 4A A schematic diagram of an exemplary scope stack 400 according to an embodiment of this disclosure is shown. Figure 4A As shown, in the initial state, the scope stack 400 is empty.
[0071] Figure 4B A schematic diagram of another exemplary scope stack 400 according to an embodiment of this disclosure is shown. Figure 4B As shown, when computer device 100 creates a first scope 304 in target object 300, it will correspondingly store the information of the first scope (e.g., 1) into scope stack 400. In this way, during subsequent processing, computer device 100 can know the information of the current scope by reading the last pushed information into scope stack 400, and thus store the corresponding information into the corresponding scope in target object 300.
[0072] Next, after recognizing the first tag start character 2022, the computer device 100 can continue to recognize the characters in the text 200 after the first tag start character 2022.
[0073] Since the tag name is generally recorded after the start character "<", if the computer device 100 recognizes a first character or first string 2024 that conforms to the tag naming convention after the first tag start character 2022, it can store the first character or first string 2024 in the first scope 304, which is the current scope, as the tag name of the first tag 202. For example, Figure 3C The string svg in the text.
[0074] Then, the computer device 100 can continue to recognize the text 200, and when a character or a string of characters that does not conform to the label naming specification is recognized in the text 200, the character or the string of characters 2026 that conforms to the attribute naming specification (e.g., a letter, a number, or a combination thereof) can be stored in the first scope 304 as the current scope as the attribute name of the first label 202, and the character or the string of characters 2030 after the attribute assignment character 2028 can be stored in the first scope 304 as the current scope as the attribute value of the first label 202. As shown in Figure 3C , the attribute name id and the corresponding attribute value 1213 are recorded in the first scope 304. In some embodiments, it can be understood that the attribute value is generally written within " " or'', and thus the attribute assignment character 2028 can be a string, such as = " or = '. When the computer device 100 recognizes " " or'', the string within " " or'' can be recorded as the attribute value in the current scope.
[0075] Next, the computer device 100 continues to recognize the text 200, and in combination with Figure 2 , it can be further recognized that a start character "<" (rather than an end character ">") is recognized before the end character of the label is recognized, and thus it can be considered that the second label start character 2042 of the second label 204 is recognized.
[0076] At this time, as shown in Figure 4B , the computer device 100 determines that the current scope (i.e., the first scope) already exists by reading the latest information of the scope stack 400, and thus the second scope 306 as the child scope of the first scope 304 can be created in the target object 300 according to the information. Figure 3D , for example, the second scope 306 can be added in the field_children of the first scope 304 to indicate that the second scope 306 is the child scope of the first scope 304 and the second label 204 corresponding to the second scope 306 is the child label of the first label 202 corresponding to the first scope 304.
[0077] Then, the computer device 100 can store the information (e.g., 2) of the second scope 306 in the scope stack 400 to replace the first scope 304 as the current scope, as shown in Figure 4C . Since the information of the second scope 306 is later than the information of the first scope 304 in the stack, after the information of the second scope 306 is stacked, the computer device 100 can determine that the second scope 306 is the current scope (rather than the first scope 304) by reading the latest information in the scope stack 400.
[0078] After the second scope 306 is created, the computer device 100 can continue to identify the text 200. If a second character or a second string 2024 that meets the label naming specification is identified in the text 200, the computer device 100 can store the second character or the second string 2024 in the second scope 306 as the label name of the second label 204, for example, Figure 3D the character g in the second scope 306.
[0079] Then, if a character or a string that does not meet the label naming specification is identified in the text 200, the computer device 100 can store a character or a string 2046 that meets the attribute naming specification in the second scope 306 as the attribute name of the second label 204, and store a character or a string 2050 after the attribute assignment character 2048 in the second scope 306 as the attribute value of the second label 204. As shown in Figure 3D , the attribute name prop and the corresponding attribute value 0 1 3 are recorded in the second scope 306.
[0080] Next, the computer device 100 continues to identify the text 200, and in combination with Figure 2 , a start character "<" (instead of an end character ")") can be further identified before the end character of the label is identified, so it can be considered that the third label start character 2062 of the third label 206 is identified.
[0081] At this time, as shown in Figure 4C , the computer device 100 determines that there is already a current scope (i.e., the second scope) by reading the latest information of the scope stack 400, and therefore, according to the information, the third scope 308 as a child scope of the second scope 306 can be created in the target object 300. As shown in Figure 3E , for example, the third scope 308 can be added in the field_children of the second scope 306 to show that the third scope 308 is a child scope of the second scope 306 and the third label 206 corresponding to the third scope 308 is a child label of the second label 204 corresponding to the second scope 306.
[0082] Then, the computer device 100 can store the information (e.g., 3) of the third scope 308 in the scope stack 400 to replace the second scope 306 with the third scope 308 as the current scope, as shown in Figure 4D As shown in the figure. Since the information of the third scope 308 is pushed onto the stack after the information of the second scope 306, after the information of the third scope 308 is pushed onto the stack, the computer device 100 can determine that the third scope 308 is the current scope (rather than the first scope 304 or the second scope 306) by reading the latest information in the scope stack 400.
[0083] After creating the third scope 308, the computer device 100 can continue to identify the text 200, and store the identified tag names, attribute names, and attribute values correspondingly in the third scope 308. The specific implementation method is similar to the foregoing embodiments and will not be elaborated herein.
[0084] Next, the computer device 100 can recognize a tag end character (or string) "< / ". Since the current scope is the third scope 308, this tag end character is the third tag end character 2064 corresponding to the third tag 206. At this time, it indicates that the information of the third tag 206 has been collected completely, and the information of the third scope can be popped (stacked out) from the scope stack 400, as Figure 4E shown. Thus, the current scope changes from the third scope 308 to the second scope 306.
[0085] Then, the computer device 100 can continue to recognize a tag end character (or string) "< / ". Since the current scope is the second scope 306, this tag end character is the second tag end character 2052 corresponding to the second tag 204. At this time, it indicates that the information of the second tag 204 has been collected completely, and the information of the second scope can be popped from the scope stack 400, as Figure 4F shown. Thus, the current scope changes from the second scope 306 to the first scope 304 again.
[0086] Next, the computer device 100 can continue to identify the text 200. As shown in Figure 2 the figure, before recognizing the tag end character again, it can further recognize a start character "<" (instead of the end character "< / "), so it can be considered that the fourth tag start character 2082 of the fourth tag 208 is recognized.
[0087] At this time, as shown in Figure 4F the figure, the computer device 100 determines that the current scope already exists (that is, the first scope) by reading the latest information in the scope stack 400. Therefore, according to this information, a fourth scope 310, which is a sub-scope of the first scope 304, can be created in the target object 300. As Figure 3F As shown, for example, the fourth scope 310 can continue to be added in the field_children of the first scope 304 to show that the fourth scope 310 is a child scope of the first scope 304 and the fourth label 208 corresponding to the fourth scope 310 is a child label of the first label 202 corresponding to the first scope 304.
[0088] Then, the computer device 100 can store the information (e.g., 4) of the fourth scope 310 in the scope stack 400 to replace the first scope 304 as the current scope, as shown in the following. Figure 4G As the information of the fourth scope 310 is later than the information of the first scope 304 in the stack, after the information of the fourth scope 310 is stacked, the computer device 100 can determine that the fourth scope 310 is the current scope (rather than the first scope 304) by reading the latest information in the scope stack 400.
[0089] After the fourth scope 310 is created, the computer device 100 can continue to identify the text 200 and store the identified label names, attribute names and attribute values in the fourth scope 310, which is similar to the foregoing embodiments and will not be described here.
[0090] Next, the computer device 100 can identify the fourth label end character 2084 of the fourth label 208 and the first label end character 2032 of the first label 202 in turn, and then can pop the information of the fourth scope and the information of the first scope from the scope stack 400 in turn.
[0091] Then, the computer device 100 can determine that the scope stack 400 is empty and can end the text conversion process. The target object 400 records the final converted text, as shown in the following. Figure 3F
[0092] At this time, the image information recorded in the SVG text 200 can be obtained by reading the text recorded in the target object 400.
[0093] In some embodiments, the computer device 100 can call the API interface of WebGL to make WebGL render the target object 400, so as to obtain the image corresponding to the SVG text 200.
[0094] It can be seen that, compared with DOMParser which first parses the SVG text into a DOM tree and then recursively finds the information of each child node of the DOM tree, the disclosed embodiments only traverse the SVG text 200 once in the process of parsing the text 200, which greatly improves the parsing efficiency and reduces the resource usage.
[0095] In some embodiments, SVG text is converted into a more general JSON format, providing support for environments where SVG graphics cannot be directly drawn (such as WebGL). Actual experiments show that for the same SVG text 200, assuming the method using DOMParser to parse it into a DOM tree and then traversing it takes 100ms, the method of this disclosure only takes about 10ms to complete the text conversion, significantly improving efficiency. Furthermore, because it eliminates the need for DOMParser and DOM tree traversal, it also greatly saves resources.
[0096] It should be noted that the above embodiments are only illustrated with the exemplary SVG text 200. It can be understood that the actual SVG text may not have only four tags, and may not be limited to having a three-level hierarchical relationship. When the SVG text has more tags and more levels, corresponding parsing and processing can also be performed based on the ideas of the above embodiments, which will not be elaborated here.
[0097] Figure 5 A flowchart illustrating an exemplary method 500 provided in an embodiment of this disclosure is shown.
[0098] This method 500 can be derived from Figure 1 The computer device 100 is used to implement this and can be used to convert text, for example, to... Figure 2 The SVG text of 200 characters is converted. For example... Figure 5 As shown, the method 500 may specifically include the following steps.
[0099] In step 502, computer device 100 may create a target object for storing the text conversion results (e.g., Figure 3A Target objects 300).
[0100] In step 504, the computer device 100 responds to recognizing the first tag in the text (e.g., Figure 2 The first label 202) and the second label (e.g., Figure 2 The second tag 204) can, based on the hierarchical relationship between the first tag and the second tag, create a first scope in the target object corresponding to the first tag and the second tag respectively (e.g., ...). Figure 3C First scope 304) and second scope (e.g., Figure 3D The second scope (306). Wherein, the second scope is a sub-scope of the first scope, such as... Figure 3D As shown.
[0101] In some embodiments, in response to identifying the first label and the second label in the text, a first scope and a second scope corresponding to the first label and the second label respectively are created in the target object according to a hierarchical relationship between the first label and the second label, including:
[0102] storing information of the first scope and the second scope in a scope stack (e.g., scope stack 400 of Figure 4A ); wherein the information of the first scope is stacked before the information of the second scope, as shown in Figure 4C ; and
[0103] determining the hierarchical relationship between the first label and the second label according to the information stored in the scope stack, for example, the information of the first scope corresponding to the first label is stacked first, thus the second label is determined to be a child label of the first label according to the stacking order.
[0104] In some embodiments, in response to identifying the first label and the second label in the text, a first scope and a second scope corresponding to the first label and the second label respectively are created in the target object according to a hierarchical relationship between the first label and the second label, including:
[0105] in response to identifying a first label start character (e.g., character 2022 of Figure 2 ) of the first label in the text, creating the first scope in the target object and storing information (e.g., "1" of Figure 4B ) of the first scope in the scope stack; and
[0106] in response to identifying a second label start character (e.g., character 2042 of Figure 2 ) of the second label in the text and not identifying a first label end character (e.g., character 2032 of Figure 2 ) corresponding to the first label start character, creating the second scope as a child scope of the first scope in the target object and storing information (e.g., "2" of Figure 4C ) of the second scope in the scope stack.
[0107] At step 506, the computer device 100 can store label names, attribute names and attribute values corresponding to the first label and the second label respectively in the first scope and the second scope.
[0108] In some embodiments, storing label names, attribute names and attribute values corresponding to the first label and the second label respectively in the first scope and the second scope, including:
[0109] after identifying the first label start character and before identifying the second label start character, in response to identifying a first character or a first string of characters (e.g., the string 2024) in the text that conforms to a label naming convention, storing the first character or the first string of characters into the first scope as a label name of the first label; and Figure 2
[0110] in response to identifying a character or a string of characters in the text that does not conform to the label naming convention, storing a character or a string of characters (e.g., the string 2026) that satisfies an attribute naming convention into the first scope as an attribute name of the first label, and storing a character or a string of characters (e.g., the string 2030) after an attribute assignment character (e.g., the character 2028) into the first scope as an attribute value of the first label. Figure 2 Figure 2 Figure 2
[0111] In some embodiments, storing the corresponding label name, attribute name, and attribute value of the first label and the second label in the first scope and the second scope, respectively, includes:
[0112] after identifying the second label start character and before identifying other label start characters and the second label end character, in response to identifying a second character or a second string of characters (e.g., the character 2044) in the text that conforms to a label naming convention, storing the second character or the second string of characters into the second scope as a label name of the second label; and Figure 2
[0113] in response to identifying a character or a string of characters in the text that does not conform to the label naming convention, storing a character or a string of characters (e.g., the string 2046) that satisfies an attribute naming convention into the second scope as an attribute name of the second label, and storing a character or a string of characters (e.g., the string 2050) after an attribute assignment character (e.g., the character 2048) into the second scope as an attribute value of the second label. Figure 2 Figure 2 Figure 6
[0114] In some embodiments, the method 500 further includes:
[0115] in response to identifying the second label end character corresponding to the second label start character in the text, popping the information of the second scope from the scope stack; or
[0116] In response to identifying, in the text, a first tag end character corresponding to the first tag start character, popping the information of the first scope from the scope stack.
[0117] In some embodiments, the text is SVG text, and the target object is a JSON object.
[0118] In some embodiments, the method 500 further includes rendering the target object using WebGL.
[0119] It should be noted that the method of the embodiments of the present disclosure can be executed by a single device, such as a computer or a server, etc. The method of the embodiments can also be applied to a distributed scenario, and be completed by multiple devices cooperating with each other. In the case of such a distributed scenario, one of the multiple devices can only execute one or more steps in the method of the embodiments of the present disclosure, and the multiple devices can interact with each other to complete the method.
[0120] It should be noted that some embodiments of the present disclosure have been described above. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in a different order and still achieve desirable results. Additionally, the processes depicted in the figures do not necessarily require the particular order shown, or sequential order, to achieve the desired results. In certain implementations, multitasking and parallel processing can be advantageous.
[0121] The embodiments of the present disclosure further provide a device for detecting a frame rate. A schematic diagram of an exemplary device 600 provided by the embodiments of the present disclosure is shown. The device 600 can include the following structures.
[0122] A creating module 602 is configured to create a target object for saving a conversion result.
[0123] A converting module 604 is configured to, in response to identifying, in the text, the first tag and the second tag, create, according to a hierarchical relationship between the first tag and the second tag, a first scope and a second scope corresponding to the first tag and the second tag respectively in the target object, the second scope being a child scope of the first scope, and store the first tag and the second tag corresponding tag names, attribute names and attribute values respectively in the first scope and the second scope.
[0124] In some embodiments, the conversion module 604 is configured to store information of the first scope and the second scope by using a scope stack; wherein the information of the first scope is stacked before the information of the second scope; and determine the hierarchical relationship between the first label and the second label according to the information stored in the scope stack.
[0125] In some embodiments, the conversion module 604 is configured to, in response to identifying a first label start character of the first label in the text, create the first scope in the target object and store information of the first scope in the scope stack; and in response to identifying a second label start character of the second label in the text and not identifying a first label end character corresponding to the first label start character, create the second scope as a child scope of the first scope in the target object and store information of the second scope in the scope stack.
[0126] In some embodiments, the conversion module 604 is configured to, in response to identifying a second label end character corresponding to the second label start character in the text, pop the information of the second scope from the scope stack; or in response to identifying a first label end character corresponding to the first label start character in the text, pop the information of the first scope from the scope stack.
[0127] In some embodiments, the conversion module 604 is configured to, after identifying the first label start character and before identifying the second label start character, in response to identifying a first character or a first character string that meets the label naming specification in the text, store the first character or the first character string into the first scope as a label name of the first label; and in response to identifying a character or a character string that does not meet the label naming specification in the text, store a character or a character string that meets the attribute naming specification into the first scope as an attribute name of the first label, and store a character or a character string after an attribute assignment character into the first scope as an attribute value of the first label.
[0128] In some embodiments, the conversion module 604 is configured to, after identifying the second label start character and before identifying other label start characters and the second label end character, in response to identifying a second character or a second character string in the text that conforms to a label naming specification, store the second character or the second character string into the second scope as a label name of the second label; and in response to identifying a character or a character string in the text that does not conform to the label naming specification, store a character or a character string that satisfies an attribute naming specification into the second scope as an attribute name of the second label, and store a character or a character string after an attribute assignment character into the second scope as an attribute value of the second label.
[0129] In some embodiments, the text is an SVG text, and the target object is a JSON object.
[0130] In some embodiments, the apparatus 600 further comprises a rendering module 606 configured to render the target object by using WebGL.
[0131] For ease of description, the above apparatus is described in various modules respectively according to functions. Of course, in the implementation of the present disclosure, the functions of each module can be implemented in one or more software and / or hardware.
[0132] The apparatus of the above embodiments is used to implement the corresponding method 500 in any of the preceding embodiments, and has the beneficial effects of the corresponding method embodiments, which are not described here again.
[0133] Based on the same inventive concept, the present disclosure also provides a non-transitory computer-readable storage medium storing computer instructions for causing a computer to perform the method 500 of any of the above embodiments.
[0134] The computer-readable medium of the present embodiment includes permanent and non-permanent, removable and non-removable media, which can be implemented by any method or technology to store information. The information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible by a computing device.
[0135] The computer program product of the above embodiments is configured to cause the processor to perform the method 500 as described in any of the above embodiments, and has the beneficial effects of the corresponding method embodiments, which will not be repeated here.
[0136] Based on the same inventive concept, the disclosure also provides a computer program product comprising a computer program corresponding to the method 500 of any of the above embodiments. In some embodiments, the computer program is executable by one or more processors to cause the processor to perform the method 500. The processor performing the corresponding step can belong to the corresponding execution subject corresponding to each step in each embodiment of the method 500.
[0137] The computer program product of the above embodiments is configured to cause the processor to perform the method 500 as described in any of the above embodiments, and has the beneficial effects of the corresponding method embodiments, which will not be repeated here.
[0138] Those skilled in the art will understand that the discussion of any of the above embodiments is merely exemplary and is not intended to suggest that the scope of the disclosure (including the claims) is limited to these examples; the embodiments or technical features among different embodiments can be combined, the steps can be implemented in any order, and there are many other variations of the aspects of the embodiments of the disclosure as described above, which are not provided in detail in order to be brief..
[0139] In addition, in order to simplify the description and discussion, and so as not to make the embodiments of the disclosure difficult to understand, the known power / ground connections of integrated circuit (IC) chips and other components can or can not be shown in the provided drawings. In addition, the devices can be shown in the form of block diagrams in order to avoid making the embodiments of the disclosure difficult to understand, and this also takes into account the fact that the details of the implementation of these block diagram devices are highly dependent on the platform on which the embodiments of the disclosure are to be implemented (i.e., these details should be fully within the understanding of those skilled in the art). Where specific details (e.g., circuitry) are set forth in order to describe an illustrative embodiment of the disclosure, it will be apparent to those skilled in the art that the embodiments of the disclosure can be practiced without these specific details or with variations on these specific details. Therefore, these descriptions should be considered as illustrative rather than limiting.
[0140] Although the disclosure has been described in conjunction with specific embodiments thereof, many alternatives, modifications and variations will be apparent to those skilled in the art in light of the foregoing description. For example, other memory architectures (e.g., dynamic RAM (DRAM)) can use the embodiments discussed.
[0141] Embodiments of the present disclosure are intended to cover all such alternatives, modifications, and variations as fall within the broad scope of the appended claims. Accordingly, any one or more of the omitted, modified, equivalently replaced, improved, and the like, as long as it is within the spirit and principle of the embodiments of the present disclosure, should be included in the scope of protection of the present disclosure.< / g> < / g>
Claims
1. A method of converting text, wherein, The text comprises a first label and a second label, the second label being a sub-label of the first label, and the method comprises: creating a target object for saving a text conversion result; performing character-by-character parsing on the text; in response to identifying the first label and the second label in the text, creating a first scope and a second scope corresponding to the first label and the second label respectively in the target object according to a hierarchical relationship between the first label and the second label, the second scope being a sub-scope of the first scope; and storing label names, attribute names and attribute values corresponding to the first label and the second label respectively in the first scope and the second scope; wherein, in response to identifying the first label and the second label in the text, creating the first scope and the second scope corresponding to the first label and the second label respectively in the target object according to the hierarchical relationship between the first label and the second label comprises: storing information of the first scope and the second scope in a scope stack; wherein the scope stack is used to maintain a hierarchical relationship of labels, and the information of the first scope is stacked before the information of the second scope; and determining the hierarchical relationship between the first label and the second label according to the information stored in the scope stack.
2. The method of claim 1, wherein, in response to identifying the first label and the second label in the text, creating the first scope and the second scope corresponding to the first label and the second label respectively in the target object according to the hierarchical relationship between the first label and the second label comprises: in response to identifying a first label start character of the first label in the text, creating the first scope in the target object and storing information of the first scope in the scope stack; and in response to identifying a second label start character of the second label in the text and not identifying a first label end character corresponding to the first label start character, creating the second scope as a sub-scope of the first scope in the target object and storing information of the second scope in the scope stack.
3. The method of claim 2, further comprising: in response to identifying a second label end character corresponding to the second label start character in the text, popping the information of the second scope from the scope stack; or in response to identifying a first label end character corresponding to the first label start character in the text, popping the information of the first scope from the scope stack. storing label names, attribute names and attribute values corresponding to the first label and the second label respectively in the first scope and the second scope comprises:
4. The method of claim 2, wherein, in response to identifying a first character or a first character string in the text that conforms to a label naming specification, storing the first character or the first character string into the first scope as a label name of the first label before the first label start character is identified and before the second label start character is identified; and in response to identifying a character or a character string in the text that does not conform to the label naming specification, storing a character or a character string that satisfies an attribute naming specification into the first scope as an attribute name of the first label, and storing a character or a character string after an attribute assignment character into the first scope as an attribute value of the first label.
5. The method of claim 4, wherein, storing corresponding label names, attribute names and attribute values of the first label and the second label in the first scope and the second scope respectively, including: in response to identifying a second character or a second character string in the text that conforms to a label naming specification, storing the second character or the second character string into the second scope as a label name of the second label after the second label start character is identified and before other label start characters and the second label end character are identified; and in response to identifying a character or a character string in the text that does not conform to the label naming specification, storing a character or a character string that satisfies an attribute naming specification into the second scope as an attribute name of the second label, and storing a character or a character string after an attribute assignment character into the second scope as an attribute value of the second label.
6. The method of any one of claims 1-5, wherein, the text is an SVG text, and the target object is a JSON object.
7. The method of claim 6, further comprising: rendering the target object using WebGL.
8. An apparatus for converting text, wherein, the text includes a first label and a second label, the second label being a child label of the first label, and the apparatus includes: a creating module configured to create a target object for saving a conversion result; a converting module configured to: perform character-by-character parsing on the text; in response to identifying the first label and the second label in the text, create a first scope and a second scope corresponding to the first label and the second label respectively in the target object according to a hierarchical relationship between the first label and the second label, the second scope being a child scope of the first scope; and store corresponding label names, attribute names and attribute values of the first label and the second label in the first scope and the second scope respectively; wherein in response to identifying the first label and the second label in the text, creating a first scope and a second scope corresponding to the first label and the second label respectively in the target object according to a hierarchical relationship between the first label and the second label includes: storing information of the first scope and the second scope using a scope stack; wherein the scope stack is used to maintain a hierarchical relationship of labels, and information of the first scope is stacked before information of the second scope; and According to information stored in the scope stack, a hierarchical relationship between the first label and the second label is determined.
9. A computer device comprising one or more processors, memory; and one or more programs, wherein the one or more programs are stored in the memory and executed by the one or more processors, the programs including instructions for performing the method according to any one of claims 1-7.
10. A non-transitory computer-readable storage medium containing a computer program which, when executed by one or more processors, causes the processors to perform the method of any one of claims 1-7.
11. A computer program product comprising computer program instructions which, when run on a computer, cause the computer to perform the method of any one of claims 1-7.
Citation Information
Patent Citations
Method for indirect access to controller data using name stored in string tag
CN101295169A
Canvas code generation method and device, electronic equipment and storage medium
CN107291476A