Java Code Examples for java.awt.font.TextLayout#getDescent()
The following examples show how to use
java.awt.font.TextLayout#getDescent() .
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: MainPanel.java From java-swing-tips with MIT License | 6 votes |
@Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(getForeground()); g2.setFont(getFont()); SwingUtilities.calculateInnerArea(this, RECT); float x = RECT.x; float y = RECT.y; int w = RECT.width; AttributedString as = new AttributedString(getText()); as.addAttribute(TextAttribute.FONT, getFont()); // TEST: .deriveFont(at)); // TEST: as.addAttribute(TextAttribute.TRANSFORM, at); 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 3
Source File: Font.java From Java8CN 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 4
Source File: TemplatePreviewPanel.java From nextreports-designer with Apache License 2.0 | 5 votes |
private void drawText(Graphics2D g2, BandElement band, FontRenderContext frc, int w, int rowHeight, int titleDelta, int rowPos, String text) { TextLayout detailLayout = new TextLayout(text, band.getFont(), frc); float detailHeight = detailLayout.getAscent() + detailLayout.getDescent(); int detailWidth = g2.getFontMetrics(band.getFont()).stringWidth(text); for (int i = 0; i < columns; i++) { int alignH = band.getHorizontalAlign(); //System.out.println("alignH=" + alignH); int x0H; // left if (alignH == 2) { x0H = i * w / columns + 5; // right } else if (alignH == 4) { x0H = (i + 1) * w / columns - detailWidth - 5; // center } else { x0H = i * w / columns + (w / columns - detailWidth) / 2; } int y0H = titleDelta + (int) ((rowPos * rowHeight + detailHeight) / 2); //System.out.println("x0H=" + x0H + " y0H=" + y0H); AffineTransform atH = AffineTransform.getTranslateInstance(x0H, y0H); Shape outlineH = detailLayout.getOutline(atH); g2.setColor(band.getForeground()); g2.fill(outlineH); } }
Example 5
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 6
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 7
Source File: Font.java From jdk8u-dev-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 8
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 9
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 10
Source File: AttrStringUtils.java From ccu-historian 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 11
Source File: AttrStringUtils.java From buffer_bci 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 12
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 13
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 14
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 15
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 16
Source File: AttrStringUtils.java From SIMVA-SoS with Apache License 2.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 17
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 18
Source File: AttrStringUtils.java From openstock 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 19
Source File: TextBoxView.java From SwingBox with GNU Lesser General Public License v3.0 | 2 votes |
/** * Gets the string bounds. * * @param tl * textlayout instance * @return the string bounds */ protected Rectangle2D getStringBounds(TextLayout tl) { return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(), tl.getAscent() + tl.getDescent() + tl.getLeading()); }
Example 20
Source File: AvatarChooser.java From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License | votes |
private void drawAvatarText(Graphics2D g2, double y, double bulletHeight) { FontRenderContext context = g2.getFontRenderContext(); Font font = new Font("Dialog", Font.PLAIN, 18); TextLayout layout = new TextLayout(textAvatar, font, context); Rectangle2D bounds = layout.getBounds(); float text_x = (float) ((getWidth() - bounds.getWidth()) / 2.0); float text_y = (float) (y + (bulletHeight - layout.getAscent() - layout.getDescent()) / 2.0) + layout.getAscent() - layout.getLeading(); g2.setColor(Color.BLACK); layout.draw(g2, text_x, text_y + 1); g2.setColor(Color.WHITE); layout.draw(g2, text_x, text_y); }