1
2 package org.freehep.graphicsio.svg;
3
4 import java.awt.Shape;
5 import java.awt.font.GlyphMetrics;
6 import java.awt.geom.AffineTransform;
7
8
9
10
11
12
13
14 public class SVGGlyph {
15
16 public static int FONT_SIZE = 100;
17
18 public static int UNITS_PER_EM = 2048;
19
20
21
22
23 private Shape glyph;
24
25
26
27
28 private int unicode;
29
30
31
32
33 private GlyphMetrics glyphMetrics;
34
35
36
37
38 private static AffineTransform defaultTransform = new AffineTransform(1, 0,
39 0, -1, 0, 0);
40
41
42
43
44
45
46
47 public SVGGlyph(Shape glyph, int unicode, GlyphMetrics glyphMetrics) {
48
49 this.unicode = unicode;
50 this.glyph = glyph;
51 this.glyphMetrics = glyphMetrics;
52 }
53
54 public String toString() {
55 StringBuffer result = new StringBuffer("<glyph ");
56
57
58 result.append("unicode=\"");
59
60 String hex = "0000" + Integer.toHexString(unicode);
61 result.append("&#x");
62 result.append(hex.substring(hex.length() - 4));
63 result.append(';');
64 result.append("\" ");
65
66 if (glyphMetrics != null) {
67
68 result.append(getHorizontalAdvanceXString());
69
70
71 result.append(getHorizontalAdvanceYString());
72 }
73
74
75 result.append(getPathString());
76 result.append("/>");
77
78 return result.toString();
79 }
80
81
82
83
84 protected String getPathString() {
85 return SVGGraphics2D.getPathContent(glyph
86 .getPathIterator(defaultTransform));
87 }
88
89
90
91
92 public String getHorizontalAdvanceXString() {
93 StringBuffer result = new StringBuffer();
94
95 if (glyphMetrics.getAdvanceX() != 0) {
96 result.append("horiz-adv-x=\"");
97 result.append(SVGGraphics2D.fixedPrecision(glyphMetrics
98 .getAdvanceX()));
99 result.append("\" ");
100 }
101
102 return result.toString();
103 }
104
105
106
107
108 public String getHorizontalAdvanceYString() {
109 StringBuffer result = new StringBuffer();
110
111 if (glyphMetrics.getAdvanceY() != 0) {
112 result.append("horiz-adv-y=\"");
113 result.append(SVGGraphics2D.fixedPrecision(glyphMetrics
114 .getAdvanceY()));
115 result.append("\" ");
116 }
117
118 return result.toString();
119 }
120 }