tesseract 3.04.01

training/GlyphLessFont.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GlyphLessFont.h"

Go to the source code of this file.

Defines

#define LITTLE_ENDIAN

Functions

int main (int argc, char **argv)

Variables

Offset_Table Offsets
head_table head
hhea_table hhea
maxp_table maxp
OS2_table OS2
hmtx_table hmtx
cmap_table cmap
char Macnamestring [] = {'V', 'e', 'r', 's', 'i', 'o', 'n', ' ', '1', '.', '0'}
char Unamestring []
name_table name
post_table post

Define Documentation

#define LITTLE_ENDIAN

Definition at line 11 of file GlyphLessFont.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

This program reads in a text file consisting of feature samples from a training page in the following format:

      FontName UTF8-char-str xmin ymin xmax ymax page-number
       NumberOfFeatureTypes(N)
         FeatureTypeName1 NumberOfFeatures(M)
            Feature1
            ...
            FeatureM
         FeatureTypeName2 NumberOfFeatures(M)
            Feature1
            ...
            FeatureM
         ...
         FeatureTypeNameN NumberOfFeatures(M)
            Feature1
            ...
            FeatureM
      FontName CharName ...
    

The result of this program is a binary inttemp file used by the OCR engine.

Parameters:
argcnumber of command line arguments
argvarray of command line arguments
Returns:
none
Note:
Exceptions: none
History: Fri Aug 18 08:56:17 1989, DSJ, Created.
History: Mon May 18 1998, Christy Russson, Revistion started.

Definition at line 381 of file GlyphLessFont.c.

{
    FILE *OutputFile;
    TableRecord Table[10];
    unsigned long offset =
        sizeof(Offset_Table) + (sizeof(TableRecord) * 10),
        length = 0, checksum = 0, HeadTableOffset, Working;
    short fword = -1;
    short loca = 0;
    long glyf = 0;
    unsigned int NameLength, i, FileLength;

    printf("Ken's Glyph-free font creator\n");
    if (argc != 2) {
        fprintf (stderr, "Usage: GlyphLessFont <output filename>\n");
            exit (1);
    }

    OutputFile = fopen (argv[1], "wb+");
    if (OutputFile == 0) {
        fprintf (stderr, "Couldn't open file %s for writing\n", argv[1]);
            exit (1);
    }

    fwrite (&Offsets, sizeof(Offset_Table), 1, OutputFile);
    memset(&Table, 0x00, sizeof(TableRecord) + 10);
    fwrite (&Table, sizeof (TableRecord), 10, OutputFile);
    offset = ftell(OutputFile);
    Table[3].offset = HeadTableOffset = offset;

    /* The whole business of writing a TrueType file is complicated by
     * the way its laid out Firstly there is the fact that it wants
     * the tables to be laid out in alphabetical order, but it wants
     * the actual table data (which the table record points to) to be
     * in quite a different order.  Then there's the requirement to
     * have all the table offsets be a multiple of 4 bytes.  Finally
     * we have to calculate a checksum for each table as well, which
     * we cna't realistically do until we have written the table data,
     * but which gets stored in the table record at the start of the
     * file.
     *
     * So we start by writing a dumm set of table records, we'll fill
     * in the array as we go and once we've written all the data and
     * worked out the offsets and checksums of all the tables, we'll
     * come back and write the table records into the area we left
     * reserved.
     */
    fwrite (&head, sizeof(head_table), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[4].offset = offset;

    fwrite (&hhea, sizeof(hhea_table), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[7].offset = offset;

    fwrite (&maxp, sizeof(maxp_table), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[0].offset = offset;

    fwrite (&OS2, sizeof(OS2_table), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[5].offset = offset;

    fwrite (&hmtx, sizeof(hmtx_table), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[1].offset = offset;

    fwrite (&cmap, sizeof(cmap_table), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[6].offset = offset;

    fwrite (&loca, sizeof(short), 1, OutputFile);
    fwrite (&loca, sizeof(short), 1, OutputFile);
    fwrite (&loca, sizeof(short), 1, OutputFile);
    fwrite (&loca, sizeof(short), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[2].offset = offset;

    fwrite (&glyf, sizeof(long), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[8].offset = offset;

    length = (sizeof(name_table) + sizeof(Macnamestring) +
              sizeof(Unamestring) + 3) / 4;
    length *= 4;
    NameLength = length;
    fwrite (&name, sizeof(name_table), 1, OutputFile);
    fwrite (&Macnamestring, sizeof(Macnamestring), 1, OutputFile);
    fwrite (&Unamestring, NameLength -
            (sizeof(name_table) + sizeof(Macnamestring)), 1, OutputFile);
    offset = ftell(OutputFile);
    Table[9].offset = offset;

    fwrite (&post, sizeof(post_table), 1, OutputFile);
    FileLength = ftell(OutputFile);

    Table[3].tag[0] = 'h';
    Table[3].tag[1] = 'e';
    Table[3].tag[2] = 'a';
    Table[3].tag[3] = 'd';
    Table[3].checkSum = 0;
    Table[3].length = sizeof(head_table) - 2; /* Don't count size
                                                 of padding bytes in table */

    Table[4].tag[0] = 'h';
    Table[4].tag[1] = 'h';
    Table[4].tag[2] = 'e';
    Table[4].tag[3] = 'a';
    Table[4].checkSum = 0;
    Table[4].length = sizeof(hhea_table);

    Table[7].tag[0] = 'm';
    Table[7].tag[1] = 'a';
    Table[7].tag[2] = 'x';
    Table[7].tag[3] = 'p';
    Table[7].checkSum = 0;
    Table[7].length = sizeof(maxp_table);

    Table[0].tag[0] = 'O';
    Table[0].tag[1] = 'S';
    Table[0].tag[2] = '/';
    Table[0].tag[3] = '2';
    Table[0].checkSum = 0;
    Table[0].length = sizeof(OS2_table);

    Table[5].tag[0] = 'h';
    Table[5].tag[1] = 'm';
    Table[5].tag[2] = 't';
    Table[5].tag[3] = 'x';
    Table[5].checkSum = 0;
    Table[5].length = sizeof(hmtx_table);

    Table[1].tag[0] = 'c';
    Table[1].tag[1] = 'm';
    Table[1].tag[2] = 'a';
    Table[1].tag[3] = 'p';
    Table[1].checkSum = 0;
    Table[1].length = sizeof(cmap_table);

    Table[6].tag[0] = 'l';
    Table[6].tag[1] = 'o';
    Table[6].tag[2] = 'c';
    Table[6].tag[3] = 'a';
    Table[6].checkSum = 0;
    Table[6].length = sizeof(USHORT) * 3;

    Table[2].tag[0] = 'g';
    Table[2].tag[1] = 'l';
    Table[2].tag[2] = 'y';
    Table[2].tag[3] = 'f';
    Table[2].checkSum = 0;
    Table[2].length = 1;

    Table[8].tag[0] = 'n';
    Table[8].tag[1] = 'a';
    Table[8].tag[2] = 'm';
    Table[8].tag[3] = 'e';
    Table[8].checkSum = 0;
    Table[8].length = (sizeof(name_table) +
                       sizeof(Macnamestring) +
                       sizeof(Unamestring) + 3) / 4;
    Table[8].length *= 4;
    NameLength = Table[8].length;

    Table[9].tag[0] = 'p';
    Table[9].tag[1] = 'o';
    Table[9].tag[2] = 's';
    Table[9].tag[3] = 't';
    Table[9].checkSum = 0;
    Table[9].length = sizeof(post_table);

    for (i=0;i<10;i++) {
        ULONG LENGTH, Sum = 0L;
        ULONG *EndPtr, *Data, *Current;

        offset = Table[i].offset;
        length = Table[i].length;
        LENGTH = (length + 3 & ~3);
        Data = (ULONG *)malloc(LENGTH);
        memset(Data, 0x00, LENGTH);
        fseek(OutputFile, offset, SEEK_SET);
        fread(Data, length, 1, OutputFile);

        Current = Data;
        EndPtr = Data + (LENGTH / sizeof(ULONG));
        while(Current < EndPtr){
#ifdef LITTLE_ENDIAN
            Working = *Current++;
            Sum += ((Working & 0xff) << 24) +
                ((Working & 0xff00) << 8) +
                ((Working & 0xff0000) >> 8) +
                (Working >> 24);
#else
            Sum += *Current++;
#endif
        }
        free(Data);

#ifdef LITTLE_ENDIAN
        Table[i].offset =
            ((offset & 0xff) << 24) +
            ((offset & 0xff00) << 8) +
            ((offset & 0xff0000) >> 8) +
            (offset >> 24);
        Table[i].length =
            ((length & 0xff) << 24) +
            ((length & 0xff00) << 8) +
            ((length & 0xff0000) >> 8) +
            (length >> 24);
        Table[i].checkSum =
            ((Sum & 0xff) << 24) +
            ((Sum & 0xff00) << 8) +
            ((Sum & 0xff0000) >> 8) +
            (Sum >> 24);
#else
        Table[i].checkSum = Sum;
#endif
    }

    fseek(OutputFile, sizeof(Offset_Table), SEEK_SET);
    fwrite (&Table, sizeof(TableRecord), 10, OutputFile);

    fseek(OutputFile, 0, SEEK_SET);

    for (i=0;i < FileLength / sizeof(long);i++) {
        fread(&Working, sizeof(long), 1, OutputFile);
#ifdef LITTLE_ENDIAN
            checksum += ((Working & 0xff) << 24) +
                ((Working & 0xff00) << 8) +
                ((Working & 0xff0000) >> 8) +
                (Working >> 24);
#else
            checksum += Working;
#endif
    }
    checksum = 0xB1B0AFBA - checksum;
#ifdef LITTLE_ENDIAN
    head.checkSumAdjustment =
        ((checksum & 0xff) << 24) +
        ((checksum & 0xff00) << 8) +
        ((checksum & 0xff0000) >> 8) +
        (checksum >> 24);
#else
    head.checkSumAdjustment = checksum;
#endif
    fseek(OutputFile, HeadTableOffset, SEEK_SET);
    fwrite (&head, sizeof(head_table), 1, OutputFile);
    fclose(OutputFile);

    return 0;
}

Variable Documentation

Definition at line 255 of file GlyphLessFont.c.

Definition at line 29 of file GlyphLessFont.c.

Definition at line 103 of file GlyphLessFont.c.

Initial value:
 {
0x0000, 0x0000,
0x0000, 0x0000
}

Definition at line 250 of file GlyphLessFont.c.

char Macnamestring[] = {'V', 'e', 'r', 's', 'i', 'o', 'n', ' ', '1', '.', '0'}

Definition at line 304 of file GlyphLessFont.c.

Definition at line 136 of file GlyphLessFont.c.

Definition at line 308 of file GlyphLessFont.c.

Initial value:
 {

    0x00000100,    
    0x0A00,        
    0x8000,        
    0x0300,        
    0x2000,        







}

Definition at line 13 of file GlyphLessFont.c.

Definition at line 172 of file GlyphLessFont.c.

Initial value:
 {

    0x0100,      



    0x00000000,  
    0x0000,      
    0x0000,      

    0x01000000,  



    0x00000000,  
    0x00000000,  
    0x00000000,  
    0x00000000,  
}

Definition at line 361 of file GlyphLessFont.c.

char Unamestring[]
Initial value:
 {0x00, 'V', 0x00, 'e', 0x00, 'r', 0x00, 's', 0x00, 'i',
                      0x00, 'o', 0x00, 'n', 0x00, ' ', 0x00, '1', 0x00, '.',
                      0x00, '0', 0x00, 0x00, 0x00}

Definition at line 305 of file GlyphLessFont.c.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines