...

Text file src/github.com/golang/freetype/cmd/print-glyph-points/main.c

Documentation: github.com/golang/freetype/cmd/print-glyph-points

     1/*
     2gcc main.c -I/usr/include/freetype2 -lfreetype && ./a.out 12 ../../testdata/luxisr.ttf with_hinting
     3*/
     4
     5#include <stdio.h>
     6#include <ft2build.h>
     7#include FT_FREETYPE_H
     8
     9void usage(char** argv) {
    10	fprintf(stderr, "usage: %s font_size font_file [with_hinting|sans_hinting]\n", argv[0]);
    11}
    12
    13int main(int argc, char** argv) {
    14	FT_Error error;
    15	FT_Library library;
    16	FT_Face face;
    17	FT_Glyph_Metrics* m;
    18	FT_Outline* o;
    19	FT_Int major, minor, patch;
    20	int i, j, font_size, no_hinting;
    21
    22	if (argc != 4) {
    23		usage(argv);
    24		return 1;
    25	}
    26	font_size = atoi(argv[1]);
    27	if (font_size <= 0) {
    28		fprintf(stderr, "invalid font_size\n");
    29		usage(argv);
    30		return 1;
    31	}
    32	if (!strcmp(argv[3], "with_hinting")) {
    33		no_hinting = 0;
    34	} else if (!strcmp(argv[3], "sans_hinting")) {
    35		no_hinting = 1;
    36	} else {
    37		fprintf(stderr, "neither \"with_hinting\" nor \"sans_hinting\"\n");
    38		usage(argv);
    39		return 1;
    40	};
    41	error = FT_Init_FreeType(&library);
    42	if (error) {
    43		fprintf(stderr, "FT_Init_FreeType: error #%d\n", error);
    44		return 1;
    45	}
    46	FT_Library_Version(library, &major, &minor, &patch);
    47	printf("freetype version %d.%d.%d\n", major, minor, patch);
    48	error = FT_New_Face(library, argv[2], 0, &face);
    49	if (error) {
    50		fprintf(stderr, "FT_New_Face: error #%d\n", error);
    51		return 1;
    52	}
    53	error = FT_Set_Char_Size(face, 0, font_size*64, 0, 0);
    54	if (error) {
    55		fprintf(stderr, "FT_Set_Char_Size: error #%d\n", error);
    56		return 1;
    57	}
    58	for (i = 0; i < face->num_glyphs; i++) {
    59		error = FT_Load_Glyph(face, i, no_hinting ? FT_LOAD_NO_HINTING : FT_LOAD_DEFAULT);
    60		if (error) {
    61			fprintf(stderr, "FT_Load_Glyph: glyph %d: error #%d\n", i, error);
    62			return 1;
    63		}
    64		if (face->glyph->format != FT_GLYPH_FORMAT_OUTLINE) {
    65			fprintf(stderr, "glyph format for glyph %d is not FT_GLYPH_FORMAT_OUTLINE\n", i);
    66			return 1;
    67		}
    68		m = &face->glyph->metrics;
    69		/* Print what Go calls the AdvanceWidth, and then: XMin, YMin, XMax, YMax. */
    70		printf("%ld %ld %ld %ld %ld;",
    71				m->horiAdvance,
    72				m->horiBearingX,
    73				m->horiBearingY - m->height,
    74				m->horiBearingX + m->width,
    75				m->horiBearingY);
    76		/* Print the glyph points. */
    77		o = &face->glyph->outline;
    78		for (j = 0; j < o->n_points; j++) {
    79			if (j != 0) {
    80				printf(", ");
    81			}
    82			printf("%ld %ld %d", o->points[j].x, o->points[j].y, o->tags[j] & 0x01);
    83		}
    84		printf("\n");
    85	}
    86	return 0;
    87}

View as plain text