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

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

MapCSS: an identifier literal at the right side of a declaration is now parsed as Keyword and not as String. This means 'color: "blue";' does not work any longer, but you have to write 'color: blue;'. This is useful for future extensions.

  • Property svn:eol-style set to native
File size: 3.8 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;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
11import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
12import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
13
14abstract public class ElemStyle {
15
16 public float z_index;
17 public float object_z_index;
18 public boolean isModifier; // false, if style can serve as main style for the
19 // primitive; true, if it is a highlight or modifier
20
21 public ElemStyle(float z_index, float object_z_index, boolean isModifier) {
22 this.z_index = z_index;
23 this.object_z_index = object_z_index;
24 this.isModifier = isModifier;
25 }
26
27 protected ElemStyle(Cascade c) {
28 z_index = c.get("z-index", 0f, Float.class);
29 object_z_index = c.get("object-z-index", 0f, Float.class);
30 isModifier = c.get("modifier", false, Boolean.class);
31 }
32
33 public abstract void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member);
34
35 /**
36 * Get a property value of type Width
37 * @param c the cascade
38 * @param key property key for the width value
39 * @param relativeTo reference width. Only needed, when relative width syntax
40 * is used, e.g. "+4".
41 */
42 protected static Float getWidth(Cascade c, String key, Float relativeTo) {
43 Float width = c.get(key, null, Float.class, true);
44 if (width != null) {
45 if (width > 0)
46 return width;
47 } else {
48 Keyword widthKW = c.get(key, null, Keyword.class, true);
49 if (equal(widthKW, Keyword.THINNEST))
50 return 0f;
51 if (equal(widthKW, Keyword.DEFAULT))
52 return (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
53 if (relativeTo != null) {
54 RelativeFloat width_rel = c.get(key, null, RelativeFloat.class, true);
55 if (width_rel != null)
56 return relativeTo + width_rel.val;
57 }
58 }
59 return null;
60 }
61
62 protected static Font getFont(Cascade c) {
63 String name = c.get("font-family", Main.pref.get("mappaint.font", "Helvetica"), String.class);
64 float size = c.get("font-size", (float) Main.pref.getInteger("mappaint.fontsize", 8), Float.class);
65 int weight = Font.PLAIN;
66 Keyword weightKW = c.get("font-wheight", null, Keyword.class);
67 if (weightKW != null && equal(weightKW, "bold")) {
68 weight = Font.BOLD;
69 }
70 int style = Font.PLAIN;
71 Keyword styleKW = c.get("font-style", null, Keyword.class);
72 if (styleKW != null && equal(styleKW.val, "italic")) {
73 style = Font.ITALIC;
74 }
75 return new Font(name, weight | style, Math.round(size));
76 }
77
78 @Override
79 public boolean equals(Object o) {
80 if (!(o instanceof ElemStyle))
81 return false;
82 ElemStyle s = (ElemStyle) o;
83 return z_index == s.z_index && object_z_index == s.object_z_index && isModifier == s.isModifier;
84 }
85
86 @Override
87 public int hashCode() {
88 int hash = 5;
89 hash = 41 * hash + Float.floatToIntBits(this.z_index);
90 hash = 41 * hash + Float.floatToIntBits(this.object_z_index);
91 hash = 41 * hash + (isModifier ? 1 : 0);
92 return hash;
93 }
94
95 @Override
96 public String toString() {
97 if (z_index != 0f || object_z_index != 0f)
98 return String.format("z_idx=%s/%s ", z_index, object_z_index) + (isModifier ? "modifier " : "");
99 return "";
100 }
101}
Note: See TracBrowser for help on using the repository browser.