NEOERR* mimg_create_from_string(char *s, char *path, double size, void **pic) { gdImagePtr im; int draw, back, brect[8], x, y; char *gderr; MCS_NOT_NULLB(s, pic); gderr = gdImageStringFT(NULL, &brect[0], 0, path, size, 0., 0, 0, s); if (gderr) return nerr_raise(NERR_ASSERT, "create image failure %s", gderr); x = brect[2]-brect[6] + 6; y = brect[3]-brect[7] + 6; im = gdImageCreate(x, y); if (!im) return nerr_raise(NERR_ASSERT, "create image failure"); /* background color */ back = gdImageColorResolve(im, 252, 252, 252); /* foreground color */ draw = gdImageColorResolve(im, 150, 40, 40); x = 3 - brect[6]; y = 3 - brect[7]; gderr = gdImageStringFT(im, &brect[0], draw, path, size, 0.0, x, y, s); if (gderr) return nerr_raise(NERR_ASSERT, "rend image failure %s", gderr); *pic = (void*)im; return STATUS_OK; }
/** * Draws a strings centered in the rectangle specified by x, y, w, and h */ void CarModule::draw_string(int x, int y, int w, int h, int col, char *font, int size, char *str) { int brect[8]; gdImageStringFT(NULL, &brect[0], col, font, size, 0.0, 0, 0, str); gdImageStringFT(img, &brect[0], col, font, size, 0.0, x + (w - brect[2] - brect[0])/2, y + (h - brect[1] - brect[7])/2, str); }
int renderTruetypeSymbolGD(imageObj *img, double x, double y, symbolObj *symbol, symbolStyleObj *s) { #ifdef USE_GD_FREETYPE int bbox[8]; char *error; int c,oc = 0; gdImagePtr ip; if(!(ip = MS_IMAGE_GET_GDIMAGEPTR(img))) return MS_FAILURE; SETPEN(ip, s->color); SETPEN(ip, s->outlinecolor); if(s->style->antialias) { c = s->color->pen; if(s->outlinecolor) oc = s->outlinecolor->pen; } else { c = -s->color->pen; if(s->outlinecolor) oc = -s->outlinecolor->pen; } gdImageStringFT(NULL, bbox, c, symbol->full_font_path, s->scale, s->rotation, 0,0, symbol->character); x -= (bbox[2] - bbox[0])/2 + bbox[0]; y += (bbox[1] - bbox[5])/2 - bbox[1]; if( s->outlinecolor ) { error = gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x, y-1, symbol->character); if(error) { msSetError(MS_TTFERR, "%s", "renderTruetypeSymbolGD()", error); return MS_FAILURE; } gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x, y+1, symbol->character); gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x+1, y, symbol->character); gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x-1, y, symbol->character); gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x+1, y+1, symbol->character); gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x+1, y-1, symbol->character); gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x-1, y+1, symbol->character); gdImageStringFT(ip, bbox, oc, symbol->full_font_path, s->scale, s->rotation, x-1, y-1, symbol->character); } if(s->color) gdImageStringFT(ip, bbox, c, symbol->full_font_path, s->scale, s->rotation, x, y, symbol->character); return MS_SUCCESS; #else msSetError(MS_TTFERR, "Freetype support not enabled in this GD build", "renderTruetypeSymbolGD()"); return MS_FAILURE; #endif }
int graphicsGdImageStringFT(gdImage *im, ScmPair **brect0, ScmPair **brect1, ScmPair **brect2, ScmPair **brect3, int fg, ScmString *fontlist, double ptsize, double angle, int x, int y, ScmString *str) { int brect[8]; char *s = Scm_GetString(str); char *fl = Scm_GetString(fontlist); char *e = gdImageStringFT(im, brect, fg, fl, ptsize, angle, x, y, s); if (e == NULL) { *brect0 = SCM_NEW(ScmPair); SCM_SET_CAR(*brect0, Scm_MakeInteger(brect[0])); SCM_SET_CDR(*brect0, Scm_MakeInteger(brect[1])); *brect1 = SCM_NEW(ScmPair); SCM_SET_CAR(*brect1, Scm_MakeInteger(brect[2])); SCM_SET_CDR(*brect1, Scm_MakeInteger(brect[3])); *brect2 = SCM_NEW(ScmPair); SCM_SET_CAR(*brect2, Scm_MakeInteger(brect[4])); SCM_SET_CDR(*brect2, Scm_MakeInteger(brect[5])); *brect3 = SCM_NEW(ScmPair); SCM_SET_CAR(*brect3, Scm_MakeInteger(brect[6])); SCM_SET_CDR(*brect3, Scm_MakeInteger(brect[7])); return 0; } else { graphicsGdRaiseCondition("gdImageStringFT failed: %s", e); return -1; } }
point gd_textsize(char *str, char *fontname, double fontsz) { char *fontlist,*err; point rv; int brect[8]; fontlist = gd_alternate_fontlist(fontname); rv.x = rv.y = 0.0; if (fontlist && *str) { if (fontsz <= FONTSIZE_MUCH_TOO_SMALL) { /* ignore entirely */ rv.x = rv.y = 0; return rv; } else if (fontsz <= FONTSIZE_TOO_SMALL) { /* draw line in place of text */ /* fake a finite fontsize so that line length is calculated */ fontsz = FONTSIZE_TOO_SMALL; } /* call gdImageStringFT with null *im to get brect */ err = gdImageStringFT(NULL, brect, -1, fontlist, fontsz, 0, 0, 0, str); if (!err) { rv.x = (brect[4] - brect[0]); /* rv.y = (brect[5] - brect[1]); */ rv.y = (brect[5] - 0 ); /* ignore descenders */ rv.x /= SCALE; rv.y /= SCALE; } } return rv; }
int main() { gdImagePtr im, im2; int error = 0; char path[2048]; const char *file_exp = "bug00132_exp.png"; char *ret = NULL; im = gdImageCreateTrueColor(50, 30); if (!im) { printf("can't get truecolor image\n"); return 1; } gdImageAlphaBlending(im, 0); gdImageFilledRectangle(im, 0, 0, 200, 200, gdTrueColorAlpha(0, 0, 0, 127)); sprintf(path, "%s/freetype/DejaVuSans.ttf", GDTEST_TOP_DIR); ret = gdImageStringFT(im, NULL, - 0xFFFFFF, path, 14.0, 0.0, 10, 20, "ϑ θ"); if (ret) { error = 1; printf(ret); } else { sprintf(path, "%s/freetype/%s", GDTEST_TOP_DIR, file_exp); if (!gdAssertImageEqualsToFile(path, im)) { error = 1; printf("Reference image and destination differ\n"); } } gdImageDestroy(im); return error; }
char * gdImageStringTTF (gdImage * im, int *brect, int fg, char *fontlist, double ptsize, double angle, int x, int y, char *string) { /* 2.0.6: valid return */ return gdImageStringFT (im, brect, fg, fontlist, ptsize, angle, x, y, string); }
int renderGlyphsGD(imageObj *img, double x, double y, labelStyleObj *style, char *text) { #ifdef USE_GD_FREETYPE gdImagePtr ip; char *error=NULL; int bbox[8]; int c = 0,oc = 0; x = MS_NINT(x); y = MS_NINT(y); if(!(ip = MS_IMAGE_GET_GDIMAGEPTR(img))) return MS_FAILURE; if(!text || !strlen(text)) return(MS_SUCCESS); /* not errors, just don't want to do anything */ SETPEN(ip, style->color); SETPEN(ip, style->outlinecolor); if(style->antialias) { if(style->color) c = style->color->pen; if(style->outlinewidth > 0) oc = style->outlinecolor->pen; } else { if(style->color) c = -style->color->pen; if(style->outlinewidth > 0) oc = -style->outlinecolor->pen; } if(style->outlinewidth > 0) { /* handle the outline color */ error = gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x, y-1, text); if(error) { msSetError(MS_TTFERR, "%s", "msDrawTextGD()", error); return(MS_FAILURE); } gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x, y+1, text); gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x+1, y, text); gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x-1, y, text); gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x-1, y-1, text); gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x-1, y+1, text); gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x+1, y-1, text); gdImageStringFT(ip, bbox, oc, style->fonts[0], style->size, style->rotation, x+1, y+1, text); } if(style->color) gdImageStringFT(ip, bbox, c, style->fonts[0], style->size, style->rotation, x, y, text); return MS_SUCCESS; #else msSetError(MS_TTFERR, "Freetype support not enabled in this GD build", "renderGlyphsGD()"); return MS_FAILURE; #endif }
int getTruetypeTextBBoxGD(rendererVTableObj *renderer, char **fonts, int numfonts, double size, char *string, rectObj *rect, double **advances, int bAdjustBaseline) { #ifdef USE_GD_FT int bbox[8]; char *error; if(advances) { #if defined (GD_HAS_FTEX_XSHOW) char *s; int k; gdFTStringExtra strex; strex.flags = gdFTEX_XSHOW; error = gdImageStringFTEx(NULL, bbox, 0, fonts[0], size, 0, 0, 0, string, &strex); if(error) { msSetError(MS_TTFERR, error, "gdImageStringFTEx()"); return(MS_FAILURE); } *advances = (double*)malloc( strlen(string) * sizeof(double) ); MS_CHECK_ALLOC(*advances, strlen(string) * sizeof(double), MS_FAILURE); s = strex.xshow; k = 0; /* TODO this smells buggy and can cause errors at a higher level. strlen NOK here*/ while ( *s && k < strlen(string) ) { (*advances)[k++] = atof(s); while ( *s && *s != ' ') s++; if ( *s == ' ' ) s++; } gdFree(strex.xshow); /* done with character advances */ rect->minx = bbox[0]; rect->miny = bbox[5]; rect->maxx = bbox[2]; rect->maxy = bbox[1]; return MS_SUCCESS; #else msSetError(MS_TTFERR, "gdImageStringFTEx support is not available or is not current enough (requires 2.0.29 or higher).", "msGetTruetypeTextBBox()"); return(MS_FAILURE); #endif } else { error = gdImageStringFT(NULL, bbox, 0, fonts[0], size, 0, 0, 0, string); if(error) { msSetError(MS_TTFERR, error, "msGetTruetypeTextBBox()"); return(MS_FAILURE); } rect->minx = bbox[0]; rect->miny = bbox[5]; rect->maxx = bbox[2]; rect->maxy = bbox[1]; return MS_SUCCESS; } #else msSetError(MS_TTFERR,"Truetype support not available", "getTruetypeTextBBoxGD()"); return(MS_FAILURE); #endif }
void dosizes (gdImagePtr im, int color, char *fontfile, int x, int y, const char *string) { int brect[8]; double curang = 0.0; char *cp; int cursize; char buf[60]; for (cursize = 1; cursize <= 20; cursize++) { sprintf (buf, "%d: %s", cursize, string); /* The case of newlines is taken care of in the gdImageStringTTF call */ #if defined(OLDER_GD) cp = gdImageStringTTF (im, brect, color, fontfile, cursize, curang, x, y, buf); #else cp = gdImageStringFT (im, brect, color, fontfile, cursize, curang, x, y, buf); #endif if (cp) fprintf (stderr, "%s\n", cp); y += cursize + 4; /* render the same fontsize with antialiasing turned off */ #if defined(OLDER_GD) cp = gdImageStringTTF (im, brect, 0 - color, fontfile, cursize, curang, x, y, buf); #else cp = gdImageStringFT (im, brect, 0 - color, fontfile, cursize, curang, x, y, buf); #endif if (cp) fprintf (stderr, "%s\n", cp); y += cursize + 4; } }
int main(int argc, char *argv[], char *env[]) { int text = 0; int background = 0; int height = 50; int width = 0; int x = 0, y = 0; int size = 30; int string_rectangle[8]; double angle = 0.0; char value[255] = ""; char font[256] = "/usr/share/fonts/TTF/DejaVuSansMono.ttf"; char *err = NULL; gdImagePtr im_out = NULL; decode_value("TEXT=", value, 255); // call gdImageStringFT with NULL image to obtain size err = gdImageStringFT(NULL, &string_rectangle[0], 0, font, size, angle, 0, 0, value); x = string_rectangle[2] - string_rectangle[6] + 6; y = string_rectangle[3] - string_rectangle[7] + 6; /* width = strlen(value) * 10 + 5; */ /* im_out = gdImageCreate(width, height); */ im_out = gdImageCreate(x, y); background = gdImageColorAllocate(im_out, 255, 0, 255); text = gdImageColorAllocate(im_out, 0, 0, 255); /* get starting position */ x = 3 - string_rectangle[6]; y = 3 - string_rectangle[7]; gdImageFilledRectangle(im_out, 0, 0, x - 1, y - 1, background); /* gdImageString(im_out, gdFontGetSmall(), 10, 10, value, text); */ err = gdImageStringFT(im_out, &string_rectangle[0], text, font, size, angle, x, y, value); printf("Content-type:image/gif\n\n"); gdImageGif(im_out, stdout); gdImageDestroy(im_out); return 0; }
int GetStringHeight(char *szString, char* font, int size) { int brect[8]; char* err; gdFontPtr pFont; if(font && strlen(font) != 0 && size > 0) { err = gdImageStringFT(NULL, brect, 0, font,size,0.,0,0,szString); if (err) { /* if error occur, use native font */ return GetStringHeight(szString, "", size); } return brect[3]-brect[5]; } else { /* negative size */ switch(size) { case UF_TINY: pFont = gdFontGetTiny(); break; case UF_SMALL: pFont = gdFontGetSmall(); break; case UF_MEDIUM: pFont = gdFontGetMediumBold(); break; case UF_LARGE: pFont = gdFontGetLarge(); break; case UF_GIANT: pFont = gdFontGetGiant(); break; default: pFont = gdFontGetMediumBold(); break; } /* positive size */ if(size > 0) { if(size > 9) { pFont = gdFontGetGiant(); } else { pFont = gdFontGetSmall(); } } return pFont->h; } }
static bool picNote(const char *path, char *text) { FILE *fp; int c; int x, y, w, h; int rect[8] = { 0 }; gdImagePtr im; if ((fp = fopen(path, "rb")) == NULL) { return false; } im = gdImageCreateFromJpeg(fp); fclose(fp); if (im == NULL) { return false; } c = gdImageColorAllocate(im, 255, 255, 255); w = gdImageSX(im); h = gdImageSY(im); memset(rect, 0x00, sizeof(rect)); gdImageStringFT(NULL, rect, c, "SimHei", 13, 0.0, 0, 0, text); x = w - rect[4] - 5; y = h - 10; gdImageStringFT(im, NULL, c, "SimHei", 13, 0.0, x, y, text); if ((fp = fopen(path, "wb")) == NULL) { gdImageDestroy(im); return false; } gdImageJpeg(im, fp, -1); fclose(fp); gdImageDestroy(im); return true; }
int render_text (gdImagePtr output, int col, int x, int y, char * fmt, ...) { static char buffer[2048]; va_list va_args; memset (buffer, '\0', 2048); va_start (va_args, fmt); vsnprintf (buffer, sizeof (buffer), fmt, va_args); va_end (va_args); log_write (buffer); gdImageStringFT (output, NULL, col, FONTPATH, 10, 0, x, y, buffer); return 0; }
void fswc_DrawText(gdImagePtr im, char *font, double size, int x, int y, char align, uint32_t colour, char shadow, char *text) { char *err; int brect[8]; if(!text) return; if(shadow) { uint32_t scolour = colour & 0xFF000000; fswc_DrawText(im, font, size, x + 1, y + 1, align, scolour, 0, text); } /* Correct alpha value for GD. */ colour = (((colour & 0xFF000000) / 2) & 0xFF000000) + (colour & 0xFFFFFF); /* Pre-render the text. We use the results during alignment. */ err = gdImageStringFT(NULL, &brect[0], colour, font, size, 0.0, 0, 0, text); if(err) { WARN("%s", err); return; } /* Adjust the coordinates according to the alignment. */ switch(align) { case ALIGN_CENTER: x -= brect[4] / 2; break; case ALIGN_RIGHT: x -= brect[4]; break; } /* Render the text onto the image. */ gdImageStringFT(im, NULL, colour, font, size, 0.0, x, y, text); }
void doFTString(FILE *stream) { float x, y, p, d; int a, c; int brect[8]; gdPoint points[4], point; x = getFloat(stream); y = getFloat(stream); c = getColor(getNumber(stream)); a = getNumber(stream); // anchor p = getFloat(stream); // point size d = getFloat(stream); // angle getLine(stream); // Convert angle to radians d = (d * (22.0 / 7.0)) / 180.0; if (currentFontIdx == 0) { point.x = viewx(x); point.y = viewy(y); gdImageStringFT(NULL, brect, c, currentFontFT, p, d, point.x, point.y, buffer); point = stringAnchorFT(point, brect, a); // fprintf(stderr, "Printing %d chars at %d, %d\n", strlen(buffer), point.x, point.y); gdImageStringFT(image, brect, c, currentFontFT, p, d, point.x, point.y, buffer); /* points[0].x = brect[0]; points[0].y = brect[1]; points[1].x = brect[2]; points[1].y = brect[3]; points[2].x = brect[4]; points[2].y = brect[5]; points[3].x = brect[6]; points[3].y = brect[7]; gdImagePolygon(image, points, 4, c); */ } }
void Ebalda::Draw(const char *outfile) { gdImagePtr im; FILE *pngout; int black, white; char buf[512]; snprintf(buf, 512, "%s Age: %d Size %d x %d", name, step, endcol - startcol + 1, endrow - startrow + 1); im = gdImageCreateTrueColor((width + 1) * DOT_SIZE, (height + 1) * DOT_SIZE + 20); white = gdImageColorAllocate(im, 255, 255, 255); // first allocated color is background black = gdImageColorAllocate(im, 0, 0, 0); int brect[8]; gdImageStringFT(im, brect, white, (char *)FONT_FILE, 11.0, 0.0, /*x*/5, /*y*/(height + 1) * DOT_SIZE + 10, buf); for(int row = startrow; row <= endrow; row++) { for(int col = startcol; col <= endcol; col++) { int color; int c = ebalda_himself[ (width + 2) * row + col ] - 'A'; if (row == headrow && col == headcol) color = gdImageColorAllocate(im, 255, 0, 0); else color = gdImageColorAllocate(im, 250 - c * 25, 250 - c * 25, 250 - c * 25); gdImageFilledRectangle(im, (col - startcol) * DOT_SIZE, (row - startrow) * DOT_SIZE, (col - startcol) * DOT_SIZE + DOT_SIZE - 1, (row - startrow) * DOT_SIZE + DOT_SIZE - 1, color); } } pngout = fopen(outfile, "wb"); gdImagePng(im, pngout); fclose(pngout); if (step % SCREENSHOT_EACH == 0) { snprintf(buf, 512, "%s/%08d.png", SCREENSHOT_DIR, step); pngout = fopen(buf, "wb"); gdImagePng(im, pngout); fclose(pngout); } gdImageDestroy(im); }
int main (int argc, char *argv[]) { #ifdef HAVE_LIBFREETYPE FILE *out; int transparent, green, black; gdImagePtr im; im = gdImageCreateTrueColor(100,100); black = gdImageColorResolveAlpha(im, 0, 0, 0, gdAlphaOpaque); green = gdImageColorResolveAlpha(im, 0, gdGreenMax, 0, gdAlphaOpaque); transparent = gdImageColorResolveAlpha(im, gdRedMax-1, gdGreenMax, gdBlueMax, gdAlphaTransparent); gdImageColorTransparent(im, transparent); /* Blending must be off to lay a transparent basecolor. Nothing to blend with anyway. */ gdImageAlphaBlending(im, FALSE); gdImageFill (im, im->sx/2, im->sy/2, transparent); /* Blend everything else together, especially fonts over non-transparent backgrounds */ gdImageAlphaBlending(im, TRUE); gdImageFilledRectangle (im, 30, 30, 70, 70, green); gdImageStringFT (im, NULL, black, "Times", 18, 0, 50, 50, "Hello"); gdImageSaveAlpha (im, TRUE); #ifdef HAVE_LIBPNG out = fopen ("testtr.png", "wb"); gdImagePng (im, out); fclose (out); #else fprintf(stderr, "Compiled without libpng support\n"); #endif /* HAVE_LIBPNG */ gdImageDestroy (im); return 0; #else fprintf(stderr, "Compiled without freetype support\n"); return 0; #endif /* HAVE_LIBFREETYPE */ }
/* ------------------------------------------------------------------------ */ struct fnt_sz_t GDCfnt_sz( char *s, enum GDC_font_size gdfontsz, char *ftfont, double ftfptsz, double rad, /* w,h still relative to horiz. */ char **sts ) { struct fnt_sz_t rtnval; int len; char *err = NULL; #ifdef HAVE_LIBFREETYPE if( ftfont && ftfptsz ) { int brect[8]; /* obtain brect so that we can size the image */ if( (err = gdImageStringFT( (gdImagePtr)NULL, &brect[0], 0, ftfont, ftfptsz, 0.0, /* rad, */ /* always match angled size??? */ 0, 0, s)) == NULL ) { rtnval.h = brect[1] - brect[7]; rtnval.w = brect[2] - brect[0]; if( sts ) *sts = err; return rtnval; } } #endif rtnval.h = cnt_nl(s,&len) * GDC_fontc[gdfontsz].h; rtnval.w = len * GDC_fontc[gdfontsz].w; if( sts ) *sts = err; return rtnval; }
void doString(FILE *stream) { float x, y; int a, c; gdPoint size, point; x = getFloat(stream); y = getFloat(stream); c = getColor(getNumber(stream)); a = getNumber(stream); getLine(stream); if (currentFontIdx == 0) { gdImageStringFT(image, NULL, c, currentFontFT, 10.0, 0.0, viewx(x), viewy(y), buffer); } else { size = stringSize(buffer, currentFontIdx); point.x = viewx(x); point.y = viewy(y); point = stringAnchor(point, size, a, 0); gdImageString(image, currentFont, point.x, point.y, buffer, c); } }
void dowheel (gdImagePtr im, int color, char *fontfile, int fontsize, double angle, int x, int y, int offset, char *string) { int brect[8]; FILE *err; double curangrads, curang, x0, y0; char *cp; err = fopen ("err.out", "a"); doerr (err, "------------- New fontwheel --------------"); doerr (err, fontfile); doerr (err, string); doerr (err, "------------------------------------------"); for (curang = 0.0; curang < 360.0; curang += angle) { curangrads = DEGTORAD(curang); x0 = x + cos (curangrads) * offset; y0 = y - sin (curangrads) * offset; /* The case of newlines is taken care of in the gdImageStringTTF call */ #if defined(OLDER_GD) cp = gdImageStringTTF (im, brect, color, fontfile, fontsize, curangrads, x0, y0, string); #else cp = gdImageStringFT (im, brect, color, fontfile, fontsize, curangrads, x0, y0, string); #endif if (cp) doerr (err, cp); gdImagePolygon (im, (gdPointPtr)brect, 4, color); } fclose (err); }
static void gd_textline(point p, textline_t *line) { char *fontlist, *err; pointf mp,ep; int brect[8]; char *str = line->str; double fontsz = cstk[SP].fontsz; if (cstk[SP].pen == P_NONE) return; fontlist = gd_alternate_fontlist(cstk[SP].fontfam); switch(line->just) { case 'l': mp.x = p.x; break; case 'r': mp.x = p.x - line->width; break; default: case 'n': mp.x = p.x - line->width / 2; break; } ep.y = mp.y = p.y; ep.x = mp.x + line->width; mp = gdpt(mp); if (fontsz <= FONTSIZE_MUCH_TOO_SMALL) { /* ignore entirely */ } else if (fontsz <= FONTSIZE_TOO_SMALL) { /* draw line in place of text */ ep = gdpt(ep); gdImageLine(im, ROUND(mp.x), ROUND(mp.y), ROUND(ep.x), ROUND(ep.y), cstk[SP].pencolor); } else { err = gdImageStringFT(im, brect, cstk[SP].pencolor, fontlist, fontsz, (Rot? 90.0 : 0.0) * PI / 180.0, ROUND(mp.x), ROUND(mp.y), str); if (err) { /* revert to builtin fonts */ gd_missingfont (err, cstk[SP].fontfam); if (fontsz <= 8.5) { gdImageString(im, gdFontTiny, ROUND(mp.x), ROUND(mp.y-9.), (unsigned char *)str, cstk[SP].pencolor); } else if (fontsz <= 9.5) { gdImageString(im, gdFontSmall, ROUND(mp.x), ROUND(mp.y-12.), (unsigned char *)str, cstk[SP].pencolor); } else if (fontsz <= 10.5) { gdImageString(im, gdFontMediumBold, ROUND(mp.x), ROUND(mp.y-13.), (unsigned char *)str, cstk[SP].pencolor); } else if (fontsz <= 11.5) { gdImageString(im, gdFontLarge, ROUND(mp.x), ROUND(mp.y-14.), (unsigned char *)str, cstk[SP].pencolor); } else { gdImageString(im, gdFontGiant, ROUND(mp.x), ROUND(mp.y-15.), (unsigned char *)str, cstk[SP].pencolor); } } } }
int fswc_output(fswebcam_config_t *config, char *name, gdImage *image) { char filename[FILENAME_MAX]; gdImage *im; FILE *f; if(!name) return(-1); if(!strncmp(name, "-", 2) && config->background) { ERROR("stdout is unavailable in background mode."); return(-1); } fswc_strftime(filename, FILENAME_MAX, name, config->start, config->gmt); /* Create a temporary image buffer. */ im = fswc_gdImageDuplicate(image); if(!im) { ERROR("Out of memory."); return(-1); } /* Draw the underlay. */ fswc_draw_overlay(config, config->underlay, im); /* Draw the banner. */ if(config->banner != NO_BANNER) { char *err; /* Check if drawing text works */ err = gdImageStringFT(NULL, NULL, 0, config->font, config->fontsize, 0.0, 0, 0, ""); if(!err) fswc_draw_banner(config, im); else { /* Can't load the font - display a warning */ WARN("Unable to load font '%s': %s", config->font, err); WARN("Disabling the the banner."); } } /* Draw the overlay. */ fswc_draw_overlay(config, config->overlay, im); /* Write to a file if a filename was given, otherwise stdout. */ if(strncmp(name, "-", 2)) f = fopen(filename, "wb"); else f = stdout; if(!f) { ERROR("Error opening file for output: %s", filename); ERROR("fopen: %s", strerror(errno)); gdImageDestroy(im); return(-1); } /* Write the compressed image. */ switch(config->format) { case FORMAT_JPEG: MSG("Writing JPEG image to '%s'.", filename); gdImageJpeg(im, f, config->compression); break; case FORMAT_PNG: MSG("Writing PNG image to '%s'.", filename); gdImagePngEx(im, f, config->compression); break; } if(f != stdout) fclose(f); gdImageDestroy(im); return(0); }
/* ------------------------------------------------------------------ *\ * gd out a string with '\n's * handle FTs (TTFs) and gd fonts * gdImageString() draws from the upper left; * gdImageStringFT() draws from lower left (one font height, even with '\n's)! >:-| \* ------------------------------------------------------------------ */ int GDCImageStringNL( gdImagePtr im, struct GDC_FONT_T *f, char *ftfont, double ftptsz, double rad, int x, int y, char *str, int clr, GDC_justify_t justify, char **sts ) { int retval = 0; char *err = NULL; #ifdef HAVE_LIBFREETYPE /* TODO: honor justifies */ if( ftfont && ftptsz ) { /* need one line height */ /* remember last one (will likely be the same) */ /* is this needed? */ /* gdImageStringFT() utilizes some caching */ /* saves a couple floating point trig calls */ static int f1hgt = 0; static double xs, ys; static double lftptsz = 0.0; static char *lftfont = (char*)-1; if( !f1hgt || ( lftfont != ftfont || lftptsz != ftptsz ) ) { f1hgt = GDCfnt_sz( "Aj", 0, ftfont, ftptsz, rad, NULL ).h; xs = (double)f1hgt * sin(rad); ys = (double)(f1hgt-1) * cos(rad); } x += (int)xs; y += (int)ys; if( (err = gdImageStringFT( im, (int*)NULL, clr, ftfont, ftptsz, rad, x, y, str)) == NULL ) { if( sts ) *sts = err; return 0; } else { /* TTF failed */ retval = 1; /* fall through - default to gdFonts */ /* reinstate upper left reference */ x -= (int)xs; y -= (int)ys; } } #endif { int i; int len; int max_len; short strs_num = cnt_nl( str, &max_len ); CREATE_ARRAY1( sub_str, unsigned char, max_len+1 ); /* char sub_str[max_len+1]; */ len = -1; strs_num = -1; i = -1; do { ++i; ++len; sub_str[len] = *(str+i); if( *(str+i) == '\n' || *(str+i) == '\0' ) { int xpos; sub_str[len] = '\0'; ++strs_num; switch( justify ) { case GDC_JUSTIFY_LEFT: xpos = 0; break; case GDC_JUSTIFY_RIGHT: xpos = f->w*(max_len-len); break; case GDC_JUSTIFY_CENTER: default: xpos = f->w*(max_len-len)/2; } if( rad == 0.0 ) gdImageString( im, f->f, x + xpos, y + (f->h-1)*strs_num, sub_str, clr ); else /* if( rad == M_PI/2.0 ) */ gdImageStringUp( im, f->f, x + (f->h-1)*strs_num, y - xpos, sub_str, clr ); len = -1; } } while( *(str+i) ); } if( sts ) *sts = err; return retval; }
void CarApp::draw_buttons(int selection) { logger->log(DEBUG, "CarApp::draw_buttons", "start"); int i, top, col, blue; gdPoint points[3]; int brect[8]; int white = gdTrueColor(255,255,255); char *font = conf->get_value("font"); col = gdTrueColor(64,64,198); blue = gdTrueColor(64,64,198); for(i=0; i<6; i++) { top = i * button_height; if (i == selection) col = gdTrueColor(100, 110, 216); else col = gdTrueColor(57, 76, 116); /* special colors for the mute button) */ if (sound->is_mute() && (i == MUTE) && (i != selection)) col = gdTrueColor(200, 94, 94); else if (sound->is_mute() && (i == MUTE) && (i == selection)) col = gdTrueColor(255, 120, 120); gdImageFilledRectangle(fb->img, 1, top+1, button_width - 2, top + button_height - 2, col); if (i == MUTE) { gdImageStringFT(fb->img, &brect[0], white, font, 14, 0.0, 20, 45, "Mute"); } else if (i == UP) { col = gdTrueColor(180, 60, 60); points[0].x = MARGIN + (button_width / 2); points[0].y = top + 20; points[1].x = MARGIN + 20; points[1].y = top + button_height - 20; points[2].x = MARGIN + button_width - 20; points[2].y = top + button_height - 20; gdImageFilledPolygon(fb->img, points, 3, col); gdImagePolygon(fb->img, points, 3, 0); } else if (i ==DOWN) { col = gdTrueColor(180, 60, 60); points[0].x = MARGIN + 20; points[0].y = top + 20; points[1].x = MARGIN + button_width - 20; points[1].y = top + 20; points[2].x = MARGIN + (button_width / 2); points[2].y = top + button_height - 20; gdImageFilledPolygon(fb->img, points, 3, col); gdImagePolygon(fb->img, points, 3, 0); } else if (i == RIGHT) { col = gdTrueColor(100, 160, 100); points[0].x = MARGIN + 20; points[0].y = top + 20; points[1].x = MARGIN + button_width - 20; points[1].y = top + (button_height) / 2; points[2].x = MARGIN + 20; points[2].y = top + button_height - 20; gdImageFilledPolygon(fb->img, points, 3, col); gdImagePolygon(fb->img, points, 3, 0); } else if (i == LEFT) { col = gdTrueColor(100, 160, 100); points[0].x = MARGIN + button_width - 20; points[0].y = top + 20; points[1].x = MARGIN + 20; points[1].y = top + (button_height) / 2; points[2].x = MARGIN + button_width - 20; points[2].y = top + button_height - 20; gdImageFilledPolygon(fb->img, points, 3, col); gdImagePolygon(fb->img, points, 3, 0); } else if (i == MENU) { gdImageStringFT(fb->img, &brect[0], white, font, 14, 0.0, 17, 445, "Menu"); } } fb->update(0, 0, button_width, fb->height-1); logger->log(DEBUG, "CarApp::draw_buttons", "end"); }
int CFunctions::imgTtfBBox ( lua_State* luaVM ) { // bool/int imageTtfBBox( int size, int angle, string fontFile, string text) if ( luaVM ) { if ( lua_type ( luaVM, 1 ) == LUA_TNUMBER && lua_type ( luaVM, 2 ) == LUA_TNUMBER && lua_type ( luaVM, 3 ) == LUA_TSTRING && lua_type ( luaVM, 4 ) == LUA_TSTRING ) { std::string mResource; if( pModuleManager->GetResourceName(luaVM, mResource) ) { const char* fName = lua_tostring(luaVM, 3); std::string fNewPath, fMetaPath; if( ParseResourcePathInput( std::string(fName), mResource, fNewPath, fMetaPath ) ) { double size = lua_tonumber(luaVM, 1); double angle = lua_tonumber(luaVM, 2); const char* strTxt = lua_tostring(luaVM, 4); int rect[8] = {0, 0, 0, 0, 0, 0, 0, 0}; char * err = gdImageStringFT( NULL, &rect[0], 0, (char *)fNewPath.c_str(), size, angle, 0, 0, (char *)strTxt ); if (err) { pModuleManager->DebugPrintf( luaVM, "Error with gdImageStringFT(%s) in: imageTtfBBox", err ); return 1; } lua_createtable( luaVM, 0, 8 ); for( int i = 0; i < 8; i++ ) { lua_pushnumber( luaVM, i+1 ); lua_pushnumber( luaVM, rect[i]); lua_settable( luaVM, -3 ); } return 1; } else { pModuleManager->DebugPrintf( luaVM, "Bad filepath in: imageTtfBBox" ); lua_pushboolean( luaVM, false ); return 1; } } else { lua_pushboolean( luaVM, false ); return 1; } } else { pModuleManager->DebugPrintf( luaVM, "Incorrect parameters in: imageTtfBBox" ); lua_pushboolean( luaVM, false ); } return 1; } return 0; }
static void water_mark(void *conf) { ngx_image_conf_t *info = conf; int water_w=0;//水印宽度 int water_h=0;//水印高度 int posX = 0;//X位置 int posY = 0;//Y位置 int water_color = 0;//文字水印GD颜色值 char *water_text;//图片文字 char *water_font;//文字字体 char *water_color_text;//图片颜色值 water_text = NULL; water_font = NULL; water_color_text = NULL; if(info->water_status)//如果水印功能打开了 { if(info->water_type == 0)//如果为图片水印 { if(file_exists((char *)info->water_image.data) == 0)//判断水印图片是否存在 { water_image_from(conf);//获取水印图片信息 if(info->water_im == NULL)//判断对象是否为空 { return;//水印文件异常 }else{ water_w = info->water_im->sx; water_h = info->water_im->sy; } } else { return;//水印图片不存在 } } else//文字水印 { water_text = (char *) info->water_text.data; water_color_text = (char *) info->water_color.data; water_font = (char *)info->water_font.data; if(file_exists((char *)water_font) == 0)//如果水印字体存在 { int R,G,B; char R_str[3],G_str[3],B_str[3]; int brect[8]; gdImagePtr font_im; font_im = gdImageCreateTrueColor(info->dst_im->sx,info->dst_im->sy); sprintf(R_str,"%.*s",2,water_color_text+1); sprintf(G_str,"%.*s",2,water_color_text+3); sprintf(B_str,"%.*s",2,water_color_text+5); sscanf(R_str,"%x",&R); sscanf(G_str,"%x",&G); sscanf(B_str,"%x",&B); water_color = gdImageColorAllocate(info->dst_im,R,G,B); gdImageStringFT(font_im, &brect[0], water_color, water_font, info->water_font_size, 0.0, 0, 0,water_text/*, &strex*/); //water_w = abs(brect[2] - brect[6] + 10); water_w = abs(brect[2] - brect[6] + 10); water_h = abs(brect[3] - brect[7]); gdImageDestroy(font_im); } } if( (info->width < info->water_width_min) || info->height < info->water_height_min) { return;//如果图片宽度/高度比配置文件里规定的宽度/高度宽度小 } if ((info->width < water_w) || (info->height < water_h)) { return;//如果图片宽度/高度比水印宽度/高度宽度小 } if(info->water_pos < 1 ||info->water_pos > 9) { srand((unsigned)time(NULL)); //info->water_pos = rand() % 9 + 1; info->water_pos = 1+(int)(9.0*rand()/(RAND_MAX+1.0)); //info->water_pos = rand() % 9; } switch(info->water_pos) { case 1: posX = 10; posY = 15; break; case 2: posX = (info->width - water_w) / 2; posY = 15; break; case 3: posX = info->width - water_w; posY = 15; break; case 4: posX = 0; posY = (info->height - water_h) / 2; break; case 5: posX = (info->width - water_w) / 2; posY = (info->height - water_h) / 2; break; case 6: posX = info->width - water_w; posY = (info->height - water_h) / 2; break; case 7: posX = 0; posY = (info->height - water_h); break; case 8: posX = (info->width - water_w) /2; posY = info->width - water_h; break; case 9: posX = info->width - water_w; posY = info->height - water_h; break; default: posX = info->width - water_w; posY = info->height - water_h; break; } if(info->water_type == 0) { gdImagePtr tmp_im; tmp_im = NULL; tmp_im = gdImageCreateTrueColor(water_w, water_h); gdImageCopy(tmp_im, info->dst_im, 0, 0, posX, posY, water_w, water_h); gdImageCopy(tmp_im, info->water_im, 0, 0, 0, 0, water_w, water_h); gdImageCopyMerge(info->dst_im, tmp_im,posX, posY, 0, 0, water_w,water_h,info->water_transparent); gdImageDestroy(tmp_im); gdImageDestroy(info->water_im); } else { gdImageAlphaBlending(info->dst_im,-1); gdImageSaveAlpha(info->dst_im,0); gdImageStringFT(info->dst_im,0,water_color,water_font,info->water_font_size, 0.0, posX, posY,water_text); } } }
int CFunctions::imgTtfText ( lua_State* luaVM ) { // bool/int imageTtfText( userdata im, int size, int angle, int x, int y, int color, string fontFile, string text) if ( luaVM ) { if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA && lua_type ( luaVM, 2 ) == LUA_TNUMBER && lua_type ( luaVM, 3 ) == LUA_TNUMBER && lua_type ( luaVM, 4 ) == LUA_TNUMBER && lua_type ( luaVM, 5 ) == LUA_TNUMBER && lua_type ( luaVM, 6 ) == LUA_TNUMBER && lua_type ( luaVM, 7 ) == LUA_TSTRING && lua_type ( luaVM, 8 ) == LUA_TSTRING ) { std::string mResource; if( pModuleManager->GetResourceName(luaVM, mResource) ) { const char* fName = lua_tostring(luaVM, 7); std::string fNewPath, fMetaPath; if( ParseResourcePathInput( std::string(fName), mResource, fNewPath, fMetaPath ) ) { gdImagePtr im = mImgManager->GetImage( lua_touserdata( luaVM, 1 ) ); if( im != NULL ) { double size = lua_tonumber(luaVM, 2); double angle = lua_tonumber(luaVM, 3); int x = (int)lua_tonumber(luaVM, 4); int y = (int)lua_tonumber(luaVM, 5); int color = (int)lua_tonumber(luaVM, 6); const char* strTxt = lua_tostring(luaVM, 8); int rect[8] = {0, 0, 0, 0, 0, 0, 0, 0}; char * err = gdImageStringFT( im, &rect[0], color, (char *)fNewPath.c_str(), size, angle, x, y, (char *)strTxt ); if (err) { pModuleManager->DebugPrintf( luaVM, "Error with gdImageStringFT(%s) %s in: imageTtfText", err ); return 1; } lua_createtable( luaVM, 0, 8 ); for( int i = 0; i < 8; i++ ) { lua_pushnumber( luaVM, i+1 ); lua_pushnumber( luaVM, rect[i]); lua_settable( luaVM, -3 ); } return 1; } else { lua_pushboolean( luaVM, false ); return 1; } } else { pModuleManager->DebugPrintf( luaVM, "Bad filepath in: imageTtfText" ); lua_pushboolean( luaVM, false ); return 1; } } else { lua_pushboolean( luaVM, false ); return 1; } } else { pModuleManager->DebugPrintf( luaVM, "Incorrect parameters in: imageTtfText" ); lua_pushboolean( luaVM, false ); } return 1; } return 0; }
int main(int argc, char *argv[]) { #ifndef HAVE_LIBFREETYPE (void)argc; (void)argv; /* 2.0.12 */ fprintf(stderr, "annotate is not useful without freetype.\n" "Install freetype, then './configure; make clean; make install'\n" "the gd library again.\n" ); return 1; #else gdImagePtr im; char *iin, *iout; FILE *in, *out; char s[1024]; int bounds[8]; int lines = 1; int color = gdTrueColor(0, 0, 0); char font[1024]; int size = 12; int align = left; int x = 0, y = 0; char *fontError; strcpy(font, "times"); if(argc != 3) { fprintf(stderr, "Usage: annotate imagein.jpg imageout.jpg\n\n"); fprintf(stderr, "Standard input should consist of\n"); fprintf(stderr, "lines in the following formats:\n"); fprintf(stderr, "color r g b (0-255 each) [a (0-127, 0 is opaque)]\n"); fprintf(stderr, "font fontname (max name length 1024)\n"); fprintf(stderr, "size pointsize\n"); fprintf(stderr, "align (left|right|center)\n"); fprintf(stderr, "move x y\n"); fprintf(stderr, "text actual-output-text\n\n"); fprintf(stderr, "If the file 'paris.ttf' exists in /usr/share/fonts/truetype or in a\n"); fprintf(stderr, "location specified in the GDFONTPATH environment variable, 'font paris' is\n"); fprintf(stderr, "sufficient. You may also specify the full, rooted path of a font file.\n"); exit(1); } iin = argv[1]; iout = argv[2]; in = fopen(iin, "rb"); if(!in) { fprintf(stderr, "Couldn't open %s\n", iin); exit(2); } #ifdef HAVE_LIBJPEG im = gdImageCreateFromJpeg(in); #else fprintf(stderr, "No JPEG library support available.\n"); exit(1); #endif fclose(in); if(!im) { fprintf(stderr, "%s did not load properly\n", iin); exit(3); } while(fgets(s, sizeof(s), stdin)) { char *st; char *text; st = strtok(s, " \t\r\n"); if(!st) { /* Be nice about blank lines */ continue; } if(!strcmp(st, "font")) { char *st = strtok(0, " \t\r\n"); if(!st) { goto badLine; } else { const unsigned int font_len = strlen(st); if (font_len >= 1024) { fprintf(stderr, "Font maximum length is 1024, %d given\n", font_len); goto badLine; } strncpy(font, st, font_len); } } else if(!strcmp(st, "align")) { char *st = strtok(0, " \t\r\n"); if(!st) { goto badLine; } if(!strcmp(st, "left")) { align = 0; } else if(!strcmp(st, "center")) { align = 1; } else if(!strcmp(st, "right")) { align = 2; } } else if(!strcmp(st, "size")) { char *st = strtok(0, " \t\r\n"); if(!st) { goto badLine; } size = atoi(st); } else if(!strcmp(st, "color")) { char *st = strtok(0, "\r\n"); int r, g, b, a = 0; if(!st) { goto badLine; } if(sscanf(st, "%d %d %d %d", &r, &g, &b, &a) < 3) { fprintf(stderr, "Bad color at line %d\n", lines); exit(2); } color = gdTrueColorAlpha(r, g, b, a); } else if(!strcmp(st, "move")) { char *st = strtok(0, "\r\n"); if(!st) { goto badLine; } if(sscanf(st, "%d %d", &x, &y) != 2) { fprintf(stderr, "Missing coordinates at line %d\n", lines); exit(3); } } else if(!strcmp(st, "text")) { int rx = x; text = strtok(0, "\r\n"); if(!text) { text = ""; } gdImageStringFT(0, bounds, color, font, size, 0, x, y, text); switch(align) { case left: break; case center: rx -= (bounds[2] - bounds[0]) / 2; break; case right: rx -= (bounds[2] - bounds[0]); break; } fontError = gdImageStringFT(im, 0, color, font, size, 0, rx, y, text); if(fontError) { fprintf(stderr, "font error at line %d: %s\n", lines, fontError); exit(7); } y -= (bounds[7] - bounds[1]); } else { goto badLine; } lines++; continue; badLine: fprintf(stderr, "Bad syntax, line %d\n", lines); exit(4); } out = fopen(iout, "wb"); if(!out) { fprintf(stderr, "Cannot create %s\n", iout); exit(5); } #ifdef HAVE_LIBJPEG gdImageJpeg(im, out, 95); #else fprintf(stderr, "No JPEG library support available.\n"); #endif gdImageDestroy(im); fclose(out); return 0; #endif /* HAVE_LIBFREETYPE */ }
int main() { gdImagePtr im; int black; int white; int brect[8]; int x, y; char *err; char s[5]; /* String to draw. */ double sz = 40.; char fontpath[256]; unsigned int r; char buf[50]; char userid[IDLEN + 1]; struct MD5Context mdc; unsigned int pass[4]; if (initbbsinfo(&bbsinfo) < 0) return -1; bzero(pass, sizeof(pass)); strsncpy(buf, getsenv("QUERY_STRING"), sizeof (buf)); __unhcode(buf); if (strncmp(buf, "userid=", 7)) { userid[0] = 0; } else { strsncpy(userid, buf+7, IDLEN+1); // errlog("%s",userid); } if (userid[0] == 0) { s[0] = 0; } else { MD5Init(&mdc); MD5Update(&mdc, (void *)(&bbsinfo.ucachehashshm->regkey), sizeof(int) * 4); MD5Update(&mdc, userid, strlen(userid)); MD5Final((char *)pass, &mdc); sprintf(s, "%d%d%d%d", pass[0]%10, pass[1] % 10, pass[2] % 10, pass[3] % 10); } getrandomint(&r); sprintf(fontpath, MY_BBS_HOME "/etc/fonts/%d.ttf", r % MAXFONTS); /* User supplied font */ fprintf(stdout, "Content-type: image/png\r\n\r\n"); /* obtain brect so that we can size the image */ err = gdImageStringTTF(NULL, &brect[0], 0, fontpath, sz, 0., 0, 0, s); if (err) { return 1; } /* create an image big enough for the string plus a little whitespace */ x = brect[2] - brect[6] + 6; y = brect[3] - brect[7] + 6; im = gdImageCreate(x, y); /* Background color (first allocated) */ white = gdImageColorResolve(im, 255, 255, 255); black = gdImageColorResolve(im, 0, 0, 0); /* render the string, offset origin to center string*/ /* note that we use top-left coordinate for adjustment * since gd origin is in top-left with y increasing downwards. */ x = 3 - brect[6]; y = 3 - brect[7]; err = gdImageStringFT(im, &brect[0], black, fontpath, sz, 0.0, x, y, s); if (err) { return 1; } /* Write img to stdout */ gdImagePng(im, stdout); /* Destroy it */ gdImageDestroy(im); return 0; }