text2.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include "driver.h"
  4. #include "driverlib.h"
  5. struct rectangle
  6. {
  7. double t, b, l, r;
  8. };
  9. static void remember(struct rectangle *box, double x, double y)
  10. {
  11. if (x > box->r)
  12. box->r = x;
  13. if (x < box->l)
  14. box->l = x;
  15. if (y > box->b)
  16. box->b = y;
  17. if (y < box->t)
  18. box->t = y;
  19. }
  20. static void transform(double *x, double *y,
  21. int ix, int iy,
  22. double orig_x, double orig_y)
  23. {
  24. double ax = text_size_x * ix / 25;
  25. double ay = text_size_y * iy / 25;
  26. double rx = ax * text_cosrot - ay * text_sinrot;
  27. double ry = ax * text_sinrot + ay * text_cosrot;
  28. *x = orig_x + rx;
  29. *y = orig_y - ry;
  30. }
  31. static void draw_char(double *px, double *py, unsigned char character, struct rectangle *box)
  32. {
  33. unsigned char *X;
  34. unsigned char *Y;
  35. int n_vects;
  36. int i;
  37. void (*func)(double, double);
  38. get_char_vects(character, &n_vects, &X, &Y);
  39. if (!box)
  40. COM_Begin();
  41. func = COM_Move;
  42. for (i = 1; i < n_vects; i++) {
  43. int ix, iy;
  44. double x, y;
  45. if (X[i] == ' ') {
  46. func = COM_Move;
  47. continue;
  48. }
  49. ix = 10 + X[i] - 'R';
  50. iy = 10 - Y[i] + 'R';
  51. transform(&x, &y, ix, iy, *px, *py);
  52. if (box)
  53. remember(box, x, y);
  54. else {
  55. (*func)(x, y);
  56. func = COM_Cont;
  57. }
  58. }
  59. transform(px, py, 20, 0, *px, *py);
  60. if (box)
  61. remember(box, *px, *py);
  62. else
  63. COM_Stroke();
  64. }
  65. static void draw_text(const char *string, struct rectangle *box)
  66. {
  67. double base_x = cur_x;
  68. double base_y = cur_y;
  69. const unsigned char *p;
  70. for (p = (const unsigned char *) string; *p; p++)
  71. draw_char(&base_x, &base_y, *p, box);
  72. }
  73. void get_text_ext(const char *string, double *top, double *bot, double *left, double *rite)
  74. {
  75. struct rectangle box;
  76. box.t = 1e300;
  77. box.b = -1e300;
  78. box.l = 1e300;
  79. box.r = -1e300;
  80. draw_text(string, &box);
  81. *top = box.t;
  82. *bot = box.b;
  83. *left = box.l;
  84. *rite = box.r;
  85. }
  86. void soft_text(const char *string)
  87. {
  88. draw_text(string, NULL);
  89. }