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 31 public class FontSizeRenderState extends RenderStateDelegator { 32 private final float fontSize; 33 private final int fontStyle; 34 35 public FontSizeRenderState(RenderState prevRenderState, float fontSize, int fontStyle) { 36 super(prevRenderState); 37 this.fontSize = fontSize; 38 this.fontStyle = fontStyle; 39 } 40 41 public FontSizeRenderState(RenderState prevRenderState, float fontSize) { 42 super(prevRenderState); 43 this.fontSize = fontSize; 44 this.fontStyle = Font.PLAIN; 45 } 46 47 private Font iFont; 48 49 public Font getFont() { 50 Font f = this.iFont; 51 if(f != null) { 52 return f; 53 } 54 Font parentFont = this.prevRenderState.getFont(); 55 f = parentFont.deriveFont(this.fontSize); 56 f = f.deriveFont(this.fontStyle | f.getStyle()); 57 this.iFont = f; 58 return f; 59 } 60 61 private FontMetrics iFontMetrics; 62 63 public FontMetrics getFontMetrics() { 64 FontMetrics fm = this.iFontMetrics; 65 if(fm == null) { 66 fm = Toolkit.getDefaultToolkit().getFontMetrics(this.getFont()); 68 this.iFontMetrics = fm; 69 } 70 return fm; 71 } 72 73 public void invalidate() { 74 this.prevRenderState.invalidate(); 75 this.iFont = null; 76 this.iFontMetrics = null; 77 Map map = this.iWordInfoMap; 78 if(map != null) { 79 map.clear(); 80 } 81 } 82 83 Map iWordInfoMap = null; 84 85 public final WordInfo getWordInfo(String word) { 86 Map map = this.iWordInfoMap; 89 if(map == null) { 90 map = new HashMap (1); 91 this.iWordInfoMap = map; 92 } 93 WordInfo wi = (WordInfo) map.get(word); 94 if(wi != null) { 95 return wi; 96 } 97 wi = new WordInfo(); 98 FontMetrics fm = this.getFontMetrics(); 99 wi.fontMetrics = fm; 100 wi.ascentPlusLeading = fm.getAscent() + fm.getLeading(); 101 wi.descent = fm.getDescent(); 102 wi.height = fm.getHeight(); 103 wi.width = fm.stringWidth(word); 104 map.put(word, wi); 105 return wi; 106 } 107 } 108 | Popular Tags |