Example usage for java.awt.font FontRenderContext FontRenderContext

List of usage examples for java.awt.font FontRenderContext FontRenderContext

Introduction

In this page you can find the example usage for java.awt.font FontRenderContext FontRenderContext.

Prototype

public FontRenderContext(AffineTransform tx, Object aaHint, Object fmHint) 

Source Link

Document

Constructs a FontRenderContext object from an optional AffineTransform and two Object values that determine if the newly constructed object has anti-aliasing or fractional metrics.

Usage

From source file:org.apache.jetspeed.security.mfa.impl.CaptchaImageResource.java

/**
 * Renders this image/* w w w .ja v a 2s  .  co  m*/
 * 
 * @return The image data
 */
private final byte[] render() throws IOException {
    Graphics2D gfx = (Graphics2D) this.image.getGraphics();
    if (config.isFontAntialiasing())
        gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int curWidth = config.getTextMarginLeft();
    FontRenderContext ctx = new FontRenderContext(null, config.isFontAntialiasing(), false);
    for (int i = 0; i < charAttsList.size(); i++) {
        CharAttributes cf = (CharAttributes) charAttsList.get(i);
        TextLayout text = new TextLayout(cf.getChar() + "", getFont(cf.getName()), ctx); //gfx.getFontRenderContext());
        AffineTransform textAt = new AffineTransform();
        textAt.translate(curWidth, this.height - cf.getRise());
        if (cf.getRotation() != 0) {
            textAt.rotate(cf.getRotation());
        }
        if (cf.getShearX() > 0.0)
            textAt.shear(cf.getShearX(), cf.getShearY());
        Shape shape = text.getOutline(textAt);
        curWidth += shape.getBounds().getWidth() + config.getTextSpacing();
        if (config.isUseImageBackground())
            gfx.setColor(Color.BLACK);
        else
            gfx.setXORMode(Color.BLACK);
        gfx.fill(shape);
    }
    if (config.isEffectsNoise()) {
        noiseEffects(gfx, image);
    }
    if (config.isUseTimestamp()) {
        if (config.isEffectsNoise())
            gfx.setColor(Color.WHITE);
        else
            gfx.setColor(Color.BLACK);

        TimeZone tz = TimeZone.getTimeZone(config.getTimestampTZ());
        Calendar cal = new GregorianCalendar(tz);
        SimpleDateFormat formatter;
        if (config.isUseTimestamp24hr())
            formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
        else
            formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a, z");
        formatter.setTimeZone(tz);
        Font font = gfx.getFont();
        Font newFont = new Font(font.getName(), font.getStyle(), config.getTimestampFontSize());
        gfx.setFont(newFont);
        gfx.drawString(formatter.format(cal.getTime()), config.getTextMarginLeft() * 4, this.height - 1);
    }

    return toImageData(image);
}

From source file:org.apache.pdfbox.pdmodel.font.PDSimpleFont.java

/**
 * {@inheritDoc}//from  w w  w.  j  ava  2s . c o m
 */
public void drawString(String string, int[] codePoints, Graphics g, float fontSize, AffineTransform at, float x,
        float y) throws IOException {
    Font awtFont = getawtFont();
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
    GlyphVector glyphs = null;
    boolean useCodepoints = codePoints != null && isType0Font();
    PDFont descendantFont = useCodepoints ? ((PDType0Font) this).getDescendantFont() : null;
    // symbolic fonts may trigger the same fontmanager.so/dll error as described below
    if (useCodepoints && !descendantFont.getFontDescriptor().isSymbolic()) {
        PDCIDFontType2Font cid2Font = null;
        if (descendantFont instanceof PDCIDFontType2Font) {
            cid2Font = (PDCIDFontType2Font) descendantFont;
        }
        if ((cid2Font != null && cid2Font.hasCIDToGIDMap()) || isFontSubstituted) {
            // we still have to use the string if a CIDToGIDMap is used 
            glyphs = awtFont.createGlyphVector(frc, string);
        } else {
            glyphs = awtFont.createGlyphVector(frc, codePoints);
        }
    } else {
        // mdavis - fix fontmanager.so/dll on sun.font.FileFont.getGlyphImage
        // for font with bad cmaps?
        // Type1 fonts are not affected as they don't have cmaps
        if (!isType1Font() && awtFont.canDisplayUpTo(string) != -1) {
            LOG.warn("Changing font on <" + string + "> from <" + awtFont.getName() + "> to the default font");
            awtFont = Font.decode(null).deriveFont(1f);
        }
        glyphs = awtFont.createGlyphVector(frc, string);
    }
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    writeFont(g2d, at, x, y, glyphs);
}

From source file:org.dishevelled.brainstorm.BrainStorm.java

/**
 * Calculate the text area size in rows and columns based on the current
 * window size and text area font size.//  w w  w .j  av  a 2 s  . co m
 */
private void calculateTextAreaSize() {
    double width = (double) getWidth();
    FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
    TextLayout textLayout = new TextLayout("W", textArea.getFont(), frc);
    Rectangle2D textBounds = textLayout.getBounds();
    int columns = Math.min(45, (int) (width / textBounds.getWidth()) - 4);
    textArea.setColumns(columns);
}

From source file:org.eclipse.birt.chart.device.svg.SVGGraphics2D.java

public SVGGraphics2D(Document dom, boolean scriptable) {
    this.dom = dom;
    this.scriptable = scriptable;
    fontRenderContext = new FontRenderContext(new AffineTransform(), true, false);
    currentElement = dom.getDocumentElement();
    parentStack.push(currentElement);/*from   ww  w. jav a 2  s.  c om*/
    currentParent = currentElement;
    // add default styles
    currentElement = dom.createElement("g"); //$NON-NLS-1$
    definitions = dom.createElement("defs"); //$NON-NLS-1$
    // give the outer group element an ID
    currentElement.setAttribute("id", "outerG"); //$NON-NLS-1$ //$NON-NLS-2$
    currentElement.appendChild(definitions);
    currentElement.setAttribute("style", defaultStyles); //$NON-NLS-1$
    pushParent(currentElement);

    transforms = new AffineTransform();
    initializeScriptStyles();
}

From source file:org.pentaho.reporting.engine.classic.core.layout.process.text.ComplexTextMinorAxisLayoutStep.java

private FontRenderContext createFontRenderContext(final StyleSheet layoutContext) {
    final boolean antiAliasing = RenderUtility.isFontSmooth(layoutContext, getMetaData());
    return new FontRenderContext(null, antiAliasing, true);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics2D#getFontRenderContext()
 *//*from   www.  j a  va2  s  . c  o m*/
@Override
public FontRenderContext getFontRenderContext() {
    final boolean antialias = RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            .equals(getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING));
    final boolean fractions = RenderingHints.VALUE_FRACTIONALMETRICS_ON
            .equals(getRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS));
    return new FontRenderContext(new AffineTransform(), antialias, fractions);
}

From source file:org.schemaspy.view.DotNode.java

private int getTextWidth(String text) {
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
    int fontSize = Config.getInstance().getFontSize() + 1;
    Font font = new Font(Config.getInstance().getFont(), Font.BOLD, fontSize);
    int fontWidth = (int) (font.getStringBounds(text, frc).getWidth());
    return fontWidth;
}