Initial revision
[swftools.git] / pdf2swf / swfoutput_x11.cc
1 /* This file is only for testing purposes. it replaces swfoutput.cc, and dumps 
2    the information it gets into an X11 Window, not into an swf.
3
4    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
5
6    This file is distributed under the GPL, see file COPYING for details */
7
8 #include <string.h>
9 #include "swfoutput.h"
10 #include "kramm/xwindows.h"
11 #include "spline.h"
12 #include "log.h"
13
14 modexwindow*vga;
15
16 void inline pp(int x,int y)
17 {
18     if(x<0) return;
19     if(y<0) return;
20     vga->putpixel(x,y);
21 }
22
23 void circle(int mx,int my,int r)
24 {
25    int d=3-(2*r);
26    int x=0;
27    int y=r;
28
29
30    while(x<=y)
31    {
32     pp(mx+x,my+y);
33     pp(mx+x,my-y);
34     pp(mx-x,my+y);
35     pp(mx-x,my-y);
36     pp(mx+y,my+x);
37     pp(mx+y,my-x);
38     pp(mx-y,my+x);
39     pp(mx-y,my-x);
40
41     if(d<0)
42     {
43      d=d+(4*x)+6;
44     }
45     else
46     {y--;
47      d=d+4*(x-y)+10;
48     };
49     x++;
50    }
51 }
52
53 void qspline(plotxy p0,plotxy p1,plotxy p2)
54 {
55     double d;
56     //vga->setcolor(0x0000ff);
57     for(d=0.00;d<=1.00;d+=0.001) {
58         int x = (int)(p0.x * d*d + p1.x * 2*(1-d)*d + p2.x * (1-d)*(1-d));
59         int y = (int)(p0.y * d*d + p1.y * 2*(1-d)*d + p2.y * (1-d)*(1-d));
60         pp(x,y);
61     }
62 }
63 void transform (plotxy*p0,struct swfmatrix*m)
64 {
65     double x,y;
66     x = m->m11*p0->x+m->m12*p0->y;
67     y = m->m21*p0->x+m->m22*p0->y;
68     p0->x = x + m->m13;
69     p0->y = y + m->m23;
70 //    p0->x *= 3;
71 //    p0->y *= 3;
72 }
73
74 void spline(plotxy p0,plotxy p1,plotxy p2,plotxy p3,struct swfmatrix*m)
75 {
76     double d;
77     struct qspline q[16];
78     int num;
79     int t;
80     transform(&p0,m);
81     transform(&p1,m);
82     transform(&p2,m);
83     transform(&p3,m);
84     //vga->setcolor(0xffff00);
85     //vga->drawline(p3.x,p3.y,p2.x,p2.y);
86     //vga->drawline(p2.x,p2.y,p1.x,p1.y);
87     //vga->drawline(p1.x,p1.y,p0.x,p0.y);
88
89 /*  vga->setcolor(0x00ff00);
90     circle(p0.x,p0.y,5);
91     circle(p1.x,p1.y,5);
92     circle(p2.x,p2.y,5);
93     circle(p3.x,p3.y,5);*/
94
95 /*    vga->setcolor(0xff00ff);
96     for(d=0.00;d<1.00;d+=0.001) {
97         int x = (int)(p0.x * d*d*d + p1.x * 3*(1-d)*d*d + p2.x * 3*(1-d)*(1-d)*d + p3.x * (1-d)*(1-d)*(1-d));
98         int y = (int)(p0.y * d*d*d + p1.y * 3*(1-d)*d*d + p2.y * 3*(1-d)*(1-d)*d + p3.y * (1-d)*(1-d)*(1-d));
99         pp(x,y);
100     }*/
101
102     num = approximate(p0,p1,p2,p3,q);
103     for(t=0;t<num;t++)
104     {
105         qspline(q[t].start,q[t].control,q[t].end);
106     }
107 }
108 void line(plotxy p0, plotxy p1, struct swfmatrix*m)
109 {
110     transform(&p0,m);
111     transform(&p1,m);
112     vga->drawline((int)p0.x,(int)p0.y,(int)p1.x,(int)p1.y);
113 }
114
115 void swfoutput_drawpath(swfoutput*output, T1_OUTLINE*outline, struct swfmatrix*m)
116 {
117     double x=0,y=0;
118     double lastx=0,lasty=0;
119
120     vga->setcolor(0xffffff);
121     while (outline)
122     {
123         logf("<debug> Pathtype:%s",outline->type == T1_PATHTYPE_MOVE?"MOVE":
124                                                     (outline->type == T1_PATHTYPE_LINE?"LINE"
125                                                                                       :"BEZIER"));
126         logf("<debug> relative coordinates: %08x,%08x", outline->dest.x, outline->dest.y);
127         x += (outline->dest.x/(float)0xffff);
128         y += (outline->dest.y/(float)0xffff);
129         logf("<debug> coordinates: %f,%f", x, y);
130         if(outline->type == T1_PATHTYPE_MOVE)
131         {
132         }
133         else if(outline->type == T1_PATHTYPE_LINE) 
134         {
135             plotxy p0;
136             plotxy p1;
137             p0.x=lastx;
138             p0.y=lasty;
139             p1.x=x;
140             p1.y=y;
141             line(p0,p1,m);
142         }
143         else if(outline->type == T1_PATHTYPE_BEZIER)
144         {
145             plotxy p0;
146             plotxy p1;
147             plotxy p2;
148             plotxy p3;
149             T1_BEZIERSEGMENT*o2 = (T1_BEZIERSEGMENT*)outline;
150             p0.x=x; 
151             p0.y=y;
152             p1.x=o2->C.x/(float)0xffff+lastx;
153             p1.y=o2->C.y/(float)0xffff+lasty;
154             p2.x=o2->B.x/(float)0xffff+lastx;
155             p2.y=o2->B.y/(float)0xffff+lasty;
156             p3.x=lastx;
157             p3.y=lasty;
158             spline(p0,p1,p2,p3,m);
159 //          vga->drawline(320+lastx, 240+lasty, 320+x, 240+y);
160         } 
161         else {
162          printf("outline type:%d\n", outline->type);
163         }
164         
165         lastx=x;
166         lasty=y;
167         outline = outline->link;
168     }
169     XFlush(X.d);
170 }
171
172 void processchar(struct swfoutput*obj, int i, char c, swfmatrix*m)
173 {
174     T1_OUTLINE*outline;
175     int width = T1_GetCharWidth(i, c);
176     BBox bbox = T1_GetCharBBox(i, c);
177     char*charname= T1_GetCharName(i, c);
178     logf("<debug> Font name is %s", T1_GetFontFileName(i));
179     logf("<debug> char 0x%02x is named %s\n",c,charname);
180     logf("<debug> bbox: %d %d %d %d\n",bbox.llx,bbox.lly,bbox.urx,bbox.ury);
181     if(!charname || charname[0] == '.') 
182     {
183      logf("<error> Char to set is not defined!");
184      logf("<error> -  font file is %s\n", T1_GetFontFileName(i));
185      logf("<error> -  char 0x%02x is named %s\n",c,charname);
186     }
187     swfmatrix m2=*m;    
188     m2.m11/=100;
189     m2.m21/=100;
190     m2.m12/=100;
191     m2.m22/=100;
192     outline =  T1_GetCharOutline( i, c, 100.0, 0);
193     swfoutput_drawpath(obj, outline, &m2);
194 }
195
196 void swfoutput_init(struct swfoutput* obj, int sizex, int sizey) 
197 {
198   GLYPH *glyph;
199   int i;
200
201   logf("<verbose> initializing swf output for size %d*%d\n", sizex,sizey);
202
203   vga=new modexwindow(sizex,sizey,"");
204   vga->setdirect();
205   if(!vga->on()) exit(1);
206   vga->setcolor(0xff00ff);
207   
208   obj->t1font = 0;
209   obj->t1fontsize = 0.0025;
210 }
211
212 int swfoutput_setfont(struct swfoutput*obj, int t1id)
213 {
214     obj->t1font = t1id;
215 }
216
217 void swfoutput_setfontsize(struct swfoutput* obj, double size)
218 {
219     obj->t1fontsize = size; 
220 }
221
222 void swfoutput_setfontmatrix(struct swfoutput*obj,double m11,double m12,
223                                                   double m21,double m22)
224 {
225     obj->fontm11 = m11;
226     obj->fontm12 = m12;
227     obj->fontm21 = m21;
228     obj->fontm22 = m22;
229 }
230
231 void swfoutput_drawchar(struct swfoutput* obj,double x,double y,char a) 
232 {
233     swfmatrix m;
234     m.m11 = obj->fontm11*obj->t1fontsize;
235     m.m12 = obj->fontm12*obj->t1fontsize;
236     m.m21 = obj->fontm21*obj->t1fontsize;
237     m.m22 = obj->fontm22*obj->t1fontsize;
238     m.m13 = x;
239     m.m23 = y;
240     processchar(obj, obj->t1font, a, &m);
241 }
242
243 void swfoutput_destroy(struct swfoutput* obj) 
244 {
245     T1_CloseLib();
246     vga->off();
247     delete vga;
248 }
249