Java Code Examples for java.awt.font.GlyphVector#getVisualBounds()

The following examples show how to use java.awt.font.GlyphVector#getVisualBounds() . 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: TextFont.java    From libreveris with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Convenient method to compute a font size using a string content
 * and dimension.
 *
 * @param content  the string value
 * @param fontInfo OCR-based font information
 * @param dim      string dimension in pixels
 * @return the computed font size
 */
public static Float computeFontSize (String content,
                                     FontInfo fontInfo,
                                     Dimension dim)
{
    if (content == null) {
        return null;
    }

    Font font = new TextFont(fontInfo);
    float fontSize = font.getSize2D();
    GlyphVector glyphVector = font.createGlyphVector(frc, content);
    Rectangle2D basicRect = glyphVector.getVisualBounds();

    if (dim.width >= dim.height) {
        return fontSize * (dim.width / (float) basicRect.getWidth());
    } else {
        return fontSize * (dim.height / (float) basicRect.getHeight());
    }
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static GlyphVector makeJustifiedGlyphVector(GlyphVector gv, int width) {
  Rectangle2D r = gv.getVisualBounds();
  float jw = (float) width;
  float vw = (float) r.getWidth();
  if (jw > vw) {
    int num = gv.getNumGlyphs();
    float xx = (jw - vw) / (num - 1f);
    float pos = num == 1 ? (jw - vw) * .5f : 0f;
    Point2D gmPos = new Point2D.Float();
    for (int i = 0; i < num; i++) {
      GlyphMetrics gm = gv.getGlyphMetrics(i);
      gmPos.setLocation(pos, 0f);
      gv.setGlyphPosition(i, gmPos);
      pos += gm.getAdvance() + xx;
    }
  }
  return gv;
}
 
Example 3
Source File: TextMeasureTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 4
Source File: TextMeasureTests.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 5
Source File: TextMeasureTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 6
Source File: TextMeasureTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 7
Source File: RenderedText.java    From workcraft with MIT License 5 votes vote down vote up
public RenderedText(String text, Font font, Positioning positioning, Point2D offset) {
    this.text = text;
    this.font = font;
    this.positioning = positioning;
    this.xOffset = offset.getX();
    this.yOffset = offset.getY();

    Rectangle2D textBounds = null;
    glyphVectors = new LinkedList<>();
    String[] lines = {""};
    if (text != null) {
        lines = text.split("\\|");
    }
    for (String line: lines) {
        final FontRenderContext context = new FontRenderContext(AffineTransform.getScaleInstance(1000.0, 1000.0), true, true);
        final GlyphVector glyphVector = font.createGlyphVector(context, line.trim());
        glyphVectors.add(glyphVector);
        Rectangle2D lineBounds = glyphVector.getVisualBounds();
        if (textBounds != null) {
            textBounds = BoundingBoxHelper.move(textBounds, 0.0, -lineBounds.getHeight());
        }
        textBounds = BoundingBoxHelper.union(textBounds, lineBounds);
    }
    int lineCount = lines.length;
    spacing = (lineCount < 2) ? 0.0 : (spacingRatio * textBounds.getHeight() / (lineCount - 1));
    textBounds = BoundingBoxHelper.transform(textBounds, AffineTransform.getScaleInstance(1.0, 1.0 + spacingRatio));
    double x = xOffset + positioning.xOffset + 0.5 * positioning.xSign * textBounds.getWidth() - textBounds.getCenterX();
    double y = yOffset + positioning.yOffset + 0.5 * positioning.ySign * textBounds.getHeight() - textBounds.getCenterY();
    boundingBox = BoundingBoxHelper.move(textBounds, x, y);
}
 
Example 8
Source File: TextMeasureTests.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 9
Source File: TextMeasureTests.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) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 10
Source File: TextMeasureTests.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 11
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  int w = getWidth();
  int h = getHeight();
  g.setColor(Color.WHITE);
  g.fillRect(0, 0, w, h);

  Graphics2D g2 = (Graphics2D) g.create();
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

  FontRenderContext frc = g2.getFontRenderContext();
  GlyphVector gv = getFont().createGlyphVector(frc, text);
  Rectangle2D b = gv.getVisualBounds();
  double cx = w / 2d - b.getCenterX();
  double cy = h / 2d - b.getCenterY();
  AffineTransform toCenterAtf = AffineTransform.getTranslateInstance(cx, cy);

  double dh = b.getHeight() / 3d;
  Rectangle2D clip = new Rectangle2D.Double(b.getX(), b.getY(), b.getWidth(), b.getHeight());
  Rectangle2D clip1 = new Rectangle2D.Double(b.getX(), b.getY(), b.getWidth(), dh);
  Rectangle2D clip2 = new Rectangle2D.Double(b.getX(), b.getY() + 2d * dh, b.getWidth(), dh);

  Shape s = toCenterAtf.createTransformedShape(gv.getOutline());

  g2.setClip(toCenterAtf.createTransformedShape(clip1));
  g2.setPaint(Color.BLUE);
  g2.fill(s);

  g2.setClip(toCenterAtf.createTransformedShape(clip2));
  g2.setPaint(Color.RED);
  g2.fill(s);

  g2.setClip(toCenterAtf.createTransformedShape(clip));
  g2.setPaint(Color.BLACK);
  g2.draw(s);
  g2.dispose();
}
 
Example 12
Source File: TextMeasureTests.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void runTest(Object ctx, int numReps) {
    GVContext gvctx = (GVContext)ctx;
    GlyphVector gv = gvctx.gv;
    Rectangle2D r;
    do {
        r = gv.getVisualBounds();
    } while (--numReps >= 0);
}
 
Example 13
Source File: TessFont.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private TessOutput tesselateString(String s) {
	GlyphVector gv = _font.createGlyphVector(_frc, s);

    Shape shape = gv.getOutline();
	//
    AffineTransform at = new AffineTransform();
    at.scale(1, -1);
	PathIterator pIt = shape.getPathIterator(at, _font.getSize()/200.0);

	// Create a GLU tesselator
	GLUtessellator tess = GLU.gluNewTess();
	CharTesselator tessAdapt = new CharTesselator();

	GLU.gluTessCallback(tess, GLU.GLU_TESS_VERTEX, tessAdapt);
	GLU.gluTessCallback(tess, GLU.GLU_TESS_BEGIN, tessAdapt);
	GLU.gluTessCallback(tess, GLU.GLU_TESS_END, tessAdapt);
	GLU.gluTessCallback(tess, GLU.GLU_TESS_COMBINE, tessAdapt);
	GLU.gluTessCallback(tess, GLU.GLU_TESS_ERROR, tessAdapt);

	int winding = pIt.getWindingRule();

	if (winding == PathIterator.WIND_EVEN_ODD)
		GLU.gluTessProperty(tess, GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_ODD);
	else if (winding == PathIterator.WIND_NON_ZERO)
		GLU.gluTessProperty(tess, GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_NONZERO);
	else
		assert(false); // PathIterator should only return these two winding rules

	GLU.gluBeginPolygon(tess);
	GLU.gluTessNormal(tess, 0, 0, 1);
	double[] first = null;
	double[] v;
	while (!pIt.isDone()) {
		v = new double[3];
		int type = pIt.currentSegment(v);
		v[2] = 0.0;
		if (type == PathIterator.SEG_MOVETO) {
			first = v;
			GLU.gluNextContour(tess, GLU.GLU_UNKNOWN);
			GLU.gluTessVertex(tess, v, 0, v);
		}
		else if (type == PathIterator.SEG_LINETO) {
			GLU.gluTessVertex(tess, v, 0, v);
		}
		else if (type == PathIterator.SEG_CLOSE) {
			assert(first != null); // If this is true, there is an error in the AWT path iterator
			GLU.gluTessVertex(tess, first, 0, first);
			first = null;
		}
		else
		{
			assert(false); // The path itertor should not return other path types here
		}
		pIt.next();
	}
	GLU.gluEndPolygon(tess);

	int numVerts = tessAdapt.getVerts().size();
	double[] verts = new double[numVerts];
	int count = 0;
	for (double d : tessAdapt.getVerts()) {
		verts[count++] = d;
	}

	TessOutput ret = new TessOutput();
	ret.verts = verts;
	ret.bounds = gv.getVisualBounds();

	ret.advances = new double[s.length()];
	for (int i = 0; i < s.length(); ++i) {
		ret.advances[i] = gv.getGlyphMetrics(i).getAdvance();
	}
	return ret;
}
 
Example 14
Source File: FontPanel.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 15
Source File: FontPanel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 16
Source File: FontPanel.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 17
Source File: FontPanel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 18
Source File: FontPanel.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 19
Source File: FontPanel.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}
 
Example 20
Source File: FontPanel.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void modeSpecificDrawChar( Graphics2D g2, int charCode,
                                  int baseX, int baseY ) {
    GlyphVector gv;
    int oneGlyph[] = { charCode };
    char charArray[] = Character.toChars( charCode );

    FontRenderContext frc = g2.getFontRenderContext();
    AffineTransform oldTX = g2.getTransform();

    /// Create GlyphVector to measure the exact visual advance
    /// Using that number, adjust the position of the character drawn
    if ( textToUse == ALL_GLYPHS )
      gv = testFont.createGlyphVector( frc, oneGlyph );
    else
      gv = testFont.createGlyphVector( frc, charArray );
    Rectangle2D r2d2 = gv.getPixelBounds(frc, 0, 0);
    int shiftedX = baseX;
    // getPixelBounds returns a result in device space.
    // we need to convert back to user space to be able to
    // calculate the shift as baseX is in user space.
    try {
         double pt[] = new double[4];
         pt[0] = r2d2.getX();
         pt[1] = r2d2.getY();
         pt[2] = r2d2.getX()+r2d2.getWidth();
         pt[3] = r2d2.getY()+r2d2.getHeight();
         oldTX.inverseTransform(pt,0,pt,0,2);
         shiftedX = baseX - (int) ( pt[2] / 2 + pt[0] );
    } catch (NoninvertibleTransformException e) {
    }

    /// ABP - keep track of old tform, restore it later

    g2.translate( shiftedX, baseY );
    g2.transform( getAffineTransform( g2Transform ) );

    if ( textToUse == ALL_GLYPHS )
      g2.drawGlyphVector( gv, 0f, 0f );
    else {
        if ( testFont.canDisplay( charCode ))
          g2.setColor( Color.black );
        else {
          g2.setColor( Color.lightGray );
        }

        switch ( drawMethod ) {
          case DRAW_STRING:
            g2.drawString( new String( charArray ), 0, 0 );
            break;
          case DRAW_CHARS:
            g2.drawChars( charArray, 0, 1, 0, 0 );
            break;
          case DRAW_BYTES:
            if ( charCode > 0xff )
              throw new CannotDrawException( DRAW_BYTES_ERROR );
            byte oneByte[] = { (byte) charCode };
            g2.drawBytes( oneByte, 0, 1, 0, 0 );
            break;
          case DRAW_GLYPHV:
            g2.drawGlyphVector( gv, 0f, 0f );
            break;
          case TL_DRAW:
            TextLayout tl = new TextLayout( new String( charArray ), testFont, frc );
            tl.draw( g2, 0f, 0f );
            break;
          case GV_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            g2.draw( gv.getOutline( 0f, 0f ));
            break;
          case TL_OUTLINE:
            r2d2 = gv.getVisualBounds();
            shiftedX = baseX - (int) ( r2d2.getWidth() / 2 + r2d2.getX() );
            TextLayout tlo =
              new TextLayout( new String( charArray ), testFont,
                              g2.getFontRenderContext() );
            g2.draw( tlo.getOutline( null ));
        }
    }

    /// ABP - restore old tform
    g2.setTransform ( oldTX );
}