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

The following examples show how to use java.awt.font.GlyphVector#getPixelBounds() . 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: 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.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
    } while (--numReps >= 0);
}
 
Example 2
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.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
    } while (--numReps >= 0);
}
 
Example 3
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.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
    } while (--numReps >= 0);
}
 
Example 4
Source File: FlexWordlePanel.java    From swcv with MIT License 5 votes vote down vote up
private double computeOriginalScale(String word, double screenWidth, double screenHeight, Graphics2D g2)
{
    Font font = FontUtils.getFont();
    FontRenderContext frc = g2.getFontRenderContext();

    //get a new position for the text
    GlyphVector gv = font.layoutGlyphVector(frc, word.toCharArray(), 0, word.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    Rectangle2D bb = gv.getPixelBounds(frc, 0, 0);

    double scaleX = screenWidth / bb.getWidth();
    double scaleY = screenHeight / bb.getHeight();

    double scale = Math.min(scaleX, scaleY);
    return scale * 0.75;
}
 
Example 5
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.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
    } while (--numReps >= 0);
}
 
Example 6
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.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
    } while (--numReps >= 0);
}
 
Example 7
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.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
    } while (--numReps >= 0);
}
 
Example 8
Source File: FontUtils.java    From swcv with MIT License 5 votes vote down vote up
@Override
public SWCRectangle getBoundingBox(String text)
{
    Font font = getFont();

    FontRenderContext frc = ((Graphics2D)dummy.getGraphics()).getFontRenderContext();

    GlyphVector gv = font.layoutGlyphVector(frc, text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
    Rectangle2D bb = gv.getPixelBounds(frc, 0, 0);

    return new SWCRectangle(bb.getX(), bb.getY(), bb.getWidth(), bb.getHeight());
}
 
Example 9
Source File: FlexWordlePanel.java    From swcv with MIT License 5 votes vote down vote up
private boolean canPlaceWord(String word, GlyphVector gv, double screenWidth, double screenHeight, Graphics2D g2)
{
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D bb = gv.getPixelBounds(frc, 0, 0);

    double x = rnd.nextDouble() * screenWidth;
    double y = rnd.nextDouble() * screenHeight;

    boolean fit = (0 <= x && x <= screenWidth - bb.getWidth() && bb.getHeight() <= y && y <= screenHeight - bb.getHeight());
    if (!fit)
        return false;

    Shape outline1 = gv.getOutline((float)x, (float)y);
    Area newArea = new Area(outline1);

    for (Area ua : occupiedAreas)
    {
        if (!ua.getBounds().intersects(newArea.getBounds()))
            continue;

        Area ar2 = new Area(ua);
        ar2.intersect(newArea);
        if (!ar2.isEmpty())
            return false;
    }

    g2.setColor(getWordColor(word, gv, x, y));
    g2.drawGlyphVector(gv, (float)x, (float)y);
    occupiedAreas.add(newArea);

    return true;
}
 
Example 10
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 11
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 );
}
 
Example 12
Source File: AboutBox.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
private Rectangle getStringBounds(Graphics2D g2, String str, float x, float y) {
	FontRenderContext frc = g2.getFontRenderContext();
	GlyphVector gv = g2.getFont().createGlyphVector(frc, str);
	return gv.getPixelBounds(null, x, y);
}
 
Example 13
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 14
Source File: SymbolRipper.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void paintComponent (Graphics g)
{
    // For background
    super.paintComponent(g);

    // Meant for visual check
    if (image != null) {
        Graphics2D g2 = (Graphics2D) g;

        g2.drawImage(image, 1, 1, this);

        g.setColor(Color.BLUE);
        g.drawLine(
                0,
                yOffset.getValue(),
                width.getValue(),
                yOffset.getValue());
        g.drawLine(
                xOffset.getValue(),
                0,
                xOffset.getValue(),
                height.getValue());

        g.setColor(Color.ORANGE);
        g.drawRect(0, 0, width.getValue(), height.getValue());

        FontRenderContext frc = g2.getFontRenderContext();
        GlyphVector glyphVector = musicFont.createGlyphVector(
                frc,
                string);

        Rectangle rect = glyphVector.getPixelBounds(
                frc,
                xOffset.getValue(),
                yOffset.getValue());
        g.setColor(Color.RED);
        g2.draw(rect);

        // Debug
        TextLayout layout = new TextLayout(string, musicFont, frc);
        logger.debug(
                "getAdvance(): {} getVisibleAdvance(): {}",
                layout.getAdvance(),
                layout.getVisibleAdvance());
    }
}
 
Example 15
Source File: FontPanel.java    From openjdk-jdk9 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 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 17
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 18
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 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 dragonwell8_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 );
}