| 1 | /*
|
|---|
| 2 | * Font.java
|
|---|
| 3 | *
|
|---|
| 4 | *
|
|---|
| 5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
|---|
| 6 | * Copyright (C) 2004 Mark McKay
|
|---|
| 7 | *
|
|---|
| 8 | * This library is free software; you can redistribute it and/or
|
|---|
| 9 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 10 | * License as published by the Free Software Foundation; either
|
|---|
| 11 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This library is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * Lesser General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 19 | * License along with this library; if not, write to the Free Software
|
|---|
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 21 | *
|
|---|
| 22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 23 | * projects can be found at http://www.kitfox.com
|
|---|
| 24 | *
|
|---|
| 25 | * Created on February 20, 2004, 10:00 PM
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | package com.kitfox.svg;
|
|---|
| 29 |
|
|---|
| 30 | import com.kitfox.svg.xml.*;
|
|---|
| 31 |
|
|---|
| 32 | import java.awt.*;
|
|---|
| 33 | import java.awt.geom.*;
|
|---|
| 34 | import java.util.*;
|
|---|
| 35 |
|
|---|
| 36 | import com.kitfox.svg.pathcmd.*;
|
|---|
| 37 | //import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Implements an embedded font.
|
|---|
| 41 | *
|
|---|
| 42 | * SVG specification: http://www.w3.org/TR/SVG/fonts.html
|
|---|
| 43 | *
|
|---|
| 44 | * @author Mark McKay
|
|---|
| 45 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 46 | */
|
|---|
| 47 | public class MissingGlyph extends ShapeElement
|
|---|
| 48 | {
|
|---|
| 49 | //We may define a path
|
|---|
| 50 | // ExtendedGeneralPath path = null;
|
|---|
| 51 | Shape path = null;
|
|---|
| 52 |
|
|---|
| 53 | //Alternately, we may have child graphical elements
|
|---|
| 54 |
|
|---|
| 55 | int horizAdvX = -1; //Inherits font's value if not set
|
|---|
| 56 | int vertOriginX = -1; //Inherits font's value if not set
|
|---|
| 57 | int vertOriginY = -1; //Inherits font's value if not set
|
|---|
| 58 | int vertAdvY = -1; //Inherits font's value if not set
|
|---|
| 59 |
|
|---|
| 60 | /** Creates a new instance of Font */
|
|---|
| 61 | public MissingGlyph()
|
|---|
| 62 | {
|
|---|
| 63 | }
|
|---|
| 64 | /*
|
|---|
| 65 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
|---|
| 66 | {
|
|---|
| 67 | //Load style string
|
|---|
| 68 | super.loaderStartElement(helper, attrs, parent);
|
|---|
| 69 |
|
|---|
| 70 | //If glyph path was specified, calculate it
|
|---|
| 71 | String commandList = attrs.getValue("d");
|
|---|
| 72 | if (commandList != null)
|
|---|
| 73 | {
|
|---|
| 74 | StyleAttribute atyleAttrib = getStyle("fill-rule");
|
|---|
| 75 | String fillRule = (atyleAttrib == null) ? "nonzero" : atyleAttrib.getStringValue();
|
|---|
| 76 |
|
|---|
| 77 | PathCommand[] commands = parsePathList(commandList);
|
|---|
| 78 |
|
|---|
| 79 | // ExtendedGeneralPath buildPath = new ExtendedGeneralPath(
|
|---|
| 80 | GeneralPath buildPath = new GeneralPath(
|
|---|
| 81 | fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
|
|---|
| 82 | commands.length);
|
|---|
| 83 |
|
|---|
| 84 | BuildHistory hist = new BuildHistory();
|
|---|
| 85 |
|
|---|
| 86 | for (int i = 0; i < commands.length; i++)
|
|---|
| 87 | {
|
|---|
| 88 | PathCommand cmd = commands[i];
|
|---|
| 89 | cmd.appendPath(buildPath, hist);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | //Reflect glyph path to put it in user coordinate system
|
|---|
| 93 | AffineTransform at = new AffineTransform();
|
|---|
| 94 | at.scale(1, -1);
|
|---|
| 95 | path = at.createTransformedShape(buildPath);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | //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 | }
|
|---|