Search in sources :

Example 1 with FontRenderContext

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

the class TextObjectGUI method drawMathObject.

public void drawMathObject(TextObject object, Graphics g, Point pageOrigin, float zoomLevel) {
    ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
    if (!object.getText().equals("")) {
        Font f = g.getFont();
        String message = object.getText();
        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;
        try {
            TextLayout textLayout;
            while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
                textLayout = messageLBM.nextLayout(wrappingWidth);
                y += textLayout.getAscent();
                if (object.getAlignment().equals(TextObject.LEFT)) {
                    textLayout.draw(graphics2D, x, y);
                } else if (object.getAlignment().equals(TextObject.RIGHT)) {
                    textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()), y);
                } else {
                    //centered
                    textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()) / 2, y);
                }
                y += textLayout.getDescent() + textLayout.getLeading();
                x = sap.getxOrigin() + insets.left;
            }
        } catch (Exception e) {
        // TODO - logging and error reporting to user
        //				System.out.println("error with text rendering");
        }
        object.setHeight((int) ((y - sap.getyOrigin()) / zoomLevel));
        g.setFont(f);
    } else {
        // draw the black box around the text box if nothing is in it
        g.setColor(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), (int) (object.getWidth() * zoomLevel), (int) (object.getHeight() * zoomLevel));
    }
    if (((BooleanAttribute) object.getAttributeWithName(TextObject.SHOW_BOX)).getValue()) {
        g.setColor(Color.BLACK);
        g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
    }
}
Also used : BooleanAttribute(doc.attributes.BooleanAttribute) Insets(java.awt.Insets) 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) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext)

Example 2 with FontRenderContext

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

the class Toolkit method getMonoFontList.

// Gets the plain (not bold, not italic) version of each
private static List<Font> getMonoFontList() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = ge.getAllFonts();
    List<Font> outgoing = new ArrayList<Font>();
    // Using AffineTransform.getScaleInstance(100, 100) doesn't change sizes
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), Preferences.getBoolean("editor.antialias"), // use fractional metrics
    true);
    for (Font font : fonts) {
        if (font.getStyle() == Font.PLAIN && font.canDisplay('i') && font.canDisplay('M') && font.canDisplay(' ') && font.canDisplay('.')) {
            // The old method just returns 1 or 0, and using deriveFont(size)
            // is overkill. It also causes deprecation warnings
            //        @SuppressWarnings("deprecation")
            //        FontMetrics fm = awtToolkit.getFontMetrics(font);
            //FontMetrics fm = awtToolkit.getFontMetrics(font.deriveFont(24));
            //        System.out.println(fm.charWidth('i') + " " + fm.charWidth('M'));
            //        if (fm.charWidth('i') == fm.charWidth('M') &&
            //            fm.charWidth('M') == fm.charWidth(' ') &&
            //            fm.charWidth(' ') == fm.charWidth('.')) {
            double w = font.getStringBounds(" ", frc).getWidth();
            if (w == font.getStringBounds("i", frc).getWidth() && w == font.getStringBounds("M", frc).getWidth() && w == font.getStringBounds(".", frc).getWidth()) {
                //          //PApplet.printArray(font.getAvailableAttributes());
                //          Map<TextAttribute,?> attr = font.getAttributes();
                //          System.out.println(font.getFamily() + " > " + font.getName());
                //          System.out.println(font.getAttributes());
                //          System.out.println("  " + attr.get(TextAttribute.WEIGHT));
                //          System.out.println("  " + attr.get(TextAttribute.POSTURE));
                outgoing.add(font);
            //          System.out.println("  good " + w);
            }
        }
    }
    return outgoing;
}
Also used : ArrayList(java.util.ArrayList) AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext) GraphicsEnvironment(java.awt.GraphicsEnvironment) Font(java.awt.Font)

Example 3 with FontRenderContext

use of java.awt.font.FontRenderContext in project scriptographer by scriptographer.

the class GraphicContext method getFontRenderContext.

/**
     * Get the rendering context of the <code>Font</code> within this
     * <code>Graphics2D</code> context.
     * The {@link FontRenderContext}
     * encapsulates application hints such as anti-aliasing and
     * fractional metrics, as well as target device specific information
     * such as dots-per-inch.  This information should be provided by the
     * application when using objects that perform typographical
     * formatting, such as <code>Font</code> and
     * <code>TextLayout</code>.  This information should also be provided
     * by applications that perform their own layout and need accurate
     * measurements of various characteristics of glyphs such as advance
     * and line height when various rendering hints have been applied to
     * the text rendering.
     *
     * @return a reference to an instance of FontRenderContext.
     * @see java.awt.font.FontRenderContext
     * @see java.awt.Font#createGlyphVector(FontRenderContext,char[])
     * @see java.awt.font.TextLayout
     * @since     JDK1.2
     */
public FontRenderContext getFontRenderContext() {
    //
    // Find if antialiasing should be used.
    //
    Object antialiasingHint = hints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
    boolean isAntialiased = true;
    if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_ON && antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT) {
        // hint.
        if (antialiasingHint != RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) {
            antialiasingHint = hints.get(RenderingHints.KEY_ANTIALIASING);
            // Test general hint
            if (antialiasingHint != RenderingHints.VALUE_ANTIALIAS_ON && antialiasingHint != RenderingHints.VALUE_ANTIALIAS_DEFAULT) {
                // off explicitly, use it.
                if (antialiasingHint == RenderingHints.VALUE_ANTIALIAS_OFF)
                    isAntialiased = false;
            }
        } else
            isAntialiased = false;
    }
    //
    // Find out whether fractional metrics should be used.
    //
    boolean useFractionalMetrics = true;
    if (hints.get(RenderingHints.KEY_FRACTIONALMETRICS) == RenderingHints.VALUE_FRACTIONALMETRICS_OFF)
        useFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(defaultTransform, isAntialiased, useFractionalMetrics);
    return frc;
}
Also used : FontRenderContext(java.awt.font.FontRenderContext)

Example 4 with FontRenderContext

use of java.awt.font.FontRenderContext in project limelight by slagyr.

the class PropPanelTest method hasChangesWhenTextIsChanged.

@Test
public void hasChangesWhenTextIsChanged() throws Exception {
    TextPanel.staticFontRenderingContext = new FontRenderContext(new AffineTransform(), true, true);
    Layouts.on(panel, panel.getDefaultLayout());
    panel.resetNeededLayout();
    panel.setText("blah");
    assertEquals(true, panel.needsLayout());
    panel.resetNeededLayout();
    panel.setText("blah");
    assertEquals(false, panel.needsLayout());
    panel.setText("new text");
    assertEquals(true, panel.needsLayout());
}
Also used : AffineTransform(java.awt.geom.AffineTransform) FontRenderContext(java.awt.font.FontRenderContext) Test(org.junit.Test)

Example 5 with FontRenderContext

use of java.awt.font.FontRenderContext in project playn by threerings.

the class JavaTextLayout method layoutText.

public static JavaTextLayout[] layoutText(JavaGraphics gfx, String text, TextFormat format, TextWrap wrap) {
    // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF)
    text = normalizeEOL(text);
    // we do some fiddling to work around the fact that TextLayout chokes on the empty string
    String ltext = text.length() == 0 ? " " : text;
    // set up an attributed character iterator so that we can measure the text
    AttributedString astring = new AttributedString(ltext);
    if (format.font != null) {
        astring.addAttribute(TextAttribute.FONT, ((JavaFont) format.font).jfont);
    }
    List<JavaTextLayout> layouts = new ArrayList<JavaTextLayout>();
    FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
    LineBreakMeasurer measurer = new LineBreakMeasurer(astring.getIterator(), frc);
    int lastPos = ltext.length(), curPos = 0;
    char eol = '\n';
    while (curPos < lastPos) {
        int nextRet = ltext.indexOf(eol, measurer.getPosition() + 1);
        if (nextRet == -1) {
            nextRet = lastPos;
        }
        TextLayout layout = measurer.nextLayout(wrap.width, nextRet, false);
        int endPos = measurer.getPosition();
        while (curPos < endPos && ltext.charAt(curPos) == eol) // skip over EOLs
        curPos += 1;
        layouts.add(new JavaTextLayout(ltext.substring(curPos, endPos), format, layout));
        curPos = endPos;
    }
    return layouts.toArray(new JavaTextLayout[layouts.size()]);
}
Also used : AttributedString(java.text.AttributedString) ArrayList(java.util.ArrayList) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) AbstractTextLayout(playn.core.AbstractTextLayout) TextLayout(java.awt.font.TextLayout)

Aggregations

FontRenderContext (java.awt.font.FontRenderContext)75 Font (java.awt.Font)25 TextLayout (java.awt.font.TextLayout)21 GlyphVector (java.awt.font.GlyphVector)18 Graphics2D (java.awt.Graphics2D)16 Rectangle2D (java.awt.geom.Rectangle2D)15 AttributedString (java.text.AttributedString)14 AffineTransform (java.awt.geom.AffineTransform)13 Paint (java.awt.Paint)8 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)7 AttributedCharacterIterator (java.text.AttributedCharacterIterator)7 LineMetrics (java.awt.font.LineMetrics)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 Point (java.awt.Point)4 Shape (java.awt.Shape)4 BasicStroke (java.awt.BasicStroke)3 Color (java.awt.Color)3 FontMetrics (java.awt.FontMetrics)3 RoundRectangle2D (java.awt.geom.RoundRectangle2D)3