Java Code Examples for java.awt.font.TextLayout#getLeading()
The following examples show how to use
java.awt.font.TextLayout#getLeading() .
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: MainPanel.java From java-swing-tips with MIT License | 6 votes |
@Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); g2.setPaint(getForeground()); Insets i = getInsets(); float x = i.left; float y = i.top; int w = getWidth() - i.left - i.right; AttributedString as = new AttributedString(getText()); as.addAttribute(TextAttribute.FONT, getFont()); AttributedCharacterIterator aci = as.getIterator(); FontRenderContext frc = g2.getFontRenderContext(); LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); while (lbm.getPosition() < aci.getEndIndex()) { TextLayout tl = lbm.nextLayout(w); tl.draw(g2, x, y + tl.getAscent()); y += tl.getDescent() + tl.getLeading() + tl.getAscent(); } g2.dispose(); }
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: Font.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified {@code FontRenderContext}. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * {@code TextLayout}. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified {@code FontRenderContext} * @return a {@code Rectangle2D} that is the bounding box of the * specified array of characters in the specified * {@code FontRenderContext}. * @throws IndexOutOfBoundsException if {@code beginIndex} is * less than zero, or {@code limit} is greater than the * length of {@code chars}, or {@code beginIndex} * is greater than {@code limit}. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 4
Source File: Font.java From jdk1.8-source-analysis with Apache License 2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified <code>FontRenderContext</code>. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * <code>TextLayout</code>. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified <code>FontRenderContext</code> * @return a <code>Rectangle2D</code> that is the bounding box of the * specified array of characters in the specified * <code>FontRenderContext</code>. * @throws IndexOutOfBoundsException if <code>beginIndex</code> is * less than zero, or <code>limit</code> is greater than the * length of <code>chars</code>, or <code>beginIndex</code> * is greater than <code>limit</code>. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 5
Source File: Font.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified <code>FontRenderContext</code>. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * <code>TextLayout</code>. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified <code>FontRenderContext</code> * @return a <code>Rectangle2D</code> that is the bounding box of the * specified array of characters in the specified * <code>FontRenderContext</code>. * @throws IndexOutOfBoundsException if <code>beginIndex</code> is * less than zero, or <code>limit</code> is greater than the * length of <code>chars</code>, or <code>beginIndex</code> * is greater than <code>limit</code>. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 6
Source File: Font.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified <code>FontRenderContext</code>. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * <code>TextLayout</code>. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified <code>FontRenderContext</code> * @return a <code>Rectangle2D</code> that is the bounding box of the * specified array of characters in the specified * <code>FontRenderContext</code>. * @throws IndexOutOfBoundsException if <code>beginIndex</code> is * less than zero, or <code>limit</code> is greater than the * length of <code>chars</code>, or <code>beginIndex</code> * is greater than <code>limit</code>. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 7
Source File: Font.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified <code>FontRenderContext</code>. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * <code>TextLayout</code>. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified <code>FontRenderContext</code> * @return a <code>Rectangle2D</code> that is the bounding box of the * specified array of characters in the specified * <code>FontRenderContext</code>. * @throws IndexOutOfBoundsException if <code>beginIndex</code> is * less than zero, or <code>limit</code> is greater than the * length of <code>chars</code>, or <code>beginIndex</code> * is greater than <code>limit</code>. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 8
Source File: Font.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified <code>FontRenderContext</code>. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * <code>TextLayout</code>. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified <code>FontRenderContext</code> * @return a <code>Rectangle2D</code> that is the bounding box of the * specified array of characters in the specified * <code>FontRenderContext</code>. * @throws IndexOutOfBoundsException if <code>beginIndex</code> is * less than zero, or <code>limit</code> is greater than the * length of <code>chars</code>, or <code>beginIndex</code> * is greater than <code>limit</code>. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 9
Source File: DefaultProcessDiagramCanvas.java From flowable-engine with Apache License 2.0 | 5 votes |
public void drawLabel(String text, GraphicInfo graphicInfo, boolean centered) { float interline = 1.0f; // text if (text != null && text.length() > 0) { Paint originalPaint = g.getPaint(); Font originalFont = g.getFont(); g.setPaint(LABEL_COLOR); g.setFont(LABEL_FONT); int wrapWidth = 100; double textY = graphicInfo.getY(); // TODO: use drawMultilineText() AttributedString as = new AttributedString(text); as.addAttribute(TextAttribute.FOREGROUND, g.getPaint()); as.addAttribute(TextAttribute.FONT, g.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(wrapWidth); textY += tl.getAscent(); Rectangle2D bb = tl.getBounds(); double tX = graphicInfo.getX(); if (centered) { tX += (int) (graphicInfo.getWidth() / 2 - bb.getWidth() / 2); } tl.draw(g, (float) tX, (float) textY); textY += tl.getDescent() + tl.getLeading() + (interline - 1.0f) * tl.getAscent(); } // restore originals g.setFont(originalFont); g.setPaint(originalPaint); } }
Example 10
Source File: FreeColToolTipUI.java From freecol with GNU General Public License v2.0 | 5 votes |
@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(); } } }
Example 11
Source File: Font.java From jdk-1.7-annotated with Apache License 2.0 | 5 votes |
/** * Returns the logical bounds of the specified array of characters * in the specified <code>FontRenderContext</code>. The logical * bounds contains the origin, ascent, advance, and height, which * includes the leading. The logical bounds does not always enclose * all the text. For example, in some languages and in some fonts, * accent marks can be positioned above the ascent or below the * descent. To obtain a visual bounding box, which encloses all the * text, use the {@link TextLayout#getBounds() getBounds} method of * <code>TextLayout</code>. * <p>Note: The returned bounds is in baseline-relative coordinates * (see {@link java.awt.Font class notes}). * @param chars an array of characters * @param beginIndex the initial offset in the array of * characters * @param limit the end offset in the array of characters * @param frc the specified <code>FontRenderContext</code> * @return a <code>Rectangle2D</code> that is the bounding box of the * specified array of characters in the specified * <code>FontRenderContext</code>. * @throws IndexOutOfBoundsException if <code>beginIndex</code> is * less than zero, or <code>limit</code> is greater than the * length of <code>chars</code>, or <code>beginIndex</code> * is greater than <code>limit</code>. * @see FontRenderContext * @see Font#createGlyphVector * @since 1.2 */ public Rectangle2D getStringBounds(char [] chars, int beginIndex, int limit, FontRenderContext frc) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex: " + beginIndex); } if (limit > chars.length) { throw new IndexOutOfBoundsException("limit: " + limit); } if (beginIndex > limit) { throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex)); } // this code should be in textlayout // quick check for simple text, assume GV ok to use if simple boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0 && values.getBaselineTransform() == null); if (simple) { simple = ! FontUtilities.isComplexText(chars, beginIndex, limit); } if (simple) { GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc); return gv.getLogicalBounds(); } else { // need char array constructor on textlayout String str = new String(chars, beginIndex, limit - beginIndex); TextLayout tl = new TextLayout(str, this, frc); return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); } }
Example 12
Source File: ProcessDiagramCanvas.java From activiti-in-action-codes with Apache License 2.0 | 4 votes |
protected void drawMultilineText(String text, int x, int y, int boxWidth, int boxHeight) { int availableHeight = boxHeight - ICON_SIZE - ICON_PADDING; // Create an attributed string based in input text AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, g.getFont()); attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black); AttributedCharacterIterator characterIterator = attributedString.getIterator(); int width = boxWidth - (2 * TEXT_PADDING); int currentHeight = 0; // Prepare a list of lines of text we'll be drawing List<TextLayout> layouts = new ArrayList<TextLayout>(); String lastLine = null; LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext()); TextLayout layout = null; while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= availableHeight) { int previousPosition = measurer.getPosition(); // Request next layout layout = measurer.nextLayout(width); int height = ((Float) (layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue(); if (currentHeight + height > availableHeight) { // The line we're about to add should NOT be added anymore, append three dots to previous one instead // to indicate more text is truncated layouts.remove(layouts.size() - 1); if (lastLine.length() >= 4) { lastLine = lastLine.substring(0, lastLine.length() - 4) + "..."; } layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext())); } else { layouts.add(layout); lastLine = text.substring(previousPosition, measurer.getPosition()); currentHeight += height; } } int currentY = y + ICON_SIZE + ICON_PADDING + ((availableHeight - currentHeight) / 2); int currentX = 0; // Actually draw the lines for (TextLayout textLayout : layouts) { currentY += textLayout.getAscent(); currentX = TEXT_PADDING + x + ((width - ((Double) textLayout.getBounds().getWidth()).intValue()) / 2); textLayout.draw(g, currentX, currentY); currentY += textLayout.getDescent() + textLayout.getLeading(); } }
Example 13
Source File: DefaultCaseDiagramCanvas.java From flowable-engine with Apache License 2.0 | 4 votes |
protected void drawMultilineText(String text, int x, int y, int boxWidth, int boxHeight, boolean centered) { // Create an attributed string based in input text AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, g.getFont()); attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black); AttributedCharacterIterator characterIterator = attributedString.getIterator(); int currentHeight = 0; // Prepare a list of lines of text we'll be drawing List<TextLayout> layouts = new ArrayList<>(); String lastLine = null; LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext()); TextLayout layout = null; while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= boxHeight) { int previousPosition = measurer.getPosition(); // Request next layout layout = measurer.nextLayout(boxWidth); int height = ((Float) (layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue(); if (currentHeight + height > boxHeight) { // The line we're about to add should NOT be added anymore, append three dots to previous one instead // to indicate more text is truncated if (!layouts.isEmpty()) { layouts.remove(layouts.size() - 1); if (lastLine.length() >= 4) { lastLine = lastLine.substring(0, lastLine.length() - 4) + "..."; } layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext())); } break; } else { layouts.add(layout); lastLine = text.substring(previousPosition, measurer.getPosition()); currentHeight += height; } } int currentY = y + (centered ? ((boxHeight - currentHeight) / 2) : 0); int currentX = 0; // Actually draw the lines for (TextLayout textLayout : layouts) { currentY += textLayout.getAscent(); currentX = x + (centered ? ((boxWidth - ((Double) textLayout.getBounds().getWidth()).intValue()) / 2) : 0); textLayout.draw(g, currentX, currentY); currentY += textLayout.getDescent() + textLayout.getLeading(); } }
Example 14
Source File: AttrStringUtils.java From ccu-historian with GNU General Public License v3.0 | 4 votes |
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2, AttributedString text, TextAnchor anchor, Rectangle2D textBounds) { TextLayout layout = new TextLayout(text.getIterator(), g2.getFontRenderContext()); Rectangle2D bounds = layout.getBounds(); float[] result = new float[3]; float ascent = layout.getAscent(); result[2] = -ascent; float halfAscent = ascent / 2.0f; float descent = layout.getDescent(); float leading = layout.getLeading(); float xAdj = 0.0f; float yAdj = 0.0f; if (isHorizontalCenter(anchor)) { xAdj = (float) -bounds.getWidth() / 2.0f; } else if (isHorizontalRight(anchor)) { xAdj = (float) -bounds.getWidth(); } if (isTop(anchor)) { //yAdj = -descent - leading + (float) bounds.getHeight(); yAdj = (float) bounds.getHeight(); } else if (isHalfAscent(anchor)) { yAdj = halfAscent; } else if (isHalfHeight(anchor)) { yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0); } else if (isBaseline(anchor)) { yAdj = 0.0f; } else if (isBottom(anchor)) { yAdj = -descent - leading; } if (textBounds != null) { textBounds.setRect(bounds); } result[0] = xAdj; result[1] = yAdj; return result; }
Example 15
Source File: TextBounds.java From ramus with GNU General Public License v3.0 | 4 votes |
public void drawNative(Graphics2D g, int align, int pos, float fRectHeight, MovingArea area) { float left = 0; float fromY = 0; if (pos == 1) fromY += fRectHeight / 2 - height / 2; else if (pos == 2) fromY += fRectHeight; if (fromY < 0) fromY = 0; int size = layouts.size(); for (int i = 0; i < size; i++) { TextLayout textLayout = layouts.get(i); if (align == Line.CENTER_ALIGN) left = (float) maxWidth / 2f - textLayout.getAdvance() / 2f; else if (align == Line.RIGHT_ALIGN) left = (float) maxWidth - textLayout.getAdvance(); fromY += textLayout.getAscent(); if (fromY >= fRectHeight && i + 1 < size) break; if (fromY + textLayout.getAscent() + textLayout.getDescent() // + textLayout.getLeading() >= fRectHeight && i + 1 < size) { // textLayout= textLayout.getJustifiedLayout(maxWidth-20); // textLayout.draw(g, left, fromY); g.drawString(texts.get(i), left, fromY); /* * Color color = g.getColor(); g.setColor(new Color(120, 120, * 120)); * * g.drawString("(.)", left + textLayout.getAdvance() + (float) * area.getIDoubleOrdinate(-0.0), fromY); g.setColor(color); */ break; } else g.drawString(texts.get(i), left, fromY); // textLayout.draw(g, left, fromY); fromY += textLayout.getDescent() + textLayout.getLeading(); } }
Example 16
Source File: AttrStringUtils.java From SIMVA-SoS with Apache License 2.0 | 4 votes |
/** * A utility method that calculates the rotation anchor offsets for a * string. These offsets are relative to the text starting coordinate * (BASELINE_LEFT). * * @param g2 the graphics device. * @param text the text. * @param anchor the anchor point. * * @return The offsets. */ private static float[] deriveRotationAnchorOffsets(Graphics2D g2, AttributedString text, TextAnchor anchor) { float[] result = new float[2]; TextLayout layout = new TextLayout(text.getIterator(), g2.getFontRenderContext()); Rectangle2D bounds = layout.getBounds(); float ascent = layout.getAscent(); float halfAscent = ascent / 2.0f; float descent = layout.getDescent(); float leading = layout.getLeading(); float xAdj = 0.0f; float yAdj = 0.0f; if (isHorizontalLeft(anchor)) { xAdj = 0.0f; } else if (isHorizontalCenter(anchor)) { xAdj = (float) bounds.getWidth() / 2.0f; } else if (isHorizontalRight(anchor)) { xAdj = (float) bounds.getWidth(); } if (isTop(anchor)) { yAdj = descent + leading - (float) bounds.getHeight(); } else if (isHalfHeight(anchor)) { yAdj = descent + leading - (float) (bounds.getHeight() / 2.0); } else if (isHalfAscent(anchor)) { yAdj = -halfAscent; } else if (isBaseline(anchor)) { yAdj = 0.0f; } else if (isBottom(anchor)) { yAdj = descent + leading; } result[0] = xAdj; result[1] = yAdj; return result; }
Example 17
Source File: AttrStringUtils.java From buffer_bci with GNU General Public License v3.0 | 4 votes |
/** * A utility method that calculates the rotation anchor offsets for a * string. These offsets are relative to the text starting coordinate * (BASELINE_LEFT). * * @param g2 the graphics device. * @param text the text. * @param anchor the anchor point. * * @return The offsets. */ private static float[] deriveRotationAnchorOffsets(Graphics2D g2, AttributedString text, TextAnchor anchor) { float[] result = new float[2]; TextLayout layout = new TextLayout(text.getIterator(), g2.getFontRenderContext()); Rectangle2D bounds = layout.getBounds(); float ascent = layout.getAscent(); float halfAscent = ascent / 2.0f; float descent = layout.getDescent(); float leading = layout.getLeading(); float xAdj = 0.0f; float yAdj = 0.0f; if (isHorizontalLeft(anchor)) { xAdj = 0.0f; } else if (isHorizontalCenter(anchor)) { xAdj = (float) bounds.getWidth() / 2.0f; } else if (isHorizontalRight(anchor)) { xAdj = (float) bounds.getWidth(); } if (isTop(anchor)) { yAdj = descent + leading - (float) bounds.getHeight(); } else if (isHalfHeight(anchor)) { yAdj = descent + leading - (float) (bounds.getHeight() / 2.0); } else if (isHalfAscent(anchor)) { yAdj = -halfAscent; } else if (isBaseline(anchor)) { yAdj = 0.0f; } else if (isBottom(anchor)) { yAdj = descent + leading; } result[0] = xAdj; result[1] = yAdj; return result; }
Example 18
Source File: DefaultProcessDiagramCanvas.java From activiti6-boot2 with Apache License 2.0 | 4 votes |
protected void drawMultilineText(String text, int x, int y, int boxWidth, int boxHeight, boolean centered) { // Create an attributed string based in input text AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, g.getFont()); attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black); AttributedCharacterIterator characterIterator = attributedString.getIterator(); int currentHeight = 0; // Prepare a list of lines of text we'll be drawing List<TextLayout> layouts = new ArrayList<TextLayout>(); String lastLine = null; LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext()); TextLayout layout = null; while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= boxHeight) { int previousPosition = measurer.getPosition(); // Request next layout layout = measurer.nextLayout(boxWidth); int height = ((Float)(layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue(); if(currentHeight + height > boxHeight) { // The line we're about to add should NOT be added anymore, append three dots to previous one instead // to indicate more text is truncated if (!layouts.isEmpty()) { layouts.remove(layouts.size() - 1); if(lastLine.length() >= 4) { lastLine = lastLine.substring(0, lastLine.length() - 4) + "..."; } layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext())); } break; } else { layouts.add(layout); lastLine = text.substring(previousPosition, measurer.getPosition()); currentHeight += height; } } int currentY = y + (centered ? ((boxHeight - currentHeight) /2) : 0); int currentX = 0; // Actually draw the lines for(TextLayout textLayout : layouts) { currentY += textLayout.getAscent(); currentX = x + (centered ? ((boxWidth - ((Double)textLayout.getBounds().getWidth()).intValue()) /2) : 0); textLayout.draw(g, currentX, currentY); currentY += textLayout.getDescent() + textLayout.getLeading(); } }
Example 19
Source File: AttrStringUtils.java From ECG-Viewer with GNU General Public License v2.0 | 4 votes |
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2, AttributedString text, TextAnchor anchor, Rectangle2D textBounds) { TextLayout layout = new TextLayout(text.getIterator(), g2.getFontRenderContext()); Rectangle2D bounds = layout.getBounds(); float[] result = new float[3]; float ascent = layout.getAscent(); result[2] = -ascent; float halfAscent = ascent / 2.0f; float descent = layout.getDescent(); float leading = layout.getLeading(); float xAdj = 0.0f; float yAdj = 0.0f; if (isHorizontalCenter(anchor)) { xAdj = (float) -bounds.getWidth() / 2.0f; } else if (isHorizontalRight(anchor)) { xAdj = (float) -bounds.getWidth(); } if (isTop(anchor)) { //yAdj = -descent - leading + (float) bounds.getHeight(); yAdj = (float) bounds.getHeight(); } else if (isHalfAscent(anchor)) { yAdj = halfAscent; } else if (isHalfHeight(anchor)) { yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0); } else if (isBaseline(anchor)) { yAdj = 0.0f; } else if (isBottom(anchor)) { yAdj = -descent - leading; } if (textBounds != null) { textBounds.setRect(bounds); } result[0] = xAdj; result[1] = yAdj; return result; }
Example 20
Source File: ProcessDiagramCanvas.java From activiti-in-action-codes with Apache License 2.0 | 4 votes |
protected void drawMultilineText(String text, int x, int y, int boxWidth, int boxHeight) { int availableHeight = boxHeight - ICON_SIZE - ICON_PADDING; // Create an attributed string based in input text AttributedString attributedString = new AttributedString(text); attributedString.addAttribute(TextAttribute.FONT, g.getFont()); attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black); AttributedCharacterIterator characterIterator = attributedString.getIterator(); int width = boxWidth - (2 * TEXT_PADDING); int currentHeight = 0; // Prepare a list of lines of text we'll be drawing List<TextLayout> layouts = new ArrayList<TextLayout>(); String lastLine = null; LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, g.getFontRenderContext()); TextLayout layout = null; while (measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= availableHeight) { int previousPosition = measurer.getPosition(); // Request next layout layout = measurer.nextLayout(width); int height = ((Float) (layout.getDescent() + layout.getAscent() + layout.getLeading())).intValue(); if (currentHeight + height > availableHeight) { // The line we're about to add should NOT be added anymore, append three dots to previous one instead // to indicate more text is truncated layouts.remove(layouts.size() - 1); if (lastLine.length() >= 4) { lastLine = lastLine.substring(0, lastLine.length() - 4) + "..."; } layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext())); } else { layouts.add(layout); lastLine = text.substring(previousPosition, measurer.getPosition()); currentHeight += height; } } int currentY = y + ICON_SIZE + ICON_PADDING + ((availableHeight - currentHeight) / 2); int currentX = 0; // Actually draw the lines for (TextLayout textLayout : layouts) { currentY += textLayout.getAscent(); currentX = TEXT_PADDING + x + ((width - ((Double) textLayout.getBounds().getWidth()).intValue()) / 2); textLayout.draw(g, currentX, currentY); currentY += textLayout.getDescent() + textLayout.getLeading(); } }