#include <stdio.h>
#include "gd.h"

#define START \
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" \
"<head>\n" \
"\t<title>Font sample</title>\n" \
"\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" \
"</head>\n" \
"\n" \
"<body>\n" \
"\n" \
"<div id='container'>\n" \
"\t<ul>\n"

#define END \
"\t</ul>\n" \
"</body>\n" \
"</html>\n" 


#define NAMELEN 255
#define NAMEPATTERN "%s.png"

#define MARGIN 16

#define SAMPLE \
	"다람쥐 헌 쳇바퀴에 타고파. 1234567890" 

int main( int argc, char** argv ){


	gdImagePtr im;
	FILE* out;

	int rect[7][8];
	int x, y, w, h;

	int black;
	int i, j;

	char* err;
	char filename[NAMELEN];
	char* fc[] = {
		"Arial Unicode MS:style=Regular",
		"구슬:style=Regular",
		"굴림:style=Regular",
		"궁서:style=Regular",
		"돋움:style=Regular",
		"맑은 고딕:style=Bold",
		"맑은 고딕:style=Regular",
		"바탕:style=Regular",
		"반달:style=Regular",
		"방울:style=Regular",
		"백묵 굴림:style=Regular",
		"백묵 돋움:style=Regular",
		"백묵 바탕:style=Regular",
		"백묵 헤드라인:style=Regular",
		"서울남산체:style=Light",
		"서울남산체:style=Regular",
		"서울남산체:style=Bold",
		"서울남산체:style=ExtraBold",
		"서울한강체:style=Light",
		"서울한강체:style=Regular",
		"아리따:style=Light",
		"아리따:style=Regular",
		"아리따:style=SemiBold",
		"아리따:style=Bold",
		"윤고딕:style=Regular",
		"윤고딕:style=Bold",
		"윤고딕:style=ExtraBold",
		"윤명조:style=Regular",
		"윤명조:style=Bold",
		"윤명조:style=ExtraBold",
		"윤체:style=Regular",
		"윤체:style=Bold",
		"윤체:style=ExtraBold",
		"은 궁서:style=Regular",
		"은 그래픽:style=Regular",
		"은 그래픽:style=Bold",
		"은 돋움:style=Regular",
		"은 돋움:style=Bold",
		"은 바탕:style=Regular",
		"은진:style=Regular",
		"은진낙서:style=Regular",
		"한겨레결체:style=Regular",
		NULL
	};

	double pt[7] = {
		12.,
		18.,
		24.,
		36.,
		48.,
		60.,
		72.
	};

	gdFTStringExtra option;
	option.flags = gdFTEX_DISABLE_KERNING;

	gdFTUseFontConfig(1);

	fprintf( stdout, "%s", START );

	for( i = 0 ; fc[i] != NULL ; i++ ){

		h = 0;
		for( j = 0 ; j < 7 ; j++ ){
			err = gdImageStringFTEx( NULL, rect[j], 0, fc[i], pt[j], 0., 0, 0, SAMPLE, &option );
			if( err ){
				fprintf( stderr, "%s", err );
				goto error2;
			}
			h += rect[j][3] - rect[j][7] + MARGIN;
		}
		w = rect[6][2] - rect[6][6] + MARGIN;

		im = gdImageCreate( w, h );

		gdImageColorResolve(im, 255, 255, 255 );
		black = gdImageColorResolve(im, 0, 0, 0 );

		h = MARGIN/2;
		for( j = 0 ; j < 7 ; j++ ){
			x = MARGIN/2 - rect[j][6];
			y = h - rect[j][7];
			err = gdImageStringFTEx( im , rect[j], black, fc[i], pt[j], 0., x, y, SAMPLE, &option );
			if( err ){
				fprintf( stderr, "%s", err );
				goto error1;
			}
			h += rect[j][3] - rect[j][7] + MARGIN;
		}

		snprintf( filename, NAMELEN-1, NAMEPATTERN, fc[i] );
		out = fopen( filename, "wb" );
		gdImagePng( im, out );
		fclose(out);

		fprintf( stdout, "\t<li>\n\t\t<h3>%s</h3>\n\t\t<img src='./%s' />\n\t</li>\n", fc[i], filename );

error1:
		gdImageDestroy(im);

error2:
		;
	}
	fprintf( stdout, "%s", END );

	return 0;

}
