layout = measurer.nextLayout(boxWidth);
TextLayout layout = measurer.nextLayout((float)wrappingWidth, nextBreak, true); if (layout == null) { layout = measurer.nextLayout((float)wrappingWidth, nextBreak, false);
TextLayout tl = lbm.nextLayout(wrapWidth); textY += tl.getAscent(); Rectangle2D bb = tl.getBounds();
private WrappedLines breakLines(Graphics2D g) { Dimension size = label.getSize(); float maxWidth = size.width; AttributedString attributedText = new AttributedString(label.getText(), textAttributes()); AttributedCharacterIterator textIterator = attributedText.getIterator(); FontRenderContext fontRendering = g.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(textIterator, fontRendering); WrappedLines lines = new WrappedLines(); while(measurer.getPosition() < textIterator.getEndIndex()) { TextLayout layout = measurer.nextLayout(maxWidth); lines.add(layout); } return lines; }
TextLayout layout = measure.nextLayout(availWidth, newLineCheck.getIndex(), false); float ascent = layout.getAscent(); yPos += ascent;
public TextLayout next() { if (first) { first = false; } else { y += ascent * lineHeight; } double layoutWidth = width == 0 ? Float.MAX_VALUE : width; TextLayout layout = currentMeasurer.nextLayout((float) layoutWidth); if (width == 0) { layoutWidth = layout.getAdvance(); if (align == Align.RIGHT) { x = -layoutWidth; } else if (align == Align.CENTER) { x = -layoutWidth / 2.0; } } else if (align == Align.RIGHT) { x = width - layout.getAdvance(); } else if (align == Align.CENTER) { x = (width - layout.getAdvance()) / 2.0; } else if (align == Align.JUSTIFY) { // Don't justify the last line. if (currentMeasurer.getPosition() < currentText.length()) { layout = layout.getJustifiedLayout((float) width); } } ascent = layout.getAscent(); // y += layout.getDescent() + layout.getLeading() + layout.getAscent(); return layout; }
TextLayout textLayout = measurer.nextLayout(wrapWidth); float ascent = textLayout.getAscent();
lineMeasurer.nextLayout( labelItem.getAutoWrap(), lineText.length(), true); int newPosition = prevPosition;
/** * Compute the next layout using the specified line break measurer, * available width, and next line break offset. * * @param lineBreakMeasurer line break measurer * @param availableWidth available width * @param nextLineBreakOffset next line break offset * @return the next layout computed using the specified line break measurer, * available width, and next line break offset */ protected TextLayout computeNextLayout(final LineBreakMeasurer lineBreakMeasurer, final float availableWidth, final int nextLineBreakOffset) { return lineBreakMeasurer.nextLayout(availableWidth, nextLineBreakOffset, false); }
/** * @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; }
/** * @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 = textLayouts.get(currentLayout); } currentLayout++; // advance to next return l; }
lineMeasurer.setPosition(paragraphStart); while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(maxTextWidth); layoutHeight += layout.getAscent() + layout.getDescent() + layout.getLeading(); lineMeasurer.setPosition(paragraphStart); while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(maxTextWidth); float posX = (float) pointAdjusted.x; float posY = drawPosY;
public MetaFont.GlyphVector[] createGlyphVectors(String s, int wrapWidth) { java.util.ArrayList rv = new java.util.ArrayList(); java.text.AttributedString attStr = new java.text.AttributedString(s); attStr.addAttribute(java.awt.font.TextAttribute.FONT, myFont); java.awt.font.LineBreakMeasurer lbm = new java.awt.font.LineBreakMeasurer(attStr.getIterator(), frc); int idx0 = 0; java.awt.font.TextLayout currLay = lbm.nextLayout(wrapWidth); while (currLay != null) { rv.add(new JavaGlyphVector(this, s.substring(idx0, lbm.getPosition()), currLay, frc)); idx0 = lbm.getPosition(); currLay = lbm.nextLayout(wrapWidth); } return (MetaFont.GlyphVector[]) rv.toArray(new MetaFont.GlyphVector[0]); }
double GetWidthOfAttributedString(Graphics2D graphics2D, AttributedString attributedString) { AttributedCharacterIterator characterIterator = attributedString.getIterator(); FontRenderContext fontRenderContext = graphics2D.getFontRenderContext(); LineBreakMeasurer lbm = new LineBreakMeasurer(characterIterator, fontRenderContext); TextLayout textLayout = lbm.nextLayout(Integer.MAX_VALUE); return textLayout.getBounds().getWidth(); }
/** Evaluate of the List of TextLayout objects corresponding * to this MText. Some things are approximations! */ private ArrayList<TextLayout> getLayouts(Graphics g) { ArrayList<TextLayout> layouts = new ArrayList<TextLayout>(); Graphics2D g2d = (Graphics2D) g; FontRenderContext frc = g2d.getFontRenderContext(); AttributedString attrStr = new AttributedString(text); attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length()); LineBreakMeasurer measurer = new LineBreakMeasurer( attrStr.getIterator(), frc); float wrappingWidth; wrappingWidth = getSize().width - 15; while (measurer.getPosition() < text.length()) { TextLayout layout = measurer.nextLayout(wrappingWidth); layouts.add(layout); } return layouts; }
private TextLayout performWordBreak( final int start, final TextLayout textLayout, final LineBreakMeasurer lineBreakMeasurer, final int end ) { final TextLayout layout; if ( wordInstance.isBoundary( end ) != false ) { return textLayout; } int preceding = wordInstance.preceding( end ); if ( preceding == start ) { // single word does not fit on the line, so print full word lineBreakMeasurer.setPosition( start ); return lineBreakMeasurer.nextLayout( INFINITY, wordInstance.following( end ), false ); } else { lineBreakMeasurer.setPosition( start ); return lineBreakMeasurer.nextLayout( INFINITY, preceding, false ); } } }
private void drawErrorText(String errStr, int w, Graphics2D g2) { g2.setColor(colorModel.getColor("alertColor")); g2.setFont(new Font(res.getString("g2_Font"), Font.BOLD, 20)); g2.drawString(res.getString("ERROR"), 5, 20); AttributedString as = new AttributedString(errStr); as.addAttribute(TextAttribute.FONT, font12, 0, errStr.length()); AttributedCharacterIterator aci = as.getIterator(); FontRenderContext frc = g2.getFontRenderContext(); LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); float x = 5, y = 25; lbm.setPosition(0); while (lbm.getPosition() < errStr.length()) { TextLayout tl = lbm.nextLayout(w - x - 5); if (!tl.isLeftToRight()) { x = w - tl.getAdvance(); } tl.draw(g2, x, y += tl.getAscent()); y += tl.getDescent() + tl.getLeading(); } }
public LineBreakIteratorState next() { // For each line produced by the LinebreakMeasurer int start = lineBreakMeasurer.getPosition(); // float is the worst option to have accurate layouts. So we have to 'adjust' for rounding errors // and hope that no one notices .. TextLayout textLayout = lineBreakMeasurer.nextLayout( wrappingWidth + 0.5f, ci.getEndIndex(), false ); textLayout = postProcess( start, textLayout, lineBreakMeasurer ); int end = lineBreakMeasurer.getPosition(); // check if the text must be justified return new LineBreakIteratorState( textLayout, start, end ); }
public void paint(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; FontRenderContext frc = g2.getFontRenderContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator, frc); measurer.setPosition(m_start); float y = 0; while (measurer.getPosition() < m_end) { double ix = Math.sqrt((m_width / 2 - y) * y); float x = m_width / 2.0F - (float) ix; int width = (int) ix * 2; 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(); } }
private static int countLines(JTextArea textArea) { AttributedString text = new AttributedString(textArea.getText()); text.addAttribute(TextAttribute.FONT, textArea.getFont()); FontRenderContext frc = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext(); AttributedCharacterIterator charIt = text.getIterator(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIt, frc); Insets textAreaInsets = textArea.getInsets(); float formatWidth = textArea.getWidth() - textAreaInsets.left - textAreaInsets.right; lineMeasurer.setPosition(charIt.getBeginIndex()); int noLines = 0; while (lineMeasurer.getPosition() < charIt.getEndIndex()) { lineMeasurer.nextLayout(formatWidth); noLines++; } return noLines; }