Java Code Examples for java.awt.font.TextLayout#getJustifiedLayout()
The following examples show how to use
java.awt.font.TextLayout#getJustifiedLayout() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DirectReactionDrawer.java From ReactionDecoder with GNU Lesser General Public License v3.0 | 6 votes |
/** * * @param tree * @param labels * @param g * @return */ public double getMaxLabelHeight(BoundsTree tree, Map<String, String> labels, Graphics2D g) { double maxHeight = 0; Font font = new Font(params.labelPanelFont, PLAIN, params.labelPanelFontSize); FontRenderContext frc = g.getFontRenderContext(); for (String boundsLabel : labels.keySet()) { String label = labels.get(boundsLabel); Rectangle2D bounds = tree.get(boundsLabel); float boundsWidth = (float) bounds.getWidth(); TextLayout textLayout = new TextLayout(label, font, frc); if (boundsWidth <= 0) { continue; // XXX } TextLayout justifiedLayout = textLayout.getJustifiedLayout(boundsWidth); double height = justifiedLayout.getBounds().getHeight(); if (height > maxHeight) { maxHeight = height; } } return maxHeight; }
Example 2
Source File: JMultilineLabel.java From Spark with Apache License 2.0 | 6 votes |
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); }
Example 3
Source File: LineBreakIterator.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 5 votes |
protected TextLayout postProcess( final int start, final TextLayout textLayout, final LineBreakMeasurer lineBreakMeasurer ) { if ( justifiedLayout ) { return textLayout.getJustifiedLayout( wrappingWidth ); } else { return textLayout; } }
Example 4
Source File: DrawTextParagraph.java From lams with GNU General Public License v2.0 | 4 votes |
/** * break text into lines, each representing a line of text that fits in the wrapping width * * @param graphics The drawing context for computing text-lengths. */ protected void breakText(Graphics2D graphics){ lines.clear(); DrawFactory fact = DrawFactory.getInstance(graphics); fact.fixFonts(graphics); StringBuilder text = new StringBuilder(); AttributedString at = getAttributedString(graphics, text); boolean emptyParagraph = ("".equals(text.toString().trim())); AttributedCharacterIterator it = at.getIterator(); LineBreakMeasurer measurer = new LineBreakMeasurer(it, graphics.getFontRenderContext()); for (;;) { int startIndex = measurer.getPosition(); // add a pixel to compensate rounding errors double wrappingWidth = getWrappingWidth(lines.isEmpty(), graphics) + 1; // shape width can be smaller that the sum of insets (this was proved by a test file) if(wrappingWidth < 0) { wrappingWidth = 1; } int nextBreak = text.indexOf("\n", startIndex + 1); if (nextBreak == -1) { nextBreak = it.getEndIndex(); } TextLayout layout = measurer.nextLayout((float)wrappingWidth, nextBreak, true); if (layout == null) { // layout can be null if the entire word at the current position // does not fit within the wrapping width. Try with requireNextWord=false. layout = measurer.nextLayout((float)wrappingWidth, nextBreak, false); } if(layout == null) { // exit if can't break any more break; } int endIndex = measurer.getPosition(); // skip over new line breaks (we paint 'clear' text runs not starting or ending with \n) if(endIndex < it.getEndIndex() && text.charAt(endIndex) == '\n'){ measurer.setPosition(endIndex + 1); } TextAlign hAlign = paragraph.getTextAlign(); if(hAlign == TextAlign.JUSTIFY || hAlign == TextAlign.JUSTIFY_LOW) { layout = layout.getJustifiedLayout((float)wrappingWidth); } AttributedString str = (emptyParagraph) ? null // we will not paint empty paragraphs : new AttributedString(it, startIndex, endIndex); DrawTextFragment line = fact.getTextFragment(layout, str); lines.add(line); maxLineHeight = Math.max(maxLineHeight, line.getHeight()); if(endIndex == it.getEndIndex()) { break; } } rawText = text.toString(); }
Example 5
Source File: SwingUtilities2.java From Bytecoder with Apache License 2.0 | 4 votes |
/** * Draws the string at the specified location underlining the specified * character. * * @param c JComponent that will display the string, may be null * @param g Graphics to draw the text to * @param text String to display * @param underlinedIndex Index of a character in the string to underline * @param x X coordinate to draw the text at * @param y Y coordinate to draw the text at * @param useFPAPI use floating point API */ public static void drawStringUnderlineCharAt(JComponent c, Graphics g, String text, int underlinedIndex, float x, float y, boolean useFPAPI) { if (text == null || text.length() <= 0) { return; } SwingUtilities2.drawString(c, g, text, x, y, useFPAPI); int textLength = text.length(); if (underlinedIndex >= 0 && underlinedIndex < textLength ) { float underlineRectY = y; int underlineRectHeight = 1; float underlineRectX = 0; int underlineRectWidth = 0; boolean isPrinting = isPrinting(g); boolean needsTextLayout = isPrinting; if (!needsTextLayout) { synchronized (charsBufferLock) { syncCharsBuffer(text); needsTextLayout = isComplexLayout(charsBuffer, 0, textLength); } } if (!needsTextLayout) { FontMetrics fm = g.getFontMetrics(); underlineRectX = x + SwingUtilities2.stringWidth(c,fm, text.substring(0,underlinedIndex)); underlineRectWidth = fm.charWidth(text. charAt(underlinedIndex)); } else { Graphics2D g2d = getGraphics2D(g); if (g2d != null) { TextLayout layout = createTextLayout(c, text, g2d.getFont(), g2d.getFontRenderContext()); if (isPrinting) { float screenWidth = (float)g2d.getFont(). getStringBounds(text, getFontRenderContext(c)).getWidth(); // If text fits the screenWidth, then do not need to justify if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(), text) > screenWidth) { layout = layout.getJustifiedLayout(screenWidth); } } TextHitInfo leading = TextHitInfo.leading(underlinedIndex); TextHitInfo trailing = TextHitInfo.trailing(underlinedIndex); Shape shape = layout.getVisualHighlightShape(leading, trailing); Rectangle rect = shape.getBounds(); underlineRectX = x + rect.x; underlineRectWidth = rect.width; } } g.fillRect((int) underlineRectX, (int) underlineRectY + 1, underlineRectWidth, underlineRectHeight); } }
Example 6
Source File: SwingUtilities2.java From Bytecoder with Apache License 2.0 | 4 votes |
public static float drawChars(JComponent c, Graphics g, char[] data, int offset, int length, float x, float y, boolean useFPAPI) { if ( length <= 0 ) { //no need to paint empty strings return x; } float nextX = x + getFontCharsWidth(data, offset, length, getFontMetrics(c, g), useFPAPI); if (isPrinting(g)) { Graphics2D g2d = getGraphics2D(g); if (g2d != null) { FontRenderContext deviceFontRenderContext = g2d. getFontRenderContext(); FontRenderContext frc = getFontRenderContext(c); if (frc != null && !isFontRenderContextPrintCompatible (deviceFontRenderContext, frc)) { String text = new String(data, offset, length); TextLayout layout = new TextLayout(text, g2d.getFont(), deviceFontRenderContext); String trimmedText = trimTrailingSpaces(text); if (!trimmedText.isEmpty()) { float screenWidth = (float)g2d.getFont(). getStringBounds(trimmedText, frc).getWidth(); // If text fits the screenWidth, then do not need to justify if (SwingUtilities2.stringWidth(c, g2d.getFontMetrics(), trimmedText) > screenWidth) { layout = layout.getJustifiedLayout(screenWidth); } /* Use alternate print color if specified */ Color col = g2d.getColor(); if (col instanceof PrintColorUIResource) { g2d.setColor(((PrintColorUIResource)col).getPrintColor()); } layout.draw(g2d,x,y); g2d.setColor(col); } return nextX; } } } // Assume we're not printing if we get here, or that we are invoked // via Swing text printing which is laid out for the printer. Object aaHint = (c == null) ? null : c.getClientProperty(KEY_TEXT_ANTIALIASING); if (!(g instanceof Graphics2D)) { g.drawChars(data, offset, length, (int) x, (int) y); return nextX; } Graphics2D g2 = (Graphics2D) g; if (aaHint != null) { Object oldContrast = null; Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING); if (aaHint != null && aaHint != oldAAValue) { g2.setRenderingHint(KEY_TEXT_ANTIALIASING, aaHint); } else { oldAAValue = null; } Object lcdContrastHint = c.getClientProperty(KEY_TEXT_LCD_CONTRAST); if (lcdContrastHint != null) { oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST); if (lcdContrastHint.equals(oldContrast)) { oldContrast = null; } else { g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, lcdContrastHint); } } g2.drawString(new String(data, offset, length), x, y); if (oldAAValue != null) { g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue); } if (oldContrast != null) { g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast); } } else { g2.drawString(new String(data, offset, length), x, y); } return nextX; }