Search in sources :

Example 46 with AttributedString

use of java.text.AttributedString in project chipKIT32-MAX by chipKIT32.

the class CompositionTextManager method getCommittedText.

public AttributedCharacterIterator getCommittedText(int beginIndex, int endIndex) {
    int length = endIndex - beginIndex;
    String textAreaString = textArea.getText(beginIndex, length);
    return new AttributedString(textAreaString).getIterator();
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) Point(java.awt.Point)

Example 47 with AttributedString

use of java.text.AttributedString 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 48 with AttributedString

use of java.text.AttributedString 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)

Example 49 with AttributedString

use of java.text.AttributedString in project robovm by robovm.

the class NativeDecimalFormat method formatToCharacterIterator.

public AttributedCharacterIterator formatToCharacterIterator(Object object) {
    if (object == null) {
        throw new NullPointerException("object == null");
    }
    if (!(object instanceof Number)) {
        throw new IllegalArgumentException("object not a Number: " + object.getClass());
    }
    Number number = (Number) object;
    FieldPositionIterator fpIter = new FieldPositionIterator();
    String text;
    if (number instanceof BigInteger || number instanceof BigDecimal) {
        text = new String(formatDigitList(this.address, number.toString(), fpIter));
    } else if (number instanceof Double || number instanceof Float) {
        double dv = number.doubleValue();
        text = new String(formatDouble(this.address, dv, fpIter));
    } else {
        long lv = number.longValue();
        text = new String(formatLong(this.address, lv, fpIter));
    }
    AttributedString as = new AttributedString(text);
    while (fpIter.next()) {
        Format.Field field = fpIter.field();
        as.addAttribute(field, field, fpIter.start(), fpIter.limit());
    }
    // return the CharacterIterator from AttributedString
    return as.getIterator();
}
Also used : AttributedString(java.text.AttributedString) Format(java.text.Format) NumberFormat(java.text.NumberFormat) BigInteger(java.math.BigInteger) AttributedString(java.text.AttributedString) BigDecimal(java.math.BigDecimal)

Example 50 with AttributedString

use of java.text.AttributedString 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)

Aggregations

AttributedString (java.text.AttributedString)86 AttributedCharacterIterator (java.text.AttributedCharacterIterator)37 TextLayout (java.awt.font.TextLayout)29 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)16 FontRenderContext (java.awt.font.FontRenderContext)13 Font (java.awt.Font)11 Point (java.awt.Point)11 Paint (java.awt.Paint)10 Graphics2D (java.awt.Graphics2D)9 WeakHashMap (java.util.WeakHashMap)8 Color (java.awt.Color)4 Bidi (java.text.Bidi)4 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)4 Rectangle (java.awt.Rectangle)3 HashSet (java.util.HashSet)3 Dimension (java.awt.Dimension)2 Insets (java.awt.Insets)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2