使用 Type1 字体进行文本渲染 | Java
Contents
[
Hide
Show
]渲染文字
为了渲染文本,渲染子系统需要实现 Aspose.Font.Rendering.IGlyphOutlinePainter 接口才能绘制字形。这可以通过以下步骤来实现。
- 创建
DrawText()方法,将指定的文本绘制到 System.Drawing.Bitmap 对象中并将生成的位图保存到磁盘上。这将包括以下步骤:
- 迭代文本字符串中的所有符号。
- 获取每个处理过的符号的字形标识符 - GID。
- 创建类型为 GlyphOutlinePainter 的对象,这是渲染子系统绘制当前字形所需的。
- 创建类型为 Aspose.Font.Renderers.GlyphOutlineRenderer 的对象,并将刚刚创建的类型为 GlyphOutlinePainter 的对象传递给 GlyphOutlineRenderer 的构造函数。此 GlyphOutlineRenderer 对象旨在渲染指定的字形。
- 使用 GlyphOutlineRenderer.RenderGlyph() 方法渲染当前处理的字形。 Aspose.Fonts.Matrix 对象用于指定字形坐标。要渲染的字形通过 GID 参数指定。
此策略的辅助步骤
- 此代码段的 “Y” 轴字形坐标是恒定的。
- 为每个处理过的字形计算 “X” 轴的字形坐标。
- “X” 和 “Y” 坐标都传递到 Aspose.Fonts.Matrix 对象中,GlyphOutlineRenderer 使用该对象来绘制字形。
- 在每个迭代步骤中计算刚处理的字形与前一个字形之间的距离。此距离会影响每个字形的 “X” 坐标。
- 类型为 GlyphOutlinePainter 的对象在 GlyphOutlinePainter 的帮助下不是直接绘制到位图中,而是绘制到传递给 GlyphOutlinePainter 构造函数的 GraphicsPath 对象中,因此我们使用类型为 System.Drawing.Graphics 的对象将 GraphicsPath 绘制到位图中。
- FontWidthToImageWith() 方法计算位图坐标系的字形宽度。
实现如下所示。
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-Java
2static void drawText(String text, IFont font, double fontSize,
3 Paint backgroundBrush, Paint textBrush, String outFile) throws Exception
4{
5 //Get glyph identifiers for every symbol in text line
6 GlyphId[] gids = new GlyphId[text.length()];
7 for (int i = 0; i < text.length(); i++)
8 gids[i] = font.getEncoding().decodeToGid(text.charAt(i));
9 // set common drawing settings
10 double dpi = 300;
11
12 double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
13 // prepare output bitmap
14 BufferedImage outBitmap = new BufferedImage(960, 720, BufferedImage.TYPE_INT_BGR);
15 //outBitmap.getRaster().SetResolution((float)dpi, (float)dpi);
16 java.awt.Graphics2D outGraphics = (java.awt.Graphics2D) outBitmap.getGraphics();
17 outGraphics.setPaint(backgroundBrush);
18 outGraphics.fillRect(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
19 outGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
20 //declare coordinate variables and previous gid
21 GlyphId previousGid = null;
22 double glyphXCoordinate = 0;
23 double glyphYCoordinate = fontSize * resolutionCorrection;
24 //loop which paints every glyph in gids
25 for (GlyphId gid : gids)
26 {
27 // if the font contains the gid
28 if (gid != null)
29 {
30 Glyph glyph = font.getGlyphAccessor().getGlyphById(gid);
31 if (glyph == null)
32 continue;
33
34 // path that accepts drawing instructions
35 GeneralPath path = new GeneralPath();
36
37 // Create IGlyphOutlinePainter implementation
38 GlyphOutlinePainter outlinePainter = new GlyphOutlinePainter(path);
39
40 // Create the renderer
41 IGlyphRenderer renderer = new GlyphOutlineRenderer(outlinePainter);
42
43 // get common glyph properties
44 double kerning = 0;
45
46 // get kerning value
47 if (previousGid != null)
48 {
49 kerning = (font.getMetrics().getKerningValue(previousGid, gid) /
50 glyph.getSourceResolution()) * fontSize * resolutionCorrection;
51 kerning += fontWidthToImageWith(font.getMetrics().getGlyphWidth(previousGid),
52 glyph.getSourceResolution(), fontSize, 300);
53 }
54
55 // glyph positioning - increase glyph X coordinate according to kerning distance
56 glyphXCoordinate += kerning;
57
58 // Glyph placement matrix
59 TransformationMatrix glyphMatrix =
60 new TransformationMatrix(
61 new double[]
62 {
63 fontSize*resolutionCorrection,
64 0,
65 0,
66 // negative because of bitmap coordinate system begins from the top
67 - fontSize*resolutionCorrection,
68 glyphXCoordinate,
69 glyphYCoordinate
70 });
71
72 // render current glyph
73 renderer.renderGlyph(font, gid, glyphMatrix);
74 // fill the path
75 path.setWindingRule(GeneralPath.WIND_NON_ZERO);
76 outGraphics.setPaint(textBrush);
77 outGraphics.fill(path);
78 }
79 //set current gid as previous to get correct kerning for next glyph
80 previousGid = gid;
81 }
82 //Save results
83 ImageIO.write(outBitmap, "jpg", new File(outFile));
84}- 创建实用程序方法以计算字体宽度至图像宽度,如下所示:
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-Java
2static double fontWidthToImageWidth(double width, int fontSourceResulution, double fontSize, double dpi)
3{
4 double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
5 return (width / fontSourceResulution) * fontSize * resolutionCorrection;
6}调用渲染文本功能
要使用上述实现,可以从基于控制台应用程序的 Main 方法执行以下示例代码。
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-Java
2String fileName = Utils.getDataDir() + "courier.pfb"; //Font file name with full path
3
4FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
5 Type1Font font = (Type1Font) Font.open(fd);
6
7 try {
8 drawText("Hello world", font, 14, java.awt.Color.WHITE, java.awt.Color.BLACK, Utils.getDataDir() + "hello1_type1_out.jpg");
9 drawText("Hello world", font, 14, java.awt.Color.YELLOW, java.awt.Color.RED, Utils.getDataDir() + "hello2_type1_out.jpg");
10 } catch (Exception ex) {
11 ex.printStackTrace();
12 }