1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.Reader;
12 import java.text.BreakIterator;
13 import org.eclipse.swt.graphics.GC;
16 * Not a real reader. Could change if requested
18 public class LineBreakingReader {
20 private BufferedReader fReader;
22 private int fMaxWidth;
27 private BreakIterator fLineBreakIterator;
30 * Creates a reader that breaks an input text to fit in a given width.
31 * @param reader Reader of the input text
32 * @param gc The graphic context that defines the currently used font sizes
33 * @param maxLineWidth The max width (pixes) where the text has to fit in
35 public LineBreakingReader(Reader reader, GC gc, int maxLineWidth) {
36 fReader= new BufferedReader(reader);
38 fMaxWidth= maxLineWidth;
41 fLineBreakIterator= BreakIterator.getLineInstance();
44 public boolean isFormattedLine() {
49 * Reads the next line. The lengths of the line will not exceed the gived maximum
52 public String readLine() throws IOException {
54 String line= fReader.readLine();
58 int lineLen= fGC.textExtent(line).x;
59 if (lineLen < fMaxWidth) {
63 fLineBreakIterator.setText(line);
66 int breakOffset= findNextBreakOffset(fOffset);
68 if (breakOffset != BreakIterator.DONE) {
69 res= fLine.substring(fOffset, breakOffset);
70 fOffset= findWordBegin(breakOffset);
71 if (fOffset == fLine.length()) {
75 res= fLine.substring(fOffset);
81 private int findNextBreakOffset(int currOffset) {
83 int nextOffset= fLineBreakIterator.following(currOffset);
84 while (nextOffset != BreakIterator.DONE) {
85 String word= fLine.substring(currOffset, nextOffset);
86 int wordWidth= fGC.textExtent(word).x;
87 int nextWidth= wordWidth + currWidth;
88 if (nextWidth > fMaxWidth) {
96 currOffset= nextOffset;
97 nextOffset= fLineBreakIterator.next();
102 private int findWordBegin(int idx) {
103 while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) {