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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/** \file
* \brief Simulation Base Driver
*
* See Copyright Notice in cd.h
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <memory.h>
#include "cd.h"
#include "cd_private.h"
#include "sim.h"
static unsigned char SimHatchBits[6][8] = { /* [style][y] (8x8) */
{0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00}, /* CD_HORIZONTAL */
{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}, /* CD_VERTICAL */
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}, /* CD_BDIAGONAL */
{0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}, /* CD_FDIAGONAL */
{0x10, 0x10, 0x10, 0xFF, 0x10, 0x10, 0x10, 0x10}, /* CD_CROSS */
{0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81}};/* CD_DIAGCROSS */
#define CalcYPat(y, h) (canvas->invert_yaxis? h-1-(y%h): y%h)
#define CalcYHatch(y, h) (canvas->invert_yaxis? h-(y&h): y&h)
void simFillDrawAAPixel(cdCanvas *canvas, int x, int y, unsigned short alpha_weigth)
{
unsigned char aa_alpha;
long color, aa_color;
switch(canvas->interior_style)
{
default: /* CD_SOLID */
{
color = canvas->foreground;
break;
}
case CD_PATTERN:
{
long *pattern = canvas->pattern;
int yp = CalcYPat(y, canvas->pattern_h);
int xp = x % canvas->pattern_w;
color = pattern[canvas->pattern_w*yp + xp];
break;
}
case CD_HATCH:
{
unsigned char hatch = SimHatchBits[canvas->hatch_style][CalcYHatch(y, 7)];
unsigned char n = (unsigned char)(x&7);
simRotateHatchN(hatch, n);
if (hatch & 0x80)
color = canvas->foreground;
else if (canvas->back_opacity == CD_OPAQUE)
color = canvas->background;
else
return;
break;
}
case CD_STIPPLE:
{
unsigned char *stipple = canvas->stipple;
int yp = CalcYPat(y, canvas->stipple_h);
int xp = x % canvas->stipple_w;
if(stipple[canvas->stipple_w*yp + xp])
color = canvas->foreground;
else if (canvas->back_opacity == CD_OPAQUE)
color = canvas->background;
else
return;
break;
}
}
aa_alpha = (unsigned char)((alpha_weigth * cdAlpha(color)) / 255);
aa_color = cdEncodeAlpha(color, aa_alpha);
canvas->cxPixel(canvas->ctxcanvas, x, y, aa_color);
}
void simFillHorizLine(cdSimulation* simulation, int xmin, int y, int xmax)
{
cdCanvas* canvas = simulation->canvas;
if(xmin > xmax)
_cdSwapInt(xmin, xmax);
switch(canvas->interior_style)
{
case CD_SOLID:
simulation->SolidLine(canvas, xmin,y,xmax, canvas->foreground);
break;
case CD_PATTERN:
simulation->PatternLine(canvas, xmin,xmax,y,canvas->pattern_w,
canvas->pattern +
canvas->pattern_w*CalcYPat(y, canvas->pattern_h));
break;
case CD_HATCH:
simulation->HatchLine(canvas, xmin,xmax,y,SimHatchBits[canvas->hatch_style][CalcYHatch(y, 7)]);
break;
case CD_STIPPLE:
simulation->StippleLine(canvas, xmin, xmax, y,
canvas->stipple_w,
canvas->stipple +
canvas->stipple_w*CalcYPat(y, canvas->stipple_h));
}
}
void simFillHorizBox(cdSimulation* simulation, int xmin, int xmax, int ymin, int ymax)
{
int y;
for(y=ymin;y<=ymax;y++)
simFillHorizLine(simulation, xmin, y, xmax);
}
static void simSolidLine(cdCanvas* canvas, int xmin, int y, int xmax, long color)
{
int x;
for (x = xmin; x <= xmax; x++)
{
canvas->cxPixel(canvas->ctxcanvas, x,y,color);
}
}
static void simPatternLine(cdCanvas* canvas, int xmin, int xmax, int y, int pw, const long *pattern)
{
cdSimulation* simulation = canvas->simulation;
int x,i;
int xb;
long curColor;
i = xmin % pw;
for (x = xmin; x <= xmax;)
{
if (i == pw)
i = 0;
curColor=pattern[i];
xb=x;
while(pattern[i]==curColor && (x <= xmax))
{
i++;
if (i == pw)
i = 0;
x++;
}
if(xb==x-1)
canvas->cxPixel(canvas->ctxcanvas, xb,y,curColor);
else
simulation->SolidLine(canvas, xb,y,x-1, curColor);
}
}
static void simStippleLine(cdCanvas* canvas, int xmin, int xmax, int y, int pw, const unsigned char *stipple)
{
cdSimulation* simulation = canvas->simulation;
int x,xb,i;
long fgColor,bgColor;
int opacity;
int curCase;
fgColor = canvas->foreground;
opacity=canvas->back_opacity;
if(opacity==CD_OPAQUE)
{
bgColor=canvas->background;
for (x = xmin, i=xmin%pw ; x <= xmax;)
{
if(i==pw)
i=0;
xb=x;
curCase=stipple[i];
while (stipple[i]==curCase && (x<=xmax))
{
x++;
i++;
if(i==pw)
i=0;
}
if (curCase)
{
if(xb==x-1)
canvas->cxPixel(canvas->ctxcanvas, xb,y,fgColor);
else
simulation->SolidLine(canvas, xb,y,x-1,fgColor);
}
}
for (x = xmin, i=xmin%pw ; x <= xmax;)
{
if(i==pw)
i=0;
xb=x;
curCase=stipple[i];
while (stipple[i]==curCase && (x<=xmax))
{
x++;
i++;
if(i==pw)
i=0;
}
if (!curCase)
{
if(xb==x-1)
canvas->cxPixel(canvas->ctxcanvas, xb,y,bgColor);
else
simulation->SolidLine(canvas, xb,y,x-1,bgColor);
}
}
}
else
{
for (x = xmin,i=xmin%pw; x <= xmax;)
{
xb=x;
curCase=stipple[i];
while (stipple[i]==curCase && (x<=xmax))
{
i++;
x++;
if(i==pw)
i=0;
}
if(curCase)
{
if(xb==x-1)
canvas->cxPixel(canvas->ctxcanvas, xb,y,fgColor);
else
simulation->SolidLine(canvas, xb,y,x-1,fgColor);
}
}
}
}
static void simHatchLine(cdCanvas* canvas, int xmin, int xmax, int y, unsigned char hatch)
{
cdSimulation* simulation = canvas->simulation;
int x,xb;
int opacity, mask;
unsigned char startp, n;
long curColor, fgColor, bgColor;
n = (unsigned char)(xmin&7);
simRotateHatchN(hatch,n);
fgColor=canvas->foreground;
opacity=canvas->back_opacity;
if(opacity==CD_OPAQUE)
{
bgColor=canvas->background;
for (x = xmin; x <= xmax; x++)
{
curColor=(hatch&0x80)?fgColor:bgColor;
xb=x;
startp = hatch&0x80? 1: 0;
_cdRotateHatch(hatch);
while (startp == (hatch&0x80? 1: 0) && x <= xmax)
{
x++;
_cdRotateHatch(hatch);
}
if(xb==x)
canvas->cxPixel(canvas->ctxcanvas, xb,y,curColor);
else
simulation->SolidLine(canvas, xb,y,x, curColor);
}
}
else
{
for (x = xmin; x <= xmax; x++)
{
mask=(hatch&0x80)?1:0;
xb=x;
startp = hatch&0x80? 1: 0;
_cdRotateHatch(hatch);
while (startp == (hatch&0x80? 1: 0) && x <= xmax)
{
x++;
_cdRotateHatch(hatch);
}
if(mask)
{
if(xb==x)
canvas->cxPixel(canvas->ctxcanvas, xb,y,fgColor);
else
simulation->SolidLine(canvas, xb,y,x,fgColor);
}
}
}
}
cdSimulation* cdCreateSimulation(cdCanvas* canvas)
{
cdSimulation* simulation = (cdSimulation*)malloc(sizeof(cdSimulation));
memset(simulation, 0, sizeof(cdSimulation));
simulation->canvas = canvas;
simulation->SolidLine = simSolidLine;
simulation->PatternLine = simPatternLine;
simulation->StippleLine = simStippleLine;
simulation->HatchLine = simHatchLine;
return simulation;
}
void cdKillSimulation(cdSimulation* simulation)
{
memset(simulation, 0, sizeof(cdSimulation));
free(simulation);
}
|