X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Ffilters%2Fremove_font_transforms.c;h=c94ee94976d73021731b71acf603df71a6ff8577;hp=a2d7c445b5dfb42b9ae576568e42c2f9263b3ff8;hb=a51d4f82e57c96baa125d0937870d1e4896d6ee3;hpb=7fb4a4ac393f19a0b8a8998a2f1deac88c97eda0 diff --git a/lib/filters/remove_font_transforms.c b/lib/filters/remove_font_transforms.c index a2d7c44..c94ee94 100644 --- a/lib/filters/remove_font_transforms.c +++ b/lib/filters/remove_font_transforms.c @@ -99,22 +99,22 @@ void matrix_convert(gfxmatrix_t*in, const char*id, mymatrix_t*out, gfxmatrix_t*s { 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; } out->m00 = in->m00 / l; - out->m01 = in->m01 / l; out->m10 = in->m10 / l; - out->m11 = in->m11 / l; + out->m01 = -in->m01 / l; + out->m11 = -in->m11 / l; out->id = (char*)id; if(scalematrix) { scalematrix->m00 = l; scalematrix->m01 = 0; scalematrix->m10 = 0; - scalematrix->m11 = l; + scalematrix->m11 = -l; scalematrix->tx = in->tx; scalematrix->ty = in->ty; } @@ -129,6 +129,7 @@ typedef struct _transformedfont { gfxfont_t*font; mymatrix_t matrix; int*used; + double dx; } transformedfont_t; static transformedfont_t* transformedfont_new(gfxfont_t*orig, mymatrix_t*m) @@ -136,7 +137,12 @@ 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) + f->used[t]=1; //always preserve the space char in fonts + } return f; } @@ -144,7 +150,9 @@ 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"); + matrix_convert(matrix, font->id?font->id:"unknown", &m, 0); transformedfont_t*fd = dict_lookup(i->matrices, &m); if(!fd) { fd = transformedfont_new(font, &m); @@ -163,10 +171,10 @@ static void glyph_transform(gfxglyph_t*g, mymatrix_t*mm) m.m11 = mm->m11; m.tx = 0; m.ty = 0; + g->line = gfxline_clone(g->line); gfxline_transform(g->line, &m); - g->advance *= m.m00; - if(g->advance<0) - g->advance = 0; + if(m.m00>0) + g->advance *= m.m00; } static gfxresult_t* pass1_finish(gfxfilter_t*f, gfxdevice_t*out) @@ -195,11 +203,43 @@ static gfxresult_t* pass1_finish(gfxfilter_t*f, gfxdevice_t*out) count++; } } + + /* adjust the origin so that every character is to the + right of the origin */ + gfxbbox_t total = {0,0,0,0}; + double average_xmax = 0; + for(t=0;tglyphs[t].line; + gfxbbox_t b = gfxline_getbbox(line); + total = gfxbbox_expand_to_bbox(total, b); + } + if(count) + average_xmax /= count; + + fd->dx = -total.xmin; + + font->ascent = total.ymax; + font->descent = -total.ymin; + + for(t=0;tglyphs[t].line; + while(line) { + line->x += fd->dx; + line->sx += fd->dx; + line = line->next; + } + } gfxfont_fix_unicode(font); } return out->finish(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; @@ -213,8 +253,9 @@ 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); transformedfont_t*d = dict_lookup(i->matrices, &m); + scalematrix.tx -= d->dx*scalematrix.m00; out->drawchar(out, d->font, d->used[glyphnr], color, &scalematrix); } @@ -231,6 +272,7 @@ 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.internal = i;