Question

I'm trying to learn how to read an open type font file.

The thing is I am having problems reading some tables. I thought it would be enough with the information given at http://www.microsoft.com/typography/otspec/otff.htm

When trying to read the glyf table I find under simple glyph description flags[n], where n is the number of flags.

The problem is I don't know where to get the number of flags from, and without the number of flags reading the table doesn't seem possible.

Was it helpful?

Solution

Ok, I found the answer to my questions, endPtsOfContours had the position of the last point of every contour in the list of coordinates.

I used TTFDUMP to find out what the answer was.

OTHER TIPS

Just to clarify how the number of flags is computed. The number of flags, n, is the last element in the endPtsOfContours + 1.

Although this is an old question I feel that I need add some more information to this topic as the answers above may produce wrong results.

The number of points in a glyph equals the last endPtsOfContours + 1. Usually this equals number of flags n, but not necessarily. Bit 3 in a flag (the Repeat flag) is used indicate that the same flag is repeated several times. If this flag is set, the number of flags can be be lower than the number of points.

The truth is, before reading the flags you cannot know how many bytes you will have to read. You will have to keep reading the flags until you reach the number of points. Here is a small Javascript snipplet that shows how reading the flags could be implemented.

var pointCount; // This has to the set to the last endPtsOfContours + 1 
var flags = []; // our internal array of flags
while (flags.length < pointCount) {  
    var flag = fontReader.readByte(); // flag is a single byte
    flags.push(flag); // add flag
    if (flag & 8) {  // if bit 3 is set
        var repeats = fontReader.readByte(); // next byte indicates repeats of the flag
        for (; repeats > 0; repeats--) {
            flags.push(flag); // add same flag again
        }
    }
}

I cannot tell you if or how often the Repeat flag is used in real world fonts (probably the two fonts Bradley Hand ITC Regular and Rage Italic mentioned in the comments above use it), but if you want to make sure that you read the correct number of flags, you will have to take that bit it into account.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top