001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /*
018: * @author Oleg V. Khaschansky
019: * @version $Revision$
020: */
021:
022: package java.awt.font;
023:
024: import java.text.AttributedCharacterIterator;
025: import java.text.BreakIterator;
026:
027: import org.apache.harmony.awt.internal.nls.Messages;
028:
029: public final class LineBreakMeasurer {
030: private TextMeasurer tm = null;
031: private BreakIterator bi = null;
032: private int position = 0;
033: int maxpos = 0;
034:
035: public LineBreakMeasurer(AttributedCharacterIterator text,
036: FontRenderContext frc) {
037: this (text, BreakIterator.getLineInstance(), frc);
038: }
039:
040: public LineBreakMeasurer(AttributedCharacterIterator text,
041: BreakIterator bi, FontRenderContext frc) {
042: tm = new TextMeasurer(text, frc);
043: this .bi = bi;
044: this .bi.setText(text);
045: position = text.getBeginIndex();
046: maxpos = tm.aci.getEndIndex();
047: }
048:
049: public void deleteChar(AttributedCharacterIterator newText, int pos) {
050: tm.deleteChar(newText, pos);
051: bi.setText(newText);
052:
053: position = newText.getBeginIndex();
054:
055: maxpos--;
056: }
057:
058: public int getPosition() {
059: return position;
060: }
061:
062: public void insertChar(AttributedCharacterIterator newText, int pos) {
063: tm.insertChar(newText, pos);
064: bi.setText(newText);
065:
066: position = newText.getBeginIndex();
067:
068: maxpos++;
069: }
070:
071: public TextLayout nextLayout(float wrappingWidth, int offsetLimit,
072: boolean requireNextWord) {
073: if (position == maxpos) {
074: return null;
075: }
076:
077: int nextPosition = nextOffset(wrappingWidth, offsetLimit,
078: requireNextWord);
079:
080: if (nextPosition == position) {
081: return null;
082: }
083: TextLayout layout = tm.getLayout(position, nextPosition);
084: position = nextPosition;
085: return layout;
086: }
087:
088: public TextLayout nextLayout(float wrappingWidth) {
089: return nextLayout(wrappingWidth, maxpos, false);
090: }
091:
092: public int nextOffset(float wrappingWidth) {
093: return nextOffset(wrappingWidth, maxpos, false);
094: }
095:
096: public int nextOffset(float wrappingWidth, int offsetLimit,
097: boolean requireNextWord) {
098: if (offsetLimit <= position) {
099: // awt.203=Offset limit should be greater than current position.
100: throw new IllegalArgumentException(Messages
101: .getString("awt.203")); //$NON-NLS-1$
102: }
103:
104: if (position == maxpos) {
105: return position;
106: }
107:
108: int breakPos = tm.getLineBreakIndex(position, wrappingWidth);
109: int correctedPos = breakPos;
110:
111: // This check is required because bi.preceding(maxpos) throws an exception
112: if (breakPos == maxpos) {
113: correctedPos = maxpos;
114: } else if (Character.isWhitespace(bi.getText().setIndex(
115: breakPos))) {
116: correctedPos = bi.following(breakPos);
117: } else {
118: correctedPos = bi.preceding(breakPos);
119: }
120:
121: if (position >= correctedPos) {
122: if (requireNextWord) {
123: correctedPos = position;
124: } else {
125: correctedPos = Math.max(position + 1, breakPos);
126: }
127: }
128:
129: return Math.min(correctedPos, offsetLimit);
130: }
131:
132: public void setPosition(int pos) {
133: if (tm.aci.getBeginIndex() > pos || maxpos < pos) {
134: // awt.33=index is out of range
135: throw new IllegalArgumentException(Messages
136: .getString("awt.33")); //$NON-NLS-1$
137: }
138: position = pos;
139: }
140: }
|