eps.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <math.h>
  2. #include <string.h>
  3. #include <grass/glocale.h>
  4. #include "local_proto.h"
  5. /* test if file is realy EPS file and find bbox
  6. * returns 1 if OK
  7. * 0 on error
  8. */
  9. int eps_bbox(char *eps, double *llx, double *lly, double *urx, double *ury)
  10. {
  11. char buf[201];
  12. FILE *fp;
  13. int v1, v2, v3, v4;
  14. /* test if file is realy eps and find bbox */
  15. if ((fp = fopen(eps, "r")) == NULL) {
  16. G_warning(_("Can't open eps file <%s>"), eps);
  17. return (0);
  18. }
  19. /* test if first row contains '%!PS-Adobe-m.n EPSF-m.n' string */
  20. fgets(buf, 200, fp);
  21. if (sscanf(buf, "%%!PS-Adobe-%d.%d EPSF-%d.%d", &v1, &v2, &v3, &v4) < 4) {
  22. fprintf(stderr, "file <%s> is not in EPS format\n", eps);
  23. fclose(fp);
  24. return (0);
  25. }
  26. /* looking for bbox */
  27. while (fgets(buf, 200, fp) != NULL) {
  28. if (sscanf
  29. (buf, "%%%%BoundingBox: %lf %lf %lf %lf", llx, lly, urx,
  30. ury) == 4) {
  31. fclose(fp);
  32. return (1);
  33. }
  34. }
  35. G_warning(_("Bounding box in eps file <%s> was not found"), eps);
  36. fclose(fp);
  37. return (0);
  38. }
  39. /* calculate translation for EPS file
  40. * rotate is in degrees
  41. */
  42. int eps_trans(double llx, double lly, double urx, double ury,
  43. double x, double y, double scale, double rotate,
  44. double *xt, double *yt)
  45. {
  46. double xc, yc, angle;
  47. xc = (llx + urx) / 2;
  48. yc = (lly + ury) / 2;
  49. angle = M_PI * rotate / 180;
  50. *xt = x + scale * (yc * sin(angle) - xc * cos(angle));
  51. *yt = y - scale * (yc * cos(angle) + xc * sin(angle));
  52. return (1);
  53. }
  54. /* save EPS file into PS file for later use */
  55. int eps_save(FILE * fp, char *epsf, char *name)
  56. {
  57. char buf[1024];
  58. FILE *epsfp;
  59. if ((epsfp = fopen(epsf, "r")) == NULL)
  60. return (0);
  61. fprintf(fp, "\n/%s {\n", name);
  62. while (fgets(buf, 1024, epsfp) != NULL)
  63. fprintf(fp, "%s", buf);
  64. fprintf(fp, "} def\n");
  65. fclose(epsfp);
  66. return (1);
  67. }
  68. /* draw EPS file saved by eps_save */
  69. int eps_draw_saved(char *name, double x, double y, double scale,
  70. double rotate)
  71. {
  72. fprintf(PS.fp, "\nBeginEPSF\n");
  73. fprintf(PS.fp, "%.5f %.5f translate\n", x, y);
  74. fprintf(PS.fp, "%.5f rotate\n", rotate);
  75. fprintf(PS.fp, "%.5f %.5f scale\n", scale, scale);
  76. fprintf(PS.fp, "%%BeginDocument: %s\n", name);
  77. fprintf(PS.fp, "%s\n", name);
  78. fprintf(PS.fp, "%%EndDocument\n");
  79. fprintf(PS.fp, "EndEPSF\n");
  80. return (1);
  81. }
  82. /* write EPS file into PS file */
  83. int eps_draw(FILE * fp, char *eps, double x, double y, double scale,
  84. double rotate)
  85. {
  86. char buf[1024];
  87. FILE *epsfp;
  88. if ((epsfp = fopen(eps, "r")) == NULL)
  89. return (0);
  90. fprintf(PS.fp, "\nBeginEPSF\n");
  91. fprintf(PS.fp, "%.5f %.5f translate\n", x, y);
  92. fprintf(PS.fp, "%.5f rotate\n", rotate);
  93. fprintf(PS.fp, "%.5f %.5f scale\n", scale, scale);
  94. fprintf(PS.fp, "%%BeginDocument: %s\n", eps);
  95. while (fgets(buf, 1024, epsfp) != NULL)
  96. fprintf(fp, "%s", buf);
  97. fprintf(PS.fp, "%%EndDocument\n");
  98. fprintf(PS.fp, "EndEPSF\n");
  99. fclose(epsfp);
  100. return (1);
  101. }
  102. /* save EPS patter file into PS file for later use */
  103. /* For pattern we have to remove header comments */
  104. int pat_save(FILE * fp, char *epsf, char *name)
  105. {
  106. char buf[1024];
  107. FILE *epsfp;
  108. if ((epsfp = fopen(epsf, "r")) == NULL)
  109. return (0);
  110. fprintf(fp, "\n/%s {\n", name);
  111. while (fgets(buf, 1024, epsfp) != NULL) {
  112. if (strncmp(buf, "%!PS-Adobe", 10) == 0 ||
  113. strncmp(buf, "%%BoundingBox", 13) == 0)
  114. continue;
  115. fprintf(fp, "%s", buf);
  116. }
  117. fprintf(fp, "} def\n");
  118. fclose(epsfp);
  119. return (1);
  120. }