129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.link.contentassist.LineBreakingReader;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 import java.io.BufferedReader;
|
|
19 import java.io.IOException;
|
|
20 import java.io.Reader;
|
|
21
|
|
22 import com.ibm.icu.text.BreakIterator;
|
|
23
|
|
24 import dwt.graphics.GC;
|
|
25
|
|
26 /*
|
|
27 * Not a real reader. Could change if requested
|
|
28 */
|
|
29 public class LineBreakingReader {
|
|
30
|
|
31 private BufferedReader fReader;
|
|
32 private GC fGC;
|
|
33 private int fMaxWidth;
|
|
34
|
|
35 private String fLine;
|
|
36 private int fOffset;
|
|
37
|
|
38 private BreakIterator fLineBreakIterator;
|
|
39 private bool fBreakWords;
|
|
40
|
|
41 /**
|
|
42 * Creates a reader that breaks an input text to fit in a given width.
|
|
43 *
|
|
44 * @param reader Reader of the input text
|
|
45 * @param gc The graphic context that defines the currently used font sizes
|
|
46 * @param maxLineWidth The max width (pixels) where the text has to fit in
|
|
47 */
|
|
48 public LineBreakingReader(Reader reader, GC gc, int maxLineWidth) {
|
|
49 fReader= new BufferedReader(reader);
|
|
50 fGC= gc;
|
|
51 fMaxWidth= maxLineWidth;
|
|
52 fOffset= 0;
|
|
53 fLine= null;
|
|
54 fLineBreakIterator= BreakIterator.getLineInstance();
|
|
55 fBreakWords= true;
|
|
56 }
|
|
57
|
|
58 public bool isFormattedLine() {
|
|
59 return fLine !is null;
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * Reads the next line. The lengths of the line will not exceed the given maximum
|
|
64 * width.
|
|
65 *
|
|
66 * @return the next line
|
|
67 * @throws IOException
|
|
68 */
|
|
69 public String readLine() throws IOException {
|
|
70 if (fLine is null) {
|
|
71 String line= fReader.readLine();
|
|
72 if (line is null)
|
|
73 return null;
|
|
74
|
|
75 int lineLen= fGC.textExtent(line).x;
|
|
76 if (lineLen < fMaxWidth) {
|
|
77 return line;
|
|
78 }
|
|
79 fLine= line;
|
|
80 fLineBreakIterator.setText(line);
|
|
81 fOffset= 0;
|
|
82 }
|
|
83 int breakOffset= findNextBreakOffset(fOffset);
|
|
84 String res;
|
|
85 if (breakOffset !is BreakIterator.DONE) {
|
|
86 res= fLine.substring(fOffset, breakOffset);
|
|
87 fOffset= findWordBegin(breakOffset);
|
|
88 if (fOffset is fLine.length()) {
|
|
89 fLine= null;
|
|
90 }
|
|
91 } else {
|
|
92 res= fLine.substring(fOffset);
|
|
93 fLine= null;
|
|
94 }
|
|
95 return res;
|
|
96 }
|
|
97
|
|
98 private int findNextBreakOffset(int currOffset) {
|
|
99 int currWidth= 0;
|
|
100 int nextOffset= fLineBreakIterator.following(currOffset);
|
|
101 while (nextOffset !is BreakIterator.DONE) {
|
|
102 String word= fLine.substring(currOffset, nextOffset);
|
|
103 int wordWidth= fGC.textExtent(word).x;
|
|
104 int nextWidth= wordWidth + currWidth;
|
|
105 if (nextWidth > fMaxWidth) {
|
|
106 if (currWidth > 0)
|
|
107 return currOffset;
|
|
108
|
|
109 if (!fBreakWords)
|
|
110 return nextOffset;
|
|
111
|
|
112 // need to fit into fMaxWidth
|
|
113 int length= word.length();
|
|
114 while (length >= 0) {
|
|
115 length--;
|
|
116 word= word.substring(0, length);
|
|
117 wordWidth= fGC.textExtent(word).x;
|
|
118 if (wordWidth + currWidth < fMaxWidth)
|
|
119 return currOffset + length;
|
|
120 }
|
|
121 return nextOffset;
|
|
122 }
|
|
123 currWidth= nextWidth;
|
|
124 currOffset= nextOffset;
|
|
125 nextOffset= fLineBreakIterator.next();
|
|
126 }
|
|
127 return nextOffset;
|
|
128 }
|
|
129
|
|
130 private int findWordBegin(int idx) {
|
|
131 while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) {
|
|
132 idx++;
|
|
133 }
|
|
134 return idx;
|
|
135 }
|
|
136 }
|