Kerning
Kerning (different from tracking) is a font's ability to adjust the spacing between certain pairs of letters. The kerning is dependent on how a particular combination of letters fit together when sitting side by side and will typically only be defined for non fixed width fonts. The kerning values for character combinations are defined in the font's kerning pair table but not every font will contain this kerning information. Of the built in Core Fonts, all Helvetica and Times New Roman fonts include kerning information.
We can see font kerning demonstrated if we look at the character combinations of the uppercase "T" and the lowercase "o". With an uppercase "T" there is plenty of room to the right of the "T" to fit a smaller letter. For this reason, the kerning pair table may decrease the amount of space between an uppercase "T" and a lowercase "o" when the two are displayed in that order.
By default, all the kerning values are read directly from the font. If changes are needed, the DynamicPDF API gives users the ability to adjust or set the kerning values for any font. Adjusting a fonts kerning values is done through the use of the TextArea page element. Once a font (either built in font or a user defined font) is specified in the TextArea there are basically three things that can be done:
- Check to see if the font being used has supplied the kerning pairs table.
- Enable the TextArea to modify the kerning pairs (this must always be done to modify the kerning pairs).
- Get the kern values to read or modify.
Checking if a font has supplied the kerning pairs table is done by checking the font's HasKerning method. If this method returns true then the font has supplied a table of kerning pairs; otherwise, the font has not. If it is desired to customize any kerning pairs then the TextArea's EnabledKerning property must be set to true. Retrieving the actual kerning pairs for the text supplied in a TextArea is done by calling the TextArea's GetKernValue method. This method returns a KernValues object which consists of one short array (Spacing) and one character array (Text). The character array is an array of every character in the TextArea while the short array is an array of the kerning value for each combination of the characters. Even if the font used in the TextArea did not contain a kerning pairs table, the TextArea's EnableKerning property can still be set to true. This will result in a KernValues object with the Spacing array of all zeros.
Tracking, which is the amount of overall space between all letters, can be increased using similar methods as described above. However to increase the tracking you will just need to loop over the entire TextArea's KernValues' Spacing array and assign a certain value for every pair (or a subset of pairs). While with kerning the focus is usually on specific pairs of characters, tracking will usually set the overall space for every character in that array. The following example demonstrates how tracking would be applied to a block of text in a TextArea:
[Java]
Document document = new Document();
Page page = new Page();
String text = "To and WA are examples of when kerning would be used.";
TextArea textArea = new TextArea(text, 10, 10, 400, 700, Font.getTimesRoman());
// Must be enabled before kern values can be read
textArea.setKerningEnabled(true);
KerningValues kernValues = textArea.getKerningValue();
// Assign positive value for making the space between chars closer and negative value for making the space further
for (int i = 0; i < kernValues.getSpacing().length; i++) {
kernValues.getSpacing()[i] = (short)(kernValues.getSpacing()[i] - 400); }
page.getElements().add(textArea);
document.getPages().add(page);
document.draw("[PhysicalPath]/MyDocument.pdf");
The values stored in the KernValues' Spacing array are expressed as thousandths of a unit of font size. Specifically, a value in the Spacing array of 100 with a font size of 20 will make an adjustment of 2 points (100 x 20 /1000). Remember that a positive value in the Spacing array will pull characters closer together (as is usual for kerning) while a negative value will increase the space between letters (as is usual for tracking).