1 30 31 package com.jgoodies.animation.renderer; 32 33 import java.awt.Color ; 34 import java.awt.Font ; 35 import java.awt.Graphics2D ; 36 import java.awt.Shape ; 37 import java.awt.font.FontRenderContext ; 38 import java.awt.font.GlyphVector ; 39 import java.awt.geom.Rectangle2D ; 40 41 import com.jgoodies.animation.*; 42 43 49 public abstract class AbstractTextRenderer implements AnimationRenderer { 50 51 private String text; 52 private Font font; 53 private Color color; 54 private HeightMode heightMode = HeightMode.CAPITAL_ASCENT; 55 56 protected GlyphVector cachedGlyphVector; 58 protected Shape [] cachedGlyphShapes; 59 protected float cachedTextWidth; 60 protected float cachedTextAscent; 61 protected float cachedTextHeight; 62 protected float capitalMAscent = -1f; private boolean cacheValid = false; 64 65 66 68 AbstractTextRenderer(String text) { 69 this(text, null); 70 } 71 72 AbstractTextRenderer(String text, Font font) { 73 this.text = text == null ? "Karsten Lentzsch" : text; 74 this.font = font == null ? createDefaultFont() : font; 75 } 76 77 82 private static Font createDefaultFont() { 83 return new Font ("dialog", Font.BOLD, 12); 84 } 85 86 87 89 public Color getColor() { 90 return color; 91 } 92 93 public Font getFont() { 94 return font; 95 } 96 97 public String getText() { 98 return text; 99 } 100 101 public HeightMode getHeightMode() { 102 return heightMode; 103 } 104 105 public void setColor(Color color) { 106 this.color = color; 107 } 108 109 public void setHeightMode(HeightMode heightMode) { 110 this.heightMode = heightMode; 111 } 112 113 118 public void setFont(Font newFont) { 119 if (newFont == null) 120 throw new NullPointerException ("The font must not be null."); 121 122 if (newFont.equals(font)) 123 return; 124 125 font = newFont; 126 invalidateCache(); 127 } 128 129 134 public void setText(String newText) { 135 if (newText == null) 136 throw new NullPointerException ("The text must not be null."); 137 138 if (newText.equals(text)) 139 return; 140 141 text = newText; 142 invalidateCache(); 143 } 144 145 151 protected float getAdjustedAscent() { 152 if (heightMode == HeightMode.CAPITAL_ASCENT) 153 return capitalMAscent; 154 else if (heightMode == HeightMode.TEXT_ASCENT) 155 return cachedTextAscent; 156 else 157 return cachedTextHeight; 158 } 159 160 166 protected float getAdjustedDescent() { 167 if (heightMode == HeightMode.CAPITAL_ASCENT) 168 return 0; 169 else if (heightMode == HeightMode.TEXT_ASCENT) 170 return 0; 171 else 172 return cachedTextHeight - cachedTextAscent; 173 } 174 175 177 protected boolean isCacheValid() { 178 return cacheValid; 179 } 180 181 protected void setCacheValid(boolean b) { 182 cacheValid = b; 183 } 184 185 protected void ensureValidCache(Graphics2D g2) { 186 if (!isCacheValid()) 187 validateCache(g2); 188 } 189 190 196 protected void validateCache(Graphics2D g2) { 197 FontRenderContext frc = g2.getFontRenderContext(); 198 199 ensureCapitalMAscentComputed(frc); 200 201 cachedGlyphVector = font.createGlyphVector(frc, text); 202 Rectangle2D bounds = cachedGlyphVector.getVisualBounds(); 203 cachedTextWidth = (float) bounds.getWidth(); 204 cachedTextAscent = (float) - bounds.getY(); 205 cachedTextHeight = (float) bounds.getHeight(); 206 207 int glyphCount = cachedGlyphVector.getNumGlyphs(); 208 cachedGlyphShapes = new Shape [glyphCount]; 209 for (int i = 0; i < glyphCount; i++) { 210 cachedGlyphShapes[i] = cachedGlyphVector.getGlyphOutline(i); 211 } 212 setCacheValid(true); 213 214 218 } 219 220 225 private void ensureCapitalMAscentComputed(FontRenderContext frc) { 226 if (capitalMAscent > 0) 227 return; 228 GlyphVector mGlyphVector = font.createGlyphVector(frc, "M"); 229 capitalMAscent = (float) - mGlyphVector.getVisualBounds().getY(); 230 } 231 232 235 protected void invalidateCache() { 236 setCacheValid(false); 237 cachedGlyphVector = null; 238 cachedGlyphShapes = null; 239 } 240 241 } | Popular Tags |