001    /*
002     * Font.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on February 20, 2004, 10:00 PM
026     */
027    
028    package com.kitfox.svg;
029    
030    import com.kitfox.svg.xml.*;
031    
032    import java.awt.*;
033    import java.awt.geom.*;
034    import java.util.*;
035    
036    import com.kitfox.svg.pathcmd.*;
037    //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
038    
039    /**
040     * Implements an embedded font.
041     *
042     * SVG specification: http://www.w3.org/TR/SVG/fonts.html
043     *
044     * @author Mark McKay
045     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
046     */
047    public class MissingGlyph extends ShapeElement
048    {
049        //We may define a path
050    //    ExtendedGeneralPath path = null;
051        Shape path = null;
052    
053        //Alternately, we may have child graphical elements
054    
055        int horizAdvX = -1;  //Inherits font's value if not set
056        int vertOriginX = -1;  //Inherits font's value if not set
057        int vertOriginY = -1;  //Inherits font's value if not set
058        int vertAdvY = -1;  //Inherits font's value if not set
059    
060        /** Creates a new instance of Font */
061        public MissingGlyph()
062        {
063        }
064    /*
065        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
066        {
067                    //Load style string
068            super.loaderStartElement(helper, attrs, parent);
069    
070            //If glyph path was specified, calculate it
071            String commandList = attrs.getValue("d");
072            if (commandList != null)
073            {
074                StyleAttribute atyleAttrib = getStyle("fill-rule");
075                String fillRule = (atyleAttrib == null) ? "nonzero" : atyleAttrib.getStringValue();
076    
077                PathCommand[] commands = parsePathList(commandList);
078    
079    //            ExtendedGeneralPath buildPath = new ExtendedGeneralPath(
080                GeneralPath buildPath = new GeneralPath(
081                    fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
082                    commands.length);
083    
084                BuildHistory hist = new BuildHistory();
085    
086                for (int i = 0; i < commands.length; i++)
087                {
088                    PathCommand cmd = commands[i];
089                    cmd.appendPath(buildPath, hist);
090                }
091    
092                //Reflect glyph path to put it in user coordinate system
093                AffineTransform at = new AffineTransform();
094                at.scale(1, -1);
095                path = at.createTransformedShape(buildPath);
096            }
097    
098    
099            //Read glyph spacing info
100            String horizAdvX = attrs.getValue("horiz-adv-x");
101            String vertOriginX = attrs.getValue("vert-origin-x");
102            String vertOriginY = attrs.getValue("vert-origin-y");
103            String vertAdvY = attrs.getValue("vert-adv-y");
104    
105            if (horizAdvX != null) this.horizAdvX = XMLParseUtil.parseInt(horizAdvX);
106            if (vertOriginX != null) this.vertOriginX = XMLParseUtil.parseInt(vertOriginX);
107            if (vertOriginY != null) this.vertOriginY = XMLParseUtil.parseInt(vertOriginY);
108            if (vertAdvY != null) this.vertAdvY = XMLParseUtil.parseInt(vertAdvY);
109    
110        }
111    */
112        /**
113         * Called after the start element but before the end element to indicate
114         * each child tag that has been processed
115         */
116        public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
117        {
118            super.loaderAddChild(helper, child);
119        }
120    
121        
122        protected void build() throws SVGException
123        {
124            super.build();
125            
126            StyleAttribute sty = new StyleAttribute();
127            
128            String commandList = "";
129            if (getPres(sty.setName("d"))) commandList = sty.getStringValue();
130    
131        
132            //If glyph path was specified, calculate it
133            if (commandList != null)
134            {
135    //            StyleAttribute atyleAttrib = getStyle("fill-rule");
136                String fillRule = getStyle(sty.setName("fill-rule")) ? sty.getStringValue() : "nonzero";
137    
138                PathCommand[] commands = parsePathList(commandList);
139    
140    //            ExtendedGeneralPath buildPath = new ExtendedGeneralPath(
141                GeneralPath buildPath = new GeneralPath(
142                    fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
143                    commands.length);
144    
145                BuildHistory hist = new BuildHistory();
146    
147                for (int i = 0; i < commands.length; i++)
148                {
149                    PathCommand cmd = commands[i];
150                    cmd.appendPath(buildPath, hist);
151                }
152    
153                //Reflect glyph path to put it in user coordinate system
154                AffineTransform at = new AffineTransform();
155                at.scale(1, -1);
156                path = at.createTransformedShape(buildPath);
157            }
158    
159    
160            //Read glyph spacing info
161            if (getPres(sty.setName("horiz-adv-x"))) horizAdvX = sty.getIntValue();
162    
163            if (getPres(sty.setName("vert-origin-x"))) vertOriginX = sty.getIntValue();
164    
165            if (getPres(sty.setName("vert-origin-y"))) vertOriginY = sty.getIntValue();
166    
167            if (getPres(sty.setName("vert-adv-y"))) vertAdvY = sty.getIntValue();
168        }
169    
170        public Shape getPath()
171        {
172            return path;
173        }
174    
175        public void render(Graphics2D g) throws SVGException
176        {
177            //Do not push or pop stack
178    
179            if (path != null) renderShape(g, path);
180            
181            Iterator it = children.iterator();
182            while (it.hasNext())
183            {
184                SVGElement ele = (SVGElement)it.next();
185                if (ele instanceof RenderableElement)
186                {
187                    ((RenderableElement)ele).render(g);
188                }
189            }
190    
191            //Do not push or pop stack
192        }
193    
194        public int getHorizAdvX()
195        {
196            if (horizAdvX == -1)
197                horizAdvX = ((Font)parent).getHorizAdvX();
198            return horizAdvX;
199        }
200    
201        public int getVertOriginX()
202        {
203            if (vertOriginX == -1)
204                vertOriginX = getHorizAdvX() / 2;
205            return vertOriginX;
206        }
207    
208        public int getVertOriginY()
209        {
210            if (vertOriginY == -1)
211                vertOriginY = ((Font)parent).getFontFace().getAscent();
212            return vertOriginY;
213        }
214    
215        public int getVertAdvY()
216        {
217            if (vertAdvY == -1)
218                vertAdvY = ((Font)parent).getFontFace().getUnitsPerEm();
219            return vertAdvY;
220    
221        }
222    
223        public Shape getShape()
224        {
225            if (path != null) return shapeToParent(path);
226            return null;
227        }
228    
229        public Rectangle2D getBoundingBox() throws SVGException
230        {
231            if (path != null) return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
232            return null;
233        }
234    
235        /**
236         * Updates all attributes in this diagram associated with a time event.
237         * Ie, all attributes with track information.
238         * @return - true if this node has changed state as a result of the time
239         * update
240         */
241        public boolean updateTime(double curTime) throws SVGException
242        {
243            //Fonts can't change
244            return false;
245        }
246    }