1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: public final class LineBreakMeasurer
49: {
50: private AttributedCharacterIterator text;
51: private int position;
52: private FontRenderContext frc;
53: private TextLayout totalLayout;
54: private int numChars;
55:
56: public LineBreakMeasurer(AttributedCharacterIterator text,
57: BreakIterator breakIter, FontRenderContext frc)
58: {
59: this.text = text;
60: this.frc = frc;
61: position = 0;
62: totalLayout = new TextLayout(text, frc);
63: numChars = totalLayout.getCharacterCount();
64: }
65:
66: public LineBreakMeasurer(AttributedCharacterIterator text,
67: FontRenderContext frc)
68: {
69: this.text = text;
70: this.frc = frc;
71: position = 0;
72: totalLayout = new TextLayout(text, frc);
73: numChars = totalLayout.getCharacterCount();
74: }
75:
76: public void deleteChar(AttributedCharacterIterator newParagraph,
77: int deletePos)
78: {
79: totalLayout = new TextLayout(newParagraph, frc);
80: if( deletePos < 0 || deletePos > totalLayout.getCharacterCount() )
81: throw new NullPointerException("Invalid deletePos:"+deletePos);
82: numChars = totalLayout.getCharacterCount();
83: text = newParagraph;
84: position = 0;
85: }
86:
87: public void insertChar(AttributedCharacterIterator newParagraph,
88: int insertPos)
89: {
90: totalLayout = new TextLayout(newParagraph, frc);
91: if( insertPos < 0 || insertPos > totalLayout.getCharacterCount() )
92: throw new NullPointerException("Invalid insertPos:"+insertPos);
93: numChars = totalLayout.getCharacterCount();
94: text = newParagraph;
95: position = 0;
96: }
97:
98: public TextLayout nextLayout(float wrappingWidth)
99: {
100: return nextLayout( wrappingWidth, numChars, false );
101: }
102:
103: public TextLayout nextLayout(float wrappingWidth, int offsetLimit,
104: boolean requireNextWord)
105: {
106: int next = nextOffset( wrappingWidth, offsetLimit, requireNextWord );
107: AttributedCharacterIterator aci = (new AttributedString( text,
108: position, next )
109: ).getIterator();
110: position = next;
111: return new TextLayout( aci, frc );
112: }
113:
114: public int nextOffset(float wrappingWidth)
115: {
116: return nextOffset( wrappingWidth, numChars, false );
117: }
118:
119: public int nextOffset(float wrappingWidth, int offsetLimit,
120: boolean requireNextWord)
121: {
122: Shape s = totalLayout.getBlackBoxBounds( position, offsetLimit );
123: double remainingLength = s.getBounds2D().getWidth();
124:
125: int guessOffset = (int)( ( (double)wrappingWidth / (double)remainingLength)
126: * ( (double)numChars - (double)position ) );
127: guessOffset += position;
128: if( guessOffset > offsetLimit )
129: guessOffset = offsetLimit;
130:
131: s = totalLayout.getBlackBoxBounds( position, guessOffset );
132: double guessLength = s.getBounds2D().getWidth();
133:
134: boolean makeSmaller = ( guessLength > wrappingWidth );
135: int inc = makeSmaller ? -1 : 1;
136: boolean keepGoing = true;
137:
138: do
139: {
140: guessOffset = guessOffset + inc;
141: if( guessOffset <= position || guessOffset > offsetLimit )
142: {
143: keepGoing = false;
144: }
145: else
146: {
147: s = totalLayout.getBlackBoxBounds( position, guessOffset );
148: guessLength = s.getBounds2D().getWidth();
149: if( makeSmaller && ( guessLength <= wrappingWidth) )
150: keepGoing = false;
151: if( !makeSmaller && ( guessLength >= wrappingWidth) )
152: keepGoing = false;
153: }
154: }
155: while( keepGoing );
156:
157: if( !makeSmaller )
158: guessOffset--;
159:
160: if( guessOffset >= offsetLimit )
161: return offsetLimit;
162:
163: text.setIndex( guessOffset );
164: if( !requireNextWord )
165: {
166: char c = text.previous();
167: while( !Character.isWhitespace( c ) && c != '-' &&
168: guessOffset > position )
169: {
170: guessOffset--;
171: c = text.previous();
172: }
173: }
174: else
175: {
176: char c = text.next();
177: while( !Character.isWhitespace( c ) && c != '-' &&
178: guessOffset < offsetLimit )
179: {
180: guessOffset++;
181: c = text.next();
182: }
183: }
184:
185: return guessOffset;
186: }
187:
188: public void setPosition(int newPosition)
189: {
190: position = newPosition;
191: }
192:
193: public int getPosition()
194: {
195: return position;
196: }
197: }