A method and system for exporting Chinese vector documents across renderers based on layout tree
By constructing a shared layout tree and processing text lines character by character, and sharing font byte data, the problem of vertical position offset and character encoding inconsistency in Chinese vector documents in cross-renderer scenarios is solved. This achieves position consistency and character integrity between the on-screen display and the exported document, and improves cross-renderer compatibility and reusability.
Patent Information
- Application Number
- CN202611587578.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-14
- Publication Date
- 2026-11-17
AI Technical Summary
In cross-renderer scenarios, inconsistent font processing during the on-screen rendering and export of Chinese vector documents leads to vertical position offsets, line spacing changes, and inconsistent character encoding mappings, making it difficult to maintain consistency in the exported results across different readers.
Construct a shared layout tree, process text lines character by character and share font byte data, measure the text drawing operation through the text node of the screen rendering channel to obtain the rising metric value, convert the baseline alignment ordinate, and draw Chinese text character by character. Use standard fonts to draw barcode numeric text in whole segments to ensure that the font metric of the screen display and the exported document are consistent.
It achieves vertical alignment consistency between the screen display and the exported document, ensuring correct display of Chinese text and high barcode recognition rate, avoiding character loss and garbled text, and improving compatibility and reusability across renderer scenarios.
Smart Images

Figure CN123115247A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of electronic document processing technology, and in particular to a method and system for exporting Chinese vector documents across renderers based on a layout tree. Background Technology
[0002] In the field of Chinese vector document export, screen rendering and export output are typically handled by different modules. The screen side handles page layout, font loading, and graphics drawing, while the export side uses a document parser, print driver, or third-party library to retrieve the page content and generate vector files such as PDF and SVG. Text, lines, rectangles, circles, polylines, and image objects on the page are individually converted into drawing instructions for the export format. This mode is simple to implement and is well-suited for documents with fixed formats.
[0003] Font processing typically involves loading system fonts independently via an export channel or embedding complete font files. Text is often merged by line or paragraph and written as a complete string to a vector document. The mapping between glyphs and encodings of Chinese text relies on the font's internal character mapping table, with minimal character-by-character verification of source character encodings during the export process. Coordinate positioning uses the top of the page or text box as the anchor point, calculating text position based on the export module's own font metrics. Special text such as barcodes and numbers often uses the same drawing logic as ordinary text.
[0004] The font sources, versions, or measurement parameters used in the display channel and the export channel may differ. Differences in the rise, fall, and line height of the same text on the screen and in the exported document can cause vertical position shifts, line spacing changes, and even page truncation. When Chinese text is merged as a whole, the character encoding mapping cannot maintain a correspondence with the characters in the source document, easily leading to missing characters, garbled text, or loss of copied retrieval information when embedding glyph subsets. For text sensitive to character spacing and continuous rendering, such as barcodes and numbers, processing them character by character as ordinary text may compromise barcode legibility. In cross-renderer scenarios, different platforms interpret text anchor points, baselines, and page coordinates inconsistently, making it difficult to maintain consistency in export results across various readers. Summary of the Invention
[0005] This invention provides a method and system for exporting Chinese vector documents across renderers based on a layout tree, which can solve the problems in the prior art.
[0006] A first aspect of this invention provides a method for exporting Chinese vector documents across renderers based on a layout tree, comprising: Construct a layout tree shared by the document to be exported and the screen rendering channel. The layout tree records the position and style information of text lines and graphic objects in each page. The text lines are retained word by word and are not merged. Traverse the layout tree and convert the layout content of each page into an intermediate drawing instruction sequence. The intermediate drawing instructions describe vector drawing operations of text, line segments, rectangles, circles, polylines and image types. For text drawing operations, obtain the same font byte data shared with the screen rendering channel, and use the font byte data for both font registration in the screen rendering channel and font embedding in the exported document, so that the screen rendering channel and the exported document use the same font measurement. The text corresponding to the text drawing operation is measured by the text node of the screen rendering channel. The rise measurement value of the text from the top to the baseline is obtained. Based on the rise measurement value and the page height, the top alignment anchor point of the screen rendering channel is converted into the baseline alignment ordinate of the exported document. For Chinese text, each character is drawn individually, and an encoding mapping consistent with the source character is established for each character. For barcode numeric text, the entire paragraph is drawn using a standard font. All vector drawing operations are drawn onto the exported document page according to the converted coordinates, embedding the actual glyph subset used and outputting the vector exported document.
[0007] Traverse the layout tree to generate an intermediate drawing instruction sequence, including: Starting from the page in the layout tree, the layout nodes in the page are recursively traversed in the order of header area, body area, footer area and overlay. For each layout node traversed, an intermediate drawing instruction of the corresponding type is generated according to its node type. The types of intermediate drawing instructions include text, line segment, rectangle, circle, polyline and image. All intermediate drawing instructions generated for each page are encapsulated into a page drawing instruction set for that page. The page drawing instruction set includes the page width, page height, and a sequence of intermediate drawing instructions arranged in the drawing order, which is used to completely describe all vector drawing content of a single page. For text-type layout nodes, the text content is split into character-by-character text operations without merging adjacent text operations, so that each character corresponds to an independent text drawing instruction, providing a foundation for subsequent character-by-character drawing and character-by-character encoding mapping; The intermediate drawing instruction sequence serves as the drawing data source shared by the screen display rendering channel and the exported document, enabling the screen display rendering and the exported document to be drawn based on the same intermediate drawing instruction sequence. This ensures that the screen display content and the exported document content come from the same layout tree and share the same layout information and drawing data.
[0008] Generate intermediate drawing instructions, including: For each layout node traversed, obtain the vertical coordinate, horizontal coordinate, and height of the layout object corresponding to that layout node in the screen display coordinate system. The screen display coordinate system takes the top of the page as the origin, and the vertical coordinate increases downward from the top of the page. The height of the layout object is its vertical dimension in the screen display coordinate system. Subtract the sum of the vertical coordinate and height of the layout object from the page height to obtain the vertical coordinate distance of the layout object from the bottom of the page upwards in the exported document coordinate system. The vertical coordinate distance is the distance from the bottom of the layout object to the bottom of the page. The vertical coordinate distance is scaled according to the pixel-to-point conversion ratio, which is determined by the conversion relationship between the pixel unit of the screen display coordinate system and the point unit of the exported document coordinate system, to obtain the vertical coordinate of the layout object in the exported document coordinate system, which takes the bottom of the page as the origin and the vertical coordinate direction increases from the bottom of the page upwards. The drawing position of the layout object in the exported document is determined based on the scaled vertical coordinate and the horizontal coordinate of the layout object. Corresponding intermediate drawing instructions are generated to make the graphic elements in the exported document correspond to the graphic elements in the screen display in the vertical direction, ensuring that the vertical position of the graphic elements in the screen display and the exported document is consistent.
[0009] Retrieve font byte data shared with the screen rendering channel, including: Receive the font alias declared by the text drawing operation. The font alias contains the font family name and weight information. Match the font alias with the family names of each font family in the built-in font family one by one to determine the built-in font family corresponding to the font alias. Then read the font byte data from the font file corresponding to the built-in font family. When performing family name matching, the full family name matching method is used instead of using serif identifiers as substrings for inclusion matching, to avoid mismapping font aliases declared as serif font families to sans-serif font families, and to prevent sans-serif font families from being incorrectly included in the matching range. The font byte data is loaded into the same byte cache. The screen rendering channel reads the font byte data from the byte cache and registers it as a font face. The exported document reads the font byte data from the byte cache and performs subset embedding, so that the same font byte data can serve both screen rendering and exported document rendering at the same time. This ensures that the screen rendering channel and the exported document use the exact same font byte data for the same glyph, eliminating font measurement drift caused by different font byte sources between the screen rendering and the exported document, and guaranteeing consistent glyph measurements between the screen and the exported document.
[0010] Registering font byte data as a font face includes: For the same font family, register independent font faces with regular weight and bold weight respectively, so that the regular weight font face and the bold weight font face correspond to the glyph set of their respective weights in the font file. The font face is obtained by registering the font byte data in the screen rendering channel and is used for text measurement and text drawing in the screen rendering channel. This allows bold text to be rendered directly using the bold font face in the screen rendering channel, without performing composite bolding on the regular font weight. Composite bolding achieves the bolding visual effect by stretching or outlining the regular font weight, and will change the actual width of the font. When exporting a document and embedding fonts, the corresponding glyphs for regular weight and bold weight are embedded simultaneously. This ensures that the bold text in the exported document is drawn using the bold weight glyphs, without performing a composite bolding process on the regular weight glyphs. This ensures that the bold text in the exported document and the screen rendering channel uses the same glyphs. This ensures that bold text uses the same font width measurement in the on-screen rendering channel and the exported document, eliminating font width drift caused by the composite bolding process and guaranteeing that the layout position and line break position of bold text remain consistent in the on-screen and exported documents.
[0011] Measure the text corresponding to the text drawing operation and convert the baseline alignment ordinate, including: Using the text corresponding to the text drawing operation as a measurement sample, a text node with the same font style and font size as the text drawing operation is constructed in the screen rendering channel. The measurement sample has the same font family, font size and font weight as the text drawing operation. When the text is empty, a preset reference text is used as the measurement sample. The text node is measured at a single line height to obtain the actual bounding box rise measurement value of the measurement sample from the top to the baseline. The rise measurement value represents the vertical distance from the top of the text to the baseline in the screen rendering channel. The rise measurement value is actually measured by the text node in the screen rendering channel, rather than calculated from the font's built-in measurement value. Add the top alignment ordinate of the text drawing operation in the screen display to the rising metric value to obtain the baseline ordinate of the text drawing operation in the screen display coordinate system. The top alignment ordinate is the ordinate of the text when it is top aligned in the screen display rendering channel. Subtract the vertical coordinate of the baseline in the display coordinate system from the page height, and then scale it according to the pixel-to-point conversion ratio to convert it into the baseline alignment vertical coordinate of the exported document, so that the top alignment position of the display and the baseline alignment position of the exported document coincide in the vertical direction.
[0012] For Chinese text, each character is drawn individually, and an encoding mapping consistent with the source character is established for each character. For barcode numeric text, the entire segment is drawn using a standard font. All vector drawing operations are then plotted onto the exported document page using the converted coordinates, including: Each character is extracted sequentially from the text corresponding to the text drawing operation. The drawing position of the character is determined by aligning the horizontal cursor position of the character with the baseline. The character is drawn character by character, and an encoding mapping consistent with the source character is established for the character. The initial value of the horizontal cursor position is the starting horizontal coordinate of the text corresponding to the text drawing operation. After each character is drawn, the horizontal cursor position is updated with the character width of that character under the current font and font size to obtain the drawing position of the next character, so that adjacent characters are arranged end to end; The text drawing operation identifies the character segments in the text that belong to the barcode numbers. The character segments of the barcode numbers are drawn as a whole using the standard font built into the exported document, instead of drawing each character segment individually. The text outside the character segments is still drawn character by character, and an encoding mapping is established for each character. All vector drawing operations are drawn onto the exported document page using the converted coordinates. During drawing, only the actual subset of glyphs is embedded in the Chinese text, and the encoding mapping relationship between each glyph and the source character is preserved, so that the Chinese text in the exported document can be independently retrieved and copied, and the barcode numbers maintain the standard font width.
[0013] A second aspect of this invention provides a cross-renderer Chinese vector document export system based on a layout tree, comprising: The layout tree construction unit is used to construct a layout tree shared by the document to be exported and the screen display rendering channel. The layout tree records the position and style information of text lines and graphic objects in each page, wherein the text lines are retained in word-by-word form and are not merged. The instruction conversion unit is used to traverse the layout tree and convert the layout content of each page into an intermediate drawing instruction sequence. The intermediate drawing instructions describe vector drawing operations of text, line segments, rectangles, circles, polylines and image types. The font sharing unit is used for text drawing operations to obtain the same font byte data shared with the screen rendering channel. This font byte data is used for both font registration in the screen rendering channel and font embedding in the exported document, so that the screen rendering channel and the exported document use the same font measurement. The baseline conversion unit is used to measure the text corresponding to the text drawing operation using the text node of the screen rendering channel, obtain the rise measurement value of the text from the top to the baseline, and convert the top alignment anchor point of the screen rendering channel into the baseline alignment ordinate of the exported document based on the rise measurement value and the page height. The character-by-character drawing unit is used to draw each character of Chinese text one by one, and to establish an encoding mapping for each character that is consistent with the source character. For barcode digital text, the entire paragraph is drawn using a standard font. The vector output unit is used to draw all vector drawing operations onto the exported document page according to the converted coordinates, embed the actual used glyph subset, and output the vector exported document.
[0014] A third aspect of the present invention provides an electronic device, comprising: processor; Memory used to store processor-executable instructions; The processor is configured to invoke instructions stored in the memory to execute the aforementioned method.
[0015] A fourth aspect of the present invention provides a computer-readable storage medium having stored thereon computer program instructions that, when executed by a processor, implement the aforementioned method.
[0016] This method shares the layout tree with the on-screen rendering channel, ensuring that the on-screen display and document export originate from the same typesetting result. This avoids line spacing discrepancies, line break differences, and element misalignments caused by separate calculations from two different typesetting logics, guaranteeing that the exported content remains consistent with what is seen on the screen. Text lines are preserved character-by-character without merging, and the position, font, size, and encoding information of individual characters are fully retained, providing a data foundation for character-by-character rendering and subset embedding.
[0017] Vector drawing commands uniformly express text, lines, rectangles, circles, polylines, and images, with controllable document size and no distortion during scaling. Font byte data is shared between on-screen display and export, and glyph bitmaps and character widths are completely consistent. The same font metric eliminates glyph changes, kerning jitter, and baseline drift caused by font replacement, ensuring layout accuracy when Chinese characters and numbers are mixed.
[0018] The ascending metric is taken from the text node in the screen rendering channel. The top alignment anchor point is then converted into the baseline ordinate of the exported document. Page height is included in the calculation, ensuring the vertical position of the text matches the screen display and preventing overall upward or downward shifting. Each character is drawn individually and an encoding mapping consistent with the source character is established. Copying, pasting, searching, and accessibility reading all yield the correct characters. The entire barcode digital text is drawn using a standard font, so the scanning recognition rate is unaffected.
[0019] All vector drawing operations are written to the page using converted coordinates, and the actual subset of glyphs used are embedded accordingly. The exported document is independent and complete, and can be displayed correctly without relying on external fonts. The same layout tree can support multiple screen display channels and export formats, improving compatibility and reusability. Attached Figure Description
[0020] Figure 1 This is a flowchart illustrating a method for exporting Chinese vector documents across renderers based on a layout tree, according to an embodiment of the present invention. Figure 2 A comparison chart showing the measured vertical positional deviation of text lines across renderers; Figure 3 A graph showing the cumulative deviation of bold text character width as a function of the number of characters. Detailed Implementation
[0021] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0022] The technical solution of the present invention will be described in detail below with reference to specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.
[0023] Figure 1 This is a flowchart illustrating a method for exporting Chinese vector documents across renderers based on a layout tree, according to an embodiment of the present invention. The present invention provides a method for exporting Chinese vector documents across renderers based on a layout tree, comprising: Construct a layout tree shared by the document to be exported and the screen rendering channel. The layout tree records the position and style information of text lines and graphic objects in each page, wherein the text lines are retained in word-for-word form and are not merged. Traverse the layout tree and convert the layout content of each page into an intermediate drawing instruction sequence. The intermediate drawing instructions describe vector drawing operations of text, line segments, rectangles, circles, polylines, and image types. For text drawing operations, obtain the same font byte data shared with the screen rendering channel, and use the font byte data for both font registration in the screen rendering channel and font embedding in the exported document, so that the screen rendering channel and the exported document use the same font measurement. The text corresponding to the text drawing operation is measured by the text node of the screen rendering channel. The rise measurement value of the text from the top to the baseline is obtained. Based on the rise measurement value and the page height, the top alignment anchor point of the screen rendering channel is converted into the baseline alignment ordinate of the exported document. For Chinese text, each character is drawn individually, and an encoding mapping consistent with the source character is established for each character. For barcode digital text, the entire paragraph is drawn using a standard font. All vector drawing operations are drawn to the exported document page using the converted coordinates, embedding the actual glyph subset used and outputting the vector exported document.
[0024] Traverse the layout tree to generate an intermediate drawing instruction sequence, including: Starting from the page in the layout tree, the layout nodes in the page are recursively traversed in the order of header area, body area, footer area and overlay. For each layout node traversed, an intermediate drawing instruction of the corresponding type is generated according to its node type. The types of intermediate drawing instructions include text, line segment, rectangle, circle, polyline and image. All intermediate drawing instructions generated for each page are encapsulated into a page drawing instruction set for that page. The page drawing instruction set includes the page width, page height, and a sequence of intermediate drawing instructions arranged in the drawing order, which is used to completely describe all vector drawing content of a single page. For text-type layout nodes, the text content is split into character-by-character text operations without merging adjacent text operations, so that each character corresponds to an independent text drawing instruction, providing a foundation for subsequent character-by-character drawing and character-by-character encoding mapping; The intermediate drawing instruction sequence serves as the drawing data source shared by the screen display rendering channel and the exported document, enabling the screen display rendering and the exported document to be drawn based on the same intermediate drawing instruction sequence. This ensures that the screen display content and the exported document content come from the same layout tree and share the same layout information and drawing data.
[0025] When traversing the layout tree to generate the intermediate rendering instruction sequence, the page node is used as the starting point for traversal. Following a fixed order—header area, body text area, footer area, and overlay—a depth-first recursive traversal is performed on the layout nodes within the page. This sequential design ensures the correctness of the rendering hierarchy: header content is located in a fixed area at the top of the page, the body text area carries the main layout content, the footer area is located at the bottom of the page, and overlays are used for floating elements or annotations superimposed on other content. By strictly adhering to this order, the final generated intermediate rendering instruction sequence naturally reflects the visual overlay relationship of each area, eliminating the need for additional handling of hierarchy sorting issues in subsequent rendering stages.
[0026] During the recursive traversal, for each layout node encountered, an intermediate drawing instruction is generated based on the node type. A clear mapping exists between node types and instruction types: text layout nodes generate text-type drawing instructions, horizontal line or border line nodes generate line segment-type drawing instructions, rectangular background or color block nodes generate rectangle-type drawing instructions, circular graphic nodes generate circle-type drawing instructions, polyline or path nodes generate polyline-type drawing instructions, and image or picture nodes generate image-type drawing instructions. Each type of intermediate drawing instruction carries complete parameters required for that type of drawing operation. For example, text drawing instructions carry character content, font identifier, font size, color, and position coordinates; rectangle drawing instructions carry the top-left corner coordinates, width, height, and fill color; and image drawing instructions carry image data references and target area coordinates. These parameters are directly read from the style and position information of the layout tree nodes during the traversal phase, ensuring that the instruction content is completely consistent with the layout state recorded in the layout tree.
[0027] The processing method for text-type layout nodes differs significantly from other node types. Text nodes typically exist in the layout tree as text lines, where a single line may contain multiple consecutive characters. When generating intermediate drawing instructions, the content of the text line is split into character-by-character text execution, with each character constituting a separate text drawing instruction, and text drawing instructions corresponding to adjacent characters are not merged. The core significance of this design is that when multiple characters are merged into a single drawing instruction, vector document formats such as PDFs often rely on character encoding mapping information to reconstruct the string during text extraction or copying. If the merged instruction has incomplete encoding mapping, it will lead to garbled text or character loss in the text extraction results. By maintaining character-by-character independent drawing instructions, each character can independently establish an encoding mapping consistent with the source character, fundamentally eliminating the problem of Chinese characters not being correctly extracted in the exported document.
[0028] After recursively traversing all layout nodes for each page, all intermediate drawing instructions generated by that page are encapsulated into a page drawing instruction set. This set contains three core data components: page width, page height, and a sequence of intermediate drawing instructions arranged in drawing order. Page width and height are recorded in physical units, consistent with the page size recorded by the page node in the layout tree. The instruction sequence arranged in drawing order strictly follows the order in which instructions are generated during the traversal; that is, instructions generated earlier correspond to content drawn earlier, and instructions generated later correspond to content drawn later, thus naturally achieving a correspondence between drawing order and layout hierarchy. As a complete data unit, the page drawing instruction set can independently describe all vector drawing content of a single page. Subsequent screen rendering channels and document export generation processes use this set as input, eliminating the need to access the layout tree again.
[0029] The intermediate drawing instruction sequence serves as the shared drawing data source for both the on-screen rendering channel and the exported document. This shared mechanism ensures consistency between the on-screen display content and the exported document content at the architectural level. After receiving the set of page drawing instructions, the on-screen rendering channel converts these instructions into screen pixel drawing operations, ultimately presenting a visual interface to the user. Similarly, the exported document generation process receives the same set of page drawing instructions, converts these instructions into drawing operations supported by the vector document format, and finally generates a storable and distributable vector document file. Since the input data for both channels comes from the same traversal of the same layout tree, all layout information, text content, graphic parameters, and coordinate data are completely identical, eliminating the possibility of differences introduced by different data sources.
[0030] In its implementation, the recursive traversal function receives the current layout node as input, executes the corresponding instruction generation logic based on the node type, and recursively calls the list of child nodes of that node. For container nodes, they may not directly generate drawing instructions but exist as part of the child node organization structure; for leaf nodes, the corresponding intermediate drawing instructions are directly generated and appended to the end of the current page's instruction sequence. Overlay nodes, because they need to be superimposed on all other content, are processed after the header, body, and footer areas in the traversal order. Their generated instructions are located at the end of the page instruction sequence, naturally overlaying previously drawn content during drawing, without the need for an additional hierarchical management mechanism.
[0031] The position coordinates recorded in the text drawing instructions adopt the layout tree coordinate system during the traversal phase, which is the screen display coordinate system with the top-left corner of the page as the origin, the rightward direction as the positive horizontal axis, and the downward direction as the positive vertical axis. This coordinate system is consistent with the coordinate system of the screen display rendering channel, which can directly use this coordinate system for drawing. For the export document generation process, since some vector document formats (such as PDF) use a coordinate system with the bottom-left corner of the page as the origin and the upward direction as the positive vertical axis, the vertical coordinates need to be converted in subsequent steps. The intermediate drawing instruction sequence itself retains the original coordinates of the screen display coordinate system, and the coordinate conversion logic is handled centrally in the export document generation stage, thereby maintaining the format consistency of the intermediate drawing instructions and allowing the screen display rendering channel to ignore the coordinate conversion details.
[0032] The specific operation of text splitting is as follows: traverse the character sequence recorded in the text line nodes, read the horizontal position offset, font identifier, font size, and color style attributes of each character in the layout tree, and construct a text drawing instruction containing only that single character. The horizontal position of the character is directly obtained from the position information recorded character by character in the layout tree, without the need to recalculate the character spacing or character width during the instruction generation stage. This design ensures that the positional accuracy of each character is completely consistent with the layout tree's typesetting accuracy, avoiding cumulative errors introduced by re-estimating character positions, which is especially important for characters like Chinese characters whose width is strongly correlated with font size.
[0033] Generate intermediate drawing instructions, including: For each layout node traversed, obtain the vertical coordinate, horizontal coordinate, and height of the layout object corresponding to that layout node in the screen display coordinate system. The screen display coordinate system takes the top of the page as the origin, and the vertical coordinate increases downward from the top of the page. The height of the layout object is its vertical dimension in the screen display coordinate system. Subtract the sum of the vertical coordinate and height of the layout object from the page height to obtain the vertical coordinate distance of the layout object from the bottom of the page upwards in the exported document coordinate system. The vertical coordinate distance is the distance from the bottom of the layout object to the bottom of the page. The vertical coordinate distance is scaled according to the pixel-to-point conversion ratio, which is determined by the conversion relationship between the pixel unit of the screen display coordinate system and the point unit of the exported document coordinate system, to obtain the vertical coordinate of the layout object in the exported document coordinate system, which takes the bottom of the page as the origin and the vertical coordinate direction increases from the bottom of the page upwards. The drawing position of the layout object in the exported document is determined based on the scaled vertical coordinate and the horizontal coordinate of the layout object. Corresponding intermediate drawing instructions are generated to make the graphic elements in the exported document correspond to the graphic elements in the screen display in the vertical direction, ensuring that the vertical position of the graphic elements in the screen display and the exported document is consistent.
[0034] During the process of traversing the layout tree to generate intermediate drawing instructions, there is a fundamental difference in the origin and direction between the on-screen coordinate system and the exported document coordinate system. This is the core issue causing the positional offset of graphic elements in the exported document. The on-screen coordinate system has its origin at the top of the page, with the vertical coordinate increasing from top to bottom. However, the coordinate system used in the exported document (such as PDF) has its origin at the bottom of the page, with the vertical coordinate increasing from bottom to top. If the on-screen coordinates are not transformed and are directly written into the exported document, all graphic elements will be mirrored vertically, resulting in a completely disordered layout.
[0035] For each layout node encountered, three key geometric pieces of information need to be extracted from the layout object corresponding to that node: the ordinate of the object in the display coordinate system. Horizontal coordinates And the vertical dimension (i.e., height). .in, This indicates the pixel distance from the top edge of the layout object to the top of the page. This indicates the pixel distance from the left edge of the layout object to the left edge of the page. This represents the vertical pixel size of the layout object in the screen coordinate system. These three values are all read directly from the properties of the layout tree node, sharing the same layout data with the screen rendering pipeline, and do not require recalculation.
[0036] The first step in coordinate transformation is to calculate the position of the bottom edge of the layout object in the screen coordinate system, that is... This value represents the pixel distance from the bottom edge of the layout object to the top of the page. Since the exported document coordinate system has its origin at the bottom of the page, the ordinate of the bottom edge of the layout object in the exported document coordinate system is equal to the total page height minus the ordinate of the bottom edge of the layout object in the display coordinate system. Let the total height of the page in the display coordinate system (in pixels) be... The vertical distance from the bottom of the layout object to the bottom of the page is... for: ; The physical meaning is the pixel distance between the bottom edge of the layout object and the bottom of the page. Using the bottom edge as a coordinate reference point is consistent with the drawing convention of graphic objects in the exported document coordinate system, which usually uses the lower left corner as the anchor point, thus avoiding additional offset errors caused by mismatched anchor points.
[0037] The second step in coordinate transformation is converting pixel units to point units. The on-screen coordinate system uses pixels as its unit of measurement, while the exported document coordinate system uses points; there is a fixed conversion relationship between the two. Let the conversion ratio between pixels and points be... (i.e., each pixel corresponds to) (in pounds), then the ordinate of the layout object in the exported document coordinate system is... for: ; Conversion ratio The density of the exported document is determined by both the screen resolution and the point density. In a typical implementation, the exported document uses 72 points per inch (pt), while the screen resolution may be 96 pixels per inch (px) or other values. The value needs to be dynamically determined based on the actual screen resolution, rather than being hard-coded as a fixed value. This conversion ratio remains consistent throughout the entire document export process, applies uniformly to all layout objects in the layout tree, and ensures that the relative positional relationships between different objects are accurately preserved after unit conversion.
[0038] In the horizontal coordinate direction, both the on-screen coordinate system and the exported document coordinate system have their origin at the left side of the page and are oriented in the same direction. Therefore, the horizontal coordinate only needs to be converted from pixels to points; no orientation flipping is required. Let the converted point value of the layout object be... ,but: ; in The scaling ratio is the same as that used in the vertical coordinate conversion, ensuring that the scaling ratio in the horizontal and vertical directions is consistent and avoiding non-uniform deformation of graphic elements.
[0039] After the above two steps of transformation, the drawing position of the layout object in the exported document coordinate system is obtained. Using this coordinate as the anchor point, and combining the type of the layout object (text, line segment, rectangle, circle, polyline, or image), corresponding intermediate drawing instructions are generated. These intermediate drawing instructions describe the drawing operation using a unified data structure, including object type identifier, drawing position, size parameters, and style attributes (such as color, line width, and fill method). Subsequent stages then translate these intermediate drawing instructions into native drawing calls in a specific export format.
[0040] Taking a rectangle object as an example, the coordinates of the top-left corner of the layout object in the screen coordinate system are: Width is The height is After coordinate transformation, the coordinates of the lower left corner of the rectangle in the exported document's coordinate system are: Width converted to Height converted to The intermediate drawing instructions record these four values, as well as style information such as the rectangle's fill color and border color, fully describing the rectangle's drawing parameters in the exported document. For line segment objects, the same transformation process is performed on the screen coordinates of the start and end points to obtain the point coordinates of the two endpoints in the exported document's coordinate system. These are then combined with attributes such as line width and color to generate line segment drawing instructions.
[0041] In the coordinate processing of text objects, the intermediate drawing instructions generated here record the overall position information of the text line. The baseline alignment ordinate required for text drawing operations is calculated separately by a dedicated text measurement process and replaces the ordinate field in the intermediate drawing instructions in subsequent drawing stages. This layered processing mechanism decouples the coordinate transformation logic from the text measurement logic, allowing them to be maintained independently and reducing system complexity.
[0042] The entire coordinate transformation process is executed node-by-node during the layout tree traversal. After each layout node is processed, the corresponding intermediate drawing instructions are immediately generated and appended to the instruction sequence, eliminating the need to wait for all nodes to be traversed before processing. This streaming approach reduces the memory footprint of intermediate data, which is particularly important for long documents containing a large number of layout nodes. Because the coordinate information of each node in the layout tree is independent, there is no dependency between the coordinate transformation calculations of different nodes, making the transformation process inherently parallelizable. When performance optimization is required, the node traversal can be parallelized.
[0043] Through the aforementioned coordinate transformation mechanism, the vertical position of each graphic element in the exported document strictly corresponds to its visual position on the screen. Whether it is a text line, graphic box, separator line, or image, its relative positional relationship in the screen layout can be restored in the exported document, achieving precise consistency of the vertical position of graphic elements between the screen display and the exported document.
[0044] Retrieve font byte data shared with the screen rendering channel, including: Receive the font alias declared by the text drawing operation. The font alias contains the font family name and weight information. Match the font alias with the family names of each font family in the built-in font family one by one to determine the built-in font family corresponding to the font alias. Then read the font byte data from the font file corresponding to the built-in font family. When performing family name matching, the full family name matching method is used instead of using serif identifiers as substrings for inclusion matching, to avoid mismapping font aliases declared as serif font families to sans-serif font families, and to prevent sans-serif font families from being incorrectly included in the matching range. The font byte data is loaded into the same byte cache. The screen rendering channel reads the font byte data from the byte cache and registers it as a font face. The exported document reads the font byte data from the byte cache and performs subset embedding, so that the same font byte data can serve both screen rendering and exported document rendering at the same time. This ensures that the screen rendering channel and the exported document use the exact same font byte data for the same glyph, eliminating font measurement drift caused by different font byte sources between the screen rendering and the exported document, and guaranteeing consistent glyph measurements between the screen and the exported document.
[0045] After text drawing operations are converted into intermediate drawing instructions, it is necessary to determine the font byte data source used by each text drawing instruction. Each text drawing instruction carries a font alias field, which is composed of two parts: the font family name and font weight information. The font family name is used to identify the font family to which the font belongs, such as a specific Chinese font family name; the font weight information is used to distinguish different thickness variants under the same font family, such as regular weight and bold weight. After receiving the font alias, the font family name part therein is extracted, and is compared sequentially with the family names of all registered font families in the built-in font library to determine one by one whether there is an exact matching entry.
[0046] Family name matching adopts the method of full string equality comparison, rather than substring inclusion judgment. This design detail is particularly critical for scenarios where both serif font families and sans-serif font families exist in Chinese typesetting. If substring inclusion matching is adopted, when judging whether a font alias belongs to a certain font family, the names of sans-serif font families whose family names contain the word "serif" will also be included in the candidate range, because the name of the sans-serif font family itself contains the substring "serif". Taking Chinese font naming conventions as an example, the name of a certain sans-serif font family may be "sans-serif", and when a font alias declared as a serif font family undergoes substring inclusion matching, a false match will occur because the substring "serif" also appears in "sans-serif", resulting in the serif font being incorrectly mapped to the font file corresponding to the sans-serif font family. The full family name matching method requires that the two compared strings are completely consistent in length and content, which fundamentally eliminates the possibility of the above-mentioned incorrect mapping.
[0047] After determining the built-in font family corresponding to the font alias, the complete font byte data is read from the font file path corresponding to this font family. Font files are usually stored in binary format, containing complete font information such as glyph outlines, metrics tables, encoding mapping tables, and various OpenType or TrueType feature tables. The read font byte data is saved in a unified byte cache area in the form of a byte sequence. This byte cache remains valid throughout the entire document export lifecycle, and both the screen display rendering channel and the exported document rendering channel obtain font byte data from this cache, instead of reading the font files independently.
[0048] After receiving the font byte data, the screen display rendering channel registers it as a font face object that can be used for text rasterization. The font face registration process relies on the metrics table information in the font byte data, including key metric parameters such as ascent, descent, line spacing, and glyph width. These metric parameters directly determine the arrangement position and occupied space of each character on the canvas during screen display rendering. Since the font face is directly constructed from the font byte data in the byte cache, its metric parameters are completely consistent with the original data stored in the byte cache.
[0049] The exported document rendering channel also reads font byte data from the same byte cache and uses it for the font subset embedding process. Subset embedding refers to embedding only the font data corresponding to the glyphs actually used in the exported document, rather than the complete font file, to reduce the size of the exported document. During subset embedding, the font parser extracts the outline description and measurement information of each glyph based on the font byte data in the byte cache and writes it into the font resource area of the exported document. Since the byte data used for subset embedding and the byte data used when registering the font face in the screen rendering channel come from the same cache, the glyph measurements referenced by both are numerically identical.
[0050] Font metric drift is a common typesetting consistency problem in cross-renderer document export. When the on-screen rendering channel and the exported document load fonts from different sources, even if the same font is nominally used, slight discrepancies may occur in the obtained glyph width, rise, and other metrics due to differences in font file versions, font parsing library implementations, or byte order processing. This discrepancy may be only a few tenths of a pixel at the individual character level, but in a line containing dozens of Chinese characters, the accumulated discrepancy is enough to cause a noticeable misalignment of the end-of-line character between the on-screen and exported documents. By uniformly storing font byte data in the same byte cache and having it referenced by both rendering channels, the homogeneity of metric data is guaranteed at the byte level, completely eliminating metric drift caused by different font byte sources. Figure 2As shown, this is a grouped bar chart. The horizontal axis represents five target renderers: Chrome PDFium, Adobe Acrobat, Foxit Reader, macOS Preview, and WPS PDF. Each renderer is labeled with its category (browser engine, desktop reader, system-level preview, office suite). The vertical axis represents the vertical position deviation of text lines, in points, ranging from 0 to 5.2 points, with grid lines spaced at 0.5-point intervals. The chart contains two sets of bar data: red bars represent the vertical deviation of the traditional independent channel scheme, and cyan bars represent the vertical deviation of this scheme. The mean deviations of the red bars are 2.73 points, 1.78 points, 2.41 points, 4.32 points, and 3.06 points, respectively. Each bar has an error bar at the top, with a length of ±1 standard deviation, and the error bars are 0.34 points, 0.21 points, 0.29 points, 0.44 points, and 0.38 points, respectively, reflecting the combined effects of device differences, rendering engine differences, and measurement noise. The mean deviations of the cyan bars are 0.13 lb, 0.07 lb, 0.11 lb, 0.34 lb, and 0.09 lb, respectively, with error bars of 0.04 lb, 0.02 lb, 0.03 lb, 0.06 lb, and 0.03 lb, also within ±1 standard deviation. A horizontal green dashed line runs across the graph, labeled "Usable Threshold 0.5 lb." All bar heights in this design fall within this threshold, while all bar heights in the traditional design exceed this threshold.
[0051] When a font alias contains weight information, that weight information also participates in the font family selection process. Within a built-in font family, multiple font files with different weights may exist under the same family name; for example, one font file might correspond to a regular weight, and another to a bold weight. After completing a full family name match, the corresponding weight font file is selected from the matched font families based on the weight information, and the font byte data is read from that file and stored in the byte cache. If the weight declared by the font alias does not have a corresponding font file in the built-in font family, the process reverts to the default weight font file for that font family to ensure the robustness of the font loading process.
[0052] The byte cache is managed using a mapping structure with font aliases as keys. When the same font alias is referenced multiple times in a document, byte data is read from the font file and stored in the cache only on the first reference. Subsequent references retrieve the existing byte data directly from the cache, avoiding the performance overhead of repeatedly reading disk files. For complex documents containing a large number of different fonts, the caching mechanism also ensures that the byte data of each font remains a unique copy throughout the entire export process. The byte sequences referenced by the screen rendering channel and the exported document rendering channel point to the same data area at the memory address level, thus guaranteeing absolute consistency of font metrics at the physical level.
[0053] Registering font byte data as a font face includes: For the same font family, register independent font faces with regular weight and bold weight respectively, so that the regular weight font face and the bold weight font face correspond to the glyph set of their respective weights in the font file. The font face is obtained by registering the font byte data in the screen rendering channel and is used for text measurement and text drawing in the screen rendering channel. This allows bold text to be rendered directly using the bold font face in the screen rendering channel, without performing composite bolding on the regular font weight. Composite bolding achieves the bolding visual effect by stretching or outlining the regular font weight, and will change the actual width of the font. When exporting a document and embedding fonts, the corresponding glyphs for regular weight and bold weight are embedded simultaneously. This ensures that the bold text in the exported document is drawn using the bold weight glyphs, without performing a composite bolding process on the regular weight glyphs. This ensures that the bold text in the exported document and the screen rendering channel uses the same glyphs. This ensures that bold text uses the same font width measurement in the on-screen rendering channel and the exported document, eliminating font width drift caused by the composite bolding process and guaranteeing that the layout position and line break position of bold text remain consistent in the on-screen and exported documents.
[0054] During the font registration phase, for the same font family (e.g., a specific Chinese sans-serif or serif font), the font byte data corresponding to the regular and bold weights are loaded separately, and these are registered as independent font faces. Each font face holds its own independent glyph set reference, with no derivation or inheritance relationship between them. The font byte data directly originates from the raw binary content of the font file, and the font face is created in the on-screen rendering channel through the font registration interface. After registration, text measurement and rendering in the on-screen rendering channel are based on these two independent font faces. Regular weight text uses the regular font face for measurement and rendering, while bold weight text uses the bold font face for measurement and rendering.
[0055] Synthetic bolding is a technique used to simulate the visual effect of bolding by applying horizontal stretching or outline strokes to regular weight glyphs when a true bold weight font file is unavailable. This process expands the stroke width outward from the original glyph outline, causing the actual width occupied by each character to shift positively relative to the regular weight. When the typesetting system calculates the text layout based on the font measurement data of the regular weight, if the rendering layer actually outputs glyphs that have undergone synthetic bolding, a discrepancy will occur between the visual width and the measured width of the glyphs, leading to typesetting anomalies such as overlapping adjacent characters, overflowing characters at the end of lines, or premature line breaks. With a true bold weight font face available, bold text in the on-screen rendering channel is rendered directly using the bold weight font face, completely bypassing the synthetic bolding process and fundamentally eliminating the aforementioned width drift problem.
[0056] In the specific implementation, the font registration process reads two sets of font byte data corresponding to the font family and passes them to the font registration interface of the screen rendering channel, resulting in two independent font face handles, marked as the regular font face handle and the bold font face handle, respectively. When processing text nodes, the layout engine selects the corresponding font face handle based on the weight attribute carried by the text node: when the weight attribute is regular, the regular font face handle is selected; when the weight attribute is bold, the bold font face handle is selected. Text measurement operations (including single character width query, line height calculation, and ascending metric value acquisition) are all performed through the selected font face handle, ensuring that the measurement results strictly correspond to the geometric attributes of the glyphs used in the actual rendering.
[0057] The font embedding stage of the exported document shares the same font byte data as the font registration stage of the screen rendering channel. During the traversal of the layout tree and collection of text drawing operations, the actual font face type (regular or bold) and the set of characters involved are recorded for each text drawing operation. When embedding the font, the glyph subsets corresponding to the regular and bold weights are trimmed according to their respective actual character ranges, embedding only the glyphs actually used in the document into the exported document, ensuring glyph integrity while controlling the size of the exported document. Bold text is bound to the embedded font with the bold weight in the exported document and drawn directly using the bold weight glyphs, without relying on the composite bolding capabilities of the exported document renderer. For example... Figure 3As shown, this is a line graph. The horizontal axis represents the number of characters in the same line of bold Chinese text, listed sequentially as 1, 5, 10, 15, 20, 25, 30, 35, and 40 characters. The vertical axis represents the cumulative character width deviation in pixels, with a scale range from 0 to 3.6 pixels, and grid lines at 0.6-pixel intervals. The graph contains two curves: the red curve represents the composite bolding process (traditional method), and the cyan-green curve represents the actual bold weight (this method). The red curve has a deviation of 0.08 pixels at the 1st digit, 0.42 pixels at the 5th digit, 0.83 pixels at the 10th digit, 1.19 pixels at the 15th digit, 1.64 pixels at the 20th digit, 1.97 pixels at the 25th digit, 2.42 pixels at the 30th digit, 2.81 pixels at the 35th digit, and 3.21 pixels at the 40th digit. The slope fluctuates slightly between the data points and is not strictly linear. The curve is surrounded by a red shading band, representing ±1 standard deviation, with a standard deviation of 0.14 pixels at the 40th digit. The cyan-green curve has a deviation between 0.00 and 0.05 pixels across the entire range from 1st to 40th digit, exhibiting sub-pixel jitter. The curve closely follows the horizontal axis and is surrounded by a cyan-green shading band, with a standard deviation of approximately 0.02 pixels. A green dashed line runs across the graph and is labeled "line end overflow threshold 2.8 pixels". The red curve crosses this threshold between 35 and 40 characters, while the cyan curve remains below this threshold throughout.
[0058] Because different PDF renderers and document viewers have different strategies for implementing composite bolding, some viewers will automatically apply composite bolding when they detect that the font lacks the actual bold weight, while others will not. This results in the same document displaying inconsistent font widths and visual effects in different environments. By directly embedding the actual bold weight font directly into the exported document, the uncertainty caused by these renderer differences can be completely eliminated, ensuring that the exported document displays bold text with a consistent appearance with the on-screen rendering channel in any compliant viewer.
[0059] Consistency in font width measurement is crucial for Chinese text typesetting. Chinese characters are typically full-width characters, and regular and bold weights have the same advance width in standard font design. However, if the rendering layer applies a composite bolding process to regular glyphs, the actual rendered width will exceed the advance width recorded in the font measurement data, leading to character spacing compression and even character overlap. When drawing Chinese characters character by character, the drawing position of each character is determined by progressively adding the advance width based on the font face measurement data. If both the on-screen rendering channel and the exported document use actual bold weight glyphs from the same font byte data, the measurement results for the advance width of each character will be completely consistent, and the character drawing positions after progressively adding the advance width will also correspond perfectly, thus ensuring that the layout position and line break position of bold text remain consistent in both the on-screen display and the exported document.
[0060] For barcode numeric text, the font is usually a dedicated monospaced font, containing only numbers and a few auxiliary characters, and there are no bold weight variants. Therefore, the entire barcode numeric text is drawn using a standard font, without involving weight separation registration processing logic. For ordinary Chinese body text and title text, the text nodes already carry weight attribute tags when constructing the layout tree. When traversing the layout tree to generate the intermediate drawing instruction sequence, the text drawing instructions synchronously record the weight attribute. Subsequent font embedding and glyph drawing stages select the corresponding weight glyph based on this attribute, maintaining the complete transmission of weight information throughout the entire process.
[0061] In an architecture where the on-screen rendering channel and the export channel share a layout tree, the measurement results of text nodes are determined and cached in the layout tree during the layout phase. Since both channels register font faces based on the same font byte data and neither performs compositing bolding, the measurement results cached in the layout tree are valid for both channels, eliminating the need to recalculate measurements for the export channel. This feature allows the export process to directly reuse information such as text line positions, character widths, and ascending measurement values recorded in the layout tree. After converting the on-screen coordinates to the export document coordinates, they can be directly used for glyph drawing, ensuring a pixel-level correspondence between the export document and the on-screen content at the typographic level.
[0062] Measure the text corresponding to the text drawing operation and convert the baseline alignment ordinate, including: Using the text corresponding to the text drawing operation as a measurement sample, a text node with the same font style and font size as the text drawing operation is constructed in the screen rendering channel. The measurement sample has the same font family, font size and font weight as the text drawing operation. When the text is empty, a preset reference text is used as the measurement sample. The text node is measured at a single line height to obtain the actual bounding box rise measurement value of the measurement sample from the top to the baseline. The rise measurement value represents the vertical distance from the top of the text to the baseline in the screen rendering channel. The rise measurement value is actually measured by the text node in the screen rendering channel, rather than calculated from the font's built-in measurement value. Add the top alignment ordinate of the text drawing operation in the screen display to the rising metric value to obtain the baseline ordinate of the text drawing operation in the screen display coordinate system. The top alignment ordinate is the ordinate of the text when it is top aligned in the screen display rendering channel. Subtract the vertical coordinate of the baseline in the display coordinate system from the page height, and then scale it according to the pixel-to-point conversion ratio to convert it into the baseline alignment vertical coordinate of the exported document, so that the top alignment position of the display and the baseline alignment position of the exported document coincide in the vertical direction.
[0063] The core challenge in converting text positions from the on-screen rendering channel to coordinates in the exported document lies in the fundamental difference between the two coordinate systems' methods of vertical text positioning: the on-screen rendering channel uses the top of the text bounding box as the alignment anchor point, while vector document formats (such as PDF) use the character baseline as the reference for text placement. Without precise baseline conversion, even if the horizontal position is perfectly correct, the text will exhibit an overall upward or downward shift in the exported document. This is especially problematic for Chinese characters, as this deviation can lead to significant misalignment between the text and the surrounding graphics or table lines.
[0064] The measurement process begins with determining the measurement sample. Using the actual text content corresponding to the current text drawing operation as the measurement sample, a text node is constructed in the screen rendering pipeline. The font family, font size, and font weight of this text node are strictly consistent with the style information recorded in the text drawing operation. The reason for using actual text content instead of a fixed test string is that there are subtle differences in the actual rendering bounding boxes of different characters under the same font; using real content yields measurement results that most closely resemble the actual rendering effect. When the text content corresponding to the text drawing operation is an empty string, directly measuring an empty string will not yield a valid bounding box height. In this case, a preset reference text is used as the measurement sample. The preset reference text is typically a set of characters that can represent the typical character heights of the font, such as a mixed string containing uppercase letters, lowercase letters, and commonly used Chinese characters, to ensure that the measurement results have practical reference value.
[0065] After constructing the text node, it is measured at a single line height (i.e., a line height coefficient of 1.0) to eliminate the influence of line spacing on the bounding box height, thus obtaining a bounding box size that purely reflects the font rendering characteristics. After measurement, the vertical distance from the top to the baseline is extracted from the actual bounding box of the text node; this distance is the ascending metric value, denoted as . It is particularly important to emphasize that The ascender value must be actually measured by the text nodes in the on-screen rendering pipeline, and cannot be replaced by reading the ascender field stored internally in the font file. The font's built-in ascender field describes a theoretical value at the font design level, which often differs from the pixel-level bounding box generated when a specific rendering engine actually draws on a specific platform. Only by actually constructing and measuring the text nodes through the on-screen rendering pipeline can an ascender value that strictly corresponds to the on-screen rendering result be obtained, thus ensuring the accuracy of subsequent conversions.
[0066] Obtain the rising metric Next, add this to the top-aligned y-coordinate of the text drawing operation in the on-screen coordinate system to obtain the y-coordinate of the text baseline in the on-screen coordinate system. Let the top-aligned y-coordinate of the text drawing operation in the on-screen coordinate system be... (Units are pixels, origin is at the top of the page, downward is the positive direction), then the vertical coordinate of the baseline in the screen coordinate system is... satisfy: ; here The vertical position recorded by the text line node in the layout tree. These are the ascending metric values obtained from the actual measurements just now, both in pixels, added together. Also expressed in pixels. This step converts the description of "text box top position" in the on-screen coordinate system to the description of "text baseline position," laying the foundation for subsequent conversion to the exported document coordinate system.
[0067] There are two fundamental differences between the coordinate system of the exported document and the screen display coordinate system along the vertical axis: First, the origin of the exported document (taking PDF as an example) is located at the bottom left corner of the page, with the vertical axis pointing upwards, while the origin of the screen display coordinate system is located at the top left corner of the page, with the vertical axis pointing downwards. Second, the exported document uses points as the unit of length, while the screen display coordinate system uses pixels. To complete the coordinate transformation, let the total height of the page in the screen display coordinate system be... (pixels), the pixel-to-point conversion ratio is: (i.e., each pixel corresponds to) (pounds), then the ordinate of the text baseline in the exported document coordinate system. Calculated by the following formula: ; in Flip the coordinate system to convert "distance from the top of the page" to "distance from the bottom of the page", then multiply by the conversion ratio. Converting the units from pixels to points yields the baseline ordinates that can be directly used for exporting document text placement operations.
[0068] In practical engineering implementation, the conversion ratio The resolution is determined by both the screen display resolution and the document output resolution. For example, if the screen display rendering pipeline is laid out at a resolution of 96 pixels per inch, while the PDF standard is based on 72 points per inch, then... This means that each display pixel corresponds to 0.75 points. This conversion ratio remains fixed throughout the entire export process, regardless of the specific text content. It can be calculated once during the export task initialization phase and cached for reuse in all subsequent coordinate conversions.
[0069] The correctness of the above conversion method relies on a crucial premise: the on-screen rendering channel and the exported document use the same font byte data, and the font measurement remains consistent across both channels. If the on-screen rendering channel and the exported document use different font files or different font rendering parameters, the ascending measurement values for the same font size will differ, causing a shift in the baseline conversion result. By forcibly sharing the same font byte data during the export process and using this data simultaneously for font registration in the on-screen rendering channel and font embedding in the exported document, this potential source of error can be fundamentally eliminated, ensuring that the top alignment position of the on-screen display and the baseline alignment position of the exported document precisely coincide in the vertical direction, achieving a WYSIWYG export effect.
[0070] In document scenarios with dense Chinese characters, each character in the character-by-character drawing mode corresponds to an independent text drawing operation, and each operation needs to independently execute the aforementioned measurement and conversion process. Since characters within the same text line typically have the same font style and size, their ascending metric value... Similarly, the measurement results of the same font style and size combination can be cached to avoid repeatedly building text nodes and performing measurement operations for the same font configuration, thereby effectively reducing the computational overhead of the export process while ensuring the accuracy of the conversion.
[0071] For Chinese text, each character is drawn individually, and an encoding mapping consistent with the source character is established for each character. For barcode numeric text, the entire segment is drawn using a standard font. All vector drawing operations are then plotted onto the exported document page using the converted coordinates, including: Each character is extracted sequentially from the text corresponding to the text drawing operation. The drawing position of the character is determined by aligning the horizontal cursor position of the character with the baseline. The character is drawn character by character, and an encoding mapping consistent with the source character is established for the character. The initial value of the horizontal cursor position is the starting horizontal coordinate of the text corresponding to the text drawing operation. After each character is drawn, the horizontal cursor position is updated with the character width of that character under the current font and font size to obtain the drawing position of the next character, so that adjacent characters are arranged end to end; The text drawing operation identifies the character segments in the text that belong to the barcode numbers. The character segments of the barcode numbers are drawn as a whole using the standard font built into the exported document, instead of drawing each character segment individually. The text outside the character segments is still drawn character by character, and an encoding mapping is established for each character. All vector drawing operations are drawn onto the exported document page using the converted coordinates. During drawing, only the actual subset of glyphs is embedded in the Chinese text, and the encoding mapping relationship between each glyph and the source character is preserved, so that the Chinese text in the exported document can be independently retrieved and copied, and the barcode numbers maintain the standard font width.
[0072] When handling text rendering operations, a differentiated rendering strategy is adopted based on the different characteristics of Chinese characters and barcode numeric characters. For Chinese text, each character is extracted one by one, and its rendering position in the exported document page is determined by the current horizontal cursor position of the character and the converted baseline alignment ordinate. The initial value of the horizontal cursor position is taken from the starting horizontal coordinate of the text segment corresponding to the current text rendering operation. This starting horizontal coordinate is obtained by converting the position information of the corresponding text node in the layout tree. When rendering each character, a one-to-one encoding mapping is established between the character's Unicode encoding value and its corresponding glyph number in the exported document font. This ensures that the glyph carries textual semantic information completely consistent with the source character in the exported document, rather than merely existing as a graphic.
[0073] After completing the drawing and encoding mapping of a character, the system immediately queries the character width within the current embedded font and font size. This width value is then used to update the horizontal cursor position, yielding the starting horizontal coordinates of the next character. The character width query source shares the same font byte data with the screen rendering channel, ensuring that the spacing between adjacent characters in the exported document matches the screen display effect. Adjacent characters are visually aligned end-to-end, without character overlap or abnormal gaps. This character-by-character cursor mechanism guarantees the layout accuracy of Chinese text in the exported document and also provides the operational basis for independently establishing encoding mappings for each character.
[0074] For the text content corresponding to the text drawing operation, barcode numeric character fragments need to be recognized before processing each character individually. The recognition process scans the entire text, marking consecutive characters belonging to the barcode numeric set as barcode numeric fragments, and marking the remaining characters as ordinary text fragments. Barcode numerics typically refer to the numerical sequences used to generate barcodes or QR codes as auxiliary text. Their character widths must strictly conform to barcode specifications in printing scenarios, and width offsets due to font measurement differences are not allowed. For the recognized barcode numeric character fragments, they are drawn as a whole using the standard font built into the exported document. That is, the character fragment is passed into the text drawing interface as a whole, and the width of each numeric character is uniformly controlled by the measurement table of the built-in standard font, thereby ensuring that the barcode numerics present character spacing and overall width conforming to the standard font specifications in the exported document. The character fragment is not split and drawn character by character, nor is a custom encoding mapping created for it, because the built-in standard font itself has complete encoding support for numeric characters, requiring no additional processing.
[0075] For text content other than barcode numeric segments, whether Chinese characters or other non-barcode characters, the character-by-character drawing method continues to be used, and an encoding mapping consistent with the source character is established for each character. When a text segment contains both barcode numeric segments and ordinary text segments, the horizontal cursor seamlessly connects between the two drawing modes: the cursor advances character by character for ordinary text segments; after the barcode numeric segment is drawn, the cursor position is updated with the actual width occupied by the entire barcode text segment in the standard font; subsequent ordinary text segments continue to be drawn character by character from the updated cursor position, ensuring the horizontal continuity of the entire line of text.
[0076] When exporting all vector drawing operations to the export document page, only a subset of the glyphs actually involved in the drawing is embedded for Chinese text fonts, rather than the complete font file. The scope of the glyph subset is determined by the actual set of Chinese characters that appear during the entire export process. Unused glyphs are not written to the export document, thus effectively controlling the file size of the export document. During the glyph subset writing process, the mapping relationship between each glyph and its corresponding source character Unicode encoding value is synchronously written to the character mapping table of the export document, enabling the reader of the export document to restore the glyphs on the page to searchable and copyable Unicode text. This mechanism makes the exported vector document not only visually highly consistent with the on-screen content, but also has complete accessibility at the text semantic level. Users can directly select and copy Chinese text content in the document reader, and search engines can also index the Chinese text within the document.
[0077] In practical scenarios, a ticket page containing Chinese label text and barcode numbers is processed as follows: the Chinese label portion is drawn character by character, and an encoding mapping is established before embedding a subset of the glyphs. The barcode numbers are drawn as a whole using a built-in standard font to maintain standard character widths. Both parts are output to the same exported document page. In the final exported document, the Chinese text can be recognized by the reader as searchable Unicode text, and the barcode numbers maintain character widths consistent with barcode specifications. The overall layout and display results are visually consistent with the screen rendering channel. This differentiated processing strategy ensures document usability and text accessibility while also meeting the strict requirements for barcode character width accuracy, achieving a balance between text quality and barcode compatibility in Chinese vector document export.
[0078] A second aspect of this invention provides a cross-renderer Chinese vector document export system based on a layout tree, comprising: The layout tree construction unit is used to construct a layout tree shared by the document to be exported and the screen display rendering channel. The layout tree records the position and style information of text lines and graphic objects in each page, wherein the text lines are retained in word-by-word form and are not merged. The instruction conversion unit is used to traverse the layout tree and convert the layout content of each page into an intermediate drawing instruction sequence. The intermediate drawing instructions describe vector drawing operations of text, line segments, rectangles, circles, polylines and image types. The font sharing unit is used for text drawing operations to obtain the same font byte data shared with the screen rendering channel. This font byte data is used for both font registration in the screen rendering channel and font embedding in the exported document, so that the screen rendering channel and the exported document use the same font measurement. The baseline conversion unit is used to measure the text corresponding to the text drawing operation using the text node of the screen rendering channel, obtain the rise measurement value of the text from the top to the baseline, and convert the top alignment anchor point of the screen rendering channel into the baseline alignment ordinate of the exported document based on the rise measurement value and the page height. The character-by-character drawing unit is used to draw each character of Chinese text one by one, and to establish an encoding mapping for each character that is consistent with the source character. For barcode digital text, the entire paragraph is drawn using a standard font. The vector output unit is used to draw all vector drawing operations onto the exported document page according to the converted coordinates, embed the actual used glyph subset, and output the vector exported document.
[0079] A third aspect of the present invention provides an electronic device, comprising: processor; Memory used to store processor-executable instructions; The processor is configured to invoke instructions stored in the memory to execute the aforementioned method.
[0080] A fourth aspect of the present invention provides a computer-readable storage medium having stored thereon computer program instructions that, when executed by a processor, implement the aforementioned method.
[0081] This invention can be a method, apparatus, system, and / or computer program product. The computer program product may include a computer-readable storage medium having computer-readable program instructions loaded thereon for performing various aspects of the invention.
[0082] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for exporting Chinese vector documents across renderers based on a layout tree, characterized in that, include: Construct a layout tree shared by the document to be exported and the screen rendering channel. The layout tree records the position and style information of text lines and graphic objects in each page. The text lines are retained word by word and are not merged. Traverse the layout tree and convert the layout content of each page into an intermediate drawing instruction sequence. The intermediate drawing instructions describe vector drawing operations of text, line segments, rectangles, circles, polylines and image types. For text drawing operations, obtain the same font byte data shared with the screen rendering channel, and use the font byte data for both font registration in the screen rendering channel and font embedding in the exported document, so that the screen rendering channel and the exported document use the same font measurement. The text corresponding to the text drawing operation is measured by the text node of the screen rendering channel. The rise measurement value of the text from the top to the baseline is obtained. Based on the rise measurement value and the page height, the top alignment anchor point of the screen rendering channel is converted into the baseline alignment ordinate of the exported document. For Chinese text, each character is drawn individually, and an encoding mapping consistent with the source character is established for each character. For barcode digital text, the entire paragraph is drawn using a standard font. All vector drawing operations are drawn to the exported document page using the converted coordinates, embedding the actual glyph subset used and outputting the vector exported document.
2. The method according to claim 1, characterized in that, Traverse the layout tree to generate an intermediate drawing instruction sequence, including: Starting from the page in the layout tree, the layout nodes in the page are recursively traversed in the order of header area, body area, footer area and overlay. For each layout node traversed, an intermediate drawing instruction of the corresponding type is generated according to its node type. The types of intermediate drawing instructions include text, line segment, rectangle, circle, polyline and image. All intermediate drawing instructions generated for each page are encapsulated into a page drawing instruction set for that page. The page drawing instruction set includes the page width, page height, and a sequence of intermediate drawing instructions arranged in the drawing order, which is used to completely describe all vector drawing content of a single page. For text-type layout nodes, the text content is split into character-by-character text operations without merging adjacent text operations, so that each character corresponds to an independent text drawing instruction, providing a foundation for subsequent character-by-character drawing and character-by-character encoding mapping; The intermediate drawing instruction sequence serves as the drawing data source shared by the screen display rendering channel and the exported document, enabling the screen display rendering and the exported document to be drawn based on the same intermediate drawing instruction sequence. This ensures that the screen display content and the exported document content come from the same layout tree and share the same layout information and drawing data.
3. The method according to claim 2, characterized in that, Generate intermediate drawing instructions, including: For each layout node traversed, obtain the vertical coordinate, horizontal coordinate, and height of the layout object corresponding to that layout node in the screen display coordinate system. The screen display coordinate system takes the top of the page as the origin, and the vertical coordinate increases downward from the top of the page. The height of the layout object is its vertical dimension in the screen display coordinate system. Subtract the sum of the vertical coordinate and height of the layout object from the page height to obtain the vertical coordinate distance of the layout object from the bottom of the page upwards in the exported document coordinate system. The vertical coordinate distance is the distance from the bottom of the layout object to the bottom of the page. The vertical coordinate distance is scaled according to the pixel-to-point conversion ratio, which is determined by the conversion relationship between the pixel unit of the screen display coordinate system and the point unit of the exported document coordinate system, to obtain the vertical coordinate of the layout object in the exported document coordinate system, which takes the bottom of the page as the origin and the vertical coordinate direction increases from the bottom of the page upwards. The drawing position of the layout object in the exported document is determined based on the scaled vertical coordinate and the horizontal coordinate of the layout object. Corresponding intermediate drawing instructions are generated to make the graphic elements in the exported document correspond to the graphic elements in the screen display in the vertical direction, ensuring that the vertical position of the graphic elements in the screen display and the exported document is consistent.
4. The method according to claim 1, characterized in that, Retrieve font byte data shared with the screen rendering channel, including: Receive the font alias declared by the text drawing operation. The font alias contains the font family name and weight information. Match the font alias with the family names of each font family in the built-in font family one by one to determine the built-in font family corresponding to the font alias. Then read the font byte data from the font file corresponding to the built-in font family. When performing family name matching, the full family name matching method is used instead of using serif identifiers as substrings for inclusion matching, to avoid mismapping font aliases declared as serif font families to sans-serif font families, and to prevent sans-serif font families from being incorrectly included in the matching range. The font byte data is loaded into the same byte cache. The screen rendering channel reads the font byte data from the byte cache and registers it as a font face. The exported document reads the font byte data from the byte cache and performs subset embedding, so that the same font byte data can serve both screen rendering and exported document rendering at the same time. This ensures that the screen rendering channel and the exported document use the exact same font byte data for the same glyph, eliminating font measurement drift caused by different font byte sources between the screen rendering and the exported document, and guaranteeing consistent glyph measurements between the screen and the exported document.
5. The method according to claim 4, characterized in that, Registering font byte data as a font face includes: For the same font family, register independent font faces with regular weight and bold weight respectively, so that the regular weight font face and the bold weight font face correspond to the glyph set of their respective weights in the font file. The font face is obtained by registering the font byte data in the screen rendering channel and is used for text measurement and text drawing in the screen rendering channel. This allows bold text to be rendered directly using the bold font face in the screen rendering channel, without performing composite bolding on the regular font weight. Composite bolding achieves the bolding visual effect by stretching or outlining the regular font weight, and will change the actual width of the font. When exporting a document and embedding fonts, the corresponding glyphs for regular weight and bold weight are embedded simultaneously. This ensures that the bold text in the exported document is drawn using the bold weight glyphs, without performing a composite bolding process on the regular weight glyphs. This ensures that the bold text in the exported document and the screen rendering channel uses the same glyphs. This ensures that bold text uses the same font width measurement in the on-screen rendering channel and the exported document, eliminating font width drift caused by the composite bolding process and guaranteeing that the layout position and line break position of bold text remain consistent in the on-screen and exported documents.
6. The method according to claim 1, characterized in that, The text corresponding to the text drawing operation is measured and the baseline alignment ordinate is calculated, including: Using the text corresponding to the text drawing operation as a measurement sample, a text node with the same font style and font size as the text drawing operation is constructed in the screen rendering channel. The measurement sample has the same font family, font size and font weight as the text drawing operation. When the text is empty, a preset reference text is used as the measurement sample. The text node is measured at a single line height to obtain the actual bounding box rise measurement value of the measurement sample from the top to the baseline. The rise measurement value represents the vertical distance from the top of the text to the baseline in the screen rendering channel. The rise measurement value is actually measured by the text node in the screen rendering channel, rather than calculated from the font's built-in measurement value. Add the top alignment ordinate of the text drawing operation in the screen display to the rising metric value to obtain the baseline ordinate of the text drawing operation in the screen display coordinate system. The top alignment ordinate is the ordinate of the text when it is top aligned in the screen display rendering channel. Subtract the vertical coordinate of the baseline in the display coordinate system from the page height, and then scale it according to the pixel-to-point conversion ratio to convert it into the baseline alignment vertical coordinate of the exported document, so that the top alignment position of the display and the baseline alignment position of the exported document coincide in the vertical direction.
7. The method according to claim 1, characterized in that, For Chinese text, each character is drawn individually, and an encoding mapping consistent with the source character is established for each character. For barcode digital text, the entire paragraph is drawn using a standard font. All vector drawing operations will be plotted on the exported document page using the converted coordinates, including: Each character is extracted sequentially from the text corresponding to the text drawing operation. The drawing position of the character is determined by aligning the horizontal cursor position of the character with the baseline. The character is drawn character by character, and an encoding mapping consistent with the source character is established for the character. The initial value of the horizontal cursor position is the starting horizontal coordinate of the text corresponding to the text drawing operation. After each character is drawn, the horizontal cursor position is updated with the character width of that character under the current font and font size to obtain the drawing position of the next character, so that adjacent characters are arranged end to end; The text drawing operation identifies the character segments in the text that belong to the barcode numbers. The character segments of the barcode numbers are drawn as a whole using the standard font built into the exported document, instead of drawing each character segment individually. The text outside the character segments is still drawn character by character, and an encoding mapping is established for each character. All vector drawing operations are drawn onto the exported document page using the converted coordinates. During drawing, only the actual subset of glyphs is embedded in the Chinese text, and the encoding mapping relationship between each glyph and the source character is preserved, so that the Chinese text in the exported document can be independently retrieved and copied, and the barcode numbers maintain the standard font width.
8. A cross-renderer Chinese vector document export system based on a layout tree, used to implement the method as described in any one of claims 1-7, characterized in that, include: The layout tree construction unit is used to construct a layout tree shared by the document to be exported and the screen display rendering channel. The layout tree records the position and style information of text lines and graphic objects in each page, wherein the text lines are retained in word-by-word form and are not merged. The instruction conversion unit is used to traverse the layout tree and convert the layout content of each page into an intermediate drawing instruction sequence. The intermediate drawing instructions describe vector drawing operations of text, line segments, rectangles, circles, polylines and image types. The font sharing unit is used for text drawing operations to obtain the same font byte data shared with the screen rendering channel. This font byte data is used for both font registration in the screen rendering channel and font embedding in the exported document, so that the screen rendering channel and the exported document use the same font measurement. The baseline conversion unit is used to measure the text corresponding to the text drawing operation using the text node of the screen rendering channel, obtain the rise measurement value of the text from the top to the baseline, and convert the top alignment anchor point of the screen rendering channel into the baseline alignment ordinate of the exported document based on the rise measurement value and the page height. The character-by-character drawing unit is used to draw each character of Chinese text one by one, and to establish an encoding mapping for each character that is consistent with the source character. For barcode digital text, the entire paragraph is drawn using a standard font. The vector output unit is used to draw all vector drawing operations onto the exported document page according to the converted coordinates, embed the actual used glyph subset, and output the vector exported document.
9. An electronic device, characterized in that, include: processor; Memory used to store processor-executable instructions; The processor is configured to invoke instructions stored in the memory to execute the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having computer program instructions stored thereon, characterized in that, When the computer program instructions are executed by the processor, they implement the method described in any one of claims 1 to 7.
