From 173cbc1e8f9acdc0908e0836ea9a3b21bf8cac15 Mon Sep 17 00:00:00 2001 From: zybi Date: Tue, 21 Aug 2001 14:00:57 +0000 Subject: Implemented new Gaussian blur algorithm, extended font.desc format, optimized for speed and memory usage. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@1608 b3059339-0415-0410-9bf9-f77b7e298cf2 --- TOOLS/subfont-c/Makefile | 7 +- TOOLS/subfont-c/README | 39 +++++- TOOLS/subfont-c/runme | 10 +- TOOLS/subfont-c/subfont.c | 323 +++++++++++++++++++++++++++------------------- 4 files changed, 234 insertions(+), 145 deletions(-) (limited to 'TOOLS') diff --git a/TOOLS/subfont-c/Makefile b/TOOLS/subfont-c/Makefile index 23d35a07d4..061f5d5c2c 100644 --- a/TOOLS/subfont-c/Makefile +++ b/TOOLS/subfont-c/Makefile @@ -1,16 +1,19 @@ - include ../../config.mak LDLIBS=-lm $(shell freetype-config --libs) CFLAGS=$(OPTFLAGS) $(shell freetype-config --cflags) -#CFLAGS+=-DOLD_FREETYPE2 +#CFLAGS+=-O0 # for RedHat's gcc-2.96-95 +#CFLAGS+=-DOLD_FREETYPE2 # for FreeType 2.0.1 #CFLAGS+=-g #CFLAGS+=-DDEBUG +#CFLAGS+=-DNEW_DESC subfont: subfont.o +subfont.o: subfont.c Makefile + subfont.S: subfont.c $(CC) $(CFLAGS) -S $^ -o $@ diff --git a/TOOLS/subfont-c/README b/TOOLS/subfont-c/README index 5989787d1a..dc1b78bd16 100644 --- a/TOOLS/subfont-c/README +++ b/TOOLS/subfont-c/README @@ -2,10 +2,11 @@ Usage: ~~~~~~ 1. Make sure you have FreeType 2 installed. 2. Get a TrueType or Type 1 font. -3. Modify `runme' script for your encoding and font path. -4. Type: ./runme -5. Copy *.raw and font.desc files to ~/.mplayer/font/ -6. Run subfont alone to see more options. +3. Run ./configure from mplayer's root directory. +4. Modify `runme' script for your encoding and font path. +5. Type: ./runme +6. Copy *.raw and font.desc files to ~/.mplayer/font/ +7. Run subfont alone to see more options. About: @@ -28,7 +29,7 @@ I found on http://rpmfind.net/ and euc-kr encoding. Custom encoding file for euc-kr was generated from charmap I found in /usr/share/i18n/charmaps/EUC-KR.gz (glibc package). This should work with -unicode switch for mplayer (though it is not Unicode encoding). -It took about 14 seconds to render over 8000 characters on P3 @ 600MHz. +It took about 10 seconds to render over 8000 characters on P3 @ 600MHz. Custom encodings: @@ -45,6 +46,34 @@ of a sigle line: 0105 B1 +New font.desc format (proposal): +~~~~~~~~~~~~~~~~~~~~~==========~ +Subfont will generate new font.desc format when compiled with NEW_DESC macro defined +(uncomment appropriate line in Makefile). + +These changes are to make bitmaps smaller and processing faster. + +Changes to [info] section: + There is no `spacewidth'. It will not be useful. + `height` is the distance from one baseline to the next. + `ascender' is the distance from the baseline to the highest grid coordinate used to place the outline point. + `descender' is the distance from the baseline to the lowest grid coordinate used to place the outline point. +Note: upwards direction is positive. +Read more: freetype-2.*/docs/glyphs/glyphs-3.html + +Changes to [characters] section: + Bitmap start and bitmap end are replaced with: + bitmap start, + bitmap width, + bitmap height, + left bearing -- the horizontal distance from the current pen position to the bitmaps's left edge, + top bearing -- the vertical distance from the baseline to the bitmaps's top edge, + advance -- the horizontal distance the pen position must be incremented by after each glyph is rendered. + +To anderstand this you must think in verctorial coordinates. +Necessarily read freetype-2.*/docs/glyphs/glyphs-7.html about vectorial coordinates! + + Notes: ~~~~~~ + Starting x position of each character and the bitmap width is aligned diff --git a/TOOLS/subfont-c/runme b/TOOLS/subfont-c/runme index fc1bb1efe9..47019986a8 100755 --- a/TOOLS/subfont-c/runme +++ b/TOOLS/subfont-c/runme @@ -1,6 +1,7 @@ #!/bin/bash font=arial.ttf +#font=verdana.ttf encoding=iso-8859-2 fontsize=24 symbolssize=35 @@ -9,18 +10,15 @@ outline=1.5 make || exit -./subfont --blur $blur --outline $outline "$encoding" $fontsize "$font" &>log -status=$? -cat log -[ $status == 0 ] || exit - +./subfont --blur $blur --outline $outline "$encoding" $fontsize "$font" || exit ./subfont --append --blur $blur --outline $outline encodings/osd-mplayer $symbolssize osd/osd.pfb || exit #cp font.desc *.raw ~/.mplayer/font/ exit -SIZE=`awk '/^bitmap size/ {print $NF}' log`+800 +# display *.raw files +SIZE=`awk '/bitmap size:/ {print $NF; exit}' font.desc`+800 display -size $SIZE gray:$encoding-a.raw & display -size $SIZE gray:$encoding-b.raw & #convert -size $SIZE gray:$encoding-a.raw $encoding-a.png diff --git a/TOOLS/subfont-c/subfont.c b/TOOLS/subfont-c/subfont.c index fabad13551..67e61ef2b6 100644 --- a/TOOLS/subfont-c/subfont.c +++ b/TOOLS/subfont-c/subfont.c @@ -1,10 +1,7 @@ /* * Renders antialiased fonts for mplayer using freetype library. * Should work with TrueType, Type1 and any other font supported by libfreetype. - * - * Goals: - * - internationalization: supports any 8 bit encoding (uses iconv). - * - nice look: creates glyph `shadows' using algorithm derived from gaussian blur. + * Can generate font.desc for any encoding. * * * Artur Zaprzala @@ -66,7 +63,7 @@ char *font_path; //char *font_metrics; int append_mode = 0; -unsigned char *buffer, *abuffer; +unsigned char *bbuffer, *abuffer; int width, height; static FT_ULong charset[max_charset_size]; /* characters we want to render; Unicode */ static FT_ULong charcodes[max_charset_size]; /* character codes in 'encoding' */ @@ -80,9 +77,14 @@ iconv_t cd; // iconv conversion descriptor #define ERROR(...) ERROR_(__VA_ARGS__, NULL) #define WARNING(...) WARNING_(__VA_ARGS__, NULL) -#define f266toInt(x) (((x)+32)>>6) // round fractional fixed point number to integer + +#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 ALIGN(x) (((x)+7)&~7) // 8 byte align +#define f266CeilToInt(x) (((x)+63)>>6) // ceiling +#define f266FloorToInt(x) ((x)>>6) // floor +#define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16 + +#define ALIGN(x) (((x)+7)&~7) // 8 byte align @@ -93,11 +95,11 @@ void paste_bitmap(FT_Bitmap *bitmap, int x, int y) { if (bitmap->pixel_mode==ft_pixel_mode_mono) for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch) for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp) - buffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0; + bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0; else for (h = bitmap->rows; h>0; --h, drow+=width, srow+=bitmap->pitch) for (w = bitmap->width, sp=dp=0; w>0; --w, ++dp, ++sp) - buffer[drow+dp] = bitmap->buffer[srow+sp]; + bbuffer[drow+dp] = bitmap->buffer[srow+sp]; } @@ -113,24 +115,17 @@ void write_header(FILE *f) { } -void write_bitmap() { +void write_bitmap(void *buffer, char type) { FILE *f; int const max_name = 128; char name[max_name]; - snprintf(name, max_name, "%s-b.raw", encoding_name); + snprintf(name, max_name, "%s-%c.raw", encoding_name, type); f = fopen(name, "wb"); if (f==NULL) ERROR("fopen failed."); write_header(f); fwrite(buffer, 1, width*height, f); fclose(f); - - snprintf(name, max_name, "%s-a.raw", encoding_name); - f = fopen(name, "wb"); - if (f==NULL) ERROR("fopen failed."); - write_header(f); - fwrite(abuffer, 1, width*height, f); - fclose(f); } @@ -138,27 +133,27 @@ void render() { FT_Library library; FT_Face face; FT_Error error; - FT_GlyphSlot slot; - FT_ULong glyph_index, character, code; - //FT_Glyph glyphs[max_charset_size]; - FT_Glyph glyph; + FT_Glyph *glyphs; + FT_BitmapGlyph glyph; FILE *f; int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING; - int pen_x, pen_xa, pen_y, ymin, ymax; + int pen_x = 0, pen_xa; + int ymin = INT_MAX, ymax = INT_MIN; int i, uni_charmap = 1; int baseline, space_advance = 20; + int glyphs_count = 0; /* initialize freetype */ error = FT_Init_FreeType(&library); if (error) ERROR("Init_FreeType failed."); error = FT_New_Face(library, font_path, 0, &face); - if (error) ERROR("New_Face failed."); + if (error) ERROR("New_Face failed. Maybe the font path `%s' is wrong.", font_path); /* if (font_metrics) { error = FT_Attach_File(face, font_metrics); - if (error) WARNING("Attach_File failed."); + if (error) WARNING("FT_Attach_File failed."); } */ @@ -173,8 +168,10 @@ void render() { //error = FT_Select_Charmap(face, ft_encoding_adobe_standard); //error = FT_Select_Charmap(face, ft_encoding_adobe_custom); //error = FT_Set_Charmap(face, face->charmaps[1]); - //if (error) WARNING("Select_Charmap failed."); + //if (error) WARNING("FT_Select_Charmap failed."); #endif + + #if 0 /************************************************************/ if (FT_HAS_GLYPH_NAMES(face)) { @@ -214,7 +211,7 @@ void render() { 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("Set_Pixel_Sizes failed."); + if (error) WARNING("FT_Set_Pixel_Sizes failed."); if (FT_IS_FIXED_WIDTH(face)) @@ -224,13 +221,14 @@ void render() { /* 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); + else space_advance = f266ToInt(face->glyph->advance.x); /* create font.desc */ f = fopen(font_desc, append_mode ? "a":"w"); if (f==NULL) ERROR("fopen failed."); + /* print font.desc header */ if (append_mode) { fprintf(f, "\n\n# Subtitle font for %s encoding, face \"%s%s%s\", ppem=%i\n", @@ -246,10 +244,20 @@ void render() { face->family_name, face->style_name ? " ":"", face->style_name ? face->style_name:"", ppem); +#ifdef NEW_DESC + fprintf(f, "descversion 2\n"); +#else fprintf(f, "descversion 1\n"); +#endif fprintf(f, "spacewidth %i\n", 2*padding + space_advance); +#ifndef NEW_DESC fprintf(f, "charspace %i\n", -2*padding); - fprintf(f, "height %i\n", f266toInt(face->size->metrics.height)); +#endif + fprintf(f, "height %i\n", f266ToInt(face->size->metrics.height)); +#ifdef NEW_DESC + fprintf(f, "ascender %i\n", f266CeilToInt(face->size->metrics.ascender)); + fprintf(f, "descender %i\n", f266FloorToInt(face->size->metrics.descender)); +#endif } fprintf(f, "\n[files]\n"); fprintf(f, "alpha %s-a.raw\n", encoding_name); @@ -257,17 +265,18 @@ void render() { fprintf(f, "\n[characters]\n"); - /* compute bbox and [characters] section*/ - pen_x = 0; - pen_y = 0; - ymin = INT_MAX; - ymax = INT_MIN; + // render glyphs, compute bitmap size and [characters] section + glyphs = (FT_Glyph*)malloc(charset_size*sizeof(FT_Glyph*)); for (i= 0; iglyph; - error = FT_Get_Glyph(slot, &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); + continue; + } + } - FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_pixels, &bbox); - if (pen_y+bbox.yMax>ymax) { - ymax = pen_y+bbox.yMax; - // eprintf("%3i: ymax %i (%c)\n", code, ymax, code); + // 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); + continue; } - if (pen_y+bbox.yMinbitmap.rows > height) height = glyph->bitmap.rows; + + // advance pen + pen_xa = pen_x + glyph->bitmap.width + 2*padding; + + // font.desc + fprintf(f, "0x%02x %i %i %i %i %i %i;\tU+%04X|%c\n", code, + pen_x, // bitmap start + glyph->bitmap.width + 2*padding, // bitmap width + glyph->bitmap.rows + 2*padding, // bitmap height + glyph->left - padding, // left bearing + glyph->top + padding, // top bearing + f266ToInt(slot->advance.x), // advance + character, code<' '||code>255 ? '.':code); +#else + // max height + if (glyph->top > ymax) { + ymax = glyph->top; + //eprintf("%3i: ymax %i (%c)\n", code, ymax, code); + } + if (glyph->top - glyph->bitmap.rows < ymin) { + ymin = glyph->top - glyph->bitmap.rows; + //eprintf("%3i: ymin %i (%c)\n", code, ymin, code); } /* advance pen */ - pen_xa = pen_x + f266toInt(slot->advance.x) + 2*padding; - // pen_y += f266toInt(slot->advance.y); // for vertical layout + pen_xa = pen_x + f266ToInt(slot->advance.x) + 2*padding; /* font.desc */ fprintf(f, "0x%02x %i %i;\tU+%04X|%c\n", code, pen_x, pen_xa-1, character, code<' '||code>255 ? '.':code); +#endif pen_x = ALIGN(pen_xa); - } - fclose(f); - - if (ymax<=ymin) ERROR("Something went wrong. Use the source!"); - width = pen_x; + pen_x = 0; +#ifdef NEW_DESC + if (height<=0) ERROR("Something went wrong. Use the source!"); + height += 2*padding; +#else + if (ymax<=ymin) ERROR("Something went wrong. Use the source!"); height = ymax - ymin + 2*padding; baseline = ymax + padding; - eprintf("bitmap size: %ix%i\n", width, height); +#endif - buffer = (unsigned char*)malloc(width*height); - abuffer = (unsigned char*)malloc(width*height); - if (buffer==NULL || abuffer==NULL) ERROR("malloc failed."); + // end of font.desc + if (DEBUG) eprintf("bitmap size: %ix%i\n", width, height); + fprintf(f, "# bitmap size: %ix%i\n", width, height); + fclose(f); - memset(buffer, 0, width*height); + bbuffer = (unsigned char*)malloc(width*height); + if (bbuffer==NULL) ERROR("malloc failed."); + memset(bbuffer, 0, width*height); - /* render glyphs */ - pen_x = 0; - pen_y = baseline; - for (i= 0; ibitmap, + pen_x + padding, + padding); - error = FT_Render_Glyph(face->glyph, ft_render_mode_normal); - if (error) WARNING("Render_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character); - - slot = face->glyph; - - paste_bitmap(&slot->bitmap, - pen_x + padding + slot->bitmap_left, - pen_y - slot->bitmap_top ); + /* advance pen */ + pen_x += glyph->bitmap.width + 2*padding; +#else + paste_bitmap(&glyph->bitmap, + pen_x + padding + glyph->left, + baseline - glyph->top); /* advance pen */ - pen_x += f266toInt(slot->advance.x) + 2*padding; - // pen_y += f266toInt(slot->advance.y); // for vertical layout + pen_x += f1616ToInt(glyph->root.advance.x) + 2*padding; +#endif pen_x = ALIGN(pen_x); + + FT_Done_Glyph((FT_Glyph)glyph); } + free(glyphs); error = FT_Done_FreeType(library); - if (error) ERROR("Done_FreeType failed."); + if (error) ERROR("FT_Done_FreeType failed."); } @@ -512,10 +544,10 @@ void outline1( } -// brute-force gaussian blur +// gaussian blur void blur( - unsigned char *s, - unsigned char *t, + unsigned char *buffer, + unsigned char *tmp, int width, int height, int *m, @@ -525,27 +557,57 @@ void blur( int x, y; + unsigned char *s = buffer - r; + unsigned char *t = tmp; for (y = 0; y=width)?(width-x-1):r; + int x1 = (x=width) ? (r+width-x):mwidth; + int mx; + for (mx = x1; mx=height) ? (r+height-y):mwidth; + unsigned char *smy = s + y1*width; int my; + for (my = y1; my= height) break; - for (mx = x1; mx<=x2; ++mx) - sum+= srow[mx] * mrow[mx]; +// Gaussian matrix +// Maybe for future use. +unsigned gmatrix(unsigned *m, int r, int w, double const A) { + unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A + int mx, my; - } - *t = (sum + volume/2) / volume; + for (my = 0; my