import java.awt.Font; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; ... String text = "Hello World"; AffineTransform affinetransform = new AffineTransform(); FontRenderContext frc = new FontRenderContext(affinetransform,true,true); Font font = new Font("Tahoma", Font.PLAIN, 12); int textwidth = (int)(font.getStringBounds(text, frc).getWidth()); int textheight = (int)(font.getStringBounds(text, frc).getHeight());
private FontRenderContext getCurrentContext(Editor editor) { FontRenderContext editorContext = FontInfo.getFontRenderContext(editor.getContentComponent()); return new FontRenderContext(editorContext.getTransform(), AntialiasingType.getKeyForCurrentScope(false), editor instanceof EditorImpl ? ((EditorImpl)editor).myFractionalMetricsHintValue : RenderingHints.VALUE_FRACTIONALMETRICS_OFF); } }
private void paintSum(Graphics2D g, Rectangle2D pieBounds, double total) { // draw total value if visible if (pieStyler.isSumVisible()) { SumFormatter sumFormatter = pieStyler.getSumFormatter(); String annotation = sumFormatter.format(total); TextLayout textLayout = new TextLayout( annotation, pieStyler.getSumFont(), new FontRenderContext(null, true, false)); Shape shape = textLayout.getOutline(null); g.setColor(pieStyler.getChartFontColor()); // compute center Rectangle2D annotationRectangle = textLayout.getBounds(); double xCenter = pieBounds.getX() + pieBounds.getWidth() / 2 - annotationRectangle.getWidth() / 2; double yCenter = pieBounds.getY() + pieBounds.getHeight() / 2 + annotationRectangle.getHeight() / 2; // set text AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform(); at.translate(xCenter, yCenter); g.transform(at); g.fill(shape); g.setTransform(orig); } } }
@Override public void accept(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); Graphics2D g = (Graphics2D) image.getGraphics(); g.setTransform(AffineTransform.getRotateInstance(Math.toRadians(-45))); int y = height / 4; // calculate the rectangle where the label is rendered FontRenderContext frc = new FontRenderContext(g.getTransform(), true, true); int maxLabelWidth = calculateMaxLabelWidth(y); g.setFont(getFont(maxLabelWidth, frc)); Rectangle2D labelBounds = g.getFont().getStringBounds(label, frc); // draw the ribbon g.setColor(ribbonColor); g.fillRect(-width, y, width * 2, (int) (labelBounds.getHeight())); // draw the label g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(labelColor); FontMetrics fm = g.getFontMetrics(); drawString(g, label, (int) -labelBounds.getWidth() / 2, y + fm.getAscent()); g.dispose(); }
private BufferedImage drawImage(String string,Font font){ FontRenderContext context=new FontRenderContext(null,false,true); GlyphVector glyphVector=font.createGlyphVector(context,string); float x=(float)glyphVector.getVisualBounds().getX(); float y=(float)glyphVector.getVisualBounds().getY(); int width=(int)(glyphVector.getVisualBounds().getWidth()+1); int height=(int)(glyphVector.getVisualBounds().getHeight()+1); BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_BINARY); Graphics2D g2d=bi.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0,0,width,height); g2d.setColor(Color.BLACK); g2d.drawGlyphVector(glyphVector,-x,-y); return bi; } public static void main(String[] args){
Font font = new Font("Default",0,size*150/200); FontRenderContext frc = new FontRenderContext(new AffineTransform(),true,true); g.setPaint(new GradientPaint(0,bi.getHeight(),Color.red,bi.getWidth(),0,Color.yellow,true)); } else { g.setPaint(new GradientPaint(0,0,Color.blue,bi.getWidth(),bi.getHeight(),Color.green,true)); g.setPaint(new Color(0xE19839)); } else { g.setPaint(new Color(0x3B4E92)); g.setColor(Color.black); g.setFont(font); float width = (float)font.getStringBounds(text,frc).getWidth(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
&& axesChartStyler.isYAxisTitleVisible()) { TextLayout textLayout = new TextLayout( yAxisTitle, axesChartStyler.getAxisTitleFont(), new FontRenderContext(null, true, false)); Rectangle2D rectangle = textLayout.getBounds(); titleHeight = rectangle.getHeight() + axesChartStyler.getAxisTitlePadding(); new TextLayout( sampleLabel.length() == 0 ? " " : sampleLabel, axesChartStyler.getAxisTickLabelsFont(), new FontRenderContext(null, true, false)); Rectangle2D rectangle = textLayout.getBounds(); rectangle.getWidth() + axesChartStyler.getAxisTickPadding() + axesChartStyler.getAxisTickMarkLength();
static void drawString(Graphics2D g, String str, int x, int y) { g.drawString(str, x, y); if (debug) { FontMetrics fm = g.getFontMetrics(); Rectangle2D bounds = g.getFont().getStringBounds(str, new FontRenderContext(g.getTransform(), true, true)); g.drawRect(x, y - fm.getAscent(), (int) bounds.getWidth(), fm.getAscent()); } }
public void addSample(Font font,int codePoint,int toCodePoint){ if(!font.canDisplay(codePoint)){ return; } FontRenderContext context=new FontRenderContext(null,false,true); GlyphVector glyphVector=font.createGlyphVector(context,new String(new int[]{codePoint},0,1)); float x=(float)glyphVector.getVisualBounds().getX(); float y=(float)glyphVector.getVisualBounds().getY(); int width=(int)(glyphVector.getVisualBounds().getWidth()+0.5); int height=(int)(glyphVector.getVisualBounds().getHeight()+0.5); int fontSize=(int)(glyphVector.getLogicalBounds().getHeight()+0.5); if(width==0||height==0){ return; } BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_BINARY); Graphics2D g2d=bi.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0,0,width,height); g2d.setColor(Color.BLACK); g2d.drawGlyphVector(glyphVector,-x,-y); ConnectedComponent ele=new ConnectedComponent(bi); BoundBox box=new BoundBox((int)x,(int)x+width,(int)y,(int)y+height); CharacterPrototype prototype=new CharacterPrototype(toCodePoint,box,font.getFamily(),fontSize,font.getStyle()); addSample(prototype,ele); } public int getCount(){
public void sayWorld(Graphics2D g2D, int x, int y, boolean shear) { final String txt = "Hello World!"; // gradient color from blue to red GradientPaint gp = new GradientPaint((float)x, (float)y, Color.blue, x+100, y+20, Color.red); g2D.setPaint(gp); if (shear) g2D.shear(-0.5,0.0); else g2D.shear(+0.5, 0); g2D.drawString(txt, x, y); FontRenderContext frc = new FontRenderContext(null,false,false); TextLayout tl = new TextLayout(txt, font, frc); AffineTransform textAt = new AffineTransform(); //textAt.translate(0, (float)tl.getBounds().getHeight()); textAt.translate(x,y); //textAt.shear(-0.5,0.0); Shape outline = tl.getOutline(textAt); g2D.setColor(Color.yellow); BasicStroke wideStroke = new BasicStroke(2); g2D.setStroke(wideStroke); g2D.draw(outline); }
@Override public AffineTransform getTransform() { if(font == null) return AffineTransform.getScaleInstance(1, 1); AffineTransform transform = new AffineTransform(); FontRenderContext frc = new FontRenderContext(null, antiAliased, fractionalMetrics); TextLayout layout = new TextLayout(text, font, frc); Rectangle2D bounds = layout.getBounds(); float height = size; float width = (float) Math.ceil(bounds.getWidth()); if(angle != 0) { transform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(angle),x+w/2, y+h/2)); } if(scaleX != 1 || scaleY != 1) { double sw = width*scaleX; double sh = height*scaleY; double dx = sw/2-width/2; double dy = sh/2-height/2; transform.translate(x-width/2-dx, y-height/2+dy); AffineTransform tr2 = new AffineTransform(); tr2.translate(width/2, height/2); tr2.scale(scaleX, scaleY); tr2.translate(-x, -y); transform.concatenate(tr2); } return transform; }
Font font = Font.createFont(Font.TRUETYPE_FONT, iStream); iStream.close(); AffineTransform at = AffineTransform.getTranslateInstance(0, 0); FontRenderContext fontCtx = new FontRenderContext(at, true, true); TextLayout tl = new TextLayout(text, font, fontCtx); Shape glyphOutline = tl.getOutline(at); double width = bounds2D.getWidth(); double height = bounds2D.getHeight(); at = AffineTransform.getScaleInstance(dim.getX() / width, dim.getY() / height); fontCtx = new FontRenderContext(at, true, true); tl = new TextLayout(text, font, fontCtx); glyphOutline = tl.getOutline(at); at = AffineTransform.getTranslateInstance(-gb.getCenterX(), -gb.getCenterY()); return at.createTransformedShape(glyphOutline);
g2d.setColor(relief); g2d.setStroke(fat); this.drawLines(g2d, x0, y0, extra); g2d.setColor(color); g2d.setStroke(thin); this.drawLines(g2d, x0, y0, extra); FontRenderContext frc = new FontRenderContext(null, true, true); String[] labels = this.getLabels(); for (int i = 0; i < labels.length; i++) { TextLayout layout = new TextLayout(labels[i], font, frc); // NOPMD Rectangle2D bounds = layout.getBounds(); double x = x0 + extra + cLabels[i] / unitsPerPixel - bounds.getCenterX(); double y = y0 + bounds.getHeight() + 5; if (relief != null) { Shape outline = layout.getOutline(AffineTransform.getTranslateInstance(x, y + 0.5)); g2d.setStroke(outlineStroke);
public AWTFontMetrics( final FontNativeContext record, final Font font, final FontContext context ) { this.record = record; this.font = font; this.frc = new FontRenderContext( null, context.isAntiAliased(), context.isFractionalMetrics() ); final Graphics2D graphics = createGraphics( context ); // This is some sort of evil hack and does only play well as long as we deal with scalable fonts // It will happily cause troubles with bitmap fonts. java.awt.FontMetrics fontMetrics = graphics.getFontMetrics( font.deriveFont( font.getSize2D() * 1000 ) ); this.leading = fontMetrics.getLeading(); this.maxAscent = fontMetrics.getMaxAscent(); this.maxDescent = fontMetrics.getMaxDescent(); this.uniformLineMetrics = fontMetrics.hasUniformLineMetrics(); final Rectangle2D rect = this.font.getMaxCharBounds( frc ); this.maxCharAdvance = FontStrictGeomUtility.toInternalValue( rect.getWidth() ); this.ascent = FontStrictGeomUtility.toInternalValue( -rect.getY() ); this.descent = FontStrictGeomUtility.toInternalValue( rect.getHeight() + rect.getY() ); final GlyphVector gv = font.createGlyphVector( frc, "x" ); final Rectangle2D bounds = gv.getVisualBounds(); this.xheight = FontStrictGeomUtility.toInternalValue( bounds.getHeight() ); this.cpBuffer = new char[ 4 ]; this.cachedBaselines = new BaselineInfo[ 256 - 32 ]; this.cachedWidths = new long[ 256 - 32 ]; Arrays.fill( cachedWidths, -1 ); }
localFRC = new FontRenderContext(new AffineTransform(), frc.isAntiAliased(), frc.usesFractionalMetrics()); String fontSize = doubleString(font.getSize2D()) + "px"; String fontWeight = weightToSVG(font); String fontStyle = styleToSVG(font); String fontKey = (commonSizeFont.getFamily() + commonSizeFont.getStyle()); GlyphVector gv; gv = commonSizeFont.createGlyphVector(localFRC, missingGlyphCode); Shape missingGlyphShape = gv.getGlyphOutline(0); GlyphMetrics gm = gv.getGlyphMetrics(0); AffineTransform at = AffineTransform.getScaleInstance(1, -1); missingGlyphShape = at.createTransformedShape(missingGlyphShape); gv = commonSizeFont.createGlyphVector(localFRC, ""+c); Shape glyphShape = gv.getGlyphOutline(0); GlyphMetrics gm = gv.getGlyphMetrics(0);
public Shape createStrokedShape(Shape shape) { FontRenderContext frc = new FontRenderContext(null, true, true); GlyphVector glyphVector = font.createGlyphVector(frc, text); float next = 0; int currentChar = 0; int length = glyphVector.getNumGlyphs(); / (float) glyphVector.getLogicalBounds().getWidth() : 1.0f; float height = (float) glyphVector.getLogicalBounds().getHeight(); float nextAdvance = 0; nextAdvance = currentChar < length - 1 ? glyphVector.getGlyphMetrics( currentChar + 1).getAdvance() * 0.5f : 0; t.setToTranslation(x, y); t.rotate(angle); t.translate(-px - advance, -py + height * factor / 2.0f); result.append(t.createTransformedShape(glyph), false); next += (advance + nextAdvance) * factor;
@Override public void simpleDraw(Graphics g, int x, int y) { if (font == null) { font = g.getFont().deriveFont(style, size); } g.setFont(font); if (!border) { g.setColor(color); g.drawString(text, x, y); } else { FontRenderContext frc = new FontRenderContext(null, antiAliased, fractionalMetrics); TextLayout layout = new TextLayout(text, font, frc); Shape sha = layout.getOutline(AffineTransform.getTranslateInstance(x, y)); g.getGraphics().setStroke(new BasicStroke(borderWidth)); g.setColor(borderColor); g.draw(sha); g.setColor(color); g.fill(sha); g.getGraphics().setStroke(new BasicStroke(1)); } }
private double getTrueTypeGlyphMaxSize(Map<String,Object> map, /*Double scale, Double dpi,*/ RealParameter markIndex) throws IOException, ParameterException { try { InputStream iStream = uri.toURL().openStream(); Font font = Font.createFont(Font.TRUETYPE_FONT, iStream); iStream.close(); double value = markIndex.getValue(map); char[] data = {(char) value}; String text = String.copyValueOf(data); // Scale is used to have an high resolution AffineTransform at = AffineTransform.getTranslateInstance(0, 0); FontRenderContext fontCtx = new FontRenderContext(at, true, true); TextLayout tl = new TextLayout(text, font, fontCtx); Shape glyphOutline = tl.getOutline(at); Rectangle2D bounds2D = glyphOutline.getBounds2D(); return Math.max(bounds2D.getWidth(), bounds2D.getHeight()); } catch (FontFormatException ex) { Logger.getLogger(OnlineResource.class.getName()).log(Level.SEVERE, null, ex); throw new ParameterException(ex); } }
@Override public void paint(Graphics g) { try { // translate location Point srcLocation = new Point(0, 0); Point dstLocation = SwingUtilities.convertPoint(scrollPane, srcLocation, this); Graphics2D g2 = (Graphics2D) g.create(); g2.translate(dstLocation.x, dstLocation.y); // enable AntiAliasing if (!g2.getFontRenderContext().isAntiAliased()) { g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } // draw marker for (Marker marker : markers) { drawMarker(g2, marker.location, String.valueOf(Character.toChars(marker.title))); } g2.dispose(); } catch (Exception e) { System.out.println(e.getMessage()); } }
@Before public void setUp() throws TransformException, FactoryException { graphics = Mockito.mock(Graphics2D.class); Mockito.when(graphics.getFontRenderContext()) .thenReturn( new FontRenderContext( new AffineTransform(), RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT)); style = new TextStyle2D(); style.setFont(new Font("Serif", Font.PLAIN, 10)); shape = new LiteShape2( geometryFactory.createPoint(new Coordinate(10, 10)), ProjectiveTransform.create(new AffineTransform()), null, false); symbolizer = styleFactory.createTextSymbolizer(); }