| 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.xml.StyleAttribute;
|
|---|
| 39 | import java.util.HashMap;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Implements an embedded font.
|
|---|
| 43 | *
|
|---|
| 44 | * SVG specification: http://www.w3.org/TR/SVG/fonts.html
|
|---|
| 45 | *
|
|---|
| 46 | * @author Mark McKay
|
|---|
| 47 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 48 | */
|
|---|
| 49 | public class Font extends SVGElement
|
|---|
| 50 | {
|
|---|
| 51 |
|
|---|
| 52 | public static final String TAG_NAME = "font";
|
|---|
| 53 | int horizOriginX = 0;
|
|---|
| 54 | int horizOriginY = 0;
|
|---|
| 55 | int horizAdvX = -1; //Must be specified
|
|---|
| 56 | int vertOriginX = -1; //Defaults to horizAdvX / 2
|
|---|
| 57 | int vertOriginY = -1; //Defaults to font's ascent
|
|---|
| 58 | int vertAdvY = -1; //Defaults to one 'em'. See font-face
|
|---|
| 59 | FontFace fontFace = null;
|
|---|
| 60 | MissingGlyph missingGlyph = null;
|
|---|
| 61 | final HashMap glyphs = new HashMap();
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Creates a new instance of Font
|
|---|
| 65 | */
|
|---|
| 66 | public Font()
|
|---|
| 67 | {
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public String getTagName()
|
|---|
| 71 | {
|
|---|
| 72 | return TAG_NAME;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Called after the start element but before the end element to indicate
|
|---|
| 77 | * each child tag that has been processed
|
|---|
| 78 | */
|
|---|
| 79 | public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
|
|---|
| 80 | {
|
|---|
| 81 | super.loaderAddChild(helper, child);
|
|---|
| 82 |
|
|---|
| 83 | if (child instanceof Glyph)
|
|---|
| 84 | {
|
|---|
| 85 | glyphs.put(((Glyph) child).getUnicode(), child);
|
|---|
| 86 | } else if (child instanceof MissingGlyph)
|
|---|
| 87 | {
|
|---|
| 88 | missingGlyph = (MissingGlyph) child;
|
|---|
| 89 | } else if (child instanceof FontFace)
|
|---|
| 90 | {
|
|---|
| 91 | fontFace = (FontFace) child;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | public void loaderEndElement(SVGLoaderHelper helper) throws SVGParseException
|
|---|
| 96 | {
|
|---|
| 97 | super.loaderEndElement(helper);
|
|---|
| 98 |
|
|---|
| 99 | //build();
|
|---|
| 100 |
|
|---|
| 101 | helper.universe.registerFont(this);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | protected void build() throws SVGException
|
|---|
| 105 | {
|
|---|
| 106 | super.build();
|
|---|
| 107 |
|
|---|
| 108 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 109 |
|
|---|
| 110 | if (getPres(sty.setName("horiz-origin-x")))
|
|---|
| 111 | {
|
|---|
| 112 | horizOriginX = sty.getIntValue();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (getPres(sty.setName("horiz-origin-y")))
|
|---|
| 116 | {
|
|---|
| 117 | horizOriginY = sty.getIntValue();
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | if (getPres(sty.setName("horiz-adv-x")))
|
|---|
| 121 | {
|
|---|
| 122 | horizAdvX = sty.getIntValue();
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (getPres(sty.setName("vert-origin-x")))
|
|---|
| 126 | {
|
|---|
| 127 | vertOriginX = sty.getIntValue();
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | if (getPres(sty.setName("vert-origin-y")))
|
|---|
| 131 | {
|
|---|
| 132 | vertOriginY = sty.getIntValue();
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | if (getPres(sty.setName("vert-adv-y")))
|
|---|
| 136 | {
|
|---|
| 137 | vertAdvY = sty.getIntValue();
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public FontFace getFontFace()
|
|---|
| 142 | {
|
|---|
| 143 | return fontFace;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | public void setFontFace(FontFace face)
|
|---|
| 147 | {
|
|---|
| 148 | fontFace = face;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | public MissingGlyph getGlyph(String unicode)
|
|---|
| 152 | {
|
|---|
| 153 | Glyph retVal = (Glyph) glyphs.get(unicode);
|
|---|
| 154 | if (retVal == null)
|
|---|
| 155 | {
|
|---|
| 156 | return missingGlyph;
|
|---|
| 157 | }
|
|---|
| 158 | return retVal;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | public int getHorizOriginX()
|
|---|
| 162 | {
|
|---|
| 163 | return horizOriginX;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | public int getHorizOriginY()
|
|---|
| 167 | {
|
|---|
| 168 | return horizOriginY;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | public int getHorizAdvX()
|
|---|
| 172 | {
|
|---|
| 173 | return horizAdvX;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | public int getVertOriginX()
|
|---|
| 177 | {
|
|---|
| 178 | if (vertOriginX != -1)
|
|---|
| 179 | {
|
|---|
| 180 | return vertOriginX;
|
|---|
| 181 | }
|
|---|
| 182 | vertOriginX = getHorizAdvX() / 2;
|
|---|
| 183 | return vertOriginX;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | public int getVertOriginY()
|
|---|
| 187 | {
|
|---|
| 188 | if (vertOriginY != -1)
|
|---|
| 189 | {
|
|---|
| 190 | return vertOriginY;
|
|---|
| 191 | }
|
|---|
| 192 | vertOriginY = fontFace.getAscent();
|
|---|
| 193 | return vertOriginY;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | public int getVertAdvY()
|
|---|
| 197 | {
|
|---|
| 198 | if (vertAdvY != -1)
|
|---|
| 199 | {
|
|---|
| 200 | return vertAdvY;
|
|---|
| 201 | }
|
|---|
| 202 | vertAdvY = fontFace.getUnitsPerEm();
|
|---|
| 203 | return vertAdvY;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * Updates all attributes in this diagram associated with a time event. Ie,
|
|---|
| 208 | * all attributes with track information.
|
|---|
| 209 | *
|
|---|
| 210 | * @return - true if this node has changed state as a result of the time
|
|---|
| 211 | * update
|
|---|
| 212 | */
|
|---|
| 213 | public boolean updateTime(double curTime) throws SVGException
|
|---|
| 214 | {
|
|---|
| 215 | //Fonts can't change
|
|---|
| 216 | return false;
|
|---|
| 217 | /*
|
|---|
| 218 | if (trackManager.getNumTracks() == 0) return false;
|
|---|
| 219 |
|
|---|
| 220 | //Get current values for parameters
|
|---|
| 221 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 222 | boolean stateChange = false;
|
|---|
| 223 |
|
|---|
| 224 | if (getPres(sty.setName("horiz-origin-x")))
|
|---|
| 225 | {
|
|---|
| 226 | int newVal = sty.getIntValue();
|
|---|
| 227 | if (newVal != horizOriginX)
|
|---|
| 228 | {
|
|---|
| 229 | horizOriginX = newVal;
|
|---|
| 230 | stateChange = true;
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | if (getPres(sty.setName("horiz-origin-y")))
|
|---|
| 235 | {
|
|---|
| 236 | int newVal = sty.getIntValue();
|
|---|
| 237 | if (newVal != horizOriginY)
|
|---|
| 238 | {
|
|---|
| 239 | horizOriginY = newVal;
|
|---|
| 240 | stateChange = true;
|
|---|
| 241 | }
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if (getPres(sty.setName("horiz-adv-x")))
|
|---|
| 245 | {
|
|---|
| 246 | int newVal = sty.getIntValue();
|
|---|
| 247 | if (newVal != horizAdvX)
|
|---|
| 248 | {
|
|---|
| 249 | horizAdvX = newVal;
|
|---|
| 250 | stateChange = true;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | if (getPres(sty.setName("vert-origin-x")))
|
|---|
| 255 | {
|
|---|
| 256 | int newVal = sty.getIntValue();
|
|---|
| 257 | if (newVal != vertOriginX)
|
|---|
| 258 | {
|
|---|
| 259 | vertOriginX = newVal;
|
|---|
| 260 | stateChange = true;
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | if (getPres(sty.setName("vert-origin-y")))
|
|---|
| 265 | {
|
|---|
| 266 | int newVal = sty.getIntValue();
|
|---|
| 267 | if (newVal != vertOriginY)
|
|---|
| 268 | {
|
|---|
| 269 | vertOriginY = newVal;
|
|---|
| 270 | stateChange = true;
|
|---|
| 271 | }
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | if (getPres(sty.setName("vert-adv-y")))
|
|---|
| 275 | {
|
|---|
| 276 | int newVal = sty.getIntValue();
|
|---|
| 277 | if (newVal != vertAdvY)
|
|---|
| 278 | {
|
|---|
| 279 | vertAdvY = newVal;
|
|---|
| 280 | stateChange = true;
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | return shapeChange;
|
|---|
| 285 | */
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|