Search in sources :

Example 56 with TextLayout

use of java.awt.font.TextLayout in project binnavi by google.

the class ZyLabelContent method draw.

public void draw(final Graphics2D gfx, final double xpos, final double ypos) {
    if (m_sloppy) {
        return;
    }
    if (!m_selectable || !m_showCaret) {
        final float x = (float) xpos + m_leftPadding;
        float y = (float) ypos + m_topPadding + FONTSIZE;
        for (final ZyLineContent line : getContent()) {
            line.draw(gfx, x, y);
            y += LINEHEIGHT;
        }
        return;
    }
    final AffineTransform affineTrans = gfx.getTransform();
    final Color color = gfx.getColor();
    final float x = (float) xpos + getPaddingLeft();
    final float y = (float) ypos + getPaddingTop() + getFontSize();
    final double lineheight = getLineHeight();
    gfx.setColor(Color.BLACK);
    for (int linenr = 0; linenr < getLineCount(); ++linenr) {
        final TextLayout textLayout = getLineContent(linenr).getTextLayout();
        if (!getLineContent(linenr).isEmpty()) {
            textLayout.draw(gfx, x, (float) (y + (linenr * lineheight)));
        }
        if (linenr == m_caret.getYmousePressed()) {
            gfx.setTransform(calcSelectionTransformationMatrix(affineTrans, x, y, linenr));
            if (getLineContent(linenr).isEmpty()) {
                m_caret.setCaretStartPos(0);
            } else if (m_caret.getCaretStartPos() >= textLayout.getCharacterCount()) {
                m_caret.setCaretStartPos(textLayout.getCharacterCount());
            }
            final Shape[] startCarets = textLayout.getCaretShapes(m_caret.getCaretStartPos());
            gfx.setColor(m_selectionColor.darker());
            gfx.draw(startCarets[0]);
            gfx.setTransform(affineTrans);
        }
        if (linenr == m_caret.getYmouseReleased()) {
            gfx.setTransform(calcSelectionTransformationMatrix(affineTrans, x, y, linenr));
            if (getLineContent(linenr).isEmpty()) {
                m_caret.setCaretEndPos(0);
            } else if (m_caret.getCaretEndPos() >= textLayout.getCharacterCount()) {
                m_caret.setCaretEndPos(textLayout.getCharacterCount());
            }
            final Shape[] endCarets = textLayout.getCaretShapes(m_caret.getCaretEndPos());
            gfx.setColor(m_selectionColor.darker().darker());
            gfx.draw(endCarets[0]);
            gfx.setTransform(affineTrans);
        }
    }
    if (m_caret.getXmousePressed() != m_caret.getXmouseReleased()) {
        int pl = m_caret.getYmousePressed();
        int rl = m_caret.getYmouseReleased();
        if (pl > rl) {
            pl = m_caret.getYmouseReleased();
            rl = m_caret.getYmousePressed();
        }
        int pp = m_caret.getXmousePressed();
        int rp = m_caret.getXmouseReleased();
        if (pp > rp) {
            pp = m_caret.getXmouseReleased();
            rp = m_caret.getXmousePressed();
        }
        gfx.setColor(m_selectionColor);
        gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        for (int linenr = pl; linenr <= rl; ++linenr) {
            gfx.setTransform(calcSelectionTransformationMatrix(affineTrans, x, y, linenr));
            final TextLayout textLayout = getLineContent(linenr).getTextLayout();
            int rp2 = rp;
            if (pp > textLayout.getCharacterCount()) {
                continue;
            }
            if (getLineContent(linenr).isEmpty()) {
                rp2 = pp;
            } else if (rp > textLayout.getCharacterCount()) {
                rp2 = textLayout.getCharacterCount();
            }
            final Shape selection = textLayout.getLogicalHighlightShape(pp, rp2);
            gfx.fill(selection);
        }
        gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        gfx.setTransform(affineTrans);
    }
    gfx.setColor(color);
    gfx.setTransform(affineTrans);
}
Also used : Shape(java.awt.Shape) Color(java.awt.Color) AffineTransform(java.awt.geom.AffineTransform) TextLayout(java.awt.font.TextLayout)

Example 57 with TextLayout

use of java.awt.font.TextLayout in project binnavi by google.

the class ZyLineContent method setBackgroundColor.

/**
   * Changes the background color of a part of the line.
   * 
   * @param position The index of the first character that gets the new background color.
   * @param length Number of characters that get the new background color.
   * @param color The new background color.
   */
public void setBackgroundColor(final int position, final int length, final Color color) {
    if (!isEmpty()) {
        final int realLength = calculateRealLength(position, length);
        validatePartialLineArguments(position, realLength);
        // needs to be tested: if color = null, no text background is set? Thats what i expect!
        m_atext.addAttribute(TextAttribute.BACKGROUND, color, position, position + realLength);
        m_textLayout = new TextLayout(m_atext.getIterator(), m_fontContext);
    }
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 58 with TextLayout

use of java.awt.font.TextLayout in project binnavi by google.

the class ZyLineContent method setFont.

/**
   * Changes the font that is used to display part of the line.
   * 
   * Note that the font must be a monospaced font.
   * 
   * @param position The index of the first character that gets the new font.
   * @param length Number of characters that get the new font.
   * @param font The new font to be used.
   */
public void setFont(final int position, final int length, final Font font) {
    Preconditions.checkNotNull(font, "Error: Font argument can't be null");
    if (!isEmpty()) {
        final int realLength = calculateRealLength(position, length);
        validatePartialLineArguments(position, realLength);
        Preconditions.checkNotNull(font, "Error: Font argument can't be null");
        m_atext.addAttribute(TextAttribute.FONT, font, position, position + realLength);
        m_textLayout = new TextLayout(m_atext.getIterator(), m_fontContext);
        updateCharBounds(font);
    }
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 59 with TextLayout

use of java.awt.font.TextLayout in project binnavi by google.

the class ZyLineContent method setTextColor.

/**
   * Changes the text color of a part of the line.
   * 
   * @param position The index of the first character that gets the new font color.
   * @param length Number of characters that get the new font color.
   * @param color The new text color.
   */
public void setTextColor(final int position, final int length, final Color color) {
    if (!isEmpty()) {
        final int realLength = calculateRealLength(position, length);
        validatePartialLineArguments(position, realLength);
        m_atext.addAttribute(TextAttribute.FOREGROUND, color, position, position + realLength);
        m_textLayout = new TextLayout(m_atext.getIterator(), m_fontContext);
    }
}
Also used : TextLayout(java.awt.font.TextLayout)

Example 60 with TextLayout

use of java.awt.font.TextLayout in project binnavi by google.

the class ZyLineContent method regenerateLine.

private void regenerateLine(final String text, final Font font, final List<CStyleRunData> textColorStyleRun) {
    m_text = Preconditions.checkNotNull(text, "Error: text argument can not be null");
    Preconditions.checkNotNull(textColorStyleRun, "Error: textColorStyleRun argument can not be null");
    m_atext = new AttributedString(text);
    if (!isEmpty()) {
        if (font != null) {
            m_atext.addAttribute(TextAttribute.FONT, font);
        }
        // After the line is created we can process the accumulated style information.
        for (final CStyleRunData data : textColorStyleRun) {
            final int position = data.getStart();
            final int realLength = calculateRealLength(position, data.getLength());
            validatePartialLineArguments(position, realLength);
            m_atext.addAttribute(TextAttribute.FOREGROUND, data.getColor(), position, position + realLength);
            if (data.getLineObject() != null) {
                m_lineObjects.add(data.getLineObject());
            }
            if (data.getObject() != null) {
                setObject(position, realLength, data.getObject());
            }
        }
        m_textLayout = new TextLayout(m_atext.getIterator(), m_fontContext);
    }
    if (font != null) {
        updateCharBounds(font);
    }
}
Also used : AttributedString(java.text.AttributedString) TextLayout(java.awt.font.TextLayout)

Aggregations

TextLayout (java.awt.font.TextLayout)108 AttributedString (java.text.AttributedString)32 Graphics2D (java.awt.Graphics2D)25 FontRenderContext (java.awt.font.FontRenderContext)25 Font (java.awt.Font)20 AttributedCharacterIterator (java.text.AttributedCharacterIterator)17 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 Point (java.awt.Point)11 Rectangle (java.awt.Rectangle)10 Rectangle2D (java.awt.geom.Rectangle2D)10 Color (java.awt.Color)9 Paint (java.awt.Paint)8 AffineTransform (java.awt.geom.AffineTransform)8 Shape (java.awt.Shape)7 TextLayoutInfo (g4p_controls.StyledString.TextLayoutInfo)5 Dimension (java.awt.Dimension)5 TextHitInfo (java.awt.font.TextHitInfo)4 ArrayList (java.util.ArrayList)4 Graphics (java.awt.Graphics)3 Insets (java.awt.Insets)3