2 /* Copyright (C) 1994, 2000, 2001, 2003, 2004, 2005, 2009
3 Free Software Foundation, Inc.
4 Written by Francisco Andrés Verdú <pandres@dragonet.es>
6 groff is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 groff is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* This file contains a set of utility functions to use canon CAPSL printers
20 * (lbp-4 and lbp-8 series printers) */
28 static FILE *lbpoutput = NULL;
29 static FILE *vdmoutput = NULL;
33 lbpinit(FILE *outfile)
40 lbpprintf(const char *format, ... )
41 { /* Taken from cjet */
44 va_start(stuff, format);
45 vfprintf(lbpoutput, format, stuff);
51 lbpputs(const char *data)
53 fputs(data,lbpoutput);
58 lbpputc(unsigned char c)
65 lbpsavestatus(int idx )
67 fprintf(lbpoutput,"\033[%d%%y",idx);
72 lbprestorestatus(int idx )
74 fprintf(lbpoutput,"\033[%d%cz",idx ,'%');
81 fprintf(lbpoutput,"\033[1;%d;0x",idx);
86 lbprestorepos(int idx)
88 fprintf(lbpoutput,"\033[0;%d;0x",idx);
93 lbprestoreposx(int idx)
95 fprintf(lbpoutput,"\033[0;%d;1x",idx);
100 lbpmoverel(int despl, char direction)
102 fprintf(lbpoutput,"\033[%d%c",despl,direction);
107 lbplinerel(int width,int despl,char direction )
109 fprintf(lbpoutput,"\033[%d;0;9{\033[%d%c\033[9}",width,despl,direction);
114 lbpmoveabs(int x, int y)
116 fprintf(lbpoutput,"\033[%d;%df",y,x);
121 lbplineto(int x,int y, int width )
123 fprintf(lbpoutput,"\033[%d;0;9{",width);
125 fprintf(lbpoutput,"\033[9}\n");
130 lbpruleabs(int x, int y, int hsize, int vsize)
133 fprintf(lbpoutput,"\033[0;9;000s");
134 lbpmoveabs(x+hsize,y+vsize);
135 fprintf(lbpoutput,"\033[9r");
139 static void vdmprintf(const char *format, ... );
143 vdmnum(int num,char *result)
150 /* First byte 1024 - 32768 */
151 b1 = ((nm >> 10) & 0x3F);
152 if (b1) *p++ = b1 | 0x40;
154 /* Second Byte 16 - 1024 */
155 b2 = ((nm >> 4) & 0x3F);
156 if ( b1 || b2) *p++= b2 | 0x40;
158 /* Third byte 0 - 15 */
159 b3 = ((nm & 0x0F) | 32);
160 if (num >= 0) b3 |= 16;
162 *p = 0x00; /* End of the resulting string */
168 vdmorigin(int newx, int newy)
172 vdmprintf("}\"%s%s\x1e",vdmnum(newx,nx),vdmnum(newy,ny));
177 vdminit(FILE *vdmfile)
179 char scale[4],size[4],lineend[4];
181 /* vdmoutput = tmpfile();*/
183 /* Initialize the VDM mode */
184 vdmprintf("\033[0&}#GROLBP\x1e!0%s%s\x1e$\x1e}F%s\x1e",\
185 vdmnum(-3,scale),vdmnum(1,size),vdmnum(1,lineend));
199 vdmprintf(const char *format, ... )
200 { /* Taken from cjet */
203 if (vdmoutput == NULL) vdminit(tmpfile());
204 va_start(stuff, format);
205 vfprintf(vdmoutput, format, stuff);
211 vdmsetfillmode(int pattern,int perimeter, int inverted)
213 char patt[4],perim[4],
214 rot[4], /* rotation */
215 espejo[4], /* espejo */
216 inv[4]; /* Inverted */
218 vdmprintf("I%s%s%s%s%s\x1e",vdmnum(pattern,patt),\
219 vdmnum(perimeter,perim),vdmnum(0,rot),
220 vdmnum(0,espejo),vdmnum(inverted,inv));
225 vdmcircle(int centerx, int centery, int radius)
227 char x[4],y[4],rad[4];
229 vdmprintf("5%s%s%s\x1e",vdmnum(centerx,x),vdmnum(centery,y),\
235 vdmaarc(int centerx, int centery, int radius,int startangle,int angle,int style,int arcopen)
237 char x[4],y[4],rad[4],stx[4],sty[4],styl[4],op[4];
239 vdmprintf("}6%s%s%s%s%s%s%s\x1e",vdmnum(arcopen,op),\
240 vdmnum(centerx,x),vdmnum(centery,y),\
241 vdmnum(radius,rad),vdmnum(startangle,stx),vdmnum(angle,sty),\
247 vdmvarc(int centerx, int centery,int radius, int startx, int starty, int endx, int endy,\
248 int style,int arcopen)
250 char x[4],y[4],rad[4],stx[4],sty[4],enx[4],eny[4],styl[4],op[4];
252 vdmprintf("}6%s%s%s%s%s%s%s%s%s\x1e",vdmnum(arcopen,op),\
253 vdmnum(centerx,x),vdmnum(centery,y),\
254 vdmnum(radius,rad),vdmnum(startx,stx),vdmnum(starty,sty),\
255 vdmnum(endx,enx),vdmnum(endy,eny),vdmnum(style,styl));
260 vdmellipse(int centerx, int centery, int radiusx, int radiusy,int rotation)
262 char x[4],y[4],radx[4],rady[4],rotat[4];
264 vdmprintf("}7%s%s%s%s%s\x1e\n",vdmnum(centerx,x),vdmnum(centery,y),\
265 vdmnum(radiusx,radx),vdmnum(radiusy,rady),\
266 vdmnum(rotation,rotat));
271 vdmsetlinetype(int lintype)
273 char ltyp[4], expfact[4];
275 vdmprintf("E1%s%s\x1e",vdmnum(lintype,ltyp),vdmnum(1,expfact));
281 vdmsetlinestyle(int lintype, int pattern,int unionstyle)
283 char patt[4],ltip[4],
284 rot[4], /* rotation */
285 espejo[4], /* espejo */
286 in[4]; /* Inverted */
288 vdmprintf("}G%s%s%s%s%s\x1e",vdmnum(lintype,ltip),\
289 vdmnum(pattern,patt),vdmnum(0,rot),
290 vdmnum(0,espejo),vdmnum(0,in));
291 vdmprintf("}F%s",vdmnum(unionstyle,rot));
296 vdmlinewidth(int width)
300 vdmprintf("F1%s\x1e",vdmnum(width,wh));
305 vdmrectangle(int origx, int origy,int dstx, int dsty)
307 char xcoord[4],ycoord[4],sdstx[4],sdsty[4];
309 vdmprintf("}:%s%s%s%s\x1e\n",vdmnum(origx,xcoord),vdmnum(dstx,sdstx),\
310 vdmnum(origy,ycoord),vdmnum(dsty,sdsty));
315 vdmpolyline(int numpoints, int *points)
318 char xcoord[4],ycoord[4];
320 if (numpoints < 2) return;
321 vdmprintf("1%s%s",vdmnum(*p,xcoord),vdmnum(*(p+1),ycoord));
323 for (i = 1; i < numpoints ; i++) {
324 vdmprintf("%s%s",vdmnum(*p,xcoord),vdmnum(*(p+1),ycoord));
332 vdmpolygon(int numpoints, int *points)
335 char xcoord[4],ycoord[4];
337 if (numpoints < 2) return;
338 vdmprintf("2%s%s",vdmnum(*p,xcoord),vdmnum(*(p+1),ycoord));
340 for (i = 1; i < numpoints ; i++) {
341 vdmprintf("%s%s",vdmnum(*p,xcoord),vdmnum(*(p+1),ycoord));
349 /************************************************************************
350 * Highter level auxiliary functions *
351 ************************************************************************/
355 return (vdmoutput != NULL);
360 vdmline(int startx, int starty, int sizex, int sizey)
369 vdmpolyline(2,points);
374 /*#define THRESHOLD .05 */ /* inch */
375 #define THRESHOLD 1 /* points (1/300 inch) */
377 splinerel(double px,double py,int flush)
379 static int lx = 0 ,ly = 0;
380 static double pend = 0.0;
381 static int dy = 0, despx = 0, despy = 0, sigpend = 0;
382 int dxnew = 0, dynew = 0, sg;
383 char xcoord[4],ycoord[4];
386 if (flush == -1) {lx = (int)px; ly = (int)py; return;}
391 if ((dxnew == 0) && (dynew == 0)) return;
392 sg = (dxnew < 0)? -1 : 0;
393 /* fprintf(stderr,"s (%d,%d) (%d,%d)\n",dxnew,dynew,despx,despy);*/
396 if ((sg == sigpend) && (dy == 0)){
403 npend = (1.0*dxnew)/dynew;
404 if (( npend == pend) && (sigpend == sg))
405 { despy = dynew; despx = dxnew; return; }
409 } /* else (( npend == pend) && ... */
410 } /* else (if (dynew == 0)) */
413 /* if we've changed direction we must draw the line */
414 /* fprintf(stderr," (%d) %.2f,%.2f\n",flush,(float)px,(float)py);*/
415 if ((despx != 0) || (despy != 0)) vdmprintf("%s%s",vdmnum(despx,xcoord),\
416 vdmnum(despy,ycoord));
417 /*if ((despx != 0) || (despy != 0)) fprintf(stderr,"2
418 *%d,%d\n",despx,despy);*/
420 dxnew = dy = despx = despy = 0;
425 if ((dxnew != 0) || (dynew != 0)) vdmprintf("%s%s",vdmnum(dxnew,xcoord),\
426 vdmnum(dynew,ycoord));
428 /* if ((dxnew != 0) || (dynew != 0)) fprintf(stderr,"3
429 * %d,%d\n",dxnew,dynew);*/
430 lx = (int)px; ly = (int)py;
431 dxnew = dy = despx = despy = 0;
436 /**********************************************************************
437 * The following code to draw splines is adapted from the transfig package
440 quadratic_spline(double a_1, double b_1, double a_2, double b_2, \
441 double a_3, double b_3, double a_4, double b_4)
443 double x_1, y_1, x_4, y_4;
446 x_1 = a_1; y_1 = b_1;
447 x_4 = a_4; y_4 = b_4;
448 x_mid = (a_2 + a_3)/2.0;
449 y_mid = (b_2 + b_3)/2.0;
450 if ((fabs(x_1 - x_mid) < THRESHOLD)
451 && (fabs(y_1 - y_mid) < THRESHOLD)) {
452 splinerel(x_mid, y_mid, 0);
453 /* fprintf(tfp, "PA%.4f,%.4f;\n", x_mid, y_mid);*/
456 quadratic_spline(x_1, y_1, ((x_1+a_2)/2.0), ((y_1+b_2)/2.0),
457 ((3.0*a_2+a_3)/4.0), ((3.0*b_2+b_3)/4.0), x_mid, y_mid);
460 if ((fabs(x_mid - x_4) < THRESHOLD)
461 && (fabs(y_mid - y_4) < THRESHOLD)) {
462 splinerel(x_4, y_4, 0);
463 /* fprintf(tfp, "PA%.4f,%.4f;\n", x_4, y_4);*/
466 quadratic_spline(x_mid, y_mid,
467 ((a_2+3.0*a_3)/4.0), ((b_2+3.0*b_3)/4.0),
468 ((a_3+x_4)/2.0), ((b_3+y_4)/2.0), x_4, y_4);
473 #define XCOORD(i) numbers[(2*i)]
474 #define YCOORD(i) numbers[(2*i)+1]
476 vdmspline(int numpoints, int o_x, int o_y, int *numbers)
478 double cx_1, cy_1, cx_2, cy_2, cx_3, cy_3, cx_4, cy_4;
479 double x_1, y_1, x_2, y_2;
480 char xcoord[4],ycoord[4];
490 x_2 = o_x + XCOORD(0);
491 y_2 = o_y + YCOORD(0);
492 cx_1 = (x_1 + x_2)/2.0;
493 cy_1 = (y_1 + y_2)/2.0;
494 cx_2 = (x_1 + 3.0*x_2)/4.0;
495 cy_2 = (y_1 + 3.0*y_2)/4.0;
497 /* fprintf(stderr,"Spline %d (%d,%d)\n",numpoints,(int)x_1,(int)y_1);*/
498 vdmprintf("1%s%s",vdmnum((int)x_1,xcoord),vdmnum((int)y_1,ycoord));
499 splinerel(x_1,y_1,-1);
500 splinerel(cx_1,cy_1,0);
501 /* fprintf(tfp, "PA%.4f,%.4f;PD%.4f,%.4f;\n",
502 x_1, y_1, cx_1, cy_1);*/
504 /*for (p = p->next; p != NULL; p = p->next) {*/
505 for (i = 1; i < (numpoints); i++) {
510 x_2 = x_1 + XCOORD(i);
511 y_2 = y_1 + YCOORD(i);
512 cx_3 = (3.0*x_1 + x_2)/4.0;
513 cy_3 = (3.0*y_1 + y_2)/4.0;
514 cx_4 = (x_1 + x_2)/2.0;
515 cy_4 = (y_1 + y_2)/2.0;
516 /* fprintf(stderr,"Point (%d,%d) - (%d,%d)\n",(int)x_1,(int)(y_1),(int)x_2,(int)y_2);*/
517 quadratic_spline(cx_1, cy_1, cx_2, cy_2, cx_3, cy_3, cx_4, cy_4);
520 cx_2 = (x_1 + 3.0*x_2)/4.0;
521 cy_2 = (y_1 + 3.0*y_2)/4.0;
525 /* p = s->points->next;
528 x_2 = o_x + XCOORD(0);
529 y_2 = o_y + YCOORD(0);
530 cx_3 = (3.0*x_1 + x_2)/4.0;
531 cy_3 = (3.0*y_1 + y_2)/4.0;
532 cx_4 = (x_1 + x_2)/2.0;
533 cy_4 = (y_1 + y_2)/2.0;
534 splinerel(x_1, y_1, 0);
535 splinerel(x_1, y_1, 1);
536 /*vdmprintf("%s%s",vdmnum((int)(x_1-lx),xcoord),\
537 vdmnum((int)(y_1-ly),ycoord));*/
539 /* fprintf(tfp, "PA%.4f,%.4f;PU;\n", x_1, y_1);*/