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

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

mapcss: new LinePattern style element similar to mapnik's LinePatternSymbolizer; split off the text part of LineElemStyle, so it also works for LinePattern

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