本文整理匯總了Java中java.awt.font.LineBreakMeasurer類的典型用法代碼示例。如果您正苦於以下問題:Java LineBreakMeasurer類的具體用法?Java LineBreakMeasurer怎麽用?Java LineBreakMeasurer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
LineBreakMeasurer類屬於java.awt.font包,在下文中一共展示了LineBreakMeasurer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getLineBreakMeasurers
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private LineBreakMeasurer[] getLineBreakMeasurers(Graphics2D g) {
if (lbmTexto == null && (Texto != null && !Texto.equals(""))) {
lbmTexto = new LineBreakMeasurer[Textos.length];
for (int i = 0; i < lbmTexto.length; i++) {
String tmp = Textos[i].isEmpty()? " " : Textos[i];
AttributedString attribString = new AttributedString(tmp);
attribString.addAttribute(TextAttribute.FONT, getFont());
//attribString.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator attribCharIterator = attribString.getIterator();
//FontRenderContext frc = new FontRenderContext(null, true, false);
FontRenderContext frc = g.getFontRenderContext();
lbmTexto[i] = new LineBreakMeasurer(attribCharIterator, frc);
}
}
return lbmTexto;
}
示例2: getPreferredSize
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
@Override
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip)c).getTipText();
if (tipText == null || tipText.isEmpty()) {
return new Dimension(0, 0);
}
float x = 0f;
float y = 0f;
for (String line : lineBreak.split(tipText)) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText
= new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
x = Math.max(x, layout.getVisibleAdvance());
y += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
}
return new Dimension((int) (x + 2 * margin),
(int) (y + 2 * margin));
}
示例3: measureExactLineBreakIndex
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
protected int measureExactLineBreakIndex(float width, int endLimit, boolean requireWord)
{
//FIXME would it be faster to create and cache a LineBreakMeasurer for the whole paragraph?
Map<Attribute, Object> attributes = new HashMap<Attribute, Object>();
// we only need the font as it includes the size and style
attributes.put(TextAttribute.FONT, fontInfo.fontInfo.font);
String textLine = paragraphText.substring(paragraphPosition, endLimit);
AttributedString attributedLine = new AttributedString(textLine, attributes);
// we need a fresh iterator for the line
BreakIterator breakIterator = paragraphTruncateAtChar ? BreakIterator.getCharacterInstance()
: BreakIterator.getLineInstance();
LineBreakMeasurer breakMeasurer = new LineBreakMeasurer(attributedLine.getIterator(),
breakIterator, context.getFontRenderContext());
int breakIndex = breakMeasurer.nextOffset(width, endLimit - paragraphPosition, requireWord)
+ paragraphPosition;
if (logTrace)
{
log.trace("exact line break index measured at " + (paragraphOffset + breakIndex));
}
return breakIndex;
}
示例4: drawTextInBoundedArea
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private static void drawTextInBoundedArea(Graphics2D g2d, int x1, int y1,
int x2, int y2, String text) {
float interline = 1;
float width = x2 - x1;
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FOREGROUND, g2d.getPaint());
as.addAttribute(TextAttribute.FONT, g2d.getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = new FontRenderContext(null, true, false);
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < text.length()) {
TextLayout tl = lbm.nextLayout(width);
y1 += tl.getAscent();
tl.draw(g2d, x1, y1);
y1 += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.
getAscent();
if (y1 > y2) {
break;
}
}
}
示例5: pixelDataFromString
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private final static void pixelDataFromString(int width, int height, String str, java.awt.Font font, int[] pixels, LineBreakMeasurer measurer) {
measurer.setPosition(0);
g2d.clearRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
float wrapping_width = width;
float y = 0;
int length = str.length();
while (measurer.getPosition() < length) {
TextLayout layout = measurer.nextLayout(wrapping_width);
y += (layout.getAscent());
float x = layout.isLeftToRight() ? 0 : (wrapping_width - layout.getAdvance());
layout.draw(g2d, x, y);
y += layout.getDescent() + layout.getLeading();
}
image.getRaster().getDataElements(0, 0, image.getWidth(), image.getHeight(), pixels);
}
示例6: nextLayout
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
/**
* @param wrappingWidth width of the layout
* @param frCtx for possible new instance of LineBreakMeasurer
* @return next TextLayout that is to be rendered
*/
private TextLayout nextLayout(float wrappingWidth, FontRenderContext frc) {
TextLayout l;
if (currentLayout == textLayouts.size()) {
LineBreakMeasurer old = lineBreakMeasurer;
LineBreakMeasurer measurer = getMeasurer(frc);
if (measurer == null) {
return null;
}
l = measurer.nextLayout(wrappingWidth);
textLayouts.add(l);
if (old != measurer) { // new line
startLayouts.add(l);
}
} else {
l = (TextLayout) textLayouts.get(currentLayout);
}
currentLayout++; // advance to next
return l;
}
示例7: drawText
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private void drawText(Graphics2D g2, String text, float width) {
FontRenderContext frc = g2.getFontRenderContext();
AttributedString styledText = new AttributedString(text);
AttributedCharacterIterator aci = styledText.getIterator();
int start = aci.getBeginIndex();
int end = aci.getEndIndex();
LineBreakMeasurer measurer = new LineBreakMeasurer(aci, frc);
measurer.setPosition(start);
float x = 0, y = 0;
while (measurer.getPosition() < end) {
TextLayout layout = measurer.nextLayout(width);
y += layout.getAscent();
float dx = layout.isLeftToRight() ? 0 : width - layout.getAdvance();
layout.draw(g2, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
示例8: wrapLines
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private String wrapLines(String s, float wrappingWidth, FontRenderContext frc, String lineBreak) {
AttributedCharacterIterator aci = new AttributedString(s).getIterator();
int end = aci.getEndIndex();
LineBreakMeasurer measurer = new LineBreakMeasurer(aci, frc);
StringBuffer output = new StringBuffer();
int position = 0;
int lastPosition = 0;
do {
measurer.nextLayout(wrappingWidth);
position = measurer.getPosition();
output.append(s, lastPosition, position);
output.append(lineBreak);
lastPosition = position;
} while (position < end);
return output.toString();
}
示例9: setFont
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
@Override
public void setFont(Font f) {
super.setFont(f);
atText.addAttribute(TextAttribute.FONT,
usesSpecificFont() ? getFont() : getMainFont());
lbm = new LineBreakMeasurer(atText.getIterator(), DEFAULT_FRC);
invalidate();
}
示例10: getPreferredSize
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip)c).getTipText();
if (tipText == null) {
return new Dimension(0, 0);
}
float x = 0f;
float y = 0f;
for (String line : lineBreak.split(((JToolTip) c).getTipText())) {
AttributedCharacterIterator styledText
= new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
x = Math.max(x, layout.getVisibleAdvance());
y += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
}
return new Dimension((int) (x + 2 * margin),
(int) (y + 2 * margin));
}
示例11: countLines
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private static int countLines(JTextArea textArea)
{
AttributedString text = new AttributedString(textArea.getText());
FontRenderContext frc = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext();
AttributedCharacterIterator charIt = text.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(charIt, frc);
float formatWidth = (float) textArea.getSize().width;
lbm.setPosition(charIt.getBeginIndex());
int numLines =0;
while (lbm.getPosition()<charIt.getEndIndex())
{
lbm.nextLayout(formatWidth);
numLines++;
}
return numLines;
}
示例12: paintOrGetSize
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
private Dimension paintOrGetSize(Graphics2D g, int width) {
Insets insets = getInsets();
width -= insets.left + insets.right + margin.left + margin.right;
float w = insets.left + insets.right + margin.left + margin.right;
float x = insets.left + margin.left, y = insets.top + margin.top;
if (width > 0 && text != null && text.length() > 0) {
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator aci = as.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
float max = 0;
while (lbm.getPosition() < aci.getEndIndex()) {
TextLayout textLayout = lbm.nextLayout(width);
if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width)
textLayout = textLayout.getJustifiedLayout(width);
if (g != null)
textLayout.draw(g, x, y + textLayout.getAscent());
y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();
max = Math.max(max, textLayout.getVisibleAdvance());
}
w += max;
}
return new Dimension((int)Math.ceil(w), (int)Math.ceil(y) + insets.bottom + margin.bottom);
}
示例13: render
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
@Override
public void render(final Graphics2D g) {
if (this.displayedText == null || this.displayedText.isEmpty() || !Game.getRenderEngine().canRender(this.entity)) {
return;
}
final Point2D location = Game.getCamera().getViewPortLocation(this.entity);
RenderEngine.renderImage(g, this.bubble, new Point2D.Double(location.getX() + this.entity.getWidth() / 2.0 - this.textBoxWidth / 2.0 - PADDING, location.getY() - this.height - PADDING));
g.setColor(SPEAK_FONT_COLOR);
final FontRenderContext frc = g.getFontRenderContext();
final String text = this.displayedText;
final AttributedString styledText = new AttributedString(text);
styledText.addAttribute(TextAttribute.FONT, this.font);
final AttributedCharacterIterator iterator = styledText.getIterator();
final LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc);
measurer.setPosition(0);
final float x = (float) Game.getCamera().getViewPortLocation(this.entity).getX() + this.entity.getWidth() / 2.0f - this.textBoxWidth / 2.0f;
float y = (float) Game.getCamera().getViewPortLocation(this.entity).getY() - this.height;
while (measurer.getPosition() < text.length()) {
final TextLayout layout = measurer.nextLayout(this.textBoxWidth);
y += layout.getAscent();
final float dx = layout.isLeftToRight() ? 0 : this.textBoxWidth - layout.getAdvance();
layout.draw(g, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
示例14: paint
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
@Override
public void paint(Graphics g, JComponent c) {
if (c.isOpaque()) {
ImageLibrary.drawTiledImage("image.background.FreeColToolTip", g, c, null);
}
g.setColor(Color.BLACK); // FIXME: find out why this is necessary
Graphics2D graphics = (Graphics2D)g;
float x = margin;
float y = margin;
for (String line : lineBreak.split(((JToolTip) c).getTipText())) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText =
new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
y += (layout.getAscent());
float dx = layout.isLeftToRight() ?
0 : (maximumWidth - layout.getAdvance());
layout.draw(graphics, x + dx, y);
y += layout.getDescent() + layout.getLeading();
}
}
}
示例15: test
import java.awt.font.LineBreakMeasurer; //導入依賴的package包/類
public void test() {
// construct a paragraph as follows: MIXED + [SPACING + WORD] + ...
StringBuffer text = new StringBuffer(MIXED);
for (int i=0; i < NUM_WORDS; i++) {
text.append(SPACING);
text.append(WORD);
}
AttributedString attrString = new AttributedString(text.toString());
attrString.addAttribute(TextAttribute.SIZE, new Float(24.0));
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(),
DEFAULT_FRC);
// get width of a space-word sequence, in context
int sequenceLength = WORD.length()+SPACING.length();
measurer.setPosition(text.length() - sequenceLength);
TextLayout layout = measurer.nextLayout(10000.0f);
if (layout.getCharacterCount() != sequenceLength) {
throw new Error("layout length is incorrect!");
}
final float sequenceAdvance = layout.getVisibleAdvance();
float wrappingWidth = sequenceAdvance * 2;
// now run test with a variety of widths
while (wrappingWidth < (sequenceAdvance*NUM_WORDS)) {
measurer.setPosition(0);
checkMeasurer(measurer,
wrappingWidth,
sequenceAdvance,
text.length());
wrappingWidth += sequenceAdvance / 5;
}
}