Ignore:
Timestamp:
2011-03-13T20:44:57+01:00 (13 years ago)
Author:
bastiK
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java

    r3967 r3987  
    55
    66import java.awt.Font;
     7import java.util.HashMap;
     8import java.util.Map;
    79
    810import org.openstreetmap.josm.Main;
     
    1719    public float object_z_index;
    1820    public boolean isModifier;  // false, if style can serve as main style for the
    19                                 // primitive; true, if it is a highlight or modifier
     21    // primitive; true, if it is a highlight or modifier
    2022
    2123    public ElemStyle(float z_index, float object_z_index, boolean isModifier) {
     
    6062    }
    6163
     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
    62138    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);
     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);
    65142        int weight = Font.PLAIN;
    66         Keyword weightKW = c.get("font-wheight", null, Keyword.class);
     143        Keyword weightKW = c.get("font-weight", null, Keyword.class);
    67144        if (weightKW != null && equal(weightKW, "bold")) {
    68145            weight = Font.BOLD;
     
    73150            style = Font.ITALIC;
    74151        }
    75         return new Font(name, weight | style, Math.round(size));
     152        return getCachedFont(name, style | weight, Math.round(size));
    76153    }
    77154
Note: See TracChangeset for help on using the changeset viewer.