1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "matchbox-stroke.h"
void*
util_malloc0(int size)
{
void *p;
p = malloc(size);
memset(p, 0, size);
return p;
}
void
util_fatal_error(char *msg)
{
fprintf(stderr, "matchbox-stroke: *Error* %s", msg);
exit(1);
}
#define UTF8_COMPUTE(Char, Mask, Len) \
if (Char < 128) \
{ \
Len = 1; \
Mask = 0x7f; \
} \
else if ((Char & 0xe0) == 0xc0) \
{ \
Len = 2; \
Mask = 0x1f; \
} \
else if ((Char & 0xf0) == 0xe0) \
{ \
Len = 3; \
Mask = 0x0f; \
} \
else if ((Char & 0xf8) == 0xf0) \
{ \
Len = 4; \
Mask = 0x07; \
} \
else if ((Char & 0xfc) == 0xf8) \
{ \
Len = 5; \
Mask = 0x03; \
} \
else if ((Char & 0xfe) == 0xfc) \
{ \
Len = 6; \
Mask = 0x01; \
} \
else \
Len = -1;
int
util_utf8_char_cnt(const unsigned char *str)
{
const unsigned char *p = str;
int mask, len, result = 0;
/* XXX Should validate too */
while (*p != '\0')
{
UTF8_COMPUTE(*p, mask, len);
p += len;
result++;
}
return result;
}
boolean
util_file_readable(char *path)
{
struct stat st;
if (stat(path, &st))
return False;
return True;
}
void /* skip_first_last */
util_bresenham_line(int x1,
int y1,
int x2,
int y2,
UtilPlotFunc plot_func,
void *userdata)
{
int dy = y2 - y1;
int dx = x2 - x1;
int stepx, stepy;
if (dy < 0)
{
dy = -dy;
stepy = -1;
}
else stepy = 1;
if (dx < 0)
{
dx = -dx;
stepx = -1;
}
else stepx = 1;
if (dx == 0 && dy == 0)
return;
dy <<= 1;
dx <<= 1;
if (dx > dy)
{
int fraction = dy - (dx >> 1);
while (1)
{
if (fraction >= 0)
{
y1 += stepy;
fraction -= dx;
}
x1 += stepx;
fraction += dy;
if (x1 == x2)
break;
(plot_func)(x1, y1, userdata);
}
}
else
{
int fraction = dx - (dy >> 1);
while (1)
{
if (fraction >= 0)
{
x1 += stepx;
fraction -= dy;
}
y1 += stepy;
fraction += dx;
if (y1 == y2)
break;
(plot_func)(x1, y1, userdata);
}
}
}
|