Debian Codegraph @

draw.c

raw symbols SCIP
1 /*
2 Qxw is a program to help construct and publish crosswords.
3
4 Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant
5 http://www.quinapalus.com
6 E-mail: [email protected]
7
8 This file is part of Qxw.
9
10 Qxw is free software: you can redistribute it and/or modify
11 it under the terms of version 2 of the GNU General Public License
12 as published by the Free Software Foundation.
13
14 Qxw is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Qxw. If not, see <http://www.gnu.org/licenses/> or
21 write to the Free Software Foundation, Inc., 51 Franklin Street,
22 Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25 #include <wchar.h>
26 #include <wctype.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <cairo.h>
30 #include <cairo-ps.h>
31 #include <cairo-svg.h>
32 #include <pango/pangocairo.h>
33 #include <gdk/gdkpango.h>
34 #include <glib.h>
35 #include <glib/gstdio.h>
36
37 #include "common.h"
38 #include "qxw.h"
39 #include "draw.h"
40 #include "gui.h"
41 #include "dicts.h"
42
43 int bawdpx,hbawdpx;
44
45 static int sfcxoff=0,sfcyoff=0;
46
47 static double sdirdx[NGTYPE][MAXNDIR]={{1,0},{ 1.2,1.2,0 },{1.4,0.7,-0.7},{1,0},{1,0},{1,0},{1,0},{1,0},{1,0},{1,0},{1,0},{1,0},{1,0}};
48 static double sdirdy[NGTYPE][MAXNDIR]={{0,1},{-0.7,0.7,1.4},{0 ,1.2, 1.2},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1}};
49 char*dname[NGTYPE][MAXNDIR]={{"Across","Down"},{"Northeast","Southeast","South"},{"East","Southeast","Southwest"},{"Ring","Radial"},{"Ring","Radial"},
50 {"Across","Down"},{"Across","Down"},{"Across","Down"},{"Across","Down"},{"Across","Down"},
51 {"Across","Down"},{"Across","Down"},{"Across","Down"}
52 };
53
54 double gscale=1; // global scaling for cairo operations
55
56 // GERNERAL DRAWING
57
58 void moveto(cairo_t*cc,double x,double y) {cairo_move_to(cc,x*gscale,y*gscale);}
59 void lineto(cairo_t*cc,double x,double y) {cairo_line_to(cc,x*gscale,y*gscale);}
60 void rmoveto(cairo_t*cc,double x,double y) {cairo_rel_move_to(cc,x*gscale,y*gscale);}
61 void rlineto(cairo_t*cc,double x,double y) {cairo_rel_line_to(cc,x*gscale,y*gscale);}
62 void setlinewidth(cairo_t*cc,double w) {cairo_set_line_width(cc,w*gscale);}
63 void setlinecap(cairo_t*cc,int c) {cairo_set_line_cap(cc,c);}
64 void closepath(cairo_t*cc) {cairo_close_path(cc);}
65 void fill(cairo_t*cc) {cairo_fill(cc);}
66 void stroke(cairo_t*cc) {cairo_stroke(cc);}
67 void strokepreserve(cairo_t*cc) {cairo_stroke_preserve(cc);}
68 void clip(cairo_t*cc) {cairo_clip(cc);}
69 void gsave(cairo_t*cc) {cairo_save(cc);}
70 void grestore(cairo_t*cc) {cairo_restore(cc);}
71 void setrgbcolor(cairo_t*cc,double r,double g, double b) {cairo_set_source_rgb(cc,r,g,b);}
72 void setrgbacolor(cairo_t*cc,double r,double g, double b,double a) {cairo_set_source_rgba(cc,r,g,b,a);}
73
74 static PangoLayout*playout;
75 static PangoFontDescription*fdesc;
76 static double bheight;
77
78 void settext(cairo_t*cc,char*s,double h,int fs) {
79 static cairo_t*occ=0;
80 static int ofs=-1;
81 static double oh=-1;
82
83 h*=gscale;
84 if(cc!=occ) {
85 if(playout) g_object_unref(playout);
86 playout=pango_cairo_create_layout(cc);
87 occ=cc;
88 ofs=-1;
89 oh=-1;
90 }
91 DEB_RF printf("settext(%s,%g,%d)\n",s,h,fs);
92 if(!fdesc) fdesc=pango_font_description_from_string("Sans");
93 if(h!=oh) {
94 pango_font_description_set_absolute_size(fdesc,(int)(h*PANGO_SCALE+.5));
95 pango_layout_set_font_description(playout,fdesc);
96 oh=h;
97 }
98 if(fs!=ofs) {
99 pango_font_description_set_weight(fdesc,(fs&1)?PANGO_WEIGHT_BOLD :PANGO_WEIGHT_NORMAL);
100 pango_font_description_set_style (fdesc,(fs&2)?PANGO_STYLE_ITALIC:PANGO_STYLE_NORMAL);
101 pango_layout_set_font_description(playout,fdesc);
102 ofs=fs;
103 }
104 pango_layout_set_text(playout,s,-1);
105 pango_cairo_update_layout(cc,playout);
106 bheight=(double)pango_layout_get_baseline(playout)/PANGO_SCALE/gscale;
107 }
108
109 void showtext(cairo_t*cc) {
110 rmoveto(cc,0,-bheight);
111 pango_cairo_show_layout(cc,playout);
112 }
113
114 double textwidth(cairo_t*cc) {
115 int x,y;
116 // PangoRectangle ir,lr;
117 pango_layout_get_size(playout,&x,&y); // seems that this doesn't work rounds to multiples of 1024=PANGO_SCALE in some versions of Pango; hence need for gscale everywhere
118 DEB_RF printf(" (x=%d y=%d)\n",x,y);
119 // pango_layout_get_extents(playout,&ir,&lr);
120 //DEB_RF printf(" (PANGO_SCALE=%d ir=[%d %d %d %d] lr=[%d %d %d %d])\n",PANGO_SCALE, ir.x,ir.y,ir.width,ir.height, lr.x,lr.y,lr.width,lr.height );
121 return (double)x/PANGO_SCALE/gscale;
122 // return (double)ir.width/PANGO_SCALE/gscale;
123 }
124
125 void ltext(cairo_t*cc,char*s,double h,int fs) {
126 moveto(cc,0,0);
127 settext(cc,s,h,fs);
128 showtext(cc);
129 }
130
131 // Centered text. If ocm==0, print normally spaced; if ocm==1, print one character at a time, regularly spaced
132 void ctext(cairo_t*cc,char*s,double x,double y,double h,int fs,int ocm) {
133 int i,l,m;
134 char*p,t[SLEN+1];
135 double u;
136 DEB_RF printf("ctext(..,<%s>,x=%f,y=%f,h=%f,fs=%d,ocm=%d)\n",s,x,y,h,fs,ocm);
137 if(!ocm) {
138 settext(cc,s,h,fs);
139 u=x-textwidth(cc)/2.0;
140 DEB_RF printf(" tw=%f,u=%f\n",textwidth(cc),u);
141 moveto(cc,u,y);
142 showtext(cc);
143 return;
144 }
145 for(l=0,p=s;*p;l++) {
146 m=q_mblen(p);
147 if(m<1) break;
148 p+=m;
149 }
150 for(i=0,p=s;*p;i++) {
151 m=q_mblen(p);
152 if(m<1) return;
153 strncpy(t,p,m); t[m]=0;
154 settext(cc,t,h,fs);
155 u=x-textwidth(cc)/2.0+(i+i+1-l)*h*.4;
156 moveto(cc,u,y);
157 showtext(cc);
158 p+=m;
159 }
160 }
161
162 // Cairo's arc drawing does not seem to work well as part of a path, so we do our own
163 void arc(cairo_t*cc,double x,double y,double r,double t0,double t1) {double u;
164 if(t1<t0) t1+=2*PI;
165 if((t1-t0)*(t1-t0)*r>1.0/pxsq) { // error of approx 1/4 px?
166 u=(t0+t1)/2;
167 arc(cc,x,y,r,t0,u);
168 arc(cc,x,y,r, u,t1);
169 }
170 else lineto(cc,x+r*cos(t1),y+r*sin(t1));
171 }
172
173 void arcn(cairo_t*cc,double x,double y,double r,double t0,double t1) {double u;
174 if(t0<t1) t0+=2*PI;
175 if((t1-t0)*(t1-t0)*r>1.0/pxsq) { // error of approx 1/4 px?
176 u=(t0+t1)/2;
177 arcn(cc,x,y,r,t0,u);
178 arcn(cc,x,y,r, u,t1);
179 }
180 else lineto(cc,x+r*cos(t1),y+r*sin(t1));
181 }
182
183 static void setrgbcolor24(cairo_t*cc,int c) {setrgbcolor(cc,((c>>16)&255)/255.0,((c>>8)&255)/255.0,(c&255)/255.0);}
184 static void setrgbcolor24a(cairo_t*cc,int c,double a) {setrgbacolor(cc,((c>>16)&255)/255.0,((c>>8)&255)/255.0,(c&255)/255.0,a);}
185
186 // GRID GEOMETRY
187
188 static void vxcoords(double*x0,double*y0,double x,double y,int d) {double t;
189 switch(gshape[gtype]) {
190 case 0:
191 *x0=x;
192 *y0=y;
193 switch(d) {
194 case 3: break;
195 case 0:*x0+=1.0; break;
196 case 1:*x0+=1.0;*y0+=1.0;break;
197 case 2: *y0+=1.0;break;
198 default:assert(0);break;
199 }
200 break;
201 case 1:
202 *x0=x*1.2+.4;
203 *y0=y*1.4+((int)floor(x)&1)*.7;
204 switch(d) {
205 case 5: break;
206 case 0:*x0+=0.8; break;
207 case 1:*x0+=1.2;*y0+=0.7;break;
208 case 2:*x0+=0.8;*y0+=1.4;break;
209 case 3: *y0+=1.4;break;
210 case 4:*x0-=0.4;*y0+=0.7;break;
211 default:assert(0);break;
212 }
213 break;
214 case 2:
215 *y0=y*1.2+.4;
216 *x0=x*1.4+((int)floor(y)&1)*.7;
217 switch(d) {
218 case 4: break;
219 case 3:*y0+=0.8; break;
220 case 2:*y0+=1.2;*x0+=0.7;break;
221 case 1:*y0+=0.8;*x0+=1.4;break;
222 case 0: *x0+=1.4;break;
223 case 5:*y0-=0.4;*x0+=0.7;break;
224 default:assert(0);break;
225 }
226 break;
227 case 3:case 4:
228 switch(d) {
229 case 3: break;
230 case 0:x+=1.0; break;
231 case 1:x+=1.0;y+=1.0;break;
232 case 2: y+=1.0;break;
233 }
234 t=2*PI*((gshape[gtype]==4)?x-0.5:x)/width;
235 *x0= (height-y)*sin(t)+height;
236 *y0=-(height-y)*cos(t)+height;
237 break;
238 }
239 }
240
241 void edgecoords(double*x0,double*y0,double*x1,double*y1,int x,int y,int d) {
242 vxcoords(x0,y0,(double)x,(double)y,d);
243 vxcoords(x1,y1,(double)x,(double)y,(d+1)%(ndir[gtype]*2));
244 }
245
246 // find centre coordinates for merge group containing x,y
247 void mgcentre(double*u,double*v,int x,int y,int d,int l) {
248 double x0=0,y0=0,x1=0,y1=0;
249 if(gshape[gtype]==3||gshape[gtype]==4) {
250 if(EVEN(d)) vxcoords(u,v,x+l/2.0,y+0.5,3);
251 else vxcoords(u,v,x+0.5,y+l/2.0,3);
252 return;
253 }
254 if(gtype>4) {
255 // we are on a square-format grid but there is potential for wrap-around
256 while(l>2) stepforw(&x,&y,d),l-=2;
257 if((d==0&&x==width -1)||(d==1&&y==height-1)) l=1;
258 }
259 vxcoords(&x0,&y0,(double)x,(double)y,0);
260 vxcoords(&x1,&y1,(double)x,(double)y,ndir[gtype]);
261 *u=(x0+x1)/2+sdirdx[gtype][d]*(l-1)/2.0;
262 *v=(y0+y1)/2+sdirdy[gtype][d]*(l-1)/2.0;
263 }
264
265 static void addvxcoordsbbox(double*x0,double*y0,double*x1,double*y1,double x,double y,int d) {double vx=0,vy=0;
266 vxcoords(&vx,&vy,x,y,d);
267 if(vx<*x0) *x0=vx;
268 if(vx>*x1) *x1=vx;
269 if(vy<*y0) *y0=vy;
270 if(vy>*y1) *y1=vy;
271 }
272
273 static void addsqbbox(double*x0,double*y0,double*x1,double*y1,int x,int y) {int d; double vx=0,vy=0;
274 mgcentre(&vx,&vy,x,y,0,1);
275 if(vx-0.5<*x0) *x0=vx-0.5; // include a 1x1 square centred in the middle of the merge group for the letter
276 if(vx+0.5>*x1) *x1=vx+0.5;
277 if(vy-0.5<*y0) *y0=vy-0.5;
278 if(vy+0.5>*y1) *y1=vy+0.5;
279 for(d=0;d<ndir[gtype]*2;d++) addvxcoordsbbox(x0,y0,x1,y1,(double)x,(double)y,d);
280 if(gshape[gtype]!=3&&gshape[gtype]!=4) return; // done if grid is not circular
281 // edge 3 is convex; one of the following points may be tangent to a vertical
282 // or horizontal
283 addvxcoordsbbox(x0,y0,x1,y1,x+0.25,(double)y,3);
284 addvxcoordsbbox(x0,y0,x1,y1,x+0.5 ,(double)y,3);
285 addvxcoordsbbox(x0,y0,x1,y1,x+0.75,(double)y,3);
286 }
287
288 void getsqbbox(double*x0,double*y0,double*x1,double*y1,int x,int y) {
289 *x0=1e9; *x1=-1e9;
290 *y0=1e9; *y1=-1e9;
291 addsqbbox(x0,y0,x1,y1,x,y);
292 }
293
294 void getmgbbox(double*x0,double*y0,double*x1,double*y1,int x,int y) {int i,l,mx[MXCL],my[MXCL];
295 *x0=1e9; *x1=-1e9;
296 *y0=1e9; *y1=-1e9;
297 l=getmergegroup(mx,my,x,y);
298 for(i=0;i<l;i++) addsqbbox(x0,y0,x1,y1,mx[i],my[i]);
299 }
300
301
302 static void getdxdy(double*c,double*s,int x,int y,int d) {double t,u,v;
303 switch(gshape[gtype]) {
304 case 0:case 1:case 2:
305 u=sdirdx[gtype][d];
306 v=sdirdy[gtype][d];
307 t=sqrt(u*u+v*v);
308 *c=u/t;*s=v/t;
309 break;
310 case 3:case 4:
311 t=2*PI*((gshape[gtype]==4)?x:x+0.5)/width;
312 if(d==0) {*c= cos(t); *s=sin(t);}
313 else {*c=-sin(t); *s=cos(t);}
314 break;
315 }
316 }
317
318 // get suitable scale factor (so that chars fit in small cells) for group
319 // in direction d of length l
320 static double getscale(int x,int y,int d,int l) {double s;
321 if(gshape[gtype]<3) return 1;
322 s=(height-y-.3)*2*PI/width;
323 if(EVEN(d)) s*=l;
324 if(s>1) s=1;
325 return s;
326 }
327
328
329 static void movetoorig(cairo_t*cc,int x,int y) {double x0=0,y0=0;
330 switch(gshape[gtype]) {
331 case 0: vxcoords(&x0,&y0,(double)x,(double)y,3);break;
332 case 1: vxcoords(&x0,&y0,(double)x,(double)y,5);break;
333 case 2: vxcoords(&x0,&y0,(double)x,(double)y,4);break;
334 case 3:case 4:
335 vxcoords(&x0,&y0,(double)x,(double)y,1);
336 break;
337 }
338 moveto(cc,x0,y0);
339 }
340
341 static void movetomgcentre(cairo_t*cc,int x,int y,int d,int l) {double u=0,v=0;
342 mgcentre(&u,&v,x,y,d,l);
343 moveto(cc,u,v);
344 }
345
346
347 // draw path for edges with bits set in mask b
348 static void edges(cairo_t*cc,int x,int y,int b) {int f,g,i,j,n,o;double x0=0,y0=0,x1=0,y1=0,t;
349 n=ndir[gtype]*2;
350 b&=(1<<n)-1;
351 if(b==0) return;
352 for(o=0;o<n;o++) if(((b>>o)&1)==0) break; // find a place to start drawing
353 f=0;g=0;
354 for(i=0;i<ndir[gtype]*2;i++) {
355 j=(o+i)%n;
356 if((b>>j)&1) {
357 edgecoords(&x0,&y0,&x1,&y1,x,y,j);
358 if(f==0) moveto(cc,x0,y0);
359 if((gshape[gtype]==3||gshape[gtype]==4)) {
360 if(j&1) {
361 t=2*PI*((gshape[gtype]==4)?x-0.5:x)/width-PI/2;
362 if(j==1) arcn(cc,(double)height,(double)height,height-y-.999, t+2*PI/width,t);
363 else arc (cc,(double)height,(double)height,height-y ,t,t+2*PI/width);
364 }
365 else lineto(cc,x1,y1);
366 }
367 else {
368 // the following is to defeat this bug:
369 // https://bugs.freedesktop.org/show_bug.cgi?id=39551
370 lineto(cc,(x0+x1)/2,(y0+y1)/2);
371 moveto(cc,(x0+x1)/2,(y0+y1)/2);
372 lineto(cc,x1,y1);
373 }
374 f=1;
375 }
376 else f=0,g=1;
377 }
378 if(g==0) closepath(cc);
379 }
380
381 // draw path for outline of a `square'
382 static void sqpath(cairo_t*cc,int x,int y) {int i;double x0=0,y0=0,t;
383 vxcoords(&x0,&y0,(double)x,(double)y,0);
384 moveto(cc,x0,y0);
385 switch(gshape[gtype]) {
386 case 0: case 1: case 2:
387 for(i=1;i<ndir[gtype]*2;i++) {
388 vxcoords(&x0,&y0,(double)x,(double)y,i);
389 lineto(cc,x0,y0);
390 }
391 break;
392 case 3: case 4:
393 vxcoords(&x0,&y0,(double)x,(double)y,1);
394 lineto(cc,x0,y0);
395 t=2*PI*((gshape[gtype]==4)?x-0.5:x)/width-PI/2;
396 arcn(cc,(double)height,(double)height,height-y-.999,t+2*PI/width,t);
397 vxcoords(&x0,&y0,(double)x,(double)y,3);
398 lineto(cc,x0,y0);
399 arc (cc,(double)height,(double)height,height-y ,t,t+2*PI/width);
400 break;
401 }
402 closepath(cc);
403 }
404
405 static void rtop(double*r,double*t,double x,double y) {
406 x-=height;y=height-y;
407 *r=sqrt(x*x+y*y);
408 if(*r<1e-3) *t=0;
409 else *t=atan2(x,y);
410 if(*t<0) *t+=2*PI;
411 }
412
413 static void ptor(double*x,double*y,double r,double t) {
414 *x=r*sin(t)+height;
415 *y=height-r*cos(t);
416 }
417
418 // move to suitable position to plot number (or other marker text) of width w, height h
419 // in corner number c
420 static void movetonum(cairo_t*cc,int x,int y,double w,double h,int c) {double x0=0,y0=0,r0,t0; int d;
421 switch(gshape[gtype]) {
422 case 0:
423 movetoorig(cc,x,y);
424 switch(c) {
425 case 0: rmoveto(cc,.03 , h*1.25); break;
426 case 1: rmoveto(cc,.97-w, h*1.25); break;
427 case 2: rmoveto(cc,.97-w,.97-h*.1 ); break;
428 case 3: rmoveto(cc,.03 ,.97-h*.1 ); break;
429 }
430 break;
431 case 1:
432 movetoorig(cc,x,y);
433 switch(c) {
434 case 0: rmoveto(cc,.00 ,h*1.25 ); break;
435 case 1: rmoveto(cc,.80-w ,h*1.25 ); break;
436 case 2: rmoveto(cc,1.20-w-h*.5,.7+h*.5 ); break;
437 case 3: rmoveto(cc,.80-w ,1.37-h*.1); break;
438 case 4: rmoveto(cc,.00 ,1.37-h*.1); break;
439 case 5: rmoveto(cc,-.40+h*.5 ,.7+h*.5 ); break;
440 }
441 break;
442 case 2:
443 movetoorig(cc,x,y);
444 switch(c) {
445 case 0: rmoveto(cc,.03 ,h*1.25 ); break;
446 case 1: rmoveto(cc,.7-w*.5,h*1.25-.45+w*.5); break;
447 case 2: rmoveto(cc,1.37-w ,h*1.25 ); break;
448 case 3: rmoveto(cc,1.37-w ,0.8 ); break;
449 case 4: rmoveto(cc,.7-w*.5,1.25-w*.5 ); break;
450 case 5: rmoveto(cc,.03 ,0.8 ); break;
451 }
452 break;
453 case 3:case 4:
454 vxcoords(&x0,&y0,(double)x,(double)y,(c+3)%4);
455 rtop(&r0,&t0,x0,y0);
456 switch(c) {
457 case 0: r0-=.05;t0+=2*PI*0.015/r0; break;
458 case 1: r0-=.05;t0-=2*PI*0.015/r0; break;
459 case 2: r0+=.05;t0-=2*PI*0.015/r0; break;
460 case 3: r0+=.05;t0+=2*PI*0.015/r0; break;
461 }
462 if (t0<= PI/2) d=0;
463 else if(t0<= PI) d=1;
464 else if(t0<=3*PI/2) d=2;
465 else d=3;
466 h*=1.25;
467 w+=0.03;
468 switch((4-c+d)%4) {
469 case 0: r0-=w*sin(t0); break;
470 case 1: r0+=h*cos(t0); break;
471 case 2: r0+=w*sin(t0); break;
472 case 3: r0-=h*cos(t0); break;
473 }
474 ptor(&x0,&y0,r0,t0);
475 if(c==0||c==3) d^=2;
476 if(d==0||d==3) x0-=w;
477 if(d==2||d==3) y0+=h;
478 moveto(cc,x0,y0);
479 break;
480 }
481 }
482
483
484
485
486
487 /*
488 Surfaces that make up main grid drawing area:
489
490 sfb : background
491 sfc : cursor
492 sfs : selected cells/lights
493 sfh : hint letters, hotspots
494 sfn : numbers and marks
495 sfq[x][y] : one for each square, containing entry letter, number, edges/bars, block, cutout
496 */
497
498 static cairo_surface_t*sfb=0,*sfc=0,*sfs=0,*sfh=0,*sfn=0,*sfq[MXSZ][MXSZ]={{0}};
499 static int sfqx0[MXSZ][MXSZ],sfqy0[MXSZ][MXSZ],sfqx1[MXSZ][MXSZ],sfqy1[MXSZ][MXSZ]; // bounding boxes of squares
500
501 void draw_printversions() { printf("pango version %s\ncairo version %s\n",pango_version_string(),cairo_version_string()); }
502
503 void draw_finit() {int x,y;
504 if(sfb) cairo_surface_destroy(sfb),sfb=0;
505 if(sfc) cairo_surface_destroy(sfc),sfc=0;
506 if(sfs) cairo_surface_destroy(sfs),sfs=0;
507 if(sfn) cairo_surface_destroy(sfn),sfn=0;
508 if(sfh) cairo_surface_destroy(sfh),sfh=0;
509 for(x=0;x<MXSZ;x++) for(y=0;y<MXSZ;y++) {
510 if(sfq[x][y]) cairo_surface_destroy(sfq[x][y]),sfq[x][y]=0;
511 }
512 }
513
514 // call this at init and whenever
515 // - grid props change
516 // - zoom changes
517 void draw_init() {
518 int x,y;
519 double x0,y0,x1,y1;
520 int sx0=0,sy0=0,sx1=0,sy1=0;
521 int gx0=0,gy0=0,gx1=0,gy1=0;
522
523 draw_finit(); // make sure everything is freed
524 hbawdpx=((int)floor(pxsq*BAWD/2.0+0.5));
525 if(hbawdpx<2) hbawdpx=2;
526 bawdpx=hbawdpx*2-1;
527 DEB_RF printf("bawdpx=%d hbawdpx=%d\n",bawdpx,hbawdpx);
528 for(x=0;x<MXSZ;x++) for(y=0;y<MXSZ;y++) if(isingrid(x,y)) {
529 getsqbbox(&x0,&y0,&x1,&y1,x,y);
530 sx0=(int)floor(x0*pxsq-hbawdpx);
531 sy0=(int)floor(y0*pxsq-hbawdpx);
532 sx1=(int) ceil(x1*pxsq+hbawdpx);
533 sy1=(int) ceil(y1*pxsq+hbawdpx);
534 sfqx0[x][y]=sx0;
535 sfqy0[x][y]=sy0;
536 sfqx1[x][y]=sx1;
537 sfqy1[x][y]=sy1;
538 if(sx0<gx0) gx0=sx0;
539 if(sy0<gy0) gy0=sy0;
540 if(sx1>gx1) gx1=sx1;
541 if(sy1>gy1) gy1=sy1;
542 sfq[x][y]=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,sx1-sx0,sy1-sy0);
543 }
544 DEB_RF printf("Overall bbox = (%d,%d)-(%d,%d)\n",gx0,gy0,gx1,gy1);
545 sfb=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,gx1,gy1); // take top left as (0,0)
546 sfc=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,pxsq,pxsq);
547 sfs=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,gx1+hbawdpx-1,gy1+hbawdpx-1);
548 sfh=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,gx1,gy1);
549 sfn=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,gx1,gy1);
550 }
551
552 // Refresh drawing area: called here with clip already set on cr, so we can
553 // just repaint everything.
554 void repaint(cairo_t*cc) {int i,j;
555 assert(sfb);
556 cairo_set_source_surface(cc,sfb,bawdpx,bawdpx); // paint background
557 cairo_paint(cc);
558 cairo_set_source_surface(cc,sfc,sfcxoff,sfcyoff); // paint cursor
559 cairo_paint(cc);
560 cairo_set_source_surface(cc,sfh,bawdpx,bawdpx); // paint hints
561 cairo_paint(cc);
562 for(j=0;j<MXSZ;j++) for(i=0;i<MXSZ;i++) if(isingrid(i,j)) { // paint each square
563 assert(sfq[i][j]);
564 cairo_set_source_surface(cc,sfq[i][j],sfqx0[i][j]+bawdpx,sfqy0[i][j]+bawdpx);
565 cairo_paint(cc);
566 }
567 cairo_set_source_surface(cc,sfn,bawdpx,bawdpx); // paint numbers
568 cairo_paint(cc);
569 cairo_set_source_surface(cc,sfs,hbawdpx,hbawdpx); // paint selection
570 cairo_paint(cc);
571 }
572
573
574 static unsigned int hpat[]={ // hatch pattern
575 0x808080, 0xcccccc, 0xffffff, 0xffffff, 0xffffff, 0xcccccc, 0x808080, 0x808080,
576 0xcccccc, 0xffffff, 0xffffff, 0xffffff, 0xcccccc, 0x808080, 0x808080, 0x808080,
577 0xffffff, 0xffffff, 0xffffff, 0xcccccc, 0x808080, 0x808080, 0x808080, 0xcccccc,
578 0xffffff, 0xffffff, 0xcccccc, 0x808080, 0x808080, 0x808080, 0xcccccc, 0xffffff,
579 0xffffff, 0xcccccc, 0x808080, 0x808080, 0x808080, 0xcccccc, 0xffffff, 0xffffff,
580 0xcccccc, 0x808080, 0x808080, 0x808080, 0xcccccc, 0xffffff, 0xffffff, 0xffffff,
581 0x808080, 0x808080, 0x808080, 0xcccccc, 0xffffff, 0xffffff, 0xffffff, 0xcccccc,
582 0x808080, 0x808080, 0xcccccc, 0xffffff, 0xffffff, 0xffffff, 0xcccccc, 0x808080};
583
584 // ex=1 (export mode): don't stroke around edge, don't shade cutouts
585 static void drawsqbg(cairo_t*cc,int x,int y,int sq,int ex) {int f,bg;
586 cairo_surface_t*cs;
587 cairo_pattern_t*cp;
588 cairo_matrix_t cm;
589
590 f=getflags(x,y);
591 bg=getbgcol(x,y);
592 if(f&8) { // hatching for cutout
593 if(ex) return;
594 gsave(cc);
595 cs=cairo_image_surface_create_for_data((unsigned char*)hpat,CAIRO_FORMAT_RGB24,8,8,32);
596 cp=cairo_pattern_create_for_surface(cs);
597 cairo_matrix_init_scale(&cm,1,1);
598 cairo_pattern_set_matrix(cp,&cm);
599 cairo_set_source(cc,cp);
600 cairo_pattern_set_extend(cairo_get_source(cc),CAIRO_EXTEND_REPEAT);
601 sqpath(cc,x,y);
602 fill(cc);
603 cairo_pattern_destroy(cp);
604 cairo_surface_destroy(cs);
605 grestore(cc);
606 } else {
607 sqpath(cc,x,y);
608 if(f&1) setrgbcolor(cc,0,0,0); // solid block? then black
609 else setrgbcolor24(cc,bg); // otherwise bg
610 setlinewidth(cc,1.0/sq);
611 if(!ex) strokepreserve(cc);
612 fill(cc);
613 }
614 }
615
616 // Draw cell contents. ocm ("one character at a time mode") flag passed on to ctext.
617 static void drawct(cairo_t*cc,int xr,int yr,struct square*sq,int ocm,int dots,double textscale) {
618 int i,j,l,md,de,nd;
619 double h,sc,u,v;
620 char s[MXCT*MAXNDIR*16+1];
621
622 l=getmergegroup(0,0,xr,yr);
623 md=getmergedir(xr,yr);
624 if(md<0) md=0;
625 mgcentre(&u,&v,xr,yr,md,l);
626 sc=getscale(xr,yr,0,l)*textscale;
627 de=getdech(xr,yr);
628 if(de==0) nd=1,de=1; else nd=ndir[gtype];
629 if(de==2) { // vertical display
630 l=1;
631 for(i=0;i<nd;i++) if(sq->ctlen[i]>l) l=sq->ctlen[i]; // find longest contents string
632 h=0.7/nd;
633 if(h>1.0/l) h=1.0/l;
634 for(i=0;i<nd;i++) {
635 abmstodispstr(s,sq->ctbm[i],sq->ctlen[i]);
636 if(dots) for(j=0;s[j];j++) if(s[j]==' ') s[j]='.';
637 ctext(cc,s,u,v+((i-(nd-1.0)/2.0)*h+0.42*h)*sc,h*sc,getfstyle(xr,yr),ocm);
638 }
639 } else {
640 s[0]=0;
641 l=0; for(i=0;i<nd;i++) l+=sq->ctlen[i]; // total content length
642 for(i=0;i<nd;i++) abmstodispstr(s+strlen(s),sq->ctbm[i],sq->ctlen[i]);
643 // printf("drawct: %016llx >%s< l=%d\n",sq->ctbm[0][0],s,l);
644 if(dots&&l>1) for(i=0;s[i];i++) if(s[i]==' ') s[i]='.';
645 h=0.7;
646 if(l>1) h=1.0/l;
647 ctext(cc,s,u,v+0.42*h*sc,h*sc,getfstyle(xr,yr),ocm);
648 }
649 }
650
651 // draw bars and edges
652 // lf=1 draw entered letter also
653 // ocm flag passed on to ctext
654 static void drawsqfg(cairo_t*cc,int x,int y,int sq,int ba,int lf,int ocm,double textscale) {
655 int b,d,f,m,xr,yr;
656
657 // edges and bars
658 b=0; for(d=0;d<ndir[gtype]*2;d++) if(isbar (x,y,d)) b|=1<<d;
659 m=0; for(d=0;d<ndir[gtype]*2;d++) if(ismerge(x,y,d)) m|=1<<d;
660 setrgbcolor(cc,0,0,0);
661 if(sqexists(x,y)) {
662 setlinewidth(cc,1.0/pxsq);
663 edges(cc,x,y,~m);
664 stroke(cc);
665 }
666 setlinewidth(cc,ba/(double)sq);
667 edges(cc,x,y,b);
668 stroke(cc);
669 // entered letter
670 if(!lf) return;
671 getmergerep(&xr,&yr,x,y);
672 f=getflags(xr,yr);
673 if((f&9)!=0) return;
674 setrgbcolor24(cc,getfgcol(xr,yr));
675 drawct(cc,xr,yr,&gsq[xr][yr],ocm,0,textscale);
676 }
677
678 // Draw one square into sfq[x][y].
679 void refreshsq(int x,int y) {
680 cairo_t*cc;
681
682 DEB_RF printf("refreshsq(%d,%d)\n",x,y);
683 assert(sfq[x][y]);
684 // background
685 cc=cairo_create(sfb);
686 cairo_translate(cc,0.5,0.5);
687 gscale=pxsq;
688 drawsqbg(cc,x,y,pxsq,0);
689 cairo_destroy(cc);
690 // foreground
691 cc=cairo_create(sfq[x][y]);
692 setrgbacolor(cc,0.0,0.0,0.0,0.0);
693 //DEB_RF setrgbacolor(cc,drand48(),drand48(),drand48(),0.5);
694 cairo_set_operator(cc,CAIRO_OPERATOR_SOURCE);
695 cairo_paint(cc);
696 cairo_set_operator(cc,CAIRO_OPERATOR_OVER);
697 cairo_translate(cc,-sfqx0[x][y]+0.5,-sfqy0[x][y]+0.5);
698 gscale=pxsq;
699 drawsqfg(cc,x,y,pxsq,bawdpx,1,1,1.0);
700 cairo_destroy(cc);
701 invaldarect(sfqx0[x][y]+bawdpx,sfqy0[x][y]+bawdpx,sfqx1[x][y]+bawdpx,sfqy1[x][y]+bawdpx);
702 gscale=1;
703 }
704
705 void refreshsqlist(int l,int*gx,int*gy) {int i;
706 DEB_RF printf("refreshsqlist([%d])\n",l);
707 for(i=0;i<l;i++) refreshsq(gx[i],gy[i]);
708 }
709
710 void refreshsqmg(int x,int y) {int l,gx[MXCL],gy[MXCL];
711 DEB_RF printf("refreshsqmg(%d,%d)\n",x,y);
712 if(!isingrid(x,y)) return;
713 l=getmergegroup(gx,gy,x,y);
714 refreshsqlist(l,gx,gy);
715 }
716
717 static void refreshsel0(cairo_t*cc) {int i,j; // fill selected cells
718 for(j=0;j<MXSZ;j++) for(i=0;i<MXSZ;i++) if(isingrid(i,j)&&(getflags(i,j)&0x10)) {
719 DEB_RF printf("sel fill %d,%d\n",i,j);
720 sqpath(cc,i,j);
721 fill(cc);
722 }
723 }
724
725 static void refreshsel1(cairo_t*cc) { // refresh selected lights
726 int d,i,j,k,l,lx[MXCL],ly[MXCL],tl;
727 double u,v,u0,v0;
728
729 setlinecap(cc,1);
730 setlinewidth(cc,.5);
731 for(j=0;j<height;j++) for(i=0;i<width;i++) for(d=0;d<ndir[gtype];d++)
732 if(isstartoflight(i,j,d)&&issellight(i,j,d)) {
733 l=getlightd(lx,ly,i,j,d);
734 assert(l>=2);
735 tl=getmergegroupd(0,0,lx[0],ly[0],d);
736 movetomgcentre(cc,lx[0],ly[0],d,tl);
737 mgcentre(&u,&v,lx[0],ly[0],d,tl);
738 for(k=1;k<l;k++) {
739 u0=u; v0=v;
740 tl=getmergegroupd(0,0,lx[k],ly[k],d);
741 mgcentre(&u,&v,lx[k],ly[k],d,tl);
742 if(gtype>=5) { // potential for wrap-around?
743 if(d==0&&(lx[k]<lx[k-1]||ly[k]!=ly[k-1])) { lineto(cc,width+0.5,v0); moveto(cc,-0.5,v); }
744 if(d==1&&(ly[k]<ly[k-1]||lx[k]!=lx[k-1])) { lineto(cc,u0,height+0.5); moveto(cc,u,-0.5); }
745 }
746 DEB_RF printf("%f,%f (%f) - %f,%f (%f)\n",u0,v0,atan2(v0,u0),u,v,atan2(v,u));
747 if((gshape[gtype]==3||gshape[gtype]==4)&&d==0) {
748 arc(cc,(double)height,(double)height,height-ly[k]-0.5,atan2(v0-height,u0-height),atan2(v-height,u-height));
749 } else lineto(cc,u,v);
750 }
751 stroke(cc);
752 }
753 setlinecap(cc,0);
754 }
755
756 static void refreshsel2(cairo_t*cc) { // refresh virtual lights, including current one if curdir>=100
757 int i,j,l,md,nc,pd[MXSZ][MXSZ],vis[MXSZ][MXSZ],x,y,x0,y0;
758 double sc[MXCL],u,v,xc[MXCL],yc[MXCL];
759
760 setlinecap(cc,1);
761 memset(vis,0,sizeof(vis));
762 for(i=0;i<nvl;i++) for(j=0;j<vls[i].l;j++) vis[vls[i].x[j]][vls[i].y[j]]++; // count visits: allocation does not depend on which are selected
763 for(i=0;i<MXSZ;i++) for(j=0;j<MXSZ;j++) pd[i][j]=(int)ceil(sqrt(vis[i][j])); // packing density in each square
764 memset(vis,0,sizeof(vis));
765 for(i=0;i<nvl;i++) {
766 nc=vls[i].l;
767 for(j=0;j<nc;j++) {
768 x0=vls[i].x[j];
769 y0=vls[i].y[j];
770 if(vls[i].sel||i+100==curdir) {
771 getmergerep(&x,&y,x0,y0);
772 l=getmergegroup(0,0,x,y);
773 md=getmergedir(x,y);
774 if(md<0) md=0;
775 mgcentre(&u,&v,x,y,md,l);
776 sc[j]=getscale(x,y,0,1);
777 xc[j]=u+(((vis[x0][y0]%pd[x0][y0])*2+1)/(pd[x0][y0]*2.0)-0.5)*sc[j];
778 yc[j]=v+(((vis[x0][y0]/pd[x0][y0])*2+1)/(pd[x0][y0]*2.0)-0.5)*sc[j];
779 }
780 vis[x0][y0]++;
781 }
782 if(vls[i].sel||i+100==curdir) { // is selected or is current?
783 if (vls[i].sel&&i+100!=curdir) setrgbacolor(cc,1.0,0.8,0.0,0.5); // just selected
784 else if(vls[i].sel&&i+100==curdir) setrgbacolor(cc,0.8,0.7,0.3,0.5); // selected and current
785 else setrgbacolor(cc,0.6,0.6,0.6,0.5); // just current
786 for(j=0;j<nc;j++) { // draw blobs
787 DEB_RF printf("refreshsel2: %d,%5.2f %5.2f %5.3f\n",j,xc[j],yc[j],sc[j]);
788 if(j==0) setlinecap(cc,2); // first one square
789 if(j==1) setlinecap(cc,1); // remainder circular (like IC pads)
790 if(j==0||sc[j]!=sc[j-1]) setlinewidth(cc,sc[j]/4);
791 moveto(cc,xc[j]-0.001,yc[j]);
792 rlineto(cc,0.001,0);
793 stroke(cc);
794 }
795 setlinewidth(cc,0.05); // joining lines
796 moveto(cc,xc[0],yc[0]);
797 for(j=1;j<nc;j++) lineto(cc,xc[j],yc[j]);
798 stroke(cc);
799 }
800 }
801 setlinecap(cc,0);
802 }
803
804 void refreshsel() {
805 cairo_t*cc;
806
807 cc=cairo_create(sfs);
808 setrgbacolor(cc,0.0,0.0,0.0,0.0);
809 cairo_set_operator(cc,CAIRO_OPERATOR_SOURCE);
810 cairo_paint(cc);
811 cairo_set_operator(cc,CAIRO_OPERATOR_OVER);
812 cairo_translate(cc,hbawdpx-0.5,hbawdpx-0.5);
813 gscale=pxsq;
814 setrgbacolor(cc,1.0,0.8,0.0,0.5);
815 if(selmode==0) refreshsel0(cc);
816 if(selmode==1) refreshsel1(cc);
817 if(selmode==2||curdir>=ndir[gtype]) refreshsel2(cc);
818 cairo_destroy(cc);
819 invaldaall();
820 gscale=1;
821 }
822
823 void refreshcur() {
824 double sc,c=0,s=0,u,v;
825 int m,x,y;
826 char t[20];
827 static int ocx=0,ocy=0;
828 cairo_t*cc;
829
830 cc=cairo_create(sfc);
831 setrgbacolor(cc,0.0,0.0,0.0,0.0);
832 cairo_set_operator(cc,CAIRO_OPERATOR_SOURCE);
833 cairo_paint(cc);
834 cairo_set_operator(cc,CAIRO_OPERATOR_OVER);
835 sc=getscale(curx,cury,0,1)*pxsq;
836 gscale=1;
837 setrgbacolor(cc,0.6,0.6,0.6,0.8);
838 setlinewidth(cc,2);
839 if(curdir<ndir[gtype]) {
840 getdxdy(&c,&s,curx,cury,curdir);c*=sc;s*=sc;
841 moveto(cc,pxsq/2.0,pxsq/2.0);
842 rmoveto(cc,c* .4 , s* .4);
843 rlineto(cc,c*-.8+s*-.3,c* .3+s*-.8);
844 rlineto(cc, s* .6,c*-.6 );
845 closepath(cc);
846 fill(cc);
847 if(ismux(curx,cury,curdir)) { // draw multiplex number?
848 m=getmaxmux(curx,cury,curdir);
849 if(m>1) {
850 curmux=curmux%m;
851 sprintf(t,"%d",curmux+1);
852 setrgbacolor(cc,0.0,1.0,0.0,1.0);
853 ctext(cc,t,pxsq*0.5,pxsq*0.3,pxsq*0.35,0,0);
854 setrgbacolor(cc,0.6,0.6,0.6,0.8);
855 }
856 }
857 }
858 else {
859 // moveto(cc,pxsq/2+sc/4,pxsq/2); // circle in VL mode
860 // arc(cc,pxsq/2,pxsq/2,sc/4,0,2*PI);
861 // closepath(cc);
862 // fill(cc);
863 }
864 mgcentre(&u,&v,curx,cury,0,1);
865 x=(int)floor(u*pxsq+0.5); y=(int)floor(v*pxsq+0.5);
866 sfcxoff=x-pxsq/2+bawdpx;
867 sfcyoff=y-pxsq/2+bawdpx;
868 invaldarect(sfcxoff,sfcyoff,sfcxoff+pxsq,sfcyoff+pxsq);
869 mgcentre(&u,&v,ocx,ocy,0,1);
870 x=(int)floor(u*pxsq+0.5); y=(int)floor(v*pxsq+0.5);
871 invaldarect(x-pxsq/2+bawdpx,y-pxsq/2+bawdpx,x+pxsq/2+bawdpx,y+pxsq/2+bawdpx);
872 ocx=curx;ocy=cury;
873 cairo_destroy(cc);
874 gscale=1;
875 }
876
877 static int ismkcirc(int x,int y,int c) {char s[MXMK+1]; getmk(s,x,y,c); return !STRCASECMP(s,"\\o"); }
878
879 static int isanymkcirc(int x,int y) {int c;
880 for(c=0;c<ndir[gtype]*2;c++) if(ismkcirc(x,y,c)) return 1;
881 return 0;
882 }
883
884 // get text to be drawn in corner c of square x,y; f=1 maps \# to light number, blank otherwise
885 static char*getmktext(int x,int y,int c,int f) {int i,i0,n; static char s[MXMK+1];
886 getmk(s,x,y,c);
887 if(!strcmp(s,"")) return "";
888 if(!STRCASECMP(s,"\\c")) {
889 i=geteicc(x,y);
890 if(i<=0||i>=MAXICC) strcpy(s,"-");
891 else sprintf(s,"%d",cwperm[i]);
892 }
893 else if(!STRCASECMP(s,"\\#")) {
894 if(f==0) return "";
895 n=getnumber(x,y);
896 if(n<=0) return "";
897 sprintf(s,"%d",n);
898 }
899 else if(!STRCASECMP(s,"\\i")) {
900 if(nvl<1) return "";
901 for(i0=-1,i=0;i<vls[0].l;i++) if(vls[0].x[i]==x&&vls[0].y[i]==y) {i0=i; break;}
902 if(i0==-1) return "";
903 sprintf(s,"%d",i0+1);
904 }
905 else if(!STRCASECMP(s,"\\o")) return "";
906 return s;
907 }
908
909 // draw all marks and numbers, including circles; f passed to getmktext
910 static void drawnums(cairo_t*cc,int f) {int c,l,md,x,y,x0,y0; double sc,u,v; char*p;
911 for(y=0;y<MXSZ;y++) for(x=0;x<MXSZ;x++) if(isingrid(x,y)) {
912 sc=getscale(x,y,0,1);
913 for(c=0;c<ndir[gtype]*2;c++) {
914 if(ismkcirc(x,y,c)) {
915 getmergerep(&x0,&y0,x,y);
916 l=getmergegroup(0,0,x0,y0);
917 md=getmergedir(x,y);
918 if(md<0) md=0;
919 mgcentre(&u,&v,x,y,md,l);
920 setrgbcolor24a(cc,getmkcol(x,y),0.5);
921 setlinewidth(cc,0.04);
922 moveto(cc,u+sc/2-0.02,v);
923 arc(cc,u,v,sc/2-0.02,0,2*PI);
924 closepath(cc);
925 stroke(cc);
926 continue;
927 }
928 p=getmktext(x,y,c,f);
929 if(!strcmp(p,"")) continue;
930 settext(cc,p,.25*sc,0);
931 movetonum(cc,x,y,textwidth(cc)+.1*sc,.27*sc,c);
932 rmoveto(cc,.05*sc,-.05*sc);
933 setrgbcolor24(cc,getmkcol(x,y));
934 showtext(cc);
935 }
936 }
937 }
938
939 void refreshnum() {
940 cairo_t*cc;
941
942 cc=cairo_create(sfn);
943 setrgbacolor(cc,0.0,0.0,0.0,0.0);
944 cairo_set_operator(cc,CAIRO_OPERATOR_SOURCE);
945 cairo_paint(cc);
946 cairo_set_operator(cc,CAIRO_OPERATOR_OVER);
947 cairo_translate(cc,0.5,0.5);
948 gscale=pxsq;
949 drawnums(cc,shownums);
950 cairo_destroy(cc);
951 invaldaall();
952 gscale=1;
953 }
954
955 void refreshhin() {
956 int de,nd,f,i,j,k,l,md,x,y;
957 struct square sq;
958 ABM m;
959 int c;
960 char s[SLEN];
961 double sc,u,v;
962 cairo_t*cc;
963
964 cc=cairo_create(sfh);
965 setrgbacolor(cc,0.0,0.0,0.0,0.0);
966 cairo_set_operator(cc,CAIRO_OPERATOR_SOURCE);
967 cairo_paint(cc);
968 cairo_set_operator(cc,CAIRO_OPERATOR_OVER);
969 cairo_translate(cc,0.5,0.5);
970 gscale=pxsq;
971 setrgbcolor(cc,0,0,0);
972 for(y=0;y<MXSZ;y++) for(x=0;x<MXSZ;x++) if(isingrid(x,y)&&isownmergerep(x,y)) {
973 l=getmergegroup(0,0,x,y);
974 md=getmergedir(x,y);
975 if(md<0) md=0;
976 f=getflags(x,y);
977 if((f&9)!=0) continue; // not empty square
978 sc=getscale(x,y,0,l);
979 c=geteicc(x,y);
980 if(c==-3) { // not the single-character case
981 // printf("refreshhin A:");
982 de=getdech(x,y);
983 if(de==0) nd=1,de=1; else nd=ndir[gtype];
984 memcpy(&sq,&gsq[x][y],sizeof(sq)); // in particular initialise ctlen[]:s
985 // printf("*** %d ",gsq[x][y].ctlen[0]);
986 for(i=0;i<nd;i++) for(j=0;j<gsq[x][y].ctlen[i];j++) {
987 if(!onebit(sq.ctbm[i][j])&&sq.ents[i][j]!=0) sq.ctbm[i][j]=sq.ents[i][j]->flbm; // not already entered? show hints
988 }
989 setrgbcolor(cc,0.75,0.75,0.75);
990 drawct(cc,x,y,&sq,1,1,1.0);
991 }
992 else if(c<0) { // normally checked one-character cell with none/more than one bit set in bitmap
993 if(gsq[x][y].ents[0][0]!=0) m=gsq[x][y].ents[0][0]->flbm;
994 else m=0;
995 k=cbits(m); // any info from filler?
996 if(k==0) strcpy(s,"?"); // no feasible letters
997 else if(k==1) strcpy(s,icctoutf8[abmtoicc(m)]); // unique feasible letter
998 else strcpy(s," ");
999 setrgbcolor(cc,0.75,0.75,0.75);
1000 mgcentre(&u,&v,x,y,md,l);
1001 ctext(cc,s,u,v+0.3*sc,.7*sc,getfstyle(x,y),1);
1002 if(k>1&&k<pxsq/2) { // more than one feasible letter
1003 u=1.0/k*sc; // size of red square
1004 setrgbcolor(cc,1,0,0);
1005 movetomgcentre(cc,x,y,md,l);
1006 rmoveto(cc,u/2.0,u/2.0);
1007 rlineto(cc,0,-u);
1008 rlineto(cc,-u,0);
1009 rlineto(cc,0,u);
1010 closepath(cc);
1011 fill(cc);
1012 }
1013 }
1014 }
1015 cairo_destroy(cc);
1016 invaldaall();
1017 gscale=1;
1018 }
1019
1020 void refreshall() {int i,j;
1021 for(j=0;j<MXSZ;j++) for(i=0;i<MXSZ;i++) if(isingrid(i,j)) {refreshsq(i,j);}
1022 refreshcur();
1023 refreshnum();
1024 refreshhin();
1025 refreshsel();
1026 }
1027
1028
1029 static int gwidth(int sq,int ba) {
1030 switch(gshape[gtype]) {
1031 case 0:return width *sq+ba*2-1;
1032 case 1:return (int)floor((width*1.2+0.4)*sq+ba*2-1+.5);
1033 case 2:if(EVEN(height)) return (int)floor((width*1.4+0.7)*sq+ba*2-1+.5);
1034 else return (int)floor((width*1.4 )*sq+ba*2-1+.5);
1035 case 3:case 4:return height*sq*2+ba*2-1;
1036 }
1037 assert(0);
1038 return 0; // keep VC happy
1039 }
1040
1041 static int gheight(int sq,int ba) {
1042 switch(gshape[gtype]) {
1043 case 0:return (int)floor(height*sq+ba*2-1+.5);
1044 case 1:if(EVEN(width)) return (int)floor((height*1.4+0.7)*sq+ba*2-1+.5);
1045 else return (int)floor((height*1.4 )*sq+ba*2-1+.5);
1046 case 2:return (int)floor((height*1.2+0.4)*sq+ba*2-1+.5);
1047 case 3:case 4:return height*sq*2+ba*2-1;
1048 }
1049 assert(0);
1050 return 0; // keep VC happy
1051 }
1052
1053 int dawidth(void) {return gwidth (pxsq,bawdpx);}
1054 int daheight(void) {return gheight(pxsq,bawdpx);}
1055
1056
1057 // EXPORT FUNCTIONS
1058
1059 // XML escape
1060 static void xmlesc(FILE*fp,char*p) {
1061 char*s;
1062 s=g_markup_escape_text(p,-1);
1063 if(s) {
1064 fputs(s,fp);
1065 g_free(s);
1066 }
1067 }
1068
1069 // XML escape with font styling
1070 static void xmlescfs(FILE*fp,char*s,int fs) {
1071 if(fs&1) fprintf(fp,"<b>");
1072 if(fs&2) fprintf(fp,"<i>");
1073 xmlesc(fp,s);
1074 if(fs&2) fprintf(fp,"</i>");
1075 if(fs&1) fprintf(fp,"</b>");
1076 }
1077
1078 // convert light/answer to string form for display
1079 void ansform(char*t0,int t0l,int ln,int wlen,unsigned int dmask) {
1080 struct answer*a;
1081 int em,f,i;
1082 em=lts[ln].em;
1083 t0[0]=0;
1084 if(em>0&&em<4) { // special entry method (but not jumble)? give grid form too
1085 for(i=0;lts[ln].s[i];i++) strcat(t0,icctoutf8[(int)lts[ln].s[i]]);
1086 strcat(t0,": ");
1087 }
1088 DEB_EX printf("ansform: t0=<%s> ln=%d dmask=0x%08x\n",t0,ln,dmask);
1089 if(lts[ln].ans<0) return; // negative ans values should not occur here
1090 a=ansp[lts[ln].ans];
1091 f=0;
1092 for(;a;a=a->acf) {
1093 DEB_EX printf(" a=%p cfdmask=%08x cf=<%s> acf=%p t0=<%s>\n",(void*)a,a->cfdmask,a->cf,(void*)a->acf,t0);
1094 if(a->cfdmask&dmask) {
1095 if((int)strlen(t0)+(int)strlen(a->cf)+2>t0l-LEMDESCLEN-10) {strcat(t0,"...");break;}
1096 if(f) strcat(t0,", ");
1097 strcat(t0,a->cf);
1098 f++;
1099 DEB_EX printf(" t0=<%s>\n",t0);
1100 }
1101 }
1102 if(em>0) strcat(t0,lemdesc[lts[ln].em]);
1103 DEB_EX printf(" done: acf=%p t0=<%s>\n",(void*)a->acf,t0);
1104 }
1105
1106 // Output answers in direction dir; if dir==-1 do VL:s
1107 // html=0: plain text
1108 // html=1: block HTML with enumeration
1109 // html=2: block HTML
1110 // html=3: table HTML
1111 // html=4: CCW XML words
1112 // html=5: CCW XML clues
1113 // cf=1: word lists are valid, can be used for citation forms
1114 // wordid: pointer to wordid counter, used for CCW XML output
1115 static void panswers(FILE*fp,int dir,int html,int cf,int*wordid) {
1116 int d,f,i,j,k,ll,m,m0,m1,n,vlf;
1117 char s[SLEN];
1118 char t[MXLE*16+1];
1119 char t0[MXFL*10+100];
1120 struct word*w;
1121
1122 vlf=(dir==-1); // doing virtual lights?
1123 for(j=0;j<(vlf?1:height);j++) for(i=0;i<(vlf?1:width);i++) for(d=vlf?100:dir;d<(vlf?100+nvl:(dir+1));d++) { // loop over all possible answers
1124 if(!vlf&&!isstartoflight(i,j,d)) continue;
1125 if(ismux(i,j,d)) {m0=0;m1=getmaxmux(i,j,d);}
1126 else {m0=-1;m1=0;}
1127 for(m=m0;m<m1;m++) { // loop over all multiplexes
1128 ll=getwordutf8(i,j,d,t,m);
1129 if(ll<2) {
1130 if(ll==-2&&html<3) fprintf(fp,"(entry too long)\n");
1131 continue; // silently skip other errors
1132 }
1133 f=cf;
1134 for(k=0;t[k];k++) if(t[k]=='.'||t[k]=='?') f=0;
1135 // f=1 now if we are to print citation forms
1136 n=vlf?d-100+1:gsq[i][j].number; // answer number
1137 if(n>0) sprintf(s,"%d",n);
1138 else strcpy(s,"-"); // unnumbered light
1139 if (html==0) fprintf(fp,"%s ",s);
1140 else if(html==1) fprintf(fp,"<b>%s</b>&nbsp;",s);
1141 else if(html==2) fprintf(fp,"<b>%s</b>&nbsp;",s);
1142 else if(html==3) fprintf(fp,"<tr><td width=\"0\" align=\"right\" valign=\"top\"><b>%s</b></td>\n<td valign=\"top\">",s);
1143 else if(html==4) {
1144 if(dir==0) fprintf(fp,"<word id=\"%d\" x=\"%d-%d\" y=\"%d\"></word>\n",*wordid,i+1,i+ll,j+1);
1145 if(dir==1) fprintf(fp,"<word id=\"%d\" x=\"%d\" y=\"%d-%d\"></word>\n",*wordid,i+1,j+1,j+ll);
1146 }
1147 else if(html==5) fprintf(fp,"<clue word=\"%d\" number=\"%s\" format=\"%d\">",*wordid,s,ll);
1148 if(html!=4) {
1149 if(f) {
1150 if(vlf) w=vls[d-100].w;
1151 else w=gsq[i][j].w[d][(m<0)?0:m];
1152 }
1153 else w=0;
1154 if(w&&w->flistlen>0) {
1155 for(k=0;k<w->flistlen;k++) {
1156 if(k>0) fprintf(fp,"; ");
1157 ansform(t0,sizeof(t0),w->flist[k],w->wlen,w->lp->dmask);
1158 if(html==0) fprintf(fp,"%s",t0);
1159 else xmlesc(fp,t0);
1160 }
1161 }
1162 else xmlesc(fp,t);
1163 }
1164 if (html==0) fprintf(fp," (%d)\n",ll);
1165 else if(html==1) fprintf(fp,"&nbsp;(%d)<br>\n",ll);
1166 else if(html==2) fprintf(fp,"\n");
1167 else if(html==3) fprintf(fp,"&nbsp;(%d)</td></tr>\n",ll); // answer and length
1168 else if(html==5) fprintf(fp,"</clue>\n");
1169 if(wordid) (*wordid)++;
1170 }
1171 }
1172 }
1173
1174 // HTML export
1175 // f b0: include grid
1176 // f b1: include answers in grid
1177 // f b2: include numbers in grid
1178 // f b3: include answers in table (to be edited into clues)
1179 // f b4: include answers in compact format
1180 // f b5: include link to grid image (given URL)
1181 // Thanks to Paul Boorer for CSS suggestions
1182 void a_exportgh(int f,char*url) {
1183 int c,cf,d,fl,i,j,i0,j0,k,l,md,u,v,x,y,bg,fg,mk,fs,fh;
1184 int de,nd;
1185 int hbawd;
1186 double textscale;
1187 FILE*fp;
1188
1189 textscale=((f&6)==6)?0.8:1; // smaller text if we are doing numbers and entries
1190 hbawd=hpxsq/8; // a suitable bar width
1191 if(hbawd<3) hbawd=3;
1192 hbawd|=1; // ensure it is odd
1193 DEB_EX printf("export HTML %s\n",filename);
1194 fp=q_fopen(filename,"w");
1195 if(!fp) {fserror();return;}
1196 cf=!preexport();
1197 // emit header and CSS
1198 fprintf(fp,
1199 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n"
1200 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
1201 "<html>\n"
1202 "<!-- Created by Qxw %s http://www.quinapalus.com -->\n"
1203 "<head>\n"
1204 "<title>",RELEASE);
1205 xmlesc(fp,titlebyauthor());
1206 fprintf(fp,"</title>\n");
1207 fprintf(fp,"<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">\n");
1208 if(f&1) { // need the styles for the grid
1209 fprintf(fp,"<style type=\"text/css\">\n");
1210 fprintf(fp,"div.bk {position:absolute;font-size:0px;border-left:black 0px solid;border-right:black 0px solid;border-top:black %dpx solid;border-bottom:black 0px solid;width:%dpx;height:%dpx;}\n",hpxsq,hpxsq,hpxsq);
1211 fprintf(fp,"div.hb {position:absolute;font-size:0px;border-left:black 0px solid;border-right:black 0px solid;border-top:black %dpx solid;border-bottom:black 0px solid;width:%dpx;}\n",hbawd,hpxsq+1);
1212 fprintf(fp,"div.hb0 {position:absolute;font-size:0px;border-left:black 0px solid;border-right:black 0px solid;border-top:black %dpx solid;border-bottom:black 0px solid;width:%dpx;}\n",hbawd,hpxsq+hbawd/2+1);
1213 fprintf(fp,"div.vb {position:absolute;font-size:0px;border-left:black %dpx solid;border-right:black 0px solid;border-top:black 0px solid;border-bottom:black 0px solid;height:%dpx;}\n",hbawd,hpxsq+1);
1214 fprintf(fp,"div.vb0 {position:absolute;font-size:0px;border-left:black %dpx solid;border-right:black 0px solid;border-top:black 0px solid;border-bottom:black 0px solid;height:%dpx;}\n",hbawd,hpxsq+hbawd/2+1);
1215 fprintf(fp,"div.hr {position:absolute;font-size:0px;border-left:black 0px solid;border-right:black 0px solid;border-top:black 1px solid;border-bottom:black 0px solid;}\n");
1216 fprintf(fp,"div.hr {position:absolute;font-size:0px;border-left:black 0px solid;border-right:black 0px solid;border-top:black 1px solid;border-bottom:black 0px solid;}\n");
1217 fprintf(fp,"div.vr {position:absolute;font-size:0px;border-left:black 1px solid;border-right:black 0px solid;border-top:black 0px solid;border-bottom:black 0px solid;}\n");
1218 fprintf(fp,"div.nu {position:absolute;font-size:%dpx;font-family:sans-serif;width:%dpx;height:%dpx}\n",hpxsq/3,hpxsq-4,hpxsq-4);
1219 fprintf(fp,"div.lt {position:absolute;text-align:center;width:%dpx;font-family:sans-serif}\n",hpxsq);
1220 fprintf(fp,"</style>\n");
1221 }
1222 fprintf(fp,"</head>\n\n<body>\n");
1223 if(f&1) {
1224 fprintf(fp,"<center>\n");
1225 fprintf(fp,"<div style=\"border-width:0px;width:%dpx;height:%dpx;position:relative;\">\n",width*hpxsq+1,height*hpxsq+1);
1226 fprintf(fp,"<div style=\"border-width:0px;top:0px;left:0px;width:%dpx;height:%dpx;position:absolute;\">\n",width*hpxsq+1,height*hpxsq+1);
1227 for(j=0;j<height;j++) for(i=0;i<width;i++) {
1228 bg=getbgcol(i,j);
1229 fl=getflags(i,j);
1230 if(fl&1) {fprintf(fp,"<div class=\"bk\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq+1,j*hpxsq+1);continue;}
1231 fprintf(fp,"<div class=\"bk\" style=\"left:%dpx;top:%dpx;border-top:#%06X %dpx solid;\"></div>\n",i*hpxsq+1,j*hpxsq+1,bg,hpxsq);
1232 }
1233 for(j=0;j<height;j++) for(i=0;i<width;i++) { // plots each bar twice
1234 if(isbar(i,j,0)) {
1235 if(isbar(i,j,1)) fprintf(fp,"<div class=\"vb0\" style=\"left:%dpx;top:%dpx;\"></div>\n",(i+1)*hpxsq-hbawd/2,j*hpxsq);
1236 else fprintf(fp,"<div class=\"vb\" style=\"left:%dpx;top:%dpx;\"></div>\n",(i+1)*hpxsq-hbawd/2,j*hpxsq);
1237 }
1238 if(isbar(i,j,1)) {
1239 if(isbar(i,j,2)) fprintf(fp,"<div class=\"hb0\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq-hbawd/2,(j+1)*hpxsq-hbawd/2);
1240 else fprintf(fp,"<div class=\"hb\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq,(j+1)*hpxsq-hbawd/2);
1241 }
1242 if(isbar(i,j,2)) {
1243 if(isbar(i,j,3)) fprintf(fp,"<div class=\"vb0\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq-hbawd/2,j*hpxsq-hbawd/2);
1244 else fprintf(fp,"<div class=\"vb\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq-hbawd/2,j*hpxsq);
1245 }
1246 if(isbar(i,j,3)) {
1247 if(isbar(i,j,0)) fprintf(fp,"<div class=\"hb0\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq,j*hpxsq-hbawd/2);
1248 else fprintf(fp,"<div class=\"hb\" style=\"left:%dpx;top:%dpx;\"></div>\n",i*hpxsq,j*hpxsq-hbawd/2);
1249 }
1250 for(c=0;c<4;c++) {
1251 char*p;
1252 p=getmktext(i,j,c,!!(f&4)); // pass number-enable flag
1253 if(!strcmp(p,"")) continue;
1254 mk=getmkcol(i,j);
1255 switch(c) {
1256 case 0: fprintf(fp,"<div class=\"nu\" style=\"left:%dpx;top:%dpx;color:%06X;text-align:left\" >",i*hpxsq+4,j*hpxsq+1 ,mk); xmlesc(fp,p); fprintf(fp,"</div>\n"); break;
1257 case 1: fprintf(fp,"<div class=\"nu\" style=\"left:%dpx;top:%dpx;color:%06X;text-align:right\">",i*hpxsq+1,j*hpxsq+1 ,mk); xmlesc(fp,p); fprintf(fp,"</div>\n"); break;
1258 case 2: fprintf(fp,"<div class=\"nu\" style=\"left:%dpx;top:%dpx;color:%06X;text-align:right\">",i*hpxsq+1,j*hpxsq+hpxsq*2/3-4,mk); xmlesc(fp,p); fprintf(fp,"</div>\n"); break;
1259 case 3: fprintf(fp,"<div class=\"nu\" style=\"left:%dpx;top:%dpx;color:%06X;text-align:left\" >",i*hpxsq+4,j*hpxsq+hpxsq*2/3-4,mk); xmlesc(fp,p); fprintf(fp,"</div>\n"); break;
1260 }
1261 }
1262 }
1263
1264 for(j=0;j<height;j++) for(i=0;i<width;i++) {
1265 if((f&2)&&isingrid(i,j)&&isownmergerep(i,j)) {
1266 char s[MAXNDIR*MXCT*16+1];
1267 fg=getfgcol(i,j);
1268 fs=getfstyle(i,j);
1269 l=getmergegroup(0,0,i,j);
1270 md=getmergedir(i,j);
1271 if(md<0) md=0;
1272 i0=i; j0=j;
1273 while(l>2) stepforw(&i0,&j0,md),l-=2;
1274 if((md==0&&i0==width-1)||(md==1&&j0==height-1)) l=1;
1275 x=((i0*2+(md==0)*(l-1))*hpxsq)/2+1;
1276 y=((j0*2+(md==1)*(l-1))*hpxsq)/2+hpxsq/2;
1277 de=getdech(i,j);
1278 nd=ndir[gtype];
1279 if(de==0) nd=1,de=1;
1280 if(de==2) {
1281 l=3;
1282 for(k=0;k<nd;k++) if(gsq[i][j].ctlen[k]>l) l=gsq[i][j].ctlen[k]; // find longest
1283 fh=(int)floor(hpxsq*textscale/l+.5);
1284 abmstodispstr(s,gsq[i][j].ctbm[0],gsq[i][j].ctlen[0]);
1285 fprintf(fp,"<div class=\"lt\" style=\"left:%dpx;top:%dpx;color:%06X;font-size:%dpx;\">",x,y-fh,fg,fh);
1286 xmlescfs(fp,s,fs);
1287 fprintf(fp,"</div>\n");
1288 abmstodispstr(s,gsq[i][j].ctbm[1],gsq[i][j].ctlen[1]);
1289 fprintf(fp,"<div class=\"lt\" style=\"left:%dpx;top:%dpx;color:%06X;font-size:%dpx;\">",x,y,fg,fh);
1290 xmlescfs(fp,s,fs);
1291 fprintf(fp,"</div>\n");
1292 } else {
1293 s[0]=0;
1294 for(k=0,l=0;k<nd;k++) abmstodispstr(s+strlen(s),gsq[i][j].ctbm[k],gsq[i][j].ctlen[k]),l+=gsq[i][j].ctlen[k]; // total length of contents
1295 if(l<=1) fh=(int)floor(hpxsq*textscale*3/4+.5);
1296 else fh=(int)floor(hpxsq*textscale/l+.5);
1297 fprintf(fp,"<div class=\"lt\" style=\"left:%dpx;top:%dpx;color:%06X;font-size:%dpx;\">",x,y-fh/2,fg,fh);
1298 xmlescfs(fp,s,fs);
1299 fprintf(fp,"</div>\n");
1300 }
1301 }
1302 }
1303 for(j=0;j<=height;j++) for(i=0,v=0;i<=width;i++) {
1304 if(j==0) u=!ismerge(i,j ,3);
1305 else u=!ismerge(i,j-1,1);
1306 u&=sqexists(i,j)|sqexists(i,j-1);
1307 if(u==0&&v>0) fprintf(fp,"<div class=\"hr\" style=\"left:%dpx;top:%dpx;width:%dpx;\"></div>\n",(i-v)*hpxsq,j*hpxsq,v*hpxsq+1);
1308 if(u==0) v=0; else v++;
1309 }
1310 for(i=0;i<=width;i++) for(j=0,v=0;j<=height;j++) {
1311 if(i==0) u=!ismerge(i ,j,2);
1312 else u=!ismerge(i-1,j,0);
1313 u&=sqexists(i,j)|sqexists(i-1,j);
1314 if(u==0&&v>0) fprintf(fp,"<div class=\"vr\" style=\"left:%dpx;top:%dpx;height:%dpx;\"></div>\n",i*hpxsq,(j-v)*hpxsq,v*hpxsq+1);
1315 if(u==0) v=0; else v++;
1316 }
1317 fprintf(fp,"</div></div></center>\n");
1318 }
1319 if(f&0x20) {
1320 fprintf(fp,"<center>\n");
1321 fprintf(fp,"<img src=\"%s\" alt=\"grid image\">\n",url);
1322 fprintf(fp,"</center>\n");
1323 }
1324
1325 if(f&8) { // print table of `clues'
1326 fprintf(fp,"<center><table><tr>\n");
1327 for(d=0;d<ndir[gtype];d++) {
1328 fprintf(fp,"<td width=\"%d%%\"><b>%s</b></td>\n",96/ndir[gtype],dname[gtype][d]);
1329 if(d<ndir[gtype]-1) fprintf(fp,"<td>&nbsp;</td>\n");
1330 }
1331 fprintf(fp,"</tr><tr>\n");
1332 for(d=0;d<ndir[gtype];d++) {
1333 fprintf(fp,"<td valign=\"top\"><table>\n");
1334 panswers(fp,d,3,cf,0);
1335 fprintf(fp,"</table></td>\n");
1336 if(d<ndir[gtype]-1) fprintf(fp,"<td>&nbsp;</td>\n");
1337 }
1338 fprintf(fp,"</tr>\n");
1339 if(nvl>0) {
1340 fprintf(fp,"<tr><td colspan=\"%d\"><b>Other</b></td></tr>\n",ndir[gtype]*2-1);
1341 fprintf(fp,"<tr><td colspan=\"%d\"><table>\n",ndir[gtype]*2-1);
1342 panswers(fp,-1,2,cf,0);
1343 fprintf(fp,"</table></td></tr>\n");
1344 }
1345 fprintf(fp,"</table></center>\n");
1346 }
1347
1348 if(f&16) { // print answers in condensed form
1349 for(d=0;d<ndir[gtype];d++) {
1350 fprintf(fp,"<b>%s:</b>\n",dname[gtype][d]);
1351 panswers(fp,d,2,cf,0);
1352 fprintf(fp,"<br>\n");
1353 }
1354 if(nvl>0) {
1355 fprintf(fp,"<b>Other:</b>\n");
1356 panswers(fp,-1,2,cf,0);
1357 fprintf(fp,"<br>\n");
1358 }
1359 }
1360
1361 fprintf(fp,"</body></html>\n");
1362 if(ferror(fp)) fserror();
1363 if(fclose(fp)) fserror();
1364 postexport();
1365 }
1366
1367
1368 // GRID AS EPS OR PNG
1369
1370 static cairo_status_t filewrite(FILE*fp,unsigned char*p,unsigned int n) {
1371 return (fwrite(p,1,n,fp)==n)?CAIRO_STATUS_SUCCESS:CAIRO_STATUS_WRITE_ERROR;
1372 }
1373
1374 // f0 b1: include answers in grid
1375 // f0 b2: include numbers and marks in grid
1376 // f1: 0=EPS 1=SVG 2=PNG
1377 void a_exportg(char*fn,int f0,int f1) {
1378 cairo_surface_t*sf=0;
1379 cairo_t*cc;
1380 int i,j,sq=0,ba,px,py;
1381 char s[1000];
1382 double textscale;
1383 FILE*fp=0;
1384
1385 DEB_EX printf("a_exportg(\"%s\",%d,%d)\n",fn,f0,f1);
1386 switch(f1) {
1387 case 0: sq=eptsq; break;
1388 case 1: sq=eptsq; break;
1389 case 2: sq=hpxsq; break;
1390 }
1391 textscale=((f0&6)==6)?0.8:1; // smaller text if we are doing numbers and entries
1392 ba=sq/8; // a suitable bar width
1393 if(ba<2) ba=2;
1394 px=gwidth(sq,ba);
1395 py=gheight(sq,ba);
1396 switch(f1) {
1397 case 0:
1398 fp=q_fopen(fn,"w");
1399 if(!fp) {fserror();return;}
1400 sf=cairo_ps_surface_create_for_stream((cairo_write_func_t)filewrite,fp,px,py);
1401 cairo_ps_surface_restrict_to_level(sf,CAIRO_PS_LEVEL_2);
1402 cairo_ps_surface_set_eps(sf,1);
1403 sprintf(s,"%%%%Title: %s generated by Qxw",fn);
1404 cairo_ps_surface_dsc_comment(sf,s);
1405 break;
1406 case 1:
1407 fp=q_fopen(fn,"w");
1408 if(!fp) {fserror();return;}
1409 sf=cairo_svg_surface_create_for_stream((cairo_write_func_t)filewrite,fp,px,py);
1410 cairo_svg_surface_restrict_to_version(sf,CAIRO_SVG_VERSION_1_1);
1411 break;
1412 case 2:
1413 sf=cairo_image_surface_create(CAIRO_FORMAT_ARGB32,px,py);
1414 break;
1415 }
1416 cc=cairo_create(sf);
1417 if(f1==2) {
1418 cairo_set_source_rgb(cc,1,1,1); // flood PNG to white to avoid nasty transparency effect in merged cells
1419 cairo_paint(cc);
1420 }
1421 cairo_translate(cc,ba-.5,ba-.5);
1422 gscale=sq;
1423 for(j=0;j<MXSZ;j++) for(i=0;i<MXSZ;i++) if(isingrid(i,j)) {
1424 drawsqbg(cc,i,j,sq,1);
1425 }
1426 for(j=0;j<MXSZ;j++) for(i=0;i<MXSZ;i++) if(isingrid(i,j)) {
1427 gsave(cc);
1428 drawsqfg(cc,i,j,sq,ba,!!(f0&2),0,textscale);
1429 grestore(cc);
1430 }
1431 setrgbcolor(cc,0,0,0);
1432 drawnums(cc,!!(f0&4));
1433 switch(f1) {
1434 case 0:
1435 case 1:
1436 cairo_show_page(cc);
1437 break;
1438 case 2:
1439 if(cairo_surface_write_to_png(sf,fn)!=CAIRO_STATUS_SUCCESS) fsgerr();
1440 break;
1441 }
1442 cairo_destroy(cc);
1443 cairo_surface_destroy(sf);
1444 switch(f1) {
1445 case 0:
1446 case 1:
1447 if(ferror(fp)) {fserror();fclose(fp);}
1448 else if(fclose(fp)) fserror();
1449 break;
1450 case 2:
1451 break;
1452 }
1453 gscale=1;
1454 }
1455
1456
1457 // ANSWERS
1458
1459 // f: 0=text, 1=HTML
1460 void a_exporta(int f) {
1461 int cf,d;
1462 FILE*fp;
1463
1464 cf=!preexport();
1465 fp=q_fopen(filename,"w");
1466 if(!fp) {fserror();return;}
1467 if(!f) {
1468 fprintf(fp,"# file %s generated by Qxw %s: http://www.quinapalus.com\n",filename,RELEASE);
1469 } else {
1470 fprintf(fp,"<html><head>\n");
1471 fprintf(fp,"<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">\n");
1472 fprintf(fp,"</head>\n\n<body>\n<!-- Created by Qxw %s http://www.quinapalus.com -->\n",RELEASE);
1473 }
1474 for(d=0;d<ndir[gtype];d++) {
1475 if(f) fprintf(fp,"\n<b>%s</b><br>\n\n",dname[gtype][d]);
1476 else fprintf(fp,"\n%s\n\n",dname[gtype][d]);
1477 panswers(fp,d,f,cf,0);
1478 }
1479 if(nvl>0) {
1480 if(f) fprintf(fp,"\n<b>Other</b><br>\n\n");
1481 else fprintf(fp,"\nOther\n\n");
1482 panswers(fp,-1,f,cf,0);
1483 }
1484 if(f) fprintf(fp,"</body></html>\n");
1485 if(ferror(fp)) fserror();
1486 if(fclose(fp)) fserror();
1487 postexport();
1488 }
1489
1490 // COMBINED HTML AND PNG
1491
1492 // f: 0=puzzle, 1=solution
1493 // f0: f0 flags for exportg
1494 // f1: 1=SVG, 2=PNG
1495 void a_exporthp(int f,int f0,int f1) {
1496 char url[SLEN+200];
1497 char*base;
1498 strcpy(url,filename);
1499 strcat(url,"_files");
1500 g_mkdir(url,0777);
1501 base=g_path_get_basename(url);
1502 strcat(url,f?"/solution":"/grid");
1503 strcat(url,f1==1?".svg":".png");
1504 a_exportg(url,f0,f1); // write out the image
1505 strcpy(url,base);
1506 strcat(url,f?"/solution":"/grid");
1507 strcat(url,f1==1?".svg":".png");
1508 a_exportgh(f?0x30:0x28,url); // write out the HTML with a link to the image
1509 g_free(base);
1510 }
1511
1512 // Crossword Compiler XML
1513 // only the most basic of Qxw's features are represented in this export format
1514 void a_exportccwxml(char*fn) {
1515 FILE*fp;
1516 int cf,d,fl,i,j,n,wordid;
1517 if(gshape[gtype]!=0) { // check for basic limitations
1518 reperr("Cannot export non-rectangular grids\nto Crossword Compiler XML");
1519 return;
1520 }
1521 for(j=0;j<height;j++) for(i=0;i<width;i++) {
1522 if(gsq[i][j].ctlen[0]!=1||gsq[i][j].ctlen[1]!=0) {
1523 reperr("Cannot export multi-character or\nde-checked cells to Crossword Compiler XML");
1524 return;
1525 }
1526 if(!isownmergerep(i,j)) {
1527 reperr("Cannot export merged cells\nto Crossword Compiler XML");
1528 return;
1529 }
1530 }
1531 cf=!preexport();
1532 fp=q_fopen(filename,"w");
1533 if(!fp) {fserror();return;}
1534 fprintf(fp,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1535 fprintf(fp,"<crossword-compiler xmlns=\"http://crossword.info/xml/crossword-compiler\">\n");
1536 fprintf(fp,"<rectangular-puzzle xmlns=\"http://crossword.info/xml/rectangular-puzzle\" alphabet=\"");
1537 for(i=0;i<niccused;i++) xmlesc(fp,icctoutf8[(int)iccused[i]]);
1538 fprintf(fp,"\">\n\n");
1539 fprintf(fp,"<metadata>\n");
1540 fprintf(fp,"<title>"); xmlesc(fp,gtitle); fprintf(fp,"</title>\n");
1541 fprintf(fp,"<creator>"); xmlesc(fp,gauthor); fprintf(fp,"</creator>\n");
1542 fprintf(fp,"<created>Qxw %s http://www.quinapalus.com</created>\n",RELEASE);
1543 fprintf(fp,"<copyright></copyright>\n");
1544 fprintf(fp,"<description></description>\n");
1545 fprintf(fp,"</metadata>\n\n");
1546
1547 fprintf(fp,"<crossword>\n\n");
1548
1549 fprintf(fp,"<grid width=\"%d\" height=\"%d\">\n",width,height);
1550 fprintf(fp,"<grid-look numbering-scheme=\"normal\" cell-size-in-pixels=\"%d\"></grid-look>\n",hpxsq);
1551 for(j=0;j<height;j++) for(i=0;i<width;i++) {
1552 fl=getflags(i,j);
1553 fprintf(fp,"<cell x=\"%d\" y=\"%d\"",i+1,j+1);
1554 if(fl&8) {fprintf(fp," type=\"void\""); goto ew0;}
1555 if(fl&1) {fprintf(fp," type=\"block\""); goto ew0;}
1556 fprintf(fp," foreground-color=\"#%06X\"",getfgcol(i,j));
1557 // apparently CC uses "background-color" to specify the colour of the circle if present, the background
1558 // colour otherwise; we use a dimmed version of the foreground colour
1559 if(isanymkcirc(i,j)) fprintf(fp," background-color=\"#%06X\" background-shape=\"circle\"",(((getfgcol(i,j)>>1)&0x7f7f7f)|0x808080));
1560 else fprintf(fp," background-color=\"#%06X\"", getbgcol(i,j));
1561 if(gsq[i][j].ctlen[0]==1&&onebit(gsq[i][j].ctbm[0][0])) { // only one character allowed
1562 fprintf(fp," solution=\"");
1563 xmlesc(fp,icctoutf8[abmtoicc(gsq[i][j].ctbm[0][0])]);
1564 fprintf(fp,"\"");
1565 }
1566 n=getnumber(i,j);
1567 if(n>0) fprintf(fp," number=\"%d\"",n);
1568 if(isbar(i,j,2)) fprintf(fp," left-bar=\"true\"");
1569 if(isbar(i,j,3)) fprintf(fp," top-bar=\"true\"");
1570 ew0:
1571 fprintf(fp,"></cell>\n");
1572 }
1573 fprintf(fp,"</grid>\n\n");
1574
1575 wordid=1;
1576 panswers(fp,0,4,cf,&wordid);
1577 panswers(fp,1,4,cf,&wordid);
1578
1579 wordid=1;
1580 for(d=0;d<2;d++) {
1581 fprintf(fp,"<clues ordering=\"normal\"><title><b>%s</b></title>\n",dname[gtype][d]);
1582 panswers(fp,d,5,cf,&wordid);
1583 fprintf(fp,"</clues>\n");
1584 }
1585
1586 fprintf(fp,"</crossword>\n\n");
1587 fprintf(fp,"</rectangular-puzzle>\n");
1588 fprintf(fp,"</crossword-compiler>\n");
1589 if(ferror(fp)) fserror();
1590 if(fclose(fp)) fserror();
1591 postexport();
1592 }