java.awt.Font#getStringBounds ( )源码实例Demo

下面列出了java.awt.Font#getStringBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-8   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码2 项目: jdk8u-jdk   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码3 项目: TencentKona-8   文件: WPathGraphics.java
private boolean okGDIMetrics(String str, Font font,
                             FontRenderContext frc, double scaleX) {

    Rectangle2D bds = font.getStringBounds(str, frc);
    double jdkAdvance = bds.getWidth();
    jdkAdvance = Math.round(jdkAdvance*scaleX);
    int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str);
    if (jdkAdvance > 0 && gdiAdvance > 0) {
        double diff = Math.abs(gdiAdvance-jdkAdvance);
        double ratio = gdiAdvance/jdkAdvance;
        if (ratio < 1) {
            ratio = 1/ratio;
        }
        return diff <= 1 || ratio < 1.01;
    }
    return true;
}
 
源代码4 项目: TencentKona-8   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码5 项目: jdk8u60   文件: WPathGraphics.java
private boolean okGDIMetrics(String str, Font font,
                             FontRenderContext frc, double scaleX) {

    Rectangle2D bds = font.getStringBounds(str, frc);
    double jdkAdvance = bds.getWidth();
    jdkAdvance = Math.round(jdkAdvance*scaleX);
    int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str);
    if (jdkAdvance > 0 && gdiAdvance > 0) {
        double diff = Math.abs(gdiAdvance-jdkAdvance);
        double ratio = gdiAdvance/jdkAdvance;
        if (ratio < 1) {
            ratio = 1/ratio;
        }
        return diff <= 1 || ratio < 1.002;
    }
    return true;
}
 
源代码6 项目: jdk8u60   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码7 项目: jdk8u-jdk   文件: WPathGraphics.java
private boolean okGDIMetrics(String str, Font font,
                             FontRenderContext frc, double scaleX) {

    Rectangle2D bds = font.getStringBounds(str, frc);
    double jdkAdvance = bds.getWidth();
    jdkAdvance = Math.round(jdkAdvance*scaleX);
    int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str);
    if (jdkAdvance > 0 && gdiAdvance > 0) {
        double diff = Math.abs(gdiAdvance-jdkAdvance);
        double ratio = gdiAdvance/jdkAdvance;
        if (ratio < 1) {
            ratio = 1/ratio;
        }
        return diff <= 1 || ratio < 1.002;
    }
    return true;
}
 
源代码8 项目: gsn   文件: SVGUtils.java
public static Width_Height_Decendent_ValueBean getBoundsOfAString ( String s , Font f ) {
   int width = 0;
   s = s.replace( "\t" , "   " );
   StringTokenizer stringTokenizer = new StringTokenizer( s , "\n" , false );
   FontRenderContext context = new FontRenderContext( null , false , false );
   String [ ] tokens = new String [ stringTokenizer.countTokens( ) ];
   int [ ] heights = new int [ stringTokenizer.countTokens( ) ];
   int [ ] decendents = new int [ stringTokenizer.countTokens( ) ];
   int i = 0;
   while ( stringTokenizer.hasMoreElements( ) ) {
      String nextLine = stringTokenizer.nextToken( ).trim( );
      // TextLayout textLayout = new TextLayout ( nextLine, f, context
      // );
      Rectangle2D temp = f.getStringBounds( nextLine , context );
      // Rectangle2D temp = f.createGlyphVector ( context, nextLine
      // ).getLogicalBounds ();
      decendents[ i ] = ( int ) f.getLineMetrics( s , context ).getDescent( );
      width = Math.max( width , ( int ) temp.getWidth( ) );
      heights[ i ] = ( int ) temp.getHeight( );
      tokens[ i++ ] = nextLine;
      
   }
   return new Width_Height_Decendent_ValueBean( tokens , width , heights , decendents );
}
 
源代码9 项目: openjdk-jdk8u   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码10 项目: rapidminer-studio   文件: ProgressAnimation.java
/**
 * Finds the font that fits into the progress arc. Stores the associated height and width.
 */
private static void initializeFontMeasures() {
	platformSpecificFont = DEFAULT_FONT;
	Graphics2D testGraphics = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB).createGraphics();
	float[] possibleFontSizes = { 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10 };

	for (float size : possibleFontSizes) {
		Font deriveFont = DEFAULT_FONT.deriveFont(size);
		testGraphics.setFont(deriveFont);
		String testString = "54";
		Rectangle2D stringBounds = deriveFont.getStringBounds(testString, testGraphics.getFontRenderContext());

		if (stringBounds.getWidth() <= MAXIMAL_TEXT_WIDTH) {
			platformSpecificFont = deriveFont;
			textWidth = stringBounds.getWidth();
			textHeight = deriveFont.createGlyphVector(testGraphics.getFontRenderContext(), testString).getVisualBounds()
					.getHeight();
			break;
		}

	}
}
 
源代码11 项目: openjdk-jdk8u-backup   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码12 项目: openjdk-jdk9   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码13 项目: jdk8u-dev-jdk   文件: StyledSpaceAdvance.java
static void test(int sz) {
     Font reg = new Font(name, Font.PLAIN, sz);
     Font bold = new Font(name, Font.BOLD, sz);
     //System.out.println("reg="+reg);
     //System.out.println("bold="+bold);
     FontRenderContext frc = new FontRenderContext(null, false, false);
     if (reg.getFontName(Locale.ENGLISH).equals(name) &&
         bold.getFontName(Locale.ENGLISH).equals(name)) {
         Rectangle2D rb = reg.getStringBounds(" ", frc);
         Rectangle2D bb = bold.getStringBounds(" ", frc);
         if (bb.getWidth() > rb.getWidth() + 1.01f) {
             System.err.println("reg="+reg+" bds = " + rb);
             System.err.println("bold="+bold+" bds = " + bb);
             throw new RuntimeException("Advance difference too great.");
         }
     } else {
         System.out.println("Skipping test because fonts aren't as expected");
     }
}
 
源代码14 项目: JavaWeb   文件: AllOpenController.java
private String drawImg(ByteArrayOutputStream output){
	//final int verifyCodeLength = 10;
	final int verifyCodeLength = 4;
	String code = getVerifyCode(verifyCodeLength);
	//int width = 125;
	int width = 70;
	int height = 25;
	BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
	Font font = new Font("Times New Roman",Font.PLAIN,20);
	Graphics2D g = bi.createGraphics();
	g.setFont(font);
	g.setColor(new Color(66,2,82));
	g.setBackground(new Color(226,226,240));
	g.clearRect(0, 0, width, height);
	FontRenderContext context = g.getFontRenderContext();
	Rectangle2D bounds = font.getStringBounds(code, context);
	double x = (width - bounds.getWidth()) / 2;
	double y = (height - bounds.getHeight()) / 2;
	double ascent = bounds.getY();
	double baseY = y - ascent;
	g.drawString(code, (int)x, (int)baseY);
	g.dispose();
	try {
		ImageIO.write(bi, "jpg", output);
	} catch (IOException e) {
		//do nothing
	}
	return code;
}
 
源代码15 项目: nextreports-designer   文件: MemoryStatus.java
public MemoryStatus() {
//        Font font = new JLabel().getFont();
        Font font = UIManager.getFont("Label.font");
        MemoryStatus.this.setFont(font);

        FontRenderContext fontRendererContext = new FontRenderContext(null, false, false);
        Rectangle2D bounds = font.getStringBounds(MEMORY_TEST_STRING, fontRendererContext);
        Dimension dimension = new Dimension((int) bounds.getWidth(), (int) bounds.getHeight());
        setPreferredSize(dimension);
        setMaximumSize(dimension);
        lineMetrics = font.getLineMetrics(MEMORY_TEST_STRING, fontRendererContext);        
        addMouseListener(new MouseHandler());
    }
 
源代码16 项目: jdk8u_jdk   文件: StyledFontLayoutTest.java
private static void runTest() {
    im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = im.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, W, H);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chs = "Sample Text.".toCharArray();
    int len = chs.length;

    int x = 50, y = 100;

    FontRenderContext frc = g2d.getFontRenderContext();
    Font plain = new Font("Serif", Font.PLAIN, 48);
    GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
    g2d.setFont(plain);
    g2d.drawChars(chs, 0, len, x, y); y +=50;

    g2d.drawGlyphVector(pgv, x, y); y += 50;
    Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
    Rectangle2D plainGVBounds = pgv.getLogicalBounds();
    Font bold = new Font("Serif", Font.BOLD, 48);
    GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
    Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
    Rectangle2D boldGVBounds = bgv.getLogicalBounds();
    g2d.setFont(bold);
    g2d.drawChars(chs, 0, len, x, y); y +=50;
    g2d.drawGlyphVector(bgv, x, y);
    System.out.println("Plain String Bounds = " + plainStrBounds);
    System.out.println("Bold String Bounds = " + boldStrBounds);
    System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
    System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
    if (!plainStrBounds.equals(boldStrBounds) &&
         plainGVBounds.equals(boldGVBounds))
    {
        System.out.println("Test failed: Plain GV bounds same as Bold");
        if (!interactive) {
            throw new RuntimeException("Plain GV bounds same as Bold");
        }
    }

}
 
源代码17 项目: gama   文件: FontCache.java
@Override
public Rectangle2D getBounds(final CharSequence str, final Font font, final FontRenderContext frc) {
	return font.getStringBounds(str.toString(), frc);
}
 
源代码18 项目: geomajas-project-server   文件: TextServiceImpl.java
@Override
public Rectangle2D getStringBounds(String text, FontStyleInfo fontStyle) {
	Font font = getFont(fontStyle);
	return font.getStringBounds(text, 0, text.length(), new FontRenderContext(new AffineTransform(), true, true));
}
 
源代码19 项目: openjdk-jdk9   文件: StyledFontLayoutTest.java
private static void runTest() {
    im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = im.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, W, H);
    g2d.setColor(Color.black);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    char[] chs = "Sample Text.".toCharArray();
    int len = chs.length;

    int x = 50, y = 100;

    FontRenderContext frc = g2d.getFontRenderContext();
    Font plain = new Font("Serif", Font.PLAIN, 48);
    GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
    g2d.setFont(plain);
    g2d.drawChars(chs, 0, len, x, y); y +=50;

    g2d.drawGlyphVector(pgv, x, y); y += 50;
    Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
    Rectangle2D plainGVBounds = pgv.getLogicalBounds();
    Font bold = new Font("Serif", Font.BOLD, 48);
    GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
    Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
    Rectangle2D boldGVBounds = bgv.getLogicalBounds();
    g2d.setFont(bold);
    g2d.drawChars(chs, 0, len, x, y); y +=50;
    g2d.drawGlyphVector(bgv, x, y);
    System.out.println("Plain String Bounds = " + plainStrBounds);
    System.out.println("Bold String Bounds = " + boldStrBounds);
    System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
    System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
    if (!plainStrBounds.equals(boldStrBounds) &&
         plainGVBounds.equals(boldGVBounds))
    {
        System.out.println("Test failed: Plain GV bounds same as Bold");
        if (!interactive) {
            throw new RuntimeException("Plain GV bounds same as Bold");
        }
    }

}
 
源代码20 项目: gama   文件: FontCache.java
@Override
public Rectangle2D getBounds(final String str, final Font font, final FontRenderContext frc) {
	return font.getStringBounds(str, frc);
}