Commits

Anonymous committed d7ddc1c

- example of antialiased text using alpha and not blending ops

  • Participants
  • Parent commits 247b7bd

Comments (0)

Files changed (1)

text_on_transparent_background/text_aa_alpha.c

+#include <gd.h>
+#include <stdio.h>
+#include <math.h>
+
+int main()
+{
+	gdImagePtr im, tmp_im;
+	int error = 0;
+	char *fontname = "/var/lib/defoma/fontconfig.d/B/Bitstream-Vera-Sans.ttf";
+	char *string = "AA using alpha";
+	int linespacing	= 1.05;
+	int charmap    	= gdFTEX_Unicode;
+	int resolution		= 96;
+	int color_text 	= 0xFFFFFF;
+	int pt_size 		= 50;
+	double angle 		= -45 * M_PI / 180;
+	int pos_x = 0, pos_y = 40;
+	FILE *fp;
+	gdFTStringExtra extra;
+
+	/* Optional but it makes the code clearer and forward compatible */
+	extra.flags = gdFTEX_CHARMAP|gdFTEX_RESOLUTION;
+	extra.charmap = gdFTEX_Unicode;
+	extra.hdpi = resolution;
+	extra.vdpi = resolution;
+
+	im = gdImageCreateTrueColor(400,400);
+
+	/* We want to presereve the alpha information, don't blend */
+	gdImageAlphaBlending(im, 0);
+
+	/* let fill it with zero opacity (gdMaxAlphaMax) */
+	gdImageFilledRectangle(im, 0,0, 399,399, gdImageColorAllocateAlpha(im, 0,0,0, gdAlphaMax));
+	gdImageStringFTEx(im, NULL, color_text, fontname, pt_size, angle, pos_x, pos_y, string, &extra);
+
+	/* Save the alpha channel in the image, that disallows the usage
+		of a background/transparent color. See PNG specs for the details.
+	 */
+ 	gdImageSaveAlpha(im, 1);
+	fp = fopen("1.png", "wb");
+ 	gdImagePng(im,fp);
+	fclose(fp);
+
+ 	/* Destroy images */
+ 	gdImageDestroy(im);
+	return error;
+}