backend_hpgl.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdarg.h>
00003 #include <string.h>
00004 #include <math.h>
00005 
00006 #include "potracelib.h"
00007 #include "curve.h"
00008 #include "main.h"
00009 #include "backend_hpgl.h"
00010 #include "lists.h"
00011 #include "auxiliary.h"
00012 
00013 #ifdef HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 /* ---------------------------------------------------------------------- */
00018 /* path-drawing auxiliary functions */
00019 
00020 /* coordinate quantization */
00021 static inline point_t unit(dpoint_t p) {
00022   point_t q;
00023 
00024   q.x = (long)(floor(p.x*info.unit+.5));
00025   q.y = (long)(floor(p.y*info.unit+.5));
00026   return q;
00027 }
00028 
00029 static point_t cur;
00030 static char lastop = 0;
00031 static int column = 0;
00032 static int newline = 1;
00033 
00034 static void shiptoken(FILE *fout, char *token) {
00035   int c = strlen(token);
00036   if (!newline && column+c+1 > 75) {
00037     //    fprintf(fout, "\n");
00038     column = 0;
00039     newline = 1;
00040   } else if (!newline) {
00041     //    fprintf(fout, " ");  // seperator between commands
00042     column++;
00043   }
00044   fprintf(fout, "%s", token);
00045   column += c;
00046   newline = 0;
00047 }
00048 
00049 static void ship(FILE *fout, char *fmt, ...) {
00050   va_list args;
00051   static char buf[4096]; /* static string limit is okay here because
00052                             we only use constant format strings - for
00053                             the same reason, it is okay to use
00054                             vsprintf instead of vsnprintf below. */
00055   char *p, *q;
00056 
00057   va_start(args, fmt);
00058   vsprintf(buf, fmt, args);
00059   buf[4095] = 0;
00060   va_end(args);
00061 
00062   p = buf;
00063   while ((q = strchr(p, ' ')) != NULL) {
00064     *q = 0;
00065     shiptoken(fout, p);
00066     p = q+1;
00067   }
00068   shiptoken(fout, p);
00069 }
00070 
00071 static void hpgl_moveto(FILE *fout, dpoint_t p) {
00072   cur = unit(p);
00073 
00074   ship(fout, "PU;PA%ld,%ld;", cur.x, cur.y);
00075   lastop = 'M';
00076 }
00077 
00078 static void hpgl_rmoveto(FILE *fout, dpoint_t p) {
00079   point_t q;
00080 
00081   q = unit(p);
00082   ship(fout, "PU;PR%ld,%ld;", q.x-cur.x, q.y-cur.y);
00083   cur = q;
00084   lastop = 'm';
00085 }
00086 
00087 static void hpgl_lineto(FILE *fout, dpoint_t p) {
00088   point_t q;
00089 
00090   q = unit(p);
00091 
00092   if (lastop != 'l') {
00093     ship(fout, "PR;PD%ld,%ld", q.x-cur.x, q.y-cur.y);
00094   } else { 
00095     ship(fout, ",%ld,%ld", q.x-cur.x, q.y-cur.y);
00096   }
00097   cur = q;
00098   lastop = 'l';
00099 }
00100 
00101 
00102 static int hpgl_draw_path(FILE *fout, point_t *pt, int n, int abs, int subpoly) {
00103   int i;
00104   point_t cur, prev;
00105 
00106   if (subpoly == 1) {
00107     ship(fout, ";PM1;");
00108 /*     ship(fout, ";"); */
00109   }
00110   
00111   if (abs) {
00112     cur = prev = pt[n-1];
00113     hpgl_moveto(fout, dpoint(cur));
00114     for (i=0; i<n; i++) {
00115       if (pt[i].x != cur.x && pt[i].y != cur.y) {
00116         cur = prev;
00117         hpgl_lineto(fout, dpoint(cur));
00118       }
00119       prev = pt[i];
00120     }
00121     hpgl_lineto(fout, dpoint(pt[n-1]));
00122   } else {
00123     cur = prev = pt[0];
00124     hpgl_rmoveto(fout, dpoint(cur));
00125     for (i=n-1; i>=0; i--) {
00126       if (pt[i].x != cur.x && pt[i].y != cur.y) {
00127         cur = prev;
00128         hpgl_lineto(fout, dpoint(cur));
00129       }
00130       prev = pt[i];
00131     }
00132     hpgl_lineto(fout, dpoint(pt[0]));
00133   }
00134   newline = 1;
00135 /*   shiptoken(fout, "z"); */
00136   return 0;
00137 }
00138 
00139 static void write_paths_transparent(FILE *fout, potrace_path_t *tree) {
00140   potrace_path_t *p, *q;
00141   int c;
00142 
00143   for (p=tree; p; p=p->sibling) {
00144     c = fprintf(fout, "PM0;");
00145     column = c;
00146     newline = 1;
00147     lastop = 0;
00148 
00149     hpgl_draw_path(fout, p->priv->pt, p->priv->len, 1, 0);
00150     
00151     for (q=p->childlist; q; q=q->sibling) {
00152 
00153       hpgl_draw_path(fout, q->priv->pt, q->priv->len, 0, 1);
00154       
00155     }
00156     fprintf(fout, ";PM2;FP;EP;\n");
00157 
00158     for (q=p->childlist; q; q=q->sibling) {
00159       write_paths_transparent(fout, q->childlist);
00160     }
00161   }
00162 }
00163 
00164 static void write_paths_opaque(FILE *fout, potrace_path_t *tree) {
00165   return write_paths_transparent(fout, tree);
00166 }
00167 
00168 /* ---------------------------------------------------------------------- */
00169 /* Backend. */
00170 
00171 /* public interface for HPGL */
00172 int page_hpgl(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) {
00173 
00174 /*   int bboxx = (int)ceil(imginfo->trans.bb[0]+imginfo->lmar+imginfo->rmar); */
00175 /*   int bboxy = (int)ceil(imginfo->trans.bb[1]+imginfo->tmar+imginfo->bmar); */
00176 /*   double origx = imginfo->trans.orig[0] + imginfo->lmar; */
00177 /*   double origy = bboxy - imginfo->trans.orig[1] - imginfo->bmar; */
00178 /*   double scalex = imginfo->width / imginfo->pixwidth / info.unit; */
00179 /*   double scaley = -imginfo->height / imginfo->pixheight / info.unit; */
00180 
00181   /* header */
00182 /*   fprintf(fout, "<?xml version=\"1.0\" standalone=\"no\"?>\n"); */
00183 /*   fprintf(fout, "<!DOCTYPE hpgl PUBLIC \"-//W3C//DTD HPGL 20010904//EN\"\n"); */
00184 /*   fprintf(fout, " \"http://www.w3.org/TR/2001/REC-HPGL-20010904/DTD/hpgl10.dtd\">\n"); */
00185 
00186   /* set bounding box and namespace */
00187 /*   fprintf(fout, "<hpgl version=\"1.0\" xmlns=\"http://www.w3.org/2000/hpgl\"\n"); */
00188 /*   fprintf(fout, " width=\"%dpt\" height=\"%dpt\" viewBox=\"0 0 %d %d\"\n",  */
00189 /*        bboxx, bboxy, bboxx, bboxy); */
00190 /*   fprintf(fout, " preserveAspectRatio=\"xMidYMid meet\">\n"); */
00191 
00192 /*   /\* metadata: creator *\/ */
00193 /*   fprintf(fout, "<metadata>\n"); */
00194 /*   fprintf(fout, "Created by "POTRACE" "VERSION", written by Peter Selinger 2001-2007\n"); */
00195 /*   fprintf(fout, "</metadata>\n"); */
00196 
00197 /*   /\* use a "group" tag to establish coordinate system and style *\/ */
00198 /*   fprintf(fout, "<g transform=\""); */
00199 /*   if (origx != 0 || origy != 0) { */
00200 /*     fprintf(fout, "translate(%.0f,%.0f) ", origx, origy); */
00201 /*   } */
00202 /*   if (info.angle != 0) { */
00203 /*     fprintf(fout, "rotate(%.2f) ", -info.angle); */
00204 /*   } */
00205 /*   fprintf(fout, "scale(%f,%f)", scalex, scaley); */
00206 /*   fprintf(fout, "\"\n"); */
00207 /*   fprintf(fout, "fill=\"#%06x\" stroke=\"none\">\n", info.color); */
00208 
00209 
00210   fprintf(fout, "IN;\n"); 
00211 
00212   if (info.opaque) {
00213     write_paths_opaque(fout, plist);
00214   } else {
00215     write_paths_transparent(fout, plist);
00216   }
00217 
00218   /* write footer */
00219 /*   fprintf(fout, "</g>\n"); */
00220 /*   fprintf(fout, "</hpgl>\n"); */
00221   fflush(fout);
00222 
00223   return 0;
00224 }
00225 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


portrait_painter
Author(s): Niklas Meinzer, Ina Baumgarten
autogenerated on Wed Dec 26 2012 16:00:42