1 50 51 package com.lowagie.text.pdf.codec.wmf; 52 53 import java.awt.Color ; 54 import java.awt.Point ; 55 import java.util.ArrayList ; 56 import java.util.Stack ; 57 58 import com.lowagie.text.pdf.PdfContentByte; 59 60 public class MetaState { 61 62 public static final int TA_NOUPDATECP = 0; 63 public static final int TA_UPDATECP = 1; 64 public static final int TA_LEFT = 0; 65 public static final int TA_RIGHT = 2; 66 public static final int TA_CENTER = 6; 67 public static final int TA_TOP = 0; 68 public static final int TA_BOTTOM = 8; 69 public static final int TA_BASELINE = 24; 70 71 public static final int TRANSPARENT = 1; 72 public static final int OPAQUE = 2; 73 74 public static final int ALTERNATE = 1; 75 public static final int WINDING = 2; 76 77 public Stack savedStates; 78 public ArrayList MetaObjects; 79 public Point currentPoint; 80 public MetaPen currentPen; 81 public MetaBrush currentBrush; 82 public MetaFont currentFont; 83 public Color currentBackgroundColor = Color.white; 84 public Color currentTextColor = Color.black; 85 public int backgroundMode = OPAQUE; 86 public int polyFillMode = ALTERNATE; 87 public int lineJoin = 1; 88 public int textAlign; 89 public int offsetWx; 90 public int offsetWy; 91 public int extentWx; 92 public int extentWy; 93 public float scalingX; 94 public float scalingY; 95 96 97 98 public MetaState() { 99 savedStates = new Stack (); 100 MetaObjects = new ArrayList (); 101 currentPoint = new Point (0, 0); 102 currentPen = new MetaPen(); 103 currentBrush = new MetaBrush(); 104 currentFont = new MetaFont(); 105 } 106 107 public MetaState(MetaState state) { 108 setMetaState(state); 109 } 110 111 public void setMetaState(MetaState state) { 112 savedStates = state.savedStates; 113 MetaObjects = state.MetaObjects; 114 currentPoint = state.currentPoint; 115 currentPen = state.currentPen; 116 currentBrush = state.currentBrush; 117 currentFont = state.currentFont; 118 currentBackgroundColor = state.currentBackgroundColor; 119 currentTextColor = state.currentTextColor; 120 backgroundMode = state.backgroundMode; 121 polyFillMode = state.polyFillMode; 122 textAlign = state.textAlign; 123 lineJoin = state.lineJoin; 124 offsetWx = state.offsetWx; 125 offsetWy = state.offsetWy; 126 extentWx = state.extentWx; 127 extentWy = state.extentWy; 128 scalingX = state.scalingX; 129 scalingY = state.scalingY; 130 } 131 132 public void addMetaObject(MetaObject object) { 133 for (int k = 0; k < MetaObjects.size(); ++k) { 134 if (MetaObjects.get(k) == null) { 135 MetaObjects.set(k, object); 136 return; 137 } 138 } 139 MetaObjects.add(object); 140 } 141 142 public void selectMetaObject(int index, PdfContentByte cb) { 143 MetaObject obj = (MetaObject)MetaObjects.get(index); 144 if (obj == null) 145 return; 146 int style; 147 switch (obj.getType()) { 148 case MetaObject.META_BRUSH: 149 currentBrush = (MetaBrush)obj; 150 style = currentBrush.getStyle(); 151 if (style == MetaBrush.BS_SOLID) { 152 Color color = currentBrush.getColor(); 153 cb.setColorFill(color); 154 } 155 else if (style == MetaBrush.BS_HATCHED) { 156 Color color = currentBackgroundColor; 157 cb.setColorFill(color); 158 } 159 break; 160 case MetaObject.META_PEN: 161 { 162 currentPen = (MetaPen)obj; 163 style = currentPen.getStyle(); 164 if (style != MetaPen.PS_NULL) { 165 Color color = currentPen.getColor(); 166 cb.setColorStroke(color); 167 cb.setLineWidth(Math.abs((float)currentPen.getPenWidth() * scalingX / extentWx)); 168 switch (style) { 169 case MetaPen.PS_DASH: 170 cb.setLineDash(18, 6, 0); 171 break; 172 case MetaPen.PS_DASHDOT: 173 cb.setLiteral("[9 6 3 6]0 d\n"); 174 break; 175 case MetaPen.PS_DASHDOTDOT: 176 cb.setLiteral("[9 3 3 3 3 3]0 d\n"); 177 break; 178 case MetaPen.PS_DOT: 179 cb.setLineDash(3, 0); 180 break; 181 default: 182 cb.setLineDash(0); 183 break; 184 } 185 } 186 break; 187 } 188 case MetaObject.META_FONT: 189 { 190 currentFont = (MetaFont)obj; 191 break; 192 } 193 } 194 } 195 196 public void deleteMetaObject(int index) { 197 MetaObjects.set(index, null); 198 } 199 200 public void saveState(PdfContentByte cb) { 201 cb.saveState(); 202 MetaState state = new MetaState(this); 203 savedStates.push(state); 204 } 205 206 public void restoreState(int index, PdfContentByte cb) { 207 int pops; 208 if (index < 0) 209 pops = Math.min(-index, savedStates.size()); 210 else 211 pops = Math.max(savedStates.size() - index, 0); 212 if (pops == 0) 213 return; 214 MetaState state = null; 215 while (pops-- != 0) { 216 cb.restoreState(); 217 state = (MetaState)savedStates.pop(); 218 } 219 setMetaState(state); 220 } 221 222 public void cleanup(PdfContentByte cb) { 223 int k = savedStates.size(); 224 while (k-- > 0) 225 cb.restoreState(); 226 } 227 228 public float transformX(int x) { 229 return ((float)x - offsetWx) * scalingX / extentWx; 230 } 231 232 public float transformY(int y) { 233 return (1f - ((float)y - offsetWy) / extentWy) * scalingY; 234 } 235 236 public void setScalingX(float scalingX) { 237 this.scalingX = scalingX; 238 } 239 240 public void setScalingY(float scalingY) { 241 this.scalingY = scalingY; 242 } 243 244 public void setOffsetWx(int offsetWx) { 245 this.offsetWx = offsetWx; 246 } 247 248 public void setOffsetWy(int offsetWy) { 249 this.offsetWy = offsetWy; 250 } 251 252 public void setExtentWx(int extentWx) { 253 this.extentWx = extentWx; 254 } 255 256 public void setExtentWy(int extentWy) { 257 this.extentWy = extentWy; 258 } 259 260 public float transformAngle(float angle) { 261 float ta = scalingY < 0 ? -angle : angle; 262 return (float)(scalingX < 0 ? Math.PI - ta : ta); 263 } 264 265 public void setCurrentPoint(Point p) { 266 currentPoint = p; 267 } 268 269 public Point getCurrentPoint() { 270 return currentPoint; 271 } 272 273 public MetaBrush getCurrentBrush() { 274 return currentBrush; 275 } 276 277 public MetaPen getCurrentPen() { 278 return currentPen; 279 } 280 281 public MetaFont getCurrentFont() { 282 return currentFont; 283 } 284 285 288 public Color getCurrentBackgroundColor() { 289 return currentBackgroundColor; 290 } 291 292 295 public void setCurrentBackgroundColor(Color currentBackgroundColor) { 296 this.currentBackgroundColor = currentBackgroundColor; 297 } 298 299 302 public Color getCurrentTextColor() { 303 return currentTextColor; 304 } 305 306 309 public void setCurrentTextColor(Color currentTextColor) { 310 this.currentTextColor = currentTextColor; 311 } 312 313 316 public int getBackgroundMode() { 317 return backgroundMode; 318 } 319 320 323 public void setBackgroundMode(int backgroundMode) { 324 this.backgroundMode = backgroundMode; 325 } 326 327 330 public int getTextAlign() { 331 return textAlign; 332 } 333 334 337 public void setTextAlign(int textAlign) { 338 this.textAlign = textAlign; 339 } 340 341 344 public int getPolyFillMode() { 345 return polyFillMode; 346 } 347 348 351 public void setPolyFillMode(int polyFillMode) { 352 this.polyFillMode = polyFillMode; 353 } 354 355 public void setLineJoinRectangle(PdfContentByte cb) { 356 if (lineJoin != 0) { 357 lineJoin = 0; 358 cb.setLineJoin(0); 359 } 360 } 361 362 public void setLineJoinPolygon(PdfContentByte cb) { 363 if (lineJoin == 0) { 364 lineJoin = 1; 365 cb.setLineJoin(1); 366 } 367 } 368 369 public boolean getLineNeutral() { 370 return (lineJoin == 0); 371 } 372 373 } 374 | Popular Tags |