Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4bfebf2ac8c704f3cf8917123dd825f9c22f4c25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.internal.text.link.contentassist;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import com.ibm.icu.text.BreakIterator;

import org.eclipse.swt.graphics.GC;

/*
 * Not a real reader. Could change if requested
 */
public class LineBreakingReader {

	private BufferedReader fReader;
	private GC fGC;
	private int fMaxWidth;

	private String fLine;
	private int fOffset;

	private BreakIterator fLineBreakIterator;
	private boolean fBreakWords;

	/**
	 * Creates a reader that breaks an input text to fit in a given width.
	 * 
	 * @param reader reader for the input text
	 * @param gc The graphic context that defines the currently used font sizes
	 * @param maxLineWidth The max width (pixels) where the text has to fit in
	 */
	public LineBreakingReader(Reader reader, GC gc, int maxLineWidth) {
		this(new BufferedReader(reader), gc, maxLineWidth);
	}

	/**
	 * Creates a reader that breaks an input text to fit in a given width.
	 * 
	 * @param reader the reader for the input text
	 * @param bufferSize the buffer size
	 * @param gc The graphic context that defines the currently used font sizes
	 * @param maxLineWidth The max width (pixels) where the text has to fit in
	 */
	public LineBreakingReader(Reader reader, int bufferSize, GC gc, int maxLineWidth) {
		this(new BufferedReader(reader, bufferSize), gc, maxLineWidth);
	}

	/**
	 * Creates a reader that breaks an input text to fit in a given width.
	 * 
	 * @param reader the buffered reader for the input text
	 * @param gc The graphic context that defines the currently used font sizes
	 * @param maxLineWidth The max width (pixels) where the text has to fit in
	 */
	private LineBreakingReader(BufferedReader reader, GC gc, int maxLineWidth) {
		fReader= reader;
		fGC= gc;
		fMaxWidth= maxLineWidth;
		fOffset= 0;
		fLine= null;
		fLineBreakIterator= BreakIterator.getLineInstance();
		fBreakWords= true;
	}

	public boolean isFormattedLine() {
		return fLine != null;
	}

	/**
	 * Reads the next line. The lengths of the line will not exceed the given maximum width.
	 *
	 * @return the next line
	 * @throws IOException if an I/O error occurs
	 */
	public String readLine() throws IOException {
		if (fLine == null) {
			String line= fReader.readLine();
			if (line == null)
				return null;

			int lineLen= fGC.textExtent(line).x;
			if (lineLen < fMaxWidth) {
				return line;
			}
			fLine= line;
			fLineBreakIterator.setText(line);
			fOffset= 0;
		}
		int breakOffset= findNextBreakOffset(fOffset);
		String res;
		if (breakOffset != BreakIterator.DONE) {
			res= fLine.substring(fOffset, breakOffset);
			fOffset= findWordBegin(breakOffset);
			if (fOffset == fLine.length()) {
				fLine= null;
			}
		} else {
			res= fLine.substring(fOffset);
			fLine= null;
		}
		return res;
	}

	private int findNextBreakOffset(int currOffset) {
		int currWidth= 0;
		int nextOffset= fLineBreakIterator.following(currOffset);
		while (nextOffset != BreakIterator.DONE) {
			String word= fLine.substring(currOffset, nextOffset);
			int wordWidth= fGC.textExtent(word).x;
			int nextWidth= wordWidth + currWidth;
			if (nextWidth > fMaxWidth) {
				if (currWidth > 0)
					return currOffset;

				if (!fBreakWords)
					return nextOffset;

				// need to fit into fMaxWidth
				int length= word.length();
				while (length > 0) {
					length--;
					word= word.substring(0, length);
					wordWidth= fGC.textExtent(word).x;
					if (wordWidth + currWidth < fMaxWidth)
						return currOffset + length;
				}
				return nextOffset;
			}
			currWidth= nextWidth;
			currOffset= nextOffset;
			nextOffset= fLineBreakIterator.next();
		}
		return nextOffset;
	}

	private int findWordBegin(int idx) {
		while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) {
			idx++;
		}
		return idx;
	}
}

Back to the top