[Mplayer-cvslog] CVS: main/TOOLS/subfont-c Makefile,NONE,1.1 README,NONE,1.1 font.desc.tail,NONE,1.1 subfont.c,NONE,1.1

Arpi of Ize arpi at mplayer.dev.hu
Thu Aug 9 19:28:09 CEST 2001


Update of /cvsroot/mplayer/main/TOOLS/subfont-c
In directory mplayer:/var/tmp.root/cvs-serv7693

Added Files:
	Makefile README font.desc.tail subfont.c 
Log Message:
mplayer font-generator using FreeType 2.x by Artur Zaprzala <zybi at fanthom.irc.pl>

--- NEW FILE ---
#font="/mnt/win/windows/fonts/arial.ttf"
#font="/mnt/win/windows/fonts/comic.ttf"
#font="/mnt/win/windows/fonts/verdanai.ttf"
font="/mnt/win/windows/fonts/verdana.ttf"

#encoding=windows-1250
encoding=iso-8859-2

fontsize=20


LDLIBS=-lm $(shell freetype-config --libs)
CFLAGS=-O3 $(shell freetype-config --cflags)


subfont:	subfont.o

run:	subfont
	./subfont $(encoding) $(fontsize) $(font)
	cat font.desc.tail >> font.desc
	cp font.desc *.raw ~/.mplayer/font/

clean:
	rm -f subfont subfont.o

--- NEW FILE ---
About:
~~~~~
`subfont' program 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 (slow!).


Note:
~~~~
Starting x position of each char and the bitmap width is aligned to multiple of 8
(required for under-development MMX renderer).


Usage:
~~~~~
Usage: subfont encoding ppem font [alphaFactor [minAlpha [radius]]]
  Program creates 3 files: font.desc, <encoding>-a.raw, <encoding>-b.raw.
  You should append font.desc.tail (desc for OSD characters by a'rpi & chass) to font.desc,
  and copy font.desc and all *.raw files to ~/.mplayer/font/ directory.

  encoding     must be 8 bit encoding, like iso-8859-2.
               To list encodings available on your system use iconv -l.
  ppem         Font size in pixels (e.g. 24).
  font         Font file path. Any format supported by freetype library (*.ttf, *.pf?, *).
  alphaFactor  Alpha map scaling factor (default is 1.0), float.
  minAlpha     Alpha map minimum value (default is 1.0, max is 255), float.
  radius       Alpha map blur radius (default is 6 pixels), integer.


Example:
~~~~~~~
make
./subfont iso-8859-2 20 verdana.ttf
cat font.desc.tail >> font.desc
cp font.desc *.raw ~/.mplayer/font/


Author:
~~~~~~
Artur Zaprzala <zybi at fanthom.irc.pl>

--- NEW FILE ---

;
; Mplayer subtitle font description file - HighreS ONE
; font created by a'rpi & chass
; .desc created by chass & atmosfear
; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;
;
; This file contains the data for Mplayer to build up the font
; table from the bitmap files. These fonts are used for the OSD
; (On Screen Display) and for the subtitles.
;
;
; A few words for the novice user about the fonts:
; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;
; Mplayer uses antialiased bitmap fonts, which look far better
; then the TTF rendering used by other players. A letter consists
; of two main parts: the Bitmap and the Alpha chanel. The bitmap
; is what you see on the screen, and the Aplha chanel makes the 
; Font fade smoothly ito the background. And in this font we use
; the alpha to get a smooth black outline of the letters, which 
; makes them visible even on white areas of the movie.
;
; Mplayer needs at least two seperate 8bit/pixel format bitmap 
; files and a .desc file to be able to use the font. One of the
; files is the bitmap the other is the alpha. The .desc (like this
; one) gives Mplayer the information needed to convert a bitmap
; to characters. But the .desc allows to use more bitmap and alpha
; files: this feature is useful when one wants to add local language
; support for the subtitles.
;
; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;
; ABOUT THE .DESC
;
; [1],File layout: the file consists of several sections,
; the sections are marked like [XXX] where XXX is the section
; name. The three main sections:
;
;  [info]: gives general information about the font, like the
;          version of the .desc, the author's name and general 
;          rendering options like kerning.
;
;  [files] & [characters] : they go togeather, they describe
;                           the place of characters in the bitmap.
;                           Up to 16 is allowed per .desc
;
; [2],The variables:
;
; [info]
;    name        string            name of the font, and misc information
;    desc        int               Version of this file
;    spacewidth  int               num of pixels for #32
;    charspace   int               the distance between chars
;    height      int               the distance between rows
; [files]
;    alpha       string            name of alpha map file
;    bitmap      string            name of bitmap file
; [character]
;                string int int    the positon of a character
;
;
; MPlayer can build up it's font table from up to 16*2 files (16 bitmap
; and 16 alpha channels).
;                           


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;informations about the font

;[info]

;the author and other stuff
;name "HighreS ONE - created by A'rpi and cHaSS - pre-alpha version"

;version number of the file (if greater than version MPlayer can
;handle, then ignores the unknown variables)

;descversion 1

;the length of #32 in pixels

;spacewidth 13

;the distance between chars. the alpha renderer can handle
;negative numbers too

;charspace -3

;height 26
;height 35

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;file section

;[files]

;the name of the alpha file used for the next [characters]
;section 

;alpha arpi_a.raw
;alpha nfont_a.raw

;the name of the bitmap file used for the next [characters]
;section 

;bitmap arpi_b.raw
;bitmap nfont_b.raw



;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;the description of the characters: 
;each row specifies one character: first, the char (between " or ')
;then the x-coordinate of the beginning and the end.


;[characters]

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
[files]
alpha arpi_osd_a.raw
bitmap arpi_osd_b.raw

[characters]
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;Symbols for the OSD
; range form 01 to 1F

;play
0x01 0 36
;pause
0x02 35 71
;stop
0x03 70 106
;rewind
0x04 116 152
;fast forward
0x05 164 200
;clock symbol for the OSD
0x06 209 245
;contrast
0x07 256 292
;stauration
0x08 305 342
;volume
0x09 354 400
;brightness
0x0A 407 442
;Hue
0x0B 457 494

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;OSD progress bar characters
;
[files]

alpha arpi_progress_a.raw
bitmap arpi_progress_b.raw

[characters]
; [ character
0x10 4 21
; | character
0x11 30 41
; ] character
0x12 50 66
; . char
0x13 74 85

--- NEW FILE ---
/*
 * 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.
 *
 *
 * Artur Zaprzala <zybi at fanthom.irc.pl>
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <math.h>
#include <string.h>

#if 1			/* freetype 2.0.1 */
#include <freetype/freetype.h>
#else			/* freetype 2.0.3 */
#include <ft2build.h>	
#include FT_FREETYPE_H
#endif

#include FT_GLYPH_H



#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) */


int const	test = 0;

/* default values */
char		*encoding = "iso-8859-1";	/* target encoding */
char		*charmap = "ucs-4le";		/* font charmap encoding */
int		ppem = 20;			/* font size in pixels */

int const	colors = 256;
int const	maxcolor = 255;
int		radius = 6;			/* blur radius */
double		minalpha = 1.0;			/* good value for minalpha is 0.5 */
double		alpha_factor = 1.0;

int const	first_char = 33;
int const	charset_size = 256;

char		*command;
char		*font_path = NULL;
/*char		*font_metrics = NULL;*/


unsigned char	*buffer;
unsigned char	*abuffer;
int		width, height;
FT_ULong	ustring[256];

#define eprintf(...)		fprintf(stderr, __VA_ARGS__)
#define ERROR(msg, ...)		(eprintf("%s: error: " msg "\n", command, ##__VA_ARGS__), exit(1))
#define WARNING(msg, ...)	eprintf("%s: warning: " msg "\n", command, ##__VA_ARGS__)



void paste_bitmap(FT_Bitmap *bitmap, int x, int y) {
    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; --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;
    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];
}

void write_header(FILE *f) {
    static unsigned char   header[800] = "mhwanh";
    int i;
    header[7] = 4;
    header[8] = width>>8;	header[9] = (unsigned char)width;
    header[10] = height>>8;	header[11] = (unsigned char)height;
    header[12] = colors>>8;	header[13] = (unsigned char)colors;
    for (i = 32; i<800; ++i) header[i] = (i-32)/3;
    fwrite(header, 1, 800, f);
}

void write_bitmap() {
    FILE *f;
    int const max_name = 128;
    char name[max_name];

    snprintf(name, max_name, "%s-b.raw", encoding);
    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);
    f = fopen(name, "wb");
    if (f==NULL) ERROR("fopen failed.");
    write_header(f);
    fwrite(abuffer, 1, width*height, f);
    fclose(f);
}

void render() {
    FT_Library	library;
    FT_Face	face;
    FT_Error	error;
    FT_GlyphSlot	slot;
    FT_ULong	glyph_index;
    FT_Glyph	glyphs[charset_size];
    FILE	*f;
    int	const	load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
    int		pen_x, pen_xa, pen_y, ymin, ymax;
    int		i, c;
    int		baseline, space_advance = 20;


    /* 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 (font_metrics) {
	error = FT_Attach_File(face, font_metrics);
	if (error) WARNING("Attach_File failed.");
    }
    */


    if (face->charmap->encoding!=ft_encoding_unicode)
	WARNING("Selected font has no unicode charmap. Very bad!");


    /* set size */
    if (FT_IS_SCALABLE(face)) {
	error = FT_Set_Pixel_Sizes(face, ppem, ppem);
    } else {
	int j = 0;
	int jppem = face->available_sizes[0].height;
	/* find closest size */
	for (i = 0; i<face->num_fixed_sizes; ++i) {
	    if (abs(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("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);	/* +32 is for rounding */


    /* create font.desc */
    f = fopen("font.desc", "w");
    if (f==NULL) ERROR("fopen failed.");

    /* print font.desc header */
    fprintf(f, "[info]\n");
    fprintf(f, "name 'File generated for %s encoding using `%s%s%s' face (%s), ppem=%i'\n",
	    encoding,
	    face->family_name,
	    face->style_name ? " ":"", face->style_name ? face->style_name:"",
	    font_path,
	    ppem);
    fprintf(f, "descversion 1\n");
    fprintf(f, "spacewidth %i\n",	2*radius + space_advance);
    fprintf(f, "charspace %i\n",	-2*radius);
    fprintf(f, "height %i\n",	f266toInt(face->size->metrics.height));
    fprintf(f, "\n[files]\n");
    fprintf(f, "alpha %s-a.raw\n",	encoding);
    fprintf(f, "bitmap %s-b.raw\n",	encoding);
    fprintf(f, "\n[characters]\n");


    /* compute bbox and [characters] section*/
    pen_x = 0;
    pen_y = 0;
    ymin = INT_MAX;
    ymax = INT_MIN;
    for (c= first_char, i= 0; c<charset_size; ++c, ++i) {
	FT_UInt	glyph_index;
	FT_BBox	bbox;

	glyph_index = FT_Get_Char_Index(face, ustring[i]);
	if (glyph_index<=0) {
	    WARNING("Glyph for char %3i|%2X|U%04X not found.", c, c, ustring[i]);
	    continue;
	}

	error = FT_Load_Glyph(face, glyph_index, load_flags);
	if (error) {
	    WARNING("Load_Glyph %3u|%2X (char %3i|%2X|U%04X) failed.", glyph_index, glyph_index, c, c, ustring[i]);
	    continue;
	}

	slot = face->glyph;
	error = FT_Get_Glyph(slot, &glyphs[i]);


	FT_Glyph_Get_CBox(glyphs[i], ft_glyph_bbox_pixels, &bbox);
	if (pen_y+bbox.yMax>ymax) {
	    ymax = pen_y+bbox.yMax;
	    /* eprintf("%3i: ymax %i (%c)\n", c, ymax, c); */
	}
	if (pen_y+bbox.yMin<ymin) {
	    ymin = pen_y+bbox.yMin;
	    /* eprintf("%3i: ymin %i (%c)\n", c, ymin, c); */
	}

	/* advance pen */
	pen_xa = pen_x + f266toInt(slot->advance.x) + 2*radius;
	/* pen_y += f266toInt(slot->advance.y);		// for vertical layout */

	/* font.desc */
	if (c=='\'')
	    fprintf(f, "\"%c\" %i %i\n", c, pen_x,  pen_xa-1);
	else
	    fprintf(f, "'%c' %i %i\n", c, pen_x, pen_xa-1);
	pen_x = (pen_xa+7)&~7;				/* 8 byte align */

    }

    fclose(f);

    if (ymax<=ymin) ERROR("Something went wrong.");


    width = pen_x;
    height = ymax - ymin + 2*radius;
    baseline = ymax + radius;
    eprintf("bitmap size: %ix%i\n", width, height);

    buffer = (unsigned char*)malloc(width*height);
    abuffer = (unsigned char*)malloc(width*height);
    if (buffer==NULL || abuffer==NULL) ERROR("malloc failed.");


    /* render glyphs */
    pen_x = 0;
    pen_y = baseline;
    for (c= first_char, i= 0; c<charset_size; ++c, ++i) {
	FT_UInt	glyph_index;

	glyph_index = FT_Get_Char_Index(face, ustring[i]);
	if (glyph_index==0) continue;
	error = FT_Load_Glyph(face, glyph_index, load_flags);
	if (error) {
	    /* WARNING("Load_Glyph failed"); */
	    continue;
	}

	error = FT_Render_Glyph(face->glyph, ft_render_mode_normal);
	if (error) WARNING("Render_Glyph %3i|%2X (char %3i|%2X|U%04X) failed.", glyph_index, glyph_index, c, c, ustring[i]);

	slot = face->glyph;

	paste_bitmap(&slot->bitmap,
	    pen_x + radius + slot->bitmap_left,
	    pen_y - slot->bitmap_top );

	/* advance pen */
	pen_x += f266toInt(slot->advance.x) + 2*radius;
	/* pen_y += f266toInt(slot->advance.y);	// for vertical layout */
	pen_x = (pen_x+7)&~7;			/* 8 byte align */
    }


    error = FT_Done_FreeType(library);
    if (error) ERROR("Done_FreeType failed.");
}

void prepare_charset() {
    iconv_t cd;
    unsigned char text[charset_size];
    char *inbuf = text;
    char *outbuf = (char*) ustring;
    int inbuf_left = charset_size;
    int outbuf_left = 4*charset_size;
    int i;
    size_t count;

    for (i = first_char; i<charset_size; ++i) text[i-first_char] = i;

    /* check if ucs-4le is available */
    cd = iconv_open(charmap, charmap);
    if (cd==(iconv_t)-1) ERROR("iconv doesn't know %s encoding. Use the source!", charmap);
    iconv_close(cd);

    cd = iconv_open(charmap, encoding);
    if (cd==(iconv_t)-1) ERROR("Unsupported encoding, use iconv -l to list character sets known on your system.");
    while (1) {
	count = iconv(cd, &inbuf, &inbuf_left, &outbuf, &outbuf_left);
    	if (inbuf_left==0) break;
	/* skip undefined characters */
	inbuf+= 1;
	inbuf_left-= 1;
	*(FT_ULong*)outbuf = 0;
	outbuf+= 4;
    }
    iconv_close(cd);
}

void blur() {
    int const r = radius;
    int const w = 2*r+1;	/* matrix size */
    double const A = log(minalpha/maxcolor)/((r+1)*(r+1));
    double const B = alpha_factor * maxcolor;

    int i, x, y, mx, my;
    unsigned char *m = (unsigned char*)malloc(w*w);

    if (m==NULL) ERROR("malloc failed");


    /* Gaussian matrix */
    for (my = 0; my<w; ++my) {
	for (mx = 0; mx<w; ++mx) {
	    m[mx+my*w] = (int)(exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * B + .5);
	    if (test) eprintf("%3i ", m[mx+my*w]);
	}
	if (test) eprintf("\n");
    }

    /* This is not a gaussian blur! */
    /* And is very slow */
    for (y = 0; y<height; ++y)
	for (x = 0; x<width; ++x) {
	    int max = -1;
	    for (my = -r; my<=r; ++my)
		if (y+my>=0 && y+my<height)
		    for (mx = -r; mx<=r; ++mx) {
			if (x+mx>=0 && x+mx<width) {
			    int p = buffer[x+mx+(y+my)*width] * m[mx+r+(my+r)*w];
			    if (p>max) {
				max = p;
				abuffer[x+y*width] = (p + maxcolor/2) / maxcolor;
			    }
			}
		    
		    }
	}
    free(m);
}

void usage() {
    printf("Usage: %s encoding ppem font [alphaFactor [minAlpha [radius]]]\n", command);
    printf(
	    "  Program creates 3 files: font.desc, <encoding>-a.raw, <encoding>-b.raw.\n"
	    "  You should append font.desc.tail (desc for OSD characters by a'rpi & chass) to font.desc,\n"
	    "  and copy font.desc and all *.raw files to ~/.mplayer/font/ directory.\n"
	    "\n"
	    "  encoding     must be 8 bit encoding, like iso-8859-2.\n"
	    "               To list encodings available on your system use iconv -l.\n"
	    "  ppem         Font size in pixels (e.g. 24).\n"
	    "  font         Font file path. Any format supported by freetype library (*.ttf, *.pf?, *).\n"
	    "  alphaFactor  Alpha map scaling factor (default is 1.0), float.\n"
	    "  minAlpha     Alpha map minimum value (default is 1.0, max is 255), float.\n"
	    "  radius       Alpha map blur radius (default is 6 pixels), integer.\n"
	    );
    exit(1);
}

void parse_args(int argc, char **argv) {
    int i;
    double d;

    command = strrchr(argv[0], '/');
    if (command==NULL) command = argv[0];
    else ++command;

    if (argc<4) usage();

    encoding = argv[1];

    i = atoi(argv[2]);
    if (i>1) ppem = i;

    font_path = argv[3];

    if (argc>4) {
	d = atof(argv[4]);
	if (d>0.001 && d<1000.) alpha_factor = d;
	else WARNING("alphaFactor set to default.");
    }
    if (argc>5) {
	d = atof(argv[5]);
	if (d>0.1 && d<=maxcolor) minalpha = d;
	else WARNING("minAlpha set to default.");
    }
    if (argc>6) {
	i = atoi(argv[6]);
	if (i>=0 && i<20) radius = i;
	else WARNING("radius set to default.");
    }
}

int main(int argc, char **argv) {
    parse_args(argc, argv);

    prepare_charset();
    render();
    blur();
    write_bitmap();

    free(buffer);
    free(abuffer);

    puts(
	    "\n"
	    "*****************************************\n"
	    "*  Remember to run:                     *\n"
	    "*  cat font.desc.tail >> font.desc      *\n"
	    "*****************************************"
	    );

    return 0;
}




More information about the MPlayer-cvslog mailing list