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