| 1 | /*
|
|---|
| 2 | * SVG Salamander
|
|---|
| 3 | * Copyright (c) 2004, Mark McKay
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or
|
|---|
| 7 | * without modification, are permitted provided that the following
|
|---|
| 8 | * conditions are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above
|
|---|
| 11 | * copyright notice, this list of conditions and the following
|
|---|
| 12 | * disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above
|
|---|
| 14 | * copyright notice, this list of conditions and the following
|
|---|
| 15 | * disclaimer in the documentation and/or other materials
|
|---|
| 16 | * provided with the distribution.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|---|
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|---|
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|---|
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|---|
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 30 | *
|
|---|
| 31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 32 | * projects can be found at http://www.kitfox.com
|
|---|
| 33 | *
|
|---|
| 34 | * Created on February 20, 2004, 10:00 PM
|
|---|
| 35 | */
|
|---|
| 36 | package com.kitfox.svg;
|
|---|
| 37 |
|
|---|
| 38 | import com.kitfox.svg.pathcmd.BuildHistory;
|
|---|
| 39 | import com.kitfox.svg.pathcmd.PathCommand;
|
|---|
| 40 | import com.kitfox.svg.xml.StyleAttribute;
|
|---|
| 41 | import java.awt.Graphics2D;
|
|---|
| 42 | import java.awt.Shape;
|
|---|
| 43 | import java.awt.geom.AffineTransform;
|
|---|
| 44 | import java.awt.geom.GeneralPath;
|
|---|
| 45 | import java.awt.geom.Rectangle2D;
|
|---|
| 46 | import java.util.Iterator;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Implements an embedded font.
|
|---|
| 50 | *
|
|---|
| 51 | * SVG specification: http://www.w3.org/TR/SVG/fonts.html
|
|---|
| 52 | *
|
|---|
| 53 | * @author Mark McKay
|
|---|
| 54 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 55 | */
|
|---|
| 56 | public class MissingGlyph extends ShapeElement
|
|---|
| 57 | {
|
|---|
| 58 | public static final String TAG_NAME = "missingglyph";
|
|---|
| 59 |
|
|---|
| 60 | //We may define a path
|
|---|
| 61 | private Shape path = null;
|
|---|
| 62 | //Alternately, we may have child graphical elements
|
|---|
| 63 | private int horizAdvX = -1; //Inherits font's value if not set
|
|---|
| 64 | private int vertOriginX = -1; //Inherits font's value if not set
|
|---|
| 65 | private int vertOriginY = -1; //Inherits font's value if not set
|
|---|
| 66 | private int vertAdvY = -1; //Inherits font's value if not set
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Creates a new instance of Font
|
|---|
| 70 | */
|
|---|
| 71 | public MissingGlyph()
|
|---|
| 72 | {
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | public String getTagName()
|
|---|
| 76 | {
|
|---|
| 77 | return TAG_NAME;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Called after the start element but before the end element to indicate
|
|---|
| 82 | * each child tag that has been processed
|
|---|
| 83 | */
|
|---|
| 84 | public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
|
|---|
| 85 | {
|
|---|
| 86 | super.loaderAddChild(helper, child);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | protected void build() throws SVGException
|
|---|
| 90 | {
|
|---|
| 91 | super.build();
|
|---|
| 92 |
|
|---|
| 93 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 94 |
|
|---|
| 95 | String commandList = "";
|
|---|
| 96 | if (getPres(sty.setName("d")))
|
|---|
| 97 | {
|
|---|
| 98 | commandList = sty.getStringValue();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | //If glyph path was specified, calculate it
|
|---|
| 103 | if (commandList != null)
|
|---|
| 104 | {
|
|---|
| 105 | String fillRule = getStyle(sty.setName("fill-rule")) ? sty.getStringValue() : "nonzero";
|
|---|
| 106 |
|
|---|
| 107 | PathCommand[] commands = parsePathList(commandList);
|
|---|
| 108 |
|
|---|
| 109 | GeneralPath buildPath = new GeneralPath(
|
|---|
| 110 | fillRule.equals("evenodd") ? GeneralPath.WIND_EVEN_ODD : GeneralPath.WIND_NON_ZERO,
|
|---|
| 111 | commands.length);
|
|---|
| 112 |
|
|---|
| 113 | BuildHistory hist = new BuildHistory();
|
|---|
| 114 |
|
|---|
| 115 | for (int i = 0; i < commands.length; i++)
|
|---|
| 116 | {
|
|---|
| 117 | PathCommand cmd = commands[i];
|
|---|
| 118 | cmd.appendPath(buildPath, hist);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | //Reflect glyph path to put it in user coordinate system
|
|---|
| 122 | AffineTransform at = new AffineTransform();
|
|---|
| 123 | at.scale(1, -1);
|
|---|
| 124 | path = at.createTransformedShape(buildPath);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | //Read glyph spacing info
|
|---|
| 129 | if (getPres(sty.setName("horiz-adv-x")))
|
|---|
| 130 | {
|
|---|
| 131 | horizAdvX = sty.getIntValue();
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (getPres(sty.setName("vert-origin-x")))
|
|---|
| 135 | {
|
|---|
| 136 | vertOriginX = sty.getIntValue();
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | if (getPres(sty.setName("vert-origin-y")))
|
|---|
| 140 | {
|
|---|
| 141 | vertOriginY = sty.getIntValue();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | if (getPres(sty.setName("vert-adv-y")))
|
|---|
| 145 | {
|
|---|
| 146 | vertAdvY = sty.getIntValue();
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | public Shape getPath()
|
|---|
| 151 | {
|
|---|
| 152 | return path;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | public void render(Graphics2D g) throws SVGException
|
|---|
| 156 | {
|
|---|
| 157 | //Do not push or pop stack
|
|---|
| 158 |
|
|---|
| 159 | if (path != null)
|
|---|
| 160 | {
|
|---|
| 161 | renderShape(g, path);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | Iterator it = children.iterator();
|
|---|
| 165 | while (it.hasNext())
|
|---|
| 166 | {
|
|---|
| 167 | SVGElement ele = (SVGElement) it.next();
|
|---|
| 168 | if (ele instanceof RenderableElement)
|
|---|
| 169 | {
|
|---|
| 170 | ((RenderableElement) ele).render(g);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | //Do not push or pop stack
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | public int getHorizAdvX()
|
|---|
| 178 | {
|
|---|
| 179 | if (horizAdvX == -1)
|
|---|
| 180 | {
|
|---|
| 181 | horizAdvX = ((Font) parent).getHorizAdvX();
|
|---|
| 182 | }
|
|---|
| 183 | return horizAdvX;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | public int getVertOriginX()
|
|---|
| 187 | {
|
|---|
| 188 | if (vertOriginX == -1)
|
|---|
| 189 | {
|
|---|
| 190 | vertOriginX = getHorizAdvX() / 2;
|
|---|
| 191 | }
|
|---|
| 192 | return vertOriginX;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | public int getVertOriginY()
|
|---|
| 196 | {
|
|---|
| 197 | if (vertOriginY == -1)
|
|---|
| 198 | {
|
|---|
| 199 | vertOriginY = ((Font) parent).getFontFace().getAscent();
|
|---|
| 200 | }
|
|---|
| 201 | return vertOriginY;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | public int getVertAdvY()
|
|---|
| 205 | {
|
|---|
| 206 | if (vertAdvY == -1)
|
|---|
| 207 | {
|
|---|
| 208 | vertAdvY = ((Font) parent).getFontFace().getUnitsPerEm();
|
|---|
| 209 | }
|
|---|
| 210 | return vertAdvY;
|
|---|
| 211 |
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | public Shape getShape()
|
|---|
| 215 | {
|
|---|
| 216 | if (path != null)
|
|---|
| 217 | {
|
|---|
| 218 | return shapeToParent(path);
|
|---|
| 219 | }
|
|---|
| 220 | return null;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | public Rectangle2D getBoundingBox() throws SVGException
|
|---|
| 224 | {
|
|---|
| 225 | if (path != null)
|
|---|
| 226 | {
|
|---|
| 227 | return boundsToParent(includeStrokeInBounds(path.getBounds2D()));
|
|---|
| 228 | }
|
|---|
| 229 | return null;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /**
|
|---|
| 233 | * Updates all attributes in this diagram associated with a time event. Ie,
|
|---|
| 234 | * all attributes with track information.
|
|---|
| 235 | *
|
|---|
| 236 | * @return - true if this node has changed state as a result of the time
|
|---|
| 237 | * update
|
|---|
| 238 | */
|
|---|
| 239 | public boolean updateTime(double curTime) throws SVGException
|
|---|
| 240 | {
|
|---|
| 241 | //Fonts can't change
|
|---|
| 242 | return false;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /**
|
|---|
| 246 | * @param path the path to set
|
|---|
| 247 | */
|
|---|
| 248 | public void setPath(Shape path)
|
|---|
| 249 | {
|
|---|
| 250 | this.path = path;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * @param horizAdvX the horizAdvX to set
|
|---|
| 255 | */
|
|---|
| 256 | public void setHorizAdvX(int horizAdvX)
|
|---|
| 257 | {
|
|---|
| 258 | this.horizAdvX = horizAdvX;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /**
|
|---|
| 262 | * @param vertOriginX the vertOriginX to set
|
|---|
| 263 | */
|
|---|
| 264 | public void setVertOriginX(int vertOriginX)
|
|---|
| 265 | {
|
|---|
| 266 | this.vertOriginX = vertOriginX;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /**
|
|---|
| 270 | * @param vertOriginY the vertOriginY to set
|
|---|
| 271 | */
|
|---|
| 272 | public void setVertOriginY(int vertOriginY)
|
|---|
| 273 | {
|
|---|
| 274 | this.vertOriginY = vertOriginY;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /**
|
|---|
| 278 | * @param vertAdvY the vertAdvY to set
|
|---|
| 279 | */
|
|---|
| 280 | public void setVertAdvY(int vertAdvY)
|
|---|
| 281 | {
|
|---|
| 282 | this.vertAdvY = vertAdvY;
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|