Skip to main content Skip to footer

How to Apply Font Hinting for CJK Characters in C# .NET

Many TrueType fonts render CJK characters with hinting instructions. Hinting instructions improve legibility and prevent pixelating in small font sizes at low resolution.

Hinting instructions are mainly used for CJK fonts but will improve the clarity of any font by reusing or combining specific glyph shapes that appear in different characters. Without hinting instructions, CJK text and glyphs may render incorrectly.

Gc Imaging v3.1 supports TrueType hinting. This feature allows you to:

1. Improve the text in a PDF file and save the PDF as an image
2. Apply hinting instructions to text in an image directly

The following use cases discuss the process for improving CJK text in PDFs and images with GcPdf and Gc Imaging.

Use Cases: Applying Font Hinting

Apply Text Hinting to a PDF and Save It as an Image

1. Create a new GcPdf Document, add a page, and set the Text Format

GcPdfDocument
doc = new GcPdfDocument();    

Page page= doc.Pages.Add(); 
  TextFormat tf1 = new TextFormat()  
    {  
      Font = null,  
      FontName = 'DFKai-SB', 
      FontSize = 20.0F  
    };   

2. Enter CJK text

//Chinese 
string str1 = @'CJK';  
page.Graphics.DrawString(str1,tf1, new PointF(50, 150)); 

//Japanese  
string str2 = @'CJK';  
page.Graphics.DrawString(str2,tf1, new PointF(50, 200));    

//Korean  
string str3 = @'    CJK    '; 
page.Graphics.DrawString(str3,tf1, new PointF(50, 250)); 

3. Save the document as a PDF

string pdfFileStr = String.Format('{0}/ GcPdfFile.pdf', Environment.CurrentDirectory); 

doc.Save(pdfFileStr); 

4. Save the PDF as an image. Set hinting while instantiating SaveAsImageOptions.

string jpgFileStr = String.Format('{0}/ GcPdf_Image.jpg', Environment.CurrentDirectory); 
doc.SaveAsJpeg(jpgFileStr, null, new SaveAsImageOptions() { EnableFontHinting = true });

Note: EnableFontHinting is set to "true" by default.

Compare the text rendered with and without font hinting.

How to Apply Font Hinting for CJK Characters

How to Apply Font Hinting for CJK Characters

Apply Font Hinting When Adding Text to an Image

1. Create a GcBitmap instance. Load an image to img.FilePath

GcBitmap fileBmp = new GcBitmap(); 
fileBmp.Load(img.FilePath);  

2. Create graphics for the same GcBitmap instance. Add a title to the image, "Japanese string."

          using (var g = fileBmp.CreateGraphics())  
          {  
              var rc = new RectangleF(100,50, 350, 200);    

              var tf = new TextFormat  
              {  
                  Font = null,  
                  FontName = 'DFKai-SB', 
                  FontSize = 24, 
                  ForeColor=Color.Red, 
                  EnableFontHinting = true  
              };  
              //Add Japanese text  
              g.DrawString('', tf, rc, TextAlignment.Center, ParagraphAlignment.Center, false);    
          } 

Note: EnableFontHintingis set to "true" by default.

3. Save the image.

//Save GcBitmap to jpeg format 
fileBmp.SaveAsPng('AddTextToImage.png');  

The following images provide a comparison of CJK text with and without preserved glyphs.

How to Apply Font Hinting for CJK Characters

How to Apply Font Hinting for CJK Characters

Let us know how this feature makes your work easier.

Visit Help and Demo for more details.

Esha Dhir

Associate Software Engineer
comments powered by Disqus