3

I'm having a problem with Text figures (see Wikipedia) in a PDF document created with itextsharp.

The distances between the baseline and the lowest point of a number (e.g. 9) is NOT the same as the normal descender height of a font.

With the following code i can calculate the height of the descender of a font:

var fontSize = 60;
var fontPath = @"CorpusGothic-Condensed.otf";
var font = BaseFont.CreateFont(fontPath, BaseFont.CP1252, BaseFont.EMBEDDED);
var descentHeight = font.GetFontDescriptor(BaseFont.DESCENT, fontSize)

My question now:
Is it possible to get the descender- or ascender-heights of the numbers in a font with text-figures as shown on the Wikipedia page?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
rudy
  • 185
  • 2
  • 11
  • 1
    I've made a support ticket about this at iText Software and they wrote: "OTF fonts can have TT or CFF outlines and should have the standard tables. I wonder if that particular foundry just doesn't include the ascender/descender in the main tables and has those values in the OTF tables. Any chance of having one of those OTF files?" Can you share a link to one of the OTF files you're using? – Bruno Lowagie Jul 04 '14 at 12:24

1 Answers1

2

A font contains a general value of the ascender and the descender. This is the value that you are getting in your own code sample.

However, every glyph also has its own dimensions. The glyph for letter h has a higher ascender and a lower descender than the glyph for the letter g.

Only four hours ago, I answered the question How to calculate the height of an element? (making your question a duplicate).

This is what I wrote:

If bf is a BaseFont instance, then you can use:

float ascent = bf.getAscentPoint("Some String", 12);
float descent = bf.getDescentPoint("Some String", 12);

This will return the height above the baseline and the height below the baseline, when we use a font size of 12. As you probably know, the font size is an indication of the average height. It's not the actual height. It's just a number we work with.

The total height will be:

float height = ascent - descent;

Note that providing a String (in your case for instance "9") isn't sufficient. You also need to pass a font size (in my case 12pt) to get the value of the ascender and descender in points.

Remark: if you're using iTextSharp, replace get with Get in the methods I mentioned.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for your quick answer! Whowever, if i call the functions GetAscentPoint and GetDescentPoint, both are returning a value of 0.0f. I tried fontsizes from 12 to 600 as well as texts with and without letters (e.g. "9.67" or "12g"). The result is everytime the same using the code in my question (calling the functions you mentioned right after BaseFont.CreateFont(...)) – rudy Jul 03 '14 at 15:09
  • I've seen this before, but only with fonts that were corrupt (wrong metrics in the font file). I should get hold of CorpusGothic-Condensed.otf to find out if it's the case with that font. Did you try with other fonts? – Bruno Lowagie Jul 03 '14 at 15:43
  • I've tried a some ttf-fonts which returned results different to 0.0f, but all of the few otf-fonts that i have tried returned 0.0f. Is this maybe a general issue on open-type fonts? Is there a way i can send you this font? – rudy Jul 03 '14 at 16:18
  • I didn't write the OTF support. If you can reproduce it with all OTF fonts it may be a problem with the way iText handles the Compact Font Format. If so, it will need to be fixed. FYI: we've scheduled to rewrite the complete font functionality in the next quarter. See the roadmap I presented in my keynote at the iText summit: http://www.slideshare.net/iTextPDF/itext-summit-2014-keynote-talk – Bruno Lowagie Jul 03 '14 at 16:52
  • Thanks for the link. I've just tested this case with all the 140 otf-Fonts on my machine: with none of the fonts i get a ascent- or descent-point value bigger than 0.0f – rudy Jul 04 '14 at 09:32
  • I'll file it in our JIRA repository, so that it's fixed when we overhaul the font classes. Thanks for accepting the answer although it's not possible for me to fix the problem on a short term. – Bruno Lowagie Jul 04 '14 at 09:40