blob: d1cb5710c804a5594ddf25c5c9424743da43520a (
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
|
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
* Jens Lukowski/Innoopract - initial renaming/restructuring
*
*/
package org.eclipse.wst.sse.ui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import org.eclipse.swt.graphics.GC;
/*
* Not a real reader. Could change if requested
*/
public class StructuredTextLineBreakingReader {
private BufferedReader fReader;
private GC fGC;
private int fMaxWidth;
private String fLine;
private int fIndex;
/**
* Creates a reader that breaks an input text to fit in a given width.
* @param reader Reader of the input text
* @param gc The graphic context that defines the currently used font sizes
* @param maxLineWidth The max width (pixes) where the text has to fit in
*/
public StructuredTextLineBreakingReader(Reader reader, GC gc, int maxLineWidth) {
fReader = new BufferedReader(reader);
fGC = gc;
fMaxWidth = maxLineWidth;
fLine = null;
fIndex = 0;
}
private int findNextBreakIndex(int currIndex) {
int currWidth = 0;
int lineLength = fLine.length();
while (currIndex < lineLength) {
char ch = fLine.charAt(currIndex);
int nextIndex = currIndex + 1;
// leading whitespaces are counted to the following word
if (Character.isWhitespace(ch)) {
while (nextIndex < lineLength && Character.isWhitespace(fLine.charAt(nextIndex))) {
nextIndex++;
}
}
while (nextIndex < lineLength && !Character.isWhitespace(fLine.charAt(nextIndex))) {
nextIndex++;
}
String word = fLine.substring(currIndex, nextIndex);
int wordWidth = fGC.textExtent(word).x;
int nextWidth = wordWidth + currWidth;
if (nextWidth > fMaxWidth && wordWidth < fMaxWidth) {
return currIndex;
}
currWidth = nextWidth;
currIndex = nextIndex;
}
return currIndex;
}
private int findWordBegin(int idx) {
while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) {
idx++;
}
return idx;
}
/**
* Reads the next line. The lengths of the line will not exceed the gived maximum
* width.
*/
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;
fIndex = 0;
}
int breakIdx = findNextBreakIndex(fIndex);
String res = fLine.substring(fIndex, breakIdx);
if (breakIdx < fLine.length()) {
fIndex = findWordBegin(breakIdx);
}
else {
fLine = null;
}
return res;
}
}
|