PlainText.java (pdfbox-2.0.23-src) | : | PlainText.java (pdfbox-2.0.24-src) | ||
---|---|---|---|---|
skipping to change at line 24 | skipping to change at line 24 | |||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package org.apache.pdfbox.pdmodel.interactive.annotation.layout; | package org.apache.pdfbox.pdmodel.interactive.annotation.layout; | |||
import java.io.IOException; | import java.io.IOException; | |||
import java.text.AttributedString; | import java.text.AttributedString; | |||
import java.text.BreakIterator; | import java.text.BreakIterator; | |||
import java.text.AttributedCharacterIterator.Attribute; | import java.text.AttributedCharacterIterator.Attribute; | |||
import java.util.ArrayList; | import java.util.ArrayList; | |||
import java.util.Arrays; | ||||
import java.util.List; | import java.util.List; | |||
import org.apache.pdfbox.pdmodel.font.PDFont; | import org.apache.pdfbox.pdmodel.font.PDFont; | |||
/** | /** | |||
* A block of text. | * A block of text. | |||
* <p> | * <p> | |||
* A block of text can contain multiple paragraphs which will | * A block of text can contain multiple paragraphs which will | |||
* be treated individually within the block placement. | * be treated individually within the block placement. | |||
* </p> | * </p> | |||
skipping to change at line 54 | skipping to change at line 53 | |||
* Construct the text block from a single value. | * Construct the text block from a single value. | |||
* | * | |||
* Constructs the text block from a single value splitting | * Constructs the text block from a single value splitting | |||
* into individual {@link Paragraph} when a new line character is | * into individual {@link Paragraph} when a new line character is | |||
* encountered. | * encountered. | |||
* | * | |||
* @param textValue the text block string. | * @param textValue the text block string. | |||
*/ | */ | |||
public PlainText(String textValue) | public PlainText(String textValue) | |||
{ | { | |||
List<String> parts = Arrays.asList(textValue.replaceAll("\t", " ").split | String[] parts = textValue.replace('\t', ' ').split("\\r\\n|\\n|\\r|\\u2 | |||
("\\r\\n|\\n|\\r|\\u2028|\\u2029")); | 028|\\u2029"); | |||
paragraphs = new ArrayList<Paragraph>(); | paragraphs = new ArrayList<Paragraph>(parts.length); | |||
for (String part : parts) | for (String part : parts) | |||
{ | { | |||
// Acrobat prints a space for an empty paragraph | // Acrobat prints a space for an empty paragraph | |||
if (part.length() == 0) | if (part.length() == 0) | |||
{ | { | |||
part = " "; | part = " "; | |||
} | } | |||
paragraphs.add(new Paragraph(part)); | paragraphs.add(new Paragraph(part)); | |||
} | } | |||
} | } | |||
/** | /** | |||
* Construct the text block from a list of values. | * Construct the text block from a list of values. | |||
* | * | |||
* Constructs the text block from a list of values treating each | * Constructs the text block from a list of values treating each | |||
* entry as an individual {@link Paragraph}. | * entry as an individual {@link Paragraph}. | |||
* | * | |||
* @param listValue the text block string. | * @param listValue the text block string. | |||
*/ | */ | |||
public PlainText(List<String> listValue) | public PlainText(List<String> listValue) | |||
{ | { | |||
paragraphs = new ArrayList<Paragraph>(); | paragraphs = new ArrayList<Paragraph>(listValue.size()); | |||
for (String part : listValue) | for (String part : listValue) | |||
{ | { | |||
paragraphs.add(new Paragraph(part)); | paragraphs.add(new Paragraph(part)); | |||
} | } | |||
} | } | |||
/** | /** | |||
* Get the list of paragraphs. | * Get the list of paragraphs. | |||
* | * | |||
* @return the paragraphs. | * @return the paragraphs. | |||
skipping to change at line 228 | skipping to change at line 227 | |||
void setWidth(float width) | void setWidth(float width) | |||
{ | { | |||
lineWidth = width; | lineWidth = width; | |||
} | } | |||
float calculateWidth(PDFont font, float fontSize) throws IOException | float calculateWidth(PDFont font, float fontSize) throws IOException | |||
{ | { | |||
final float scale = fontSize/FONTSCALE; | final float scale = fontSize/FONTSCALE; | |||
float calculatedWidth = 0f; | float calculatedWidth = 0f; | |||
int indexOfWord = 0; | ||||
for (Word word : words) | for (Word word : words) | |||
{ | { | |||
calculatedWidth = calculatedWidth + | calculatedWidth = calculatedWidth + | |||
(Float) word.getAttributes().getIterator().getAttribute( TextAttribute.WIDTH); | (Float) word.getAttributes().getIterator().getAttribute( TextAttribute.WIDTH); | |||
String text = word.getText(); | String text = word.getText(); | |||
if (words.indexOf(word) == words.size() -1 && Character.isWhites pace(text.charAt(text.length()-1))) | if (indexOfWord == words.size() -1 && Character.isWhitespace(tex t.charAt(text.length()-1))) | |||
{ | { | |||
float whitespaceWidth = font.getStringWidth(text.substring(t ext.length()-1)) * scale; | float whitespaceWidth = font.getStringWidth(text.substring(t ext.length()-1)) * scale; | |||
calculatedWidth = calculatedWidth - whitespaceWidth; | calculatedWidth = calculatedWidth - whitespaceWidth; | |||
} | } | |||
++indexOfWord; | ||||
} | } | |||
return calculatedWidth; | return calculatedWidth; | |||
} | } | |||
List<Word> getWords() | List<Word> getWords() | |||
{ | { | |||
return words; | return words; | |||
} | } | |||
float getInterWordSpacing(float width) | float getInterWordSpacing(float width) | |||
End of changes. 7 change blocks. | ||||
11 lines changed or deleted | 12 lines changed or added |