added fontalign polygon detector
[swftools.git] / lib / modules / swfalignzones.c
1 #include "../rfxswf.h"
2
3 static inline double sqr(double x) {return x*x;}
4
5 static void draw_line(float*row, float x1, float x2, float y1, float y2, int min, int max)
6 {
7     if(x2<x1) {int x=x1;x1=x2;x2=x;}
8     if(x1<min || x2>max) {
9         fprintf(stderr, "error: glyph x stroke out of bounds\n");
10         return;
11     }
12     x1 -= min;
13     x2 -= min;
14
15     double d = sqrt(sqr(y2-y1)+sqr(x2-x1));
16     if(floor(x1)==floor(x2)) {
17         row[(int)floor(x1)] += d;
18     } else {
19         double i = d/(x2-x1);
20         int x;
21         int xx1 = ceil(x1);
22         int xx2 = floor(x2);
23         row[xx1] += i*(xx1-x1);
24         row[xx2] += i*(x2-xx2);
25         for(x=xx1;x<xx2;x++) {
26             row[x]+=i;
27         }
28     }
29 }
30
31 static void draw_line_xy(float*row,float*column, float x1, float y1, float x2, float y2,SRECT* area)
32 {
33     draw_line(row, x1, x2, y1, y2, area->xmin, area->xmax);
34     draw_line(column, y1, y2, x1, x2, area->ymin, area->ymax);
35 }
36
37 static void find_best(float*_row, int width, int*_x1, int*_x2, int min_dist)
38 {
39     int x1=-1, x2=-1;
40     float max1=-1,max2=-1;
41     int t;
42     float*row = malloc(sizeof(float)*(width+1));
43     int filter_size = 100;
44     float* filter = malloc(sizeof(float)*(filter_size*2+1));
45     double var = filter_size/3;
46     for(t=-filter_size;t<=filter_size;t++) {
47         double v = t/var;
48         float r = v*v/2;
49         filter[filter_size+t] = exp(-r);
50     }
51     filter[0]=1;filter_size=0;
52
53     for(t=0;t<=width;t++) {
54         int s;
55         double sum = 0;
56         for(s=-filter_size;s<=filter_size;s++) {
57             if(t+s<0) continue;
58             if(t+s>width) continue;
59             sum += _row[t+s]*filter[s+filter_size];
60         }
61         row[t] = sum;
62     }
63
64     for(t=0;t<=width;t++) {
65         if(row[t]>max1) {
66             max1 = row[t];
67             x1 = t;
68         }
69     }
70     // invalidate everything around the maximum
71     for(t=-min_dist+1;t<min_dist;t++) {
72         if(t+x1<0 || t+x1>width) continue;
73         row[t+x1]=-1;
74     }
75     for(t=0;t<=width;t++) {
76         if(row[t]>max2) {
77             max2 = row[t];
78             x2 = t;
79         }
80     }
81
82     if(x1>0 && x2>0 && x1>x2) {int x=x1;x1=x2;x2=x;}
83     *_x1=x1;
84     *_x2=x2;
85     free(row);
86 }
87
88 static ALIGNZONE detect_for_char(SWFFONT * f, int nr)
89 {
90     SWFGLYPH*g = &f->glyph[nr];
91     SRECT b = f->layout->bounds[nr];
92     ALIGNZONE a = {0xffff,0xffff,0xffff,0xffff};
93
94     // negate y
95     int by1=b.ymin,by2=b.ymax;
96     b.ymin = -by2;
97     b.ymax = -by1;
98
99     int width = b.xmax - b.xmin + 1;
100     int height = b.ymax - b.ymin + 1;
101     if(!width || !height)
102         return a;
103             
104     float*row = rfx_calloc(sizeof(float)*width);
105     float*column = rfx_calloc(sizeof(float)*height);
106
107     SHAPE2*s = swf_ShapeToShape2(g->shape);
108     SHAPELINE*l = s->lines;
109     int x=0,y=0;
110     while(l) {
111         if(l->type == lineTo) {
112             draw_line_xy(row,column,x,-y,l->x,-l->y,&b);
113         } else if(l->type == splineTo) {
114             double x1=x,x2=l->sx,x3=l->x;
115             double y1=y,y2=l->sy,y3=l->y;
116             double c = fabs(x3-2*x2+x1) + fabs(y3-2*y2+y1);
117             int parts = ((int)(sqrt(c)/6))*2+1;
118             float xx=x1,yy=y1;
119             int t;
120             for(t=1;t<=parts;t++) {
121                 float nx = ((t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts));
122                 float ny = ((t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts));
123                 draw_line_xy(row,column,xx,-yy,nx,-ny,&b);
124                 xx = nx;
125                 yy = ny;
126             }
127         }
128         x = l->x;
129         y = l->y;
130         l = l->next;
131     }
132
133     int t;
134
135     /* find two best x values */
136     int x1=-1,y1=-1,x2=-1,y2=-1;
137     //int min_dist = 4000;
138     int min_dist = 1;
139     find_best(row,width,&x1,&x2,min_dist);
140     find_best(column,height,&y1,&y2,min_dist);
141
142     if(x1>=0) a.x  = floatToF16((x1+b.xmin) / 20480.0);
143     if(x2>=0) a.dx = floatToF16((x2-x1) / 20480.0);
144     if(y1>=0) a.y  = floatToF16((y1+b.ymin) / 20480.0);
145     if(y2>=0) a.dy = floatToF16((y2-y1) / 20480.0);
146     return a;
147 }
148
149 void swf_FontCreateAlignZones(SWFFONT * f)
150 {
151     if(f->alignzones)
152         return;
153
154     if(!f->layout) {
155         fprintf(stderr, "Error: font needs a layout for alignzones to be detected.");
156         return;
157     }
158
159     
160     f->alignzones = (ALIGNZONE*)rfx_calloc(sizeof(ALIGNZONE)*f->numchars);
161     f->alignzone_flags = FONTALIGN_THIN;
162
163     if(!f->layout) {
164         int t;
165         for(t=0;t<f->numchars;t++) {
166             // just align the baseline
167             f->alignzones[t].x = 0xffff;
168             f->alignzones[t].y = 0;
169             f->alignzones[t].dx = 0xffff;
170             f->alignzones[t].dy = 0xffff;//floatToF16(460.80 / 1024.0);
171         }
172     } else {
173         int t;
174         for(t=0;t<f->numchars;t++) {
175             f->alignzones[t] = detect_for_char(f, t);
176         }
177     }
178
179 /*
180     "-^_~\xad\xaf+`\xac\xb7\xf7" //chars for which to detect one y value
181     "#=:;\xb1" //chars for which to detect two y values
182     "\"\xa8" //chars for which to detect two x values
183 */
184 }
185
186 void swf_FontSetAlignZones(TAG*t, SWFFONT *f)
187 {
188     swf_SetU16(t, f->id);
189     swf_SetU8(t, f->alignzone_flags);
190     int i;
191     for(i=0;i<f->numchars;i++) {
192         ALIGNZONE*a = &f->alignzones[i];
193         U8 flags = 0;
194         if((a->x & a->dx)!=0xffff)
195             flags |= 1;
196         if((a->y & a->dy)!=0xffff)
197             flags |= 2;
198         int num = 1;
199         if(a->dx != 0xffff || a->dy != 0xffff)
200             num++;
201         swf_SetU8(t, num);
202         if(flags&1) swf_SetU16(t, a->x); else swf_SetU16(t, 0);
203         if(flags&2) swf_SetU16(t, a->y); else swf_SetU16(t, 0);
204         if(num==2) {
205             if((flags&1) && a->dx!=0xffff) swf_SetU16(t, a->dx); else swf_SetU16(t, 0);
206             if((flags&2) && a->dy!=0xffff) swf_SetU16(t, a->dy); else swf_SetU16(t, 0);
207         }
208         swf_SetU8(t, flags);
209     }
210 }
211
212