URL url = new URL("http://i.stack.imgur.com/Nqf3H.jpg"); BufferedImage originalImage = ImageIO.read(url); final BufferedImage textImage = new BufferedImage( originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = textImage.createGraphics(); FontRenderContext frc = g.getFontRenderContext(); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 250); GlyphVector gv = font.createGlyphVector(frc, "Cat"); Rectangle2D box = gv.getVisualBounds(); int xOff = 25+(int)-box.getX(); int yOff = 80+(int)-box.getY(); Shape shape = gv.getOutline(xOff,yOff); g.setClip(shape); g.drawImage(originalImage,0,0,null); g.setClip(null); g.setStroke(new BasicStroke(2f)); g.setColor(Color.BLACK); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
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; moveY = lastY = points[1]; result.moveTo(moveX, moveY); nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f; next = nextAdvance; break; float angle = (float) FastMath.atan2(dy, dx); while (currentChar < length && distance >= next) { Shape glyph = glyphVector.getGlyphOutline(currentChar); Point2D p = glyphVector.getGlyphPosition(currentChar); float px = (float) p.getX(); float py = (float) p.getY(); float y = lastY + next * dy * r; float advance = nextAdvance; 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);
/** Queues the glyphs in the specified text to be loaded. Note that the glyphs are not actually loaded until * {@link #loadGlyphs()} is called. */ public void addGlyphs (String text) { if (text == null) throw new IllegalArgumentException("text cannot be null."); char[] chars = text.toCharArray(); GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT); for (int i = 0, n = vector.getNumGlyphs(); i < n; i++) { int codePoint = text.codePointAt(vector.getGlyphCharIndex(i)); Rectangle bounds = getGlyphBounds(vector, i, codePoint); getGlyph(vector.getGlyphCode(i), codePoint, bounds, vector, i); } }
public int getWidth (String text) { if (text == null) throw new IllegalArgumentException("text cannot be null."); if (text.length() == 0) return 0; char[] chars = text.toCharArray(); GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT); int width = 0; int extraX = 0; boolean startNewLine = false; for (int glyphIndex = 0, n = vector.getNumGlyphs(); glyphIndex < n; glyphIndex++) { int charIndex = vector.getGlyphCharIndex(glyphIndex); int codePoint = text.codePointAt(charIndex); Rectangle bounds = getGlyphBounds(vector, glyphIndex, codePoint); if (startNewLine && codePoint != '\n') extraX = -bounds.x; if (glyphIndex > 0) extraX += paddingLeft + paddingRight + paddingAdvanceX; width = Math.max(width, bounds.x + extraX + bounds.width); if (codePoint == '\n') startNewLine = true; } return width; }
public static Shape generateShapeFromText(Font font, String string) { BufferedImage img = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = img.createGraphics(); try { GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string); Shape shape = vect.getOutline(0f,(float)-vect.getVisualBounds().getY()); return shape; } finally { g2.dispose(); } } }
static Font font = new Font("Sans-Serif", Font.PLAIN, 50); static Random rnd = new Random(); ArrayList<Shape> regions = new ArrayList<Shape>(); PathIterator pi = shape.getPathIterator(null); int ii = 0; GeneralPath gp = new GeneralPath(); Color bgColor, Color outlineColor, boolean blackSquare) { GlyphVector gv = font.createGlyphVector(frc, s); Rectangle2D box1 = gv.getVisualBounds(); Shape shape1 = gv.getOutline(); Rectangle r = shape1.getBounds(); System.out.println("shape rect: " + r); int spaceX = sz - r.width; int spaceY = sz - r.height; AffineTransform trans = AffineTransform.getTranslateInstance( -r.x + (spaceX / 2), -r.y + (spaceY / 2)); System.out.println("Box2D " + trans); Shape shapeCentered = trans.createTransformedShape(shape1); Rectangle r1 = region.getBounds(); if (r1.getX() < 0.001 && r1.getY() < 0.001) { } else {
@SuppressWarnings("PMD.SystemPrintln") public static void main(String[] args) { BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2d = (Graphics2D) image.getGraphics(); g2d.setColor(Color.BLACK); Font font = new Font("Webdings", Font.PLAIN, 60); for (int i = 0; i < 65536; i++) if (font.canDisplay(i)) System.out.println(((int) i) + ": " + Long.toHexString(i)); GlyphVector textGlyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, new char[] {,}); Shape shape = textGlyphVector.getOutline(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.translate(150, 150); g2d.setColor(Color.BLUE); g2d.fill(shape);
private ConnectedComponent getComponent(Font font,int codePoint){ 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); 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 new ConnectedComponent(bi); } }
private static void paintCharacter(Graphics2D g2, Font font, Character letter, Color textColor, int x, int y, int w, int h) { GlyphVector firstLetterGV = font.createGlyphVector(g2.getFontRenderContext(), letter.toString()); Shape firstLetterShape = firstLetterGV.getOutline(); Rectangle2D rect = firstLetterShape.getBounds2D(); double sx = (w - 1) / rect.getWidth(); double sy = (h - 1) / rect.getHeight(); AffineTransform t = new AffineTransform(); t.translate(x, y); t.scale(sx, sy); t.translate(-rect.getX(), -rect.getY()); Shape s = t.createTransformedShape(firstLetterShape); g2.setColor(textColor); g2.fill(s); } }
/** * Renders text as image. * * @param text The text. * @param size Size of the image. * @param color Color of the text. * @return The image. */ private Image generateTextImage(String text, float scaling, Dimension size, Color color) { Image ret = new BufferedImage(size.width, size.height, BufferedImage.TYPE_4BYTE_ABGR_PRE); Graphics2D g = ((BufferedImage) ret).createGraphics(); g.setColor(Color.BLACK); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); Font font = iconfont.deriveFont(BASE_ICON_SIZE * 0.5625f * scaling); Shape s = font.createGlyphVector(g.getFontRenderContext(), text). getOutline(0f, 0f); Rectangle b = s.getBounds(); g.translate(size.width * 0.5 - b.getWidth() * 0.5 - b.getX(), size.height * 0.5 - b.getHeight() * 0.5 - b.getY()); g.fill(s); g.dispose(); return ret; }
private void paintBottomText(Graphics2D g2, int x, int y, int w, int h) { g2 = (Graphics2D) g2.create(); GlyphVector otherGV = this.getFont().createGlyphVector( g2.getFontRenderContext(), text); Shape bottomTextShape = otherGV.getOutline(); Rectangle2D rect = bottomTextShape.getBounds2D(); AffineTransform t = new AffineTransform(); t.translate(x, y + (h + rect.getWidth()) / 2); t.rotate(-Math.PI / 2); t.translate(-rect.getX(), -rect.getY()); g2.setColor(textColor); Shape s = t.createTransformedShape(bottomTextShape); g2.fill(s); g2.dispose(); } }
@Override public int getTextHeight(String text) { Graphics2D graphics2d = bufferedImage.createGraphics(); FontMetrics fontMetrics = graphics2d.getFontMetrics(this.font); graphics2d.dispose(); return (int) this.font.createGlyphVector(fontMetrics.getFontRenderContext(), text).getVisualBounds().getHeight(); }
/** * Draw a String centered in the middle of a Rectangle. * * @param g The Graphics instance. * @param text The String to draw. * @param rect The Rectangle to center the text in. * @param font */ public void drawCenteredStringWOutline(Graphics2D g, String text, Rectangle rect, Font font) { // Get the FontMetrics FontMetrics metrics = g.getFontMetrics(font); // Determine the X coordinate for the text int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2; // Determine the Y coordinate for the text (note we add the ascent, as in java 2d 0 is top of the screen) int y = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent(); // Set the font g.setFont(font); GlyphVector gv = font.createGlyphVector(g.getFontRenderContext(), text); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g.drawGlyphVector(gv, x, y); g.translate(x - 1, y - 1); for (int i = 0; i < text.length(); i++) { g.setColor(Color.BLACK); g.draw(gv.getGlyphOutline(i)); } g.translate(-x + 1, -y + 1); }
Font f = getFont().deriveFont(Font.BOLD, 70); GlyphVector v = f.createGlyphVector(getFontMetrics(f).getFontRenderContext(), "Hello"); s = v.getOutline(); setPreferredSize(new Dimension(300,300)); super.paintComponent(g); Graphics2D g2 = (Graphics2D)g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.translate(100, 150); g2.rotate(0.4); g2.setPaint(Color.red); g2.fill(s);
Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 24); FontRenderContext frc = g2.getFontRenderContext(); g2.translate(40, 80); GlyphVector gv = font.createGlyphVector(frc, s); int length = gv.getNumGlyphs(); for (int i = 0; i < length; i++) { Point2D p = gv.getGlyphPosition(i); double theta = (double) i / (double) (length - 1) * Math.PI / 4; AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY()); at.rotate(theta); Shape glyph = gv.getGlyphOutline(i); Shape transformedGlyph = at.createTransformedShape(glyph); g2.fill(transformedGlyph);
private Shape getShape(String fontFamilyName, char character) { Font font = FontCache.getDefaultInstance().getFont(fontFamilyName); if (font == null) { throw new IllegalArgumentException("Unknown font " + fontFamilyName); } // handle charmap code reporting issues if (!font.canDisplay(character)) { char alternative = (char) (0xF000 | character); if (font.canDisplay(alternative)) { character = alternative; } } // build the shape out of the font GlyphVector textGlyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, new char[] {(char) character}); Shape s = textGlyphVector.getOutline(); // have the shape be centered in the origin, and sitting in a square of side 1 Rectangle2D bounds = s.getBounds2D(); AffineTransform tx = new AffineTransform(); double max = Math.max(bounds.getWidth(), bounds.getHeight()); // all shapes are defined looking "upwards" (see ShapeMarkFactory or WellKnownMarkFactory) // but the fonts ones are flipped to compensate for the fact the y coords grow from top // to bottom on the screen. We have to flip the symbol so that it conforms to the // other marks convention tx.scale(1 / max, -1 / max); tx.translate(-bounds.getCenterX(), -bounds.getCenterY()); ExplicitBoundsShape shape = new ExplicitBoundsShape(tx.createTransformedShape(s)); shape.setBounds(new Rectangle2D.Double(-0.5, 0.5, 1.0, 1.0)); return shape; }
Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); String s = "dasdasdasd1"; Font font = new Font("Courier", Font.PLAIN, 12); g2d.translate(20, 20); FontRenderContext frc = g2d.getFontRenderContext(); GlyphVector gv = font.createGlyphVector(frc, s); int length = gv.getNumGlyphs(); Rectangle2D barra=new Rectangle2D.Double(0, 0, 700, 500); g2d.draw(barra); for (int i = 0; i < length; i++) { Point2D p = gv.getGlyphPosition(i); AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY()); at.rotate((double) i / (double) (length - 1) * Math.PI / 3); Shape glyph = gv.getGlyphOutline(i); Shape transformedGlyph = at.createTransformedShape(glyph); g2d.fill(transformedGlyph); }
if (labelItem.getTextStyle().getFont().getSize() == 0) return; AffineTransform oldTransform = graphics.getTransform(); LineInfo line = lines.get(0); Coordinate c = new Coordinate(); c = cursor.getCurrentPosition(c); graphics.setPaint(Color.BLACK); for (LineComponent component : line.getComponents()) { GlyphVector glyphVector = component.getGlyphVector(); final int numGlyphs = glyphVector.getNumGlyphs(); float nextAdvance = glyphVector.getGlyphMetrics(0).getAdvance() * 0.5f; double start = cursor.getCurrentOrdinate(); Shape[] outlines = new Shape[numGlyphs]; AffineTransform[] transforms = new AffineTransform[numGlyphs]; final Font font = component.getGlyphVector().getFont(); Number tracking = (Number) font.getAttributes().get(TextAttribute.TRACKING); for (int i = 0; i < numGlyphs; i++) { outlines[i] = glyphVector.getGlyphOutline(i); Point2D p = glyphVector.getGlyphPosition(i); float advance = nextAdvance; if (tracking != null) { advance = advance + font.getSize2D() * tracking.floatValue(); ? glyphVector.getGlyphMetrics(i + 1).getAdvance() * 0.5f : 0; cursor.moveTo(start + glyphVector.getGlyphPosition(numGlyphs).getX());
String text = "Hello world!"; Font font = new Font(Font.SERIF, Font.PLAIN, 12); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); BufferedImage bufferedImage = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB); Graphics2D g2d = ge.createGraphics(bufferedImage); FontRenderContext frc = g2d.getFontRenderContext(); GlyphVector glyphVector = font.createGlyphVector(frc, text); Rectangle2D rectangle2D = glyphVector.getLogicalBounds(); double w = rectangle2D.getWidth(); double h = rectangle2D.getHeight(); System.out.println("width = "+w+"\theight = "+h);
g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); Font font = new Font("Arial", Font.BOLD, 48); g2d.setFont(font); FontRenderContext frc = g2d.getFontRenderContext(); GlyphVector gv = font.createGlyphVector(frc, "Cat"); Rectangle2D box = gv.getVisualBounds(); g2d.drawString("Cat", x, y); gv = font.createGlyphVector(frc, "Dog"); box = gv.getVisualBounds(); g2d.drawString("Dog", x, y);