Adaptive font rendering method and system based on AFM file
Through the adaptive font rendering method based on AFM files, the problem of inaccurate and insufficient flexibility in font metric control in traditional font rendering methods is solved, and efficient and clear font rendering in different display environments is achieved.
Patent Information
- Application Number
- CN202510413680.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-03
- Publication Date
- 2025-07-22
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Traditional font rendering methods cannot accurately control font metric parameters, resulting in character misalignment or uneven spacing, and lack flexibility, making it impossible to maintain consistent rendering effects in different display environments, especially on high-resolution or large-size devices that are prone to distortion.
Based on the adaptive font rendering method of AFM files, the font metric parameters are extracted by scanning the AFM files step by step, baseline mapping and adaptive control point set determination are performed, color intensity fields and adaptive triangulated grids are generated, and the rendered pixel matrix is finally generated to realize adaptive font rendering.
It realizes high-precision font rendering, adapts to different display devices and resolutions, ensures that characters maintain clarity and consistency under various display conditions, and improves rendering efficiency and effect.
Smart Images

Figure CN120354825A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of font rendering, and particularly relates to an adaptive font rendering method and system based on AFM files. Background Art
[0002] An AFM file is an Adobe font metrics file, which is usually used to store metric information about fonts, such as data on the width, height, kerning, baseline position, character spacing, etc. of glyphs.
[0003] Traditional methods usually cannot be controlled by precise font metric parameters and lack precise calculations based on font metric parameters. For example, traditional methods may ignore details such as character spacing and baseline position during the rendering process, resulting in inaccurate font rendering. There may be phenomena such as character misalignment or uneven spacing. Moreover, traditional font rendering methods generally adopt fixed control point sets and rendering strategies. These methods lack flexibility and cannot automatically optimize the rendering effect according to different font sizes, styles, resolutions, or device conditions. The rendering effect is prone to distortion in different display environments. Especially on high-resolution or large-size devices, traditional methods often cannot adapt to different display conditions, resulting in blurred or distorted fonts. And traditional font rendering methods usually use fixed grids or pixelated methods for rendering and cannot automatically adjust the density of the grid according to the details of the rendering object. Also, traditional methods cannot adaptively adjust according to changes in the display environment and often require manual configuration or optimization under specific conditions. In different devices, resolutions, or display environments, the font rendering effects of traditional methods may not be consistent, and may even cause blurred, distorted fonts or incompatibility with the screen sizes of different devices. Summary of the Invention
[0004] The technical problem to be solved by the present invention is to overcome the above-mentioned disadvantages of the prior art and provide an adaptive font rendering method and system based on AFM files.
[0005] The technical solution adopted to solve the above technical problem is: An adaptive font rendering method based on an AFM file, including:
[0006] Obtain a target AFM file, obtain a character sequence to be rendered, and extract font metric parameters by scanning the AFM file line by line and using regular expressions;
[0007] Perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain a baseline grid matrix corresponding to the character sequence to be rendered;
[0008] Determine an adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix;
[0009] Determine the color intensity field corresponding to the character sequence to be rendered according to the adaptive control point set corresponding to the character sequence to be rendered;
[0010] Determine the adaptive triangulation mesh corresponding to the character sequence to be rendered according to the color intensity field;
[0011] Determine the rendering pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation mesh, and perform adaptive font rendering on the character sequence to be rendered according to the rendering pixel matrix.
[0012] Preferably, the font metric parameters include character width, kerning, vertical displacement parameter, font bounding box, character path, standard horizontal stroke width, and standard vertical stroke width. Among them, the vertical displacement parameter includes ascender and descender, the font bounding box represents the minimum and maximum boundaries of the font, and the character path is used to describe the contour path of the character, where the contour path is composed of Bezier control points.
[0013] Preferably, perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain the baseline grid matrix corresponding to the character sequence to be rendered, including:
[0014] Convert the Unicode encoding of each character to be rendered in the character sequence to be rendered into an AFM internal index to obtain the character index vector corresponding to the character to be rendered;
[0015] Determine the horizontal offset vector corresponding to the character sequence to be rendered according to the character index vector and the font metric parameters;
[0016] Determine the baseline vertical coordinate of the character according to the font metric parameters and the vertical resolution of the target device;
[0017] Determine the vertical offset correction value corresponding to the character sequence to be rendered according to the font metric parameters and the vertical resolution of the target device;
[0018] Determine the baseline grid matrix corresponding to the character sequence to be rendered according to the horizontal offset vector, vertical offset correction value corresponding to the character sequence to be rendered, and the baseline vertical coordinate of the character.
[0019] Preferably, the calculation formula of the horizontal offset vector is as follows:
[0020]
[0021] where x k represents the starting horizontal position of the k-th character to be rendered in the character sequence to be rendered, Denote the (k - 1)-th character index vector i corresponding to the (k - 1)-th character to be rendered in the character sequence to be rendered k-1 The corresponding character width Denote the (k - 1)-th character index vector i corresponding to the (k - 1)-th character to be rendered in the character sequence to be rendered k-1 The corresponding inter-character spacing
[0022] The calculation formula for the vertical coordinate of the baseline of the character is as follows
[0023] y base = round(A + |D|·Y res )
[0024] where, y base denotes the vertical coordinate of the baseline of the character, A denotes the ascender in the vertical displacement parameter, D denotes the descender in the vertical displacement parameter, Y res denotes the vertical resolution of the target device, and round denotes the rounding function
[0025] The calculation formula for the vertical offset correction value of the character is as follows
[0026]
[0027] where, Δy k denotes the vertical offset correction value of the k-th character in the character sequence to be rendered
[0028] The calculation formula for the baseline grid matrix is as follows
[0029] G base [k, :] = (x k , y base + Δy k )
[0030] where, G base [k, :] denotes the baseline grid of the k-th character in the character sequence to be rendered
[0031] Preferably, determining the adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix includes
[0032] Converting a plurality of the Bezier control points into normalized coordinates relative to the baseline grid
[0033] Determining the discrete curvature field of the contour path according to the normalized coordinates of a plurality of the Bezier control points, and determining the curvature change rate gradient of the contour path according to the discrete curvature field
[0034] Determining the modulation displacement amount of the contour path according to the curvature change rate gradient
[0035] Determine the adaptive control points according to the modulation displacement of the contour path and the maximum curvature point of the contour path.
[0036] Preferably, the calculation formula of the normalized coordinates is as follows:
[0037]
[0038] where P norm represents the normalized coordinates of the Bezier control point, P x and P y represent the coordinates of the Bezier control point, and G base [k, x] represents the x coordinate in the baseline grid;
[0039] The calculation formula of the modulation displacement is as follows:
[0040]
[0041] where ΔP i represents the modulation displacement of the i-th Bezier control point in the contour path, α represents the Hinting coefficient, and κ i represents the curvature change rate of the i-th Bezier control point in the contour path, represents the curvature change rate gradient of the i-th Bezier control point in the contour path, and DPI represents the DPI value of the target device;
[0042] The calculation formula of the adaptive control point is as follows:
[0043] P adj = P + ΔP i · exp(-λ · d(P, P peak ));
[0044] where P adj represents the adaptive control point, λ represents the attenuation coefficient, and d(P, P peak ) represents the Euclidean distance between the Bezier control point P and the maximum curvature point P peak of the contour path.
[0045] Preferably, determining the color intensity field corresponding to the character sequence to be rendered according to the set of adaptive control points corresponding to the character sequence to be rendered includes:
[0046] Discretize the set of adaptive control points to the pixel grid of the target device according to the physical pixel size of the target device, where the calculation formula of the pixel grid is as follows:
[0047]
[0048] Among them, (u i , v i ) represents the control point coordinates in the pixel coordinate system, and represent the x - coordinate and y - coordinate of the adaptive control point, and W and H represent the pixel physical size of the target device;
[0049] Determine the anisotropic stroke width according to the pixel physical size of the target device and the font metric parameters, where the calculation formula of the anisotropic stroke width is as follows:
[0050]
[0051] Among them, σ x and σ y represent the anisotropic stroke width, and StdHW and StdVW represent the standard horizontal stroke width and the standard vertical stroke width;
[0052] Determine the Gaussian kernel set according to the anisotropic stroke width, where the calculation formula of the Gaussian kernel is as follows:
[0053]
[0054] Among them, G k (u, v) represents the Gaussian kernel, σ k represents the kernel function, and
[0055] Generate a color intensity field according to the Gaussian kernel set, where the calculation formula of the color intensity field is as follows:
[0056]
[0057] Among them, I k (u, v) represents the color intensity field, C pixel represents the set of control point coordinates in the pixel coordinate system, κ c represents the curvature value of the c - th control point coordinate in the set of control point coordinates in the pixel coordinate system, tahn represents the hyperbolic tangent function, and κ0 represents the curvature normalization threshold.
[0058] Preferably, determine the adaptive triangulation mesh corresponding to the character sequence to be rendered according to the color intensity field, including:
[0059] Determine the gradient tensor field through the color intensity field according to the central difference method, and determine the energy density field according to the gradient tensor field and the font metric parameters, where the calculation formula of the energy density field is as follows:
[0060]
[0061] Among them, E density (u, v) represents the energy density field, represents the gradient tensor field, and ε represents a parameter to prevent division by zero;
[0062] The adaptive triangulation mesh is determined according to the energy density field, where the calculation formula of the adaptive triangulation mesh is as follows:
[0063] V new = argmax(E density (u, v));
[0064] Among them, V new represents the adaptive triangulation mesh.
[0065] Preferably, determining the rendering pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation mesh includes:
[0066] Performing a Fourier transform on the adaptive triangulation mesh to obtain the frequency domain information corresponding to the adaptive triangulation mesh;
[0067] Performing high-frequency aliasing suppression on the frequency domain information according to the sinc function to obtain adjusted frequency domain information;
[0068] Performing an inverse Fourier transform on the adjusted frequency domain information to obtain an adjusted adaptive triangulation mesh;
[0069] Performing Gaussian kernel convolution on the adjusted adaptive triangulation mesh to obtain a smoothed adaptive triangulation mesh;
[0070] Determining the rendering pixel matrix of the smoothed adaptive triangulation mesh according to the convolution operation, where the calculation formula of the rendering pixel matrix is as follows:
[0071]
[0072] Among them, P out (u, v) represents the rendering pixel matrix, F represents the Fourier transform, F -1 represents the inverse Fourier transform, f represents the frequency, f c represents the cut-off frequency, and K blur represents the Gaussian kernel.
[0073] The technical solution adopted to solve the above technical problems is: an adaptive font rendering system based on an AFM file, which is applicable to the adaptive font rendering method based on the AFM file, including:
[0074] A parameter extraction unit, which is used to obtain the target AFM file, obtain the character sequence to be rendered, and extract font metric parameters by scanning the AFM file line by line and using regular expressions;
[0075] A baseline mapping unit, which is configured to perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain a baseline grid matrix corresponding to the character sequence to be rendered;
[0076] A control determination unit, which is configured to determine an adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix;
[0077] A color extraction unit, which is configured to determine a color intensity field corresponding to the character sequence to be rendered according to the adaptive control point set corresponding to the character sequence to be rendered;
[0078] A grid extraction unit, which is configured to determine an adaptive triangulated grid corresponding to the character sequence to be rendered according to the color intensity field;
[0079] A font rendering unit, which is configured to determine a rendered pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulated grid, and perform adaptive font rendering on the character sequence to be rendered according to the rendered pixel matrix.
[0080] The beneficial effects of the present invention are as follows: (1) By obtaining font metric parameters based on AFM files and extracting them using regular expressions, the present invention can accurately obtain the metric information of each character. Such accurate metric data can help better control details such as the size, spacing, and baseline position of characters during the rendering process, thereby ensuring high-precision rendering of fonts. Moreover, by dynamically determining an adaptive control point set based on font metric parameters and a baseline grid matrix, it can be optimized for different characters and different font sizes, styles, etc. The determination of the adaptive control point set ensures the flexibility of font rendering, can adapt to different display devices, resolutions, and environments, and thus provides better visual effects; (2) Based on the adaptive control point set, the present invention determines the color intensity field, which can achieve delicate rendering of characters. For example, more delicate control can be used for parts with more rendering details (such as the bends and small parts of characters), thereby avoiding pixelation and ensuring the smoothness and clarity of characters. This is particularly important when displaying on low-resolution devices or with large-sized fonts. And by generating an adaptive triangulation grid according to the color intensity field, the fineness of the grid can be dynamically adjusted. Smaller grids (finer triangles) are used for parts with rich details, and larger grids are used for relatively flat parts. This adaptive triangulation grid method not only improves the rendering accuracy but also optimizes the rendering performance and reduces the amount of calculation, especially significantly improving the efficiency in the rendering of complex fonts; (3) The present invention can perform adaptive adjustment according to the rendering environment (such as different resolutions and display devices), thereby ensuring that under various display conditions, the appearance and display effects of characters can achieve the best performance. This adaptive adjustment ability is one of its important features and can be widely applied to various screen and device types. BRIEF DESCRIPTION OF THE DRAWINGS
[0081] Figure 1 It is a schematic flowchart of the steps of the overall method in an embodiment proposed by the present invention;
[0082] Figure 2 It is a schematic diagram of the system architecture of the overall system in an embodiment proposed by the present invention.
[0083] Reference numerals: 1, parameter extraction unit; 2, baseline mapping unit; 3, control determination unit; 4, color extraction unit; 5, grid extraction unit; 6, font rendering unit. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0084] Embodiment 1, as Figure 1 shown, the adaptive font rendering method based on AFM files proposed by the present invention includes:
[0085] S1. Obtain the target AFM file, obtain the character sequence to be rendered, and extract font metric parameters by scanning the AFM file line by line and using regular expressions;
[0086] S2. Perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain the baseline grid matrix corresponding to the character sequence to be rendered;
[0087] S3. Determine the adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix;
[0088] S4. Determine the color intensity field corresponding to the character sequence to be rendered according to the adaptive control point set corresponding to the character sequence to be rendered;
[0089] S5. Determine the adaptive triangulation grid corresponding to the character sequence to be rendered according to the color intensity field;
[0090] S6. Determine the rendering pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation grid, and perform adaptive font rendering on the character sequence to be rendered according to the rendering pixel matrix.
[0091] In the present invention, the AFM file is an Adobe font metrics file, which is usually used to store font metrics information, such as data on the width, height, kerning, baseline position, character spacing, etc. of glyphs; the character sequence to be rendered refers to the set of characters or text to be rendered, and this sequence will be rendered according to the font metrics parameters extracted from the AFM file to ensure correct display; when parsing the AFM file, the file content is read line by line. Usually, the AFM file is in text format, and techniques such as regular expressions can be used to scan the file to extract the required font metrics information; regular expressions are a pattern matching tool used to search for and extract specific patterns in strings; in font typesetting, the baseline is an imaginary line at the bottom of characters, and the bases of all characters are usually aligned on this line. The process of baseline mapping is to map the character sequence to be rendered to the appropriate baseline position to ensure that all characters are vertically aligned when displayed; the baseline grid matrix is a two-dimensional matrix used to represent the position and arrangement of characters on the baseline. It sets the specific positions of characters according to metrics information such as the width and baseline position of each character, and the baseline grid matrix precisely represents the position information of each character; the adaptive control points are key points in the rendering process. They optimize the display effect of the font by adjusting the shape, size, or angle of the font. In adaptive rendering, the control points can be dynamically adjusted according to font metrics information and other factors (such as the complexity of the characters, the display environment, etc.) to ensure the optimal rendering effect; the color intensity field is a color intensity distribution based on the rendering area, which assigns a color intensity value to each pixel or area. Through the color intensity field, complex font rendering effects such as shadows and gradients can be achieved; the adaptive triangulation grid is a geometric structure used to characterize the shape of the font. By dividing the outline of the font into a triangular grid, the shape and size of each small area can be precisely controlled. The term "adaptive" means that these triangular grids can be dynamically adjusted according to the complexity of the characters or the required resolution, thereby improving the rendering efficiency; the rendering pixel matrix is the pixel-level representation of the finally displayed characters. It renders the characters onto the screen or output device according to the triangulation grid and color intensity field generated in the previous steps. Each pixel is colored according to the triangular grid it belongs to and the color intensity value; adaptive font rendering refers to dynamically adjusting the font rendering method according to the needs of different characters, display environments, or devices, including the shape, size, spacing, color, etc. of the font. By this method, the rendering process can be more flexible and efficient, ensuring that no matter the size or complexity of the characters, they will be presented in the optimal way.
[0092] Embodiment 2. The adaptive font rendering method based on AFM files proposed by the present invention, compared with Embodiment 1, this embodiment further includes: The font metric parameters include character width, inter-character spacing, vertical displacement parameters, font bounding box, character path, standard horizontal stroke width, and standard vertical stroke width. Among them, the vertical displacement parameters include ascender and descender. The font bounding box represents the minimum and maximum boundaries of the font. The character path is used to describe the contour path of the character. Among them, the contour path is composed of Bezier control points.
[0093] In this embodiment, the character path is a path used to describe the contour shape of each character. The path of the character is defined by some mathematical points (usually Bezier curve control points), which are connected by mathematical formulas to form the outer contour of the character. The character path ensures that the shape of the character can be accurately drawn; the Bezier control points are the key points that describe the Bezier curve. The Bezier curve is a mathematical curve commonly used in graphic design, especially widely used in character paths and vector graphics. The Bezier curve is defined by four control points to form its shape. The curve will form a smooth path under the guidance of these points. The character path is usually composed of multiple connected Bezier curves to accurately describe the font contour; the standard horizontal stroke width refers to the standard width of all horizontal strokes in the font. In font design, strokes are the lines that make up the glyphs, and the horizontal stroke width refers to the width of those strokes parallel to the baseline. The standard horizontal stroke width is usually used to ensure the visual consistency and symmetry of the font; the standard vertical stroke width refers to the standard width of all vertical strokes in the font. The vertical strokes are the lines perpendicular to the baseline, usually used for the vertical parts of the characters (such as the vertical lines in the letters H and I). The standard vertical stroke width is also used to ensure the symmetry and consistency of the font shape.
[0094] In an optional embodiment, baseline mapping is performed on the character sequence to be rendered according to the font metric parameters to obtain the baseline grid matrix corresponding to the character sequence to be rendered, including:
[0095] A1. Convert the Unicode encoding of each character to be rendered in the character sequence to be rendered into an AFM internal index to obtain the character index vector corresponding to the character to be rendered;
[0096] A2. Determine the horizontal offset vector corresponding to the character sequence to be rendered according to the character index vector and the font metric parameters;
[0097] A3. Determine the baseline vertical coordinate of the character according to the font metric parameters and the vertical resolution of the target device;
[0098] A4. Determine the vertical offset correction value corresponding to the character sequence to be rendered according to the font metric parameters and the vertical resolution of the target device;
[0099] A5. Determine the baseline grid matrix corresponding to the character sequence to be rendered based on the horizontal offset vector, vertical offset correction value, and baseline vertical coordinate of the character corresponding to the character sequence to be rendered.
[0100] It should be noted that the Unicode encoding is a character encoding standard designed to provide a unique encoding for all characters and symbols worldwide, and each character is assigned a unique number (code point); the character index vector is a vector containing the index values corresponding to the characters to be rendered, and these index values are used in the AFM file to identify specific characters; the vertical resolution of the target device refers to the number of vertical pixels displayed per inch of the device.
[0101] In an optional embodiment, the calculation formula for the horizontal offset vector is as follows:
[0102]
[0103] where x k represents the starting horizontal position of the k-th character to be rendered in the character sequence to be rendered, represents the character width corresponding to the (k - 1)-th character index vector i k-1 corresponding to the (k - 1)-th character to be rendered in the character sequence to be rendered, represents the character spacing corresponding to the (k - 1)-th character index vector i k-1 corresponding to the (k - 1)-th character to be rendered in the character sequence to be rendered;
[0104] The calculation formula for the baseline vertical coordinate of the character is as follows:
[0105] y base = round(A + |D|·Y res );
[0106] where y base represents the baseline vertical coordinate of the character, A represents the ascender in the vertical displacement parameter, D represents the descender in the vertical displacement parameter, Y res represents the vertical resolution of the target device, and round represents the rounding function;
[0107] The calculation formula for the vertical offset correction value of the character is as follows:
[0108]
[0109] where Δy k represents the vertical offset correction value of the k-th character in the character sequence to be rendered;
[0110] The calculation formula for the baseline grid matrix is as follows:
[0111] G base[k, :] = (x k , y base + Δy k );
[0112] Among them, G base [k, :] represents the baseline grid of the k-th character in the character sequence to be rendered.
[0113] In an alternative embodiment, determining the adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix includes:
[0114] B1. Convert multiple Bezier control points to normalized coordinates relative to the baseline grid;
[0115] B2. Determine the discrete curvature field of the contour path according to the normalized coordinates of the multiple Bezier control points, and determine the curvature change rate gradient according to the discrete curvature field;
[0116] B3. Determine the modulation displacement amount of the contour path according to the curvature change rate gradient;
[0117] B4. Determine the adaptive control points according to the modulation displacement amount of the contour path and the maximum curvature points of the contour path.
[0118] It should be noted that the normalized coordinates refer to mapping the coordinates to a standard range (usually between 0 and 1). In computer graphics, the normalized coordinates are used to eliminate the influence of different coordinate systems and resolutions, ensuring consistent display effects on different devices. Through normalization, coordinates of different sizes and resolutions can be unified into a standard space, facilitating calculation and comparison; curvature is an important geometric quantity describing the degree of curve bending. In the case of discretization, the curvature field refers to the curvature values calculated at a series of discrete points. On a curve or path, the discrete curvature field can help understand the bending behavior of the curve, usually estimating the change in curvature at a series of finite discrete points; the curvature change rate gradient is a mathematical quantity describing the speed and direction of curvature change; the modulation displacement amount refers to adjusting the point positions of a curve or path according to a specific mathematical model or rule; the adaptive control points are control points dynamically adjusted according to the geometric characteristics of the curve (such as curvature, curvature change rate, etc.).
[0119] In an alternative embodiment, the calculation formula of the normalized coordinates is as follows:
[0120]
[0121] Among them, P norm represents the normalized coordinates of the Bezier control point, P x and P y represent the coordinates of the Bezier control point, G base[k, x] represents the x - coordinate in the baseline grid;
[0122] The calculation formula for the modulation displacement is as follows:
[0123]
[0124] Where, ΔP i represents the modulation displacement of the i - th Bezier control point in the contour path, α represents the Hinting coefficient, κ i represents the curvature change rate of the i - th Bezier control point in the contour path, represents the gradient of the curvature change rate of the i - th Bezier control point in the contour path, and DPI represents the DPI value of the target device;
[0125] The calculation formula for the adaptive control point is as follows:
[0126] P adj = P + ΔP i ·exp(-λ·d(P, P peak ));
[0127] Where, P adj represents the adaptive control point, λ represents the attenuation coefficient, and d(P, P peak ) represents the Euclidean distance between the Bezier control point P and the maximum curvature point P peak of the contour path.
[0128] In an alternative embodiment, determining the color intensity field corresponding to the character sequence to be rendered according to the set of adaptive control points corresponding to the character sequence to be rendered includes:
[0129] Discretizing the set of adaptive control points to the pixel grid of the target device according to the physical pixel size of the target device, where the calculation formula for the pixel grid is as follows:
[0130]
[0131] Where, (u i , v i ) represents the control point coordinates in the pixel coordinate system, and represent the x - coordinate and y - coordinate of the adaptive control point respectively, and W and H represent the physical pixel size of the target device;
[0132] Determining the anisotropic stroke width according to the physical pixel size of the target device and the font metric parameters, where the calculation formula for the anisotropic stroke width is as follows:
[0133]
[0134] Where, σ xand σ y denotes the anisotropic stroke width, StdHW and StdVW denote the standard horizontal stroke width and the standard vertical stroke width;
[0135] Determine a Gaussian kernel set according to the anisotropic stroke width, wherein the calculation formula of the Gaussian kernel is as follows:
[0136]
[0137] wherein, G k (u, v) represents the Gaussian kernel, σ k represents the kernel function, and
[0138] Generate a color intensity field according to the Gaussian kernel set, wherein the calculation formula of the color intensity field is as follows:
[0139]
[0140] wherein, I k (u, v) represents the color intensity field, C pixel represents the set of control point coordinates in the pixel coordinate system, κ c represents the curvature value of the c-th control point coordinate in the set of control point coordinates in the pixel coordinate system, tahn represents the hyperbolic tangent function, and κ0 represents the curvature normalization threshold.
[0141] It should be noted that the pixel grid is a coordinate system composed of the pixels of the target device. The physical pixel size of the target device (for example, the number of pixels per inch of the screen or printing device) is related to the resolution and size of the device. When the physical pixel size of the target device is given, the pixel grid will map the coordinates to the pixel level of the device according to this size to ensure that the display of graphics and text on the device is accurate.
[0142] In an alternative embodiment, determining an adaptive triangulation mesh corresponding to the character sequence to be rendered according to the color intensity field includes:
[0143] C1. Determine the gradient tensor field according to the color intensity field by the central difference method, and determine the energy density field according to the gradient tensor field and the font metric parameters, wherein the calculation formula of the energy density field is as follows:
[0144]
[0145] wherein, E density (u, v) represents the energy density field, represents the gradient tensor field, and ε represents a parameter to prevent division by zero;
[0146] C2. Determine the adaptive triangulation mesh according to the energy density field, wherein the calculation formula of the adaptive triangulation mesh is as follows:
[0147] V new = argmax(E density (u, v));
[0148] Wherein, V new represents an adaptive triangulation mesh.
[0149] It should be noted that the central difference method is a numerical differentiation method used to calculate the derivative of a function. Different from the forward difference method and the backward difference method, the central difference method estimates the derivative through the difference between the function values on both sides of the current position; the gradient tensor field is a mathematical object used to describe the variation of a scalar field (such as a color intensity field) at each point in space; the energy density field is a scalar field representing the energy distribution per unit volume or unit area in a certain region.
[0150] In an optional embodiment, determining the rendering pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation mesh includes:
[0151] D1. Performing a Fourier transform on the adaptive triangulation mesh to obtain the frequency domain information corresponding to the adaptive triangulation mesh;
[0152] D2. Suppressing high-frequency aliasing of the frequency domain information according to the sinc function to obtain adjusted frequency domain information;
[0153] D3. Performing an inverse Fourier transform on the adjusted frequency domain information to obtain an adjusted adaptive triangulation mesh;
[0154] D4. Performing a Gaussian kernel convolution on the adjusted adaptive triangulation mesh to obtain a smoothed adaptive triangulation mesh;
[0155] D5. Determining the rendering pixel matrix of the smoothed adaptive triangulation mesh according to the convolution operation, where the calculation formula of the rendering pixel matrix is as follows:
[0156]
[0157] Wherein, P out (u, v) represents the rendering pixel matrix, F represents the Fourier transform, F -1 represents the inverse Fourier transform, f represents the frequency, f c represents the cut-off frequency, K blur represents the Gaussian kernel.
[0158] Embodiment 3, as Figure 2 shown, the adaptive font rendering system based on the AFM file proposed by the present invention is applicable to the adaptive font rendering method based on the AFM file, and includes:
[0159] Parameter extraction unit 1 is used to obtain the target AFM file, obtain the character sequence to be rendered, and extract font metric parameters by scanning the AFM file line by line and using regular expressions.
[0160] Baseline mapping unit 2 is used to perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain the baseline grid matrix corresponding to the character sequence to be rendered.
[0161] Control determination unit 3 is used to determine the adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix.
[0162] Color extraction unit 4 is used to determine the color intensity field corresponding to the character sequence to be rendered according to the adaptive control point set corresponding to the character sequence to be rendered.
[0163] Mesh extraction unit 5 is used to determine the adaptive triangulated mesh corresponding to the character sequence to be rendered according to the color intensity field.
[0164] Font rendering unit 6 is used to determine the rendered pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulated mesh, and perform adaptive font rendering on the character sequence to be rendered according to the rendered pixel matrix.
[0165] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited thereto. Various changes can be made without departing from the spirit of the present invention within the knowledge of those skilled in the art.
Claims
1. An adaptive font rendering method based on AFM files, characterized in that, Including: Obtain a target AFM file, obtain a character sequence to be rendered, scan the AFM file line by line, and extract font metric parameters using regular expressions; Perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain a baseline grid matrix corresponding to the character sequence to be rendered; Determine an adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix; Determine a color intensity field corresponding to the character sequence to be rendered according to the adaptive control point set corresponding to the character sequence to be rendered; Determine an adaptive triangulation grid corresponding to the character sequence to be rendered according to the color intensity field; Determine a rendering pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation grid, and perform adaptive font rendering on the character sequence to be rendered according to the rendering pixel matrix.
2. The adaptive font rendering method based on an AFM file according to claim 1, characterized in that The font metric parameters include character width, kerning, vertical displacement parameters, font bounding box, character path, standard horizontal stroke width, and standard vertical stroke width. Among them, the vertical displacement parameters include ascender and descender. The font bounding box represents the minimum and maximum boundaries of the font. The character path is used to describe the contour path of the character, and the contour path is composed of Bezier control points.
3. The adaptive font rendering method based on the AFM file according to claim 2, wherein Performing baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain a baseline grid matrix corresponding to the character sequence to be rendered includes: Convert the Unicode encoding of each character to be rendered in the character sequence to be rendered into an AFM internal index to obtain a character index vector corresponding to the character to be rendered; Determine a horizontal offset vector corresponding to the character sequence to be rendered according to the character index vector and the font metric parameters; Determine the baseline vertical coordinate of the character according to the font metric parameters and the vertical resolution of the target device; Determine a vertical offset correction value corresponding to the character sequence to be rendered according to the font metric parameters and the vertical resolution of the target device; Determine a baseline grid matrix corresponding to the character sequence to be rendered according to the horizontal offset vector, vertical offset correction value, and baseline vertical coordinate of the character corresponding to the character sequence to be rendered.
4. The adaptive font rendering method based on AFM files according to claim 3, wherein The calculation formula for the horizontal offset vector is as follows: Among them, x k represents the starting horizontal position of the k-th character to be rendered in the character sequence to be rendered, represents the k-1-th character index vector i corresponding to the (k-1)-th character to be rendered in the character sequence to be rendered k-1 corresponding to the character width, represents the k-1-th character index vector i corresponding to the (k-1)-th character to be rendered in the character sequence to be rendered k-1 corresponding to the kerning; The calculation formula for the baseline vertical coordinate of the character is as follows: y base = round(A + |D|·Y res ); where y base represents the vertical coordinate of the baseline of the character, A represents the ascender in the vertical displacement parameter, D represents the descender in the vertical displacement parameter, Y res represents the vertical resolution of the target device, and round represents the rounding function; The calculation formula for the vertical offset correction value of the character is as follows: where, Δy k represents the vertical offset correction value of the k-th character in the character sequence to be rendered; The calculation formula for the baseline grid matrix is as follows: G base [k, :] = (x k , y base + Δy k ); Among them, G base [k,:] represents the baseline grid of the k-th character in the character sequence to be rendered.
5. The adaptive font rendering method based on an AFM file according to claim 4, wherein Determining an adaptive control point set corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix includes: Convert multiple Bezier control points to normalized coordinates relative to the baseline grid; Determine a discrete curvature field of the contour path according to the normalized coordinates of multiple Bezier control points, and determine a curvature change rate gradient of the contour path according to the discrete curvature field; Determine a modulation displacement amount of the contour path according to the curvature change rate gradient; Determine the adaptive control points according to the modulation displacement amount of the contour path and the curvature maximum points of the contour path.
6. The adaptive font rendering method based on AFM files according to claim 5, wherein The calculation formula for the normalized coordinates is as follows: where P norm represents the normalized coordinates of the Bezier control points, P x and P y represent the coordinates of the Bezier control points, and G base [k, x] represents the x coordinate in the baseline grid; The calculation formula for the modulation displacement amount is as follows: where, ΔP i represents the modulation displacement of the i-th Bezier control point in the contour path, α represents the Hinting coefficient, κ i represents the curvature change rate of the i-th Bezier control point in the contour path, represents the curvature change rate gradient of the i-th Bezier control point in the contour path, and DPI represents the DPI value of the target device; The calculation formula for the adaptive control points is as follows: P adj = P + ΔP i · exp(-λ·d(P, P peak )); Among them, P adj represents an adaptive control point, λ represents an attenuation coefficient, and d(P, P peak ) represents the Euclidean distance between the Bézier control point P and the point P peak which is the point with the maximum curvature of the contour path.
7. The adaptive font rendering method based on the AFM file according to claim 6, wherein Determining the color intensity field corresponding to the character sequence to be rendered according to the set of adaptive control points corresponding to the character sequence to be rendered includes: Discretizing the set of adaptive control points to the pixel grid of the target device according to the physical pixel size of the target device, where the calculation formula for the pixel grid is as follows: Among them, (u i , v i ) represents the control point coordinates in the pixel coordinate system, and represent the x - coordinate and y - coordinate of the adaptive control point, and W and H represent the pixel physical size of the target device; Determining the anisotropic stroke width according to the physical pixel size of the target device and the font metric parameters, where the calculation formula for the anisotropic stroke width is as follows: where σ x and σ y represent the anisotropic stroke widths, and StdHW and StdVW represent the standard horizontal stroke width and the standard vertical stroke width; Determining the Gaussian kernel set according to the anisotropic stroke width, where the calculation formula for the Gaussian kernel is as follows: Among them, G k (u, v) represents the Gaussian kernel, and σ k represents the kernel function, and Generating a color intensity field according to the Gaussian kernel set, where the calculation formula for the color intensity field is as follows: Among them, I k (u, v) represents the color intensity field, C pixel represents the set of control point coordinates in the pixel coordinate system, κ c represents the curvature value of the c-th control point coordinate in the set of control point coordinates in the pixel coordinate system, tahn represents the hyperbolic tangent function, and κ0 represents the curvature normalization threshold.
8. The adaptive font rendering method based on an AFM file according to claim 7, characterized in that Determining the adaptive triangulation grid corresponding to the character sequence to be rendered according to the color intensity field includes: Determining the gradient tensor field through the color intensity field according to the central difference method, and determining the energy density field according to the gradient tensor field and the font metric parameters, where the calculation formula for the energy density field is as follows: Among them, E density (u, v) represents the energy density field, represents the gradient tensor field, and ε represents a parameter to prevent division by zero; Determining the adaptive triangulation grid according to the energy density field, where the calculation formula for the adaptive triangulation grid is as follows: V new = arg max(E density (u, v)); Among them, V new represents an adaptive triangular mesh.
9. The adaptive font rendering method based on AFM files according to claim 8, characterized in that Determining the rendered pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation grid includes: Performing a Fourier transform on the adaptive triangulation grid to obtain the frequency domain information corresponding to the adaptive triangulation grid; Performing high-frequency aliasing suppression on the frequency domain information according to the sinc function to obtain adjusted frequency domain information; Performing an inverse Fourier transform on the adjusted frequency domain information to obtain an adjusted adaptive triangulation grid; Performing Gaussian kernel convolution on the adjusted adaptive triangulation grid to obtain a smoothed adaptive triangulation grid; Determining the rendered pixel matrix of the smoothed adaptive triangulation grid according to the convolution operation, where the calculation formula for the rendered pixel matrix is as follows: Among them, P out (u, v) represents the rendered pixel matrix, F represents the Fourier transform, F -1 represents the inverse Fourier transform, f represents the frequency, f c represents the cut-off frequency, K blur represents the Gaussian kernel.
10. An adaptive font rendering system based on an AFM file, which is applicable to the adaptive font rendering method based on an AFM file according to any one of claims 9, characterized in that, Including: A parameter extraction unit (1), which is used to obtain a target AFM file, obtain a character sequence to be rendered, and extract font metric parameters by scanning the AFM file line by line and using regular expressions; A baseline mapping unit (2), which is used to perform baseline mapping on the character sequence to be rendered according to the font metric parameters to obtain a baseline grid matrix corresponding to the character sequence to be rendered; A control determination unit (3), which is used to determine a set of adaptive control points corresponding to the character sequence to be rendered according to the font metric parameters and the baseline grid matrix; A color extraction unit (4), which is used to determine a color intensity field corresponding to the character sequence to be rendered according to the set of adaptive control points corresponding to the character sequence to be rendered; A grid extraction unit (5), which is used to determine an adaptive triangulation grid corresponding to the character sequence to be rendered according to the color intensity field; A font rendering unit (6), which is configured to determine a rendered pixel matrix corresponding to the character sequence to be rendered according to the adaptive triangulation mesh, and perform adaptive font rendering on the character sequence to be rendered according to the rendered pixel matrix.