📄 gdft.c
字号:
im = b->im; /* if fg is specified by a negative color idx, then don't antialias */ if (fg < 0) { if ((pixel + pixel) >= NUMCOLORS) a->tweencolor = -fg; else a->tweencolor = bg; } else { npixel = NUMCOLORS - pixel; if (im->trueColor) { /* 2.0.1: use gdImageSetPixel to do the alpha blending work, or to just store the alpha level. All we have to do here is incorporate our knowledge of the percentage of this pixel that is really "lit" by pushing the alpha value up toward transparency in edge regions. */ a->tweencolor = gdTrueColorAlpha (gdTrueColorGetRed (fg), gdTrueColorGetGreen (fg), gdTrueColorGetBlue (fg), gdAlphaMax - (gdTrueColorGetAlpha (fg) * pixel / NUMCOLORS)); } else { a->tweencolor = gdImageColorResolve (im, (pixel * im->red[fg] + npixel * im->red[bg]) / NUMCOLORS, (pixel * im->green[fg] + npixel * im->green[bg]) / NUMCOLORS, (pixel * im->blue[fg] + npixel * im->blue[bg]) / NUMCOLORS); } } return (void *) a;}static voidtweenColorRelease (void *element){ gdFree ((char *) element);}/* draw_bitmap - transfers glyph bitmap to GD image */static char *gdft_draw_bitmap (gdCache_head_t * tc_cache, gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y){ unsigned char *pixel = NULL; int *tpixel = NULL; int opixel; int x, y, row, col, pc, pcr; tweencolor_t *tc_elem; tweencolorkey_t tc_key; /* copy to image, mapping colors */ tc_key.fgcolor = fg; tc_key.im = im; /* Truecolor version; does not require the cache */ if (im->trueColor) { for (row = 0; row < bitmap.rows; row++) { pc = row * bitmap.pitch; pcr = pc; y = pen_y + row; /* clip if out of bounds */ /* 2.0.16: clipping rectangle, not image bounds */ if ((y > im->cy2) || (y < im->cy1)) continue; for (col = 0; col < bitmap.width; col++, pc++) { int level; if (bitmap.pixel_mode == ft_pixel_mode_grays) { /* * Scale to 128 levels of alpha for gd use. * alpha 0 is opacity, so be sure to invert at the end */ level = (bitmap.buffer[pc] * gdAlphaMax / (bitmap.num_grays - 1)); } else if (bitmap.pixel_mode == ft_pixel_mode_mono) { /* 2.0.5: mode_mono fix from Giuliano Pochini */ level = ((bitmap. buffer[(col >> 3) + pcr]) & (1 << (~col & 0x07))) ? gdAlphaTransparent : gdAlphaOpaque; } else { return "Unsupported ft_pixel_mode"; } if (level == 0) /* if background */ continue; if ((fg >= 0) && (im->trueColor)) { /* Consider alpha in the foreground color itself to be an upper bound on how opaque things get, when truecolor is available. Without truecolor this results in far too many color indexes. */ level = level * (gdAlphaMax - gdTrueColorGetAlpha (fg)) / gdAlphaMax; } level = gdAlphaMax - level; /* inverting to get alpha */ x = pen_x + col; /* clip if out of bounds */ /* 2.0.16: clip to clipping rectangle, Matt McNabb */ if ((x > im->cx2) || (x < im->cx1)) continue; /* get pixel location in gd buffer */ tpixel = &im->tpixels[y][x]; if (fg < 0) { if (level < (gdAlphaMax / 2)) { *tpixel = -fg; } } else { if (im->alphaBlendingFlag) { opixel = *tpixel; if (gdTrueColorGetAlpha(opixel) != gdAlphaTransparent) { *tpixel = gdAlphaBlend (opixel, (level << 24) + (fg & 0xFFFFFF)); } else { *tpixel = (level << 24) + (fg & 0xFFFFFF); } } else { *tpixel = (level << 24) + (fg & 0xFFFFFF); } } } } return (char *) NULL; } /* Non-truecolor case, restored to its more or less original form */ for (row = 0; row < bitmap.rows; row++) { int pcr; pc = row * bitmap.pitch; pcr = pc; if (bitmap.pixel_mode == ft_pixel_mode_mono) pc *= 8; /* pc is measured in bits for monochrome images */ y = pen_y + row; /* clip if out of bounds */ if (y > im->cy2 || y < im->cy1) continue; for (col = 0; col < bitmap.width; col++, pc++) { if (bitmap.pixel_mode == ft_pixel_mode_grays) { /* * Round to NUMCOLORS levels of antialiasing for * index color images since only 256 colors are * available. */ tc_key.pixel = ((bitmap.buffer[pc] * NUMCOLORS) + bitmap.num_grays / 2) / (bitmap.num_grays - 1); } else if (bitmap.pixel_mode == ft_pixel_mode_mono) { tc_key.pixel = ((bitmap.buffer[pc / 8] << (pc % 8)) & 128) ? NUMCOLORS : 0; /* 2.0.5: mode_mono fix from Giuliano Pochini */ tc_key.pixel = ((bitmap. buffer[(col >> 3) + pcr]) & (1 << (~col & 0x07))) ? NUMCOLORS : 0; } else { return "Unsupported ft_pixel_mode"; } if (tc_key.pixel == 0) /* if background */ continue; x = pen_x + col; /* clip if out of bounds */ if (x > im->cx2 || x < im->cx1) continue; /* get pixel location in gd buffer */ pixel = &im->pixels[y][x]; if (tc_key.pixel == NUMCOLORS) { /* use fg color directly. gd 2.0.2: watch out for negative indexes (thanks to David Marwood). */ *pixel = (fg < 0) ? -fg : fg; } else { /* find antialised color */ tc_key.bgcolor = *pixel; tc_elem = (tweencolor_t *) gdCacheGet (tc_cache, &tc_key); *pixel = tc_elem->tweencolor; } } } return (char *) NULL;}extern int any2eucjp (char *, char *, unsigned int);/* Persistent font cache until explicitly cleared *//* Fonts can be used across multiple images *//* 2.0.16: thread safety (the font cache is shared) */gdMutexDeclare (gdFontCacheMutex);static gdCache_head_t *fontCache;static FT_Library library;BGD_DECLARE(void) gdFreeFontCache (){ gdFontCacheShutdown ();}BGD_DECLARE(void) gdFontCacheShutdown (){ if (fontCache) { gdMutexShutdown (gdFontCacheMutex); gdCacheDelete (fontCache); FT_Done_FreeType (library); /* 2.0.16: Gustavo Scotti: make sure we don't free this twice */ fontCache = 0; }}/********************************************************************//* gdImageStringFT - render a utf8 string onto a gd image */BGD_DECLARE(char *) gdImageStringFT (gdImage * im, int *brect, int fg, char *fontlist, double ptsize, double angle, int x, int y, char *string){ return gdImageStringFTEx (im, brect, fg, fontlist, ptsize, angle, x, y, string, 0);}BGD_DECLARE(int) gdFontCacheSetup (void){ if (fontCache) { /* Already set up */ return 0; } gdMutexSetup (gdFontCacheMutex); if (FT_Init_FreeType (&library)) { gdMutexShutdown (gdFontCacheMutex); return -1; } fontCache = gdCacheCreate (FONTCACHESIZE, fontTest, fontFetch, fontRelease); return 0;}/* the platform-independent resolution used for size and position calculations *//* the size of the error introduced by rounding is affected by this number */#define METRIC_RES 300BGD_DECLARE(char *) gdImageStringFTEx (gdImage * im, int *brect, int fg, char *fontlist, double ptsize, double angle, int x, int y, char *string, gdFTStringExtraPtr strex){ FT_Matrix matrix; FT_Vector penf, oldpenf, delta, total_min = {0,0}, total_max = {0,0}, glyph_min, glyph_max; FT_Face face; FT_CharMap charmap; FT_Glyph image; FT_GlyphSlot slot; FT_Error err; FT_UInt glyph_index, previous; double sin_a = sin (angle); double cos_a = cos (angle); int len, i, ch; font_t *font; fontkey_t fontkey; char *next; char *tmpstr = 0; int render = (im && (im->trueColor || (fg <= 255 && fg >= -255))); FT_BitmapGlyph bm; /* 2.0.13: Bob Ostermann: don't force autohint, that's just for testing freetype and doesn't look as good */ int render_mode = FT_LOAD_DEFAULT; int encoding, encodingfound; /* Now tuneable thanks to Wez Furlong */ double linespace = LINESPACE; /* 2.0.6: put this declaration with the other declarations! */ /* * make a new tweenColorCache on every call * because caching colormappings between calls * is not safe. If the im-pointer points to a * brand new image, the cache gives out bogus * colorindexes. -- 27.06.2001 <krisku@arrak.fi> */ gdCache_head_t *tc_cache; /* Tuneable horizontal and vertical resolution in dots per inch */ int hdpi, vdpi, horiAdvance, xshow_alloc = 0, xshow_pos = 0; FT_Size platform_specific, platform_independent; if (strex) { if ((strex->flags & gdFTEX_LINESPACE) == gdFTEX_LINESPACE) { linespace = strex->linespacing; } } tc_cache = gdCacheCreate (TWEENCOLORCACHESIZE, tweenColorTest, tweenColorFetch, tweenColorRelease);/***** initialize font library and font cache on first call ******/ if (!fontCache) { if (gdFontCacheSetup () != 0) { gdCacheDelete (tc_cache); return "Failure to initialize font library"; } }/*****/ gdMutexLock (gdFontCacheMutex); /* get the font (via font cache) */ fontkey.fontlist = fontlist; if (strex) fontkey.flags = strex->flags & (gdFTEX_FONTPATHNAME | gdFTEX_FONTCONFIG); else fontkey.flags = 0; fontkey.library = &library; font = (font_t *) gdCacheGet (fontCache, &fontkey); if (!font) { gdCacheDelete (tc_cache); gdMutexUnlock (gdFontCacheMutex); return fontCache->error; } face = font->face; /* shortcut */ slot = face->glyph; /* shortcut */ /* * Added hdpi and vdpi to support images at non-screen resolutions, i.e. 300 dpi TIFF, * or 100h x 50v dpi FAX format. 2.0.23. * 2004/02/27 Mark Shackelford, mark.shackelford@acs-inc.com */ hdpi = GD_RESOLUTION; vdpi = GD_RESOLUTION; encoding = gdFTEX_Unicode; if (strex) { if (strex->flags & gdFTEX_RESOLUTION) { hdpi = strex->hdpi; vdpi = strex->vdpi; } if (strex->flags & gdFTEX_XSHOW) { strex->xshow = NULL; } /* 2.0.12: allow explicit specification of the preferred map; but we still fall back if it is not available. */ if (strex->flags & gdFTEX_CHARMAP) { encoding = strex->charmap; } /* 2.0.29: we can return the font path if desired */ if (strex->flags & gdFTEX_RETURNFONTPATHNAME) strex->fontpath = strdup(font->fontpath); else strex->fontpath = 0; } matrix.xx = (FT_Fixed) (cos_a * (1 << 16)); matrix.yx = (FT_Fixed) (sin_a * (1 << 16)); matrix.xy = -matrix.yx; matrix.yy = matrix.xx; /* set rotation transform */ FT_Set_Transform (face, &matrix, NULL); FT_New_Size (face, &platform_independent); FT_Activate_Size (platform_independent); if (FT_Set_Char_Size (face, 0, (FT_F26Dot6)(ptsize*64), METRIC_RES, METRIC_RES)) { gdCacheDelete (tc_cache); gdMutexUnlock (gdFontCacheMutex); return "Could not set character size"; } if (render) { FT_New_Size (face, &platform_specific); FT_Activate_Size (platform_specific); if (FT_Set_Char_Size (face, 0, (FT_F26Dot6)(ptsize*64), hdpi, vdpi)) { gdCacheDelete (tc_cache); gdMutexUnlock (gdFontCacheMutex); return "Could not set character size"; } } if (fg < 0) render_mode |= FT_LOAD_MONOCHROME; /* find requested charmap */ encodingfound = 0; for (i = 0; i < face->num_charmaps; i++) { charmap = face->charmaps[i];#if ((defined(FREETYPE_MAJOR)) && (((FREETYPE_MAJOR == 2) && (((FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 3)) || (FREETYPE_MINOR > 1))) || (FREETYPE_MAJOR > 2))) if (encoding == gdFTEX_Unicode) { if (charmap->encoding == FT_ENCODING_MS_SYMBOL || charmap->encoding == FT_ENCODING_UNICODE || charmap->encoding == FT_ENCODING_ADOBE_CUSTOM || charmap->encoding == FT_ENCODING_ADOBE_STANDARD) { encodingfound++; break; } } else if (encoding == gdFTEX_Big5) {/* renamed sometime after freetype-2.1.4 */#ifndef FT_ENCODING_BIG5#define FT_ENCODING_BIG5 FT_ENCODING_MS_BIG5#endif if (charmap->encoding == FT_ENCODING_BIG5) { encodingfound++; break; } } else if (encoding == gdFTEX_Shift_JIS) {/* renamed sometime after freetype-2.1.4 */#ifndef FT_ENCODING_SJIS#define FT_ENCODING_SJIS FT_ENCODING_MS_SJIS#endif if (charmap->encoding == FT_ENCODING_SJIS) { encodingfound++; break; } }#else if (encoding == gdFTEX_Unicode) { if ((charmap->platform_id = 3 && charmap->encoding_id == 1) /* Windows Unicode */ || (charmap->platform_id == 3 && charmap->encoding_id == 0) /* Windows Symbol */ || (charmap->platform_id == 2 && charmap->encoding_id == 1) /* ISO Unicode */ || (charmap->platform_id == 0)) /* Apple Unicode */ { encodingfound++; break; } } else if (encoding == gdFTEX_Big5) { if (charmap->platform_id == 3 && charmap->encoding_id == 4) /* Windows Big5 */ { encodingfound++; break; } } else if (encoding == gdFTEX_Shift_JIS) { if (charmap->platform_id == 3 && charmap->encoding_id == 2) /* Windows Sjis */ { encodingfound++; break; } }#endif } if (encodingfound) { FT_Set_Charmap(face, charmap); } else { /* No character set found! */ gdMutexUnlock (gdFontCacheMutex); return "No character set found"; }#ifndef JISX0208 if (encoding == gdFTEX_Shift_JIS) {#endif if ((tmpstr = (char *) gdMalloc (BUFSIZ))) { any2eucjp (tmpstr, string, BUFSIZ); next = tmpstr; } else { next = string; }#ifndef JISX0208 } else { next = string; }#endif#if 0fprintf(stderr,"dpi=%d,%d metric_res=%d ptsize=%g\n",hdpi,vdpi,METRIC_RES,ptsize);#endif oldpenf.x = oldpenf.y = 0; /* for postscript xshow operator */ penf.x = penf.y = 0; /* running position of non-rotated glyphs */ previous = 0; /* index of previous glyph for kerning calculations */ for (i=0; *next; i++) { FT_Activate_Size (platform_independent);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -