bezctx_ps.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. ppedit - A pattern plate editor for Spiro splines.
  3. Copyright (C) 2007 Raph Levien
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. #include <stdio.h>
  18. #include "zmisc.h"
  19. #include "bezctx.h"
  20. #include "bezctx_ps.h"
  21. typedef struct {
  22. bezctx base;
  23. int is_open;
  24. double x, y;
  25. FILE *f;
  26. } bezctx_ps;
  27. const char *ps_prolog = "%!PS\n"
  28. "/m { moveto } bind def\n"
  29. "/l { lineto } bind def\n"
  30. "/c { curveto } bind def\n"
  31. "/z { closepath } bind def\n"
  32. "1 -1 scale\n"
  33. "0 -792 translate\n";
  34. const char *ps_postlog = "stroke\n"
  35. "showpage\n";
  36. static void
  37. bezctx_ps_moveto(bezctx *z, double x, double y, int is_open) {
  38. bezctx_ps *bc = (bezctx_ps *)z;
  39. if (!bc->is_open) fprintf(bc->f, "z\n");
  40. fprintf(bc->f, "%g %g m\n", x, y);
  41. bc->is_open = is_open;
  42. bc->x = x;
  43. bc->y = y;
  44. }
  45. void
  46. bezctx_ps_lineto(bezctx *z, double x, double y) {
  47. bezctx_ps *bc = (bezctx_ps *)z;
  48. fprintf(bc->f, "%g %g l\n", x, y);
  49. bc->x = x;
  50. bc->y = y;
  51. }
  52. void
  53. bezctx_ps_quadto(bezctx *z, double xm, double ym, double x3, double y3)
  54. {
  55. bezctx_ps *bc = (bezctx_ps *)z;
  56. double x0, y0;
  57. double x1, y1;
  58. double x2, y2;
  59. x0 = bc->x;
  60. y0 = bc->y;
  61. x1 = xm + (1./3) * (x0 - xm);
  62. y1 = ym + (1./3) * (y0 - ym);
  63. x2 = xm + (1./3) * (x3 - xm);
  64. y2 = ym + (1./3) * (y3 - ym);
  65. fprintf(bc->f, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3);
  66. bc->x = x3;
  67. bc->y = y3;
  68. }
  69. void
  70. bezctx_ps_curveto(bezctx *z, double x1, double y1, double x2, double y2,
  71. double x3, double y3)
  72. {
  73. bezctx_ps *bc = (bezctx_ps *)z;
  74. fprintf(bc->f, "%g %g %g %g %g %g c\n", x1, y1, x2, y2, x3, y3);
  75. bc->x = x3;
  76. bc->y = y3;
  77. }
  78. bezctx *
  79. new_bezctx_ps(FILE *f) {
  80. bezctx_ps *result = znew(bezctx_ps, 1);
  81. result->base.moveto = bezctx_ps_moveto;
  82. result->base.lineto = bezctx_ps_lineto;
  83. result->base.quadto = bezctx_ps_quadto;
  84. result->base.curveto = bezctx_ps_curveto;
  85. result->base.mark_knot = NULL;
  86. result->is_open = 1;
  87. result->f = f;
  88. return &result->base;
  89. }
  90. void
  91. bezctx_ps_close(bezctx *z)
  92. {
  93. bezctx_ps *bc = (bezctx_ps *)z;
  94. if (!bc->is_open) fprintf(bc->f, "z\n");
  95. zfree(bc);
  96. }