new parameter -s textonly
[swftools.git] / lib / filters / vectors_to_glyphs.c
1 /* vectors_to_fonts.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 <math.h>
24 #include "../gfxfilter.h"
25 #include "../gfxtools.h"
26 #include "../gfxpoly.h"
27 #include "../gfxfont.h"
28 #include "../types.h"
29 #include "../mem.h"
30 #include "../q.h"
31
32 typedef struct _vglyph {
33     gfxglyph_t glyph;
34     struct _vglyph*next;
35 } vglyph_t;
36
37 typedef struct _internal {
38     vglyph_t*first_glyph;
39     vglyph_t*last_glyph;
40     gfxfont_t*font;
41     char first;
42     int glyph_nr;
43 } internal_t;
44
45 #define FONT_SIZE 1024.0
46
47 static void pass1_fill(gfxfilter_t*f, gfxline_t*line, gfxcolor_t*color, gfxdevice_t*out)
48 {
49     internal_t*i = (internal_t*)f->internal;
50     gfxbbox_t bbox = gfxline_getbbox(line);
51     double height = bbox.ymax - bbox.ymin;
52     double width = bbox.xmax - bbox.xmin;
53     double size = fmax(width,height);
54     if(fabs(size) < 1e-3) return;
55     double scale = FONT_SIZE/size;
56     gfxmatrix_t m = {scale, 0,     -bbox.xmin*scale,
57                      0,     scale, -bbox.ymax*scale};
58     line = gfxline_clone(line);
59     gfxline_transform(line, &m);
60     line = gfxline_restitch(line);
61     vglyph_t*v = rfx_calloc(sizeof(vglyph_t));
62     v->glyph.line = line;
63     v->glyph.advance = width*scale;
64     v->glyph.unicode = 32+i->glyph_nr;
65
66     i->last_glyph->next = v;
67     i->last_glyph = v;
68     
69     gfxmatrix_t m2 = {1.0/scale, 0,         bbox.xmin,
70                       0,         1.0/scale, bbox.ymax};
71     out->drawchar(out, 0, i->glyph_nr, color, &m2);
72
73     i->glyph_nr++;
74 }
75 static void pass1_stroke(gfxfilter_t*f, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit, gfxdevice_t*out)
76 {
77     return;
78     internal_t*i = (internal_t*)f->internal;
79     gfxpoly_t*poly = gfxpoly_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID);
80     gfxline_t*line2 = gfxline_from_gfxpoly_with_direction(poly);
81     pass1_fill(f, line2, color, out);
82     gfxline_free(line2);
83     gfxpoly_destroy(poly);
84 }
85
86 static void make_font(internal_t*i)
87 {
88     i->font = rfx_calloc(sizeof(gfxfont_t));
89     i->font->id = "SWFTools-Vector-Magic";
90     i->font->num_glyphs = i->glyph_nr;
91     i->font->glyphs = malloc(sizeof(gfxglyph_t)*i->font->num_glyphs);
92     i->font->ascent = FONT_SIZE;
93     i->font->descent = 0;
94     int pos = 0;
95     vglyph_t*g = i->first_glyph;
96     while(g) {
97         i->font->glyphs[pos] = g->glyph;
98         pos++;
99         g = g->next;
100     }
101     gfxfont_fix_unicode(i->font);
102 }
103
104 static void pass2_drawchar(gfxfilter_t*f, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix, gfxdevice_t*out)
105 {
106     internal_t*i = (internal_t*)f->internal;
107     if(i->first) {
108         make_font(i);
109         out->addfont(out, i->font);
110         i->first = 0;
111     }
112     if(!font) {
113         font = i->font;
114         
115         gfxcolor_t red = {255,254,1,1};
116         int x;
117         gfxmatrix_t m = {1.0,  0,0,
118                          0,  1.0,0};
119         out->drawchar(out, font, 1, &red, &m);
120
121         /*gfxmatrix_t m2 = {1.0,  0,600,
122                          0,  1.0,600};
123         out->drawchar(out, font, 1, &red, &m2);*/
124         gfxline_t*line = gfxline_makerectangle(0, 0, 1, 1);
125         gfximage_t img;
126         img.data = color;
127         img.width = 1;
128         img.height = 1;
129         out->fillbitmap(out, line, &img, &m, 0);
130     }
131     out->drawchar(out, font, glyphnr, color, matrix);
132 }
133
134 void gfxtwopassfilter_vectors_to_glyphs_init(gfxtwopassfilter_t*f)
135 {
136     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
137
138     memset(f, 0, sizeof(gfxtwopassfilter_t));
139     f->type = gfxfilter_twopass;
140
141     f->pass1.name = "vectors to glyphs pass 1";
142     f->pass1.fill = pass1_fill;
143     f->pass1.stroke = pass1_stroke;
144     f->pass1.internal = i;
145
146     f->pass2.name = "vectors to glyphs pass 2";
147     f->pass2.drawchar = pass2_drawchar;
148     f->pass2.internal = i;
149
150     i->first = 1;
151     
152     i->first_glyph = rfx_calloc(sizeof(vglyph_t));
153     i->first_glyph->glyph.line = 0;
154     i->first_glyph->glyph.advance = FONT_SIZE;
155     i->first_glyph->glyph.unicode = 32;
156     i->last_glyph = rfx_calloc(sizeof(vglyph_t));
157     // dummy char
158     i->first_glyph->next = i->last_glyph;
159     i->last_glyph->glyph.line = 0;
160     i->last_glyph->glyph.advance = FONT_SIZE/8;
161     i->last_glyph->glyph.unicode = 33;
162     i->glyph_nr = 2;
163 }
164
165