java.awt.font.FontRenderContext Java Examples

The following examples show how to use java.awt.font.FontRenderContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextBlock.java    From pumpernickel with MIT License 7 votes vote down vote up
@Override
public synchronized void paint(Graphics2D g) {
	if (dirty)
		revalidate();
	FontRenderContext frc = createFontRenderContext();
	if (frc.isAntiAliased()) {
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
	} else {
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_OFF);
	}
	if (frc.usesFractionalMetrics()) {
		g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
				RenderingHints.VALUE_FRACTIONALMETRICS_ON);
	} else {
		g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
				RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
	}

	paintBackground(g, body);
	paintBorder(g, body);
	paintText(g, body);
}
 
Example #2
Source File: HeadIconUtil.java    From Autumn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 创建图片
 * @param content 内容
 * @param font  字体
 * @param width 宽
 * @param height 高
 * @return
 */
private static BufferedImage createImage(String content, Font font, Integer width, Integer height){
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D)bi.getGraphics();
    g2.setBackground(Color.blue);
    g2.clearRect(0, 0, width, height);
    g2.setPaint(Color.BLACK);

    FontRenderContext context = g2.getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(content, context);
    double x = (width - bounds.getWidth()) / 2;
    double y = (height - bounds.getHeight()) / 2;
    double ascent = - bounds.getY();
    double baseY = y + ascent;

    g2.drawString(content, (int)x, (int)baseY);

    return bi;
}
 
Example #3
Source File: HelvLtOblTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2 = (Graphics2D)g;
     FontRenderContext frc = new FontRenderContext(null, true, true);
     Font f = helvFont.deriveFont(Font.PLAIN, 40);
     System.out.println("font = " +f.getFontName());
     GlyphVector gv = f.createGlyphVector(frc, codes);
     g.setFont(f);
     g.setColor(Color.white);
     g.fillRect(0,0,400,400);
     g.setColor(Color.black);
     g2.drawGlyphVector(gv, 5,200);
     g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                         RenderingHints.VALUE_FRACTIONALMETRICS_ON);
     g2.drawString(str, 5, 250);
}
 
Example #4
Source File: GlyphListPipe.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void drawGlyphVector(SunGraphics2D sg2d, GlyphVector gv,
                            float x, float y)
{
    FontRenderContext frc = gv.getFontRenderContext();
    FontInfo info = sg2d.getGVFontInfo(gv.getFont(), frc);
    if (info.pixelHeight > OutlineTextRenderer.THRESHHOLD) {
        SurfaceData.outlineTextRenderer.drawGlyphVector(sg2d, gv, x, y);
        return;
    }
    if (sg2d.transformState >= SunGraphics2D.TRANSFORM_TRANSLATESCALE) {
        double origin[] = {x, y};
        sg2d.transform.transform(origin, 0, origin, 0, 1);
        x = (float) origin[0];
        y = (float) origin[1];
    } else {
        x += sg2d.transX; // don't use the glyph info origin, already in gv.
        y += sg2d.transY;
    }

    GlyphList gl = GlyphList.getInstance();
    gl.setFromGlyphVector(info, gv, x, y);
    drawGlyphList(sg2d, gl, info.aaHint);
    gl.dispose();
}
 
Example #5
Source File: StandardGlyphVector.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void init(Font font, char[] text, int start, int count,
                  FontRenderContext frc, int flags) {

    if (start < 0 || count < 0 || start + count > text.length) {
        throw new ArrayIndexOutOfBoundsException("start or count out of bounds");
    }

    this.font = font;
    this.frc = frc;
    this.flags = flags;

    if (getTracking(font) != 0) {
        addFlags(FLAG_HAS_POSITION_ADJUSTMENTS);
    }

    // !!! change mapper interface?
    if (start != 0) {
        char[] temp = new char[count];
        System.arraycopy(text, start, temp, 0, count);
        text = temp;
    }

    initFontData(); // sets up font2D

    // !!! no layout for now, should add checks
    // !!! need to support creating a StandardGlyphVector from a TextMeasurer's info...
    glyphs = new int[count]; // hmmm
    /* Glyphs obtained here are already validated by the font */
    userGlyphs = glyphs;
    font2D.getMapper().charsToGlyphs(count, text, glyphs);
}
 
Example #6
Source File: TextConstructionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars;
    final FontRenderContext frc = tcctx.frc;
    GlyphVector gv;
    do {
        gv = font.createGlyphVector(frc, chars);
    } while (--numReps >= 0);
}
 
Example #7
Source File: StandardGlyphVector.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility used by getStandardGV.
 * Constructs a StandardGlyphVector from a generic glyph vector.
 * Do not call this from new contexts without considering the comment
 * about "userGlyphs".
 */
private StandardGlyphVector(GlyphVector gv, FontRenderContext frc) {
    this.font = gv.getFont();
    this.frc = frc;
    initFontData();

    int nGlyphs = gv.getNumGlyphs();
    this.userGlyphs = gv.getGlyphCodes(0, nGlyphs, null);
    if (gv instanceof StandardGlyphVector) {
        /* userGlyphs will be OK because this is a private constructor
         * and the returned instance is used only for rendering.
         * It's not constructable by user code, nor returned to the
         * application. So we know "userGlyphs" are valid as having
         * been either already validated or are the result of layout.
         */
        this.glyphs = userGlyphs;
    } else {
        this.glyphs = getValidatedGlyphs(this.userGlyphs);
    }
    this.flags = gv.getLayoutFlags() & FLAG_MASK;

    if ((flags & FLAG_HAS_POSITION_ADJUSTMENTS) != 0) {
        this.positions = gv.getGlyphPositions(0, nGlyphs + 1, null);
    }

    if ((flags & FLAG_COMPLEX_GLYPHS) != 0) {
        this.charIndices = gv.getGlyphCharIndices(0, nGlyphs, null);
    }

    if ((flags & FLAG_HAS_TRANSFORMS) != 0) {
        AffineTransform[] txs = new AffineTransform[nGlyphs]; // worst case
        for (int i = 0; i < nGlyphs; ++i) {
            txs[i] = gv.getGlyphTransform(i); // gv doesn't have getGlyphsTransforms
        }

        setGlyphTransforms(txs);
    }
}
 
Example #8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override protected void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g.create();
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  g2.setPaint(Color.WHITE);
  g2.fillRect(0, 0, getWidth(), getHeight());

  String str = getCharacterString();

  FontRenderContext frc = g2.getFontRenderContext();
  Shape exShape = new TextLayout(str, ipaEx, frc).getOutline(null);
  Shape mjShape = new TextLayout(str, ipaMj, frc).getOutline(null);

  Rectangle2D b = exShape.getBounds2D();
  double cx = getWidth() / 2d - b.getCenterX();
  double cy = getHeight() / 2d - b.getCenterY();
  AffineTransform toCenterAtf = AffineTransform.getTranslateInstance(cx, cy);

  g2.setPaint(Color.YELLOW);
  g2.draw(toCenterAtf.createTransformedShape(b));

  Shape s1 = toCenterAtf.createTransformedShape(exShape);
  Shape s2 = toCenterAtf.createTransformedShape(mjShape);

  if (fontPaintFlag.contains(FontPaint.IPA_EX_MINCHO)) {
    g2.setPaint(Color.CYAN);
    g2.fill(s1);
  }
  if (fontPaintFlag.contains(FontPaint.IPA_MJ_MINCHO)) {
    g2.setPaint(Color.MAGENTA);
    g2.fill(s2);
  }
  if (fontPaintFlag.containsAll(EnumSet.allOf(FontPaint.class))) {
    g2.setClip(s1);
    g2.setPaint(Color.BLACK);
    g2.fill(s2);
  }
  g2.dispose();
}
 
Example #9
Source File: TranslatedOutlineTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String a[]) {
    /* prepare blank image */
    BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) bi.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, 50, 50);

    /* draw outline somethere in the middle of the image */
    FontRenderContext frc = new FontRenderContext(null, false, false);
    g2.setColor(Color.RED);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    GlyphVector gv = g2.getFont().createGlyphVector(frc, "test");
    g2.fill(gv.getOutline(20, 20));

    /* Check if anything was drawn.
     * If y direction is not correct then image is still blank and
     * test will fail.
     */
    int bgcolor = Color.WHITE.getRGB();
    for (int i=0; i<bi.getWidth(); i++) {
        for(int j=0; j<bi.getHeight(); j++) {
           if (bi.getRGB(i, j) != bgcolor) {
               System.out.println("Test passed.");
               return;
           }
        }
    }
    throw new RuntimeException("Outline was not detected");
}
 
Example #10
Source File: TextLabelFactory.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize a factory to produce glyph arrays.
 * @param frc the FontRenderContext to use for the arrays to be produced.
 * @param text the text of the paragraph.
 * @param bidi the bidi information for the paragraph text, or null if the
 * entire text is left-to-right text.
 */
public TextLabelFactory(FontRenderContext frc,
                        char[] text,
                        Bidi bidi,
                        int flags) {
  this.frc = frc;
  this.text = text.clone();
  this.bidi = bidi;
  this.flags = flags;
  this.lineBidi = bidi;
  this.lineStart = 0;
  this.lineLimit = text.length;
}
 
Example #11
Source File: StandardGlyphVector.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, CharacterIterator iter, FontRenderContext frc) {
    int offset = iter.getBeginIndex();
    char[] text = new char [iter.getEndIndex() - offset];
    for(char c = iter.first();
        c != CharacterIterator.DONE;
        c = iter.next()) {
        text[iter.getIndex() - offset] = c;
    }
    init(font, text, 0, text.length, frc, UNINITIALIZED_FLAGS);
}
 
Example #12
Source File: LogAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum tick label height.
 *
 * @param g2  the graphics device.
 *
 * @return The maximum height.
 *
 * @since 1.0.7
 */
protected double estimateMaximumTickLabelHeight(Graphics2D g2) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    result += tickLabelFont.getLineMetrics("123", frc).getHeight();
    return result;

}
 
Example #13
Source File: MultiIconSimpleColoredComponent.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private float computeStringWidth(int fragmentIndex, Font font) {
  String text = myFragments.get(fragmentIndex);
  if (StringUtil.isEmpty(text)) return 0;
  FontRenderContext fontRenderContext = getFontMetrics(font).getFontRenderContext();
  TextLayout layout = getTextLayout(fragmentIndex, font, fontRenderContext);
  if (layout != null) {
    return layout.getAdvance();
  }
  else {
    return (float)font.getStringBounds(text, fontRenderContext).getWidth();
  }
}
 
Example #14
Source File: TextConstructionTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final CharacterIterator ci = tcctx.ci;
    final FontRenderContext frc = tcctx.frc;
    GlyphVector gv;
    do {
        gv = font.createGlyphVector(frc, ci);
    } while (--numReps >= 0);
}
 
Example #15
Source File: StandardGlyphVector.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, FontRenderContext frc, int[] glyphs, float[] positions,
                           int[] indices, int flags) {
    initGlyphVector(font, frc, glyphs, positions, indices, flags);

    // this code should go into layout
    float track = getTracking(font);
    if (track != 0) {
        track *= font.getSize2D();
        Point2D.Float trackPt = new Point2D.Float(track, 0); // advance delta
        if (font.isTransformed()) {
            AffineTransform at = font.getTransform();
            at.deltaTransform(trackPt, trackPt);
        }

        // how do we know its a base glyph
        // for now, it is if the natural advance of the glyph is non-zero
        Font2D f2d = FontUtilities.getFont2D(font);
        FontStrike strike = f2d.getStrike(font, frc);

        float[] deltas = { trackPt.x, trackPt.y };
        for (int j = 0; j < deltas.length; ++j) {
            float inc = deltas[j];
            if (inc != 0) {
                float delta = 0;
                for (int i = j, n = 0; n < glyphs.length; i += 2) {
                    if (strike.getGlyphAdvance(glyphs[n++]) != 0) { // might be an inadequate test
                        positions[i] += delta;
                        delta += inc;
                    }
                }
                positions[positions.length-2+j] += delta;
            }
        }
    }
}
 
Example #16
Source File: TextSourceLabel.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected GlyphVector createGV() {
  Font font = source.getFont();
  FontRenderContext frc = source.getFRC();
  int flags = source.getLayoutFlags();
  char[] context = source.getChars();
  int start = source.getStart();
  int length = source.getLength();

  GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines
  StandardGlyphVector gv = gl.layout(font, frc, context, start, length,
                                     flags, null); // ??? use textsource
  GlyphLayout.done(gl);

  return gv;
}
 
Example #17
Source File: TextConstructionTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final char[] chars = tcctx.chars1;
    final int start = 1;
    final int limit = chars.length - 1;
    final FontRenderContext frc = tcctx.frc;
    final int flags = tcctx.flags;
    GlyphVector gv;
    do {
        gv = font.layoutGlyphVector(frc, chars, start, limit, flags);
    } while (--numReps >= 0);
}
 
Example #18
Source File: StandardTextSource.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Create a StandardTextSource whose context and source are all the text in the String. */
public StandardTextSource(String str,
                          int level,
                          int flags,
                          Font font,
                          FontRenderContext frc) {
  this(str.toCharArray(), 0, str.length(), 0, str.length(), level, flags, font, frc, null);
}
 
Example #19
Source File: StandardGlyphVector.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public StandardGlyphVector(Font font, FontRenderContext frc, int[] glyphs, float[] positions,
                           int[] indices, int flags) {
    initGlyphVector(font, frc, glyphs, positions, indices, flags);

    // this code should go into layout
    float track = getTracking(font);
    if (track != 0) {
        track *= font.getSize2D();
        Point2D.Float trackPt = new Point2D.Float(track, 0); // advance delta
        if (font.isTransformed()) {
            AffineTransform at = font.getTransform();
            at.deltaTransform(trackPt, trackPt);
        }

        // how do we know its a base glyph
        // for now, it is if the natural advance of the glyph is non-zero
        Font2D f2d = FontUtilities.getFont2D(font);
        FontStrike strike = f2d.getStrike(font, frc);

        float[] deltas = { trackPt.x, trackPt.y };
        for (int j = 0; j < deltas.length; ++j) {
            float inc = deltas[j];
            if (inc != 0) {
                float delta = 0;
                for (int i = j, n = 0; n < glyphs.length; i += 2) {
                    if (strike.getGlyphAdvance(glyphs[n++]) != 0) { // might be an inadequate test
                        positions[i] += delta;
                        delta += inc;
                    }
                }
                positions[positions.length-2+j] += delta;
            }
        }
    }
}
 
Example #20
Source File: DateAxis.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
        DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr, upperStr;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #21
Source File: TextConstructionTests.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    TCContext tcctx = (TCContext)ctx;
    final Font font = tcctx.font;
    final CharacterIterator ci = tcctx.ci;
    final FontRenderContext frc = tcctx.frc;
    GlyphVector gv;
    do {
        gv = font.createGlyphVector(frc, ci);
    } while (--numReps >= 0);
}
 
Example #22
Source File: bug6302464.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testFontRenderingContext(Object aaHint) {

        JLabel label = new JLabel("Test");
        label.putClientProperty(KEY_TEXT_ANTIALIASING, aaHint);
        FontRenderContext frc = label.getFontMetrics(
                label.getFont()).getFontRenderContext();

        if (!aaHint.equals(frc.getAntiAliasingHint())) {
            throw new RuntimeException("Wrong aa hint in FontRenderContext");
        }
    }
 
Example #23
Source File: FontTests.java    From swcv with MIT License 5 votes vote down vote up
private void drawTextInBox(Graphics2D g2, String text, Rectangle2D positionOnScreen)
{
    Font font = FontUtils.getFont();
    FontRenderContext frc = g2.getFontRenderContext();

    //bounding box of the word
    GlyphVector gv2 = font.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    Rectangle bb = gv2.getPixelBounds(frc, 0.0f, 0.0f);

    //find correct font size
    float scaleX = (float)(positionOnScreen.getWidth() / bb.getWidth());
    float scaleY = (float)(positionOnScreen.getHeight() / bb.getHeight());

    //get a new position for the text
    float x = (float)(positionOnScreen.getX() - bb.getX() * scaleX);
    float y = (float)(positionOnScreen.getY() - bb.getY() * scaleY);

    //preparing font
    AffineTransform at = new AffineTransform(scaleX, 0, 0, scaleY, 0, 0);
    Font deriveFont = font.deriveFont(at);
    g2.setFont(deriveFont);
    g2.setColor(Color.black);

    //draw the label
    GlyphVector gv = deriveFont.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    g2.drawGlyphVector(gv, x, y);
    g2.draw(positionOnScreen);
}
 
Example #24
Source File: NumberAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
protected double estimateMaximumTickLabelWidth(Graphics2D g2,
                                               TickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of the
        // font)...
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = getTickLabelFont().getLineMetrics("0", frc);
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
        Range range = getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        String lowerStr = "";
        String upperStr = "";
        NumberFormat formatter = getNumberFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.valueToString(lower);
            upperStr = unit.valueToString(upper);
        }
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #25
Source File: FontDesignMetrics.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private FontDesignMetrics(Font font, FontRenderContext frc) {
  super(font);
  this.font = font;
  this.frc = frc;

  this.isAntiAliased = frc.isAntiAliased();
  this.usesFractionalMetrics = frc.usesFractionalMetrics();

  frcTx = frc.getTransform();

  matrix = new double[4];
  initMatrixAndMetrics();

  initAdvCache();
}
 
Example #26
Source File: OutlineTextRenderer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void drawGlyphVector(SunGraphics2D g2d, GlyphVector gv,
                            float x, float y) {


    Shape s = gv.getOutline(x, y);
    int prevaaHint = - 1;
    FontRenderContext frc = gv.getFontRenderContext();
    boolean aa = frc.isAntiAliased();

    /* aa will be true if any AA mode has been specified.
     * ie for LCD and 'gasp' modes too.
     * We will check if 'gasp' has resolved AA to be "OFF", and
     * in all other cases (ie AA ON and all LCD modes) use AA outlines.
     */
    if (aa) {
        if (g2d.getGVFontInfo(gv.getFont(), frc).aaHint ==
            SunHints.INTVAL_TEXT_ANTIALIAS_OFF) {
            aa = false;
        }
    }

    if (aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) {
        prevaaHint = g2d.antialiasHint;
        g2d.antialiasHint =  SunHints.INTVAL_ANTIALIAS_ON;
        g2d.validatePipe();
    } else if (!aa && g2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_OFF) {
        prevaaHint = g2d.antialiasHint;
        g2d.antialiasHint =  SunHints.INTVAL_ANTIALIAS_OFF;
        g2d.validatePipe();
    }

    g2d.fill(s);

    if (prevaaHint != -1) {
         g2d.antialiasHint = prevaaHint;
         g2d.validatePipe();
    }
}
 
Example #27
Source File: NumberAxis.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
protected double estimateMaximumTickLabelWidth(Graphics2D g2,
                                               TickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of the
        // font)...
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = getTickLabelFont().getLineMetrics("0", frc);
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        FontMetrics fm = g2.getFontMetrics(getTickLabelFont());
        Range range = getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        String lowerStr, upperStr;
        NumberFormat formatter = getNumberFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.valueToString(lower);
            upperStr = unit.valueToString(upper);
        }
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #28
Source File: NumberAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Estimates the maximum tick label height.
 * 
 * @param g2  the graphics device.
 * 
 * @return The maximum height.
 */
protected double estimateMaximumTickLabelHeight(Graphics2D g2) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();
    
    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    result += tickLabelFont.getLineMetrics("123", frc).getHeight();
    return result;
    
}
 
Example #29
Source File: TextSourceLabel.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected GlyphVector createGV() {
  Font font = source.getFont();
  FontRenderContext frc = source.getFRC();
  int flags = source.getLayoutFlags();
  char[] context = source.getChars();
  int start = source.getStart();
  int length = source.getLength();

  GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines
  StandardGlyphVector gv = gl.layout(font, frc, context, start, length,
                                     flags, null); // ??? use textsource
  GlyphLayout.done(gl);

  return gv;
}
 
Example #30
Source File: StandardGlyphVector.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility used by getStandardGV.
 * Constructs a StandardGlyphVector from a generic glyph vector.
 * Do not call this from new contexts without considering the comment
 * about "userGlyphs".
 */
private StandardGlyphVector(GlyphVector gv, FontRenderContext frc) {
    this.font = gv.getFont();
    this.frc = frc;
    initFontData();

    int nGlyphs = gv.getNumGlyphs();
    this.userGlyphs = gv.getGlyphCodes(0, nGlyphs, null);
    if (gv instanceof StandardGlyphVector) {
        /* userGlyphs will be OK because this is a private constructor
         * and the returned instance is used only for rendering.
         * It's not constructable by user code, nor returned to the
         * application. So we know "userGlyphs" are valid as having
         * been either already validated or are the result of layout.
         */
        this.glyphs = userGlyphs;
    } else {
        this.glyphs = getValidatedGlyphs(this.userGlyphs);
    }
    this.flags = gv.getLayoutFlags() & FLAG_MASK;

    if ((flags & FLAG_HAS_POSITION_ADJUSTMENTS) != 0) {
        this.positions = gv.getGlyphPositions(0, nGlyphs + 1, null);
    }

    if ((flags & FLAG_COMPLEX_GLYPHS) != 0) {
        this.charIndices = gv.getGlyphCharIndices(0, nGlyphs, null);
    }

    if ((flags & FLAG_HAS_TRANSFORMS) != 0) {
        AffineTransform[] txs = new AffineTransform[nGlyphs]; // worst case
        for (int i = 0; i < nGlyphs; ++i) {
            txs[i] = gv.getGlyphTransform(i); // gv doesn't have getGlyphsTransforms
        }

        setGlyphTransforms(txs);
    }
}