All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GlyphLessFont.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GlyphLessFont.h"

Go to the source code of this file.

Macros

#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
 

Macro Definition 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.

382 {
383  FILE *OutputFile;
384  TableRecord Table[10];
385  unsigned long offset =
386  sizeof(Offset_Table) + (sizeof(TableRecord) * 10),
387  length = 0, checksum = 0, HeadTableOffset, Working;
388  short fword = -1;
389  short loca = 0;
390  long glyf = 0;
391  unsigned int NameLength, i, FileLength;
392 
393  printf("Ken's Glyph-free font creator\n");
394  if (argc != 2) {
395  fprintf (stderr, "Usage: GlyphLessFont <output filename>\n");
396  exit (1);
397  }
398 
399  OutputFile = fopen (argv[1], "wb+");
400  if (OutputFile == 0) {
401  fprintf (stderr, "Couldn't open file %s for writing\n", argv[1]);
402  exit (1);
403  }
404 
405  fwrite (&Offsets, sizeof(Offset_Table), 1, OutputFile);
406  memset(&Table, 0x00, sizeof(TableRecord) + 10);
407  fwrite (&Table, sizeof (TableRecord), 10, OutputFile);
408  offset = ftell(OutputFile);
409  Table[3].offset = HeadTableOffset = offset;
410 
411  /* The whole business of writing a TrueType file is complicated by
412  * the way its laid out Firstly there is the fact that it wants
413  * the tables to be laid out in alphabetical order, but it wants
414  * the actual table data (which the table record points to) to be
415  * in quite a different order. Then there's the requirement to
416  * have all the table offsets be a multiple of 4 bytes. Finally
417  * we have to calculate a checksum for each table as well, which
418  * we cna't realistically do until we have written the table data,
419  * but which gets stored in the table record at the start of the
420  * file.
421  *
422  * So we start by writing a dumm set of table records, we'll fill
423  * in the array as we go and once we've written all the data and
424  * worked out the offsets and checksums of all the tables, we'll
425  * come back and write the table records into the area we left
426  * reserved.
427  */
428  fwrite (&head, sizeof(head_table), 1, OutputFile);
429  offset = ftell(OutputFile);
430  Table[4].offset = offset;
431 
432  fwrite (&hhea, sizeof(hhea_table), 1, OutputFile);
433  offset = ftell(OutputFile);
434  Table[7].offset = offset;
435 
436  fwrite (&maxp, sizeof(maxp_table), 1, OutputFile);
437  offset = ftell(OutputFile);
438  Table[0].offset = offset;
439 
440  fwrite (&OS2, sizeof(OS2_table), 1, OutputFile);
441  offset = ftell(OutputFile);
442  Table[5].offset = offset;
443 
444  fwrite (&hmtx, sizeof(hmtx_table), 1, OutputFile);
445  offset = ftell(OutputFile);
446  Table[1].offset = offset;
447 
448  fwrite (&cmap, sizeof(cmap_table), 1, OutputFile);
449  offset = ftell(OutputFile);
450  Table[6].offset = offset;
451 
452  fwrite (&loca, sizeof(short), 1, OutputFile);
453  fwrite (&loca, sizeof(short), 1, OutputFile);
454  fwrite (&loca, sizeof(short), 1, OutputFile);
455  fwrite (&loca, sizeof(short), 1, OutputFile);
456  offset = ftell(OutputFile);
457  Table[2].offset = offset;
458 
459  fwrite (&glyf, sizeof(long), 1, OutputFile);
460  offset = ftell(OutputFile);
461  Table[8].offset = offset;
462 
463  length = (sizeof(name_table) + sizeof(Macnamestring) +
464  sizeof(Unamestring) + 3) / 4;
465  length *= 4;
466  NameLength = length;
467  fwrite (&name, sizeof(name_table), 1, OutputFile);
468  fwrite (&Macnamestring, sizeof(Macnamestring), 1, OutputFile);
469  fwrite (&Unamestring, NameLength -
470  (sizeof(name_table) + sizeof(Macnamestring)), 1, OutputFile);
471  offset = ftell(OutputFile);
472  Table[9].offset = offset;
473 
474  fwrite (&post, sizeof(post_table), 1, OutputFile);
475  FileLength = ftell(OutputFile);
476 
477  Table[3].tag[0] = 'h';
478  Table[3].tag[1] = 'e';
479  Table[3].tag[2] = 'a';
480  Table[3].tag[3] = 'd';
481  Table[3].checkSum = 0;
482  Table[3].length = sizeof(head_table) - 2; /* Don't count size
483  of padding bytes in table */
484 
485  Table[4].tag[0] = 'h';
486  Table[4].tag[1] = 'h';
487  Table[4].tag[2] = 'e';
488  Table[4].tag[3] = 'a';
489  Table[4].checkSum = 0;
490  Table[4].length = sizeof(hhea_table);
491 
492  Table[7].tag[0] = 'm';
493  Table[7].tag[1] = 'a';
494  Table[7].tag[2] = 'x';
495  Table[7].tag[3] = 'p';
496  Table[7].checkSum = 0;
497  Table[7].length = sizeof(maxp_table);
498 
499  Table[0].tag[0] = 'O';
500  Table[0].tag[1] = 'S';
501  Table[0].tag[2] = '/';
502  Table[0].tag[3] = '2';
503  Table[0].checkSum = 0;
504  Table[0].length = sizeof(OS2_table);
505 
506  Table[5].tag[0] = 'h';
507  Table[5].tag[1] = 'm';
508  Table[5].tag[2] = 't';
509  Table[5].tag[3] = 'x';
510  Table[5].checkSum = 0;
511  Table[5].length = sizeof(hmtx_table);
512 
513  Table[1].tag[0] = 'c';
514  Table[1].tag[1] = 'm';
515  Table[1].tag[2] = 'a';
516  Table[1].tag[3] = 'p';
517  Table[1].checkSum = 0;
518  Table[1].length = sizeof(cmap_table);
519 
520  Table[6].tag[0] = 'l';
521  Table[6].tag[1] = 'o';
522  Table[6].tag[2] = 'c';
523  Table[6].tag[3] = 'a';
524  Table[6].checkSum = 0;
525  Table[6].length = sizeof(USHORT) * 3;
526 
527  Table[2].tag[0] = 'g';
528  Table[2].tag[1] = 'l';
529  Table[2].tag[2] = 'y';
530  Table[2].tag[3] = 'f';
531  Table[2].checkSum = 0;
532  Table[2].length = 1;
533 
534  Table[8].tag[0] = 'n';
535  Table[8].tag[1] = 'a';
536  Table[8].tag[2] = 'm';
537  Table[8].tag[3] = 'e';
538  Table[8].checkSum = 0;
539  Table[8].length = (sizeof(name_table) +
540  sizeof(Macnamestring) +
541  sizeof(Unamestring) + 3) / 4;
542  Table[8].length *= 4;
543  NameLength = Table[8].length;
544 
545  Table[9].tag[0] = 'p';
546  Table[9].tag[1] = 'o';
547  Table[9].tag[2] = 's';
548  Table[9].tag[3] = 't';
549  Table[9].checkSum = 0;
550  Table[9].length = sizeof(post_table);
551 
552  for (i=0;i<10;i++) {
553  ULONG LENGTH, Sum = 0L;
554  ULONG *EndPtr, *Data, *Current;
555 
556  offset = Table[i].offset;
557  length = Table[i].length;
558  LENGTH = (length + 3 & ~3);
559  Data = (ULONG *)malloc(LENGTH);
560  memset(Data, 0x00, LENGTH);
561  fseek(OutputFile, offset, SEEK_SET);
562  fread(Data, length, 1, OutputFile);
563 
564  Current = Data;
565  EndPtr = Data + (LENGTH / sizeof(ULONG));
566  while(Current < EndPtr){
567 #ifdef LITTLE_ENDIAN
568  Working = *Current++;
569  Sum += ((Working & 0xff) << 24) +
570  ((Working & 0xff00) << 8) +
571  ((Working & 0xff0000) >> 8) +
572  (Working >> 24);
573 #else
574  Sum += *Current++;
575 #endif
576  }
577  free(Data);
578 
579 #ifdef LITTLE_ENDIAN
580  Table[i].offset =
581  ((offset & 0xff) << 24) +
582  ((offset & 0xff00) << 8) +
583  ((offset & 0xff0000) >> 8) +
584  (offset >> 24);
585  Table[i].length =
586  ((length & 0xff) << 24) +
587  ((length & 0xff00) << 8) +
588  ((length & 0xff0000) >> 8) +
589  (length >> 24);
590  Table[i].checkSum =
591  ((Sum & 0xff) << 24) +
592  ((Sum & 0xff00) << 8) +
593  ((Sum & 0xff0000) >> 8) +
594  (Sum >> 24);
595 #else
596  Table[i].checkSum = Sum;
597 #endif
598  }
599 
600  fseek(OutputFile, sizeof(Offset_Table), SEEK_SET);
601  fwrite (&Table, sizeof(TableRecord), 10, OutputFile);
602 
603  fseek(OutputFile, 0, SEEK_SET);
604 
605  for (i=0;i < FileLength / sizeof(long);i++) {
606  fread(&Working, sizeof(long), 1, OutputFile);
607 #ifdef LITTLE_ENDIAN
608  checksum += ((Working & 0xff) << 24) +
609  ((Working & 0xff00) << 8) +
610  ((Working & 0xff0000) >> 8) +
611  (Working >> 24);
612 #else
613  checksum += Working;
614 #endif
615  }
616  checksum = 0xB1B0AFBA - checksum;
617 #ifdef LITTLE_ENDIAN
619  ((checksum & 0xff) << 24) +
620  ((checksum & 0xff00) << 8) +
621  ((checksum & 0xff0000) >> 8) +
622  (checksum >> 24);
623 #else
624  head.checkSumAdjustment = checksum;
625 #endif
626  fseek(OutputFile, HeadTableOffset, SEEK_SET);
627  fwrite (&head, sizeof(head_table), 1, OutputFile);
628  fclose(OutputFile);
629 
630  return 0;
631 }
char Unamestring[]
maxp_table maxp
head_table head
Definition: GlyphLessFont.c:29
post_table post
unsigned short USHORT
Definition: GlyphLessFont.h:14
Offset_Table Offsets
Definition: GlyphLessFont.c:13
ULONG checkSum
Definition: GlyphLessFont.h:42
ULONG checkSumAdjustment
hhea_table hhea
name_table name
cmap_table cmap
char Macnamestring[]
OS2_table OS2
hmtx_table hmtx
#define LENGTH(a)
Definition: vecfuncs.h:70
unsigned long ULONG
Definition: GlyphLessFont.h:17

Variable Documentation

cmap_table cmap

Definition at line 255 of file GlyphLessFont.c.

head_table head

Definition at line 29 of file GlyphLessFont.c.

hhea_table hhea

Definition at line 103 of file GlyphLessFont.c.

hmtx_table hmtx
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.

maxp_table maxp

Definition at line 136 of file GlyphLessFont.c.

name_table name

Definition at line 308 of file GlyphLessFont.c.

Offset_Table Offsets
Initial value:
= {
0x00000100,
0x0A00,
0x8000,
0x0300,
0x2000,
}

Definition at line 13 of file GlyphLessFont.c.

OS2_table OS2

Definition at line 172 of file GlyphLessFont.c.

post_table post
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.