1 48 package com.lowagie.text.pdf; 49 import java.awt.Color ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 53 import com.lowagie.text.Chunk; 54 import com.lowagie.text.Element; 55 import com.lowagie.text.Phrase; 56 57 62 public class VerticalText { 63 64 65 public static final int NO_MORE_TEXT = 1; 66 67 68 public static final int NO_MORE_COLUMN = 2; 69 70 71 protected ArrayList chunks = new ArrayList (); 72 73 74 protected PdfContentByte text; 75 76 77 protected int alignment = Element.ALIGN_LEFT; 78 79 80 protected int currentChunkMarker = -1; 81 82 83 protected PdfChunk currentStandbyChunk; 84 85 86 protected String splittedChunkText; 87 88 90 protected float leading; 91 92 94 protected float startX; 95 96 98 protected float startY; 99 100 102 protected int maxLines; 103 104 106 protected float height; 107 108 112 public VerticalText(PdfContentByte text) { 113 this.text = text; 114 } 115 116 120 public void addText(Phrase phrase) { 121 for (Iterator j = phrase.getChunks().iterator(); j.hasNext();) { 122 chunks.add(new PdfChunk((Chunk)j.next(), null)); 123 } 124 } 125 126 130 public void addText(Chunk chunk) { 131 chunks.add(new PdfChunk(chunk, null)); 132 } 133 134 141 public void setVerticalLayout(float startX, float startY, float height, int maxLines, float leading) { 142 this.startX = startX; 143 this.startY = startY; 144 this.height = height; 145 this.maxLines = maxLines; 146 setLeading(leading); 147 } 148 149 152 public void setLeading(float leading) { 153 this.leading = leading; 154 } 155 156 159 public float getLeading() { 160 return leading; 161 } 162 163 168 protected PdfLine createLine(float width) { 169 if (chunks.isEmpty()) 170 return null; 171 splittedChunkText = null; 172 currentStandbyChunk = null; 173 PdfLine line = new PdfLine(0, width, alignment, 0); 174 String total; 175 for (currentChunkMarker = 0; currentChunkMarker < chunks.size(); ++currentChunkMarker) { 176 PdfChunk original = (PdfChunk)(chunks.get(currentChunkMarker)); 177 total = original.toString(); 178 currentStandbyChunk = line.add(original); 179 if (currentStandbyChunk != null) { 180 splittedChunkText = original.toString(); 181 original.setValue(total); 182 return line; 183 } 184 } 185 return line; 186 } 187 188 191 protected void shortenChunkArray() { 192 if (currentChunkMarker < 0) 193 return; 194 if (currentChunkMarker >= chunks.size()) { 195 chunks.clear(); 196 return; 197 } 198 PdfChunk split = (PdfChunk)(chunks.get(currentChunkMarker)); 199 split.setValue(splittedChunkText); 200 chunks.set(currentChunkMarker, currentStandbyChunk); 201 for (int j = currentChunkMarker - 1; j >= 0; --j) 202 chunks.remove(j); 203 } 204 205 210 public int go() { 211 return go(false); 212 } 213 214 220 public int go(boolean simulate) { 221 boolean dirty = false; 222 PdfContentByte graphics = null; 223 if (text != null) { 224 graphics = text.getDuplicate(); 225 } 226 else if (!simulate) 227 throw new NullPointerException ("VerticalText.go with simulate==false and text==null."); 228 int status = 0; 229 for (;;) { 230 if (maxLines <= 0) { 231 status = NO_MORE_COLUMN; 232 if (chunks.isEmpty()) 233 status |= NO_MORE_TEXT; 234 break; 235 } 236 if (chunks.isEmpty()) { 237 status = NO_MORE_TEXT; 238 break; 239 } 240 PdfLine line = createLine(height); 241 if (!simulate && !dirty) { 242 text.beginText(); 243 dirty = true; 244 } 245 shortenChunkArray(); 246 if (!simulate) { 247 text.setTextMatrix(startX, startY - line.indentLeft()); 248 writeLine(line, text, graphics); 249 } 250 --maxLines; 251 startX -= leading; 252 } 253 if (dirty) { 254 text.endText(); 255 text.add(graphics); 256 } 257 return status; 258 } 259 260 void writeLine(PdfLine line, PdfContentByte text, PdfContentByte graphics) { 261 PdfFont currentFont = null; 262 PdfChunk chunk; 263 for (Iterator j = line.iterator(); j.hasNext(); ) { 264 chunk = (PdfChunk) j.next(); 265 266 if (chunk.font().compareTo(currentFont) != 0) { 267 currentFont = chunk.font(); 268 text.setFontAndSize(currentFont.getFont(), currentFont.size()); 269 } 270 Color color = chunk.color(); 271 if (color != null) 272 text.setColorFill(color); 273 text.showText(chunk.toString()); 274 if (color != null) 275 text.resetRGBColorFill(); 276 } 277 } 278 279 283 public void setOrigin(float startX, float startY) { 284 this.startX = startX; 285 this.startY = startY; 286 } 287 288 292 public float getOriginX() { 293 return startX; 294 } 295 296 299 public float getOriginY() { 300 return startY; 301 } 302 303 307 public int getMaxLines() { 308 return maxLines; 309 } 310 311 314 public void setMaxLines(int maxLines) { 315 this.maxLines = maxLines; 316 } 317 318 321 public float getHeight() { 322 return height; 323 } 324 325 328 public void setHeight(float height) { 329 this.height = height; 330 } 331 332 336 public void setAlignment(int alignment) { 337 this.alignment = alignment; 338 } 339 340 344 public int getAlignment() { 345 return alignment; 346 } 347 } 348 | Popular Tags |