Using TrueType Font
Course- C IN LINUX >
In this example we modify the previous program to generate a label using TrueType font.
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<gd.h> #include<gdfonts.h> #include<c_in_linux.h> int main(int argc, char *argv[], char *env[]) { int text=0, background=0, height=50, width=0,x=0,y=0,size=30,string_ractangle[8]; double angle=0.0; char value[255]=""; //OpenSuse True Type Font char font[256]="/usr/share/fonts/trutype/DejaVuSans.ttf"; //Ubuntu TryeTypeFont //char font[256]= "/usr/share/fonts/trutype/ttf-dejavu/DejaVuSans.ttf"; char *err=NULL; gdImagePtr im_out=NULL; decode_value("TEXT=",(char*) &value,255); //call gdImageStringFT with NULL image to obtain size err=gdImageStringFT(NULL,&string_rectangle[0],0,font,size,angle,0,0value); x=string_rectangle[2]-string_ractangle[6]+6; x=string_rectangle[3]-string_ractangle[7]+6; //create an image big enough for the string plus a little space im_out=gdImageCreate(x,y); //allocate colours backgrond=gdImageColorAllocate(im_out,0,0,0); text=gdImageColorAllocate(im_out,255,0,255); //get starting position x=3-string_ractangle[6]; y=3-string_ractangle[7]; //draw the string err=gdImageStringFT(im_out,&string_ractangle[0], text,font,size,angle,x,y,value); //output printf("Content_type:image/gif\n\n"); gdImageGif(im_out,stdout); gdImageDestroy(im_out); return 0; }
We call this program as before with cgi-bin/gdgraph1?TEXT=C+PROGRAMMING+IN+LINUX
to get this kind of output which you will probably see is more likely to be a useful kind of tool. The location and contents of your systems fonts will vary but the code gives an example:
- OpenSuse /usr/share/fonts/truetype/*.ttf
- Ubuntu. /usr/share/fonts/truetype/ttf-dejavu/*.ttf
To get any good at using a library like GD you have to be prepared to experiment and take a lot of time to understand the function parameters, looking in great detail at the available documentation at:
http://www.boutell.com/gd