diff options
-rw-r--r-- | DOCS/mplayer.1 | 42 | ||||
-rw-r--r-- | Gui/Makefile | 2 | ||||
-rw-r--r-- | Gui/interface.c | 4 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | TOOLS/subfont-c/osd/gen_osd_h.c | 15 | ||||
-rw-r--r-- | cfg-common.h | 8 | ||||
-rwxr-xr-x | configure | 41 | ||||
-rw-r--r-- | libvo/Makefile | 4 | ||||
-rw-r--r-- | libvo/font_load.c | 6 | ||||
-rw-r--r-- | libvo/font_load.h | 81 | ||||
-rw-r--r-- | libvo/font_load_ft.c | 1094 | ||||
-rw-r--r-- | libvo/osd_font.h | 522 | ||||
-rw-r--r-- | mencoder.c | 4 | ||||
-rw-r--r-- | mplayer.c | 22 |
14 files changed, 1839 insertions, 12 deletions
diff --git a/DOCS/mplayer.1 b/DOCS/mplayer.1 index 2ea20be891..17d2c307ee 100644 --- a/DOCS/mplayer.1 +++ b/DOCS/mplayer.1 @@ -696,6 +696,48 @@ Search for the OSD/SUB fonts in an alternative directory (default: .I EXAMPLE: \-font ~/.mplayer/arial\-14/font.desc + +.I NOTE: + With FreeType, this option determines path to the text font file, +eg. + + -font ~/.mplayer/my_cool_font.ttf +.TP +.I NOTE: +The -subfont-* options are available only with FreeType support +compiled in. +.TP +.B \-subfont-encoding +Sets the font encoding. + +FreeType 2.1: When set to "unicode", all the glyphs from the +font file will be rendered and unicode will be used. This is +also the default setting. + +FreeType 2.0: Default is iso-8859-1. Unicode is not available. +.TP +.B \-subfont-text-scale +Sets the subtitle text autoscale coefficient (percentage of the +screen size). +.TP +.B \-subfont-osd-scale +Sets the osd elements autoscale coefficient. +.TP +.B \-subfont-blur +Sets the font blur radius. +.TP +.B \-subfont-outline +Sets the font outline thickness. +.TP +.B \-subfont-autoscale <0-3> +Sets the autoscale mode. Can be + 0 no autoscale, + 1 proportional to movie width, + 2 proportional to movie height, + 3 proportional to movie diagonal. + +Default is 3 (diagonal). Zero means that text-scale and osd-scale are +font heights in points. .TP .B \-noautosub Turns off automatic subtitles. diff --git a/Gui/Makefile b/Gui/Makefile index 6fab07f1c2..01cac949c4 100644 --- a/Gui/Makefile +++ b/Gui/Makefile @@ -4,7 +4,7 @@ LIB = libgui.a include ../config.mak include config.mak -INCDIR = -I. -I./event -I./wm -I./skin $(GTKINC) $(EXTRA_INC) +INCDIR = -I. -I./event -I./wm -I./skin $(GTKINC) $(EXTRA_INC) $(FREETYPE_INC) OPTIMIZE = $(OPTFLAGS) -fomit-frame-pointer \ -fexpensive-optimizations -fschedule-insns2 -Wall diff --git a/Gui/interface.c b/Gui/interface.c index f2225f4e44..97d9d0c7e3 100644 --- a/Gui/interface.c +++ b/Gui/interface.c @@ -201,6 +201,9 @@ extern char ** vo_plugin_args; #if defined( USE_OSD ) || defined( USE_SUB ) void guiLoadFont( void ) { +#ifdef HAVE_FREETYPE + load_font(vo_image_width, vo_image_height); +#else if ( vo_font ) { int i; @@ -235,6 +238,7 @@ void guiLoadFont( void ) vo_font=read_font_desc( font_name,font_factor,0 ); } } +#endif } #endif @@ -32,7 +32,7 @@ MANDIR = ${prefix}/man INSTALL = install SRCS_COMMON = xacodec.c cpudetect.c codec-cfg.c cfgparser.c my_profile.c spudec.c playtree.c playtreeparser.c asxparser.c vobsub.c subreader.c sub_cc.c find_sub.c -SRCS_MENCODER = mencoder.c mp_msg-mencoder.c $(SRCS_COMMON) libao2/afmt.c divx4_vbr.c libvo/aclib.c libvo/osd.c libvo/sub.c libvo/font_load.c me-opt-reg.c +SRCS_MENCODER = mencoder.c mp_msg-mencoder.c $(SRCS_COMMON) libao2/afmt.c divx4_vbr.c libvo/aclib.c libvo/osd.c libvo/sub.c libvo/font_load.c libvo/font_load_ft.c me-opt-reg.c SRCS_MPLAYER = mplayer.c mp_msg.c $(SRCS_COMMON) mixer.c mp-opt-reg.c OBJS_MENCODER = $(SRCS_MENCODER:.c=.o) @@ -46,11 +46,11 @@ AO_LIBS = -Llibao2 -lao2 A_LIBS = $(ALSA_LIB) $(ARTS_LIB) $(NAS_LIB) $(MAD_LIB) $(VORBIS_LIB) $(FAAD_LIB) $(SGIAUDIO_LIB) CODEC_LIBS = -Llibmpcodecs -lmpcodecs -Lmp3lib -lMP3 -Lliba52 -la52 -Llibmpeg2 -lmpeg2 $(AV_LIB) $(FAME_LIB) $(XVID_LIB) -COMMON_LIBS = $(CODEC_LIBS) -Llibmpdemux -lmpdemux -Linput -linput $(LIB_LOADER) $(A_LIBS) $(CSS_LIB) $(ARCH_LIB) -Lpostproc -lpostproc $(DECORE_LIB) -Llinux -losdep $(TERMCAP_LIB) $(STREAMING_LIB) $(Z_LIB) $(GTK_LIBS) $(PNG_LIB) $(JPEG_LIB) $(GIF_LIB) $(CDPARANOIA_LIB) -lm +COMMON_LIBS = $(CODEC_LIBS) -Llibmpdemux -lmpdemux -Linput -linput $(LIB_LOADER) $(A_LIBS) $(CSS_LIB) $(ARCH_LIB) -Lpostproc -lpostproc $(DECORE_LIB) -Llinux -losdep $(TERMCAP_LIB) $(STREAMING_LIB) $(Z_LIB) $(GTK_LIBS) $(PNG_LIB) $(JPEG_LIB) $(GIF_LIB) $(CDPARANOIA_LIB) $(FREETYPE_LIB) -lm ifeq ($(VIDIX),yes) MISC_LIBS += -Llibdha -ldha -Lvidix -lvidix endif -CFLAGS = $(OPTFLAGS) -Ilibmpdemux -Iloader $(VO_INC) $(EXTRA_INC) $(CDPARANOIA_INC)# -Wall +CFLAGS = $(OPTFLAGS) -Ilibmpdemux -Iloader $(VO_INC) $(EXTRA_INC) $(CDPARANOIA_INC) $(FREETYPE_INC) # -Wall PARTS = libfame libmpdemux libmpcodecs mp3lib liba52 libmp1e libmpeg2 libavcodec libao2 drivers linux postproc input libmpdvdkit libvo ifeq ($(VIDIX),yes) diff --git a/TOOLS/subfont-c/osd/gen_osd_h.c b/TOOLS/subfont-c/osd/gen_osd_h.c new file mode 100644 index 0000000000..ad59c7ef99 --- /dev/null +++ b/TOOLS/subfont-c/osd/gen_osd_h.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main() +{ + int c; + int cnt; + printf("unsigned char *osd_font_pfb = {"); + for (cnt = 0;;cnt++) { + if (cnt % 16 == 0) printf("\n"); + c = getchar(); + if (c < 0) break; + printf("0x%02x,", c); + } + printf("};\n"); +} diff --git a/cfg-common.h b/cfg-common.h index c0ef24b39f..974411c245 100644 --- a/cfg-common.h +++ b/cfg-common.h @@ -163,6 +163,14 @@ {"font", &font_name, CONF_TYPE_STRING, 0, 0, 0, NULL}, {"ffactor", &font_factor, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 10.0, NULL}, {"subpos", &sub_pos, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, +#ifdef HAVE_FREETYPE + {"subfont-encoding", &subtitle_font_encoding, CONF_TYPE_STRING, 0, 0, 0, NULL}, + {"subfont-text-scale", &text_font_scale_factor, CONF_TYPE_FLOAT, CONF_RANGE, 0, 100, NULL}, + {"subfont-osd-scale", &osd_font_scale_factor, CONF_TYPE_FLOAT, CONF_RANGE, 0, 100, NULL}, + {"subfont-blur", &subtitle_font_radius, CONF_TYPE_FLOAT, CONF_RANGE, 0, 8, NULL}, + {"subfont-outline", &subtitle_font_thickness, CONF_TYPE_FLOAT, CONF_RANGE, 0, 8, NULL}, + {"subfont-autoscale", &subtitle_autoscale, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL}, +#endif #endif #else @@ -168,6 +168,7 @@ Optional features: --disable-cdparanoia Disable cdparanoia support [autodetect] --disable-big-endian Force byte order to little endian [autodetect] --enable-big-endian Force byte order to big endian [autodetect] + --enable-freetype Enable freetype support [disabled] Video: --enable-gl build with OpenGL render support [autodetect] @@ -978,6 +979,7 @@ _sighandler=yes _libdv=auto _cdparanoia=auto _big_endian=auto +_freetype=no for ac_option do case "$ac_option" in @@ -1136,6 +1138,8 @@ for ac_option do --disable-cdparanoia) _cdparanoia=no ;; --enable-big-endian) _big_endian=yes ;; --disable-big-endian) _big_endian=no ;; + --enable-freetype) _freetype=yes ;; + --disable-freetype) _freetype=no ;; --enable-dga) _dga=auto ;; # as we don't know if it's 1 or 2 --enable-dga=*) _dga=`echo $ac_option | cut -d '=' -f 2` ;; @@ -3185,7 +3189,7 @@ if test "$_cdparanoia" = auto ; then int main(void) { return 1; } EOF _cdparanoia=no - cc_check $_inc_cdparnoia $_ld_cdparanoia -lcdda_interface -lcdda_paranoia && _cdparanoia=yes + cc_check $_inc_cdparanoia $_ld_cdparanoia -lcdda_interface -lcdda_paranoia && _cdparanoia=yes fi if test "$_cdparanoia" = yes ; then _def_cdparanoia='#define HAVE_CDDA' @@ -3197,6 +3201,34 @@ else fi echores "$_cdparanoia" +echocheck "freetype 2" +if test "$_freetype" = yes ; then + if ( freetype-config --version ) >/dev/null 2>&1 ; then + cat > $TMPC << EOF +#include <freetype/freetype.h> +#if !(FREETYPE_MAJOR >= 2) +#error "Need FreeType 2.0 or newer" +#endif +int main() +{ + return 0; +} +EOF + _freetype=no + cc_check `freetype-config --cflags` && _freetype=yes + else + _freetype=no + fi +fi +if test "$_freetype" = yes ; then + _def_freetype='#define HAVE_FREETYPE' + _inc_freetype=`freetype-config --cflags` + _ld_freetype=`freetype-config --libs` +else + _def_freetype='#undef HAVE_FREETYPE' +fi +echores "$_freetype" + echocheck "zlib" cat > $TMPC << EOF #include <zlib.h> @@ -4274,8 +4306,10 @@ MENCODER = $_mencoder ENCORE_LIB = $_ld_encore $_ld_mp3lame DIRECTFB_INC = $_inc_directfb DIRECTFB_LIB = $_ld_directfb -CDPARANOIA_INC = $_inc_cdparnoia +CDPARANOIA_INC = $_inc_cdparanoia CDPARANOIA_LIB = $_ld_cdparanoia +FREETYPE_INC = $_inc_freetype +FREETYPE_LIB = $_ld_freetype # --- Some stuff for autoconfigure ---- $_target_arch @@ -4609,6 +4643,9 @@ $_def_jpg $_def_gif $_def_gif_4 +/* enable FreeType support */ +$_def_freetype + /* libmad support */ $_def_mad diff --git a/libvo/Makefile b/libvo/Makefile index fcda823aec..212784943f 100644 --- a/libvo/Makefile +++ b/libvo/Makefile @@ -3,14 +3,14 @@ include config.mak LIBNAME = libvo.a -SRCS=aspect.c aclib.c osd.c font_load.c gtf.c spuenc.c video_out.c vo_null.c vo_pgm.c vo_md5.c vo_mpegpes.c vo_yuv4mpeg.c $(OPTIONAL_SRCS) sub.c +SRCS=aspect.c aclib.c osd.c font_load.c gtf.c spuenc.c video_out.c vo_null.c vo_pgm.c vo_md5.c vo_mpegpes.c vo_yuv4mpeg.c $(OPTIONAL_SRCS) sub.c font_load_ft.c OBJS=$(SRCS:.c=.o) ifeq ($(VIDIX),yes) SRCS += vosub_vidix.c endif -CFLAGS = $(OPTFLAGS) -I. -I.. $(SDL_INC) $(X11_INC) $(EXTRA_INC) $(DXR2_INC) $(DVB_INC) $(DIRECTFB_INC) -DMPG12PLAY #-Wall +CFLAGS = $(OPTFLAGS) -I. -I.. $(SDL_INC) $(X11_INC) $(EXTRA_INC) $(DXR2_INC) $(DVB_INC) $(DIRECTFB_INC) $(FREETYPE_INC) -DMPG12PLAY #-Wall ifeq ($(VIDIX),yes) CFLAGS += -DVIDIX_PATH='"$(prefix)/lib/mplayer/vidix/"' endif diff --git a/libvo/font_load.c b/libvo/font_load.c index 4a35a5eb65..646817e0c1 100644 --- a/libvo/font_load.c +++ b/libvo/font_load.c @@ -1,3 +1,6 @@ +#include "config.h" + +#ifndef HAVE_FREETYPE #include <stdio.h> #include <stdlib.h> @@ -6,7 +9,6 @@ #include <sys/stat.h> #include <unistd.h> -#include "config.h" #include "font_load.h" extern char *get_path ( char * ); @@ -300,3 +302,5 @@ read_font_desc("high_arpi.desc",1); } #endif + +#endif /* HAVE_FREETYPE */ diff --git a/libvo/font_load.h b/libvo/font_load.h index 6e144d5926..594aaaf10b 100644 --- a/libvo/font_load.h +++ b/libvo/font_load.h @@ -1,8 +1,18 @@ +#ifndef __MPLAYER_FONT_LOAD_H +#define __MPLAYER_FONT_LOAD_H + +#ifdef HAVE_FREETYPE +#include <freetype/freetype.h> +#endif typedef struct { unsigned char *bmp; unsigned char *pal; int w,h,c; +#ifdef HAVE_FREETYPE + int charwidth,charheight,pen,baseline,padding; + int current_count, current_alloc; +#endif } raw_file; typedef struct { @@ -18,9 +28,80 @@ typedef struct { short font[65536]; int start[65536]; // short is not enough for unicode fonts short width[65536]; + +#ifdef HAVE_FREETYPE + int face_cnt; + + FT_Face faces[16]; + FT_UInt glyph_index[65536]; + + int max_width, max_height; + + struct + { + int g_r; + int o_r; + int g_w; + int o_w; + int o_size; + unsigned volume; + + unsigned *g; + unsigned *gt; + unsigned *gt2; + unsigned *om; + unsigned char *omt; + unsigned short *tmp; + } tables; +#endif + } font_desc_t; extern font_desc_t* vo_font; +#ifdef HAVE_FREETYPE + +extern char *subtitle_font_encoding; +extern float text_font_scale_factor; +extern float osd_font_scale_factor; +extern float subtitle_font_radius; +extern float subtitle_font_thickness; +extern int subtitle_autoscale; + +extern int vo_image_width; +extern int vo_image_height; + +extern int force_load_font; + +int init_freetype(); +int done_freetype(); + +font_desc_t* read_font_desc(char* fname,float factor,int movie_width, int movie_height); +void free_font_desc(font_desc_t *desc); + +void render_one_glyph(font_desc_t *desc, int c); +int kerning(font_desc_t *desc, int prevc, int c); + +void load_font(int width, int height); + +#else + raw_file* load_raw(char *name,int verbose); font_desc_t* read_font_desc(char* fname,float factor,int verbose); + +static void inline render_one_glyph(font_desc_t *desc, int c) +{ +} + +static int inline kerning(font_desc_t *desc, int prevc, int c) +{ + return 0; +} + +static void inline load_font(int width, int height) +{ +} + +#endif + +#endif /* ! __MPLAYER_FONT_LOAD_H */ diff --git a/libvo/font_load_ft.c b/libvo/font_load_ft.c new file mode 100644 index 0000000000..4272809577 --- /dev/null +++ b/libvo/font_load_ft.c @@ -0,0 +1,1094 @@ +/* + * Renders antialiased fonts for mplayer using freetype library. + * Should work with TrueType, Type1 and any other font supported by libfreetype. + * + * Artur Zaprzala <zybi@fanthom.irc.pl> + * + * ported inside mplayer by Jindrich Makovicka + * <makovick@kmlinux.fjfi.cvut.cz> + * + */ + +#include "config.h" + +#ifdef HAVE_FREETYPE + +#include <stdio.h> +#include <stdlib.h> +#include <iconv.h> +#include <math.h> +#include <string.h> +#include <libgen.h> + +#include <freetype/freetype.h> +#include <freetype/ftglyph.h> + +#include "../bswap.h" +#include "font_load.h" +#include "mp_msg.h" +#include "../mplayer.h" +#include "osd_font.h" + +#if (FREETYPE_MAJOR > 2) || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 1) +#define HAVE_FREETYPE21 +#endif + +char *get_path(char *filename); + +char *subtitle_font_encoding = NULL; +float text_font_scale_factor = 5.0; +float osd_font_scale_factor = 6.0; +float subtitle_font_radius = 2.0; +float subtitle_font_thickness = 2.0; +// 0 = no autoscale +// 1 = video height +// 2 = video width +// 3 = diagonal +int subtitle_autoscale = 3; + +int vo_image_width = 0; +int vo_image_height = 0; +int force_load_font; + +//// constants +static int const colors = 256; +static int const maxcolor = 255; +static unsigned const base = 256; +static unsigned const first_char = 33; +#define MAX_CHARSET_SIZE 60000 + +static FT_Library library; + +#define OSD_CHARSET_SIZE 15 + +static FT_ULong osd_charset[OSD_CHARSET_SIZE] = +{ + 0xe001, 0xe002, 0xe003, 0xe004, 0xe005, 0xe006, 0xe007, 0xe008, + 0xe009, 0xe00a, 0xe00b, 0xe010, 0xe011, 0xe012, 0xe013 +}; + +static FT_ULong osd_charcodes[OSD_CHARSET_SIZE] = +{ + 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, + 0x09,0x0a,0x0b,0x10,0x11,0x12,0x13 +}; + +#define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer + // coordinates are in 26.6 pixels (i.e. 1/64th of pixels) +#define f266CeilToInt(x) (((x)+63)>>6) // ceiling +#define f266FloorToInt(x) ((x)>>6) // floor +#define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16 +#define floatTof266(x) ((int)((x)*(1<<6)+0.5)) + +#define ALIGN(x) (((x)+7)&~7) // 8 byte align + +#define WARNING(msg, args...) mp_msg(MSGT_OSD, MSGL_WARN, msg "\n", ## args) + +#define DEBUG 0 + +//static double ttime; + + +static void paste_bitmap(unsigned char *bbuffer, FT_Bitmap *bitmap, int x, int y, int width, int height, int bwidth) { + int drow = x+y*width; + int srow = 0; + int sp, dp, w, h; + if (bitmap->pixel_mode==ft_pixel_mode_mono) + for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch) + for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp) + bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0; + else + for (h = bitmap->rows; h>0 && height > 0; --h, height--, drow+=width, srow+=bitmap->pitch) + for (w = bwidth, sp=dp=0; w>0; --w, ++dp, ++sp) + bbuffer[drow+dp] = bitmap->buffer[srow+sp]; +} + + +static int check_font(font_desc_t *desc, float ppem, int padding, int pic_idx, + int charset_size, FT_ULong *charset, FT_ULong *charcodes, + int unicode) { + FT_Error error; + FT_Face face = desc->faces[pic_idx]; + int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; + int ymin = INT_MAX, ymax = INT_MIN; + int baseline, space_advance = 20; + int width, height; + unsigned char *bbuffer; + int i, uni_charmap = 1; + + + if (face->charmap==NULL || face->charmap->encoding!=ft_encoding_unicode) { + WARNING("Unicode charmap not available for this font. Very bad!"); + uni_charmap = 0; + error = FT_Set_Charmap(face, face->charmaps[0]); + if (error) WARNING("No charmaps! Strange."); + } + + /* set size */ + if (FT_IS_SCALABLE(face)) { + error = FT_Set_Char_Size(face, 0, floatTof266(ppem), 0, 0); + if (error) WARNING("FT_Set_Char_Size failed."); + } else { + int j = 0; + int jppem = face->available_sizes[0].height; + /* find closest size */ + for (i = 0; i<face->num_fixed_sizes; ++i) { + if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) { + j = i; + jppem = face->available_sizes[i].height; + } + } + WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height); + error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height); + if (error) WARNING("FT_Set_Pixel_Sizes failed."); + } + + if (FT_IS_FIXED_WIDTH(face)) + WARNING("Selected font is fixed-width."); + + /* compute space advance */ + error = FT_Load_Char(face, ' ', load_flags); + if (error) WARNING("spacewidth set to default."); + else space_advance = f266ToInt(face->glyph->advance.x); + + if (!desc->spacewidth) desc->spacewidth = 2*padding + space_advance; + if (!desc->charspace) desc->charspace = -2*padding; + if (!desc->height) desc->height = f266ToInt(face->size->metrics.height); + + + for (i= 0; i<charset_size; ++i) { + FT_ULong character, code; + FT_UInt glyph_index; + + character = charset[i]; + code = charcodes[i]; + desc->font[unicode?character:code] = pic_idx; + // get glyph index + if (character==0) + glyph_index = 0; + else { + glyph_index = FT_Get_Char_Index(face, uni_charmap ? character:code); + if (glyph_index==0) { + WARNING("Glyph for char 0x%02x|U+%04X|%c not found.", code, character, + code<' '||code>255 ? '.':code); + desc->font[unicode?character:code] = -1; + continue; + } + } + desc->glyph_index[unicode?character:code] = glyph_index; + } +// fprintf(stderr, "font height: %lf\n", (double)(face->bbox.yMax-face->bbox.yMin)/(double)face->units_per_EM*ppem); +// fprintf(stderr, "font width: %lf\n", (double)(face->bbox.xMax-face->bbox.xMin)/(double)face->units_per_EM*ppem); + + ymax = (double)(face->bbox.yMax)/(double)face->units_per_EM*ppem+1; + ymin = (double)(face->bbox.yMin)/(double)face->units_per_EM*ppem-1; + + width = ppem*(face->bbox.xMax-face->bbox.xMin)/face->units_per_EM+3+2*padding; + if (desc->max_width < width) desc->max_width = width; + width = ALIGN(width); + desc->pic_b[pic_idx]->charwidth = width; + + if (ymax<=ymin) { + mp_msg(MSGT_OSD, MSGL_ERR, "Something went wrong. Use the source!\n"); + return -1; + } + + height = ymax - ymin + 2*padding; + if (desc->max_height < height) desc->max_height = height; + desc->pic_b[pic_idx]->charheight = height; + +// fprintf(stderr, "font height2: %d\n", height); + desc->pic_b[pic_idx]->baseline = ymax + padding; + desc->pic_b[pic_idx]->padding = padding; + desc->pic_b[pic_idx]->current_alloc = 0; + desc->pic_b[pic_idx]->current_count = 0; + + bbuffer = NULL; + + desc->pic_b[pic_idx]->w = width; + desc->pic_b[pic_idx]->h = height; + desc->pic_b[pic_idx]->c = colors; + desc->pic_b[pic_idx]->bmp = bbuffer; + desc->pic_b[pic_idx]->pen = 0; + return 0; +} + +// general outline +void outline( + unsigned char *s, + unsigned char *t, + int width, + int height, + int stride, + unsigned char *m, + int r, + int mwidth, + int msize) { + + int x, y; + + for (y = 0; y<height; y++) { + for (x = 0; x<width; x++) { + const int src= s[x]; + if(src==0) continue; + { + const int x1=(x<r) ? r-x : 0; + const int y1=(y<r) ? r-y : 0; + const int x2=(x+r>=width ) ? r+width -x : 2*r+1; + const int y2=(y+r>=height) ? r+height-y : 2*r+1; + register unsigned char *dstp= t + (y1+y-r)* stride + x-r; + //register int *mp = m + y1 *mwidth; + register unsigned char *mp= m + msize*src + y1*mwidth; + int my; + + for(my= y1; my<y2; my++){ + register int mx; + for(mx= x1; mx<x2; mx++){ + if(dstp[mx] < mp[mx]) dstp[mx]= mp[mx]; + } + dstp+=stride; + mp+=mwidth; + } + } + } + s+= stride; + } +} + + +// 1 pixel outline +void outline1( + unsigned char *s, + unsigned char *t, + int width, + int height, + int stride) { + + int x, y, mx, my; + int skip = stride-width; + + for (x = 0; x<width; ++x, ++s, ++t) *t = *s; + s += skip; + t += skip; + for (y = 1; y<height-1; ++y) { + *t++ = *s++; + for (x = 1; x<width-1; ++x, ++s, ++t) { + unsigned v = ( + s[-1-stride]+ + s[-1+stride]+ + s[+1-stride]+ + s[+1+stride] + )/2 + ( + s[-1]+ + s[+1]+ + s[-stride]+ + s[+stride]+ + s[0] + ); + *t = v>maxcolor ? maxcolor : v; + } + *t++ = *s++; + s += skip; + t += skip; + } + for (x = 0; x<width; ++x, ++s, ++t) *t = *s; +} + + +// gaussian blur +void blur( + unsigned char *buffer, + unsigned short *tmp2, + int width, + int height, + int stride, + int *m, + int *m2, + int r, + int mwidth, + unsigned volume) { + + int x, y; + + unsigned char *s = buffer; + unsigned short *t = tmp2+1; + for(y=0; y<height; y++){ + memset(t-1, 0, (width+1)*sizeof(short)); + + for(x=0; x<r; x++){ + const int src= s[x]; + if(src){ + register unsigned short *dstp= t + x-r; + int mx; + unsigned *m3= m2 + src*mwidth; + for(mx=r-x; mx<mwidth; mx++){ + dstp[mx]+= m3[mx]; + } + } + } + + for(; x<width-r; x++){ + const int src= s[x]; + if(src){ + register unsigned short *dstp= t + x-r; + int mx; + unsigned *m3= m2 + src*mwidth; + for(mx=0; mx<mwidth; mx++){ + dstp[mx]+= m3[mx]; + } + } + } + + for(; x<width; x++){ + const int src= s[x]; + if(src){ + register unsigned short *dstp= t + x-r; + int mx; + const int x2= r+width -x; + const int off= src*mwidth; + unsigned *m3= m2 + src*mwidth; + for(mx=0; mx<x2; mx++){ + dstp[mx]+= m3[mx]; + } + } + } + + s+= stride; + t+= width + 1; + } + + t = tmp2; + for(x=0; x<width; x++){ + for(y=0; y<r; y++){ + unsigned short *srcp= t + y*(width+1) + 1; + int src= *srcp; + if(src){ + register unsigned short *dstp= srcp - 1 + width+1; + const int src2= (src + 128)>>8; + unsigned *m3= m2 + src2*mwidth; + + int mx; + *srcp= 128; + for(mx=r-1; mx<mwidth; mx++){ + *dstp += m3[mx]; + dstp+= width+1; + } + } + } + for(; y<height-r; y++){ + unsigned short *srcp= t + y*(width+1) + 1; + int src= *srcp; + if(src){ + register unsigned short *dstp= srcp - 1 - r*(width+1); + const int src2= (src + 128)>>8; + unsigned *m3= m2 + src2*mwidth; + + int mx; + *srcp= 128; + for(mx=0; mx<mwidth; mx++){ + *dstp += m3[mx]; + dstp+= width+1; + } + } + } + for(; y<height; y++){ + unsigned short *srcp= t + y*(width+1) + 1; + int src= *srcp; + if(src){ + const int y2=r+height-y; + register unsigned short *dstp= srcp - 1 - r*(width+1); + const int src2= (src + 128)>>8; + unsigned *m3= m2 + src2*mwidth; + + int mx; + *srcp= 128; + for(mx=0; mx<y2; mx++){ + *dstp += m3[mx]; + dstp+= width+1; + } + } + } + t++; + } + + t = tmp2; + s = buffer; + for(y=0; y<height; y++){ + for(x=0; x<width; x++){ + s[x]= t[x]>>8; + } + s+= stride; + t+= width + 1; + } +} + +// Gaussian matrix +static unsigned gmatrix(unsigned char *m, int r, int w, double const A) { + unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A + int mx, my; + + for (my = 0; my<w; ++my) { + for (mx = 0; mx<w; ++mx) { + m[mx+my*w] = (exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * base + .5); + volume+= m[mx+my*w]; + } + } + mp_msg(MSGT_OSD, MSGL_DBG2, "A= %f\n", A); + mp_msg(MSGT_OSD, MSGL_DBG2, "volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A)); + return volume; +} + +static void resample_alpha(unsigned char *abuf, unsigned char *bbuf, int width, int height, int stride, float factor) +{ + int f=factor*256.0f; + int i,j; + for (i = 0; i < height; i++) { + unsigned char *a = abuf+i*stride; + unsigned char *b = bbuf+i*stride; + for(j=0;j<width;j++,a++,b++){ + int x=*a; // alpha + int y=*b; // bitmap + x=255-((x*f)>>8); // scale + if (x+y>255) x=255-y; // to avoid overflows + if (x<1) x=1; else if (x>=252) x=0; + *a=x; + } + } +} + +#define ALLOC_INCR 32 +void render_one_glyph(font_desc_t *desc, int c) +{ + FT_GlyphSlot slot; + FT_ULong character, code; + FT_UInt glyph_index; + FT_BBox bbox; + FT_BitmapGlyph glyph; + int width, height, stride, maxw, off; + unsigned char *abuffer, *bbuffer; + + int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; + int pen_xa; + int font = desc->font[c]; + int error; + +// fprintf(stderr, "render_one_glyph %d\n", c); + + if (desc->width[c] != -1) return; + if (desc->font[c] == -1) return; + + glyph_index = desc->glyph_index[c]; + + // load glyph + error = FT_Load_Glyph(desc->faces[font], glyph_index, load_flags); + if (error) { + WARNING("FT_Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); + desc->font[c] = -1; + return; + } + slot = desc->faces[font]->glyph; + + // render glyph + if (slot->format != ft_glyph_format_bitmap) { + error = FT_Render_Glyph(slot, ft_render_mode_normal); + if (error) { + WARNING("FT_Render_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); + desc->font[c] = -1; + return; + } + } + + // extract glyph image + error = FT_Get_Glyph(slot, (FT_Glyph*)&glyph); + if (error) { + WARNING("FT_Get_Glyph 0x%04x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); + desc->font[c] = -1; + return; + } + +// fprintf(stderr, "glyph generated\n"); + + maxw = desc->pic_b[font]->charwidth; + + if (glyph->bitmap.width > maxw) { + fprintf(stderr, "glyph too wide!\n"); + } + + // allocate new memory, if needed + if (desc->pic_b[font]->current_count >= desc->pic_b[font]->current_alloc) { + int newsize = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*(desc->pic_b[font]->current_alloc+ALLOC_INCR); + int increment = desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight*ALLOC_INCR; + + printf("\nincr=%d w=%d h=%d \n",increment,desc->pic_b[font]->charwidth,desc->pic_b[font]->charheight); + + desc->pic_b[font]->current_alloc += ALLOC_INCR; + + desc->pic_b[font]->bmp = realloc(desc->pic_b[font]->bmp, newsize); + desc->pic_a[font]->bmp = realloc(desc->pic_a[font]->bmp, newsize); + + off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight; + memset(desc->pic_b[font]->bmp+off, 0, increment); + memset(desc->pic_a[font]->bmp+off, 0, increment); + } + + abuffer = desc->pic_a[font]->bmp; + bbuffer = desc->pic_b[font]->bmp; + + off = desc->pic_b[font]->current_count*desc->pic_b[font]->charwidth*desc->pic_b[font]->charheight; + + paste_bitmap(bbuffer+off, + &glyph->bitmap, + desc->pic_b[font]->padding + glyph->left, + desc->pic_b[font]->baseline - glyph->top, + desc->pic_b[font]->charwidth, desc->pic_b[font]->charheight, + glyph->bitmap.width <= maxw ? glyph->bitmap.width : maxw); + +// fprintf(stderr, "glyph pasted\n"); + FT_Done_Glyph((FT_Glyph)glyph); + + /* advance pen */ + pen_xa = f266ToInt(slot->advance.x) + 2*desc->pic_b[font]->padding; + if (pen_xa > maxw) pen_xa = maxw; + + desc->start[c] = off; + width = desc->width[c] = pen_xa; + height = desc->pic_b[font]->charheight; + stride = desc->pic_b[font]->w; + + if (desc->tables.o_r <= 1) { + outline1(bbuffer+off, abuffer+off, width, height, stride); + } else { + outline(bbuffer+off, abuffer+off, width, height, stride, + desc->tables.omt, desc->tables.o_r, desc->tables.o_w, + desc->tables.o_size); + } +// fprintf(stderr, "fg: outline t = %lf\n", GetTimer()-t); + + if (desc->tables.g_r) { + blur(abuffer+off, desc->tables.tmp, width, height, stride, + desc->tables.gt, desc->tables.gt2, desc->tables.g_r, + desc->tables.g_w, desc->tables.volume); +// fprintf(stderr, "fg: blur t = %lf\n", GetTimer()-t); + } + + resample_alpha(abuffer+off, bbuffer+off, width, height, stride, font_factor); + + desc->pic_b[font]->current_count++; +} + + +static int prepare_font(font_desc_t *desc, FT_Face face, float ppem, int pic_idx, + int charset_size, FT_ULong *charset, FT_ULong *charcodes, int unicode, + double thickness, double radius) +{ + int i, err; + int padding = ceil(radius) + ceil(thickness); + + desc->faces[pic_idx] = face; + + desc->pic_a[pic_idx] = (raw_file*)malloc(sizeof(raw_file)); + if (!desc->pic_a[pic_idx]) return -1; + desc->pic_b[pic_idx] = (raw_file*)malloc(sizeof(raw_file)); + if (!desc->pic_b[pic_idx]) return -1; + + desc->pic_a[pic_idx]->bmp = NULL; + desc->pic_a[pic_idx]->pal = NULL; + desc->pic_b[pic_idx]->bmp = NULL; + desc->pic_b[pic_idx]->pal = NULL; + + desc->pic_a[pic_idx]->pal = (unsigned char*)malloc(sizeof(unsigned char)*256*3); + if (!desc->p |