File: gdtestft.c

package info (click to toggle)
libgd2 2.0.36~rc1~dfsg-6.1+deb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,764 kB
  • sloc: ansic: 33,529; sh: 9,607; makefile: 678; perl: 160; tcl: 45; awk: 43
file content (206 lines) | stat: -rw-r--r-- 5,867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gd.h"
#include <string.h>
#include <stdlib.h>

#define PI 3.141592
#define DEG2RAD(x) ((x)*PI/180.)

#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y))

#define MAX4(x,y,z,w) \
	((MAX((x),(y))) > (MAX((z),(w))) ? (MAX((x),(y))) : (MAX((z),(w))))
#define MIN4(x,y,z,w) \
	((MIN((x),(y))) < (MIN((z),(w))) ? (MIN((x),(y))) : (MIN((z),(w))))

#define MAXX(x) MAX4(x[0],x[2],x[4],x[6])
#define MINX(x) MIN4(x[0],x[2],x[4],x[6])
#define MAXY(x) MAX4(x[1],x[3],x[5],x[7])
#define MINY(x) MIN4(x[1],x[3],x[5],x[7])

int
main (int argc, char *argv[])
{
#ifndef HAVE_LIBFREETYPE
  fprintf (stderr, "gd was not compiled with HAVE_LIBFREETYPE defined.\n");
  fprintf (stderr, "Install the FreeType library, including the\n");
  fprintf (stderr, "header files. Then edit the gd Makefile, type\n");
  fprintf (stderr, "make clean, and type make again.\n");
  return 1;
#else
  gdImagePtr im;
  int blue;
  int blueAlpha;
  int white;
  int brect[8];
  int x, y, sx, sy;
  char *err;
  FILE *out;
#ifdef JISX0208
  char *s = "Hello. ����ɂ��� Qyjpqg,";	/* String to draw. */
#else
  char *s = "Hello. ��� Qyjpqg,";	/* String to draw. */
#endif

  double sz = 40.;

#if 0
  double angle = 0.;
#else
  double angle = DEG2RAD (90);
#endif
  char *f;
  if (argc == 2)
    {
      f = argv[1];
    }
  else
    {
      /* 2.02: usage message. Defaulting to Times wasn't working well for the
         many people with no /usr/share/fonts/truetype. */
      fprintf (stderr, "Usage: gdtestft fontfilename\n");
      fprintf (stderr,
	       "If fontfilename is not a full or relative path, GDFONTPATH is searched for\n"
	       "it. If GDFONTPATH is not set, /usr/share/fonts/truetype is searched.\n");
      exit (1);
    }
  /* obtain brect so that we can size the image */
  err =
    gdImageStringFT ((gdImagePtr) NULL, &brect[0], 0, f, sz, angle, 0, 0, s);
  if (err)
    {
      fprintf (stderr, "%s\n", err);
      return 1;
    }

  /* create an image just big enough for the string (x3) */
  sx = MAXX (brect) - MINX (brect) + 6;
  sy = MAXY (brect) - MINY (brect) + 6;
#if 0
  /* Would be palette color 8-bit (which of course is still allowed,
     but not impressive when used with a JPEG background and antialiasing
     and alpha channel and so on!) */
  im = gdImageCreate (sx * 3, sy);
#else
  /* gd 2.0: true color images can use freetype too,
     and they can do antialiasing against arbitrary
     complex backgrounds. */
  im = gdImageCreateTrueColor (sx * 3, sy);
#endif
  /* Background color. gd 2.0: fill the image with it; truecolor
     images have a black background otherwise. */
  white = gdImageColorResolve (im, 255, 255, 255);
  /* Load a pretty background and resample it to cover the entire image */
  {
    FILE *in = fopen ("eleanor.jpg", "rb");
    gdImagePtr imb;
    if (in)
      {
#ifdef HAVE_LIBJPEG
	imb = gdImageCreateFromJpeg (in);
#else
	fprintf (stderr, "No JPEG library support.\n");
#endif
	if (!imb)
	  {
	    fprintf (stderr, "gdImageCreateFromJpeg failed\n");
	    exit (1);
	  }
	if (!im->trueColor)
	  {
	    /* If destination is not truecolor, convert the JPEG to a
	       reasonably high-quality palette version. This is not as good
	       as creating a truecolor output file, of course. Leave many
	       colors for text smoothing. */
#if 1
	    gdImageTrueColorToPalette (imb, 0, 128);
#endif
	  }
	/* Resample background image to cover new image exactly */
	gdImageCopyResampled (im, imb, 0, 0, 0, 0, sx * 3, sy,
			      gdImageSX (imb), gdImageSY (imb));
      }
    else
      {
	/* Can't get background, so paint a simple one */
	/* Truecolor images start out black, so paint it white */
	gdImageFilledRectangle (im, 0, 0, sx * 3, sy, white);
      }
  }
  /* TBB 2.0.2: only black was working, and I didn't know it because
     the test program used black. Funny, huh? Let's do a more interesting
     color this time.  */
  blue = gdImageColorResolve (im, 128, 192, 255);
  /* Almost-transparent blue (alpha blending), with antialiasing */
  blueAlpha = gdImageColorResolveAlpha (im, 128, 192, 255, gdAlphaMax / 2);
  /* render the string, offset origin to center string */
  x = 0 - MINX (brect) + 3;
  y = 0 - MINY (brect) + 3;

  /* With antialiasing (positive color value) */
  err = gdImageStringFT (im, NULL, blue, f, sz, angle, x, y, s);
  if (err)
    {
      fprintf (stderr, "%s\n", err);
      return 1;
    }
  /* Without antialiasing (negative color value) */
  err = gdImageStringFT (im, NULL, -blue, f, sz, angle, sx + x, y, s);
  if (err)
    {
      fprintf (stderr, "%s\n", err);
      return 1;
    }
  /* With antialiasing, and 50% alpha blending (truecolor only) */
  err = gdImageStringFT (im, NULL, blueAlpha, f, sz, angle, sx * 2 + x, y, s);
  if (err)
    {
      fprintf (stderr, "%s\n", err);
      return 1;
    }
  /* TBB: Write img to test/fttest.jpg or test/fttest.png */
  if (im->trueColor)
    {
      out = fopen ("test/fttest.jpg", "wb");
      if (!out)
	{
	  fprintf (stderr, "Can't create test/fttest.jpg\n");
	  exit (1);
	}
      /* Fairly high JPEG quality setting */
#ifdef HAVE_LIBJPEG
      gdImageJpeg (im, out, 90);
#else
      fprintf (stderr, "No JPEG library support.\n");
#endif
      fclose (out);
      fprintf (stderr, "Test image written to test/fttest.jpg\n");
    }
  else
    {
      out = fopen ("test/fttest.png", "wb");
      if (!out)
	{
	  fprintf (stderr, "Can't create test/fttest.png\n");
	  exit (1);
	}
      /* 2.0.10: correct ifdef, thanks to Gabriele Verzeletti */
#ifdef HAVE_LIBPNG
      gdImagePng (im, out);
#else
      fprintf (stderr, "No PNG library support.\n");
#endif
      fclose (out);
      fprintf (stderr, "Test image written to test/fttest.png\n");
    }
  /* Destroy it */
  gdImageDestroy (im);

  return 0;
#endif /* HAVE_LIBFREETYPE */
}