summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/svg/MissingGlyph.java
blob: e36be01b58e320fc0229b3981b58271c7b5c8de5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Font.java
 *
 *
 *  The Salamander Project - 2D and 3D graphics libraries in Java
 *  Copyright (C) 2004 Mark McKay
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
 *  projects can be found at http://www.kitfox.com
 *
 * Created on February 20, 2004, 10:00 PM
 */

package com.kitfox.svg;

import com.kitfox.svg.xml.*;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;

import com.kitfox.svg.pathcmd.*;
//import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;

/**
 * Implements an embedded font.
 *
 * SVG specification: http://www.w3.org/TR/SVG/fonts.html
 *
 * @author Mark McKay
 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
 */
public class MissingGlyph extends ShapeElement
{
    //We may define a path
//    ExtendedGeneralPath path = null;
    Shape path = null;

    //Alternately, we may have child graphical elements

    int horizAdvX = -1;  //Inherits font's value if not set
    int vertOriginX = -1;  //Inherits font's value if not set
    int vertOriginY = -1;  //Inherits font's value if not set
    int vertAdvY = -1;  //Inherits font's value if not set

    /** Creates a new instance of Font */
    public MissingGlyph()
    {
    }
/*
    public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
    {
		//Load style string
        super.loaderStartElement(helper, attrs, parent);

        //If glyph path was specified, calculate it
        String commandList = attrs.getValue("d");
        if (commandList != null)
        {
            StyleAttribute atyleAttrib = getStyle("fill-rule");
            String fillRule = (atyleAttrib == null) ? "nonzero" : atyleAttrib.getStringValue();

            PathCommand[] commands = parsePathList(commandList);

//            ExtendedGeneralPath buildPath = new ExtendedGeneralPath(
            GeneralPath buildPath = new GeneralPath(
                fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
                commands.length);

            BuildHistory hist = new BuildHistory();

            for (int i = 0; i < commands.length; i++)
            {
                PathCommand cmd = commands[i];
                cmd.appendPath(buildPath, hist);
            }

            //Reflect glyph path to put it in user coordinate system
            AffineTransform at = new AffineTransform();
            at.scale(1, -1);
            path = at.createTransformedShape(buildPath);
        }


        //Read glyph spacing info
        String horizAdvX = attrs.getValue("horiz-adv-x");
        String vertOriginX = attrs.getValue("vert-origin-x");
        String vertOriginY = attrs.getValue("vert-origin-y");
        String vertAdvY = attrs.getValue("vert-adv-y");

        if (horizAdvX != null) this.horizAdvX = XMLParseUtil.parseInt(horizAdvX);
        if (vertOriginX != null) this.vertOriginX = XMLParseUtil.parseInt(vertOriginX);
        if (vertOriginY != null) this.vertOriginY = XMLParseUtil.parseInt(vertOriginY);
        if (vertAdvY != null) this.vertAdvY = XMLParseUtil.parseInt(vertAdvY);

    }
*/
    /**
     * Called after the start element but before the end element to indicate
     * each child tag that has been processed
     */
    public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
    {
        super.loaderAddChild(helper, child);
    }

    
    protected void build() throws SVGException
    {
        super.build();
        
        StyleAttribute sty = new StyleAttribute();
        
        String commandList = "";
        if (getPres(sty.setName("d"))) commandList = sty.getStringValue();

    
        //If glyph path was specified, calculate it
        if (commandList != null)
        {
//            StyleAttribute atyleAttrib = getStyle("fill-rule");
            String fillRule = getStyle(sty.setName("fill-rule")) ? sty.getStringValue() : "nonzero";

            PathCommand[] commands = parsePathList(commandList);

//            ExtendedGeneralPath buildPath = new ExtendedGeneralPath(
            GeneralPath buildPath = new GeneralPath(
                fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
                commands.length);

            BuildHistory hist = new BuildHistory();

            for (int i = 0; i < commands.length; i++)
            {
                PathCommand cmd = commands[i];
                cmd.appendPath(buildPath, hist);
            }

            //Reflect glyph path to put it in user coordinate system
            AffineTransform at = new AffineTransform();
            at.scale(1, -1);
            path = at.createTransformedShape(buildPath);
        }


        //Read glyph spacing info
        if (getPres(sty.setName("horiz-adv-x"))) horizAdvX = sty.getIntValue();

        if (getPres(sty.setName("vert-origin-x"))) vertOriginX = sty.getIntValue();

        if (getPres(sty.setName("vert-origin-y"))) vertOriginY = sty.getIntValue();

        if (getPres(sty.setName("vert-adv-y"))) vertAdvY = sty.getIntValue();
    }

    public Shape getPath()
    {
        return path;
    }

    public void render(Graphics2D g) throws SVGException
    {
        //Do not push or pop stack

        if (path != null) renderShape(g, path);
        
        Iterator it = children.iterator();
        while (it.hasNext())
        {
            SVGElement ele = (SVGElement)it.next();
            if (ele instanceof RenderableElement)
            {
                ((RenderableElement)ele).render(g);
            }
        }

        //Do not push or pop stack
    }

    public int getHorizAdvX()
    {
        if (horizAdvX == -1)
            horizAdvX = ((Font)parent).getHorizAdvX();
        return horizAdvX;
    }

    public int getVertOriginX()
    {
        if (vertOriginX == -1)
            vertOriginX = getHorizAdvX() / 2;
        return vertOriginX;
    }

    public int getVertOriginY()
    {
        if (vertOriginY == -1)
            vertOriginY = ((Font)parent).getFontFace().getAscent();
        return vertOriginY;
    }

    public int getVertAdvY()
    {
        if (vertAdvY == -1)
            vertAdvY = ((Font)parent).getFontFace().getUnitsPerEm();
        return vertAdvY;

    }

    public Shape getShape()
    {
        if (path != null) return shapeToParent(path);
        return null;
    }

    public Rectangle2D getBoundingBox() throws SVGException
    {
        if (path != null) return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
        return null;
    }

    /**
     * Updates all attributes in this diagram associated with a time event.
     * Ie, all attributes with track information.
     * @return - true if this node has changed state as a result of the time
     * update
     */
    public boolean updateTime(double curTime) throws SVGException
    {
        //Fonts can't change
        return false;
    }
}