+static void Font_Postprocess_Update(ft2_font_t *fnt, int bpp, int w, int h)
+{
+ int needed, x, y;
+ float gausstable[2*POSTPROCESS_MAXRADIUS+1];
+ qboolean need_gauss = (!pp.buf || pp.blur != fnt->settings->blur || pp.shadowz != fnt->settings->shadowz);
+ qboolean need_circle = (!pp.buf || pp.outline != fnt->settings->outline || pp.shadowx != fnt->settings->shadowx || pp.shadowy != fnt->settings->shadowy);
+ pp.blur = fnt->settings->blur;
+ pp.outline = fnt->settings->outline;
+ pp.shadowx = fnt->settings->shadowx;
+ pp.shadowy = fnt->settings->shadowy;
+ pp.shadowz = fnt->settings->shadowz;
+ pp.outlinepadding_l = bound(0, ceil(pp.outline - pp.shadowx), POSTPROCESS_MAXRADIUS);
+ pp.outlinepadding_r = bound(0, ceil(pp.outline + pp.shadowx), POSTPROCESS_MAXRADIUS);
+ pp.outlinepadding_t = bound(0, ceil(pp.outline - pp.shadowy), POSTPROCESS_MAXRADIUS);
+ pp.outlinepadding_b = bound(0, ceil(pp.outline + pp.shadowy), POSTPROCESS_MAXRADIUS);
+ pp.blurpadding_lt = bound(0, ceil(pp.blur - pp.shadowz), POSTPROCESS_MAXRADIUS);
+ pp.blurpadding_rb = bound(0, ceil(pp.blur + pp.shadowz), POSTPROCESS_MAXRADIUS);
+ pp.padding_l = pp.blurpadding_lt + pp.outlinepadding_l;
+ pp.padding_r = pp.blurpadding_rb + pp.outlinepadding_r;
+ pp.padding_t = pp.blurpadding_lt + pp.outlinepadding_t;
+ pp.padding_b = pp.blurpadding_rb + pp.outlinepadding_b;
+ if(need_gauss)
+ {
+ float sum = 0;
+ for(x = -POSTPROCESS_MAXRADIUS; x <= POSTPROCESS_MAXRADIUS; ++x)
+ gausstable[POSTPROCESS_MAXRADIUS+x] = (pp.blur > 0 ? exp(-(pow(x + pp.shadowz, 2))/(pp.blur*pp.blur * 2)) : (floor(x + pp.shadowz + 0.5) == 0));
+ for(x = -pp.blurpadding_rb; x <= pp.blurpadding_lt; ++x)
+ sum += gausstable[POSTPROCESS_MAXRADIUS+x];
+ for(x = -POSTPROCESS_MAXRADIUS; x <= POSTPROCESS_MAXRADIUS; ++x)
+ pp.gausstable[POSTPROCESS_MAXRADIUS+x] = floor(gausstable[POSTPROCESS_MAXRADIUS+x] / sum * 255 + 0.5);
+ }
+ if(need_circle)
+ {
+ for(y = -POSTPROCESS_MAXRADIUS; y <= POSTPROCESS_MAXRADIUS; ++y)
+ for(x = -POSTPROCESS_MAXRADIUS; x <= POSTPROCESS_MAXRADIUS; ++x)
+ {
+ float d = pp.outline + 1 - sqrt(pow(x + pp.shadowx, 2) + pow(y + pp.shadowy, 2));
+ pp.circlematrix[POSTPROCESS_MAXRADIUS+y][POSTPROCESS_MAXRADIUS+x] = (d >= 1) ? 255 : (d <= 0) ? 0 : floor(d * 255 + 0.5);
+ }
+ }
+ pp.bufwidth = w + pp.padding_l + pp.padding_r;
+ pp.bufheight = h + pp.padding_t + pp.padding_b;
+ pp.bufpitch = pp.bufwidth;
+ needed = pp.bufwidth * pp.bufheight;
+ if(!pp.buf || pp.bufsize < needed * 2)
+ {
+ if(pp.buf)
+ Mem_Free(pp.buf);
+ pp.bufsize = needed * 4;
+ pp.buf = (unsigned char *)Mem_Alloc(font_mempool, pp.bufsize);
+ pp.buf2 = pp.buf + needed;
+ }
+}
+
+static void Font_Postprocess(ft2_font_t *fnt, unsigned char *imagedata, int pitch, int bpp, int w, int h, int *pad_l, int *pad_r, int *pad_t, int *pad_b)
+{
+ int x, y;
+
+ // calculate gauss table
+ Font_Postprocess_Update(fnt, bpp, w, h);
+
+ if(imagedata)
+ {
+ // enlarge buffer
+ // perform operation, not exceeding the passed padding values,
+ // but possibly reducing them
+ *pad_l = min(*pad_l, pp.padding_l);
+ *pad_r = min(*pad_r, pp.padding_r);
+ *pad_t = min(*pad_t, pp.padding_t);
+ *pad_b = min(*pad_b, pp.padding_b);
+
+ // outline the font (RGBA only)
+ if(bpp == 4 && (pp.outline > 0 || pp.blur > 0 || pp.shadowx != 0 || pp.shadowy != 0 || pp.shadowz != 0)) // we can only do this in BGRA
+ {
+ // this is like mplayer subtitle rendering
+ // bbuffer, bitmap buffer: this is our font
+ // abuffer, alpha buffer: this is pp.buf
+ // tmp: this is pp.buf2
+
+ // create outline buffer
+ memset(pp.buf, 0, pp.bufwidth * pp.bufheight);
+ for(y = -*pad_t; y < h + *pad_b; ++y)
+ for(x = -*pad_l; x < w + *pad_r; ++x)
+ {
+ int x1 = max(-x, -pp.outlinepadding_r);
+ int y1 = max(-y, -pp.outlinepadding_b);
+ int x2 = min(pp.outlinepadding_l, w-1-x);
+ int y2 = min(pp.outlinepadding_t, h-1-y);
+ int mx, my;
+ int cur = 0;
+ int highest = 0;
+ for(my = y1; my <= y2; ++my)
+ for(mx = x1; mx <= x2; ++mx)
+ {
+ cur = pp.circlematrix[POSTPROCESS_MAXRADIUS+my][POSTPROCESS_MAXRADIUS+mx] * (int)imagedata[(x+mx) * bpp + pitch * (y+my) + (bpp - 1)];
+ if(cur > highest)
+ highest = cur;
+ }
+ pp.buf[((x + pp.padding_l) + pp.bufpitch * (y + pp.padding_t))] = (highest + 128) / 255;
+ }
+
+ // blur the outline buffer
+ if(pp.blur > 0 || pp.shadowz != 0)
+ {
+ // horizontal blur
+ for(y = 0; y < pp.bufheight; ++y)
+ for(x = 0; x < pp.bufwidth; ++x)
+ {
+ int x1 = max(-x, -pp.blurpadding_rb);
+ int x2 = min(pp.blurpadding_lt, pp.bufwidth-1-x);
+ int mx;
+ int blurred = 0;
+ for(mx = x1; mx <= x2; ++mx)
+ blurred += pp.gausstable[POSTPROCESS_MAXRADIUS+mx] * (int)pp.buf[(x+mx) + pp.bufpitch * y];
+ pp.buf2[x + pp.bufpitch * y] = bound(0, blurred, 65025) / 255;
+ }
+
+ // vertical blur
+ for(y = 0; y < pp.bufheight; ++y)
+ for(x = 0; x < pp.bufwidth; ++x)
+ {
+ int y1 = max(-y, -pp.blurpadding_rb);
+ int y2 = min(pp.blurpadding_lt, pp.bufheight-1-y);
+ int my;
+ int blurred = 0;
+ for(my = y1; my <= y2; ++my)
+ blurred += pp.gausstable[POSTPROCESS_MAXRADIUS+my] * (int)pp.buf2[x + pp.bufpitch * (y+my)];
+ pp.buf[x + pp.bufpitch * y] = bound(0, blurred, 65025) / 255;
+ }
+ }
+
+ // paste the outline below the font
+ for(y = -*pad_t; y < h + *pad_b; ++y)
+ for(x = -*pad_l; x < w + *pad_r; ++x)
+ {
+ unsigned char outlinealpha = pp.buf[(x + pp.padding_l) + pp.bufpitch * (y + pp.padding_t)];
+ if(outlinealpha > 0)
+ {
+ unsigned char oldalpha = imagedata[x * bpp + pitch * y + (bpp - 1)];
+ // a' = 1 - (1 - a1) (1 - a2)
+ unsigned char newalpha = 255 - ((255 - (int)outlinealpha) * (255 - (int)oldalpha)) / 255; // this is >= oldalpha
+ // c' = (a2 c2 - a1 a2 c1 + a1 c1) / a' = (a2 c2 + a1 (1 - a2) c1) / a'
+ unsigned char oldfactor = (255 * (int)oldalpha) / newalpha;
+ //unsigned char outlinefactor = ((255 - oldalpha) * (int)outlinealpha) / newalpha;
+ int i;
+ for(i = 0; i < bpp-1; ++i)
+ {
+ unsigned char c = imagedata[x * bpp + pitch * y + i];
+ c = (c * (int)oldfactor) / 255 /* + outlinecolor[i] * (int)outlinefactor */;
+ imagedata[x * bpp + pitch * y + i] = c;
+ }
+ imagedata[x * bpp + pitch * y + (bpp - 1)] = newalpha;
+ }
+ //imagedata[x * bpp + pitch * y + (bpp - 1)] |= 0x80;
+ }
+ }
+ }
+ else if(pitch)
+ {
+ // perform operation, not exceeding the passed padding values,
+ // but possibly reducing them
+ *pad_l = min(*pad_l, pp.padding_l);
+ *pad_r = min(*pad_r, pp.padding_r);
+ *pad_t = min(*pad_t, pp.padding_t);
+ *pad_b = min(*pad_b, pp.padding_b);
+ }
+ else
+ {
+ // just calculate parameters
+ *pad_l = pp.padding_l;
+ *pad_r = pp.padding_r;
+ *pad_t = pp.padding_t;
+ *pad_b = pp.padding_b;
+ }
+}
+
+static float Font_SearchSize(ft2_font_t *font, FT_Face fontface, float size);