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:
026: import org.apache.harmony.awt.gl.font.TextMetricsCalculator;
027: import org.apache.harmony.awt.gl.font.TextRunBreaker;
028:
029: public final class TextMeasurer implements Cloneable {
030: AttributedCharacterIterator aci;
031: FontRenderContext frc;
032: TextRunBreaker breaker = null;
033: TextMetricsCalculator tmc = null;
034:
035: public TextMeasurer(AttributedCharacterIterator text,
036: FontRenderContext frc) {
037: this .aci = text;
038: this .frc = frc;
039: breaker = new TextRunBreaker(aci, this .frc);
040: tmc = new TextMetricsCalculator(breaker);
041: }
042:
043: public void insertChar(AttributedCharacterIterator newParagraph,
044: int insertPos) {
045: AttributedCharacterIterator oldAci = aci;
046: aci = newParagraph;
047: if ((oldAci.getEndIndex() - oldAci.getBeginIndex())
048: - (aci.getEndIndex() - aci.getBeginIndex()) != -1) {
049: breaker = new TextRunBreaker(aci, this .frc);
050: tmc = new TextMetricsCalculator(breaker);
051: } else {
052: breaker.insertChar(newParagraph, insertPos);
053: }
054: }
055:
056: public void deleteChar(AttributedCharacterIterator newParagraph,
057: int deletePos) {
058: AttributedCharacterIterator oldAci = aci;
059: aci = newParagraph;
060: if ((oldAci.getEndIndex() - oldAci.getBeginIndex())
061: - (aci.getEndIndex() - aci.getBeginIndex()) != 1) {
062: breaker = new TextRunBreaker(aci, this .frc);
063: tmc = new TextMetricsCalculator(breaker);
064: } else {
065: breaker.deleteChar(newParagraph, deletePos);
066: }
067: }
068:
069: @Override
070: protected Object clone() {
071: return new TextMeasurer((AttributedCharacterIterator) aci
072: .clone(), frc);
073: }
074:
075: public TextLayout getLayout(int start, int limit) {
076: breaker.pushSegments(start - aci.getBeginIndex(), limit
077: - aci.getBeginIndex());
078:
079: breaker.createAllSegments();
080: TextLayout layout = new TextLayout((TextRunBreaker) breaker
081: .clone());
082:
083: breaker.popSegments();
084: return layout;
085: }
086:
087: public float getAdvanceBetween(int start, int end) {
088: breaker.pushSegments(start - aci.getBeginIndex(), end
089: - aci.getBeginIndex());
090:
091: breaker.createAllSegments();
092: float retval = tmc.createMetrics().getAdvance();
093:
094: breaker.popSegments();
095: return retval;
096: }
097:
098: public int getLineBreakIndex(int start, float maxAdvance) {
099: breaker.createAllSegments();
100: return breaker.getLineBreakIndex(start - aci.getBeginIndex(),
101: maxAdvance)
102: + aci.getBeginIndex();
103: }
104: }
|