1 #include <stdlib.h>
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <locale.h>
6 #include <libps/pslib-mp.h>
7 
my_malloc(PSDoc * p,size_t size,const char * caller)8 void * my_malloc(PSDoc *p, size_t size, const char *caller) {
9 	void *a;
10 	a = (void *) malloc(size);
11 	return(a);
12 }
13 
my_realloc(PSDoc * p,void * mem,size_t size,const char * caller)14 void * my_realloc(PSDoc *p, void *mem, size_t size, const char *caller) {
15 	return((void *) realloc(mem, size));
16 }
17 
my_free(PSDoc * p,void * mem)18 void my_free(PSDoc *p, void *mem) {
19 	free(mem);
20 }
21 
usage()22 void usage() {
23 	fprintf(stderr, "Usage: fontsample <list of fontnames>\n\n");
24 }
25 
26 
main(int argc,char * argv[])27 int main(int argc, char *argv[]) {
28 	PSDoc *psdoc;
29 	int psfont, samplefont;
30 	float bottommargin, colwidth, rowheight, leftmargin;
31 	float textwidth, fontsize, fontsamplesize;
32 	int i, j, k;
33 	char *text;
34 
35 	colwidth = 25;
36 	rowheight = 25;
37 	leftmargin = 100;
38 	bottommargin = 270;
39 	fontsize = 10.0;
40 	fontsamplesize = 12.0;
41 
42 	if(argc < 2) {
43 		usage();
44 		exit(1);
45 	}
46 
47 	PS_mp_init();
48 	PS_boot();
49 	psdoc = PS_new2(NULL, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
50 	PS_open_file(psdoc, "fontsample.ps");
51 	PS_set_info(psdoc, "Creator", __FILE__);
52 	PS_set_info(psdoc, "Author", "Uwe Steinmann");
53 	PS_set_info(psdoc, "Title", "Font sample");
54 	PS_set_info(psdoc, "Keywords", "font, sample");
55 	PS_set_info(psdoc, "BoundingBox", "0 0 596 842");
56 
57 	PS_set_parameter(psdoc, "hyphenation", "true");
58 	PS_set_parameter(psdoc, "hyphendict", "hyph_en.dic");
59 
60 	psfont = PS_findfont(psdoc, "Helvetica", NULL, 1);
61 	fprintf(stdout, "Creating %d pages\n", argc-1);
62 	for(k=1; k<argc; k++) {
63 		fprintf(stdout, "Creating page for %s\n", argv[k]);
64 		text = malloc(100);
65 
66 		PS_begin_page(psdoc, 596, 842);
67 		samplefont = PS_findfont(psdoc, argv[k], "builtin", 1);
68 
69 		PS_setfont(psdoc, psfont, 15.0);
70 		sprintf(text, "Fontsample for '%s'", PS_get_parameter(psdoc, "fontname", (float) samplefont));
71 		PS_show_xy(psdoc, text, leftmargin, bottommargin+20*rowheight);
72 		PS_setfont(psdoc, psfont, 10.0);
73 		PS_show_xy(psdoc, "Font encoding: ", leftmargin, bottommargin+19.0*rowheight);
74 		PS_show(psdoc, PS_get_parameter(psdoc, "fontencoding", (float) samplefont));
75 		PS_show_xy(psdoc, "capheight: ", leftmargin, bottommargin+18.5*rowheight);
76 		sprintf(text, "%.2f", PS_get_value(psdoc, "capheight", (float) samplefont));
77 		PS_show(psdoc, text);
78 		PS_show_xy(psdoc, "ascender: ", leftmargin, bottommargin+18.0*rowheight);
79 		sprintf(text, "%.2f", PS_get_value(psdoc, "ascender", (float) samplefont));
80 		PS_show(psdoc, text);
81 		PS_show_xy(psdoc, "descender: ", leftmargin, bottommargin+17.5*rowheight);
82 		sprintf(text, "%.2f", PS_get_value(psdoc, "descender", (float) samplefont));
83 		PS_show(psdoc, text);
84 
85 		for(i=0; i<=16; i++) {
86 			PS_moveto(psdoc, leftmargin+i*colwidth, bottommargin);
87 			PS_lineto(psdoc, leftmargin+i*colwidth, bottommargin+16*rowheight);
88 			PS_stroke(psdoc);
89 		}
90 		for(j=0; j<=16; j++) {
91 			PS_moveto(psdoc, leftmargin, bottommargin+j*rowheight);
92 			PS_lineto(psdoc, leftmargin+16*colwidth, bottommargin+j*rowheight);
93 			PS_stroke(psdoc);
94 		}
95 
96 		PS_setfont(psdoc, psfont, fontsize);
97 		for(i=0; i<16; i++) {
98 			sprintf(text, "x%X", i);
99 			textwidth = PS_stringwidth2(psdoc, text,  -1, psfont, fontsize);
100 			PS_show_xy(psdoc, text, leftmargin+i*colwidth+colwidth/2-textwidth/2, bottommargin+16*rowheight+fontsize/2);
101 		}
102 		for(j=0; j<16; j++) {
103 			sprintf(text, "%Xx", j);
104 			PS_show_xy(psdoc, text, leftmargin-1.7*fontsize, bottommargin+(15-j)*rowheight+rowheight/2-fontsize/2);
105 		}
106 
107 		/* The symbol itself */
108 		PS_setfont(psdoc, samplefont, fontsamplesize);
109 		PS_setlinewidth(psdoc, 0.4);
110 		for(j=0; j<16; j++) {
111 			for(i=0; i<16; i++) {
112 				textwidth = PS_symbol_width(psdoc, j*16+i, 0, fontsamplesize);
113 				PS_set_value(psdoc, "textx", leftmargin+i*colwidth+colwidth/2-textwidth/2);
114 				PS_set_value(psdoc, "texty", bottommargin+(15-j)*rowheight+rowheight/2+3-fontsize/2);
115 				//PS_show_xy(psdoc, text, leftmargin+i*colwidth+colwidth/2-textwidth/2, bottommargin+(15-j)*rowheight+rowheight/2-fontsize/2);
116 	//			PS_show(psdoc, text);
117 				PS_symbol(psdoc, j*16+i);
118 				PS_moveto(psdoc, leftmargin+i*colwidth+colwidth/2-textwidth/2, bottommargin+(15-j)*rowheight+rowheight/2+3-fontsize/2);
119 				PS_lineto(psdoc, leftmargin+i*colwidth+colwidth/2+textwidth/2, bottommargin+(15-j)*rowheight+rowheight/2+3-fontsize/2);
120 				PS_stroke(psdoc);
121 			}
122 		}
123 
124 		/* The name of the symbol */
125 		PS_setfont(psdoc, psfont, 3);
126 		for(j=0; j<16; j++) {
127 			for(i=0; i<16; i++) {
128 				PS_symbol_name(psdoc, j*16+i, samplefont, text, 100);
129 				PS_set_value(psdoc, "textx", leftmargin+i*colwidth+2);
130 				PS_set_value(psdoc, "texty", bottommargin+(15-j)*rowheight+2);
131 				PS_show(psdoc, text);
132 				sprintf(text, " (%d)", j*16+i);
133 				PS_show(psdoc, text);
134 			}
135 		}
136 		free(text);
137 
138 		text = "This fontsample matrix is created with the PS_symbol() function. It prints a glyph by its decimal value in the font encoding vector. pslib can use the TeXBase1 encoding (default) or the encoding shipped with the font itself, which is usually AdobeStandardEncoding. The above matrix uses the encoding of the font which is used when the parameter 'encoding' of PS_findfont() is set to 'builtin'. This usually results in a subset of all available glyphs of the font.\n\nQuite a lot of glyphs cannot be reached without the PS_symbol() function. The reason is simple: If text is output with PS_show(), then each character will be first mapped to a glyph by applying the ISO-8859-1 input encoding on it. Since ISO-8859-1 has less characters then a font usually provides glyphs, there will be some glyphs not reachable with regular text.";
139 		PS_setfont(psdoc, psfont, 8);
140 		PS_set_value(psdoc, "leading", 12.0);
141 		PS_set_value(psdoc, "parindent", 12.0);
142 		PS_show_boxed(psdoc, text, leftmargin, bottommargin-250, colwidth*7.5, 230, "fulljustify", NULL);
143 
144 		PS_setfont(psdoc, samplefont, 8);
145 		PS_set_value(psdoc, "leading", 12.0);
146 		PS_set_value(psdoc, "parindent", 12.0);
147 		PS_show_boxed(psdoc, text, leftmargin+colwidth*8.5, bottommargin-250, colwidth*7.5, 230, "fulljustify", NULL);
148 
149 		PS_deletefont(psdoc, samplefont);
150 		PS_end_page(psdoc);
151 	}
152 	PS_deletefont(psdoc, psfont);
153 	PS_close(psdoc);
154 	PS_delete(psdoc);
155 	PS_mp_list_unfreed();
156 	PS_shutdown();
157 	exit(0);
158 }
159