View Javadoc

1   // Copyright 2001-2006, FreeHEP.
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    * Class for embedding a Font in a SVG file.
10   * 
11   * @author Steffen Greiffenberg
12   * @version $Id: SVGGlyph.java 8584 2006-08-10 23:06:37Z duns $
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       * glyp
22       */
23      private Shape glyph;
24  
25      /**
26       * Character index
27       */
28      private int unicode;
29  
30      /**
31       * Metrics of that character
32       */
33      private GlyphMetrics glyphMetrics;
34  
35      /**
36       * Flip the drawing upside down
37       */
38      private static AffineTransform defaultTransform = new AffineTransform(1, 0,
39              0, -1, 0, 0);
40  
41      /**
42       * stores the glyph data
43       * 
44       * @param unicode
45       * @param glyph
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          // unicode
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              // advance X
68              result.append(getHorizontalAdvanceXString());
69  
70              // advance Y
71              result.append(getHorizontalAdvanceYString());
72          }
73  
74          // path
75          result.append(getPathString());
76          result.append("/>");
77  
78          return result.toString();
79      }
80  
81      /**
82       * @return SVG path tag using SVGGraphics2D and defaultTransform
83       */
84      protected String getPathString() {
85          return SVGGraphics2D.getPathContent(glyph
86                  .getPathIterator(defaultTransform));
87      }
88  
89      /**
90       * @return SVG tag horiz-adv-x
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      * @return SVG tag horiz-adv-y
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 }