Back to index

php5  5.3.10
gdtestft.c
Go to the documentation of this file.
00001 
00002 #include "gd.h"
00003 #include <string.h>
00004 
00005 #define PI 3.141592
00006 #define DEG2RAD(x) ((x)*PI/180.)
00007 
00008 #define MAX(x,y) ((x) > (y) ? (x) : (y))
00009 #define MIN(x,y) ((x) < (y) ? (x) : (y))
00010 
00011 #define MAX4(x,y,z,w) \
00012        ((MAX((x),(y))) > (MAX((z),(w))) ? (MAX((x),(y))) : (MAX((z),(w))))
00013 #define MIN4(x,y,z,w) \
00014        ((MIN((x),(y))) < (MIN((z),(w))) ? (MIN((x),(y))) : (MIN((z),(w))))
00015 
00016 #define MAXX(x) MAX4(x[0],x[2],x[4],x[6])
00017 #define MINX(x) MIN4(x[0],x[2],x[4],x[6])
00018 #define MAXY(x) MAX4(x[1],x[3],x[5],x[7])
00019 #define MINY(x) MIN4(x[1],x[3],x[5],x[7])
00020 
00021 int
00022 main (int argc, char *argv[])
00023 {
00024 #ifndef HAVE_LIBFREETYPE
00025   fprintf (stderr, "gd was not compiled with HAVE_LIBFREETYPE defined.\n");
00026   fprintf (stderr, "Install the FreeType library, including the\n");
00027   fprintf (stderr, "header files. Then edit the gd Makefile, type\n");
00028   fprintf (stderr, "make clean, and type make again.\n");
00029   return 1;
00030 #else
00031   gdImagePtr im;
00032   int black;
00033   int white;
00034   int brect[8];
00035   int x, y;
00036   char *err;
00037   FILE *out;
00038 #ifdef JISX0208
00039   char *s = "Hello. こんにちは Qyjpqg,";  /* String to draw. */
00040 #else
00041   char *s = "Hello. Qyjpqg,";      /* String to draw. */
00042 #endif
00043 
00044   double sz = 40.;
00045 
00046 #if 0
00047   double angle = 0.;
00048 #else
00049   double angle = DEG2RAD (-90);
00050 #endif
00051 
00052 #ifdef JISX0208
00053   char *f = "/usr/openwin/lib/locale/ja/X11/fonts/TT/HG-MinchoL.ttf"; /* UNICODE */
00054   /* char *f = "/usr/local/lib/fonts/truetype/DynaFont/dfpop1.ttf"; *//* SJIS */
00055 #else
00056   char *f = "times";        /* TrueType font */
00057 #endif
00058 
00059   /* obtain brect so that we can size the image */
00060   err = gdImageStringFT ((gdImagePtr) NULL, &brect[0], 0, f, sz, angle, 0, 0, s);
00061   if (err)
00062     {
00063       fprintf (stderr, "%s", err);
00064       return 1;
00065     }
00066 
00067   /* create an image just big enough for the string */
00068   x = MAXX (brect) - MINX (brect) + 6;
00069   y = MAXY (brect) - MINY (brect) + 6;
00070 #if 0
00071   im = gdImageCreate (500, 500);
00072 #else
00073   /* gd 2.0: true color images can use freetype too */
00074   im = gdImageCreateTrueColor (x, y);
00075 #endif
00076 
00077   /* Background color. gd 2.0: fill the image with it; truecolor
00078      images have a black background otherwise. */
00079   white = gdImageColorResolve (im, 255, 255, 255);
00080   gdImageFilledRectangle (im, 0, 0, x, y, white);
00081   black = gdImageColorResolve (im, 0, 0, 0);
00082 
00083   /* render the string, offset origin to center string */
00084   x = 0 - MINX (brect) + 3;
00085   y = 0 - MINY (brect) + 3;
00086 
00087   err = gdImageStringFT (im, NULL, black, f, sz, angle, x, y, s);
00088   if (err)
00089     {
00090       fprintf (stderr, "%s", err);
00091       return 1;
00092     }
00093   /* TBB: Write img to test/fttest.png */
00094   out = fopen ("test/fttest.png", "wb");
00095   if (!out)
00096     {
00097       fprintf (stderr, "Can't create test/fttest.png\n");
00098       exit (1);
00099     }
00100   gdImagePng (im, out);
00101   fclose (out);
00102   fprintf (stderr, "Test image written to test/fttest.png\n");
00103   /* Destroy it */
00104   gdImageDestroy (im);
00105 
00106   return 0;
00107 #endif /* HAVE_FREETYPE */
00108 }