title: Bengali Joint letters in Kalpurush font in java messed up
body: <p>I have written a program (collecting from various sources, I am a beginner) in Java that takes a text(bengali) written in a .txt file and converts it to a .bmp image using the drawString function. The code is :</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  import java.awt.image.*;
  import javax.imageio.*;
  import java.io.*;
  import java.util.*;
  import java.awt.font.*;
  import java.awt.geom.*;
  
  class TextToImageDemo
  {
  
      public static void main(String[] args) throws IOException
      {
          String sampleText = "আমার",s="নাম";
          BufferedReader br = null;
          for (int u=1;u&lt;=9;u++)
              {
                  
                  try
                      {
                          br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\Java\\bengtext\\f2-0"+u+".txt")),"UTF-8"));
                          
                          while ((sampleText = br.readLine()) != null)
                              {
                                  System.out.println(sampleText);
                                  s=sampleText;
                              }
                      }
                  catch (IOException e)
                      {
                          e.printStackTrace();
                      }
                  finally
                      {
                          try
                              {
                                  if (br != null)br.close();
                              }
                          catch (IOException ex)
                              {
                                  ex.printStackTrace();
                              }
                      }
                  
                  
                   //Image file name
                   String fileName = "Image";
                   
                   //create a File Object
                   File newFile= new File("./" + fileName + ".jpeg");
                   
                   //create the font you wish to use
                   Font font = new Font(/*Lohit Bengali*/"Kalpurush", Font.PLAIN, 50);
                   
                   //create the FontRenderContext object which helps us to measure the text
                   FontRenderContext frc = new FontRenderContext(null, true, true);
                   
                   //create a BufferedImage object
                   BufferedImage image = new BufferedImage(2000, 200,   BufferedImage.TYPE_INT_RGB);
                   
                   //calling createGraphics() to get the Graphics2D
                   Graphics2D g = image.createGraphics();
                   System.out.println(s);
                   //set color and other parameters
                   g.setColor(Color.WHITE);
                   g.fillRect(0, 0, 2000, 200);
                   g.setColor(Color.BLACK);
                   g.setFont(font);
                   FontMetrics fm=g.getFontMetrics();
                   Rectangle r=new Rectangle(fm.getStringBounds(s, g).getBounds());
                   String d=s.substring(1,s.length());
                   g.drawString(d, image.getWidth()/2-r.width/2, image.getHeight()/2+r.height/2);     
                   //releasing resources
                   g.dispose();
                   
                   try
                       {
                           FileOutputStream fos = new FileOutputStream("E:\\Java\\bengtext\\f2-0"+u+".bmp");
                           ImageIO.write(image,"bmp",fos);
                           fos.close();
                       }
                   catch(Exception ex)
                       {
                           ex.printStackTrace();
                       }
              }  
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>My main problem lies in the fact that a text like</p>
  
  <blockquote>
    <p>দ্বিতীয়তা , একাগ্রতা , " জ্ঞান আহরনের একটি মাত্র উপায় </p>
  </blockquote>
  
  <p>is formed like</p>
  
  <p><a href="https://i.stack.imgur.com/qB4Xw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qB4Xw.png" alt="The problem with র&#39;s"></a></p>
  
  <p>How can this be solved?</p>
  
  <p>Edit:</p>
  
  <p>On checking all the 263 fonts available , I found only 6 of them were able to display bengali somewhat correctly. But in everyone of them the problem with the joint words lie as shown in the above picture. So question is : What is the exact problem happening here? Is JAVA not being able to read the joint words correctly, or is it not being able to draw them correctly ?</p>
  
  <p>And secondly , how to make java draw the joint words correctly?</p>