Search in sources :

Example 71 with FontRenderContext

use of java.awt.font.FontRenderContext in project poi by apache.

the class DrawTextParagraph method tab2space.

/**
     * Replace a tab with the effective number of white spaces.
     */
private String tab2space(TextRun tr) {
    AttributedString string = new AttributedString(" ");
    String fontFamily = tr.getFontFamily();
    if (fontFamily == null) {
        fontFamily = "Lucida Sans";
    }
    string.addAttribute(TextAttribute.FAMILY, fontFamily);
    Double fs = tr.getFontSize();
    if (fs == null) {
        fs = 12d;
    }
    string.addAttribute(TextAttribute.SIZE, fs.floatValue());
    TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true));
    double wspace = l.getAdvance();
    Double tabSz = paragraph.getDefaultTabSize();
    if (tabSz == null) {
        tabSz = wspace * 4;
    }
    int numSpaces = (int) Math.ceil(tabSz / wspace);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < numSpaces; i++) {
        buf.append(' ');
    }
    return buf.toString();
}
Also used : AttributedString(java.text.AttributedString) AttributedString(java.text.AttributedString) FontRenderContext(java.awt.font.FontRenderContext) Paint(java.awt.Paint) TextLayout(java.awt.font.TextLayout)

Example 72 with FontRenderContext

use of java.awt.font.FontRenderContext in project jgnash by ccavanaugh.

the class AWTFontUtilities method getFontRenderContext.

private static synchronized FontRenderContext getFontRenderContext() {
    FontRenderContext context = contextReference.get();
    if (context == null) {
        context = new FontRenderContext(null, true, true);
        contextReference = new SoftReference<>(context);
    }
    return context;
}
Also used : FontRenderContext(java.awt.font.FontRenderContext)

Example 73 with FontRenderContext

use of java.awt.font.FontRenderContext in project jgnash by ccavanaugh.

the class AWTFontUtilities method getStringWidth.

/**
     * Calculates the width of the specified {@code String}.
     * 
     * @param text the text to be weighted.
     * @param font the font to be weighted
     * @return the width of the given string in 1/72" dpi.
     */
private static int getStringWidth(final String text, final Font font) {
    final FontRenderContext frc = getFontRenderContext();
    // text is padded by one space
    final Rectangle2D bounds = font.getStringBounds(text + " ", frc);
    return (int) ceil(bounds.getWidth()) + 5;
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext)

Example 74 with FontRenderContext

use of java.awt.font.FontRenderContext 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 75 with FontRenderContext

use of java.awt.font.FontRenderContext in project android_frameworks_base by crdroidandroid.

the class BidiRenderer method render.

/**
     * Renders the text to the right of the bounds with the given font.
     * @param font The font to render the text with.
     */
private void render(int start, int limit, Font font, int flag, float[] advances, int advancesIndex, boolean draw) {
    FontRenderContext frc;
    if (mGraphics != null) {
        frc = mGraphics.getFontRenderContext();
    } else {
        frc = Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
        // Metrics obtained this way don't have anti-aliasing set. So,
        // we create a new FontRenderContext with anti-aliasing set.
        frc = new FontRenderContext(font.getTransform(), mPaint.isAntiAliased(), frc.usesFractionalMetrics());
    }
    GlyphVector gv = font.layoutGlyphVector(frc, mText, start, limit, flag);
    int ng = gv.getNumGlyphs();
    int[] ci = gv.getGlyphCharIndices(0, ng, null);
    if (advances != null) {
        for (int i = 0; i < ng; i++) {
            int adv_idx = advancesIndex + ci[i];
            advances[adv_idx] += gv.getGlyphMetrics(i).getAdvanceX();
        }
    }
    if (draw && mGraphics != null) {
        mGraphics.drawGlyphVector(gv, mBounds.right, mBaseline);
    }
    // Update the bounds.
    Rectangle2D awtBounds = gv.getLogicalBounds();
    RectF bounds = awtRectToAndroidRect(awtBounds, mBounds.right, mBaseline);
    // coordinates from the bounds as an offset.
    if (Math.abs(mBounds.right - mBounds.left) == 0) {
        mBounds = bounds;
    } else {
        mBounds.union(bounds);
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext)

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