gdtestft.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "gd.h"
  2. #include <string.h>
  3. #define PI 3.141592
  4. #define DEG2RAD(x) ((x)*PI/180.)
  5. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  6. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  7. #define MAX4(x,y,z,w) \
  8. ((MAX((x),(y))) > (MAX((z),(w))) ? (MAX((x),(y))) : (MAX((z),(w))))
  9. #define MIN4(x,y,z,w) \
  10. ((MIN((x),(y))) < (MIN((z),(w))) ? (MIN((x),(y))) : (MIN((z),(w))))
  11. #define MAXX(x) MAX4(x[0],x[2],x[4],x[6])
  12. #define MINX(x) MIN4(x[0],x[2],x[4],x[6])
  13. #define MAXY(x) MAX4(x[1],x[3],x[5],x[7])
  14. #define MINY(x) MIN4(x[1],x[3],x[5],x[7])
  15. int
  16. main (int argc, char *argv[])
  17. {
  18. #ifndef HAVE_LIBFREETYPE
  19. fprintf (stderr, "gd was not compiled with HAVE_LIBFREETYPE defined.\n");
  20. fprintf (stderr, "Install the FreeType library, including the\n");
  21. fprintf (stderr, "header files. Then edit the gd Makefile, type\n");
  22. fprintf (stderr, "make clean, and type make again.\n");
  23. return 1;
  24. #else
  25. gdImagePtr im;
  26. int black;
  27. int white;
  28. int brect[8];
  29. int x, y;
  30. char *err;
  31. FILE *out;
  32. #ifdef JISX0208
  33. char *s = "Hello. ‚±‚ñ‚É‚¿‚Í Qyjpqg,"; /* String to draw. */
  34. #else
  35. char *s = "Hello. Qyjpqg,"; /* String to draw. */
  36. #endif
  37. double sz = 40.;
  38. #if 0
  39. double angle = 0.;
  40. #else
  41. double angle = DEG2RAD (-90);
  42. #endif
  43. #ifdef JISX0208
  44. char *f = "/usr/openwin/lib/locale/ja/X11/fonts/TT/HG-MinchoL.ttf"; /* UNICODE */
  45. /* char *f = "/usr/local/lib/fonts/truetype/DynaFont/dfpop1.ttf"; *//* SJIS */
  46. #else
  47. char *f = "times"; /* TrueType font */
  48. #endif
  49. /* obtain brect so that we can size the image */
  50. err = gdImageStringFT ((gdImagePtr) NULL, &brect[0], 0, f, sz, angle, 0, 0, s);
  51. if (err)
  52. {
  53. fprintf (stderr, "%s", err);
  54. return 1;
  55. }
  56. /* create an image just big enough for the string */
  57. x = MAXX (brect) - MINX (brect) + 6;
  58. y = MAXY (brect) - MINY (brect) + 6;
  59. #if 0
  60. im = gdImageCreate (500, 500);
  61. #else
  62. /* gd 2.0: true color images can use freetype too */
  63. im = gdImageCreateTrueColor (x, y);
  64. #endif
  65. /* Background color. gd 2.0: fill the image with it; truecolor
  66. images have a black background otherwise. */
  67. white = gdImageColorResolve (im, 255, 255, 255);
  68. gdImageFilledRectangle (im, 0, 0, x, y, white);
  69. black = gdImageColorResolve (im, 0, 0, 0);
  70. /* render the string, offset origin to center string */
  71. x = 0 - MINX (brect) + 3;
  72. y = 0 - MINY (brect) + 3;
  73. err = gdImageStringFT (im, NULL, black, f, sz, angle, x, y, s);
  74. if (err)
  75. {
  76. fprintf (stderr, "%s", err);
  77. return 1;
  78. }
  79. /* TBB: Write img to test/fttest.png */
  80. out = fopen ("test/fttest.png", "wb");
  81. if (!out)
  82. {
  83. fprintf (stderr, "Can't create test/fttest.png\n");
  84. exit (1);
  85. }
  86. gdImagePng (im, out);
  87. fclose (out);
  88. fprintf (stderr, "Test image written to test/fttest.png\n");
  89. /* Destroy it */
  90. gdImageDestroy (im);
  91. return 0;
  92. #endif /* HAVE_LIBFREETYPE */
  93. }