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;
11 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;
24 private int fMaxWidth;
30 private BreakIterator fLineBreakIterator;
33 * Creates a reader that breaks an input text to fit in a given width.
36 * Reader of the input text
38 * The graphic context that defines the currently used font sizes
40 * The max width (pixes) where the text has to fit in
42 public LineBreakingReader(Reader reader, GC gc, int maxLineWidth) {
43 fReader = new BufferedReader(reader);
45 fMaxWidth = maxLineWidth;
48 fLineBreakIterator = BreakIterator.getLineInstance();
51 public boolean isFormattedLine() {
56 * Reads the next line. The lengths of the line will not exceed the gived
59 public String readLine() throws IOException {
61 String line = fReader.readLine();
65 int lineLen = fGC.textExtent(line).x;
66 if (lineLen < fMaxWidth) {
70 fLineBreakIterator.setText(line);
73 int breakOffset = findNextBreakOffset(fOffset);
75 if (breakOffset != BreakIterator.DONE) {
76 res = fLine.substring(fOffset, breakOffset);
77 fOffset = findWordBegin(breakOffset);
78 if (fOffset == fLine.length()) {
82 res = fLine.substring(fOffset);
88 private int findNextBreakOffset(int currOffset) {
90 int nextOffset = fLineBreakIterator.following(currOffset);
91 while (nextOffset != BreakIterator.DONE) {
92 String word = fLine.substring(currOffset, nextOffset);
93 int wordWidth = fGC.textExtent(word).x;
94 int nextWidth = wordWidth + currWidth;
95 if (nextWidth > fMaxWidth) {
102 currWidth = nextWidth;
103 currOffset = nextOffset;
104 nextOffset = fLineBreakIterator.next();
109 private int findWordBegin(int idx) {
110 while (idx < fLine.length()
111 && Character.isWhitespace(fLine.charAt(idx))) {