source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java@ 3987

Last change on this file since 3987 was 3987, checked in by bastiK, 14 years ago

mapcss: improvements in text label selection, performance and typo (patch by anonymous, see #6107)

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.Utils.equal;
5
6import java.awt.Font;
7import java.util.HashMap;
8import java.util.Map;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.osm.OsmPrimitive;
12import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
13import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
14import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
15
16abstract public class ElemStyle {
17
18 public float z_index;
19 public float object_z_index;
20 public boolean isModifier; // false, if style can serve as main style for the
21 // primitive; true, if it is a highlight or modifier
22
23 public ElemStyle(float z_index, float object_z_index, boolean isModifier) {
24 this.z_index = z_index;
25 this.object_z_index = object_z_index;
26 this.isModifier = isModifier;
27 }
28
29 protected ElemStyle(Cascade c) {
30 z_index = c.get("z-index", 0f, Float.class);
31 object_z_index = c.get("object-z-index", 0f, Float.class);
32 isModifier = c.get("modifier", false, Boolean.class);
33 }
34
35 public abstract void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member);
36
37 /**
38 * Get a property value of type Width
39 * @param c the cascade
40 * @param key property key for the width value
41 * @param relativeTo reference width. Only needed, when relative width syntax
42 * is used, e.g. "+4".
43 */
44 protected static Float getWidth(Cascade c, String key, Float relativeTo) {
45 Float width = c.get(key, null, Float.class, true);
46 if (width != null) {
47 if (width > 0)
48 return width;
49 } else {
50 Keyword widthKW = c.get(key, null, Keyword.class, true);
51 if (equal(widthKW, Keyword.THINNEST))
52 return 0f;
53 if (equal(widthKW, Keyword.DEFAULT))
54 return (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
55 if (relativeTo != null) {
56 RelativeFloat width_rel = c.get(key, null, RelativeFloat.class, true);
57 if (width_rel != null)
58 return relativeTo + width_rel.val;
59 }
60 }
61 return null;
62 }
63
64 /* ------------------------------------------------------------------------------- */
65 /* cached values */
66 /* ------------------------------------------------------------------------------- */
67 /*
68 * Two preference values and the set of created fonts are cached in order to avoid
69 * expensive lookups and to avoid too many font objects
70 * (in analogy to flyweight pattern).
71 *
72 * FIXME: cached preference values are not updated if the user changes them during
73 * a JOSM session. Should have a listener listening to preference changes.
74 */
75 static private String DEFAULT_FONT_NAME = null;
76 static private Float DEFAULT_FONT_SIZE = null;
77 static private void initDefaultFontParameters() {
78 if (DEFAULT_FONT_NAME != null) return; // already initialized - skip initialization
79 DEFAULT_FONT_NAME = Main.pref.get("mappaint.font", "Helvetica");
80 DEFAULT_FONT_SIZE = (float) Main.pref.getInteger("mappaint.fontsize", 8);
81 }
82
83 static private class FontDescriptor {
84 public String name;
85 public int style;
86 public int size;
87
88 public FontDescriptor(String name, int style, int size){
89 this.name = name;
90 this.style = style;
91 this.size = size;
92 }
93
94 @Override
95 public int hashCode() {
96 final int prime = 31;
97 int result = 1;
98 result = prime * result + ((name == null) ? 0 : name.hashCode());
99 result = prime * result + size;
100 result = prime * result + style;
101 return result;
102 }
103 @Override
104 public boolean equals(Object obj) {
105 if (this == obj)
106 return true;
107 if (obj == null)
108 return false;
109 if (getClass() != obj.getClass())
110 return false;
111 FontDescriptor other = (FontDescriptor) obj;
112 if (name == null) {
113 if (other.name != null)
114 return false;
115 } else if (!name.equals(other.name))
116 return false;
117 if (size != other.size)
118 return false;
119 if (style != other.style)
120 return false;
121 return true;
122 }
123 }
124
125 static private final Map<FontDescriptor, Font> FONT_MAP = new HashMap<FontDescriptor, Font>();
126 static private Font getCachedFont(FontDescriptor fd) {
127 Font f = FONT_MAP.get(fd);
128 if (f != null) return f;
129 f = new Font(fd.name, fd.style, fd.size);
130 FONT_MAP.put(fd, f);
131 return f;
132 }
133
134 static private Font getCachedFont(String name, int style, int size){
135 return getCachedFont(new FontDescriptor(name, style, size));
136 }
137
138 protected static Font getFont(Cascade c) {
139 initDefaultFontParameters(); // populated cached preferences, if necesary
140 String name = c.get("font-family", DEFAULT_FONT_NAME, String.class);
141 float size = c.get("font-size", DEFAULT_FONT_SIZE, Float.class);
142 int weight = Font.PLAIN;
143 Keyword weightKW = c.get("font-weight", null, Keyword.class);
144 if (weightKW != null && equal(weightKW, "bold")) {
145 weight = Font.BOLD;
146 }
147 int style = Font.PLAIN;
148 Keyword styleKW = c.get("font-style", null, Keyword.class);
149 if (styleKW != null && equal(styleKW.val, "italic")) {
150 style = Font.ITALIC;
151 }
152 return getCachedFont(name, style | weight, Math.round(size));
153 }
154
155 @Override
156 public boolean equals(Object o) {
157 if (!(o instanceof ElemStyle))
158 return false;
159 ElemStyle s = (ElemStyle) o;
160 return z_index == s.z_index && object_z_index == s.object_z_index && isModifier == s.isModifier;
161 }
162
163 @Override
164 public int hashCode() {
165 int hash = 5;
166 hash = 41 * hash + Float.floatToIntBits(this.z_index);
167 hash = 41 * hash + Float.floatToIntBits(this.object_z_index);
168 hash = 41 * hash + (isModifier ? 1 : 0);
169 return hash;
170 }
171
172 @Override
173 public String toString() {
174 if (z_index != 0f || object_z_index != 0f)
175 return String.format("z_idx=%s/%s ", z_index, object_z_index) + (isModifier ? "modifier " : "");
176 return "";
177 }
178}
Note: See TracBrowser for help on using the repository browser.