"Fossies" - the Fresh Open Source Software Archive

Member "libgd-2.3.2/src/gdtestft.c" (3 Mar 2021, 5269 Bytes) of package /linux/www/libgd-2.3.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gdtestft.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.3.1_vs_2.3.2.

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