source: josm/trunk/src/com/kitfox/svg/Font.java@ 6209

Last change on this file since 6209 was 6002, checked in by Don-vip, 11 years ago

fix #8742 - update svgsalamander to release 0.1.18+patch (fix bug SVGSALAMANDER-26) -> allow to open more SVG files

File size: 7.4 KB
Line 
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 */
36package com.kitfox.svg;
37
38import com.kitfox.svg.xml.StyleAttribute;
39import 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 */
49public 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 MissingGlyph getGlyph(String unicode)
147 {
148 Glyph retVal = (Glyph) glyphs.get(unicode);
149 if (retVal == null)
150 {
151 return missingGlyph;
152 }
153 return retVal;
154 }
155
156 public int getHorizOriginX()
157 {
158 return horizOriginX;
159 }
160
161 public int getHorizOriginY()
162 {
163 return horizOriginY;
164 }
165
166 public int getHorizAdvX()
167 {
168 return horizAdvX;
169 }
170
171 public int getVertOriginX()
172 {
173 if (vertOriginX != -1)
174 {
175 return vertOriginX;
176 }
177 vertOriginX = getHorizAdvX() / 2;
178 return vertOriginX;
179 }
180
181 public int getVertOriginY()
182 {
183 if (vertOriginY != -1)
184 {
185 return vertOriginY;
186 }
187 vertOriginY = fontFace.getAscent();
188 return vertOriginY;
189 }
190
191 public int getVertAdvY()
192 {
193 if (vertAdvY != -1)
194 {
195 return vertAdvY;
196 }
197 vertAdvY = fontFace.getUnitsPerEm();
198 return vertAdvY;
199 }
200
201 /**
202 * Updates all attributes in this diagram associated with a time event. Ie,
203 * all attributes with track information.
204 *
205 * @return - true if this node has changed state as a result of the time
206 * update
207 */
208 public boolean updateTime(double curTime) throws SVGException
209 {
210 //Fonts can't change
211 return false;
212 /*
213 if (trackManager.getNumTracks() == 0) return false;
214
215 //Get current values for parameters
216 StyleAttribute sty = new StyleAttribute();
217 boolean stateChange = false;
218
219 if (getPres(sty.setName("horiz-origin-x")))
220 {
221 int newVal = sty.getIntValue();
222 if (newVal != horizOriginX)
223 {
224 horizOriginX = newVal;
225 stateChange = true;
226 }
227 }
228
229 if (getPres(sty.setName("horiz-origin-y")))
230 {
231 int newVal = sty.getIntValue();
232 if (newVal != horizOriginY)
233 {
234 horizOriginY = newVal;
235 stateChange = true;
236 }
237 }
238
239 if (getPres(sty.setName("horiz-adv-x")))
240 {
241 int newVal = sty.getIntValue();
242 if (newVal != horizAdvX)
243 {
244 horizAdvX = newVal;
245 stateChange = true;
246 }
247 }
248
249 if (getPres(sty.setName("vert-origin-x")))
250 {
251 int newVal = sty.getIntValue();
252 if (newVal != vertOriginX)
253 {
254 vertOriginX = newVal;
255 stateChange = true;
256 }
257 }
258
259 if (getPres(sty.setName("vert-origin-y")))
260 {
261 int newVal = sty.getIntValue();
262 if (newVal != vertOriginY)
263 {
264 vertOriginY = newVal;
265 stateChange = true;
266 }
267 }
268
269 if (getPres(sty.setName("vert-adv-y")))
270 {
271 int newVal = sty.getIntValue();
272 if (newVal != vertAdvY)
273 {
274 vertAdvY = newVal;
275 stateChange = true;
276 }
277 }
278
279 return shapeChange;
280 */
281 }
282}
Note: See TracBrowser for help on using the repository browser.