generated html version of ttfglyrnd.cHOME

#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"

/*this program reads and edits a ttf to fix the glyph outlines to 
correct 64 point boundaries.
*/

uint32_t read32(FILE *f){
	uint32_t c;
	fread(&c,4,1,f);
	c = c >> 16 | c << 16;
	c = c >> 8 & 0xFF00FF | c << 8 & 0xFF00FF00;
	return c;
}

uint16_t read16(FILE *f){
	uint16_t c;
	fread(&c,2,1,f);
	c = c >> 8 | c << 8;
	return c;
}

uint8_t read8(FILE *f){
	uint8_t c;
	fread(&c,1,1,f);
	return c;
}

typedef uint32_t u4;
typedef uint16_t u2;
typedef uint8_t u1;

int main(int argc, char **argv){
	FILE *f = fopen(argv[1],"r+");
	do fseek(f,12,SEEK_CUR); while(read32(f)!=*(u4*)"pxam");
	read32(f);//discard
	fseek(f,read32(f),SEEK_SET);//goto the maxp
	printf("found maxp\n");
	read32(f);//discard
	u2 N = read16(f);
	fseek(f,0,SEEK_SET);//rewind
	do fseek(f,12,SEEK_CUR); while(read32(f)!=*(u4*)"acol");
	read32(f);//discard
	u4 loca_loc = read32(f);
	printf("found loca\n");
	fseek(f,0,SEEK_SET);//rewind
	do fseek(f,12,SEEK_CUR); while(read32(f)!=*(u4*)"fylg");
	read32(f);//discard
	u4 glyf_loc = read32(f);
	printf("found glyf\n");
	int i,j;
	for(i=0;i<N;i++){
		fseek(f,loca_loc+i*4,SEEK_SET);//find ith glyph
		u4 start = read32(f);
		fseek(f,glyf_loc+start,SEEK_SET);//goto it
		u2 contour_n = read16(f);
		u2 *cends = (u2*)malloc(2*contour_n);
		fseek(f,8,SEEK_CUR);//skip some crap
		u2 pt_n = 0;
		for(j=0;j<contour_n;j++){
			u2 q = read16(f);
			cends[j] = q;
			if(q>pt_n)pt_n=q;
		}
		pt_n++;
		u2 instr_n = read16(f);
		fseek(f,instr_n,SEEK_CUR);//skip instructions
		printf("%d: %x conr=%d ins=%d pt=%d ",i,glyf_loc+start,contour_n,instr_n,pt_n);
		for(j=0;j<contour_n;j++){
			printf("%d:%d ",j,cends[j]);
		}
		//now try to skip the flags;
		u1 *flgs = (u1*)malloc(pt_n);
		for(j=0;j<pt_n;){
			u1 x = read8(f);
			flgs[j++]=x;
			if(x&0x08){
				int z=read8(f);
				while(z>0){
					flgs[j++]=x;
					z--;
				}
			}
		}
		u4 xes_off = ftell(f);
		for(j=0;j<pt_n;j++){
			if(flgs[j]&0x02){//one byte x coord
				u1 x = read8(f);
				if(x%64)printf("x%d %x",j,x);
			}else{
				if((flgs[j]&0x10)==0){//two byte
					short x = read16(f);
					if(x%64)printf("X%d",j);
				}
			}
		}
		printf("\n");
		free(cends);
		free(flgs);
	}
	return 0;
}