Search in sources :

Example 61 with TextLayout

use of java.awt.font.TextLayout in project processing by processing.

the class CompositionTextManager method getTextLayout.

private TextLayout getTextLayout(AttributedCharacterIterator text, int committedCount) {
    boolean antialias = Preferences.getBoolean("editor.smooth");
    TextAreaPainter painter = textArea.getPainter();
    // create attributed string with font info.
    AttributedString composed = new AttributedString(text, committedCount, text.getEndIndex());
    Font font = painter.getFontMetrics().getFont();
    composed.addAttribute(TextAttribute.FONT, font);
    // set hint of antialiasing to render target.
    Graphics2D g2d = (Graphics2D) painter.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    FontRenderContext frc = g2d.getFontRenderContext();
    Messages.log("debug: FontRenderContext is Antialiased = " + frc.getAntiAliasingHint());
    return new TextLayout(composed.getIterator(), frc);
}
Also used : AttributedString(java.text.AttributedString) TextAreaPainter(processing.app.syntax.TextAreaPainter) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 62 with TextLayout

use of java.awt.font.TextLayout in project processing by processing.

the class InputMethodSupport method getTextLayout.

private TextLayout getTextLayout(AttributedCharacterIterator text, int committedCount) {
    boolean antialias = Preferences.getBoolean("editor.smooth");
    TextAreaPainter painter = textArea.getPainter();
    // create attributed string with font info.
    if (text.getEndIndex() - (text.getBeginIndex() + committedCount) > 0) {
        composedTextString = new AttributedString(text, committedCount, text.getEndIndex(), CUSTOM_IM_ATTRIBUTES);
        Font font = painter.getFontMetrics().getFont();
        TextAreaDefaults defaults = textArea.getDefaults();
        Color bgColor = defaults.lineHighlight ? defaults.lineHighlightColor : defaults.bgcolor;
        composedTextString.addAttribute(TextAttribute.FONT, font);
        composedTextString.addAttribute(TextAttribute.FOREGROUND, defaults.fgcolor);
        composedTextString.addAttribute(TextAttribute.BACKGROUND, bgColor);
    } else {
        composedTextString = new AttributedString("");
        return null;
    }
    // set hint of antialiasing to render target.
    Graphics2D g2d = (Graphics2D) painter.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    FontRenderContext frc = g2d.getFontRenderContext();
    if (Base.DEBUG) {
        Messages.log("debug: FontRenderContext is Antialiased = " + frc.getAntiAliasingHint());
    }
    return new TextLayout(composedTextString.getIterator(), frc);
}
Also used : TextAreaDefaults(processing.app.syntax.TextAreaDefaults) AttributedString(java.text.AttributedString) TextAreaPainter(processing.app.syntax.TextAreaPainter) Color(java.awt.Color) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 63 with TextLayout

use of java.awt.font.TextLayout in project hid-serial by rayshobby.

the class GButton method updateBuffer.

protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        bufferInvalid = false;
        buffer.beginDraw();
        // Back ground colour
        switch(status) {
            case OVER_CONTROL:
                buffer.background(palette[6]);
                break;
            case PRESS_CONTROL:
                buffer.background(palette[14]);
                break;
            default:
                buffer.background(palette[4]);
        }
        g2d.setColor(jpalette[3]);
        g2d.setStroke(pen_1_0);
        g2d.drawRect(0, 0, (int) width - 1, (int) height - 1);
        // Calculate text and icon placement
        calcAlignment();
        // If there is an icon draw it
        if (iconW != 0)
            buffer.image(bicon[status], siX, siY);
        float wrapWidth = stext.getWrapWidth();
        float sx = 0, tw = 0;
        buffer.translate(stX, stY);
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            switch(textAlignH) {
                case CENTER:
                    tw = layout.getVisibleAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = (wrapWidth - tw) / 2;
                    break;
                case RIGHT:
                    tw = layout.getVisibleAdvance();
                    tw = (tw > wrapWidth) ? tw - wrapWidth : tw;
                    sx = wrapWidth - tw;
                    break;
                case LEFT:
                case JUSTIFY:
                default:
                    sx = 0;
            }
            // display text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, sx, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        buffer.endDraw();
    }
}
Also used : TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 64 with TextLayout

use of java.awt.font.TextLayout in project hid-serial by rayshobby.

the class GDropList method updateBuffer.

protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        bufferInvalid = false;
        buffer.beginDraw();
        buffer.background(buffer.color(255, 0));
        buffer.noStroke();
        buffer.fill(palette[BACK_COLOR]);
        buffer.rect(0, 0, width, itemHeight);
        if (expanded) {
            buffer.fill(palette[ITEM_BACK_COLOR]);
            buffer.rect(0, itemHeight, width, itemHeight * dropListActualSize);
        }
        float px = TPAD, py;
        TextLayout line;
        // Get selected text for display
        line = selText.getLines(g2d).getFirst().layout;
        py = (itemHeight + line.getAscent() - line.getDescent()) / 2;
        g2d.setColor(jpalette[FORE_COLOR]);
        line.draw(g2d, px, py);
        if (expanded) {
            g2d.setColor(jpalette[ITEM_FORE_COLOR]);
            for (int i = 0; i < dropListActualSize; i++) {
                py += itemHeight;
                if (currOverItem == startItem + i)
                    g2d.setColor(jpalette[OVER_ITEM_FORE_COLOR]);
                else
                    g2d.setColor(jpalette[ITEM_FORE_COLOR]);
                line = sitems[startItem + i].getLines(g2d).getFirst().layout;
                line.draw(g2d, px, py);
            }
        }
        buffer.endDraw();
    }
}
Also used : Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 65 with TextLayout

use of java.awt.font.TextLayout in project hid-serial by rayshobby.

the class GTextArea method updateBuffer.

/**
	 * Add text to the end of the current text. This is useful for a logging' type activity.
	 * 
	 * @param extraText the text to append
	 */
//	public void appendText(String extraText){
//		appendText(extraText, false);
//	}
/**
	 * If the buffer is invalid then redraw it.
	 */
protected void updateBuffer() {
    if (bufferInvalid) {
        Graphics2D g2d = buffer.g2;
        // Get the latest lines of text
        LinkedList<TextLayoutInfo> lines = stext.getLines(g2d);
        if (lines.isEmpty() && defaultText != null)
            lines = defaultText.getLines(g2d);
        bufferInvalid = false;
        TextLayoutHitInfo startSelTLHI = null, endSelTLHI = null;
        buffer.beginDraw();
        // Whole control surface if opaque
        if (opaque)
            buffer.background(palette[6]);
        else
            buffer.background(buffer.color(255, 0));
        // Now move to top left corner of text display area
        buffer.translate(tx, ty);
        // Typing area surface
        buffer.noStroke();
        buffer.fill(palette[7]);
        buffer.rect(-1, -1, tw + 2, th + 2);
        g2d.setClip(gpTextDisplayArea);
        buffer.translate(-ptx, -pty);
        // Translate in preparation for display selection and text
        if (hasSelection()) {
            if (endTLHI.compareTo(startTLHI) == -1) {
                startSelTLHI = endTLHI;
                endSelTLHI = startTLHI;
            } else {
                startSelTLHI = startTLHI;
                endSelTLHI = endTLHI;
            }
        }
        // Display selection and text
        for (TextLayoutInfo lineInfo : lines) {
            TextLayout layout = lineInfo.layout;
            buffer.translate(0, layout.getAscent());
            // Draw selection if any
            if (hasSelection() && lineInfo.compareTo(startSelTLHI.tli) >= 0 && lineInfo.compareTo(endSelTLHI.tli) <= 0) {
                int ss = 0;
                ss = (lineInfo.compareTo(startSelTLHI.tli) == 0) ? startSelTLHI.thi.getInsertionIndex() : 0;
                int ee = endSelTLHI.thi.getInsertionIndex();
                ee = (lineInfo.compareTo(endSelTLHI.tli) == 0) ? endSelTLHI.thi.getInsertionIndex() : lineInfo.nbrChars - 1;
                g2d.setColor(jpalette[14]);
                Shape selShape = layout.getLogicalHighlightShape(ss, ee);
                g2d.fill(selShape);
            }
            // display text
            g2d.setColor(jpalette[2]);
            lineInfo.layout.draw(g2d, 0, 0);
            buffer.translate(0, layout.getDescent() + layout.getLeading());
        }
        g2d.setClip(null);
        buffer.endDraw();
    }
}
Also used : TextLayoutHitInfo(g4p_controls.StyledString.TextLayoutHitInfo) Shape(java.awt.Shape) TextLayoutInfo(g4p_controls.StyledString.TextLayoutInfo) Graphics2D(java.awt.Graphics2D) 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