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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
#include <stdio.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/colors.h>
#include <grass/raster.h>
#include <grass/display.h>
#include <grass/symbol.h>
#include <grass/glocale.h>
#include "options.h"
#include "local_proto.h"
#define CHUNK 128
static int coors_allocated = 0;
static double *xarray;
static double *yarray;
static float xincr;
static float yincr;
static double rotation; /* degrees counter-clockwise from east */
static RGBA_Color last_color;
static double t, b, l, r;
static double cur_x, cur_y;
int set_graph_stuff(void)
{
D_get_dst(&t, &b, &l, &r);
if (mapunits) {
xincr = (r - l) / 100.;
if (xincr < 0.0)
xincr = -xincr; /* mod: shapiro 13 jun 1991 */
yincr = (b - t) / 100.;
if (yincr < 0.0)
yincr = -yincr; /* mod: shapiro 13 jun 1991 */
}
else
xincr = yincr = 1;
rotation = 0.0; /* init */
return 0;
}
int set_text_size(void)
{
if (hsize >= 0. && vsize >= 0. && hsize <= 100. && vsize <= 100.) {
D_text_size(hsize * xincr, vsize * yincr);
G_debug(3, "text size initialized to [%.1f,%.1f]", hsize * xincr,
vsize * yincr);
}
return (0);
}
int do_draw(const char *str)
{
float xper, yper;
if (2 != sscanf(str, "%*s %f %f", &xper, &yper)) {
G_warning(_("Problem parsing coordinates [%s]"), str);
return (-1);
}
D_line_abs(cur_x, cur_y, xper, yper);
cur_x = xper;
cur_y = yper;
return (0);
}
int do_move(const char *str)
{
float xper, yper;
if (2 != sscanf(str, "%*s %f %f", &xper, &yper)) {
G_warning(_("Problem parsing coordinates [%s]"), str);
return (-1);
}
D_pos_abs(xper, yper);
cur_x = xper;
cur_y = yper;
return (0);
}
int do_color(const char *str)
{
char in_color[64];
int R, G, B, color = 0;
if (1 != sscanf(str, "%*s %s", in_color)) {
G_warning(_("Unable to read color"));
return (-1);
}
/* Parse and select color */
color = G_str_to_color(in_color, &R, &G, &B);
if (color == 0) {
G_warning(_("[%s]: No such color"), in_color);
/* store for backup */
last_color.a = RGBA_COLOR_NONE;
return (-1);
}
if (color == 1) {
D_RGB_color(R, G, B);
/* store for backup */
set_last_color(R, G, B, RGBA_COLOR_OPAQUE);
}
if (color == 2) { /* color == 'none' */
R = D_translate_color(DEFAULT_BG_COLOR);
D_use_color(R);
/* store for backup */
set_last_color(0, 0, 0, RGBA_COLOR_NONE);
}
return (0);
}
int do_linewidth(const char *str)
{
double width;
if (1 != sscanf(str, "%*s %lf", &width)) {
G_warning(_("Problem parsing command [%s]"), str);
return (-1);
}
D_line_width(width);
G_debug(3, "line width set to %.1f", width);
return (0);
}
int do_poly(char *buff, FILE *infile)
{
int num;
char origcmd[64];
float xper, yper;
int to_return;
sscanf(buff, "%s", origcmd);
num = 0;
for (;;) {
if ((to_return = G_getl2(buff, 128, infile)) != 1)
break;
if (2 != sscanf(buff, "%f %f", &xper, &yper)) {
if ('#' == buff[0]) {
G_debug(3, " skipping comment line [%s]", buff);
continue;
}
G_debug(3, "coordinate pair not found. ending polygon. [%s]", buff);
break;
}
check_alloc(num + 1);
xarray[num] = xper;
yarray[num] = yper;
num++;
}
if (num) {
/* this check is here so you can use the "polyline" command
to make an unfilled polygon */
if (!strcmp(origcmd, "polygon"))
D_polygon_abs(xarray, yarray, num);
else
D_polyline_abs(xarray, yarray, num);
}
return (to_return);
}
int do_size(const char *str)
{
float xper, yper;
int ret;
ret = sscanf(str, "%*s %f %f", &xper, &yper);
if (ret != 2 && ret != 1) {
G_warning(_("Problem parsing command [%s]"), str);
return (-1);
}
/* if only one size is given assume same value in both axes */
if (ret == 1)
yper = xper;
if (xper < 0. || yper < 0. || xper > 100. || yper > 100.)
return (-1);
D_text_size(xper * xincr, yper * yincr);
G_debug(3, "text size set to [%.1f,%.1f]", xper * xincr, yper * yincr);
return (0);
}
int do_rotate(const char *str)
{
if (1 != sscanf(str, "%*s %lf", &rotation)) {
G_warning(_("Problem parsing command [%s]"), str);
return (-1);
}
D_text_rotation(rotation);
G_debug(3, "rotation set to %.1f degrees", rotation);
return (0);
}
int do_text(const char *str)
{
const char *ptr = str;
/* skip to beginning of actual text */
for (; *ptr != ' '; ptr++)
;
for (; *ptr == ' '; ptr++)
;
D_text(ptr);
return 0;
}
int check_alloc(int num)
{
int to_alloc;
if (num < coors_allocated)
return 0;
to_alloc = num + CHUNK;
xarray = G_realloc(xarray, to_alloc * sizeof(double));
yarray = G_realloc(yarray, to_alloc * sizeof(double));
coors_allocated = to_alloc;
return 0;
}
int do_icon(const char *str)
{
double xper, yper;
char type;
double size;
double ix, iy;
if (4 != sscanf(str, "%*s %c %lf %lf %lf", &type, &size, &xper, &yper)) {
G_warning(_("Problem parsing command [%s]"), str);
return (-1);
}
ix = xper;
iy = yper;
size *= yincr;
D_begin();
switch (type & 0x7F) {
case 'o':
D_move_abs(ix - size, iy - size);
D_cont_abs(ix - size, iy + size);
D_cont_abs(ix + size, iy + size);
D_cont_abs(ix + size, iy - size);
D_cont_abs(ix - size, iy - size);
break;
case 'x':
D_move_abs(ix - size, iy - size);
D_cont_abs(ix + size, iy + size);
D_move_abs(ix - size, iy + size);
D_cont_abs(ix + size, iy - size);
break;
case '+':
default:
D_move_abs(ix, iy - size);
D_cont_abs(ix, iy + size);
D_move_abs(ix - size, iy);
D_cont_abs(ix + size, iy);
break;
}
D_end();
D_stroke();
return (0);
}
int do_symbol(const char *str)
{
double xper, yper;
double size;
double ix, iy;
char *symb_name;
SYMBOL *Symb;
char *line_color_str, *fill_color_str;
RGBA_Color *line_color, *fill_color;
int R, G, B, ret;
line_color = G_malloc(sizeof(RGBA_Color));
fill_color = G_malloc(sizeof(RGBA_Color));
symb_name =
G_malloc(strlen(str) + 1); /* well, it won't be any bigger than this */
line_color_str = G_malloc(strlen(str) + 1);
fill_color_str = G_malloc(strlen(str) + 1);
G_debug(3, "do_symbol() [%s]", str);
/* set default colors so colors are optional */
strcpy(line_color_str, DEFAULT_FG_COLOR);
strcpy(fill_color_str, "grey");
if (sscanf(str, "%*s %s %lf %lf %lf %s %s", symb_name, &size, &xper, &yper,
line_color_str, fill_color_str) < 4) {
G_warning(_("Problem parsing command [%s]"), str);
return (-1);
}
ix = xper;
iy = yper;
size *= yincr;
/* parse line color */
ret = G_str_to_color(line_color_str, &R, &G, &B);
line_color->r = (unsigned char)R;
line_color->g = (unsigned char)G;
line_color->b = (unsigned char)B;
if (ret == 1) {
/* here alpha is only used as an on/off switch, otherwise unused by the
* display drivers */
line_color->a = RGBA_COLOR_OPAQUE;
}
else if (ret == 2)
line_color->a = RGBA_COLOR_NONE;
else {
G_warning(_("[%s]: No such color"), line_color_str);
return (-1);
}
/* parse fill color */
ret = G_str_to_color(fill_color_str, &R, &G, &B);
fill_color->r = (unsigned char)R;
fill_color->g = (unsigned char)G;
fill_color->b = (unsigned char)B;
if (ret == 1)
fill_color->a = RGBA_COLOR_OPAQUE;
else if (ret == 2)
fill_color->a = RGBA_COLOR_NONE;
else {
G_warning(_("[%s]: No such color"), fill_color_str);
return (-1);
}
Symb = S_read(symb_name);
if (Symb == NULL) {
G_warning(_("Cannot read symbol, cannot display points"));
return (-1);
}
else
S_stroke(Symb, size, rotation, 0);
D_symbol(Symb, ix, iy, line_color, fill_color);
/* restore previous d.graph draw color */
if (last_color.a == RGBA_COLOR_OPAQUE)
D_RGB_color(last_color.r, last_color.g, last_color.b);
else if (last_color.a == RGBA_COLOR_NONE)
D_use_color(D_parse_color(DEFAULT_BG_COLOR, 0));
else /* unset or bad */
D_RGB_color(line_color->r, line_color->g, line_color->b);
G_free(symb_name);
G_free(line_color_str);
G_free(fill_color_str);
G_free(line_color);
G_free(fill_color);
return (0);
}
/* RGBA are 0-255; alpha is only used as an on/off switch. maybe test a<127<a ?
*/
void set_last_color(int R, int G, int B, int alpha)
{
if (alpha == RGBA_COLOR_OPAQUE) {
last_color.r = (unsigned char)R;
last_color.g = (unsigned char)G;
last_color.b = (unsigned char)B;
last_color.a = RGBA_COLOR_OPAQUE;
}
else if (alpha == RGBA_COLOR_NONE) {
last_color.a = RGBA_COLOR_NONE;
}
else
last_color.a = RGBA_COLOR_NONE;
}
|