root / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / SimpleTextSymbol.java @ 23070
History | View | Annotate | Download (9.02 KB)
1 | 21200 | vcaballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package org.gvsig.fmap.mapcontext.rendering.symbols; |
||
42 | |||
43 | import java.awt.Color; |
||
44 | import java.awt.Font; |
||
45 | 22751 | vcaballero | import java.awt.FontMetrics; |
46 | 21200 | vcaballero | import java.awt.Graphics2D; |
47 | import java.awt.Rectangle; |
||
48 | import java.awt.RenderingHints; |
||
49 | import java.awt.Shape; |
||
50 | import java.awt.font.FontRenderContext; |
||
51 | import java.awt.font.GlyphVector; |
||
52 | import java.awt.geom.AffineTransform; |
||
53 | import java.awt.geom.Point2D; |
||
54 | import java.awt.geom.Rectangle2D; |
||
55 | |||
56 | import javax.print.attribute.PrintRequestAttributeSet; |
||
57 | 23070 | vcaballero | import javax.print.attribute.standard.PrintQuality; |
58 | 21200 | vcaballero | |
59 | 23070 | vcaballero | import org.gvsig.fmap.data.ReadException; |
60 | 21200 | vcaballero | import org.gvsig.fmap.geom.Geometry; |
61 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
62 | import org.gvsig.fmap.geom.primitive.Surface2D; |
||
63 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
64 | |||
65 | import com.iver.utiles.StringUtilities; |
||
66 | import com.iver.utiles.XMLEntity; |
||
67 | import com.iver.utiles.swing.threads.Cancellable; |
||
68 | |||
69 | /**
|
||
70 | * SimpleTextSymbol is a class used to create symbols composed using a text defined by
|
||
71 | * the user.This text can be edited (changing the color, the font of the characters, and
|
||
72 | * the rotation of the text).
|
||
73 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
74 | *
|
||
75 | */
|
||
76 | public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol { |
||
77 | private String text = ""; |
||
78 | private Font font = new Font("Arial", Font.PLAIN, 12); |
||
79 | private Color textColor = Color.BLACK; |
||
80 | private double rotation; |
||
81 | private FontRenderContext frc = new FontRenderContext( |
||
82 | new AffineTransform(), false, true); |
||
83 | 22751 | vcaballero | private boolean autoresize; |
84 | 21200 | vcaballero | public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Cancellable cancel) { |
85 | if (!isShapeVisible()) return; |
||
86 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
||
87 | g.setColor(textColor); |
||
88 | g.setFont(font); |
||
89 | g.translate(((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), ((org.gvsig.fmap.geom.primitive.Point2D) geom).getY()); |
||
90 | |||
91 | g.rotate(rotation); |
||
92 | Rectangle2D bounds = getBounds();
|
||
93 | 22751 | vcaballero | |
94 | g.drawString(getText(), 0, (int) bounds.getHeight()); |
||
95 | 21200 | vcaballero | g.rotate(-rotation); |
96 | g.translate(-((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), -((org.gvsig.fmap.geom.primitive.Point2D) geom).getY()); |
||
97 | } |
||
98 | |||
99 | public void drawInsideRectangle(Graphics2D g, |
||
100 | AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException { |
||
101 | int s = getFont().getSize();
|
||
102 | 22751 | vcaballero | if (autoresize) {
|
103 | if (s==0) { |
||
104 | s =1;
|
||
105 | setFontSize(s); |
||
106 | } |
||
107 | g.setFont(getFont()); |
||
108 | FontMetrics fm = g.getFontMetrics();
|
||
109 | Rectangle2D rect = fm.getStringBounds(text, g);
|
||
110 | double width = rect.getWidth();
|
||
111 | double height = rect.getHeight();
|
||
112 | double rWidth = r.getWidth();
|
||
113 | double rHeight = r.getHeight();
|
||
114 | double ratioText = width/height;
|
||
115 | double ratioRect = rWidth/rHeight;
|
||
116 | |||
117 | if (ratioText>ratioRect) {
|
||
118 | s = (int) (s*(rWidth/width));
|
||
119 | } else {
|
||
120 | s = (int) (s*(rHeight/height));
|
||
121 | } |
||
122 | // r.setLocation(r.x, (int) Math.round(r.getY()+r.getHeight()));
|
||
123 | } |
||
124 | setFontSize(s); |
||
125 | // g.setColor(Color.red);
|
||
126 | // g.draw(r);
|
||
127 | 21200 | vcaballero | draw(g, null, new org.gvsig.fmap.geom.primitive.Point2D(r.getX(), r.getY()), null); |
128 | 22751 | vcaballero | |
129 | |||
130 | 21200 | vcaballero | } |
131 | |||
132 | public int getOnePointRgb() { |
||
133 | return textColor.getRGB();
|
||
134 | } |
||
135 | |||
136 | public void getPixExtentPlus(org.gvsig.fmap.geom.Geometry geom, float[] distances, |
||
137 | ViewPort viewPort, int dpi) {
|
||
138 | throw new Error("Not yet implemented!"); |
||
139 | |||
140 | } |
||
141 | |||
142 | public ISymbol getSymbolForSelection() {
|
||
143 | return this; // a text is not selectable |
||
144 | } |
||
145 | |||
146 | public int getSymbolType() { |
||
147 | return Geometry.TYPES.TEXT;
|
||
148 | } |
||
149 | |||
150 | public XMLEntity getXMLEntity() {
|
||
151 | XMLEntity xml = new XMLEntity();
|
||
152 | xml.putProperty("className", getClassName());
|
||
153 | xml.putProperty("desc", getDescription());
|
||
154 | xml.putProperty("isShapeVisible", isShapeVisible());
|
||
155 | xml.putProperty("font", font.getName());
|
||
156 | xml.putProperty("fontStyle", font.getStyle());
|
||
157 | xml.putProperty("size", font.getSize());
|
||
158 | xml.putProperty("text", text);
|
||
159 | xml.putProperty("textColor", StringUtilities.color2String(textColor));
|
||
160 | xml.putProperty("unit", getUnit());
|
||
161 | xml.putProperty("referenceSystem", getReferenceSystem());
|
||
162 | 22751 | vcaballero | xml.putProperty("autoresizeFlag", isAutoresizeEnabled());
|
163 | 21200 | vcaballero | return xml;
|
164 | } |
||
165 | |||
166 | public boolean isSuitableFor(Geometry geom) { |
||
167 | return true; |
||
168 | } |
||
169 | |||
170 | public void setXMLEntity(XMLEntity xml) { |
||
171 | font = new Font(xml.getStringProperty("font"), |
||
172 | xml.getIntProperty("fontStyle"),
|
||
173 | xml.getIntProperty("size"));
|
||
174 | setDescription(xml.getStringProperty("desc"));
|
||
175 | setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
||
176 | text = xml.getStringProperty("text");
|
||
177 | textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
|
||
178 | setUnit(xml.getIntProperty("unit"));
|
||
179 | setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
||
180 | 22751 | vcaballero | setAutoresizeEnabled(xml.getBooleanProperty("autoresizeFlag"));
|
181 | 21200 | vcaballero | } |
182 | |||
183 | public String getClassName() { |
||
184 | return getClass().getName();
|
||
185 | } |
||
186 | |||
187 | public void print(Graphics2D g, AffineTransform at, org.gvsig.fmap.geom.Geometry geom, PrintRequestAttributeSet properties){ |
||
188 | 23070 | vcaballero | float originalSize = getFont().getSize2D();
|
189 | float size=originalSize;
|
||
190 | // scale it to size
|
||
191 | PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class); |
||
192 | if (pq.equals(PrintQuality.NORMAL)){ |
||
193 | size *= (double) 300/72; |
||
194 | }else if (pq.equals(PrintQuality.HIGH)){ |
||
195 | size *= (double) 600/72; |
||
196 | }else if (pq.equals(PrintQuality.DRAFT)){ |
||
197 | // unitFactor *= 72; (which is the same than doing nothing)
|
||
198 | } |
||
199 | setFont(getFont().deriveFont(size)); |
||
200 | draw(g,at,geom,null);
|
||
201 | setFont(getFont().deriveFont(originalSize)); |
||
202 | 21200 | vcaballero | } |
203 | |||
204 | public String getText() { |
||
205 | return text;
|
||
206 | } |
||
207 | |||
208 | public Font getFont() { |
||
209 | return font;
|
||
210 | } |
||
211 | |||
212 | public Color getTextColor() { |
||
213 | return textColor;
|
||
214 | } |
||
215 | |||
216 | public void setText(String text) { |
||
217 | this.text = text;
|
||
218 | } |
||
219 | |||
220 | public void setFont(Font font) { |
||
221 | this.font = font;
|
||
222 | } |
||
223 | |||
224 | public void setTextColor(Color color) { |
||
225 | this.textColor = color;
|
||
226 | } |
||
227 | |||
228 | public void setFontSize(double size) { |
||
229 | this.font = new Font(font.getName(), font.getStyle(), (int) size); |
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | * Defines the angle of rotation for the text that composes the symbol
|
||
234 | *
|
||
235 | * @param rotation
|
||
236 | */
|
||
237 | public void setRotation(double rotation) { |
||
238 | this.rotation = rotation;
|
||
239 | } |
||
240 | |||
241 | /**
|
||
242 | 22782 | vcaballero | * Returns an Geometry which represents a rectangle containing the text in
|
243 | 21200 | vcaballero | * <b>screen</b> units.
|
244 | */
|
||
245 | public Geometry getTextWrappingShape(org.gvsig.fmap.geom.primitive.Point2D p) {
|
||
246 | Font font = getFont();
|
||
247 | GlyphVector gv = font.createGlyphVector(frc, text);
|
||
248 | |||
249 | Shape shape = gv.getOutline((float) p.getX(), (float) p.getY()); |
||
250 | Geometry myFShape = new Surface2D(new GeneralPathX(shape.getBounds2D())); |
||
251 | |||
252 | myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
|
||
253 | |||
254 | if (rotation != 0) { |
||
255 | myFShape.transform(AffineTransform.getRotateInstance(rotation));
|
||
256 | } |
||
257 | return myFShape;
|
||
258 | } |
||
259 | |||
260 | public Rectangle getBounds() { |
||
261 | // FontMetrics fm = g.getFontMetrics();
|
||
262 | // Rectangle2D rect = fm.getStringBounds("graphics", g);
|
||
263 | |||
264 | Rectangle bounds = getTextWrappingShape(new org.gvsig.fmap.geom.primitive.Point2D(0,0)).getBounds(); |
||
265 | return bounds;
|
||
266 | } |
||
267 | |||
268 | public void setCartographicSize(double cartographicSize, Geometry geom) { |
||
269 | setFontSize(cartographicSize); |
||
270 | } |
||
271 | |||
272 | public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
273 | double oldSize = getFont().getSize();
|
||
274 | setCartographicSize(getCartographicSize( |
||
275 | viewPort, |
||
276 | dpi, |
||
277 | geom), |
||
278 | geom); |
||
279 | return oldSize;
|
||
280 | } |
||
281 | |||
282 | public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
283 | return CartographicSupportToolkit.
|
||
284 | getCartographicLength(this,
|
||
285 | getFont().getSize(), |
||
286 | viewPort, |
||
287 | dpi); |
||
288 | } |
||
289 | 22751 | vcaballero | public boolean isAutoresizeEnabled() { |
290 | return autoresize;
|
||
291 | } |
||
292 | |||
293 | public void setAutoresizeEnabled(boolean autoresizeFlag) { |
||
294 | this.autoresize = autoresizeFlag;
|
||
295 | } |
||
296 | 21200 | vcaballero | } |