GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-761b3a08c9
text2.c
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include <grass/gis.h>
4 #include "driver.h"
5 #include "driverlib.h"
6 
7 struct rectangle {
8  double t, b, l, r;
9 };
10 
11 static void remember(struct rectangle *box, double x, double y)
12 {
13  if (x > box->r)
14  box->r = x;
15  if (x < box->l)
16  box->l = x;
17  if (y > box->b)
18  box->b = y;
19  if (y < box->t)
20  box->t = y;
21 }
22 
23 static void transform(double *x, double *y, int ix, int iy, double orig_x,
24  double orig_y)
25 {
26  double ax = text_size_x * ix / 25;
27  double ay = text_size_y * iy / 25;
28  double rx = ax * text_cosrot - ay * text_sinrot;
29  double ry = ax * text_sinrot + ay * text_cosrot;
30  *x = orig_x + rx;
31  *y = orig_y - ry;
32 }
33 
34 static void draw_char(double *px, double *py, unsigned char character,
35  struct rectangle *box)
36 {
37  unsigned char *X;
38  unsigned char *Y;
39  int n_vects;
40  int i;
41  void (*func)(double, double);
42 
43  get_char_vects(character, &n_vects, &X, &Y);
44 
45  if (!box)
46  COM_Begin();
47 
48  func = COM_Move;
49 
50  for (i = 1; i < n_vects; i++) {
51  int ix, iy;
52  double x, y;
53 
54  if (X[i] == ' ') {
55  func = COM_Move;
56  continue;
57  }
58 
59  ix = 10 + X[i] - 'R';
60  iy = 10 - Y[i] + 'R';
61 
62  transform(&x, &y, ix, iy, *px, *py);
63 
64  if (box)
65  remember(box, x, y);
66  else {
67  (*func)(x, y);
68  func = COM_Cont;
69  }
70  }
71 
72  transform(px, py, 20, 0, *px, *py);
73 
74  if (box)
75  remember(box, *px, *py);
76  else
77  COM_Stroke();
78 }
79 
80 static void draw_text(const char *string, struct rectangle *box)
81 {
82  double base_x = cur_x;
83  double base_y = cur_y;
84  const unsigned char *p;
85 
86  for (p = (const unsigned char *)string; *p; p++)
87  draw_char(&base_x, &base_y, *p, box);
88 }
89 
90 void get_text_ext(const char *string, double *top, double *bot, double *left,
91  double *rite)
92 {
93  struct rectangle box;
94 
95  box.t = 1e300;
96  box.b = -1e300;
97  box.l = 1e300;
98  box.r = -1e300;
99 
100  draw_text(string, &box);
101 
102  *top = box.t;
103  *bot = box.b;
104  *left = box.l;
105  *rite = box.r;
106 }
107 
108 void soft_text(const char *string)
109 {
110  draw_text(string, NULL);
111 }
#define NULL
Definition: ccmath.h:32
void COM_Cont(double x, double y)
Definition: driver/draw.c:22
void COM_Stroke(void)
Definition: driver/draw.c:34
void COM_Move(double x, double y)
Definition: driver/draw.c:16
void COM_Begin(void)
Definition: driver/draw.c:10
double text_size_y
Definition: driver/init.c:36
double text_cosrot
Definition: driver/init.c:39
double text_size_x
Definition: driver/init.c:35
double cur_x
Definition: driver/init.c:32
double text_sinrot
Definition: driver/init.c:38
double cur_y
Definition: driver/init.c:33
int get_char_vects(unsigned char, int *, unsigned char **, unsigned char **)
Definition: font2.c:191
#define X
Definition: ogsf.h:140
#define Y
Definition: ogsf.h:141
double l
Definition: r_raster.c:39
double t
Definition: r_raster.c:39
Definition: clip.h:14
double r
Definition: text2.c:8
double left
Definition: clip.h:15
double t
Definition: text2.c:8
double bot
Definition: clip.h:15
double b
Definition: text2.c:8
double l
Definition: text2.c:8
double rite
Definition: clip.h:15
double top
Definition: clip.h:15
void get_text_ext(const char *string, double *top, double *bot, double *left, double *rite)
Definition: text2.c:90
void soft_text(const char *string)
Definition: text2.c:108
#define x