|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 앞의 클래스 다음의 클래스 | 프레임 있어 프레임 없음 | |||||||||
| 개요: 상자 | 필드 | 생성자 | 메소드 | 상세: 필드 | 생성자 | 메소드 | |||||||||
java.lang.Objectjava.awt.font.LineBreakMeasurer
public final class LineBreakMeasurer
LineBreakMeasurer 클래스를 사용하면, 서식 첨부 텍스트를, 특정의 가시 유효폭에 들어가는 행 (또는 세그먼트(segment))으로 나눌 수가 있습니다. 이것은, 고유의 폭 (랩핑폭으로 불린다)에 들어가는 텍스트의 단락을 클라이언트에 표시하는 경우에 편리합니다.
LineBreakMeasurer 는, 서식 첨부 텍스트에 대한 반복자를 사용해 구축됩니다. 반복자의 범위는 텍스트내의 1 개의 단락입니다. LineBreakMeasurer 는, 다음의 텍스트 세그먼트(segment)를 개시하기 위해서, 텍스트내의 위치를 포함합니다. 처음은, 이 정도치가 텍스트의 시점입니다. 단락의 방향은, 쌍방향 포맷 규칙에 따라, 전방향 (왼쪽에서 오른쪽 또는 오른쪽에서 왼쪽)에 이릅니다. 단락으로부터 취득된 모든 세그먼트(segment)는, 그 단락과 같은 방향이 됩니다.
텍스트의 세그먼트(segment)는,nextLayout 메소드를 호출하는 것으로 취득됩니다. 이 메소드는, 랩핑폭에 들어가는 텍스트를 나타내는 TextLayout 를 돌려줍니다. nextLayout 메소드는,nextLayout 가 돌려준 레이아웃의 구석에 현재의 위치를 이동합니다.
LineBreakMeasurer 는, 가장 일반적인 다음의 개행을 구현합니다. 랩핑폭에 들어가는 모든 단어는, 같은 행에 배치됩니다. 최초의 단어가 수습되지 않으면, 랩핑폭에 들어갈 뿐(만큼)의 문자가 그 행에 배치됩니다. 각 행에는 적어도 1 문자가 배치됩니다.
LineBreakMeasurer 에 의해 반환되는 TextLayout 의 인스턴스는, 탭을 폭 0 의 스페이스와 같게 취급합니다. 위치 결정을 위해서(때문에) 탭 단락의 세그먼트(segment)를 취득하는 클라이언트는, 텍스트에 대한 리밋트 오프셋(offset)를 취하는 nextLayout 의 overload를 사용하도록 해 주세요. 리밋트 오프셋(offset)는, 탭 이후의 최초의 문자입니다. 이 메소드가 돌려주는 TextLayout 객체는, 지정된 리밋트 (현재의 위치와 리밋트와의 사이의 텍스트가 랩핑폭에 들어가지 않는 경우에는, 리밋트의 전)로 끝납니다.
탭 단락의 텍스트를 레이아웃 하는 클라이언트에는, 최초의 세그먼트(segment)를 행에 배치한 뒤, 약간 다른 개행 정책가 필요합니다. 나머지의 영역에 일부의 단어를 거두는 것이 아니라, 전체를 다음의 행에 배치합니다. 정책의 이 변경은,boolean 파라미터를 취하는 nextLayout 의 overload로 요구할 수 있습니다. 이 파라미터가 true 의 경우,nextLayout 는, 최초의 단어가 지정된 영역에 들어가지 않을 때에 null 를 돌려줍니다. 아래와 같은 탭 샘플을 참조해 주세요.
일반적으로,LineBreakMeasurer 의 작성에 사용된 텍스트가 변경되었을 경우는, 변경을 반영하기 위해서 새로운 LineBreakMeasurer 를 작성할 필요가 있습니다 (지금까지의 LineBreakMeasurer 는 그대로 정상적으로 동작하지만, 텍스트의 변경에는 대응하지 않는다). 다만, 텍스트의 변경이 1 문자의 삽입 또는 삭제의 경우에는,insertChar 또는 deleteChar 를 호출해, 기존의 LineBreakMeasurer 를 「갱신」해도 괜찮습니다. 기존의 LineBreakMeasurer 를 갱신하는 (분)편이, 새롭게 작성하는 것보다도 처리 시간이 걸리지 않습니다. 사용자의 키 입력에 의해 텍스트를 변경하는 경우는, 이러한 방법을 이용하면(자) 좋을 것입니다.
례:
컴퍼넌트에 단락을 draw 합니다.
public void paint(Graphics graphics) {
Point2D pen = new Point2D(10, 20);
Graphics2D g2d = (Graphics2D) graphics;
FontRenderContext frc = g2d.getFontRenderContext();
// let styledText be an AttributedCharacterIterator containing at least
// one character
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
float wrappingWidth = getSize(). width - 15;
while (measurer.getPosition() < fStyledText.length()) {
TextLayout layout = measurer.nextLayout(wrappingWidth);
pen.y += (layout.getAscent());
float dx = layout.isLeftToRight() ?
0 : (wrappingWidth - layout.getAdvance());
layout.draw(graphics, pen.x + dx, pen.y);
pen.y += layout.getDescent() + layout.getLeading();
}
}
탭 첨부의 텍스트를 draw 합니다. 알기 쉽게하기 위해(때문에), 텍스트의 방향은 모두 왼쪽에서 오른쪽으로 합니다.
public void paint(Graphics graphics) {
float leftMargin = 10, rightMargin = 310;
float[] tabStops = { 100, 250 };
// assume styledText is an AttributedCharacterIterator, and the number
// of tabs in styledText is tabCount
int[] tabLocations = new int[tabCount+1];
int i = 0;
for (char c = styledText.first(); c ! = styledText.DONE; c = styledText.next()) {
if (c == '\t') {
tabLocations[i++] = styledText.getIndex();
}
}
tabLocations[tabCount] = styledText.getEndIndex() - 1;
// Now tabLocations has an entry for every tab's offset in
// the text. For convenience, the last entry is tabLocations
// is the offset of the last character in the text.
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText);
int currentTab = 0;
float verticalPos = 20;
while (measurer.getPosition() < styledText.getEndIndex()) {
// Lay out and draw each line. All segments on a line
// must be computed before any drawing can occur, since
// we must know the largest ascent on the line.
// TextLayouts are computed and stored in a Vector;
// their horizontal positions are stored in a parallel
// Vector.
// lineContainsText is true after first segment is drawn
boolean lineContainsText = false;
boolean lineComplete = false;
float maxAscent = 0, maxDescent = 0;
float horizontalPos = leftMargin;
Vector layouts = new Vector(1);
Vector penPositions = new Vector(1);
while (! lineComplete) {
float wrappingWidth = rightMargin - horizontalPos;
TextLayout layout =
measurer.nextLayout(wrappingWidth,
tabLocations[currentTab]+1,
lineContainsText);
// layout can be null if lineContainsText is true
if (layout ! = null) {
layouts.addElement(layout);
penPositions.addElement(new Float(horizontalPos));
horizontalPos += layout.getAdvance();
maxAscent = Math.max(maxAscent, layout.getAscent());
maxDescent = Math.max(maxDescent,
layout.getDescent() + layout.getLeading());
} else {
lineComplete = true;
}
lineContainsText = true;
if (measurer.getPosition() == tabLocations[currentTab]+1) {
currentTab++;
}
if (measurer.getPosition() == styledText.getEndIndex())
lineComplete = true;
else if (horizontalPos >= tabStops[tabStops.length-1])
lineComplete = true;
if (! lineComplete) {
// move to next tab stop
int j;
for (j=0; horizontalPos >= tabStops[j]; j++) {}
horizontalPos = tabStops[j];
}
}
verticalPos += maxAscent;
Enumeration layoutEnum = layouts.elements();
Enumeration positionEnum = penPositions.elements();
// now iterate through layouts and draw them
while (layoutEnum.hasMoreElements()) {
TextLayout nextLayout = (TextLayout) layoutEnum.nextElement();
Float nextPosition = (Float) positionEnum.nextElement();
nextLayout.draw(graphics, nextPosition.floatValue(), verticalPos);
}
verticalPos += maxDescent;
}
}
TextLayout | 생성자 의 개요 | |
|---|---|
LineBreakMeasurer (AttributedCharacterIterator text,
BreakIterator breakIter,
FontRenderContext frc)
지정된 텍스트에 대한 LineBreakMeasurer 를 구축합니다. |
|
LineBreakMeasurer (AttributedCharacterIterator text,
FontRenderContext frc)
지정된 텍스트에 대한 LineBreakMeasurer 를 구축합니다. |
|
| 메소드의 개요 | |
|---|---|
void |
deleteChar (AttributedCharacterIterator newParagraph,
int deletePos)
텍스트로부터 문자가 1 개 삭제된 뒤에 LineBreakMeasurer 를 갱신해, 현재의 위치를 그 단락의 선두로 설정합니다. |
int |
getPosition ()
LineBreakMeasurer 의 현재의 위치를 돌려줍니다. |
void |
insertChar (AttributedCharacterIterator newParagraph,
int insertPos)
텍스트에 문자가 1 개 삽입된 뒤에 LineBreakMeasurer 를 갱신해, 현재의 위치를 그 단락의 선두로 설정합니다. |
TextLayout |
nextLayout (float wrappingWidth)
다음의 레이아웃을 돌려주어, 현재의 위치를 갱신합니다. |
TextLayout |
nextLayout (float wrappingWidth,
int offsetLimit,
boolean requireNextWord)
다음의 레이아웃을 돌려주어, 현재의 위치를 갱신합니다. |
int |
nextOffset (float wrappingWidth)
다음의 레이아웃의 마지막 위치를 돌려줍니다. |
int |
nextOffset (float wrappingWidth,
int offsetLimit,
boolean requireNextWord)
다음의 레이아웃의 마지막 위치를 돌려줍니다. |
void |
setPosition (int newPosition)
LineBreakMeasurer 의 현재의 위치를 설정합니다. |
| 클래스 java.lang. Object 로부터 상속된 메소드 |
|---|
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
| 생성자 의 상세 |
|---|
public LineBreakMeasurer(AttributedCharacterIterator text,
FontRenderContext frc)
LineBreakMeasurer 를 구축합니다.
text - 이 LineBreakMeasurer 가
TextLayout 객체의 생성 대상으로 하는 텍스트.
이 텍스트에는, 1 개(살) 이상의 문자가 포함되어 있지 않으면 안 된다.
iter 로 얻을 수 있는 텍스트가 변경되었을 경우,
그 후의 이 LineBreakMeasurer 의 인스턴스에의 호출의 결과는 보증되지 않는다
(다만, 나중에 insertChar 또는
deleteChar 를 호출하는 경우를 제외하다. 관련 항목을 참조)frc - 텍스트를 정확하게 측정하기 위해서 필요한
그래픽스 디바이스에 관한 정보를 포함한다.
텍스트 측정은, 디바이스의 해상도에 의해 조금 달라,
에일리어징 제거등의 속성에 따라서 다르다.
이 파라미터는,
LineBreakMeasurer 와 사용자 공간의 사이의 이동은 지정하지 않는insertChar(java.text.AttributedCharacterIterator, int) ,
deleteChar(java.text.AttributedCharacterIterator, int)
public LineBreakMeasurer(AttributedCharacterIterator text,
BreakIterator breakIter,
FontRenderContext frc)
LineBreakMeasurer 를 구축합니다.
text - 이 LineBreakMeasurer 가
TextLayout 객체의 생성 대상으로 하는 텍스트.
이 텍스트에는, 1 개(살) 이상의 문자가 포함되어 있지 않으면 안 된다.
iter 로 얻을 수 있는 텍스트가 변경되었을 경우,
그 후의 이 LineBreakMeasurer 의 인스턴스에의 호출의 결과는 보증되지 않는다
(다만, 나중에 insertChar 또는
deleteChar 를 호출하는 경우를 제외하다. 관련 항목을 참조)breakIter - 개행을 정의한다
BreakIterator frc - 텍스트를 정확하게 측정하기 위해서 필요한
그래픽스 디바이스에 관한 정보를 포함한다.
텍스트 측정은, 디바이스의 해상도에 의해 조금 달라,
에일리어징 제거등의 속성에 따라서 다르다.
이 파라미터는,
LineBreakMeasurer 와 사용자 공간의 사이의 이동은 지정하지 않는다
IllegalArgumentException - 텍스트가 1 문자에 못 미친 경우insertChar(java.text.AttributedCharacterIterator, int) ,
deleteChar(java.text.AttributedCharacterIterator, int) | 메소드의 상세 |
|---|
public int nextOffset(float wrappingWidth)
LineBreakMeasurer 의 현재의 위치를 갱신하지 않습니다.
wrappingWidth - 다음의 레이아웃내의 텍스트에
허용 되는 최대의 가시 유효폭
TextLayout 의
리밋트를 나타내는 텍스트내의 좌표
public int nextOffset(float wrappingWidth,
int offsetLimit,
boolean requireNextWord)
LineBreakMeasurer 의 현재의 위치를 갱신하지 않습니다.
wrappingWidth - 다음의 레이아웃내의 텍스트에
허용 되는 최대의 가시 유효폭offsetLimit - 리밋트 이후의 텍스트가 랩핑폭에 들어가는 경우에서도,
다음의 레이아웃에 포함되지 않는 최초의 문자.
offsetLimit 는,
현재의 위치보다 크지 않으면 안 되는requireNextWord - true 의 경우,
다음의 단어 전체가 wrappingWidth 에 들어가지 않을 때는 현재의 위치가 반환된다.
false 의 경우,
반환되는 좌표는 현재의 위치보다 적어도 1 크다
TextLayout 의
리밋트를 나타내는 텍스트내의 좌표public TextLayout nextLayout(float wrappingWidth)
wrappingWidth - 다음의 레이아웃내의 텍스트에
허용 되는 최대의 가시 유효폭
wrappingWidth 에 들어가는 다음의 행을 나타내,
현재의 위치로부터 시작된다
TextLayout
public TextLayout nextLayout(float wrappingWidth,
int offsetLimit,
boolean requireNextWord)
wrappingWidth - 다음의 레이아웃내의 텍스트에
허용 되는 최대의 가시 유효폭offsetLimit - 리밋트 이후의 텍스트가 랩핑폭에 들어가는 경우에서도,
다음의 레이아웃에 포함되지 않는 최초의 문자.
offsetLimit 는,
현재의 위치보다 크지 않으면 안 되는requireNextWord - true 의 경우,
현재의 위치에 있는 단어 전체가 랩핑폭에 들어가지 않을 때는
null 가 반환된다. false 의 경우,
적어도 현재의 위치에 있는 문자를 포함한,
유효한 레이아웃이 반환된다
wrappingWidth 에 들어가는 다음의 행을 나타내,
현재의 위치로부터 시작된다
TextLayout. 현재의 위치가,
LineBreakMeasurer 의 사용하는 텍스트의 끝에 있는 경우,
null 가 반환된다. public int getPosition()
LineBreakMeasurer 의 현재의 위치를 돌려줍니다.
LineBreakMeasurer 의 현재의 위치setPosition(int) public void setPosition(int newPosition)
LineBreakMeasurer 의 현재의 위치를 설정합니다.
newPosition - LineBreakMeasurer 의 현재의 위치.
이 정도치는,
LineBreakMeasurer 를 작성하기 위해서 사용되는 텍스트
(또는 insertChar 인가
deleteChar 에 마지막에 건네받은 텍스트) 안에 있는getPosition()
public void insertChar(AttributedCharacterIterator newParagraph,
int insertPos)
LineBreakMeasurer 를 갱신해, 현재의 위치를 그 단락의 선두로 설정합니다.
newParagraph - 삽입 후의 텍스트insertPos - 텍스트내의,
문자가 삽입된 위치
IndexOutOfBoundsException - insertPos 가
newParagraph 의 개시 위치보다 전,
또는 newParagraph 의 종료 위치와 같은가, 그 이후인 경우
NullPointerException - newParagraph 가
null 의 경우deleteChar(java.text.AttributedCharacterIterator, int)
public void deleteChar(AttributedCharacterIterator newParagraph,
int deletePos)
LineBreakMeasurer 를 갱신해, 현재의 위치를 그 단락의 선두로 설정합니다.
newParagraph - 삭제 후의 텍스트deletePos - 텍스트내의,
문자가 삭제된 위치
IndexOutOfBoundsException - deletePos 가
newParagraph 의 개시 위치보다 전,
또는 newParagraph 의 종료 위치에서(보다) 뒤인 경우
NullPointerException - newParagraph 가
null 의 경우insertChar(java.text.AttributedCharacterIterator, int)
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 앞의 클래스 다음의 클래스 | 프레임 있어 프레임 없음 | |||||||||
| 개요: 상자 | 필드 | 생성자 | 메소드 | 상세: 필드 | 생성자 | 메소드 | |||||||||
Copyright 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms . Documentation Redistribution Policy 도 참조해 주세요.