List of usage examples for java.awt.font LineBreakMeasurer LineBreakMeasurer
public LineBreakMeasurer(AttributedCharacterIterator text, BreakIterator breakIter, FontRenderContext frc)
From source file:forge.view.arcane.util.OutlinedLabel.java
/** {@inheritDoc} */ @Override//from w w w . j ava 2s . c o m public final void paint(final Graphics g) { if (getText().length() == 0) { return; } Dimension size = getSize(); // // if( size.width < 50 ) { // g.setColor(Color.cyan); // g.drawRect(0, 0, size.width-1, size.height-1); // } Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); int textX = outlineSize, textY = 0; int wrapWidth = Math.max(0, wrap ? size.width - outlineSize * 2 : Integer.MAX_VALUE); final String text = getText(); AttributedString attributedString = new AttributedString(text); if (!StringUtils.isEmpty(text)) { attributedString.addAttribute(TextAttribute.FONT, getFont()); } AttributedCharacterIterator charIterator = attributedString.getIterator(); FontRenderContext fontContext = g2d.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, BreakIterator.getWordInstance(Locale.ENGLISH), fontContext); int lineCount = 0; while (measurer.getPosition() < charIterator.getEndIndex()) { measurer.nextLayout(wrapWidth); lineCount++; if (lineCount > 2) { break; } } charIterator.first(); // Use char wrap if word wrap would cause more than two lines of text. if (lineCount > 2) { measurer = new LineBreakMeasurer(charIterator, BreakIterator.getCharacterInstance(Locale.ENGLISH), fontContext); } else { measurer.setPosition(0); } while (measurer.getPosition() < charIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout(wrapWidth); float ascent = textLayout.getAscent(); textY += ascent; // Move down to baseline. g2d.setColor(outlineColor); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); textLayout.draw(g2d, textX + outlineSize, textY - outlineSize); textLayout.draw(g2d, textX + outlineSize, textY + outlineSize); textLayout.draw(g2d, textX - outlineSize, textY - outlineSize); textLayout.draw(g2d, textX - outlineSize, textY + outlineSize); g2d.setColor(getForeground()); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); textLayout.draw(g2d, textX, textY); // Move down to top of next line. textY += textLayout.getDescent() + textLayout.getLeading(); } }
From source file:net.sf.jasperreports.engine.fill.SimpleTextLineWrapper.java
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;/*from ww w .ja v a2 s . c o m*/ if (logTrace) { log.trace("exact line break index measured at " + (paragraphOffset + breakIndex)); } return breakIndex; }