1 21 22 package org.lobobrowser.html.style; 23 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Toolkit ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 public class FontStyleRenderState extends RenderStateDelegator { 31 private final int style; 32 33 public FontStyleRenderState(RenderState prevRenderState, int style) { 34 super(prevRenderState); 35 this.style = style; 36 } 37 38 private Font iFont; 39 40 public Font getFont() { 41 Font f = this.iFont; 42 if(f != null) { 43 return f; 44 } 45 Font parentFont = this.prevRenderState.getFont(); 46 f = parentFont.deriveFont(this.style | parentFont.getStyle()); 47 this.iFont = f; 48 return f; 49 } 50 51 private FontMetrics iFontMetrics; 52 53 public FontMetrics getFontMetrics() { 54 FontMetrics fm = this.iFontMetrics; 55 if(fm == null) { 56 fm = Toolkit.getDefaultToolkit().getFontMetrics(this.getFont()); 58 this.iFontMetrics = fm; 59 } 60 return fm; 61 } 62 63 public void invalidate() { 64 this.prevRenderState.invalidate(); 65 this.iFont = null; 66 this.iFontMetrics = null; 67 Map map = this.iWordInfoMap; 68 if(map != null) { 69 map.clear(); 70 } 71 } 72 73 Map iWordInfoMap = null; 74 75 public final WordInfo getWordInfo(String word) { 76 Map map = this.iWordInfoMap; 79 if(map == null) { 80 map = new HashMap (1); 81 this.iWordInfoMap = map; 82 } 83 WordInfo wi = (WordInfo) map.get(word); 84 if(wi != null) { 85 return wi; 86 } 87 wi = new WordInfo(); 88 FontMetrics fm = this.getFontMetrics(); 89 wi.fontMetrics = fm; 90 wi.ascentPlusLeading = fm.getAscent() + fm.getLeading(); 91 wi.descent = fm.getDescent(); 92 wi.height = fm.getHeight(); 93 wi.width = fm.stringWidth(word); 94 map.put(word, wi); 95 return wi; 96 } 97 } 98 | Popular Tags |