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

Last change on this file since 12713 was 11525, checked in by Don-vip, 7 years ago

see #14319 - update to latest version of svgSalamander (2017-01-07, patched)

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