2
* $Id: PdfLine.java,v 1.66 2006/01/18 20:35:48 psoares33 Exp $
5
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* (the "License"); you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the License.
15
* The Original Code is 'iText, a free JAVA-PDF library'.
17
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19
* All Rights Reserved.
20
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
23
* Contributor(s): all the names of the contributors are added in the source code
26
* Alternatively, the contents of this file may be used under the terms of the
27
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28
* provisions of LGPL are applicable instead of those above. If you wish to
29
* allow use of your version of this file only under the terms of the LGPL
30
* License and not to allow others to use your version of this file under
31
* the MPL, indicate your decision by deleting the provisions above and
32
* replace them with the notice and other provisions required by the LGPL.
33
* If you do not delete the provisions above, a recipient may use your version
34
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36
* This library is free software; you can redistribute it and/or modify it
37
* under the terms of the MPL as stated above or under the terms of the GNU
38
* Library General Public License as published by the Free Software Foundation;
39
* either version 2 of the License, or any later version.
41
* This library is distributed in the hope that it will be useful, but WITHOUT
42
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46
* If you didn't download this code from the following link, you should check if
47
* you aren't using an obsolete version:
48
* http://www.lowagie.com/iText/
51
package com.lowagie.text.pdf;
53
import java.util.ArrayList;
54
import java.util.Iterator;
56
import com.lowagie.text.Element;
57
import com.lowagie.text.ListItem;
58
import com.lowagie.text.Chunk;
61
* <CODE>PdfLine</CODE> defines an array with <CODE>PdfChunk</CODE>-objects
62
* that fit into 1 line.
65
public class PdfLine {
69
/** The arraylist containing the chunks. */
70
protected ArrayList line;
72
/** The left indentation of the line. */
75
/** The width of the line. */
76
protected float width;
78
/** The alignment of the line. */
79
protected int alignment;
81
/** The heigth of the line. */
82
protected float height;
84
/** The listsymbol (if necessary). */
85
protected Chunk listSymbol = null;
87
/** The listsymbol (if necessary). */
88
protected float symbolIndent;
90
/** <CODE>true</CODE> if the chunk splitting was caused by a newline. */
91
protected boolean newlineSplit = false;
93
/** The original width. */
94
protected float originalWidth;
96
protected boolean isRTL = false;
101
* Constructs a new <CODE>PdfLine</CODE>-object.
103
* @param left the limit of the line at the left
104
* @param right the limit of the line at the right
105
* @param alignment the alignment of the line
106
* @param height the height of the line
109
PdfLine(float left, float right, int alignment, float height) {
111
this.width = right - left;
112
this.originalWidth = this.width;
113
this.alignment = alignment;
114
this.height = height;
115
this.line = new ArrayList();
118
PdfLine(float left, float remainingWidth, int alignment, boolean newlineSplit, ArrayList line, boolean isRTL) {
120
this.width = remainingWidth;
121
this.alignment = alignment;
123
this.newlineSplit = newlineSplit;
130
* Adds a <CODE>PdfChunk</CODE> to the <CODE>PdfLine</CODE>.
132
* @param chunk the <CODE>PdfChunk</CODE> to add
133
* @return <CODE>null</CODE> if the chunk could be added completely; if not
134
* a <CODE>PdfChunk</CODE> containing the part of the chunk that could
135
* not be added is returned
138
PdfChunk add(PdfChunk chunk) {
139
// nothing happens if the chunk is null.
140
if (chunk == null || chunk.toString().equals("")) {
144
// we split the chunk to be added
145
PdfChunk overflow = chunk.split(width);
146
newlineSplit = (chunk.isNewlineSplit() || overflow == null);
147
// if (chunk.isNewlineSplit() && alignment == Element.ALIGN_JUSTIFIED)
148
// alignment = Element.ALIGN_LEFT;
151
// if the length of the chunk > 0 we add it to the line
152
if (chunk.length() > 0) {
153
if (overflow != null)
154
chunk.trimLastSpace();
155
width -= chunk.width();
159
// if the length == 0 and there were no other chunks added to the line yet,
160
// we risk to end up in an endless loop trying endlessly to add the same chunk
161
else if (line.size() < 1) {
163
overflow = chunk.truncate(width);
164
width -= chunk.width();
165
if (chunk.length() > 0) {
169
// if the chunck couldn't even be truncated, we add everything, so be it
171
if (overflow != null)
177
width += ((PdfChunk)(line.get(line.size() - 1))).trimLastSpace();
182
// methods to retrieve information
185
* Returns the number of chunks in the line.
195
* Returns an iterator of <CODE>PdfChunk</CODE>s.
197
* @return an <CODE>Iterator</CODE>
200
public Iterator iterator() {
201
return line.iterator();
205
* Returns the height of the line.
215
* Returns the left indentation of the line taking the alignment of the line into account.
223
case Element.ALIGN_LEFT:
225
case Element.ALIGN_CENTER:
226
return left + (width / 2f);
233
case Element.ALIGN_RIGHT:
235
case Element.ALIGN_CENTER:
236
return left + (width / 2f);
244
* Checks if this line has to be justified.
246
* @return <CODE>true</CODE> if the alignment equals <VAR>ALIGN_JUSTIFIED</VAR> and there is some width left.
249
public boolean hasToBeJustified() {
250
return ((alignment == Element.ALIGN_JUSTIFIED || alignment == Element.ALIGN_JUSTIFIED_ALL) && width != 0);
254
* Resets the alignment of this line.
256
* The alignment of the last line of for instance a <CODE>Paragraph</CODE>
257
* that has to be justified, has to be reset to <VAR>ALIGN_LEFT</VAR>.
260
public void resetAlignment() {
261
if (alignment == Element.ALIGN_JUSTIFIED) {
262
alignment = Element.ALIGN_LEFT;
267
* Returns the width that is left, after a maximum of characters is added to the line.
277
* Returns the number of space-characters in this line.
282
int numberOfSpaces() {
283
String string = toString();
284
int length = string.length();
285
int numberOfSpaces = 0;
286
for (int i = 0; i < length; i++) {
287
if (string.charAt(i) == ' ') {
291
return numberOfSpaces;
295
* Sets the listsymbol of this line.
297
* This is only necessary for the first line of a <CODE>ListItem</CODE>.
299
* @param listItem the list symbol
302
public void setListItem(ListItem listItem) {
303
this.listSymbol = listItem.listSymbol();
304
this.symbolIndent = listItem.indentationLeft();
308
* Returns the listsymbol of this line.
310
* @return a <CODE>PdfChunk</CODE> if the line has a listsymbol; <CODE>null</CODE> otherwise
313
public Chunk listSymbol() {
318
* Return the indentation needed to show the listsymbol.
323
public float listIndent() {
328
* Get the string representation of what is in this line.
330
* @return a <CODE>String</CODE>
333
public String toString() {
334
StringBuffer tmp = new StringBuffer();
335
for (Iterator i = line.iterator(); i.hasNext(); ) {
336
tmp.append(((PdfChunk) i.next()).toString());
338
return tmp.toString();
342
* Checks if a newline caused the line split.
343
* @return <CODE>true</CODE> if a newline caused the line split
345
public boolean isNewlineSplit() {
346
return newlineSplit && (alignment != Element.ALIGN_JUSTIFIED_ALL);
350
* Gets the index of the last <CODE>PdfChunk</CODE> with metric attributes
351
* @return the last <CODE>PdfChunk</CODE> with metric attributes
353
public int getLastStrokeChunk() {
354
int lastIdx = line.size() - 1;
355
for (; lastIdx >= 0; --lastIdx) {
356
PdfChunk chunk = (PdfChunk)line.get(lastIdx);
357
if (chunk.isStroked())
364
* Gets a <CODE>PdfChunk</CODE> by index.
365
* @param idx the index
366
* @return the <CODE>PdfChunk</CODE> or null if beyond the array
368
public PdfChunk getChunk(int idx) {
369
if (idx < 0 || idx >= line.size())
371
return (PdfChunk)line.get(idx);
375
* Gets the original width of the line.
376
* @return the original width of the line
378
public float getOriginalWidth() {
379
return originalWidth;
383
* Gets the maximum size of all the fonts used in this line
384
* including images (if there are images in the line and if
385
* the leading has to be changed).
386
* @return maximum size of all the fonts used in this line
390
for (int k = 0; k < line.size(); ++k) {
391
PdfChunk chunk = (PdfChunk)line.get(k);
392
if (!chunk.isImage() || !chunk.changeLeading()) {
393
maxSize = Math.max(chunk.font().size(), maxSize);
396
maxSize = Math.max(chunk.getImage().scaledHeight() + chunk.getImageOffsetY() , maxSize);
403
* Gets the maximum size of all the fonts used in this line
405
* @return maximum size of all the fonts used in this line
407
float getMaxSizeSimple() {
409
for (int k = 0; k < line.size(); ++k) {
410
PdfChunk chunk = (PdfChunk)line.get(k);
411
if (!chunk.isImage()) {
412
maxSize = Math.max(chunk.font().size(), maxSize);
415
maxSize = Math.max(chunk.getImage().scaledHeight() + chunk.getImageOffsetY() , maxSize);
426
* Gets a width corrected with a charSpacing and wordSpacing.
429
* @return a corrected width
431
public float getWidthCorrected(float charSpacing, float wordSpacing) {
433
for (int k = 0; k < line.size(); ++k) {
434
PdfChunk ck = (PdfChunk)line.get(k);
435
total += ck.getWidthCorrected(charSpacing, wordSpacing);
441
* Gets the maximum size of the ascender for all the fonts used
443
* @return maximum size of all the ascenders used in this line
445
public float getAscender() {
447
for (int k = 0; k < line.size(); ++k) {
448
PdfChunk ck = (PdfChunk)line.get(k);
450
ascender = Math.max(ascender, ck.getImage().scaledHeight() + ck.getImageOffsetY());
452
PdfFont font = ck.font();
453
ascender = Math.max(ascender, font.getFont().getFontDescriptor(BaseFont.ASCENT, font.size()));
460
* Gets the biggest descender for all the fonts used
461
* in this line. Note that this is a negative number.
462
* @return maximum size of all the ascenders used in this line
464
public float getDescender() {
466
for (int k = 0; k < line.size(); ++k) {
467
PdfChunk ck = (PdfChunk)line.get(k);
469
descender = Math.min(descender, ck.getImageOffsetY());
471
PdfFont font = ck.font();
472
descender = Math.min(descender, font.getFont().getFontDescriptor(BaseFont.DESCENT, font.size()));
b'\\ No newline at end of file'