예제 #1
0
//
// Load a Hershey font .jhf file into a new struct hershey_font.
//
struct hershey_font *
hershey_jhf_font_load( const char *jhffile )
{
    struct hershey_font *hf;

    FILE *fp = fopen(jhffile, "r");
    if (!fp)
	return 0;

    hf = calloc(1, sizeof(struct hershey_font));
    assert(hf);

#define BUFSIZE 4096

    char linebuf[BUFSIZE];
    int linecount = 0;
    int glyph_index = 32;

    while ( fgets(linebuf, BUFSIZE, fp) ) {
	int r;
	struct hershey_glyph *hg;
	hg = &(hf->glyphs[glyph_index++]);
	r = hershey_jhf_load_glyph(hg, linebuf);
	linecount++;
	if ( ! r ) {
	    perror(jhffile);
	    fprintf(stderr, "%s: ... at line number %d\n", jhffile, linecount);
	    hershey_font_free(hf);
	    hf = 0;
	    break;
	}
    }

    fclose(fp);

    return hf;
}
예제 #2
0
int
main( int argc, char **argv )
{
    const char *render_text = "HELLO";

    //
    // Load a Hershey font with hershey_font_load().
    //

    const char *fontname = "rowmans";
    struct hershey_font *hf = hershey_font_load(fontname);
    if ( !hf ) {
	perror(fontname);
	return 1;
    }

    //
    // Render each character's line paths with hershey_font_glyph().
    //

    int x_render_pos = 0;

    const char *p;
    for ( p=render_text; *p; p++ ) {

	// get the character c to be rendered
	int c = *p;

	// get the hershey_glyph for character c
	// and check whether there actually is a glyph for this character
	struct hershey_glyph *hg = hershey_font_glyph(hf, c);
	if ( hg->width == 0 )
	    continue;

	printf("# %c\n", c);

	// walk the paths-list for this glyph
	struct hershey_path *hp;
	for ( hp=hg->paths; hp; hp=hp->next ) {
	    // begin draw path
	    printf("\t");
	    int i;
	    for ( i=0; i<hp->nverts; i++ ) {
		short x = hp->verts[i].x + x_render_pos;
		short y = hp->verts[i].y;
		printf(" {%d,%d}", x, y);
	    }
	    // end draw path
	    printf("\n");
	}

	// advance the x_render_pos by the width of this glyph
	x_render_pos += hg->width;
    }

    //
    // Destroy the hershey_font with hershey_font_free().
    //
    hershey_font_free(hf);

    return 0;
}
예제 #3
0
int
main( int argc, char **argv )
{
    char *gnuplot_term_opts = "wxt";
    int gnuplot_height = 100;

    int opt;
    while ((opt = getopt(argc, argv, "h:T:")) != -1) {
	switch (opt) {
	case 'h':
	    gnuplot_height = strtoul(optarg, 0, 0);
	    break;
	case 'T':
	    gnuplot_term_opts = optarg;
	    break;
	default: /* '?' */
	   usage();
	   return 1;
	}
    }

    opt = argc - optind;
    if ( opt < 1 || opt > 2 ) {
	usage();
	return 1;
    }
    if ( opt == 2 )
	sample_sheet_mode = 0;


    // load a hershey_font structure either by filename or fontname
    const char *fontname = argv[optind++];
    struct hershey_font *hf;

    if ( strchr(fontname,'.') )
	hf = hershey_jhf_font_load(fontname);
    else
	hf = hershey_font_load(fontname);

    if ( !hf ) {
	perror(fontname);
	return 1;
    }

    // render the text

    printf("#\n# gnuplot input file generated by hershey-font-gnuplot\n#\n\n");

    printf("\n");

    int terminal_width, terminal_height;

    char render_text_buf[256];
    if ( sample_sheet_mode ) {
	int i;
	for ( i=0; i<256-32; i++ )
	    render_text_buf[i] = i+32;
	render_text = render_text_buf;
	terminal_width = 16 * gnuplot_height;
	terminal_height = 8 * gnuplot_height;
    } else {
	render_text = argv[optind++];
	terminal_width = 16 * gnuplot_height;
	terminal_height = gnuplot_height;
    }

    printf("set terminal %s size %d,%d\n",
	    gnuplot_term_opts, terminal_width, terminal_height);
    printf("\n");

    printf("unset xtics\n");
    printf("unset ytics\n");
    printf("unset border\n");
    printf("unset key\n");
    if ( sample_sheet_mode ) {
	printf("set title \"%s\"\n", fontname );
	printf("set size ratio 0.5\n");
	printf("set xrange [0:%d]\n", 32*32);
	printf("set yrange [0:%d]\n", 8*64);
    } else {
	printf("set size ratio 0.125\n");
	printf("set xrange [0:%d]\n", 16*16);
	printf("set yrange [0:%d]\n", 2*16);
    }
    printf("set style arrow 1 nohead\n" );
    printf("set style line 1 lc rgb \"black\" lw 0.5\n" );

    int x_render_pos = 0;
    int y_render_pos = 8;

    const char *p;
    for ( p=render_text; *p; p++ ) {

	// get the character c to be rendered
	int c = *((unsigned char *)p);

	// get the hershey_glyph for ASCII character c
	struct hershey_glyph *hg = hershey_font_glyph(hf, c);

	// check whether there actually is a glyph for this character
	if ( hg->width == 0 )
	    continue;

	// handle special placement for sample_sheet_mode
	if ( sample_sheet_mode ) {
	    x_render_pos = (c % 32) * 32;
	    y_render_pos = (c / 32) * 64;
	    y_render_pos = 8*64 - y_render_pos;
	}

	printf("#  [[ %c ]] glyph(%d) width=%u npaths=%u\n",
			c, c, hg->width, hg->npaths);

	// walk the paths-list for this glyph
	struct hershey_path *hp;
	short px, py;
	for ( hp=hg->paths; hp; hp=hp->next ) {
	    // begin draw path
	    printf("#\t\tpath: nverts=%d\n", hp->nverts);
	    int i;
	    for ( i=0; i<hp->nverts; i++ ) {
		short x = hp->verts[i].x + x_render_pos;
		short y = hp->verts[i].y + y_render_pos;
		if ( i > 0 )
		    printf("set arrow from %d,%d to %d,%d as 1 ls 1\n",
			    px, py, x, y);
		px = x;
		py = y;
	    }
	    // end draw path
	}

	// advance the x_render_pos by the width of this glyph
	x_render_pos += hg->width;
    }

    printf("plot NaN notitle\n");

    // destroy the hershey_font
    hershey_font_free(hf);

    return 0;
}