2001-08-09 17:28:06 +00:00
|
|
|
/*
|
|
|
|
* Renders antialiased fonts for mplayer using freetype library.
|
|
|
|
* Should work with TrueType, Type1 and any other font supported by libfreetype.
|
2001-08-21 14:00:57 +00:00
|
|
|
* Can generate font.desc for any encoding.
|
2001-08-09 17:28:06 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Artur Zaprzala <zybi@fanthom.irc.pl>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2001-08-13 18:37:10 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OLD_FREETYPE2
|
2001-08-09 17:28:06 +00:00
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
#include FT_GLYPH_H
|
2001-08-13 18:37:10 +00:00
|
|
|
#else /* freetype 2.0.1 */
|
|
|
|
#include <freetype/freetype.h>
|
|
|
|
#include <freetype/ftglyph.h>
|
|
|
|
#endif
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-24 10:46:31 +00:00
|
|
|
|
|
|
|
#include "../../bswap.h"
|
|
|
|
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
#ifndef DEBUG
|
|
|
|
#define DEBUG 0
|
|
|
|
#endif
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
//// default values
|
2001-08-09 17:28:06 +00:00
|
|
|
char *encoding = "iso-8859-1"; /* target encoding */
|
2001-08-13 18:37:10 +00:00
|
|
|
char *charmap = "ucs-4"; /* font charmap encoding, I hope ucs-4 is always big endian */
|
|
|
|
/* gcc 2.1.3 doesn't support ucs-4le, but supports ucs-4 (==ucs-4be) */
|
2001-08-24 08:55:00 +00:00
|
|
|
float ppem = 22; /* font size in pixels */
|
2001-08-13 18:37:10 +00:00
|
|
|
|
|
|
|
double radius = 2; /* blur radius */
|
|
|
|
double thickness = 1.5; /* outline thickness */
|
|
|
|
|
|
|
|
char* font_desc = "font.desc";
|
|
|
|
//char* font_desc = "/dev/stdout";
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2002-03-03 19:18:42 +00:00
|
|
|
char *outdir = ".";
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
//// constants
|
2001-08-09 17:28:06 +00:00
|
|
|
int const colors = 256;
|
|
|
|
int const maxcolor = 255;
|
2001-08-13 18:37:10 +00:00
|
|
|
unsigned const base = 256;
|
|
|
|
unsigned const first_char = 33;
|
|
|
|
#define max_charset_size 60000
|
|
|
|
//int const max_charset_size = 256;
|
|
|
|
unsigned charset_size = 0;
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
////
|
2001-08-09 17:28:06 +00:00
|
|
|
char *command;
|
2001-08-13 18:39:23 +00:00
|
|
|
char *encoding_name;
|
2001-08-13 18:37:10 +00:00
|
|
|
char *font_path;
|
|
|
|
//char *font_metrics;
|
|
|
|
int append_mode = 0;
|
2001-08-24 08:55:00 +00:00
|
|
|
int unicode_desc = 0;
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
unsigned char *bbuffer, *abuffer;
|
2001-08-09 17:28:06 +00:00
|
|
|
int width, height;
|
2001-08-24 08:55:00 +00:00
|
|
|
int padding;
|
2001-08-13 18:37:10 +00:00
|
|
|
static FT_ULong charset[max_charset_size]; /* characters we want to render; Unicode */
|
|
|
|
static FT_ULong charcodes[max_charset_size]; /* character codes in 'encoding' */
|
|
|
|
iconv_t cd; // iconv conversion descriptor
|
|
|
|
|
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
|
2001-08-13 18:37:10 +00:00
|
|
|
#define ERROR_(msg, ...) (eprintf("%s: error: " msg "\n", command, __VA_ARGS__), exit(1))
|
|
|
|
#define WARNING_(msg, ...) eprintf("%s: warning: " msg "\n", command, __VA_ARGS__)
|
|
|
|
#define ERROR(...) ERROR_(__VA_ARGS__, NULL)
|
|
|
|
#define WARNING(...) WARNING_(__VA_ARGS__, NULL)
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
|
|
|
|
#define f266ToInt(x) (((x)+32)>>6) // round fractional fixed point number to integer
|
2001-08-13 18:37:10 +00:00
|
|
|
// coordinates are in 26.6 pixels (i.e. 1/64th of pixels)
|
2001-08-21 14:00:57 +00:00
|
|
|
#define f266CeilToInt(x) (((x)+63)>>6) // ceiling
|
|
|
|
#define f266FloorToInt(x) ((x)>>6) // floor
|
|
|
|
#define f1616ToInt(x) (((x)+0x8000)>>16) // 16.16
|
2001-08-24 08:55:00 +00:00
|
|
|
#define floatTof266(x) ((int)((x)*(1<<6)+0.5))
|
2001-08-21 14:00:57 +00:00
|
|
|
|
|
|
|
#define ALIGN(x) (((x)+7)&~7) // 8 byte align
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2001-08-21 14:00:57 +00:00
|
|
|
bbuffer[drow+dp] = (bitmap->buffer[srow+sp/8] & (0x80>>(sp%8))) ? 255:0;
|
2001-08-09 17:28:06 +00:00
|
|
|
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)
|
2001-08-21 14:00:57 +00:00
|
|
|
bbuffer[drow+dp] = bitmap->buffer[srow+sp];
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
void write_header(FILE *f) {
|
|
|
|
static unsigned char header[800] = "mhwanh";
|
|
|
|
int i;
|
|
|
|
header[7] = 4;
|
2002-05-02 01:56:28 +00:00
|
|
|
if (width < 0x10000) { // are two bytes enough for the width?
|
2001-08-09 17:28:06 +00:00
|
|
|
header[8] = width>>8; header[9] = (unsigned char)width;
|
2002-05-02 01:56:28 +00:00
|
|
|
} else { // store width using 4 bytes at the end of the header
|
|
|
|
header[8] = header[9] = 0;
|
|
|
|
header[28] = (width >> 030) & 0xFF;
|
|
|
|
header[29] = (width >> 020) & 0xFF;
|
|
|
|
header[30] = (width >> 010) & 0xFF;
|
|
|
|
header[31] = (width ) & 0xFF;
|
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
void write_bitmap(void *buffer, char type) {
|
2001-08-09 17:28:06 +00:00
|
|
|
FILE *f;
|
|
|
|
int const max_name = 128;
|
|
|
|
char name[max_name];
|
|
|
|
|
2002-03-03 19:18:42 +00:00
|
|
|
snprintf(name, max_name, "%s/%s-%c.raw", outdir, encoding_name, type);
|
2001-08-09 17:28:06 +00:00
|
|
|
f = fopen(name, "wb");
|
2001-08-13 18:37:10 +00:00
|
|
|
if (f==NULL) ERROR("fopen failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
write_header(f);
|
|
|
|
fwrite(buffer, 1, width*height, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
void render() {
|
|
|
|
FT_Library library;
|
|
|
|
FT_Face face;
|
|
|
|
FT_Error error;
|
2001-08-21 14:00:57 +00:00
|
|
|
FT_Glyph *glyphs;
|
|
|
|
FT_BitmapGlyph glyph;
|
2001-08-09 17:28:06 +00:00
|
|
|
FILE *f;
|
|
|
|
int const load_flags = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
|
2001-08-21 14:00:57 +00:00
|
|
|
int pen_x = 0, pen_xa;
|
|
|
|
int ymin = INT_MAX, ymax = INT_MIN;
|
2001-08-13 18:37:10 +00:00
|
|
|
int i, uni_charmap = 1;
|
2001-08-09 17:28:06 +00:00
|
|
|
int baseline, space_advance = 20;
|
2001-08-21 14:00:57 +00:00
|
|
|
int glyphs_count = 0;
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* initialize freetype */
|
|
|
|
error = FT_Init_FreeType(&library);
|
2001-08-13 18:37:10 +00:00
|
|
|
if (error) ERROR("Init_FreeType failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
error = FT_New_Face(library, font_path, 0, &face);
|
2001-08-21 14:00:57 +00:00
|
|
|
if (error) ERROR("New_Face failed. Maybe the font path `%s' is wrong.", font_path);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
if (font_metrics) {
|
|
|
|
error = FT_Attach_File(face, font_metrics);
|
2001-08-21 14:00:57 +00:00
|
|
|
if (error) WARNING("FT_Attach_File failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
#if 0
|
|
|
|
/************************************************************/
|
|
|
|
eprintf("Font encodings:\n");
|
|
|
|
for (i = 0; i<face->num_charmaps; ++i)
|
|
|
|
eprintf("'%.4s'\n", (char*)&face->charmaps[i]->encoding);
|
|
|
|
|
|
|
|
//error = FT_Select_Charmap(face, ft_encoding_unicode);
|
|
|
|
//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]);
|
2001-08-21 14:00:57 +00:00
|
|
|
//if (error) WARNING("FT_Select_Charmap failed.");
|
2001-08-13 18:37:10 +00:00
|
|
|
#endif
|
2001-08-21 14:00:57 +00:00
|
|
|
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
#if 0
|
|
|
|
/************************************************************/
|
|
|
|
if (FT_HAS_GLYPH_NAMES(face)) {
|
|
|
|
int const max_gname = 128;
|
|
|
|
char gname[max_gname];
|
|
|
|
for (i = 0; i<face->num_glyphs; ++i) {
|
|
|
|
FT_Get_Glyph_Name(face, i, gname, max_gname);
|
|
|
|
eprintf("%02x `%s'\n", i, gname);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* set size */
|
|
|
|
if (FT_IS_SCALABLE(face)) {
|
2001-08-24 08:55:00 +00:00
|
|
|
error = FT_Set_Char_Size(face, floatTof266(ppem), 0, 0, 0);
|
|
|
|
if (error) WARNING("FT_Set_Char_Size failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
} else {
|
|
|
|
int j = 0;
|
|
|
|
int jppem = face->available_sizes[0].height;
|
|
|
|
/* find closest size */
|
|
|
|
for (i = 0; i<face->num_fixed_sizes; ++i) {
|
2001-08-24 08:55:00 +00:00
|
|
|
if (fabs(face->available_sizes[i].height - ppem) < abs(face->available_sizes[i].height - jppem)) {
|
2001-08-09 17:28:06 +00:00
|
|
|
j = i;
|
|
|
|
jppem = face->available_sizes[i].height;
|
|
|
|
}
|
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
WARNING("Selected font is not scalable. Using ppem=%i.", face->available_sizes[j].height);
|
2001-08-09 17:28:06 +00:00
|
|
|
error = FT_Set_Pixel_Sizes(face, face->available_sizes[j].width, face->available_sizes[j].height);
|
2001-08-24 08:55:00 +00:00
|
|
|
if (error) WARNING("FT_Set_Pixel_Sizes failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (FT_IS_FIXED_WIDTH(face))
|
2001-08-13 18:37:10 +00:00
|
|
|
WARNING("Selected font is fixed-width.");
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* compute space advance */
|
|
|
|
error = FT_Load_Char(face, ' ', load_flags);
|
2001-08-13 18:37:10 +00:00
|
|
|
if (error) WARNING("spacewidth set to default.");
|
2001-08-21 14:00:57 +00:00
|
|
|
else space_advance = f266ToInt(face->glyph->advance.x);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* create font.desc */
|
2002-03-03 19:18:42 +00:00
|
|
|
{
|
|
|
|
int const max_name = 128;
|
|
|
|
char name[max_name];
|
|
|
|
|
|
|
|
snprintf(name, max_name, "%s/%s", outdir, font_desc);
|
|
|
|
f = fopen(name, append_mode ? "a":"w");
|
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
if (f==NULL) ERROR("fopen failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
/* print font.desc header */
|
2001-08-13 18:37:10 +00:00
|
|
|
if (append_mode) {
|
2001-08-24 08:55:00 +00:00
|
|
|
fprintf(f, "\n\n# ");
|
2001-08-13 18:37:10 +00:00
|
|
|
} else {
|
2001-08-24 08:55:00 +00:00
|
|
|
fprintf(f, "# This file was generated with subfont for Mplayer.\n"
|
|
|
|
"# Subfont by Artur Zaprzala <zybi@fanthom.irc.pl>.\n\n");
|
2001-08-13 18:37:10 +00:00
|
|
|
fprintf(f, "[info]\n");
|
2001-08-24 08:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f, "name 'Subtitle font for %s %s, \"%s%s%s\" face, size: %.1f pixels.'\n",
|
|
|
|
encoding_name,
|
|
|
|
unicode_desc ? "charset, Unicode encoding":"encoding",
|
2002-07-25 13:36:59 +00:00
|
|
|
face->family_name ? face->family_name : font_path,
|
2001-08-24 08:55:00 +00:00
|
|
|
face->style_name ? " ":"", face->style_name ? face->style_name:"",
|
|
|
|
ppem);
|
|
|
|
|
|
|
|
if (!append_mode) {
|
2001-08-21 14:00:57 +00:00
|
|
|
#ifdef NEW_DESC
|
|
|
|
fprintf(f, "descversion 2\n");
|
|
|
|
#else
|
2001-08-13 18:37:10 +00:00
|
|
|
fprintf(f, "descversion 1\n");
|
2001-08-21 14:00:57 +00:00
|
|
|
#endif
|
2001-08-13 18:37:10 +00:00
|
|
|
fprintf(f, "spacewidth %i\n", 2*padding + space_advance);
|
2001-08-21 14:00:57 +00:00
|
|
|
#ifndef NEW_DESC
|
2001-08-13 18:37:10 +00:00
|
|
|
fprintf(f, "charspace %i\n", -2*padding);
|
2001-08-21 14:00:57 +00:00
|
|
|
#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
|
2001-08-13 18:37:10 +00:00
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
fprintf(f, "\n[files]\n");
|
2001-08-13 18:39:23 +00:00
|
|
|
fprintf(f, "alpha %s-a.raw\n", encoding_name);
|
|
|
|
fprintf(f, "bitmap %s-b.raw\n", encoding_name);
|
2001-08-09 17:28:06 +00:00
|
|
|
fprintf(f, "\n[characters]\n");
|
|
|
|
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// render glyphs, compute bitmap size and [characters] section
|
|
|
|
glyphs = (FT_Glyph*)malloc(charset_size*sizeof(FT_Glyph*));
|
2001-08-13 18:37:10 +00:00
|
|
|
for (i= 0; i<charset_size; ++i) {
|
2001-08-21 14:00:57 +00:00
|
|
|
FT_GlyphSlot slot;
|
|
|
|
FT_ULong character, code;
|
|
|
|
FT_UInt glyph_index;
|
|
|
|
FT_BBox bbox;
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
character = charset[i];
|
|
|
|
code = charcodes[i];
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// get glyph index
|
2001-08-13 18:37:10 +00:00
|
|
|
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);
|
|
|
|
continue;
|
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// load glyph
|
2001-08-09 17:28:06 +00:00
|
|
|
error = FT_Load_Glyph(face, glyph_index, load_flags);
|
|
|
|
if (error) {
|
2001-08-21 14:00:57 +00:00
|
|
|
WARNING("FT_Load_Glyph 0x%02x (char 0x%02x|U+%04X) failed.", glyph_index, code, character);
|
2001-08-09 17:28:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
slot = face->glyph;
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// 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;
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-21 14:00:57 +00:00
|
|
|
glyphs[glyphs_count++] = (FT_Glyph)glyph;
|
|
|
|
|
|
|
|
#ifdef NEW_DESC
|
|
|
|
// max height
|
|
|
|
if (glyph->bitmap.rows > height) height = glyph->bitmap.rows;
|
|
|
|
|
|
|
|
// advance pen
|
|
|
|
pen_xa = pen_x + glyph->bitmap.width + 2*padding;
|
|
|
|
|
|
|
|
// font.desc
|
2001-08-24 08:55:00 +00:00
|
|
|
fprintf(f, "0x%04x %i %i %i %i %i %i;\tU+%04X|%c\n", unicode_desc ? character:code,
|
2001-08-21 14:00:57 +00:00
|
|
|
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);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance pen */
|
2001-08-21 14:00:57 +00:00
|
|
|
pen_xa = pen_x + f266ToInt(slot->advance.x) + 2*padding;
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
/* font.desc */
|
2001-08-24 08:55:00 +00:00
|
|
|
fprintf(f, "0x%04x %i %i;\tU+%04X|%c\n", unicode_desc ? character:code,
|
|
|
|
pen_x, // bitmap start
|
|
|
|
pen_xa-1, // bitmap end
|
|
|
|
character, code<' '||code>255 ? '.':code);
|
2001-08-21 14:00:57 +00:00
|
|
|
#endif
|
2001-08-13 18:37:10 +00:00
|
|
|
pen_x = ALIGN(pen_xa);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
width = pen_x;
|
2001-08-21 14:00:57 +00:00
|
|
|
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!");
|
2001-08-13 18:37:10 +00:00
|
|
|
height = ymax - ymin + 2*padding;
|
|
|
|
baseline = ymax + padding;
|
2001-08-21 14:00:57 +00:00
|
|
|
#endif
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// 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);
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
bbuffer = (unsigned char*)malloc(width*height);
|
|
|
|
if (bbuffer==NULL) ERROR("malloc failed.");
|
|
|
|
memset(bbuffer, 0, width*height);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
/* paste glyphs */
|
|
|
|
for (i= 0; i<glyphs_count; ++i) {
|
|
|
|
glyph = (FT_BitmapGlyph)glyphs[i];
|
|
|
|
#ifdef NEW_DESC
|
|
|
|
paste_bitmap(&glyph->bitmap,
|
|
|
|
pen_x + padding,
|
|
|
|
padding);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
/* advance pen */
|
|
|
|
pen_x += glyph->bitmap.width + 2*padding;
|
|
|
|
#else
|
|
|
|
paste_bitmap(&glyph->bitmap,
|
|
|
|
pen_x + padding + glyph->left,
|
|
|
|
baseline - glyph->top);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
/* advance pen */
|
2001-08-21 14:00:57 +00:00
|
|
|
pen_x += f1616ToInt(glyph->root.advance.x) + 2*padding;
|
|
|
|
#endif
|
2001-08-13 18:37:10 +00:00
|
|
|
pen_x = ALIGN(pen_x);
|
2001-08-21 14:00:57 +00:00
|
|
|
|
|
|
|
FT_Done_Glyph((FT_Glyph)glyph);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-21 14:00:57 +00:00
|
|
|
free(glyphs);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
error = FT_Done_FreeType(library);
|
2001-08-21 14:00:57 +00:00
|
|
|
if (error) ERROR("FT_Done_FreeType failed.");
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
|
|
|
/* decode from 'encoding' to unicode */
|
|
|
|
FT_ULong decode_char(char c) {
|
|
|
|
FT_ULong o;
|
|
|
|
char *inbuf = &c;
|
|
|
|
char *outbuf = (char*)&o;
|
|
|
|
int inbytesleft = 1;
|
|
|
|
int outbytesleft = sizeof(FT_ULong);
|
|
|
|
|
|
|
|
size_t count = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
|
|
|
|
|
2001-08-24 10:46:31 +00:00
|
|
|
/* convert unicode BigEndian -> MachineEndian */
|
|
|
|
o = be2me_32(o);
|
|
|
|
|
|
|
|
// if (count==-1) o = 0; // not OK, at least my iconv() returns E2BIG for all
|
|
|
|
if (outbytesleft!=0) o = 0;
|
2001-08-13 18:37:10 +00:00
|
|
|
|
|
|
|
/* we don't want control characters */
|
|
|
|
if (o>=0x7f && o<0xa0) o = 0;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
void prepare_charset() {
|
2001-08-13 18:37:10 +00:00
|
|
|
FILE *f;
|
|
|
|
FT_ULong i;
|
|
|
|
|
|
|
|
f = fopen(encoding, "r"); // try to read custom encoding
|
|
|
|
if (f==NULL) {
|
|
|
|
int count = 0;
|
|
|
|
// check if ucs-4 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 `%s', use iconv --list to list character sets known on your system.", encoding);
|
|
|
|
|
|
|
|
charset_size = 256 - first_char;
|
|
|
|
for (i = 0; i<charset_size; ++i) {
|
|
|
|
charcodes[count] = i+first_char;
|
|
|
|
charset[count] = decode_char(i+first_char);
|
|
|
|
//eprintf("%04X U%04X\n", charcodes[count], charset[count]);
|
|
|
|
if (charset[count]!=0) ++count;
|
|
|
|
}
|
|
|
|
charcodes[count] = charset[count] = 0; ++count;
|
|
|
|
charset_size = count;
|
|
|
|
|
|
|
|
iconv_close(cd);
|
|
|
|
} else {
|
|
|
|
unsigned int character, code;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
eprintf("Reading custom encoding from file '%s'.\n", encoding);
|
|
|
|
|
|
|
|
while ((count = fscanf(f, "%x%*[ \t]%x", &character, &code)) != EOF) {
|
|
|
|
if (charset_size==max_charset_size) {
|
|
|
|
WARNING("There is no place for more than %i characters. Use the source!", max_charset_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (count==0) ERROR("Unable to parse custom encoding file.");
|
|
|
|
if (character<32) continue; // skip control characters
|
|
|
|
charset[charset_size] = character;
|
|
|
|
charcodes[charset_size] = count==2 ? code : character;
|
|
|
|
++charset_size;
|
|
|
|
}
|
|
|
|
fclose(f);
|
2001-08-13 18:39:23 +00:00
|
|
|
// encoding = basename(encoding);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
if (charset_size==0) ERROR("No characters to render!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// general outline
|
|
|
|
void outline(
|
|
|
|
unsigned char *s,
|
|
|
|
unsigned char *t,
|
|
|
|
int width,
|
|
|
|
int height,
|
2002-08-16 16:35:43 +00:00
|
|
|
unsigned char *m,
|
2001-08-13 18:37:10 +00:00
|
|
|
int r,
|
2002-08-16 16:35:43 +00:00
|
|
|
int mwidth,
|
|
|
|
int msize) {
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-13 19:33:41 +00:00
|
|
|
int x, y;
|
2002-08-15 22:45:34 +00:00
|
|
|
#if 1
|
|
|
|
for (y = 0; y<height; y++) {
|
|
|
|
for (x = 0; x<width; x++) {
|
|
|
|
const int src= s[x];
|
|
|
|
if(src==0) continue;
|
|
|
|
#if 0
|
|
|
|
if(src==255 && x>0 && y>0 && x+1<width && y+1<height
|
|
|
|
&& s[x-1]==255 && s[x+1]==255 && s[x-width]==255 && s[x+width]==255){
|
|
|
|
t[x + y*width]=255;
|
|
|
|
}else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
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;
|
2002-08-16 16:06:02 +00:00
|
|
|
register unsigned char *dstp= t + (y1+y-r)* width + x-r;
|
2002-08-16 16:35:43 +00:00
|
|
|
//register int *mp = m + y1 *mwidth;
|
|
|
|
register unsigned char *mp= m + msize*src + y1*mwidth;
|
2002-08-15 22:45:34 +00:00
|
|
|
int my;
|
|
|
|
|
|
|
|
for(my= y1; my<y2; my++){
|
2002-08-16 16:06:02 +00:00
|
|
|
// unsigned char *dstp= t + (my+y-r)* width + x-r;
|
|
|
|
// int *mp = m + my *mwidth;
|
|
|
|
register int mx;
|
2002-08-15 22:45:34 +00:00
|
|
|
for(mx= x1; mx<x2; mx++){
|
2002-08-16 16:35:43 +00:00
|
|
|
// const int tmp= (src*mp[mx] + 128)>>8;
|
|
|
|
// if(dstp[mx] < tmp) dstp[mx]= tmp;
|
|
|
|
if(dstp[mx] < mp[mx]) dstp[mx]= mp[mx];
|
2002-08-15 22:45:34 +00:00
|
|
|
}
|
2002-08-16 16:06:02 +00:00
|
|
|
dstp+=width;
|
|
|
|
mp+=mwidth;
|
2002-08-15 22:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s+= width;
|
|
|
|
}
|
|
|
|
#else
|
2001-08-13 18:37:10 +00:00
|
|
|
for (y = 0; y<height; ++y) {
|
|
|
|
for (x = 0; x<width; ++x, ++s, ++t) {
|
2002-08-15 20:50:13 +00:00
|
|
|
//if(s[0]>=192) printf("%d\n",s[0]);
|
|
|
|
if(s[0]!=255){
|
2001-08-13 18:37:10 +00:00
|
|
|
unsigned max = 0;
|
|
|
|
unsigned *mrow = m + r;
|
|
|
|
unsigned char *srow = s -r*width;
|
2001-08-13 19:33:41 +00:00
|
|
|
int x1=(x<r)?-x:-r;
|
|
|
|
int x2=(x+r>=width)?(width-x-1):r;
|
|
|
|
int my;
|
2001-08-13 18:37:10 +00:00
|
|
|
|
|
|
|
for (my = -r; my<=r; ++my, srow+= width, mrow+= mwidth) {
|
2001-08-13 19:33:41 +00:00
|
|
|
int mx;
|
2001-08-13 18:37:10 +00:00
|
|
|
if (y+my < 0) continue;
|
|
|
|
if (y+my >= height) break;
|
|
|
|
|
2001-08-13 19:33:41 +00:00
|
|
|
for (mx = x1; mx<=x2; ++mx) {
|
|
|
|
unsigned v = srow[mx] * mrow[mx];
|
2001-08-13 18:37:10 +00:00
|
|
|
if (v>max) max = v;
|
|
|
|
}
|
|
|
|
}
|
2002-08-15 20:50:13 +00:00
|
|
|
// if(!max) *t = 0; else
|
2001-08-13 18:37:10 +00:00
|
|
|
*t = (max + base/2) / base;
|
2002-08-15 20:50:13 +00:00
|
|
|
} else
|
|
|
|
*t = 255;
|
2001-08-13 18:37:10 +00:00
|
|
|
}
|
2001-08-09 17:29:25 +00:00
|
|
|
}
|
2002-08-15 22:45:34 +00:00
|
|
|
#endif
|
2001-08-13 18:37:10 +00:00
|
|
|
}
|
|
|
|
|
2001-08-09 17:29:25 +00:00
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
// 1 pixel outline
|
|
|
|
void outline1(
|
|
|
|
unsigned char *s,
|
|
|
|
unsigned char *t,
|
|
|
|
int width,
|
|
|
|
int height) {
|
|
|
|
|
|
|
|
int x, y, mx, my;
|
|
|
|
|
|
|
|
for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
|
|
|
|
for (y = 1; y<height-1; ++y) {
|
|
|
|
*t++ = *s++;
|
|
|
|
for (x = 1; x<width-1; ++x, ++s, ++t) {
|
|
|
|
unsigned v = (
|
|
|
|
s[-1-width]+
|
|
|
|
s[-1+width]+
|
|
|
|
s[+1-width]+
|
|
|
|
s[+1+width]
|
|
|
|
)/2 + (
|
|
|
|
s[-1]+
|
|
|
|
s[+1]+
|
|
|
|
s[-width]+
|
|
|
|
s[+width]+
|
|
|
|
s[0]
|
|
|
|
);
|
|
|
|
*t = v>maxcolor ? maxcolor : v;
|
|
|
|
}
|
|
|
|
*t++ = *s++;
|
|
|
|
}
|
|
|
|
for (x = 0; x<width; ++x, ++s, ++t) *t = *s;
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// gaussian blur
|
2001-08-13 18:37:10 +00:00
|
|
|
void blur(
|
2001-08-21 14:00:57 +00:00
|
|
|
unsigned char *buffer,
|
2002-08-16 22:36:54 +00:00
|
|
|
unsigned short *tmp2,
|
2001-08-13 18:37:10 +00:00
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int *m,
|
2002-08-16 22:36:54 +00:00
|
|
|
int *m2,
|
2001-08-13 18:37:10 +00:00
|
|
|
int r,
|
|
|
|
int mwidth,
|
|
|
|
unsigned volume) {
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-13 19:33:41 +00:00
|
|
|
int x, y;
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2002-08-16 22:39:02 +00:00
|
|
|
#if 1
|
2002-08-16 22:36:54 +00:00
|
|
|
unsigned char *s = buffer;
|
|
|
|
unsigned short *t = tmp2+1;
|
|
|
|
for(y=0; y<height; y++){
|
2002-08-18 08:21:24 +00:00
|
|
|
memset(t-1, 0, (width+1)*sizeof(short));
|
2002-08-16 22:36:54 +00:00
|
|
|
// for(x=0; x<width+1; x++)
|
|
|
|
// t[x]= 128;
|
|
|
|
|
|
|
|
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+= width;
|
|
|
|
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+= width;
|
|
|
|
t+= width + 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
unsigned char *tmp = (unsigned char*)tmp2;
|
2001-08-21 14:00:57 +00:00
|
|
|
unsigned char *s = buffer - r;
|
|
|
|
unsigned char *t = tmp;
|
2002-08-16 21:02:50 +00:00
|
|
|
|
|
|
|
int *m_end=m+256*mwidth;
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
for (y = 0; y<height; ++y) {
|
|
|
|
for (x = 0; x<width; ++x, ++s, ++t) {
|
2002-08-16 21:02:50 +00:00
|
|
|
unsigned sum = 65536/2;
|
2001-08-21 14:00:57 +00:00
|
|
|
int x1 = (x<r) ? r-x:0;
|
|
|
|
int x2 = (x+r>=width) ? (r+width-x):mwidth;
|
2002-08-16 21:02:50 +00:00
|
|
|
unsigned* mp = m + 256*x1;
|
2001-08-21 14:00:57 +00:00
|
|
|
int mx;
|
2002-08-16 22:36:54 +00:00
|
|
|
|
2002-08-16 21:02:50 +00:00
|
|
|
for (mx = x1; mx<x2; ++mx, mp+=256) sum+= mp[s[mx]];
|
|
|
|
*t = sum>>16;
|
2001-08-21 14:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-16 21:02:50 +00:00
|
|
|
|
2002-08-16 22:36:54 +00:00
|
|
|
tmp -= r*width;
|
2001-08-21 14:00:57 +00:00
|
|
|
for (x = 0; x<width; ++x, ++tmp, ++buffer) {
|
2002-08-16 21:02:50 +00:00
|
|
|
int y1max=(r<height)?r:height;
|
|
|
|
int y2min=height-r;
|
|
|
|
if(y2min<y1max) y2min=y1max;
|
2001-08-21 14:00:57 +00:00
|
|
|
s = tmp;
|
|
|
|
t = buffer;
|
2002-08-16 21:02:50 +00:00
|
|
|
#if 0
|
2001-08-21 14:00:57 +00:00
|
|
|
for (y = 0; y<height; ++y, s+= width, t+= width) {
|
2002-08-16 21:02:50 +00:00
|
|
|
unsigned sum = 65536/2;
|
2001-08-21 14:00:57 +00:00
|
|
|
int y1 = (y<r) ? r-y:0;
|
|
|
|
int y2 = (y+r>=height) ? (r+height-y):mwidth;
|
2002-08-16 21:02:50 +00:00
|
|
|
register unsigned *mp = m + 256*y1;
|
|
|
|
register unsigned char *smy = s + y1*width;
|
2001-08-13 19:33:41 +00:00
|
|
|
int my;
|
2002-08-16 21:02:50 +00:00
|
|
|
for (my = y1; my<y2; ++my, smy+= width, mp+=256)
|
|
|
|
sum+= mp[*smy];
|
|
|
|
*t = sum>>16;
|
2001-08-21 14:00:57 +00:00
|
|
|
}
|
2002-08-16 21:02:50 +00:00
|
|
|
#else
|
|
|
|
// pass 1: 0..r
|
|
|
|
for (y = 0; y<y1max; ++y, s+= width, t+= width) {
|
|
|
|
unsigned sum = 65536/2;
|
|
|
|
int y1 = r-y;
|
|
|
|
int my = y1;
|
|
|
|
int y2 = (y+r>=height) ? (r+height-y):mwidth;
|
|
|
|
unsigned char *smy = s + y1*width;
|
|
|
|
unsigned* mp = m + 256*y1;
|
|
|
|
for (; my<y2; ++my, smy+= width, mp+=256) sum+=mp[*smy];
|
|
|
|
*t = sum>>16;
|
|
|
|
}
|
|
|
|
// pass 2: r..(height-r)
|
|
|
|
for (; y<y2min; ++y, s+= width, t+= width) {
|
|
|
|
unsigned sum = 65536/2;
|
|
|
|
unsigned char *smy = s;
|
|
|
|
unsigned* mp = m;
|
|
|
|
// int my=0;
|
|
|
|
// for (; my<mwidth; ++my, smy+=width, mp+=256) sum+=mp[*smy];
|
|
|
|
for (; mp<m_end; smy+=width, mp+=256) sum+=mp[*smy];
|
|
|
|
*t = sum>>16;
|
|
|
|
}
|
|
|
|
// pass 3: (height-r)..height
|
|
|
|
for (; y<height; ++y, s+= width, t+= width) {
|
|
|
|
unsigned sum = 65536/2;
|
|
|
|
int y2 = r+height-y;
|
|
|
|
unsigned char *smy = s;
|
|
|
|
unsigned* mp = m;
|
|
|
|
int my=0;
|
|
|
|
for (; my<y2; ++my, smy+= width, mp+=256) sum+=mp[*smy];
|
|
|
|
*t = sum>>16;
|
|
|
|
}
|
|
|
|
#endif
|
2001-08-21 14:00:57 +00:00
|
|
|
}
|
2002-08-16 22:36:54 +00:00
|
|
|
#endif
|
2001-08-21 14:00:57 +00:00
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
// 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;
|
2001-08-13 19:33:41 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
for (my = 0; my<w; ++my) {
|
|
|
|
for (mx = 0; mx<w; ++mx) {
|
|
|
|
m[mx+my*w] = (unsigned)(exp(A * ((mx-r)*(mx-r)+(my-r)*(my-r))) * base + .5);
|
|
|
|
volume+= m[mx+my*w];
|
|
|
|
if (DEBUG) eprintf("%3i ", m[mx+my*w]);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-21 14:00:57 +00:00
|
|
|
if (DEBUG) eprintf("\n");
|
|
|
|
}
|
|
|
|
if (DEBUG) {
|
|
|
|
eprintf("A= %f\n", A);
|
|
|
|
eprintf("volume: %i; exact: %.0f; volume/exact: %.6f\n\n", volume, -M_PI*base/A, volume/(-M_PI*base/A));
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-21 14:00:57 +00:00
|
|
|
return volume;
|
2001-08-13 18:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void alpha() {
|
2002-08-15 20:50:13 +00:00
|
|
|
unsigned int ttime;
|
2001-08-13 18:37:10 +00:00
|
|
|
int const g_r = ceil(radius);
|
|
|
|
int const o_r = ceil(thickness);
|
|
|
|
int const g_w = 2*g_r+1; // matrix size
|
|
|
|
int const o_w = 2*o_r+1; // matrix size
|
2002-08-16 16:35:43 +00:00
|
|
|
int const o_size = o_w * o_w;
|
2001-08-13 18:37:10 +00:00
|
|
|
double const A = log(1.0/base)/(radius*radius*2);
|
2002-08-16 22:36:54 +00:00
|
|
|
double volume_factor=0.0;
|
|
|
|
double volume_diff;
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
int mx, my, i;
|
2001-08-13 18:37:10 +00:00
|
|
|
unsigned volume = 0; // volume under Gaussian area is exactly -pi*base/A
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
unsigned *g = (unsigned*)malloc(g_w * sizeof(unsigned));
|
2002-08-16 21:02:50 +00:00
|
|
|
unsigned *gt = (unsigned*)malloc(256 * g_w * sizeof(unsigned));
|
2002-08-16 22:36:54 +00:00
|
|
|
unsigned *gt2 = (unsigned*)malloc(256 * g_w * sizeof(unsigned));
|
2001-08-13 18:37:10 +00:00
|
|
|
unsigned *om = (unsigned*)malloc(o_w*o_w * sizeof(unsigned));
|
2002-08-16 16:35:43 +00:00
|
|
|
unsigned char *omt = malloc(o_size*256);
|
|
|
|
unsigned char *omtp = omt;
|
2002-08-16 22:36:54 +00:00
|
|
|
unsigned short *tmp = malloc((width+1)*height*sizeof(short));
|
2002-08-16 16:35:43 +00:00
|
|
|
|
2002-08-16 22:36:54 +00:00
|
|
|
if (g==NULL || gt==NULL || gt2==NULL || om==NULL || omt==NULL) ERROR("malloc failed.");
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2002-08-16 22:36:54 +00:00
|
|
|
// gaussian curve with volume = 256
|
|
|
|
for (volume_diff=10000000; volume_diff>0.0000001; volume_diff*=0.5){
|
|
|
|
volume_factor+= volume_diff;
|
|
|
|
volume=0;
|
|
|
|
for (i = 0; i<g_w; ++i) {
|
|
|
|
g[i] = (unsigned)(exp(A * (i-g_r)*(i-g_r)) * volume_factor + .5);
|
|
|
|
volume+= g[i];
|
|
|
|
}
|
|
|
|
if(volume>256) volume_factor-= volume_diff;
|
|
|
|
}
|
|
|
|
volume=0;
|
2001-08-21 14:00:57 +00:00
|
|
|
for (i = 0; i<g_w; ++i) {
|
2002-08-16 22:36:54 +00:00
|
|
|
g[i] = (unsigned)(exp(A * (i-g_r)*(i-g_r)) * volume_factor + .5);
|
2001-08-21 14:00:57 +00:00
|
|
|
volume+= g[i];
|
|
|
|
if (DEBUG) eprintf("%3i ", g[i]);
|
2001-08-13 18:37:10 +00:00
|
|
|
}
|
2002-08-16 22:36:54 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
//volume *= volume;
|
|
|
|
if (DEBUG) eprintf("\n");
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2002-08-16 21:02:50 +00:00
|
|
|
// gauss table:
|
|
|
|
for(mx=0;mx<g_w;mx++){
|
2002-08-16 22:36:54 +00:00
|
|
|
for(i=0;i<256;i++){
|
2002-08-16 21:02:50 +00:00
|
|
|
gt[256*mx+i] = (i*g[mx]*65536+(volume/2))/volume;
|
2002-08-16 22:36:54 +00:00
|
|
|
gt2[mx+i*g_w] = i*g[mx];
|
|
|
|
}
|
2002-08-16 21:02:50 +00:00
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
/* outline matrix */
|
|
|
|
for (my = 0; my<o_w; ++my) {
|
|
|
|
for (mx = 0; mx<o_w; ++mx) {
|
|
|
|
// antialiased circle would be perfect here, but this one is good enough
|
|
|
|
double d = thickness + 1 - sqrt((mx-o_r)*(mx-o_r)+(my-o_r)*(my-o_r));
|
|
|
|
om[mx+my*o_w] = d>=1 ? base : d<=0 ? 0 : (d*base + .5);
|
|
|
|
if (DEBUG) eprintf("%3i ", om[mx+my*o_w]);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
if (DEBUG) eprintf("\n");
|
2001-08-09 17:29:25 +00:00
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
if (DEBUG) eprintf("\n");
|
|
|
|
|
2002-08-16 16:35:43 +00:00
|
|
|
// outline table:
|
|
|
|
for(i=0;i<256;i++){
|
|
|
|
for(mx=0;mx<o_size;mx++) *(omtp++) = (i*om[mx] + (base/2))/base;
|
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer();
|
2001-08-13 19:33:41 +00:00
|
|
|
if(thickness==1.0)
|
2001-08-21 14:00:57 +00:00
|
|
|
outline1(bbuffer, abuffer, width, height); // FAST solid 1 pixel outline
|
2001-08-13 19:33:41 +00:00
|
|
|
else
|
2002-08-16 16:35:43 +00:00
|
|
|
outline(bbuffer, abuffer, width, height, omt, o_r, o_w, o_size); // solid outline
|
2001-08-21 14:00:57 +00:00
|
|
|
//outline(bbuffer, abuffer, width, height, gm, g_r, g_w); // Gaussian outline
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer()-ttime;
|
|
|
|
printf("outline: %7d us\n",ttime);
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer();
|
2002-08-16 21:02:50 +00:00
|
|
|
// blur(abuffer, bbuffer, width, height, g, g_r, g_w, volume);
|
2002-08-16 22:36:54 +00:00
|
|
|
blur(abuffer, tmp, width, height, gt, gt2, g_r, g_w, volume);
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer()-ttime;
|
|
|
|
printf("gauss: %7d us\n",ttime);
|
2001-08-09 20:07:45 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
free(g);
|
2001-08-13 18:37:10 +00:00
|
|
|
free(om);
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
void usage() {
|
2002-03-03 19:18:42 +00:00
|
|
|
printf("Usage: %s [--outdir dir] [--append] [--unicode] [--blur b] [--outline o] encoding ppem font\n", command);
|
2001-08-13 18:37:10 +00:00
|
|
|
printf("\n"
|
2001-08-09 17:28:06 +00:00
|
|
|
" Program creates 3 files: font.desc, <encoding>-a.raw, <encoding>-b.raw.\n"
|
|
|
|
"\n"
|
2002-03-03 19:18:42 +00:00
|
|
|
" --outdir output directory to place files.\n"
|
2001-08-24 08:55:00 +00:00
|
|
|
" --append append results to existing font.desc, suppress info header.\n"
|
|
|
|
" --unicode use Unicode in font.desc. This will work with -utf8 option of mplayer.\n"
|
2001-08-13 18:37:10 +00:00
|
|
|
" --blur b specify blur radius, float.\n"
|
|
|
|
" --outline o specify outline thickness, float.\n"
|
|
|
|
" encoding must be an 8 bit encoding, like iso-8859-2, or path to custom encoding file (see README).\n"
|
|
|
|
" To list encodings available on your system use iconv --list.\n"
|
2001-08-24 08:55:00 +00:00
|
|
|
" ppem Font size in pixels (default 24), float.\n"
|
2001-08-13 18:37:10 +00:00
|
|
|
" font Font file path. Any format supported by the freetype library (*.ttf, *.pfb, ...).\n"
|
2001-08-09 17:28:06 +00:00
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
void parse_args(int argc, char **argv) {
|
2001-08-13 18:37:10 +00:00
|
|
|
int i, a = 0;
|
2001-08-09 17:28:06 +00:00
|
|
|
double d;
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
command = strrchr(argv[a], '/');
|
|
|
|
if (command==NULL) command = argv[a];
|
2001-08-09 17:28:06 +00:00
|
|
|
else ++command;
|
2001-08-13 18:37:10 +00:00
|
|
|
++a; --argc;
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2002-03-03 19:18:42 +00:00
|
|
|
if (argc>=1 && strcmp(argv[a], "--outdir")==0) {
|
|
|
|
++a; --argc;
|
|
|
|
if (argc==0) usage();
|
|
|
|
|
|
|
|
outdir = strdup(argv[a]);
|
|
|
|
++a; --argc;
|
|
|
|
}
|
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
if (argc>=1 && strcmp(argv[a], "--append")==0) {
|
2001-08-13 18:37:10 +00:00
|
|
|
append_mode = 1;
|
|
|
|
++a; --argc;
|
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
if (argc>=1 && strcmp(argv[a], "--unicode")==0) {
|
|
|
|
unicode_desc = 1;
|
|
|
|
++a; --argc;
|
|
|
|
}
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
if (argc>=1 && strcmp(argv[a], "--blur")==0) {
|
2001-08-13 18:37:10 +00:00
|
|
|
++a; --argc;
|
|
|
|
if (argc==0) usage();
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
d = atof(argv[a]);
|
|
|
|
if (d>=0 && d<20) radius = d;
|
|
|
|
else WARNING("using default blur radius.");
|
|
|
|
++a; --argc;
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
if (argc>=1 && strcmp(argv[a], "--outline")==0) {
|
2001-08-13 18:37:10 +00:00
|
|
|
++a; --argc;
|
|
|
|
if (argc==0) usage();
|
|
|
|
|
|
|
|
d = atof(argv[a]);
|
|
|
|
if (d>=0 && d<20) thickness = d;
|
|
|
|
else WARNING("using default outline thickness.");
|
|
|
|
++a; --argc;
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
2001-08-13 18:37:10 +00:00
|
|
|
|
|
|
|
if (argc<3) usage();
|
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
// encoding
|
2001-08-13 18:37:10 +00:00
|
|
|
if (argv[a][0]!=0)
|
|
|
|
encoding = argv[a];
|
2001-08-13 18:39:23 +00:00
|
|
|
encoding_name = strrchr(encoding, '/');
|
|
|
|
if (!encoding_name) encoding_name=encoding;
|
|
|
|
else ++encoding_name;
|
2001-08-13 18:37:10 +00:00
|
|
|
++a; --argc;
|
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
// ppem
|
|
|
|
d = atof(argv[a]);
|
|
|
|
if (d>2.) ppem = d;
|
2001-08-13 18:37:10 +00:00
|
|
|
++a; --argc;
|
|
|
|
|
2001-08-24 08:55:00 +00:00
|
|
|
// font
|
2001-08-13 18:37:10 +00:00
|
|
|
font_path = argv[a];
|
|
|
|
++a; --argc;
|
2001-08-09 17:28:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
|
2001-08-09 17:28:06 +00:00
|
|
|
int main(int argc, char **argv) {
|
2002-08-15 20:50:13 +00:00
|
|
|
unsigned int ttime;
|
2001-08-09 17:28:06 +00:00
|
|
|
parse_args(argc, argv);
|
2001-08-21 14:00:57 +00:00
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
padding = ceil(radius) + ceil(thickness);
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer();
|
2001-08-09 17:28:06 +00:00
|
|
|
prepare_charset();
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer()-ttime;
|
|
|
|
printf("charset: %7d us\n",ttime);
|
2001-08-21 14:00:57 +00:00
|
|
|
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer();
|
2001-08-09 17:28:06 +00:00
|
|
|
render();
|
2002-08-15 20:50:13 +00:00
|
|
|
ttime=GetTimer()-ttime;
|
|
|
|
printf("render: %7d us\n",ttime);
|
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
write_bitmap(bbuffer, 'b');
|
|
|
|
|
|
|
|
abuffer = (unsigned char*)malloc(width*height);
|
|
|
|
if (abuffer==NULL) ERROR("malloc failed.");
|
2001-08-13 18:37:10 +00:00
|
|
|
alpha();
|
2001-08-21 14:00:57 +00:00
|
|
|
write_bitmap(abuffer, 'a');
|
2001-08-09 17:28:06 +00:00
|
|
|
|
2001-08-21 14:00:57 +00:00
|
|
|
free(bbuffer);
|
2001-08-09 17:28:06 +00:00
|
|
|
free(abuffer);
|
|
|
|
|
2001-08-13 18:37:10 +00:00
|
|
|
// fflush(stderr);
|
2001-08-09 17:28:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|