1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include "gd.h"
6 #include <string.h>
7 #include <stdlib.h>
8 
9 #define PI 3.141592
10 #define DEG2RAD(x) ((x)*PI/180.)
11 
12 #define MAX(x,y) ((x) > (y) ? (x) : (y))
13 #define MIN(x,y) ((x) < (y) ? (x) : (y))
14 
15 #define MAX4(x,y,z,w) \
16 	((MAX((x),(y))) > (MAX((z),(w))) ? (MAX((x),(y))) : (MAX((z),(w))))
17 #define MIN4(x,y,z,w) \
18 	((MIN((x),(y))) < (MIN((z),(w))) ? (MIN((x),(y))) : (MIN((z),(w))))
19 
20 #define MAXX(x) MAX4(x[0],x[2],x[4],x[6])
21 #define MINX(x) MIN4(x[0],x[2],x[4],x[6])
22 #define MAXY(x) MAX4(x[1],x[3],x[5],x[7])
23 #define MINY(x) MIN4(x[1],x[3],x[5],x[7])
24 
25 int
main(int argc,char * argv[])26 main (int argc, char *argv[])
27 {
28 	gdImagePtr im;
29 	int blue;
30 	int blueAlpha;
31 	int white;
32 	int brect[8];
33 	int x, y, sx, sy;
34 	char *err;
35 #ifdef JISX0208
36 	char *s = "Hello. ����ɂ��� Qyjpqg,";	/* String to draw. */
37 #else
38 	char *s = "Hello. こんにちは Qyjpqg,";	/* String to draw. */
39 #endif
40 
41 	double sz = 40.;
42 
43 #if 0
44 	double angle = 0.;
45 #else
46 	double angle = DEG2RAD (90);
47 #endif
48 	char *f;
49 	if (argc == 2) {
50 		f = argv[1];
51 	} else {
52 		/* 2.02: usage message. Defaulting to Times wasn't working well for the
53 		   many people with no /usr/share/fonts/truetype. */
54 		fprintf(stderr, "Usage: gdtestft fontfilename\n"
55 			 "If fontfilename is not a full or relative path, GDFONTPATH is searched for\n"
56 		         "it. If GDFONTPATH is not set, /usr/share/fonts/truetype is searched.\n");
57 		return 1;
58 	}
59 	/* obtain brect so that we can size the image */
60 	err =
61 	    gdImageStringFT ((gdImagePtr) NULL, &brect[0], 0, f, sz, angle, 0, 0, s);
62 	if (err) {
63 		fprintf(stderr, "%s\n", err);
64 		return 1;
65 	}
66 
67 	/* create an image just big enough for the string (x3) */
68 	sx = MAXX (brect) - MINX (brect) + 6;
69 	sy = MAXY (brect) - MINY (brect) + 6;
70 #if 0
71 	/* Would be palette color 8-bit (which of course is still allowed,
72 	   but not impressive when used with a JPEG background and antialiasing
73 	   and alpha channel and so on!) */
74 	im = gdImageCreate (sx * 3, sy);
75 #else
76 	/* gd 2.0: true color images can use freetype too,
77 	   and they can do antialiasing against arbitrary
78 	   complex backgrounds. */
79 	im = gdImageCreateTrueColor (sx * 3, sy);
80 #endif
81 	/* Background color. gd 2.0: fill the image with it; truecolor
82 	   images have a black background otherwise. */
83 	white = gdImageColorResolve (im, 255, 255, 255);
84 	/* Load a pretty background and resample it to cover the entire image */
85 	{
86 		FILE *in = fopen ("eleanor.jpg", "rb");
87 		gdImagePtr imb = NULL;
88 		if (in) {
89 #ifdef HAVE_LIBJPEG
90 			imb = gdImageCreateFromJpeg (in);
91 #else
92 			fprintf(stderr, "No JPEG library support.\n");
93 #endif
94 			fclose(in);
95 
96 			if (!imb) {
97 				fprintf(stderr, "gdImageCreateFromJpeg failed\n");
98 				return 1;
99 			}
100 			if (!im->trueColor) {
101 				/* If destination is not truecolor, convert the JPEG to a
102 				   reasonably high-quality palette version. This is not as good
103 				   as creating a truecolor output file, of course. Leave many
104 				   colors for text smoothing. */
105 #if 1
106 				gdImageTrueColorToPalette (imb, 0, 128);
107 #endif
108 			}
109 			/* Resample background image to cover new image exactly */
110 			gdImageCopyResampled (im, imb, 0, 0, 0, 0, sx * 3, sy,
111 			                      gdImageSX (imb), gdImageSY (imb));
112 		} else {
113 			/* Can't get background, so paint a simple one */
114 			/* Truecolor images start out black, so paint it white */
115 			gdImageFilledRectangle (im, 0, 0, sx * 3, sy, white);
116 		}
117 	}
118 	/* TBB 2.0.2: only black was working, and I didn't know it because
119 	   the test program used black. Funny, huh? Let's do a more interesting
120 	   color this time.  */
121 	blue = gdImageColorResolve (im, 128, 192, 255);
122 	/* Almost-transparent blue (alpha blending), with antialiasing */
123 	blueAlpha = gdImageColorResolveAlpha (im, 128, 192, 255, gdAlphaMax / 2);
124 	/* render the string, offset origin to center string */
125 	x = 0 - MINX (brect) + 3;
126 	y = 0 - MINY (brect) + 3;
127 
128 	/* With antialiasing (positive color value) */
129 	err = gdImageStringFT (im, NULL, blue, f, sz, angle, x, y, s);
130 	if (err) {
131 		fprintf(stderr, "%s\n", err);
132 		return 1;
133 	}
134 	/* Without antialiasing (negative color value) */
135 	err = gdImageStringFT (im, NULL, -blue, f, sz, angle, sx + x, y, s);
136 	if (err) {
137 		fprintf(stderr, "%s\n", err);
138 		return 1;
139 	}
140 	/* With antialiasing, and 50% alpha blending (truecolor only) */
141 	err = gdImageStringFT (im, NULL, blueAlpha, f, sz, angle, sx * 2 + x, y, s);
142 	if (err) {
143 		fprintf(stderr, "%s\n", err);
144 		return 1;
145 	}
146 	/* TBB: Write img to test/fttest.jpg or test/fttest.png */
147 	if (im->trueColor) {
148 #ifdef HAVE_LIBJPEG
149 		FILE *out = fopen ("test/fttest.jpg", "wb");
150 		if (!out) {
151 			fprintf(stderr, "Can't create test/fttest.jpg\n");
152 			exit (1);
153 		}
154 		/* Fairly high JPEG quality setting */
155 		gdImageJpeg (im, out, 90);
156 		fclose (out);
157 		fprintf(stderr, "Test image written to test/fttest.jpg\n");
158 #else
159 		fprintf(stderr, "Test image not written; No JPEG library support.\n");
160 #endif
161 	} else {
162 #ifdef HAVE_LIBPNG
163 		FILE *out = fopen ("test/fttest.png", "wb");
164 		if (!out) {
165 			fprintf(stderr, "Can't create test/fttest.png\n");
166 			exit (1);
167 		}
168 		/* 2.0.10: correct ifdef, thanks to Gabriele Verzeletti */
169 		gdImagePng (im, out);
170 		fclose (out);
171 		fprintf(stderr, "Test image written to test/fttest.png\n");
172 #else
173 		fprintf(stderr, "Test image not written; No PNG library support.\n");
174 #endif
175 	}
176 	/* Destroy it */
177 	gdImageDestroy (im);
178 
179 	return 0;
180 }
181