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 junit.framework.Test;
025: import junit.framework.TestSuite;
026: import junit.framework.TestCase;
027:
028: import java.awt.*;
029: import java.awt.image.BufferedImage;
030: import java.text.AttributedString;
031: import java.text.AttributedCharacterIterator;
032:
033: public class TextMeasurerTest extends TestCase {
034: TextMeasurer measurer;
035:
036: private final int width = 500;
037: private final int height = 200;
038: private final BufferedImage im = new BufferedImage(width, height,
039: BufferedImage.TYPE_INT_RGB);
040:
041: private final String s = "I TestItalic TestPlain I";
042: private final Font f = new Font("times new roman", Font.ITALIC, 24);
043: private final Font f1 = new Font("serif", Font.PLAIN, 60);
044: private final FontRenderContext frc = ((Graphics2D) im
045: .getGraphics()).getFontRenderContext();
046:
047: public TextMeasurerTest(String name) {
048: super (name);
049: }
050:
051: @Override
052: public void setUp() throws Exception {
053: super .setUp();
054:
055: AttributedString as = new AttributedString(s);
056: as.addAttribute(TextAttribute.FONT, f, 0, 12);
057: as.addAttribute(TextAttribute.FONT, f1, 12, s.length());
058: AttributedCharacterIterator aci = as.getIterator();
059:
060: measurer = new TextMeasurer(aci, frc);
061: }
062:
063: @Override
064: public void tearDown() throws Exception {
065: super .tearDown();
066: }
067:
068: public void testInsertChar() throws Exception {
069: float oldAdvance = measurer.getAdvanceBetween(5, 14);
070: float oldAdvanceNoChange = measurer.getAdvanceBetween(0, 5);
071:
072: String s1 = "I TestItalic WTestPlain I";
073: AttributedString as = new AttributedString(s1);
074: as.addAttribute(TextAttribute.FONT, f, 0, 12);
075: as.addAttribute(TextAttribute.FONT, f1, 12, s1.length());
076: AttributedCharacterIterator aci = as.getIterator();
077:
078: measurer.insertChar(aci, 13);
079:
080: assertTrue(measurer.getAdvanceBetween(5, 14) > oldAdvance);
081: assertEquals(oldAdvanceNoChange, measurer.getAdvanceBetween(0,
082: 5), 0.01);
083: }
084:
085: public void testDeleteChar() throws Exception {
086: String s1 = "I TestItalic estPlain I";
087:
088: AttributedString as = new AttributedString(s1);
089: as.addAttribute(TextAttribute.FONT, f, 0, 12);
090: as.addAttribute(TextAttribute.FONT, f1, 12, s1.length());
091: AttributedCharacterIterator aci = as.getIterator();
092: float oldAdvance = measurer.getAdvanceBetween(aci
093: .getBeginIndex(), aci.getEndIndex() + 1);
094: float oldAdvanceNoChange = measurer.getAdvanceBetween(0, 5);
095:
096: measurer.deleteChar(aci, 13);
097:
098: assertTrue(measurer.getAdvanceBetween(aci.getBeginIndex(), aci
099: .getEndIndex()) < oldAdvance);
100: assertEquals(oldAdvanceNoChange, measurer.getAdvanceBetween(0,
101: 5), 0.01);
102: }
103:
104: public void testClone() throws Exception {
105: TextMeasurer m = (TextMeasurer) measurer.clone();
106: assertNotNull(m);
107: assertTrue(m != measurer);
108: }
109:
110: public void testGetLayout() throws Exception {
111: TextLayout l1 = measurer.getLayout(0, 15);
112: TextLayout l2 = measurer.getLayout(2, 15);
113: TextLayout l3 = measurer.getLayout(2, 4);
114:
115: assertTrue(l1.getAdvance() > l2.getAdvance());
116: assertTrue(l2.getAdvance() > l3.getAdvance());
117: }
118:
119: public void testGetAdvanceBetween() throws Exception {
120: float adv1 = measurer.getAdvanceBetween(1, 4);
121: float adv2 = measurer.getAdvanceBetween(1, 5);
122: float adv3 = measurer.getAdvanceBetween(0, 5);
123: float adv4 = measurer.getAdvanceBetween(1, 4);
124:
125: assertTrue(adv1 < adv2);
126: assertTrue(adv2 < adv3);
127: assertEquals(adv1, adv4, 0.01);
128: }
129:
130: public void testGetLineBreakIndex() throws Exception {
131: assertEquals(5, measurer.getLineBreakIndex(2, 35));
132: assertEquals(11, measurer.getLineBreakIndex(0, 100));
133: assertEquals(4, measurer.getLineBreakIndex(4, 1));
134: assertEquals(24, measurer.getLineBreakIndex(0, 1000));
135: }
136:
137: public static Test suite() {
138: return new TestSuite(TextMeasurerTest.class);
139: }
140: }
|