1 50 51 package com.lowagie.text.pdf; 52 53 import java.util.ArrayList ; 54 import java.util.Iterator ; 55 56 import com.lowagie.text.Chunk; 57 import com.lowagie.text.Element; 58 import com.lowagie.text.ListItem; 59 60 64 65 public class PdfLine { 66 67 69 70 protected ArrayList line; 71 72 73 protected float left; 74 75 76 protected float width; 77 78 79 protected int alignment; 80 81 82 protected float height; 83 84 85 protected Chunk listSymbol = null; 86 87 88 protected float symbolIndent; 89 90 91 protected boolean newlineSplit = false; 92 93 94 protected float originalWidth; 95 96 protected boolean isRTL = false; 97 98 100 108 109 PdfLine(float left, float right, int alignment, float height) { 110 this.left = left; 111 this.width = right - left; 112 this.originalWidth = this.width; 113 this.alignment = alignment; 114 this.height = height; 115 this.line = new ArrayList (); 116 } 117 118 PdfLine(float left, float remainingWidth, int alignment, boolean newlineSplit, ArrayList line, boolean isRTL) { 119 this.left = left; 120 this.width = remainingWidth; 121 this.alignment = alignment; 122 this.line = line; 123 this.newlineSplit = newlineSplit; 124 this.isRTL = isRTL; 125 } 126 127 129 137 138 PdfChunk add(PdfChunk chunk) { 139 if (chunk == null || chunk.toString().equals("")) { 141 return null; 142 } 143 144 PdfChunk overflow = chunk.split(width); 146 newlineSplit = (chunk.isNewlineSplit() || overflow == null); 147 150 151 if (chunk.length() > 0) { 153 if (overflow != null) 154 chunk.trimLastSpace(); 155 width -= chunk.width(); 156 addToLine(chunk); 157 } 158 159 else if (line.size() < 1) { 162 chunk = overflow; 163 overflow = chunk.truncate(width); 164 width -= chunk.width(); 165 if (chunk.length() > 0) { 166 addToLine(chunk); 167 return overflow; 168 } 169 else { 171 if (overflow != null) 172 addToLine(overflow); 173 return null; 174 } 175 } 176 else { 177 width += ((PdfChunk)(line.get(line.size() - 1))).trimLastSpace(); 178 } 179 return overflow; 180 } 181 182 private void addToLine(PdfChunk chunk) { 183 if (chunk.changeLeading && chunk.isImage()) { 184 float f = chunk.getImage().getScaledHeight() + chunk.getImageOffsetY(); 185 if (f > height) height = f; 186 } 187 line.add(chunk); 188 } 189 190 192 197 198 public int size() { 199 return line.size(); 200 } 201 202 207 208 public Iterator iterator() { 209 return line.iterator(); 210 } 211 212 217 218 float height() { 219 return height; 220 } 221 222 227 228 float indentLeft() { 229 if (isRTL) { 230 switch (alignment) { 231 case Element.ALIGN_LEFT: 232 return left + width; 233 case Element.ALIGN_CENTER: 234 return left + (width / 2f); 235 default: 236 return left; 237 } 238 } 239 else { 240 switch (alignment) { 241 case Element.ALIGN_RIGHT: 242 return left + width; 243 case Element.ALIGN_CENTER: 244 return left + (width / 2f); 245 default: 246 return left; 247 } 248 } 249 } 250 251 256 257 public boolean hasToBeJustified() { 258 return ((alignment == Element.ALIGN_JUSTIFIED || alignment == Element.ALIGN_JUSTIFIED_ALL) && width != 0); 259 } 260 261 267 268 public void resetAlignment() { 269 if (alignment == Element.ALIGN_JUSTIFIED) { 270 alignment = Element.ALIGN_LEFT; 271 } 272 } 273 274 275 void setExtraIndent(float extra) { 276 left += extra; 277 width -= extra; 278 } 279 280 285 286 float widthLeft() { 287 return width; 288 } 289 290 295 296 int numberOfSpaces() { 297 String string = toString(); 298 int length = string.length(); 299 int numberOfSpaces = 0; 300 for (int i = 0; i < length; i++) { 301 if (string.charAt(i) == ' ') { 302 numberOfSpaces++; 303 } 304 } 305 return numberOfSpaces; 306 } 307 308 315 316 public void setListItem(ListItem listItem) { 317 this.listSymbol = listItem.getListSymbol(); 318 this.symbolIndent = listItem.getIndentationLeft(); 319 } 320 321 326 327 public Chunk listSymbol() { 328 return listSymbol; 329 } 330 331 336 337 public float listIndent() { 338 return symbolIndent; 339 } 340 341 346 347 public String toString() { 348 StringBuffer tmp = new StringBuffer (); 349 for (Iterator i = line.iterator(); i.hasNext(); ) { 350 tmp.append(((PdfChunk) i.next()).toString()); 351 } 352 return tmp.toString(); 353 } 354 355 359 public boolean isNewlineSplit() { 360 return newlineSplit && (alignment != Element.ALIGN_JUSTIFIED_ALL); 361 } 362 363 367 public int getLastStrokeChunk() { 368 int lastIdx = line.size() - 1; 369 for (; lastIdx >= 0; --lastIdx) { 370 PdfChunk chunk = (PdfChunk)line.get(lastIdx); 371 if (chunk.isStroked()) 372 break; 373 } 374 return lastIdx; 375 } 376 377 382 public PdfChunk getChunk(int idx) { 383 if (idx < 0 || idx >= line.size()) 384 return null; 385 return (PdfChunk)line.get(idx); 386 } 387 388 392 public float getOriginalWidth() { 393 return originalWidth; 394 } 395 396 401 float getMaxSizeSimple() { 402 float maxSize = 0; 403 for (int k = 0; k < line.size(); ++k) { 404 PdfChunk chunk = (PdfChunk)line.get(k); 405 if (!chunk.isImage()) { 406 maxSize = Math.max(chunk.font().size(), maxSize); 407 } 408 else { 409 maxSize = Math.max(chunk.getImage().getScaledHeight() + chunk.getImageOffsetY() , maxSize); 410 } 411 } 412 return maxSize; 413 } 414 415 boolean isRTL() { 416 return isRTL; 417 } 418 419 425 public float getWidthCorrected(float charSpacing, float wordSpacing) { 426 float total = 0; 427 for (int k = 0; k < line.size(); ++k) { 428 PdfChunk ck = (PdfChunk)line.get(k); 429 total += ck.getWidthCorrected(charSpacing, wordSpacing); 430 } 431 return total; 432 } 433 434 439 public float getAscender() { 440 float ascender = 0; 441 for (int k = 0; k < line.size(); ++k) { 442 PdfChunk ck = (PdfChunk)line.get(k); 443 if (ck.isImage()) 444 ascender = Math.max(ascender, ck.getImage().getScaledHeight() + ck.getImageOffsetY()); 445 else { 446 PdfFont font = ck.font(); 447 ascender = Math.max(ascender, font.getFont().getFontDescriptor(BaseFont.ASCENT, font.size())); 448 } 449 } 450 return ascender; 451 } 452 453 458 public float getDescender() { 459 float descender = 0; 460 for (int k = 0; k < line.size(); ++k) { 461 PdfChunk ck = (PdfChunk)line.get(k); 462 if (ck.isImage()) 463 descender = Math.min(descender, ck.getImageOffsetY()); 464 else { 465 PdfFont font = ck.font(); 466 descender = Math.min(descender, font.getFont().getFontDescriptor(BaseFont.DESCENT, font.size())); 467 } 468 } 469 return descender; 470 } 471 } | Popular Tags |