5

I'm working with graphcis2d in Java and am currently using this to draw text into a bufferedImage

Font font1 = new Font("Arial", Font.PLAIN, 120);
g2d.setFont(font1);
FontMetrics fm1 = g2d.getFontMetrics(font1);     
g2d.drawString(s[1], width/2-fm1.stringWidth(s[1])/2, height/4-70);

I want to draw this text with an different color outline.

GlyphVector gv = font1.createGlyphVector(g2d.getFontRenderContext(), s[1]);
Shape shape = gv.getOutline();
g2d.setStroke(new BasicStroke(4.0f));
g2d.translate(width/2-fm1.stringWidth(s[1])/2, height/4-70);
g2d.draw(shape);        

The problem with using this method, which works, is that I am working with arabic characters and using GlyphVector reverses the order and doesn't make the letters flow with one another.

Can someone please explain to me how to render arabic text in one color and have an outline with another?

Heres a sample of the text I would be using: الرحمن

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kam
  • 213
  • 3
  • 5

3 Answers3

2

One trick is to draw the text several times in the outline color, varying the position by the outline width in +/- x and +/- y directions, then draw in the foreground color at the nominal position. It isn't perfect, but it tends to look pretty good provided the outline isn't too thick with respect to the stroke width of the letters.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I see black spots on the border. – Sorter Aug 03 '15 at 11:11
  • 1
    @Sorter - Sorry to hear that. Without any details about what you're doing or exactly what you're seeing, I can't guess why that is. Like I said, though, this method "isn't perfect". – Ted Hopp Aug 03 '15 at 15:38
2

You may be able to use the method createStrokedShape() on the glyph's Shape returned by getOutline(). See also CompositeStroke, demonstrated here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Try to use

layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags) 

instead of the createGlyphVector

StanislavL
  • 56,971
  • 9
  • 68
  • 98