X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Ffilters%2Fremove_font_transforms.c;h=c04d110683023301dd289cf572262360d5b5a0f1;hp=16d02add7576cf18e89786e49ae7a307d7f66d49;hb=0e93bb43429d78593cd45ae509e5114f6ab31a52;hpb=b9f1e7011f2ad4be624ab777a5091623a807b3d9 diff --git a/lib/filters/remove_font_transforms.c b/lib/filters/remove_font_transforms.c index 16d02ad..c04d110 100644 --- a/lib/filters/remove_font_transforms.c +++ b/lib/filters/remove_font_transforms.c @@ -27,10 +27,12 @@ #include "../types.h" #include "../mem.h" #include "../q.h" +#include "../log.h" typedef struct _mymatrix { float m00,m01,m10,m11; char*id; + unsigned char alpha; } mymatrix_t; static void* mymatrix_clone(const void*_m) { @@ -51,6 +53,7 @@ static unsigned int mymatrix_hash(const void*_m) { h = crc32_add_bytes(h, (char*)&m->m01, sizeof(m->m01)); h = crc32_add_bytes(h, (char*)&m->m10, sizeof(m->m10)); h = crc32_add_bytes(h, (char*)&m->m11, sizeof(m->m11)); + h = crc32_add_bytes(h, (char*)&m->alpha, 1); h = crc32_add_string(h, m->id); return h; } @@ -75,6 +78,7 @@ static char mymatrix_equals(const void*_m1, const void*_m2) { *(U32*)&m1->m01 == *(U32*)&m2->m01 && *(U32*)&m1->m10 == *(U32*)&m2->m10 && *(U32*)&m1->m11 == *(U32*)&m2->m11 && + m1->alpha == m2->alpha && !strcmp(m1->id, m2->id); } type_t mymatrix_type = { @@ -91,24 +95,25 @@ typedef struct _internal { #ifdef __GNUC__ -void __attribute__((noinline)) - matrix_convert(gfxmatrix_t*in, const char*id, mymatrix_t*out, gfxmatrix_t*scalematrix) +int __attribute__((noinline)) + matrix_convert(gfxmatrix_t*in, const char*id, mymatrix_t*out, gfxmatrix_t*scalematrix, unsigned char alpha) #else -void matrix_convert(gfxmatrix_t*in, const char*id, mymatrix_t*out, gfxmatrix_t*scalematrix) +int matrix_convert(gfxmatrix_t*in, const char*id, mymatrix_t*out, gfxmatrix_t*scalematrix, unsigned char alpha) #endif { double l1 = sqrt(in->m00 * in->m00 + in->m01 * in->m01); double l2 = sqrt(in->m10 * in->m10 + in->m11 * in->m11); - double l = l1+l2; + double l = (l1+l2)/2.0; if(l < 1e-10) { memset(out, 0, sizeof(*out)); - return; + return 0; } out->m00 = in->m00 / l; out->m10 = in->m10 / l; out->m01 = -in->m01 / l; out->m11 = -in->m11 / l; out->id = (char*)id; + out->alpha = alpha?1:0; if(scalematrix) { scalematrix->m00 = l; @@ -118,6 +123,7 @@ void matrix_convert(gfxmatrix_t*in, const char*id, mymatrix_t*out, gfxmatrix_t*s scalematrix->tx = in->tx; scalematrix->ty = in->ty; } + return 1; } typedef struct _matrixdata { @@ -129,7 +135,7 @@ typedef struct _transformedfont { gfxfont_t*font; mymatrix_t matrix; int*used; - double dx,dy; + double dx; } transformedfont_t; static transformedfont_t* transformedfont_new(gfxfont_t*orig, mymatrix_t*m) @@ -137,7 +143,15 @@ static transformedfont_t* transformedfont_new(gfxfont_t*orig, mymatrix_t*m) transformedfont_t*f = rfx_calloc(sizeof(transformedfont_t)); f->orig = orig; f->matrix = *m; - f->used = malloc(sizeof(f->used[0])*orig->num_glyphs); + f->used = rfx_calloc(sizeof(f->used[0])*orig->num_glyphs); + int t; + for(t=0;tnum_glyphs;t++) { + if(orig->glyphs[t].unicode==32 && + (!orig->glyphs[t].line || + !orig->glyphs[t].line->next || + !orig->glyphs[t].line->next->next)) + f->used[t]=1; //always preserve the space char in fonts + } return f; } @@ -145,7 +159,10 @@ static void pass1_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_ { internal_t*i = (internal_t*)f->internal; mymatrix_t m; - matrix_convert(matrix, font->id, &m, 0); + if(!font->id) + msg(" Font has no ID"); + if(!matrix_convert(matrix, font->id?font->id:"unknown", &m, 0, color->a)) + return; transformedfont_t*fd = dict_lookup(i->matrices, &m); if(!fd) { fd = transformedfont_new(font, &m); @@ -164,6 +181,8 @@ static void glyph_transform(gfxglyph_t*g, mymatrix_t*mm) m.m11 = mm->m11; m.tx = 0; m.ty = 0; + if(m.m00>0) + g->advance *= m.m00; g->line = gfxline_clone(g->line); gfxline_transform(g->line, &m); } @@ -190,7 +209,7 @@ static gfxresult_t* pass1_finish(gfxfilter_t*f, gfxdevice_t*out) if(fd->used[t]) { font->glyphs[count] = fd->orig->glyphs[t]; glyph_transform(&font->glyphs[count], &fd->matrix); - fd->used[t] = count; + fd->used[t] = count + 1; count++; } } @@ -203,26 +222,57 @@ static gfxresult_t* pass1_finish(gfxfilter_t*f, gfxdevice_t*out) gfxline_t*line = font->glyphs[t].line; gfxbbox_t b = gfxline_getbbox(line); total = gfxbbox_expand_to_bbox(total, b); - if(b.xmax > 0) - font->glyphs[t].advance = b.xmax; } if(count) average_xmax /= count; - fd->dx = -total.xmin; - fd->dy = 0; + fd->dx = 0;//-total.xmin; + + font->ascent = total.ymax; + font->descent = -total.ymin; - double adx = fd->dx>0?fd->dx:0; - for(t=0;tglyphs[t]; gfxline_t*line = font->glyphs[t].line; - font->glyphs[t].advance += adx; - while(line) { - line->x += fd->dx; - line->y += fd->dy; - line->sx += fd->dx; - line->sy += fd->dy; - line = line->next; + + if(fd->matrix.alpha) { + while(line) { + line->x += fd->dx; + line->sx += fd->dx; + line = line->next; + } + } else { + gfxline_free(g->line); + /* for OCR: remove the outlines of characters that are only + ever displayed with alpha=0 */ + g->line = (gfxline_t*)rfx_calloc(sizeof(gfxline_t)); + g->line->type = gfx_moveTo; + g->line->x = g->advance; + } + } + + if(fd->matrix.m00>0) { + /* subset kerning table */ + count = 0; + for(t=0;torig->kerning_size;t++) { + int char1 = fd->used[fd->orig->kerning[t].c1]-1; + int char2 = fd->used[fd->orig->kerning[t].c2]-1; + if(char1>=0 && char2>=0) { + count++; + } + } + font->kerning = malloc(sizeof(font->kerning[0])*count); + font->kerning_size = count; + count = 0; + for(t=0;torig->kerning_size;t++) { + int char1 = fd->used[fd->orig->kerning[t].c1]-1; + int char2 = fd->used[fd->orig->kerning[t].c2]-1; + if(char1>=0 && char2>=0) { + font->kerning[count].c1 = char1; + font->kerning[count].c2 = char2; + font->kerning[count].advance = fd->orig->kerning[t].advance * fd->matrix.m00; + count++; + } } } gfxfont_fix_unicode(font); @@ -230,9 +280,16 @@ static gfxresult_t* pass1_finish(gfxfilter_t*f, gfxdevice_t*out) return out->finish(out); } -static void pass2_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix, gfxdevice_t*out) +static void pass2_addfont(gfxfilter_t*f, gfxfont_t*font, gfxdevice_t*out) +{ + /* we throw away original fonts, and do the addfont() for the transformed + fonts in the first drawchar() */ +} + +static void pass2_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*_color, gfxmatrix_t*matrix, gfxdevice_t*out) { internal_t*i = (internal_t*)f->internal; + gfxcolor_t color = *_color; if(i->first) { i->first = 0; @@ -243,11 +300,30 @@ static void pass2_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_ mymatrix_t m; gfxmatrix_t scalematrix; - matrix_convert(matrix, font->id, &m, &scalematrix); + matrix_convert(matrix, font->id?font->id:"unknown", &m, &scalematrix, color.a); transformedfont_t*d = dict_lookup(i->matrices, &m); - scalematrix.tx -= d->dx; - scalematrix.ty -= d->dy; - out->drawchar(out, d->font, d->used[glyphnr], color, &scalematrix); + scalematrix.tx -= d->dx*scalematrix.m00; + + /* if this character is invisible (alpha=0), then we will have removed the + outline, so we make set the alpha color channel to "fully visible" again to allow + output devices to be more performant (transparency is expensive) */ + if(!m.alpha) + color.a = 255; + + out->drawchar(out, d->font, d->used[glyphnr]-1, &color, &scalematrix); +} + +static gfxresult_t* pass2_finish(gfxfilter_t*f, gfxdevice_t*out) +{ + internal_t*i = (internal_t*)f->internal; + DICT_ITERATE_DATA(i->matrices, transformedfont_t*, fd) { + if(fd->used) { + free(fd->used);fd->used=0; + } + free(fd); + } + dict_destroy(i->matrices);i->matrices=0; + return out->finish(out); } void gfxtwopassfilter_remove_font_transforms_init(gfxtwopassfilter_t*f) @@ -263,7 +339,9 @@ void gfxtwopassfilter_remove_font_transforms_init(gfxtwopassfilter_t*f) f->pass1.internal = i; f->pass2.name = "remove font transforms pass 2"; + f->pass2.addfont = pass2_addfont; f->pass2.drawchar = pass2_drawchar; + f->pass2.finish = pass2_finish; f->pass2.internal = i; i->matrices = dict_new2(&mymatrix_type);