added gfxfilter for merging fonts
[swftools.git] / lib / filters / one_big_font.c
1 /* remove_font_transform.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2010 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <memory.h>
23 #include "../gfxfilter.h"
24 #include "../gfxtools.h"
25 #include "../gfxfont.h"
26 #include "../types.h"
27 #include "../mem.h"
28
29 typedef struct _internal {
30     gfxfontlist_t*fonts;
31     gfxfont_t*font;
32     int num_glyphs;
33 } internal_t;
34
35 typedef struct _fontdata {
36     gfxfont_t*font;
37     int start;
38 } fontdata_t;
39
40 static void pass1_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix, gfxdevice_t*out)
41 {
42     internal_t*i = (internal_t*)f->internal;
43     fontdata_t*d = gfxfontlist_getuserdata(i->fonts, font->id);
44     if(!d) {
45         d = rfx_calloc(sizeof(fontdata_t));
46         d->font = font;
47         d->start = i->num_glyphs;
48         i->num_glyphs  += font->num_glyphs;
49         i->fonts = gfxfontlist_addfont2(i->fonts, font, d);
50     }
51     out->drawchar(out, font, glyphnr, color, matrix);
52 }
53 static gfxresult_t*pass1_finish(gfxfilter_t*f, gfxdevice_t*out)
54 {
55     internal_t*i = (internal_t*)f->internal;
56     gfxfont_t*font = i->font = rfx_calloc(sizeof(gfxfont_t));
57     font->id = strdup("onebigfont");
58     font->num_glyphs = i->num_glyphs;
59     font->glyphs = rfx_calloc(sizeof(gfxglyph_t)*i->num_glyphs);
60     gfxfontlist_t*l = i->fonts;
61     while(l) {
62         gfxfont_t*old = l->font;
63         fontdata_t*d = l->user;
64         memcpy(font->glyphs + d->start, old->glyphs, sizeof(gfxglyph_t)*old->num_glyphs);
65         if(old->ascent > font->ascent)
66             font->ascent = old->ascent;
67         if(old->descent > font->descent)
68             font->descent = old->descent;
69         l = l->next;
70     }
71     gfxfont_fix_unicode(font);
72     return out->finish(out);
73 }
74
75 static void pass2_addfont(gfxfilter_t*f, gfxfont_t*font, gfxdevice_t*out)
76 {
77     internal_t*i = (internal_t*)f->internal;
78     out->addfont(out, i->font);
79 }
80 static void pass2_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix, gfxdevice_t*out)
81 {
82     internal_t*i = (internal_t*)f->internal;
83     fontdata_t*d = gfxfontlist_getuserdata(i->fonts, font->id);
84     out->drawchar(out, i->font, glyphnr + d->start, color, matrix);
85 }
86 static gfxresult_t*pass2_finish(gfxfilter_t*f, gfxdevice_t*out)
87 {
88     internal_t*i = (internal_t*)f->internal;
89     // clean up
90     gfxfontlist_t*l = i->fonts;
91     while(l) {
92         free(l->user);l->user=0;
93         l=l->next;
94     }
95     gfxfontlist_free(i->fonts, 0);i->fonts=0;
96     return out->finish(out);
97 }
98
99 void gfxtwopassfilter_one_big_font_init(gfxtwopassfilter_t*f)
100 {
101     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
102     
103     memset(f, 0, sizeof(gfxtwopassfilter_t));
104     f->type = gfxfilter_twopass;
105
106     f->pass1.name = "filter \"one big font\" pass 1";
107     f->pass1.drawchar = pass1_drawchar;
108     f->pass1.finish = pass1_finish;
109     f->pass1.internal = i;
110
111     f->pass2.name = "filter \"one big font\" pass 2";
112     f->pass2.addfont = pass2_addfont;
113     f->pass2.drawchar = pass2_drawchar;
114     f->pass2.finish = pass2_finish;
115     f->pass2.internal = i;
116 }
117