1 package org.joshy.html.util; 2 3 import java.awt.Font ; 4 import org.w3c.dom.Node ; 5 import org.w3c.dom.Element ; 6 import org.joshy.u; 7 import java.awt.Graphics2D ; 8 import java.awt.font.*; 9 import org.joshy.html.box.*; 10 import org.joshy.html.Context; 11 import org.joshy.html.InlineLayout; 12 13 public class FontUtil { 14 15 public static int len(Context c, Node node, String str) { 16 return c.getGraphics().getFontMetrics(getFont(c,node)).stringWidth(str); 17 } 18 19 public static int lineHeight(Context c, Node node) { 20 return c.getGraphics().getFontMetrics(getFont(c,node)).getHeight(); 21 } 22 23 public static Font getFont(Context c, Node e) { 24 27 if(e.getNodeType() == e.TEXT_NODE) { 29 Element el = (Element )e.getParentNode(); 31 return getElementFont(c,el); 32 } 33 34 if(e.getNodeType() == e.ELEMENT_NODE) { 35 Element el = (Element )e; 36 return getElementFont(c,el); 37 } 38 39 u.p("big error in getFont(). Got a node that is neither txt nor element"); 40 return null; 41 } 42 43 static boolean quick = false; 44 public static Font getElementFont(Context c, Element el) { 45 Font f = c.getGraphics().getFont(); 47 if(quick) { 48 return f; 50 } 51 52 if(el.getParentNode().getNodeType() == el.DOCUMENT_NODE) { 53 return c.getGraphics().getFont().deriveFont((float)10); 55 } 56 57 58 59 Element par = (Element )el.getParentNode(); 65 float parent_size = c.css.getFloatProperty(par,"font-size"); 66 float size = c.css.getFloatProperty(el,"font-size",parent_size); 67 68 String weight = c.css.getStringProperty(el,"font-weight"); 69 String [] family = c.css.getStringArrayProperty(el,"font-family"); 70 79 String style = c.css.getStringProperty(el,"font-style"); 80 81 f = c.getFontResolver().resolveFont(c,family,size,weight,style); 82 83 c.getGraphics().setColor(c.css.getColor(el)); 85 return f; 86 } 87 88 89 public static void setupTextDecoration(Context c, Node node, InlineBox box) { 90 Element el = null; 91 if(node instanceof Element ) { 92 el = (Element )node; 93 } else { 94 el = (Element )node.getParentNode(); 95 } 96 String text_decoration = c.css.getStringProperty(el,"text-decoration"); 97 if(text_decoration != null && text_decoration.equals("underline")) { 98 box.underline = true; 99 } 100 if(text_decoration != null && text_decoration.equals("line-through")) { 101 box.strikethrough = true; 102 } 103 if(text_decoration != null && text_decoration.equals("overline")) { 104 box.overline = true; 105 } 106 107 } 108 109 public static void setupVerticalAlign(Context c, Node node, InlineBox box) { 110 Node parent = node.getParentNode(); 113 Element elem = null; 115 if(node.getNodeType() == node.TEXT_NODE) { 116 parent = parent.getParentNode(); 117 elem = (Element )node.getParentNode(); 118 } else { 119 elem = (Element )node; 120 } 121 123 Font parent_font = FontUtil.getFont(c,parent); 125 LineMetrics parent_metrics = null; 126 if(!InlineLayout.isReplaced(node)) { 127 if(!InlineLayout.isFloatedBlock(node,c)) { 128 parent_metrics = parent_font.getLineMetrics(box.text, ((Graphics2D )c.getGraphics()).getFontRenderContext()); 129 } else { 130 parent_metrics = parent_font.getLineMetrics("Test", ((Graphics2D )c.getGraphics()).getFontRenderContext()); 131 } 132 } else { 133 parent_metrics = parent_font.getLineMetrics("Test", ((Graphics2D )c.getGraphics()).getFontRenderContext()); 134 } 135 136 137 float parent_height = parent_metrics.getHeight(); 139 String vertical_align = c.css.getStringProperty(elem,"vertical-align"); 141 142 if(!InlineLayout.isReplaced(node)) { 144 box.height = FontUtil.lineHeight(c,node); 145 } 146 148 if(vertical_align == null) { 149 vertical_align = "baseline"; 150 } 151 box.baseline = 0; 152 box.y = 0; 154 box.vset = true; 156 if(vertical_align.equals("baseline")) { 157 } 159 160 if(vertical_align.equals("super")) { 162 box.y = box.y + (int) (parent_metrics.getStrikethroughOffset()*2.0); 163 } 164 165 if(vertical_align.equals("sub")) { 167 box.y = box.y - (int) parent_metrics.getStrikethroughOffset(); 168 } 169 170 if(vertical_align.equals("text-top")) { 173 box.y = -((int)parent_height - box.height); } 178 179 if(vertical_align.equals("text-bottom")) { 181 box.y = 0; 182 } 183 184 if(vertical_align.equals("top")) { 186 box.y = box.y - box.baseline; box.top_align = true; 190 box.vset = false; 192 } 193 if(vertical_align.equals("bottom")) { 194 box.y = box.y - box.baseline; box.bottom_align = true; 198 box.vset = false; 200 } 201 } 203 204 public static void setupVerticalAlign(Context c, Node node, LineBox box) { 205 Node parent = node.getParentNode(); 207 Element elem = null; 208 if(node.getNodeType() == node.TEXT_NODE) { 209 parent = parent.getParentNode(); 210 elem = (Element )node.getParentNode(); 211 } else { 212 elem = (Element )node; 213 } 214 215 216 int top = 0; 218 int bot = 0; 219 int height = 0; 220 for(int i=0; i<box.getChildCount(); i++) { 221 InlineBox inline = (InlineBox)box.getChild(i); 222 if(inline.floated) { continue; } 224 if(inline.vset) { 225 if(inline.y - inline.height < top) { 228 top = inline.y - inline.height; 229 } 230 if(inline.y + 0 > bot) { 232 bot = inline.y + 0; 233 } 234 } else { 235 if(inline.height > height) { 238 height = inline.height; 239 } 240 } 241 } 242 243 if(bot-top > height) { 245 box.height = bot-top; 246 box.baseline = box.height - bot; 247 } else { 248 box.height = height; 249 box.baseline = box.height; 250 } 251 252 253 255 for(int i=0; i<box.getChildCount(); i++) { 257 InlineBox inline = (InlineBox)box.getChild(i); 258 if(inline.floated) { 259 inline.y = -box.baseline+inline.height; 260 } else { 261 if(!inline.vset) { 262 inline.vset = true; 263 if(inline.top_align) { 264 inline.y = -box.baseline+inline.height; 265 } 266 if(inline.bottom_align) { 267 inline.y = 0; 268 } 269 } 270 } 271 } 273 274 } 275 276 } 277 | Popular Tags |