comparison dwtx/jface/internal/text/link/contentassist/LineBreakingReader.d @ 158:25f1f92fa3df

...
author Frank Benoit <benoit@tionex.de>
date Tue, 26 Aug 2008 02:46:34 +0200
parents 6dcb0baaa031
children 1a5b8f8129df
comparison
equal deleted inserted replaced
157:7f75eaa8103a 158:25f1f92fa3df
22 import dwtx.jface.internal.text.link.contentassist.ContentAssistant2; // packageimport 22 import dwtx.jface.internal.text.link.contentassist.ContentAssistant2; // packageimport
23 import dwtx.jface.internal.text.link.contentassist.AdditionalInfoController2; // packageimport 23 import dwtx.jface.internal.text.link.contentassist.AdditionalInfoController2; // packageimport
24 24
25 25
26 import dwt.dwthelper.utils; 26 import dwt.dwthelper.utils;
27 27 import dwtx.dwtxhelper.BufferedReader;
28 28 import dwtx.dwtxhelper.mangoicu.UBreakIterator;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.Reader;
32
33 import com.ibm.icu.text.BreakIterator;
34 29
35 import dwt.graphics.GC; 30 import dwt.graphics.GC;
36 31
37 /* 32 /*
38 * Not a real reader. Could change if requested 33 * Not a real reader. Could change if requested
44 private int fMaxWidth; 39 private int fMaxWidth;
45 40
46 private String fLine; 41 private String fLine;
47 private int fOffset; 42 private int fOffset;
48 43
49 private BreakIterator fLineBreakIterator; 44 private UBreakIterator fLineBreakIterator;
50 private bool fBreakWords; 45 private bool fBreakWords;
51 46
52 /** 47 /**
53 * Creates a reader that breaks an input text to fit in a given width. 48 * Creates a reader that breaks an input text to fit in a given width.
54 * 49 *
55 * @param reader Reader of the input text 50 * @param reader Reader of the input text
56 * @param gc The graphic context that defines the currently used font sizes 51 * @param gc The graphic context that defines the currently used font sizes
57 * @param maxLineWidth The max width (pixels) where the text has to fit in 52 * @param maxLineWidth The max width (pixels) where the text has to fit in
58 */ 53 */
59 public this(Reader reader, GC gc, int maxLineWidth) { 54 public this(Reader reader, GC gc, int maxLineWidth) {
60 fReader= new BufferedReader(reader); 55 fReader= new BufferedReader(reader);
61 fGC= gc; 56 fGC= gc;
62 fMaxWidth= maxLineWidth; 57 fMaxWidth= maxLineWidth;
63 fOffset= 0; 58 fOffset= 0;
64 fLine= null; 59 fLine= null;
65 fLineBreakIterator= BreakIterator.getLineInstance(); 60 fLineBreakIterator= UBreakIterator.getLineInstance();
66 fBreakWords= true; 61 fBreakWords= true;
67 } 62 }
68 63
69 public bool isFormattedLine() { 64 public bool isFormattedLine() {
70 return fLine !is null; 65 return fLine !is null;
71 } 66 }
72 67
73 /** 68 /**
74 * Reads the next line. The lengths of the line will not exceed the given maximum 69 * Reads the next line. The lengths of the line will not exceed the given maximum
75 * width. 70 * width.
76 * 71 *
77 * @return the next line 72 * @return the next line
78 * @throws IOException 73 * @throws IOException
79 */ 74 */
80 public String readLine() { 75 public String readLine() {
81 if (fLine is null) { 76 if (fLine is null) {
82 String line= fReader.readLine(); 77 String line= fReader.readLine();
83 if (line is null) 78 if (line is null)
91 fLineBreakIterator.setText(line); 86 fLineBreakIterator.setText(line);
92 fOffset= 0; 87 fOffset= 0;
93 } 88 }
94 int breakOffset= findNextBreakOffset(fOffset); 89 int breakOffset= findNextBreakOffset(fOffset);
95 String res; 90 String res;
96 if (breakOffset !is BreakIterator.DONE) { 91 if (breakOffset !is UBreakIterator.DONE) {
97 res= fLine.substring(fOffset, breakOffset); 92 res= fLine.substring(fOffset, breakOffset);
98 fOffset= findWordBegin(breakOffset); 93 fOffset= findWordBegin(breakOffset);
99 if (fOffset is fLine.length()) { 94 if (fOffset is fLine.length()) {
100 fLine= null; 95 fLine= null;
101 } 96 }
107 } 102 }
108 103
109 private int findNextBreakOffset(int currOffset) { 104 private int findNextBreakOffset(int currOffset) {
110 int currWidth= 0; 105 int currWidth= 0;
111 int nextOffset= fLineBreakIterator.following(currOffset); 106 int nextOffset= fLineBreakIterator.following(currOffset);
112 while (nextOffset !is BreakIterator.DONE) { 107 while (nextOffset !is UBreakIterator.DONE) {
113 String word= fLine.substring(currOffset, nextOffset); 108 String word= fLine.substring(currOffset, nextOffset);
114 int wordWidth= fGC.textExtent(word).x; 109 int wordWidth= fGC.textExtent(word).x;
115 int nextWidth= wordWidth + currWidth; 110 int nextWidth= wordWidth + currWidth;
116 if (nextWidth > fMaxWidth) { 111 if (nextWidth > fMaxWidth) {
117 if (currWidth > 0) 112 if (currWidth > 0)