| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
#include "zmisc.h" |
| 24 |
#include "bezctx.h" |
| 25 |
#include "bezctx_ps.h" |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
bezctx base; |
| 29 |
int is_open; |
| 30 |
double x, y; |
| 31 |
FILE *f; |
| 32 |
} bezctx_ps; |
| 33 |
|
| 34 |
const char *ps_prolog = "%!PS\n" |
| 35 |
"/m { moveto } bind def\n" |
| 36 |
"/l { lineto } bind def\n" |
| 37 |
"/c { curveto } bind def\n" |
| 38 |
"/z { closepath } bind def\n" |
| 39 |
"1 -1 scale\n" |
| 40 |
"0 -792 translate\n"; |
| 41 |
|
| 42 |
const char *ps_postlog = "stroke\n" |
| 43 |
"showpage\n"; |
| 44 |
|
| 45 |
static void |
| 46 |
bezctx_ps_moveto(bezctx *z, double x, double y, int is_open) { |
| 47 |
bezctx_ps *bc = (bezctx_ps *)z; |
| 48 |
|
| 49 |
if (!bc->is_open) fprintf(bc->f, "z\n"); |
| 50 |
fprintf(bc->f, "%g %g m\n", x, y); |
| 51 |
bc->is_open = is_open; |
| 52 |
bc->x = x; |
| 53 |
bc->y = y; |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
bezctx_ps_lineto(bezctx *z, double x, double y) { |
| 58 |
bezctx_ps *bc = (bezctx_ps *)z; |
| 59 |
|
| 60 |
fprintf(bc->f, "%g %g l\n", x, y); |
| 61 |
bc->x = x; |
| 62 |
bc->y = y; |
| 63 |
} |
| 64 |
|
| 65 |
void |
| 66 |
bezctx_ps_quadto(bezctx *z, double xm, double ym, double x3, double y3) |
| 67 |
{ |
| 68 |
bezctx_ps *bc = (bezctx_ps *)z; |
| 69 |
double x0, y0; |
| 70 |
double x1, y1; |
| 71 |
double x2, y2; |
| 72 |
|
| 73 |
x0 = bc->x; |
| 74 |
y0 = bc->y; |
| 75 |
x1 = xm + (1./3) * (x0 - xm); |
| 76 |
y1 = ym + (1./3) * (y0 - ym); |
| 77 |
x2 = xm + (1./3) * (x3 - xm); |
| 78 |
y2 = ym + (1./3) * (y3 - ym); |
| 79 |
fprintf(bc->f, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3); |
| 80 |
bc->x = x3; |
| 81 |
bc->y = y3; |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
bezctx_ps_curveto(bezctx *z, double x1, double y1, double x2, double y2, |
| 86 |
double x3, double y3) |
| 87 |
{ |
| 88 |
bezctx_ps *bc = (bezctx_ps *)z; |
| 89 |
|
| 90 |
fprintf(bc->f, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3); |
| 91 |
bc->x = x3; |
| 92 |
bc->y = y3; |
| 93 |
} |
| 94 |
|
| 95 |
bezctx * |
| 96 |
new_bezctx_ps(FILE *f) { |
| 97 |
bezctx_ps *result = znew(bezctx_ps, 1); |
| 98 |
|
| 99 |
result->base.moveto = bezctx_ps_moveto; |
| 100 |
result->base.lineto = bezctx_ps_lineto; |
| 101 |
result->base.quadto = bezctx_ps_quadto; |
| 102 |
result->base.curveto = bezctx_ps_curveto; |
| 103 |
result->base.mark_knot = NULL; |
| 104 |
result->is_open = 1; |
| 105 |
result->f = f; |
| 106 |
return &result->base; |
| 107 |
} |
| 108 |
|
| 109 |
void |
| 110 |
bezctx_ps_close(bezctx *z) |
| 111 |
{ |
| 112 |
bezctx_ps *bc = (bezctx_ps *)z; |
| 113 |
|
| 114 |
if (!bc->is_open) fprintf(bc->f, "z\n"); |
| 115 |
zfree(bc); |
| 116 |
} |