Search in sources :

Example 51 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class OutlineTextRenderer method drawString.

public void drawString(SunGraphics2D g2d, String str, double x, double y) {
    if ("".equals(str)) {
        // TextLayout constructor throws IAE on "".
        return;
    }
    TextLayout tl = new TextLayout(str, g2d.getFont(), g2d.getFontRenderContext());
    Shape s = tl.getOutline(AffineTransform.getTranslateInstance(x, y));
    int textAAHint = g2d.getFontInfo().aaHint;
    int prevaaHint = -1;
    if (textAAHint != SunHints.INTVAL_TEXT_ANTIALIAS_OFF && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {
        prevaaHint = g2d.antialiasHint;
        g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_ON;
        g2d.validatePipe();
    } else if (textAAHint == SunHints.INTVAL_TEXT_ANTIALIAS_OFF && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {
        prevaaHint = g2d.antialiasHint;
        g2d.antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
        g2d.validatePipe();
    }
    g2d.fill(s);
    if (prevaaHint != -1) {
        g2d.antialiasHint = prevaaHint;
        g2d.validatePipe();
    }
}
Also used : Shape(java.awt.Shape) TextLayout(java.awt.font.TextLayout)

Example 52 with TextLayout

use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.

the class GlyphListPipe method drawChars.

public void drawChars(SunGraphics2D sg2d, char[] data, int offset, int length, int ix, int iy) {
    FontInfo info = sg2d.getFontInfo();
    float x, y;
    if (info.pixelHeight > OutlineTextRenderer.THRESHHOLD) {
        SurfaceData.outlineTextRenderer.drawChars(sg2d, data, offset, length, ix, iy);
        return;
    }
    if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
        double[] origin = { ix + info.originX, iy + info.originY };
        sg2d.transform.transform(origin, 0, origin, 0, 1);
        x = (float) origin[0];
        y = (float) origin[1];
    } else {
        x = ix + info.originX + sg2d.transX;
        y = iy + info.originY + sg2d.transY;
    }
    GlyphList gl = GlyphList.getInstance();
    if (gl.setFromChars(info, data, offset, length, x, y)) {
        drawGlyphList(sg2d, gl);
        gl.dispose();
    } else {
        // release this asap.
        gl.dispose();
        TextLayout tl = new TextLayout(new String(data, offset, length), sg2d.getFont(), sg2d.getFontRenderContext());
        tl.draw(sg2d, ix, iy);
    }
}
Also used : GlyphList(sun.font.GlyphList) FontInfo(sun.java2d.loops.FontInfo) TextLayout(java.awt.font.TextLayout)

Example 53 with TextLayout

use of java.awt.font.TextLayout in project chipKIT32-MAX by chipKIT32.

the class CompositionTextManager method getTextLayout.

private TextLayout getTextLayout(AttributedCharacterIterator text, int committed_count) {
    AttributedString composed = new AttributedString(text, committed_count, text.getEndIndex());
    Font font = textArea.getPainter().getFont();
    FontRenderContext context = ((Graphics2D) (textArea.getPainter().getGraphics())).getFontRenderContext();
    composed.addAttribute(TextAttribute.FONT, font);
    TextLayout layout = new TextLayout(composed.getIterator(), context);
    return layout;
}
Also used : AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 54 with TextLayout

use of java.awt.font.TextLayout in project OpenNotebook by jaltekruse.

the class AnswerBoxGUI method drawMathObject.

public void drawMathObject(AnswerBoxObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
    // TODO - decide how extra whitespace should be handled, should it always be stored?
    // students may use it to format a multi-line answer
    // although useful whitespace will likely not coming at the very beginning or very end
    // of an answer
    List<? extends MathObjectAttribute> correctAnswers = object.getListWithName(AnswerBoxObject.CORRECT_ANSWERS).getValues();
    if (!object.getStudentAnswer().trim().equals("") || !correctAnswers.isEmpty()) {
        Font f = g.getFont();
        g.setColor(new Color(150, 210, 255));
        g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
        String message = object.getStudentAnswer();
        for (MathObjectAttribute mAtt : correctAnswers) {
            message += mAtt.getValue().toString() + ";";
        }
        message = message.substring(0, message.length() - 1);
        if (message.isEmpty()) {
            // cannot have an empty string in AttributedString
            message = " ";
        }
        g.setFont(f.deriveFont(sap.getFontSize()));
        g.setColor(Color.BLACK);
        Graphics2D graphics2D = (Graphics2D) g;
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        AttributedString messageAS = new AttributedString(message);
        messageAS.addAttribute(TextAttribute.FONT, g.getFont());
        AttributedCharacterIterator messageIterator = messageAS.getIterator();
        FontRenderContext messageFRC = graphics2D.getFontRenderContext();
        LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
        Insets insets = new Insets(2, 2, 2, 2);
        float wrappingWidth = sap.getWidth() - insets.left - insets.right;
        float x = sap.getxOrigin() + insets.left;
        float y = sap.getyOrigin() + insets.top;
        while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
            TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
            y += textLayout.getAscent();
            textLayout.draw(graphics2D, x, y);
            y += textLayout.getDescent() + textLayout.getLeading();
            x = sap.getxOrigin() + insets.left;
        }
        g.setFont(f);
    } else {
        g.setColor(new Color(230, 230, 230));
        g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
    }
    g.setColor(Color.BLACK);
    g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
Also used : Insets(java.awt.Insets) Color(java.awt.Color) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout) MathObjectAttribute(doc.attributes.MathObjectAttribute) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext)

Example 55 with TextLayout

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

the class ZyCaret method calcCaretPosition.

private int calcCaretPosition(final int hitLine, final double x, final double y) {
    final ZyLineContent content = m_content.getLineContent(hitLine);
    final TextLayout textlayout = m_content.getLineContent(hitLine).getTextLayout();
    final TextHitInfo hitInfo = textlayout.hitTestChar((float) x, (float) y, content.getBounds());
    return hitInfo.getInsertionIndex();
}
Also used : TextHitInfo(java.awt.font.TextHitInfo) 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