1 47 48 package com.lowagie.text.html.simpleparser; 49 50 import java.awt.Color ; 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 import java.util.Properties ; 54 import java.util.StringTokenizer ; 55 56 import com.lowagie.text.Chunk; 57 import com.lowagie.text.Element; 58 import com.lowagie.text.Font; 59 import com.lowagie.text.FontFactory; 60 import com.lowagie.text.FontFactoryImp; 61 import com.lowagie.text.ListItem; 62 import com.lowagie.text.Paragraph; 63 import com.lowagie.text.html.Markup; 64 import com.lowagie.text.pdf.BaseFont; 65 66 70 public class FactoryProperties { 71 72 private FontFactoryImp fontImp = FontFactory.getFontImp(); 73 74 75 public FactoryProperties() { 76 } 77 78 public Chunk createChunk(String text, ChainedProperties props) { 79 Font font = getFont(props); 80 float size = font.getSize(); 81 size /= 2; 82 Chunk ck = new Chunk(text, font); 83 if (props.hasProperty("sub")) 84 ck.setTextRise(-size); 85 else if (props.hasProperty("sup")) 86 ck.setTextRise(size); 87 return ck; 88 } 89 90 private static void setParagraphLeading(Paragraph p, String leading) { 91 if (leading == null) { 92 p.setLeading(0, 1.5f); 93 return; 94 } 95 try { 96 StringTokenizer tk = new StringTokenizer (leading, " ,"); 97 String v = tk.nextToken(); 98 float v1 = Float.parseFloat(v); 99 if (!tk.hasMoreTokens()) { 100 p.setLeading(v1, 0); 101 return; 102 } 103 v = tk.nextToken(); 104 float v2 = Float.parseFloat(v); 105 p.setLeading(v1, v2); 106 } 107 catch (Exception e) { 108 p.setLeading(0, 1.5f); 109 } 110 } 111 112 public static Paragraph createParagraph(HashMap props) { 113 Paragraph p = new Paragraph(); 114 String value = (String )props.get("align"); 115 if (value != null) { 116 if (value.equalsIgnoreCase("center")) 117 p.setAlignment(Element.ALIGN_CENTER); 118 else if (value.equalsIgnoreCase("right")) 119 p.setAlignment(Element.ALIGN_RIGHT); 120 else if (value.equalsIgnoreCase("justify")) 121 p.setAlignment(Element.ALIGN_JUSTIFIED); 122 } 123 setParagraphLeading(p, (String )props.get("leading")); 124 return p; 125 } 126 127 public static void createParagraph(Paragraph p, ChainedProperties props) { 128 String value = props.getProperty("align"); 129 if (value != null) { 130 if (value.equalsIgnoreCase("center")) 131 p.setAlignment(Element.ALIGN_CENTER); 132 else if (value.equalsIgnoreCase("right")) 133 p.setAlignment(Element.ALIGN_RIGHT); 134 else if (value.equalsIgnoreCase("justify")) 135 p.setAlignment(Element.ALIGN_JUSTIFIED); 136 } 137 setParagraphLeading(p, props.getProperty("leading")); 138 value = props.getProperty("before"); 139 if (value != null) { 140 try { 141 p.setSpacingBefore(Float.parseFloat(value)); 142 } 143 catch (Exception e) {} 144 } 145 value = props.getProperty("after"); 146 if (value != null) { 147 try { 148 p.setSpacingAfter(Float.parseFloat(value)); 149 } 150 catch (Exception e) {} 151 } 152 value = props.getProperty("extraparaspace"); 153 if (value != null) { 154 try { 155 p.setExtraParagraphSpace(Float.parseFloat(value)); 156 } 157 catch (Exception e) {} 158 } 159 } 160 161 public static Paragraph createParagraph(ChainedProperties props) { 162 Paragraph p = new Paragraph(); 163 createParagraph(p, props); 164 return p; 165 } 166 167 public static ListItem createListItem(ChainedProperties props) { 168 ListItem p = new ListItem(); 169 createParagraph(p, props); 170 return p; 171 } 172 173 public Font getFont(ChainedProperties props) { 174 String face = props.getProperty("face"); 175 if (face != null) { 176 StringTokenizer tok = new StringTokenizer (face, ","); 177 while (tok.hasMoreTokens()) { 178 face = tok.nextToken().trim(); 179 if (face.startsWith("\"")) 180 face = face.substring(1); 181 if (face.endsWith("\"")) 182 face = face.substring(0, face.length() - 1); 183 if (fontImp.isRegistered(face)) 184 break; 185 } 186 } 187 int style = 0; 188 if (props.hasProperty("i")) 189 style |= Font.ITALIC; 190 if (props.hasProperty("b")) 191 style |= Font.BOLD; 192 if (props.hasProperty("u")) 193 style |= Font.UNDERLINE; 194 String value = props.getProperty("size"); 195 float size = 12; 196 if (value != null) 197 size = Float.parseFloat(value); 198 Color color = Markup.decodeColor(props.getProperty("color")); 199 String encoding = props.getProperty("encoding"); 200 if (encoding == null) 201 encoding = BaseFont.WINANSI; 202 return fontImp.getFont(face, encoding, true, size, style, color); 203 } 204 205 public static void insertStyle(HashMap h) { 206 String style = (String )h.get("style"); 207 if (style == null) 208 return; 209 Properties prop = Markup.parseAttributes(style); 210 for (Iterator it = prop.keySet().iterator(); it.hasNext();) { 211 String key = (String )it.next(); 212 if (key.equals(Markup.CSS_KEY_FONTFAMILY)) { 213 h.put("face", prop.getProperty(key)); 214 } 215 else if (key.equals(Markup.CSS_KEY_FONTSIZE)) { 216 h.put("size", Float.toString(Markup.parseLength(prop.getProperty(key))) + "px"); 217 } 218 else if (key.equals(Markup.CSS_KEY_FONTSTYLE)) { 219 String ss = prop.getProperty(key).trim().toLowerCase(); 220 if (ss.equals("italic") || ss.equals("oblique")) 221 h.put("i", null); 222 } 223 else if (key.equals(Markup.CSS_KEY_FONTWEIGHT)) { 224 String ss = prop.getProperty(key).trim().toLowerCase(); 225 if (ss.equals("bold") || ss.equals("700") || ss.equals("800") || ss.equals("900")) 226 h.put("b", null); 227 } 228 else if (key.equals(Markup.CSS_KEY_FONTWEIGHT)) { 229 String ss = prop.getProperty(key).trim().toLowerCase(); 230 if (ss.equals("underline")) 231 h.put("u", null); 232 } 233 else if (key.equals(Markup.CSS_KEY_COLOR)) { 234 Color c = Markup.decodeColor(prop.getProperty(key)); 235 if (c != null) { 236 int hh = c.getRGB(); 237 String hs = Integer.toHexString(hh); 238 hs = "000000" + hs; 239 hs = "#" + hs.substring(hs.length() - 6); 240 h.put("color", hs); 241 } 242 } 243 else if (key.equals(Markup.CSS_KEY_LINEHEIGHT)) { 244 String ss = prop.getProperty(key).trim(); 245 float v = Markup.parseLength(prop.getProperty(key)); 246 if (ss.endsWith("%")) { 247 h.put("leading", "0," + (v / 100)); 248 } 249 else { 250 h.put("leading", v + ",0"); 251 } 252 } 253 else if (key.equals(Markup.CSS_KEY_TEXTALIGN)) { 254 String ss = prop.getProperty(key).trim().toLowerCase(); 255 h.put("align", ss); 256 } 257 } 258 } 259 260 public FontFactoryImp getFontImp() { 261 return fontImp; 262 } 263 264 public void setFontImp(FontFactoryImp fontImp) { 265 this.fontImp = fontImp; 266 } 267 268 public static HashMap followTags = new HashMap (); 269 static { 270 followTags.put("i", "i"); 271 followTags.put("b", "b"); 272 followTags.put("u", "u"); 273 followTags.put("sub", "sub"); 274 followTags.put("sup", "sup"); 275 followTags.put("em", "i"); 276 followTags.put("strong", "b"); 277 } 278 } 279 | Popular Tags |