3 static inline double sqr(double x) {return x*x;}
5 static void draw_line(float*row, float x1, float x2, float y1, float y2, int min, int max)
7 if(x2<x1) {int x=x1;x1=x2;x2=x;}
9 fprintf(stderr, "error: glyph x stroke out of bounds\n");
15 double d = sqrt(sqr(y2-y1)+sqr(x2-x1));
16 if(floor(x1)==floor(x2)) {
17 row[(int)floor(x1)] += d;
23 row[xx1] += i*(xx1-x1);
24 row[xx2] += i*(x2-xx2);
25 for(x=xx1;x<xx2;x++) {
31 static void draw_line_xy(float*row,float*column, float x1, float y1, float x2, float y2,SRECT* area)
33 draw_line(row, x1, x2, y1, y2, area->xmin, area->xmax);
34 draw_line(column, y1, y2, x1, x2, area->ymin, area->ymax);
37 static void find_best(float*_row, int width, int*_x1, int*_x2, int min_size, int min, int max, char debug)
40 float max1=-1e20,max2=-1e20;
42 float*row = malloc(sizeof(float)*(width+1));
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++) {
49 filter[filter_size+t] = exp(-r);
51 //filter[0]=1;filter_size=0;
53 for(t=0;t<=width;t++) {
56 for(s=-filter_size;s<=filter_size;s++) {
58 if(t+s>width) continue;
59 sum += _row[t+s]*filter[s+filter_size];
64 for(t=0;t<=width;t++) {
71 double scale = min_size/1024.0;
72 for(t=0;t<=width;t++) {
77 double r1 = (t<x1?t:x1)*scale;
78 double r2 = (t<x1?x1:t)*scale;
83 double ext2 = width*scale-r2;
84 double add1 = ext1*s - ext1;
85 double add2 = ext2*s - ext2;
87 /* don't allow the char to grow more than one pixel */
88 if(add1>=1 || add2>=1) {
93 for(t=0;t<=width;t++) {
100 if(x1>=0 && x2>=0 && x1>x2) {int x=x1;x1=x2;x2=x;}
106 static int find_min_distance(int min_size, int height)
108 /* find a minimum distance between two alignzones so
109 that when they both, due to pixel snapping, move
110 into opposite directions, the subsequent character
111 scaling doesn't cause the upper/lower end of the char
112 to move outwards more than m pixels */
114 double scale = min_size / 1024.0;
115 double max_move = (2 * 1.0 / scale)/m;
116 return (int)(height*max_move);
119 static ALIGNZONE detect_for_char(SWFFONT * f, int nr)
121 SWFGLYPH*g = &f->glyph[nr];
122 SRECT b = f->layout->bounds[nr];
123 ALIGNZONE a = {0xffff,0xffff,0xffff,0xffff};
126 int by1=b.ymin,by2=b.ymax;
130 int width = b.xmax - b.xmin + 1;
131 int height = b.ymax - b.ymin + 1;
132 if(!width || !height)
135 float*row = rfx_calloc(sizeof(float)*width);
136 float*column = rfx_calloc(sizeof(float)*height);
138 SHAPE2*s = swf_ShapeToShape2(g->shape);
139 SHAPELINE*l = s->lines;
142 if(l->type == lineTo) {
143 draw_line_xy(row,column,x,-y,l->x,-l->y,&b);
144 } else if(l->type == splineTo) {
145 double x1=x,x2=l->sx,x3=l->x;
146 double y1=y,y2=l->sy,y3=l->y;
147 double c = fabs(x3-2*x2+x1) + fabs(y3-2*y2+y1);
148 int parts = ((int)(sqrt(c)/6))*2+1;
151 for(t=1;t<=parts;t++) {
152 float nx = ((t*t*x3 + 2*t*(parts-t)*x2 + (parts-t)*(parts-t)*x1)/(double)(parts*parts));
153 float ny = ((t*t*y3 + 2*t*(parts-t)*y2 + (parts-t)*(parts-t)*y1)/(double)(parts*parts));
154 draw_line_xy(row,column,xx,-yy,nx,-ny,&b);
166 /* find two best x values */
167 int x1=-1,y1=-1,x2=-1,y2=-1;
168 find_best(row,width,&x1,&x2,f->use->smallest_size,b.xmin,b.xmax,0);
169 char debug = f->glyph2ascii[nr]=='o' && f->id==7;
170 find_best(column,height,&y1,&y2,f->use->smallest_size,b.ymin,b.ymax,debug);
172 //if(x1>=0) a.x = floatToF16((x1+b.xmin) / 20480.0);
173 //if(x2>=0) a.dx = floatToF16((x2-x1) / 20480.0);
174 if(y1>=0) a.y = floatToF16((y1+b.ymin) / 20480.0);
175 if(y2>=0) a.dy = floatToF16((y2-y1) / 20480.0);
179 void swf_FontCreateAlignZones(SWFFONT * f)
185 fprintf(stderr, "Error: font needs a layout for alignzones to be detected.");
190 f->alignzones = (ALIGNZONE*)rfx_calloc(sizeof(ALIGNZONE)*f->numchars);
191 f->alignzone_flags = FONTALIGN_MEDIUM;
195 for(t=0;t<f->numchars;t++) {
196 // just align the baseline
197 f->alignzones[t].x = 0xffff;
198 f->alignzones[t].y = 0;
199 f->alignzones[t].dx = 0xffff;
200 f->alignzones[t].dy = 0xffff;//floatToF16(460.80 / 1024.0);
204 for(t=0;t<f->numchars;t++) {
205 f->alignzones[t] = detect_for_char(f, t);
210 "-^_~\xad\xaf+`\xac\xb7\xf7" //chars for which to detect one y value
211 "#=:;\xb1" //chars for which to detect two y values
212 "\"\xa8" //chars for which to detect two x values
216 void swf_FontSetAlignZones(TAG*t, SWFFONT *f)
218 swf_SetU16(t, f->id);
219 swf_SetU8(t, f->alignzone_flags);
221 for(i=0;i<f->numchars;i++) {
222 ALIGNZONE*a = &f->alignzones[i];
224 if((a->x & a->dx)!=0xffff)
226 if((a->y & a->dy)!=0xffff)
229 if(a->dx != 0xffff || a->dy != 0xffff)
232 if(flags&1) swf_SetU16(t, a->x); else swf_SetU16(t, 0);
233 if(flags&2) swf_SetU16(t, a->y); else swf_SetU16(t, 0);
235 if((flags&1) && a->dx!=0xffff) swf_SetU16(t, a->dx); else swf_SetU16(t, 0);
236 if((flags&2) && a->dy!=0xffff) swf_SetU16(t, a->dy); else swf_SetU16(t, 0);