Search in sources :

Example 76 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 77 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)

Example 78 with FontRenderContext

use of java.awt.font.FontRenderContext in project vcell by virtualcell.

the class ShapePaintUtil method paintLinkMark.

public static void paintLinkMark(Graphics2D g, Shape shape, Color color) {
    Font fontOld = g.getFont();
    Color colorOld = g.getColor();
    Font font = fontOld.deriveFont((float) (shape.getHeight() / 2)).deriveFont(Font.BOLD);
    g.setFont(font);
    g.setColor(color);
    FontRenderContext fontRenderContext = g.getFontRenderContext();
    String text = "L";
    TextLayout textLayout = new TextLayout(text, font, fontRenderContext);
    Rectangle2D textBounds = textLayout.getBounds();
    int xText = (int) (shape.getAbsX() + (shape.getWidth() - textBounds.getWidth()) / 2);
    int yText = (int) (shape.getAbsY() + (shape.getHeight() + textBounds.getHeight()) / 2 + 1);
    textLayout.draw(g, xText, yText);
    g.setFont(fontOld);
    g.setColor(colorOld);
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout)

Example 79 with FontRenderContext

use of java.awt.font.FontRenderContext in project vcell by virtualcell.

the class ShapePaintUtil method paintLinkMarkRule.

public static void paintLinkMarkRule(Graphics2D g, Shape shape, Color color) {
    Font fontOld = g.getFont();
    Color colorOld = g.getColor();
    Font font = fontOld.deriveFont((float) (shape.getHeight() / 2)).deriveFont(Font.BOLD);
    g.setFont(font);
    g.setColor(color);
    FontRenderContext fontRenderContext = g.getFontRenderContext();
    String text = "L";
    TextLayout textLayout = new TextLayout(text, font, fontRenderContext);
    Rectangle2D textBounds = textLayout.getBounds();
    int xText = (int) (shape.getAbsX() + (shape.getWidth() + 4 - textBounds.getWidth()) / 2 - 1);
    int yText = (int) (shape.getAbsY() + (shape.getHeight() - 2 + textBounds.getHeight()) / 2 + 1);
    textLayout.draw(g, xText, yText);
    g.setFont(fontOld);
    g.setColor(colorOld);
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Font(java.awt.Font) TextLayout(java.awt.font.TextLayout)

Aggregations

FontRenderContext (java.awt.font.FontRenderContext)79 Font (java.awt.Font)27 TextLayout (java.awt.font.TextLayout)25 GlyphVector (java.awt.font.GlyphVector)18 Graphics2D (java.awt.Graphics2D)17 Rectangle2D (java.awt.geom.Rectangle2D)17 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 Color (java.awt.Color)5 Shape (java.awt.Shape)5 LineMetrics (java.awt.font.LineMetrics)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 Point (java.awt.Point)4 BasicStroke (java.awt.BasicStroke)3 FontMetrics (java.awt.FontMetrics)3 Rectangle (java.awt.Rectangle)3