Refine search
boolean centered) { AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, g.getFont()); attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black); String lastLine = null; LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext()); while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= boxHeight) { int previousPosition = measurer.getPosition(); layout = measurer.nextLayout(boxWidth); int height = ((Float) (layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue(); layouts.add(layout); lastLine = text.substring(previousPosition, measurer.getPosition()); currentHeight += height;
BufferedImage image = GraphicsUtilities.createCompatibleTranslucentImage(size.width, size.height); Graphics2D g2d = image.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); AttributedString attributedString = new AttributedString(key.text); attributedString.addAttribute(TextAttribute.FONT, key.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 = 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(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); int glowSize = key.glowSize; textLayout.draw(g2d, textX + glowSize / 2 + 1, textY + glowSize / 2 - 1); textLayout.draw(g2d, textX + glowSize / 2 + 1, textY + glowSize / 2 + 1); textLayout.draw(g2d, textX + glowSize / 2 - 1, textY + glowSize / 2 - 1); textLayout.draw(g2d, textX + glowSize / 2 - 1, textY + glowSize / 2 + 1);
boolean emptyParagraph = text.toString().trim().isEmpty(); AttributedCharacterIterator it = at.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext()); for (;;) { int startIndex = measurer.getPosition(); TextLayout layout = measurer.nextLayout((float)wrappingWidth, nextBreak, true); if (layout == null) { layout = measurer.nextLayout((float)wrappingWidth, nextBreak, false); int endIndex = measurer.getPosition(); measurer.setPosition(endIndex + 1); layout = layout.getJustifiedLayout((float)wrappingWidth); : new AttributedString(it, startIndex, endIndex); DrawTextFragment line = fact.getTextFragment(layout, str); lines.add(line);
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; }
FontRenderContext frc = g.getFontRenderContext(); AttributedCharacterIterator textIter = text.getIterator(); LineBreakMeasurer measure = new LineBreakMeasurer(textIter, frc); float yPos = y; float remain = h; AttributedCharacterIterator newLineCheck = text.getIterator(); while (measure.getPosition() < textIter.getEndIndex()) { newLineCheck.setIndex(measure.getPosition()); char ch; while ((ch = newLineCheck.next()) != CharacterIterator.DONE) { TextLayout layout = measure.nextLayout(availWidth, newLineCheck.getIndex(), false); float ascent = layout.getAscent(); yPos += ascent; remain -= ascent; g.setColor(Color.black); layout.draw(g, x + inset, yPos); yPos += layout.getDescent() + layout.getLeading() - 2; 4); g.setColor(new Color(0, 0, 0, 128)); g.fillRect(x + 2, y + advance + 1, w - 2, 1); g.setColor(Color.black);
private void createBubbleImage() { final BufferedImage img = Imaging.getCompatibleImage(500, 500); final Graphics2D g = img.createGraphics(); g.setFont(this.getFont()); final float stringWidth = g.getFontMetrics().stringWidth(this.currentText); if (stringWidth < this.textBoxWidth) { this.textBoxWidth = stringWidth; final FontRenderContext frc = g.getFontRenderContext(); final AttributedString styledText = new AttributedString(this.currentText); styledText.addAttribute(TextAttribute.FONT, this.getFont()); final AttributedCharacterIterator iterator = styledText.getIterator(); final LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc); measurer.setPosition(0); float y = 0; while (measurer.getPosition() < this.currentText.length()) { final TextLayout layout = measurer.nextLayout(this.textBoxWidth); y += layout.getAscent() + layout.getLeading() + layout.getDescent();
g2.setBackground(imageBackgroundColor); g2.clearRect(0, 0, w, h); g2.setColor(Color.white); g2.setColor(jfcBlue); g2.setFont(new Font("serif", Font.BOLD, 18)); g2.drawString("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();
final Font initFont = g2d.getFont(); final Color initColor = g2d.getColor(); final double[] coord = initGraphics(g2d, _format); final AttributedString text = new AttributedString(_s); text.addAttribute(TextAttribute.FONT, FONTBASE); final LineBreakMeasurer lineBreaker = new LineBreakMeasurer(text.getIterator(), g2d.getFontRenderContext()); TextLayout layoutEnCours = lineBreaker.nextLayout(w); y += layoutEnCours.getAscent(); layoutEnCours.draw(g2d, x, y); y += layoutEnCours.getDescent() + layoutEnCours.getLeading(); layoutEnCours = lineBreaker.nextLayout(w); g2d.setFont(initFont);
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; }
return; RenderingHints originalHints = g.getRenderingHints(); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliasing); final FontRenderContext frc = g.getFontRenderContext(); final AttributedString styledText = new AttributedString(text); styledText.addAttribute(TextAttribute.FONT, g.getFont()); final AttributedCharacterIterator iterator = styledText.getIterator(); final LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, frc); measurer.setPosition(0); float textY = (float) y; while (measurer.getPosition() < text.length()) { final TextLayout nextLayout = measurer.nextLayout((float) lineWidth); textY += nextLayout.getAscent(); final float dx = (float) (nextLayout.isLeftToRight() ? 0 : lineWidth - nextLayout.getAdvance()); nextLayout.draw(g, (float) (x + dx), textY); textY += nextLayout.getDescent() + nextLayout.getLeading();
|| textLength == fonts[0].canDisplayUpTo(text.toCharArray(), 0, textLength); if (!(text.contains("\n") || labelItem.getAutoWrap() > 0) && singleFont) { FontRenderContext frc = graphics.getFontRenderContext(); TextLayout layout = new TextLayout(text, fonts[0], frc); LineInfo lineInfo = new LineInfo(); List<LineComponent> components = AttributedCharacterIterator iter = attributed.getIterator(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer( iter, BreakIterator.getLineInstance(), while (lineMeasurer.getPosition() < iter.getEndIndex()) { lineMeasurer.nextLayout( labelItem.getAutoWrap(), lineText.length(), true); int newPosition = prevPosition; newPosition = lineMeasurer.getPosition(); } else { int nextBoundary = breaks.following(prevPosition); attributed.getIterator(null, prevPosition, newPosition); layout = new TextLayout(subIter, graphics.getFontRenderContext()); lineMeasurer.setPosition(newPosition);
final AttributedString text = new AttributedString(_s); text.addAttribute(TextAttribute.FONT, FONTBASE); final LineBreakMeasurer lineBreaker = new LineBreakMeasurer(text.getIterator(), g2d.getFontRenderContext()); TextLayout layoutEnCours = lineBreaker.nextLayout(w); TextLayout layoutSuivant = lineBreaker.nextLayout(w); while (layoutSuivant != null) { layoutEnCours = layoutEnCours.getJustifiedLayout(w); y += layoutEnCours.getAscent(); layoutEnCours.draw(g2d, x, y); y += layoutEnCours.getDescent() + layoutEnCours.getLeading(); layoutSuivant = lineBreaker.nextLayout(w); layoutEnCours.draw(g2d, x, y + layoutEnCours.getAscent()); g2d.translate(-coord[0], -coord[1]);
attributes.put(TextAttribute.FONT, g.getFont()); AttributedString as = new AttributedString(line, attributes); AttributedCharacterIterator aci = as.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer( aci, this.g.getFontRenderContext() ); TextLayout tl = measurer.nextLayout((float)lineBreakPxls); if (start >= line.length() || tl == null) { if (start < line.length()) { int end = measurer.getPosition(); String text = line.substring(start, end); float lenInPx = tl.getAdvance(); if (lenInPx > lineBreakPxls) { Log.error(String.format(
// Create Font object with Font attribute (e.g. Font family, Font size, etc) for calculation java.awt.Font currFont = new java.awt.Font(fontName, 0, fontSize); AttributedString attrStr = new AttributedString(cellValue); attrStr.addAttribute(TextAttribute.FONT, currFont); // Use LineBreakMeasurer to count number of lines needed for the text FontRenderContext frc = new FontRenderContext(null, true, true); LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc); int nextPos = 0; int lineCnt = 0; while (measurer.getPosition() < cellValue.length()) { nextPos = measurer.nextOffset(mergedCellWidth); // mergedCellWidth is the max width of each line lineCnt++; measurer.setPosition(nextPos); } Row currRow = currSht.getRow(rowNum); currRow.setHeight((short)(currRow.getHeight() * lineCnt)); // The above solution doesn't handle the newline character, i.e. "\n", and only // tested under horizontal merged cells.
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); } }
FontRenderContext frc = graphics.getFontRenderContext(); TextLayout layout = new TextLayout(text, labelItem.getTextStyle().getFont(), frc); LineInfo line = new LineInfo(text, layoutSentence(text, labelItem), layout); labelBounds = line.gv.getVisualBounds(); AttributedString attributed = new AttributedString(line, map); AttributedCharacterIterator iter = attributed.getIterator(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(iter, BreakIterator .getWordInstance(), graphics.getFontRenderContext()); BreakIterator breaks = BreakIterator.getWordInstance(); breaks.setText(line); while (lineMeasurer.getPosition() < iter.getEndIndex()) { TextLayout layout = lineMeasurer.nextLayout(labelItem.getAutoWrap(), line.length(), true); int newPosition = prevPosition; newPosition = lineMeasurer.getPosition(); } else { int nextBoundary = breaks.following(prevPosition); newPosition = nextBoundary; AttributedCharacterIterator subIter = attributed.getIterator(null, prevPosition, newPosition); layout = new TextLayout(subIter, graphics.getFontRenderContext()); lineMeasurer.setPosition(newPosition);
new AttributedString( " ", new AttributedString( allParagraphs, lastParagraphStart, LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, LINE_BREAK_FONT_RENDER_CONTEXT);//grx.getFontRenderContext() while (lineMeasurer.getPosition() < paragraph.getEndIndex() && !isMaxHeightReached) int startIndex = lineMeasurer.getPosition(); TextLayout layout = lineMeasurer.nextLayout(formatWidth); paragraph, startIndex, startIndex + layout.getCharacterCount() ); layout = new TextLayout(tmpText.getIterator(), grx.getFontRenderContext()); drawPosX = formatWidth - layout.getAdvance(); if (lineMeasurer.getPosition() < paragraph.getEndIndex()) layout = layout.getJustifiedLayout(formatWidth);
AttributedCharacterIterator it = at.getIterator(); int paragraphStart = it.getBeginIndex(); int paragraphEnd = it.getEndIndex(); LineBreakMeasurer measurer = new LineBreakMeasurer(it, frc); measurer.setPosition(paragraphStart); while (measurer.getPosition() < paragraphEnd) { int startIndex = measurer.getPosition(); int nextBreak = text.indexOf('\n', measurer.getPosition() + 1); if(prStart) measurer.setPosition(startIndex++); TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1, nextBreak == -1 ? paragraphEnd : nextBreak, true); if (textLayout == null) { textLayout = measurer.nextLayout(textWidth, nextBreak == -1 ? paragraphEnd : nextBreak, false); logger.log(POILogger.WARN, "Failed to break text into lines: wrappingWidth: "+wrappingWidth+ "; text: " + rt.getText()); measurer.setPosition(rt.getEndIndex()); continue; int endIndex = measurer.getPosition(); it.setIndex(startIndex); AttributedString bat = new AttributedString(Character.toString(rt.getBulletChar()));
AttributedString attStr = new AttributedString(paragraphs[paragraph]); AttributedCharacterIterator it = attStr.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(it, g2d.getFontRenderContext()); measurer.setPosition(it.getBeginIndex()); while (measurer.getPosition() < it.getEndIndex()) { TextLayout layout = measurer.nextLayout(img.getWidth()-MARGIN*2); if (drawPosY > img.getHeight() - layout.getAscent() - layout.getDescent() - layout.getLeading()) { drawPosY = 0; img = new BufferedImage((int)(
final AttributedString atString = new AttributedString(text); atString.addAttribute(TextAttribute.FONT, getFont()); final AttributedCharacterIterator itr = atString.getIterator(); final LineBreakMeasurer measurer = new LineBreakMeasurer(itr, PPaintContext.RENDER_QUALITY_HIGH_FRC); final float availableWidth; if (constrainWidthToTextWidth) { while (measurer.getPosition() < itr.getEndIndex()) { final TextLayout aTextLayout = computeNextLayout(measurer, availableWidth, nextLineBreakOffset); if (nextLineBreakOffset == measurer.getPosition()) { nextLineBreakOffset = text.indexOf('\n', measurer.getPosition()); if (nextLineBreakOffset == -1) { nextLineBreakOffset = Integer.MAX_VALUE; textHeight += aTextLayout.getAscent(); textHeight += aTextLayout.getDescent() + aTextLayout.getLeading(); textWidth = Math.max(textWidth, aTextLayout.getAdvance());